diff --git a/CMakeLists.txt b/CMakeLists.txt index 6326228..72e1c00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,10 +61,6 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "Testing") add_executable(keyboard_activate_deactivate_get_modifier tests/keyboard_activate_deactivate_get_modifier.c) target_link_libraries(keyboard_activate_deactivate_get_modifier squirrel) add_test(NAME keyboard_activate_deactivate_get_modifier COMMAND keyboard_activate_deactivate_get_modifier) - - add_executable(multikey tests/multikey.c) - target_link_libraries(multikey squirrel) - add_test(NAME multikey COMMAND multikey) else() add_compile_options(-Os) # Enable size optimizations endif() diff --git a/README.md b/README.md index 33bfe94..94d5484 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Tests the library ```bash cmake -DCMAKE_BUILD_TYPE=Testing .. make -j4 -ctest -T Test -T Coverage . +ctest -T Test -T Coverage --output-on-failure . ``` ### Clean @@ -57,14 +57,3 @@ Cleans the build directory for a fresh build. rm -rf ./build mkdir build ``` - -### Test -Directory: ./build - -Runs the unit tests. - -```bash -cmake -S. -DBUILD_TESTING=true .. -cmake --build . -ctest -``` diff --git a/include/squirrel.h b/include/squirrel.h index 8944a2a..251768f 100644 --- a/include/squirrel.h +++ b/include/squirrel.h @@ -5,7 +5,6 @@ enum squirrel_error { ERR_NONE = 0, - ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT, ERR_PASSTHROUGH_ON_BOTTOM_LAYER, ERR_OUT_OF_MEMORY_KEYS, ERR_OUT_OF_MEMORY_KEY_STATES, diff --git a/include/squirrel_key.h b/include/squirrel_key.h index 534ed62..9402116 100644 --- a/include/squirrel_key.h +++ b/include/squirrel_key.h @@ -11,6 +11,7 @@ struct key { uint8_t key_index, void *arg); // called when the key is released void *released_argument; // argument to pass to released + int debug; }; extern int key_count; // number of keys on the keyboard diff --git a/include/squirrel_quantum.h b/include/squirrel_quantum.h index e811948..3ea607d 100644 --- a/include/squirrel_quantum.h +++ b/include/squirrel_quantum.h @@ -7,8 +7,8 @@ #include struct layer { - bool active; // true if this layer is currently active - struct key *keys[]; // array of keys in this layer + 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, diff --git a/src/squirrel_init.c b/src/squirrel_init.c index 4d24c22..f5963c8 100644 --- a/src/squirrel_init.c +++ b/src/squirrel_init.c @@ -3,16 +3,20 @@ #include "squirrel_key.h" #include "squirrel_quantum.h" #include +#include #include #include enum squirrel_error squirrel_init(int total_keys) { key_count = total_keys; - struct key *passthrough_key = - &(struct key){.pressed = quantum_passthrough_press, - .released = quantum_passthrough_release}; - for (uint8_t j = 16; j != 255; j--) { - for (uint8_t i = 0; i < total_keys; i++) { + struct key passthrough_key = (struct key){ + .pressed = quantum_passthrough_press, + .released = quantum_passthrough_release, + }; + for (int j = 16; j >= 0; j--) { + layers[j].active = false; + layers[j].keys = (struct key *)malloc(total_keys * sizeof(struct key)); + for (int i = 0; i < total_keys; i++) { layers[j].keys[i] = passthrough_key; } } diff --git a/src/squirrel_key.c b/src/squirrel_key.c index b234937..aa40663 100644 --- a/src/squirrel_key.c +++ b/src/squirrel_key.c @@ -2,6 +2,7 @@ #include "squirrel.h" #include "squirrel_quantum.h" #include +#include #include int key_count = 0; // This should be overwritten by squirrel_init @@ -11,32 +12,32 @@ void copy_key(struct key *source, struct key *destination) { } enum squirrel_error press_key(uint8_t key_index) { - for (uint8_t i = 16; i != 255; i--) { + for (int i = 16; i >= 0; i--) { if (!layers[i].active) { continue; } - 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); + 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); 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; } enum squirrel_error release_key(uint8_t key_index) { - for (uint8_t i = 16; i != 255; i--) { + for (int i = 16; i >= 0; i--) { if (!layers[i].active) { continue; } - 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); + 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); if (err != ERR_NONE) { return err; } @@ -46,7 +47,7 @@ enum squirrel_error release_key(uint8_t key_index) { struct key passthrough_key; passthrough_key.pressed = quantum_passthrough_press; passthrough_key.released = quantum_passthrough_release; - copy_key(&passthrough_key, layers[16].keys[key_index]); + copy_key(&passthrough_key, &layers[16].keys[key_index]); } return ERR_NONE; } diff --git a/src/squirrel_quantum.c b/src/squirrel_quantum.c index 19bef10..c2bdb61 100644 --- a/src/squirrel_quantum.c +++ b/src/squirrel_quantum.c @@ -4,9 +4,10 @@ #include "squirrel_key.h" #include "squirrel_keyboard.h" #include +#include #include -struct layer layers[17]; +struct layer layers[17] = {}; enum squirrel_error key_nop(struct key *key, uint8_t layer, uint8_t key_index, void *arg) { @@ -69,20 +70,20 @@ enum squirrel_error quantum_passthrough_press(struct key *key, uint8_t layer, if (layer == 0) { return ERR_PASSTHROUGH_ON_BOTTOM_LAYER; }; - for (uint8_t i = layer - 1; i != 255; i--) { + for (int i = layer - 1; i >= 0; i--) { if (!layers[i].active) { continue; } - 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); + 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); 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; } @@ -93,13 +94,13 @@ enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer, if (layer == 0) { return ERR_PASSTHROUGH_ON_BOTTOM_LAYER; }; - for (uint8_t i = layer - 1; i != 255; i--) { + for (int i = layer - 1; i >= 0; i--) { if (!layers[i].active) { continue; } - 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); + 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); if (err != ERR_NONE) { return err; } @@ -109,7 +110,7 @@ enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer, struct key passthrough_key; passthrough_key.pressed = quantum_passthrough_press; passthrough_key.released = quantum_passthrough_release; - copy_key(&passthrough_key, layers[16].keys[key_index]); + copy_key(&passthrough_key, &layers[16].keys[key_index]); } return ERR_NONE; } diff --git a/tests/consumer_press_release.c b/tests/consumer_press_release.c index 90ef875..b0156f2 100644 --- a/tests/consumer_press_release.c +++ b/tests/consumer_press_release.c @@ -3,74 +3,104 @@ #include "squirrel_key.h" #include "squirrel_quantum.h" #include +#include #include // test: consumer_press + consumer_release - in squirrel_quantum.c int main() { struct key test_key; // values unused enum squirrel_error err; - void **args = malloc(sizeof(void *)); - for (uint16_t test_consumer_code = 0; test_consumer_code <= 65534; + for (uint16_t test_consumer_code = 0; test_consumer_code != 0xFFFF; test_consumer_code++) { - args[0] = &test_consumer_code; // consumer_press // no code becomes a code consumer_code = 0; - err = consumer_press(&test_key, 0, 0, 1, args); + err = consumer_press(&test_key, 0, 0, &test_consumer_code); if (err != ERR_NONE) { + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("err from consumer_press in test 1: %d\n", err); return 1; } if (consumer_code != test_consumer_code) { - return 2; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("consumer_code not equal to test_consumer_code in consumer_press " + "test 1: " + "%d\n", + consumer_code); + return 1; } // a code stays a code - err = consumer_press(&test_key, 0, 0, 1, args); + err = consumer_press(&test_key, 0, 0, &test_consumer_code); if (err != ERR_NONE) { - return 3; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("err from consumer_press in test 2: %d\n", err); + return 1; } if (consumer_code != test_consumer_code) { - return 4; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("consumer_code not equal to test_consumer_code in consumer_press " + "test 2: " + "%d\n", + consumer_code); + return 1; } // another code becomes a code consumer_code = 0xFFFF; - err = consumer_press(&test_key, 0, 0, 1, args); + err = consumer_press(&test_key, 0, 0, &test_consumer_code); if (err != ERR_NONE) { - return 5; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("err from consumer_press in test 3: %d\n", err); + return 1; } if (consumer_code != test_consumer_code) { - return 6; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("consumer_code not equal to test_consumer_code in consumer_press " + "test 3: " + "%d\n", + consumer_code); + return 1; } // consumer_release // a code becomes no code consumer_code = test_consumer_code; - err = consumer_release(&test_key, 0, 0, 1, args); + err = consumer_release(&test_key, 0, 0, &test_consumer_code); if (err != ERR_NONE) { - return 7; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("err from consumer_release in test 1: %d\n", err); + return 1; } if (consumer_code != 0) { - return 8; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("consumer_code not equal to 0 in consumer_release test 1: %d\n", + consumer_code); + return 1; } // another code stays another code consumer_code = 0xFFFF; - err = consumer_release(&test_key, 0, 0, 1, args); + err = consumer_release(&test_key, 0, 0, &test_consumer_code); if (err != ERR_NONE) { - return 9; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("err from consumer_release in test 2: %d\n", err); + return 1; } if (consumer_code != 0xFFFF) { - return 10; + printf("err while testing with test_consumer_code %d,\n", + test_consumer_code); + printf("consumer_code not equal to 0xFFFF in consumer_release test 2: " + "%d\n", + consumer_code); + return 1; } } - - // Test expected errors - err = consumer_press(&test_key, 0, 0, 0, 0); - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 11; - } - err = consumer_release(&test_key, 0, 0, 0, 0); - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 12; - } - return 0; }; diff --git a/tests/key.c b/tests/key.c index 8b43be7..163e853 100644 --- a/tests/key.c +++ b/tests/key.c @@ -8,33 +8,22 @@ uint8_t test_result = 1; enum squirrel_error test_press(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, void **args) { + uint8_t key_index, void *arg) { (void)layer; (void)key_index; - - if (arg_count != 2) { - return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; - }; - uint8_t code1 = *(uint8_t *)args[0]; - uint8_t code2 = *(uint8_t *)args[1]; - if (code1 == 0x0F && code2 == 0xF0) { + uint8_t code = *(uint8_t *)arg; + if (code == 0xF0) { test_result = 0; } return ERR_NONE; } enum squirrel_error test_release(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, - void **args) { + uint8_t key_index, void *arg) { (void)layer; (void)key_index; - - if (arg_count != 2) { - return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; - }; - uint8_t code1 = *(uint8_t *)args[0]; - uint8_t code2 = *(uint8_t *)args[1]; - if (code1 == 0xF0 && code2 == 0x0F) { + uint8_t code = *(uint8_t *)arg; + if (code == 0xF0) { test_result = 0; } return ERR_NONE; @@ -45,23 +34,14 @@ int main() { squirrel_init(1); // press_key + release_key - uint8_t code1 = 0x0F; - uint8_t code2 = 0xF0; + uint8_t code = 0xF0; struct key testkey; testkey.pressed = test_press; - testkey.pressed_argument_count = 2; - testkey.pressed_arguments = malloc(2 * sizeof(void *)); - testkey.pressed_arguments[0] = &code1; - testkey.pressed_arguments[1] = &code2; - + testkey.pressed_argument = &code; testkey.released = test_release; - testkey.released_argument_count = 2; - testkey.released_arguments = malloc(2 * sizeof(void *)); - testkey.released_arguments[0] = &code2; - testkey.released_arguments[1] = &code1; - - layers[0].keys[0] = &testkey; + testkey.released_argument = &code; + copy_key(&testkey, &layers[0].keys[0]); layers[0].active = true; // check that arguments are correct, and in the correct order. @@ -73,10 +53,10 @@ int main() { return 3; } // keys are copied to layer 17 (index 16) when pressed, to avoid layer issues. - if (layers[16].keys[0]->pressed_arguments[0] != &code1) { + if (layers[16].keys[0].pressed_argument != &code) { return 4; } - if (layers[16].keys[0]->pressed_arguments[1] != &code2) { + if (layers[16].keys[0].pressed_argument != &code) { return 5; } @@ -89,10 +69,10 @@ int main() { return 7; } // Keys are replaced with passthrough on layer 17 when released. - if (layers[16].keys[0]->pressed_arguments != NULL) { + if (layers[16].keys[0].pressed_argument != NULL) { return 8; } - if (layers[16].keys[0]->released_arguments != NULL) { + if (layers[16].keys[0].released_argument != NULL) { return 9; } @@ -149,8 +129,5 @@ int main() { return 21; } - free(testkey.pressed_arguments); - free(testkey.released_arguments); - return 0; } diff --git a/tests/keyboard_modifier_press_release.c b/tests/keyboard_modifier_press_release.c index 389775c..5ab142a 100644 --- a/tests/keyboard_modifier_press_release.c +++ b/tests/keyboard_modifier_press_release.c @@ -8,14 +8,13 @@ // squirrel_quantum.c int main() { struct key test_key; // values unused - void **args = malloc(sizeof(void *)); // keyboard_modifier_press // no modifiers adding no modifiers is no modifiers uint8_t current_modifier = 0b00000000; keyboard_modifiers = 0; - args[0] = ¤t_modifier; - enum squirrel_error err = keyboard_modifier_press(&test_key, 0, 0, 1, args); + enum squirrel_error err = + keyboard_modifier_press(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -27,8 +26,8 @@ int main() { current_modifier = 0b00000001; for (uint8_t i = 0; i < 8; i++) { keyboard_modifiers = 0; - args[0] = ¤t_modifier; - enum squirrel_error err = keyboard_modifier_press(&test_key, 0, 0, 1, args); + enum squirrel_error err = + keyboard_modifier_press(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -43,8 +42,8 @@ int main() { for (uint8_t i = 0; i < 8; i++) { // modifiers stack, ORd to become the same number uint8_t old_modifiers = keyboard_modifiers; - args[0] = ¤t_modifier; - enum squirrel_error err = keyboard_modifier_press(&test_key, 0, 0, 1, args); + enum squirrel_error err = + keyboard_modifier_press(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -54,24 +53,12 @@ int main() { current_modifier = current_modifier << 1; } - // checking expected errors: - // incorrect arguments error. - err = keyboard_modifier_press(&test_key, 0, 0, 2, 0); // too many arguments - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 254; - } - err = keyboard_modifier_press(&test_key, 0, 0, 0, 0); // too few arguments - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 254; - } - // keyboard_modifier_release // no modifiers removing no modifiers is no modifiers current_modifier = 0b00000000; keyboard_modifiers = 0; - args[0] = ¤t_modifier; - err = keyboard_modifier_release(&test_key, 0, 0, 1, args); + err = keyboard_modifier_release(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -83,9 +70,8 @@ int main() { current_modifier = 0b00000001; for (uint8_t i = 0; i < 8; i++) { keyboard_modifiers = current_modifier; - args[0] = ¤t_modifier; enum squirrel_error err = - keyboard_modifier_release(&test_key, 0, 0, 1, args); + keyboard_modifier_release(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -101,9 +87,8 @@ int main() { for (uint8_t i = 0; i < 8; i++) { // modifiers stack, ORd to become the same number uint8_t old_modifiers = keyboard_modifiers; - args[0] = ¤t_modifier; enum squirrel_error err = - keyboard_modifier_release(&test_key, 0, 0, 1, args); + keyboard_modifier_release(&test_key, 0, 0, ¤t_modifier); if (err != ERR_NONE) { return 255; } @@ -113,18 +98,5 @@ int main() { current_modifier = current_modifier << 1; } - free(args); - - // checking expected errors: - // incorrect arguments error. - err = keyboard_modifier_release(&test_key, 0, 0, 2, 0); // too many arguments - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 254; - } - err = keyboard_modifier_release(&test_key, 0, 0, 0, 0); // too few arguments - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 254; - } - return 0; }; diff --git a/tests/keyboard_press_release.c b/tests/keyboard_press_release.c index 236a279..8c020bc 100644 --- a/tests/keyboard_press_release.c +++ b/tests/keyboard_press_release.c @@ -8,13 +8,11 @@ int main() { struct key test_key; // values unused enum squirrel_error err; - void **args = malloc(sizeof(void *)); for (uint8_t keycode = 0; keycode != 255; keycode++) { - args[0] = &keycode; // keyboard_press // Off becomes on keyboard_keycodes[keycode] = false; - err = keyboard_press(&test_key, 0, 0, 1, args); + err = keyboard_press(&test_key, 0, 0, &keycode); if (err != ERR_NONE) { return 1; } @@ -22,7 +20,7 @@ int main() { return 2; } // On stays on - err = keyboard_press(&test_key, 0, 0, 1, args); + err = keyboard_press(&test_key, 0, 0, &keycode); if (err != ERR_NONE) { return 3; } @@ -33,7 +31,7 @@ int main() { // keyboard_release // On becomes off keyboard_keycodes[keycode] = true; - err = keyboard_release(&test_key, 0, 0, 1, args); + err = keyboard_release(&test_key, 0, 0, &keycode); if (err != ERR_NONE) { return 5; } @@ -41,7 +39,7 @@ int main() { return 6; } // Off stays off - err = keyboard_release(&test_key, 0, 0, 1, args); + err = keyboard_release(&test_key, 0, 0, &keycode); if (err != ERR_NONE) { return 7; } @@ -49,24 +47,5 @@ int main() { return 8; } } - free(args); - - // Test expected errors - err = consumer_press(&test_key, 0, 0, 0, 0); // too few - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 9; - } - err = consumer_press(&test_key, 0, 0, 2, 0); // too many - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 10; - } - err = consumer_release(&test_key, 0, 0, 0, 0); // too few - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 10; - } - err = consumer_release(&test_key, 0, 0, 2, 0); // too many - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 10; - } return 0; }; diff --git a/tests/layer_press_release.c b/tests/layer_press_release.c index c7122da..e11c0d1 100644 --- a/tests/layer_press_release.c +++ b/tests/layer_press_release.c @@ -3,7 +3,7 @@ #include "squirrel_key.h" #include "squirrel_quantum.h" #include -#include +#include // test: // layer_momentary_press @@ -18,53 +18,27 @@ int main() { enum squirrel_error err; struct key test_key; - // Test expected errors - err = layer_momentary_press(&test_key, 0, 0, 0, 0); // too few - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 1; - } - err = layer_momentary_release(&test_key, 0, 0, 2, 0); // too many - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 2; - } - err = layer_toggle_press(&test_key, 0, 0, 0, 0); // too few - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 3; - } - err = layer_toggle_release(&test_key, 0, 0, 2, 0); // too many - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 4; - } - err = layer_solo_press(&test_key, 0, 0, 0, 0); // too few - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 5; - } - err = layer_solo_release(&test_key, 0, 0, 2, 0); // too many - if (err != ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT) { - return 6; - } - - void **args = malloc(sizeof(void *)); uint8_t target_layer = 1; - args[0] = &target_layer; // layer_momentary - layer_momentary_press(&test_key, 0, 0, 1, - args); // should activate target_layer only + layer_momentary_press(&test_key, 0, 0, &target_layer); for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. - return 7; + printf("err while testing with target_layer %d,\n", target_layer); + printf("layer unexpectedly active: %d\n", i); + return 1; } } if (!layers[target_layer].active) { // if target_layer is not active, fail. return 8; } + for (uint8_t i = 0; i < 16; i++) { // activate all layers layers[i].active = true; } - layer_momentary_release(&test_key, 0, 0, 1, - args); // should deactivate target_layer only + layer_momentary_release(&test_key, 0, 0, + &target_layer); // should deactivate target_layer only for (uint8_t i = 0; i < 16; i++) { if (!layers[i].active && i != target_layer) { // if any other layers are not active, fail. @@ -79,7 +53,8 @@ int main() { } // layer_toggle - layer_toggle_press(&test_key, 0, 0, 1, args); // should activate target_layer + layer_toggle_press(&test_key, 0, 0, + &target_layer); // should activate target_layer for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. @@ -89,7 +64,7 @@ int main() { if (!layers[target_layer].active) { // if target_layer is not active, fail. return 12; } - layer_toggle_release(&test_key, 0, 0, 1, args); // should not deactivate + layer_toggle_release(&test_key, 0, 0, &target_layer); // should not deactivate for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. @@ -99,8 +74,8 @@ int main() { if (!layers[target_layer].active) { // if target_layer is not active, fail. return 14; } - layer_toggle_press(&test_key, 0, 0, 1, - args); // should deactivate target_layer + layer_toggle_press(&test_key, 0, 0, + &target_layer); // should deactivate target_layer for (uint8_t i = 0; i < 16; i++) { // if any other layers are active, fail. if (layers[i].active && i != target_layer) { return 15; @@ -114,8 +89,9 @@ int main() { for (uint8_t i = 0; i < 16; i++) { // turn on all layers layers[i].active = true; } - layer_solo_press(&test_key, 0, 0, 1, - args); // solo should turn off all layers except target_layer + layer_solo_press( + &test_key, 0, 0, + &target_layer); // solo should turn off all layers except target_layer for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. @@ -126,8 +102,8 @@ int main() { return 18; } layer_solo_press( - &test_key, 0, 0, 1, - args); // solo should not turn off target_layer if called again + &test_key, 0, 0, + &target_layer); // solo should not turn off target_layer if called again for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. @@ -137,8 +113,8 @@ int main() { if (!layers[target_layer].active) { // if target_layer is not active, fail. return 20; } - layer_solo_release(&test_key, 0, 0, 1, - args); // release should not do anything + layer_solo_release(&test_key, 0, 0, + &target_layer); // release should not do anything for (uint8_t i = 0; i < 16; i++) { if (layers[i].active && i != target_layer) { // if any other layers are active, fail. @@ -148,7 +124,6 @@ int main() { if (!layers[target_layer].active) { // if target_layer is not active, fail. return 22; } - free(args); return 0; }; diff --git a/tests/multikey.c b/tests/multikey.c deleted file mode 100644 index 44ea5e2..0000000 --- a/tests/multikey.c +++ /dev/null @@ -1,171 +0,0 @@ -#include "squirrel.h" -#include "squirrel_init.h" -#include "squirrel_key.h" -#include "squirrel_quantum.h" -#include -#include - -uint8_t test_result = 1; - -enum squirrel_error test_press(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, void **args) { - (void)layer; - (void)key_index; - - if (arg_count != 2) { - return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; - }; - uint8_t code1 = *(uint8_t *)args[0]; - uint8_t code2 = *(uint8_t *)args[1]; - if (code1 == 0x0F && code2 == 0xF0) { - test_result = 0; - } - return ERR_NONE; -} - -enum squirrel_error test_release(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, - void **args) { - (void)layer; - (void)key_index; - - if (arg_count != 2) { - return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT; - }; - uint8_t code1 = *(uint8_t *)args[0]; - uint8_t code2 = *(uint8_t *)args[1]; - if (code1 == 0xF0 && code2 == 0x0F) { - test_result = 0; - } - return ERR_NONE; -} - -uint8_t code1 = 0x0F; -uint8_t code2 = 0xF0; - -void make_testkey(int index) { - // press_key + release_key - struct key testkey; - testkey.pressed = test_press; - testkey.pressed_argument_count = 2; - testkey.pressed_arguments = malloc(2 * sizeof(void *)); - testkey.pressed_arguments[0] = &code1; - testkey.pressed_arguments[1] = &code2; - - testkey.released = test_release; - testkey.released_argument_count = 2; - testkey.released_arguments = malloc(2 * sizeof(void *)); - testkey.released_arguments[0] = &code2; - testkey.released_arguments[1] = &code1; - - layers[0].keys[index] = &testkey; -}; - -// test: press_key, release_key + check_key - in squirrel_key.c, -// but with multiple keys in a keyboard. -int main() { - squirrel_init(5); - make_testkey(0); - make_testkey(1); - make_testkey(2); - make_testkey(3); - make_testkey(4); - layers[0].active = true; - - for (int i = 0; i < 5; i++) { - // check that arguments are correct, and in the correct order. - enum squirrel_error err = press_key(i); - if (err != ERR_NONE) { - return 2; - } - return 0; - if (test_result != 0) { - return 3; - } - // keys are copied to layer 17 (index 16) when pressed, to avoid layer - // issues. - if (layers[16].keys[i]->pressed_arguments[0] != &code1) { - return 4; - } - if (layers[16].keys[i]->pressed_arguments[1] != &code2) { - return 5; - } - - test_result = 1; - err = release_key(i); - if (err != ERR_NONE) { - return 6; - } - if (test_result != 0) { - return 7; - } - // Keys are replaced with passthrough on layer 17 when released. - if (layers[16].keys[i]->pressed_arguments != NULL) { - return 8; - } - if (layers[16].keys[i]->released_arguments != NULL) { - return 9; - } - - // check_key - test_result = 1; - key_states[i] = false; - err = check_key(i, true); // should call press_key - if (err != ERR_NONE) { - return 10; - } - if (test_result != 0) { - return 11; - } - if (key_states[i] != true) { - return 12; - } - - test_result = 1; - key_states[i] = true; - err = check_key(i, false); // should call release_key - if (err != ERR_NONE) { - return 13; - } - if (test_result != 0) { - return 14; - } - if (key_states[i] != false) { - return 15; - } - - test_result = 1; - key_states[i] = true; - err = check_key(i, true); // should not call press_key - if (err != ERR_NONE) { - return 16; - } - if (test_result != 1) { - return 17; - } - if (key_states[i] != true) { - return 18; - } - - test_result = 1; - key_states[i] = false; - err = check_key(i, false); // should not call release_key - if (err != ERR_NONE) { - return 19; - } - if (test_result != 1) { - return 20; - } - if (key_states[i] != false) { - return 21; - } - }; - - // cleanup - for (int i = 0; i < 5; i++) { - free(layers[0].keys[i]->pressed_arguments); - free(layers[0].keys[i]->released_arguments); - } - - return 0; -} diff --git a/tests/quantum_passthrough_press_release.c b/tests/quantum_passthrough_press_release.c index f888580..f0a853c 100644 --- a/tests/quantum_passthrough_press_release.c +++ b/tests/quantum_passthrough_press_release.c @@ -8,28 +8,25 @@ uint8_t test_result = 1; // 0 = pass, 1 = fail bool bad_test = false; // true = fail enum squirrel_error test_press(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, void **args) { + uint8_t key_index, void *arg) { test_result = 0; return ERR_NONE; } enum squirrel_error test_release(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, - void **args) { + uint8_t key_index, void *arg) { test_result = 0; return ERR_NONE; } enum squirrel_error bad_test_press(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, - void **args) { + uint8_t key_index, void *arg) { bad_test = true; return ERR_NONE; } enum squirrel_error bad_test_release(struct key *key, uint8_t layer, - uint8_t key_index, int arg_count, - void **args) { + uint8_t key_index, void *arg) { bad_test = true; return ERR_NONE; } @@ -41,17 +38,13 @@ int main() { struct key testkey; testkey.pressed = test_press; - testkey.pressed_argument_count = 0; testkey.released = test_release; - testkey.released_argument_count = 0; - layers[0].keys[0] = &testkey; // When testkey is pressed, the test is passing. + layers[0].keys[0] = testkey; // When testkey is pressed, the test is passing. struct key passthroughkey; passthroughkey.pressed = quantum_passthrough_press; - passthroughkey.pressed_argument_count = 0; passthroughkey.released = quantum_passthrough_release; - passthroughkey.released_argument_count = 0; - layers[1].keys[0] = &passthroughkey; // This is the key being tested. + layers[1].keys[0] = passthroughkey; // This is the key being tested. layers[0].active = true; layers[1].active = true; @@ -80,15 +73,13 @@ int main() { false; // this layer contains the badtestkey and should be ignored. layers[2].active = true; // this layer contains the passthrough key. - layers[2].keys[0] = &passthroughkey; + layers[2].keys[0] = passthroughkey; struct key badtestkey; badtestkey.pressed = bad_test_press; - badtestkey.pressed_argument_count = 0; badtestkey.released = bad_test_release; - badtestkey.released_argument_count = 0; - layers[1].keys[0] = &badtestkey; // When badtestkey is pressed, the test is - // failing. + layers[1].keys[0] = badtestkey; // When badtestkey is pressed, the test is + // failing. test_result = 1; // Reset the test result to failing again. err = press_key(0); // Press the key.