1 /* RetroArch - A frontend for libretro.
2 * Copyright (C) 2010-2015 - Hans-Kristian Arntzen
3 * Copyright (C) 2011-2017 - Daniel De Matteis
4 *
5 * RetroArch is free software: you can redistribute it and/or modify it under the terms
6 * of the GNU General Public License as published by the Free Software Found-
7 * ation, either version 3 of the License, or (at your option) any later version.
8 *
9 * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with RetroArch.
14 * If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <stdint.h>
18 #include <string.h>
19
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include <limits.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include <wayland-client.h>
29 #include <wayland-cursor.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include "../../config.h"
33 #endif
34
35 #include <file/file_path.h>
36 #include <compat/strl.h>
37 #include <string/stdstring.h>
38 #include <retro_miscellaneous.h>
39
40 #include "../input_keymaps.h"
41
42 #include "../common/linux_common.h"
43 #include "../common/wayland_common.h"
44
45 #include "../../retroarch.h"
46 #include "../../verbosity.h"
47
48 /* TODO/FIXME -
49 * fix game focus toggle */
50
51 /* Forward declaration */
52
53 void flush_wayland_fd(void *data);
54
wayland_context_gettouchpos(gfx_ctx_wayland_data_t * wl,unsigned id,unsigned * touch_x,unsigned * touch_y)55 static bool wayland_context_gettouchpos(
56 gfx_ctx_wayland_data_t *wl,
57 unsigned id,
58 unsigned* touch_x, unsigned* touch_y)
59 {
60 if (id >= MAX_TOUCHES)
61 return false;
62 *touch_x = wl->active_touch_positions[id].x;
63 *touch_y = wl->active_touch_positions[id].y;
64 return wl->active_touch_positions[id].active;
65 }
66
input_wl_poll(void * data)67 static void input_wl_poll(void *data)
68 {
69 int id;
70 unsigned touch_x = 0;
71 unsigned touch_y = 0;
72 input_ctx_wayland_data_t *wl = (input_ctx_wayland_data_t*)data;
73 if (!wl)
74 return;
75
76 flush_wayland_fd(wl);
77
78 wl->mouse.delta_x = wl->mouse.x - wl->mouse.last_x;
79 wl->mouse.delta_y = wl->mouse.y - wl->mouse.last_y;
80 wl->mouse.last_x = wl->mouse.x;
81 wl->mouse.last_y = wl->mouse.y;
82
83 if (!wl->mouse.focus)
84 {
85 wl->mouse.delta_x = 0;
86 wl->mouse.delta_y = 0;
87 }
88
89 for (id = 0; id < MAX_TOUCHES; id++)
90 {
91 if (wayland_context_gettouchpos(wl->gfx, id, &touch_x, &touch_y))
92 wl->touches[id].active = true;
93 else
94 wl->touches[id].active = false;
95 wl->touches[id].x = touch_x;
96 wl->touches[id].y = touch_y;
97 }
98 }
99
input_wl_touch_state(input_ctx_wayland_data_t * wl,unsigned idx,unsigned id,bool screen)100 static int16_t input_wl_touch_state(input_ctx_wayland_data_t *wl,
101 unsigned idx, unsigned id, bool screen)
102 {
103 struct video_viewport vp;
104
105 bool inside = false;
106 int16_t res_x = 0;
107 int16_t res_y = 0;
108 int16_t res_screen_x = 0;
109 int16_t res_screen_y = 0;
110
111 vp.x = 0;
112 vp.y = 0;
113 vp.width = 0;
114 vp.height = 0;
115 vp.full_width = 0;
116 vp.full_height = 0;
117
118 if (idx > MAX_TOUCHES)
119 return 0;
120
121 if (!(video_driver_translate_coord_viewport_wrap(&vp,
122 wl->touches[idx].x, wl->touches[idx].y,
123 &res_x, &res_y, &res_screen_x, &res_screen_y)))
124 return 0;
125
126 if (screen)
127 {
128 res_x = res_screen_x;
129 res_y = res_screen_y;
130 }
131
132 inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
133
134 if (!inside)
135 return 0;
136
137 switch (id)
138 {
139 case RETRO_DEVICE_ID_POINTER_X:
140 return res_x;
141 case RETRO_DEVICE_ID_POINTER_Y:
142 return res_y;
143 case RETRO_DEVICE_ID_POINTER_PRESSED:
144 return wl->touches[idx].active;
145 }
146
147 return 0;
148 }
149
input_wl_state(void * data,const input_device_driver_t * joypad,const input_device_driver_t * sec_joypad,rarch_joypad_info_t * joypad_info,const struct retro_keybind ** binds,bool keyboard_mapping_blocked,unsigned port,unsigned device,unsigned idx,unsigned id)150 static int16_t input_wl_state(
151 void *data,
152 const input_device_driver_t *joypad,
153 const input_device_driver_t *sec_joypad,
154 rarch_joypad_info_t *joypad_info,
155 const struct retro_keybind **binds,
156 bool keyboard_mapping_blocked,
157 unsigned port,
158 unsigned device,
159 unsigned idx,
160 unsigned id)
161 {
162 input_ctx_wayland_data_t *wl = (input_ctx_wayland_data_t*)data;
163
164 switch (device)
165 {
166 case RETRO_DEVICE_JOYPAD:
167 if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
168 {
169 unsigned i;
170 int16_t ret = 0;
171
172 for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
173 {
174 if (binds[port][i].valid)
175 {
176 if (BIT_GET(wl->key_state,
177 rarch_keysym_lut[binds[port][i].key]) )
178 ret |= (1 << i);
179 }
180 }
181
182 return ret;
183 }
184
185 if (id < RARCH_BIND_LIST_END)
186 {
187 if (binds[port][id].valid)
188 if (BIT_GET(wl->key_state,
189 rarch_keysym_lut[binds[port][id].key]))
190 return 1;
191 }
192 break;
193 case RETRO_DEVICE_ANALOG:
194 if (binds[port])
195 {
196 int id_minus_key = 0;
197 int id_plus_key = 0;
198 unsigned id_minus = 0;
199 unsigned id_plus = 0;
200 int16_t ret = 0;
201 bool id_plus_valid = false;
202 bool id_minus_valid = false;
203
204 input_conv_analog_id_to_bind_id(idx, id, id_minus, id_plus);
205
206 id_minus_valid = binds[port][id_minus].valid;
207 id_plus_valid = binds[port][id_plus].valid;
208 id_minus_key = binds[port][id_minus].key;
209 id_plus_key = binds[port][id_plus].key;
210
211 if (id_plus_valid && id_plus_key < RETROK_LAST)
212 {
213 unsigned sym = rarch_keysym_lut[(enum retro_key)id_plus_key];
214 if (BIT_GET(wl->key_state, sym))
215 ret = 0x7fff;
216 }
217 if (id_minus_valid && id_minus_key < RETROK_LAST)
218 {
219 unsigned sym = rarch_keysym_lut[(enum retro_key)id_minus_key];
220 if (BIT_GET(wl->key_state, sym))
221 ret += -0x7fff;
222 }
223
224 return ret;
225 }
226 break;
227 case RETRO_DEVICE_KEYBOARD:
228 return id < RETROK_LAST &&
229 BIT_GET(wl->key_state, rarch_keysym_lut[(enum retro_key)id]);
230 case RETRO_DEVICE_MOUSE:
231 case RARCH_DEVICE_MOUSE_SCREEN:
232 {
233 bool screen = device == RARCH_DEVICE_MOUSE_SCREEN;
234 switch (id)
235 {
236 case RETRO_DEVICE_ID_MOUSE_X:
237 return screen ? wl->mouse.x : wl->mouse.delta_x;
238 case RETRO_DEVICE_ID_MOUSE_Y:
239 return screen ? wl->mouse.y : wl->mouse.delta_y;
240 case RETRO_DEVICE_ID_MOUSE_LEFT:
241 return wl->mouse.left;
242 case RETRO_DEVICE_ID_MOUSE_RIGHT:
243 return wl->mouse.right;
244 case RETRO_DEVICE_ID_MOUSE_MIDDLE:
245 return wl->mouse.middle;
246 /* TODO: Rest of the mouse inputs. */
247 }
248 }
249 break;
250 case RETRO_DEVICE_POINTER:
251 if (idx == 0)
252 {
253 struct video_viewport vp;
254 bool screen =
255 (device == RARCH_DEVICE_POINTER_SCREEN);
256 bool inside = false;
257 int16_t res_x = 0;
258 int16_t res_y = 0;
259 int16_t res_screen_x = 0;
260 int16_t res_screen_y = 0;
261
262 vp.x = 0;
263 vp.y = 0;
264 vp.width = 0;
265 vp.height = 0;
266 vp.full_width = 0;
267 vp.full_height = 0;
268
269 if (video_driver_translate_coord_viewport_wrap(&vp,
270 wl->mouse.x, wl->mouse.y,
271 &res_x, &res_y, &res_screen_x, &res_screen_y))
272 {
273 if (screen)
274 {
275 res_x = res_screen_x;
276 res_y = res_screen_y;
277 }
278
279 inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
280
281 switch (id)
282 {
283 case RETRO_DEVICE_ID_POINTER_X:
284 if (inside)
285 return res_x;
286 break;
287 case RETRO_DEVICE_ID_POINTER_Y:
288 if (inside)
289 return res_y;
290 break;
291 case RETRO_DEVICE_ID_POINTER_PRESSED:
292 return wl->mouse.left;
293 case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
294 return !inside;
295 default:
296 break;
297 }
298 }
299 }
300 break;
301 case RARCH_DEVICE_POINTER_SCREEN:
302 if (idx < MAX_TOUCHES)
303 return input_wl_touch_state(wl, idx, id,
304 device == RARCH_DEVICE_POINTER_SCREEN);
305 break;
306 case RETRO_DEVICE_LIGHTGUN:
307 switch (id)
308 {
309 case RETRO_DEVICE_ID_LIGHTGUN_X:
310 return wl->mouse.delta_x;
311 case RETRO_DEVICE_ID_LIGHTGUN_Y:
312 return wl->mouse.delta_y;
313 case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
314 return wl->mouse.left;
315 case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
316 return wl->mouse.middle;
317 case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
318 return wl->mouse.right;
319 case RETRO_DEVICE_ID_LIGHTGUN_START:
320 return wl->mouse.middle && wl->mouse.right;
321 case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
322 return wl->mouse.middle && wl->mouse.left;
323 }
324 break;
325 }
326
327 return 0;
328 }
329
input_wl_free(void * data)330 static void input_wl_free(void *data) { }
331
input_wl_init(void * data,const char * joypad_name)332 bool input_wl_init(void *data, const char *joypad_name)
333 {
334 input_ctx_wayland_data_t *wl = (input_ctx_wayland_data_t*)data;
335
336 if (!wl)
337 return false;
338
339 input_keymaps_init_keyboard_lut(rarch_key_map_linux);
340
341 return true;
342 }
343
input_wl_get_capabilities(void * data)344 static uint64_t input_wl_get_capabilities(void *data)
345 {
346 return
347 (1 << RETRO_DEVICE_JOYPAD) |
348 (1 << RETRO_DEVICE_ANALOG) |
349 (1 << RETRO_DEVICE_KEYBOARD) |
350 (1 << RETRO_DEVICE_MOUSE) |
351 (1 << RETRO_DEVICE_LIGHTGUN);
352 }
353
354 input_driver_t input_wayland = {
355 NULL,
356 input_wl_poll,
357 input_wl_state,
358 input_wl_free,
359 NULL,
360 NULL,
361 input_wl_get_capabilities,
362 "wayland",
363 NULL, /* grab_mouse */
364 NULL
365 };
366