diff options
Diffstat (limited to 'sway/tree/workspace.c')
-rw-r--r-- | sway/tree/workspace.c | 114 |
1 files changed, 114 insertions, 0 deletions
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 | |||
541 | struct 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 | |||
552 | static struct wl_list pid_workspaces; | ||
553 | |||
554 | struct 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); | ||
578 | found: | ||
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 | |||
599 | static 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 | |||
606 | void 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 | } | ||