diff options
-rw-r--r-- | common/util.c | 39 | ||||
-rw-r--r-- | include/config.h | 8 | ||||
-rw-r--r-- | include/util.h | 8 | ||||
-rw-r--r-- | include/workspace.h | 2 | ||||
-rw-r--r-- | sway/commands.c | 13 | ||||
-rw-r--r-- | sway/config.c | 15 | ||||
-rw-r--r-- | sway/handlers.c | 30 | ||||
-rw-r--r-- | sway/workspace.c | 53 |
8 files changed, 163 insertions, 5 deletions
diff --git a/common/util.c b/common/util.c index 13397437..e760443a 100644 --- a/common/util.c +++ b/common/util.c | |||
@@ -1,6 +1,10 @@ | |||
1 | #include <math.h> | 1 | #include <math.h> |
2 | 2 | #include <stdio.h> | |
3 | #include <string.h> | ||
4 | #include <stdlib.h> | ||
5 | #include "readline.h" | ||
3 | #include "util.h" | 6 | #include "util.h" |
7 | #include "log.h" | ||
4 | 8 | ||
5 | int wrap(int i, int max) { | 9 | int wrap(int i, int max) { |
6 | return ((i % max) + max) % max; | 10 | return ((i % max) + max) % max; |
@@ -64,3 +68,36 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { | |||
64 | 68 | ||
65 | return length; | 69 | return length; |
66 | } | 70 | } |
71 | |||
72 | pid_t get_parent_pid(pid_t child) { | ||
73 | pid_t parent; | ||
74 | char file_name[100]; | ||
75 | char *buffer = NULL; | ||
76 | char *token = NULL; | ||
77 | const char sep[2] = " "; | ||
78 | FILE *stat = NULL; | ||
79 | |||
80 | sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child); | ||
81 | |||
82 | sprintf(file_name, "/proc/%d/stat", child); | ||
83 | |||
84 | if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { | ||
85 | return -1; | ||
86 | } | ||
87 | |||
88 | fclose(stat); | ||
89 | |||
90 | sway_log(L_DEBUG, "buffer string is %s", buffer); | ||
91 | |||
92 | token = strtok(buffer, sep); | ||
93 | |||
94 | for (int i = 0; i < 3; i++) { | ||
95 | token = strtok(NULL, sep); | ||
96 | } | ||
97 | |||
98 | parent = strtol(token, NULL, 10); | ||
99 | |||
100 | sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); | ||
101 | |||
102 | return (parent == child) ? -1 : parent; | ||
103 | } | ||
diff --git a/include/config.h b/include/config.h index d591daf2..35797ac2 100644 --- a/include/config.h +++ b/include/config.h | |||
@@ -92,6 +92,13 @@ struct workspace_output { | |||
92 | char *workspace; | 92 | char *workspace; |
93 | }; | 93 | }; |
94 | 94 | ||
95 | struct pid_workspace { | ||
96 | pid_t *pid; | ||
97 | char *workspace; | ||
98 | }; | ||
99 | |||
100 | void free_pid_workspace(struct pid_workspace *pw); | ||
101 | |||
95 | struct bar_config { | 102 | struct bar_config { |
96 | /** | 103 | /** |
97 | * One of "dock", "hide", "invisible" | 104 | * One of "dock", "hide", "invisible" |
@@ -175,6 +182,7 @@ struct sway_config { | |||
175 | list_t *bars; | 182 | list_t *bars; |
176 | list_t *cmd_queue; | 183 | list_t *cmd_queue; |
177 | list_t *workspace_outputs; | 184 | list_t *workspace_outputs; |
185 | list_t *pid_workspaces; | ||
178 | list_t *output_configs; | 186 | list_t *output_configs; |
179 | list_t *input_configs; | 187 | list_t *input_configs; |
180 | list_t *criteria; | 188 | list_t *criteria; |
diff --git a/include/util.h b/include/util.h index dc47e343..6f21bff0 100644 --- a/include/util.h +++ b/include/util.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define _SWAY_UTIL_H | 2 | #define _SWAY_UTIL_H |
3 | 3 | ||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | #include <unistd.h> | ||
5 | #include <wlc/wlc.h> | 6 | #include <wlc/wlc.h> |
6 | #include <xkbcommon/xkbcommon.h> | 7 | #include <xkbcommon/xkbcommon.h> |
7 | 8 | ||
@@ -36,4 +37,11 @@ const char *get_modifier_name_by_mask(uint32_t modifier); | |||
36 | */ | 37 | */ |
37 | int get_modifier_names(const char **names, uint32_t modifier_masks); | 38 | int get_modifier_names(const char **names, uint32_t modifier_masks); |
38 | 39 | ||
40 | /** | ||
41 | * Get the pid of a parent process given the pid of a child process. | ||
42 | * | ||
43 | * Returns the parent pid or NULL if the parent pid cannot be determined. | ||
44 | */ | ||
45 | pid_t get_parent_pid(pid_t pid); | ||
46 | |||
39 | #endif | 47 | #endif |
diff --git a/include/workspace.h b/include/workspace.h index 6911e3d4..c268fafa 100644 --- a/include/workspace.h +++ b/include/workspace.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define _SWAY_WORKSPACE_H | 2 | #define _SWAY_WORKSPACE_H |
3 | 3 | ||
4 | #include <wlc/wlc.h> | 4 | #include <wlc/wlc.h> |
5 | #include <unistd.h> | ||
5 | #include "list.h" | 6 | #include "list.h" |
6 | #include "layout.h" | 7 | #include "layout.h" |
7 | 8 | ||
@@ -16,5 +17,6 @@ swayc_t *workspace_output_next(); | |||
16 | swayc_t *workspace_next(); | 17 | swayc_t *workspace_next(); |
17 | swayc_t *workspace_output_prev(); | 18 | swayc_t *workspace_output_prev(); |
18 | swayc_t *workspace_prev(); | 19 | swayc_t *workspace_prev(); |
20 | swayc_t *workspace_for_pid(pid_t pid); | ||
19 | 21 | ||
20 | #endif | 22 | #endif |
diff --git a/sway/commands.c b/sway/commands.c index 07e7e53d..3a6b2af5 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -549,12 +549,19 @@ static struct cmd_results *cmd_exec_always(int argc, char **argv) { | |||
549 | close(fd[0]); | 549 | close(fd[0]); |
550 | // cleanup child process | 550 | // cleanup child process |
551 | wait(0); | 551 | wait(0); |
552 | if (*child > 0) { | 552 | swayc_t *ws = swayc_active_workspace(); |
553 | sway_log(L_DEBUG, "Child process created with pid %d", *child); | 553 | if (*child > 0 && ws) { |
554 | sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name); | ||
555 | struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); | ||
556 | pw->pid = child; | ||
557 | pw->workspace = strdup(ws->name); | ||
558 | list_add(config->pid_workspaces, pw); | ||
554 | // TODO: keep track of this pid and open the corresponding view on the current workspace | 559 | // TODO: keep track of this pid and open the corresponding view on the current workspace |
555 | // blocked pending feature in wlc | 560 | // blocked pending feature in wlc |
561 | } else { | ||
562 | free(child); | ||
556 | } | 563 | } |
557 | free(child); | 564 | |
558 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | 565 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); |
559 | } | 566 | } |
560 | 567 | ||
diff --git a/sway/config.c b/sway/config.c index 7530e530..07b1f2f7 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -89,6 +89,15 @@ static void free_workspace_output(struct workspace_output *wo) { | |||
89 | free(wo); | 89 | free(wo); |
90 | } | 90 | } |
91 | 91 | ||
92 | void free_pid_workspace(struct pid_workspace *pw) { | ||
93 | if (!pw) { | ||
94 | return; | ||
95 | } | ||
96 | free(pw->pid); | ||
97 | free(pw->workspace); | ||
98 | free(pw); | ||
99 | } | ||
100 | |||
92 | void free_config(struct sway_config *config) { | 101 | void free_config(struct sway_config *config) { |
93 | int i; | 102 | int i; |
94 | for (i = 0; i < config->symbols->length; ++i) { | 103 | for (i = 0; i < config->symbols->length; ++i) { |
@@ -113,6 +122,11 @@ void free_config(struct sway_config *config) { | |||
113 | } | 122 | } |
114 | list_free(config->workspace_outputs); | 123 | list_free(config->workspace_outputs); |
115 | 124 | ||
125 | for (i = 0; i < config->pid_workspaces->length; ++i) { | ||
126 | free_pid_workspace(config->pid_workspaces->items[i]); | ||
127 | } | ||
128 | list_free(config->pid_workspaces); | ||
129 | |||
116 | for (i = 0; i < config->criteria->length; ++i) { | 130 | for (i = 0; i < config->criteria->length; ++i) { |
117 | free_criteria(config->criteria->items[i]); | 131 | free_criteria(config->criteria->items[i]); |
118 | } | 132 | } |
@@ -148,6 +162,7 @@ static void config_defaults(struct sway_config *config) { | |||
148 | config->modes = create_list(); | 162 | config->modes = create_list(); |
149 | config->bars = create_list(); | 163 | config->bars = create_list(); |
150 | config->workspace_outputs = create_list(); | 164 | config->workspace_outputs = create_list(); |
165 | config->pid_workspaces = create_list(); | ||
151 | config->criteria = create_list(); | 166 | config->criteria = create_list(); |
152 | config->input_configs = create_list(); | 167 | config->input_configs = create_list(); |
153 | config->output_configs = create_list(); | 168 | config->output_configs = create_list(); |
diff --git a/sway/handlers.c b/sway/handlers.c index f8dd9f4d..b38f05a6 100644 --- a/sway/handlers.c +++ b/sway/handlers.c | |||
@@ -180,11 +180,32 @@ static bool handle_view_created(wlc_handle handle) { | |||
180 | wlc_handle parent = wlc_view_get_parent(handle); | 180 | wlc_handle parent = wlc_view_get_parent(handle); |
181 | swayc_t *focused = NULL; | 181 | swayc_t *focused = NULL; |
182 | swayc_t *newview = NULL; | 182 | swayc_t *newview = NULL; |
183 | swayc_t *current_ws = swayc_active_workspace(); | ||
184 | bool return_to_workspace = false; | ||
185 | struct wl_client *client = wlc_view_get_wl_client(handle); | ||
186 | pid_t pid; | ||
183 | 187 | ||
184 | // Get parent container, to add view in | 188 | // Get parent container, to add view in |
185 | if (parent) { | 189 | if (parent) { |
186 | focused = swayc_by_handle(parent); | 190 | focused = swayc_by_handle(parent); |
187 | } | 191 | } |
192 | |||
193 | if (client) { | ||
194 | // below only works on wayland windows. need a wlc | ||
195 | // api that will work for both wayland and x. | ||
196 | wl_client_get_credentials(client, &pid, NULL, NULL); | ||
197 | |||
198 | if (pid) { | ||
199 | // using newview as a temp storage location here, | ||
200 | // rather than adding yet another workspace var | ||
201 | if ((newview = workspace_for_pid(pid))) { | ||
202 | focused = newview; | ||
203 | newview = NULL; | ||
204 | return_to_workspace = true; | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | |||
188 | if (!focused || focused->type == C_OUTPUT) { | 209 | if (!focused || focused->type == C_OUTPUT) { |
189 | focused = get_focused_container(&root_container); | 210 | focused = get_focused_container(&root_container); |
190 | // Move focus from floating view | 211 | // Move focus from floating view |
@@ -220,7 +241,7 @@ static bool handle_view_created(wlc_handle handle) { | |||
220 | // Dmenu keeps viewfocus, but others with this flag don't, for now simulate | 241 | // Dmenu keeps viewfocus, but others with this flag don't, for now simulate |
221 | // dmenu | 242 | // dmenu |
222 | case WLC_BIT_OVERRIDE_REDIRECT: | 243 | case WLC_BIT_OVERRIDE_REDIRECT: |
223 | // locked_view_focus = true; | 244 | // locked_view_focus = true; |
224 | wlc_view_focus(handle); | 245 | wlc_view_focus(handle); |
225 | wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); | 246 | wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); |
226 | wlc_view_bring_to_front(handle); | 247 | wlc_view_bring_to_front(handle); |
@@ -273,6 +294,13 @@ static bool handle_view_created(wlc_handle handle) { | |||
273 | list_add(output->unmanaged, h); | 294 | list_add(output->unmanaged, h); |
274 | } | 295 | } |
275 | wlc_view_set_mask(handle, VISIBLE); | 296 | wlc_view_set_mask(handle, VISIBLE); |
297 | |||
298 | if (return_to_workspace && current_ws) { | ||
299 | // we were on one workspace, switched to another to add this view, | ||
300 | // now let's return to where we were | ||
301 | workspace_switch(current_ws); | ||
302 | set_focused_container(current_ws->focused); | ||
303 | } | ||
276 | return true; | 304 | return true; |
277 | } | 305 | } |
278 | 306 | ||
diff --git a/sway/workspace.c b/sway/workspace.c index 82573d2e..0c5c70a3 100644 --- a/sway/workspace.c +++ b/sway/workspace.c | |||
@@ -5,6 +5,7 @@ | |||
5 | #include <wlc/wlc.h> | 5 | #include <wlc/wlc.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <strings.h> | 7 | #include <strings.h> |
8 | #include <sys/types.h> | ||
8 | #include "ipc-server.h" | 9 | #include "ipc-server.h" |
9 | #include "workspace.h" | 10 | #include "workspace.h" |
10 | #include "layout.h" | 11 | #include "layout.h" |
@@ -309,3 +310,55 @@ bool workspace_switch(swayc_t *workspace) { | |||
309 | arrange_windows(output, -1, -1); | 310 | arrange_windows(output, -1, -1); |
310 | return true; | 311 | return true; |
311 | } | 312 | } |
313 | |||
314 | swayc_t *workspace_for_pid(pid_t pid) { | ||
315 | int i; | ||
316 | swayc_t *ws = NULL; | ||
317 | struct pid_workspace *pw = NULL; | ||
318 | |||
319 | sway_log(L_DEBUG, "looking for workspace for pid %d", pid); | ||
320 | |||
321 | // leaving this here as it's useful for debugging | ||
322 | // sway_log(L_DEBUG, "all pid_workspaces"); | ||
323 | // for (int k = 0; k < config->pid_workspaces->length; k++) { | ||
324 | // pw = config->pid_workspaces->items[k]; | ||
325 | // sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); | ||
326 | // } | ||
327 | |||
328 | do { | ||
329 | for (i = 0; i < config->pid_workspaces->length; i++) { | ||
330 | pw = config->pid_workspaces->items[i]; | ||
331 | pid_t *pw_pid = pw->pid; | ||
332 | |||
333 | if (pid == *pw_pid) { | ||
334 | sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace); | ||
335 | break; // out of for loop | ||
336 | } | ||
337 | |||
338 | pw = NULL; | ||
339 | } | ||
340 | |||
341 | if (pw) { | ||
342 | break; // out of do-while loop | ||
343 | } | ||
344 | |||
345 | pid = get_parent_pid(pid); | ||
346 | // no sense in looking for matches for pid 0. | ||
347 | // also, if pid == getpid(), that is the compositor's | ||
348 | // pid, which definitely isn't helpful | ||
349 | } while (pid > 0 && pid != getpid()); | ||
350 | |||
351 | if (pw) { | ||
352 | ws = workspace_by_name(pw->workspace); | ||
353 | |||
354 | if (!ws) { | ||
355 | sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); | ||
356 | ws = workspace_create(pw->workspace); | ||
357 | } | ||
358 | |||
359 | list_del(config->pid_workspaces, i); | ||
360 | } | ||
361 | |||
362 | free_pid_workspace(pw); | ||
363 | return ws; | ||
364 | } | ||