RmlUi_Platform_X11.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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-2023 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 == "rmlui-scroll-idle")
  107. cursor_handle = cursor_move;
  108. else if (cursor_name == "rmlui-scroll-up")
  109. cursor_handle = cursor_move;
  110. else if (cursor_name == "rmlui-scroll-down")
  111. cursor_handle = cursor_move;
  112. else if (cursor_name == "unavailable")
  113. cursor_handle = cursor_unavailable;
  114. if (cursor_handle)
  115. {
  116. XDefineCursor(display, window, cursor_handle);
  117. }
  118. }
  119. }
  120. void SystemInterface_X11::SetClipboardText(const Rml::String& text)
  121. {
  122. clipboard_text = text;
  123. XSetSelectionOwner(display, CLIPBOARD_atom, window, 0);
  124. }
  125. void SystemInterface_X11::GetClipboardText(Rml::String& text)
  126. {
  127. if (XGetSelectionOwner(display, CLIPBOARD_atom) != window)
  128. {
  129. if (!UTF8_atom || !XPaste(UTF8_atom, text))
  130. {
  131. // fallback
  132. XPaste(XA_STRING_atom, text);
  133. }
  134. }
  135. else
  136. {
  137. text = clipboard_text;
  138. }
  139. }
  140. void SystemInterface_X11::XCopy(const Rml::String& clipboard_data, const XEvent& event)
  141. {
  142. Atom format = (UTF8_atom ? UTF8_atom : XA_STRING_atom);
  143. XSelectionEvent ev = {
  144. SelectionNotify, // the event type that will be sent to the requestor
  145. 0, // serial
  146. 0, // send_event
  147. event.xselectionrequest.display, event.xselectionrequest.requestor, event.xselectionrequest.selection, event.xselectionrequest.target,
  148. event.xselectionrequest.property,
  149. 0 // time
  150. };
  151. int retval = 0;
  152. if (ev.target == TARGETS_atom)
  153. {
  154. retval = XChangeProperty(ev.display, ev.requestor, ev.property, XA_atom, 32, PropModeReplace, (unsigned char*)&format, 1);
  155. }
  156. else if (ev.target == XA_STRING_atom || ev.target == TEXT_atom)
  157. {
  158. retval = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING_atom, 8, PropModeReplace, (unsigned char*)clipboard_data.c_str(),
  159. clipboard_data.size());
  160. }
  161. else if (ev.target == UTF8_atom)
  162. {
  163. retval = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8_atom, 8, PropModeReplace, (unsigned char*)clipboard_data.c_str(),
  164. clipboard_data.size());
  165. }
  166. else
  167. {
  168. ev.property = 0;
  169. }
  170. if ((retval & 2) == 0)
  171. {
  172. // Notify the requestor that clipboard data is available
  173. XSendEvent(display, ev.requestor, 0, 0, (XEvent*)&ev);
  174. }
  175. }
  176. bool SystemInterface_X11::XPaste(Atom target_atom, Rml::String& clipboard_data)
  177. {
  178. XEvent ev;
  179. // A SelectionRequest event will be sent to the clipboard owner, which should respond with SelectionNotify
  180. XConvertSelection(display, CLIPBOARD_atom, target_atom, XSEL_DATA_atom, window, CurrentTime);
  181. XSync(display, 0);
  182. XNextEvent(display, &ev);
  183. if (ev.type == SelectionNotify)
  184. {
  185. if (ev.xselection.property == 0)
  186. {
  187. // If no owner for the specified selection exists, the X server generates
  188. // a SelectionNotify event with property None (0).
  189. return false;
  190. }
  191. if (ev.xselection.selection == CLIPBOARD_atom)
  192. {
  193. int actual_format;
  194. unsigned long bytes_after, nitems;
  195. char* prop = nullptr;
  196. Atom actual_type;
  197. XGetWindowProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property,
  198. 0L, // offset
  199. (~0L), // length
  200. 0, // delete?
  201. AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char**)&prop);
  202. if (actual_type == UTF8_atom || actual_type == XA_STRING_atom)
  203. {
  204. clipboard_data = Rml::String(prop, prop + nitems);
  205. XFree(prop);
  206. }
  207. XDeleteProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property);
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. bool RmlX11::HandleInputEvent(Rml::Context* context, Display* display, const XEvent& ev)
  214. {
  215. switch (ev.type)
  216. {
  217. case ButtonPress:
  218. {
  219. switch (ev.xbutton.button)
  220. {
  221. case Button1:
  222. case Button2:
  223. case Button3: return context->ProcessMouseButtonDown(ConvertMouseButton(ev.xbutton.button), RmlX11::GetKeyModifierState(ev.xbutton.state));
  224. case Button4: return context->ProcessMouseWheel(-1, RmlX11::GetKeyModifierState(ev.xbutton.state));
  225. case Button5: return context->ProcessMouseWheel(1, RmlX11::GetKeyModifierState(ev.xbutton.state));
  226. default: return true;
  227. }
  228. }
  229. break;
  230. case ButtonRelease:
  231. {
  232. switch (ev.xbutton.button)
  233. {
  234. case Button1:
  235. case Button2:
  236. case Button3: return context->ProcessMouseButtonUp(ConvertMouseButton(ev.xbutton.button), RmlX11::GetKeyModifierState(ev.xbutton.state));
  237. default: 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: break;
  275. }
  276. return true;
  277. }
  278. int RmlX11::GetKeyModifierState(int x11_state)
  279. {
  280. int key_modifier_state = 0;
  281. if (x11_state & ShiftMask)
  282. key_modifier_state |= Rml::Input::KM_SHIFT;
  283. if (x11_state & LockMask)
  284. key_modifier_state |= Rml::Input::KM_CAPSLOCK;
  285. if (x11_state & ControlMask)
  286. key_modifier_state |= Rml::Input::KM_CTRL;
  287. if (x11_state & Mod5Mask)
  288. key_modifier_state |= Rml::Input::KM_ALT;
  289. if (x11_state & Mod2Mask)
  290. key_modifier_state |= Rml::Input::KM_NUMLOCK;
  291. return key_modifier_state;
  292. }
  293. /**
  294. X11 Key data used for key code conversion.
  295. */
  296. struct XKeyData {
  297. #ifdef HAS_X11XKBLIB
  298. bool has_xkblib = false;
  299. #endif // HAS_X11XKBLIB
  300. bool initialized = false;
  301. int min_keycode = 0, max_keycode = 0, keysyms_per_keycode = 0;
  302. KeySym* x11_key_mapping = nullptr;
  303. };
  304. // Get the key data, which is initialized and filled on the first fetch.
  305. static const XKeyData& GetXKeyData(Display* display)
  306. {
  307. RMLUI_ASSERT(display);
  308. static XKeyData data;
  309. if (!data.initialized)
  310. {
  311. data.initialized = true;
  312. #ifdef HAS_X11XKBLIB
  313. int opcode_rtrn = -1;
  314. int event_rtrn = -1;
  315. int error_rtrn = -1;
  316. int major_in_out = -1;
  317. int minor_in_out = -1;
  318. // Xkb extension may not exist in the server. This checks for its existence and initializes the extension if available.
  319. data.has_xkblib = XkbQueryExtension(display, &opcode_rtrn, &event_rtrn, &error_rtrn, &major_in_out, &minor_in_out);
  320. // if Xkb isn't available, fall back to using XGetKeyboardMapping, which may occur if RmlUi is compiled with Xkb support but the server
  321. // doesn't support it. This occurs with older X11 servers or virtual framebuffers such as x11vnc server.
  322. if (!data.has_xkblib)
  323. #endif // HAS_X11XKBLIB
  324. {
  325. XDisplayKeycodes(display, &data.min_keycode, &data.max_keycode);
  326. data.x11_key_mapping = XGetKeyboardMapping(display, data.min_keycode, data.max_keycode + 1 - data.min_keycode, &data.keysyms_per_keycode);
  327. }
  328. }
  329. return data;
  330. }
  331. Rml::Input::KeyIdentifier RmlX11::ConvertKey(Display* display, unsigned int x11_key_code)
  332. {
  333. RMLUI_ASSERT(display);
  334. const XKeyData& key_data = GetXKeyData(display);
  335. const int group_index = 0; // this is always 0 for our limited example
  336. KeySym sym = {};
  337. #ifdef HAS_X11XKBLIB
  338. if (key_data.has_xkblib)
  339. {
  340. sym = XkbKeycodeToKeysym(display, x11_key_code, 0, group_index);
  341. }
  342. else
  343. #endif // HAS_X11XKBLIB
  344. {
  345. KeySym sym_full = key_data.x11_key_mapping[(x11_key_code - key_data.min_keycode) * key_data.keysyms_per_keycode + group_index];
  346. KeySym lower_sym, upper_sym;
  347. XConvertCase(sym_full, &lower_sym, &upper_sym);
  348. sym = lower_sym;
  349. }
  350. // clang-format off
  351. switch (sym & 0xFF)
  352. {
  353. case XK_BackSpace & 0xFF: return Rml::Input::KI_BACK;
  354. case XK_Tab & 0xFF: return Rml::Input::KI_TAB;
  355. case XK_Clear & 0xFF: return Rml::Input::KI_CLEAR;
  356. case XK_Return & 0xFF: return Rml::Input::KI_RETURN;
  357. case XK_Pause & 0xFF: return Rml::Input::KI_PAUSE;
  358. case XK_Scroll_Lock & 0xFF: return Rml::Input::KI_SCROLL;
  359. case XK_Escape & 0xFF: return Rml::Input::KI_ESCAPE;
  360. case XK_Delete & 0xFF: return Rml::Input::KI_DELETE;
  361. case XK_Kanji & 0xFF: return Rml::Input::KI_KANJI;
  362. // case XK_Muhenkan & 0xFF: return Rml::Input::; /* Cancel Conversion */
  363. // case XK_Henkan_Mode & 0xFF: return Rml::Input::; /* Start/Stop Conversion */
  364. // case XK_Henkan & 0xFF: return Rml::Input::; /* Alias for Henkan_Mode */
  365. // case XK_Romaji & 0xFF: return Rml::Input::; /* to Romaji */
  366. // case XK_Hiragana & 0xFF: return Rml::Input::; /* to Hiragana */
  367. // case XK_Katakana & 0xFF: return Rml::Input::; /* to Katakana */
  368. // case XK_Hiragana_Katakana & 0xFF: return Rml::Input::; /* Hiragana/Katakana toggle */
  369. // case XK_Zenkaku & 0xFF: return Rml::Input::; /* to Zenkaku */
  370. // case XK_Hankaku & 0xFF: return Rml::Input::; /* to Hankaku */
  371. // case XK_Zenkaku_Hankaku & 0xFF: return Rml::Input::; /* Zenkaku/Hankaku toggle */
  372. case XK_Touroku & 0xFF: return Rml::Input::KI_OEM_FJ_TOUROKU;
  373. // case XK_Massyo & 0xFF: return Rml::Input::KI_OEM_FJ_MASSHOU;
  374. // case XK_Kana_Lock & 0xFF: return Rml::Input::; /* Kana Lock */
  375. // case XK_Kana_Shift & 0xFF: return Rml::Input::; /* Kana Shift */
  376. // case XK_Eisu_Shift & 0xFF: return Rml::Input::; /* Alphanumeric Shift */
  377. // case XK_Eisu_toggle & 0xFF: return Rml::Input::; /* Alphanumeric toggle */
  378. case XK_Home & 0xFF: return Rml::Input::KI_HOME;
  379. case XK_Left & 0xFF: return Rml::Input::KI_LEFT;
  380. case XK_Up & 0xFF: return Rml::Input::KI_UP;
  381. case XK_Right & 0xFF: return Rml::Input::KI_RIGHT;
  382. case XK_Down & 0xFF: return Rml::Input::KI_DOWN;
  383. case XK_Prior & 0xFF: return Rml::Input::KI_PRIOR;
  384. case XK_Next & 0xFF: return Rml::Input::KI_NEXT;
  385. case XK_End & 0xFF: return Rml::Input::KI_END;
  386. case XK_Begin & 0xFF: return Rml::Input::KI_HOME;
  387. // case XK_Print & 0xFF: return Rml::Input::KI_SNAPSHOT;
  388. // case XK_Insert & 0xFF: return Rml::Input::KI_INSERT;
  389. case XK_Num_Lock & 0xFF: return Rml::Input::KI_NUMLOCK;
  390. case XK_KP_Space & 0xFF: return Rml::Input::KI_SPACE;
  391. case XK_KP_Tab & 0xFF: return Rml::Input::KI_TAB;
  392. case XK_KP_Enter & 0xFF: return Rml::Input::KI_NUMPADENTER;
  393. case XK_KP_F1 & 0xFF: return Rml::Input::KI_F1;
  394. case XK_KP_F2 & 0xFF: return Rml::Input::KI_F2;
  395. case XK_KP_F3 & 0xFF: return Rml::Input::KI_F3;
  396. case XK_KP_F4 & 0xFF: return Rml::Input::KI_F4;
  397. case XK_KP_Home & 0xFF: return Rml::Input::KI_NUMPAD7;
  398. case XK_KP_Left & 0xFF: return Rml::Input::KI_NUMPAD4;
  399. case XK_KP_Up & 0xFF: return Rml::Input::KI_NUMPAD8;
  400. case XK_KP_Right & 0xFF: return Rml::Input::KI_NUMPAD6;
  401. case XK_KP_Down & 0xFF: return Rml::Input::KI_NUMPAD2;
  402. case XK_KP_Prior & 0xFF: return Rml::Input::KI_NUMPAD9;
  403. case XK_KP_Next & 0xFF: return Rml::Input::KI_NUMPAD3;
  404. case XK_KP_End & 0xFF: return Rml::Input::KI_NUMPAD1;
  405. case XK_KP_Begin & 0xFF: return Rml::Input::KI_NUMPAD5;
  406. case XK_KP_Insert & 0xFF: return Rml::Input::KI_NUMPAD0;
  407. case XK_KP_Delete & 0xFF: return Rml::Input::KI_DECIMAL;
  408. case XK_KP_Equal & 0xFF: return Rml::Input::KI_OEM_NEC_EQUAL;
  409. case XK_KP_Multiply & 0xFF: return Rml::Input::KI_MULTIPLY;
  410. case XK_KP_Add & 0xFF: return Rml::Input::KI_ADD;
  411. case XK_KP_Separator & 0xFF: return Rml::Input::KI_SEPARATOR;
  412. case XK_KP_Subtract & 0xFF: return Rml::Input::KI_SUBTRACT;
  413. case XK_KP_Decimal & 0xFF: return Rml::Input::KI_DECIMAL;
  414. case XK_KP_Divide & 0xFF: return Rml::Input::KI_DIVIDE;
  415. case XK_F1 & 0xFF: return Rml::Input::KI_F1;
  416. case XK_F2 & 0xFF: return Rml::Input::KI_F2;
  417. case XK_F3 & 0xFF: return Rml::Input::KI_F3;
  418. case XK_F4 & 0xFF: return Rml::Input::KI_F4;
  419. case XK_F5 & 0xFF: return Rml::Input::KI_F5;
  420. case XK_F6 & 0xFF: return Rml::Input::KI_F6;
  421. case XK_F7 & 0xFF: return Rml::Input::KI_F7;
  422. case XK_F8 & 0xFF: return Rml::Input::KI_F8;
  423. case XK_F9 & 0xFF: return Rml::Input::KI_F9;
  424. case XK_F10 & 0xFF: return Rml::Input::KI_F10;
  425. case XK_F11 & 0xFF: return Rml::Input::KI_F11;
  426. case XK_F12 & 0xFF: return Rml::Input::KI_F12;
  427. case XK_F13 & 0xFF: return Rml::Input::KI_F13;
  428. case XK_F14 & 0xFF: return Rml::Input::KI_F14;
  429. case XK_F15 & 0xFF: return Rml::Input::KI_F15;
  430. case XK_F16 & 0xFF: return Rml::Input::KI_F16;
  431. case XK_F17 & 0xFF: return Rml::Input::KI_F17;
  432. case XK_F18 & 0xFF: return Rml::Input::KI_F18;
  433. case XK_F19 & 0xFF: return Rml::Input::KI_F19;
  434. case XK_F20 & 0xFF: return Rml::Input::KI_F20;
  435. case XK_F21 & 0xFF: return Rml::Input::KI_F21;
  436. case XK_F22 & 0xFF: return Rml::Input::KI_F22;
  437. case XK_F23 & 0xFF: return Rml::Input::KI_F23;
  438. case XK_F24 & 0xFF: return Rml::Input::KI_F24;
  439. case XK_Shift_L & 0xFF: return Rml::Input::KI_LSHIFT;
  440. case XK_Shift_R & 0xFF: return Rml::Input::KI_RSHIFT;
  441. case XK_Control_L & 0xFF: return Rml::Input::KI_LCONTROL;
  442. case XK_Control_R & 0xFF: return Rml::Input::KI_RCONTROL;
  443. case XK_Caps_Lock & 0xFF: return Rml::Input::KI_CAPITAL;
  444. case XK_Alt_L & 0xFF: return Rml::Input::KI_LMENU;
  445. case XK_Alt_R & 0xFF: return Rml::Input::KI_RMENU;
  446. case XK_space & 0xFF: return Rml::Input::KI_SPACE;
  447. case XK_apostrophe & 0xFF: return Rml::Input::KI_OEM_7;
  448. case XK_comma & 0xFF: return Rml::Input::KI_OEM_COMMA;
  449. case XK_minus & 0xFF: return Rml::Input::KI_OEM_MINUS;
  450. case XK_period & 0xFF: return Rml::Input::KI_OEM_PERIOD;
  451. case XK_slash & 0xFF: return Rml::Input::KI_OEM_2;
  452. case XK_0 & 0xFF: return Rml::Input::KI_0;
  453. case XK_1 & 0xFF: return Rml::Input::KI_1;
  454. case XK_2 & 0xFF: return Rml::Input::KI_2;
  455. case XK_3 & 0xFF: return Rml::Input::KI_3;
  456. case XK_4 & 0xFF: return Rml::Input::KI_4;
  457. case XK_5 & 0xFF: return Rml::Input::KI_5;
  458. case XK_6 & 0xFF: return Rml::Input::KI_6;
  459. case XK_7 & 0xFF: return Rml::Input::KI_7;
  460. case XK_8 & 0xFF: return Rml::Input::KI_8;
  461. case XK_9 & 0xFF: return Rml::Input::KI_9;
  462. case XK_semicolon & 0xFF: return Rml::Input::KI_OEM_1;
  463. case XK_equal & 0xFF: return Rml::Input::KI_OEM_PLUS;
  464. case XK_bracketleft & 0xFF: return Rml::Input::KI_OEM_4;
  465. case XK_backslash & 0xFF: return Rml::Input::KI_OEM_5;
  466. case XK_bracketright & 0xFF: return Rml::Input::KI_OEM_6;
  467. case XK_grave & 0xFF: return Rml::Input::KI_OEM_3;
  468. case XK_a & 0xFF: return Rml::Input::KI_A;
  469. case XK_b & 0xFF: return Rml::Input::KI_B;
  470. case XK_c & 0xFF: return Rml::Input::KI_C;
  471. case XK_d & 0xFF: return Rml::Input::KI_D;
  472. case XK_e & 0xFF: return Rml::Input::KI_E;
  473. case XK_f & 0xFF: return Rml::Input::KI_F;
  474. case XK_g & 0xFF: return Rml::Input::KI_G;
  475. case XK_h & 0xFF: return Rml::Input::KI_H;
  476. case XK_i & 0xFF: return Rml::Input::KI_I;
  477. case XK_j & 0xFF: return Rml::Input::KI_J;
  478. case XK_k & 0xFF: return Rml::Input::KI_K;
  479. case XK_l & 0xFF: return Rml::Input::KI_L;
  480. case XK_m & 0xFF: return Rml::Input::KI_M;
  481. case XK_n & 0xFF: return Rml::Input::KI_N;
  482. case XK_o & 0xFF: return Rml::Input::KI_O;
  483. case XK_p & 0xFF: return Rml::Input::KI_P;
  484. case XK_q & 0xFF: return Rml::Input::KI_Q;
  485. case XK_r & 0xFF: return Rml::Input::KI_R;
  486. case XK_s & 0xFF: return Rml::Input::KI_S;
  487. case XK_t & 0xFF: return Rml::Input::KI_T;
  488. case XK_u & 0xFF: return Rml::Input::KI_U;
  489. case XK_v & 0xFF: return Rml::Input::KI_V;
  490. case XK_w & 0xFF: return Rml::Input::KI_W;
  491. case XK_x & 0xFF: return Rml::Input::KI_X;
  492. case XK_y & 0xFF: return Rml::Input::KI_Y;
  493. case XK_z & 0xFF: return Rml::Input::KI_Z;
  494. default: break;
  495. }
  496. // clang-format on
  497. return Rml::Input::KI_UNKNOWN;
  498. }
  499. int RmlX11::ConvertMouseButton(unsigned int x11_mouse_button)
  500. {
  501. switch (x11_mouse_button)
  502. {
  503. case Button1: return 0;
  504. case Button2: return 2;
  505. case Button3: return 1;
  506. default: break;
  507. }
  508. return 0;
  509. }
  510. /**
  511. This map contains 4 different mappings from key identifiers to character codes. Each entry represents a different
  512. combination of shift and capslock state.
  513. */
  514. static char ascii_map[4][51] = {
  515. // shift off and capslock off
  516. {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',
  517. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ';', '=', ',', '-', '.', '/', '`', '[', '\\', ']', '\'', 0, 0},
  518. // shift on and capslock off
  519. {0, ' ', ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  520. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ':', '+', '<', '_', '>', '?', '~', '{', '|', '}', '"', 0, 0},
  521. // shift on and capslock on
  522. {0, ' ', ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  523. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ':', '+', '<', '_', '>', '?', '~', '{', '|', '}', '"', 0, 0},
  524. // shift off and capslock on
  525. {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',
  526. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ';', '=', ',', '-', '.', '/', '`', '[', '\\', ']', '\'', 0, 0}};
  527. static char keypad_map[2][18] = {{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\n', '*', '+', 0, '-', '.', '/', '='},
  528. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\n', '*', '+', 0, '-', 0, '/', '='}};
  529. // Returns the character code for a key identifer / key modifier combination.
  530. static Rml::Character GetCharacterCode(Rml::Input::KeyIdentifier key_identifier, int key_modifier_state)
  531. {
  532. using Rml::Character;
  533. // Check if we have a keycode capable of generating characters on the main keyboard (ie, not on the numeric
  534. // keypad; that is dealt with below).
  535. if (key_identifier <= Rml::Input::KI_OEM_102)
  536. {
  537. // Get modifier states
  538. bool shift = (key_modifier_state & Rml::Input::KM_SHIFT) > 0;
  539. bool capslock = (key_modifier_state & Rml::Input::KM_CAPSLOCK) > 0;
  540. // Return character code based on identifier and modifiers
  541. if (shift && !capslock)
  542. return (Character)ascii_map[1][key_identifier];
  543. if (shift && capslock)
  544. return (Character)ascii_map[2][key_identifier];
  545. if (!shift && capslock)
  546. return (Character)ascii_map[3][key_identifier];
  547. return (Character)ascii_map[0][key_identifier];
  548. }
  549. // Check if we have a keycode from the numeric keypad.
  550. else if (key_identifier <= Rml::Input::KI_OEM_NEC_EQUAL)
  551. {
  552. if (key_modifier_state & Rml::Input::KM_NUMLOCK)
  553. return (Character)keypad_map[0][key_identifier - Rml::Input::KI_NUMPAD0];
  554. else
  555. return (Character)keypad_map[1][key_identifier - Rml::Input::KI_NUMPAD0];
  556. }
  557. else if (key_identifier == Rml::Input::KI_RETURN)
  558. return (Character)'\n';
  559. return Character::Null;
  560. }