From 0f1859ed25741927117b31cdd3ef2560f0327688 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Mon, 6 Jun 2016 06:58:53 -0500 Subject: [PATCH 1/4] messy, unfinished version --- include/config.h | 8 +++++++ sway/commands.c | 13 ++++++++--- sway/config.c | 15 ++++++++++++ sway/handlers.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 4 deletions(-) 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 { char *workspace; }; +struct pid_workspace { + pid_t *pid; + char *workspace; +}; + +void free_pid_workspace(struct pid_workspace *pw); + struct bar_config { /** * One of "dock", "hide", "invisible" @@ -175,6 +182,7 @@ struct sway_config { list_t *bars; list_t *cmd_queue; list_t *workspace_outputs; + list_t *pid_workspaces; list_t *output_configs; list_t *input_configs; list_t *criteria; diff --git a/sway/commands.c b/sway/commands.c index 83a9e7e9..08920c1c 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -549,12 +549,19 @@ static struct cmd_results *cmd_exec_always(int argc, char **argv) { close(fd[0]); // cleanup child process wait(0); - if (*child > 0) { - sway_log(L_DEBUG, "Child process created with pid %d", *child); + swayc_t *ws = swayc_active_workspace(); + if (*child > 0 && ws) { + sway_log(L_DEBUG, "Child process created with pid %d for workspace %s", *child, ws->name); + struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); + pw->pid = child; + pw->workspace = strdup(ws->name); + list_add(config->pid_workspaces, pw); // TODO: keep track of this pid and open the corresponding view on the current workspace // blocked pending feature in wlc + } else { + free(child); } - free(child); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/config.c b/sway/config.c index 15108123..321534ed 100644 --- a/sway/config.c +++ b/sway/config.c @@ -89,6 +89,15 @@ static void free_workspace_output(struct workspace_output *wo) { free(wo); } +void free_pid_workspace(struct pid_workspace *pw) { + if (!pw) { + return; + } + free(pw->pid); + free(pw->workspace); + free(pw); +} + void free_config(struct sway_config *config) { int i; for (i = 0; i < config->symbols->length; ++i) { @@ -113,6 +122,11 @@ void free_config(struct sway_config *config) { } list_free(config->workspace_outputs); + for (i = 0; i < config->pid_workspaces->length; ++i) { + free_pid_workspace(config->pid_workspaces->items[i]); + } + list_free(config->pid_workspaces); + for (i = 0; i < config->criteria->length; ++i) { free_criteria(config->criteria->items[i]); } @@ -148,6 +162,7 @@ static void config_defaults(struct sway_config *config) { config->modes = create_list(); config->bars = create_list(); config->workspace_outputs = create_list(); + config->pid_workspaces = create_list(); config->criteria = create_list(); config->input_configs = create_list(); config->output_configs = create_list(); diff --git a/sway/handlers.c b/sway/handlers.c index f8dd9f4d..317b03a8 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -185,6 +185,63 @@ static bool handle_view_created(wlc_handle handle) { if (parent) { focused = swayc_by_handle(parent); } + + // TODO: test with wayland apps (gnome terminal or corebird) + + // try to match this up to a pid_workspace + struct wl_client *client = wlc_view_get_wl_client(handle); + pid_t pid; + struct pid_workspace *pw = NULL; + + sway_log(L_DEBUG, "checking pid workspaces, handle is %lu", handle); + + if (client) { + sway_log(L_DEBUG, "found client"); + wl_client_get_credentials(client, &pid, NULL, NULL); + } + + sway_log(L_DEBUG, "all pid_workspaces"); + for (int k = 0; k < config->pid_workspaces->length; k++) { + pw = config->pid_workspaces->items[k]; + sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); + } + + if (pid) { + sway_log(L_DEBUG, "found pid %d for client", pid); + int i; + for (i = 0; i < config->pid_workspaces->length; i++) { + pw = config->pid_workspaces->items[i]; + pid_t *pw_pid = pw->pid; + sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i); + if (pid == *pw_pid) { + sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace); + break; + } + pw = NULL; + } + + swayc_t *ws = NULL; + + if (pw) { + ws = workspace_by_name(pw->workspace); + + if (!ws) { + sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); + ws = workspace_create(pw->workspace); + } + + if (ws) { + sway_log(L_DEBUG, "workspace exists, name is %s", ws->name); + focused = ws; + } + + list_del(config->pid_workspaces, i); + } + } + + free_pid_workspace(pw); + // free(&pid); + if (!focused || focused->type == C_OUTPUT) { focused = get_focused_container(&root_container); // Move focus from floating view @@ -220,7 +277,7 @@ static bool handle_view_created(wlc_handle handle) { // Dmenu keeps viewfocus, but others with this flag don't, for now simulate // dmenu case WLC_BIT_OVERRIDE_REDIRECT: -// locked_view_focus = true; + // locked_view_focus = true; wlc_view_focus(handle); wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true); wlc_view_bring_to_front(handle); From 03d79b41c71f091f61f4712963a3760fd24fdb62 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Fri, 10 Jun 2016 06:08:59 -0500 Subject: [PATCH 2/4] semi-working (only non-client/server wayland apps) --- common/util.c | 39 ++++++++++++++++++++++++++++++++++++++- include/util.h | 8 ++++++++ sway/handlers.c | 43 +++++++++++++++++++++++++++++++++---------- 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 @@ #include - +#include +#include +#include +#include "readline.h" #include "util.h" +#include "log.h" int wrap(int i, int max) { return ((i % max) + max) % max; @@ -64,3 +68,36 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { return length; } + +pid_t get_parent_pid(pid_t child) { + pid_t parent; + char file_name[100]; + char *buffer = NULL; + char *token = NULL; + const char sep[2] = " "; + FILE *stat = NULL; + + sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child); + + sprintf(file_name, "/proc/%d/stat", child); + + if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { + return -1; + } + + fclose(stat); + + sway_log(L_DEBUG, "buffer string is %s", buffer); + + token = strtok(buffer, sep); + + for (int i = 0; i < 3; i++) { + token = strtok(NULL, sep); + } + + parent = strtol(token, NULL, 10); + + sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); + + return (parent == child) ? -1 : parent; +} 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 @@ #define _SWAY_UTIL_H #include +#include #include #include @@ -36,4 +37,11 @@ const char *get_modifier_name_by_mask(uint32_t modifier); */ int get_modifier_names(const char **names, uint32_t modifier_masks); +/** + * 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. + */ +pid_t get_parent_pid(pid_t pid); + #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 @@ #include #include #include +#include +#include "util.h" #include "handlers.h" #include "border.h" #include "log.h" @@ -180,6 +182,12 @@ static bool handle_view_created(wlc_handle handle) { wlc_handle parent = wlc_view_get_parent(handle); swayc_t *focused = NULL; swayc_t *newview = NULL; + swayc_t *current_ws = swayc_active_workspace(); + bool return_to_workspace = false; + + if (current_ws) { + sway_log(L_DEBUG, "current workspace is %s", current_ws->name); + } // Get parent container, to add view in if (parent) { @@ -209,16 +217,22 @@ static bool handle_view_created(wlc_handle handle) { if (pid) { sway_log(L_DEBUG, "found pid %d for client", pid); int i; - for (i = 0; i < config->pid_workspaces->length; i++) { - pw = config->pid_workspaces->items[i]; - pid_t *pw_pid = pw->pid; - sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i); - if (pid == *pw_pid) { - sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace); - break; + do { + for (i = 0; i < config->pid_workspaces->length; i++) { + pw = config->pid_workspaces->items[i]; + pid_t *pw_pid = pw->pid; + sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i); + if (pid == *pw_pid) { + sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace); + break; // out of for loop + } + pw = NULL; } - pw = NULL; - } + if (pw) { + break; // out of do-while loop + } + pid = get_parent_pid(pid); + } while (pid > -1 && pid != getpid()); swayc_t *ws = NULL; @@ -233,6 +247,10 @@ static bool handle_view_created(wlc_handle handle) { if (ws) { sway_log(L_DEBUG, "workspace exists, name is %s", ws->name); focused = ws; + + if (current_ws && (strcmp(current_ws->name, ws->name) != 0)) { + return_to_workspace = true; + } } list_del(config->pid_workspaces, i); @@ -240,7 +258,6 @@ static bool handle_view_created(wlc_handle handle) { } free_pid_workspace(pw); - // free(&pid); if (!focused || focused->type == C_OUTPUT) { focused = get_focused_container(&root_container); @@ -330,6 +347,12 @@ static bool handle_view_created(wlc_handle handle) { list_add(output->unmanaged, h); } wlc_view_set_mask(handle, VISIBLE); + + if (return_to_workspace && current_ws) { + sway_log(L_DEBUG, "return_to_workspace && current_ws"); + workspace_switch(current_ws); + set_focused_container(current_ws->focused); + } return true; } From beaa03344eda931274b75275bfc2d622e6875956 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Sat, 11 Jun 2016 09:20:09 -0500 Subject: [PATCH 3/4] clean up pid/workspace stuff --- include/workspace.h | 2 ++ sway/handlers.c | 78 ++++++++------------------------------------- sway/workspace.c | 53 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 65 deletions(-) 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 @@ #define _SWAY_WORKSPACE_H #include +#include #include "list.h" #include "layout.h" @@ -16,5 +17,6 @@ swayc_t *workspace_output_next(); swayc_t *workspace_next(); swayc_t *workspace_output_prev(); swayc_t *workspace_prev(); +swayc_t *workspace_for_pid(pid_t pid); #endif diff --git a/sway/handlers.c b/sway/handlers.c index 9efd3ffc..b38f05a6 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -7,9 +7,7 @@ #include #include #include -#include -#include "util.h" #include "handlers.h" #include "border.h" #include "log.h" @@ -184,81 +182,30 @@ static bool handle_view_created(wlc_handle handle) { swayc_t *newview = NULL; swayc_t *current_ws = swayc_active_workspace(); bool return_to_workspace = false; - - if (current_ws) { - sway_log(L_DEBUG, "current workspace is %s", current_ws->name); - } + struct wl_client *client = wlc_view_get_wl_client(handle); + pid_t pid; // Get parent container, to add view in if (parent) { focused = swayc_by_handle(parent); } - // TODO: test with wayland apps (gnome terminal or corebird) - - // try to match this up to a pid_workspace - struct wl_client *client = wlc_view_get_wl_client(handle); - pid_t pid; - struct pid_workspace *pw = NULL; - - sway_log(L_DEBUG, "checking pid workspaces, handle is %lu", handle); - if (client) { - sway_log(L_DEBUG, "found client"); + // below only works on wayland windows. need a wlc + // api that will work for both wayland and x. wl_client_get_credentials(client, &pid, NULL, NULL); - } - sway_log(L_DEBUG, "all pid_workspaces"); - for (int k = 0; k < config->pid_workspaces->length; k++) { - pw = config->pid_workspaces->items[k]; - sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); - } - - if (pid) { - sway_log(L_DEBUG, "found pid %d for client", pid); - int i; - do { - for (i = 0; i < config->pid_workspaces->length; i++) { - pw = config->pid_workspaces->items[i]; - pid_t *pw_pid = pw->pid; - sway_log(L_DEBUG, "checking pid %d against pid %d, i is %d", pid, *pw_pid, i); - if (pid == *pw_pid) { - sway_log(L_DEBUG, "found pid_workspace for pid, %d %s", pid, pw->workspace); - break; // out of for loop - } - pw = NULL; + if (pid) { + // using newview as a temp storage location here, + // rather than adding yet another workspace var + if ((newview = workspace_for_pid(pid))) { + focused = newview; + newview = NULL; + return_to_workspace = true; } - if (pw) { - break; // out of do-while loop - } - pid = get_parent_pid(pid); - } while (pid > -1 && pid != getpid()); - - swayc_t *ws = NULL; - - if (pw) { - ws = workspace_by_name(pw->workspace); - - if (!ws) { - sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); - ws = workspace_create(pw->workspace); - } - - if (ws) { - sway_log(L_DEBUG, "workspace exists, name is %s", ws->name); - focused = ws; - - if (current_ws && (strcmp(current_ws->name, ws->name) != 0)) { - return_to_workspace = true; - } - } - - list_del(config->pid_workspaces, i); } } - free_pid_workspace(pw); - if (!focused || focused->type == C_OUTPUT) { focused = get_focused_container(&root_container); // Move focus from floating view @@ -349,7 +296,8 @@ static bool handle_view_created(wlc_handle handle) { wlc_view_set_mask(handle, VISIBLE); if (return_to_workspace && current_ws) { - sway_log(L_DEBUG, "return_to_workspace && current_ws"); + // we were on one workspace, switched to another to add this view, + // now let's return to where we were workspace_switch(current_ws); set_focused_container(current_ws->focused); } 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 @@ #include #include #include +#include #include "ipc-server.h" #include "workspace.h" #include "layout.h" @@ -309,3 +310,55 @@ bool workspace_switch(swayc_t *workspace) { arrange_windows(output, -1, -1); return true; } + +swayc_t *workspace_for_pid(pid_t pid) { + int i; + swayc_t *ws = NULL; + struct pid_workspace *pw = NULL; + + sway_log(L_DEBUG, "looking for workspace for pid %d", pid); + + // leaving this here as it's useful for debugging + // sway_log(L_DEBUG, "all pid_workspaces"); + // for (int k = 0; k < config->pid_workspaces->length; k++) { + // pw = config->pid_workspaces->items[k]; + // sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); + // } + + do { + for (i = 0; i < config->pid_workspaces->length; i++) { + pw = config->pid_workspaces->items[i]; + pid_t *pw_pid = pw->pid; + + if (pid == *pw_pid) { + sway_log(L_DEBUG, "found pid_workspace for pid %d, workspace %s", pid, pw->workspace); + break; // out of for loop + } + + pw = NULL; + } + + if (pw) { + break; // out of do-while loop + } + + pid = get_parent_pid(pid); + // no sense in looking for matches for pid 0. + // also, if pid == getpid(), that is the compositor's + // pid, which definitely isn't helpful + } while (pid > 0 && pid != getpid()); + + if (pw) { + ws = workspace_by_name(pw->workspace); + + if (!ws) { + sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); + ws = workspace_create(pw->workspace); + } + + list_del(config->pid_workspaces, i); + } + + free_pid_workspace(pw); + return ws; +} From 2298143d09ce8810d9772f95e1cb605fb6b08536 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Sat, 11 Jun 2016 12:43:34 -0500 Subject: [PATCH 4/4] cleanup + add timeouts for pid_workspace list --- common/util.c | 32 ++++++++++++-------------------- include/config.h | 5 +++++ sway/commands.c | 2 +- sway/config.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ sway/workspace.c | 5 ++--- 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/common/util.c b/common/util.c index e760443a..12cb7470 100644 --- a/common/util.c +++ b/common/util.c @@ -74,30 +74,22 @@ pid_t get_parent_pid(pid_t child) { char file_name[100]; char *buffer = NULL; char *token = NULL; - const char sep[2] = " "; + const char *sep = " "; FILE *stat = NULL; - sway_log(L_DEBUG, "trying to get parent pid for child pid %d", child); - sprintf(file_name, "/proc/%d/stat", child); - if (!(stat = fopen(file_name, "r")) || !(buffer = read_line(stat))) { - return -1; + if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { + fclose(stat); + + 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); + return (parent == child) ? -1 : parent; } - fclose(stat); - - sway_log(L_DEBUG, "buffer string is %s", buffer); - - token = strtok(buffer, sep); - - for (int i = 0; i < 3; i++) { - token = strtok(NULL, sep); - } - - parent = strtol(token, NULL, 10); - - sway_log(L_DEBUG, "found parent pid %d for child pid %d", parent, child); - - return (parent == child) ? -1 : parent; + return -1; } diff --git a/include/config.h b/include/config.h index 35797ac2..bf278ddb 100644 --- a/include/config.h +++ b/include/config.h @@ -1,11 +1,14 @@ #ifndef _SWAY_CONFIG_H #define _SWAY_CONFIG_H +#define PID_WORKSPACE_TIMEOUT 60 + #include #include #include #include #include +#include #include "wayland-desktop-shell-server-protocol.h" #include "list.h" #include "layout.h" @@ -95,8 +98,10 @@ struct workspace_output { struct pid_workspace { pid_t *pid; char *workspace; + time_t *time_added; }; +void pid_workspace_add(struct pid_workspace *pw); void free_pid_workspace(struct pid_workspace *pw); struct bar_config { diff --git a/sway/commands.c b/sway/commands.c index 3a6b2af5..5e84ea9a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -555,7 +555,7 @@ static struct cmd_results *cmd_exec_always(int argc, char **argv) { struct pid_workspace *pw = malloc(sizeof(struct pid_workspace)); pw->pid = child; pw->workspace = strdup(ws->name); - list_add(config->pid_workspaces, pw); + pid_workspace_add(pw); // TODO: keep track of this pid and open the corresponding view on the current workspace // blocked pending feature in wlc } else { diff --git a/sway/config.c b/sway/config.c index 07b1f2f7..819a70ce 100644 --- a/sway/config.c +++ b/sway/config.c @@ -89,12 +89,57 @@ static void free_workspace_output(struct workspace_output *wo) { free(wo); } +static void pid_workspace_cleanup() { + struct timespec ts; + struct pid_workspace *pw = NULL; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + // work backwards through list and remove any entries + // older than PID_WORKSPACE_TIMEOUT + for (int i = config->pid_workspaces->length - 1; i > -1; i--) { + pw = config->pid_workspaces->items[i]; + + if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { + list_del(config->pid_workspaces, i); + } + } +} + +// de-dupe pid_workspaces to ensure pid uniqueness +void pid_workspace_add(struct pid_workspace *pw) { + struct pid_workspace *list_pw = NULL; + struct timespec ts; + time_t *now = malloc(sizeof(time_t)); + + pid_workspace_cleanup(); + + // add current time to pw + clock_gettime(CLOCK_MONOTONIC, &ts); + *now = ts.tv_sec; + + pw->time_added = now; + + // work backwards through list and delete any entries that + // have the same pid as that in our new pid_workspace + for (int i = config->pid_workspaces->length - 1; i > -1; i--) { + list_pw = config->pid_workspaces->items[i]; + + if (pw->pid == list_pw->pid) { + list_del(config->pid_workspaces, i); + } + } + + list_add(config->pid_workspaces, pw); +} + void free_pid_workspace(struct pid_workspace *pw) { if (!pw) { return; } free(pw->pid); free(pw->workspace); + free(pw->time_added); free(pw); } diff --git a/sway/workspace.c b/sway/workspace.c index 0c5c70a3..5319aec4 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -322,7 +322,7 @@ swayc_t *workspace_for_pid(pid_t pid) { // sway_log(L_DEBUG, "all pid_workspaces"); // for (int k = 0; k < config->pid_workspaces->length; k++) { // pw = config->pid_workspaces->items[k]; - // sway_log(L_DEBUG, "pid %d workspace %s", *pw->pid, pw->workspace); + // sway_log(L_DEBUG, "pid %d workspace %s time_added %li", *pw->pid, pw->workspace, *pw->time_added); // } do { @@ -352,13 +352,12 @@ swayc_t *workspace_for_pid(pid_t pid) { ws = workspace_by_name(pw->workspace); if (!ws) { - sway_log(L_DEBUG, "creating workspace %s because it disappeared", pw->workspace); + sway_log(L_DEBUG, "Creating workspace %s for pid %d because it disappeared", pw->workspace, pid); ws = workspace_create(pw->workspace); } list_del(config->pid_workspaces, i); } - free_pid_workspace(pw); return ws; }