• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Benjamin Franzke
3  * Copyright © 2013 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #include "config.h"
28 
29 #include <libweston/libweston.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <linux/vt.h>
39 #include <linux/kd.h>
40 #include <linux/major.h>
41 
42 #include "launcher-impl.h"
43 
44 #define DRM_MAJOR 226
45 
46 #ifndef KDSKBMUTE
47 #define KDSKBMUTE	0x4B51
48 #endif
49 
50 /* major()/minor() */
51 #ifdef MAJOR_IN_MKDEV
52 #include <sys/mkdev.h>
53 #endif
54 #ifdef MAJOR_IN_SYSMACROS
55 #include <sys/sysmacros.h>
56 #endif
57 
58 #ifdef BUILD_DRM_COMPOSITOR
59 
60 #include <xf86drm.h>
61 
62 static inline int
is_drm_master(int drm_fd)63 is_drm_master(int drm_fd)
64 {
65 	drm_magic_t magic;
66 
67 	return drmGetMagic(drm_fd, &magic) == 0 &&
68 		drmAuthMagic(drm_fd, magic) == 0;
69 }
70 
71 #else
72 
73 static inline int
drmDropMaster(int drm_fd)74 drmDropMaster(int drm_fd)
75 {
76 	return 0;
77 }
78 
79 static inline int
drmSetMaster(int drm_fd)80 drmSetMaster(int drm_fd)
81 {
82 	return 0;
83 }
84 
85 static inline int
is_drm_master(int drm_fd)86 is_drm_master(int drm_fd)
87 {
88 	return 0;
89 }
90 
91 #endif
92 
93 struct launcher_direct {
94 	struct weston_launcher base;
95 	struct weston_compositor *compositor;
96 	int kb_mode, tty, drm_fd;
97 	struct wl_event_source *vt_source;
98 };
99 
100 static int
vt_handler(int signal_number,void * data)101 vt_handler(int signal_number, void *data)
102 {
103 	struct launcher_direct *launcher = data;
104 	struct weston_compositor *compositor = launcher->compositor;
105 
106 	if (compositor->session_active) {
107 		compositor->session_active = false;
108 		wl_signal_emit(&compositor->session_signal, compositor);
109 		drmDropMaster(launcher->drm_fd);
110 		ioctl(launcher->tty, VT_RELDISP, 1);
111 	} else {
112 		ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
113 		drmSetMaster(launcher->drm_fd);
114 		compositor->session_active = true;
115 		wl_signal_emit(&compositor->session_signal, compositor);
116 	}
117 
118 	return 1;
119 }
120 
121 static int
setup_tty(struct launcher_direct * launcher,int tty)122 setup_tty(struct launcher_direct *launcher, int tty)
123 {
124 	struct wl_event_loop *loop;
125 	struct vt_mode mode = { 0 };
126 	struct stat buf;
127 	char tty_device[32] ="<stdin>";
128 	int ret, kd_mode;
129 
130 	if (tty == 0) {
131 		launcher->tty = dup(tty);
132 		if (launcher->tty == -1) {
133 			weston_log("couldn't dup stdin: %s\n",
134 				   strerror(errno));
135 			return -1;
136 		}
137 	} else {
138 		snprintf(tty_device, sizeof tty_device, "/dev/tty%d", tty);
139 		launcher->tty = open(tty_device, O_RDWR | O_CLOEXEC);
140 		if (launcher->tty == -1) {
141 			weston_log("couldn't open tty %s: %s\n", tty_device,
142 				   strerror(errno));
143 			return -1;
144 		}
145 	}
146 
147 	if (fstat(launcher->tty, &buf) == -1 ||
148 	    major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
149 		weston_log("%s not a vt\n", tty_device);
150 		weston_log("if running weston from ssh, "
151 			   "use --tty to specify a tty\n");
152 		goto err_close;
153 	}
154 
155 	ret = ioctl(launcher->tty, KDGETMODE, &kd_mode);
156 	if (ret) {
157 		weston_log("failed to get VT mode: %s\n", strerror(errno));
158 		return -1;
159 	}
160 	if (kd_mode != KD_TEXT) {
161 		weston_log("%s is already in graphics mode, "
162 			   "is another display server running?\n", tty_device);
163 	}
164 
165 	ioctl(launcher->tty, VT_ACTIVATE, minor(buf.st_rdev));
166 	ioctl(launcher->tty, VT_WAITACTIVE, minor(buf.st_rdev));
167 
168 	if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
169 		weston_log("failed to read keyboard mode: %s\n",
170 			   strerror(errno));
171 		goto err_close;
172 	}
173 
174 	if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
175 	    ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
176 		weston_log("failed to set K_OFF keyboard mode: %s\n",
177 			   strerror(errno));
178 		goto err_close;
179 	}
180 
181 	ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
182 	if (ret) {
183 		weston_log("failed to set KD_GRAPHICS mode on tty: %s\n",
184 			   strerror(errno));
185 		goto err_close;
186 	}
187 
188 	/*
189 	 * SIGRTMIN is used as global VT-acquire+release signal. Note that
190 	 * SIGRT* must be tested on runtime, as their exact values are not
191 	 * known at compile-time. POSIX requires 32 of them to be available.
192 	 */
193 	if (SIGRTMIN > SIGRTMAX) {
194 		weston_log("not enough RT signals available: %u-%u\n",
195 			   SIGRTMIN, SIGRTMAX);
196 		ret = -EINVAL;
197 		goto err_close;
198 	}
199 
200 	mode.mode = VT_PROCESS;
201 	mode.relsig = SIGRTMIN;
202 	mode.acqsig = SIGRTMIN;
203 	if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
204 		weston_log("failed to take control of vt handling\n");
205 		goto err_close;
206 	}
207 
208 	loop = wl_display_get_event_loop(launcher->compositor->wl_display);
209 	launcher->vt_source =
210 		wl_event_loop_add_signal(loop, SIGRTMIN, vt_handler, launcher);
211 	if (!launcher->vt_source)
212 		goto err_close;
213 
214 	return 0;
215 
216  err_close:
217 	close(launcher->tty);
218 	return -1;
219 }
220 
221 static int
launcher_direct_open(struct weston_launcher * launcher_base,const char * path,int flags)222 launcher_direct_open(struct weston_launcher *launcher_base, const char *path, int flags)
223 {
224 	struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
225 	struct stat s;
226 	int fd;
227 
228 	fd = open(path, flags | O_CLOEXEC);
229 	if (fd == -1)
230 		return -1;
231 
232 	if (fstat(fd, &s) == -1) {
233 		close(fd);
234 		return -1;
235 	}
236 	// OHOS
237 	weston_log("launcher_direct_open %s\n", path);
238 
239 	if (major(s.st_rdev) == DRM_MAJOR) {
240 		launcher->drm_fd = fd;
241 // OHOS
242 //		if (!is_drm_master(fd)) {
243 //			weston_log("drm fd not master %s\n", path);
244 //			close(fd);
245 //			return -1;
246 //		}
247 	}
248 
249 	return fd;
250 }
251 
252 static void
launcher_direct_close(struct weston_launcher * launcher_base,int fd)253 launcher_direct_close(struct weston_launcher *launcher_base, int fd)
254 {
255 	close(fd);
256 }
257 
258 static void
launcher_direct_restore(struct weston_launcher * launcher_base)259 launcher_direct_restore(struct weston_launcher *launcher_base)
260 {
261 	struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
262 	struct vt_mode mode = { 0 };
263 
264 	if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
265 	    ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
266 		weston_log("failed to restore kb mode: %s\n",
267 			   strerror(errno));
268 
269 	if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
270 		weston_log("failed to set KD_TEXT mode on tty: %s\n",
271 			   strerror(errno));
272 
273 	/* We have to drop master before we switch the VT back in
274 	 * VT_AUTO, so we don't risk switching to a VT with another
275 	 * display server, that will then fail to set drm master. */
276 	drmDropMaster(launcher->drm_fd);
277 
278 	mode.mode = VT_AUTO;
279 	if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
280 		weston_log("could not reset vt handling\n");
281 }
282 
283 static int
launcher_direct_activate_vt(struct weston_launcher * launcher_base,int vt)284 launcher_direct_activate_vt(struct weston_launcher *launcher_base, int vt)
285 {
286 	struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
287 	return ioctl(launcher->tty, VT_ACTIVATE, vt);
288 }
289 
290 static int
launcher_direct_connect(struct weston_launcher ** out,struct weston_compositor * compositor,int tty,const char * seat_id,bool sync_drm)291 launcher_direct_connect(struct weston_launcher **out, struct weston_compositor *compositor,
292 			int tty, const char *seat_id, bool sync_drm)
293 {
294 	struct launcher_direct *launcher;
295 
296 	if (geteuid() != 0)
297 		return -EINVAL;
298 
299 	launcher = zalloc(sizeof(*launcher));
300 	if (launcher == NULL)
301 		return -ENOMEM;
302 
303 	launcher->base.iface = &launcher_direct_iface;
304 	launcher->compositor = compositor;
305 
306 	if (strcmp("seat0", seat_id) == 0) {
307 		if (setup_tty(launcher, tty) == -1) {
308 			free(launcher);
309 			return -1;
310 		}
311 	} else {
312 		launcher->tty = -1;
313 	}
314 
315 	* (struct launcher_direct **) out = launcher;
316 	return 0;
317 }
318 
319 static void
launcher_direct_destroy(struct weston_launcher * launcher_base)320 launcher_direct_destroy(struct weston_launcher *launcher_base)
321 {
322 	struct launcher_direct *launcher = wl_container_of(launcher_base, launcher, base);
323 
324 	if (launcher->tty >= 0) {
325 		launcher_direct_restore(&launcher->base);
326 		wl_event_source_remove(launcher->vt_source);
327 		close(launcher->tty);
328 	}
329 
330 	free(launcher);
331 }
332 
333 static int
launcher_direct_get_vt(struct weston_launcher * base)334 launcher_direct_get_vt(struct weston_launcher *base)
335 {
336 	struct launcher_direct *launcher = wl_container_of(base, launcher, base);
337 	struct stat s;
338 	if (fstat(launcher->tty, &s) < 0)
339 		return -1;
340 
341 	return minor(s.st_rdev);
342 }
343 
344 const struct launcher_interface launcher_direct_iface = {
345 	.connect = launcher_direct_connect,
346 	.destroy = launcher_direct_destroy,
347 	.open = launcher_direct_open,
348 	.close = launcher_direct_close,
349 	.activate_vt = launcher_direct_activate_vt,
350 	.get_vt = launcher_direct_get_vt,
351 };
352