aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2018-01-22 01:16:23 +0100
committerLibravatar GitHub <noreply@github.com>2018-01-22 01:16:23 +0100
commit0c58673c6a108ba241419a0f1d5fecd47f22370e (patch)
treec3e19af6dd70f04fc5c617e932b4afcc7a1b41d9 /sway/commands.c
parentRemove sway/old/ (diff)
parentdont allow kill command in config (diff)
downloadsway-0c58673c6a108ba241419a0f1d5fecd47f22370e.tar.gz
sway-0c58673c6a108ba241419a0f1d5fecd47f22370e.tar.zst
sway-0c58673c6a108ba241419a0f1d5fecd47f22370e.zip
Merge pull request #1574 from acrisci/config-refactor
Command criteria
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c81
1 files changed, 69 insertions, 12 deletions
diff --git a/sway/commands.c b/sway/commands.c
index 1005cf68..a77ff791 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -8,8 +8,10 @@
8#include <json-c/json.h> 8#include <json-c/json.h>
9#include "sway/commands.h" 9#include "sway/commands.h"
10#include "sway/config.h" 10#include "sway/config.h"
11#include "sway/criteria.h"
11#include "sway/security.h" 12#include "sway/security.h"
12#include "sway/input/input-manager.h" 13#include "sway/input/input-manager.h"
14#include "sway/input/seat.h"
13#include "stringop.h" 15#include "stringop.h"
14#include "log.h" 16#include "log.h"
15 17
@@ -70,10 +72,7 @@ void apply_input_config(struct input_config *input) {
70 list_add(config->input_configs, input); 72 list_add(config->input_configs, input);
71 } 73 }
72 74
73 struct input_config *old_input_config = current_input_config;
74 current_input_config = input;
75 sway_input_manager_apply_input_config(input_manager, input); 75 sway_input_manager_apply_input_config(input_manager, input);
76 current_input_config = old_input_config;
77} 76}
78 77
79void apply_seat_config(struct seat_config *seat) { 78void apply_seat_config(struct seat_config *seat) {
@@ -89,7 +88,6 @@ void apply_seat_config(struct seat_config *seat) {
89 list_add(config->seat_configs, seat); 88 list_add(config->seat_configs, seat);
90 } 89 }
91 90
92 current_seat_config = seat;
93 sway_input_manager_apply_seat_config(input_manager, seat); 91 sway_input_manager_apply_seat_config(input_manager, seat);
94} 92}
95 93
@@ -136,6 +134,7 @@ static struct cmd_handler handlers[] = {
136 { "exit", cmd_exit }, 134 { "exit", cmd_exit },
137 { "include", cmd_include }, 135 { "include", cmd_include },
138 { "input", cmd_input }, 136 { "input", cmd_input },
137 { "kill", cmd_kill },
139 { "output", cmd_output }, 138 { "output", cmd_output },
140 { "seat", cmd_seat }, 139 { "seat", cmd_seat },
141 { "set", cmd_set }, 140 { "set", cmd_set },
@@ -204,9 +203,41 @@ struct cmd_results *handle_command(char *_exec) {
204 char *head = exec; 203 char *head = exec;
205 char *cmdlist; 204 char *cmdlist;
206 char *cmd; 205 char *cmd;
206 list_t *containers = NULL;
207 207
208 head = exec; 208 head = exec;
209 do { 209 do {
210 // Extract criteria (valid for this command list only).
211 bool has_criteria = false;
212 if (*head == '[') {
213 has_criteria = true;
214 ++head;
215 char *criteria_string = argsep(&head, "]");
216 if (head) {
217 ++head;
218 list_t *tokens = create_list();
219 char *error;
220
221 if ((error = extract_crit_tokens(tokens, criteria_string))) {
222 wlr_log(L_DEBUG, "criteria string parse error: %s", error);
223 results = cmd_results_new(CMD_INVALID, criteria_string,
224 "Can't parse criteria string: %s", error);
225 free(error);
226 free(tokens);
227 goto cleanup;
228 }
229 containers = container_for_crit_tokens(tokens);
230
231 free(tokens);
232 } else {
233 if (!results) {
234 results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched [");
235 }
236 goto cleanup;
237 }
238 // Skip leading whitespace
239 head += strspn(head, whitespace);
240 }
210 // Split command list 241 // Split command list
211 cmdlist = argsep(&head, ";"); 242 cmdlist = argsep(&head, ";");
212 cmdlist += strspn(cmdlist, whitespace); 243 cmdlist += strspn(cmdlist, whitespace);
@@ -239,16 +270,42 @@ struct cmd_results *handle_command(char *_exec) {
239 free_argv(argc, argv); 270 free_argv(argc, argv);
240 goto cleanup; 271 goto cleanup;
241 } 272 }
242 struct cmd_results *res = handler->handle(argc-1, argv+1); 273
243 if (res->status != CMD_SUCCESS) { 274 if (!has_criteria) {
244 free_argv(argc, argv); 275 // without criteria, the command acts upon the focused
245 if (results) { 276 // container
246 free_cmd_results(results); 277 struct sway_seat *seat = config->handler_context.seat;
278 if (!seat) {
279 seat = sway_input_manager_get_default_seat(input_manager);
280 }
281 if (seat) {
282 config->handler_context.current_container = seat->focus;
283 struct cmd_results *res = handler->handle(argc-1, argv+1);
284 if (res->status != CMD_SUCCESS) {
285 free_argv(argc, argv);
286 if (results) {
287 free_cmd_results(results);
288 }
289 results = res;
290 goto cleanup;
291 }
292 free_cmd_results(res);
293 }
294 } else {
295 for (int i = 0; i < containers->length; ++i) {
296 config->handler_context.current_container = containers->items[i];
297 struct cmd_results *res = handler->handle(argc-1, argv+1);
298 if (res->status != CMD_SUCCESS) {
299 free_argv(argc, argv);
300 if (results) {
301 free_cmd_results(results);
302 }
303 results = res;
304 goto cleanup;
305 }
306 free_cmd_results(res);
247 } 307 }
248 results = res;
249 goto cleanup;
250 } 308 }
251 free_cmd_results(res);
252 free_argv(argc, argv); 309 free_argv(argc, argv);
253 } while(cmdlist); 310 } while(cmdlist);
254 } while(head); 311 } while(head);