diff --git a/src/config.c b/src/config.c index 512f7c0..2e27904 100644 --- a/src/config.c +++ b/src/config.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -472,6 +474,85 @@ bool atobool(char *arr) return false; } +unsigned int wm_string_to_mask(const char* str) +{ + int ret; + ret = strncmp(str, "Shift", CONFIG_ARGLEN_MAX); + if (ret == 0) return ShiftMask; + + ret = strncmp(str, "Control", CONFIG_ARGLEN_MAX); + if (ret == 0) return ControlMask; + + ret = strncmp(str, "Ctrl", CONFIG_ARGLEN_MAX); + if (ret == 0) return ControlMask; + + ret = strncmp(str, "Caps", CONFIG_ARGLEN_MAX); + if (ret == 0) return LockMask; + + ret = strncmp(str, "CapsLock", CONFIG_ARGLEN_MAX); + if (ret == 0) return LockMask; + + ret = strncmp(str, "Alt", CONFIG_ARGLEN_MAX); + if (ret == 0) return Mod1Mask; + + ret = strncmp(str, "Win", CONFIG_ARGLEN_MAX); + if (ret == 0) return Mod4Mask; + + ret = strncmp(str, "Windows", CONFIG_ARGLEN_MAX); + if (ret == 0) return Mod4Mask; + + ret = strncmp(str, "Super", CONFIG_ARGLEN_MAX); + if (ret == 0) return Mod4Mask; + + fprintf(stderr, "wm: encountered invalid modifier token %s\n. exiting.", + str); + abort(); +} + +KeySym* wm_keybind_str_to_mask_keysym(const char* kb_argv) +{ + KeySym *ret = calloc(2, sizeof(KeySym)); + char* str = strdup(kb_argv); + char tokens[CONFIG_ARGC_MAX][CONFIG_ARGLEN_MAX]; + + char *token = strtok(str, "+"); + + long token_count = 0; + while(token != NULL) { + token_count++; + if (token_count >= CONFIG_ARGC_MAX) { + fprintf(stderr, "wm: maximum number of arguments (%d) exceeded." + "exiting.\n", CONFIG_ARGC_MAX); + abort(); + } + + strncpy(tokens[token_count - 1], token, CONFIG_ARGLEN_MAX); + + token = strtok(NULL, "+"); + } + + DEBUG_PRINT("str: %s token count: %d\n", kb_argv, token_count); + + ret[0] = wm_string_to_mask(tokens[0]); + + for (size_t i = 1; i < token_count - 1; i++) { + ret[0] |= wm_string_to_mask(tokens[i]); + } + + KeySym keysym = XStringToKeysym(tokens[token_count - 1]); + if (keysym == NoSymbol) { + fprintf(stderr, "wm: encountered invalid keybind token %s\n. exiting.", + tokens[token_count-1]); + abort(); + } + + ret[1] = keysym; + + free(str); + + return ret; +} + // TODO Keybind wm_config_parse_bind_command(ConfigCommand *command) { diff --git a/src/config.h b/src/config.h index 39b1084..25e1d43 100644 --- a/src/config.h +++ b/src/config.h @@ -94,6 +94,14 @@ void wm_configcommand_copy(ConfigCommand *dest, const ConfigCommand *src); void wm_configcommand_argv_parse(ConfigCommand *command); void wm_configcommand_free(ConfigCommand *command); bool atobool(char *arr); + +/* Converts string to Key mask (see X.h:218-228). */ +unsigned int wm_string_to_mask(const char* str); + +/* The first element is the mask, the second element is the + * key. The returned array needs to be freed. */ +KeySym* wm_keybind_str_to_mask_keysym(const char* kb_argv); + Keybind wm_config_parse_bind_command(ConfigCommand *command); #endif // CONFIG_H