Skip to content

Commit

Permalink
WIP: pointer fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Oct 6, 2024
1 parent 7eab251 commit 33ef349
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 406 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
1 change: 0 additions & 1 deletion include/squirrel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions include/squirrel_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 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 {
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,
Expand Down
14 changes: 9 additions & 5 deletions src/squirrel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
#include "squirrel_key.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/squirrel_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "squirrel.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int key_count = 0; // This should be overwritten by squirrel_init
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
23 changes: 12 additions & 11 deletions src/squirrel_quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "squirrel_key.h"
#include "squirrel_keyboard.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
86 changes: 58 additions & 28 deletions tests/consumer_press_release.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,104 @@
#include "squirrel_key.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// 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;
};
Loading

0 comments on commit 33ef349

Please sign in to comment.