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