Move modifier name table to common/util.c

Lookup of modifier names is required in several places, thus it makes
sense to move it to a general place.
This commit is contained in:
Mikkel Oscar Lyderik 2016-01-05 18:07:43 +01:00
parent c20c63b677
commit 95e0f44c73
4 changed files with 69 additions and 66 deletions

View file

@ -13,3 +13,41 @@ int numlen(int n) {
if (n >= 10) return 2;
return 1;
}
static struct modifier_key {
char *name;
uint32_t mod;
} modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
{ "Ctrl", WLC_BIT_MOD_CTRL },
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
{ "Alt", WLC_BIT_MOD_ALT },
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
{ "Mod3", WLC_BIT_MOD_MOD3 },
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
{ "Mod5", WLC_BIT_MOD_MOD5 },
};
uint32_t get_modifier_mask_by_name(const char *name) {
int i;
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
if (strcasecmp(modifiers[i].name, name) == 0) {
return modifiers[i].mod;
}
}
return 0;
}
const char *get_modifier_name_by_mask(uint32_t modifier) {
int i;
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
if (modifiers[i].mod == modifier) {
return modifiers[i].name;
}
}
return NULL;
}

View file

@ -1,6 +1,10 @@
#ifndef _SWAY_UTIL_H
#define _SWAY_UTIL_H
#include <stdint.h>
#include <wlc/wlc.h>
#include <xkbcommon/xkbcommon.h>
/**
* Wrap i into the range [0, max[
*/
@ -11,4 +15,18 @@ int wrap(int i, int max);
*/
int numlen(int n);
/**
* Get modifier mask from modifier name.
*
* Returns the modifer mask or 0 if the name isn't found.
*/
uint32_t get_modifier_mask_by_name(const char *name);
/**
* Get modifier name from modifier mask.
*
* Returns the modifier name or NULL if it isn't found.
*/
const char *get_modifier_name_by_mask(uint32_t modifier);
#endif

View file

@ -99,22 +99,6 @@ static sway_cmd bar_colors_cmd_urgent_workspace;
swayc_t *sp_view;
int sp_index = 0;
static struct modifier_key {
char *name;
uint32_t mod;
} modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
{ "Ctrl", WLC_BIT_MOD_CTRL },
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
{ "Alt", WLC_BIT_MOD_ALT },
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
{ "Mod3", WLC_BIT_MOD_MOD3 },
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
{ "Mod5", WLC_BIT_MOD_MOD5 },
};
static char *bg_options[] = {
"stretch",
"center",
@ -187,16 +171,11 @@ static struct cmd_results *cmd_bindsym(int argc, char **argv) {
list_t *split = split_string(argv[0], "+");
for (int i = 0; i < split->length; ++i) {
// Check for a modifier key
int j;
bool is_mod = false;
for (j = 0; j < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++j) {
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
binding->modifiers |= modifiers[j].mod;
is_mod = true;
break;
uint32_t mod;
if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) {
binding->modifiers |= mod;
continue;
}
}
if (is_mod) continue;
// Check for xkb key
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
if (!sym) {
@ -408,17 +387,13 @@ static struct cmd_results *cmd_floating_mod(int argc, char **argv) {
if ((error = checkarg(argc, "floating_modifier", EXPECTED_AT_LEAST, 1))) {
return error;
}
int i, j;
int i;
list_t *split = split_string(argv[0], "+");
config->floating_mod = 0;
// set modifier keys
for (i = 0; i < split->length; ++i) {
for (j = 0; j < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++j) {
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
config->floating_mod |= modifiers[j].mod;
}
}
config->floating_mod |= get_modifier_mask_by_name(split->items[i]);
}
free_flat_list(split);
if (!config->floating_mod) {
@ -1893,16 +1868,11 @@ static struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
list_t *split = split_string(argv[0], "+");
for (int i = 0; i < split->length; ++i) {
int j;
bool is_mod = false;
for (j = 0; j < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++j) {
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
mod = modifiers[j].mod;
is_mod = true;
break;
}
}
if (!is_mod) {
uint32_t tmp_mod;
if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) {
mod |= tmp_mod;
continue;
} else {
free_flat_list(split);
return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]);
}

View file

@ -19,6 +19,7 @@
#include "commands.h"
#include "list.h"
#include "stringop.h"
#include "util.h"
static int ipc_socket = -1;
static struct wlc_event_source *ipc_event_source = NULL;
@ -35,22 +36,6 @@ struct ipc_client {
enum ipc_command_type subscribed_events;
};
static struct modifier_key {
char *name;
uint32_t mod;
} modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
{ "Ctrl", WLC_BIT_MOD_CTRL },
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
{ "Alt", WLC_BIT_MOD_ALT },
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
{ "Mod3", WLC_BIT_MOD_MOD3 },
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
{ "Mod5", WLC_BIT_MOD_MOD5 },
};
struct sockaddr_un *ipc_user_sockaddr(void);
int ipc_handle_connection(int fd, uint32_t mask, void *data);
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
@ -640,15 +625,7 @@ void ipc_event_modifier(uint32_t modifier, const char *state) {
json_object *obj = json_object_new_object();
json_object_object_add(obj, "change", json_object_new_string(state));
const char *modifier_name = NULL;
int i;
for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) {
if (modifiers[i].mod == modifier) {
modifier_name = modifiers[i].name;
break;
}
}
const char *modifier_name = get_modifier_name_by_mask(modifier);
json_object_object_add(obj, "modifier", json_object_new_string(modifier_name));
const char *json_string = json_object_to_json_string(obj);