From ca9f0c27eaf1f4c3fae0c6fc82a9604ee2511558 Mon Sep 17 00:00:00 2001 From: Edward Hesketh Date: Sun, 18 Aug 2024 00:52:06 +0100 Subject: [PATCH] feat: use variable arg functions for key funcs --- include/squirrel.h | 13 ++++++-- src/squirrel_quantum.c | 76 +++++++++++++++++++++++++++++++++++------- 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/include/squirrel.h b/include/squirrel.h index facf72b..8d32239 100644 --- a/include/squirrel.h +++ b/include/squirrel.h @@ -4,12 +4,19 @@ #include #include +enum squirrel_error { + ERR_NONE = 0, + ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT, +}; + struct key { - void (*pressed)(struct key *, ...); // called when the key is pressed - void *pressed_arguments; // arguments to pass to pressed + void (*pressed)(struct key *, ...); // called when the key is pressed + void *pressed_arguments; // arguments to pass to pressed + int pressed_argument_count; // the amount of arguments in pressed_arguments void (*released)(struct key *, ...); // called when the key is released void *released_arguments; // arguments to pass to released - bool is_pressed; // true if the key is currently pressed + int released_argument_count; // the amount of arguments in released_arguments + bool is_pressed; // true if the key is currently pressed }; extern int key_count; // number of keys on the keyboard diff --git a/src/squirrel_quantum.c b/src/squirrel_quantum.c index e11dc55..3cd1be1 100644 --- a/src/squirrel_quantum.c +++ b/src/squirrel_quantum.c @@ -4,26 +4,78 @@ #include #include -void key_nop(struct key *key, ...) { (void)key; } +enum squirrel_error key_nop(struct key *key, int arg_count, ...) { + (void)key; + (void)arg_count; +} // keyboard_press expects a single uint8 keycode -void keyboard_press(struct key *key, ...) { va_list args; }; +enum squirrel_error keyboard_press(struct key *key, int arg_count, ...) { + va_list args; + va_start(args, arg_count); + if (arg_count != 1) { + return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; + }; + uint8_t keycode = va_arg(args, int); + activate_keycode(keycode); // squirrel_keyboard + va_end(args); + return ERR_NONE; +}; // keyboard_release expects a single uint8 keycode -void keyboard_release(struct key *key, uint8_t keycode) { - deactivate_keycode(keycode); +enum squirrel_error keyboard_release(struct key *key, int arg_count, ...) { + va_list args; + va_start(args, arg_count); + if (arg_count != 1) { + return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; + }; + uint8_t keycode = va_arg(args, int); + deactivate_keycode(keycode); // squirrel_keyboard + va_end(args); + return ERR_NONE; } -void keyboard_modifier_press(struct key *key, uint8_t modifier) { - activate_modifier(modifier); +// keyboard_modifier_press expects a single uint8 modifier +enum squirrel_error keyboard_modifier_press(struct key *key, int arg_count, + ...) { + va_list args; + va_start(args, arg_count); + if (arg_count != 1) { + return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; + }; + uint8_t modifier = va_arg(args, int); + activate_modifier(modifier); // squirrel_keyboard + va_end(args); + return ERR_NONE; } -void keyboard_modifier_release(struct key *key, uint8_t modifier) { - deactivate_modifier(modifier); +enum squirrel_error keyboard_modifier_release(struct key *key, int arg_count, + ...) { + va_list args; + va_start(args, arg_count); + if (arg_count != 1) { + return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; + }; + uint8_t modifier = va_arg(args, int); + deactivate_modifier(modifier); // squirrel_keyboard + va_end(args); + return ERR_NONE; } -void quantum_passthrough_press(struct key *key, uint8_t layer, - uint8_t key_index) { +// quantum_passthrough_press expects a uint8 with the layer of the key it was +// activated from, and a uint8 of the key's index in the layer. +enum squirrel_error quantum_passthrough_press(struct key *key, int arg_count, + ...) { + va_list args; + va_start(args, arg_count); + if (arg_count != 1) { + return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; + }; + uint8_t modifier = va_arg(args, int); + deactivate_modifier(modifier); // squirrel_keyboard + va_end(args); + return ERR_NONE; + for (int i = layer; i >= 0; i--) { if (!layers[i].active) { break; @@ -33,8 +85,8 @@ void quantum_passthrough_press(struct key *key, uint8_t layer, } } -void quantum_passthrough_release(struct key *key, uint8_t layer, - uint8_t key_index) { +enum squirrel_error quantum_passthrough_release(struct key *key, int arg_count, + ...) { for (int i = layer; i >= 0; i--) { if (!layers[i].active) { break;