From 93815d9ed25ecdcbf6a2fcf6aa1c2978384eb80b Mon Sep 17 00:00:00 2001 From: _N0x Date: Mon, 21 Feb 2022 00:03:24 +0100 Subject: [PATCH] Extended config handling so now multiple macros can be read in correctly --- include/defines.h | 2 + include/keycodes_conv.h | 4 - src/config_handling.c | 159 ++++++++++++++++++++++------------------ src/firmware_handling.c | 54 ++++++-------- src/keycodes_conv.c | 49 ++++++------- src/main.c | 16 +++- 6 files changed, 148 insertions(+), 136 deletions(-) diff --git a/include/defines.h b/include/defines.h index 7d6172f..8a9a103 100644 --- a/include/defines.h +++ b/include/defines.h @@ -7,6 +7,7 @@ #define DEFINES_H_ /* THESE ARE THE AVALIABLE KEYS FOR THE "MOD_KEYS" PARAMETER */ +#define MODKEY_NONE 0x00 #define MODKEY_LCTR 0x01 #define MODKEY_LSHI 0x02 #define MODKEY_LALT 0x03 @@ -25,6 +26,7 @@ #define MODKEY_RALT_RCTR_RSHI 0x10 /* THESE ARE THE AVAIRABLE KEYS FOR THE "KEYS" PARAMETER */ +#define KEY_NONE 0x00 #define KEY_ErrorRollOver 0x01 #define KEY_POSTFail 0x02 #define KEY_ErrorUndefined 0x03 diff --git a/include/keycodes_conv.h b/include/keycodes_conv.h index 51e4760..f5235e4 100644 --- a/include/keycodes_conv.h +++ b/include/keycodes_conv.h @@ -1,7 +1,4 @@ -#include "../include/defines.h" - #include -#include #ifndef KEYCODE_CONV_H_ #define KEYCODE_CONV_H_ @@ -16,6 +13,5 @@ struct keyCode { { KC, #KC } uint8_t get_keycode_by_name(char *key_name); -uint8_t get_modkeycode_by_name(char *modkey_name); #endif /* KEYCODE_CONV_H_ */ diff --git a/src/config_handling.c b/src/config_handling.c index 1fa9577..2bf9b32 100644 --- a/src/config_handling.c +++ b/src/config_handling.c @@ -6,6 +6,7 @@ #include #include #include +#include #include /* define declarations */ @@ -28,7 +29,9 @@ enum MacroNumber { }; /* method declarations */ -char *read_in_config(const char *conf_name); +char *read_in_config(const char *conf_name); +void set_action(const cJSON *action, f8_macro_actionP pma); +uint8_t get_key_from_json(cJSON *key); f8_macroP * get_f8_macros(char *conf_name) { @@ -45,7 +48,10 @@ get_f8_macros(char *conf_name) { const cJSON *macro = NULL; /* pmarse the read in config file */ - cJSON *config_json = cJSON_Parse(read_in_config(conf_name)); + char *conf = read_in_config(conf_name); + cJSON *config_json = cJSON_Parse(conf); + free(conf); + if (config_json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { @@ -55,102 +61,110 @@ get_f8_macros(char *conf_name) { } /* READ IN VALUES */ + int i = 0; macros = cJSON_GetObjectItemCaseSensitive(config_json, JO_MACROS); cJSON_ArrayForEach(macro, macros) { /* create new macro-pointer */ - f8_macroP pm = calloc(1, sizeof(*pm)); + f8_macroP pm = return_macros[i++]; /* Get the macro number from the JSON and set the offest */ cJSON *macro_no = cJSON_GetObjectItemCaseSensitive(macro, JO_MACRO_NO); switch ((int)cJSON_GetNumberValue(macro_no)) { - case MACRO1: - return_macros[MACRO1 - 1] = pm; - pm->offset = PROG1_OFFSET; - break; - case MACRO2: - return_macros[MACRO2 - 1] = pm; - pm->offset = PROG2_OFFSET; - break; - case MACRO3: - return_macros[MACRO3 - 1] = pm; - pm->offset = PROG3_OFFSET; - break; - case MACRO4: - return_macros[MACRO4 - 1] = pm; - pm->offset = PROG4_OFFSET; - break; - case MACRO5: - return_macros[MACRO5 - 1] = pm; - pm->offset = PROG5_OFFSET; - break; - case MACRO6: - return_macros[MACRO6 - 1] = pm; - pm->offset = PROG6_OFFSET; - break; - case MACRO7: - return_macros[MACRO7 - 1] = pm; - pm->offset = PROG7_OFFSET; - break; - case MACRO8: - return_macros[MACRO8 - 1] = pm; - pm->offset = PROG8_OFFSET; - break; + case MACRO1: + return_macros[MACRO1 - 1] = pm; + pm->offset = PROG1_OFFSET; + break; + case MACRO2: + return_macros[MACRO2 - 1] = pm; + pm->offset = PROG2_OFFSET; + break; + case MACRO3: + return_macros[MACRO3 - 1] = pm; + pm->offset = PROG3_OFFSET; + break; + case MACRO4: + return_macros[MACRO4 - 1] = pm; + pm->offset = PROG4_OFFSET; + break; + case MACRO5: + return_macros[MACRO5 - 1] = pm; + pm->offset = PROG5_OFFSET; + break; + case MACRO6: + return_macros[MACRO6 - 1] = pm; + pm->offset = PROG6_OFFSET; + break; + case MACRO7: + return_macros[MACRO7 - 1] = pm; + pm->offset = PROG7_OFFSET; + break; + case MACRO8: + return_macros[MACRO8 - 1] = pm; + pm->offset = PROG8_OFFSET; + break; } + printf("For Macro %d the values are\r\n", + (int)cJSON_GetNumberValue(macro_no)); + /* Move through all the actions in the current macro and set the values */ cJSON *action = NULL; cJSON *actions = cJSON_GetObjectItemCaseSensitive(macro, JO_ACTIONS); - int i = 0; + int j = 0; cJSON_ArrayForEach(action, actions) { + printf(" >> Action %d\r\n", j); f8_macro_actionP pma = calloc(1, sizeof(*pma)); - cJSON *mod_keys = - cJSON_GetObjectItemCaseSensitive(action, JO_MOD_KEYS); - // cJSON *delay = cJSON_GetObjectItemCaseSensitive(action, - // JO_DELAY); - cJSON *keys = cJSON_GetObjectItemCaseSensitive(action, JO_KEYS); - - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 0))); - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 1))); - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 2))); - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 3))); - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 4))); - printf(" >> %s\r\n", - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 5))); - - pma->modifier = - get_modkeycode_by_name(cJSON_GetStringValue(mod_keys)); - pma->delay = 0x00; // TODO: Set acutal value from JSON - pma->action1 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 0))); - pma->action2 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 1))); - pma->action3 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 2))); - pma->action4 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 3))); - pma->action5 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 4))); - pma->action6 = get_keycode_by_name( - cJSON_GetStringValue(cJSON_GetArrayItem(keys, 5))); - + set_action(action, pma); /* append the new action to the macro */ - pm->actions[i++] = pma; + pm->actions[j++] = pma; } } + cJSON_Delete(config_json); return return_macros; } +void +set_action(const cJSON *action, f8_macro_actionP pma) { + + cJSON *mod_keys = cJSON_GetObjectItemCaseSensitive(action, JO_MOD_KEYS); + cJSON *delay = cJSON_GetObjectItemCaseSensitive(action, JO_DELAY); + cJSON *keys = cJSON_GetObjectItemCaseSensitive(action, JO_KEYS); + + pma->modifier = get_key_from_json(mod_keys); + + printf(" >> Delay: %.2f\r\n", cJSON_GetNumberValue(delay)); + pma->delay = 0x00; // TODO: Set actual value from JSON + + pma->action1 = get_key_from_json(cJSON_GetArrayItem(keys, 0)); + pma->action2 = get_key_from_json(cJSON_GetArrayItem(keys, 1)); + pma->action3 = get_key_from_json(cJSON_GetArrayItem(keys, 2)); + pma->action4 = get_key_from_json(cJSON_GetArrayItem(keys, 3)); + pma->action5 = get_key_from_json(cJSON_GetArrayItem(keys, 4)); + pma->action6 = get_key_from_json(cJSON_GetArrayItem(keys, 5)); +} + +/* returns the key-code from the given string. If no keycode is matched or the + * string is empty keycode 0x00 will be supplied */ +uint8_t +get_key_from_json(cJSON *key) { + uint8_t key_val = 0x00; /* default the key value to zero */ + + if (strcmp(cJSON_GetStringValue(key), "") != 0) { + key_val = get_keycode_by_name(cJSON_GetStringValue(key)); + } + + printf(" >> Got keyvalue 0x%02x for key \"%s\"\r\n", key_val, + cJSON_GetStringValue(key)); + + return key_val; +} + char * read_in_config(const char *conf_name) { @@ -168,6 +182,7 @@ read_in_config(const char *conf_name) { char *config = malloc(sb.st_size); fread(config, sb.st_size, 1, fp); + fclose(fp); return config; } diff --git a/src/firmware_handling.c b/src/firmware_handling.c index 84123b1..45ec9ac 100644 --- a/src/firmware_handling.c +++ b/src/firmware_handling.c @@ -30,9 +30,6 @@ get_firmware_buffer(char *filename) { p_fb->buffer = malloc(p_fb->size); fread(p_fb->buffer, p_fb->size, 1, firmware); - /* for testing if the buffer could be read correclty */ - // printf("buffer size: %d", p_fb->size); - fclose(firmware); return p_fb; @@ -61,34 +58,31 @@ void set_program(f_bffrP p_fb, f8_macroP mp) { for (int i = 0; i < MAX_ACTION; i++) { // Check if an action for that position exists - if (mp) { - if (mp->actions[i]) { - /* calculate the offset for the action - * -> general prog_offset + current prog_action i times bytes - * per action - */ - int actionpointer = mp->offset + (i * sizeof(*mp->actions[0])); + if (mp && mp->actions[i]) { + /* calculate the offset for the action + * -> general prog_offset + current prog_action i times bytes + * per action + */ + int actionpointer = mp->offset + (i * sizeof(*mp->actions[0])); - /* set the modifiers */ - memset(p_fb->buffer + actionpointer + 0, - mp->actions[i]->modifier, 1); - /* set timing delay */ - memset(p_fb->buffer + actionpointer + 1, mp->actions[i]->delay, - 1); - /* set action 1-6 */ - memset(p_fb->buffer + actionpointer + 2, - mp->actions[i]->action1, 1); - memset(p_fb->buffer + actionpointer + 3, - mp->actions[i]->action2, 1); - memset(p_fb->buffer + actionpointer + 4, - mp->actions[i]->action3, 1); - memset(p_fb->buffer + actionpointer + 5, - mp->actions[i]->action4, 1); - memset(p_fb->buffer + actionpointer + 6, - mp->actions[i]->action5, 1); - memset(p_fb->buffer + actionpointer + 7, - mp->actions[i]->action6, 1); - } + /* set the modifiers */ + memset(p_fb->buffer + actionpointer + 0, mp->actions[i]->modifier, + 1); + /* set timing delay */ + memset(p_fb->buffer + actionpointer + 1, mp->actions[i]->delay, 1); + /* set action 1-6 */ + memset(p_fb->buffer + actionpointer + 2, mp->actions[i]->action1, + 1); + memset(p_fb->buffer + actionpointer + 3, mp->actions[i]->action2, + 1); + memset(p_fb->buffer + actionpointer + 4, mp->actions[i]->action3, + 1); + memset(p_fb->buffer + actionpointer + 5, mp->actions[i]->action4, + 1); + memset(p_fb->buffer + actionpointer + 6, mp->actions[i]->action5, + 1); + memset(p_fb->buffer + actionpointer + 7, mp->actions[i]->action6, + 1); } } } diff --git a/src/keycodes_conv.c b/src/keycodes_conv.c index 5cc0a92..41aa580 100644 --- a/src/keycodes_conv.c +++ b/src/keycodes_conv.c @@ -1,9 +1,13 @@ #include "../include/keycodes_conv.h" +#include "../include/defines.h" + #include +#include const keyCode keycodes[] = { + KEYCODE(KEY_NONE), KEYCODE(KEY_ErrorRollOver), KEYCODE(KEY_POSTFail), KEYCODE(KEY_ErrorUndefined), @@ -222,41 +226,34 @@ const keyCode keycodes[] = { KEYCODE(KEY_RightShift), KEYCODE(KEY_RightAlt), KEYCODE(KEY_RightGUI), -}; -const keyCode modkeycodes[] = { - KEYCODE(MODKEY_LCTR), KEYCODE(MODKEY_LSHI), - KEYCODE(MODKEY_LALT), KEYCODE(MODKEY_LWIN), - KEYCODE(MODKEY_RCTR), KEYCODE(MODKEY_RSHI), - KEYCODE(MODKEY_RALT), KEYCODE(MODKEY_RWIN), - KEYCODE(MODKEY_RWIN_RSHI), KEYCODE(MODKEY_RWIN_RCTR), - KEYCODE(MODKEY_RWIN_RALT), KEYCODE(MODKEY_RWIN_RCTR_RSHI), - KEYCODE(MODKEY_RCTR_RALT), KEYCODE(MODKEY_RCTR_RSHI), - KEYCODE(MODKEY_RALT_RSHI), KEYCODE(MODKEY_RALT_RCTR_RSHI), + /* SPECIAL KEYCODES FOR THE MODIFIFER KEY */ + KEYCODE(MODKEY_NONE), + KEYCODE(MODKEY_LCTR), + KEYCODE(MODKEY_LSHI), + KEYCODE(MODKEY_LALT), + KEYCODE(MODKEY_LWIN), + KEYCODE(MODKEY_RCTR), + KEYCODE(MODKEY_RSHI), + KEYCODE(MODKEY_RALT), + KEYCODE(MODKEY_RWIN), + KEYCODE(MODKEY_RWIN_RSHI), + KEYCODE(MODKEY_RWIN_RCTR), + KEYCODE(MODKEY_RWIN_RALT), + KEYCODE(MODKEY_RWIN_RCTR_RSHI), + KEYCODE(MODKEY_RCTR_RALT), + KEYCODE(MODKEY_RCTR_RSHI), + KEYCODE(MODKEY_RALT_RSHI), + KEYCODE(MODKEY_RALT_RCTR_RSHI), }; uint8_t get_keycode_by_name(char *key_name) { - uint8_t keycode = -1; + uint8_t keycode = 0x00; for (int i = 0; i < (sizeof(keycodes) / sizeof(keycodes[0])); i++) { if (strcmp(keycodes[i].key_name, key_name) == 0) { keycode = keycodes[i].key_code; - printf(" >> Getting keycode for %s: 0x%02x\r\n", key_name, keycode); - break; - } - } - - return keycode; -} - -uint8_t -get_modkeycode_by_name(char *modkey_name) { - uint8_t keycode = -1; - - for (int i = 0; i < (sizeof(modkeycodes) / sizeof(modkeycodes[0])); i++) { - if (strcmp(modkeycodes[i].key_name, modkey_name) == 0) { - keycode = modkeycodes[i].key_code; break; } } diff --git a/src/main.c b/src/main.c index 971442a..196e485 100644 --- a/src/main.c +++ b/src/main.c @@ -11,23 +11,31 @@ void testing(f_bffrP p_fb); char *updated_file_name(char *orig); int -main(int argc, char *argv[]) { +main(int argc, char *argv + + []) { /* Read in the firmware file into a f_bffr_t struct */ f_bffrP p_fb = get_firmware_buffer(argv[1]); /* Call testing method */ + // testing(p_fb); - f8_macroP *my_macros = get_f8_macros("config.json"); + f8_macroP *my_macros = get_f8_macros(argv[2]); int len = sizeof(my_macros); printf("Length of my_macros: %d\r\n", len); - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; + + i++) { set_program(p_fb, my_macros[i]); + free(my_macros[i]); } - write_firmware_buffer(updated_file_name(argv[1]), p_fb); + char *new_name = updated_file_name(argv[1]); + write_firmware_buffer(new_name, p_fb); + free(new_name); free(p_fb->buffer); free(p_fb);