config: add functions for parsing keybind strings

This commit is contained in:
Akos Horvath 2024-03-10 15:33:44 +01:00
parent b7adb5349e
commit 4c17699c14
2 changed files with 89 additions and 0 deletions

View File

@ -1,3 +1,5 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
@ -472,6 +474,85 @@ bool atobool(char *arr)
return false; 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 // TODO
Keybind wm_config_parse_bind_command(ConfigCommand *command) Keybind wm_config_parse_bind_command(ConfigCommand *command)
{ {

View File

@ -94,6 +94,14 @@ void wm_configcommand_copy(ConfigCommand *dest, const ConfigCommand *src);
void wm_configcommand_argv_parse(ConfigCommand *command); void wm_configcommand_argv_parse(ConfigCommand *command);
void wm_configcommand_free(ConfigCommand *command); void wm_configcommand_free(ConfigCommand *command);
bool atobool(char *arr); 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); Keybind wm_config_parse_bind_command(ConfigCommand *command);
#endif // CONFIG_H #endif // CONFIG_H