Skip to content

Commit

Permalink
fix: keyboard_press_release fails
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Aug 18, 2024
1 parent ca9f0c2 commit a66fba6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 81 deletions.
62 changes: 35 additions & 27 deletions include/squirrel_quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,42 @@ struct layer {
bool active; // true if this layer is currently active
};

// layers is a list of all the layers in the keyboard. 0-15 are to be used by
// the user. Don't modify layer 16 unless you know what you're doing :).
// layers is a list of all the layers in the keyboard. 0-15 are configured,
// layer 16 is used for held keys and should only be modified by SQUIRREL.
extern struct layer layers[17];

// key_nop is a function that does nothing, can be used as a placeholder.
void key_nop(struct key *key, ...);

// keyboard_press is a function that activates a keycode on the keyboard.
void keyboard_press(struct key *key, ...);

// keyboard_release is a function that deactivates a keycode on the keyboard.
void keyboard_release(struct key *key, uint8_t keycode);

// keyboard_modifier_press is a function that activates a modifier on the
// keyboard.
void keyboard_modifier_press(struct key *key, uint8_t modifier);

// keyboard_modifier_release is a function that deactivates a modifier on the
// keyboard.
void keyboard_modifier_release(struct key *key, uint8_t modifier);

// quantum_passthrough is a function that passes the keypress to the next layer
void quantum_passthrough_press(struct key *key, uint8_t layer,
uint8_t key_index);

// quantum_passthrough_release is a function that passes the keyrelease to the
// next layer
void quantum_passthrough_release(struct key *key, uint8_t layer,
uint8_t key_index);
// key_nop does nothing (no operation)
enum squirrel_error key_nop(struct key *key, uint8_t layer, uint8_t key_index,
int arg_count, ...);

// 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, ...);

// 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, ...);

// 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,
...);

// 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,
...);

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

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

#endif
57 changes: 36 additions & 21 deletions src/squirrel_quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
#include <stdarg.h>
#include <stdint.h>

enum squirrel_error key_nop(struct key *key, int arg_count, ...) {
enum squirrel_error key_nop(struct key *key, uint8_t layer, uint8_t key_index,
int arg_count, ...) {
(void)key;
(void)arg_count;
return ERR_NONE;
}

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

va_list args;
va_start(args, arg_count);
if (arg_count != 1) {
Expand All @@ -22,8 +27,11 @@ enum squirrel_error keyboard_press(struct key *key, int arg_count, ...) {
return ERR_NONE;
};

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

va_list args;
va_start(args, arg_count);
if (arg_count != 1) {
Expand All @@ -35,9 +43,12 @@ enum squirrel_error keyboard_release(struct key *key, int arg_count, ...) {
return ERR_NONE;
}

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

va_list args;
va_start(args, arg_count);
if (arg_count != 1) {
Expand All @@ -49,8 +60,12 @@ enum squirrel_error keyboard_modifier_press(struct key *key, int arg_count,
return ERR_NONE;
}

enum squirrel_error keyboard_modifier_release(struct key *key, int arg_count,
enum squirrel_error keyboard_modifier_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
...) {
(void)layer;
(void)key_index;

va_list args;
va_start(args, arg_count);
if (arg_count != 1) {
Expand All @@ -62,36 +77,36 @@ enum squirrel_error keyboard_modifier_release(struct key *key, int arg_count,
return ERR_NONE;
}

// 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,
// quantum_passthrough_press does not take extra arguments.
enum squirrel_error quantum_passthrough_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
...) {
va_list args;
va_start(args, arg_count);
if (arg_count != 1) {
if (arg_count != 0) {
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;
}
struct key next_key = layers[i].keys[key_index];
next_key.pressed(&next_key, next_key.pressed_arguments);
}
return ERR_NONE;
}

enum squirrel_error quantum_passthrough_release(struct key *key, int arg_count,
...) {
// quantum_passthrough_release does not take extra arguments.
enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer,
uint8_t key_index,
int arg_count, ...) {
if (arg_count != 0) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
for (int i = layer; i >= 0; i--) {
if (!layers[i].active) {
break;
}
struct key next_key = layers[i].keys[key_index];
next_key.released(&next_key, next_key.released_arguments);
}
return ERR_NONE;
}
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ enable_testing()

include_directories(../include)

add_executable(keyboard_press_release keyboard_press_release.c ../src/squirrel_keyboard.c)
add_executable(keyboard_press_release keyboard_press_release.c ../src/squirrel_keyboard.c ../src/squirrel_quantum.c)
target_link_libraries(keyboard_press_release squirrel)
add_test(NAME keyboard_press_release COMMAND keyboard_press_release)
49 changes: 17 additions & 32 deletions tests/keyboard_press_release.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
#include "squirrel.h"
#include "squirrel_keyboard.h"
#include "squirrel_quantum.h"

// test: keyboard_press + keyboard_release end-to-end test in squirrel_quantum.c
int main() {
// keyboard_press
struct key test_key;
for (uint8_t i = 0; i <= 255; i++) {
// FALSE becomes TRUE
for (uint8_t j = 0; j <= 255; j++) {
keycodes[j] = false;
}
keyboard_press(&test_key, i);
if (keycodes[i] == false) {
struct key test_key; // values unused
for (uint8_t keycode; keycode <= 127; keycode++) {
// Off becomes on
keycodes[keycode] = false;
keyboard_press(&test_key, 0, 0, 1, keycode);
if (keycodes[keycode] != true) {
return 1;
}

// TRUE stays TRUE
for (uint8_t j = 0; j <= 255; j++) {
keycodes[j] = true;
}
keyboard_press(&test_key, i);
if (keycodes[i] == false) {
// On stays on
keyboard_press(&test_key, 0, 0, 1, keycode);
if (keycodes[keycode] != true) {
return 2;
}
};

// keyboard_release
for (uint8_t i = 0; i <= 255; i++) {
// TRUE becomes FALSE
for (uint8_t j = 0; j <= 255; j++) {
keycodes[j] = true;
}
keyboard_release(&test_key, i);
if (keycodes[i] == true) {
// On becomes off
keycodes[keycode] = true;
keyboard_release(&test_key, 0, 0, 1, keycode);
if (keycodes[keycode] != false) {
return 3;
}
// FALSE stays FALSE
for (uint8_t j = 0; j <= 255; j++) {
keycodes[j] = false;
}
keyboard_release(&test_key, i);
if (keycodes[i] == true) {
// Off stays off
keyboard_release(&test_key, 0, 0, 1, keycode);
if (keycodes[keycode] != false) {
return 4;
}
};
}
return 0;
};

0 comments on commit a66fba6

Please sign in to comment.