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