summaryrefslogtreecommitdiff
path: root/libweston/launcher-libseat.c
blob: 6aa8ad0f2a0c8a16da015f44ccfd021972f7c391 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright © 2020 Kenny Levinsen
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include <libseat.h>

#include <libweston/libweston.h>
#include "backend.h"
#include "dbus.h"
#include "launcher-impl.h"

struct launcher_libseat_device {
	struct wl_list link;
	int fd;
	int device_id;
	dev_t fsdev;
};

struct launcher_libseat {
	struct weston_launcher base;
	struct weston_compositor *compositor;
	struct libseat *seat;

	struct wl_event_source *seat_ctx;
	struct wl_list devices;
};

static struct launcher_libseat_device *
find_device_by_fd(struct launcher_libseat *wl, int fd)
{
	struct launcher_libseat_device *dev;
	wl_list_for_each(dev, &wl->devices, link) {
		if (dev->fd == fd) {
			return dev;
		}
	}
	return NULL;
}

static void
handle_enable_seat(struct libseat *seat, void *data)
{
	struct launcher_libseat *wl = data;
	if (wl->compositor->session_active)
		return;

	wl->compositor->session_active = true;

	wl_signal_emit(&wl->compositor->session_signal,
		       wl->compositor);
}

static void
handle_disable_seat(struct libseat *seat, void *data)
{
	struct launcher_libseat *wl = data;
	if (!wl->compositor->session_active)
		return;

	wl->compositor->session_active = false;

	wl_signal_emit(&wl->compositor->session_signal,
		       wl->compositor);
	libseat_disable_seat(wl->seat);
}

static struct libseat_seat_listener seat_listener = {
	.enable_seat = handle_enable_seat,
	.disable_seat = handle_disable_seat,
};

static int
seat_open_device(struct weston_launcher *launcher, const char *path, int flags)
{
	struct launcher_libseat *wl = wl_container_of(launcher, wl, base);
	struct launcher_libseat_device *dev;
	struct stat st;

	dev = zalloc(sizeof(struct launcher_libseat_device));
	if (dev == NULL) {
		goto err_alloc;
	}

	dev->device_id = libseat_open_device(wl->seat, path, &dev->fd);
	if (dev->device_id == -1) {
		goto err_open;
	}

	if (fstat(dev->fd, &st) == -1) {
		goto err_fd;
	}
	dev->fsdev = st.st_rdev;

	wl_list_insert(&wl->devices, &dev->link);
	return dev->fd;

err_fd:
	libseat_close_device(wl->seat, dev->device_id);
	close(dev->fd);
err_open:
	free(dev);
err_alloc:
	return -1;
}

static void
seat_close_device(struct weston_launcher *launcher, int fd)
{
	struct launcher_libseat *wl = wl_container_of(launcher, wl, base);
	struct launcher_libseat_device *dev;

	dev = find_device_by_fd(wl, fd);
	if (dev == NULL) {
		weston_log("libseat: No device with fd %d found\n", fd);
		close(fd);
		return;
	}

	if (libseat_close_device(wl->seat, dev->device_id) == -1) {
		weston_log("libseat: Could not close device %d",
				dev->device_id);
	}

	wl_list_remove(&dev->link);
	free(dev);
	close(fd);
}

static int
seat_switch_session(struct weston_launcher *launcher, int vt)
{
	struct launcher_libseat *wl = wl_container_of(launcher, wl, base);
	return libseat_switch_session(wl->seat, vt);
}

static int
libseat_event(int fd, uint32_t mask, void *data)
{
	struct libseat *seat = data;
	if (libseat_dispatch(seat, 0) == -1) {
		weston_log("libseat: dispatch failed: %s\n", strerror(errno));
		exit(-1);
	}
	return 1;
}

static int
seat_open(struct weston_launcher **out, struct weston_compositor *compositor,
	  int tty, const char *seat_id, bool sync_drm)
{
	struct launcher_libseat *wl;
	struct wl_event_loop *event_loop;

	wl = zalloc(sizeof(*wl));
	if (wl == NULL) {
		goto err_out;
	}

	wl->base.iface = &launcher_libseat_iface;
	wl->compositor = compositor;
	wl_list_init(&wl->devices);

	wl->seat = libseat_open_seat(&seat_listener, wl);
	if (wl->seat == NULL) {
		weston_log("libseat: could not open seat\n");
		goto err_seat;
	}

	event_loop = wl_display_get_event_loop(compositor->wl_display);
	wl->seat_ctx = wl_event_loop_add_fd(event_loop,
			libseat_get_fd(wl->seat), WL_EVENT_READABLE,
			libseat_event, wl->seat);
	if (wl->seat_ctx == NULL) {
		weston_log("libseat: could not register connection to event loop\n");
		goto err_session;
	}
	if (libseat_dispatch(wl->seat, 0) == -1) {
		weston_log("libseat: dispatch failed\n");
		goto err_session;
	}

	weston_log("libseat: session control granted\n");
	*out = &wl->base;
	return 0;

err_session:
	libseat_close_seat(wl->seat);
err_seat:
	free(wl);
err_out:
	return -1;
}

static void
seat_close(struct weston_launcher *launcher)
{
	struct launcher_libseat *wl = wl_container_of(launcher, wl, base);

	if (wl->seat != NULL) {
		libseat_close_seat(wl->seat);
	}
	wl_event_source_remove(wl->seat_ctx);
	free(wl);
}

static int
seat_get_vt(struct weston_launcher *launcher)
{
	return -ENOSYS;
}

const struct launcher_interface launcher_libseat_iface = {
	.name = "libseat",
	.connect = seat_open,
	.destroy = seat_close,
	.open = seat_open_device,
	.close = seat_close_device,
	.activate_vt = seat_switch_session,
	.get_vt = seat_get_vt,
};