aboutsummaryrefslogtreecommitdiff
path: root/sway/lock.c
blob: c4fbfe0eaa71debfa3b3963b30b47d376fd2fa75 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include "log.h"
#include "sway/input/cursor.h"
#include "sway/input/keyboard.h"
#include "sway/input/seat.h"
#include "sway/layers.h"
#include "sway/output.h"
#include "sway/server.h"
#include "sway/surface.h"

struct sway_session_lock_surface {
	struct wlr_session_lock_surface_v1 *lock_surface;
	struct sway_output *output;
	struct wlr_surface *surface;
	struct wl_listener map;
	struct wl_listener destroy;
	struct wl_listener surface_commit;
	struct wl_listener output_commit;
	struct wl_listener output_destroy;
};

static void set_lock_focused_surface(struct wlr_surface *focused) {
	server.session_lock.focused = focused;

	struct sway_seat *seat;
	wl_list_for_each(seat, &server.input->seats, link) {
		seat_set_focus_surface(seat, focused, false);
	}
}

static void handle_surface_map(struct wl_listener *listener, void *data) {
	struct sway_session_lock_surface *surf = wl_container_of(listener, surf, map);
	if (server.session_lock.focused == NULL) {
		set_lock_focused_surface(surf->surface);
	}
	cursor_rebase_all();
	surface_enter_output(surf->surface, surf->output);
	output_damage_whole(surf->output);
}

static void handle_surface_commit(struct wl_listener *listener, void *data) {
	struct sway_session_lock_surface *surf = wl_container_of(listener, surf, surface_commit);
	output_damage_surface(surf->output, 0, 0, surf->surface, false);
}

static void handle_output_commit(struct wl_listener *listener, void *data) {
	struct wlr_output_event_commit *event = data;
	struct sway_session_lock_surface *surf = wl_container_of(listener, surf, output_commit);
	if (event->state->committed & (
			WLR_OUTPUT_STATE_MODE |
			WLR_OUTPUT_STATE_SCALE |
			WLR_OUTPUT_STATE_TRANSFORM)) {
		wlr_session_lock_surface_v1_configure(surf->lock_surface,
			surf->output->width, surf->output->height);
	}
}

static void destroy_lock_surface(struct sway_session_lock_surface *surf) {
	// Move the seat focus to another surface if one is available
	if (server.session_lock.focused == surf->surface) {
		struct wlr_surface *next_focus = NULL;

		struct wlr_session_lock_surface_v1 *other;
		wl_list_for_each(other, &server.session_lock.lock->surfaces, link) {
			if (other != surf->lock_surface && other->surface->mapped) {
				next_focus = other->surface;
				break;
			}
		}
		set_lock_focused_surface(next_focus);
	}

	wl_list_remove(&surf->map.link);
	wl_list_remove(&surf->destroy.link);
	wl_list_remove(&surf->surface_commit.link);
	wl_list_remove(&surf->output_commit.link);
	wl_list_remove(&surf->output_destroy.link);
	output_damage_whole(surf->output);
	free(surf);
}

static void handle_surface_destroy(struct wl_listener *listener, void *data) {
	struct sway_session_lock_surface *surf = wl_container_of(listener, surf, destroy);
	destroy_lock_surface(surf);
}

static void handle_output_destroy(struct wl_listener *listener, void *data) {
	struct sway_session_lock_surface *surf =
		wl_container_of(listener, surf, output_destroy);
	destroy_lock_surface(surf);
}

static void handle_new_surface(struct wl_listener *listener, void *data) {
	struct wlr_session_lock_surface_v1 *lock_surface = data;
	struct sway_session_lock_surface *surf = calloc(1, sizeof(*surf));
	if (surf == NULL) {
		return;
	}

	sway_log(SWAY_DEBUG, "new lock layer surface");

	struct sway_output *output = lock_surface->output->data;
	wlr_session_lock_surface_v1_configure(lock_surface, output->width, output->height);

	surf->lock_surface = lock_surface;
	surf->surface = lock_surface->surface;
	surf->output = output;
	surf->map.notify = handle_surface_map;
	wl_signal_add(&lock_surface->surface->events.map, &surf->map);
	surf->destroy.notify = handle_surface_destroy;
	wl_signal_add(&lock_surface->events.destroy, &surf->destroy);
	surf->surface_commit.notify = handle_surface_commit;
	wl_signal_add(&surf->surface->events.commit, &surf->surface_commit);
	surf->output_commit.notify = handle_output_commit;
	wl_signal_add(&output->wlr_output->events.commit, &surf->output_commit);
	surf->output_destroy.notify = handle_output_destroy;
	wl_signal_add(&output->node.events.destroy, &surf->output_destroy);
}

static void handle_unlock(struct wl_listener *listener, void *data) {
	sway_log(SWAY_DEBUG, "session unlocked");
	server.session_lock.locked = false;
	server.session_lock.lock = NULL;
	server.session_lock.focused = NULL;

	wl_list_remove(&server.session_lock.lock_new_surface.link);
	wl_list_remove(&server.session_lock.lock_unlock.link);
	wl_list_remove(&server.session_lock.lock_destroy.link);

	struct sway_seat *seat;
	wl_list_for_each(seat, &server.input->seats, link) {
		// copied from seat_set_focus_layer -- deduplicate?
		struct sway_node *previous = seat_get_focus_inactive(seat, &root->node);
		if (previous) {
			// Hack to get seat to re-focus the return value of get_focus
			seat_set_focus(seat, NULL);
			seat_set_focus(seat, previous);
		}
	}

	// Triggers a refocus of the topmost surface layer if necessary
	// TODO: Make layer surface focus per-output based on cursor position
	for (int i = 0; i < root->outputs->length; ++i) {
		struct sway_output *output = root->outputs->items[i];
		arrange_layers(output);
	}

	// redraw everything
	for (int i = 0; i < root->outputs->length; ++i) {
		struct sway_output *output = root->outputs->items[i];
		output_damage_whole(output);
	}
}

static void handle_abandon(struct wl_listener *listener, void *data) {
	sway_log(SWAY_INFO, "session lock abandoned");
	server.session_lock.lock = NULL;
	server.session_lock.focused = NULL;

	wl_list_remove(&server.session_lock.lock_new_surface.link);
	wl_list_remove(&server.session_lock.lock_unlock.link);
	wl_list_remove(&server.session_lock.lock_destroy.link);

	// redraw everything
	for (int i = 0; i < root->outputs->length; ++i) {
		struct sway_output *output = root->outputs->items[i];
		output_damage_whole(output);
	}
}

static void handle_session_lock(struct wl_listener *listener, void *data) {
	struct wlr_session_lock_v1 *lock = data;
	struct wl_client *client = wl_resource_get_client(lock->resource);

	if (server.session_lock.lock) {
		wlr_session_lock_v1_destroy(lock);
		return;
	}

	sway_log(SWAY_DEBUG, "session locked");
	server.session_lock.locked = true;
	server.session_lock.lock = lock;

	struct sway_seat *seat;
	wl_list_for_each(seat, &server.input->seats, link) {
		seat_unfocus_unless_client(seat, client);
	}

	wl_signal_add(&lock->events.new_surface, &server.session_lock.lock_new_surface);
	wl_signal_add(&lock->events.unlock, &server.session_lock.lock_unlock);
	wl_signal_add(&lock->events.destroy, &server.session_lock.lock_destroy);

	wlr_session_lock_v1_send_locked(lock);

	// redraw everything
	for (int i = 0; i < root->outputs->length; ++i) {
		struct sway_output *output = root->outputs->items[i];
		output_damage_whole(output);
	}
}

static void handle_session_lock_destroy(struct wl_listener *listener, void *data) {
	assert(server.session_lock.lock == NULL);
	wl_list_remove(&server.session_lock.new_lock.link);
	wl_list_remove(&server.session_lock.manager_destroy.link);
}

void sway_session_lock_init(void) {
	server.session_lock.manager = wlr_session_lock_manager_v1_create(server.wl_display);

	server.session_lock.lock_new_surface.notify = handle_new_surface;
	server.session_lock.lock_unlock.notify = handle_unlock;
	server.session_lock.lock_destroy.notify = handle_abandon;
	server.session_lock.new_lock.notify = handle_session_lock;
	server.session_lock.manager_destroy.notify = handle_session_lock_destroy;
	wl_signal_add(&server.session_lock.manager->events.new_lock,
		&server.session_lock.new_lock);
	wl_signal_add(&server.session_lock.manager->events.destroy,
		&server.session_lock.manager_destroy);
}