Skip to content

Commit

Permalink
feat: use variable arg functions for key funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Aug 17, 2024
1 parent 1d59e89 commit ca9f0c2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
13 changes: 10 additions & 3 deletions include/squirrel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
#include <stdbool.h>
#include <stdint.h>

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
Expand Down
76 changes: 64 additions & 12 deletions src/squirrel_quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,78 @@
#include <stdarg.h>
#include <stdint.h>

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;
Expand All @@ -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;
Expand Down

0 comments on commit ca9f0c2

Please sign in to comment.