aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree')
-rw-r--r--sway/tree/view.c42
-rw-r--r--sway/tree/workspace.c114
2 files changed, 148 insertions, 8 deletions
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}