-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinx.c
1102 lines (867 loc) · 29.7 KB
/
winx.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 "winx.h"
#define SET_HINT(HINT_ENUM, HINT_VAR) case HINT_ENUM: HINT_VAR = value; break;
#define WINX_CONTEXT_ASSERT(function) if(!winx) { winxErrorMsg = (char*) (function ": No active winx context!"); return; }
// dummy functions
void WinxDummyCursorEventHandle(int x, int y) {}
void WinxDummyButtonEventHandle(int type, int button) {}
void WinxDummyKeyboardEventHandle(int type, int key) {}
void WinxDummyScrollEventHandle(int scroll) {}
void WinxDummyCloseEventHandle() {}
void WinxDummyResizeEventHandle(int width, int height) {}
void WinxDummyFocusEventHandle(bool focus) {}
// hints
static int __winx_hint_vsync = 0;
static int __winx_hint_red_bits = 8;
static int __winx_hint_green_bits = 8;
static int __winx_hint_blue_bits = 8;
static int __winx_hint_alpha_bits = 8;
static int __winx_hint_depth_bits = 24;
static int __winx_hint_stencil_bits = 1;
static int __winx_hint_opengl_major = 3;
static int __winx_hint_opengl_minor = 0;
static int __winx_hint_opengl_core = 1;
static int __winx_hint_opengl_debug = 0;
static int __winx_hint_opengl_robust = 0;
static int __winx_hint_multisamples = 0;
// current error message
static char* winxErrorMsg = NULL;
char* winxGetError() {
char* copy = winxErrorMsg;
winxErrorMsg = NULL;
return copy;
}
void winxHint(int hint, int value) {
switch (hint) {
SET_HINT(WINX_HINT_VSYNC, __winx_hint_vsync);
SET_HINT(WINX_HINT_RED_BITS, __winx_hint_red_bits);
SET_HINT(WINX_HINT_GREEN_BITS, __winx_hint_green_bits);
SET_HINT(WINX_HINT_BLUE_BITS, __winx_hint_blue_bits);
SET_HINT(WINX_HINT_ALPHA_BITS, __winx_hint_alpha_bits);
SET_HINT(WINX_HINT_DEPTH_BITS, __winx_hint_depth_bits);
SET_HINT(WINX_HINT_STENCIL_BITS, __winx_hint_stencil_bits);
SET_HINT(WINX_HINT_OPENGL_MAJOR, __winx_hint_opengl_major);
SET_HINT(WINX_HINT_OPENGL_MINOR, __winx_hint_opengl_minor);
SET_HINT(WINX_HINT_OPENGL_CORE, __winx_hint_opengl_core);
SET_HINT(WINX_HINT_OPENGL_DEBUG, __winx_hint_opengl_debug);
SET_HINT(WINX_HINT_OPENGL_ROBUST, __winx_hint_opengl_robust);
SET_HINT(WINX_HINT_MULTISAMPLES, __winx_hint_multisamples);
}
}
#undef SET_HINT
// begin winx GLX implementation
#if defined(WINX_GLX)
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xcursor/Xcursor.h>
#include <GL/glx.h>
#include <time.h>
// copied from glxext.h
typedef int (*PFNGLXSWAPINTERVALMESAPROC) (unsigned int interval);
typedef void (*PFNGLXSWAPINTERVALEXTPROC) (Display *dpy, GLXDrawable drawable, int interval);
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list);
static PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
static PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
// winx cursor image struct
struct WinxCursor_s {
Cursor native;
};
// winx global state struct
typedef struct {
Display* display;
Window window;
GLXContext context;
Atom wm_delete_window;
Atom net_wm_icon;
Atom cardinal;
bool capture;
time_t time;
WinxCursor* cursor_icon;
WinxCursorEventHandle cursor;
WinxButtonEventHandle button;
WinxKeyboardEventHandle keyboard;
WinxScrollEventHandle scroll;
WinxCloseEventHandle close;
WinxResizeEventHandle resize;
WinxFocusEventHandle focus;
} WinxHandle;
static WinxHandle* winx = NULL;
static __GLXextFuncPtr winxGetProc(const char* name) {
if (winxErrorMsg == NULL) {
__GLXextFuncPtr proc = glXGetProcAddress((const unsigned char*) name);
if (proc == NULL) {
winxErrorMsg = (char*) "glXGetProcAddress: Failed to load function!";
}
return proc;
}
return NULL;
}
static void winxUpdateCursorState(bool captured, WinxCursor* cursor) {
if (captured) {
unsigned int events = ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
XGrabPointer(winx->display, winx->window, true, events, GrabModeAsync, GrabModeAsync, winx->window, None, CurrentTime);
} else {
XUngrabPointer(winx->display, CurrentTime);
}
if (cursor) {
XDefineCursor(winx->display, winx->window, cursor->native);
} else {
XUndefineCursor(winx->display, winx->window);
}
}
bool winxOpen(int width, int height, const char* title) {
winx = (WinxHandle*) calloc(1, sizeof(WinxHandle));
winx->capture = false;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
winx->time = spec.tv_sec;
// set dummy function pointers
winxResetEventHandles();
// get display handle
winx->display = XOpenDisplay(NULL);
if (!winx->display) {
winxErrorMsg = (char*) "XOpenDisplay: Failed to acquire display handle!";
return false;
}
// GLX attributes
int attributes[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_DOUBLEBUFFER, true,
GLX_RED_SIZE, __winx_hint_red_bits,
GLX_GREEN_SIZE, __winx_hint_green_bits,
GLX_BLUE_SIZE, __winx_hint_blue_bits,
GLX_DEPTH_SIZE, __winx_hint_depth_bits,
GLX_STENCIL_SIZE, __winx_hint_stencil_bits,
GLX_SAMPLE_BUFFERS, __winx_hint_multisamples ? 1 : 0,
GLX_SAMPLES, __winx_hint_multisamples,
None
};
int screen = DefaultScreen(winx->display);
Window root = RootWindow(winx->display, screen);
// find frame buffer config matching our attributes
int count;
GLXFBConfig *fbconfigs = glXChooseFBConfig(winx->display, screen, attributes, &count);
if (!fbconfigs || !count) {
winxErrorMsg = (char*) "glXChooseFBConfig: Failed to choose a frame buffer config!";
return false;
}
// find visual based on framebuffer's config
XVisualInfo* info = glXGetVisualFromFBConfig(winx->display, fbconfigs[0]);
if (!info) {
winxErrorMsg = (char*) "glXGetVisualFromFBConfig: Failed to choose a visual!";
return false;
}
// set X11 window attributes
XSetWindowAttributes x11_attributes;
x11_attributes.background_pixel = 0;
x11_attributes.border_pixel = 0;
x11_attributes.colormap = XCreateColormap(winx->display, root, info->visual, AllocNone);
x11_attributes.event_mask =
StructureNotifyMask | ExposureMask | PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask;
unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
// finally create X11 window
winx->window = XCreateWindow(winx->display, root, 0, 0, width, height, 0, info->depth, InputOutput, info->visual, mask, &x11_attributes);
// set name
winxSetTitle(title);
// show X11 window
XMapWindow(winx->display, winx->window);
// create GLX context
GLXContext context = glXCreateContext(winx->display, info, NULL, 1);
if (!context) {
winxErrorMsg = (char*) "glXCreateContext: Failed to create GLX context!";
return false;
}
glXMakeCurrent(winx->display, winx->window, context);
glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) winxGetProc("glXCreateContextAttribsARB");
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC) glXGetProcAddress((const unsigned char*) "glXSwapIntervalEXT"); // optional
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) glXGetProcAddress((const unsigned char*) "glXSwapIntervalMESA"); // optional
if (winxErrorMsg != NULL) {
return false;
}
glXMakeCurrent(winx->display, 0, 0);
glXDestroyContext(winx->display, context);
XFree(info);
int flags = 0;
if (__winx_hint_opengl_debug) flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
if (__winx_hint_opengl_robust) flags |= GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB;
int context_attributes[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, __winx_hint_opengl_major,
GLX_CONTEXT_MINOR_VERSION_ARB, __winx_hint_opengl_minor,
GLX_CONTEXT_PROFILE_MASK_ARB, __winx_hint_opengl_core ? GLX_CONTEXT_CORE_PROFILE_BIT_ARB : GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, flags,
None
};
winx->context = glXCreateContextAttribsARB(winx->display, fbconfigs[0], NULL, true, context_attributes);
if (!winx->context) {
winxErrorMsg = (char*) "glXCreateContextAttribsARB: Failed to create context";
return false;
}
glXMakeCurrent(winx->display, winx->window, winx->context);
// needed for window icon
winx->net_wm_icon = XInternAtom(winx->display, "_NET_WM_ICON", false);
winx->cardinal = XInternAtom(winx->display, "CARDINAL", false);
// needed to handle the close button
winx->wm_delete_window = XInternAtom(winx->display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(winx->display, winx->window, &(winx->wm_delete_window), 1);
// set vsync
winxSetVsync(__winx_hint_vsync);
return true;
}
void winxPollEvents() {
while (XPending(winx->display) > 0) {
XEvent event;
XNextEvent(winx->display, &event);
switch (event.type) {
case ClientMessage:
if ((Atom) event.xclient.data.l[0] == winx->wm_delete_window) {
winx->close();
break;
}
break;
case KeyPress:
winx->keyboard(WINX_PRESSED, XLookupKeysym(&event.xkey, 0));
break;
case KeyRelease:
winx->keyboard(WINX_RELEASED, XLookupKeysym(&event.xkey, 0));
break;
case ButtonPress:
if (event.xbutton.button == Button4) {
winx->scroll(1);
break;
}
if (event.xbutton.button == Button5) {
winx->scroll(-1);
break;
}
winx->button(WINX_PRESSED, event.xbutton.button);
break;
case ButtonRelease:
winx->button(WINX_RELEASED, event.xbutton.button);
break;
case MotionNotify:
winx->cursor(event.xmotion.x, event.xmotion.y);
break;
case ConfigureNotify:
winx->resize(event.xconfigure.width, event.xconfigure.height);
break;
case FocusIn:
winx->focus(true);
winxUpdateCursorState(winx->capture, winx->cursor_icon);
break;
case FocusOut:
winx->focus(false);
winxUpdateCursorState(false, NULL);
break;
default:
break;
}
}
}
void winxSwapBuffers() {
glXSwapBuffers(winx->display, winx->window);
}
void winxClose() {
glXDestroyContext(winx->display, winx->context);
XDestroyWindow(winx->display, winx->window);
XCloseDisplay(winx->display);
free(winx);
winx = NULL;
}
void winxSetTitle(const char* title) {
WINX_CONTEXT_ASSERT("winxSetTitle");
XStoreName(winx->display, winx->window, title);
XSetIconName(winx->display, winx->window, title);
}
void winxSetIcon(int width, int height, unsigned char* buffer) {
WINX_CONTEXT_ASSERT("winxSetIcon");
// We need to convert RGBA byte array to a suported format
// X11 expects the icon in format [[long: width] [long: height] [long: bgra]...]...
const int size = width * height;
unsigned long* icon = (unsigned long*) malloc(sizeof(long) * (size + 2));
icon[0] = width;
icon[1] = height;
for (int i = 0, j = 2; i < size * 4; i += 4) {
icon[j ++] = buffer[i + 2] | buffer[i + 1] << 8 | buffer[i + 0] << 16 | buffer[i + 3] << 24;
}
XChangeProperty(winx->display, winx->window, winx->net_wm_icon, winx->cardinal, 32, PropModeReplace, (const unsigned char*) icon, size + 2);
free(icon);
}
WinxCursor* winxCreateCursorIcon(int width, int height, unsigned char* buffer, int x, int y) {
if (!winx) {
winxErrorMsg = (char*) "winxCreateCursorIcon: No active winx context!";
return NULL;
}
XcursorImage* image = XcursorImageCreate(width, height);
if (!image) {
winxErrorMsg = (char*) "XcursorImageCreate: Failed to create cursor image!";
return NULL;
}
image->xhot = x;
image->yhot = y;
XcursorPixel* pixels = image->pixels;
for (int i = 0, j = 0; i < width * height * 4; i += 4) {
pixels[j ++] = buffer[i + 2] | buffer[i + 1] << 8 | buffer[i + 0] << 16 | buffer[i + 3] << 24;
}
WinxCursor* cursor = (WinxCursor*) malloc(sizeof(WinxCursor));
cursor->native = XcursorImageLoadCursor(winx->display, image);
XcursorImageDestroy(image);
return cursor;
}
WinxCursor* winxCreateNullCursorIcon() {
unsigned char pixels[1 * 1 * 4] = {0, 0, 0, 0};
return winxCreateCursorIcon(1, 1, pixels, 0, 0);
}
void winxDeleteCursorIcon(WinxCursor* cursor) {
if (cursor) {
XFreeCursor(winx->display, cursor->native);
free(cursor);
}
}
void winxSetVsync(int vsync) {
WINX_CONTEXT_ASSERT("winxSetVsync");
if (glXSwapIntervalEXT) {
glXSwapIntervalEXT(winx->display, glXGetCurrentDrawable(), __winx_hint_vsync);
} else {
if (glXSwapIntervalMESA) {
glXSwapIntervalMESA(__winx_hint_vsync == WINX_VSYNC_ADAPTIVE ? WINX_VSYNC_ENABLED : __winx_hint_vsync);
}
}
}
bool winxGetFocus() {
if (!winx) {
winxErrorMsg = (char*) "winxGetFocus: No active winx context!";
return false;
}
Window focused;
int state;
XGetInputFocus(winx->display, &focused, &state);
return focused == winx->window;
}
void winxSetFocus() {
WINX_CONTEXT_ASSERT("winxSetFocus");
XSetInputFocus(winx->display, winx->window, RevertToNone, CurrentTime);
// this doesn't seem to actually bring the window up
// investigate if there is a more reliable solution
XRaiseWindow(winx->display, winx->window);
XFlush(winx->display);
}
void winxSetCursorPos(int x, int y) {
WINX_CONTEXT_ASSERT("winxSetCursorPos");
XWarpPointer(winx->display, None, winx->window, 0, 0, 0, 0, x, y);
XFlush(winx->display);
}
double winxGetTime() {
if (!winx) {
winxErrorMsg = (char*) "winxGetTime: No active winx context!";
return 0;
}
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return (spec.tv_sec - winx->time) + (spec.tv_nsec / (double) 1e+9);
}
#endif // GLX
#if defined(WINX_WINAPI)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <gl/GL.h>
// copied from wglext.h
typedef BOOL(WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
typedef HGLRC(WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);
typedef BOOL(WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
#define WGL_SAMPLE_BUFFERS_ARB 0x2041
#define WGL_SAMPLES_ARB 0x2042
#define WGL_DRAW_TO_WINDOW_ARB 0x2001
#define WGL_SUPPORT_OPENGL_ARB 0x2010
#define WGL_DOUBLE_BUFFER_ARB 0x2011
#define WGL_PIXEL_TYPE_ARB 0x2013
#define WGL_TYPE_RGBA_ARB 0x202B
#define WGL_ACCELERATION_ARB 0x2003
#define WGL_FULL_ACCELERATION_ARB 0x2027
#define WGL_COLOR_BITS_ARB 0x2014
#define WGL_ALPHA_BITS_ARB 0x201B
#define WGL_DEPTH_BITS_ARB 0x2022
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
#define WGL_SWAP_EXCHANGE_ARB 0x2028
#define WGL_SWAP_METHOD_ARB 0x2007
#define WGL_SWAP_COPY_ARB 0x2029
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x00000001
#define WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB 0x00000004
#define WGL_SAMPLE_BUFFERS_ARB 0x2041
#define WGL_SAMPLES_ARB 0x2042
#define WGL_RED_BITS_ARB 0x2015
#define WGL_GREEN_BITS_ARB 0x2017
#define WGL_BLUE_BITS_ARB 0x2019
#define WGL_STENCIL_BITS_ARB 0x2023
static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
// winx cursor image struct
struct WinxCursor_s {
HCURSOR native;
};
// winx global state struct
typedef struct {
HWND hndl;
HDC device;
HGLRC context;
unsigned long long time;
bool capture;
WinxCursor* cursor_icon;
WinxCursorEventHandle cursor;
WinxButtonEventHandle button;
WinxKeyboardEventHandle keyboard;
WinxScrollEventHandle scroll;
WinxCloseEventHandle close;
WinxResizeEventHandle resize;
WinxFocusEventHandle focus;
} WinxHandle;
static WinxHandle* winx = NULL;
static PROC winxGetProc(LPCSTR name) {
if (winxErrorMsg == NULL) {
PROC proc = wglGetProcAddress(name);
if (proc == NULL) {
winxErrorMsg = (char*) "wglGetProcAddress: Failed to load function!";
}
return proc;
}
return NULL;
}
static void winxUpdateCursorState(bool captured, WinxCursor* cursor) {
if (captured) {
RECT rect;
// taken from GLFW
GetClientRect(winx->hndl, &rect);
ClientToScreen(winx->hndl, (POINT*) &rect.left);
ClientToScreen(winx->hndl, (POINT*) &rect.right);
bool success = ClipCursor(&rect);
if (!success) {
winxErrorMsg = (char*) "ClipCursor: Failed to clip cursor!";
}
} else {
ClipCursor(NULL);
}
if (cursor) {
SetCursor(cursor->native);
} else {
SetCursor(LoadCursor(NULL, IDC_ARROW));
}
}
static LRESULT CALLBACK winxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT result = 0;
switch (message) {
case WM_KEYDOWN:
winx->keyboard(WINX_PRESSED, wParam);
break;
case WM_KEYUP:
winx->keyboard(WINX_RELEASED, wParam);
break;
case WM_MOUSEMOVE:
winx->cursor(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
break;
case WM_LBUTTONDOWN:
winx->button(WINX_PRESSED, WXB_LEFT);
break;
case WM_LBUTTONUP:
winx->button(WINX_RELEASED, WXB_LEFT);
break;
case WM_MBUTTONDOWN:
winx->button(WINX_PRESSED, WXB_CENTER);
break;
case WM_MBUTTONUP:
winx->button(WINX_RELEASED, WXB_CENTER);
break;
case WM_RBUTTONDOWN:
winx->button(WINX_PRESSED, WXB_RIGHT);
break;
case WM_RBUTTONUP:
winx->button(WINX_RELEASED, WXB_RIGHT);
break;
case WM_MOUSEWHEEL:
winx->scroll(GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA);
break;
case WM_CLOSE:
winx->close();
break;
case WM_SIZE:
winx->resize(LOWORD(lParam), HIWORD(lParam));
break;
case WM_SETFOCUS:
winx->focus(true);
winxUpdateCursorState(winx->capture, winx->cursor_icon);
break;
case WM_KILLFOCUS:
winx->focus(false);
winxUpdateCursorState(false, NULL);
break;
// needed because yes
// https://docs.microsoft.com/en-us/windows/win32/learnwin32/setting-the-cursor-image
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT) {
winxUpdateCursorState(winx->capture, winx->cursor_icon);
return TRUE;
}
break;
default:
result = DefWindowProcA(hWnd, message, wParam, lParam);
}
return result;
}
bool winxOpen(int width, int height, const char* title) {
winx = (WinxHandle*) calloc(1, sizeof(WinxHandle));
winx->capture = false;
QueryPerformanceCounter((LARGE_INTEGER*) &winx->time);
HINSTANCE hinstance = GetModuleHandle(NULL);
// set dummy function pointers
winxResetEventHandles();
// register window class
const char* clazz = "WinxOpenGLClass";
WNDCLASSEXA wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC) winxWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hinstance;
wcex.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = clazz;
wcex.hIconSm = NULL;
if (!RegisterClassExA(&wcex)) {
winxErrorMsg = (char*) "RegisterClassExA: Failed to register window class!";
return false;
}
HWND fakeHndl;
HDC fakeDeviceContext;
HGLRC fakeRenderContext;
// create temporary window to get WGL context
fakeHndl = CreateWindowA(clazz, "WINX", WS_OVERLAPPEDWINDOW, 0, 0, 1, 1, NULL, NULL, hinstance, NULL);
if (!fakeHndl) {
winxErrorMsg = (char*) "CreateWindowA: Failed to create temporary window!";
return false;
}
fakeDeviceContext = GetDC(fakeHndl);
if (!fakeDeviceContext) {
winxErrorMsg = (char*) "GetDC: Failed to create temporary device context!";
return false;
}
PIXELFORMATDESCRIPTOR descriptor = {0};
descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
descriptor.nVersion = 1;
descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
descriptor.iPixelType = PFD_TYPE_RGBA;
descriptor.cRedBits = __winx_hint_red_bits;
descriptor.cGreenBits = __winx_hint_green_bits;
descriptor.cBlueBits = __winx_hint_blue_bits;
descriptor.cAlphaBits = __winx_hint_alpha_bits;
descriptor.cDepthBits = __winx_hint_depth_bits;
int fakePixelFormat = ChoosePixelFormat(fakeDeviceContext, &descriptor);
if (!fakePixelFormat) {
winxErrorMsg = (char*) "ChoosePixelFormat: Failed to choose a pixel format!";
return false;
}
if (!SetPixelFormat(fakeDeviceContext, fakePixelFormat, &descriptor)) {
winxErrorMsg = (char*) "SetPixelFormat: Failed to select a pixel format!";
return false;
}
fakeRenderContext = wglCreateContext(fakeDeviceContext);
if (!fakeRenderContext) {
winxErrorMsg = (char*) "wglCreateContext: Failed to create temporary render context!";
return false;
}
// open real window
winx->hndl = CreateWindowA(clazz, title, WS_OVERLAPPEDWINDOW, 0, 0, width, height, NULL, NULL, hinstance, NULL);
if (!winx->hndl) {
winxErrorMsg = (char*) "CreateWindowA: Failed to create window!";
return false;
}
// create context
winx->device = GetDC(winx->hndl);
if (!winx->device) {
winxErrorMsg = (char*) "GetDC: Failed to create device context!";
return false;
}
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
if (!wglMakeCurrent(fakeDeviceContext, fakeRenderContext)) {
winxErrorMsg = (char*) "wglMakeCurrent: Failed to select temporary context!";
return false;
}
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC) winxGetProc("wglChoosePixelFormatARB");
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) winxGetProc("wglCreateContextAttribsARB");
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT"); // optional
if (winxErrorMsg != NULL) {
// winxGetProc set the error message
return false;
}
int pixelFormat;
BOOL status;
UINT numFormats;
const int pixelAttribs[] = {
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_RED_BITS_ARB, __winx_hint_red_bits,
WGL_GREEN_BITS_ARB, __winx_hint_green_bits,
WGL_BLUE_BITS_ARB, __winx_hint_blue_bits,
WGL_ALPHA_BITS_ARB, __winx_hint_alpha_bits,
WGL_DEPTH_BITS_ARB, __winx_hint_depth_bits,
WGL_STENCIL_BITS_ARB, __winx_hint_stencil_bits,
WGL_SAMPLE_BUFFERS_ARB, __winx_hint_multisamples ? 1 : 0,
WGL_SAMPLES_ARB, __winx_hint_multisamples,
0
};
int flags = 0;
if (__winx_hint_opengl_debug) flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
if (__winx_hint_opengl_robust) flags |= WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB;
int contextAttributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, __winx_hint_opengl_major,
WGL_CONTEXT_MINOR_VERSION_ARB, __winx_hint_opengl_minor,
WGL_CONTEXT_PROFILE_MASK_ARB, __winx_hint_opengl_core ? WGL_CONTEXT_CORE_PROFILE_BIT_ARB : WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
WGL_CONTEXT_FLAGS_ARB, flags,
0
};
status = wglChoosePixelFormatARB(winx->device, pixelAttribs, NULL, 1, &pixelFormat, &numFormats);
if (status && numFormats) {
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
DescribePixelFormat(winx->device, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (!SetPixelFormat(winx->device, pixelFormat, &pfd)) {
winxErrorMsg = (char*) "SetPixelFormat: Failed to select a pixel format!";
return false;
}
winx->context = wglCreateContextAttribsARB(winx->device, 0, contextAttributes);
if (!winx->context) {
winxErrorMsg = (char*) "wglCreateContextAttribsARB: Failed to create a render context!";
return false;
}
} else {
winxErrorMsg = (char*) "wglChoosePixelFormatARB: Failed to choose a pixel format!";
return false;
}
// close temporary window
wglMakeCurrent(fakeDeviceContext, NULL);
wglDeleteContext(fakeRenderContext);
ReleaseDC(fakeHndl, fakeDeviceContext);
DestroyWindow(fakeHndl);
fakeDeviceContext = NULL;
fakeRenderContext = NULL;
fakeHndl = NULL;
wglMakeCurrent(winx->device, winx->context);
// set vsync
winxSetVsync(__winx_hint_vsync);
// finish window creation
ShowWindow(winx->hndl, 1);
UpdateWindow(winx->hndl);
return true;
}
void winxPollEvents() {
MSG event;
while (PeekMessageA(&event, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&event);
DispatchMessage(&event);
}
}
void winxSwapBuffers() {
SwapBuffers(winx->device);
}
void winxClose() {
wglMakeCurrent(winx->device, NULL);
wglDeleteContext(winx->context);
ReleaseDC(winx->hndl, winx->device);
DestroyWindow(winx->hndl);
free(winx);
winx = NULL;
}
void winxSetTitle(const char* title) {
WINX_CONTEXT_ASSERT("winxSetTitle");
SetWindowTextA(winx->hndl, title);
}
static HICON winxCreateIcon(int width, int height, unsigned char* buffer, bool is_icon, int x_hot, int y_hot) {
char* target = NULL;
BITMAPV5HEADER header = {0};
header.bV5Size = sizeof(header);
header.bV5Width = width;
header.bV5Height = -height;
header.bV5Planes = 1;
header.bV5BitCount = 32;
header.bV5Compression = BI_BITFIELDS;
// the order needs to be presisly like that
// otherwise alpha doesn't work
header.bV5RedMask = 0x00ff0000;
header.bV5GreenMask = 0x0000ff00;
header.bV5BlueMask = 0x000000ff;
header.bV5AlphaMask = 0xff000000;
HDC dc = GetDC(NULL);
if (!dc) {
winxErrorMsg = (char*) "GetDC: Failed to acquire drawable!";
return NULL;
}
HBITMAP mask = CreateBitmap(width, height, 1, 1, NULL);
HBITMAP color = CreateDIBSection(dc, (BITMAPINFO*) &header, DIB_RGB_COLORS, (void**) &target, NULL, 0);
ReleaseDC(NULL, dc);
for (int i = 0; i < width * height; i++) {
target[0] = buffer[0];
target[1] = buffer[1];
target[2] = buffer[2];
target[3] = buffer[3];
target += 4;
buffer += 4;
}
ICONINFO info = {0};
info.fIcon = is_icon;
info.xHotspot = x_hot;
info.yHotspot = y_hot;
info.hbmMask = mask;
info.hbmColor = color;
HICON icon = CreateIconIndirect(&info);
DeleteObject(color);
DeleteObject(mask);
if (!icon) {
winxErrorMsg = (char*) "CreateIconIndirect: Failed to create icon handle!";
return NULL;
}
return icon;
}
void winxSetIcon(int width, int height, unsigned char* buffer) {
WINX_CONTEXT_ASSERT("winxSetIcon");
HICON largeIcon, smallIcon;
if (!buffer) {
largeIcon = (HICON) GetClassLongPtrW(winx->hndl, GCLP_HICON);
smallIcon = (HICON) GetClassLongPtrW(winx->hndl, GCLP_HICONSM);
} else {
HICON icon = winxCreateIcon(width, height, buffer, true, 0, 0);
if (icon == NULL) {
// don't set the error message as it must
// have been already set by winxCreateIcon
return;
}
largeIcon = icon;
smallIcon = icon;
}
SendMessage(winx->hndl, WM_SETICON, ICON_BIG, (LPARAM) largeIcon);
SendMessage(winx->hndl, WM_SETICON, ICON_SMALL, (LPARAM) smallIcon);
}
WinxCursor* winxCreateCursorIcon(int width, int height, unsigned char* buffer, int x, int y) {
if (buffer == NULL) {
return NULL;
}
if (!winx) {
winxErrorMsg = (char*) "winxCreateCursorIcon: No active winx context!";
return NULL;
}
WinxCursor* cursor = (WinxCursor*) malloc(sizeof(WinxCursor));
cursor->native = winxCreateIcon(width, height, buffer, false, x, y);
return cursor;
}
WinxCursor* winxCreateNullCursorIcon() {
WinxCursor* cursor = (WinxCursor*) malloc(sizeof(WinxCursor));
cursor->native = NULL;
return cursor;
}
void winxDeleteCursorIcon(WinxCursor* cursor) {
if (cursor) {
if (cursor->native) {
DestroyIcon((HICON) cursor->native);
}
free(cursor);
}
}
void winxSetVsync(int vsync) {
WINX_CONTEXT_ASSERT("winxSetVsync");
if (wglSwapIntervalEXT) {
wglSwapIntervalEXT(__winx_hint_vsync);
}
}
bool winxGetFocus() {
if (!winx) {
winxErrorMsg = (char*) "winxGetFocus: No active winx context!";
return false;