Skip to content

Commit

Permalink
BREAKING: attempting to fix usage of pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Oct 6, 2024
1 parent f72e57a commit 7eab251
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 227 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-Og) # Enable debug optimizations
elseif(CMAKE_BUILD_TYPE STREQUAL "Testing")
message(STATUS "Testing enabled")
add_compile_options(-O3) # Don't include test code in coverage percent.
include(CTest)
enable_testing()

Expand Down
15 changes: 6 additions & 9 deletions include/squirrel_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@

struct key {
enum squirrel_error (*pressed)(struct key *, uint8_t layer, uint8_t key_index,
int arg_count,
void **args); // 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
enum squirrel_error (*released)(
struct key *, uint8_t layer, uint8_t key_index, int arg_count,
void **args); // called when the key is released
void **released_arguments; // arguments to pass to released
int released_argument_count; // the amount of arguments in released_arguments
void *arg); // called when the key is pressed
void *pressed_argument; // argument to pass to pressed
enum squirrel_error (*released)(struct key *, uint8_t layer,
uint8_t key_index,
void *arg); // called when the key is released
void *released_argument; // argument to pass to released
};

extern int key_count; // number of keys on the keyboard
Expand Down
48 changes: 17 additions & 31 deletions include/squirrel_quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <stdint.h>

struct layer {
struct key *keys; // array of keys in this layer
bool active; // true if this layer is currently active
bool active; // true if this layer is currently active
struct key *keys[]; // array of keys in this layer
};

// layers is a list of all the layers in the keyboard. 0-15 are configured,
Expand All @@ -17,91 +17,77 @@ extern struct layer layers[17];

// key_nop does nothing (no operation)
enum squirrel_error key_nop(struct key *key, uint8_t layer, uint8_t key_index,
int arg_count, void **args);
void *arg);

// keyboard_press expects a single uint8 keycode
enum squirrel_error keyboard_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// keyboard_release expects a single uint8 keycode
enum squirrel_error keyboard_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// keyboard_modifier_press expects a single uint8 modifier
enum squirrel_error keyboard_modifier_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// keyboard_modifier_release expects a single uint8 modifier
enum squirrel_error keyboard_modifier_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// consumer_press expects a single uint16 consumer code. See
// https://www.freebsddiary.org/APC/usb_hid_usages for all defined codes.
enum squirrel_error consumer_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// consumer_release expects a single uint16 consumer code. See
// https://www.freebsddiary.org/APC/usb_hid_usages for all defined codes.
enum squirrel_error consumer_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// quantum_passthrough_press passes the press action to the highest active layer
// below the current one. It expectes no extra args. Equivalent to KC_TRNS in
// QMK.
enum squirrel_error quantum_passthrough_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// quantum_passthrough_release passes the release action to the highest active
// layer below the current one. It expectes no extra args. Equivalent to KC_TRNS
// in QMK.
enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer,
uint8_t key_index,
int arg_count, void **args);
uint8_t key_index, void *arg);

// layer_momentary_press activates the layer with the given index. It expects
// the layer number as the first uint8 argument. Equivalent to MO() in QMK.
enum squirrel_error layer_momentary_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// layer_momentary_release deactivates the layer with the given index. It
// expects the layer number as the first uint8 argument. Equivalent to MO() in
// QMK.
enum squirrel_error layer_momentary_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// layer_toggle_press toggles the layer with the given index. It expects the
// layer number as the first uint8 argument. Equivalent to TG() in QMK.
enum squirrel_error layer_toggle_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// layer_toggle_release does nothing at the moment. It expects the layer number
// as a uint8 anyway - this is a placeholder for future functionality.
// Equivalent to TG() in QMK.
enum squirrel_error layer_toggle_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// layer_solo_press turns off all other layers than the layer with the given
// index. It expects the layer number as the first uint8 argument. Equivalent to
// TO() in QMK.
enum squirrel_error layer_solo_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);

// layer_solo_release does nothing at the moment. It expects the layer number
// as a uint8 anyway - this is a placeholder for future functionality.
// Equivalent to TO() in QMK.
enum squirrel_error layer_solo_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
uint8_t key_index, void *arg);
#endif
18 changes: 5 additions & 13 deletions src/squirrel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@

enum squirrel_error squirrel_init(int total_keys) {
key_count = total_keys;
struct key passthrough_key;
passthrough_key.pressed = quantum_passthrough_press;
passthrough_key.pressed_argument_count = 0;
passthrough_key.pressed_arguments = NULL;
passthrough_key.released = quantum_passthrough_release;
passthrough_key.released_argument_count = 0;
passthrough_key.released_arguments = NULL;
struct key *passthrough_key =
&(struct key){.pressed = quantum_passthrough_press,
.released = quantum_passthrough_release};
for (uint8_t j = 16; j != 255; j--) {
layers[j].keys = (struct key *)malloc(total_keys * sizeof(struct key));
if (layers[j].keys == NULL) {
return ERR_OUT_OF_MEMORY_KEYS;
}
for (int i = 0; i < total_keys; i++) {
copy_key(&passthrough_key, &(layers[j].keys[i]));
for (uint8_t i = 0; i < total_keys; i++) {
layers[j].keys[i] = passthrough_key;
}
}
key_states = (bool *)malloc(total_keys * sizeof(bool));
Expand Down
19 changes: 6 additions & 13 deletions src/squirrel_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ enum squirrel_error press_key(uint8_t key_index) {
if (!layers[i].active) {
continue;
}
struct key *selected_key = &(layers[i].keys[key_index]);
struct key *selected_key = layers[i].keys[key_index];
enum squirrel_error err = selected_key->pressed(
selected_key, i, key_index, selected_key->pressed_argument_count,
selected_key->pressed_arguments);
selected_key, i, key_index, selected_key->pressed_argument);
if (err != ERR_NONE) {
return err;
}
if (i == 16) {
continue;
}
copy_key(selected_key, &layers[16].keys[key_index]);
copy_key(selected_key, layers[16].keys[key_index]);
}
return ERR_NONE;
}
Expand All @@ -35,10 +34,9 @@ enum squirrel_error release_key(uint8_t key_index) {
if (!layers[i].active) {
continue;
}
struct key *selected_key = &(layers[i].keys[key_index]);
struct key *selected_key = layers[i].keys[key_index];
enum squirrel_error err = selected_key->released(
selected_key, i, key_index, selected_key->released_argument_count,
selected_key->released_arguments);
selected_key, i, key_index, selected_key->released_argument);
if (err != ERR_NONE) {
return err;
}
Expand All @@ -47,13 +45,8 @@ enum squirrel_error release_key(uint8_t key_index) {
}
struct key passthrough_key;
passthrough_key.pressed = quantum_passthrough_press;
passthrough_key.pressed_argument_count = 0;
passthrough_key.pressed_arguments = NULL;
passthrough_key.released = quantum_passthrough_release;
passthrough_key.released_argument_count = 0;
passthrough_key.released_arguments = NULL;
copy_key(&passthrough_key, &layers[16].keys[key_index]);
layers[16].keys[0].pressed_argument_count = 0;
copy_key(&passthrough_key, layers[16].keys[key_index]);
}
return ERR_NONE;
}
Expand Down
68 changes: 18 additions & 50 deletions src/squirrel_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,62 @@ struct key nop(void) {
}

struct key keyboard(uint8_t keycode) {
struct key key = {
return (struct key){
.pressed = keyboard_press,
.released = keyboard_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &keycode,
.released_argument = &keycode,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &keycode;
key.released_arguments[0] = &keycode;
return key;
}

struct key keyboard_modifier(uint8_t modifier) {
struct key key = {
return (struct key){
.pressed = keyboard_modifier_press,
.released = keyboard_modifier_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &modifier,
.released_argument = &modifier,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &modifier;
key.released_arguments[0] = &modifier;
return key;
}

struct key consumer(uint16_t consumer) {
struct key key = {
return (struct key){
.pressed = consumer_press,
.released = consumer_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &consumer,
.released_argument = &consumer,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &consumer;
key.released_arguments[0] = &consumer;
return key;
}

struct key passthrough(void) {
return (struct key){
.pressed = quantum_passthrough_press,
.released = quantum_passthrough_release,
.pressed_argument_count = 0,
.released_argument_count = 0,
};
}

struct key layer_momentary(uint8_t layer) {
struct key key = {
return (struct key){
.pressed = layer_momentary_press,
.released = layer_momentary_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &layer,
.released_argument = &layer,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &layer;
key.released_arguments[0] = &layer;
return key;
}

struct key layer_toggle(uint8_t layer) {
struct key key = {
return (struct key){
.pressed = layer_toggle_press,
.released = layer_toggle_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &layer,
.released_argument = &layer,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &layer;
key.released_arguments[0] = &layer;
return key;
}

struct key layer_solo(uint8_t layer) {
struct key key = {
return (struct key){
.pressed = layer_solo_press,
.released = layer_solo_release,
.pressed_argument_count = 1,
.released_argument_count = 1,
.pressed_argument = &layer,
.released_argument = &layer,
};
key.pressed_arguments = malloc(1 * sizeof(void *));
key.released_arguments = malloc(1 * sizeof(void *));
key.pressed_arguments[0] = &layer;
key.released_arguments[0] = &layer;
return key;
}
Loading

0 comments on commit 7eab251

Please sign in to comment.