Compare commits

...

2 Commits

3 changed files with 126 additions and 0 deletions

View File

@ -1,3 +1,5 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
@ -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)
{

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_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

View File

@ -1,3 +1,4 @@
#include <X11/X.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
@ -10,6 +11,7 @@
#include <unistd.h>
#include <limits.h>
#include <json-c/json_tokener.h>
#include "config.h"
#include "util.h"
#include "client.h"
#include "wm.h"
@ -517,6 +519,40 @@ static void test_wm_logentries_calculate_distances(void **state)
wm_treenode_free(child);
}
static void test_wm_keybind_str_to_mask_keysym(void **state)
{
char *keybind1 = "Windows+Shift+h";
char *keybind2 = "Alt+1";
char *keybind3 = "Windows+Alt+Shift+j";
char *keybind4 = "CapsLock+k";
KeySym *ret;
ret = wm_keybind_str_to_mask_keysym(keybind1);
assert_int_equal(ret[0], Mod4Mask | ShiftMask);
assert_int_equal(ret[1], XK_h);
free(ret);
ret = wm_keybind_str_to_mask_keysym(keybind2);
assert_int_equal(ret[0], Mod1Mask);
assert_int_equal(ret[1], XK_1);
free(ret);
ret = wm_keybind_str_to_mask_keysym(keybind3);
assert_int_equal(ret[0], Mod4Mask | Mod1Mask | ShiftMask);
assert_int_equal(ret[1], XK_j);
free(ret);
ret = wm_keybind_str_to_mask_keysym(keybind4);
assert_int_equal(ret[0], LockMask);
assert_int_equal(ret[1], XK_k);
free(ret);
}
static void test_wm_postorder_traversal(void **state)
{
TreeNode *node1 = (TreeNode*)*state;
@ -1022,6 +1058,7 @@ int main(void)
cmocka_unit_test(test_wm_json_obj_to_treenode),
cmocka_unit_test(test_wm_read_log),
cmocka_unit_test(test_wm_logentries_calculate_distances),
cmocka_unit_test(test_wm_keybind_str_to_mask_keysym),
};
const struct CMUnitTest test_group2[] = {