From 585e062e87dbb227e73e2cb31d5041ff5520fb4d Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 18 May 2020 23:34:36 -0400 Subject: [PATCH] Initial basic modifiers support. --- include/dt-bindings/zmk/keys.h | 22 +++++++++++++++++----- include/zmk/hid.h | 2 ++ include/zmk/keys.h | 1 + src/endpoints.c | 1 + src/hid.c | 26 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/include/dt-bindings/zmk/keys.h b/include/dt-bindings/zmk/keys.h index 4c975dbe..5a2d97a2 100644 --- a/include/dt-bindings/zmk/keys.h +++ b/include/dt-bindings/zmk/keys.h @@ -60,13 +60,25 @@ #define KC_APP 0x65 +#define KC_LCTL 0xE0 +#define KC_LSFT 0xE1 +#define KC_LALT 0xE2 +#define KC_LGUI 0xE3 +#define KC_RCTL 0xE4 +#define KC_RSFT 0xE5 +#define KC_RALT 0xE6 #define KC_RGUI 0xE7 -#define MD_SHFT 0x01 -#define KC_ALT 0x02 -#define KC_CTRL 0x03 - #define ZC_TRNS 0xF0 #define ZC_NOOP 0xF1 -#define ZC_CSTM(n) (0xFF + n) \ No newline at end of file +#define ZC_CSTM(n) (0xFF + n) + +#define MOD_LCTL 0x00 +#define MOD_LSFT 0x01 +#define MOD_LALT 0x02 +#define MOD_LGUI 0x03 +#define MOD_RCTL 0x04 +#define MOD_RSFT 0x05 +#define MOD_RALT 0x06 +#define MOD_RGUI 0x07 \ No newline at end of file diff --git a/include/zmk/hid.h b/include/zmk/hid.h index 9f0760ab..8fbcf4fd 100644 --- a/include/zmk/hid.h +++ b/include/zmk/hid.h @@ -101,6 +101,8 @@ struct zmk_hid_report u8_t keys[13]; } __packed; +int zmk_hid_register_mod(zmk_mod modifier); +int zmk_hid_unregister_mod(zmk_mod modifier); int zmk_hid_press_key(zmk_key key); int zmk_hid_release_key(zmk_key key); diff --git a/include/zmk/keys.h b/include/zmk/keys.h index b61ef811..be057841 100644 --- a/include/zmk/keys.h +++ b/include/zmk/keys.h @@ -4,6 +4,7 @@ #include typedef u64_t zmk_key; +typedef u8_t zmk_mod; struct zmk_key_event { diff --git a/src/endpoints.c b/src/endpoints.c index 6854370f..b73e7ed9 100644 --- a/src/endpoints.c +++ b/src/endpoints.c @@ -33,6 +33,7 @@ int zmk_endpoints_send_key_event(struct zmk_key_event key_event) struct zmk_hid_report *report; int err; + if (key_event.pressed) { zmk_hid_press_key(key_event.key); diff --git a/src/hid.c b/src/hid.c index faf3cfc5..234c624f 100644 --- a/src/hid.c +++ b/src/hid.c @@ -4,6 +4,22 @@ static struct zmk_hid_report report = { .modifiers = 0, .keys = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; +#define _TOGGLE_MOD(mod, state) \ + if (modifier > MOD_RGUI) \ + { \ + return -EINVAL; \ + } \ + WRITE_BIT(report.modifiers, mod, state); + +int zmk_hid_register_mod(zmk_mod modifier) +{ + _TOGGLE_MOD(modifier, true); +} +int zmk_hid_unregister_mod(zmk_mod modifier) +{ + _TOGGLE_MOD(modifier, false); +} + #define KEY_OFFSET 0x02 #define MAX_KEYS 6 @@ -24,6 +40,11 @@ static struct zmk_hid_report report = { int zmk_hid_press_key(zmk_key code) { + if (code >= KC_LCTL && code <= KC_RGUI) + { + return zmk_hid_register_mod(code - KC_LCTL); + } + if (code > ZMK_HID_MAX_KEYCODE) { return -EINVAL; @@ -38,6 +59,11 @@ int zmk_hid_press_key(zmk_key code) int zmk_hid_release_key(zmk_key code) { + if (code >= KC_LCTL && code <= KC_RGUI) + { + return zmk_hid_unregister_mod(code - KC_LCTL); + } + if (code > ZMK_HID_MAX_KEYCODE) { return -EINVAL;