Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

action.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. /* Struct to record event and tap count */
  19. typedef struct {
  20. keyevent_t event;
  21. uint8_t tap_count;
  22. } keyrecord_t;
  23. /* Action struct.
  24. *
  25. * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
  26. * AVR looks like a little endian in avr-gcc.
  27. *
  28. * NOTE: not portable across compiler/endianness?
  29. * Byte order and bit order of 0x1234:
  30. * Big endian: 15 ... 8 7 ... 210
  31. * | 0x12 | 0x34 |
  32. * 0001 0010 0011 0100
  33. * Little endian: 012 ... 7 8 ... 15
  34. * | 0x34 | 0x12 |
  35. * 0010 1100 0100 1000
  36. */
  37. typedef union {
  38. uint16_t code;
  39. struct action_kind {
  40. uint16_t param :12;
  41. uint8_t id :4;
  42. } kind;
  43. struct action_key {
  44. uint8_t code :8;
  45. uint8_t mods :4;
  46. uint8_t kind :4;
  47. } key;
  48. struct action_layer {
  49. uint8_t code :8;
  50. uint8_t val :4;
  51. uint8_t kind :4;
  52. } layer;
  53. struct action_usage {
  54. uint16_t code :10;
  55. uint8_t page :2;
  56. uint8_t kind :4;
  57. } usage;
  58. struct action_command {
  59. uint8_t id :8;
  60. uint8_t opt :4;
  61. uint8_t kind :4;
  62. } command;
  63. struct action_function {
  64. uint8_t id :8;
  65. uint8_t opt :4;
  66. uint8_t kind :4;
  67. } func;
  68. } action_t;
  69. /* layer used currently */
  70. extern uint8_t current_layer;
  71. /* layer to return or start with */
  72. extern uint8_t default_layer;
  73. /* Execute action per keyevent */
  74. void action_exec(keyevent_t event);
  75. /* action for key */
  76. action_t action_for_key(uint8_t layer, key_t key);
  77. /* user defined special function */
  78. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
  79. /*
  80. * Utilities for actions.
  81. */
  82. void register_code(uint8_t code);
  83. void unregister_code(uint8_t code);
  84. void add_mods(uint8_t mods);
  85. void del_mods(uint8_t mods);
  86. void set_mods(uint8_t mods);
  87. void clear_keyboard(void);
  88. void clear_keyboard_but_mods(void);
  89. bool sending_anykey(void);
  90. void layer_switch(uint8_t new_layer);
  91. bool is_tap_key(key_t key);
  92. bool waiting_buffer_has_anykey_pressed(void);
  93. /*
  94. * Action codes
  95. * ============
  96. * 16bit code: action_kind(4bit) + action_parameter(12bit)
  97. *
  98. * Keyboard Keys
  99. * -------------
  100. * ACT_LMODS(0000):
  101. * 0000|0000|000000|00 No action
  102. * 0000|0000|000000|01 Transparent
  103. * 0000|0000| keycode Key
  104. * 0000|mods|000000|00 Left mods
  105. * 0000|mods| keycode Key & Left mods
  106. *
  107. * ACT_RMODS(0001):
  108. * 0001|0000|000000|00 No action(not used)
  109. * 0001|0000|000000|01 Transparent(not used)
  110. * 0001|0000| keycode Key(no used)
  111. * 0001|mods|000000|00 Right mods
  112. * 0001|mods| keycode Key & Right mods
  113. *
  114. * ACT_LMODS_TAP(0010):
  115. * 0010|mods|000000|00 Left mods OneShot
  116. * 0010|mods|000000|01 (reserved)
  117. * 0010|mods|000000|10 (reserved)
  118. * 0010|mods|000000|11 (reserved)
  119. * 0010|mods| keycode Left mods + tap Key
  120. *
  121. * ACT_RMODS_TAP(0011):
  122. * 0011|mods|000000|00 Right mods OneShot
  123. * 0011|mods|000000|01 (reserved)
  124. * 0011|mods|000000|10 (reserved)
  125. * 0011|mods|000000|11 (reserved)
  126. * 0011|mods| keycode Right mods + tap Key
  127. *
  128. *
  129. * Other HID Usage
  130. * ---------------
  131. * This action handles other usages than keyboard.
  132. * ACT_USAGE(0100):
  133. * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
  134. * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
  135. * 0100|10| usage(10) (reserved)
  136. * 0100|11| usage(10) (reserved)
  137. *
  138. *
  139. * Mouse Keys
  140. * ----------
  141. * TODO: can be combined with 'Other HID Usage'? to save action kind id.
  142. * ACT_MOUSEKEY(0110):
  143. * 0101|XXXX| keycode Mouse key
  144. *
  145. *
  146. * Layer Actions
  147. * -------------
  148. * ACT_LAYER(1000): Set layer
  149. * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary)
  150. * 1000|LLLL|0000 0001 set current layer on press
  151. * 1000|LLLL|0000 0010 set current layer on release
  152. * 1000|LLLL|0000 0011 set current layer on both
  153. * 1000|LLLL| keycode set current layer on hold and send key on tap
  154. * 1000|LLLL|1111 0000 set current layer on hold and toggle on several taps
  155. * 1000|DDDD|1111 1111 set default layer on press
  156. * L: 0 means default layer
  157. *
  158. * ACT_LAYER_BIT(1001): Bit-op layer
  159. * 1001|BBBB|0000 0000 bit-on current layer on press and bit-off on release(momentary)
  160. * 1001|BBBB|0000 0001 bit-xor current layer on press
  161. * 1001|BBBB|0000 0010 bit-xor current layer on release
  162. * 1001|BBBB|0000 0011 bit-xor current layer on both
  163. * 1001|BBBB| keycode bit-xor current layer on hold and send key on tap
  164. * 1001|BBBB|1111 0000 bit-xor current layer on hold and toggle on several taps
  165. * 1001|BBBB|1111 1111 bit-xor default layer on both
  166. *
  167. * ACT_LAYER_SWITCH: Switch
  168. * 1011|LLLL|0000 0000 On on press and Off on release(momentary)
  169. * 1011|LLLL|0000 0001 Invert on press
  170. * 1011|LLLL|0000 0010 Invert on release
  171. * 1011|LLLL|0000 0011 Invert on both
  172. * 1011|LLLL| keycode Invert on hold and send key on tap
  173. * 1011|LLLL|1111 0000 Invert on hold and toggle on several taps
  174. * 1011|LLLL|1111 1111 (not used)
  175. *
  176. *
  177. * Extensions(11XX)
  178. * ----------------
  179. * NOTE: NOT FIXED
  180. *
  181. * ACT_MACRO(1100):
  182. * 1100|opt | id(8) Macro play?
  183. * 1100|1111| id(8) Macro record?
  184. *
  185. * ACT_COMMAND(1110):
  186. * 1110|opt | id(8) Built-in Command exec
  187. *
  188. * ACT_FUNCTION(1111):
  189. * 1111| address(12) Function?
  190. * 1111|opt | id(8) Function?
  191. *
  192. */
  193. enum action_kind_id {
  194. ACT_LMODS = 0b0000,
  195. ACT_RMODS = 0b0001,
  196. ACT_LMODS_TAP = 0b0010,
  197. ACT_RMODS_TAP = 0b0011,
  198. ACT_USAGE = 0b0100,
  199. ACT_MOUSEKEY = 0b0101,
  200. ACT_LAYER = 0b1000,
  201. ACT_LAYER_BIT = 0b1001,
  202. ACT_LAYER_SWITCH = 0b1011,
  203. ACT_MACRO = 0b1100,
  204. ACT_COMMAND = 0b1110,
  205. ACT_FUNCTION = 0b1111
  206. };
  207. /* action utility */
  208. #define ACTION_NO 0
  209. #define ACTION_TRANSPARENT 1
  210. #define ACTION(kind, param) ((kind)<<12 | (param))
  211. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  212. /*
  213. * Key
  214. */
  215. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  216. /* Mods & key */
  217. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
  218. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
  219. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
  220. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
  221. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  222. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  223. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  224. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  225. /* Tap key */
  226. enum mods_codes {
  227. MODS_ONESHOT = 0x00,
  228. };
  229. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  230. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  231. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  232. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  233. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  234. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  235. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  236. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  237. /*
  238. * Layer switching
  239. */
  240. enum layer_codes {
  241. LAYER_MOMENTARY = 0,
  242. LAYER_ON_PRESS = 1,
  243. LAYER_ON_RELEASE = 2,
  244. LAYER_ON_BOTH =3,
  245. LAYER_TAP_TOGGLE = 0xF0,
  246. LAYER_SET_DEFAULT_ON_PRESS = 0xFD,
  247. LAYER_SET_DEFAULT_ON_RELEASE = 0xFE,
  248. LAYER_SET_DEFAULT_ON_BOTH = 0xFF
  249. };
  250. /*
  251. * Default layer
  252. */
  253. /* set default layer */
  254. #define ACTION_LAYER_SET_DEFAULT(layer) ACTION_LAYER_SET_DEFAULT_R(layer)
  255. #define ACTION_LAYER_SET_DEFAULT_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
  256. #define ACTION_LAYER_SET_DEFAULT_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
  257. #define ACTION_LAYER_SET_DEFAULT_B(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
  258. /* bit-xor default layer */
  259. #define ACTION_LAYER_BIT_DEFAULT(bits) ACTION_LAYER_BIT_DEFAULT_R(bits)
  260. #define ACTION_LAYER_BIT_DEFAULT_P(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
  261. #define ACTION_LAYER_BIT_DEFAULT_R(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
  262. #define ACTION_LAYER_BIT_DEFAULT_B(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
  263. /*
  264. * Current layer: Return to default layer
  265. */
  266. #define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R
  267. #define ACTION_LAYER_DEFAULT_P ACTION_LAYER_SET_P(0)
  268. #define ACTION_LAYER_DEFAULT_R ACTION_LAYER_SET_R(0)
  269. #define ACTION_LAYER_DEFAULT_B ACTION_LAYER_SET_B(0)
  270. /*
  271. * Current layer: Set
  272. */
  273. #define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer)
  274. #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY)
  275. #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
  276. #define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS)
  277. #define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE)
  278. #define ACTION_LAYER_SET_B(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_BOTH)
  279. #define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE)
  280. #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
  281. /*
  282. * Current layer: Bit-op
  283. */
  284. #define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits)
  285. #define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
  286. #define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits)
  287. #define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
  288. #define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
  289. #define ACTION_LAYER_BIT_B(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_BOTH)
  290. #define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
  291. #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
  292. /*
  293. * Layer SWITCH
  294. */
  295. /* momentary */
  296. #define ACTION_LAYER_SWITCH(layer) ACTION_LAYER_SWITCH_MOMENTARY(layer)
  297. #define ACTION_LAYER_SWITCH_MOMENTARY(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_MOMENTARY)
  298. #define ACTION_LAYER_SWITCH_TOGGLE(layer) ACTION_LAYER_SWITCH_R(layer)
  299. #define ACTION_LAYER_SWITCH_P(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_PRESS)
  300. #define ACTION_LAYER_SWITCH_R(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_RELEASE)
  301. #define ACTION_LAYER_SWITCH_B(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_BOTH)
  302. #define ACTION_LAYER_SWITCH_TAP_TOGGLE(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_TAP_TOGGLE)
  303. #define ACTION_LAYER_SWITCH_TAP_KEY(layer, key) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | (key))
  304. /*
  305. * HID Usage
  306. */
  307. enum usage_pages {
  308. PAGE_SYSTEM,
  309. PAGE_CONSUMER
  310. };
  311. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
  312. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
  313. /* Mousekey */
  314. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  315. /* Macro */
  316. #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
  317. /* Command */
  318. #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  319. /* Function */
  320. enum function_opts {
  321. FUNC_TAP = 0x8, /* indciates function is tappable */
  322. };
  323. #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
  324. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
  325. #endif /* ACTION_H */