aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/util.c39
-rw-r--r--include/util.h8
-rw-r--r--sway/handlers.c43
3 files changed, 79 insertions, 11 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
5int wrap(int i, int max) { 9int 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
72pid_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/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 */
37int get_modifier_names(const char **names, uint32_t modifier_masks); 38int 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 */
45pid_t get_parent_pid(pid_t pid);
46
39#endif 47#endif
diff --git a/sway/handlers.c b/sway/handlers.c
index 317b03a8..9efd3ffc 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -7,7 +7,9 @@
7#include <wlc/wlc-render.h> 7#include <wlc/wlc-render.h>
8#include <wlc/wlc-wayland.h> 8#include <wlc/wlc-wayland.h>
9#include <ctype.h> 9#include <ctype.h>
10#include <unistd.h>
10 11
12#include "util.h"
11#include "handlers.h" 13#include "handlers.h"
12#include "border.h" 14#include "border.h"
13#include "log.h" 15#include "log.h"
@@ -180,6 +182,12 @@ static bool handle_view_created(wlc_handle handle) {
180 wlc_handle parent = wlc_view_get_parent(handle); 182 wlc_handle parent = wlc_view_get_parent(handle);
181 swayc_t *focused = NULL; 183 swayc_t *focused = NULL;
182 swayc_t *newview = NULL; 184 swayc_t *newview = NULL;
185 swayc_t *current_ws = swayc_active_workspace();
186 bool return_to_workspace = false;
187
188 if (current_ws) {
189 sway_log(L_DEBUG, "current workspace is %s", current_ws->name);
190 }
183 191
184 // Get parent container, to add view in 192 // Get parent container, to add view in
185 if (parent) { 193 if (parent) {
@@ -209,16 +217,22 @@ static bool handle_view_created(wlc_handle handle) {
209 if (pid) { 217 if (pid) {
210 sway_log(L_DEBUG, "found pid %d for client", pid); 218 sway_log(L_DEBUG, "found pid %d for client", pid);
211 int i; 219 int i;
212 for (i = 0; i < config->pid_workspaces->length; i++) { 220 do {
213 pw = config->pid_workspaces->items[i]; 221 for (i = 0; i < config->pid_workspaces->length; i++) {
214 pid_t *pw_pid = pw->pid; 222 pw = config->pid_workspaces->items[i];
215 sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i); 223 pid_t *pw_pid = pw->pid;
216 if (pid == *pw_pid) { 224 sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i);
217 sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace); 225 if (pid == *pw_pid) {
218 break; 226 sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace);
227 break; // out of for loop
228 }
229 pw = NULL;
219 } 230 }
220 pw = NULL; 231 if (pw) {
221 } 232 break; // out of do-while loop
233 }
234 pid = get_parent_pid(pid);
235 } while (pid > -1 && pid != getpid());
222 236
223 swayc_t *ws = NULL; 237 swayc_t *ws = NULL;
224 238
@@ -233,6 +247,10 @@ static bool handle_view_created(wlc_handle handle) {
233 if (ws) { 247 if (ws) {
234 sway_log(L_DEBUG, "workspace exists, name is %s", ws->name); 248 sway_log(L_DEBUG, "workspace exists, name is %s", ws->name);
235 focused = ws; 249 focused = ws;
250
251 if (current_ws && (strcmp(current_ws->name, ws->name) != 0)) {
252 return_to_workspace = true;
253 }
236 } 254 }
237 255
238 list_del(config->pid_workspaces, i); 256 list_del(config->pid_workspaces, i);
@@ -240,7 +258,6 @@ static bool handle_view_created(wlc_handle handle) {
240 } 258 }
241 259
242 free_pid_workspace(pw); 260 free_pid_workspace(pw);
243 // free(&pid);
244 261
245 if (!focused || focused->type == C_OUTPUT) { 262 if (!focused || focused->type == C_OUTPUT) {
246 focused = get_focused_container(&root_container); 263 focused = get_focused_container(&root_container);
@@ -330,6 +347,12 @@ static bool handle_view_created(wlc_handle handle) {
330 list_add(output->unmanaged, h); 347 list_add(output->unmanaged, h);
331 } 348 }
332 wlc_view_set_mask(handle, VISIBLE); 349 wlc_view_set_mask(handle, VISIBLE);
350
351 if (return_to_workspace && current_ws) {
352 sway_log(L_DEBUG, "return_to_workspace && current_ws");
353 workspace_switch(current_ws);
354 set_focused_container(current_ws->focused);
355 }
333 return true; 356 return true;
334} 357}
335 358