Repository navigation
Expand file tree
/
Copy pathX11MiniFB.c
More file actions
1619 lines (1424 loc) · 61.3 KB
/
Copy pathX11MiniFB.c
File metadata and controls
1619 lines (1424 loc) · 61.3 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 <xkbcommon/xkbcommon.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 "MiniFB_utf8.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
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);
int translate_mod(int state);
int translate_mod_ex(int key, int state, int is_pressed);
void destroy_window_data(SWindowData *window_data);
//-------------------------------------
static uint32_t
keysym_to_codepoint(KeySym keysym) {
// Direct Latin-1 keysyms.
if ((keysym >= 0x0020 && keysym <= 0x007e) ||
(keysym >= 0x00a0 && keysym <= 0x00ff)) {
return (uint32_t) keysym;
}
// X11 Unicode keysym encoding: 0x01000000 | UCS-24.
if ((keysym & 0xff000000UL) == 0x01000000UL) {
uint32_t codepoint = (uint32_t) (keysym & 0x00ffffffUL);
if (codepoint <= 0x10ffff && !(codepoint >= 0xd800 && codepoint <= 0xdfff)) {
return codepoint;
}
}
return 0;
}
//-------------------------------------
static bool
is_dead_keysym(KeySym keysym) {
switch (keysym) {
case XK_dead_grave:
case XK_dead_acute:
case XK_dead_circumflex:
case XK_dead_tilde:
case XK_dead_diaeresis:
return true;
default:
return false;
}
}
//-------------------------------------
static uint32_t
compose_dead_codepoint(KeySym dead_keysym, uint32_t codepoint) {
switch (dead_keysym) {
case XK_dead_acute:
switch (codepoint) {
case 'a': return 0x00e1; case 'A': return 0x00c1;
case 'e': return 0x00e9; case 'E': return 0x00c9;
case 'i': return 0x00ed; case 'I': return 0x00cd;
case 'o': return 0x00f3; case 'O': return 0x00d3;
case 'u': return 0x00fa; case 'U': return 0x00da;
case 'y': return 0x00fd; case 'Y': return 0x00dd;
}
break;
case XK_dead_grave:
switch (codepoint) {
case 'a': return 0x00e0; case 'A': return 0x00c0;
case 'e': return 0x00e8; case 'E': return 0x00c8;
case 'i': return 0x00ec; case 'I': return 0x00cc;
case 'o': return 0x00f2; case 'O': return 0x00d2;
case 'u': return 0x00f9; case 'U': return 0x00d9;
}
break;
case XK_dead_diaeresis:
switch (codepoint) {
case 'a': return 0x00e4; case 'A': return 0x00c4;
case 'e': return 0x00eb; case 'E': return 0x00cb;
case 'i': return 0x00ef; case 'I': return 0x00cf;
case 'o': return 0x00f6; case 'O': return 0x00d6;
case 'u': return 0x00fc; case 'U': return 0x00dc;
case 'y': return 0x00ff; case 'Y': return 0x0178;
}
break;
case XK_dead_circumflex:
switch (codepoint) {
case 'a': return 0x00e2; case 'A': return 0x00c2;
case 'e': return 0x00ea; case 'E': return 0x00ca;
case 'i': return 0x00ee; case 'I': return 0x00ce;
case 'o': return 0x00f4; case 'O': return 0x00d4;
case 'u': return 0x00fb; case 'U': return 0x00db;
}
break;
case XK_dead_tilde:
switch (codepoint) {
case 'a': return 0x00e3; case 'A': return 0x00c3;
case 'n': return 0x00f1; case 'N': return 0x00d1;
case 'o': return 0x00f5; case 'O': return 0x00d5;
}
break;
}
return 0;
}
//-------------------------------------
static uint32_t
dead_keysym_to_codepoint(KeySym dead_keysym) {
switch (dead_keysym) {
case XK_dead_grave: return 0x0060; // `
case XK_dead_acute: return 0x00b4; // ´
case XK_dead_circumflex: return 0x005e; // ^
case XK_dead_tilde: return 0x007e; // ~
case XK_dead_diaeresis: return 0x00a8; // ¨
default: return 0;
}
}
//-------------------------------------
static void
emit_codepoint_with_dead_state(SWindowData *window_data, SWindowData_X11 *window_data_specific, uint32_t codepoint) {
if (window_data == NULL || window_data_specific == NULL || codepoint == 0) {
return;
}
if (window_data_specific->pending_dead_keysym != NoSymbol) {
uint32_t composed = compose_dead_codepoint(window_data_specific->pending_dead_keysym, codepoint);
if (composed != 0) {
codepoint = composed;
}
else {
uint32_t accent = dead_keysym_to_codepoint(window_data_specific->pending_dead_keysym);
if (accent != 0) {
kCall(char_input_func, accent);
}
}
window_data_specific->pending_dead_keysym = NoSymbol;
}
kCall(char_input_func, codepoint);
}
//-------------------------------------
static void
dispatch_text_input(SWindowData *window_data, SWindowData_X11 *window_data_specific, XEvent *event) {
if (window_data == NULL || window_data_specific == NULL || event == NULL) {
return;
}
if (window_data_specific->ic != NULL) {
char stack_buffer[64];
char *text_buffer = stack_buffer;
int text_capacity = (int) sizeof(stack_buffer);
KeySym keysym = NoSymbol;
Status status = 0;
int text_size = Xutf8LookupString(window_data_specific->ic, &event->xkey, text_buffer, text_capacity, &keysym, &status);
if (status == XBufferOverflow) {
text_capacity = text_size + 1;
text_buffer = (char *) malloc((size_t) text_capacity);
if (text_buffer == NULL) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: failed to allocate buffer for Xutf8LookupString.");
return;
}
text_size = Xutf8LookupString(window_data_specific->ic, &event->xkey, text_buffer, text_capacity, &keysym, &status);
}
if ((status == XLookupChars || status == XLookupBoth) && text_size > 0) {
// Some XIMs still report dead-key keysyms while producing raw base chars.
// Keep an explicit dead-key state to force expected composition behavior.
if (is_dead_keysym(keysym)) {
window_data_specific->pending_dead_keysym = keysym;
}
else {
size_t index = 0;
while (index < (size_t) text_size) {
uint32_t codepoint = 0;
if (utf8_decode_next((const unsigned char *) text_buffer, (size_t) text_size, &index, &codepoint) && codepoint != 0) {
emit_codepoint_with_dead_state(window_data, window_data_specific, codepoint);
}
}
}
}
else if ((status == XLookupKeySym || status == XLookupBoth) && is_dead_keysym(keysym)) {
window_data_specific->pending_dead_keysym = keysym;
}
if (text_buffer != stack_buffer) {
free(text_buffer);
}
return;
}
// Fallback when XIM/XIC is unavailable.
KeySym keysym = NoSymbol;
XLookupString(&event->xkey, NULL, 0, &keysym, NULL);
if (is_dead_keysym(keysym)) {
window_data_specific->pending_dead_keysym = keysym;
return;
}
uint32_t codepoint = keysym_to_codepoint(keysym);
if (codepoint == 0) {
window_data_specific->pending_dead_keysym = NoSymbol;
return;
}
emit_codepoint_with_dead_state(window_data, window_data_specific, codepoint);
}
//-------------------------------------
static void
process_event(SWindowData *window_data, XEvent *event) {
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(event->xkey.keycode);
int is_pressed = (event->type == KeyPress);
window_data->mod_keys = translate_mod_ex(key_code, event->xkey.state, is_pressed);
if (key_code != MFB_KB_KEY_UNKNOWN) {
window_data->key_status[key_code] = (uint8_t) is_pressed;
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
if (event->type == KeyPress) {
dispatch_text_input(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);
window_data->mod_keys = translate_mod(event->xkey.state);
// 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;
}
switch (button) {
case Button1:
case Button2:
case Button3:
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;
}
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;
}
}
}
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:
{
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
kCall(resize_func, window_data->window_width, window_data->window_height);
}
break;
case EnterNotify:
case LeaveNotify:
break;
case FocusIn:
window_data->is_active = true;
if (window_data->specific) {
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific->ic != NULL) {
XSetICFocus(window_data_specific->ic);
}
}
kCall(active_func, true);
break;
case FocusOut:
window_data->is_active = false;
if (window_data->specific) {
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific->ic != NULL) {
XUnsetICFocus(window_data_specific->ic);
}
}
kCall(active_func, false);
break;
case DestroyNotify:
window_data->close = true;
return;
break;
case ClientMessage:
{
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);
process_event(window_data, &event);
}
}
//-------------------------------------
// 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) {
const char *locale = setlocale(LC_CTYPE, NULL);
if (locale == NULL || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: LC_CTYPE locale is not configured for UTF-8; IME/dead-key input may be limited.");
}
s_x11_locale_checked = true;
}
if (XSetLocaleModifiers("") == NULL) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: XSetLocaleModifiers failed; input method support may be limited.");
}
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
window_data_specific->im = XOpenIM(window_data_specific->display, NULL, NULL, NULL);
if (window_data_specific->im == NULL) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: XOpenIM failed; falling back to basic keysym text input.");
}
else {
window_data_specific->ic = XCreateIC(window_data_specific->im,
XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
XNClientWindow, window_data_specific->window,
XNFocusWindow, window_data_specific->window,
NULL);
if (window_data_specific->ic == NULL) {
MFB_LOG(MFB_LOG_WARNING, "X11MiniFB: XCreateIC failed; falling back to basic keysym text input.");
XCloseIM(window_data_specific->im);
window_data_specific->im = NULL;
}
}
XStoreName(window_data_specific->display, window_data_specific->window, 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 {
XChangeProperty(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);
#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) {
SWindowData *window_data = (SWindowData *) window;
if (window_data == NULL) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_events: invalid window");
return MFB_STATE_INVALID_WINDOW;
}
// Early exit
if (window_data->close) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_events: window requested close");
destroy_window_data(window_data);
return MFB_STATE_EXIT;
}
SWindowData_X11 *window_data_specific = (SWindowData_X11 *) window_data->specific;
if (window_data_specific == NULL) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_events: invalid window specific data");
return MFB_STATE_INVALID_WINDOW;