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
|
//- helpers
internal void x11_button_update(PlatformButton *button, b32 down)
{
if(button->ended_down != down) {
button->ended_down = down;
++button->half_transition_count;
}
}
internal u32 x11_mods(u32 state)
{
u32 mods = 0;
if(state & ShiftMask) mods |= PLATFORM_MOD_SHIFT;
if(state & ControlMask) mods |= PLATFORM_MOD_CTRL;
if(state & Mod1Mask) mods |= PLATFORM_MOD_ALT;
return mods;
}
internal u32 x11_scale_for_dpi(f32 dpi)
{
u32 scale = (u32)(dpi / 96.0f + 0.5f);
return scale ? scale : 1;
}
// NOTE: Xft.dpi from the resource database is what every toolkit uses for hidpi on x11
internal f32 x11_read_dpi(Display *display)
{
const char *resources = XResourceManagerString(display);
if(resources) {
const char *at = strstr(resources, "Xft.dpi:");
if(at) {
f32 dpi = (f32)atof(at + 8);
if(dpi >= 48.0f && dpi <= 480.0f) return dpi;
}
}
return 96.0f;
}
internal void x11_update_size(X11State *state, u32 physical_width, u32 physical_height)
{
u32 width = physical_width / state->scale;
u32 height = physical_height / state->scale;
if(physical_width != state->physical_width || physical_height != state->physical_height) {
state->physical_width = physical_width;
state->physical_height = physical_height;
state->width = width;
state->height = height;
state->resized = true;
}
}
internal void x11_send_client_message(X11State *state, Window target, Atom type, long l0, long l1, long l2, long l3, long l4)
{
XEvent event = {};
event.xclient.type = ClientMessage;
event.xclient.window = target;
event.xclient.message_type = type;
event.xclient.format = 32;
event.xclient.data.l[0] = l0;
event.xclient.data.l[1] = l1;
event.xclient.data.l[2] = l2;
event.xclient.data.l[3] = l3;
event.xclient.data.l[4] = l4;
XSendEvent(state->display, target, False, NoEventMask, &event);
}
// NOTE: window manager requests (maximize, move) go to the root window
internal void x11_send_wm_message(X11State *state, Atom type, long l0, long l1, long l2, long l3, long l4)
{
XEvent event = {};
event.xclient.type = ClientMessage;
event.xclient.window = state->window;
event.xclient.message_type = type;
event.xclient.format = 32;
event.xclient.data.l[0] = l0;
event.xclient.data.l[1] = l1;
event.xclient.data.l[2] = l2;
event.xclient.data.l[3] = l3;
event.xclient.data.l[4] = l4;
XSendEvent(state->display, state->root, False, SubstructureNotifyMask | SubstructureRedirectMask, &event);
}
//- drag and drop (XDND v5)
internal void x11_xdnd_enter(X11State *state, XClientMessageEvent *message)
{
state->xdnd_source = (Window)message->data.l[0];
state->xdnd_version = (u32)((unsigned long)message->data.l[1] >> 24);
state->xdnd_accept = false;
if(message->data.l[1] & 1) {
// NOTE: more than three types, the full list is a property on the source window
Atom type;
int format;
unsigned long count = 0, remaining = 0;
unsigned char *data = NULL;
if(XGetWindowProperty(state->display, state->xdnd_source, state->xdnd_type_list, 0, 1024, False, XA_ATOM,
&type, &format, &count, &remaining, &data) == Success && data) {
Atom *atoms = (Atom *)data;
for(unsigned long index = 0; index < count; ++index) if(atoms[index] == state->uri_list) state->xdnd_accept = true;
XFree(data);
}
} else {
for(u32 index = 2; index <= 4; ++index) if((Atom)message->data.l[index] == state->uri_list) state->xdnd_accept = true;
}
}
internal void x11_xdnd_position(X11State *state, XClientMessageEvent *message)
{
int root_x = (int)((unsigned long)message->data.l[2] >> 16), root_y = (int)(message->data.l[2] & 0xFFFF);
int x = 0, y = 0;
Window child;
XTranslateCoordinates(state->display, state->root, state->window, root_x, root_y, &x, &y, &child);
state->xdnd_x = (f32)x / (f32)state->scale;
state->xdnd_y = (f32)y / (f32)state->scale;
x11_send_client_message(state, state->xdnd_source, state->xdnd_status, (long)state->window, state->xdnd_accept ? 1 : 0, 0, 0,
state->xdnd_accept ? (long)state->xdnd_action_copy : 0);
}
internal void x11_xdnd_drop(X11State *state, XClientMessageEvent *message)
{
if(!state->xdnd_accept) {
x11_send_client_message(state, state->xdnd_source, state->xdnd_finished, (long)state->window, 0, 0, 0, 0);
return;
}
Time time = (state->xdnd_version >= 1) ? (Time)message->data.l[2] : CurrentTime;
XConvertSelection(state->display, state->xdnd_selection, state->uri_list, state->xdnd_selection, state->window, time);
}
internal void x11_xdnd_selection(X11State *state, XSelectionEvent *selection)
{
if(selection->property != state->xdnd_selection) return;
Atom type;
int format;
unsigned long count = 0, remaining = 0;
unsigned char *data = NULL;
if(XGetWindowProperty(state->display, state->window, state->xdnd_selection, 0, PLATFORM_MAX_DROPS * PLATFORM_DROP_PATH_SIZE / 4,
True, AnyPropertyType, &type, &format, &count, &remaining, &data) == Success && data) {
linux_parse_uri_list(&state->input, (const char *)data, (u64)count);
state->input.drop_x = state->xdnd_x;
state->input.drop_y = state->xdnd_y;
state->input.changed = true;
XFree(data);
}
if(state->xdnd_version >= 2 && state->xdnd_source) {
x11_send_client_message(state, state->xdnd_source, state->xdnd_finished, (long)state->window, 1, (long)state->xdnd_action_copy, 0, 0);
}
state->xdnd_source = 0;
}
//- events
internal b32 x11_key_is_down(X11State *state, u32 keycode)
{
return (state->keys_down[(keycode >> 3) & 31] >> (keycode & 7)) & 1;
}
internal void x11_set_key_down(X11State *state, u32 keycode, b32 down)
{
u8 bit = (u8)(1u << (keycode & 7));
if(down) state->keys_down[(keycode >> 3) & 31] |= bit;
else state->keys_down[(keycode >> 3) & 31] &= (u8)~bit;
}
internal void x11_handle_key(X11State *state, XKeyEvent *key)
{
PlatformInput *input = &state->input;
b32 repeat = x11_key_is_down(state, key->keycode);
x11_set_key_down(state, key->keycode, true);
char text[32];
KeySym keysym = NoSymbol;
int length = 0;
if(state->ic) {
Status status = 0;
length = Xutf8LookupString(state->ic, key, text, sizeof(text) - 1, &keysym, &status);
if(status != XLookupChars && status != XLookupBoth) length = 0;
if(status != XLookupKeySym && status != XLookupBoth) {
// NOTE: XLookupChars alone (composed text): still want a keysym for the key event
if(status == XLookupChars) keysym = NoSymbol;
}
} else {
length = XLookupString(key, text, sizeof(text) - 1, &keysym, NULL);
}
if(keysym == NoSymbol) {
// NOTE: shift aware like xkb_state_key_get_one_sym: index 1 is the shifted level
keysym = XLookupKeysym(key, (key->state & ShiftMask) ? 1 : 0);
}
u32 mods = x11_mods(key->state);
input->mods = mods;
// NOTE: XK_ISO_Left_Tab is what xkb gives for shift+tab too, XLookupKeysym may give plain tab
if(keysym == XK_Tab && (mods & PLATFORM_MOD_SHIFT)) keysym = XK_ISO_Left_Tab;
if(keysym != NoSymbol && input->key_count < PLATFORM_MAX_KEY_EVENTS) {
PlatformKeyEvent *event = &input->keys[input->key_count++];
event->keysym = (u32)keysym;
event->mods = mods;
event->repeat = repeat;
}
if(length > 0 && !(mods & (PLATFORM_MOD_CTRL | PLATFORM_MOD_ALT)) && (u8)text[0] >= 0x20 && text[0] != 0x7f &&
input->text_size + (u32)length <= sizeof(input->text)) {
MemCpy(input->text + input->text_size, text, (u64)length);
input->text_size += (u32)length;
}
}
internal void x11_handle_event(X11State *state, XEvent *event)
{
PlatformInput *input = &state->input;
if(XFilterEvent(event, None)) return;
switch(event->type) {
case KeyPress: {
input->changed = true;
x11_handle_key(state, &event->xkey);
} break;
case KeyRelease: {
input->changed = true;
x11_set_key_down(state, event->xkey.keycode, false);
input->mods = x11_mods(event->xkey.state);
} break;
case ButtonPress:
case ButtonRelease: {
input->changed = true;
b32 down = (event->type == ButtonPress);
u32 button = event->xbutton.button;
input->mouse_x = (f32)event->xbutton.x / (f32)state->scale;
input->mouse_y = (f32)event->xbutton.y / (f32)state->scale;
input->mods = x11_mods(event->xbutton.state);
if(button == Button1) x11_button_update(&input->mouse[0], down);
else if(button == Button3) x11_button_update(&input->mouse[1], down);
else if(button == Button2) x11_button_update(&input->mouse[2], down);
else if(down && button == Button4) input->scroll_y -= 1.0f;
else if(down && button == Button5) input->scroll_y += 1.0f;
else if(down && button == 6) input->scroll_x -= 1.0f;
else if(down && button == 7) input->scroll_x += 1.0f;
} break;
case MotionNotify: {
input->changed = true;
input->mouse_in_window = true;
input->mouse_x = (f32)event->xmotion.x / (f32)state->scale;
input->mouse_y = (f32)event->xmotion.y / (f32)state->scale;
} break;
case EnterNotify: {
input->changed = true;
input->mouse_in_window = true;
input->mouse_x = (f32)event->xcrossing.x / (f32)state->scale;
input->mouse_y = (f32)event->xcrossing.y / (f32)state->scale;
} break;
case LeaveNotify: {
input->changed = true;
input->mouse_in_window = false;
// NOTE: x11 keeps an implicit grab while a button is held, a leave then isn't a release
if(!(event->xcrossing.state & (Button1Mask | Button2Mask | Button3Mask))) {
for(u32 index = 0; index < ArrayCount(input->mouse); ++index) x11_button_update(&input->mouse[index], false);
}
} break;
case FocusIn: {
input->changed = true;
input->focused = true;
if(state->ic) XSetICFocus(state->ic);
} break;
case FocusOut: {
input->changed = true;
input->focused = false;
input->mods = 0;
MemSetZero(state->keys_down, sizeof(state->keys_down));
if(state->ic) XUnsetICFocus(state->ic);
} break;
case ConfigureNotify: {
input->changed = true;
x11_update_size(state, (u32)event->xconfigure.width, (u32)event->xconfigure.height);
} break;
case MapNotify: state->suspended = false; input->changed = true; break;
case UnmapNotify: state->suspended = true; input->changed = true; break;
case Expose: input->changed = true; break;
case PropertyNotify: {
if(event->xproperty.atom == state->net_wm_state) {
Atom type;
int format;
unsigned long count = 0, remaining = 0;
unsigned char *data = NULL;
state->maximized = false;
if(XGetWindowProperty(state->display, state->window, state->net_wm_state, 0, 64, False, XA_ATOM, &type, &format,
&count, &remaining, &data) == Success && data) {
Atom *atoms = (Atom *)data;
for(unsigned long index = 0; index < count; ++index) {
if(atoms[index] == state->net_wm_state_maximized_vert) state->maximized = true;
}
XFree(data);
}
}
} break;
case ClientMessage: {
XClientMessageEvent *message = &event->xclient;
if(message->message_type == state->wm_protocols && (Atom)message->data.l[0] == state->wm_delete_window) {
state->should_close = true;
input->changed = true;
} else if(message->message_type == state->xdnd_enter) {
x11_xdnd_enter(state, message);
} else if(message->message_type == state->xdnd_position) {
x11_xdnd_position(state, message);
} else if(message->message_type == state->xdnd_leave) {
state->xdnd_source = 0;
} else if(message->message_type == state->xdnd_drop) {
x11_xdnd_drop(state, message);
}
} break;
case SelectionNotify: {
x11_xdnd_selection(state, &event->xselection);
} break;
}
}
//- public api
internal b32 x11_window_open(PlatformWindow *window, WindowParams params)
{
X11State *state = &window->x11;
XInitThreads();
state->display = XOpenDisplay(NULL);
if(!state->display) {
Log("no x11 display");
return false;
}
Display *display = state->display;
state->screen = DefaultScreen(display);
state->root = RootWindow(display, state->screen);
state->dpi = x11_read_dpi(display);
state->scale = x11_scale_for_dpi(state->dpi);
state->width = params.width ? params.width : 960;
state->height = params.height ? params.height : 600;
state->is_layer = params.layer;
#define X11_ATOM(field, name) state->field = XInternAtom(display, name, False);
X11_ATOM(wm_protocols, "WM_PROTOCOLS")
X11_ATOM(wm_delete_window, "WM_DELETE_WINDOW")
X11_ATOM(net_wm_name, "_NET_WM_NAME")
X11_ATOM(net_wm_state, "_NET_WM_STATE")
X11_ATOM(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT")
X11_ATOM(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ")
X11_ATOM(net_wm_state_above, "_NET_WM_STATE_ABOVE")
X11_ATOM(net_wm_moveresize, "_NET_WM_MOVERESIZE")
X11_ATOM(net_wm_window_type, "_NET_WM_WINDOW_TYPE")
X11_ATOM(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY")
X11_ATOM(motif_wm_hints, "_MOTIF_WM_HINTS")
X11_ATOM(utf8_string, "UTF8_STRING")
X11_ATOM(net_wm_pid, "_NET_WM_PID")
X11_ATOM(xdnd_aware, "XdndAware")
X11_ATOM(xdnd_enter, "XdndEnter")
X11_ATOM(xdnd_position, "XdndPosition")
X11_ATOM(xdnd_status, "XdndStatus")
X11_ATOM(xdnd_leave, "XdndLeave")
X11_ATOM(xdnd_drop, "XdndDrop")
X11_ATOM(xdnd_finished, "XdndFinished")
X11_ATOM(xdnd_selection, "XdndSelection")
X11_ATOM(xdnd_type_list, "XdndTypeList")
X11_ATOM(xdnd_action_copy, "XdndActionCopy")
X11_ATOM(uri_list, "text/uri-list")
#undef X11_ATOM
u32 physical_width = state->width * state->scale;
u32 physical_height = state->height * state->scale;
int x = 0, y = 0;
if(state->is_layer) {
int screen_width = DisplayWidth(display, state->screen);
int screen_height = DisplayHeight(display, state->screen);
int margin = params.margin * (int)state->scale;
b32 left = (params.anchor & PLATFORM_ANCHOR_LEFT) != 0, right = (params.anchor & PLATFORM_ANCHOR_RIGHT) != 0;
b32 top = (params.anchor & PLATFORM_ANCHOR_TOP) != 0, bottom = (params.anchor & PLATFORM_ANCHOR_BOTTOM) != 0;
if(left && right) { x = margin; physical_width = (u32)(screen_width - 2 * margin); }
else if(left) { x = margin; }
else if(right) { x = screen_width - margin - (int)physical_width; }
else { x = (screen_width - (int)physical_width) / 2; }
if(top && bottom) { y = margin; physical_height = (u32)(screen_height - 2 * margin); }
else if(top) { y = margin; }
else if(bottom) { y = screen_height - margin - (int)physical_height; }
else { y = (screen_height - (int)physical_height) / 2; }
}
XSetWindowAttributes attributes = {};
attributes.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
EnterWindowMask | LeaveWindowMask | FocusChangeMask | StructureNotifyMask | ExposureMask |
PropertyChangeMask;
attributes.background_pixel = BlackPixel(display, state->screen);
state->window = XCreateWindow(display, state->root, x, y, physical_width, physical_height, 0, CopyFromParent, InputOutput,
CopyFromParent, CWEventMask | CWBackPixel, &attributes);
if(!state->window) {
Log("XCreateWindow failed");
return false;
}
//- identity: title, class (matches the .desktop file), pid
const char *title = params.title ? params.title : params.name;
XStoreName(display, state->window, title);
XChangeProperty(display, state->window, state->net_wm_name, state->utf8_string, 8, PropModeReplace, (const unsigned char *)title, (int)strlen(title));
XClassHint *class_hint = XAllocClassHint();
class_hint->res_name = (char *)params.name;
class_hint->res_class = (char *)params.name;
XSetClassHint(display, state->window, class_hint);
XFree(class_hint);
long pid = (long)getpid();
XChangeProperty(display, state->window, state->net_wm_pid, XA_CARDINAL, 32, PropModeReplace, (const unsigned char *)&pid, 1);
XSetWMProtocols(display, state->window, &state->wm_delete_window, 1);
if(params.min_width && params.min_height) {
XSizeHints *hints = XAllocSizeHints();
hints->flags = PMinSize | (state->is_layer ? PPosition : 0);
hints->min_width = (int)(params.min_width * state->scale);
hints->min_height = (int)(params.min_height * state->scale);
hints->x = x;
hints->y = y;
XSetWMNormalHints(display, state->window, hints);
XFree(hints);
}
if(state->is_layer) {
// NOTE: overlay: borderless (motif hints), utility type, always on top
long motif[5] = {2 /* MWM_HINTS_DECORATIONS */, 0, 0, 0, 0};
XChangeProperty(display, state->window, state->motif_wm_hints, state->motif_wm_hints, 32, PropModeReplace, (const unsigned char *)motif, 5);
XChangeProperty(display, state->window, state->net_wm_window_type, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&state->net_wm_window_type_utility, 1);
XChangeProperty(display, state->window, state->net_wm_state, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&state->net_wm_state_above, 1);
}
//- drag and drop
long xdnd_version = 5;
XChangeProperty(display, state->window, state->xdnd_aware, XA_ATOM, 32, PropModeReplace, (const unsigned char *)&xdnd_version, 1);
//- input method, for real text input (dead keys, compose)
XSetLocaleModifiers("");
state->im = XOpenIM(display, NULL, NULL, NULL);
if(state->im) {
state->ic = XCreateIC(state->im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, state->window,
XNFocusWindow, state->window, (char *)NULL);
}
// NOTE: no synthetic KeyRelease between autorepeated presses
Bool supported = False;
XkbSetDetectableAutoRepeat(display, True, &supported);
//- cursors, core font names (no libXcursor needed)
const unsigned int shapes[8] = {68 /* left_ptr */, 60 /* hand2 */, 52 /* fleur */, 108 /* sb_h_double_arrow */,
116 /* sb_v_double_arrow */, 34 /* crosshair */, 52 /* fleur */, 14 /* bottom_right_corner */};
for(u32 index = 0; index < 8; ++index) state->cursors[index] = XCreateFontCursor(display, shapes[index]);
state->cursor = PLATFORM_CURSOR_DEFAULT;
XDefineCursor(display, state->window, state->cursors[PLATFORM_CURSOR_DEFAULT]);
XMapWindow(display, state->window);
if(state->is_layer) XMoveWindow(display, state->window, x, y);
XFlush(display);
// NOTE: wait until mapped so the swapchain gets a real size
for(;;) {
XEvent event;
XNextEvent(display, &event);
x11_handle_event(state, &event);
if(event.type == MapNotify && event.xmap.window == state->window) break;
}
XWindowAttributes current;
XGetWindowAttributes(display, state->window, ¤t);
x11_update_size(state, (u32)current.width, (u32)current.height);
state->resized = false;
state->input.changed = true;
return true;
}
internal b32 x11_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask)
{
if(ready_mask) *ready_mask = 0;
X11State *state = &window->x11;
XFlush(state->display);
if(XPending(state->display) == 0) {
struct pollfd fds[8] = {{ConnectionNumber(state->display), POLLIN, 0}};
u32 which[8] = {};
u32 count = 1;
for(u32 index = 0; index < extra_count && count < ArrayCount(fds); ++index) {
if(extra_fds[index] >= 0) {
which[count] = index;
fds[count++] = {extra_fds[index], POLLIN, 0};
}
}
struct timespec timeout = {};
struct timespec *timeout_ptr = NULL;
if(timeout_ns >= 0) {
timeout.tv_sec = timeout_ns / 1000000000ll;
timeout.tv_nsec = timeout_ns % 1000000000ll;
timeout_ptr = &timeout;
}
int ready = ppoll(fds, count, timeout_ptr, NULL);
if(ready > 0 && ready_mask) {
for(u32 slot = 1; slot < count; ++slot) if(fds[slot].revents) *ready_mask |= 1u << which[slot];
}
}
while(XPending(state->display)) {
XEvent event;
XNextEvent(state->display, &event);
x11_handle_event(state, &event);
}
*input = state->input;
for(u32 index = 0; index < ArrayCount(state->input.mouse); ++index) state->input.mouse[index].half_transition_count = 0;
state->input.key_count = 0;
state->input.text_size = 0;
state->input.scroll_x = 0;
state->input.scroll_y = 0;
state->input.drop_count = 0;
state->input.changed = false;
return !state->should_close;
}
internal void x11_window_begin_move(PlatformWindow *window)
{
X11State *state = &window->x11;
if(state->is_layer) return;
Window root, child;
int root_x = 0, root_y = 0, x = 0, y = 0;
unsigned int mask = 0;
XQueryPointer(state->display, state->window, &root, &child, &root_x, &root_y, &x, &y, &mask);
XUngrabPointer(state->display, CurrentTime);
// NOTE: _NET_WM_MOVERESIZE_MOVE = 8, button 1, source indication 1 (normal application)
x11_send_wm_message(state, state->net_wm_moveresize, root_x, root_y, 8, Button1, 1);
XFlush(state->display);
x11_button_update(&state->input.mouse[0], false);
}
internal void x11_window_toggle_maximized(PlatformWindow *window)
{
X11State *state = &window->x11;
if(state->is_layer) return;
// NOTE: _NET_WM_STATE_TOGGLE = 2
x11_send_wm_message(state, state->net_wm_state, 2, (long)state->net_wm_state_maximized_vert, (long)state->net_wm_state_maximized_horz, 1, 0);
XFlush(state->display);
}
internal void x11_window_set_cursor(PlatformWindow *window, PlatformCursor cursor)
{
X11State *state = &window->x11;
if(state->cursor == (u32)cursor || (u32)cursor >= ArrayCount(state->cursors)) return;
state->cursor = (u32)cursor;
XDefineCursor(state->display, state->window, state->cursors[cursor]);
XFlush(state->display);
}
internal void x11_window_close(PlatformWindow *window)
{
X11State *state = &window->x11;
if(!state->display) return;
if(state->ic) XDestroyIC(state->ic);
if(state->im) XCloseIM(state->im);
for(u32 index = 0; index < ArrayCount(state->cursors); ++index) if(state->cursors[index]) XFreeCursor(state->display, state->cursors[index]);
if(state->window) XDestroyWindow(state->display, state->window);
XCloseDisplay(state->display);
state->display = NULL;
}
|