1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <stdlib.h>
#include <wlr/types/wlr_idle.h>
#include <wlr/types/wlr_idle_notify_v1.h>
#include "log.h"
#include "sway/desktop/idle_inhibit_v1.h"
#include "sway/input/seat.h"
#include "sway/tree/container.h"
#include "sway/tree/view.h"
#include "sway/server.h"
static void destroy_inhibitor(struct sway_idle_inhibitor_v1 *inhibitor) {
wl_list_remove(&inhibitor->link);
wl_list_remove(&inhibitor->destroy.link);
sway_idle_inhibit_v1_check_active();
free(inhibitor);
}
static void handle_destroy(struct wl_listener *listener, void *data) {
struct sway_idle_inhibitor_v1 *inhibitor =
wl_container_of(listener, inhibitor, destroy);
sway_log(SWAY_DEBUG, "Sway idle inhibitor destroyed");
destroy_inhibitor(inhibitor);
}
void handle_idle_inhibitor_v1(struct wl_listener *listener, void *data) {
struct wlr_idle_inhibitor_v1 *wlr_inhibitor = data;
struct sway_idle_inhibit_manager_v1 *manager =
wl_container_of(listener, manager, new_idle_inhibitor_v1);
sway_log(SWAY_DEBUG, "New sway idle inhibitor");
struct sway_idle_inhibitor_v1 *inhibitor =
calloc(1, sizeof(struct sway_idle_inhibitor_v1));
if (!inhibitor) {
return;
}
inhibitor->mode = INHIBIT_IDLE_APPLICATION;
inhibitor->wlr_inhibitor = wlr_inhibitor;
wl_list_insert(&manager->inhibitors, &inhibitor->link);
inhibitor->destroy.notify = handle_destroy;
wl_signal_add(&wlr_inhibitor->events.destroy, &inhibitor->destroy);
sway_idle_inhibit_v1_check_active();
}
void sway_idle_inhibit_v1_user_inhibitor_register(struct sway_view *view,
enum sway_idle_inhibit_mode mode) {
struct sway_idle_inhibit_manager_v1 *manager = &server.idle_inhibit_manager_v1;
struct sway_idle_inhibitor_v1 *inhibitor =
calloc(1, sizeof(struct sway_idle_inhibitor_v1));
if (!inhibitor) {
return;
}
inhibitor->mode = mode;
inhibitor->view = view;
wl_list_insert(&manager->inhibitors, &inhibitor->link);
inhibitor->destroy.notify = handle_destroy;
wl_signal_add(&view->events.unmap, &inhibitor->destroy);
sway_idle_inhibit_v1_check_active();
}
struct sway_idle_inhibitor_v1 *sway_idle_inhibit_v1_user_inhibitor_for_view(
struct sway_view *view) {
struct sway_idle_inhibit_manager_v1 *manager = &server.idle_inhibit_manager_v1;
struct sway_idle_inhibitor_v1 *inhibitor;
wl_list_for_each(inhibitor, &manager->inhibitors, link) {
if (inhibitor->mode != INHIBIT_IDLE_APPLICATION &&
inhibitor->view == view) {
return inhibitor;
}
}
return NULL;
}
struct sway_idle_inhibitor_v1 *sway_idle_inhibit_v1_application_inhibitor_for_view(
struct sway_view *view) {
struct sway_idle_inhibit_manager_v1 *manager = &server.idle_inhibit_manager_v1;
struct sway_idle_inhibitor_v1 *inhibitor;
wl_list_for_each(inhibitor, &manager->inhibitors, link) {
if (inhibitor->mode == INHIBIT_IDLE_APPLICATION &&
view_from_wlr_surface(inhibitor->wlr_inhibitor->surface) == view) {
return inhibitor;
}
}
return NULL;
}
void sway_idle_inhibit_v1_user_inhibitor_destroy(
struct sway_idle_inhibitor_v1 *inhibitor) {
if (!inhibitor) {
return;
}
if (!sway_assert(inhibitor->mode != INHIBIT_IDLE_APPLICATION,
"User should not be able to destroy application inhibitor")) {
return;
}
destroy_inhibitor(inhibitor);
}
bool sway_idle_inhibit_v1_is_active(struct sway_idle_inhibitor_v1 *inhibitor) {
switch (inhibitor->mode) {
case INHIBIT_IDLE_APPLICATION:;
// If there is no view associated with the inhibitor, assume visible
struct sway_view *view = view_from_wlr_surface(inhibitor->wlr_inhibitor->surface);
return !view || !view->container || view_is_visible(view);
case INHIBIT_IDLE_FOCUS:;
struct sway_seat *seat = NULL;
wl_list_for_each(seat, &server.input->seats, link) {
struct sway_container *con = seat_get_focused_container(seat);
if (con && con->view && con->view == inhibitor->view) {
return true;
}
}
return false;
case INHIBIT_IDLE_FULLSCREEN:
return inhibitor->view->container &&
container_is_fullscreen_or_child(inhibitor->view->container) &&
view_is_visible(inhibitor->view);
case INHIBIT_IDLE_OPEN:
// Inhibitor is destroyed on unmap so it must be open/mapped
return true;
case INHIBIT_IDLE_VISIBLE:
return view_is_visible(inhibitor->view);
}
return false;
}
void sway_idle_inhibit_v1_check_active(void) {
struct sway_idle_inhibit_manager_v1 *manager = &server.idle_inhibit_manager_v1;
struct sway_idle_inhibitor_v1 *inhibitor;
bool inhibited = false;
wl_list_for_each(inhibitor, &manager->inhibitors, link) {
if ((inhibited = sway_idle_inhibit_v1_is_active(inhibitor))) {
break;
}
}
wlr_idle_set_enabled(server.idle, NULL, !inhibited);
wlr_idle_notifier_v1_set_inhibited(server.idle_notifier_v1, inhibited);
}
bool sway_idle_inhibit_manager_v1_init(void) {
struct sway_idle_inhibit_manager_v1 *manager = &server.idle_inhibit_manager_v1;
manager->wlr_manager = wlr_idle_inhibit_v1_create(server.wl_display);
if (!manager->wlr_manager) {
return false;
}
wl_signal_add(&manager->wlr_manager->events.new_inhibitor,
&manager->new_idle_inhibitor_v1);
manager->new_idle_inhibitor_v1.notify = handle_idle_inhibitor_v1;
wl_list_init(&manager->inhibitors);
return true;
}
|