-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_mini.c
1764 lines (1573 loc) · 48.7 KB
/
window_mini.c
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 "window_mini.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#if defined(_WIN32)
//...
#elif defined(__linux__)
#include <X11/extensions/XInput2.h>
#else
#error "unsupported os"
#endif
// NOTE: assumes..
// .. numElementsToAppend > 0
static void add_or_append_num_elements(int elementSize, int* numElements, void** elements, int numElementsToAppendOrAdd)
{
void* a = *elements;
//*elements = (void*)new char[elementSize * ((*numElements) + numElementsToAppendOrAdd)];
*elements = malloc(elementSize * ((*numElements) + numElementsToAppendOrAdd));
if(*numElements > 0)
{
memcpy(*elements, a, elementSize * (*numElements));
//delete a;
free(a);
}
*numElements += numElementsToAppendOrAdd;
}
//#define add_or_append_num_elements2(numElements, elements, numElementsToAppendOrAdd) add_or_append_num_elements(sizeof **elements, numElements, (void**)elements, numElementsToAppendOrAdd)
#define add_or_append_one_element(elementSize, numElements, elements) add_or_append_num_elements(elementSize, numElements, elements, 1)
#define add_or_append_one_element2(numElements, elements) add_or_append_one_element(sizeof **elements, numElements, (void**)elements)
// NOTE: assumes..
// .. *numElements >= lastNumElementsToRemove
// .. lastNumElementsToRemove > 0
static void remove_last_num_elements(int elementSize, int* numElements, void** elements, int lastNumElementsToRemove)
{
if(*numElements == lastNumElementsToRemove)
{
//delete *elements;
free(*elements);
}
else
{
void* a = *elements;
//*elements = (void*)new char[elementSize * ((*numElements) - lastNumElementsToRemove)];
*elements = malloc(elementSize * ((*numElements) - lastNumElementsToRemove));
memcpy(*elements, a, elementSize * ((*numElements) - lastNumElementsToRemove));
//delete a;
free(a);
}
*numElements -= lastNumElementsToRemove;
}
#define remove_last_num_elements2(numElements, elements, lastNumElementsToRemove) remove_last_num_elements(sizeof **elements, numElements, (void**)elements, lastNumElementsToRemove)
#if defined(_WIN32)
static void getlasterror_to_string(int* a, char* b)
{
DWORD c = GetLastError();
char* d;
// NOTE: FORMAT_MESSAGE_MAX_WIDTH_MASK such that "[FormatMessageA]..
// .. ignores regular line breaks in the message definition..
// .. text"
// ^
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea
// NOTE: currently assuming US English is always available (I have no..
// .. proof whether or not this is so)
// v
if(FormatMessageA(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, c, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&d, 0, NULL) != 0)
{
if(b == NULL)
{
*a = strlen(d) + 1;
}
else
{
strncpy(b, d, *a);
}
LocalFree(d);
}
else
{
if(b == NULL)
{
// https://stackoverflow.com/questions/29087129/how-to-calculate-the-length-of-output-that-sprintf-will-generate
*a = snprintf(NULL, 0, "%u", c) + 1;
}
else
{
snprintf(b, *a, "%u", c);
}
}
}
#endif
//*****************************************************************************
static int bIsLoaded = 0;
#if defined(_WIN32)
struct win32_t
{
struct
{
HINSTANCE a;
} hinstance;
};
static struct win32_t win32;
#else //< #elif defined(__linux__)
struct xlib_t
{
struct
{
Display* a;
} display;
struct
{
int a;
} screen;
struct
{
Atom a;
} wmdeletewindow;
struct
{
Atom a;
} netwmstate;
struct
{
Atom a;
} netwmstatefullscreen;
};
static struct xlib_t xlib;
#endif
static void(*on_print)(char* a, FILE* b) = NULL;
static void on_printf(FILE* a, char* b, ...)
{
va_list c;
va_start(c, b);
int d = vsnprintf(NULL, 0, b, c);
char e[d + 1];
vsprintf(e, b, c);
va_end(c);
on_print(e, a);
}
//*****************************************************************************
void wm_set_on_print(void(*a)(char*, FILE*))
{
on_print = a;
}
void wm_unset_on_print()
{
on_print = NULL;
}
#if defined(_WIN32)
enum
{
ELoadWin32Progress_RegisterClassADefault = 1,
ELoadWin32Progress_RegisterClassAOpengl = 2
};
#define ELoadWin32Progress_All ELoadWin32Progress_RegisterClassAOpengl
static LRESULT CALLBACK MyWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void unload_win32(int progress, struct win32_t* a);
static int load_win32()
{
struct
{
HINSTANCE a;
} hinstance;
int progress = 0;
do
{
// NOTE: "The operating system uses [hInstance] to identify the..
// .. executable (EXE) when it is loaded in memory",
// https://learn.microsoft.com/en-us/windows/win32/learnwin32/winmain--the-application-entry-point
// "If [lpModuleName] is NULL, GetModuleHandle returns a..
// .. handle to the file used to create the calling process..
// .. (.exe file).",
// https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlea
// ^
// thus both WinMain hInstance is handle to executable and..
// .. GetModuleHandleA(NULL) returns handle to executable..?
// NOTE: https://devblogs.microsoft.com/oldnewthing/20050418-59/?p=35873
hinstance.a = GetModuleHandleA(NULL);
WNDCLASSA a;
a.style = CS_HREDRAW | CS_VREDRAW;
a.lpfnWndProc = (WNDPROC)&MyWndProc;
a.cbClsExtra = 0;
a.cbWndExtra = 0;
a.hInstance = hinstance.a;
a.hIcon = NULL;
a.hCursor = NULL;
a.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
a.lpszMenuName = NULL;
a.lpszClassName = "WM_DEFAULT";
if(RegisterClassA(&a) == 0)
{
if(on_print != NULL)
{
int c;
getlasterror_to_string(&c, NULL);
char d[c];
getlasterror_to_string(&c, d);
on_printf(stderr, "error: %s in %s\n", d, __FUNCTION__);
}
break;
}
progress = ELoadWin32Progress_RegisterClassADefault;
// https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)
WNDCLASS b;
b.style = CS_OWNDC;
b.lpfnWndProc = &MyWndProc;
b.cbClsExtra = 0;
b.cbWndExtra = 0;
b.hInstance = win32.hinstance.a;
b.hIcon = NULL;
b.hCursor = NULL;
b.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
b.lpszMenuName = NULL;
b.lpszClassName = "WM_OPENGL";
if(RegisterClassA(&b) == 0)
{
if(on_print != NULL)
{
int c;
getlasterror_to_string(&c, NULL);
char d[c];
getlasterror_to_string(&c, d);
on_printf(stderr, "error: %s in %s\n", d, __FUNCTION__);
}
break;
}
progress = ELoadWin32Progress_RegisterClassAOpengl;
} while(0);
if(progress != ELoadWin32Progress_All)
{
struct win32_t a;
a.hinstance.a = hinstance.a;
unload_win32(progress, &a);
return 0;
}
win32.hinstance.a = hinstance.a;
return 1;
}
static void unload_win32(int progress, struct win32_t* a)
{
if(progress >= ELoadWin32Progress_RegisterClassAOpengl)
{
if(UnregisterClassA("WM_OPENGL", a->hinstance.a) == 0)
{
if(on_print != NULL)
{
int b;
getlasterror_to_string(&b, NULL);
char c[b];
getlasterror_to_string(&b, c);
on_printf(stdout, "warning: %s in %s\n", c, __FUNCTION__);
}
}
}
if(progress >= ELoadWin32Progress_RegisterClassADefault)
{
if(UnregisterClassA("WM_DEFAULT", a->hinstance.a) == 0)
{
if(on_print != NULL)
{
int b;
getlasterror_to_string(&b, NULL);
char c[b];
getlasterror_to_string(&b, c);
on_printf(stdout, "warning: %s in %s\n", c, __FUNCTION__);
}
}
}
}
#else //< #elif defined(__linux__)
enum
{
ELoadXlibProgress_XInitThreads = 1,
ELoadXlibProgress_XOpenDisplay = 2,
ELoadXlibProgress_XInternAtomWmdeletewindow = 3,
ELoadXlibProgress_XInternAtomNetwmstate = 4,
ELoadXlibProgress_XInternAtomNetwmstatefullscreen = 5
};
#define ELoadXlibProgress_All ELoadXlibProgress_XInternAtomNetwmstatefullscreen
static void unload_xlib(int progress, struct xlib_t* a);
static int load_xlib()
{
struct
{
Display* a;
} display;
struct
{
Atom a;
} wmdeletewindow;
struct
{
Atom a;
} netwmstate;
struct
{
Atom a;
} netwmstatefullscreen;
int progress = 0;
do
{
if(XInitThreads() == 0)
{
if(on_print != NULL)
{
on_printf(stderr, "error: XInitThreads() == 0 in %s\n", __FUNCTION__);
}
break;
}
progress = ELoadXlibProgress_XInitThreads;
display.a = XOpenDisplay(NULL);
if(display.a == NULL)
{
if(on_print != NULL)
{
on_printf(stderr, "error: XOpenDisplay(NULL) == NULL in %s\n", __FUNCTION__);
}
break;
}
progress = ELoadXlibProgress_XOpenDisplay;
// https://stackoverflow.com/questions/1157364/intercept-wm-delete-window-on-x11
wmdeletewindow.a = XInternAtom(display.a, "WM_DELETE_WINDOW", False);
// NOTE: "If only_if_exists is False, the atom is created if it does..
// .. not exist. Therefore, XInternAtom can return None.",..
// .. https://www.x.org/releases/X11R7.5/doc/man/man3/XInternAtom.3.html
// ^
// not sure if any other cause than memory allocation failure..
// .. can cause XInternAtom to return None here
if(wmdeletewindow.a == None)
{
break;
}
progress = ELoadXlibProgress_XInternAtomWmdeletewindow;
netwmstate.a = XInternAtom(display.a, "_NET_WM_STATE", False);
if(netwmstate.a = None)
{
break;
}
progress = ELoadXlibProgress_XInternAtomNetwmstate;
netwmstatefullscreen.a = XInternAtom(display.a, "_NET_WM_STATE_FULLSCREEN", False);
if(netwmstatefullscreen.a = None)
{
break;
}
progress = ELoadXlibProgress_XInternAtomNetwmstatefullscreen;
} while(0);
if(progress != ELoadXlibProgress_All)
{
struct xlib_t a;
a.display.a = display.a;
unload_xlib(progress, &a);
return 0;
}
xlib.display.a = display.a;
xlib.screen.a = DefaultScreen(xlib.display.a);
// NOTE: ^
// no mention of possibility that DefaultScreen fails thus assumed..
// .. here that it wont,..
// .. https://www.x.org/releases/X11R7.5/doc/libX11/libX11.html
// NOTE: "Client applications can display overlapping and nested windows..
// .. on one or more screens",..
// .. https://www.x.org/releases/X11R7.5/doc/libX11/libX11.html
// ^
// thus not sure if more than one screen from a single display is..
// .. possible, but guaranteed that it isn't using DefaultScreen..
// .. as "[DefaultScreen] return[s] the default screen number..
// .. referenced by the XOpenDisplay function. This macro [...]..
// .. should be used to retrieve the screen number in applications..
// .. that will use only a single screen.", https://www.x.org/releases/X11R7.5/doc/libX11/libX11.html
// TODO: ^
// test whether it is possible to move between screens using..
// .. XScreenCount
// ^
// OR not sure if OS only uses a single screen across all monitors?
xlib.wmdeletewindow.a = wmdeletewindow.a;
xlib.netwmstate.a = netwmstate.a;
xlib.netwmstatefullscreen.a = netwmstatefullscreen.a;
return 1;
}
static void unload_xlib(int progress, struct xlib_t* a)
{
if(progress >= ELoadXlibProgress_XOpenDisplay)
{
XCloseDisplay(a->display.a);
// ^
// "XCloseDisplay can generate a BadGC error.",..
// .. https://www.x.org/releases/X11R7.5/doc/libX11/libX11.html
// ^
// not sure if only if display.a is invalid
// any way currently no use of xlib error handler meaning "When Xlib..
// .. detects an error, it calls an error handler, which your..
// .. program can provide. If you do not provide an error handler,..
// .. the error is printed, and your program terminates.",..
// .. https://www.x.org/releases/X11R7.5/doc/libX11/libX11.html
}
}
#endif
int wm_load()
{
if(bIsLoaded == 1)
{
return -1;
}
#if defined(_WIN32)
if(load_win32() != 1)
{
return 0;
}
#else //< #elif defined(__linux__)
if(load_xlib() != 1)
{
return 0;
}
#endif
bIsLoaded = 1;
return 1;
}
static int numWindows;
// ^
// is valid?
// ^
// https://stackoverflow.com/a/24783091
int wm_unload()
{
if(bIsLoaded == 0)
{
return -1;
}
if(numWindows > 0)
{
if(on_print != NULL)
{
on_printf(stdout, "warning: numWindows > 0 in %s\n", __FUNCTION__);
}
return -1;
}
#if defined(_WIN32)
unload_win32(ELoadWin32Progress_All, &win32);
#else //< #elif defined(__linux__)
unload_xlib(ELoadXlibProgress_All, &xlib);
#endif
bIsLoaded = 0;
return 1;
}
int wm_get_info(struct wm_info_t* info)
{
if(bIsLoaded == 0)
{
return -1;
}
#if defined(_WIN32)
info->win32.hinstance.a = win32.hinstance.a;
#else //< #elif defined(__linux__)
info->xlib.display.a = xlib.display.a;
#endif
return 1;
}
//*****************************************************************************
struct info_about_window_t
{
struct wm_window_source_t* source;
int minWidthInPixels;
int minHeightInPixels;
int maxWidthInPixels;
int maxHeightInPixels;
int widthInPixels;
int heightInPixels;
char* title;
int bFullscreen;
int bIsResizing;
struct
{
int newWidthInPixels;
int newHeightInPixels;
} ifResizing;
// as there are multiple places where resizing can be triggered use..
// .. variable to only write resizing callback call + <width and height>..
// .. updating once
// v
int bResize;
//int addWindowParametersFlags;
//int bIsPaused;
//int bResize;
#if defined(_WIN32)
int bIsMinimized;
int bIsMaximized;
struct
{
struct
{
HWND a;
} hwnd;
struct
{
WINDOWPLACEMENT a;
} windowplacement;
} win32;
#else //< #elif defined(__linux__)
int bIsFocused;
int bClose;
// ^
// bClose here because processing close in message loop allows for..
// .. continue
struct
{
struct
{
Window a;
} window;
} xlib;
#endif
};
static struct info_about_window_t info_about_window_default = { .bIsResizing = 0, .bResize = 0
#if defined(_WIN32)
,
.bIsMinimized = 0,
.bIsMaximized = 0
#else //< #elif defined(__linux__)
,
.bIsFocused = 0,
.bClose = 0
#endif
};
static int numWindowsTheresRoomFor = 0;
static int numWindows = 0;
static struct info_about_window_t* infoPerWindow;
static int(*on_window_closed)(int window);
static void(*on_window_resized)(int window, int widthInPixels, int heightInPixels);
static void(*on_window_focused)(int window);
static void(*on_window_unfocused)(int window);
//*****************************************************************************
void wm_set_on_window_closed(int(*a)(int))
{
on_window_closed = a;
}
void wm_unset_on_window_closed()
{
on_window_closed = NULL;
}
void wm_set_on_window_resized(void(*a)(int, int, int))
{
on_window_resized = a;
}
void wm_unset_on_window_resized()
{
on_window_resized = NULL;
}
void wm_set_on_window_focused(void(*a)(int))
{
on_window_focused = a;
}
void wm_unset_on_window_focused()
{
on_window_focused = NULL;
}
void wm_set_on_window_unfocused(void(*a)(int))
{
on_window_unfocused = a;
}
void wm_unset_on_window_unfocused()
{
on_window_unfocused = NULL;
}
#ifdef __linux__
static int toggle_fullscreen(struct info_about_window_t* infoAboutWindow);
#endif
// NOTE: assumes..
// .. infoAboutWindow->bFullscreen == 0
static int start_fullscreen(struct info_about_window_t* infoAboutWindow)
{
#if defined(_WIN32)
// NOTE: information about fullscreen..
// .. https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353
if (GetWindowPlacement(infoAboutWindow->win32.hwnd.a, &infoAboutWindow->win32.windowplacement.a) == 0)
{
int c;
getlasterror_to_string(&c, NULL);
char d[c];
getlasterror_to_string(&c, d);
on_printf(stdout, "warning: %s in %s\n", d, __FUNCTION__);
return 0;
}
MONITORINFO a;
a.cbSize = sizeof(MONITORINFO);
if(GetMonitorInfo(MonitorFromWindow(infoAboutWindow->win32.hwnd.a, MONITOR_DEFAULTTOPRIMARY), &a) == 0)
{
// NOTE: not sure if GetLastError is used by GetMonitorInfoA..
// .. (documentation doesn't mention)
// ^
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmonitorinfoa
int c;
getlasterror_to_string(&c, NULL);
char d[c];
getlasterror_to_string(&c, d);
on_printf(stdout, "warning: %s in %s\n", d, __FUNCTION__);
return 0;
}
DWORD b = GetWindowLong(infoAboutWindow->win32.hwnd.a, GWL_STYLE);
SetWindowLong(infoAboutWindow->win32.hwnd.a, GWL_STYLE, b & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(infoAboutWindow->win32.hwnd.a, HWND_TOP, a.rcMonitor.left, a.rcMonitor.top, a.rcMonitor.right - a.rcMonitor.left, a.rcMonitor.bottom - a.rcMonitor.top, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
infoAboutWindow->bFullscreen = 1;
return 1;
#else //< #elif defined(__linux__)
return toggle_fullscreen(infoAboutWindow);
#endif
}
// NOTE: assumes..
// .. infoAboutWindow->bFullscreen == 1
static int stop_fullscreen(struct info_about_window_t* infoAboutWindow)
{
#if defined(_WIN32)
// NOTE: information about fullscreen..
// .. https://devblogs.microsoft.com/oldnewthing/20100412-00/?p=14353
DWORD a = GetWindowLong(infoAboutWindow->win32.hwnd.a, GWL_STYLE);
SetWindowLong(infoAboutWindow->win32.hwnd.a, GWL_STYLE, a | WS_OVERLAPPEDWINDOW);
SetWindowPlacement(infoAboutWindow->win32.hwnd.a, &infoAboutWindow->win32.windowplacement.a);
SetWindowPos(infoAboutWindow->win32.hwnd.a, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
infoAboutWindow->bFullscreen = 0;
return 1;
#else //< #elif defined(__linux__)
return toggle_fullscreen(infoAboutWindow);
#endif
}
static int toggle_fullscreen(struct info_about_window_t* infoAboutWindow)
{
#if defined(_WIN32)
if(infoAboutWindow->bFullscreen == 0)
{
return start_fullscreen(infoAboutWindow);
}
else
{
return stop_fullscreen(infoAboutWindow);
}
#else //< #elif defined(__linux__)
XEvent a;
a.type = ClientMessage;
a.xclient.serial = 0;
a.xclient.send_event = True;
a.xclient.display = xlib.display.a;
a.xclient.window = infoAboutWindow->xlib.window.a;
a.xclient.message_type = xlib.netwmstate.a;
a.xclient.format = 32;
a.xclient.data.l[0] = 2;
a.xclient.data.l[1] = xlib.netwmstatefullscreen.a;
a.xclient.data.l[2] = 0;
a.xclient.data.l[3] = 0;
a.xclient.data.l[4] = 0;
XSendEvent(xlib.display.a, RootWindow(xlib.display.a, xlib.screen.a), False, StructureNotifyMask | ResizeRedirectMask, &a);
// NOTE: information about _NET_WM_STATE_FULLSCREEN..
// https://specifications.freedesktop.org/wm-spec/wm-spec-1.3.html#idm46435610051248
// NOTE: example of using _NET_WM_STATE_FULLSCREEN..
// .. https://mail.gnome.org/archives/metacity-devel-list/2010-February/msg00000.html
infoAboutWindow->bFullscreen = infoAboutWindow->bFullscreen == 0 ? 1 : 0;
return 1;
#endif
}
#if defined(_WIN32)
enum
{
EAddInfoAboutWindowWin32Progress_CreateWindowA = 1
};
#define EAddInfoAboutWindowWin32Progress_All EAddInfoAboutWindowWin32Progress_CreateWindowA
static void remove_info_about_window_win32(int progress, struct info_about_window_t* a);
static int add_info_about_window_win32(struct wm_add_window_parameters_t* parameters, struct wm_window_source_t* source, struct info_about_window_t* infoAboutWindow)
{
struct
{
HWND a;
} hwnd;
int progress = 0;
do
{
// NOTE: "The window is an overlapped window. Same as the WS_TILEDWINDOW..
// .. style."
// ^
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
// NOTE: "If the y parameter is CW_USEDEFAULT, then the window..
// .. manager calls ShowWindow with the SW_SHOW flag after the..
// .. window has been created.",
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowa
// "[SW_SHOW] activates the window and displays it in its current size..
// .. and position.",
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
// ^
// CW_USEDEFAULT would ignore nCmdShow // NOTE: "If an overlapped window is created with the WS_VISIBLE style..
// .. bit set and the x parameter is set to CW_USEDEFAULT, then..
// .. the y parameter determines how the window is shown. [...] If..
// .. the y parameter is [not CW_USEDEFAULT], then the window..
// .. manager calls ShowWindow with that value as the nCmdShow..
// .. parameter.",
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowa
// "[SW_SHOWDEFAULT s]ets the show state based on the SW_..
// .. value specified in the STARTUPINFO structure passed to..
// .. the CreateProcess function by the program that started..
// .. the application.",
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
// ^
// not sure if correct as not sure if every window should use..
// .. <STARTUPINFOA>.wShowWindow
// NOTE: "[nCmdShow] is ignored the first time an application calls..
// .. ShowWindow, if the program that launched the application..
// .. provides a STARTUPINFO structure.",
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
// STARTUPINFO is not optional in CreateProcessA
// ^
// "[in] lpStartupInfo",
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
// ^
// thus if ShowWindowA is not called before CreateWindowA the y..
// .. parameter to CreateWindowA will be ignored..?
// ^
// not sure if mistake in documentation
// ^
// https://github.com/MicrosoftDocs/feedback/issues/3890
DWORD a = WS_TILEDWINDOW | WS_VISIBLE; //< CreateWindowA dwStyle
int b; //< CreateWindowA nWidth
int c; //< CreateWindowA nHeight
if((source->widthInPixels != -1) | (source->heightInPixels != -1))
{
RECT e;
e.left = 0;
e.top = 0;
e.right = source->widthInPixels;
e.bottom = source->heightInPixels;
if(AdjustWindowRect(&e, a, FALSE) == 0)
{
if(on_print != NULL)
{
int f;
getlasterror_to_string(&f, NULL);
char g[f];
getlasterror_to_string(&f, g);
on_printf(stdout, "warning: %s in %s\n", g, __FUNCTION__);
}
}
b = source->widthInPixels == -1 ? CW_USEDEFAULT : e.right - e.left;
c = source->heightInPixels == -1 ? CW_USEDEFAULT : e.bottom - e.top;
}
else
{
b = CW_USEDEFAULT;
c = CW_USEDEFAULT;
}
// NOTE: "client coordinates are relative to the upper-left corner..
// .. of a window's client area",..
// .. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclientrect
// NOTE: documentation is vague about whether b.top and b.left are..
// .. not 0 or not here.. "the structure contains the..
// .. coordinates of the top-left and bottom-right corners of..
// .. the window to accommodate the desired client area",..
// .. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-adjustwindowrect
// ^
// probably if b.top and b.left were both 0 before..
// .. AdjustWindowRect will now be both negative?
char* d = (parameters->flags & EWMAddWindowParametersFlag_Opengl) != 0 ? "WM_OPENGL" : "WM_DEFAULT";
hwnd.a = CreateWindowA(d, source->title, a, CW_USEDEFAULT, SW_SHOWDEFAULT, b, c, NULL, NULL, win32.hinstance.a, NULL);
if(hwnd.a == NULL)
{
if(on_print != NULL)
{
int e;
getlasterror_to_string(&e, NULL);
char f[e];
getlasterror_to_string(&e, f);
on_printf(stderr, "error: %s in %s\n", f, __FUNCTION__);
}
break;
}
progress = EAddInfoAboutWindowWin32Progress_CreateWindowA;
} while(0);
if(progress != EAddInfoAboutWindowWin32Progress_All)
{
struct info_about_window_t a;
a.win32.hwnd.a = hwnd.a;
remove_info_about_window_win32(progress, &a);
return 0;
}
infoAboutWindow->win32.hwnd.a = hwnd.a;
return 1;
}
#else //< #elif defined(__linux__)
enum
{
EAddInfoAboutWindowXlibProgress_XCreateWindow = 1,
EAddInfoAboutWindowXlibProgress_XSetWMProtocols = 2
};
#define EAddInfoAboutWindowXlibProgress_All EAddInfoAboutWindowXlibProgress_XSetWMProtocols
static void remove_info_about_window_xlib(int progress, struct info_about_window_t* a);
static int add_info_about_window_xlib(struct wm_add_window_parameters_t* parameters, struct wm_window_source_t* source, struct info_about_window_t* infoAboutWindow)
{
struct
{
Window a;
} window;
int progress = 0;
do
{
// TODO: read xInPixels and yInPixels from file (i.e. restore previous)..
// .. as this appears to be the default on linux..?
int xInPixels = (XDisplayWidth(xlib.display.a, xlib.screen.a) / 2.0f) - (source->widthInPixels / 2.0f);
int yInPixels = (XDisplayHeight(xlib.display.a, xlib.screen.a) / 2.0f) - (source->heightInPixels / 2.0f);
// ^
// "XDisplayWidth, XDisplayHeight [...] really should be named..
// .. Screenwhatever and XScreenwhatever",..
// .. https://x.org/releases/current/doc/libX11/libX11/libX11.html
XSetWindowAttributes a;
a.background_pixel = BlackPixel(xlib.display.a, xlib.screen.a);
a.border_pixel = WhitePixel(xlib.display.a, xlib.screen.a);
a.event_mask = FocusChangeMask | StructureNotifyMask;
// TODO: XSynchronize + XSetAfterFunction + XSetErrorHandler +..
// .. X_CreateWindow (from X11/Xproto.h) <- turn on..
// .. synchronization before adding and turn off after added..?
// ^
// https://cgit.freedesktop.org/xorg/proto/xproto/tree/Xproto.h
window.a = XCreateWindow(xlib.display.a, RootWindow(xlib.display.a, xlib.screen.a), xInPixels, yInPixels, source->widthInPixels, source->heightInPixels, 1, CopyFromParent, CopyFromParent, CopyFromParent, CWBackPixel | CWBorderPixel | CWEventMask, &a);
// NOTE: ^
// seems to completely ignore x and y
progress = EAddInfoAboutWindowXlibProgress_XCreateWindow;
if(source->title != NULL)
{
XStoreName(xlib.display.a, window.a, source->title);
}
// NOTE: "If it cannot intern the WM_PROTOCOLS atom, XSetWMProtocols..
// .. returns a zero status. Otherwise, it returns a nonzero..
// .. status.",..
// .. https://x.org/releases/current/doc/libX11/libX11/libX11.html#XCreateWindow
// NOTE: https://stackoverflow.com/questions/1157364/intercept-wm-delete-window-on-x11
if(XSetWMProtocols(xlib.display.a, window.a, &xlib.wmdeletewindow.a, 1) == 0)
{
if(on_print != NULL)
{
on_printf(stderr, "error: XSetWMProtocols == 0 in %s\n", __FUNCTION__);
}
break;
}
progress = EAddInfoAboutWindowXlibProgress_XSetWMProtocols;
XMapWindow(xlib.display.a, window.a);
} while(0);
if(progress != EAddInfoAboutWindowXlibProgress_All)
{
struct info_about_window_t a;
a.xlib.window.a = window.a;
remove_info_about_window_xlib(progress, &a);
return 0;
}
infoAboutWindow->xlib.window.a = window.a;
return 1;
}
#endif
static int add_info_about_window(struct wm_add_window_parameters_t* parameters, struct wm_window_source_t* source, struct info_about_window_t* infoAboutWindow)
{
#if defined(_WIN32)
if(add_info_about_window_win32(parameters, source, infoAboutWindow) != 1)
{
return 0;
}
#else //< #elif defined(__linux__)
if(add_info_about_window_xlib(parameters, source, infoAboutWindow) != 1)
{
return 0;
}
#endif
infoAboutWindow->source = source;
infoAboutWindow->minWidthInPixels = source->minWidthInPixels;
infoAboutWindow->minHeightInPixels = source->minHeightInPixels;
infoAboutWindow->maxWidthInPixels = source->maxWidthInPixels;
infoAboutWindow->maxHeightInPixels = source->maxHeightInPixels;
infoAboutWindow->widthInPixels = source->widthInPixels;
infoAboutWindow->heightInPixels = source->heightInPixels;
if(source->title != NULL)
{
int titleLength = strlen(source->title);
//infoAboutWindow->title = new char[titleLength + 1];
infoAboutWindow->title = malloc(titleLength + 1);
memcpy(infoAboutWindow->title, source->title, titleLength + 1);
}
else
{
infoAboutWindow->title = NULL;
}
infoAboutWindow->bFullscreen = 0;
if(source->bFullscreen == 1)