aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
blob: 5ed5e070048c17b813e0451c5a6a309697939d8b (plain) (blame)
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
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-names.h>
#include <wlc/wlc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "stringop.h"
#include "layout.h"
#include "movement.h"
#include "log.h"
#include "commands.h"

struct modifier_key {
	char *name;
	uint32_t mod;
};

struct modifier_key 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 },
	{ XKB_MOD_NAME_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 },
};

int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
	if (argc < 2) {
		sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
		return 1;
	}
	argv[0] = do_var_replacement(config, argv[0]);

	struct sway_binding *binding = malloc(sizeof(struct sway_binding));
	binding->keys = create_list();
	binding->modifiers = 0;
	binding->command = join_args(argv + 1, argc - 1);

	list_t *split = split_string(argv[0], "+");
	int i;
	for (i = 0; i < split->length; ++i) {
		// Check for a modifier key
		int j;
		bool is_mod = false;
		for (j = 0; j < 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;
			}
		}
		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) {
			sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]);
			return 1;
		}
		xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
		*key = sym;
		list_add(binding->keys, key);
	}
	list_free(split);

	// TODO: Check if there are other commands with this key binding
	list_add(config->current_mode->bindings, binding);

	sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
	return 0;
}

int cmd_exec(struct sway_config *config, int argc, char **argv) {
	if (argc < 1) {
		sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc);
		return 1;
	}

    if (config->reloading) {
        sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc));
        return 0;
    }

	if (fork() == 0) {
		char *args = join_args(argv, argc);
		sway_log(L_DEBUG, "Executing %s", args);
		execl("/bin/sh", "sh", "-c", args, (char *)NULL);
		free(args);
		exit(0);
	}
	return 0;
}

int cmd_exec_always(struct sway_config *config, int argc, char **argv) {
	if (argc < 1) {
		sway_log(L_ERROR, "Invalid exec_always command (expected at least 1 argument, got %d)", argc);
		return 1;
	}

	if (fork() == 0) {
		char *args = join_args(argv, argc);
		sway_log(L_DEBUG, "Executing %s", args);
		execl("/bin/sh", "sh", "-c", args, (char *)NULL);
		free(args);
		exit(0);
	}
	return 0;
}

int cmd_exit(struct sway_config *config, int argc, char **argv) {
	if (argc != 0) {
		sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc);
		return 1;
	}
	// TODO: Some kind of clean up is probably in order
	exit(0);
	return 0;
}

int cmd_focus(struct sway_config *config, int argc, char **argv) {
	if (argc != 1) {
		sway_log(L_ERROR, "Invalid focus command (expected 1 arguments, got %d)", argc);
		return 1;
	}
	if (strcasecmp(argv[0], "left") == 0) {
		return move_focus(MOVE_LEFT);
	} else if (strcasecmp(argv[0], "right") == 0) {
		return move_focus(MOVE_RIGHT);
	} else if (strcasecmp(argv[0], "up") == 0) {
		return move_focus(MOVE_UP);
	} else if (strcasecmp(argv[0], "down") == 0) {
		return move_focus(MOVE_DOWN);
	} else if (strcasecmp(argv[0], "parent") == 0) {
		return move_focus(MOVE_PARENT);
	}
	return 0;
}

int cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) {
	if (argc != 1) {
		sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc);
		return 1;
	}

	config->focus_follows_mouse = !strcasecmp(argv[0], "yes");
	return 0;
}

int cmd_layout(struct sway_config *config, int argc, char **argv) {
	if (argc < 1) {
		sway_log(L_ERROR, "Invalid layout command (expected at least 1 argument, got %d)", argc);
		return 1;
	}
	swayc_t *parent = get_focused_container(&root_container);
	while (parent->type == C_VIEW) {
		parent = parent->parent;
	}
	if (strcasecmp(argv[0], "splith") == 0) {
		parent->layout = L_HORIZ;
	} else if (strcasecmp(argv[0], "splitv") == 0) {
		parent->layout = L_VERT;
	} else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) {
		if (parent->layout == L_VERT) {
			parent->layout = L_HORIZ;
		} else {
			parent->layout = L_VERT;
		}
	}
	arrange_windows(parent, parent->width, parent->height);

	return 0;
}

int cmd_reload(struct sway_config *config, int argc, char **argv) {
	if (argc != 0) {
		sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc);
		return 1;
	}

	// TODO: Allow use of more config file locations
	const char *name = "/.sway/config";
	const char *home = getenv("HOME");
	char *temp = malloc(strlen(home) + strlen(name) + 1);
	strcpy(temp, home);
	strcat(temp, name);
	FILE *f = fopen(temp, "r");
	if (!f) {
		sway_log(L_ERROR, "Sway config file not found, aborting reload!");
		free(temp);
        return 1;
	}
	free(temp);
	config = read_config(f, true);
	fclose(f);

	return 0;
}

int cmd_set(struct sway_config *config, int argc, char **argv) {
	if (argc != 2) {
		sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
		return 1;
	}
	struct sway_variable *var = malloc(sizeof(struct sway_variable));
	var->name = malloc(strlen(argv[0]) + 1);
	strcpy(var->name, argv[0]);
	var->value = malloc(strlen(argv[1]) + 1);
	strcpy(var->value, argv[1]);
	list_add(config->symbols, var);
	return 0;
}

int _do_split(struct sway_config *config, int argc, char **argv, int layout) {
	if (argc != 0) {
		sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc);
		return 1;
	}
	swayc_t *focused = get_focused_container(&root_container);
	swayc_t *parent = focused->parent;
	sway_log(L_DEBUG, "Splitting %p vertically with %p", parent, focused);
	int index = remove_container_from_parent(parent, focused);
	swayc_t *new_container = create_container(parent, -1);
	new_container->layout = layout;
	new_container->weight = focused->weight;
	new_container->width = focused->width;
	new_container->height = focused->height;
	new_container->x = focused->x;
	new_container->y = focused->y;
	focused->weight = 1;
	focused->parent = new_container;
	list_insert(parent->children, index, new_container);
	list_add(new_container->children, focused);
	focus_view(focused);
	arrange_windows(parent, -1, -1);
	return 0;
}

int cmd_splitv(struct sway_config *config, int argc, char **argv) {
	return _do_split(config, argc, argv, L_VERT);
}

int cmd_splith(struct sway_config *config, int argc, char **argv) {
	return _do_split(config, argc, argv, L_HORIZ);
}

int cmd_log_colors(struct sway_config *config, int argc, char **argv) {
	if (argc != 1) {
		sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc);
		return 1;
	}

	if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) {
		sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
		return 1;
	}

	sway_log_colors(!strcasecmp(argv[0], "yes"));
	return 0;
}

int cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
	if (argc != 1) {
		sway_log(L_ERROR, "Invalid fullscreen command (expected 1 arguments, got %d)", argc);
		return 1;
	}

	swayc_t *container = get_focused_container(&root_container);
	bool current = (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) > 0;
	wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current);
	arrange_windows(container, -1, -1);

	return 1;
}

/* Keep alphabetized */
struct cmd_handler handlers[] = {
	{ "bindsym", cmd_bindsym },
	{ "exec", cmd_exec },
	{ "exec_always", cmd_exec_always },
	{ "exit", cmd_exit },
	{ "focus", cmd_focus },
	{ "focus_follows_mouse", cmd_focus_follows_mouse },
	{ "fullscreen", cmd_fullscreen },
	{ "layout", cmd_layout },
	{ "log_colors", cmd_log_colors },
	{ "reload", cmd_reload },
	{ "set", cmd_set },
	{ "splith", cmd_splith },
	{ "splitv", cmd_splitv }
};

char **split_directive(char *line, int *argc) {
	const char *delimiters = " ";
	*argc = 0;
	while (isspace(*line) && *line) ++line;

	int capacity = 10;
	char **parts = malloc(sizeof(char *) * capacity);

	if (!*line) return parts;

	int in_string = 0, in_character = 0;
	int i, j, _;
	for (i = 0, j = 0; line[i]; ++i) {
		if (line[i] == '\\') {
			++i;
		} else if (line[i] == '"' && !in_character) {
			in_string = !in_string;
		} else if (line[i] == '\'' && !in_string) {
			in_character = !in_character;
		} else if (!in_character && !in_string) {
			if (strchr(delimiters, line[i]) != NULL) {
				char *item = malloc(i - j + 1);
				strncpy(item, line + j, i - j);
				item[i - j] = '\0';
				item = strip_whitespace(item, &_);
				if (item[0] == '\0') {
					free(item);
				} else {
					if (*argc == capacity) {
						capacity *= 2;
						parts = realloc(parts, sizeof(char *) * capacity);
					}
					parts[*argc] = item;
					j = i + 1;
					++*argc;
				}
			}
		}
	}
	char *item = malloc(i - j + 1);
	strncpy(item, line + j, i - j);
	item[i - j] = '\0';
	item = strip_whitespace(item, &_);
	if (*argc == capacity) {
		capacity++;
		parts = realloc(parts, sizeof(char *) * capacity);
	}
	parts[*argc] = item;
	++*argc;
	return parts;
}

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);
}

struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
	struct cmd_handler d = { .command=line };
	struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare);
	return res;
}

int handle_command(struct sway_config *config, char *exec) {
	sway_log(L_INFO, "Handling command '%s'", exec);
	char *ptr, *cmd;
	if ((ptr = strchr(exec, ' ')) == NULL) {
		cmd = malloc(strlen(exec) + 1);
		strcpy(cmd, exec);
	} else {
		int index = ptr - exec;
		cmd = malloc(index + 1);
		strncpy(cmd, exec, index);
		cmd[index] = '\0';
	}
	struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
	if (handler == NULL) {
		sway_log(L_ERROR, "Unknown command '%s'", cmd);
		return 0; // TODO: return error, probably
	}
	int argc;
	char **argv = split_directive(exec + strlen(handler->command), &argc);
	int ret = handler->handle(config, argc, argv);
	int i;
	for (i = 0; i < argc; ++i) {
		free(argv[i]);
	}
	free(argv);
	return ret;
}