Merge pull request #247 from sce/handle_bindsym_duplicates

Handle bindsym duplicates
This commit is contained in:
Drew DeVault 2015-11-22 15:29:17 -05:00
commit cb11364552
3 changed files with 70 additions and 20 deletions

View file

@ -104,6 +104,10 @@ void free_output_config(struct output_config *oc);
int workspace_output_cmp_workspace(const void *a, const void *b);
int sway_binding_cmp(const void *a, const void *b);
int sway_binding_cmp_keys(const void *a, const void *b);
void free_sway_binding(struct sway_binding *sb);
/**
* Global config singleton.
*/

View file

@ -132,19 +132,6 @@ static struct cmd_results *checkarg(int argc, const char *name, enum expected_ar
return error;
}
static int bindsym_sort(const void *_lbind, const void *_rbind) {
const struct sway_binding *lbind = *(void **)_lbind;
const struct sway_binding *rbind = *(void **)_rbind;
unsigned int lmod = 0, rmod = 0, i;
// Count how any modifiers are pressed
for (i = 0; i < 8 * sizeof(lbind->modifiers); ++i) {
lmod += lbind->modifiers & 1 << i;
rmod += rbind->modifiers & 1 << i;
}
return (rbind->keys->length + rmod) - (lbind->keys->length + lmod);
}
static struct cmd_results *cmd_bindsym(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
@ -159,8 +146,7 @@ static struct cmd_results *cmd_bindsym(int argc, char **argv) {
binding->command = join_args(argv + 1, argc - 1);
list_t *split = split_string(argv[0], "+");
int i;
for (i = 0; i < split->length; ++i) {
for (int i = 0; i < split->length; ++i) {
// Check for a modifier key
int j;
bool is_mod = false;
@ -176,9 +162,7 @@ static struct cmd_results *cmd_bindsym(int argc, char **argv) {
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
if (!sym) {
error = cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", (char *)split->items[i]);
list_free(binding->keys);
free(binding->command);
free(binding);
free_sway_binding(binding);
list_free(split);
return error;
}
@ -188,10 +172,16 @@ static struct cmd_results *cmd_bindsym(int argc, char **argv) {
}
free_flat_list(split);
// TODO: Check if there are other commands with this key binding
struct sway_mode *mode = config->current_mode;
int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding);
if (i > -1) {
sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]);
struct sway_binding *dup = mode->bindings->items[i];
free_sway_binding(dup);
list_del(mode->bindings, i);
}
list_add(mode->bindings, binding);
list_sort(mode->bindings, bindsym_sort);
list_sort(mode->bindings, sway_binding_cmp);
sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
@ -1471,6 +1461,13 @@ struct cmd_results *handle_command(char *_exec) {
return results;
}
// this is like handle_command above, except:
// 1) it ignores empty commands (empty lines)
// 2) it does variable substitution
// 3) it doesn't split commands (because the multiple commands are supposed to
// be chained together)
// 4) handle_command handles all state internally while config_command has some
// state handled outside (notably the block mode, in read_config)
struct cmd_results *config_command(char *exec) {
struct cmd_results *results = NULL;
int argc;

View file

@ -383,3 +383,52 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
const struct workspace_output *wsa = a, *wsb = b;
return lenient_strcmp(wsa->workspace, wsb->workspace);
}
int sway_binding_cmp_keys(const void *a, const void *b) {
const struct sway_binding *binda = a, *bindb = b;
if (binda->modifiers > bindb->modifiers) {
return 1;
} else if (binda->modifiers < bindb->modifiers) {
return -1;
}
if (binda->keys->length > bindb->keys->length) {
return 1;
} else if (binda->keys->length < bindb->keys->length) {
return -1;
}
for (int i = 0; i < binda->keys->length; i++) {
xkb_keysym_t *ka = binda->keys->items[i],
*kb = bindb->keys->items[i];
if (*ka > *kb) {
return 1;
} else if (*ka < *kb) {
return -1;
}
}
return 0;
}
int sway_binding_cmp(const void *a, const void *b) {
int cmp = 0;
if ((cmp = sway_binding_cmp_keys(a, b)) != 0) {
return cmp;
}
const struct sway_binding *binda = a, *bindb = b;
return lenient_strcmp(binda->command, bindb->command);
}
void free_sway_binding(struct sway_binding *binding) {
if (binding->keys) {
for (int i = 0; i < binding->keys->length; i++) {
free(binding->keys->items[i]);
}
list_free(binding->keys);
}
if (binding->command) {
free(binding->command);
}
free(binding);
}