Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. uint16_t id :4;
  44. } kind;
  45. struct action_key {
  46. uint16_t code :8;
  47. uint16_t mods :4;
  48. uint16_t kind :4;
  49. } key;
  50. struct action_layer {
  51. uint16_t code :8;
  52. uint16_t val :4;
  53. uint16_t kind :4;
  54. } layer;
  55. struct action_usage {
  56. uint16_t code :10;
  57. uint16_t page :2;
  58. uint16_t kind :4;
  59. } usage;
  60. struct action_command {
  61. uint16_t id :8;
  62. uint16_t opt :4;
  63. uint16_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| keycode Key
  95. 0000|mods|000000|00 Left mods
  96. 0000|mods| keycode Key & Left mods
  97. ACT_RMODS(0001):
  98. 0001|0000|000000|00 No action
  99. 0001|0000| keycode Key(no used)
  100. 0001|mods|000000|00 Right mods
  101. 0001|mods| keycode Key & Right mods
  102. ACT_LMODS_TAP(0010):
  103. 0010|mods|000000|00 Left mods OneShot
  104. 0010|mods|000000|01 (reserved)
  105. 0010|mods|000000|10 (reserved)
  106. 0010|mods|000000|11 (reserved)
  107. 0010|mods| keycode Left mods + tap Key
  108. ACT_RMODS_TAP(0011):
  109. 0011|mods|000000|00 Right mods OneShot
  110. 0011|mods|000000|01 (reserved)
  111. 0011|mods|000000|10 (reserved)
  112. 0011|mods|000000|11 (reserved)
  113. 0011|mods| keycode Right mods + tap Key
  114. Other HID Usage
  115. ---------------
  116. This action handles other usages than keyboard.
  117. ACT_USAGE(0100):
  118. 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
  119. 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
  120. 0100|10| usage(10) (reserved)
  121. 0100|11| usage(10) (reserved)
  122. Mouse Keys
  123. ----------
  124. TODO: can be combined with 'Other HID Usage'? to save action kind id.
  125. ACT_MOUSEKEY(0110):
  126. 0101|XXXX| keycode Mouse key
  127. Layer Actions
  128. -------------
  129. TODO: reconsider layer methods.
  130. 1 momemtary + tap key up: L, down: default
  131. 1 bitwise + tap key up: xor B, down: xor B
  132. 3 momemtary go + tap key? up: X, down:
  133. 3 toggle(mementary back) + tap key? up: down: Y
  134. 3 no tap up: X, down: Y
  135. ACT_LAYER_PRESSED(1000): Set layer on key pressed
  136. ACT_LAYER_RELEASED(1001): Set layer on key released
  137. ACT_LAYER_BIT(1010): On/Off layer bit
  138. ACT_LAYER_EXT(1011): Extentions
  139. 1000|LLLL|0000 0000 set layer L when pressed
  140. 1001|LLLL|0000 0000 set layer L when released
  141. 1010|BBBB|0000 0000 on/off bit B when pressed/released
  142. 1011|0000|0000 0000 set default layer when pressed
  143. 1011|0001|0000 0000 set default layer when released
  144. 1000|LLLL|1111 0000 set layer L when pressed + tap toggle
  145. 1001|LLLL|1111 0000 set layer L when released + tap toggle
  146. 1010|BBBB|1111 0000 on/off bit B when pressed/released + tap toggle
  147. 1011|0000|1111 0000 set default layer when pressed + tap toggle
  148. 1011|0001|1111 0000 set default layer when released + tap toggle
  149. 1000|LLLL|1111 1111 set L to default layer when pressed
  150. 1001|LLLL|1111 1111 set L to default layer when released
  151. 1010|BBBB|1111 1111 on/off bit B of default layer when pressed/released
  152. 1011|0000|1111 1111 set current to default layer when pressed
  153. 1011|0001|1111 1111 set current to default layer when released
  154. 1000|LLLL| keycode set layer L when pressed + tap key
  155. 1001|LLLL| keyocde set layer L when released + tap key
  156. 1010|BBBB| keyocde on/off bit B when pressed/released + tap key
  157. 1011|0000| keyocde set default layer when pressed + tap key
  158. 1011|0001| keyocde set default layer when released + tap key
  159. Extensions(11XX)
  160. ----------------
  161. NOTE: NOT FIXED
  162. ACT_MACRO(1100):
  163. 1100|opt | id(8) Macro play?
  164. 1100|1111| id(8) Macro record?
  165. ACT_COMMAND(1110):
  166. 1110|opt | id(8) Built-in Command exec
  167. ACT_FUNCTION(1111):
  168. 1111| address(12) Function?
  169. 1111|opt | id(8) Function?
  170. */
  171. enum action_kind_id {
  172. ACT_LMODS = 0b0000,
  173. ACT_RMODS = 0b0001,
  174. ACT_LMODS_TAP = 0b0010,
  175. ACT_RMODS_TAP = 0b0011,
  176. ACT_USAGE = 0b0100,
  177. ACT_MOUSEKEY = 0b0101,
  178. ACT_LAYER_PRESSED = 0b1000,
  179. ACT_LAYER_RELEASED = 0b1001,
  180. ACT_LAYER_BIT = 0b1010,
  181. ACT_LAYER_EXT = 0b1011,
  182. ACT_MACRO = 0b1100,
  183. ACT_COMMAND = 0b1110,
  184. ACT_FUNCTION = 0b1111
  185. };
  186. enum params {
  187. P_ONESHOT = 0x00,
  188. };
  189. enum options {
  190. O_TAP = 0x8,
  191. };
  192. /* action utility */
  193. #define ACTION_NO 0
  194. #define ACTION(kind, param) ((kind)<<12 | (param))
  195. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  196. /* Key */
  197. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  198. /* Mods & key */
  199. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00)
  200. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key))
  201. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00)
  202. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key))
  203. /* Mod & key */
  204. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  205. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  206. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  207. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  208. /* Mods + Tap key */
  209. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  210. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | P_ONESHOT)
  211. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  212. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | P_ONESHOT)
  213. /* Mod + Tap key */
  214. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  215. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT)
  216. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  217. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT)
  218. // TODO: contemplate about layer action
  219. /* Switch current layer */
  220. #define ACTION_LAYER_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00)
  221. #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00)
  222. #define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00)
  223. #define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00)
  224. #define ACTION_LAYER_SET_DEFAULT ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00)
  225. #define ACTION_LAYER_RETURN_DEFAULT ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00)
  226. #define ACTION_LAYER_SET_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00)
  227. #define ACTION_LAYER_SET_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00)
  228. /* Switch default layer */
  229. #define ACTION_LAYER_DEFAULT_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF)
  230. #define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF)
  231. #define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF)
  232. #define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF)
  233. #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF)
  234. #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF)
  235. /* Layer switch with tap key */
  236. #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key))
  237. #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
  238. #define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key))
  239. /* Layer switch with tap toggle */
  240. #define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0)
  241. #define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0)
  242. #define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0)
  243. #define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0)
  244. /* HID Usage */
  245. #define ACTION_USAGE_PAGE_SYSTEM 0
  246. #define ACTION_USAGE_PAGE_CONSUMER 1
  247. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_SYSTEM<<10 | (id))
  248. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_CONSUMER<<10 | (id))
  249. /* Mousekey */
  250. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  251. /* Macro */
  252. #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
  253. /* Command */
  254. #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  255. /* Function */
  256. #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
  257. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, O_TAP<<8 | id)
  258. #endif /* ACTION_H */