aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/bind.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-10-08 19:28:53 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-10-08 19:28:53 +1000
commit82423991a8512ab97fbc41d1e190e709c58bc346 (patch)
tree837e45f94240e02e2ab0f8f931227943e00819b3 /sway/commands/bind.c
parentMerge pull request #2786 from swaywm/no-op-client-commands (diff)
downloadsway-82423991a8512ab97fbc41d1e190e709c58bc346.tar.gz
sway-82423991a8512ab97fbc41d1e190e709c58bc346.tar.zst
sway-82423991a8512ab97fbc41d1e190e709c58bc346.zip
Reload config using idle event
This patch makes it so when you run reload, the actual reloading is deferred to the next time the event loop becomes idle. This avoids several use-after-frees and removes the workarounds we have to avoid them. When you run reload, we validate the config before creating the idle event. This is so the reload command will still return an error if there are validation errors. To allow this, load_main_config has been adjusted so it doesn't apply the config if validating is true rather than applying it unconditionally. This also fixes a memory leak in the reload command where if the config failed to load, the bar_ids list would not be freed.
Diffstat (limited to 'sway/commands/bind.c')
-rw-r--r--sway/commands/bind.c49
1 files changed, 2 insertions, 47 deletions
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index 820c2a6a..701d9746 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -30,33 +30,6 @@ void free_sway_binding(struct sway_binding *binding) {
30 free(binding); 30 free(binding);
31} 31}
32 32
33static struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
34 struct sway_binding *new_sb = calloc(1, sizeof(struct sway_binding));
35 if (!new_sb) {
36 return NULL;
37 }
38
39 new_sb->type = sb->type;
40 new_sb->order = sb->order;
41 new_sb->flags = sb->flags;
42 new_sb->modifiers = sb->modifiers;
43 new_sb->command = strdup(sb->command);
44
45 new_sb->keys = create_list();
46 int i;
47 for (i = 0; i < sb->keys->length; ++i) {
48 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
49 if (!key) {
50 free_sway_binding(new_sb);
51 return NULL;
52 }
53 *key = *(xkb_keysym_t *)sb->keys->items[i];
54 list_add(new_sb->keys, key);
55 }
56
57 return new_sb;
58}
59
60/** 33/**
61 * Returns true if the bindings have the same key and modifier combinations. 34 * Returns true if the bindings have the same key and modifier combinations.
62 * Note that keyboard layout is not considered, so the bindings might actually 35 * Note that keyboard layout is not considered, so the bindings might actually
@@ -214,9 +187,6 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
214 } 187 }
215 188
216 binding->command = join_args(argv + 1, argc - 1); 189 binding->command = join_args(argv + 1, argc - 1);
217 if (strcasestr(binding->command, "reload")) {
218 binding->flags |= BINDING_RELOAD;
219 }
220 190
221 list_t *split = split_string(argv[0], "+"); 191 list_t *split = split_string(argv[0], "+");
222 for (int i = 0; i < split->length; ++i) { 192 for (int i = 0; i < split->length; ++i) {
@@ -306,31 +276,16 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
306 * Execute the command associated to a binding 276 * Execute the command associated to a binding
307 */ 277 */
308void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding) { 278void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding) {
309 wlr_log(WLR_DEBUG, "running command for binding: %s", 279 wlr_log(WLR_DEBUG, "running command for binding: %s", binding->command);
310 binding->command);
311
312 struct sway_binding *binding_copy = binding;
313 // if this is a reload command we need to make a duplicate of the
314 // binding since it will be gone after the reload has completed.
315 if (binding->flags & BINDING_RELOAD) {
316 binding_copy = sway_binding_dup(binding);
317 if (!binding_copy) {
318 wlr_log(WLR_ERROR, "Failed to duplicate binding during reload");
319 return;
320 }
321 }
322 280
323 config->handler_context.seat = seat; 281 config->handler_context.seat = seat;
324 struct cmd_results *results = execute_command(binding->command, NULL, NULL); 282 struct cmd_results *results = execute_command(binding->command, NULL, NULL);
325 if (results->status == CMD_SUCCESS) { 283 if (results->status == CMD_SUCCESS) {
326 ipc_event_binding(binding_copy); 284 ipc_event_binding(binding);
327 } else { 285 } else {
328 wlr_log(WLR_DEBUG, "could not run command for binding: %s (%s)", 286 wlr_log(WLR_DEBUG, "could not run command for binding: %s (%s)",
329 binding->command, results->error); 287 binding->command, results->error);
330 } 288 }
331 289
332 if (binding_copy->flags & BINDING_RELOAD) {
333 free_sway_binding(binding_copy);
334 }
335 free_cmd_results(results); 290 free_cmd_results(results);
336} 291}