diff options
author | M Stoeckl <[email protected]> | 2019-01-21 12:39:16 -0500 |
---|---|---|
committer | M Stoeckl <[email protected]> | 2019-01-21 12:39:16 -0500 |
commit | d7ff776552bef524e905d85c2a5e7651c8408658 (patch) | |
tree | ae20feac64f93f776e9c9e136c62459705e97987 /sway/tree/root.c | |
parent | 410c961388bbfecb5f1b63e4a1977a78709a6e57 (diff) |
Move sway-specific functions in common/util.c into sway/
Modifier handling functions were moved into sway/input/keyboard.c;
opposite_direction for enum wlr_direction into sway/tree/output.c;
and get_parent_pid into sway/tree/root.c .
Diffstat (limited to 'sway/tree/root.c')
-rw-r--r-- | sway/tree/root.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/sway/tree/root.c b/sway/tree/root.c index 08ce7942..d4b97be3 100644 --- a/sway/tree/root.c +++ b/sway/tree/root.c @@ -169,6 +169,41 @@ struct pid_workspace { static struct wl_list pid_workspaces; +/** + * Get the pid of a parent process given the pid of a child process. + * + * Returns the parent pid or NULL if the parent pid cannot be determined. + */ +static pid_t get_parent_pid(pid_t child) { + pid_t parent = -1; + char file_name[100]; + char *buffer = NULL; + char *token = NULL; + const char *sep = " "; + FILE *stat = NULL; + size_t buf_size = 0; + + sprintf(file_name, "/proc/%d/stat", child); + + if ((stat = fopen(file_name, "r"))) { + if (getline(&buffer, &buf_size, stat) != -1) { + token = strtok(buffer, sep); // pid + token = strtok(NULL, sep); // executable name + token = strtok(NULL, sep); // state + token = strtok(NULL, sep); // parent pid + parent = strtol(token, NULL, 10); + } + free(buffer); + fclose(stat); + } + + if (parent) { + return (parent == child) ? -1 : parent; + } + + return -1; +} + struct sway_workspace *root_workspace_for_pid(pid_t pid) { if (!pid_workspaces.prev && !pid_workspaces.next) { wl_list_init(&pid_workspaces); |