Debian Codegraph @

xbmc/windowing/X11/WinEventsX11.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "WinEventsX11.h"
10
11 #include "ServiceBroker.h"
12 #include "application/AppInboundProtocol.h"
13 #include "application/Application.h"
14 #include "cores/AudioEngine/Interfaces/AE.h"
15 #include "guilib/GUIComponent.h"
16 #include "guilib/GUIWindowManager.h"
17 #include "input/InputManager.h"
18 #include "input/mouse/MouseStat.h"
19 #include "messaging/ApplicationMessenger.h"
20 #include "utils/CharsetConverter.h"
21 #include "utils/log.h"
22 #include "windowing/WinEvents.h"
23 #include "windowing/X11/WinSystemX11.h"
24
25 #include <stdexcept>
26
27 #include <X11/XF86keysym.h>
28 #include <X11/Xlib.h>
29 #include <X11/extensions/Xrandr.h>
30 #include <X11/keysymdef.h>
31
32 using namespace KODI::WINDOWING::X11;
33
34 static uint32_t SymMappingsX11[][2] =
35 {
36 {XK_BackSpace, XBMCK_BACKSPACE}
37 , {XK_Tab, XBMCK_TAB}
38 , {XK_Clear, XBMCK_CLEAR}
39 , {XK_Return, XBMCK_RETURN}
40 , {XK_Pause, XBMCK_PAUSE}
41 , {XK_Escape, XBMCK_ESCAPE}
42 , {XK_Delete, XBMCK_DELETE}
43 // multi-media keys
44 , {XF86XK_Back, XBMCK_BROWSER_BACK}
45 , {XF86XK_Forward, XBMCK_BROWSER_FORWARD}
46 , {XF86XK_Refresh, XBMCK_BROWSER_REFRESH}
47 , {XF86XK_Stop, XBMCK_BROWSER_STOP}
48 , {XF86XK_Search, XBMCK_BROWSER_SEARCH}
49 , {XF86XK_Favorites, XBMCK_BROWSER_FAVORITES}
50 , {XF86XK_HomePage, XBMCK_BROWSER_HOME}
51 , {XF86XK_AudioMute, XBMCK_VOLUME_MUTE}
52 , {XF86XK_AudioLowerVolume, XBMCK_VOLUME_DOWN}
53 , {XF86XK_AudioRaiseVolume, XBMCK_VOLUME_UP}
54 , {XF86XK_AudioNext, XBMCK_MEDIA_NEXT_TRACK}
55 , {XF86XK_AudioPrev, XBMCK_MEDIA_PREV_TRACK}
56 , {XF86XK_AudioStop, XBMCK_MEDIA_STOP}
57 , {XF86XK_AudioPause, XBMCK_MEDIA_PLAY_PAUSE}
58 , {XF86XK_Mail, XBMCK_LAUNCH_MAIL}
59 , {XF86XK_Select, XBMCK_LAUNCH_MEDIA_SELECT}
60 , {XF86XK_Launch0, XBMCK_LAUNCH_APP1}
61 , {XF86XK_Launch1, XBMCK_LAUNCH_APP2}
62 , {XF86XK_WWW, XBMCK_LAUNCH_FILE_BROWSER}
63 , {XF86XK_AudioMedia, XBMCK_LAUNCH_MEDIA_CENTER }
64 // Numeric keypad
65 , {XK_KP_0, XBMCK_KP0}
66 , {XK_KP_1, XBMCK_KP1}
67 , {XK_KP_2, XBMCK_KP2}
68 , {XK_KP_3, XBMCK_KP3}
69 , {XK_KP_4, XBMCK_KP4}
70 , {XK_KP_5, XBMCK_KP5}
71 , {XK_KP_6, XBMCK_KP6}
72 , {XK_KP_7, XBMCK_KP7}
73 , {XK_KP_8, XBMCK_KP8}
74 , {XK_KP_9, XBMCK_KP9}
75 , {XK_KP_Separator, XBMCK_KP_PERIOD}
76 , {XK_KP_Divide, XBMCK_KP_DIVIDE}
77 , {XK_KP_Multiply, XBMCK_KP_MULTIPLY}
78 , {XK_KP_Subtract, XBMCK_KP_MINUS}
79 , {XK_KP_Add, XBMCK_KP_PLUS}
80 , {XK_KP_Enter, XBMCK_KP_ENTER}
81 , {XK_KP_Equal, XBMCK_KP_EQUALS}
82 // Arrows + Home/End pad
83 , {XK_Up, XBMCK_UP}
84 , {XK_Down, XBMCK_DOWN}
85 , {XK_Right, XBMCK_RIGHT}
86 , {XK_Left, XBMCK_LEFT}
87 , {XK_Insert, XBMCK_INSERT}
88 , {XK_Home, XBMCK_HOME}
89 , {XK_End, XBMCK_END}
90 , {XK_Page_Up, XBMCK_PAGEUP}
91 , {XK_Page_Down, XBMCK_PAGEDOWN}
92 // Function keys
93 , {XK_F1, XBMCK_F1}
94 , {XK_F2, XBMCK_F2}
95 , {XK_F3, XBMCK_F3}
96 , {XK_F4, XBMCK_F4}
97 , {XK_F5, XBMCK_F5}
98 , {XK_F6, XBMCK_F6}
99 , {XK_F7, XBMCK_F7}
100 , {XK_F8, XBMCK_F8}
101 , {XK_F9, XBMCK_F9}
102 , {XK_F10, XBMCK_F10}
103 , {XK_F11, XBMCK_F11}
104 , {XK_F12, XBMCK_F12}
105 , {XK_F13, XBMCK_F13}
106 , {XK_F14, XBMCK_F14}
107 , {XK_F15, XBMCK_F15}
108 // Key state modifier keys
109 , {XK_Num_Lock, XBMCK_NUMLOCK}
110 , {XK_Caps_Lock, XBMCK_CAPSLOCK}
111 , {XK_Scroll_Lock, XBMCK_SCROLLOCK}
112 , {XK_Shift_R, XBMCK_RSHIFT}
113 , {XK_Shift_L, XBMCK_LSHIFT}
114 , {XK_Control_R, XBMCK_RCTRL}
115 , {XK_Control_L, XBMCK_LCTRL}
116 , {XK_Alt_R, XBMCK_RALT}
117 , {XK_Alt_L, XBMCK_LALT}
118 , {XK_Meta_R, XBMCK_RMETA}
119 , {XK_Meta_L, XBMCK_LMETA}
120 , {XK_Super_L, XBMCK_LSUPER}
121 , {XK_Super_R, XBMCK_RSUPER}
122 , {XK_Mode_switch, XBMCK_MODE}
123 , {XK_Multi_key, XBMCK_COMPOSE}
124 // Miscellaneous function keys
125 , {XK_Help, XBMCK_HELP}
126 , {XK_Print, XBMCK_PRINT}
127 //, {0, XBMCK_SYSREQ}
128 , {XK_Break, XBMCK_BREAK}
129 , {XK_Menu, XBMCK_MENU}
130 , {XF86XK_PowerOff, XBMCK_POWER}
131 , {XF86XK_Sleep, XBMCK_SLEEP}
132 , {XK_EcuSign, XBMCK_EURO}
133 , {XK_Undo, XBMCK_UNDO}
134 /* Media keys */
135 , {XF86XK_Eject, XBMCK_EJECT}
136 , {XF86XK_Stop, XBMCK_STOP}
137 , {XF86XK_AudioRecord, XBMCK_RECORD}
138 , {XF86XK_AudioRewind, XBMCK_REWIND}
139 , {XF86XK_Phone, XBMCK_PHONE}
140 , {XF86XK_AudioPlay, XBMCK_PLAY}
141 , {XF86XK_AudioRandomPlay, XBMCK_SHUFFLE}
142 , {XF86XK_AudioForward, XBMCK_FASTFORWARD}
143 };
144
145 CWinEventsX11::CWinEventsX11(CWinSystemX11& winSystem) : m_winSystem(winSystem)
146 {
147 }
148
149 CWinEventsX11::~CWinEventsX11()
150 {
151 Quit();
152 }
153
154 bool CWinEventsX11::Init(Display *dpy, Window win)
155 {
156 if (m_display)
157 return true;
158
159 m_display = dpy;
160 m_window = win;
161 m_keybuf_len = 32*sizeof(char);
162 m_keybuf = (char*)malloc(m_keybuf_len);
163 m_keymodState = 0;
164 m_wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
165 m_structureChanged = false;
166 m_xrrEventPending = false;
167
168 // open input method
169 char *old_locale = NULL, *old_modifiers = NULL;
170 char res_name[8];
171 const char *p;
172
173 // set resource name to xbmc, not used
174 strcpy(res_name, "xbmc");
175
176 // save current locale, this should be "C"
177 p = setlocale(LC_ALL, NULL);
178 if (p)
179 {
180 old_locale = (char*)malloc(strlen(p) +1);
181 strcpy(old_locale, p);
182 }
183 p = XSetLocaleModifiers(NULL);
184 if (p)
185 {
186 old_modifiers = (char*)malloc(strlen(p) +1);
187 strcpy(old_modifiers, p);
188 }
189
190 // set users preferences and open input method
191 p = setlocale(LC_ALL, "");
192 XSetLocaleModifiers("");
193 m_xim = XOpenIM(m_display, NULL, res_name, res_name);
194
195 // restore old locale
196 if (old_locale)
197 {
198 setlocale(LC_ALL, old_locale);
199 free(old_locale);
200 }
201 if (old_modifiers)
202 {
203 XSetLocaleModifiers(old_modifiers);
204 free(old_modifiers);
205 }
206
207 m_xic = NULL;
208 if (m_xim)
209 {
210 m_xic = XCreateIC(m_xim,
211 XNClientWindow, m_window,
212 XNFocusWindow, m_window,
213 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
214 XNResourceName, res_name,
215 XNResourceClass, res_name,
216 nullptr);
217 }
218
219 if (!m_xic)
220 CLog::Log(LOGWARNING,"CWinEventsX11::Init - no input method found");
221
222 // build Keysym lookup table
223 for (const auto& symMapping : SymMappingsX11)
224 {
225 m_symLookupTable[symMapping[0]] = symMapping[1];
226 }
227
228 // register for xrandr events
229 int iReturn;
230 XRRQueryExtension(m_display, &m_RREventBase, &iReturn);
231 int numScreens = XScreenCount(m_display);
232 for (int i = 0; i < numScreens; i++)
233 {
234 XRRSelectInput(m_display, RootWindow(m_display, i), RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | RROutputChangeNotifyMask | RROutputPropertyNotifyMask);
235 }
236
237 return true;
238 }
239
240 void CWinEventsX11::Quit()
241 {
242 free(m_keybuf);
243 m_keybuf = nullptr;
244
245 if (m_xic)
246 {
247 XUnsetICFocus(m_xic);
248 XDestroyIC(m_xic);
249 m_xic = nullptr;
250 }
251
252 if (m_xim)
253 {
254 XCloseIM(m_xim);
255 m_xim = nullptr;
256 }
257
258 m_symLookupTable.clear();
259
260 m_display = nullptr;
261 }
262
263 bool CWinEventsX11::HasStructureChanged()
264 {
265 if (!m_display)
266 return false;
267
268 bool ret = m_structureChanged;
269 m_structureChanged = false;
270 return ret;
271 }
272
273 void CWinEventsX11::SetXRRFailSafeTimer(std::chrono::milliseconds duration)
274 {
275 if (!m_display)
276 return;
277
278 m_xrrFailSafeTimer.Set(duration);
279 m_xrrEventPending = true;
280 }
281
282 bool CWinEventsX11::MessagePump()
283 {
284 if (!m_display)
285 return false;
286
287 bool ret = false;
288 XEvent xevent;
289 unsigned long serial = 0;
290 std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
291
292 while (m_display && XPending(m_display))
293 {
294 memset(&xevent, 0, sizeof (XEvent));
295 XNextEvent(m_display, &xevent);
296
297 if (m_display && (xevent.type == m_RREventBase + RRScreenChangeNotify))
298 {
299 if (xevent.xgeneric.serial == serial)
300 continue;
301
302 if (m_xrrEventPending)
303 {
304 m_winSystem.NotifyXRREvent();
305 m_xrrEventPending = false;
306 serial = xevent.xgeneric.serial;
307 }
308
309 continue;
310 }
311 else if (m_display && (xevent.type == m_RREventBase + RRNotify))
312 {
313 if (xevent.xgeneric.serial == serial)
314 continue;
315
316 XRRNotifyEvent* rrEvent = reinterpret_cast<XRRNotifyEvent*>(&xevent);
317 if (rrEvent->subtype == RRNotify_OutputChange)
318 {
319 XRROutputChangeNotifyEvent* changeEvent = reinterpret_cast<XRROutputChangeNotifyEvent*>(&xevent);
320 if (changeEvent->connection == RR_Connected ||
321 changeEvent->connection == RR_Disconnected)
322 {
323 m_winSystem.NotifyXRREvent();
324 CServiceBroker::GetActiveAE()->DeviceChange();
325 serial = xevent.xgeneric.serial;
326 }
327 }
328
329 continue;
330 }
331
332 if (XFilterEvent(&xevent, None))
333 continue;
334
335 switch (xevent.type)
336 {
337 case MapNotify:
338 {
339 if (appPort)
340 appPort->SetRenderGUI(true);
341 break;
342 }
343
344 case UnmapNotify:
345 {
346 if (appPort)
347 appPort->SetRenderGUI(false);
348 break;
349 }
350
351 case FocusIn:
352 {
353 if (m_xic)
354 XSetICFocus(m_xic);
355 g_application.m_AppFocused = true;
356 m_keymodState = 0;
357 if (serial == xevent.xfocus.serial)
358 break;
359 m_winSystem.NotifyAppFocusChange(g_application.m_AppFocused);
360 break;
361 }
362
363 case FocusOut:
364 {
365 if (m_xic)
366 XUnsetICFocus(m_xic);
367 g_application.m_AppFocused = false;
368 m_winSystem.NotifyAppFocusChange(g_application.m_AppFocused);
369 serial = xevent.xfocus.serial;
370 break;
371 }
372
373 case Expose:
374 {
375 CServiceBroker::GetGUI()->GetWindowManager().MarkDirty();
376 break;
377 }
378
379 case ConfigureNotify:
380 {
381 if (xevent.xconfigure.window != m_window)
382 break;
383
384 m_structureChanged = true;
385 XBMC_Event newEvent = {};
386 newEvent.type = XBMC_VIDEORESIZE;
387 newEvent.resize.width = xevent.xconfigure.width;
388 newEvent.resize.height = xevent.xconfigure.height;
389 newEvent.resize.scale = 1.0;
390 if (appPort)
391 ret |= appPort->OnEvent(newEvent);
392 CServiceBroker::GetGUI()->GetWindowManager().MarkDirty();
393 break;
394 }
395
396 case ClientMessage:
397 {
398 if ((unsigned int)xevent.xclient.data.l[0] == m_wmDeleteMessage)
399 if (!g_application.m_bStop)
400 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_QUIT);
401 break;
402 }
403
404 case KeyPress:
405 {
406 XBMC_Event newEvent = {};
407 newEvent.type = XBMC_KEYDOWN;
408 KeySym xkeysym;
409
410 // fallback if we have no IM
411 if (!m_xic)
412 {
413 static XComposeStatus state;
414 char keybuf[32];
415 XLookupString(&xevent.xkey, NULL, 0, &xkeysym, NULL);
416 newEvent.key.keysym.sym = LookupXbmcKeySym(xkeysym);
417 newEvent.key.keysym.scancode = xevent.xkey.keycode;
418 if (XLookupString(&xevent.xkey, keybuf, sizeof(keybuf), NULL, &state))
419 {
420 newEvent.key.keysym.unicode = keybuf[0];
421 }
422 ret |= ProcessKey(newEvent);
423 break;
424 }
425
426 Status status;
427 int len;
428 len = Xutf8LookupString(m_xic, &xevent.xkey,
429 m_keybuf, m_keybuf_len,
430 &xkeysym, &status);
431 if (status == XBufferOverflow)
432 {
433 m_keybuf_len = len;
434 m_keybuf = (char*)realloc(m_keybuf, m_keybuf_len);
435 if (m_keybuf == nullptr)
436 throw std::runtime_error("Failed to realloc memory, insufficient memory available");
437 len = Xutf8LookupString(m_xic, &xevent.xkey,
438 m_keybuf, m_keybuf_len,
439 &xkeysym, &status);
440 }
441 switch (status)
442 {
443 case XLookupNone:
444 break;
445 case XLookupChars:
446 case XLookupBoth:
447 {
448 std::string data(m_keybuf, len);
449 std::wstring keys;
450 g_charsetConverter.utf8ToW(data, keys, false);
451
452 if (keys.length() == 0)
453 {
454 break;
455 }
456
457 for (unsigned int i = 0; i < keys.length() - 1; i++)
458 {
459 newEvent.key.keysym.sym = XBMCK_UNKNOWN;
460 newEvent.key.keysym.unicode = keys[i];
461 ret |= ProcessKey(newEvent);
462 }
463 if (keys.length() > 0)
464 {
465 newEvent.key.keysym.scancode = xevent.xkey.keycode;
466 XLookupString(&xevent.xkey, NULL, 0, &xkeysym, NULL);
467 newEvent.key.keysym.sym = LookupXbmcKeySym(xkeysym);
468 newEvent.key.keysym.unicode = keys[keys.length() - 1];
469
470 ret |= ProcessKey(newEvent);
471 }
472 break;
473 }
474
475 case XLookupKeySym:
476 {
477 newEvent.key.keysym.scancode = xevent.xkey.keycode;
478 newEvent.key.keysym.sym = LookupXbmcKeySym(xkeysym);
479 ret |= ProcessKey(newEvent);
480 break;
481 }
482
483 }// switch status
484 break;
485 } //KeyPress
486
487 case KeyRelease:
488 {
489 // if we have a queued press directly after, this is a repeat
490 if (XEventsQueued(m_display, QueuedAfterReading))
491 {
492 XEvent next_event;
493 XPeekEvent(m_display, &next_event);
494 if (next_event.type == KeyPress &&
495 next_event.xkey.window == xevent.xkey.window &&
496 next_event.xkey.keycode == xevent.xkey.keycode &&
497 (next_event.xkey.time - xevent.xkey.time < 2))
498 continue;
499 }
500
501 XBMC_Event newEvent = {};
502 KeySym xkeysym;
503 newEvent.type = XBMC_KEYUP;
504 xkeysym = XLookupKeysym(&xevent.xkey, 0);
505 newEvent.key.keysym.scancode = xevent.xkey.keycode;
506 newEvent.key.keysym.sym = LookupXbmcKeySym(xkeysym);
507 ret |= ProcessKey(newEvent);
508 break;
509 }
510
511 case EnterNotify:
512 {
513 break;
514 }
515
516 // lose mouse coverage
517 case LeaveNotify:
518 {
519 CServiceBroker::GetInputManager().SetMouseActive(false);
520 break;
521 }
522
523 case MotionNotify:
524 {
525 if (xevent.xmotion.window != m_window)
526 break;
527 XBMC_Event newEvent = {};
528 newEvent.type = XBMC_MOUSEMOTION;
529 newEvent.motion.x = (int16_t)xevent.xmotion.x;
530 newEvent.motion.y = (int16_t)xevent.xmotion.y;
531 if (appPort)
532 ret |= appPort->OnEvent(newEvent);
533 break;
534 }
535
536 case ButtonPress:
537 {
538 XBMC_Event newEvent = {};
539 newEvent.type = XBMC_MOUSEBUTTONDOWN;
540 newEvent.button.button = (unsigned char)xevent.xbutton.button;
541 newEvent.button.x = (int16_t)xevent.xbutton.x;
542 newEvent.button.y = (int16_t)xevent.xbutton.y;
543 if (appPort)
544 ret |= appPort->OnEvent(newEvent);
545 break;
546 }
547
548 case ButtonRelease:
549 {
550 XBMC_Event newEvent = {};
551 newEvent.type = XBMC_MOUSEBUTTONUP;
552 newEvent.button.button = (unsigned char)xevent.xbutton.button;
553 newEvent.button.x = (int16_t)xevent.xbutton.x;
554 newEvent.button.y = (int16_t)xevent.xbutton.y;
555 if (appPort)
556 ret |= appPort->OnEvent(newEvent);
557 break;
558 }
559
560 default:
561 {
562 break;
563 }
564 }// switch event.type
565 }// while
566
567 if (m_display && m_xrrEventPending && m_xrrFailSafeTimer.IsTimePast())
568 {
569 CLog::Log(LOGERROR,"CWinEventsX11::MessagePump - missed XRR Events");
570 m_winSystem.NotifyXRREvent();
571 m_xrrEventPending = false;
572 }
573
574 return ret;
575 }
576
577 bool CWinEventsX11::ProcessKey(XBMC_Event &event)
578 {
579 if (event.type == XBMC_KEYDOWN)
580 {
581 // check key modifiers
582 switch(event.key.keysym.sym)
583 {
584 case XBMCK_LSHIFT:
585 m_keymodState |= XBMCKMOD_LSHIFT;
586 break;
587 case XBMCK_RSHIFT:
588 m_keymodState |= XBMCKMOD_RSHIFT;
589 break;
590 case XBMCK_LCTRL:
591 m_keymodState |= XBMCKMOD_LCTRL;
592 break;
593 case XBMCK_RCTRL:
594 m_keymodState |= XBMCKMOD_RCTRL;
595 break;
596 case XBMCK_LALT:
597 m_keymodState |= XBMCKMOD_LALT;
598 break;
599 case XBMCK_RALT:
600 m_keymodState |= XBMCKMOD_RCTRL;
601 break;
602 case XBMCK_LMETA:
603 m_keymodState |= XBMCKMOD_LMETA;
604 break;
605 case XBMCK_RMETA:
606 m_keymodState |= XBMCKMOD_RMETA;
607 break;
608 case XBMCK_MODE:
609 m_keymodState |= XBMCKMOD_MODE;
610 break;
611 default:
612 break;
613 }
614 event.key.keysym.mod = (XBMCMod)m_keymodState;
615 }
616 else if (event.type == XBMC_KEYUP)
617 {
618 switch(event.key.keysym.sym)
619 {
620 case XBMCK_LSHIFT:
621 m_keymodState &= ~XBMCKMOD_LSHIFT;
622 break;
623 case XBMCK_RSHIFT:
624 m_keymodState &= ~XBMCKMOD_RSHIFT;
625 break;
626 case XBMCK_LCTRL:
627 m_keymodState &= ~XBMCKMOD_LCTRL;
628 break;
629 case XBMCK_RCTRL:
630 m_keymodState &= ~XBMCKMOD_RCTRL;
631 break;
632 case XBMCK_LALT:
633 m_keymodState &= ~XBMCKMOD_LALT;
634 break;
635 case XBMCK_RALT:
636 m_keymodState &= ~XBMCKMOD_RCTRL;
637 break;
638 case XBMCK_LMETA:
639 m_keymodState &= ~XBMCKMOD_LMETA;
640 break;
641 case XBMCK_RMETA:
642 m_keymodState &= ~XBMCKMOD_RMETA;
643 break;
644 case XBMCK_MODE:
645 m_keymodState &= ~XBMCKMOD_MODE;
646 break;
647 default:
648 break;
649 }
650 event.key.keysym.mod = (XBMCMod)m_keymodState;
651 }
652
653 std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
654 if (appPort)
655 appPort->OnEvent(event);
656 return true;
657 }
658
659 XBMCKey CWinEventsX11::LookupXbmcKeySym(KeySym keysym)
660 {
661 // try direct mapping first
662 std::map<uint32_t, uint32_t>::iterator it;
663 it = m_symLookupTable.find(keysym);
664 if (it != m_symLookupTable.end())
665 {
666 return (XBMCKey)(it->second);
667 }
668
669 // try ascii mappings
670 if (keysym>>8 == 0x00)
671 return (XBMCKey)tolower(keysym & 0xFF);
672
673 return (XBMCKey)keysym;
674 }