From 336791b915f11056ae0f701b1cd5ba18e719c0bb Mon Sep 17 00:00:00 2001
From: Ladduro <ladduro@gmail.com>
Date: Fri, 22 Apr 2022 10:21:15 +0300
Subject: [PATCH] Keychron Q2 add Caps Lock indicator and layer RGB (#16641)

---
 .../q2/rev_0110/keymaps/ladduro/config.h      | 25 +++++
 .../q2/rev_0110/keymaps/ladduro/keymap.c      | 96 +++++++++++++++++++
 .../q2/rev_0110/keymaps/ladduro/keymap_user.h | 27 ++++++
 .../q2/rev_0110/keymaps/ladduro/readme.md     | 29 ++++++
 .../keymaps/ladduro/rgb_matrix_user.c         | 72 ++++++++++++++
 .../keymaps/ladduro/rgb_matrix_user.h         | 25 +++++
 .../q2/rev_0110/keymaps/ladduro/rules.mk      |  5 +
 7 files changed, 279 insertions(+)
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/config.h
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap.c
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap_user.h
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/readme.md
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.c
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.h
 create mode 100644 keyboards/keychron/q2/rev_0110/keymaps/ladduro/rules.mk

diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/config.h b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/config.h
new file mode 100644
index 0000000000..a8ad37d003
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/config.h
@@ -0,0 +1,25 @@
+/* Copyright 2022 @ Ionut Micu
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+/* RGB Matrix Configuration */
+#ifdef RGB_MATRIX_ENABLE
+#    define RGB_DISABLE_WHEN_USB_SUSPENDED
+#    define CAPS_LOCK_INDICATOR_COLOR RGB_RED
+#    define CAPS_LOCK_INDICATOR_LIGHT_ALPHAS
+#    define FN_LAYER_TRANSPARENT_KEYS_OFF
+#endif
\ No newline at end of file
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap.c b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap.c
new file mode 100644
index 0000000000..cc83640b39
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap.c
@@ -0,0 +1,96 @@
+/* Copyright 2022 @ Ionut Micu
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include QMK_KEYBOARD_H
+#include "keymap_user.h"
+#ifdef RGB_MATRIX_ENABLE
+#    include "rgb_matrix_user.h"
+#endif
+
+enum custom_keycodes {
+    KC_MISSION_CONTROL = SAFE_RANGE,
+    KC_LAUNCHPAD
+};
+
+#define KC_WAVE S(KC_GRV)
+#define KC_TASK LGUI(KC_TAB)
+#define KC_FLXP LGUI(KC_E)
+#define KC_MCTL KC_MISSION_CONTROL
+#define KC_LPAD KC_LAUNCHPAD
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+    [MAC_BASE] = LAYOUT_ansi_67(
+        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS,  KC_EQL,   KC_BSPC,          KC_INS,
+        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC,  KC_RBRC,  KC_BSLS,          KC_DEL,
+        KC_CAPS, KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT,            KC_ENT,           KC_HOME,
+        KC_LSFT,          KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM, KC_DOT,  KC_SLSH,            KC_RSFT, KC_UP,
+        KC_LCTL, KC_LOPT, KC_LCMD,                            KC_SPC,                             KC_RCMD, MO(_FN1), MO(_FN3), KC_LEFT, KC_DOWN, KC_RGHT),
+
+    [WIN_BASE] = LAYOUT_ansi_67(
+        KC_ESC,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_MINS,  KC_EQL,   KC_BSPC,          KC_INS,
+        KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_LBRC,  KC_RBRC,  KC_BSLS,          KC_DEL,
+        KC_CAPS, KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT,            KC_ENT,           KC_HOME,
+        KC_LSFT,          KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM, KC_DOT,  KC_SLSH,            KC_RSFT, KC_UP,
+        KC_LCTL, KC_LGUI, KC_LALT,                            KC_SPC,                             KC_RALT, MO(_FN2), MO(_FN3), KC_LEFT, KC_DOWN, KC_RGHT),
+
+    [_FN1] = LAYOUT_ansi_67(
+        KC_GRV,  KC_BRID, KC_BRIU, KC_MCTL, KC_LPAD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD,  KC_VOLU, KC_TRNS,          KC_TRNS,
+        RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS,          KC_TRNS,
+        KC_TRNS, RGB_RMOD,RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS,          KC_TRNS,
+        KC_TRNS,          KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS, KC_TRNS,
+        KC_TRNS, KC_TRNS, KC_TRNS,                            KC_TRNS,                            KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS),
+
+    [_FN2] = LAYOUT_ansi_67(
+        KC_GRV,  KC_BRID, KC_BRIU, KC_TASK, KC_FLXP, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD,  KC_VOLU, KC_TRNS,          KC_TRNS,
+        RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS,          KC_TRNS,
+        KC_TRNS, RGB_RMOD,RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS,          KC_TRNS,
+        KC_TRNS,          KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS, KC_TRNS,
+        KC_TRNS, KC_TRNS, KC_TRNS,                            KC_TRNS,                            KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS),
+
+    [_FN3] = LAYOUT_ansi_67(
+        KC_WAVE, KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5,   KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,   KC_F12,  KC_TRNS,          KC_TRNS,
+        RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS,          KC_TRNS,
+        KC_TRNS, RGB_RMOD,RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS,          KC_TRNS,
+        KC_TRNS,          KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,           KC_TRNS, KC_TRNS,
+        KC_TRNS, KC_TRNS, KC_TRNS,                            KC_TRNS,                            KC_TRNS, KC_TRNS,  KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS)
+};
+
+void matrix_init_user(void) {
+#ifdef RGB_MATRIX_ENABLE
+    rgb_matrix_init_user();
+#endif
+}
+
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+    switch (keycode) {
+        case KC_MISSION_CONTROL:
+            if (record->event.pressed) {
+                host_consumer_send(0x29F);
+            } else {
+                host_consumer_send(0);
+            }
+            return false;  // Skip all further processing of this key
+        case KC_LAUNCHPAD:
+            if (record->event.pressed) {
+                host_consumer_send(0x2A0);
+            } else {
+                host_consumer_send(0);
+            }
+            return false;  // Skip all further processing of this key
+        default:
+            return true;  // Process all other keycodes normally
+    }
+}
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap_user.h b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap_user.h
new file mode 100644
index 0000000000..ded78ae1b8
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/keymap_user.h
@@ -0,0 +1,27 @@
+/* Copyright 2022 @ Ionut Micu
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+// clang-format off
+
+enum layers{
+    MAC_BASE,
+    WIN_BASE,
+    _FN1,
+    _FN2,
+    _FN3,
+};
\ No newline at end of file
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/readme.md b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/readme.md
new file mode 100644
index 0000000000..42ca81a1a1
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/readme.md
@@ -0,0 +1,29 @@
+# Ladduro keymap for Keychron Q2 (ANSI rev_0110) v1.01
+
+This keymap ports Q1 logic (by gtg465x) for RGB caps lock indicator and layer RBG assigned key.
+
+## Features:
+- On macOS, F3 opens Mission Control and F4 opens Launchpad without needing to configure shortcuts in System Preferences
+- RGB lighting turns off when the computer sleeps
+- Caps Lock and alpha keys turn red to indicate when Caps Lock is on
+- When the Fn layer is active, RGB lighting turns off for keys that are not assigned
+
+Make example for this keyboard (after setting up your build environment):
+
+    make keychron/q2/rev_0110:ladduro
+
+Flashing example for this keyboard:
+
+    make keychron/q2/rev_0110:ladduro:flash
+
+See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs).
+
+## Changelog
+
+### 14/03/2021 - 1.
+
+- Initial release
+
+### 02/04/2021 - 1.01
+
+- Remove via enable from rules.mk as it breaks the layout for some reason
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.c b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.c
new file mode 100644
index 0000000000..19ddd54aa8
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.c
@@ -0,0 +1,72 @@
+/* Copyright 2022 @ Ionut Micu
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include QMK_KEYBOARD_H
+#include "rgb_matrix_user.h"
+#include "keymap_user.h"
+
+keypos_t led_index_key_position[DRIVER_LED_TOTAL];
+
+void rgb_matrix_init_user(void) {
+    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+        for (uint8_t col = 0; col < MATRIX_COLS; col++) {
+            uint8_t led_index = g_led_config.matrix_co[row][col];
+            if (led_index != NO_LED) {
+                led_index_key_position[led_index] = (keypos_t){.row = row, .col = col};
+            }
+        }
+    }
+}
+
+void rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
+    uint8_t current_layer = get_highest_layer(layer_state);
+    switch (current_layer) {
+        case MAC_BASE:
+        case WIN_BASE:
+#ifdef CAPS_LOCK_INDICATOR_COLOR
+            if (host_keyboard_led_state().caps_lock) {
+                rgb_matrix_set_color_by_keycode(led_min, led_max, current_layer, is_caps_lock_indicator, CAPS_LOCK_INDICATOR_COLOR);
+            }
+#endif
+            break;
+        case _FN1:
+        case _FN2:
+        case _FN3:
+#ifdef FN_LAYER_TRANSPARENT_KEYS_OFF
+            rgb_matrix_set_color_by_keycode(led_min, led_max, current_layer, is_transparent, RGB_OFF);
+#endif
+            break;
+    }
+}
+
+void rgb_matrix_set_color_by_keycode(uint8_t led_min, uint8_t led_max, uint8_t layer, bool (*is_keycode)(uint16_t), uint8_t red, uint8_t green, uint8_t blue) {
+    for (uint8_t i = led_min; i < led_max; i++) {
+        uint16_t keycode = keymap_key_to_keycode(layer, led_index_key_position[i]);
+        if ((*is_keycode)(keycode)) {
+            rgb_matrix_set_color(i, red, green, blue);
+        }
+    }
+}
+
+bool is_caps_lock_indicator(uint16_t keycode) {
+#ifdef CAPS_LOCK_INDICATOR_LIGHT_ALPHAS
+    return (KC_A <= keycode && keycode <= KC_Z) || keycode == KC_CAPS;
+#else
+    return keycode == KC_CAPS;
+#endif
+}
+
+bool is_transparent(uint16_t keycode) { return keycode == KC_TRNS; }
\ No newline at end of file
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.h b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.h
new file mode 100644
index 0000000000..4d33f483ae
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rgb_matrix_user.h
@@ -0,0 +1,25 @@
+
+/* Copyright 2022 @ Ionut Micu
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+void rgb_matrix_init_user(void);
+
+void rgb_matrix_set_color_by_keycode(uint8_t led_min, uint8_t led_max, uint8_t layer, bool (*is_keycode)(uint16_t), uint8_t red, uint8_t green, uint8_t blue);
+
+bool is_caps_lock_indicator(uint16_t keycode);
+bool is_transparent(uint16_t keycode);
\ No newline at end of file
diff --git a/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rules.mk b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rules.mk
new file mode 100644
index 0000000000..c0ee640cd2
--- /dev/null
+++ b/keyboards/keychron/q2/rev_0110/keymaps/ladduro/rules.mk
@@ -0,0 +1,5 @@
+MOUSEKEY_ENABLE = no
+
+ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
+    SRC += rgb_matrix_user.c
+endif