RmlUi_Platform_X11.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Platform_X11.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Core.h>
  31. #include <RmlUi/Core/Input.h>
  32. #include <RmlUi/Core/Types.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/extensions/xf86vmode.h>
  35. #include <limits.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <sys/stat.h>
  40. #include <sys/time.h>
  41. #include <sys/types.h>
  42. #include <time.h>
  43. #include <unistd.h>
  44. #ifdef HAS_X11XKBLIB
  45. #include <X11/XKBlib.h>
  46. #endif // HAS_X11XKBLIB
  47. #include <X11/keysym.h>
  48. static Rml::Character GetCharacterCode(Rml::Input::KeyIdentifier key_identifier, int key_modifier_state);
  49. SystemInterface_X11::SystemInterface_X11(Display* display) : display(display)
  50. {
  51. // Create cursors
  52. cursor_default = XCreateFontCursor(display, XC_left_ptr);
  53. cursor_move = XCreateFontCursor(display, XC_fleur);
  54. cursor_pointer = XCreateFontCursor(display, XC_hand1);
  55. cursor_resize = XCreateFontCursor(display, XC_sizing);
  56. cursor_cross = XCreateFontCursor(display, XC_crosshair);
  57. cursor_text = XCreateFontCursor(display, XC_xterm);
  58. cursor_unavailable = XCreateFontCursor(display, XC_X_cursor);
  59. // For copy & paste functions
  60. UTF8_atom = XInternAtom(display, "UTF8_STRING", 1);
  61. XSEL_DATA_atom = XInternAtom(display, "XSEL_DATA", 0);
  62. CLIPBOARD_atom = XInternAtom(display, "CLIPBOARD", 0);
  63. TARGETS_atom = XInternAtom(display, "TARGETS", 0);
  64. TEXT_atom = XInternAtom(display, "TEXT", 0);
  65. gettimeofday(&start_time, nullptr);
  66. }
  67. void SystemInterface_X11::SetWindow(Window in_window)
  68. {
  69. window = in_window;
  70. }
  71. bool SystemInterface_X11::HandleSelectionRequest(const XEvent& ev)
  72. {
  73. if (ev.type == SelectionRequest && XGetSelectionOwner(display, CLIPBOARD_atom) == window && ev.xselectionrequest.selection == CLIPBOARD_atom)
  74. {
  75. XCopy(clipboard_text, ev);
  76. return false;
  77. }
  78. return true;
  79. }
  80. double SystemInterface_X11::GetElapsedTime()
  81. {
  82. struct timeval now;
  83. gettimeofday(&now, nullptr);
  84. double sec = now.tv_sec - start_time.tv_sec;
  85. double usec = now.tv_usec - start_time.tv_usec;
  86. double result = sec + (usec / 1000000.0);
  87. return result;
  88. }
  89. void SystemInterface_X11::SetMouseCursor(const Rml::String& cursor_name)
  90. {
  91. if (display && window)
  92. {
  93. Cursor cursor_handle = 0;
  94. if (cursor_name.empty() || cursor_name == "arrow")
  95. cursor_handle = cursor_default;
  96. else if (cursor_name == "move")
  97. cursor_handle = cursor_move;
  98. else if (cursor_name == "pointer")
  99. cursor_handle = cursor_pointer;
  100. else if (cursor_name == "resize")
  101. cursor_handle = cursor_resize;
  102. else if (cursor_name == "cross")
  103. cursor_handle = cursor_cross;
  104. else if (cursor_name == "text")
  105. cursor_handle = cursor_text;
  106. else if (cursor_name == "unavailable")
  107. cursor_handle = cursor_unavailable;
  108. if (cursor_handle)
  109. {
  110. XDefineCursor(display, window, cursor_handle);
  111. }
  112. }
  113. }
  114. void SystemInterface_X11::SetClipboardText(const Rml::String& text)
  115. {
  116. clipboard_text = text;
  117. XSetSelectionOwner(display, CLIPBOARD_atom, window, 0);
  118. }
  119. void SystemInterface_X11::GetClipboardText(Rml::String& text)
  120. {
  121. if (XGetSelectionOwner(display, CLIPBOARD_atom) != window)
  122. {
  123. if (!UTF8_atom || !XPaste(UTF8_atom, text))
  124. {
  125. // fallback
  126. XPaste(XA_STRING_atom, text);
  127. }
  128. }
  129. else
  130. {
  131. text = clipboard_text;
  132. }
  133. }
  134. void SystemInterface_X11::XCopy(const Rml::String& clipboard_data, const XEvent& event)
  135. {
  136. Atom format = (UTF8_atom ? UTF8_atom : XA_STRING_atom);
  137. XSelectionEvent ev = {
  138. SelectionNotify, // the event type that will be sent to the requestor
  139. 0, // serial
  140. 0, // send_event
  141. event.xselectionrequest.display, event.xselectionrequest.requestor, event.xselectionrequest.selection, event.xselectionrequest.target,
  142. event.xselectionrequest.property,
  143. 0 // time
  144. };
  145. int retval = 0;
  146. if (ev.target == TARGETS_atom)
  147. {
  148. retval = XChangeProperty(ev.display, ev.requestor, ev.property, XA_atom, 32, PropModeReplace, (unsigned char*)&format, 1);
  149. }
  150. else if (ev.target == XA_STRING_atom || ev.target == TEXT_atom)
  151. {
  152. retval = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING_atom, 8, PropModeReplace, (unsigned char*)clipboard_data.c_str(),
  153. clipboard_data.size());
  154. }
  155. else if (ev.target == UTF8_atom)
  156. {
  157. retval = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8_atom, 8, PropModeReplace, (unsigned char*)clipboard_data.c_str(),
  158. clipboard_data.size());
  159. }
  160. else
  161. {
  162. ev.property = 0;
  163. }
  164. if ((retval & 2) == 0)
  165. {
  166. // Notify the requestor that clipboard data is available
  167. XSendEvent(display, ev.requestor, 0, 0, (XEvent*)&ev);
  168. }
  169. }
  170. bool SystemInterface_X11::XPaste(Atom target_atom, Rml::String& clipboard_data)
  171. {
  172. XEvent ev;
  173. // A SelectionRequest event will be sent to the clipboard owner, which should respond with SelectionNotify
  174. XConvertSelection(display, CLIPBOARD_atom, target_atom, XSEL_DATA_atom, window, CurrentTime);
  175. XSync(display, 0);
  176. XNextEvent(display, &ev);
  177. if (ev.type == SelectionNotify)
  178. {
  179. if (ev.xselection.property == 0)
  180. {
  181. // If no owner for the specified selection exists, the X server generates
  182. // a SelectionNotify event with property None (0).
  183. return false;
  184. }
  185. if (ev.xselection.selection == CLIPBOARD_atom)
  186. {
  187. int actual_format;
  188. unsigned long bytes_after, nitems;
  189. char* prop = nullptr;
  190. Atom actual_type;
  191. XGetWindowProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property,
  192. 0L, // offset
  193. (~0L), // length
  194. 0, // delete?
  195. AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char**)&prop);
  196. if (actual_type == UTF8_atom || actual_type == XA_STRING_atom)
  197. {
  198. clipboard_data = Rml::String(prop, prop + nitems);
  199. XFree(prop);
  200. }
  201. XDeleteProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property);
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. bool RmlX11::HandleInputEvent(Rml::Context* context, Display* display, const XEvent& ev)
  208. {
  209. switch (ev.type)
  210. {
  211. case ButtonPress:
  212. {
  213. switch (ev.xbutton.button)
  214. {
  215. case Button1:
  216. case Button2:
  217. case Button3:
  218. return context->ProcessMouseButtonDown(ConvertMouseButton(ev.xbutton.button), RmlX11::GetKeyModifierState(ev.xbutton.state));
  219. case Button4:
  220. return context->ProcessMouseWheel(-1, RmlX11::GetKeyModifierState(ev.xbutton.state));
  221. case Button5:
  222. return context->ProcessMouseWheel(1, RmlX11::GetKeyModifierState(ev.xbutton.state));
  223. default:
  224. return true;
  225. }
  226. }
  227. break;
  228. case ButtonRelease:
  229. {
  230. switch (ev.xbutton.button)
  231. {
  232. case Button1:
  233. case Button2:
  234. case Button3:
  235. return context->ProcessMouseButtonUp(ConvertMouseButton(ev.xbutton.button), RmlX11::GetKeyModifierState(ev.xbutton.state));
  236. default:
  237. return true;
  238. }
  239. }
  240. break;
  241. case MotionNotify:
  242. {
  243. return context->ProcessMouseMove(ev.xmotion.x, ev.xmotion.y, RmlX11::GetKeyModifierState(ev.xmotion.state));
  244. }
  245. break;
  246. case LeaveNotify:
  247. {
  248. return context->ProcessMouseLeave();
  249. }
  250. break;
  251. case KeyPress:
  252. {
  253. Rml::Input::KeyIdentifier key_identifier = RmlX11::ConvertKey(display, ev.xkey.keycode);
  254. const int key_modifier_state = RmlX11::GetKeyModifierState(ev.xkey.state);
  255. bool propagates = true;
  256. if (key_identifier != Rml::Input::KI_UNKNOWN)
  257. propagates = context->ProcessKeyDown(key_identifier, key_modifier_state);
  258. Rml::Character character = GetCharacterCode(key_identifier, key_modifier_state);
  259. if (character != Rml::Character::Null && !(key_modifier_state & Rml::Input::KM_CTRL))
  260. propagates &= context->ProcessTextInput(character);
  261. return propagates;
  262. }
  263. break;
  264. case KeyRelease:
  265. {
  266. Rml::Input::KeyIdentifier key_identifier = RmlX11::ConvertKey(display, ev.xkey.keycode);
  267. const int key_modifier_state = RmlX11::GetKeyModifierState(ev.xkey.state);
  268. bool propagates = true;
  269. if (key_identifier != Rml::Input::KI_UNKNOWN)
  270. propagates = context->ProcessKeyUp(key_identifier, key_modifier_state);
  271. return propagates;
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. return true;
  278. }
  279. int RmlX11::GetKeyModifierState(int x11_state)
  280. {
  281. int key_modifier_state = 0;
  282. if (x11_state & ShiftMask)
  283. key_modifier_state |= Rml::Input::KM_SHIFT;
  284. if (x11_state & LockMask)
  285. key_modifier_state |= Rml::Input::KM_CAPSLOCK;
  286. if (x11_state & ControlMask)
  287. key_modifier_state |= Rml::Input::KM_CTRL;
  288. if (x11_state & Mod5Mask)
  289. key_modifier_state |= Rml::Input::KM_ALT;
  290. if (x11_state & Mod2Mask)
  291. key_modifier_state |= Rml::Input::KM_NUMLOCK;
  292. return key_modifier_state;
  293. }
  294. /**
  295. X11 Key data used for key code conversion.
  296. */
  297. struct XKeyData {
  298. #ifdef HAS_X11XKBLIB
  299. bool has_xkblib = false;
  300. #endif // HAS_X11XKBLIB
  301. bool initialized = false;
  302. int min_keycode = 0, max_keycode = 0, keysyms_per_keycode = 0;
  303. KeySym* x11_key_mapping = nullptr;
  304. };
  305. // Get the key data, which is initialized and filled on the first fetch.
  306. static const XKeyData& GetXKeyData(Display* display)
  307. {
  308. RMLUI_ASSERT(display);
  309. static XKeyData data;
  310. if (!data.initialized)
  311. {
  312. data.initialized = true;
  313. #ifdef HAS_X11XKBLIB
  314. int opcode_rtrn = -1;
  315. int event_rtrn = -1;
  316. int error_rtrn = -1;
  317. int major_in_out = -1;
  318. int minor_in_out = -1;
  319. // Xkb extension may not exist in the server. This checks for its existence and initializes the extension if available.
  320. data.has_xkblib = XkbQueryExtension(display, &opcode_rtrn, &event_rtrn, &error_rtrn, &major_in_out, &minor_in_out);
  321. // if Xkb isn't available, fall back to using XGetKeyboardMapping, which may occur if RmlUi is compiled with Xkb support but the server
  322. // doesn't support it. This occurs with older X11 servers or virtual framebuffers such as x11vnc server.
  323. if (!data.has_xkblib)
  324. #endif // HAS_X11XKBLIB
  325. {
  326. XDisplayKeycodes(display, &data.min_keycode, &data.max_keycode);
  327. data.x11_key_mapping = XGetKeyboardMapping(display, data.min_keycode, data.max_keycode + 1 - data.min_keycode, &data.keysyms_per_keycode);
  328. }
  329. }
  330. return data;
  331. }
  332. Rml::Input::KeyIdentifier RmlX11::ConvertKey(Display* display, unsigned int x11_key_code)
  333. {
  334. RMLUI_ASSERT(display);
  335. const XKeyData& key_data = GetXKeyData(display);
  336. const int group_index = 0; // this is always 0 for our limited example
  337. KeySym sym = {};
  338. #ifdef HAS_X11XKBLIB
  339. if (key_data.has_xkblib)
  340. {
  341. sym = XkbKeycodeToKeysym(display, x11_key_code, 0, group_index);
  342. }
  343. else
  344. #endif // HAS_X11XKBLIB
  345. {
  346. KeySym sym_full = key_data.x11_key_mapping[(x11_key_code - key_data.min_keycode) * key_data.keysyms_per_keycode + group_index];
  347. KeySym lower_sym, upper_sym;
  348. XConvertCase(sym_full, &lower_sym, &upper_sym);
  349. sym = lower_sym;
  350. }
  351. // clang-format off
  352. switch (sym & 0xFF)
  353. {
  354. case XK_BackSpace & 0xFF: return Rml::Input::KI_BACK;
  355. case XK_Tab & 0xFF: return Rml::Input::KI_TAB;
  356. case XK_Clear & 0xFF: return Rml::Input::KI_CLEAR;
  357. case XK_Return & 0xFF: return Rml::Input::KI_RETURN;
  358. case XK_Pause & 0xFF: return Rml::Input::KI_PAUSE;
  359. case XK_Scroll_Lock & 0xFF: return Rml::Input::KI_SCROLL;
  360. case XK_Escape & 0xFF: return Rml::Input::KI_ESCAPE;
  361. case XK_Delete & 0xFF: return Rml::Input::KI_DELETE;
  362. case XK_Kanji & 0xFF: return Rml::Input::KI_KANJI;
  363. // case XK_Muhenkan & 0xFF: return Rml::Input::; /* Cancel Conversion */
  364. // case XK_Henkan_Mode & 0xFF: return Rml::Input::; /* Start/Stop Conversion */
  365. // case XK_Henkan & 0xFF: return Rml::Input::; /* Alias for Henkan_Mode */
  366. // case XK_Romaji & 0xFF: return Rml::Input::; /* to Romaji */
  367. // case XK_Hiragana & 0xFF: return Rml::Input::; /* to Hiragana */
  368. // case XK_Katakana & 0xFF: return Rml::Input::; /* to Katakana */
  369. // case XK_Hiragana_Katakana & 0xFF: return Rml::Input::; /* Hiragana/Katakana toggle */
  370. // case XK_Zenkaku & 0xFF: return Rml::Input::; /* to Zenkaku */
  371. // case XK_Hankaku & 0xFF: return Rml::Input::; /* to Hankaku */
  372. // case XK_Zenkaku_Hankaku & 0xFF: return Rml::Input::; /* Zenkaku/Hankaku toggle */
  373. case XK_Touroku & 0xFF: return Rml::Input::KI_OEM_FJ_TOUROKU;
  374. // case XK_Massyo & 0xFF: return Rml::Input::KI_OEM_FJ_MASSHOU;
  375. // case XK_Kana_Lock & 0xFF: return Rml::Input::; /* Kana Lock */
  376. // case XK_Kana_Shift & 0xFF: return Rml::Input::; /* Kana Shift */
  377. // case XK_Eisu_Shift & 0xFF: return Rml::Input::; /* Alphanumeric Shift */
  378. // case XK_Eisu_toggle & 0xFF: return Rml::Input::; /* Alphanumeric toggle */
  379. case XK_Home & 0xFF: return Rml::Input::KI_HOME;
  380. case XK_Left & 0xFF: return Rml::Input::KI_LEFT;
  381. case XK_Up & 0xFF: return Rml::Input::KI_UP;
  382. case XK_Right & 0xFF: return Rml::Input::KI_RIGHT;
  383. case XK_Down & 0xFF: return Rml::Input::KI_DOWN;
  384. case XK_Prior & 0xFF: return Rml::Input::KI_PRIOR;
  385. case XK_Next & 0xFF: return Rml::Input::KI_NEXT;
  386. case XK_End & 0xFF: return Rml::Input::KI_END;
  387. case XK_Begin & 0xFF: return Rml::Input::KI_HOME;
  388. // case XK_Print & 0xFF: return Rml::Input::KI_SNAPSHOT;
  389. // case XK_Insert & 0xFF: return Rml::Input::KI_INSERT;
  390. case XK_Num_Lock & 0xFF: return Rml::Input::KI_NUMLOCK;
  391. case XK_KP_Space & 0xFF: return Rml::Input::KI_SPACE;
  392. case XK_KP_Tab & 0xFF: return Rml::Input::KI_TAB;
  393. case XK_KP_Enter & 0xFF: return Rml::Input::KI_NUMPADENTER;
  394. case XK_KP_F1 & 0xFF: return Rml::Input::KI_F1;
  395. case XK_KP_F2 & 0xFF: return Rml::Input::KI_F2;
  396. case XK_KP_F3 & 0xFF: return Rml::Input::KI_F3;
  397. case XK_KP_F4 & 0xFF: return Rml::Input::KI_F4;
  398. case XK_KP_Home & 0xFF: return Rml::Input::KI_NUMPAD7;
  399. case XK_KP_Left & 0xFF: return Rml::Input::KI_NUMPAD4;
  400. case XK_KP_Up & 0xFF: return Rml::Input::KI_NUMPAD8;
  401. case XK_KP_Right & 0xFF: return Rml::Input::KI_NUMPAD6;
  402. case XK_KP_Down & 0xFF: return Rml::Input::KI_NUMPAD2;
  403. case XK_KP_Prior & 0xFF: return Rml::Input::KI_NUMPAD9;
  404. case XK_KP_Next & 0xFF: return Rml::Input::KI_NUMPAD3;
  405. case XK_KP_End & 0xFF: return Rml::Input::KI_NUMPAD1;
  406. case XK_KP_Begin & 0xFF: return Rml::Input::KI_NUMPAD5;
  407. case XK_KP_Insert & 0xFF: return Rml::Input::KI_NUMPAD0;
  408. case XK_KP_Delete & 0xFF: return Rml::Input::KI_DECIMAL;
  409. case XK_KP_Equal & 0xFF: return Rml::Input::KI_OEM_NEC_EQUAL;
  410. case XK_KP_Multiply & 0xFF: return Rml::Input::KI_MULTIPLY;
  411. case XK_KP_Add & 0xFF: return Rml::Input::KI_ADD;
  412. case XK_KP_Separator & 0xFF: return Rml::Input::KI_SEPARATOR;
  413. case XK_KP_Subtract & 0xFF: return Rml::Input::KI_SUBTRACT;
  414. case XK_KP_Decimal & 0xFF: return Rml::Input::KI_DECIMAL;
  415. case XK_KP_Divide & 0xFF: return Rml::Input::KI_DIVIDE;
  416. case XK_F1 & 0xFF: return Rml::Input::KI_F1;
  417. case XK_F2 & 0xFF: return Rml::Input::KI_F2;
  418. case XK_F3 & 0xFF: return Rml::Input::KI_F3;
  419. case XK_F4 & 0xFF: return Rml::Input::KI_F4;
  420. case XK_F5 & 0xFF: return Rml::Input::KI_F5;
  421. case XK_F6 & 0xFF: return Rml::Input::KI_F6;
  422. case XK_F7 & 0xFF: return Rml::Input::KI_F7;
  423. case XK_F8 & 0xFF: return Rml::Input::KI_F8;
  424. case XK_F9 & 0xFF: return Rml::Input::KI_F9;
  425. case XK_F10 & 0xFF: return Rml::Input::KI_F10;
  426. case XK_F11 & 0xFF: return Rml::Input::KI_F11;
  427. case XK_F12 & 0xFF: return Rml::Input::KI_F12;
  428. case XK_F13 & 0xFF: return Rml::Input::KI_F13;
  429. case XK_F14 & 0xFF: return Rml::Input::KI_F14;
  430. case XK_F15 & 0xFF: return Rml::Input::KI_F15;
  431. case XK_F16 & 0xFF: return Rml::Input::KI_F16;
  432. case XK_F17 & 0xFF: return Rml::Input::KI_F17;
  433. case XK_F18 & 0xFF: return Rml::Input::KI_F18;
  434. case XK_F19 & 0xFF: return Rml::Input::KI_F19;
  435. case XK_F20 & 0xFF: return Rml::Input::KI_F20;
  436. case XK_F21 & 0xFF: return Rml::Input::KI_F21;
  437. case XK_F22 & 0xFF: return Rml::Input::KI_F22;
  438. case XK_F23 & 0xFF: return Rml::Input::KI_F23;
  439. case XK_F24 & 0xFF: return Rml::Input::KI_F24;
  440. case XK_Shift_L & 0xFF: return Rml::Input::KI_LSHIFT;
  441. case XK_Shift_R & 0xFF: return Rml::Input::KI_RSHIFT;
  442. case XK_Control_L & 0xFF: return Rml::Input::KI_LCONTROL;
  443. case XK_Control_R & 0xFF: return Rml::Input::KI_RCONTROL;
  444. case XK_Caps_Lock & 0xFF: return Rml::Input::KI_CAPITAL;
  445. case XK_Alt_L & 0xFF: return Rml::Input::KI_LMENU;
  446. case XK_Alt_R & 0xFF: return Rml::Input::KI_RMENU;
  447. case XK_space & 0xFF: return Rml::Input::KI_SPACE;
  448. case XK_apostrophe & 0xFF: return Rml::Input::KI_OEM_7;
  449. case XK_comma & 0xFF: return Rml::Input::KI_OEM_COMMA;
  450. case XK_minus & 0xFF: return Rml::Input::KI_OEM_MINUS;
  451. case XK_period & 0xFF: return Rml::Input::KI_OEM_PERIOD;
  452. case XK_slash & 0xFF: return Rml::Input::KI_OEM_2;
  453. case XK_0 & 0xFF: return Rml::Input::KI_0;
  454. case XK_1 & 0xFF: return Rml::Input::KI_1;
  455. case XK_2 & 0xFF: return Rml::Input::KI_2;
  456. case XK_3 & 0xFF: return Rml::Input::KI_3;
  457. case XK_4 & 0xFF: return Rml::Input::KI_4;
  458. case XK_5 & 0xFF: return Rml::Input::KI_5;
  459. case XK_6 & 0xFF: return Rml::Input::KI_6;
  460. case XK_7 & 0xFF: return Rml::Input::KI_7;
  461. case XK_8 & 0xFF: return Rml::Input::KI_8;
  462. case XK_9 & 0xFF: return Rml::Input::KI_9;
  463. case XK_semicolon & 0xFF: return Rml::Input::KI_OEM_1;
  464. case XK_equal & 0xFF: return Rml::Input::KI_OEM_PLUS;
  465. case XK_bracketleft & 0xFF: return Rml::Input::KI_OEM_4;
  466. case XK_backslash & 0xFF: return Rml::Input::KI_OEM_5;
  467. case XK_bracketright & 0xFF: return Rml::Input::KI_OEM_6;
  468. case XK_grave & 0xFF: return Rml::Input::KI_OEM_3;
  469. case XK_a & 0xFF: return Rml::Input::KI_A;
  470. case XK_b & 0xFF: return Rml::Input::KI_B;
  471. case XK_c & 0xFF: return Rml::Input::KI_C;
  472. case XK_d & 0xFF: return Rml::Input::KI_D;
  473. case XK_e & 0xFF: return Rml::Input::KI_E;
  474. case XK_f & 0xFF: return Rml::Input::KI_F;
  475. case XK_g & 0xFF: return Rml::Input::KI_G;
  476. case XK_h & 0xFF: return Rml::Input::KI_H;
  477. case XK_i & 0xFF: return Rml::Input::KI_I;
  478. case XK_j & 0xFF: return Rml::Input::KI_J;
  479. case XK_k & 0xFF: return Rml::Input::KI_K;
  480. case XK_l & 0xFF: return Rml::Input::KI_L;
  481. case XK_m & 0xFF: return Rml::Input::KI_M;
  482. case XK_n & 0xFF: return Rml::Input::KI_N;
  483. case XK_o & 0xFF: return Rml::Input::KI_O;
  484. case XK_p & 0xFF: return Rml::Input::KI_P;
  485. case XK_q & 0xFF: return Rml::Input::KI_Q;
  486. case XK_r & 0xFF: return Rml::Input::KI_R;
  487. case XK_s & 0xFF: return Rml::Input::KI_S;
  488. case XK_t & 0xFF: return Rml::Input::KI_T;
  489. case XK_u & 0xFF: return Rml::Input::KI_U;
  490. case XK_v & 0xFF: return Rml::Input::KI_V;
  491. case XK_w & 0xFF: return Rml::Input::KI_W;
  492. case XK_x & 0xFF: return Rml::Input::KI_X;
  493. case XK_y & 0xFF: return Rml::Input::KI_Y;
  494. case XK_z & 0xFF: return Rml::Input::KI_Z;
  495. default: break;
  496. }
  497. // clang-format on
  498. return Rml::Input::KI_UNKNOWN;
  499. }
  500. int RmlX11::ConvertMouseButton(unsigned int x11_mouse_button)
  501. {
  502. switch (x11_mouse_button)
  503. {
  504. case Button1:
  505. return 0;
  506. case Button2:
  507. return 2;
  508. case Button3:
  509. return 1;
  510. default:
  511. break;
  512. }
  513. return 0;
  514. }
  515. /**
  516. This map contains 4 different mappings from key identifiers to character codes. Each entry represents a different
  517. combination of shift and capslock state.
  518. */
  519. static char ascii_map[4][51] = {
  520. // shift off and capslock off
  521. {0, ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  522. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ';', '=', ',', '-', '.', '/', '`', '[', '\\', ']', '\'', 0, 0},
  523. // shift on and capslock off
  524. {0, ' ', ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  525. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ':', '+', '<', '_', '>', '?', '~', '{', '|', '}', '"', 0, 0},
  526. // shift on and capslock on
  527. {0, ' ', ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  528. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ':', '+', '<', '_', '>', '?', '~', '{', '|', '}', '"', 0, 0},
  529. // shift off and capslock on
  530. {0, ' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  531. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ';', '=', ',', '-', '.', '/', '`', '[', '\\', ']', '\'', 0, 0}};
  532. static char keypad_map[2][18] = {{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\n', '*', '+', 0, '-', '.', '/', '='},
  533. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\n', '*', '+', 0, '-', 0, '/', '='}};
  534. // Returns the character code for a key identifer / key modifier combination.
  535. static Rml::Character GetCharacterCode(Rml::Input::KeyIdentifier key_identifier, int key_modifier_state)
  536. {
  537. using Rml::Character;
  538. // Check if we have a keycode capable of generating characters on the main keyboard (ie, not on the numeric
  539. // keypad; that is dealt with below).
  540. if (key_identifier <= Rml::Input::KI_OEM_102)
  541. {
  542. // Get modifier states
  543. bool shift = (key_modifier_state & Rml::Input::KM_SHIFT) > 0;
  544. bool capslock = (key_modifier_state & Rml::Input::KM_CAPSLOCK) > 0;
  545. // Return character code based on identifier and modifiers
  546. if (shift && !capslock)
  547. return (Character)ascii_map[1][key_identifier];
  548. if (shift && capslock)
  549. return (Character)ascii_map[2][key_identifier];
  550. if (!shift && capslock)
  551. return (Character)ascii_map[3][key_identifier];
  552. return (Character)ascii_map[0][key_identifier];
  553. }
  554. // Check if we have a keycode from the numeric keypad.
  555. else if (key_identifier <= Rml::Input::KI_OEM_NEC_EQUAL)
  556. {
  557. if (key_modifier_state & Rml::Input::KM_NUMLOCK)
  558. return (Character)keypad_map[0][key_identifier - Rml::Input::KI_NUMPAD0];
  559. else
  560. return (Character)keypad_map[1][key_identifier - Rml::Input::KI_NUMPAD0];
  561. }
  562. else if (key_identifier == Rml::Input::KI_RETURN)
  563. return (Character)'\n';
  564. return Character::Null;
  565. }