123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include "inttypes.h"
- #include "stdint.h"
- #include "process_key_lock.h"
- #define BV_64(shift) (((uint64_t)1) << (shift))
- #define GET_KEY_ARRAY(code) (((code) < 0x40) ? key_state[0] : \
- ((code) < 0x80) ? key_state[1] : \
- ((code) < 0xC0) ? key_state[2] : key_state[3])
- #define GET_CODE_INDEX(code) (((code) < 0x40) ? (code) : \
- ((code) < 0x80) ? (code) - 0x40 : \
- ((code) < 0xC0) ? (code) - 0x80 : (code) - 0xC0)
- #define KEY_STATE(code) (GET_KEY_ARRAY(code) & BV_64(GET_CODE_INDEX(code))) == BV_64(GET_CODE_INDEX(code))
- #define SET_KEY_ARRAY_STATE(code, val) do { \
- switch (code) { \
- case 0x00 ... 0x3F: \
- key_state[0] = (val); \
- break; \
- case 0x40 ... 0x7F: \
- key_state[1] = (val); \
- break; \
- case 0x80 ... 0xBF: \
- key_state[2] = (val); \
- break; \
- case 0xC0 ... 0xFF: \
- key_state[3] = (val); \
- break; \
- } \
- } while(0)
- #define SET_KEY_STATE(code) SET_KEY_ARRAY_STATE(code, (GET_KEY_ARRAY(code) | BV_64(GET_CODE_INDEX(code))))
- #define UNSET_KEY_STATE(code) SET_KEY_ARRAY_STATE(code, (GET_KEY_ARRAY(code)) & ~(BV_64(GET_CODE_INDEX(code))))
- #define IS_STANDARD_KEYCODE(code) ((code) <= 0xFF)
- uint64_t key_state[4] = { 0x0, 0x0, 0x0, 0x0 };
- bool watching = false;
- static inline uint16_t translate_keycode(uint16_t keycode) {
- if (keycode > QK_ONE_SHOT_MOD && keycode <= QK_ONE_SHOT_MOD_MAX) {
- return keycode ^ QK_ONE_SHOT_MOD;
- } else {
- return keycode;
- }
- }
- bool process_key_lock(uint16_t *keycode, keyrecord_t *record) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- uint16_t translated_keycode = translate_keycode(*keycode);
- if (record->event.pressed) {
-
- if (!(IS_STANDARD_KEYCODE(translated_keycode) || translated_keycode == KC_LOCK)) {
- watching = false;
- return true;
- }
-
- if (translated_keycode == KC_LOCK) {
- watching = !watching;
- return false;
- }
- if (IS_STANDARD_KEYCODE(translated_keycode)) {
-
-
-
-
- if (watching) {
- watching = false;
- SET_KEY_STATE(translated_keycode);
-
-
- *keycode = translated_keycode;
-
- return true;
- }
- if (KEY_STATE(translated_keycode)) {
- UNSET_KEY_STATE(translated_keycode);
-
-
- return false;
- }
- }
-
-
- return true;
- } else {
-
- return !(IS_STANDARD_KEYCODE(translated_keycode) && KEY_STATE(translated_keycode));
- }
- }
|