logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keymap.c (12896B)


  1. // see https://github.com/pepaslabs/hexon38
  2. #include QMK_KEYBOARD_H
  3. #define A_ KC_A
  4. #define B_ KC_B
  5. #define C_ KC_C
  6. #define D_ KC_D
  7. #define E_ KC_E
  8. #define F_ KC_F
  9. #define G_ KC_G
  10. #define H_ KC_H
  11. #define I_ KC_I
  12. #define J_ KC_J
  13. #define K_ KC_K
  14. #define L_ KC_L
  15. #define M_ KC_M
  16. #define N_ KC_N
  17. #define O_ KC_O
  18. #define P_ KC_P
  19. #define Q_ KC_Q
  20. #define R_ KC_R
  21. #define S_ KC_S
  22. #define T_ KC_T
  23. #define U_ KC_U
  24. #define V_ KC_V
  25. #define W_ KC_W
  26. #define X_ KC_X
  27. #define Y_ KC_Y
  28. #define Z_ KC_Z
  29. // Dual-role keys: modifier when held, alpha when tapped.
  30. #define A_CTL CTL_T(KC_A)
  31. #define S_ALT ALT_T(KC_S)
  32. #define D_GUI GUI_T(KC_D)
  33. #define F_SFT SFT_T(KC_F)
  34. #define J_SFT SFT_T(KC_J)
  35. #define K_GUI GUI_T(KC_K)
  36. #define L_ALT ALT_T(KC_L)
  37. #define COLN_CTL CTL_T(KC_SCLN)
  38. #define ______ KC_TRNS
  39. #define LSHIFT KC_LSFT
  40. #define RSHIFT KC_RSFT
  41. #define COMMA KC_COMM
  42. #define SLASH KC_SLSH
  43. #define SPACE KC_SPC
  44. #define TAB KC_TAB
  45. #define BKSPC KC_BSPC
  46. #define ENTER KC_ENT
  47. #define PERIOD KC_DOT
  48. #define BASE_LAYER LAYOUT
  49. #define BLANK_LAYER LAYOUT
  50. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  51. BASE_LAYER(
  52. // ,--------+--------+--------+--------. ,--------+--------+--------+--------.
  53. W_ , E_ , R_ , T_ , Y_ , U_ , I_ , O_ ,
  54. //|--------+--------+--------+--------+--------+--------| |--------+--------+--------+--------+--------+--------.
  55. Q_ , A_CTL , S_ALT , D_GUI , F_SFT , G_ , H_ , J_SFT , K_GUI , L_ALT ,COLN_CTL, P_ ,
  56. //|--------+--------+--------+--------+--------+--------' `--------+--------+--------+--------+--------+--------|
  57. B_ , Z_ , X_ , C_ , V_ , M_ , COMMA , PERIOD , SLASH , N_ ,
  58. //`--------+--------+--------+--------+--------' `--------+--------+--------+--------+--------'
  59. // ,--------+--------+--------+--------. ,--------+--------+--------+--------.
  60. LSHIFT , SPACE , TAB ,DB_TOGG , SPACE , BKSPC , ENTER , RSHIFT
  61. // `--------+--------+--------+--------' `--------+--------+--------+--------'
  62. ),
  63. BLANK_LAYER(
  64. // ,--------+--------+--------+--------. ,--------+--------+--------+--------.
  65. ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ ,
  66. //|--------+--------+--------+--------+--------+--------| |--------+--------+--------+--------+--------+--------.
  67. ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ ,
  68. //|--------+--------+--------+--------+--------+--------' `--------+--------+--------+--------+--------+--------|
  69. ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______ ,
  70. //`--------+--------+--------+--------+--------' `--------+--------+--------+--------+--------'
  71. // ,--------+--------+--------+--------. ,--------+--------+--------+--------.
  72. ______ , ______ , ______ , ______ , ______ , ______ , ______ , ______
  73. // `--------+--------+--------+--------' `--------+--------+--------+--------'
  74. )
  75. };
  76. // a linked list of pending key events (press or release) which we haven't processed yet.
  77. struct _pending_key_t {
  78. uint16_t keycode;
  79. keyrecord_t record;
  80. struct _pending_key_t *next;
  81. };
  82. typedef struct _pending_key_t pending_key_t;
  83. // worst case is 10 down strokes and 1 up stroke before we can start disambiguating.
  84. #define RINGSIZE 11
  85. // a ring buffer and linked list to store pending key events (presses and releases).
  86. // (basically, this is a fixed-allocation linked list.)
  87. struct _kring_t {
  88. // the actual key events.
  89. pending_key_t items[RINGSIZE];
  90. // the index of the oldest item, or -1 if no items.
  91. int8_t ifirst;
  92. // the index of the most recently added item, or -1 if no items.
  93. int8_t ilast;
  94. // the number of items in the ring.
  95. uint8_t count;
  96. // the head of the linked list.
  97. pending_key_t *head;
  98. };
  99. typedef struct _kring_t kring_t;
  100. // safe accessor to the i-th item of the linked list (returns pointer or NULL).
  101. pending_key_t* kring_get(kring_t *ring, uint8_t i) {
  102. if (i >= ring->count) {
  103. return NULL;
  104. }
  105. uint8_t j = (ring->ifirst + i) % RINGSIZE;
  106. return &(ring->items[j]);
  107. }
  108. // return the last key in the list of buffered keys.
  109. pending_key_t* kring_last(kring_t *ring) {
  110. if (ring->count == 0) {
  111. return NULL;
  112. }
  113. return kring_get(ring, ring->count - 1);
  114. }
  115. // remove the oldest item from the ring (the head of the list).
  116. void kring_pop(kring_t *ring) {
  117. if (ring->count > 0) {
  118. ring->ifirst += 1;
  119. ring->ifirst %= RINGSIZE;
  120. ring->head = ring->head->next;
  121. ring->count -= 1;
  122. }
  123. }
  124. // add an item to the ring (append to the list).
  125. void kring_append(kring_t *ring, uint16_t keycode, keyrecord_t *record) {
  126. if (ring->count >= RINGSIZE) {
  127. // uh oh, we overflowed the capacity of our buffer :(
  128. return;
  129. }
  130. // if the ring is empty, insert at index 0.
  131. if (ring->count == 0) {
  132. ring->count += 1;
  133. ring->ifirst = 0;
  134. ring->ilast = 0;
  135. ring->head = &(ring->items[0]);
  136. }
  137. // else, append it onto the end.
  138. else {
  139. ring->count += 1;
  140. ring->ilast += 1;
  141. ring->ilast %= RINGSIZE;
  142. }
  143. // the index at which we should insert this item.
  144. int8_t i = ring->ilast;
  145. // insert the item.
  146. ring->items[i].keycode = keycode;
  147. ring->items[i].record.event = record->event;
  148. #ifndef NO_ACTION_TAPPING
  149. ring->items[i].record.tap = record->tap;
  150. #endif
  151. ring->items[i].next = NULL;
  152. // update the previous item to point to this item.
  153. if (ring->count > 1) {
  154. kring_get(ring, ring->count - 2)->next = &(ring->items[i]);
  155. }
  156. }
  157. kring_t g_pending;
  158. void matrix_init_user(void) {
  159. g_pending.ifirst = -1;
  160. g_pending.ilast = -1;
  161. g_pending.count = 0;
  162. g_pending.head = NULL;
  163. }
  164. /*
  165. a_ a-: emit a
  166. a_ b_ b- a-: emit SHIFT+b
  167. a_ b_ a- b-: emit a, b
  168. dual1down, dual1up -> norm1down, norm1up
  169. dual1down, norm2down, norm2up -> mod1down, norm2down, norm2up
  170. dual1down, norm2down, dual1up -> norm1down, norm2down, norm1up
  171. dual1down, dual2down, norm3down, norm3up -> mod1down, mod2down, norm3down, norm3up
  172. so, a dual key can't be disambiguated until the next keyup of a keydown (not including keyups from keys before it).
  173. */
  174. bool is_ambiguous_kc(uint16_t kc) {
  175. // See the MT() define: https://github.com/qmk/qmk_firmware/blob/master/quantum/quantum_keycodes.h#L642
  176. // See the QK_MOD_TAP case: https://github.com/qmk/qmk_firmware/blob/master/quantum/keymap_common.c#L134
  177. uint8_t mod = mod_config((kc >> 0x8) & 0x1F);
  178. return mod != 0;
  179. }
  180. bool is_down(pending_key_t *k) {
  181. return k->record.event.pressed;
  182. }
  183. bool is_up(pending_key_t *k) {
  184. return !is_down(k);
  185. }
  186. bool keys_match(pending_key_t *a, pending_key_t *b) {
  187. return a->record.event.key.col == b->record.event.key.col
  188. && a->record.event.key.row == b->record.event.key.row;
  189. }
  190. // both the down and corresponding upstroke of a keypress.
  191. struct _pending_pair_t {
  192. pending_key_t *down;
  193. pending_key_t *up;
  194. };
  195. typedef struct _pending_pair_t pending_pair_t;
  196. // returns true if this keydown event has a corresponding keyup event in the
  197. // list of buffered keys. also fills out 'p'.
  198. bool is_downup_pair(pending_key_t *k, pending_pair_t *p) {
  199. // first, make sure this event is keydown.
  200. if (!is_down(k)) {
  201. return false;
  202. }
  203. // now find its matching keyup.
  204. pending_key_t *next = k->next;
  205. while (next != NULL) {
  206. if (keys_match(k, next) && is_up(next)) {
  207. // found it.
  208. if (p != NULL) {
  209. p->down = k;
  210. p->up = next;
  211. }
  212. return true;
  213. }
  214. next = next->next;
  215. }
  216. // didn't find it.
  217. return false;
  218. }
  219. // given a QK_MOD_TAP keycode, return the KC_* version of the modifier keycode.
  220. uint16_t get_mod_kc(uint16_t keycode) {
  221. uint8_t mod = mod_config((keycode >> 0x8) & 0x1F);
  222. switch (mod) {
  223. case MOD_LCTL:
  224. return KC_LCTL;
  225. case MOD_RCTL:
  226. return KC_RCTL;
  227. case MOD_LSFT:
  228. return KC_LSFT;
  229. case MOD_RSFT:
  230. return KC_RSFT;
  231. case MOD_LALT:
  232. return KC_LALT;
  233. case MOD_RALT:
  234. return KC_RALT;
  235. case MOD_LGUI:
  236. return KC_LGUI;
  237. case MOD_RGUI:
  238. return KC_RGUI;
  239. default:
  240. // shrug? this shouldn't happen.
  241. return keycode;
  242. }
  243. }
  244. bool is_mod_kc(uint16_t keycode) {
  245. switch (keycode) {
  246. case QK_MODS ... QK_MODS_MAX:
  247. return true;
  248. default:
  249. return false;
  250. }
  251. }
  252. void interpret_as_mod(pending_pair_t *p) {
  253. // see https://github.com/qmk/qmk_firmware/issues/1503
  254. pending_key_t *k;
  255. k = p->down;
  256. if (k != NULL) {
  257. k->keycode = get_mod_kc(k->keycode);
  258. }
  259. k = p->up;
  260. if (k != NULL) {
  261. k->keycode = get_mod_kc(k->keycode);
  262. }
  263. }
  264. void interpret_as_normal(pending_pair_t *p) {
  265. pending_key_t *k;
  266. k = p->down;
  267. if (k != NULL) {
  268. k->keycode = k->keycode & 0xFF;
  269. }
  270. k = p->up;
  271. if (k != NULL) {
  272. k->keycode = k->keycode & 0xFF;
  273. }
  274. }
  275. void execute_head_and_pop(kring_t *ring) {
  276. pending_key_t *head = kring_get(ring, 0);
  277. uint16_t kc = head->keycode;
  278. if (is_mod_kc(kc)) {
  279. if (is_down(head)) {
  280. dprintf(" %s: mod down 0x%04X\n", __func__, kc);
  281. set_mods(get_mods() | MOD_BIT(kc));
  282. } else {
  283. dprintf(" %s: mod up 0x%04X\n", __func__, kc);
  284. set_mods(get_mods() & ~MOD_BIT(kc));
  285. }
  286. } else {
  287. if (is_down(head)) {
  288. dprintf(" %s: key down 0x%04X\n", __func__, kc);
  289. register_code16(kc);
  290. } else {
  291. dprintf(" %s: key up 0x%04X\n", __func__, kc);
  292. unregister_code16(kc);
  293. }
  294. }
  295. kring_pop(ring);
  296. }
  297. // try to figure out what the next pending keypress means.
  298. bool parse_next(kring_t *pending) {
  299. pending_pair_t p;
  300. pending_key_t *first = kring_get(pending, 0);
  301. if (!is_ambiguous_kc(first->keycode)) {
  302. // this pending key isn't ambiguous, so execute it.
  303. dprintf(" %s: found unambiguous key\n", __func__);
  304. execute_head_and_pop(pending);
  305. return true;
  306. } else if (is_ambiguous_kc(first->keycode) && is_up(first)) {
  307. dprintf(" %s: interpreting keyup as mod\n", __func__);
  308. p.down = NULL;
  309. p.up = first;
  310. interpret_as_mod(&p);
  311. execute_head_and_pop(pending);
  312. return true;
  313. } else if (is_downup_pair(first, &p)) {
  314. // 'first' was released before any other pressed key, so treat this as
  315. // a rolling series of normal key taps.
  316. dprintf(" %s: found down-up pair, interpreting as normal key\n", __func__);
  317. interpret_as_normal(&p);
  318. execute_head_and_pop(pending);
  319. return true;
  320. } else {
  321. // if another key was pressed and released while 'first' was held, then we
  322. // should treat it like a modifier.
  323. pending_key_t *next = first->next;
  324. while (next != NULL) {
  325. if (is_downup_pair(next, NULL)) {
  326. dprintf(" %s: found subsequent downup pair, interpreting head as mod\n", __func__);
  327. p.down = first;
  328. p.up = NULL;
  329. interpret_as_mod(&p);
  330. execute_head_and_pop(pending);
  331. return true;
  332. }
  333. next = next->next;
  334. }
  335. // we can't disambiguate 'first' yet. wait for another keypress.
  336. dprintf(" %s: can't disambiguate (yet)\n", __func__);
  337. return false;
  338. }
  339. }
  340. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  341. if (keycode == QK_DEBUG_TOGGLE) {
  342. return true;
  343. }
  344. if (g_pending.count == 0 && !is_ambiguous_kc(keycode)) {
  345. // we have no pending keys and this key isn't ambiguous, so we should
  346. // just let QMK take care of it.
  347. dprintf("%s: handled by qmk\n", __func__);
  348. return true;
  349. } else {
  350. dprintf("%s: got dual-role key\n", __func__);
  351. // append the keypress and then try parsing all pending keypresses.
  352. kring_append(&g_pending, keycode, record);
  353. while (g_pending.count > 0) {
  354. dprintf("%s: looping through %d keys...\n", __func__, g_pending.count);
  355. if (!parse_next(&g_pending)) {
  356. // one of our keypresses is ambiguous and we can't proceed until
  357. // we get further keypresses to disambiguate it.
  358. dprintf("%s: %d pending keys are ambiguous\n", __func__, g_pending.count);
  359. break;
  360. }
  361. }
  362. return false;
  363. }
  364. }