From 04489ff4209dc073027419d90961367cfb998fe8 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 3 Aug 2018 23:06:01 +1000 Subject: Separate root-related code This creates a root.c and moves bits and pieces from elsewhere into it. * layout_init has been renamed to root_create and moved into root.c * root_destroy has been created and is called on shutdown * scratchpad code has been moved into root.c, because hidden scratchpad containers are stored in the root struct --- include/sway/scratchpad.h | 26 --------------------- include/sway/tree/layout.h | 23 +------------------ include/sway/tree/root.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 48 deletions(-) delete mode 100644 include/sway/scratchpad.h create mode 100644 include/sway/tree/root.h (limited to 'include') diff --git a/include/sway/scratchpad.h b/include/sway/scratchpad.h deleted file mode 100644 index 5af5256f..00000000 --- a/include/sway/scratchpad.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _SWAY_SCRATCHPAD_H -#define _SWAY_SCRATCHPAD_H - -#include "tree/container.h" - -/** - * Move a container to the scratchpad. - */ -void scratchpad_add_container(struct sway_container *con); - -/** - * Remove a container from the scratchpad. - */ -void scratchpad_remove_container(struct sway_container *con); - -/** - * Show or hide the next container on the scratchpad. - */ -void scratchpad_toggle_auto(void); - -/** - * Show or hide a specific container on the scratchpad. - */ -void scratchpad_toggle_container(struct sway_container *con); - -#endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index a4c31bf6..77cd954b 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -3,6 +3,7 @@ #include #include #include "sway/tree/container.h" +#include "sway/tree/root.h" #include "config.h" enum movement_direction { @@ -24,28 +25,6 @@ enum resize_edge { struct sway_container; -struct sway_root { - struct wlr_output_layout *output_layout; - - struct wl_listener output_layout_change; -#ifdef HAVE_XWAYLAND - struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link -#endif - struct wl_list drag_icons; // sway_drag_icon::link - - struct wlr_texture *debug_tree; - - struct wl_list outputs; // sway_output::link - - list_t *scratchpad; // struct sway_container - - struct { - struct wl_signal new_container; - } events; -}; - -void layout_init(void); - void container_add_child(struct sway_container *parent, struct sway_container *child); diff --git a/include/sway/tree/root.h b/include/sway/tree/root.h new file mode 100644 index 00000000..ada3c73f --- /dev/null +++ b/include/sway/tree/root.h @@ -0,0 +1,57 @@ +#ifndef _SWAY_ROOT_H +#define _SWAY_ROOT_H +#include +#include +#include +#include +#include "sway/tree/container.h" +#include "config.h" +#include "list.h" + +extern struct sway_container root_container; + +struct sway_root { + struct wlr_output_layout *output_layout; + + struct wl_listener output_layout_change; +#ifdef HAVE_XWAYLAND + struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link +#endif + struct wl_list drag_icons; // sway_drag_icon::link + + struct wlr_texture *debug_tree; + + struct wl_list outputs; // sway_output::link + + list_t *scratchpad; // struct sway_container + + struct { + struct wl_signal new_container; + } events; +}; + +void root_create(void); + +void root_destroy(void); + +/** + * Move a container to the scratchpad. + */ +void root_scratchpad_add_container(struct sway_container *con); + +/** + * Remove a container from the scratchpad. + */ +void root_scratchpad_remove_container(struct sway_container *con); + +/** + * Show a single scratchpad container. + */ +void root_scratchpad_show(struct sway_container *con); + +/** + * Hide a single scratchpad container. + */ +void root_scratchpad_hide(struct sway_container *con); + +#endif -- cgit v1.2.3 From 30e7e0f7c7d3d3ce2851f2e70842d735343fb402 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 4 Aug 2018 14:01:49 +1000 Subject: Move workspace pid code to root.c --- include/sway/tree/root.h | 4 ++ include/sway/tree/workspace.h | 4 -- sway/commands/exec_always.c | 2 +- sway/tree/root.c | 115 ++++++++++++++++++++++++++++++++++++++++++ sway/tree/view.c | 2 +- sway/tree/workspace.c | 113 ----------------------------------------- 6 files changed, 121 insertions(+), 119 deletions(-) (limited to 'include') diff --git a/include/sway/tree/root.h b/include/sway/tree/root.h index ada3c73f..edb7c817 100644 --- a/include/sway/tree/root.h +++ b/include/sway/tree/root.h @@ -54,4 +54,8 @@ void root_scratchpad_show(struct sway_container *con); */ void root_scratchpad_hide(struct sway_container *con); +struct sway_container *root_workspace_for_pid(pid_t pid); + +void root_record_workspace_pid(pid_t pid); + #endif diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h index 5ae0ae3a..3337f2c8 100644 --- a/include/sway/tree/workspace.h +++ b/include/sway/tree/workspace.h @@ -44,10 +44,6 @@ void workspace_output_add_priority(struct sway_container *workspace, struct sway_container *workspace_output_get_highest_available( struct sway_container *ws, struct sway_container *exclude); -struct sway_container *workspace_for_pid(pid_t pid); - -void workspace_record_pid(pid_t pid); - void workspace_detect_urgent(struct sway_container *workspace); #endif diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c index c730cb8b..00e39ae7 100644 --- a/sway/commands/exec_always.c +++ b/sway/commands/exec_always.c @@ -78,7 +78,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) { waitpid(pid, NULL, 0); if (child > 0) { wlr_log(WLR_DEBUG, "Child process created with pid %d", child); - workspace_record_pid(child); + root_record_workspace_pid(child); } else { return cmd_results_new(CMD_FAILURE, "exec_always", "Second fork() failed"); diff --git a/sway/tree/root.c b/sway/tree/root.c index 9f3965be..79f2194e 100644 --- a/sway/tree/root.c +++ b/sway/tree/root.c @@ -4,12 +4,14 @@ #include #include #include "sway/input/seat.h" +#include "sway/output.h" #include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/root.h" #include "sway/tree/workspace.h" #include "list.h" #include "log.h" +#include "util.h" struct sway_container root_container; @@ -145,3 +147,116 @@ void root_scratchpad_hide(struct sway_container *con) { } list_move_to_end(root_container.sway_root->scratchpad, con); } + +struct pid_workspace { + pid_t pid; + char *workspace; + struct timespec time_added; + + struct sway_container *output; + struct wl_listener output_destroy; + + struct wl_list link; +}; + +static struct wl_list pid_workspaces; + +struct sway_container *root_workspace_for_pid(pid_t pid) { + if (!pid_workspaces.prev && !pid_workspaces.next) { + wl_list_init(&pid_workspaces); + return NULL; + } + + struct sway_container *ws = NULL; + struct pid_workspace *pw = NULL; + + wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid); + + do { + struct pid_workspace *_pw = NULL; + wl_list_for_each(_pw, &pid_workspaces, link) { + if (pid == _pw->pid) { + pw = _pw; + wlr_log(WLR_DEBUG, + "found pid_workspace for pid %d, workspace %s", + pid, pw->workspace); + goto found; + } + } + pid = get_parent_pid(pid); + } while (pid > 1); +found: + + if (pw && pw->workspace) { + ws = workspace_by_name(pw->workspace); + + if (!ws) { + wlr_log(WLR_DEBUG, + "Creating workspace %s for pid %d because it disappeared", + pw->workspace, pid); + ws = workspace_create(pw->output, pw->workspace); + } + + wl_list_remove(&pw->output_destroy.link); + wl_list_remove(&pw->link); + free(pw->workspace); + free(pw); + } + + return ws; +} + +static void pw_handle_output_destroy(struct wl_listener *listener, void *data) { + struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy); + pw->output = NULL; + wl_list_remove(&pw->output_destroy.link); + wl_list_init(&pw->output_destroy.link); +} + +void root_record_workspace_pid(pid_t pid) { + wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid); + if (!pid_workspaces.prev && !pid_workspaces.next) { + wl_list_init(&pid_workspaces); + } + + struct sway_seat *seat = input_manager_current_seat(input_manager); + struct sway_container *ws = + seat_get_focus_inactive(seat, &root_container); + if (ws && ws->type != C_WORKSPACE) { + ws = container_parent(ws, C_WORKSPACE); + } + if (!ws) { + wlr_log(WLR_DEBUG, "Bailing out, no workspace"); + return; + } + struct sway_container *output = ws->parent; + if (!output) { + wlr_log(WLR_DEBUG, "Bailing out, no output"); + return; + } + + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + // Remove expired entries + static const int timeout = 60; + struct pid_workspace *old, *_old; + wl_list_for_each_safe(old, _old, &pid_workspaces, link) { + if (now.tv_sec - old->time_added.tv_sec >= timeout) { + wl_list_remove(&old->output_destroy.link); + wl_list_remove(&old->link); + free(old->workspace); + free(old); + } + } + + struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); + pw->workspace = strdup(ws->name); + pw->output = output; + pw->pid = pid; + memcpy(&pw->time_added, &now, sizeof(struct timespec)); + pw->output_destroy.notify = pw_handle_output_destroy; + wl_signal_add(&output->sway_output->wlr_output->events.destroy, + &pw->output_destroy); + wl_list_insert(&pid_workspaces, &pw->link); +} diff --git a/sway/tree/view.c b/sway/tree/view.c index 78baa705..9465b3a1 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -490,7 +490,7 @@ static struct sway_container *select_workspace(struct sway_view *view) { wl_resource_get_client(view->surface->resource); wl_client_get_credentials(client, &pid, NULL, NULL); #endif - ws = workspace_for_pid(pid); + ws = root_workspace_for_pid(pid); if (ws) { return ws; } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 687d9c95..cc225e79 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -539,116 +539,3 @@ void workspace_detect_urgent(struct sway_container *workspace) { container_damage_whole(workspace); } } - -struct pid_workspace { - pid_t pid; - char *workspace; - struct timespec time_added; - - struct sway_container *output; - struct wl_listener output_destroy; - - struct wl_list link; -}; - -static struct wl_list pid_workspaces; - -struct sway_container *workspace_for_pid(pid_t pid) { - if (!pid_workspaces.prev && !pid_workspaces.next) { - wl_list_init(&pid_workspaces); - return NULL; - } - - struct sway_container *ws = NULL; - struct pid_workspace *pw = NULL; - - wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid); - - do { - struct pid_workspace *_pw = NULL; - wl_list_for_each(_pw, &pid_workspaces, link) { - if (pid == _pw->pid) { - pw = _pw; - wlr_log(WLR_DEBUG, - "found pid_workspace for pid %d, workspace %s", - pid, pw->workspace); - goto found; - } - } - pid = get_parent_pid(pid); - } while (pid > 1); -found: - - if (pw && pw->workspace) { - ws = workspace_by_name(pw->workspace); - - if (!ws) { - wlr_log(WLR_DEBUG, - "Creating workspace %s for pid %d because it disappeared", - pw->workspace, pid); - ws = workspace_create(pw->output, pw->workspace); - } - - wl_list_remove(&pw->output_destroy.link); - wl_list_remove(&pw->link); - free(pw->workspace); - free(pw); - } - - return ws; -} - -static void pw_handle_output_destroy(struct wl_listener *listener, void *data) { - struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy); - pw->output = NULL; - wl_list_remove(&pw->output_destroy.link); - wl_list_init(&pw->output_destroy.link); -} - -void workspace_record_pid(pid_t pid) { - wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid); - if (!pid_workspaces.prev && !pid_workspaces.next) { - wl_list_init(&pid_workspaces); - } - - struct sway_seat *seat = input_manager_current_seat(input_manager); - struct sway_container *ws = - seat_get_focus_inactive(seat, &root_container); - if (ws && ws->type != C_WORKSPACE) { - ws = container_parent(ws, C_WORKSPACE); - } - if (!ws) { - wlr_log(WLR_DEBUG, "Bailing out, no workspace"); - return; - } - struct sway_container *output = ws->parent; - if (!output) { - wlr_log(WLR_DEBUG, "Bailing out, no output"); - return; - } - - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - - // Remove expired entries - static const int timeout = 60; - struct pid_workspace *old, *_old; - wl_list_for_each_safe(old, _old, &pid_workspaces, link) { - if (now.tv_sec - old->time_added.tv_sec >= timeout) { - wl_list_remove(&old->output_destroy.link); - wl_list_remove(&old->link); - free(old->workspace); - free(old); - } - } - - struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace)); - pw->workspace = strdup(ws->name); - pw->output = output; - pw->pid = pid; - memcpy(&pw->time_added, &now, sizeof(struct timespec)); - pw->output_destroy.notify = pw_handle_output_destroy; - wl_signal_add(&output->sway_output->wlr_output->events.destroy, - &pw->output_destroy); - wl_list_insert(&pid_workspaces, &pw->link); -} -- cgit v1.2.3