Keyboard firmwares for Atmel AVR and Cortex-M
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.

action.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <stdint.h>
  17. #include <stdbool.h>
  18. #include "keyboard.h"
  19. #include "keycode.h"
  20. #include "action_macro.h"
  21. typedef struct {
  22. keyevent_t event;
  23. #ifndef NO_ACTION_TAPPING
  24. /* tapping count and state */
  25. struct {
  26. bool interrupted :1;
  27. bool reserved2 :1;
  28. bool reserved1 :1;
  29. bool reserved0 :1;
  30. uint8_t count :4;
  31. } tap;
  32. #endif
  33. } keyrecord_t;
  34. /* Action struct.
  35. *
  36. * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
  37. * AVR looks like a little endian in avr-gcc.
  38. *
  39. * NOTE: not portable across compiler/endianness?
  40. * Byte order and bit order of 0x1234:
  41. * Big endian: 15 ... 8 7 ... 210
  42. * | 0x12 | 0x34 |
  43. * 0001 0010 0011 0100
  44. * Little endian: 012 ... 7 8 ... 15
  45. * | 0x34 | 0x12 |
  46. * 0010 1100 0100 1000
  47. */
  48. typedef union {
  49. uint16_t code;
  50. struct action_kind {
  51. uint16_t param :12;
  52. uint8_t id :4;
  53. } kind;
  54. struct action_key {
  55. uint8_t code :8;
  56. uint8_t mods :4;
  57. uint8_t kind :4;
  58. } key;
  59. struct action_layer {
  60. uint8_t code :8;
  61. uint8_t val :4;
  62. uint8_t kind :4;
  63. } layer;
  64. struct action_usage {
  65. uint16_t code :10;
  66. uint8_t page :2;
  67. uint8_t kind :4;
  68. } usage;
  69. struct action_command {
  70. uint8_t id :8;
  71. uint8_t opt :4;
  72. uint8_t kind :4;
  73. } command;
  74. struct action_function {
  75. uint8_t id :8;
  76. uint8_t opt :4;
  77. uint8_t kind :4;
  78. } func;
  79. } action_t;
  80. /* Execute action per keyevent */
  81. void action_exec(keyevent_t event);
  82. /* action for key */
  83. action_t action_for_key(uint8_t layer, key_t key);
  84. /* macro */
  85. const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
  86. /* user defined special function */
  87. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
  88. /* Utilities for actions. */
  89. void process_action(keyrecord_t *record);
  90. void register_code(uint8_t code);
  91. void unregister_code(uint8_t code);
  92. void add_mods(uint8_t mods);
  93. void del_mods(uint8_t mods);
  94. void set_mods(uint8_t mods);
  95. void clear_keyboard(void);
  96. void clear_keyboard_but_mods(void);
  97. bool sending_anykey(void);
  98. void layer_switch(uint8_t new_layer);
  99. bool is_tap_key(key_t key);
  100. /* debug */
  101. void debug_event(keyevent_t event);
  102. void debug_record(keyrecord_t record);
  103. void debug_action(action_t action);
  104. /*
  105. * Action codes
  106. * ============
  107. * 16bit code: action_kind(4bit) + action_parameter(12bit)
  108. *
  109. * Keyboard Keys(00XX)
  110. * -------------------
  111. * ACT_LMODS(0000):
  112. * 0000|0000|000000|00 No action
  113. * 0000|0000|000000|01 Transparent
  114. * 0000|0000| keycode Key
  115. * 0000|mods|000000|00 Left mods
  116. * 0000|mods| keycode Key & Left mods
  117. *
  118. * ACT_RMODS(0001):
  119. * 0001|0000|000000|00 No action(not used)
  120. * 0001|0000|000000|01 Transparent(not used)
  121. * 0001|0000| keycode Key(no used)
  122. * 0001|mods|000000|00 Right mods
  123. * 0001|mods| keycode Key & Right mods
  124. *
  125. * ACT_LMODS_TAP(0010):
  126. * 0010|mods|000000|00 Left mods OneShot
  127. * 0010|mods|000000|01 (reserved)
  128. * 0010|mods|000000|10 (reserved)
  129. * 0010|mods|000000|11 (reserved)
  130. * 0010|mods| keycode Left mods + tap Key
  131. *
  132. * ACT_RMODS_TAP(0011):
  133. * 0011|mods|000000|00 Right mods OneShot
  134. * 0011|mods|000000|01 (reserved)
  135. * 0011|mods|000000|10 (reserved)
  136. * 0011|mods|000000|11 (reserved)
  137. * 0011|mods| keycode Right mods + tap Key
  138. *
  139. *
  140. * Other keys(01XX)
  141. * --------------------
  142. * This action handles other usages than keyboard.
  143. * ACT_USAGE(0100):
  144. * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
  145. * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
  146. * 0100|10| usage(10) (reserved)
  147. * 0100|11| usage(10) (reserved)
  148. *
  149. * ACT_MOUSEKEY(0110):
  150. * 0101|XXXX| keycode Mouse key
  151. *
  152. *
  153. * Layer Actions(10XX)
  154. * -------------------
  155. * ACT_KEYMAP:
  156. * 1000|--xx|0000 0000 Clear keyamp and overlay
  157. * 1000|LLLL|0000 00xx Reset default layer and clear keymap and overlay
  158. * 1000|LLLL| keycode Invert with tap key
  159. * 1000|LLLL|1111 0000 Invert with tap toggle
  160. * 1000|LLLL|1111 00xx Invert[^= 1<<L]
  161. * 1000|LLLL|1111 0100 On/Off
  162. * 1000|LLLL|1111 01xx On[|= 1<<L]
  163. * 1000|LLLL|1111 1000 Off/On
  164. * 1000|LLLL|1111 10xx Off[&= ~(1<<L)]
  165. * 1000|LLLL|1111 1100 Set/Clear
  166. * 1000|LLLL|1111 11xx Set[= 1<<L]
  167. * default layer: 0-15(4bit)
  168. * xx: On {00:for special use, 01:press, 10:release, 11:both}
  169. *
  170. * ACT_OVERLAY:
  171. * 1011|0000|0000 0000 Clear overlay
  172. * 1011|LLLL|0000 00ss Invert 4-bit chunk [^= L<<(4*ss)]
  173. * 1011|LLLL| keycode Invert with tap key
  174. * 1011|LLLL|1111 0000 Invert with tap toggle
  175. * 1011|LLLL|1111 00xx Invert[^= 1<<L]
  176. * 1011|LLLL|1111 0100 On/Off(momentary)
  177. * 1011|LLLL|1111 01xx On[|= 1<<L]
  178. * 1011|LLLL|1111 1000 Off/On
  179. * 1011|LLLL|1111 10xx Off[&= ~(1<<L)]
  180. * 1011|LLLL|1111 1100 Set/Clear
  181. * 1011|LLLL|1111 11xx Set[= 1<<L]
  182. * overlays: 16-layer on/off status(16bit)
  183. * xx: On {00:for special use, 01:press, 10:release, 11:both}
  184. *
  185. *
  186. * Extensions(11XX)
  187. * ----------------
  188. * ACT_MACRO(1100):
  189. * 1100|opt | id(8) Macro play?
  190. * 1100|1111| id(8) Macro record?
  191. *
  192. * ACT_COMMAND(1110):
  193. * 1110|opt | id(8) Built-in Command exec
  194. *
  195. * ACT_FUNCTION(1111):
  196. * 1111| address(12) Function?
  197. * 1111|opt | id(8) Function?
  198. *
  199. */
  200. enum action_kind_id {
  201. ACT_LMODS = 0b0000,
  202. ACT_RMODS = 0b0001,
  203. ACT_LMODS_TAP = 0b0010,
  204. ACT_RMODS_TAP = 0b0011,
  205. ACT_USAGE = 0b0100,
  206. ACT_MOUSEKEY = 0b0101,
  207. ACT_KEYMAP = 0b1000,
  208. ACT_OVERLAY = 0b1001,
  209. ACT_MACRO = 0b1100,
  210. ACT_COMMAND = 0b1110,
  211. ACT_FUNCTION = 0b1111
  212. };
  213. /* action utility */
  214. #define ACTION_NO 0
  215. #define ACTION_TRANSPARENT 1
  216. #define ACTION(kind, param) ((kind)<<12 | (param))
  217. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  218. /*
  219. * Key
  220. */
  221. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  222. /* Mods & key */
  223. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
  224. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
  225. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
  226. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
  227. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  228. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  229. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  230. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  231. /* Tap key */
  232. enum mods_codes {
  233. MODS_ONESHOT = 0x00,
  234. };
  235. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  236. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  237. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  238. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  239. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  240. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  241. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  242. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  243. /* HID Usage */
  244. enum usage_pages {
  245. PAGE_SYSTEM,
  246. PAGE_CONSUMER
  247. };
  248. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
  249. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
  250. /* Mousekey */
  251. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  252. /* Layer Actions:
  253. * Invert layer ^= (1<<layer)
  254. * On layer |= (1<<layer)
  255. * Off layer &= ~(1<<layer)
  256. * Set layer = (1<<layer)
  257. * Clear layer = 0
  258. */
  259. enum layer_params {
  260. ON_PRESS = 1,
  261. ON_RELEASE = 2,
  262. ON_BOTH = 3,
  263. OP_RESET = 0x00,
  264. OP_INV4 = 0x00,
  265. OP_INV = 0xF0,
  266. OP_ON = 0xF4,
  267. OP_OFF = 0xF8,
  268. OP_SET = 0xFC,
  269. };
  270. /*
  271. * Default Layer
  272. */
  273. #define ACTION_DEFAULT_LAYER ACTION(ACT_KEYMAP, ON_RELEASE<<8 | OP_RESET | 0)
  274. #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
  275. #define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_RESET | (on))
  276. /*
  277. * Keymap Layer
  278. */
  279. #define ACTION_KEYMAP_MOMENTARY(layer) ACTION_KEYMAP_ON_OFF(layer)
  280. #define ACTION_KEYMAP_TOGGLE(layer) ACTION_KEYMAP_INV(layer, ON_RELEASE)
  281. /* Keymap Invert */
  282. #define ACTION_KEYMAP_INV(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | (on))
  283. #define ACTION_KEYMAP_TAP_TOGGLE(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | 0)
  284. /* Keymap On */
  285. #define ACTION_KEYMAP_ON(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | (on))
  286. #define ACTION_KEYMAP_ON_OFF(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | 0)
  287. /* Keymap Off */
  288. #define ACTION_KEYMAP_OFF(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | (on))
  289. #define ACTION_KEYMAP_OFF_ON(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | 0)
  290. /* Keymap Set */
  291. #define ACTION_KEYMAP_SET(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | (on))
  292. #define ACTION_KEYMAP_SET_CLEAR(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | 0)
  293. /* Keymap Invert with tap key */
  294. #define ACTION_KEYMAP_TAP_KEY(layer, key) ACTION(ACT_KEYMAP, (layer)<<8 | (key))
  295. /*
  296. * Overlay Layer
  297. */
  298. #define ACTION_OVERLAY_MOMENTARY(layer) ACTION_OVERLAY_ON_OFF(layer)
  299. #define ACTION_OVERLAY_TOGGLE(layer) ACTION_OVERLAY_INV(layer, ON_RELEASE)
  300. /* Overlay Clear */
  301. #define ACTION_OVERLAY_CLEAR(on) ACTION(ACT_OVERLAY, 0<<8 | OP_INV4 | (on))
  302. /* Overlay Invert 4-bit chunk */
  303. #define ACTION_OVERLAY_INV4(bits, shift) ACTION(ACT_OVERLAY, (bits)<<8 | OP_INV4 | shift)
  304. /* Overlay Invert */
  305. #define ACTION_OVERLAY_INV(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | (on))
  306. #define ACTION_OVERLAY_TAP_TOGGLE(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | 0)
  307. /* Overlay On */
  308. #define ACTION_OVERLAY_ON(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | (on))
  309. #define ACTION_OVERLAY_ON_OFF(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | 0)
  310. /* Overlay Off */
  311. #define ACTION_OVERLAY_OFF(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | (on))
  312. #define ACTION_OVERLAY_OFF_ON(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | 0)
  313. /* Overlay Set */
  314. #define ACTION_OVERLAY_SET(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | (on))
  315. #define ACTION_OVERLAY_SET_CLEAR(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | 0)
  316. /* Overlay Invert with tap key */
  317. #define ACTION_OVERLAY_TAP_KEY(layer, key) ACTION(ACT_OVERLAY, (layer)<<8 | (key))
  318. /*
  319. * Extensions
  320. */
  321. /* Macro */
  322. #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
  323. #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
  324. #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
  325. /* Command */
  326. #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  327. /* Function */
  328. enum function_opts {
  329. FUNC_TAP = 0x8, /* indciates function is tappable */
  330. };
  331. #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
  332. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
  333. #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
  334. #endif /* ACTION_H */