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 "launcher-util.h"
32 #include "launcher-impl.h"
33
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <linux/input.h>
37
38 static const struct launcher_interface *ifaces[] = {
39 #ifdef HAVE_SYSTEMD_LOGIN
40 &launcher_logind_iface,
41 #endif
42 &launcher_weston_launch_iface,
43 &launcher_direct_iface,
44 NULL,
45 };
46
47 WL_EXPORT struct weston_launcher *
weston_launcher_connect(struct weston_compositor * compositor,int tty,const char * seat_id,bool sync_drm)48 weston_launcher_connect(struct weston_compositor *compositor, int tty,
49 const char *seat_id, bool sync_drm)
50 {
51 const struct launcher_interface **it;
52
53 for (it = ifaces; *it != NULL; it++) {
54 const struct launcher_interface *iface = *it;
55 struct weston_launcher *launcher;
56
57 if (iface->connect(&launcher, compositor, tty, seat_id, sync_drm) == 0)
58 return launcher;
59 }
60
61 return NULL;
62 }
63
64 WL_EXPORT void
weston_launcher_destroy(struct weston_launcher * launcher)65 weston_launcher_destroy(struct weston_launcher *launcher)
66 {
67 launcher->iface->destroy(launcher);
68 }
69
70 WL_EXPORT int
weston_launcher_open(struct weston_launcher * launcher,const char * path,int flags)71 weston_launcher_open(struct weston_launcher *launcher,
72 const char *path, int flags)
73 {
74 return launcher->iface->open(launcher, path, flags);
75 }
76
77 WL_EXPORT void
weston_launcher_close(struct weston_launcher * launcher,int fd)78 weston_launcher_close(struct weston_launcher *launcher, int fd)
79 {
80 launcher->iface->close(launcher, fd);
81 }
82
83 WL_EXPORT int
weston_launcher_activate_vt(struct weston_launcher * launcher,int vt)84 weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
85 {
86 return launcher->iface->activate_vt(launcher, vt);
87 }
88
89 static void
switch_vt_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)90 switch_vt_binding(struct weston_keyboard *keyboard,
91 const struct timespec *time, uint32_t key, void *data)
92 {
93 struct weston_compositor *compositor = data;
94 struct weston_launcher *launcher = compositor->launcher;
95 int vt = key - KEY_F1 + 1;
96
97 if (vt == launcher->iface->get_vt(launcher))
98 return;
99
100 weston_launcher_activate_vt(launcher, vt);
101 }
102
103 WL_EXPORT void
weston_setup_vt_switch_bindings(struct weston_compositor * compositor)104 weston_setup_vt_switch_bindings(struct weston_compositor *compositor)
105 {
106 uint32_t key;
107 struct weston_launcher *launcher = compositor->launcher;
108
109 if (launcher->iface->get_vt(launcher) <= 0)
110 return;
111
112 if (compositor->vt_switching == false)
113 return;
114
115 for (key = KEY_F1; key < KEY_F9; key++)
116 weston_compositor_add_key_binding(compositor, key,
117 MODIFIER_CTRL | MODIFIER_ALT,
118 switch_vt_binding,
119 compositor);
120 }
121