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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_bitop {
  60. uint8_t bits :4;
  61. uint8_t xbit :1;
  62. uint8_t part :3;
  63. uint8_t on :2;
  64. uint8_t op :2;
  65. uint8_t kind :4;
  66. } layer_bitop;
  67. struct action_layer_tap {
  68. uint8_t code :8;
  69. uint8_t val :5;
  70. uint8_t kind :3;
  71. } layer_tap;
  72. struct action_usage {
  73. uint16_t code :10;
  74. uint8_t page :2;
  75. uint8_t kind :4;
  76. } usage;
  77. struct action_command {
  78. uint8_t id :8;
  79. uint8_t opt :4;
  80. uint8_t kind :4;
  81. } command;
  82. struct action_function {
  83. uint8_t id :8;
  84. uint8_t opt :4;
  85. uint8_t kind :4;
  86. } func;
  87. } action_t;
  88. /* Execute action per keyevent */
  89. void action_exec(keyevent_t event);
  90. /* action for key */
  91. action_t action_for_key(uint8_t layer, key_t key);
  92. /* macro */
  93. const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
  94. /* user defined special function */
  95. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
  96. /* Utilities for actions. */
  97. void process_action(keyrecord_t *record);
  98. void register_code(uint8_t code);
  99. void unregister_code(uint8_t code);
  100. void add_mods(uint8_t mods);
  101. void del_mods(uint8_t mods);
  102. void set_mods(uint8_t mods);
  103. void clear_keyboard(void);
  104. void clear_keyboard_but_mods(void);
  105. bool sending_anykey(void);
  106. void layer_switch(uint8_t new_layer);
  107. bool is_tap_key(key_t key);
  108. /* debug */
  109. void debug_event(keyevent_t event);
  110. void debug_record(keyrecord_t record);
  111. void debug_action(action_t action);
  112. /*
  113. * Action codes
  114. * ============
  115. * 16bit code: action_kind(4bit) + action_parameter(12bit)
  116. *
  117. * Keyboard Keys(00XX)
  118. * -------------------
  119. * ACT_LMODS(0000):
  120. * 0000|0000|000000|00 No action
  121. * 0000|0000|000000|01 Transparent
  122. * 0000|0000| keycode Key
  123. * 0000|mods|000000|00 Left mods
  124. * 0000|mods| keycode Key & Left mods
  125. *
  126. * ACT_RMODS(0001):
  127. * 0001|0000|000000|00 No action(not used)
  128. * 0001|0000|000000|01 Transparent(not used)
  129. * 0001|0000| keycode Key(no used)
  130. * 0001|mods|000000|00 Right mods
  131. * 0001|mods| keycode Key & Right mods
  132. *
  133. * ACT_LMODS_TAP(0010):
  134. * 0010|mods|000000|00 Left mods OneShot
  135. * 0010|mods|000000|01 (reserved)
  136. * 0010|mods|000000|10 (reserved)
  137. * 0010|mods|000000|11 (reserved)
  138. * 0010|mods| keycode Left mods + tap Key
  139. *
  140. * ACT_RMODS_TAP(0011):
  141. * 0011|mods|000000|00 Right mods OneShot
  142. * 0011|mods|000000|01 (reserved)
  143. * 0011|mods|000000|10 (reserved)
  144. * 0011|mods|000000|11 (reserved)
  145. * 0011|mods| keycode Right mods + tap Key
  146. *
  147. *
  148. * Other keys(01XX)
  149. * --------------------
  150. * This action handles other usages than keyboard.
  151. * ACT_USAGE(0100):
  152. * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
  153. * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
  154. * 0100|10| usage(10) (reserved)
  155. * 0100|11| usage(10) (reserved)
  156. *
  157. * ACT_MOUSEKEY(0110):
  158. * 0101|XXXX| keycode Mouse key
  159. *
  160. *
  161. * Layer Actions(10XX)
  162. * -------------------
  163. * ACT_LAYER:
  164. * 1000|oo00|pppx BBBB Default Layer Bitwise operation
  165. * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
  166. * ppp: 4-bit chunk part(0-7)
  167. * xBBBB: bits and extra bit
  168. * 1000|ooee|pppx BBBB Layer Bitwise Operation
  169. * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
  170. * ppp: 4-bit chunk part(0-7)
  171. * xBBBB: bits and extra bit
  172. * ee: on event(00:default layer, 01:press, 10:release, 11:both)
  173. *
  174. * ACT_LAYER_TAP:
  175. * 101x|LLLL| keycode Invert with tap key
  176. * 101x|LLLL|1110 xxxx Reserved(0xE0-EF)
  177. * 101x|LLLL|1111 0000 Invert with tap toggle(0xF0)
  178. * 101x|LLLL|1111 0001 On Off
  179. * 101x|LLLL|1111 0010 Off On
  180. * 101x|LLLL|1111 0011 Set Clear
  181. * 101x|LLLL|1111 xxxx Reserved(0xF4-FF)
  182. * xLLLL: layer(0-31)
  183. *
  184. *
  185. *
  186. *
  187. *
  188. * Extensions(11XX)
  189. * ----------------
  190. * ACT_MACRO(1100):
  191. * 1100|opt | id(8) Macro play?
  192. * 1100|1111| id(8) Macro record?
  193. *
  194. * ACT_COMMAND(1110):
  195. * 1110|opt | id(8) Built-in Command exec
  196. *
  197. * ACT_FUNCTION(1111):
  198. * 1111| address(12) Function?
  199. * 1111|opt | id(8) Function?
  200. *
  201. */
  202. enum action_kind_id {
  203. ACT_MODS = 0b0000,
  204. ACT_LMODS = 0b0000,
  205. ACT_RMODS = 0b0001,
  206. ACT_MODS_TAP = 0b0010,
  207. ACT_LMODS_TAP = 0b0010,
  208. ACT_RMODS_TAP = 0b0011,
  209. ACT_USAGE = 0b0100,
  210. ACT_MOUSEKEY = 0b0101,
  211. ACT_LAYER = 0b1000,
  212. ACT_LAYER_TAP = 0b1010,
  213. ACT_LAYER_TAP1 = 0b1011,
  214. ACT_MACRO = 0b1100,
  215. ACT_COMMAND = 0b1110,
  216. ACT_FUNCTION = 0b1111
  217. };
  218. /* action utility */
  219. #define ACTION_NO 0
  220. #define ACTION_TRANSPARENT 1
  221. #define ACTION(kind, param) ((kind)<<12 | (param))
  222. #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
  223. /*
  224. * Key
  225. */
  226. #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
  227. /* Mods & key */
  228. #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
  229. #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
  230. #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
  231. #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
  232. #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  233. #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  234. #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
  235. #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
  236. /* Tap key */
  237. enum mods_codes {
  238. MODS_ONESHOT = 0x00,
  239. };
  240. #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
  241. #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  242. #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
  243. #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
  244. #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  245. #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  246. #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
  247. #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
  248. /* HID Usage */
  249. enum usage_pages {
  250. PAGE_SYSTEM,
  251. PAGE_CONSUMER
  252. };
  253. #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
  254. #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
  255. /* Mousekey */
  256. #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
  257. /* Layer Actions */
  258. enum layer_param_on {
  259. ON_PRESS = 1,
  260. ON_RELEASE = 2,
  261. ON_BOTH = 3,
  262. };
  263. enum layer_param_op {
  264. OP_DEFAULT_LAYER = 0,
  265. };
  266. enum layer_param_bit_op {
  267. OP_BIT_AND = 0,
  268. OP_BIT_OR,
  269. OP_BIT_XOR,
  270. OP_BIT_SET,
  271. };
  272. enum layer_pram_tap_op {
  273. OP_TAP_TOGGLE = 0xF0,
  274. OP_ON_OFF,
  275. OP_OFF_ON,
  276. OP_SET_CLEAR,
  277. };
  278. /* Layer Operation 1000|ee00|ooov vvvv */
  279. #define ACTION_LAYER(op, val, on) (ACT_LAYER<<12 | (on)<<10 | (op)<<5 | val)
  280. /* Layer Bitwise Operation 1000|ooee|pppx BBBB */
  281. #define ACTION_LAYER_BITOP(op, part, bits, on) (ACT_LAYER<<12 | (op)<<10 | (on)<<8 | (part)<<5 | (bits)&0x1f)
  282. /* Layer with Tapping 101x|LLLL| keycode */
  283. #define ACTION_LAYER_TAP(layer, key) (ACT_LAYER_TAP<<12 | (layer)<<8 | (key))
  284. /* Default Layer Operation */
  285. #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER(layer, ON_RELEASE)
  286. #define ACTION_DEFAULT_LAYER(layer, on) ACTION_LAYER(OP_DEFAULT_LAYER, layer, on)
  287. /* Layer Operation */
  288. #define ACTION_LAYER_CLEAR(on) ACTION_LAYER_AND(0x1f, (on))
  289. #define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer)
  290. #define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INVERT(layer, ON_RELEASE)
  291. #define ACTION_LAYER_INVERT(layer, on) ACTION_LAYER_BIT_XOR((layer)/4, 1<<((layer)%4), (on))
  292. #define ACTION_LAYER_ON(layer, on) ACTION_LAYER_BIT_OR((layer)/4, 1<<((layer)%4), (on))
  293. #define ACTION_LAYER_OFF(layer, on) ACTION_LAYER_BIT_AND((layer)/4, ~(1<<((layer)%4)), (on))
  294. #define ACTION_LAYER_SET(layer, on) ACTION_LAYER_BIT_SET((layer)/4, 1<<((layer)%4), (on))
  295. #define ACTION_LAYER_ON_OFF(layer) ACTION_LAYER_TAP((layer), OP_ON_OFF)
  296. #define ACTION_LAYER_OFF_ON(layer) ACTION_LAYER_TAP((layer), OP_OFF_ON)
  297. #define ACTION_LAYER_SET_CLEAR(layer) ACTION_LAYER_TAP((layer), OP_SET_CLEAR)
  298. /* Bitwise Operation */
  299. #define ACTION_LAYER_BIT_AND(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_AND, part, bits)
  300. #define ACTION_LAYER_BIT_OR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_OR, part, bits)
  301. #define ACTION_LAYER_BIT_XOR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_XOR, part, bits)
  302. #define ACTION_LAYER_BIT_SET(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_SET, part, bits)
  303. /* with Tapping */
  304. #define ACTION_LAYER_TAP_KEY(layer, key) ACTION_LAYER_TAP((layer), (key))
  305. #define ACTION_LAYER_TAP_TOGGLE(layer) ACTION_LAYER_TAP((layer), OP_TAP_TOGGLE)
  306. /*
  307. * Extensions
  308. */
  309. /* Macro */
  310. #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
  311. #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
  312. #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
  313. /* Command */
  314. #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
  315. /* Function */
  316. enum function_opts {
  317. FUNC_TAP = 0x8, /* indciates function is tappable */
  318. };
  319. #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
  320. #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
  321. #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
  322. #endif /* ACTION_H */