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