aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/workspace.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-11-11 11:00:18 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2017-11-11 11:00:18 -0500
commit0ba6554c4f6c923274062862d895240eea4de350 (patch)
treee2b94dee4e8049f3a39365e82913559660f87bcd /sway/tree/workspace.c
parentEstablish sway input submodule (diff)
downloadsway-0ba6554c4f6c923274062862d895240eea4de350.tar.gz
sway-0ba6554c4f6c923274062862d895240eea4de350.tar.zst
sway-0ba6554c4f6c923274062862d895240eea4de350.zip
Move sway's internal tree code to sway/tree/
Diffstat (limited to 'sway/tree/workspace.c')
-rw-r--r--sway/tree/workspace.c373
1 files changed, 373 insertions, 0 deletions
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
new file mode 100644
index 00000000..14cde146
--- /dev/null
+++ b/sway/tree/workspace.c
@@ -0,0 +1,373 @@
1#define _XOPEN_SOURCE 500
2#include <stdlib.h>
3#include <stdbool.h>
4#include <limits.h>
5#include <ctype.h>
6#include <wlc/wlc.h>
7#include <string.h>
8#include <strings.h>
9#include <sys/types.h>
10#include "sway/ipc-server.h"
11#include "sway/workspace.h"
12#include "sway/layout.h"
13#include "sway/container.h"
14#include "sway/handlers.h"
15#include "sway/config.h"
16#include "sway/focus.h"
17#include "stringop.h"
18#include "util.h"
19#include "list.h"
20#include "log.h"
21#include "ipc.h"
22
23char *prev_workspace_name = NULL;
24struct workspace_by_number_data {
25 int len;
26 const char *cset;
27 const char *name;
28};
29
30static bool workspace_valid_on_output(const char *output_name, const char *ws_name) {
31 int i;
32 for (i = 0; i < config->workspace_outputs->length; ++i) {
33 struct workspace_output *wso = config->workspace_outputs->items[i];
34 if (strcasecmp(wso->workspace, ws_name) == 0) {
35 if (strcasecmp(wso->output, output_name) != 0) {
36 return false;
37 }
38 }
39 }
40
41 return true;
42}
43
44char *workspace_next_name(const char *output_name) {
45 sway_log(L_DEBUG, "Workspace: Generating new workspace name for output %s", output_name);
46 int i;
47 int l = 1;
48 // Scan all workspace bindings to find the next available workspace name,
49 // if none are found/available then default to a number
50 struct sway_mode *mode = config->current_mode;
51
52 int order = INT_MAX;
53 char *target = NULL;
54 for (i = 0; i < mode->bindings->length; ++i) {
55 struct sway_binding *binding = mode->bindings->items[i];
56 char *cmdlist = strdup(binding->command);
57 char *dup = cmdlist;
58 char *name = NULL;
59
60 // workspace n
61 char *cmd = argsep(&cmdlist, " ");
62 if (cmdlist) {
63 name = argsep(&cmdlist, ",;");
64 }
65
66 if (strcmp("workspace", cmd) == 0 && name) {
67 sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", name);
68 char *_target = strdup(name);
69 strip_quotes(_target);
70 while (isspace(*_target))
71 _target++;
72
73 // Make sure that the command references an actual workspace
74 // not a command about workspaces
75 if (strcmp(_target, "next") == 0 ||
76 strcmp(_target, "prev") == 0 ||
77 strcmp(_target, "next_on_output") == 0 ||
78 strcmp(_target, "prev_on_output") == 0 ||
79 strcmp(_target, "number") == 0 ||
80 strcmp(_target, "back_and_forth") == 0 ||
81 strcmp(_target, "current") == 0)
82 {
83 free(_target);
84 free(dup);
85 continue;
86 }
87
88 // Make sure that the workspace doesn't already exist
89 if (workspace_by_name(_target)) {
90 free(_target);
91 free(dup);
92 continue;
93 }
94
95 // make sure that the workspace can appear on the given
96 // output
97 if (!workspace_valid_on_output(output_name, _target)) {
98 free(_target);
99 free(dup);
100 continue;
101 }
102
103 if (binding->order < order) {
104 order = binding->order;
105 free(target);
106 target = _target;
107 sway_log(L_DEBUG, "Workspace: Found free name %s", _target);
108 }
109 }
110 free(dup);
111 }
112 if (target != NULL) {
113 return target;
114 }
115 // As a fall back, get the current number of active workspaces
116 // and return that + 1 for the next workspace's name
117 int ws_num = root_container.children->length;
118 if (ws_num >= 10) {
119 l = 2;
120 } else if (ws_num >= 100) {
121 l = 3;
122 }
123 char *name = malloc(l + 1);
124 if (!name) {
125 sway_log(L_ERROR, "Could not allocate workspace name");
126 return NULL;
127 }
128 sprintf(name, "%d", ws_num++);
129 return name;
130}
131
132swayc_t *workspace_create(const char* name) {
133 swayc_t *parent;
134 // Search for workspace<->output pair
135 int i, e = config->workspace_outputs->length;
136 for (i = 0; i < e; ++i) {
137 struct workspace_output *wso = config->workspace_outputs->items[i];
138 if (strcasecmp(wso->workspace, name) == 0)
139 {
140 // Find output to use if it exists
141 e = root_container.children->length;
142 for (i = 0; i < e; ++i) {
143 parent = root_container.children->items[i];
144 if (strcmp(parent->name, wso->output) == 0) {
145 return new_workspace(parent, name);
146 }
147 }
148 break;
149 }
150 }
151 // Otherwise create a new one
152 parent = get_focused_container(&root_container);
153 parent = swayc_parent_by_type(parent, C_OUTPUT);
154 return new_workspace(parent, name);
155}
156
157static bool _workspace_by_name(swayc_t *view, void *data) {
158 return (view->type == C_WORKSPACE) &&
159 (strcasecmp(view->name, (char *) data) == 0);
160}
161
162swayc_t *workspace_by_name(const char* name) {
163 if (strcmp(name, "prev") == 0) {
164 return workspace_prev();
165 }
166 else if (strcmp(name, "prev_on_output") == 0) {
167 return workspace_output_prev();
168 }
169 else if (strcmp(name, "next") == 0) {
170 return workspace_next();
171 }
172 else if (strcmp(name, "next_on_output") == 0) {
173 return workspace_output_next();
174 }
175 else if (strcmp(name, "current") == 0) {
176 return swayc_active_workspace();
177 }
178 else {
179 return swayc_by_test(&root_container, _workspace_by_name, (void *) name);
180 }
181}
182
183static bool _workspace_by_number(swayc_t *view, void *data) {
184 if (view->type != C_WORKSPACE) {
185 return false;
186 }
187 struct workspace_by_number_data *wbnd = data;
188 int a = strspn(view->name, wbnd->cset);
189 return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0;
190}
191swayc_t *workspace_by_number(const char* name) {
192 struct workspace_by_number_data wbnd = {0, "1234567890", name};
193 wbnd.len = strspn(name, wbnd.cset);
194 if (wbnd.len <= 0) {
195 return NULL;
196 }
197 return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd);
198}
199
200/**
201 * Get the previous or next workspace on the specified output.
202 * Wraps around at the end and beginning.
203 * If next is false, the previous workspace is returned, otherwise the next one is returned.
204 */
205swayc_t *workspace_output_prev_next_impl(swayc_t *output, bool next) {
206 if (!sway_assert(output->type == C_OUTPUT, "Argument must be an output, is %d", output->type)) {
207 return NULL;
208 }
209
210 int i;
211 for (i = 0; i < output->children->length; i++) {
212 if (output->children->items[i] == output->focused) {
213 return output->children->items[wrap(i + (next ? 1 : -1), output->children->length)];
214 }
215 }
216
217 // Doesn't happen, at worst the for loop returns the previously active workspace
218 return NULL;
219}
220
221/**
222 * Get the previous or next workspace. If the first/last workspace on an output is active,
223 * proceed to the previous/next output's previous/next workspace.
224 * If next is false, the previous workspace is returned, otherwise the next one is returned.
225 */
226swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) {
227 if (!sway_assert(workspace->type == C_WORKSPACE, "Argument must be a workspace, is %d", workspace->type)) {
228 return NULL;
229 }
230
231 swayc_t *current_output = workspace->parent;
232 int offset = next ? 1 : -1;
233 int start = next ? 0 : 1;
234 int end = next ? (current_output->children->length) - 1 : current_output->children->length;
235 int i;
236 for (i = start; i < end; i++) {
237 if (current_output->children->items[i] == workspace) {
238 return current_output->children->items[i + offset];
239 }
240 }
241
242 // Given workspace is the first/last on the output, jump to the previous/next output
243 int num_outputs = root_container.children->length;
244 for (i = 0; i < num_outputs; i++) {
245 if (root_container.children->items[i] == current_output) {
246 swayc_t *next_output = root_container.children->items[wrap(i + offset, num_outputs)];
247 return workspace_output_prev_next_impl(next_output, next);
248 }
249 }
250
251 // Doesn't happen, at worst the for loop returns the previously active workspace on the active output
252 return NULL;
253}
254
255swayc_t *workspace_output_next() {
256 return workspace_output_prev_next_impl(swayc_active_output(), true);
257}
258
259swayc_t *workspace_next() {
260 return workspace_prev_next_impl(swayc_active_workspace(), true);
261}
262
263swayc_t *workspace_output_prev() {
264 return workspace_output_prev_next_impl(swayc_active_output(), false);
265}
266
267swayc_t *workspace_prev() {
268 return workspace_prev_next_impl(swayc_active_workspace(), false);
269}
270
271bool workspace_switch(swayc_t *workspace) {
272 if (!workspace) {
273 return false;
274 }
275 swayc_t *active_ws = swayc_active_workspace();
276 if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) {
277 swayc_t *new_ws = workspace_by_name(prev_workspace_name);
278 workspace = new_ws ? new_ws : workspace_create(prev_workspace_name);
279 }
280
281 if (!prev_workspace_name
282 || (strcmp(prev_workspace_name, active_ws->name)
283 && active_ws != workspace)) {
284 free(prev_workspace_name);
285 prev_workspace_name = malloc(strlen(active_ws->name) + 1);
286 if (!prev_workspace_name) {
287 sway_log(L_ERROR, "Unable to allocate previous workspace name");
288 return false;
289 }
290 strcpy(prev_workspace_name, active_ws->name);
291 }
292
293 // move sticky containers
294 if (swayc_parent_by_type(active_ws, C_OUTPUT) == swayc_parent_by_type(workspace, C_OUTPUT)) {
295 // don't change list while traversing it, use intermediate list instead
296 list_t *stickies = create_list();
297 for (int i = 0; i < active_ws->floating->length; i++) {
298 swayc_t *cont = active_ws->floating->items[i];
299 if (cont->sticky) {
300 list_add(stickies, cont);
301 }
302 }
303 for (int i = 0; i < stickies->length; i++) {
304 swayc_t *cont = stickies->items[i];
305 sway_log(L_DEBUG, "Moving sticky container %p to %p:%s",
306 cont, workspace, workspace->name);
307 swayc_t *parent = remove_child(cont);
308 add_floating(workspace, cont);
309 // Destroy old container if we need to
310 destroy_container(parent);
311 }
312 list_free(stickies);
313 }
314 sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name);
315 if (!set_focused_container(get_focused_view(workspace))) {
316 return false;
317 }
318 swayc_t *output = swayc_parent_by_type(workspace, C_OUTPUT);
319 arrange_backgrounds();
320 arrange_windows(output, -1, -1);
321 return true;
322}
323
324swayc_t *workspace_for_pid(pid_t pid) {
325 int i;
326 swayc_t *ws = NULL;
327 struct pid_workspace *pw = NULL;
328
329 sway_log(L_DEBUG, "looking for workspace for pid %d", pid);
330
331 // leaving this here as it's useful for debugging
332 // sway_log(L_DEBUG, "all pid_workspaces");
333 // for (int k = 0; k < config->pid_workspaces->length; k++) {
334 // pw = config->pid_workspaces->items[k];
335 // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added);
336 // }
337
338 do {
339 for (i = 0; i < config->pid_workspaces->length; i++) {
340 pw = config->pid_workspaces->items[i];
341 pid_t *pw_pid = pw->pid;
342
343 if (pid == *pw_pid) {
344 sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace);
345 break; // out of for loop
346 }
347
348 pw = NULL;
349 }
350
351 if (pw) {
352 break; // out of do-while loop
353 }
354
355 pid = get_parent_pid(pid);
356 // no sense in looking for matches for pid 0.
357 // also, if pid == getpid(), that is the compositor's
358 // pid, which definitely isn't helpful
359 } while (pid > 0 && pid != getpid());
360
361 if (pw) {
362 ws = workspace_by_name(pw->workspace);
363
364 if (!ws) {
365 sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid);
366 ws = workspace_create(pw->workspace);
367 }
368
369 list_del(config->pid_workspaces, i);
370 }
371
372 return ws;
373}