Skip to content

Commit

Permalink
fix: multikey test
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Sep 4, 2024
1 parent 27ce6a8 commit f72e57a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
4 changes: 3 additions & 1 deletion include/squirrel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
enum squirrel_error {
ERR_NONE = 0,
ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT,
ERR_PASSTHROUGH_ON_BOTTOM_LAYER
ERR_PASSTHROUGH_ON_BOTTOM_LAYER,
ERR_OUT_OF_MEMORY_KEYS,
ERR_OUT_OF_MEMORY_KEY_STATES,
};

#endif
2 changes: 0 additions & 2 deletions include/squirrel_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
#define SQUIRREL_INIT_H
enum squirrel_error squirrel_init(
int total_keys); // Initialize the keyboard with the total number of keys.
enum squirrel_error
squirrel_deinit(); // Deinitialize the keyboard. Frees memory.
#endif
21 changes: 8 additions & 13 deletions src/squirrel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,23 @@ enum squirrel_error squirrel_init(int 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;
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]));
}
}
key_states = (bool *)malloc(total_keys * sizeof(bool));
layers[16].active = true;
return ERR_NONE;
};

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;
if (key_states == NULL) {
return ERR_OUT_OF_MEMORY_KEY_STATES;
}
key_states = NULL;
layers[16].active = true;
return ERR_NONE;
};
1 change: 1 addition & 0 deletions src/squirrel_quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "squirrel_key.h"
#include "squirrel_keyboard.h"
#include <stdint.h>
#include <stdlib.h>

struct layer layers[17];

Expand Down
35 changes: 21 additions & 14 deletions tests/multikey.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ enum squirrel_error test_release(struct key *key, uint8_t layer,
return ERR_NONE;
}

// test: press_key, release_key + check_key - in squirrel_key.c,
// but with multiple keys in a keyboard.
int main() {
squirrel_init(1);
uint8_t code1 = 0x0F;
uint8_t code2 = 0xF0;

void make_testkey(int index) {
// press_key + release_key
uint8_t code1 = 0x0F;
uint8_t code2 = 0xF0;

struct key testkey;
testkey.pressed = test_press;
testkey.pressed_argument_count = 2;
Expand All @@ -62,11 +58,18 @@ int main() {
testkey.released_arguments[0] = &code2;
testkey.released_arguments[1] = &code1;

layers[0].keys[0] = testkey;
layers[0].keys[1] = testkey;
layers[0].keys[2] = testkey;
layers[0].keys[3] = testkey;
layers[0].keys[4] = testkey;
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++) {
Expand All @@ -75,6 +78,7 @@ int main() {
if (err != ERR_NONE) {
return 2;
}
return 0;
if (test_result != 0) {
return 3;
}
Expand Down Expand Up @@ -157,8 +161,11 @@ int main() {
}
};

free(testkey.pressed_arguments);
free(testkey.released_arguments);
// cleanup
for (int i = 0; i < 5; i++) {
free(layers[0].keys[i].pressed_arguments);
free(layers[0].keys[i].released_arguments);
}

return 0;
}

0 comments on commit f72e57a

Please sign in to comment.