You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

action.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Copyright 2012,2013 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef ACTION_H
  15. #define ACTION_H
  16. #include "keyboard.h"
  17. #include "keycode.h"
  18. /* Execute action per keyevent */
  19. void action_exec(keyevent_t event);
  20. /* Struct to record event and tap count */
  21. typedef struct {
  22. keyevent_t event;
  23. uint8_t tap_count;
  24. } keyrecord_t;
  25. /* Action struct.
  26. *
  27. * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
  28. * AVR looks like a little endian in avr-gcc.
  29. *
  30. * NOTE: not portable across compiler/endianness?
  31. * Byte order and bit order of 0x1234:
  32. * Big endian: 15 ... 8 7 ... 210
  33. * | 0x12 | 0x34 |
  34. * 0001 0010 0011 0100
  35. * Little endian: 012 ... 7 8 ... 15
  36. * | 0x34 | 0x12 |
  37. * 0010 1100 0100 1000
  38. */
  39. typedef union {
  40. uint16_t code;
  41. struct action_kind {
  42. uint16_t param :12;
  43. uint8_t id :4;
  44. } kind;
  45. struct action_key {
  46. uint8_t code :8;
  47. uint8_t mods :4;
  48. uint8_t kind :4;
  49. } key;
  50. struct action_layer {
  51. uint8_t code :8;
  52. uint8_t val :4;
  53. uint8_t kind :4;
  54. } layer;
  55. struct action_usage {
  56. uint16_t code :10;
  57. uint8_t page :2;
  58. uint8_t kind :4;
  59. } usage;
  60. struct action_command {
  61. uint8_t id :8;
  62. uint8_t opt :4;
  63. uint8_t kind :4;
  64. } command;
  65. struct action_function {
  66. uint8_t id :8;
  67. uint8_t opt :4;
  68. uint8_t kind :4;
  69. } func;
  70. } action_t;
  71. /*
  72. * Utilities for actions.
  73. */
  74. void register_code(uint8_t code);
  75. void unregister_code(uint8_t code);
  76. void add_mods(uint8_t mods);
  77. void del_mods(uint8_t mods);
  78. void set_mods(uint8_t mods);
  79. void clear_keyboard(void);
  80. void clear_keyboard_but_mods(void);
  81. bool sending_anykey(void);
  82. void layer_switch(uint8_t new_layer);
  83. bool is_tap_key(key_t key);
  84. bool waiting_buffer_has_anykey_pressed(void);
  85. /*
  86. * Action codes
  87. * ============
  88. * 16bit code: action_kind(4bit) + action_parameter(12bit)
  89. *
  90. Keyboard Keys
  91. -------------
  92. ACT_LMODS(0000):
  93. 0000|0000|000000|00 No action
  94. 0000|0000|000000|01 Transparent
  95. 0000|0000| keycode Key
  96. 0000|mods|000000|00 Left mods
  97. 0000|mods| keycode Key & Left mods
  98. ACT_RMODS(0001):
  99. 0001|0000|000000|00 No action(not used)
  100. 0001|0000|000000|01 Transparent(not used)
  101. 0001|0000| keycode Key(no used)
  102. 0001|mods|000000|00 Right mods
  103. 0001|mods| keycode Key & Right mods
  104. ACT_LMODS_TAP(0010):
  105. 0010|mods|000000|00 Left mods OneShot
  106. 0010|mods|000000|01 (reserved)
  107. 0010|mods|000000|10 (reserved)
  108. 0010|mods|000000|11 (reserved)
  109. 0010|mods| keycode Left mods + tap Key
  110. ACT_RMODS_TAP(0011):
  111. 0011|mods|000000|00 Right mods OneShot
  112. 0011|mods|000000|01 (reserved)
  113. 0011|mods|000000|10 (reserved)
  114. 0011|mods|000000|11 (reserved)
  115. 0011|mods| keycode Right mods + tap Key
  116. Other HID Usage
  117. ---------------
  118. This action handles other usages than keyboard.
  119. ACT_USAGE(0100):
  120. 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
  121. 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
  122. 0100|10| usage(10) (reserved)
  123. 0100|11| usage(10) (reserved)
  124. Mouse Keys
  125. ----------
  126. TODO: can be combined with 'Other HID Usage'? to save action kind id.
  127. ACT_MOUSEKEY(0110):
  128. 0101|XXXX| keycode Mouse key
  129. Layer Actions
  130. -------------
  131. ACT_LAYER(1000): Set layer
  132. ACT_LAYER_BIT(1001): Bit-op layer
  133. 1000|LLLL|0000 0000 set L to layer on press and set default on release(momentary)
  134. 1000|LLLL|0000 0001 set L to layer on press
  135. 1000|LLLL|0000 0010 set L to layer on release
  136. 1000|----|0000 0011 set default to layer on both(return to default layer)
  137. 1000|LLLL| keycode set L to layer while hold and send key on tap
  138. 1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps
  139. 1000|LLLL|1111 1111 set L to default and layer(on press)
  140. 1001|BBBB|0000 0000 (not used)
  141. 1001|BBBB|0000 0001 bit-xor layer with B on press
  142. 1001|BBBB|0000 0010 bit-xor layer with B on release
  143. 1001|BBBB|0000 0011 bit-xor layer with B on both(momentary)
  144. 1001|BBBB| keycode bit-xor layer with B while hold and send key on tap
  145. 1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps
  146. 1001|BBBB|1111 1111 bit-xor default with B and set layer(on press)
  147. Extensions(11XX)
  148. ----------------
  149. NOTE: NOT FIXED
  150. ACT_MACRO(1100):
  151. 1100|opt | id(8) Macro play?
  152. 1100|1111| id(8) Macro record?
  153. ACT_COMMAND(1110):
  154. 1110|opt | id(8) Built-in Command exec
  155. ACT_FUNCTION(1111):
  156. 1111| address(12) Function?
  157. 1111|opt | id(8) Function?
  158. */
  159. enum action_kind_id {
  160. ACT_LMODS = 0b0000,
  161. ACT_RMODS = 0b0001,
  162. ACT_LMODS_TAP = 0b0010,
  163. ACT_RMODS_TAP = 0b0011,
  164. ACT_USAGE = 0b0100,
  165. ACT_MOUSEKEY = 0b0101,
  166. ACT_LAYER = 0b1000,
  167. ACT_LAYER_BIT = 0b1001,
  168. ACT_MACRO = 0b1100,
  169. ACT_COMMAND = 0b1110,
  170. ACT_FUNCTION = 0b1111
  171. };
  172. /* action utility */
  173. #define ACTION_NO 0
  174. #define ACTION_TRANSPARENT 1
  175. #define ACTION(kind, param) ((kind)<<12 | (param))
  176. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  177. /* Key */
  178. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  179. /* Mods & key */
  180. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
  181. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
  182. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
  183. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
  184. /* Mod & key */
  185. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  186. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  187. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  188. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  189. /* Mods + Tap key */
  190. enum mods_codes {
  191. MODS_ONESHOT = 0x00,
  192. };
  193. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  194. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  195. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  196. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  197. /* Mod + Tap key */
  198. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  199. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  200. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  201. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  202. /*
  203. * Switch layer
  204. */
  205. enum layer_codes {
  206. LAYER_MOMENTARY = 0,
  207. LAYER_ON_PRESS = 1,
  208. LAYER_ON_RELEASE = 2,
  209. LAYER_DEFAULT =3,
  210. LAYER_TAP_TOGGLE = 0xF0,
  211. LAYER_CHANGE_DEFAULT = 0xFF
  212. };
  213. enum layer_vals_default {
  214. DEFAULT_ON_PRESS = 1,
  215. DEFAULT_ON_RELEASE = 2,
  216. DEFAULT_ON_BOTH = 3,
  217. };
  218. /*
  219. * return to default layer
  220. */
  221. #define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R
  222. /* set default layer on press */
  223. #define ACTION_LAYER_DEFAULT_P ACTION(ACT_LAYER, DEFAULT_ON_PRESS<<8 | LAYER_DEFAULT)
  224. /* set default layer on release */
  225. #define ACTION_LAYER_DEFAULT_R ACTION(ACT_LAYER, DEFAULT_ON_RELEASE<<8 | LAYER_DEFAULT)
  226. /* change default layer and set layer */
  227. /*
  228. * Set layer
  229. */
  230. /* set layer on press and none on release */
  231. #define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer)
  232. /* set layer on press and set default on release (This is needed by legacy keymap support.) */
  233. #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY)
  234. /* set layer on press and none on release */
  235. #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
  236. /* set layer while hold and send key on tap */
  237. #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
  238. /* set layer on press */
  239. #define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS)
  240. /* set layer on release */
  241. #define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE)
  242. /* set layer on hold and toggle on several taps */
  243. #define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE)
  244. /* set default layer on both press and release */
  245. #define ACTION_LAYER_SET_DEFAULT(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_CHANGE_DEFAULT)
  246. /*
  247. * Bit-op layer
  248. */
  249. /* bit-xor on both press and release */
  250. #define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits)
  251. #define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
  252. /* bit-xor on press */
  253. #define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits)
  254. /* bit-xor while hold and send key on tap */
  255. #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
  256. /* bit-xor on press */
  257. #define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
  258. /* bit-xor on release */
  259. #define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
  260. /* bit-xor while hold and toggle on several taps */
  261. #define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
  262. /* bit-xor default layer and set layer */
  263. #define ACTION_LAYER_BIT_DEFAULT(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_CHANGE_DEFAULT)
  264. /* HID Usage */
  265. enum usage_pages {
  266. PAGE_SYSTEM,
  267. PAGE_CONSUMER
  268. };
  269. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
  270. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
  271. /* Mousekey */
  272. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  273. /* Macro */
  274. #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
  275. /* Command */
  276. #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  277. /* Function */
  278. enum function_opts {
  279. FUNC_TAP = 0x8, /* indciates function is tappable */
  280. };
  281. #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
  282. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
  283. #endif /* ACTION_H */