aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
commitb374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch)
tree04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands
parentMerge pull request #874 from yohanesu75/ipc-client-fix (diff)
downloadsway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip
refactor commands.c
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/assign.c51
-rw-r--r--sway/commands/bar.c51
-rw-r--r--sway/commands/bar/binding_mode_indicator.c26
-rw-r--r--sway/commands/bar/bindsym.c45
-rw-r--r--sway/commands/bar/colors.c160
-rw-r--r--sway/commands/bar/font.c26
-rw-r--r--sway/commands/bar/height.c21
-rw-r--r--sway/commands/bar/hidden_state.c77
-rw-r--r--sway/commands/bar/id.c32
-rw-r--r--sway/commands/bar/mode.c79
-rw-r--r--sway/commands/bar/modifier.c35
-rw-r--r--sway/commands/bar/output.c49
-rw-r--r--sway/commands/bar/pango_markup.c26
-rw-r--r--sway/commands/bar/position.c32
-rw-r--r--sway/commands/bar/separator_symbol.c20
-rw-r--r--sway/commands/bar/status_command.c21
-rw-r--r--sway/commands/bar/strip_workspace_numbers.c26
-rw-r--r--sway/commands/bar/swaybar_command.c21
-rw-r--r--sway/commands/bar/tray_output.c7
-rw-r--r--sway/commands/bar/tray_padding.c29
-rw-r--r--sway/commands/bar/workspace_buttons.c26
-rw-r--r--sway/commands/bar/wrap_scroll.c26
-rw-r--r--sway/commands/bind.c146
-rw-r--r--sway/commands/border.c64
-rw-r--r--sway/commands/client.c68
-rw-r--r--sway/commands/debuglog.c26
-rw-r--r--sway/commands/exec.c16
-rw-r--r--sway/commands/exec_always.c83
-rw-r--r--sway/commands/exit.c17
-rw-r--r--sway/commands/floating.c80
-rw-r--r--sway/commands/floating_maximum_size.c38
-rw-r--r--sway/commands/floating_minimum_size.c38
-rw-r--r--sway/commands/floating_mod.c41
-rw-r--r--sway/commands/floating_scroll.c44
-rw-r--r--sway/commands/focus.c91
-rw-r--r--sway/commands/focus_follows_mouse.c12
-rw-r--r--sway/commands/font.c26
-rw-r--r--sway/commands/for_window.c37
-rw-r--r--sway/commands/fullscreen.c60
-rw-r--r--sway/commands/gaps.c165
-rw-r--r--sway/commands/hide_edge_borders.c24
-rw-r--r--sway/commands/include.c15
-rw-r--r--sway/commands/input.c52
-rw-r--r--sway/commands/input/accel_profile.c26
-rw-r--r--sway/commands/input/click_method.c29
-rw-r--r--sway/commands/input/drag_lock.c25
-rw-r--r--sway/commands/input/dwt.c25
-rw-r--r--sway/commands/input/events.c29
-rw-r--r--sway/commands/input/middle_emulation.c25
-rw-r--r--sway/commands/input/natural_scroll.c25
-rw-r--r--sway/commands/input/pointer_accel.c24
-rw-r--r--sway/commands/input/scroll_method.c29
-rw-r--r--sway/commands/input/tap.c28
-rw-r--r--sway/commands/kill.c12
-rw-r--r--sway/commands/layout.c66
-rw-r--r--sway/commands/log_colors.c21
-rw-r--r--sway/commands/mode.c52
-rw-r--r--sway/commands/mouse_warping.c16
-rw-r--r--sway/commands/move.c159
-rw-r--r--sway/commands/new_float.c43
-rw-r--r--sway/commands/new_window.c43
-rw-r--r--sway/commands/orientation.c20
-rw-r--r--sway/commands/output.c182
-rw-r--r--sway/commands/reload.c19
-rw-r--r--sway/commands/resize.c122
-rw-r--r--sway/commands/scratchpad.c65
-rw-r--r--sway/commands/seamless_mouse.c12
-rw-r--r--sway/commands/set.c56
-rw-r--r--sway/commands/smart_gaps.c19
-rw-r--r--sway/commands/split.c97
-rw-r--r--sway/commands/sticky.c25
-rw-r--r--sway/commands/workspace.c78
-rw-r--r--sway/commands/workspace_auto_back_and_forth.c17
-rw-r--r--sway/commands/workspace_layout.c20
74 files changed, 3438 insertions, 0 deletions
diff --git a/sway/commands/assign.c b/sway/commands/assign.c
new file mode 100644
index 00000000..31e949fb
--- /dev/null
+++ b/sway/commands/assign.c
@@ -0,0 +1,51 @@
1#include <stdio.h>
2#include <string.h>
3#include "commands.h"
4#include "criteria.h"
5#include "list.h"
6#include "log.h"
7
8struct cmd_results *cmd_assign(int argc, char **argv) {
9 struct cmd_results *error = NULL;
10 if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) {
11 return error;
12 }
13
14 char *criteria = *argv++;
15
16 if (strncmp(*argv, "→", strlen("→")) == 0) {
17 if (argc < 3) {
18 return cmd_results_new(CMD_INVALID, "assign", "Missing workspace");
19 }
20 argv++;
21 }
22
23 char *movecmd = "move container to workspace ";
24 int arglen = strlen(movecmd) + strlen(*argv) + 1;
25 char *cmdlist = calloc(1, arglen);
26
27 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
28
29 struct criteria *crit = malloc(sizeof(struct criteria));
30 crit->crit_raw = strdup(criteria);
31 crit->cmdlist = cmdlist;
32 crit->tokens = create_list();
33 char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw);
34
35 if (err_str) {
36 error = cmd_results_new(CMD_INVALID, "assign", err_str);
37 free(err_str);
38 free_criteria(crit);
39 } else if (crit->tokens->length == 0) {
40 error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria");
41 free_criteria(crit);
42 } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
43 sway_log(L_DEBUG, "assign: Duplicate, skipping.");
44 free_criteria(crit);
45 } else {
46 sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
47 list_add(config->criteria, crit);
48 }
49 return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
50}
51
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
new file mode 100644
index 00000000..9e7d6c5c
--- /dev/null
+++ b/sway/commands/bar.c
@@ -0,0 +1,51 @@
1#include <stdio.h>
2#include <string.h>
3#include "commands.h"
4#include "config.h"
5#include "log.h"
6#include "util.h"
7
8struct cmd_results *cmd_bar(int argc, char **argv) {
9 struct cmd_results *error = NULL;
10 if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
11 return error;
12 }
13
14 if (config->reading && strcmp("{", argv[0]) != 0) {
15 return cmd_results_new(CMD_INVALID, "bar",
16 "Expected '{' at start of bar config definition.");
17 }
18
19 if (!config->reading) {
20 if (argc > 1) {
21 if (strcasecmp("mode", argv[0]) == 0) {
22 return bar_cmd_mode(argc-1, argv + 1);
23 }
24
25 if (strcasecmp("hidden_state", argv[0]) == 0) {
26 return bar_cmd_hidden_state(argc-1, argv + 1);
27 }
28 }
29
30 return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
31 }
32
33 // Create new bar with default values
34 struct bar_config *bar = default_bar_config();
35
36 // set bar id
37 int i;
38 for (i = 0; i < config->bars->length; ++i) {
39 if (bar == config->bars->items[i]) {
40 const int len = 5 + numlen(i); // "bar-" + i + \0
41 bar->id = malloc(len * sizeof(char));
42 snprintf(bar->id, len, "bar-%d", i);
43 break;
44 }
45 }
46
47 // Set current bar
48 config->current_bar = bar;
49 sway_log(L_DEBUG, "Configuring bar %s", bar->id);
50 return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
51}
diff --git a/sway/commands/bar/binding_mode_indicator.c b/sway/commands/bar/binding_mode_indicator.c
new file mode 100644
index 00000000..be72cb9b
--- /dev/null
+++ b/sway/commands/bar/binding_mode_indicator.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_binding_mode_indicator(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "binding_mode_indicator", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "binding_mode_indicator", "No bar defined.");
13 }
14
15 if (strcasecmp("yes", argv[0]) == 0) {
16 config->current_bar->binding_mode_indicator = true;
17 sway_log(L_DEBUG, "Enabling binding mode indicator on bar: %s", config->current_bar->id);
18 } else if (strcasecmp("no", argv[0]) == 0) {
19 config->current_bar->binding_mode_indicator = false;
20 sway_log(L_DEBUG, "Disabling binding mode indicator on bar: %s", config->current_bar->id);
21 } else {
22 error = cmd_results_new(CMD_INVALID, "binding_mode_indicator", "Invalid value %s", argv[0]);
23 return error;
24 }
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bar/bindsym.c b/sway/commands/bar/bindsym.c
new file mode 100644
index 00000000..4f50e708
--- /dev/null
+++ b/sway/commands/bar/bindsym.c
@@ -0,0 +1,45 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "config.h"
5#include "list.h"
6#include "log.h"
7#include "stringop.h"
8
9struct cmd_results *bar_cmd_bindsym(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
12 return error;
13 } else if (!config->reading) {
14 return cmd_results_new(CMD_FAILURE, "bindsym", "Can only be used in config file.");
15 }
16
17 if (!config->current_bar) {
18 return cmd_results_new(CMD_FAILURE, "bindsym", "No bar defined.");
19 }
20
21 if (strlen(argv[1]) != 7) {
22 return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]);
23 }
24 uint32_t numbutton = (uint32_t)atoi(argv[1] + 6);
25 if (numbutton < 1 || numbutton > 5 || strncmp(argv[1], "button", 6) != 0) {
26 return cmd_results_new(CMD_INVALID, "bindsym", "Invalid mouse binding %s", argv[1]);
27 }
28 struct sway_mouse_binding *binding = malloc(sizeof(struct sway_mouse_binding));
29 binding->button = numbutton;
30 binding->command = join_args(argv + 1, argc - 1);
31
32 struct bar_config *bar = config->current_bar;
33 int i = list_seq_find(bar->bindings, sway_mouse_binding_cmp_buttons, binding);
34 if (i > -1) {
35 sway_log(L_DEBUG, "bindsym - '%s' for swaybar already exists, overwriting", argv[0]);
36 struct sway_mouse_binding *dup = bar->bindings->items[i];
37 free_sway_mouse_binding(dup);
38 list_del(bar->bindings, i);
39 }
40 list_add(bar->bindings, binding);
41 list_qsort(bar->bindings, sway_mouse_binding_cmp_qsort);
42
43 sway_log(L_DEBUG, "bindsym - Bound %s to command %s when clicking swaybar", argv[0], binding->command);
44 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
45}
diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c
new file mode 100644
index 00000000..d33a323e
--- /dev/null
+++ b/sway/commands/bar/colors.c
@@ -0,0 +1,160 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *bar_cmd_colors(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 if (strcmp("{", argv[0]) != 0) {
11 return cmd_results_new(CMD_INVALID, "colors",
12 "Expected '{' at the start of colors config definition.");
13 }
14
15 return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL);
16}
17
18struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) {
19 struct cmd_results *error = NULL;
20 if ((error = checkarg(argc, "active_workspace", EXPECTED_EQUAL_TO, 3))) {
21 return error;
22 }
23
24 if ((error = add_color("active_workspace_border", config->current_bar->colors.active_workspace_border, argv[0]))) {
25 return error;
26 }
27
28 if ((error = add_color("active_workspace_bg", config->current_bar->colors.active_workspace_bg, argv[1]))) {
29 return error;
30 }
31
32 if ((error = add_color("active_workspace_text", config->current_bar->colors.active_workspace_text, argv[2]))) {
33 return error;
34 }
35
36 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
37}
38
39struct cmd_results *bar_colors_cmd_background(int argc, char **argv) {
40 struct cmd_results *error = NULL;
41 if ((error = checkarg(argc, "background", EXPECTED_EQUAL_TO, 1))) {
42 return error;
43 }
44
45 if ((error = add_color("background", config->current_bar->colors.background, argv[0]))) {
46 return error;
47 }
48
49 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
50}
51
52struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) {
53 struct cmd_results *error = NULL;
54 if ((error = checkarg(argc, "binding_mode", EXPECTED_EQUAL_TO, 3))) {
55 return error;
56 }
57
58 if ((error = add_color("binding_mode_border", config->current_bar->colors.binding_mode_border, argv[0]))) {
59 return error;
60 }
61
62 if ((error = add_color("binding_mode_bg", config->current_bar->colors.binding_mode_bg, argv[1]))) {
63 return error;
64 }
65
66 if ((error = add_color("binding_mode_text", config->current_bar->colors.binding_mode_text, argv[2]))) {
67 return error;
68 }
69
70 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
71}
72
73struct cmd_results *bar_colors_cmd_focused_workspace(int argc, char **argv) {
74 struct cmd_results *error = NULL;
75 if ((error = checkarg(argc, "focused_workspace", EXPECTED_EQUAL_TO, 3))) {
76 return error;
77 }
78
79 if ((error = add_color("focused_workspace_border", config->current_bar->colors.focused_workspace_border, argv[0]))) {
80 return error;
81 }
82
83 if ((error = add_color("focused_workspace_bg", config->current_bar->colors.focused_workspace_bg, argv[1]))) {
84 return error;
85 }
86
87 if ((error = add_color("focused_workspace_text", config->current_bar->colors.focused_workspace_text, argv[2]))) {
88 return error;
89 }
90
91 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
92}
93
94struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) {
95 struct cmd_results *error = NULL;
96 if ((error = checkarg(argc, "inactive_workspace", EXPECTED_EQUAL_TO, 3))) {
97 return error;
98 }
99
100 if ((error = add_color("inactive_workspace_border", config->current_bar->colors.inactive_workspace_border, argv[0]))) {
101 return error;
102 }
103
104 if ((error = add_color("inactive_workspace_bg", config->current_bar->colors.inactive_workspace_bg, argv[1]))) {
105 return error;
106 }
107
108 if ((error = add_color("inactive_workspace_text", config->current_bar->colors.inactive_workspace_text, argv[2]))) {
109 return error;
110 }
111
112 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
113}
114
115struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) {
116 struct cmd_results *error = NULL;
117 if ((error = checkarg(argc, "separator", EXPECTED_EQUAL_TO, 1))) {
118 return error;
119 }
120
121 if ((error = add_color("separator", config->current_bar->colors.separator, argv[0]))) {
122 return error;
123 }
124
125 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
126}
127
128struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) {
129 struct cmd_results *error = NULL;
130 if ((error = checkarg(argc, "statusline", EXPECTED_EQUAL_TO, 1))) {
131 return error;
132 }
133
134 if ((error = add_color("statusline", config->current_bar->colors.statusline, argv[0]))) {
135 return error;
136 }
137
138 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
139}
140
141struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) {
142 struct cmd_results *error = NULL;
143 if ((error = checkarg(argc, "urgent_workspace", EXPECTED_EQUAL_TO, 3))) {
144 return error;
145 }
146
147 if ((error = add_color("urgent_workspace_border", config->current_bar->colors.urgent_workspace_border, argv[0]))) {
148 return error;
149 }
150
151 if ((error = add_color("urgent_workspace_bg", config->current_bar->colors.urgent_workspace_bg, argv[1]))) {
152 return error;
153 }
154
155 if ((error = add_color("urgent_workspace_text", config->current_bar->colors.urgent_workspace_text, argv[2]))) {
156 return error;
157 }
158
159 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
160}
diff --git a/sway/commands/bar/font.c b/sway/commands/bar/font.c
new file mode 100644
index 00000000..afc542d2
--- /dev/null
+++ b/sway/commands/bar/font.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5
6struct cmd_results *bar_cmd_font(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (!config->current_bar) {
13 return cmd_results_new(CMD_FAILURE, "font", "No bar defined.");
14 }
15
16 char *font = join_args(argv, argc);
17 free(config->current_bar->font);
18 if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) {
19 config->current_bar->font = font;
20 } else {
21 config->current_bar->font = font;
22 }
23
24 sway_log(L_DEBUG, "Settings font '%s' for bar: %s", config->current_bar->font, config->current_bar->id);
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bar/height.c b/sway/commands/bar/height.c
new file mode 100644
index 00000000..9ea0a81a
--- /dev/null
+++ b/sway/commands/bar/height.c
@@ -0,0 +1,21 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "log.h"
5
6struct cmd_results *bar_cmd_height(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "height", EXPECTED_EQUAL_TO, 1))) {
9 return error;
10 }
11
12 int height = atoi(argv[0]);
13 if (height < 0) {
14 return cmd_results_new(CMD_INVALID, "height",
15 "Invalid height value: %s", argv[0]);
16 }
17
18 config->current_bar->height = height;
19 sway_log(L_DEBUG, "Setting bar height to %d on bar: %s", height, config->current_bar->id);
20 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
21}
diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c
new file mode 100644
index 00000000..22035c98
--- /dev/null
+++ b/sway/commands/bar/hidden_state.c
@@ -0,0 +1,77 @@
1#include <string.h>
2#include "commands.h"
3#include "config.h"
4#include "ipc-server.h"
5#include "log.h"
6
7static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, const char *hidden_state) {
8 char *old_state = bar->hidden_state;
9 if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) {
10 if (strcasecmp("hide", bar->hidden_state) == 0) {
11 bar->hidden_state = strdup("show");
12 } else if (strcasecmp("show", bar->hidden_state) == 0) {
13 bar->hidden_state = strdup("hide");
14 }
15 } else if (strcasecmp("hide", hidden_state) == 0) {
16 bar->hidden_state = strdup("hide");
17 } else if (strcasecmp("show", hidden_state) == 0) {
18 bar->hidden_state = strdup("show");
19 } else {
20 return cmd_results_new(CMD_INVALID, "hidden_state", "Invalid value %s", hidden_state);
21 }
22
23 if (strcmp(old_state, bar->hidden_state) != 0) {
24 if (!config->reading) {
25 ipc_event_barconfig_update(bar);
26 }
27 sway_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", bar->hidden_state, bar->id);
28 }
29
30 // free old mode
31 free(old_state);
32 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
33}
34
35struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) {
36 struct cmd_results *error = NULL;
37 if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) {
38 return error;
39 }
40 if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) {
41 return error;
42 }
43
44 if (config->reading && argc > 1) {
45 return cmd_results_new(CMD_INVALID, "hidden_state", "Unexpected value %s in config mode", argv[1]);
46 }
47
48 const char *state = argv[0];
49
50 if (config->reading) {
51 return bar_set_hidden_state(config->current_bar, state);
52 }
53
54 const char *id = NULL;
55 if (argc == 2) {
56 id = argv[1];
57 }
58
59 int i;
60 struct bar_config *bar;
61 for (i = 0; i < config->bars->length; ++i) {
62 bar = config->bars->items[i];
63 if (id && strcmp(id, bar->id) == 0) {
64 return bar_set_hidden_state(bar, state);
65 }
66
67 error = bar_set_hidden_state(bar, state);
68 if (error) {
69 return error;
70 }
71 }
72
73 // active bar modifiers might have changed.
74 update_active_bar_modifiers();
75
76 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
77}
diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c
new file mode 100644
index 00000000..6ca9f6b6
--- /dev/null
+++ b/sway/commands/bar/id.c
@@ -0,0 +1,32 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_id(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "id", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 const char *name = argv[0];
12 const char *oldname = config->current_bar->id;
13
14 // check if id is used by a previously defined bar
15 int i;
16 for (i = 0; i < config->bars->length; ++i) {
17 struct bar_config *find = config->bars->items[i];
18 if (strcmp(name, find->id) == 0 && config->current_bar != find) {
19 return cmd_results_new(CMD_FAILURE, "id",
20 "Id '%s' already defined for another bar. Id unchanged (%s).",
21 name, oldname);
22 }
23 }
24
25 sway_log(L_DEBUG, "Renaming bar: '%s' to '%s'", oldname, name);
26
27 // free old bar id
28 free(config->current_bar->id);
29
30 config->current_bar->id = strdup(name);
31 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
32}
diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c
new file mode 100644
index 00000000..321d5a29
--- /dev/null
+++ b/sway/commands/bar/mode.c
@@ -0,0 +1,79 @@
1#include <string.h>
2#include "commands.h"
3#include "config.h"
4#include "ipc-server.h"
5#include "log.h"
6
7static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode) {
8 char *old_mode = bar->mode;
9 if (strcasecmp("toggle", mode) == 0 && !config->reading) {
10 if (strcasecmp("dock", bar->mode) == 0) {
11 bar->mode = strdup("hide");
12 } else if (strcasecmp("hide", bar->mode) == 0) {
13 bar->mode = strdup("dock");
14 }
15 } else if (strcasecmp("dock", mode) == 0) {
16 bar->mode = strdup("dock");
17 } else if (strcasecmp("hide", mode) == 0) {
18 bar->mode = strdup("hide");
19 } else if (strcasecmp("invisible", mode) == 0) {
20 bar->mode = strdup("invisible");
21 } else {
22 return cmd_results_new(CMD_INVALID, "mode", "Invalid value %s", mode);
23 }
24
25 if (strcmp(old_mode, bar->mode) != 0) {
26 if (!config->reading) {
27 ipc_event_barconfig_update(bar);
28
29 // active bar modifiers might have changed.
30 update_active_bar_modifiers();
31 }
32 sway_log(L_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id);
33 }
34
35 // free old mode
36 free(old_mode);
37 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
38}
39
40struct cmd_results *bar_cmd_mode(int argc, char **argv) {
41 struct cmd_results *error = NULL;
42 if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
43 return error;
44 }
45 if ((error = checkarg(argc, "mode", EXPECTED_LESS_THAN, 3))) {
46 return error;
47 }
48
49 if (config->reading && argc > 1) {
50 return cmd_results_new(CMD_INVALID, "mode", "Unexpected value %s in config mode", argv[1]);
51 }
52
53 const char *mode = argv[0];
54
55 if (config->reading) {
56 return bar_set_mode(config->current_bar, mode);
57 }
58
59 const char *id = NULL;
60 if (argc == 2) {
61 id = argv[1];
62 }
63
64 int i;
65 struct bar_config *bar;
66 for (i = 0; i < config->bars->length; ++i) {
67 bar = config->bars->items[i];
68 if (id && strcmp(id, bar->id) == 0) {
69 return bar_set_mode(bar, mode);
70 }
71
72 error = bar_set_mode(bar, mode);
73 if (error) {
74 return error;
75 }
76 }
77
78 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
79}
diff --git a/sway/commands/bar/modifier.c b/sway/commands/bar/modifier.c
new file mode 100644
index 00000000..7d4501c7
--- /dev/null
+++ b/sway/commands/bar/modifier.c
@@ -0,0 +1,35 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5#include "util.h"
6
7struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
8 struct cmd_results *error = NULL;
9 if ((error = checkarg(argc, "modifier", EXPECTED_EQUAL_TO, 1))) {
10 return error;
11 }
12
13 if (!config->current_bar) {
14 return cmd_results_new(CMD_FAILURE, "modifier", "No bar defined.");
15 }
16
17 uint32_t mod = 0;
18
19 list_t *split = split_string(argv[0], "+");
20 for (int i = 0; i < split->length; ++i) {
21 uint32_t tmp_mod;
22 if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) {
23 mod |= tmp_mod;
24 continue;
25 } else {
26 free_flat_list(split);
27 return cmd_results_new(CMD_INVALID, "modifier", "Unknown modifier '%s'", split->items[i]);
28 }
29 }
30 free_flat_list(split);
31
32 config->current_bar->modifier = mod;
33 sway_log(L_DEBUG, "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]);
34 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
35}
diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c
new file mode 100644
index 00000000..9a0d7a73
--- /dev/null
+++ b/sway/commands/bar/output.c
@@ -0,0 +1,49 @@
1#include <string.h>
2#include "commands.h"
3#include "list.h"
4#include "log.h"
5
6struct cmd_results *bar_cmd_output(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "output", EXPECTED_EQUAL_TO, 1))) {
9 return error;
10 }
11
12 if (!config->current_bar) {
13 return cmd_results_new(CMD_FAILURE, "output", "No bar defined.");
14 }
15
16 const char *output = argv[0];
17 list_t *outputs = config->current_bar->outputs;
18 if (!outputs) {
19 outputs = create_list();
20 config->current_bar->outputs = outputs;
21 }
22
23 int i;
24 int add_output = 1;
25 if (strcmp("*", output) == 0) {
26 // remove all previous defined outputs and replace with '*'
27 for (i = 0; i < outputs->length; ++i) {
28 free(outputs->items[i]);
29 list_del(outputs, i);
30 }
31 } else {
32 // only add output if not already defined with either the same
33 // name or as '*'
34 for (i = 0; i < outputs->length; ++i) {
35 const char *find = outputs->items[i];
36 if (strcmp("*", find) == 0 || strcmp(output, find) == 0) {
37 add_output = 0;
38 break;
39 }
40 }
41 }
42
43 if (add_output) {
44 list_add(outputs, strdup(output));
45 sway_log(L_DEBUG, "Adding bar: '%s' to output '%s'", config->current_bar->id, output);
46 }
47
48 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
49}
diff --git a/sway/commands/bar/pango_markup.c b/sway/commands/bar/pango_markup.c
new file mode 100644
index 00000000..fcc38d13
--- /dev/null
+++ b/sway/commands/bar/pango_markup.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_pango_markup(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "pango_markup", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "pango_markup", "No bar defined.");
13 }
14
15 if (strcasecmp("enabled", argv[0]) == 0) {
16 config->current_bar->pango_markup = true;
17 sway_log(L_DEBUG, "Enabling pango markup for bar: %s", config->current_bar->id);
18 } else if (strcasecmp("disabled", argv[0]) == 0) {
19 config->current_bar->pango_markup = false;
20 sway_log(L_DEBUG, "Disabling pango markup for bar: %s", config->current_bar->id);
21 } else {
22 error = cmd_results_new(CMD_INVALID, "pango_markup", "Invalid value %s", argv[0]);
23 return error;
24 }
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bar/position.c b/sway/commands/bar/position.c
new file mode 100644
index 00000000..6f97062f
--- /dev/null
+++ b/sway/commands/bar/position.c
@@ -0,0 +1,32 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_position(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "position", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "position", "No bar defined.");
13 }
14
15 if (strcasecmp("top", argv[0]) == 0) {
16 config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_TOP;
17 } else if (strcasecmp("bottom", argv[0]) == 0) {
18 config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
19 } else if (strcasecmp("left", argv[0]) == 0) {
20 sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV");
21 config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_LEFT;
22 } else if (strcasecmp("right", argv[0]) == 0) {
23 sway_log(L_INFO, "Warning: swaybar currently only supports top and bottom positioning. YMMV");
24 config->current_bar->position = DESKTOP_SHELL_PANEL_POSITION_RIGHT;
25 } else {
26 error = cmd_results_new(CMD_INVALID, "position", "Invalid value %s", argv[0]);
27 return error;
28 }
29
30 sway_log(L_DEBUG, "Setting bar position '%s' for bar: %s", argv[0], config->current_bar->id);
31 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
32}
diff --git a/sway/commands/bar/separator_symbol.c b/sway/commands/bar/separator_symbol.c
new file mode 100644
index 00000000..16c455e7
--- /dev/null
+++ b/sway/commands/bar/separator_symbol.c
@@ -0,0 +1,20 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_separator_symbol(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "separator_symbol", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "separator_symbol", "No bar defined.");
13 }
14
15 free(config->current_bar->separator_symbol);
16 config->current_bar->separator_symbol = strdup(argv[0]);
17 sway_log(L_DEBUG, "Settings separator_symbol '%s' for bar: %s", config->current_bar->separator_symbol, config->current_bar->id);
18
19 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
20}
diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c
new file mode 100644
index 00000000..ec91e9f7
--- /dev/null
+++ b/sway/commands/bar/status_command.c
@@ -0,0 +1,21 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5
6struct cmd_results *bar_cmd_status_command(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "status_command", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (!config->current_bar) {
13 return cmd_results_new(CMD_FAILURE, "status_command", "No bar defined.");
14 }
15
16 free(config->current_bar->status_command);
17 config->current_bar->status_command = join_args(argv, argc);
18 sway_log(L_DEBUG, "Feeding bar with status command: %s", config->current_bar->status_command);
19
20 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
21}
diff --git a/sway/commands/bar/strip_workspace_numbers.c b/sway/commands/bar/strip_workspace_numbers.c
new file mode 100644
index 00000000..7a709e4b
--- /dev/null
+++ b/sway/commands/bar/strip_workspace_numbers.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "strip_workspace_numbers", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "strip_workspace_numbers", "No bar defined.");
13 }
14
15 if (strcasecmp("yes", argv[0]) == 0) {
16 config->current_bar->strip_workspace_numbers = true;
17 sway_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id);
18 } else if (strcasecmp("no", argv[0]) == 0) {
19 config->current_bar->strip_workspace_numbers = false;
20 sway_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id);
21 } else {
22 error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]);
23 return error;
24 }
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bar/swaybar_command.c b/sway/commands/bar/swaybar_command.c
new file mode 100644
index 00000000..1f28184a
--- /dev/null
+++ b/sway/commands/bar/swaybar_command.c
@@ -0,0 +1,21 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5
6struct cmd_results *bar_cmd_swaybar_command(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "swaybar_command", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (!config->current_bar) {
13 return cmd_results_new(CMD_FAILURE, "swaybar_command", "No bar defined.");
14 }
15
16 free(config->current_bar->swaybar_command);
17 config->current_bar->swaybar_command = join_args(argv, argc);
18 sway_log(L_DEBUG, "Using custom swaybar command: %s", config->current_bar->swaybar_command);
19
20 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
21}
diff --git a/sway/commands/bar/tray_output.c b/sway/commands/bar/tray_output.c
new file mode 100644
index 00000000..39b571d0
--- /dev/null
+++ b/sway/commands/bar/tray_output.c
@@ -0,0 +1,7 @@
1#include "commands.h"
2#include "log.h"
3
4struct cmd_results *bar_cmd_tray_output(int argc, char **argv) {
5 sway_log(L_ERROR, "Warning: tray_output is not supported on wayland");
6 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
7}
diff --git a/sway/commands/bar/tray_padding.c b/sway/commands/bar/tray_padding.c
new file mode 100644
index 00000000..978ce0b7
--- /dev/null
+++ b/sway/commands/bar/tray_padding.c
@@ -0,0 +1,29 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "log.h"
5
6struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "tray_padding", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (!config->current_bar) {
13 return cmd_results_new(CMD_FAILURE, "tray_padding", "No bar defined.");
14 }
15
16 int padding = atoi(argv[0]);
17 if (padding < 0) {
18 return cmd_results_new(CMD_INVALID, "tray_padding",
19 "Invalid padding value %s, minimum is 0", argv[0]);
20 }
21
22 if (argc > 1 && strcasecmp("px", argv[1]) != 0) {
23 return cmd_results_new(CMD_INVALID, "tray_padding",
24 "Unknown unit %s", argv[1]);
25 }
26 config->current_bar->tray_padding = padding;
27 sway_log(L_DEBUG, "Enabling tray padding of %d px on bar: %s", padding, config->current_bar->id);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29}
diff --git a/sway/commands/bar/workspace_buttons.c b/sway/commands/bar/workspace_buttons.c
new file mode 100644
index 00000000..ce37fde9
--- /dev/null
+++ b/sway/commands/bar/workspace_buttons.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined.");
13 }
14
15 if (strcasecmp("yes", argv[0]) == 0) {
16 config->current_bar->workspace_buttons = true;
17 sway_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id);
18 } else if (strcasecmp("no", argv[0]) == 0) {
19 config->current_bar->workspace_buttons = false;
20 sway_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id);
21 } else {
22 error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]);
23 return error;
24 }
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bar/wrap_scroll.c b/sway/commands/bar/wrap_scroll.c
new file mode 100644
index 00000000..0a5fbf93
--- /dev/null
+++ b/sway/commands/bar/wrap_scroll.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *bar_cmd_wrap_scroll(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "wrap_scroll", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 if (!config->current_bar) {
12 return cmd_results_new(CMD_FAILURE, "wrap_scroll", "No bar defined.");
13 }
14
15 if (strcasecmp("yes", argv[0]) == 0) {
16 config->current_bar->wrap_scroll = true;
17 sway_log(L_DEBUG, "Enabling wrap scroll on bar: %s", config->current_bar->id);
18 } else if (strcasecmp("no", argv[0]) == 0) {
19 config->current_bar->wrap_scroll = false;
20 sway_log(L_DEBUG, "Disabling wrap scroll on bar: %s", config->current_bar->id);
21 } else {
22 error = cmd_results_new(CMD_INVALID, "wrap_scroll", "Invalid value %s", argv[0]);
23 return error;
24 }
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
new file mode 100644
index 00000000..b511f082
--- /dev/null
+++ b/sway/commands/bind.c
@@ -0,0 +1,146 @@
1#include <xkbcommon/xkbcommon.h>
2#include <xkbcommon/xkbcommon-names.h>
3#include "commands.h"
4#include "config.h"
5#include "list.h"
6#include "log.h"
7#include "stringop.h"
8#include "util.h"
9
10int binding_order = 0;
11
12struct cmd_results *cmd_bindsym(int argc, char **argv) {
13 struct cmd_results *error = NULL;
14 if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
15 return error;
16 }
17
18 struct sway_binding *binding = malloc(sizeof(struct sway_binding));
19 binding->keys = create_list();
20 binding->modifiers = 0;
21 binding->release = false;
22 binding->bindcode = false;
23
24 // Handle --release
25 if (strcmp("--release", argv[0]) == 0) {
26 if (argc >= 3) {
27 binding->release = true;
28 argv++;
29 argc--;
30 } else {
31 free_sway_binding(binding);
32 return cmd_results_new(CMD_FAILURE, "bindsym",
33 "Invalid bindsym command "
34 "(expected more than 2 arguments, got %d)", argc);
35 }
36 }
37
38 binding->command = join_args(argv + 1, argc - 1);
39
40 list_t *split = split_string(argv[0], "+");
41 for (int i = 0; i < split->length; ++i) {
42 // Check for a modifier key
43 uint32_t mod;
44 if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) {
45 binding->modifiers |= mod;
46 continue;
47 }
48 // Check for xkb key
49 xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
50 if (!sym) {
51 error = cmd_results_new(CMD_INVALID, "bindsym", "Unknown key '%s'", (char *)split->items[i]);
52 free_sway_binding(binding);
53 list_free(split);
54 return error;
55 }
56 xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
57 *key = sym;
58 list_add(binding->keys, key);
59 }
60 free_flat_list(split);
61
62 struct sway_mode *mode = config->current_mode;
63 int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding);
64 if (i > -1) {
65 sway_log(L_DEBUG, "bindsym - '%s' already exists, overwriting", argv[0]);
66 struct sway_binding *dup = mode->bindings->items[i];
67 free_sway_binding(dup);
68 list_del(mode->bindings, i);
69 }
70 binding->order = binding_order++;
71 list_add(mode->bindings, binding);
72 list_qsort(mode->bindings, sway_binding_cmp_qsort);
73
74 sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
75 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
76}
77
78struct cmd_results *cmd_bindcode(int argc, char **argv) {
79 struct cmd_results *error = NULL;
80 if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) {
81 return error;
82 }
83
84 struct sway_binding *binding = malloc(sizeof(struct sway_binding));
85 binding->keys = create_list();
86 binding->modifiers = 0;
87 binding->release = false;
88 binding->bindcode = true;
89
90 // Handle --release
91 if (strcmp("--release", argv[0]) == 0) {
92 if (argc >= 3) {
93 binding->release = true;
94 argv++;
95 argc--;
96 } else {
97 free_sway_binding(binding);
98 return cmd_results_new(CMD_FAILURE, "bindcode",
99 "Invalid bindcode command "
100 "(expected more than 2 arguments, got %d)", argc);
101 }
102 }
103
104 binding->command = join_args(argv + 1, argc - 1);
105
106 list_t *split = split_string(argv[0], "+");
107 for (int i = 0; i < split->length; ++i) {
108 // Check for a modifier key
109 uint32_t mod;
110 if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) {
111 binding->modifiers |= mod;
112 continue;
113 }
114 // parse keycode
115 xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10);
116 if (!xkb_keycode_is_legal_ext(keycode)) {
117 error = cmd_results_new(CMD_INVALID, "bindcode", "Invalid keycode '%s'", (char *)split->items[i]);
118 free_sway_binding(binding);
119 list_free(split);
120 return error;
121 }
122 xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t));
123 *key = keycode - 8;
124 list_add(binding->keys, key);
125 }
126 free_flat_list(split);
127
128 struct sway_mode *mode = config->current_mode;
129 int i = list_seq_find(mode->bindings, sway_binding_cmp_keys, binding);
130 if (i > -1) {
131 struct sway_binding *dup = mode->bindings->items[i];
132 if (dup->bindcode) {
133 sway_log(L_DEBUG, "bindcode - '%s' already exists, overwriting", argv[0]);
134 } else {
135 sway_log(L_DEBUG, "bindcode - '%s' already exists as bindsym, overwriting", argv[0]);
136 }
137 free_sway_binding(dup);
138 list_del(mode->bindings, i);
139 }
140 binding->order = binding_order++;
141 list_add(mode->bindings, binding);
142 list_qsort(mode->bindings, sway_binding_cmp_qsort);
143
144 sway_log(L_DEBUG, "bindcode - Bound %s to command %s", argv[0], binding->command);
145 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
146}
diff --git a/sway/commands/border.c b/sway/commands/border.c
new file mode 100644
index 00000000..30798c6d
--- /dev/null
+++ b/sway/commands/border.c
@@ -0,0 +1,64 @@
1#include <errno.h>
2#include <stdlib.h>
3#include <string.h>
4#include "commands.h"
5#include "container.h"
6#include "focus.h"
7
8struct cmd_results *cmd_border(int argc, char **argv) {
9 struct cmd_results *error = NULL;
10 if (!config->active) {
11 return cmd_results_new(CMD_FAILURE, "border", "Can only be used when sway is running.");
12 }
13 if ((error = checkarg(argc, "border", EXPECTED_AT_LEAST, 1))) {
14 return error;
15 }
16
17 if (argc > 2) {
18 return cmd_results_new(CMD_INVALID, "border",
19 "Expected 'border <normal|pixel|none|toggle> [<n>]");
20 }
21
22 swayc_t *view = get_focused_view(&root_container);
23 enum swayc_border_types border = view->border_type;
24 int thickness = view->border_thickness;
25
26 if (strcasecmp(argv[0], "none") == 0) {
27 border = B_NONE;
28 } else if (strcasecmp(argv[0], "normal") == 0) {
29 border = B_NORMAL;
30 } else if (strcasecmp(argv[0], "pixel") == 0) {
31 border = B_PIXEL;
32 } else if (strcasecmp(argv[0], "toggle") == 0) {
33 switch (border) {
34 case B_NONE:
35 border = B_PIXEL;
36 break;
37 case B_NORMAL:
38 border = B_NONE;
39 break;
40 case B_PIXEL:
41 border = B_NORMAL;
42 break;
43 }
44 } else {
45 return cmd_results_new(CMD_INVALID, "border",
46 "Expected 'border <normal|pixel|none|toggle>");
47 }
48
49 if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) {
50 thickness = (int)strtol(argv[1], NULL, 10);
51 if (errno == ERANGE || thickness < 0) {
52 errno = 0;
53 return cmd_results_new(CMD_INVALID, "border", "Number is out of range.");
54 }
55 }
56
57 if (view) {
58 view->border_type = border;
59 view->border_thickness = thickness;
60 update_geometry(view);
61 }
62
63 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
64}
diff --git a/sway/commands/client.c b/sway/commands/client.c
new file mode 100644
index 00000000..993fa2f8
--- /dev/null
+++ b/sway/commands/client.c
@@ -0,0 +1,68 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4
5static struct cmd_results *parse_border_color(struct border_colors *border_colors, const char *cmd_name, int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if (argc != 5) {
8 return cmd_results_new(CMD_INVALID, cmd_name, "Requires exactly five color values");
9 }
10
11 uint32_t colors[5];
12 int i;
13 for (i = 0; i < 5; i++) {
14 char buffer[10];
15 error = add_color(cmd_name, buffer, argv[i]);
16 if (error) {
17 return error;
18 }
19 colors[i] = strtoul(buffer+1, NULL, 16);
20 }
21
22 border_colors->border = colors[0];
23 border_colors->background = colors[1];
24 border_colors->text = colors[2];
25 border_colors->indicator = colors[3];
26 border_colors->child_border = colors[4];
27
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29}
30
31struct cmd_results *cmd_client_focused(int argc, char **argv) {
32 return parse_border_color(&config->border_colors.focused, "client.focused", argc, argv);
33}
34
35struct cmd_results *cmd_client_focused_inactive(int argc, char **argv) {
36 return parse_border_color(&config->border_colors.focused_inactive, "client.focused_inactive", argc, argv);
37}
38
39struct cmd_results *cmd_client_unfocused(int argc, char **argv) {
40 return parse_border_color(&config->border_colors.unfocused, "client.unfocused", argc, argv);
41}
42
43struct cmd_results *cmd_client_urgent(int argc, char **argv) {
44 return parse_border_color(&config->border_colors.urgent, "client.urgent", argc, argv);
45}
46
47struct cmd_results *cmd_client_placeholder(int argc, char **argv) {
48 return parse_border_color(&config->border_colors.placeholder, "client.placeholder", argc, argv);
49}
50
51struct cmd_results *cmd_client_background(int argc, char **argv) {
52 char buffer[10];
53 struct cmd_results *error = NULL;
54 uint32_t background;
55
56 if (argc != 1) {
57 return cmd_results_new(CMD_INVALID, "client.background", "Requires exactly one color value");
58 }
59
60 error = add_color("client.background", buffer, argv[0]);
61 if (error) {
62 return error;
63 }
64
65 background = strtoul(buffer+1, NULL, 16);
66 config->border_colors.background = background;
67 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
68}
diff --git a/sway/commands/debuglog.c b/sway/commands/debuglog.c
new file mode 100644
index 00000000..55749797
--- /dev/null
+++ b/sway/commands/debuglog.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *cmd_debuglog(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "debuglog", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 } else if (strcasecmp(argv[0], "toggle") == 0) {
10 if (config->reading) {
11 return cmd_results_new(CMD_FAILURE, "debuglog toggle", "Can't be used in config file.");
12 }
13 if (toggle_debug_logging()) {
14 sway_log(L_DEBUG, "Debuglog turned on.");
15 }
16 } else if (strcasecmp(argv[0], "on") == 0) {
17 set_log_level(L_DEBUG);
18 sway_log(L_DEBUG, "Debuglog turned on.");
19 } else if (strcasecmp(argv[0], "off") == 0) {
20 reset_log_level();
21 } else {
22 return cmd_results_new(CMD_FAILURE, "debuglog", "Expected 'debuglog on|off|toggle'");
23 }
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
26
diff --git a/sway/commands/exec.c b/sway/commands/exec.c
new file mode 100644
index 00000000..a84ad797
--- /dev/null
+++ b/sway/commands/exec.c
@@ -0,0 +1,16 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5
6struct cmd_results *cmd_exec(int argc, char **argv) {
7 if (!config->active) return cmd_results_new(CMD_DEFER, "exec", NULL);
8 if (config->reloading) {
9 char *args = join_args(argv, argc);
10 sway_log(L_DEBUG, "Ignoring 'exec %s' due to reload", args);
11 free(args);
12 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
13 }
14 return cmd_exec_always(argc, argv);
15}
16
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c
new file mode 100644
index 00000000..dbac9f9a
--- /dev/null
+++ b/sway/commands/exec_always.c
@@ -0,0 +1,83 @@
1#include <string.h>
2#include <sys/wait.h>
3#include <unistd.h>
4#include "commands.h"
5#include "config.h"
6#include "log.h"
7#include "stringop.h"
8
9struct cmd_results *cmd_exec_always(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if (!config->active) return cmd_results_new(CMD_DEFER, NULL, NULL);
12 if ((error = checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0))) {
13 return error;
14 }
15
16 char *tmp = NULL;
17 if (strcmp((char*)*argv, "--no-startup-id") == 0) {
18 sway_log(L_INFO, "exec switch '--no-startup-id' not supported, ignored.");
19 if ((error = checkarg(argc - 1, "exec_always", EXPECTED_MORE_THAN, 0))) {
20 return error;
21 }
22
23 tmp = join_args(argv + 1, argc - 1);
24 } else {
25 tmp = join_args(argv, argc);
26 }
27
28 // Put argument into cmd array
29 char cmd[4096];
30 strncpy(cmd, tmp, sizeof(cmd));
31 cmd[sizeof(cmd) - 1] = 0;
32 free(tmp);
33 sway_log(L_DEBUG, "Executing %s", cmd);
34
35 int fd[2];
36 if (pipe(fd) != 0) {
37 sway_log(L_ERROR, "Unable to create pipe for fork");
38 }
39
40 pid_t pid;
41 pid_t *child = malloc(sizeof(pid_t)); // malloc'd so that Linux can avoid copying the process space
42 // Fork process
43 if ((pid = fork()) == 0) {
44 // Fork child process again
45 setsid();
46 if ((*child = fork()) == 0) {
47 execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
48 /* Not reached */
49 }
50 close(fd[0]);
51 ssize_t s = 0;
52 while ((size_t)s < sizeof(pid_t)) {
53 s += write(fd[1], ((uint8_t *)child) + s, sizeof(pid_t) - s);
54 }
55 close(fd[1]);
56 _exit(0); // Close child process
57 } else if (pid < 0) {
58 free(child);
59 return cmd_results_new(CMD_FAILURE, "exec_always", "Command failed (sway could not fork).");
60 }
61 close(fd[1]); // close write
62 ssize_t s = 0;
63 while ((size_t)s < sizeof(pid_t)) {
64 s += read(fd[0], ((uint8_t *)child) + s, sizeof(pid_t) - s);
65 }
66 close(fd[0]);
67 // cleanup child process
68 wait(0);
69 swayc_t *ws = swayc_active_workspace();
70 if (*child > 0 && ws) {
71 sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name);
72 struct pid_workspace *pw = malloc(sizeof(struct pid_workspace));
73 pw->pid = child;
74 pw->workspace = strdup(ws->name);
75 pid_workspace_add(pw);
76 // TODO: keep track of this pid and open the corresponding view on the current workspace
77 // blocked pending feature in wlc
78 } else {
79 free(child);
80 }
81
82 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
83}
diff --git a/sway/commands/exit.c b/sway/commands/exit.c
new file mode 100644
index 00000000..fe3212a4
--- /dev/null
+++ b/sway/commands/exit.c
@@ -0,0 +1,17 @@
1#include "commands.h"
2#include "container.h"
3
4void sway_terminate(int exit_code);
5
6struct cmd_results *cmd_exit(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if (config->reading) return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file.");
9 if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) {
10 return error;
11 }
12 // Close all views
13 close_views(&root_container);
14 sway_terminate(EXIT_SUCCESS);
15 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
16}
17
diff --git a/sway/commands/floating.c b/sway/commands/floating.c
new file mode 100644
index 00000000..2323902d
--- /dev/null
+++ b/sway/commands/floating.c
@@ -0,0 +1,80 @@
1#include <string.h>
2#include "commands.h"
3#include "container.h"
4#include "ipc-server.h"
5#include "layout.h"
6#include "list.h"
7#include "log.h"
8
9struct cmd_results *cmd_floating(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if (config->reading) return cmd_results_new(CMD_FAILURE, "floating", "Can't be used in config file.");
12 if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) {
13 return error;
14 }
15 swayc_t *view = get_focused_container(&root_container);
16 bool wants_floating;
17 if (strcasecmp(argv[0], "enable") == 0) {
18 wants_floating = true;
19 } else if (strcasecmp(argv[0], "disable") == 0) {
20 wants_floating = false;
21 } else if (strcasecmp(argv[0], "toggle") == 0) {
22 wants_floating = !view->is_floating;
23 } else {
24 return cmd_results_new(CMD_FAILURE, "floating",
25 "Expected 'floating <enable|disable|toggle>");
26 }
27
28 // Prevent running floating commands on things like workspaces
29 if (view->type != C_VIEW) {
30 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
31 }
32
33 // Change from nonfloating to floating
34 if (!view->is_floating && wants_floating) {
35 // Remove view from its current location
36 destroy_container(remove_child(view));
37
38 // and move it into workspace floating
39 add_floating(swayc_active_workspace(), view);
40 view->x = (swayc_active_workspace()->width - view->width)/2;
41 view->y = (swayc_active_workspace()->height - view->height)/2;
42 if (view->desired_width != -1) {
43 view->width = view->desired_width;
44 }
45 if (view->desired_height != -1) {
46 view->height = view->desired_height;
47 }
48 arrange_windows(swayc_active_workspace(), -1, -1);
49
50 } else if (view->is_floating && !wants_floating) {
51 // Delete the view from the floating list and unset its is_floating flag
52 remove_child(view);
53 view->is_floating = false;
54 // Get the properly focused container, and add in the view there
55 swayc_t *focused = container_under_pointer();
56 // If focused is null, it's because the currently focused container is a workspace
57 if (focused == NULL || focused->is_floating) {
58 focused = swayc_active_workspace();
59 }
60 set_focused_container(focused);
61
62 sway_log(L_DEBUG, "Non-floating focused container is %p", focused);
63
64 // Case of focused workspace, just create as child of it
65 if (focused->type == C_WORKSPACE) {
66 add_child(focused, view);
67 }
68 // Regular case, create as sibling of current container
69 else {
70 add_sibling(focused, view);
71 }
72 // Refocus on the view once its been put back into the layout
73 view->width = view->height = 0;
74 arrange_windows(swayc_active_workspace(), -1, -1);
75 remove_view_from_scratchpad(view);
76 ipc_event_window(view, "floating");
77 }
78 set_focused_container(view);
79 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
80}
diff --git a/sway/commands/floating_maximum_size.c b/sway/commands/floating_maximum_size.c
new file mode 100644
index 00000000..1728433f
--- /dev/null
+++ b/sway/commands/floating_maximum_size.c
@@ -0,0 +1,38 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "log.h"
5
6struct cmd_results *cmd_floating_maximum_size(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 int32_t width;
9 int32_t height;
10 char *ptr;
11
12 if ((error = checkarg(argc, "floating_maximum_size", EXPECTED_EQUAL_TO, 3))) {
13 return error;
14 }
15 width = strtol(argv[0], &ptr, 10);
16 height = strtol(argv[2], &ptr, 10);
17
18 if (width < -1) {
19 sway_log(L_DEBUG, "floating_maximum_size invalid width value: '%s'", argv[0]);
20
21 } else {
22 config->floating_maximum_width = width;
23
24 }
25
26 if (height < -1) {
27 sway_log(L_DEBUG, "floating_maximum_size invalid height value: '%s'", argv[2]);
28 }
29 else {
30 config->floating_maximum_height = height;
31
32 }
33
34 sway_log(L_DEBUG, "New floating_maximum_size: '%d' x '%d'", config->floating_maximum_width,
35 config->floating_maximum_height);
36
37 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
38}
diff --git a/sway/commands/floating_minimum_size.c b/sway/commands/floating_minimum_size.c
new file mode 100644
index 00000000..743e9b8f
--- /dev/null
+++ b/sway/commands/floating_minimum_size.c
@@ -0,0 +1,38 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "log.h"
5
6struct cmd_results *cmd_floating_minimum_size(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 int32_t width;
9 int32_t height;
10 char *ptr;
11
12 if ((error = checkarg(argc, "floating_minimum_size", EXPECTED_EQUAL_TO, 3))) {
13 return error;
14 }
15 width = strtol(argv[0], &ptr, 10);
16 height = strtol(argv[2], &ptr, 10);
17
18 if (width <= 0) {
19 sway_log(L_DEBUG, "floating_minimum_size invalid width value: '%s'", argv[0]);
20
21 } else {
22 config->floating_minimum_width = width;
23
24 }
25
26 if (height <= 0) {
27 sway_log(L_DEBUG, "floating_minimum_size invalid height value: '%s'", argv[2]);
28 }
29 else {
30 config->floating_minimum_height = height;
31
32 }
33
34 sway_log(L_DEBUG, "New floating_minimum_size: '%d' x '%d'", config->floating_minimum_width,
35 config->floating_minimum_height);
36
37 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
38}
diff --git a/sway/commands/floating_mod.c b/sway/commands/floating_mod.c
new file mode 100644
index 00000000..ce463429
--- /dev/null
+++ b/sway/commands/floating_mod.c
@@ -0,0 +1,41 @@
1#include <string.h>
2#include "commands.h"
3#include "input_state.h"
4#include "list.h"
5#include "log.h"
6#include "stringop.h"
7#include "util.h"
8
9struct cmd_results *cmd_floating_mod(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "floating_modifier", EXPECTED_AT_LEAST, 1))) {
12 return error;
13 }
14 int i;
15 list_t *split = split_string(argv[0], "+");
16 config->floating_mod = 0;
17
18 // set modifier keys
19 for (i = 0; i < split->length; ++i) {
20 config->floating_mod |= get_modifier_mask_by_name(split->items[i]);
21 }
22 free_flat_list(split);
23 if (!config->floating_mod) {
24 error = cmd_results_new(CMD_INVALID, "floating_modifier", "Unknown keys %s", argv[0]);
25 return error;
26 }
27
28 if (argc >= 2) {
29 if (strcasecmp("inverse", argv[1]) == 0) {
30 config->dragging_key = M_RIGHT_CLICK;
31 config->resizing_key = M_LEFT_CLICK;
32 } else if (strcasecmp("normal", argv[1]) == 0) {
33 config->dragging_key = M_LEFT_CLICK;
34 config->resizing_key = M_RIGHT_CLICK;
35 } else {
36 error = cmd_results_new(CMD_INVALID, "floating_modifier", "Invalid definition %s", argv[1]);
37 return error;
38 }
39 }
40 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
41}
diff --git a/sway/commands/floating_scroll.c b/sway/commands/floating_scroll.c
new file mode 100644
index 00000000..a9ef2c21
--- /dev/null
+++ b/sway/commands/floating_scroll.c
@@ -0,0 +1,44 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4#include "stringop.h"
5
6struct cmd_results *cmd_floating_scroll(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "floating_scroll", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11 if (!strcasecmp("up", argv[0])) {
12 free(config->floating_scroll_up_cmd);
13 if (argc < 2) {
14 config->floating_scroll_up_cmd = strdup("");
15 } else {
16 config->floating_scroll_up_cmd = join_args(argv + 1, argc - 1);
17 }
18 } else if (!strcasecmp("down", argv[0])) {
19 free(config->floating_scroll_down_cmd);
20 if (argc < 2) {
21 config->floating_scroll_down_cmd = strdup("");
22 } else {
23 config->floating_scroll_down_cmd = join_args(argv + 1, argc - 1);
24 }
25 } else if (!strcasecmp("left", argv[0])) {
26 free(config->floating_scroll_left_cmd);
27 if (argc < 2) {
28 config->floating_scroll_left_cmd = strdup("");
29 } else {
30 config->floating_scroll_left_cmd = join_args(argv + 1, argc - 1);
31 }
32 } else if (!strcasecmp("right", argv[0])) {
33 free(config->floating_scroll_right_cmd);
34 if (argc < 2) {
35 config->floating_scroll_right_cmd = strdup("");
36 } else {
37 config->floating_scroll_right_cmd = join_args(argv + 1, argc - 1);
38 }
39 } else {
40 error = cmd_results_new(CMD_INVALID, "floating_scroll", "Unknown command: '%s'", argv[0]);
41 return error;
42 }
43 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
44}
diff --git a/sway/commands/focus.c b/sway/commands/focus.c
new file mode 100644
index 00000000..18552292
--- /dev/null
+++ b/sway/commands/focus.c
@@ -0,0 +1,91 @@
1#include <string.h>
2#include <wlc/wlc.h>
3#include "commands.h"
4#include "container.h"
5#include "focus.h"
6#include "input_state.h"
7#include "output.h"
8#include "workspace.h"
9
10struct cmd_results *cmd_focus(int argc, char **argv) {
11 if (config->reading) return cmd_results_new(CMD_FAILURE, "focus", "Can't be used in config file.");
12 struct cmd_results *error = NULL;
13 if (argc > 0 && strcasecmp(argv[0], "output") == 0) {
14 swayc_t *output = NULL;
15 struct wlc_point abs_pos;
16 get_absolute_center_position(get_focused_container(&root_container), &abs_pos);
17 if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 2))) {
18 return error;
19 } else if (!(output = output_by_name(argv[1], &abs_pos))) {
20 return cmd_results_new(CMD_FAILURE, "focus output",
21 "Can't find output with name/at direction '%s' @ (%i,%i)", argv[1], abs_pos.x, abs_pos.y);
22 } else if (!workspace_switch(swayc_active_workspace_for(output))) {
23 return cmd_results_new(CMD_FAILURE, "focus output",
24 "Switching to workspace on output '%s' was blocked", argv[1]);
25 } else if (config->mouse_warping) {
26 swayc_t *focused = get_focused_view(output);
27 if (focused && focused->type == C_VIEW) {
28 center_pointer_on(focused);
29 }
30 }
31 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
32 } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) {
33 return error;
34 }
35 static int floating_toggled_index = 0;
36 static int tiled_toggled_index = 0;
37 if (strcasecmp(argv[0], "left") == 0) {
38 move_focus(MOVE_LEFT);
39 } else if (strcasecmp(argv[0], "right") == 0) {
40 move_focus(MOVE_RIGHT);
41 } else if (strcasecmp(argv[0], "up") == 0) {
42 move_focus(MOVE_UP);
43 } else if (strcasecmp(argv[0], "down") == 0) {
44 move_focus(MOVE_DOWN);
45 } else if (strcasecmp(argv[0], "parent") == 0) {
46 move_focus(MOVE_PARENT);
47 } else if (strcasecmp(argv[0], "child") == 0) {
48 move_focus(MOVE_CHILD);
49 } else if (strcasecmp(argv[0], "mode_toggle") == 0) {
50 int i;
51 swayc_t *workspace = swayc_active_workspace();
52 swayc_t *focused = get_focused_view(workspace);
53 if (focused->is_floating) {
54 if (workspace->children->length > 0) {
55 for (i = 0;i < workspace->floating->length; i++) {
56 if (workspace->floating->items[i] == focused) {
57 floating_toggled_index = i;
58 break;
59 }
60 }
61 if (workspace->children->length > tiled_toggled_index) {
62 set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index]));
63 } else {
64 set_focused_container(get_focused_view(workspace->children->items[0]));
65 tiled_toggled_index = 0;
66 }
67 }
68 } else {
69 if (workspace->floating->length > 0) {
70 for (i = 0;i < workspace->children->length; i++) {
71 if (workspace->children->items[i] == focused) {
72 tiled_toggled_index = i;
73 break;
74 }
75 }
76 if (workspace->floating->length > floating_toggled_index) {
77 swayc_t *floating = workspace->floating->items[floating_toggled_index];
78 set_focused_container(get_focused_view(floating));
79 } else {
80 swayc_t *floating = workspace->floating->items[workspace->floating->length - 1];
81 set_focused_container(get_focused_view(floating));
82 tiled_toggled_index = workspace->floating->length - 1;
83 }
84 }
85 }
86 } else {
87 return cmd_results_new(CMD_INVALID, "focus",
88 "Expected 'focus <direction|parent|child|mode_toggle>' or 'focus output <direction|name>'");
89 }
90 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
91}
diff --git a/sway/commands/focus_follows_mouse.c b/sway/commands/focus_follows_mouse.c
new file mode 100644
index 00000000..25147b2a
--- /dev/null
+++ b/sway/commands/focus_follows_mouse.c
@@ -0,0 +1,12 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_focus_follows_mouse(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 config->focus_follows_mouse = !strcasecmp(argv[0], "yes");
11 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
12}
diff --git a/sway/commands/font.c b/sway/commands/font.c
new file mode 100644
index 00000000..52502a2d
--- /dev/null
+++ b/sway/commands/font.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "border.h"
3#include "commands.h"
4#include "log.h"
5#include "stringop.h"
6
7struct cmd_results *cmd_font(int argc, char **argv) {
8 struct cmd_results *error = NULL;
9 if ((error = checkarg(argc, "font", EXPECTED_AT_LEAST, 1))) {
10 return error;
11 }
12
13 char *font = join_args(argv, argc);
14 free(config->font);
15 if (strlen(font) > 6 && strncmp("pango:", font, 6) == 0) {
16 config->font = strdup(font + 6);
17 free(font);
18 } else {
19 config->font = font;
20 }
21
22 config->font_height = get_font_text_height(config->font);
23
24 sway_log(L_DEBUG, "Settings font %s", config->font);
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
new file mode 100644
index 00000000..49badd34
--- /dev/null
+++ b/sway/commands/for_window.c
@@ -0,0 +1,37 @@
1#include <string.h>
2#include "commands.h"
3#include "criteria.h"
4#include "list.h"
5#include "log.h"
6#include "stringop.h"
7
8struct cmd_results *cmd_for_window(int argc, char **argv) {
9 struct cmd_results *error = NULL;
10 if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) {
11 return error;
12 }
13 // add command to a criteria/command pair that is run against views when they appear.
14 char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
15
16 struct criteria *crit = malloc(sizeof(struct criteria));
17 crit->crit_raw = strdup(criteria);
18 crit->cmdlist = cmdlist;
19 crit->tokens = create_list();
20 char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw);
21
22 if (err_str) {
23 error = cmd_results_new(CMD_INVALID, "for_window", err_str);
24 free(err_str);
25 free_criteria(crit);
26 } else if (crit->tokens->length == 0) {
27 error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria");
28 free_criteria(crit);
29 } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
30 sway_log(L_DEBUG, "for_window: Duplicate, skipping.");
31 free_criteria(crit);
32 } else {
33 sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
34 list_add(config->criteria, crit);
35 }
36 return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
37}
diff --git a/sway/commands/fullscreen.c b/sway/commands/fullscreen.c
new file mode 100644
index 00000000..83bbc66f
--- /dev/null
+++ b/sway/commands/fullscreen.c
@@ -0,0 +1,60 @@
1#include <stdbool.h>
2#include <string.h>
3#include <wlc/wlc.h>
4#include "commands.h"
5#include "container.h"
6#include "focus.h"
7#include "ipc-server.h"
8#include "layout.h"
9
10struct cmd_results *cmd_fullscreen(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if (config->reading) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can't be used in config file.");
13 if (!config->active) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can only be used when sway is running.");
14 if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) {
15 return error;
16 }
17 swayc_t *container = get_focused_view(&root_container);
18 if(container->type != C_VIEW){
19 return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen");
20 }
21 swayc_t *workspace = swayc_parent_by_type(container, C_WORKSPACE);
22 bool current = swayc_is_fullscreen(container);
23 wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current);
24
25 if (container->is_floating) {
26 if (current) {
27 // set dimensions back to what they were before we fullscreened this
28 container->x = container->cached_geometry.origin.x;
29 container->y = container->cached_geometry.origin.y;
30 container->width = container->cached_geometry.size.w;
31 container->height = container->cached_geometry.size.h;
32 } else {
33 // cache dimensions so we can reset them after we "unfullscreen" this
34 struct wlc_geometry geo = {
35 .origin = {
36 .x = container->x,
37 .y = container->y
38 },
39 .size = {
40 .w = container->width,
41 .h = container->height
42 }
43 };
44 container->cached_geometry = geo;
45 }
46 }
47
48 // Resize workspace if going from fullscreen -> notfullscreen
49 // otherwise just resize container
50 if (!current) {
51 arrange_windows(workspace, -1, -1);
52 workspace->fullscreen = container;
53 } else {
54 arrange_windows(container, -1, -1);
55 workspace->fullscreen = NULL;
56 }
57 ipc_event_window(container, "fullscreen_mode");
58
59 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
60}
diff --git a/sway/commands/gaps.c b/sway/commands/gaps.c
new file mode 100644
index 00000000..81ca14f4
--- /dev/null
+++ b/sway/commands/gaps.c
@@ -0,0 +1,165 @@
1#include <ctype.h>
2#include <errno.h>
3#include <stdlib.h>
4#include <string.h>
5#include "commands.h"
6#include "container.h"
7#include "focus.h"
8#include "layout.h"
9
10struct cmd_results *cmd_gaps(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if ((error = checkarg(argc, "gaps", EXPECTED_AT_LEAST, 1))) {
13 return error;
14 }
15 const char *expected_syntax =
16 "Expected 'gaps edge_gaps <on|off|toggle>' or "
17 "'gaps <inner|outer> <current|all|workspace> <set|plus|minus n>'";
18 const char *amount_str = argv[0];
19 // gaps amount
20 if (argc >= 1 && isdigit(*amount_str)) {
21 int amount = (int)strtol(amount_str, NULL, 10);
22 if (errno == ERANGE) {
23 errno = 0;
24 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
25 }
26 config->gaps_inner = config->gaps_outer = amount;
27 arrange_windows(&root_container, -1, -1);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29 }
30 // gaps inner|outer n
31 else if (argc >= 2 && isdigit((amount_str = argv[1])[0])) {
32 int amount = (int)strtol(amount_str, NULL, 10);
33 if (errno == ERANGE) {
34 errno = 0;
35 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
36 }
37 const char *target_str = argv[0];
38 if (strcasecmp(target_str, "inner") == 0) {
39 config->gaps_inner = amount;
40 } else if (strcasecmp(target_str, "outer") == 0) {
41 config->gaps_outer = amount;
42 }
43 arrange_windows(&root_container, -1, -1);
44 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
45 } else if (argc == 2 && strcasecmp(argv[0], "edge_gaps") == 0) {
46 // gaps edge_gaps <on|off|toggle>
47 if (strcasecmp(argv[1], "toggle") == 0) {
48 if (config->reading) {
49 return cmd_results_new(CMD_FAILURE, "gaps edge_gaps toggle",
50 "Can't be used in config file.");
51 }
52 config->edge_gaps = !config->edge_gaps;
53 } else {
54 config->edge_gaps =
55 (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "on") == 0);
56 }
57 arrange_windows(&root_container, -1, -1);
58 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
59 }
60 // gaps inner|outer current|all set|plus|minus n
61 if (argc < 4 || config->reading) {
62 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
63 }
64 // gaps inner|outer ...
65 const char *inout_str = argv[0];
66 enum {INNER, OUTER} inout;
67 if (strcasecmp(inout_str, "inner") == 0) {
68 inout = INNER;
69 } else if (strcasecmp(inout_str, "outer") == 0) {
70 inout = OUTER;
71 } else {
72 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
73 }
74
75 // gaps ... current|all ...
76 const char *target_str = argv[1];
77 enum {CURRENT, WORKSPACE, ALL} target;
78 if (strcasecmp(target_str, "current") == 0) {
79 target = CURRENT;
80 } else if (strcasecmp(target_str, "all") == 0) {
81 target = ALL;
82 } else if (strcasecmp(target_str, "workspace") == 0) {
83 if (inout == OUTER) {
84 target = CURRENT;
85 } else {
86 // Set gap for views in workspace
87 target = WORKSPACE;
88 }
89 } else {
90 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
91 }
92
93 // gaps ... n
94 amount_str = argv[3];
95 int amount = (int)strtol(amount_str, NULL, 10);
96 if (errno == ERANGE) {
97 errno = 0;
98 return cmd_results_new(CMD_INVALID, "gaps", "Number is out out of range.");
99 }
100
101 // gaps ... set|plus|minus ...
102 const char *method_str = argv[2];
103 enum {SET, ADD} method;
104 if (strcasecmp(method_str, "set") == 0) {
105 method = SET;
106 } else if (strcasecmp(method_str, "plus") == 0) {
107 method = ADD;
108 } else if (strcasecmp(method_str, "minus") == 0) {
109 method = ADD;
110 amount *= -1;
111 } else {
112 return cmd_results_new(CMD_INVALID, "gaps", expected_syntax);
113 }
114
115 if (target == CURRENT) {
116 swayc_t *cont;
117 if (inout == OUTER) {
118 if ((cont = swayc_active_workspace()) == NULL) {
119 return cmd_results_new(CMD_FAILURE, "gaps", "There's no active workspace.");
120 }
121 } else {
122 if ((cont = get_focused_view(&root_container))->type != C_VIEW) {
123 return cmd_results_new(CMD_FAILURE, "gaps", "Currently focused item is not a view.");
124 }
125 }
126 cont->gaps = swayc_gap(cont);
127 if (method == SET) {
128 cont->gaps = amount;
129 } else if ((cont->gaps += amount) < 0) {
130 cont->gaps = 0;
131 }
132 arrange_windows(cont->parent, -1, -1);
133 } else if (inout == OUTER) {
134 //resize all workspace.
135 int i,j;
136 for (i = 0; i < root_container.children->length; ++i) {
137 swayc_t *op = root_container.children->items[i];
138 for (j = 0; j < op->children->length; ++j) {
139 swayc_t *ws = op->children->items[j];
140 if (method == SET) {
141 ws->gaps = amount;
142 } else if ((ws->gaps += amount) < 0) {
143 ws->gaps = 0;
144 }
145 }
146 }
147 arrange_windows(&root_container, -1, -1);
148 } else {
149 // Resize gaps for all views in workspace
150 swayc_t *top;
151 if (target == WORKSPACE) {
152 if ((top = swayc_active_workspace()) == NULL) {
153 return cmd_results_new(CMD_FAILURE, "gaps", "There's currently no active workspace.");
154 }
155 } else {
156 top = &root_container;
157 }
158 int top_gap = top->gaps;
159 container_map(top, method == SET ? set_gaps : add_gaps, &amount);
160 top->gaps = top_gap;
161 arrange_windows(top, -1, -1);
162 }
163
164 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
165}
diff --git a/sway/commands/hide_edge_borders.c b/sway/commands/hide_edge_borders.c
new file mode 100644
index 00000000..7bb792dd
--- /dev/null
+++ b/sway/commands/hide_edge_borders.c
@@ -0,0 +1,24 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_hide_edge_borders(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "hide_edge_borders", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 if (strcasecmp(argv[0], "none") == 0) {
11 config->hide_edge_borders = E_NONE;
12 } else if (strcasecmp(argv[0], "vertical") == 0) {
13 config->hide_edge_borders = E_VERTICAL;
14 } else if (strcasecmp(argv[0], "horizontal") == 0) {
15 config->hide_edge_borders = E_HORIZONTAL;
16 } else if (strcasecmp(argv[0], "both") == 0) {
17 config->hide_edge_borders = E_BOTH;
18 } else {
19 return cmd_results_new(CMD_INVALID, "hide_edge_borders",
20 "Expected 'hide_edge_borders <none|vertical|horizontal|both>'");
21 }
22
23 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
24}
diff --git a/sway/commands/include.c b/sway/commands/include.c
new file mode 100644
index 00000000..e5f0a7ec
--- /dev/null
+++ b/sway/commands/include.c
@@ -0,0 +1,15 @@
1#include "commands.h"
2#include "config.h"
3
4struct cmd_results *cmd_include(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "include", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 if (!load_include_configs(argv[0], config)) {
11 return cmd_results_new(CMD_INVALID, "include", "Failed to include sub configuration file: %s", argv[0]);
12 }
13
14 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
15}
diff --git a/sway/commands/input.c b/sway/commands/input.c
new file mode 100644
index 00000000..5df63b0d
--- /dev/null
+++ b/sway/commands/input.c
@@ -0,0 +1,52 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4#include "log.h"
5
6struct cmd_results *cmd_input(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) {
9 return error;
10 }
11
12 if (config->reading && strcmp("{", argv[1]) == 0) {
13 current_input_config = new_input_config(argv[0]);
14 sway_log(L_DEBUG, "entering input block: %s", current_input_config->identifier);
15 return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL);
16 }
17
18 if (argc > 2) {
19 int argc_new = argc-2;
20 char **argv_new = argv+2;
21
22 struct cmd_results *res;
23 current_input_config = new_input_config(argv[0]);
24 if (strcasecmp("accel_profile", argv[1]) == 0) {
25 res = input_cmd_accel_profile(argc_new, argv_new);
26 } else if (strcasecmp("click_method", argv[1]) == 0) {
27 res = input_cmd_click_method(argc_new, argv_new);
28 } else if (strcasecmp("drag_lock", argv[1]) == 0) {
29 res = input_cmd_drag_lock(argc_new, argv_new);
30 } else if (strcasecmp("dwt", argv[1]) == 0) {
31 res = input_cmd_dwt(argc_new, argv_new);
32 } else if (strcasecmp("events", argv[1]) == 0) {
33 res = input_cmd_events(argc_new, argv_new);
34 } else if (strcasecmp("middle_emulation", argv[1]) == 0) {
35 res = input_cmd_middle_emulation(argc_new, argv_new);
36 } else if (strcasecmp("natural_scroll", argv[1]) == 0) {
37 res = input_cmd_natural_scroll(argc_new, argv_new);
38 } else if (strcasecmp("pointer_accel", argv[1]) == 0) {
39 res = input_cmd_pointer_accel(argc_new, argv_new);
40 } else if (strcasecmp("scroll_method", argv[1]) == 0) {
41 res = input_cmd_scroll_method(argc_new, argv_new);
42 } else if (strcasecmp("tap", argv[1]) == 0) {
43 res = input_cmd_tap(argc_new, argv_new);
44 } else {
45 res = cmd_results_new(CMD_INVALID, "input <device>", "Unknown command %s", argv[1]);
46 }
47 current_input_config = NULL;
48 return res;
49 }
50
51 return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL);
52}
diff --git a/sway/commands/input/accel_profile.c b/sway/commands/input/accel_profile.c
new file mode 100644
index 00000000..bfc3c76d
--- /dev/null
+++ b/sway/commands/input/accel_profile.c
@@ -0,0 +1,26 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_accel_profile(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "accel_profile", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "adaptive") == 0) {
16 new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
17 } else if (strcasecmp(argv[0], "flat") == 0) {
18 new_config->accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
19 } else {
20 return cmd_results_new(CMD_INVALID, "accel_profile",
21 "Expected 'accel_profile <adaptive|flat>'");
22 }
23
24 input_cmd_apply(new_config);
25 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
26}
diff --git a/sway/commands/input/click_method.c b/sway/commands/input/click_method.c
new file mode 100644
index 00000000..2001acf2
--- /dev/null
+++ b/sway/commands/input/click_method.c
@@ -0,0 +1,29 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4#include "log.h"
5
6struct cmd_results *input_cmd_click_method(int argc, char **argv) {
7 sway_log(L_DEBUG, "click_method for device: %d %s", current_input_config==NULL, current_input_config->identifier);
8 struct cmd_results *error = NULL;
9 if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) {
10 return error;
11 }
12 if (!current_input_config) {
13 return cmd_results_new(CMD_FAILURE, "click_method", "No input device defined.");
14 }
15 struct input_config *new_config = new_input_config(current_input_config->identifier);
16
17 if (strcasecmp(argv[0], "none") == 0) {
18 new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE;
19 } else if (strcasecmp(argv[0], "button_areas") == 0) {
20 new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
21 } else if (strcasecmp(argv[0], "clickfinger") == 0) {
22 new_config->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
23 } else {
24 return cmd_results_new(CMD_INVALID, "click_method", "Expected 'click_method <none|button_areas|clickfinger'");
25 }
26
27 input_cmd_apply(new_config);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29}
diff --git a/sway/commands/input/drag_lock.c b/sway/commands/input/drag_lock.c
new file mode 100644
index 00000000..7bb2e334
--- /dev/null
+++ b/sway/commands/input/drag_lock.c
@@ -0,0 +1,25 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_drag_lock(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "drag_lock", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "enabled") == 0) {
16 new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_ENABLED;
17 } else if (strcasecmp(argv[0], "disabled") == 0) {
18 new_config->drag_lock = LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
19 } else {
20 return cmd_results_new(CMD_INVALID, "drag_lock", "Expected 'drag_lock <enabled|disabled>'");
21 }
22
23 input_cmd_apply(new_config);
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
diff --git a/sway/commands/input/dwt.c b/sway/commands/input/dwt.c
new file mode 100644
index 00000000..4010126d
--- /dev/null
+++ b/sway/commands/input/dwt.c
@@ -0,0 +1,25 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_dwt(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "enabled") == 0) {
16 new_config->dwt = LIBINPUT_CONFIG_DWT_ENABLED;
17 } else if (strcasecmp(argv[0], "disabled") == 0) {
18 new_config->dwt = LIBINPUT_CONFIG_DWT_DISABLED;
19 } else {
20 return cmd_results_new(CMD_INVALID, "dwt", "Expected 'dwt <enabled|disabled>'");
21 }
22
23 input_cmd_apply(new_config);
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c
new file mode 100644
index 00000000..9bf9aa95
--- /dev/null
+++ b/sway/commands/input/events.c
@@ -0,0 +1,29 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4#include "log.h"
5
6struct cmd_results *input_cmd_events(int argc, char **argv) {
7 sway_log(L_DEBUG, "events for device: %s", current_input_config->identifier);
8 struct cmd_results *error = NULL;
9 if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) {
10 return error;
11 }
12 if (!current_input_config) {
13 return cmd_results_new(CMD_FAILURE, "events", "No input device defined.");
14 }
15 struct input_config *new_config = new_input_config(current_input_config->identifier);
16
17 if (strcasecmp(argv[0], "enabled") == 0) {
18 new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
19 } else if (strcasecmp(argv[0], "disabled") == 0) {
20 new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
21 } else if (strcasecmp(argv[0], "disabled_on_external_mouse") == 0) {
22 new_config->send_events = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
23 } else {
24 return cmd_results_new(CMD_INVALID, "events", "Expected 'events <enabled|disabled|disabled_on_external_mouse>'");
25 }
26
27 input_cmd_apply(new_config);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29}
diff --git a/sway/commands/input/middle_emulation.c b/sway/commands/input/middle_emulation.c
new file mode 100644
index 00000000..e3087f3a
--- /dev/null
+++ b/sway/commands/input/middle_emulation.c
@@ -0,0 +1,25 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "middle_emulation", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "enabled") == 0) {
16 new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED;
17 } else if (strcasecmp(argv[0], "disabled") == 0) {
18 new_config->middle_emulation = LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED;
19 } else {
20 return cmd_results_new(CMD_INVALID, "middle_emulation", "Expected 'middle_emulation <enabled|disabled>'");
21 }
22
23 input_cmd_apply(new_config);
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
diff --git a/sway/commands/input/natural_scroll.c b/sway/commands/input/natural_scroll.c
new file mode 100644
index 00000000..393f26a0
--- /dev/null
+++ b/sway/commands/input/natural_scroll.c
@@ -0,0 +1,25 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "natural_scoll", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "enabled") == 0) {
16 new_config->natural_scroll = 1;
17 } else if (strcasecmp(argv[0], "disabled") == 0) {
18 new_config->natural_scroll = 0;
19 } else {
20 return cmd_results_new(CMD_INVALID, "natural_scroll", "Expected 'natural_scroll <enabled|disabled>'");
21 }
22
23 input_cmd_apply(new_config);
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
diff --git a/sway/commands/input/pointer_accel.c b/sway/commands/input/pointer_accel.c
new file mode 100644
index 00000000..e22c51c1
--- /dev/null
+++ b/sway/commands/input/pointer_accel.c
@@ -0,0 +1,24 @@
1#include <stdlib.h>
2#include <string.h>
3#include "commands.h"
4#include "input.h"
5
6struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11 if (!current_input_config) {
12 return cmd_results_new(CMD_FAILURE, "pointer_accel", "No input device defined.");
13 }
14 struct input_config *new_config = new_input_config(current_input_config->identifier);
15
16 float pointer_accel = atof(argv[0]);
17 if (pointer_accel < -1 || pointer_accel > 1) {
18 return cmd_results_new(CMD_INVALID, "pointer_accel", "Input out of range [-1, 1]");
19 }
20 new_config->pointer_accel = pointer_accel;
21
22 input_cmd_apply(new_config);
23 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
24}
diff --git a/sway/commands/input/scroll_method.c b/sway/commands/input/scroll_method.c
new file mode 100644
index 00000000..8e900a26
--- /dev/null
+++ b/sway/commands/input/scroll_method.c
@@ -0,0 +1,29 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4
5struct cmd_results *input_cmd_scroll_method(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) {
8 return error;
9 }
10 if (!current_input_config) {
11 return cmd_results_new(CMD_FAILURE, "scroll_method", "No input device defined.");
12 }
13 struct input_config *new_config = new_input_config(current_input_config->identifier);
14
15 if (strcasecmp(argv[0], "none") == 0) {
16 new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
17 } else if (strcasecmp(argv[0], "two_finger") == 0) {
18 new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG;
19 } else if (strcasecmp(argv[0], "edge") == 0) {
20 new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE;
21 } else if (strcasecmp(argv[0], "on_button_down") == 0) {
22 new_config->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
23 } else {
24 return cmd_results_new(CMD_INVALID, "scroll_method", "Expected 'scroll_method <none|two_finger|edge|on_button_down>'");
25 }
26
27 input_cmd_apply(new_config);
28 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
29}
diff --git a/sway/commands/input/tap.c b/sway/commands/input/tap.c
new file mode 100644
index 00000000..8180dcea
--- /dev/null
+++ b/sway/commands/input/tap.c
@@ -0,0 +1,28 @@
1#include <string.h>
2#include "commands.h"
3#include "input.h"
4#include "log.h"
5
6struct cmd_results *input_cmd_tap(int argc, char **argv) {
7 sway_log(L_DEBUG, "tap for device: %s", current_input_config->identifier);
8 struct cmd_results *error = NULL;
9 if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) {
10 return error;
11 }
12 if (!current_input_config) {
13 return cmd_results_new(CMD_FAILURE, "tap", "No input device defined.");
14 }
15 struct input_config *new_config = new_input_config(current_input_config->identifier);
16
17 if (strcasecmp(argv[0], "enabled") == 0) {
18 new_config->tap = LIBINPUT_CONFIG_TAP_ENABLED;
19 } else if (strcasecmp(argv[0], "disabled") == 0) {
20 new_config->tap = LIBINPUT_CONFIG_TAP_DISABLED;
21 } else {
22 return cmd_results_new(CMD_INVALID, "tap", "Expected 'tap <enabled|disabled>'");
23 }
24
25 sway_log(L_DEBUG, "apply-tap for device: %s", current_input_config->identifier);
26 input_cmd_apply(new_config);
27 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
28}
diff --git a/sway/commands/kill.c b/sway/commands/kill.c
new file mode 100644
index 00000000..ce164e34
--- /dev/null
+++ b/sway/commands/kill.c
@@ -0,0 +1,12 @@
1#include "commands.h"
2#include "container.h"
3#include "focus.h"
4
5struct cmd_results *cmd_kill(int argc, char **argv) {
6 if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file.");
7 if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running.");
8
9 swayc_t *container = get_focused_container(&root_container);
10 close_views(container);
11 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
12}
diff --git a/sway/commands/layout.c b/sway/commands/layout.c
new file mode 100644
index 00000000..c4f5d2b5
--- /dev/null
+++ b/sway/commands/layout.c
@@ -0,0 +1,66 @@
1#include <string.h>
2#include "commands.h"
3#include "container.h"
4#include "layout.h"
5
6struct cmd_results *cmd_layout(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if (config->reading) return cmd_results_new(CMD_FAILURE, "layout", "Can't be used in config file.");
9 if (!config->active) return cmd_results_new(CMD_FAILURE, "layout", "Can only be used when sway is running.");
10 if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
11 return error;
12 }
13 swayc_t *parent = get_focused_container(&root_container);
14 if (parent->is_floating) {
15 return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows");
16 }
17
18 while (parent->type == C_VIEW) {
19 parent = parent->parent;
20 }
21
22 enum swayc_layouts old_layout = parent->layout;
23
24 if (strcasecmp(argv[0], "default") == 0) {
25 parent->layout = parent->prev_layout;
26 if (parent->layout == L_NONE) {
27 swayc_t *output = swayc_parent_by_type(parent, C_OUTPUT);
28 parent->layout = default_layout(output);
29 }
30 } else {
31 if (parent->layout != L_TABBED && parent->layout != L_STACKED) {
32 parent->prev_layout = parent->layout;
33 }
34
35 if (strcasecmp(argv[0], "tabbed") == 0) {
36 if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){
37 parent = new_container(parent, L_TABBED);
38 }
39
40 parent->layout = L_TABBED;
41 } else if (strcasecmp(argv[0], "stacking") == 0) {
42 if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)) {
43 parent = new_container(parent, L_STACKED);
44 }
45
46 parent->layout = L_STACKED;
47 } else if (strcasecmp(argv[0], "splith") == 0) {
48 parent->layout = L_HORIZ;
49 } else if (strcasecmp(argv[0], "splitv") == 0) {
50 parent->layout = L_VERT;
51 } else if (strcasecmp(argv[0], "toggle") == 0 && argc == 2 && strcasecmp(argv[1], "split") == 0) {
52 if (parent->layout == L_VERT) {
53 parent->layout = L_HORIZ;
54 } else {
55 parent->layout = L_VERT;
56 }
57 }
58 }
59
60 update_layout_geometry(parent, old_layout);
61 update_geometry(parent);
62
63 arrange_windows(parent, parent->width, parent->height);
64
65 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
66}
diff --git a/sway/commands/log_colors.c b/sway/commands/log_colors.c
new file mode 100644
index 00000000..8e6d01ca
--- /dev/null
+++ b/sway/commands/log_colors.c
@@ -0,0 +1,21 @@
1#include <string.h>
2#include "commands.h"
3#include "log.h"
4
5struct cmd_results *cmd_log_colors(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if (!config->reading) return cmd_results_new(CMD_FAILURE, "log_colors", "Can only be used in config file.");
8 if ((error = checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1))) {
9 return error;
10 }
11 if (strcasecmp(argv[0], "no") == 0) {
12 sway_log_colors(0);
13 } else if (strcasecmp(argv[0], "yes") == 0) {
14 sway_log_colors(1);
15 } else {
16 error = cmd_results_new(CMD_FAILURE, "log_colors",
17 "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
18 return error;
19 }
20 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
21}
diff --git a/sway/commands/mode.c b/sway/commands/mode.c
new file mode 100644
index 00000000..05636c61
--- /dev/null
+++ b/sway/commands/mode.c
@@ -0,0 +1,52 @@
1#include <stdbool.h>
2#include <string.h>
3#include "commands.h"
4#include "config.h"
5#include "ipc-server.h"
6#include "list.h"
7#include "log.h"
8
9struct cmd_results *cmd_mode(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
12 return error;
13 }
14
15 const char *mode_name = argv[0];
16 bool mode_make = (argc == 2 && strcmp(argv[1], "{") == 0);
17 if (mode_make) {
18 if (!config->reading)
19 return cmd_results_new(CMD_FAILURE, "mode", "Can only be used in config file.");
20 }
21 struct sway_mode *mode = NULL;
22 // Find mode
23 int i, len = config->modes->length;
24 for (i = 0; i < len; ++i) {
25 struct sway_mode *find = config->modes->items[i];
26 if (strcasecmp(find->name, mode_name) == 0) {
27 mode = find;
28 break;
29 }
30 }
31 // Create mode if it doesn't exist
32 if (!mode && mode_make) {
33 mode = malloc(sizeof*mode);
34 mode->name = strdup(mode_name);
35 mode->bindings = create_list();
36 list_add(config->modes, mode);
37 }
38 if (!mode) {
39 error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name);
40 return error;
41 }
42 if ((config->reading && mode_make) || (!config->reading && !mode_make)) {
43 sway_log(L_DEBUG, "Switching to mode `%s'",mode->name);
44 }
45 // Set current mode
46 config->current_mode = mode;
47 if (!mode_make) {
48 // trigger IPC mode event
49 ipc_event_mode(config->current_mode->name);
50 }
51 return cmd_results_new(mode_make ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL);
52}
diff --git a/sway/commands/mouse_warping.c b/sway/commands/mouse_warping.c
new file mode 100644
index 00000000..2d81febd
--- /dev/null
+++ b/sway/commands/mouse_warping.c
@@ -0,0 +1,16 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_mouse_warping(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "mouse_warping", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 } else if (strcasecmp(argv[0], "output") == 0) {
9 config->mouse_warping = true;
10 } else if (strcasecmp(argv[0], "none") == 0) {
11 config->mouse_warping = false;
12 } else {
13 return cmd_results_new(CMD_FAILURE, "mouse_warping", "Expected 'mouse_warping output|none'");
14 }
15 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
16}
diff --git a/sway/commands/move.c b/sway/commands/move.c
new file mode 100644
index 00000000..68f05b56
--- /dev/null
+++ b/sway/commands/move.c
@@ -0,0 +1,159 @@
1#include <string.h>
2#include <wlc/wlc.h>
3#include "commands.h"
4#include "container.h"
5#include "layout.h"
6#include "list.h"
7#include "output.h"
8#include "workspace.h"
9
10struct cmd_results *cmd_move(int argc, char **argv) {
11 struct cmd_results *error = NULL;
12 if (config->reading) return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file.");
13 if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
14 return error;
15 }
16 const char* expected_syntax = "Expected 'move <left|right|up|down>' or "
17 "'move <container|window> to workspace <name>' or "
18 "'move <container|window|workspace> to output <name|direction>' or "
19 "'move position mouse'";
20 swayc_t *view = get_focused_container(&root_container);
21
22 if (strcasecmp(argv[0], "left") == 0) {
23 move_container(view, MOVE_LEFT);
24 } else if (strcasecmp(argv[0], "right") == 0) {
25 move_container(view, MOVE_RIGHT);
26 } else if (strcasecmp(argv[0], "up") == 0) {
27 move_container(view, MOVE_UP);
28 } else if (strcasecmp(argv[0], "down") == 0) {
29 move_container(view, MOVE_DOWN);
30 } else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) {
31 // "move container ...
32 if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) {
33 return error;
34 } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) {
35 // move container to workspace x
36 if (view->type == C_WORKSPACE) {
37 view = new_container(view, view->layout);
38 } if (view->type != C_CONTAINER && view->type != C_VIEW) {
39 return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
40 }
41
42 const char *ws_name = argv[3];
43 swayc_t *ws;
44 if (argc == 5 && strcasecmp(ws_name, "number") == 0) {
45 // move "container to workspace number x"
46 ws_name = argv[4];
47 ws = workspace_by_number(ws_name);
48 } else {
49 ws = workspace_by_name(ws_name);
50 }
51
52 if (ws == NULL) {
53 ws = workspace_create(ws_name);
54 }
55 move_container_to(view, get_focused_container(ws));
56 } else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) {
57 // move container to output x
58 swayc_t *output = NULL;
59 struct wlc_point abs_pos;
60 get_absolute_center_position(view, &abs_pos);
61 if (view->type == C_WORKSPACE) {
62 view = new_container(view, view->layout);
63 } else if (view->type != C_CONTAINER && view->type != C_VIEW) {
64 return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
65 } else if (!(output = output_by_name(argv[3], &abs_pos))) {
66 return cmd_results_new(CMD_FAILURE, "move",
67 "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
68 }
69
70 swayc_t *container = get_focused_container(output);
71 if (container->is_floating) {
72 move_container_to(view, container->parent);
73 } else {
74 move_container_to(view, container);
75 }
76 } else {
77 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
78 }
79 } else if (strcasecmp(argv[0], "workspace") == 0) {
80 // move workspace (to output x)
81 swayc_t *output = NULL;
82 struct wlc_point abs_pos;
83 get_absolute_center_position(view, &abs_pos);
84 if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) {
85 return error;
86 } else if (strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "output") != 0) {
87 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
88 } else if (!(output = output_by_name(argv[3], &abs_pos))) {
89 return cmd_results_new(CMD_FAILURE, "move workspace",
90 "Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
91 }
92 if (view->type == C_WORKSPACE) {
93 // This probably means we're moving an empty workspace, but
94 // that's fine.
95 move_workspace_to(view, output);
96 } else {
97 swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE);
98 move_workspace_to(workspace, output);
99 }
100 } else if (strcasecmp(argv[0], "scratchpad") == 0) {
101 // move scratchpad ...
102 if (view->type != C_CONTAINER && view->type != C_VIEW) {
103 return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
104 }
105 swayc_t *view = get_focused_container(&root_container);
106 int i;
107 for (i = 0; i < scratchpad->length; i++) {
108 if (scratchpad->items[i] == view) {
109 hide_view_in_scratchpad(view);
110 sp_view = NULL;
111 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
112 }
113 }
114 list_add(scratchpad, view);
115 if (!view->is_floating) {
116 destroy_container(remove_child(view));
117 } else {
118 remove_child(view);
119 }
120 wlc_view_set_mask(view->handle, 0);
121 arrange_windows(swayc_active_workspace(), -1, -1);
122 swayc_t *focused = container_under_pointer();
123 if (focused == NULL) {
124 focused = swayc_active_workspace();
125 }
126 set_focused_container(focused);
127 } else if (strcasecmp(argv[0], "position") == 0) {
128 if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 2))) {
129 return error;
130 }
131 if (strcasecmp(argv[1], "mouse")) {
132 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
133 }
134
135 if (view->is_floating) {
136 swayc_t *output = swayc_parent_by_type(view, C_OUTPUT);
137 struct wlc_geometry g;
138 wlc_view_get_visible_geometry(view->handle, &g);
139 const struct wlc_size *size = wlc_output_get_resolution(output->handle);
140
141 struct wlc_point origin;
142 wlc_pointer_get_position(&origin);
143
144 int32_t x = origin.x - g.size.w / 2;
145 int32_t y = origin.y - g.size.h / 2;
146
147 uint32_t w = size->w - g.size.w;
148 uint32_t h = size->h - g.size.h;
149
150 view->x = g.origin.x = MIN((int32_t)w, MAX(x, 0));
151 view->y = g.origin.y = MIN((int32_t)h, MAX(y, 0));
152
153 wlc_view_set_geometry(view->handle, 0, &g);
154 }
155 } else {
156 return cmd_results_new(CMD_INVALID, "move", expected_syntax);
157 }
158 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
159}
diff --git a/sway/commands/new_float.c b/sway/commands/new_float.c
new file mode 100644
index 00000000..f847492a
--- /dev/null
+++ b/sway/commands/new_float.c
@@ -0,0 +1,43 @@
1#include <errno.h>
2#include <string.h>
3#include "commands.h"
4#include "container.h"
5
6struct cmd_results *cmd_new_float(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "new_float", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (argc > 2) {
13 return cmd_results_new(CMD_INVALID, "new_float",
14 "Expected 'new_float <normal|none|pixel> [<n>]");
15 }
16
17 enum swayc_border_types border = config->floating_border;
18 int thickness = config->floating_border_thickness;
19
20 if (strcasecmp(argv[0], "none") == 0) {
21 border = B_NONE;
22 } else if (strcasecmp(argv[0], "normal") == 0) {
23 border = B_NORMAL;
24 } else if (strcasecmp(argv[0], "pixel") == 0) {
25 border = B_PIXEL;
26 } else {
27 return cmd_results_new(CMD_INVALID, "new_float",
28 "Expected 'border <normal|none|pixel>");
29 }
30
31 if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) {
32 thickness = (int)strtol(argv[1], NULL, 10);
33 if (errno == ERANGE || thickness < 0) {
34 errno = 0;
35 return cmd_results_new(CMD_INVALID, "new_float", "Number is out out of range.");
36 }
37 }
38
39 config->floating_border = border;
40 config->floating_border_thickness = thickness;
41
42 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
43}
diff --git a/sway/commands/new_window.c b/sway/commands/new_window.c
new file mode 100644
index 00000000..e7d1b271
--- /dev/null
+++ b/sway/commands/new_window.c
@@ -0,0 +1,43 @@
1#include <errno.h>
2#include <string.h>
3#include "commands.h"
4#include "container.h"
5
6struct cmd_results *cmd_new_window(int argc, char **argv) {
7 struct cmd_results *error = NULL;
8 if ((error = checkarg(argc, "new_window", EXPECTED_AT_LEAST, 1))) {
9 return error;
10 }
11
12 if (argc > 2) {
13 return cmd_results_new(CMD_INVALID, "new_window",
14 "Expected 'new_window <normal|none|pixel> [<n>]");
15 }
16
17 enum swayc_border_types border = config->border;
18 int thickness = config->border_thickness;
19
20 if (strcasecmp(argv[0], "none") == 0) {
21 border = B_NONE;
22 } else if (strcasecmp(argv[0], "normal") == 0) {
23 border = B_NORMAL;
24 } else if (strcasecmp(argv[0], "pixel") == 0) {
25 border = B_PIXEL;
26 } else {
27 return cmd_results_new(CMD_INVALID, "new_window",
28 "Expected 'border <normal|none|pixel>");
29 }
30
31 if (argc == 2 && (border == B_NORMAL || border == B_PIXEL)) {
32 thickness = (int)strtol(argv[1], NULL, 10);
33 if (errno == ERANGE || thickness < 0) {
34 errno = 0;
35 return cmd_results_new(CMD_INVALID, "new_window", "Number is out out of range.");
36 }
37 }
38
39 config->border = border;
40 config->border_thickness = thickness;
41
42 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
43}
diff --git a/sway/commands/orientation.c b/sway/commands/orientation.c
new file mode 100644
index 00000000..b1fc328d
--- /dev/null
+++ b/sway/commands/orientation.c
@@ -0,0 +1,20 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_orientation(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if (!config->reading) return cmd_results_new(CMD_FAILURE, "orientation", "Can only be used in config file.");
7 if ((error = checkarg(argc, "orientation", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10 if (strcasecmp(argv[0], "horizontal") == 0) {
11 config->default_orientation = L_HORIZ;
12 } else if (strcasecmp(argv[0], "vertical") == 0) {
13 config->default_orientation = L_VERT;
14 } else if (strcasecmp(argv[0], "auto") == 0) {
15 // Do nothing
16 } else {
17 return cmd_results_new(CMD_INVALID, "orientation", "Expected 'orientation <horizontal|vertical|auto>'");
18 }
19 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
20}
diff --git a/sway/commands/output.c b/sway/commands/output.c
new file mode 100644
index 00000000..a2f19f38
--- /dev/null
+++ b/sway/commands/output.c
@@ -0,0 +1,182 @@
1#include <ctype.h>
2#include <libgen.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <wordexp.h>
7#include "commands.h"
8#include "config.h"
9#include "list.h"
10#include "log.h"
11#include "stringop.h"
12
13static char *bg_options[] = {
14 "stretch",
15 "center",
16 "fill",
17 "fit",
18 "tile"
19};
20
21struct cmd_results *cmd_output(int argc, char **argv) {
22 struct cmd_results *error = NULL;
23 if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) {
24 return error;
25 }
26 const char *name = argv[0];
27
28 struct output_config *output = calloc(1, sizeof(struct output_config));
29 output->x = output->y = output->width = output->height = -1;
30 output->name = strdup(name);
31 output->enabled = -1;
32 output->scale = 1;
33
34 // TODO: atoi doesn't handle invalid numbers
35
36 int i;
37 for (i = 1; i < argc; ++i) {
38 const char *command = argv[i];
39
40 if (strcasecmp(command, "disable") == 0) {
41 output->enabled = 0;
42 } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
43 if (++i >= argc) {
44 return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
45 }
46 char *res = argv[i];
47 char *x = strchr(res, 'x');
48 int width = -1, height = -1;
49 if (x != NULL) {
50 // Format is 1234x4321
51 *x = '\0';
52 width = atoi(res);
53 height = atoi(x + 1);
54 *x = 'x';
55 } else {
56 // Format is 1234 4321
57 width = atoi(res);
58 if (++i >= argc) {
59 return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height).");
60 }
61 res = argv[i];
62 height = atoi(res);
63 }
64 output->width = width;
65 output->height = height;
66 } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
67 if (++i >= argc) {
68 return cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
69 }
70 char *res = argv[i];
71 char *c = strchr(res, ',');
72 int x = -1, y = -1;
73 if (c != NULL) {
74 // Format is 1234,4321
75 *c = '\0';
76 x = atoi(res);
77 y = atoi(c + 1);
78 *c = ',';
79 } else {
80 // Format is 1234 4321
81 x = atoi(res);
82 if (++i >= argc) {
83 return cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
84 }
85 res = argv[i];
86 y = atoi(res);
87 }
88 output->x = x;
89 output->y = y;
90 } else if (strcasecmp(command, "scale") == 0) {
91 if (++i >= argc) {
92 return cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
93 }
94 output->scale = atoi(argv[i]);
95 } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
96 wordexp_t p;
97 if (++i >= argc) {
98 return cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
99 }
100 if (i + 1 >= argc) {
101 return cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
102 }
103 if (strcasecmp(argv[argc - 1], "solid_color") == 0) {
104 output->background = strdup(argv[argc - 2]);
105 output->background_option = strdup("solid_color");
106 } else {
107 char *src = join_args(argv + i, argc - i - 1);
108 char *mode = argv[argc - 1];
109 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
110 return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
111 }
112 free(src);
113 src = p.we_wordv[0];
114 if (config->reading && *src != '/') {
115 char *conf = strdup(config->current_config);
116 char *conf_path = dirname(conf);
117 src = malloc(strlen(conf_path) + strlen(src) + 2);
118 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
119 free(conf);
120 }
121 if (access(src, F_OK) == -1) {
122 return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
123 }
124 for (char *m = mode; *m; ++m) *m = tolower(*m);
125 // Check mode
126 bool valid = false;
127 size_t j;
128 for (j = 0; j < sizeof(bg_options) / sizeof(char *); ++j) {
129 if (strcasecmp(mode, bg_options[j]) == 0) {
130 valid = true;
131 break;
132 }
133 }
134 if (!valid) {
135 return cmd_results_new(CMD_INVALID, "output", "Invalid background scaling mode.");
136 }
137 output->background = strdup(src);
138 output->background_option = strdup(mode);
139 if (src != p.we_wordv[0]) {
140 free(src);
141 }
142 wordfree(&p);
143 }
144 }
145 }
146
147 i = list_seq_find(config->output_configs, output_name_cmp, name);
148 if (i >= 0) {
149 // merge existing config
150 struct output_config *oc = config->output_configs->items[i];
151 merge_output_config(oc, output);
152 free_output_config(output);
153 output = oc;
154 } else {
155 list_add(config->output_configs, output);
156 }
157
158 sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)",
159 output->name, output->enabled, output->width,
160 output->height, output->x, output->y, output->scale,
161 output->background, output->background_option);
162
163 if (output->name) {
164 // Try to find the output container and apply configuration now. If
165 // this is during startup then there will be no container and config
166 // will be applied during normal "new output" event from wlc.
167 swayc_t *cont = NULL;
168 for (int i = 0; i < root_container.children->length; ++i) {
169 cont = root_container.children->items[i];
170 if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
171 apply_output_config(output, cont);
172
173 if (strcmp(output->name, "*") != 0) {
174 // stop looking if the output config isn't applicable to all outputs
175 break;
176 }
177 }
178 }
179 }
180
181 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
182}
diff --git a/sway/commands/reload.c b/sway/commands/reload.c
new file mode 100644
index 00000000..571ae986
--- /dev/null
+++ b/sway/commands/reload.c
@@ -0,0 +1,19 @@
1#include "commands.h"
2#include "config.h"
3#include "layout.h"
4
5struct cmd_results *cmd_reload(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if (config->reading) return cmd_results_new(CMD_FAILURE, "reload", "Can't be used in config file.");
8 if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) {
9 return error;
10 }
11 if (!load_main_config(config->current_config, true)) {
12 return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config.");
13 }
14
15 load_swaybars();
16
17 arrange_windows(&root_container, -1, -1);
18 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
19}
diff --git a/sway/commands/resize.c b/sway/commands/resize.c
new file mode 100644
index 00000000..b5585920
--- /dev/null
+++ b/sway/commands/resize.c
@@ -0,0 +1,122 @@
1#include <errno.h>
2#include <stdbool.h>
3#include <stdlib.h>
4#include <string.h>
5#include "commands.h"
6#include "log.h"
7#include "resize.h"
8
9static struct cmd_results *cmd_resize_set(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "resize set", EXPECTED_AT_LEAST, 2))) {
12 return error;
13 }
14
15 if (strcasecmp(argv[0], "width") == 0 || strcasecmp(argv[0], "height") == 0) {
16 // handle `reset set width 100 px height 100 px` syntax, also allows
17 // specifying only one dimension for a `resize set`
18 int cmd_num = 0;
19 int dim;
20
21 while ((cmd_num + 1) < argc) {
22 dim = (int)strtol(argv[cmd_num + 1], NULL, 10);
23 if (errno == ERANGE || dim == 0) {
24 errno = 0;
25 return cmd_results_new(CMD_INVALID, "resize set",
26 "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'");
27 }
28
29 if (strcasecmp(argv[cmd_num], "width") == 0) {
30 set_size(dim, true);
31 } else if (strcasecmp(argv[cmd_num], "height") == 0) {
32 set_size(dim, false);
33 } else {
34 return cmd_results_new(CMD_INVALID, "resize set",
35 "Expected 'resize set <width|height> <amount> [px] [<width|height> <amount> [px]]'");
36 }
37
38 cmd_num += 2;
39
40 if (cmd_num < argc && strcasecmp(argv[cmd_num], "px") == 0) {
41 // if this was `resize set width 400 px height 300 px`, disregard the `px` arg
42 cmd_num++;
43 }
44 }
45 } else {
46 // handle `reset set 100 px 100 px` syntax
47 int width = (int)strtol(argv[0], NULL, 10);
48 if (errno == ERANGE || width == 0) {
49 errno = 0;
50 return cmd_results_new(CMD_INVALID, "resize set",
51 "Expected 'resize set <width> [px] <height> [px]'");
52 }
53
54 int height_arg = 1;
55 if (strcasecmp(argv[1], "px") == 0) {
56 height_arg = 2;
57 }
58
59 int height = (int)strtol(argv[height_arg], NULL, 10);
60 if (errno == ERANGE || height == 0) {
61 errno = 0;
62 return cmd_results_new(CMD_INVALID, "resize set",
63 "Expected 'resize set <width> [px] <height> [px]'");
64 }
65
66 set_size(width, true);
67 set_size(height, false);
68 }
69
70 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
71}
72
73struct cmd_results *cmd_resize(int argc, char **argv) {
74 struct cmd_results *error = NULL;
75 if (config->reading) return cmd_results_new(CMD_FAILURE, "resize", "Can't be used in config file.");
76 if (!config->active) return cmd_results_new(CMD_FAILURE, "resize", "Can only be used when sway is running.");
77
78 if (strcasecmp(argv[0], "set") == 0) {
79 return cmd_resize_set(argc - 1, &argv[1]);
80 }
81
82 if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) {
83 return error;
84 }
85
86 int dim_arg = argc - 1;
87
88 enum resize_dim_types dim_type = RESIZE_DIM_DEFAULT;
89 if (strcasecmp(argv[dim_arg], "ppt") == 0) {
90 dim_type = RESIZE_DIM_PPT;
91 dim_arg--;
92 } else if (strcasecmp(argv[dim_arg], "px") == 0) {
93 dim_type = RESIZE_DIM_PX;
94 dim_arg--;
95 }
96
97 int amount = (int)strtol(argv[dim_arg], NULL, 10);
98 if (errno == ERANGE || amount == 0) {
99 errno = 0;
100 amount = 10; // this is the default resize dimension used by i3 for both px and ppt
101 sway_log(L_DEBUG, "Tried to get resize dimension out of '%s' but failed; setting dimension to default %d",
102 argv[dim_arg], amount);
103 }
104
105 bool use_width = false;
106 if (strcasecmp(argv[1], "width") == 0) {
107 use_width = true;
108 } else if (strcasecmp(argv[1], "height") != 0) {
109 return cmd_results_new(CMD_INVALID, "resize",
110 "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'");
111 }
112
113 if (strcasecmp(argv[0], "shrink") == 0) {
114 amount *= -1;
115 } else if (strcasecmp(argv[0], "grow") != 0) {
116 return cmd_results_new(CMD_INVALID, "resize",
117 "Expected 'resize <shrink|grow> <width|height> [<amount>] [px|ppt]'");
118 }
119
120 resize(amount, use_width, dim_type);
121 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
122}
diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c
new file mode 100644
index 00000000..8cde85a0
--- /dev/null
+++ b/sway/commands/scratchpad.c
@@ -0,0 +1,65 @@
1#include <string.h>
2#include <wlc/wlc.h>
3#include "commands.h"
4#include "container.h"
5#include "focus.h"
6#include "layout.h"
7
8static swayc_t *fetch_view_from_scratchpad() {
9 if (sp_index >= scratchpad->length) {
10 sp_index = 0;
11 }
12 swayc_t *view = scratchpad->items[sp_index++];
13
14 if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) {
15 wlc_view_set_output(view->handle, swayc_active_output()->handle);
16 }
17 if (!view->is_floating) {
18 view->width = swayc_active_workspace()->width/2;
19 view->height = swayc_active_workspace()->height/2;
20 view->x = (swayc_active_workspace()->width - view->width)/2;
21 view->y = (swayc_active_workspace()->height - view->height)/2;
22 }
23 if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) {
24 view->x = (swayc_active_workspace()->width - view->width)/2;
25 }
26 if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) {
27 view->y = (swayc_active_workspace()->height - view->height)/2;
28 }
29
30 add_floating(swayc_active_workspace(), view);
31 wlc_view_set_mask(view->handle, VISIBLE);
32 view->visible = true;
33 arrange_windows(swayc_active_workspace(), -1, -1);
34 set_focused_container(view);
35 return view;
36}
37
38struct cmd_results *cmd_scratchpad(int argc, char **argv) {
39 struct cmd_results *error = NULL;
40 if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file.");
41 if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running.");
42 if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) {
43 return error;
44 }
45
46 if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) {
47 if (!sp_view) {
48 sp_view = fetch_view_from_scratchpad();
49 } else {
50 if (swayc_active_workspace() != sp_view->parent) {
51 hide_view_in_scratchpad(sp_view);
52 if (sp_index == 0) {
53 sp_index = scratchpad->length;
54 }
55 sp_index--;
56 sp_view = fetch_view_from_scratchpad();
57 } else {
58 hide_view_in_scratchpad(sp_view);
59 sp_view = NULL;
60 }
61 }
62 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
63 }
64 return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty.");
65}
diff --git a/sway/commands/seamless_mouse.c b/sway/commands/seamless_mouse.c
new file mode 100644
index 00000000..541c44b0
--- /dev/null
+++ b/sway/commands/seamless_mouse.c
@@ -0,0 +1,12 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_seamless_mouse(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "seamless_mouse", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 config->seamless_mouse = (strcasecmp(argv[0], "on") == 0 || strcasecmp(argv[0], "yes") == 0);
11 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
12}
diff --git a/sway/commands/set.c b/sway/commands/set.c
new file mode 100644
index 00000000..b82a30ac
--- /dev/null
+++ b/sway/commands/set.c
@@ -0,0 +1,56 @@
1#include <stdio.h>
2#include <string.h>
3#include "commands.h"
4#include "config.h"
5#include "list.h"
6#include "stringop.h"
7
8// sort in order of longest->shortest
9static int compare_set_qsort(const void *_l, const void *_r) {
10 struct sway_variable const *l = *(void **)_l;
11 struct sway_variable const *r = *(void **)_r;
12 return strlen(r->name) - strlen(l->name);
13}
14
15struct cmd_results *cmd_set(int argc, char **argv) {
16 char *tmp;
17 int size;
18 struct cmd_results *error = NULL;
19 if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file.");
20 if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) {
21 return error;
22 }
23
24 if (argv[0][0] != '$') {
25 sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]);
26
27 size = asprintf(&tmp, "%s%s", "$", argv[0]);
28 if (size == -1) {
29 return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]);
30 }
31
32 argv[0] = strdup(tmp);
33 free(tmp);
34 }
35
36 struct sway_variable *var = NULL;
37 // Find old variable if it exists
38 int i;
39 for (i = 0; i < config->symbols->length; ++i) {
40 var = config->symbols->items[i];
41 if (strcmp(var->name, argv[0]) == 0) {
42 break;
43 }
44 var = NULL;
45 }
46 if (var) {
47 free(var->value);
48 } else {
49 var = malloc(sizeof(struct sway_variable));
50 var->name = strdup(argv[0]);
51 list_add(config->symbols, var);
52 list_qsort(config->symbols, compare_set_qsort);
53 }
54 var->value = join_args(argv + 1, argc - 1);
55 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
56}
diff --git a/sway/commands/smart_gaps.c b/sway/commands/smart_gaps.c
new file mode 100644
index 00000000..d1266a13
--- /dev/null
+++ b/sway/commands/smart_gaps.c
@@ -0,0 +1,19 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_smart_gaps(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "smart_gaps", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 if (strcasecmp(argv[0], "on") == 0) {
11 config->smart_gaps = true;
12 } else if (strcasecmp(argv[0], "off") == 0) {
13 config->smart_gaps = false;
14 } else {
15 return cmd_results_new(CMD_INVALID, "smart_gaps", "Expected 'smart_gaps <on|off>'");
16 }
17
18 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
19}
diff --git a/sway/commands/split.c b/sway/commands/split.c
new file mode 100644
index 00000000..b13c9213
--- /dev/null
+++ b/sway/commands/split.c
@@ -0,0 +1,97 @@
1#include <string.h>
2#include <wlc/wlc-render.h>
3#include "border.h"
4#include "commands.h"
5#include "container.h"
6#include "focus.h"
7#include "layout.h"
8#include "log.h"
9
10static struct cmd_results *_do_split(int argc, char **argv, int layout) {
11 char *name = layout == L_VERT ? "splitv" :
12 layout == L_HORIZ ? "splith" : "split";
13 struct cmd_results *error = NULL;
14 if (config->reading) return cmd_results_new(CMD_FAILURE, name, "Can't be used in config file.");
15 if (!config->active) return cmd_results_new(CMD_FAILURE, name, "Can only be used when sway is running.");
16 if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) {
17 return error;
18 }
19 swayc_t *focused = get_focused_container(&root_container);
20
21 // Case of floating window, don't split
22 if (focused->is_floating) {
23 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
24 }
25 /* Case that focus is on an workspace with 0/1 children.change its layout */
26 if (focused->type == C_WORKSPACE && focused->children->length <= 1) {
27 sway_log(L_DEBUG, "changing workspace layout");
28 focused->layout = layout;
29 } else if (focused->type != C_WORKSPACE && focused->parent->children->length == 1) {
30 /* Case of no siblings. change parent layout */
31 sway_log(L_DEBUG, "changing container layout");
32 focused->parent->layout = layout;
33 } else {
34 /* regular case where new split container is build around focused container
35 * or in case of workspace, container inherits its children */
36 sway_log(L_DEBUG, "Adding new container around current focused container");
37 sway_log(L_INFO, "FOCUSED SIZE: %.f %.f", focused->width, focused->height);
38 swayc_t *parent = new_container(focused, layout);
39 set_focused_container(focused);
40 arrange_windows(parent, -1, -1);
41 }
42
43 // update container every time
44 // if it is tabbed/stacked then the title must change
45 // if the indicator color is different then the border must change
46 update_container_border(focused);
47 swayc_t *output = swayc_parent_by_type(focused, C_OUTPUT);
48 // schedule render to make changes take effect right away,
49 // otherwise we would have to wait for the view to render,
50 // which is unpredictable.
51 wlc_output_schedule_render(output->handle);
52
53 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
54}
55
56struct cmd_results *cmd_split(int argc, char **argv) {
57 struct cmd_results *error = NULL;
58 if (config->reading) return cmd_results_new(CMD_FAILURE, "split", "Can't be used in config file.");
59 if (!config->active) return cmd_results_new(CMD_FAILURE, "split", "Can only be used when sway is running.");
60 if ((error = checkarg(argc, "split", EXPECTED_EQUAL_TO, 1))) {
61 return error;
62 }
63 if (strcasecmp(argv[0], "v") == 0 || strcasecmp(argv[0], "vertical") == 0) {
64 _do_split(argc - 1, argv + 1, L_VERT);
65 } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) {
66 _do_split(argc - 1, argv + 1, L_HORIZ);
67 } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) {
68 swayc_t *focused = get_focused_container(&root_container);
69 if (focused->parent->layout == L_VERT) {
70 _do_split(argc - 1, argv + 1, L_HORIZ);
71 } else {
72 _do_split(argc - 1, argv + 1, L_VERT);
73 }
74 } else {
75 error = cmd_results_new(CMD_FAILURE, "split",
76 "Invalid split command (expected either horizontal or vertical).");
77 return error;
78 }
79 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
80}
81
82struct cmd_results *cmd_splitv(int argc, char **argv) {
83 return _do_split(argc, argv, L_VERT);
84}
85
86struct cmd_results *cmd_splith(int argc, char **argv) {
87 return _do_split(argc, argv, L_HORIZ);
88}
89
90struct cmd_results *cmd_splitt(int argc, char **argv) {
91 swayc_t *focused = get_focused_container(&root_container);
92 if (focused->parent->layout == L_VERT) {
93 return _do_split(argc, argv, L_HORIZ);
94 } else {
95 return _do_split(argc, argv, L_VERT);
96 }
97}
diff --git a/sway/commands/sticky.c b/sway/commands/sticky.c
new file mode 100644
index 00000000..00c47123
--- /dev/null
+++ b/sway/commands/sticky.c
@@ -0,0 +1,25 @@
1#include <string.h>
2#include "commands.h"
3#include "focus.h"
4
5struct cmd_results *cmd_sticky(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if (config->reading) return cmd_results_new(CMD_FAILURE, "sticky", "Can't be used in config file.");
8 if (!config->active) return cmd_results_new(CMD_FAILURE, "sticky", "Can only be used when sway is running.");
9 if ((error = checkarg(argc, "sticky", EXPECTED_EQUAL_TO, 1))) {
10 return error;
11 }
12 char *action = argv[0];
13 swayc_t *cont = get_focused_view(&root_container);
14 if (strcmp(action, "toggle") == 0) {
15 cont->sticky = !cont->sticky;
16 } else if (strcmp(action, "enable") == 0) {
17 cont->sticky = true;
18 } else if (strcmp(action, "disable") == 0) {
19 cont->sticky = false;
20 } else {
21 return cmd_results_new(CMD_FAILURE, "sticky",
22 "Expected 'sticky enable|disable|toggle'");
23 }
24 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
25}
diff --git a/sway/commands/workspace.c b/sway/commands/workspace.c
new file mode 100644
index 00000000..5eaca423
--- /dev/null
+++ b/sway/commands/workspace.c
@@ -0,0 +1,78 @@
1#include <string.h>
2#include "commands.h"
3#include "config.h"
4#include "input_state.h"
5#include "list.h"
6#include "log.h"
7#include "workspace.h"
8
9struct cmd_results *cmd_workspace(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) {
12 return error;
13 }
14 if (argc == 1 || (argc == 2 && strcasecmp(argv[0], "number") == 0) ) {
15 if (config->reading || !config->active) {
16 return cmd_results_new(CMD_DEFER, "workspace", NULL);
17 }
18 // Handle workspace next/prev
19 swayc_t *ws = NULL;
20 if (argc == 2) {
21 if (!(ws = workspace_by_number(argv[1]))) {
22 ws = workspace_create(argv[1]);
23 }
24 } else if (strcasecmp(argv[0], "next") == 0) {
25 ws = workspace_next();
26 } else if (strcasecmp(argv[0], "prev") == 0) {
27 ws = workspace_prev();
28 } else if (strcasecmp(argv[0], "next_on_output") == 0) {
29 ws = workspace_output_next();
30 } else if (strcasecmp(argv[0], "prev_on_output") == 0) {
31 ws = workspace_output_prev();
32 } else if (strcasecmp(argv[0], "back_and_forth") == 0) {
33 // if auto_back_and_forth is enabled, workspace_switch will swap
34 // the workspaces. If we created prev_workspace here, workspace_switch
35 // would put us back on original workspace.
36 if (config->auto_back_and_forth) {
37 ws = swayc_active_workspace();
38 } else if (prev_workspace_name && !(ws = workspace_by_name(prev_workspace_name))) {
39 ws = workspace_create(prev_workspace_name);
40 }
41 } else {
42 if (!(ws = workspace_by_name(argv[0]))) {
43 ws = workspace_create(argv[0]);
44 }
45 }
46 swayc_t *old_output = swayc_active_output();
47 workspace_switch(ws);
48 swayc_t *new_output = swayc_active_output();
49
50 if (config->mouse_warping && old_output != new_output) {
51 swayc_t *focused = get_focused_view(ws);
52 if (focused && focused->type == C_VIEW) {
53 center_pointer_on(focused);
54 }
55 }
56 } else {
57 if (strcasecmp(argv[1], "output") == 0) {
58 if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 3))) {
59 return error;
60 }
61 struct workspace_output *wso = calloc(1, sizeof(struct workspace_output));
62 wso->workspace = strdup(argv[0]);
63 wso->output = strdup(argv[2]);
64 int i = -1;
65 if ((i = list_seq_find(config->workspace_outputs, workspace_output_cmp_workspace, wso)) != -1) {
66 struct workspace_output *old = config->workspace_outputs->items[i];
67 free(old); // workspaces can only be assigned to a single output
68 list_del(config->workspace_outputs, i);
69 }
70 sway_log(L_DEBUG, "Assigning workspace %s to output %s", argv[0], argv[2]);
71 list_add(config->workspace_outputs, wso);
72 if (!config->reading) {
73 // TODO: Move workspace to output. (don't do so when reloading)
74 }
75 }
76 }
77 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
78}
diff --git a/sway/commands/workspace_auto_back_and_forth.c b/sway/commands/workspace_auto_back_and_forth.c
new file mode 100644
index 00000000..c099ad0e
--- /dev/null
+++ b/sway/commands/workspace_auto_back_and_forth.c
@@ -0,0 +1,17 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "workspace_auto_back_and_forth", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9 if (strcasecmp(argv[0], "yes") == 0) {
10 config->auto_back_and_forth = true;
11 } else if (strcasecmp(argv[0], "no") == 0) {
12 config->auto_back_and_forth = false;
13 } else {
14 return cmd_results_new(CMD_INVALID, "workspace_auto_back_and_forth", "Expected 'workspace_auto_back_and_forth <yes|no>'");
15 }
16 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
17}
diff --git a/sway/commands/workspace_layout.c b/sway/commands/workspace_layout.c
new file mode 100644
index 00000000..2b424916
--- /dev/null
+++ b/sway/commands/workspace_layout.c
@@ -0,0 +1,20 @@
1#include <string.h>
2#include "commands.h"
3
4struct cmd_results *cmd_workspace_layout(int argc, char **argv) {
5 struct cmd_results *error = NULL;
6 if ((error = checkarg(argc, "workspace_layout", EXPECTED_EQUAL_TO, 1))) {
7 return error;
8 }
9
10 if (strcasecmp(argv[0], "default") == 0) {
11 config->default_layout = L_NONE;
12 } else if (strcasecmp(argv[0], "stacking") == 0) {
13 config->default_layout = L_STACKED;
14 } else if (strcasecmp(argv[0], "tabbed") == 0) {
15 config->default_layout = L_TABBED;
16 } else {
17 return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout <default|stacking|tabbed>'");
18 }
19 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
20}