Skip to content

Commit

Permalink
feat: test copy of key to layer 16, rename init_keyboard to squirrel_…
Browse files Browse the repository at this point in the history
…init
  • Loading branch information
headblockhead committed Aug 26, 2024
1 parent b3a3d28 commit 02658c3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "Testing")
include(CodeCoverage)
APPEND_COVERAGE_COMPILER_FLAGS()
setup_target_for_coverage_gcovr_html(squirrel squirrel_test coverage)
list(APPEND GCOVR_EXCLUDES "tests/")
endif()

add_executable(keyboard_press_release tests/keyboard_press_release.c)
Expand Down
4 changes: 2 additions & 2 deletions include/squirrel_init.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SQUIRREL_INIT_H
#define SQUIRREL_INIT_H
enum squirrel_error init_keyboard(
enum squirrel_error squirrel_init(
int total_keys); // Initialize the keyboard with the total number of keys.
enum squirrel_error
deinit_keyboard(); // Deinitialize the keyboard. Frees memory.
squirrel_deinit(); // Deinitialize the keyboard. Frees memory.
#endif
5 changes: 5 additions & 0 deletions src/squirrel.c
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#include "squirrel.h"
#include "squirrel_consumer.h"
#include "squirrel_init.h"
#include "squirrel_key.h"
#include "squirrel_keyboard.h"
#include "squirrel_quantum.h"
10 changes: 8 additions & 2 deletions src/squirrel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdlib.h>
#include <string.h>

enum squirrel_error init_keyboard(int total_keys) {
enum squirrel_error squirrel_init(int total_keys) {
key_count = total_keys;
struct key passthrough_key;
passthrough_key.pressed = quantum_passthrough_press;
Expand All @@ -20,13 +20,19 @@ enum squirrel_error init_keyboard(int total_keys) {
}
}
key_states = (bool *)malloc(total_keys * sizeof(bool));
layers[16].active = true;
return ERR_NONE;
};

enum squirrel_error deinit_keyboard() {
enum squirrel_error squirrel_deinit() {
for (uint8_t j = 16; j != 255; j--) {
free(layers[j].keys);
}
free(key_states);
// NULL the pointers to prevent use after free
for (uint8_t j = 16; j != 255; j--) {
layers[j].keys = NULL;
}
key_states = NULL;
return ERR_NONE;
};
6 changes: 5 additions & 1 deletion src/squirrel_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include "squirrel.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdlib.h>

int key_count = 0; // This should be overwritten by init_keyboard
int key_count = 0; // This should be overwritten by squirrel_init

void copy_key(struct key *source, struct key *destination) {
*destination = *source;
Expand Down Expand Up @@ -47,9 +48,12 @@ 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;
}
return ERR_NONE;
}
Expand Down
44 changes: 29 additions & 15 deletions tests/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum squirrel_error test_release(struct key *key, uint8_t layer,

// test: press_key, release_key + check_key - in squirrel_key.c
int main() {
init_keyboard(1);
squirrel_init(1);

// press_key + release_key
uint8_t code1 = 0x0F;
Expand Down Expand Up @@ -72,67 +72,81 @@ int main() {
if (test_result != 0) {
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) {
return 4;
}
if (layers[16].keys[0].pressed_arguments[1] != &code2) {
return 5;
}

test_result = 1;
err = release_key(0);
if (err != ERR_NONE) {
return 4;
return 6;
}
if (test_result != 0) {
return 5;
return 7;
}
// Keys are replaced with passthrough on layer 17 when released.
if (layers[16].keys[0].pressed_arguments != NULL) {
return 8;
}
if (layers[16].keys[0].released_arguments != NULL) {
return 9;
}

// check_key
test_result = 1;
key_states[0] = false;
err = check_key(0, true); // should call press_key
if (err != ERR_NONE) {
return 6;
return 10;
}
if (test_result != 0) {
return 7;
return 11;
}
if (key_states[0] != true) {
return 8;
return 12;
}

test_result = 1;
key_states[0] = true;
err = check_key(0, false); // should call release_key
if (err != ERR_NONE) {
return 9;
return 13;
}
if (test_result != 0) {
return 10;
return 14;
}
if (key_states[0] != false) {
return 11;
return 15;
}

test_result = 1;
key_states[0] = true;
err = check_key(0, true); // should not call press_key
if (err != ERR_NONE) {
return 12;
return 16;
}
if (test_result != 1) {
return 13;
return 17;
}
if (key_states[0] != true) {
return 14;
return 18;
}

test_result = 1;
key_states[0] = false;
err = check_key(0, false); // should not call release_key
if (err != ERR_NONE) {
return 15;
return 19;
}
if (test_result != 1) {
return 16;
return 20;
}
if (key_states[0] != false) {
return 17;
return 21;
}

free(testkey.pressed_arguments);
Expand Down
2 changes: 1 addition & 1 deletion tests/layer_press_release.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// layer_solo_release
// in squirrel_quantum.c
int main() {
init_keyboard(1);
squirrel_init(1);
enum squirrel_error err;
struct key test_key;

Expand Down
2 changes: 1 addition & 1 deletion tests/quantum_passthrough_press_release.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum squirrel_error bad_test_release(struct key *key, uint8_t layer,
// test: quantum_passthrough_press + quantum_passthrough_release test - in
// squirrel_quantum.c
int main() {
init_keyboard(1);
squirrel_init(1);

struct key testkey;
testkey.pressed = test_press;
Expand Down

0 comments on commit 02658c3

Please sign in to comment.