summaryrefslogtreecommitdiffstats
path: root/sway/workspace.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-09-13 19:46:16 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-09-13 19:46:16 -0400
commite505abfe75923d06098f6230e5a7ba39c091d3ce (patch)
treefc90bdf0afed21d725f6ee7a9245e397e1577517 /sway/workspace.c
parentMerge pull request #179 from taiyu-len/master (diff)
downloadsway-e505abfe75923d06098f6230e5a7ba39c091d3ce.tar.gz
sway-e505abfe75923d06098f6230e5a7ba39c091d3ce.tar.zst
sway-e505abfe75923d06098f6230e5a7ba39c091d3ce.zip
Revert "new_workspace null behavior + testmap functions + regex"
This reverts commit e1d18e42a8f3a597b9bf5f1bb2ab6c346e4e7983. Fixes #180 cc @taiyu-len
Diffstat (limited to 'sway/workspace.c')
-rw-r--r--sway/workspace.c149
1 files changed, 79 insertions, 70 deletions
diff --git a/sway/workspace.c b/sway/workspace.c
index 80141f71..658f79bc 100644
--- a/sway/workspace.c
+++ b/sway/workspace.c
@@ -15,26 +15,7 @@
15 15
16char *prev_workspace_name = NULL; 16char *prev_workspace_name = NULL;
17 17
18static swayc_t *workspace_by_name_only(const char *name); 18char *workspace_next_name(void) {
19
20const char *workspace_output_open_name(swayc_t *output) {
21 struct workspace_output *wsop;
22 int i, len = config->workspace_outputs->length;
23 // Search config for output
24 for (i = 0; i < len; ++i) {
25 wsop = config->workspace_outputs->items[i];
26 // Find matching outputs
27 if (strcasecmp(wsop->output, output->name)) {
28 // Check if workspace is available and use that name
29 if (!workspace_by_name(wsop->workspace)) {
30 return wsop->workspace;
31 }
32 }
33 }
34 return NULL;
35}
36
37const char *workspace_next_name(void) {
38 sway_log(L_DEBUG, "Workspace: Generating new name"); 19 sway_log(L_DEBUG, "Workspace: Generating new name");
39 int i; 20 int i;
40 int l = 1; 21 int l = 1;
@@ -44,29 +25,42 @@ const char *workspace_next_name(void) {
44 25
45 for (i = 0; i < mode->bindings->length; ++i) { 26 for (i = 0; i < mode->bindings->length; ++i) {
46 struct sway_binding *binding = mode->bindings->items[i]; 27 struct sway_binding *binding = mode->bindings->items[i];
47 const char *command = binding->command; 28 const char* command = binding->command;
48 const char *ws = "workspace"; 29 list_t *args = split_string(command, " ");
49 const int wslen = sizeof("workspace") - 1; 30
50 if (strncmp(ws, command, wslen) == 0) { 31 if (strcmp("workspace", args->items[0]) == 0 && args->length > 1) {
51 command += wslen; 32 sway_log(L_DEBUG, "Got valid workspace command for target: '%s'", (char *)args->items[1]);
52 // Skip whitespace 33 char* target = malloc(strlen(args->items[1]) + 1);
53 command += strspn(command, whitespace); 34 strcpy(target, args->items[1]);
54 // make sure its not a special command 35 while (*target == ' ' || *target == '\t')
55 if (strcmp(command, "next") == 0 36 target++;
56 || strcmp(command, "prev") == 0 37
57 || strcmp(command, "next_on_output") == 0 38 // Make sure that the command references an actual workspace
58 || strcmp(command, "prev_on_output") == 0 39 // not a command about workspaces
59 || strcmp(command, "number") == 0 40 if (strcmp(target, "next") == 0 ||
60 || strcmp(command, "back_and_forth") == 0 41 strcmp(target, "prev") == 0 ||
61 || strcmp(command, "current") == 0 42 strcmp(target, "next_on_output") == 0 ||
62 // Or if it already exists 43 strcmp(target, "prev_on_output") == 0 ||
63 || workspace_by_name_only(command)) { 44 strcmp(target, "number") == 0 ||
45 strcmp(target, "back_and_forth") == 0 ||
46 strcmp(target, "current") == 0)
47 {
48 free_flat_list(args);
64 continue; 49 continue;
65 } else {
66 // otherwise we found it
67 return command;
68 } 50 }
51
52 // Make sure that the workspace doesn't already exist
53 if (workspace_by_name(target)) {
54 free_flat_list(args);
55 continue;
56 }
57
58 free_flat_list(args);
59
60 sway_log(L_DEBUG, "Workspace: Found free name %s", target);
61 return target;
69 } 62 }
63 free_flat_list(args);
70 } 64 }
71 // As a fall back, get the current number of active workspaces 65 // As a fall back, get the current number of active workspaces
72 // and return that + 1 for the next workspace's name 66 // and return that + 1 for the next workspace's name
@@ -81,40 +75,55 @@ const char *workspace_next_name(void) {
81 return name; 75 return name;
82} 76}
83 77
84swayc_t *workspace_by_name_only(const char *name) { 78swayc_t *workspace_create(const char* name) {
85 int i, len = root_container.children->length; 79 swayc_t *parent;
86 for (i = 0; i < len; ++i) { 80 // Search for workspace<->output pair
87 swayc_t *op = root_container.children->items[i]; 81 int i, e = config->workspace_outputs->length;
88 int i, len = op->children->length; 82 for (i = 0; i < e; ++i) {
89 for (i = 0; i < len; ++i) { 83 struct workspace_output *wso = config->workspace_outputs->items[i];
90 swayc_t *ws = op->children->items[i]; 84 if (strcasecmp(wso->workspace, name) == 0)
91 if (strcasecmp(ws->name, name) == 0) { 85 {
92 return ws; 86 // Find output to use if it exists
87 e = root_container.children->length;
88 for (i = 0; i < e; ++i) {
89 parent = root_container.children->items[i];
90 if (strcmp(parent->name, wso->output) == 0) {
91 return new_workspace(parent, name);
92 }
93 } 93 }
94 break;
94 } 95 }
95 } 96 }
96 return NULL; 97 // Otherwise create a new one
98 parent = get_focused_container(&root_container);
99 parent = swayc_parent_by_type(parent, C_OUTPUT);
100 return new_workspace(parent, name);
101}
102
103static bool _workspace_by_name(swayc_t *view, void *data) {
104 return (view->type == C_WORKSPACE) &&
105 (strcasecmp(view->name, (char *) data) == 0);
97} 106}
98 107
99swayc_t *workspace_by_name(const char* name) { 108swayc_t *workspace_by_name(const char* name) {
100 if (strcmp(name, "prev") == 0) { 109 if (strcmp(name, "prev") == 0) {
101 return workspace_prev(); 110 return workspace_prev();
102 } else if (!strcmp(name, "prev_on_output")) { 111 }
112 else if (strcmp(name, "prev_on_output") == 0) {
103 return workspace_output_prev(); 113 return workspace_output_prev();
104 } else if (!strcmp(name, "next")) { 114 }
115 else if (strcmp(name, "next") == 0) {
105 return workspace_next(); 116 return workspace_next();
106 } else if (!strcmp(name, "next_on_output")) { 117 }
118 else if (strcmp(name, "next_on_output") == 0) {
107 return workspace_output_next(); 119 return workspace_output_next();
108 } else if (!strcmp(name, "current")) { 120 }
121 else if (strcmp(name, "current") == 0) {
109 return swayc_active_workspace(); 122 return swayc_active_workspace();
110 } else if (!strcmp(name, "back_and_forth")) {
111 if (prev_workspace_name) {
112 name = prev_workspace_name;
113 } else { // If there is no prev workspace name. just return current
114 return swayc_active_workspace();
115 }
116 } 123 }
117 return workspace_by_name_only(name); 124 else {
125 return swayc_by_test(&root_container, _workspace_by_name, (void *) name);
126 }
118} 127}
119 128
120/** 129/**
@@ -172,19 +181,19 @@ swayc_t *workspace_prev_next_impl(swayc_t *workspace, bool next) {
172 return NULL; 181 return NULL;
173} 182}
174 183
175swayc_t *workspace_output_next(void) { 184swayc_t *workspace_output_next() {
176 return workspace_output_prev_next_impl(swayc_active_output(), true); 185 return workspace_output_prev_next_impl(swayc_active_output(), true);
177} 186}
178 187
179swayc_t *workspace_next(void) { 188swayc_t *workspace_next() {
180 return workspace_prev_next_impl(swayc_active_workspace(), true); 189 return workspace_prev_next_impl(swayc_active_workspace(), true);
181} 190}
182 191
183swayc_t *workspace_output_prev(void) { 192swayc_t *workspace_output_prev() {
184 return workspace_output_prev_next_impl(swayc_active_output(), false); 193 return workspace_output_prev_next_impl(swayc_active_output(), false);
185} 194}
186 195
187swayc_t *workspace_prev(void) { 196swayc_t *workspace_prev() {
188 return workspace_prev_next_impl(swayc_active_workspace(), false); 197 return workspace_prev_next_impl(swayc_active_workspace(), false);
189} 198}
190 199
@@ -193,17 +202,17 @@ void workspace_switch(swayc_t *workspace) {
193 return; 202 return;
194 } 203 }
195 swayc_t *active_ws = swayc_active_workspace(); 204 swayc_t *active_ws = swayc_active_workspace();
196 // set workspace to prev_workspace 205 if (config->auto_back_and_forth && active_ws == workspace && prev_workspace_name) {
197 if (config->auto_back_and_forth && active_ws == workspace) { 206 swayc_t *new_ws = workspace_by_name(prev_workspace_name);
198 workspace = new_workspace(NULL, "back_and_forth"); 207 workspace = new_ws ? new_ws : workspace_create(prev_workspace_name);
199 } 208 }
200 209
201 // set prev workspace name
202 if (!prev_workspace_name 210 if (!prev_workspace_name
203 || (strcmp(prev_workspace_name, active_ws->name) 211 || (strcmp(prev_workspace_name, active_ws->name)
204 && active_ws != workspace)) { 212 && active_ws != workspace)) {
205 free(prev_workspace_name); 213 free(prev_workspace_name);
206 prev_workspace_name = strdup(active_ws->name); 214 prev_workspace_name = malloc(strlen(active_ws->name)+1);
215 strcpy(prev_workspace_name, active_ws->name);
207 } 216 }
208 217
209 sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name); 218 sway_log(L_DEBUG, "Switching to workspace %p:%s", workspace, workspace->name);