Repository navigation
Expand file tree
/
Copy pathX11MiniFB.c
More file actions
1638 lines (1428 loc) · 63.4 KB
/
Copy pathX11MiniFB.c
File metadata and controls
1638 lines (1428 loc) · 63.4 KB
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <poll.h>
#include <sched.h>
#include <errno.h>
#include <locale.h>
#include <limits.h>
#include <MiniFB.h>
#include <MiniFB_internal.h>
#include "WindowData.h"
#include "WindowData_X11.h"
#include "X11MiniFB_text.h"
#include "MiniFB_evdev_keys.h"
#include "X11MiniFB_scale_funcs.h"
#if defined(USE_OPENGL_API)
#include <gl/MiniFB_GL.h>
#endif
#if defined(MINIFB_HAS_XRANDR)
#include <X11/extensions/Xrandr.h>
#endif
// The X protocol reserves the keycodes below eight, so a server that uses the kernel numbers
// shifts them all up by that much.
#define X11_EVDEV_KEYCODE_OFFSET 8
static Atom s_delete_window_atom;
static bool s_x11_locale_checked;
static int s_xrr_event_base = -1;
//-------------------------------------
void init_keycodes(SWindowData_X11 *window_data_specific);
Cursor create_blank_cursor(Display *display, Window window);
//-------------------------------------
int translate_key(int scancode);
void destroy_window_data(SWindowData *window_data);
//-------------------------------------
// Old window managers read WM_NAME, which is Latin-1, and current ones read _NET_WM_NAME as
// UTF8_STRING. Xutf8SetWMProperties encodes the legacy property from the same UTF-8 input,
// so both stay in step.
//-------------------------------------
static void
set_window_title(SWindowData_X11 *window_data_specific, const char *title) {
char *safe = mfb_safe_title_copy(title);
if (safe == NULL) {
return;
}
Xutf8SetWMProperties(window_data_specific->display,
window_data_specific->window,
safe,
safe,
NULL,
0,
NULL,
NULL,
NULL);
Atom net_wm_name = XInternAtom(window_data_specific->display, "_NET_WM_NAME", False);
Atom utf8_string = XInternAtom(window_data_specific->display, "UTF8_STRING", False);
if (net_wm_name != None && utf8_string != None) {
XChangeProperty(window_data_specific->display,
window_data_specific->window,
net_wm_name,
utf8_string,
8,
PropModeReplace,
(const unsigned char *) safe,
(int) strlen(safe));
}
free(safe);
}
//-------------------------------------
static uint32_t
own_mod_bit(mfb_key key_code) {
switch (key_code) {
case MFB_KB_KEY_LEFT_SHIFT:
case MFB_KB_KEY_RIGHT_SHIFT:
return MFB_KB_MOD_SHIFT;
case MFB_KB_KEY_LEFT_CONTROL:
case MFB_KB_KEY_RIGHT_CONTROL:
return MFB_KB_MOD_CONTROL;
case MFB_KB_KEY_LEFT_ALT:
case MFB_KB_KEY_RIGHT_ALT:
return MFB_KB_MOD_ALT;
case MFB_KB_KEY_LEFT_SUPER:
case MFB_KB_KEY_RIGHT_SUPER:
return MFB_KB_MOD_SUPER;
default:
return 0;
}
}
//-------------------------------------
// The state field describes the keyboard before the event. How far behind the locks are
// depends on the server, so those are asked for directly. A held modifier is only behind for
// the key of the event itself, and key_status already holds its new value.
//-------------------------------------
static uint32_t
mods_from_state(Display *display, unsigned int state, mfb_key key_code) {
unsigned int lock_state = state;
uint32_t mods = 0;
if (key_code == MFB_KB_KEY_CAPS_LOCK || key_code == MFB_KB_KEY_NUM_LOCK) {
XkbStateRec xkb_state;
if (XkbGetState(display, XkbUseCoreKbd, &xkb_state) == Success) {
lock_state = (unsigned int) xkb_state.locked_mods;
}
}
if ((lock_state & LockMask) != 0) {
mods |= MFB_KB_MOD_CAPS_LOCK;
}
if ((lock_state & Mod2Mask) != 0) {
mods |= MFB_KB_MOD_NUM_LOCK;
}
// Only the server can report a modifier activated with no key of its own.
if ((state & ShiftMask) != 0) {
mods |= MFB_KB_MOD_SHIFT;
}
if ((state & ControlMask) != 0) {
mods |= MFB_KB_MOD_CONTROL;
}
if ((state & Mod1Mask) != 0) {
mods |= MFB_KB_MOD_ALT;
}
if ((state & Mod4Mask) != 0) {
mods |= MFB_KB_MOD_SUPER;
}
return mods & ~own_mod_bit(key_code);
}
//-------------------------------------
// Keys pressed while another window had the focus produce no event here, so the buffer is
// rebuilt from the server instead of from what this window happened to see.
//-------------------------------------
static void
sync_key_status(SWindowData *window_data, SWindowData_X11 *window_data_specific) {
char keys[32];
memset(window_data->key_status, 0, sizeof(window_data->key_status));
XQueryKeymap(window_data_specific->display, keys);
for (unsigned keycode = 0; keycode < 256; ++keycode) {
if ((keys[keycode / 8] & (1 << (keycode % 8))) == 0) {
continue;
}
mfb_key key_code = (mfb_key) translate_key((int) keycode);
if (key_code != MFB_KB_KEY_UNKNOWN) {
window_data->key_status[key_code] = 1;
}
}
}
//-------------------------------------
// filtered says the input method consumed this event as part of a composition. The physical
// key is still reported, only the text is left to the method, which is what every other X11
// application does.
static void
process_event(SWindowData *window_data, XEvent *event, bool filtered) {
switch (event->type) {
case KeyPress:
case KeyRelease:
{
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if ((event->type == KeyRelease) &&
XEventsQueued(window_data_specific->display, QueuedAfterReading)) {
XEvent nev;
XPeekEvent(window_data_specific->display, &nev);
if ((nev.type == KeyPress) && (nev.xkey.time == event->xkey.time) &&
(nev.xkey.keycode == event->xkey.keycode)) {
/* Key wasn't actually released */
return;
}
}
mfb_key key_code = (mfb_key) translate_key((int) event->xkey.keycode);
int is_pressed = (event->type == KeyPress);
if (key_code != MFB_KB_KEY_UNKNOWN) {
window_data->key_status[key_code] = (uint8_t) is_pressed;
mfb_recalc_mod_keys(window_data,
mods_from_state(window_data_specific->display,
event->xkey.state,
key_code));
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
if (event->type == KeyPress && filtered == false) {
x11_text_dispatch(window_data, window_data_specific, event);
}
}
break;
case ButtonPress:
case ButtonRelease:
{
mfb_mouse_button button = (mfb_mouse_button) event->xbutton.button;
int is_pressed = (event->type == ButtonPress);
mfb_recalc_mod_keys(window_data,
mods_from_state(((SWindowData_X11 *) window_data->specific)->display,
event->xbutton.state,
MFB_KB_KEY_UNKNOWN));
// Swap mouse right and middle for parity with other platforms:
// https://github.com/emoon/minifb/issues/65
switch (button) {
case Button2:
button = Button3;
break;
case Button3:
button = Button2;
break;
}
bool is_real_button = false;
// X11 reports a wheel notch as a press followed by a release. Only the press
// is a notch, so taking both would report every scroll twice.
if (is_pressed == 0 && button >= Button4 && button <= 7) {
break;
}
switch (button) {
case Button1:
case Button2:
case Button3:
is_real_button = true;
window_data->mouse_button_status[button & MFB_MAX_MOUSE_BUTTONS_MASK] = is_pressed;
kCall(mouse_btn_func, button, (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
case Button4:
window_data->mouse_wheel_y = 1.0f;
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, window_data->mouse_wheel_y);
break;
case Button5:
window_data->mouse_wheel_y = -1.0f;
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, window_data->mouse_wheel_y);
break;
case 6:
window_data->mouse_wheel_x = 1.0f;
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, window_data->mouse_wheel_x, 0.0f);
break;
case 7:
window_data->mouse_wheel_x = -1.0f;
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, window_data->mouse_wheel_x, 0.0f);
break;
default: {
uint32_t mapped = button - 4;
if (mapped > MFB_MOUSE_BTN_7) {
MFB_LOG(MFB_LOG_WARNING, "Mouse button %u exceeds MFB_MOUSE_BTN_7; ignoring.", mapped);
break;
}
is_real_button = true;
window_data->mouse_button_status[mapped] = is_pressed;
kCall(mouse_btn_func, (mfb_mouse_button) mapped, (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
}
}
if (is_real_button == true && is_pressed == 0 &&
mfb_any_mouse_button_pressed(window_data) == false) {
bool is_inside = mfb_is_point_inside_window(window_data, event->xbutton.x, event->xbutton.y);
if (window_data->is_mouse_inside != is_inside) {
window_data->is_mouse_inside = is_inside;
kCall(mouse_enter_func, is_inside);
}
}
}
break;
case MotionNotify:
window_data->mouse_pos_x = event->xmotion.x;
window_data->mouse_pos_y = event->xmotion.y;
kCall(mouse_move_func, event->xmotion.x, event->xmotion.y);
break;
case ConfigureNotify:
{
// X11 also sends this event when the window only moves, and a window manager sends
// its own copy of every real resize.
bool size_changed = window_data->window_width != (uint32_t) event->xconfigure.width ||
window_data->window_height != (uint32_t) event->xconfigure.height;
window_data->window_width = event->xconfigure.width;
window_data->window_height = event->xconfigure.height;
resize_dst(window_data, event->xconfigure.width, event->xconfigure.height);
#if defined(USE_OPENGL_API)
resize_GL(window_data);
#else
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific->image_scaler != NULL) {
window_data_specific->image_scaler->data = NULL;
XDestroyImage(window_data_specific->image_scaler);
window_data_specific->image_scaler = NULL;
window_data_specific->image_scaler_width = 0;
window_data_specific->image_scaler_height = 0;
}
XClearWindow(window_data_specific->display, window_data_specific->window);
#endif
if (size_changed == true) {
kCall(resize_func, window_data->window_width, window_data->window_height);
}
}
break;
case EnterNotify:
case LeaveNotify:
{
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific == NULL || event->xany.window != window_data_specific->window)
break;
// A starting grab synthesizes a crossing without the pointer moving, so it is
// dropped. An ending one reports where the pointer actually is, and it is the
// only notice that arrives when a drag owned by another client finishes over
// this window. The edge check below absorbs the one our own drag produces.
if (event->xcrossing.mode == NotifyGrab)
break;
// The pointer only crossed between this window and a child of it,
// so it never left the window.
if (event->xcrossing.detail == NotifyInferior)
break;
// X11 does deliver crossings during the implicit grab of a button press, so
// they are held back here while a button is down and the state is settled when
// the last one comes up.
if (mfb_any_mouse_button_pressed(window_data) == true)
break;
bool is_inside = (event->type == EnterNotify);
if (window_data->is_mouse_inside != is_inside) {
window_data->is_mouse_inside = is_inside;
kCall(mouse_enter_func, is_inside);
}
}
break;
case FocusIn:
// A grab reports focus changes the user never made: a window menu, an alt-tab
// switcher or dragging the window all take one.
if (event->xfocus.mode == NotifyGrab || event->xfocus.mode == NotifyUngrab) {
break;
}
window_data->is_active = true;
if (window_data->specific) {
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
x11_text_focus(window_data_specific, true);
sync_key_status(window_data, window_data_specific);
}
kCall(active_func, true);
break;
case FocusOut:
if (event->xfocus.mode == NotifyGrab || event->xfocus.mode == NotifyUngrab) {
break;
}
window_data->is_active = false;
if (window_data->specific) {
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
x11_text_focus(window_data_specific, false);
}
kCall(active_func, false);
mfb_release_held_keys(window_data, window_data->mod_keys);
break;
case DestroyNotify:
window_data->close = true;
return;
break;
case ClientMessage:
{
if (filtered == true) {
break;
}
if ((Atom) event->xclient.data.l[0] == s_delete_window_atom) {
if (window_data) {
bool destroy = false;
// Obtain a confirmation of close
if (!window_data->close_func || window_data->close_func((struct mfb_window *) window_data)) {
destroy = true;
}
if (destroy) {
window_data->close = true;
return;
}
}
}
}
break;
}
#if defined(MINIFB_HAS_XRANDR)
// XRandR events have dynamic type IDs, must check with event_base
if (s_xrr_event_base >= 0 && event->type == (s_xrr_event_base + RRScreenChangeNotify)) {
MFB_LOG(MFB_LOG_TRACE, "XRRScreenChangeNotify event received");
// Scale will be refreshed on next mfb_get_monitor_scale() call
}
#endif
}
//-------------------------------------
static inline void
update_events(SWindowData *window_data, Display *display) {
XEvent event;
if (window_data != NULL) {
window_data->mouse_wheel_x = 0.0f;
window_data->mouse_wheel_y = 0.0f;
}
while (XPending(display) > 0) {
XNextEvent(display, &event);
bool filtered = x11_text_filter(&event);
process_event(window_data, &event, filtered);
}
}
//-------------------------------------
// Query the primary monitor's geometry via XRandR.
// Falls back to the first connected output if no primary is set.
// If XRandR is unavailable, the output values are left unchanged.
static void
get_primary_monitor_geometry(Display *display, Window root,
int *out_x, int *out_y, int *out_w, int *out_h) {
#if defined(MINIFB_HAS_XRANDR)
XRRScreenResources *sr = XRRGetScreenResources(display, root);
if (sr == NULL)
return;
RROutput primary = XRRGetOutputPrimary(display, root);
bool found = false;
if (primary != None) {
XRROutputInfo *oi = XRRGetOutputInfo(display, sr, primary);
if (oi != NULL) {
if (oi->crtc != None && oi->connection == RR_Connected) {
XRRCrtcInfo *ci = XRRGetCrtcInfo(display, sr, oi->crtc);
if (ci != NULL) {
*out_x = ci->x;
*out_y = ci->y;
*out_w = (int) ci->width;
*out_h = (int) ci->height;
found = true;
XRRFreeCrtcInfo(ci);
}
}
XRRFreeOutputInfo(oi);
}
}
if (!found) {
for (int o = 0; o < sr->noutput; o++) {
XRROutputInfo *oi = XRRGetOutputInfo(display, sr, sr->outputs[o]);
if (oi != NULL) {
if (oi->crtc != None && oi->connection == RR_Connected) {
XRRCrtcInfo *ci = XRRGetCrtcInfo(display, sr, oi->crtc);
if (ci != NULL) {
*out_x = ci->x;
*out_y = ci->y;
*out_w = (int) ci->width;
*out_h = (int) ci->height;
found = true;
XRRFreeCrtcInfo(ci);
}
}
XRRFreeOutputInfo(oi);
if (found)
break;
}
}
}
XRRFreeScreenResources(sr);
#else
(void) display;
(void) root;
(void) out_x;
(void) out_y;
(void) out_w;
(void) out_h;
#endif
}
//-------------------------------------
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
const unsigned known_flags = MFB_WF_RESIZABLE | MFB_WF_FULLSCREEN | MFB_WF_FULLSCREEN_DESKTOP | MFB_WF_BORDERLESS | MFB_WF_ALWAYS_ON_TOP;
unsigned effective_flags = flags;
const char *window_title = (title != NULL && title[0] != '\0') ? title : "minifb";
int depth, i, format_count, conv_depth = -1;
XPixmapFormatValues *formats;
XSetWindowAttributes window_attributes;
XSizeHints size_hints;
Visual *visual;
uint32_t buffer_stride = 0;
if (width == 0 || height == 0) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: invalid window size %ux%u.", width, height);
return NULL;
}
if (!calculate_buffer_layout(width, height, &buffer_stride, NULL) ||
buffer_stride > (uint32_t) INT_MAX) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: window size %ux%u is too large for X11 image layout.", width, height);
return NULL;
}
if ((effective_flags & ~known_flags) != 0u) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: unknown window flags 0x%x will be ignored.", effective_flags & ~known_flags);
}
if ((effective_flags & MFB_WF_FULLSCREEN) && (effective_flags & MFB_WF_FULLSCREEN_DESKTOP)) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: MFB_WF_FULLSCREEN and MFB_WF_FULLSCREEN_DESKTOP were both requested; MFB_WF_FULLSCREEN takes precedence.");
effective_flags &= ~MFB_WF_FULLSCREEN_DESKTOP;
}
SWindowData *window_data = (SWindowData *) malloc(sizeof(SWindowData));
if (!window_data) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: failed to allocate SWindowData.");
return NULL;
}
memset(window_data, 0, sizeof(SWindowData));
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) malloc(sizeof(SWindowData_X11));
if (!window_data_specific) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: failed to allocate SWindowData_X11.");
free(window_data);
return NULL;
}
memset(window_data_specific, 0, sizeof(SWindowData_X11));
window_data->specific = window_data_specific;
window_data_specific->display = XOpenDisplay(0);
if (!window_data_specific->display) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: XOpenDisplay failed.");
free(window_data);
free(window_data_specific);
return NULL;
}
#if defined(MINIFB_HAS_XRANDR)
// Initialize XRandR event base for dynamic event type IDs
if (s_xrr_event_base < 0) {
int error_base = 0;
if (XRRQueryExtension(window_data_specific->display, &s_xrr_event_base, &error_base)) {
MFB_LOG(MFB_LOG_TRACE, "X11MiniFB: XRandR event base = %d", s_xrr_event_base);
}
else {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: XRRQueryExtension failed; XRandR events unavailable.");
s_xrr_event_base = -1;
}
}
#endif
if (s_x11_locale_checked == false) {
const char *locale = setlocale(LC_CTYPE, NULL);
// A C program starts in the "C" locale, where the input method can barely work, so
// the one from the environment is taken instead. Only the default is replaced: an
// application that chose its own locale keeps it.
if (locale == NULL || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
locale = setlocale(LC_CTYPE, "");
}
if (locale == NULL || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: the environment provides no usable LC_CTYPE locale; dead keys and input methods will be limited.");
}
else if (XSupportsLocale() == False) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: Xlib does not support the locale '%s'; dead keys and input methods will be limited.", locale);
}
s_x11_locale_checked = true;
}
// Without this the server reports auto-repeat as a release followed by a press, and the
// application sees a key it never released. The peek that filters those only works when
// both events are already queued.
{
Bool supported = False;
XkbSetDetectableAutoRepeat(window_data_specific->display, True, &supported);
}
init_keycodes(window_data_specific);
window_data_specific->screen = DefaultScreen(window_data_specific->display);
visual = DefaultVisual(window_data_specific->display, window_data_specific->screen);
formats = XListPixmapFormats(window_data_specific->display, &format_count);
depth = DefaultDepth(window_data_specific->display, window_data_specific->screen);
if (formats == NULL || format_count <= 0) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: XListPixmapFormats returned no formats.");
XCloseDisplay(window_data_specific->display);
free(window_data_specific);
free(window_data);
return NULL;
}
Window default_root_window = DefaultRootWindow(window_data_specific->display);
for (i = 0; i < format_count; ++i) {
if (depth == formats[i].depth) {
conv_depth = formats[i].bits_per_pixel;
break;
}
}
XFree(formats);
// We only support 32-bit right now
if (conv_depth != 32) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: unsupported visual depth conversion (%d bpp), expected 32 bpp.", conv_depth);
XCloseDisplay(window_data_specific->display);
free(window_data_specific);
free(window_data);
return NULL;
}
int screen_width = DisplayWidth(window_data_specific->display, window_data_specific->screen);
int screen_height = DisplayHeight(window_data_specific->display, window_data_specific->screen);
int primary_x = 0, primary_y = 0;
int primary_w = screen_width, primary_h = screen_height;
get_primary_monitor_geometry(window_data_specific->display, default_root_window,
&primary_x, &primary_y, &primary_w, &primary_h);
window_attributes.border_pixel = BlackPixel(window_data_specific->display, window_data_specific->screen);
window_attributes.background_pixel = BlackPixel(window_data_specific->display, window_data_specific->screen);
window_attributes.backing_store = NotUseful;
int pos_x, pos_y;
int window_width, window_height;
bool request_maximized_desktop = (effective_flags & MFB_WF_FULLSCREEN_DESKTOP) != 0;
window_data->window_width = width;
window_data->window_height = height;
window_data->buffer_width = width;
window_data->buffer_height = height;
window_data->buffer_stride = buffer_stride;
calc_dst_factor(window_data, width, height);
if (effective_flags & MFB_WF_FULLSCREEN_DESKTOP) {
pos_x = 0;
pos_y = 0;
window_width = screen_width;
window_height = screen_height;
}
else {
pos_x = primary_x + (primary_w - (int) width) / 2;
pos_y = primary_y + (primary_h - (int) height) / 2;
window_width = width;
window_height = height;
}
window_data_specific->window = XCreateWindow(
window_data_specific->display,
default_root_window,
pos_x, pos_y,
window_width, window_height,
0,
depth,
InputOutput,
visual,
CWBackPixel | CWBorderPixel | CWBackingStore,
&window_attributes);
if (!window_data_specific->window) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: XCreateWindow failed.");
XCloseDisplay(window_data_specific->display);
free(window_data_specific);
free(window_data);
return NULL;
}
XSelectInput(window_data_specific->display, window_data_specific->window,
KeyPressMask | KeyReleaseMask
| ButtonPressMask | ButtonReleaseMask | PointerMotionMask
| StructureNotifyMask | ExposureMask
| FocusChangeMask
| EnterWindowMask | LeaveWindowMask
);
#if defined(MINIFB_HAS_XRANDR)
// Register for XRandR screen change events on the root window
if (s_xrr_event_base >= 0) {
XRRSelectInput(window_data_specific->display, default_root_window,
RRScreenChangeNotifyMask
);
MFB_LOG(MFB_LOG_TRACE, "X11MiniFB: XRRSelectInput registered for RRScreenChangeNotifyMask");
}
#endif
if (x11_text_init(window_data_specific) == false) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: no text input pipeline is available; keys will still be reported.");
}
set_window_title(window_data_specific, window_title);
if (effective_flags & MFB_WF_BORDERLESS) {
struct StyleHints {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long inputMode;
unsigned long status;
} sh = {
.flags = 2,
.functions = 0,
.decorations = 0,
.inputMode = 0,
.status = 0,
};
Atom sh_p = XInternAtom(window_data_specific->display, "_MOTIF_WM_HINTS", True);
if (sh_p == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: _MOTIF_WM_HINTS atom is unavailable; borderless hint may be ignored.");
}
else {
X
10BC8
ChangeProperty(window_data_specific->display, window_data_specific->window, sh_p, sh_p, 32, PropModeReplace, (unsigned char *) &sh, 5);
}
}
if (effective_flags & MFB_WF_ALWAYS_ON_TOP) {
Atom sa_p = XInternAtom(window_data_specific->display, "_NET_WM_STATE_ABOVE", False);
Atom state_atom = XInternAtom(window_data_specific->display, "_NET_WM_STATE", False);
if (sa_p == None || state_atom == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: always-on-top WM atoms are unavailable; request may be ignored.");
}
else {
XChangeProperty(window_data_specific->display, window_data_specific->window, state_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&sa_p, 1);
}
}
if (effective_flags & MFB_WF_FULLSCREEN) {
Atom sf_p = XInternAtom(window_data_specific->display, "_NET_WM_STATE_FULLSCREEN", True);
Atom state_atom = XInternAtom(window_data_specific->display, "_NET_WM_STATE", True);
if (sf_p == None || state_atom == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: fullscreen WM atoms are unavailable; fullscreen request may be ignored.");
}
else {
XChangeProperty(window_data_specific->display, window_data_specific->window, state_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *) &sf_p, 1);
}
}
if (request_maximized_desktop) {
Atom state_atom = XInternAtom(window_data_specific->display, "_NET_WM_STATE", True);
Atom max_horz = XInternAtom(window_data_specific->display, "_NET_WM_STATE_MAXIMIZED_HORZ", True);
Atom max_vert = XInternAtom(window_data_specific->display, "_NET_WM_STATE_MAXIMIZED_VERT", True);
if (state_atom == None || max_horz == None || max_vert == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: maximized WM atoms are unavailable; fullscreen-desktop may not behave like a maximized window.");
}
else {
Atom maximized_atoms[2] = { max_horz, max_vert };
XChangeProperty(window_data_specific->display, window_data_specific->window, state_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *) maximized_atoms, 2);
}
}
size_hints.flags = PPosition | PMinSize | PMaxSize;
size_hints.x = 0;
size_hints.y = 0;
if (request_maximized_desktop) {
size_hints.min_width = 1;
size_hints.min_height = 1;
size_hints.max_width = screen_width;
size_hints.max_height = screen_height;
}
else {
size_hints.min_width = width;
size_hints.min_height = height;
}
if (!request_maximized_desktop && (effective_flags & MFB_WF_RESIZABLE)) {
size_hints.max_width = screen_width;
size_hints.max_height = screen_height;
}
else if (!request_maximized_desktop) {
size_hints.max_width = width;
size_hints.max_height = height;
}
s_delete_window_atom = XInternAtom(window_data_specific->display, "WM_DELETE_WINDOW", False);
if (s_delete_window_atom == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: WM_DELETE_WINDOW atom unavailable; close requests may not be delivered.");
}
else {
XSetWMProtocols(window_data_specific->display, window_data_specific->window, &s_delete_window_atom, 1);
}
#if defined(USE_OPENGL_API)
if (create_GL_context(window_data) == false) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: create_GL_context failed.");
destroy_window_data(window_data);
return NULL;
}
#else
window_data_specific->image = XCreateImage(window_data_specific->display, CopyFromParent, depth, ZPixmap, 0, NULL, width, height, 32, (int) buffer_stride);
if (window_data_specific->image == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: XCreateImage failed.");
destroy_window_data(window_data);
return NULL;
}
#endif
XSetWMNormalHints(window_data_specific->display, window_data_specific->window, &size_hints);
XClearWindow(window_data_specific->display, window_data_specific->window);
XMapRaised(window_data_specific->display, window_data_specific->window);
XFlush(window_data_specific->display);
window_data_specific->gc = DefaultGC(window_data_specific->display, window_data_specific->screen);
window_data_specific->timer = mfb_timer_create();
if (window_data_specific->timer == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: mfb_timer_create failed.");
destroy_window_data(window_data);
return NULL;
}
window_data->is_cursor_visible = true;
window_data_specific->invis_cursor = create_blank_cursor(window_data_specific->display, window_data_specific->window);
if (window_data_specific->invis_cursor == None) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: failed to create invisible cursor; cursor hiding may not work.");
}
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
// A key held while the window opens has to be there from the start. Waiting for the first
// FocusIn would leave the buffer empty until the application had already pumped events
// once, and read it as if nothing were pressed.
sync_key_status(window_data, window_data_specific);
#if defined(USE_OPENGL_API)
MFB_LOG(MFB_LOG_DEBUG, "Window created using OpenGL API");
#else
MFB_LOG(MFB_LOG_DEBUG, "Window created using X11 API");
#endif
window_data->is_initialized = true;
return (struct mfb_window *) window_data;
}
//-------------------------------------
mfb_update_state
mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned height) {
uint32_t buffer_stride = 0;
SWindowData *window_data = (SWindowData *) window;
if (window_data == NULL) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid window");
return MFB_STATE_INVALID_WINDOW;
}
// Early exit
if (window_data->close) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: window requested close");
destroy_window_data(window_data);
return MFB_STATE_EXIT;
}
if (buffer == NULL) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid buffer");
return MFB_STATE_INVALID_BUFFER;
}
if (!calculate_buffer_layout(width, height, &buffer_stride, NULL)) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid buffer size %ux%u", width, height);
return MFB_STATE_INVALID_BUFFER;
}
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific == NULL) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid window specific data");
return MFB_STATE_INVALID_WINDOW;
}
Display *display = window_data_specific->display;
if (display == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: missing X11 display in mfb_update_ex.");
return MFB_STATE_INVALID_WINDOW;
}
#if !defined(USE_OPENGL_API)
bool different_size = false;
#endif
if (window_data->buffer_width != width || window_data->buffer_height != height) {
if (buffer_stride > (uint32_t) INT_MAX) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: buffer stride for size %ux%u exceeds XImage limit.", width, height);
return MFB_STATE_INVALID_BUFFER;
}
window_data->buffer_width = width;
window_data->buffer_stride = buffer_stride;
window_data->buffer_height = height;
#if !defined(USE_OPENGL_API)
different_size = true;
#endif
}
#if !defined(USE_OPENGL_API)
if (different_size || window_data->buffer_width != window_data->dst_width || window_data->buffer_height != window_data->dst_height) {
if (window_data_specific->image_scaler_width != window_data->dst_width || window_data_specific->image_scaler_height != window_data->dst_height) {
if (window_data_specific->image_scaler != NULL) {
window_data_specific->image_scaler->data = NULL;
XDestroyImage(window_data_specific->image_scaler);
}
if (window_data_specific->image_buffer != NULL) {
free(window_data_specific->image_buffer);
window_data_specific->image_buffer = NULL;
}
int depth = DefaultDepth(display, window_data_specific->screen);
uint32_t scaler_stride = 0;
size_t scaler_size = 0;
if (!calculate_buffer_layout(window_data->dst_width, window_data->dst_height, &scaler_stride, &scaler_size) ||
scaler_stride > (uint32_t) INT_MAX) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: invalid scaler layout for size %ux%u.", window_data->dst_width, window_data->dst_height);
return MFB_STATE_INTERNAL_ERROR;
}
window_data_specific->image_buffer = malloc(scaler_size);
if (window_data_specific->image_buffer == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: failed to allocate image scaler buffer.");
return MFB_STATE_INTERNAL_ERROR;
}
window_data_specific->image_scaler_width = window_data->dst_width;
window_data_specific->image_scaler_height = window_data->dst_height;
window_data_specific->image_scaler = XCreateImage(display, CopyFromParent, depth, ZPixmap, 0, NULL, window_data_specific->image_scaler_width, window_data_specific->image_scaler_height, 32, (int) scaler_stride);
if (window_data_specific->image_scaler == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: XCreateImage failed for scaler image.");
free(window_data_specific->image_buffer);
window_data_specific->image_buffer = NULL;
window_data_specific->image_scaler_width = 0;
window_data_specific->image_scaler_height = 0;
return MFB_STATE_INTERNAL_ERROR;
}
}
}
if (window_data_specific->image_scaler != NULL) {
stretch_image((uint32_t *) buffer, 0, 0, window_data->buffer_width, window_data->buffer_height, window_data->buffer_width,
(uint32_t *) window_data_specific->image_buffer, 0, 0, window_data->dst_width, window_data->dst_height, window_data->dst_width);
window_data_specific->image_scaler->data = (char *) window_data_specific->image_buffer;
XPutImage(display, window_data_specific->window, window_data_specific->gc, window_data_specific->image_scaler, 0, 0, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height);
}
else {
if (window_data_specific->image == NULL) {
MFB_LOG(MFB_LOG_ERROR, "X11MiniFB: missing base XImage in mfb_update_ex.");
return MFB_STATE_INTERNAL_ERROR;
}
window_data_specific->image->data = (char *) buffer;
XPutImage(display, window_data_specific->window, window_data_specific->gc, window_data_specific->image, 0, 0, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height);
}
#else
redraw_GL(window_data, buffer);
#endif
XFlush(display);
update_events(window_data, display);
if (window_data->close) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: window closed after event processing");
destroy_window_data(window_data);
return MFB_STATE_EXIT;
}
return MFB_STATE_OK;
}
//-------------------------------------
mfb_update_state
mfb_update_events(struct mfb_window *window) {