-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.h
More file actions
515 lines (454 loc) · 17.3 KB
/
window.h
File metadata and controls
515 lines (454 loc) · 17.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
//personal fork of fenster.h (https://github.com/zserge/fenster)
/*
This is very much a work in progress and might be buggy.
It does not create any kind of graphics context, also it has no buffer for rendering stuff.
It really just opens a window, the rest is up to you.
It should work on Windows, Linux (link against X11), and macOS (link against Cocoa: -framework Cocoa).
*/
#ifndef WINDOW_HEADER
#define WINDOW_HEADER
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment(lib, "user32.lib")
#elif defined(__APPLE__)
#include <CoreGraphics/CoreGraphics.h>
#include <objc/NSObjCRuntime.h>
#include <objc/objc-runtime.h>
#include <time.h>
#elif defined(__linux__)
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <time.h>
#else
#pragma error "[window.h] Unsupported platform!"
#endif
#ifndef WINDOW_IMPL
#define WINDOW_FN extern
#else
#define WINDOW_FN
#endif
#include <stdint.h>
#include <stdbool.h>
typedef struct window_desc_t {
const char *title;
int width;
int height;
} window_desc_t;
typedef struct window_t {
const char *title;
int width;
int height;
uint8_t keys[256];
uint8_t prev_keys[256];
int mod;
int x;
int y;
int mouse;
bool fullscreen;
bool resized;
#if defined(_WIN32)
HWND win;
#elif defined(__APPLE__)
id wnd;
#elif defined(__linux__)
Display *dpy;
Window win;
#endif
} window_t;
WINDOW_FN bool window_open(window_t *w, const window_desc_t *desc);
WINDOW_FN bool window_loop(window_t *w);
WINDOW_FN void window_close(window_t *w);
WINDOW_FN void window_toggle_fullscreen(window_t *w);
WINDOW_FN bool window_key_pressed(window_t *w, int key);
WINDOW_FN bool window_key_down(window_t *w, int key);
WINDOW_FN bool window_key_released(window_t *w, int key);
WINDOW_FN void window_sleep(int64_t ms);
WINDOW_FN int64_t window_time(void);
WINDOW_FN bool window_resized(window_t *w);
#endif //WINDOW_HEADER
#ifdef WINDOW_IMPL
#if defined(_WIN32)
static const uint8_t FENSTER_KEYCODES[] = {0,27,49,50,51,52,53,54,55,56,57,48,45,61,8,9,81,87,69,82,84,89,85,73,79,80,91,93,10,0,65,83,68,70,71,72,74,75,76,59,39,96,0,92,90,88,67,86,66,78,77,44,46,47,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,17,3,0,20,0,19,0,5,18,4,26,127};
static LRESULT CALLBACK window_wndproc(HWND win, UINT msg, WPARAM wParam, LPARAM lParam) {
window_t* w = (window_t *)GetWindowLongPtr(win, GWLP_USERDATA);
switch (msg) {
case WM_CLOSE:
DestroyWindow(win);
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
w->mouse = (msg == WM_LBUTTONDOWN);
break;
case WM_MOUSEMOVE:
w->y = HIWORD(lParam), w->x = LOWORD(lParam);
break;
case WM_KEYDOWN:
case WM_KEYUP: {
w->mod = ((GetKeyState(VK_CONTROL) & 0x8000) >> 15) |
((GetKeyState(VK_SHIFT) & 0x8000) >> 14) |
((GetKeyState(VK_MENU) & 0x8000) >> 13) |
(((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x8000) >> 12);
w->keys[FENSTER_KEYCODES[HIWORD(lParam) & 0x1ff]] = !((lParam >> 31) & 1);
} break;
case WM_SIZE:
w->width = LOWORD(lParam);
w->height = HIWORD(lParam);
w->resized = true;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(win, msg, wParam, lParam);
}
return 0;
}
bool window_open(window_t* w, const window_desc_t* desc) {
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = window_wndproc;
wc.hInstance = hInstance;
wc.lpszClassName = desc->title;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&wc);
w->win = CreateWindowEx(WS_EX_CLIENTEDGE, desc->title, desc->title,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
desc->width, desc->height, NULL, NULL, hInstance, NULL);
if (w->win == NULL) return false;
SetWindowLongPtr(w->win, GWLP_USERDATA, (LONG_PTR)w);
ShowWindow(w->win, SW_NORMAL);
UpdateWindow(w->win);
w->width = desc->width;
w->height = desc->height;
w->title = desc->title;
w->resized = false;
return true;
}
void window_close(window_t *w) { (void)w; }
bool window_loop(window_t *w) {
(void)w;
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) return false;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
bool window_key_down(window_t *w, int key) {
return w->keys[key] != 0;
}
bool window_key_pressed(window_t *w, int key) {
bool pressed = w->keys[key] && !w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return pressed;
}
bool window_key_released(window_t *w, int key) {
bool released = !w->keys[key] && w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return released;
}
bool window_resized(window_t *w) {
if (w->resized) {
w->resized = false;
return true;
}
return false;
}
void window_toggle_fullscreen(window_t *w) {
static RECT saved_rect;
HWND hwnd = w->win;
if (!w->fullscreen) {
// Save current window position and size
GetWindowRect(hwnd, &saved_rect);
// Remove window borders and extended styles, then go fullscreen
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowLong(hwnd, GWL_EXSTYLE, 0);
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mi);
SetWindowPos(hwnd, HWND_TOP,
mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
w->fullscreen = true;
} else {
// Restore window borders and position
SetWindowLong(hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE);
SetWindowPos(hwnd, HWND_NOTOPMOST,
saved_rect.left, saved_rect.top,
saved_rect.right - saved_rect.left,
saved_rect.bottom - saved_rect.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
w->fullscreen = false;
}
}
void window_sleep(int64_t ms) { Sleep((DWORD)ms); }
int64_t window_time() {
LARGE_INTEGER freq, count;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count);
return (int64_t)(count.QuadPart * 1000.0 / freq.QuadPart);
}
#elif defined(__APPLE__)
#define msg(r, o, s) ((r(*)(id, SEL))objc_msgSend)(o, sel_getUid(s))
#define msg1(r, o, s, A, a) \
((r(*)(id, SEL, A))objc_msgSend)(o, sel_getUid(s), a)
#define msg2(r, o, s, A, a, B, b) \
((r(*)(id, SEL, A, B))objc_msgSend)(o, sel_getUid(s), a, b)
#define msg3(r, o, s, A, a, B, b, C, c) \
((r(*)(id, SEL, A, B, C))objc_msgSend)(o, sel_getUid(s), a, b, c)
#define msg4(r, o, s, A, a, B, b, C, c, D, d) \
((r(*)(id, SEL, A, B, C, D))objc_msgSend)(o, sel_getUid(s), a, b, c, d)
#define cls(x) ((id)objc_getClass(x))
extern id const NSDefaultRunLoopMode;
extern id const NSApp;
static const uint8_t FENSTER_KEYCODES[] = {65,83,68,70,72,71,90,88,67,86,0,66,
81,87,69,82,89,84,49,50,51,52,54,53,61,57,55,45,56,48,93,79,85,91,73,80,10,76,
74,39,75,59,92,44,47,78,77,46,9,32,96,8,0,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,26,2,3,127,0,5,0,4,0,20,19,18,17};
static BOOL window_should_close(id v, SEL s, id w) {
(void)v, (void)s, (void)w;
msg1(void, NSApp, "terminate:", id, NSApp);
return YES;
}
static void window_did_resize(id v, SEL s, id notification) {
(void)s;
id win = msg(id, notification, "object");
window_t *w = (window_t *)objc_getAssociatedObject(v, "window");
if (w) {
CGRect frame = msg(CGRect, msg(id, win, "contentView"), "frame");
w->width = (int)frame.size.width;
w->height = (int)frame.size.height;
w->resized = true;
}
}
bool window_open(window_t* w, const window_desc_t* desc) {
msg(id, cls("NSApplication"), "sharedApplication");
msg1(void, NSApp, "setActivationPolicy:", NSInteger, 0);
w->wnd = msg4(id, msg(id, cls("NSWindow"), "alloc"),
"initWithContentRect:styleMask:backing:defer:", CGRect,
CGRectMake(0, 0, desc->width, desc->height), NSUInteger, 15,
NSUInteger, 2, BOOL, NO);
Class windelegate =
objc_allocateClassPair((Class)cls("NSObject"), "WindowDelegate", 0);
class_addMethod(windelegate, sel_getUid("windowShouldClose:"),
(IMP)window_should_close, "c@:@");
class_addMethod(windelegate, sel_getUid("windowDidResize:"),
(IMP)window_did_resize, "v@:@");
objc_registerClassPair(windelegate);
id delegate = msg(id, msg(id, (id)windelegate, "alloc"), "init");
objc_setAssociatedObject(delegate, "window", (id)w, OBJC_ASSOCIATION_ASSIGN);
msg1(void, w->wnd, "setDelegate:", id, delegate);
id title = msg1(id, cls("NSString"), "stringWithUTF8String:", const char *,
desc->title);
msg1(void, w->wnd, "setTitle:", id, title);
// Enable fullscreen support (NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7 = 128)
msg1(void, w->wnd, "setCollectionBehavior:", NSUInteger, 128);
msg1(void, w->wnd, "makeKeyAndOrderFront:", id, nil);
msg(void, w->wnd, "center");
msg1(void, NSApp, "activateIgnoringOtherApps:", BOOL, YES);
w->width = desc->width;
w->height = desc->height;
w->title = desc->title;
w->resized = false;
w->fullscreen = false;
return true;
}
void window_close(window_t* w) {
msg(void, w->wnd, "close");
}
bool window_loop(window_t* w) {
id ev = msg4(id, NSApp,
"nextEventMatchingMask:untilDate:inMode:dequeue:", NSUInteger,
NSUIntegerMax, id, NULL, id, NSDefaultRunLoopMode, BOOL, YES);
if (!ev)
return true;
NSUInteger evtype = msg(NSUInteger, ev, "type");
switch (evtype) {
case 1: /* NSEventTypeMouseDown */
w->mouse |= 1;
break;
case 2: /* NSEventTypeMouseUp*/
w->mouse &= ~1;
break;
case 5:
case 6: { /* NSEventTypeMouseMoved */
CGPoint xy = msg(CGPoint, ev, "locationInWindow");
w->x = (int)xy.x;
w->y = (int)(w->height - xy.y);
break;
}
case 10: /*NSEventTypeKeyDown*/
case 11: /*NSEventTypeKeyUp:*/ {
NSUInteger k = msg(NSUInteger, ev, "keyCode");
if (k < sizeof(FENSTER_KEYCODES)) {
w->keys[FENSTER_KEYCODES[k]] = evtype == 10;
}
NSUInteger mod = msg(NSUInteger, ev, "modifierFlags") >> 17;
w->mod = (mod & 0xc) | ((mod & 1) << 1) | ((mod >> 1) & 1);
break;
}
}
msg1(void, NSApp, "sendEvent:", id, ev);
return true;
}
bool window_key_down(window_t *w, int key) {
return w->keys[key] != 0;
}
bool window_key_pressed(window_t *w, int key) {
bool pressed = w->keys[key] && !w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return pressed;
}
bool window_key_released(window_t *w, int key) {
bool released = !w->keys[key] && w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return released;
}
bool window_resized(window_t *w) {
if (w->resized) {
w->resized = false;
return true;
}
return false;
}
void window_toggle_fullscreen(window_t *w) {
msg1(void, w->wnd, "toggleFullScreen:", id, nil);
w->fullscreen = !w->fullscreen;
}
void window_sleep(int64_t ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
int64_t window_time(void) {
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return time.tv_sec * 1000 + (time.tv_nsec / 1000000);
}
#elif defined(__linux__)
static int FENSTER_KEYCODES[124] = {XK_BackSpace,8,XK_Delete,127,XK_Down,18,XK_End,5,XK_Escape,27,XK_Home,2,XK_Insert,26,XK_Left,20,XK_Page_Down,4,XK_Page_Up,3,XK_Return,10,XK_Right,19,XK_Tab,9,XK_Up,17,XK_apostrophe,39,XK_backslash,92,XK_bracketleft,91,XK_bracketright,93,XK_comma,44,XK_equal,61,XK_grave,96,XK_minus,45,XK_period,46,XK_semicolon,59,XK_slash,47,XK_space,32,XK_a,65,XK_b,66,XK_c,67,XK_d,68,XK_e,69,XK_f,70,XK_g,71,XK_h,72,XK_i,73,XK_j,74,XK_k,75,XK_l,76,XK_m,77,XK_n,78,XK_o,79,XK_p,80,XK_q,81,XK_r,82,XK_s,83,XK_t,84,XK_u,85,XK_v,86,XK_w,87,XK_x,88,XK_y,89,XK_z,90,XK_0,48,XK_1,49,XK_2,50,XK_3,51,XK_4,52,XK_5,53,XK_6,54,XK_7,55,XK_8,56,XK_9,57};
bool window_open(window_t* w, const window_desc_t* desc) {
w->dpy = XOpenDisplay(NULL);
int screen = DefaultScreen(w->dpy);
w->win = XCreateSimpleWindow(
w->dpy,
RootWindow(w->dpy, screen), 0, 0, desc->width,
desc->height, 0, BlackPixel(w->dpy, screen),
WhitePixel(w->dpy, screen)
);
XSelectInput(w->dpy, w->win,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
XStoreName(w->dpy, w->win, desc->title);
w->title = desc->title;
// Setup WM_DELETE_WINDOW protocol
Atom wm_delete = XInternAtom(w->dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(w->dpy, w->win, &wm_delete, 1);
XMapWindow(w->dpy, w->win);
XSync(w->dpy, w->win);
w->resized = false;
return true;
}
void window_close(window_t* w) { XCloseDisplay(w->dpy);}
bool window_loop(window_t* w) {
XEvent ev;
Atom wm_delete = XInternAtom(w->dpy, "WM_DELETE_WINDOW", False);
while (XPending(w->dpy)) {
XNextEvent(w->dpy, &ev);
switch (ev.type) {
case ConfigureNotify:
w->width = ev.xconfigure.width;
w->height = ev.xconfigure.height;
w->resized = true;
break;
case ButtonPress:
case ButtonRelease:
w->mouse = (ev.type == ButtonPress);
break;
case MotionNotify:
w->x = ev.xmotion.x, w->y = ev.xmotion.y;
break;
case KeyPress:
case KeyRelease: {
int m = ev.xkey.state;
int k = XkbKeycodeToKeysym(w->dpy, ev.xkey.keycode, 0, 0);
for (unsigned int i = 0; i < 124; i += 2) {
if (FENSTER_KEYCODES[i] == k) {
w->keys[FENSTER_KEYCODES[i + 1]] = (ev.type == KeyPress);
break;
}
}
w->mod = (!!(m & ControlMask)) | (!!(m & ShiftMask) << 1) |
(!!(m & Mod1Mask) << 2) | (!!(m & Mod4Mask) << 3);
} break;
case ClientMessage:
if ((Atom)ev.xclient.data.l[0] == wm_delete) {
return false;
}
break;
}
}
return true;
}
void window_toggle_fullscreen(window_t *w) {
static int is_fullscreen = 0;
Display *dpy = w->dpy;
Window win = w->win;
Atom wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
XEvent xev = {0};
xev.type = ClientMessage;
xev.xclient.window = win;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = is_fullscreen ? 0 : 1; // 1 = add, 0 = remove
xev.xclient.data.l[1] = fullscreen;
xev.xclient.data.l[2] = 0;
xev.xclient.data.l[3] = 1;
xev.xclient.data.l[4] = 0;
XSendEvent(dpy, DefaultRootWindow(dpy), False,
SubstructureNotifyMask, &xev);
is_fullscreen = !is_fullscreen;
}
bool window_key_down(window_t *w, int key) {
return w->keys[key] != 0;
}
bool window_key_pressed(window_t *w, int key) {
bool pressed = w->keys[key] && !w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return pressed;
}
bool window_key_released(window_t *w, int key) {
bool released = !w->keys[key] && w->prev_keys[key];
w->prev_keys[key] = w->keys[key];
return released;
}
bool window_resized(window_t *w) {
if (w->resized) {
w->resized = false;
return true;
}
return false;
}
void window_sleep(int64_t ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
int64_t window_time(void) {
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return time.tv_sec * 1000 + (time.tv_nsec / 1000000);
}
#endif
#endif //WINDOW_IMPL