1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
#define _XOPEN_SOURCE 500
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-names.h>
#include <wlc/wlc.h>
#include <wlc/wlc-render.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include <wordexp.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <limits.h>
#include <float.h>
#include <libinput.h>
#include "sway/layout.h"
#include "sway/focus.h"
#include "sway/workspace.h"
#include "sway/commands.h"
#include "sway/container.h"
#include "sway/output.h"
#include "sway/handlers.h"
#include "sway/input_state.h"
#include "sway/criteria.h"
#include "sway/ipc-server.h"
#include "sway/security.h"
#include "sway/input.h"
#include "sway/border.h"
#include "stringop.h"
#include "sway.h"
#include "util.h"
#include "list.h"
#include "log.h"
struct cmd_handler {
char *command;
sway_cmd *handle;
};
int sp_index = 0;
swayc_t *current_container = NULL;
// Returns error object, or NULL if check succeeds.
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
struct cmd_results *error = NULL;
switch (type) {
case EXPECTED_MORE_THAN:
if (argc > val) {
return NULL;
}
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected more than %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break;
case EXPECTED_AT_LEAST:
if (argc >= val) {
return NULL;
}
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected at least %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break;
case EXPECTED_LESS_THAN:
if (argc < val) {
return NULL;
};
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected less than %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break;
case EXPECTED_EQUAL_TO:
if (argc == val) {
return NULL;
};
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected %d arguments, got %d)", name, val, argc);
break;
}
return error;
}
void hide_view_in_scratchpad(swayc_t *sp_view) {
if (sp_view == NULL) {
return;
}
wlc_view_set_mask(sp_view->handle, 0);
sp_view->visible = false;
swayc_t *ws = sp_view->parent;
remove_child(sp_view);
if (swayc_active_workspace() != ws && ws->floating->length == 0 && ws->children->length == 0) {
destroy_workspace(ws);
} else {
arrange_windows(ws, -1, -1);
}
set_focused_container(container_under_pointer());
}
void input_cmd_apply(struct input_config *input) {
int i;
i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier);
if (i >= 0) {
// merge existing config
struct input_config *ic = config->input_configs->items[i];
merge_input_config(ic, input);
free_input_config(input);
input = ic;
} else {
list_add(config->input_configs, input);
}
current_input_config = input;
if (input->identifier) {
// Try to find the input device and apply configuration now. If
// this is during startup then there will be no container and config
// will be applied during normal "new input" event from wlc.
struct libinput_device *device = NULL;
for (int i = 0; i < input_devices->length; ++i) {
device = input_devices->items[i];
char* dev_identifier = libinput_dev_unique_id(device);
if (!dev_identifier) {
break;
}
int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0;
free(dev_identifier);
if (match) {
apply_input_config(input, device);
break;
}
}
}
}
void remove_view_from_scratchpad(swayc_t *view) {
int i;
for (i = 0; i < scratchpad->length; i++) {
if (scratchpad->items[i] == view) {
if (sp_index == 0) {
sp_index = scratchpad->length - 1;
} else {
sp_index--;
}
list_del(scratchpad, sp_index);
sp_view = NULL;
}
}
}
/* Keep alphabetized */
static struct cmd_handler handlers[] = {
{ "assign", cmd_assign },
{ "bar", cmd_bar },
{ "bindcode", cmd_bindcode },
{ "bindsym", cmd_bindsym },
{ "border", cmd_border },
{ "client.background", cmd_client_background },
{ "client.focused", cmd_client_focused },
{ "client.focused_inactive", cmd_client_focused_inactive },
{ "client.placeholder", cmd_client_placeholder },
{ "client.unfocused", cmd_client_unfocused },
{ "client.urgent", cmd_client_urgent },
{ "commands", cmd_commands },
{ "debuglog", cmd_debuglog },
{ "default_border", cmd_default_border },
{ "default_floating_border", cmd_default_floating_border },
{ "default_orientation", cmd_orientation },
{ "exec", cmd_exec },
{ "exec_always", cmd_exec_always },
{ "exit", cmd_exit },
{ "floating", cmd_floating },
{ "floating_maximum_size", cmd_floating_maximum_size },
{ "floating_minimum_size", cmd_floating_minimum_size },
{ "floating_modifier", cmd_floating_mod },
{ "floating_scroll", cmd_floating_scroll },
{ "focus", cmd_focus },
{ "focus_follows_mouse", cmd_focus_follows_mouse },
{ "font", cmd_font },
{ "for_window", cmd_for_window },
{ "force_focus_wrapping", cmd_force_focus_wrapping },
{ "fullscreen", cmd_fullscreen },
{ "gaps", cmd_gaps },
{ "hide_edge_borders", cmd_hide_edge_borders },
{ "include", cmd_include },
{ "input", cmd_input },
{ "ipc", cmd_ipc },
{ "kill", cmd_kill },
{ "layout", cmd_layout },
{ "log_colors", cmd_log_colors },
{ "mark", cmd_mark },
{ "mode", cmd_mode },
{ "mouse_warping", cmd_mouse_warping },
{ "move", cmd_move },
{ "new_float", cmd_new_float },
{ "new_window", cmd_new_window },
{ "output", cmd_output },
{ "permit", cmd_permit },
{ "reject", cmd_reject },
{ "reload", cmd_reload },
{ "resize", cmd_resize },
{ "scratchpad", cmd_scratchpad },
{ "seamless_mouse", cmd_seamless_mouse },
{ "set", cmd_set },
{ "show_marks", cmd_show_marks },
{ "smart_gaps", cmd_smart_gaps },
{ "split", cmd_split },
{ "splith", cmd_splith },
{ "splitt", cmd_splitt },
{ "splitv", cmd_splitv },
{ "sticky", cmd_sticky },
{ "unmark", cmd_unmark },
{ "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
{ "workspace_layout", cmd_workspace_layout },
};
static struct cmd_handler bar_handlers[] = {
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
{ "bindsym", bar_cmd_bindsym },
{ "colors", bar_cmd_colors },
{ "font", bar_cmd_font },
{ "height", bar_cmd_height },
{ "hidden_state", bar_cmd_hidden_state },
{ "id", bar_cmd_id },
{ "mode", bar_cmd_mode },
{ "modifier", bar_cmd_modifier },
{ "output", bar_cmd_output },
{ "pango_markup", bar_cmd_pango_markup },
{ "position", bar_cmd_position },
{ "separator_symbol", bar_cmd_separator_symbol },
{ "status_command", bar_cmd_status_command },
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
{ "swaybar_command", bar_cmd_swaybar_command },
{ "tray_output", bar_cmd_tray_output },
{ "tray_padding", bar_cmd_tray_padding },
{ "workspace_buttons", bar_cmd_workspace_buttons },
{ "wrap_scroll", bar_cmd_wrap_scroll },
};
/**
* Check and add color to buffer.
*
* return error object, or NULL if color is valid.
*/
struct cmd_results *add_color(const char *name, char *buffer, const char *color) {
int len = strlen(color);
if (len != 7 && len != 9 ) {
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
}
if (color[0] != '#') {
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
}
int i;
for (i = 1; i < len; ++i) {
if (!isxdigit(color[i])) {
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
}
}
// copy color to buffer
strncpy(buffer, color, len);
// add default alpha channel if color was defined without it
if (len == 7) {
buffer[7] = 'f';
buffer[8] = 'f';
}
buffer[9] = '\0';
return NULL;
}
static struct cmd_handler input_handlers[] = {
{ "accel_profile", input_cmd_accel_profile },
{ "click_method", input_cmd_click_method },
{ "drag_lock", input_cmd_drag_lock },
{ "dwt", input_cmd_dwt },
{ "events", input_cmd_events },
{ "left_handed", input_cmd_left_handed },
{ "middle_emulation", input_cmd_middle_emulation },
{ "natural_scroll", input_cmd_natural_scroll },
{ "pointer_accel", input_cmd_pointer_accel },
{ "scroll_method", input_cmd_scroll_method },
{ "tap", input_cmd_tap },
};
static struct cmd_handler bar_colors_handlers[] = {
{ "active_workspace", bar_colors_cmd_active_workspace },
{ "background", bar_colors_cmd_background },
{ "binding_mode", bar_colors_cmd_binding_mode },
{ "focused_background", bar_colors_cmd_focused_background },
{ "focused_separator", bar_colors_cmd_focused_separator },
{ "focused_statusline", bar_colors_cmd_focused_statusline },
{ "focused_workspace", bar_colors_cmd_focused_workspace },
{ "inactive_workspace", bar_colors_cmd_inactive_workspace },
{ "separator", bar_colors_cmd_separator },
{ "statusline", bar_colors_cmd_statusline },
{ "urgent_workspace", bar_colors_cmd_urgent_workspace },
};
static struct cmd_handler ipc_handlers[] = {
{ "*", cmd_ipc_cmd },
{ "bar-config", cmd_ipc_cmd },
{ "command", cmd_ipc_cmd },
{ "events", cmd_ipc_events },
{ "inputs", cmd_ipc_cmd },
{ "marks", cmd_ipc_cmd },
{ "outputs", cmd_ipc_cmd },
{ "tree", cmd_ipc_cmd },
{ "workspaces", cmd_ipc_cmd },
};
static struct cmd_handler ipc_event_handlers[] = {
{ "*", cmd_ipc_event_cmd },
{ "binding", cmd_ipc_event_cmd },
{ "input", cmd_ipc_event_cmd },
{ "mode", cmd_ipc_event_cmd },
{ "output", cmd_ipc_event_cmd },
{ "window", cmd_ipc_event_cmd },
{ "workspace", cmd_ipc_event_cmd },
};
static int handler_compare(const void *_a, const void *_b) {
const struct cmd_handler *a = _a;
const struct cmd_handler *b = _b;
return strcasecmp(a->command, b->command);
}
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
struct cmd_handler d = { .command=line };
struct cmd_handler *res = NULL;
sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT);
if (block == CMD_BLOCK_BAR) {
res = bsearch(&d, bar_handlers,
sizeof(bar_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else if (block == CMD_BLOCK_BAR_COLORS){
res = bsearch(&d, bar_colors_handlers,
sizeof(bar_colors_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else if (block == CMD_BLOCK_INPUT) {
res = bsearch(&d, input_handlers,
sizeof(input_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else if (block == CMD_BLOCK_IPC) {
res = bsearch(&d, ipc_handlers,
sizeof(ipc_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else if (block == CMD_BLOCK_IPC_EVENTS) {
res = bsearch(&d, ipc_event_handlers,
sizeof(ipc_event_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else {
res = bsearch(&d, handlers,
sizeof(handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
}
return res;
}
struct cmd_results *handle_command(char *_exec, enum command_context context) {
// Even though this function will process multiple commands we will only
// return the last error, if any (for now). (Since we have access to an
// error string we could e.g. concatonate all errors there.)
struct cmd_results *results = NULL;
char *exec = strdup(_exec);
char *head = exec;
char *cmdlist;
char *cmd;
list_t *containers = NULL;
head = exec;
do {
// Extract criteria (valid for this command list only).
if (*head == '[') {
++head;
char *criteria_string = argsep(&head, "]");
if (head) {
++head;
list_t *tokens = create_list();
char *error;
if ((error = extract_crit_tokens(tokens, criteria_string))) {
results = cmd_results_new(CMD_INVALID, criteria_string,
"Can't parse criteria string: %s", error);
free(error);
free(tokens);
goto cleanup;
}
containers = container_for(tokens);
free(tokens);
} else {
if (!results) {
results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched [");
}
goto cleanup;
}
// Skip leading whitespace
head += strspn(head, whitespace);
}
// Split command list
cmdlist = argsep(&head, ";");
cmdlist += strspn(cmdlist, whitespace);
do {
// Split commands
cmd = argsep(&cmdlist, ",");
cmd += strspn(cmd, whitespace);
if (strcmp(cmd, "") == 0) {
sway_log(L_INFO, "Ignoring empty command.");
continue;
}
sway_log(L_INFO, "Handling command '%s'", cmd);
//TODO better handling of argv
int argc;
char **argv = split_args(cmd, &argc);
if (strcmp(argv[0], "exec") != 0) {
int i;
for (i = 1; i < argc; ++i) {
if (*argv[i] == '\"' || *argv[i] == '\'') {
strip_quotes(argv[i]);
}
}
}
struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
if (!handler) {
if (results) {
free_cmd_results(results);
}
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
free_argv(argc, argv);
goto cleanup;
}
if (!(get_command_policy_mask(argv[0]) & context)) {
if (results) {
free_cmd_results(results);
}
results = cmd_results_new(CMD_INVALID, cmd,
"Permission denied for %s via %s", cmd,
command_policy_str(context));
free_argv(argc, argv);
goto cleanup;
}
int i = 0;
do {
if (!containers) {
current_container = get_focused_container(&root_container);
} else if (containers->length == 0) {
break;
} else {
current_container = (swayc_t *)containers->items[i];
}
sway_log(L_INFO, "Running on container '%s'", current_container->name);
struct cmd_results *res = handler->handle(argc-1, argv+1);
if (res->status != CMD_SUCCESS) {
free_argv(argc, argv);
if (results) {
free_cmd_results(results);
}
results = res;
goto cleanup;
}
free_cmd_results(res);
++i;
} while(containers && i < containers->length);
free_argv(argc, argv);
} while(cmdlist);
if (containers) {
list_free(containers);
containers = NULL;
}
} while(head);
cleanup:
free(exec);
if (containers) {
free(containers);
}
if (!results) {
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
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, enum cmd_status block) {
struct cmd_results *results = NULL;
int argc;
char **argv = split_args(exec, &argc);
if (!argc) {
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
goto cleanup;
}
sway_log(L_INFO, "handling config command '%s'", exec);
// Endblock
if (**argv == '}') {
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
goto cleanup;
}
struct cmd_handler *handler = find_handler(argv[0], block);
if (!handler) {
char *input = argv[0] ? argv[0] : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
goto cleanup;
}
int i;
// Var replacement, for all but first argument of set
for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
argv[i] = do_var_replacement(argv[i]);
unescape_string(argv[i]);
}
/* Strip quotes for first argument.
* TODO This part needs to be handled much better */
if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) {
strip_quotes(argv[1]);
}
if (handler->handle) {
results = handler->handle(argc-1, argv+1);
} else {
results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented");
}
cleanup:
free_argv(argc, argv);
return results;
}
struct cmd_results *config_commands_command(char *exec) {
struct cmd_results *results = NULL;
int argc;
char **argv = split_args(exec, &argc);
if (!argc) {
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
goto cleanup;
}
// Find handler for the command this is setting a policy for
char *cmd = argv[0];
if (strcmp(cmd, "}") == 0) {
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
goto cleanup;
}
struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END);
if (!handler && strcmp(cmd, "*") != 0) {
char *input = cmd ? cmd : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
goto cleanup;
}
enum command_context context = 0;
struct {
char *name;
enum command_context context;
} context_names[] = {
{ "config", CONTEXT_CONFIG },
{ "binding", CONTEXT_BINDING },
{ "ipc", CONTEXT_IPC },
{ "criteria", CONTEXT_CRITERIA },
{ "all", CONTEXT_ALL },
};
for (int i = 1; i < argc; ++i) {
size_t j;
for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) {
if (strcmp(context_names[j].name, argv[i]) == 0) {
break;
}
}
if (j == sizeof(context_names) / sizeof(context_names[0])) {
results = cmd_results_new(CMD_INVALID, cmd,
"Invalid command context %s", argv[i]);
goto cleanup;
}
context |= context_names[j].context;
}
struct command_policy *policy = NULL;
for (int i = 0; i < config->command_policies->length; ++i) {
struct command_policy *p = config->command_policies->items[i];
if (strcmp(p->command, cmd) == 0) {
policy = p;
break;
}
}
if (!policy) {
policy = alloc_command_policy(cmd);
if (!policy) {
sway_abort("Unable to allocate security policy");
}
list_add(config->command_policies, policy);
}
policy->context = context;
sway_log(L_INFO, "Set command policy for %s to %d",
policy->command, policy->context);
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
cleanup:
free_argv(argc, argv);
return results;
}
struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *format, ...) {
struct cmd_results *results = malloc(sizeof(struct cmd_results));
if (!results) {
sway_log(L_ERROR, "Unable to allocate command results");
return NULL;
}
results->status = status;
if (input) {
results->input = strdup(input); // input is the command name
} else {
results->input = NULL;
}
if (format) {
char *error = malloc(256);
va_list args;
va_start(args, format);
if (error) {
vsnprintf(error, 256, format, args);
}
va_end(args);
results->error = error;
} else {
results->error = NULL;
}
return results;
}
void free_cmd_results(struct cmd_results *results) {
if (results->input) {
free(results->input);
}
if (results->error) {
free(results->error);
}
free(results);
}
const char *cmd_results_to_json(struct cmd_results *results) {
json_object *result_array = json_object_new_array();
json_object *root = json_object_new_object();
json_object_object_add(root, "success", json_object_new_boolean(results->status == CMD_SUCCESS));
if (results->input) {
json_object_object_add(root, "input", json_object_new_string(results->input));
}
if (results->error) {
json_object_object_add(root, "error", json_object_new_string(results->error));
}
json_object_array_add(result_array, root);
const char *json = json_object_to_json_string(result_array);
free(result_array);
free(root);
return json;
}
|