aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-07-24 14:44:01 -0400
committerLibravatar GitHub <noreply@github.com>2018-07-24 14:44:01 -0400
commit817d37c95032946a2e508fcc33cfa5c7ed65cc0d (patch)
tree69fb79348eaff481fabaee4c2bb25991487d438b
parentMerge pull request #2346 from RyanDwyer/fix-crash-on-click (diff)
parentAddress @emersion's review comments (diff)
downloadsway-817d37c95032946a2e508fcc33cfa5c7ed65cc0d.tar.gz
sway-817d37c95032946a2e508fcc33cfa5c7ed65cc0d.tar.zst
sway-817d37c95032946a2e508fcc33cfa5c7ed65cc0d.zip
Merge pull request #2165 from swaywm/pid-workspaces
Implement pid->workspace tracking
-rw-r--r--config.in3
-rw-r--r--include/sway/config.h11
-rw-r--r--include/sway/tree/workspace.h4
-rw-r--r--sway/commands/exec_always.c2
-rw-r--r--sway/config.c2
-rw-r--r--sway/desktop/xdg_shell.c3
-rw-r--r--sway/desktop/xdg_shell_v6.c3
-rw-r--r--sway/desktop/xwayland.c3
-rw-r--r--sway/tree/view.c42
-rw-r--r--sway/tree/workspace.c114
10 files changed, 155 insertions, 32 deletions
diff --git a/config.in b/config.in
index 4a11762a..41f53461 100644
--- a/config.in
+++ b/config.in
@@ -16,7 +16,8 @@ set $right l
16# Your preferred terminal emulator 16# Your preferred terminal emulator
17set $term urxvt 17set $term urxvt
18# Your preferred application launcher 18# Your preferred application launcher
19set $menu dmenu_run 19# Note: it's recommended that you pass the final command to sway
20set $menu dmenu_path | dmenu | xargs swaymsg exec
20 21
21### Output configuration 22### Output configuration
22# 23#
diff --git a/include/sway/config.h b/include/sway/config.h
index bcd503a4..032f4196 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -1,6 +1,5 @@
1#ifndef _SWAY_CONFIG_H 1#ifndef _SWAY_CONFIG_H
2#define _SWAY_CONFIG_H 2#define _SWAY_CONFIG_H
3#define PID_WORKSPACE_TIMEOUT 60
4#include <libinput.h> 3#include <libinput.h>
5#include <stdint.h> 4#include <stdint.h>
6#include <string.h> 5#include <string.h>
@@ -161,12 +160,6 @@ struct workspace_output {
161 char *workspace; 160 char *workspace;
162}; 161};
163 162
164struct pid_workspace {
165 pid_t *pid;
166 char *workspace;
167 time_t *time_added;
168};
169
170struct bar_config { 163struct bar_config {
171 /** 164 /**
172 * One of "dock", "hide", "invisible" 165 * One of "dock", "hide", "invisible"
@@ -317,7 +310,6 @@ struct sway_config {
317 list_t *bars; 310 list_t *bars;
318 list_t *cmd_queue; 311 list_t *cmd_queue;
319 list_t *workspace_outputs; 312 list_t *workspace_outputs;
320 list_t *pid_workspaces;
321 list_t *output_configs; 313 list_t *output_configs;
322 list_t *input_configs; 314 list_t *input_configs;
323 list_t *seat_configs; 315 list_t *seat_configs;
@@ -403,9 +395,6 @@ struct sway_config {
403 } handler_context; 395 } handler_context;
404}; 396};
405 397
406void pid_workspace_add(struct pid_workspace *pw);
407void free_pid_workspace(struct pid_workspace *pw);
408
409/** 398/**
410 * Loads the main config from the given path. is_active should be true when 399 * Loads the main config from the given path. is_active should be true when
411 * reloading the config. 400 * reloading the config.
diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h
index bc95317a..ff66da6b 100644
--- a/include/sway/tree/workspace.h
+++ b/include/sway/tree/workspace.h
@@ -44,6 +44,10 @@ void workspace_output_add_priority(struct sway_container *workspace,
44struct sway_container *workspace_output_get_highest_available( 44struct sway_container *workspace_output_get_highest_available(
45 struct sway_container *ws, struct sway_container *exclude); 45 struct sway_container *ws, struct sway_container *exclude);
46 46
47struct sway_container *workspace_for_pid(pid_t pid);
48
49void workspace_record_pid(pid_t pid);
50
47void workspace_detect_urgent(struct sway_container *workspace); 51void workspace_detect_urgent(struct sway_container *workspace);
48 52
49#endif 53#endif
diff --git a/sway/commands/exec_always.c b/sway/commands/exec_always.c
index c7727857..9bf2b320 100644
--- a/sway/commands/exec_always.c
+++ b/sway/commands/exec_always.c
@@ -74,7 +74,7 @@ struct cmd_results *cmd_exec_always(int argc, char **argv) {
74 waitpid(pid, NULL, 0); 74 waitpid(pid, NULL, 0);
75 if (child > 0) { 75 if (child > 0) {
76 wlr_log(WLR_DEBUG, "Child process created with pid %d", child); 76 wlr_log(WLR_DEBUG, "Child process created with pid %d", child);
77 // TODO: add PID to active workspace 77 workspace_record_pid(child);
78 } else { 78 } else {
79 return cmd_results_new(CMD_FAILURE, "exec_always", 79 return cmd_results_new(CMD_FAILURE, "exec_always",
80 "Second fork() failed"); 80 "Second fork() failed");
diff --git a/sway/config.c b/sway/config.c
index c2310ff7..90dfb9a9 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -93,7 +93,6 @@ void free_config(struct sway_config *config) {
93 } 93 }
94 list_free(config->cmd_queue); 94 list_free(config->cmd_queue);
95 list_free(config->workspace_outputs); 95 list_free(config->workspace_outputs);
96 list_free(config->pid_workspaces);
97 if (config->output_configs) { 96 if (config->output_configs) {
98 for (int i = 0; i < config->output_configs->length; i++) { 97 for (int i = 0; i < config->output_configs->length; i++) {
99 free_output_config(config->output_configs->items[i]); 98 free_output_config(config->output_configs->items[i]);
@@ -163,7 +162,6 @@ static void config_defaults(struct sway_config *config) {
163 if (!(config->modes = create_list())) goto cleanup; 162 if (!(config->modes = create_list())) goto cleanup;
164 if (!(config->bars = create_list())) goto cleanup; 163 if (!(config->bars = create_list())) goto cleanup;
165 if (!(config->workspace_outputs = create_list())) goto cleanup; 164 if (!(config->workspace_outputs = create_list())) goto cleanup;
166 if (!(config->pid_workspaces = create_list())) goto cleanup;
167 if (!(config->criteria = create_list())) goto cleanup; 165 if (!(config->criteria = create_list())) goto cleanup;
168 if (!(config->no_focus = create_list())) goto cleanup; 166 if (!(config->no_focus = create_list())) goto cleanup;
169 if (!(config->input_configs = create_list())) goto cleanup; 167 if (!(config->input_configs = create_list())) goto cleanup;
diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c
index 62c3abc8..f3e4fef8 100644
--- a/sway/desktop/xdg_shell.c
+++ b/sway/desktop/xdg_shell.c
@@ -418,9 +418,6 @@ void handle_xdg_shell_surface(struct wl_listener *listener, void *data) {
418 view_init(&xdg_shell_view->view, SWAY_VIEW_XDG_SHELL, &view_impl); 418 view_init(&xdg_shell_view->view, SWAY_VIEW_XDG_SHELL, &view_impl);
419 xdg_shell_view->view.wlr_xdg_surface = xdg_surface; 419 xdg_shell_view->view.wlr_xdg_surface = xdg_surface;
420 420
421 // TODO:
422 // - Look up pid and open on appropriate workspace
423
424 xdg_shell_view->map.notify = handle_map; 421 xdg_shell_view->map.notify = handle_map;
425 wl_signal_add(&xdg_surface->events.map, &xdg_shell_view->map); 422 wl_signal_add(&xdg_surface->events.map, &xdg_shell_view->map);
426 423
diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c
index 7fb85410..46fd4769 100644
--- a/sway/desktop/xdg_shell_v6.c
+++ b/sway/desktop/xdg_shell_v6.c
@@ -409,9 +409,6 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) {
409 view_init(&xdg_shell_v6_view->view, SWAY_VIEW_XDG_SHELL_V6, &view_impl); 409 view_init(&xdg_shell_v6_view->view, SWAY_VIEW_XDG_SHELL_V6, &view_impl);
410 xdg_shell_v6_view->view.wlr_xdg_surface_v6 = xdg_surface; 410 xdg_shell_v6_view->view.wlr_xdg_surface_v6 = xdg_surface;
411 411
412 // TODO:
413 // - Look up pid and open on appropriate workspace
414
415 xdg_shell_v6_view->map.notify = handle_map; 412 xdg_shell_v6_view->map.notify = handle_map;
416 wl_signal_add(&xdg_surface->events.map, &xdg_shell_v6_view->map); 413 wl_signal_add(&xdg_surface->events.map, &xdg_shell_v6_view->map);
417 414
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c
index 2546168b..65d4fcd4 100644
--- a/sway/desktop/xwayland.c
+++ b/sway/desktop/xwayland.c
@@ -514,9 +514,6 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) {
514 view_init(&xwayland_view->view, SWAY_VIEW_XWAYLAND, &view_impl); 514 view_init(&xwayland_view->view, SWAY_VIEW_XWAYLAND, &view_impl);
515 xwayland_view->view.wlr_xwayland_surface = xsurface; 515 xwayland_view->view.wlr_xwayland_surface = xsurface;
516 516
517 // TODO:
518 // - Look up pid and open on appropriate workspace
519
520 wl_signal_add(&xsurface->events.destroy, &xwayland_view->destroy); 517 wl_signal_add(&xsurface->events.destroy, &xwayland_view->destroy);
521 xwayland_view->destroy.notify = handle_destroy; 518 xwayland_view->destroy.notify = handle_destroy;
522 519
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 9d88d7aa..a55c8a29 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -3,6 +3,7 @@
3#include <wayland-server.h> 3#include <wayland-server.h>
4#include <wlr/render/wlr_renderer.h> 4#include <wlr/render/wlr_renderer.h>
5#include <wlr/types/wlr_output_layout.h> 5#include <wlr/types/wlr_output_layout.h>
6#include <wlr/xwayland.h>
6#include "list.h" 7#include "list.h"
7#include "log.h" 8#include "log.h"
8#include "sway/criteria.h" 9#include "sway/criteria.h"
@@ -561,9 +562,21 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
561 return; 562 return;
562 } 563 }
563 564
565 pid_t pid;
566 if (view->type == SWAY_VIEW_XWAYLAND) {
567 struct wlr_xwayland_surface *surf =
568 wlr_xwayland_surface_from_wlr_surface(wlr_surface);
569 pid = surf->pid;
570 } else {
571 struct wl_client *client =
572 wl_resource_get_client(wlr_surface->resource);
573 wl_client_get_credentials(client, &pid, NULL, NULL);
574 }
575
564 struct sway_seat *seat = input_manager_current_seat(input_manager); 576 struct sway_seat *seat = input_manager_current_seat(input_manager);
565 struct sway_container *focus = 577 struct sway_container *target_sibling =
566 seat_get_focus_inactive(seat, &root_container); 578 seat_get_focus_inactive(seat, &root_container);
579 struct sway_container *prev_focus = target_sibling;
567 struct sway_container *cont = NULL; 580 struct sway_container *cont = NULL;
568 581
569 // Check if there's any `assign` criteria for the view 582 // Check if there's any `assign` criteria for the view
@@ -577,22 +590,35 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
577 if (!workspace) { 590 if (!workspace) {
578 workspace = workspace_create(NULL, criteria->target); 591 workspace = workspace_create(NULL, criteria->target);
579 } 592 }
580 focus = seat_get_focus_inactive(seat, workspace); 593 prev_focus = target_sibling;
594 target_sibling = seat_get_focus_inactive(seat, workspace);
581 } else { 595 } else {
582 // CT_ASSIGN_OUTPUT 596 // CT_ASSIGN_OUTPUT
583 struct sway_container *output = output_by_name(criteria->target); 597 struct sway_container *output = output_by_name(criteria->target);
584 if (output) { 598 if (output) {
585 focus = seat_get_focus_inactive(seat, output); 599 prev_focus = seat_get_focus_inactive(seat, output);
586 } 600 }
587 } 601 }
588 } 602 }
603 list_free(criterias);
604
605 if (!workspace) {
606 workspace = workspace_for_pid(pid);
607 if (workspace) {
608 prev_focus = target_sibling;
609 target_sibling = seat_get_focus_inactive(seat, workspace);
610 }
611 }
589 // If we're about to launch the view into the floating container, then 612 // If we're about to launch the view into the floating container, then
590 // launch it as a tiled view in the root of the workspace instead. 613 // launch it as a tiled view in the root of the workspace instead.
591 if (container_is_floating(focus)) { 614 if (container_is_floating(target_sibling)) {
592 focus = focus->parent->parent; 615 if (prev_focus == target_sibling) {
616 prev_focus = target_sibling->parent->parent;
617 }
618 target_sibling = target_sibling->parent->parent;
593 } 619 }
594 list_free(criterias); 620
595 cont = container_view_create(focus, view); 621 cont = container_view_create(target_sibling, view);
596 622
597 view->surface = wlr_surface; 623 view->surface = wlr_surface;
598 view->swayc = cont; 624 view->swayc = cont;
@@ -615,7 +641,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
615 view_set_tiled(view, true); 641 view_set_tiled(view, true);
616 } 642 }
617 643
618 if (should_focus(view)) { 644 if (should_focus(view) && prev_focus == target_sibling) {
619 input_manager_set_focus(input_manager, cont); 645 input_manager_set_focus(input_manager, cont);
620 if (workspace) { 646 if (workspace) {
621 workspace_switch(workspace); 647 workspace_switch(workspace);
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index e450b87f..62974cd7 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -9,6 +9,7 @@
9#include "sway/input/input-manager.h" 9#include "sway/input/input-manager.h"
10#include "sway/input/seat.h" 10#include "sway/input/seat.h"
11#include "sway/ipc-server.h" 11#include "sway/ipc-server.h"
12#include "sway/output.h"
12#include "sway/tree/arrange.h" 13#include "sway/tree/arrange.h"
13#include "sway/tree/container.h" 14#include "sway/tree/container.h"
14#include "sway/tree/view.h" 15#include "sway/tree/view.h"
@@ -536,3 +537,116 @@ void workspace_detect_urgent(struct sway_container *workspace) {
536 container_damage_whole(workspace); 537 container_damage_whole(workspace);
537 } 538 }
538} 539}
540
541struct pid_workspace {
542 pid_t pid;
543 char *workspace;
544 struct timespec time_added;
545
546 struct sway_container *output;
547 struct wl_listener output_destroy;
548
549 struct wl_list link;
550};
551
552static struct wl_list pid_workspaces;
553
554struct sway_container *workspace_for_pid(pid_t pid) {
555 if (!pid_workspaces.prev && !pid_workspaces.next) {
556 wl_list_init(&pid_workspaces);
557 return NULL;
558 }
559
560 struct sway_container *ws = NULL;
561 struct pid_workspace *pw = NULL;
562
563 wlr_log(WLR_DEBUG, "Looking up workspace for pid %d", pid);
564
565 do {
566 struct pid_workspace *_pw = NULL;
567 wl_list_for_each(_pw, &pid_workspaces, link) {
568 if (pid == _pw->pid) {
569 pw = _pw;
570 wlr_log(WLR_DEBUG,
571 "found pid_workspace for pid %d, workspace %s",
572 pid, pw->workspace);
573 goto found;
574 }
575 }
576 pid = get_parent_pid(pid);
577 } while (pid > 1);
578found:
579
580 if (pw && pw->workspace) {
581 ws = workspace_by_name(pw->workspace);
582
583 if (!ws) {
584 wlr_log(WLR_DEBUG,
585 "Creating workspace %s for pid %d because it disappeared",
586 pw->workspace, pid);
587 ws = workspace_create(pw->output, pw->workspace);
588 }
589
590 wl_list_remove(&pw->output_destroy.link);
591 wl_list_remove(&pw->link);
592 free(pw->workspace);
593 free(pw);
594 }
595
596 return ws;
597}
598
599static void pw_handle_output_destroy(struct wl_listener *listener, void *data) {
600 struct pid_workspace *pw = wl_container_of(listener, pw, output_destroy);
601 pw->output = NULL;
602 wl_list_remove(&pw->output_destroy.link);
603 wl_list_init(&pw->output_destroy.link);
604}
605
606void workspace_record_pid(pid_t pid) {
607 wlr_log(WLR_DEBUG, "Recording workspace for process %d", pid);
608 if (!pid_workspaces.prev && !pid_workspaces.next) {
609 wl_list_init(&pid_workspaces);
610 }
611
612 struct sway_seat *seat = input_manager_current_seat(input_manager);
613 struct sway_container *ws =
614 seat_get_focus_inactive(seat, &root_container);
615 if (ws && ws->type != C_WORKSPACE) {
616 ws = container_parent(ws, C_WORKSPACE);
617 }
618 if (!ws) {
619 wlr_log(WLR_DEBUG, "Bailing out, no workspace");
620 return;
621 }
622 struct sway_container *output = ws->parent;
623 if (!output) {
624 wlr_log(WLR_DEBUG, "Bailing out, no output");
625 return;
626 }
627
628 struct timespec now;
629 clock_gettime(CLOCK_MONOTONIC, &now);
630
631 // Remove expired entries
632 static const int timeout = 60;
633 struct pid_workspace *old, *_old;
634 wl_list_for_each_safe(old, _old, &pid_workspaces, link) {
635 if (now.tv_sec - old->time_added.tv_sec >= timeout) {
636 wl_list_remove(&old->output_destroy.link);
637 wl_list_remove(&old->link);
638 free(old->workspace);
639 free(old);
640 }
641 }
642
643 struct pid_workspace *pw = calloc(1, sizeof(struct pid_workspace));
644 pw->workspace = strdup(ws->name);
645 pw->output = output;
646 pw->pid = pid;
647 memcpy(&pw->time_added, &now, sizeof(struct timespec));
648 pw->output_destroy.notify = pw_handle_output_destroy;
649 wl_signal_add(&output->sway_output->wlr_output->events.destroy,
650 &pw->output_destroy);
651 wl_list_insert(&pid_workspaces, &pw->link);
652}