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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 :5;
  62. uint8_t kind :3;
  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_LAYER:
  156. * 1000|--xx|0000 0000 Clear keyamp
  157. * 100X|LLLL|0000 00xx Reset default layer and clear keymap
  158. * 100X|LLLL| keycode Invert with tap key
  159. * 100X|LLLL|1111 0000 Invert with tap toggle
  160. * 100X|LLLL|1111 00xx Invert[^= 1<<L]
  161. * 100X|LLLL|1111 0100 On/Off
  162. * 100X|LLLL|1111 01xx On[|= 1<<L]
  163. * 100X|LLLL|1111 1000 Off/On
  164. * 100X|LLLL|1111 10xx Off[&= ~(1<<L)]
  165. * 100X|LLLL|1111 1100 Set/Clear
  166. * 100X|LLLL|1111 11xx Set[= 1<<L]
  167. * XLLLL: Layer 0-31
  168. * xx: On {00:for special use, 01:press, 10:release, 11:both}
  169. *
  170. * ACT_LAYER_BITOP:
  171. * 101B|Booo|xxxx xxxx bit operation
  172. * BB: operand. which part of layer state bits
  173. * 00: 0-7th bit
  174. * 01: 8-15th bit
  175. * 10: 16-23th bit
  176. * 11: 24-31th bit
  177. * ooo: operation.
  178. * 000: AND
  179. * 001: OR
  180. * 010: XOR
  181. * 011:
  182. * 100: LSHIFT
  183. * 101: RSHIFT
  184. * 110:
  185. * 111:
  186. * bbbb bbbb: bits
  187. * layer_state |= (((layer_state>>(0bBB*8)) & 0xff) BITOP 0bxxxxxxxx)<<(0bBB*8)
  188. * layer_state: 32-bit layer switch state
  189. *
  190. *
  191. *
  192. *
  193. * Extensions(11XX)
  194. * ----------------
  195. * ACT_MACRO(1100):
  196. * 1100|opt | id(8) Macro play?
  197. * 1100|1111| id(8) Macro record?
  198. *
  199. * ACT_COMMAND(1110):
  200. * 1110|opt | id(8) Built-in Command exec
  201. *
  202. * ACT_FUNCTION(1111):
  203. * 1111| address(12) Function?
  204. * 1111|opt | id(8) Function?
  205. *
  206. */
  207. enum action_kind_id {
  208. ACT_MODS = 0b0000,
  209. ACT_LMODS = 0b0000,
  210. ACT_RMODS = 0b0001,
  211. ACT_MODS_TAP = 0b0010,
  212. ACT_LMODS_TAP = 0b0010,
  213. ACT_RMODS_TAP = 0b0011,
  214. ACT_USAGE = 0b0100,
  215. ACT_MOUSEKEY = 0b0101,
  216. ACT_LAYER = 0b1000,
  217. ACT_LAYER1 = 0b1001,
  218. ACT_LAYER_BITOP = 0b1010,
  219. ACT_LAYER1_BITOP = 0b1011,
  220. ACT_MACRO = 0b1100,
  221. ACT_COMMAND = 0b1110,
  222. ACT_FUNCTION = 0b1111
  223. };
  224. /* action utility */
  225. #define ACTION_NO 0
  226. #define ACTION_TRANSPARENT 1
  227. #define ACTION(kind, param) ((kind)<<12 | (param))
  228. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  229. /*
  230. * Key
  231. */
  232. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  233. /* Mods & key */
  234. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
  235. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
  236. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
  237. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
  238. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  239. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  240. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  241. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  242. /* Tap key */
  243. enum mods_codes {
  244. MODS_ONESHOT = 0x00,
  245. };
  246. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  247. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  248. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  249. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  250. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  251. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  252. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  253. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  254. /* HID Usage */
  255. enum usage_pages {
  256. PAGE_SYSTEM,
  257. PAGE_CONSUMER
  258. };
  259. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
  260. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
  261. /* Mousekey */
  262. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  263. /* Layer Actions:
  264. * Invert layer ^= (1<<layer)
  265. * On layer |= (1<<layer)
  266. * Off layer &= ~(1<<layer)
  267. * Set layer = (1<<layer)
  268. * Clear layer = 0
  269. */
  270. enum layer_param_on {
  271. ON_PRESS = 1,
  272. ON_RELEASE = 2,
  273. ON_BOTH = 3,
  274. };
  275. enum layer_pram_op {
  276. OP_RESET = 0x00,
  277. OP_INV4 = 0x00,
  278. OP_INV = 0xF0,
  279. OP_ON = 0xF4,
  280. OP_OFF = 0xF8,
  281. OP_SET = 0xFC,
  282. };
  283. enum layer_pram_bitop {
  284. BITOP_AND,
  285. BITOP_OR,
  286. BITOP_XOR,
  287. BITOP_LSHIFT,
  288. BITOP_RSHIFT,
  289. };
  290. /*
  291. * Default Layer
  292. */
  293. #define ACTION_DEFAULT_LAYER ACTION(ACT_LAYER, ON_RELEASE<<8 | OP_RESET | 0)
  294. #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
  295. #define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_RESET | (on))
  296. /*
  297. * Keymap Layer
  298. */
  299. #define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer)
  300. #define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INV(layer, ON_RELEASE)
  301. /* Keymap Invert */
  302. #define ACTION_LAYER_INV(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_INV | (on))
  303. #define ACTION_LAYER_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_INV | 0)
  304. /* Keymap On */
  305. #define ACTION_LAYER_ON(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_ON | (on))
  306. #define ACTION_LAYER_ON_OFF(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_ON | 0)
  307. /* Keymap Off */
  308. #define ACTION_LAYER_OFF(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | (on))
  309. #define ACTION_LAYER_OFF_ON(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | 0)
  310. /* Keymap Set */
  311. #define ACTION_LAYER_SET(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_SET | (on))
  312. #define ACTION_LAYER_SET_CLEAR(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_SET | 0)
  313. /* Keymap Invert with tap key */
  314. #define ACTION_LAYER_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
  315. /* Layer BitOp: 101|BB|ooo|xxxxxxxx */
  316. #define ACTION_LAYER_BITOP(op, part, bits) (ACT_LAYER_BITOP<<12 | (part&0x3)<<11 | (op&0x7)<<8 | bits)
  317. #define ACTION_LAYER_AND(part, bits) ACTION_LAYER_BITOP(BITOP_AND, part, bits)
  318. #define ACTION_LAYER_OR(part, bits) ACTION_LAYER_BITOP(BITOP_OR, part, bits)
  319. #define ACTION_LAYER_XOR(part, bits) ACTION_LAYER_BITOP(BITOP_XOR, part, bits)
  320. #define ACTION_LAYER_LSHIFT(part, bits) ACTION_LAYER_BITOP(BITOP_LSHIFT, part, bits)
  321. #define ACTION_LAYER_RSHIFT(part, bits) ACTION_LAYER_BITOP(BITOP_RSHIFT, part, bits)
  322. /*
  323. * Extensions
  324. */
  325. /* Macro */
  326. #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
  327. #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
  328. #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
  329. /* Command */
  330. #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  331. /* Function */
  332. enum function_opts {
  333. FUNC_TAP = 0x8, /* indciates function is tappable */
  334. };
  335. #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
  336. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
  337. #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
  338. #endif /* ACTION_H */