-
Notifications
You must be signed in to change notification settings - Fork 0
/
glow_x11.c
executable file
·465 lines (372 loc) · 14.3 KB
/
glow_x11.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
/* Copyright (C) 2017-2019 Alaskan Emily, Transnat Games
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "glow.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glx.h>
#else
#include <GL/gl.h>
#include <GL/glx.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GLOW_X_EVENT_MASK\
(StructureNotifyMask\
|KeyPressMask\
|KeyReleaseMask\
|ButtonPressMask\
|ButtonReleaseMask\
|PointerMotionMask\
|ExposureMask)
/* Gets a display, first using the DISPLAY environment variable, then using the
* X default (which may be different than the env variable depending on the DE),
* and finally we just try 0, 0.
*/
static Display *glow_get_display(){
char *const dpy_env = getenv("DISPLAY");
Display *dpy;
if((dpy = XOpenDisplay(dpy_env)))
return dpy;
if((dpy = XOpenDisplay(NULL)))
return dpy;
if((dpy = XOpenDisplay(":0.0")))
return dpy;
return NULL;
}
/******************************************************************************/
static const GLint glow_attribs[] = {
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_RED_SIZE, 6,
GLX_GREEN_SIZE, 6,
GLX_BLUE_SIZE, 6,
GLX_ALPHA_SIZE, 6,
GLX_DEPTH_SIZE, 8,
GLX_DOUBLEBUFFER, True,
None
};
/******************************************************************************/
struct Glow_Context {
unsigned char gl[2];
Display *dpy;
Window wnd;
GLXContext ctx;
};
/******************************************************************************/
struct Glow_Window{
Display *dpy;
Screen *scr;
int scr_id;
Window wnd;
Colormap cmap;
struct Glow_Context *ctx;
XVisualInfo *vis;
GLXFBConfig fbconfig;
unsigned w, h;
int mouse_x, mouse_y;
};
/******************************************************************************/
unsigned Glow_WindowStructSize(){
return sizeof(struct Glow_Window);
}
/******************************************************************************/
void Glow_ViewportSize(unsigned w, unsigned h,
unsigned *out_w, unsigned *out_h){
out_w[0] = w;
out_h[0] = h;
}
/******************************************************************************/
void *Glow_GetProcAddress(const char *name){
return (void*)glXGetProcAddress((GLubyte*)name);
}
/******************************************************************************/
void Glow_CreateWindow(struct Glow_Window *window,
unsigned w, unsigned h, const char *title, int flags){
window->w = w;
window->h = h;
window->mouse_x = 0;
window->mouse_y = 0;
window->dpy = glow_get_display();
window->ctx = NULL;
if(window->dpy == NULL){
fputs("Could not open an X11 display\n", stderr);
return;
}
window->scr = DefaultScreenOfDisplay(window->dpy);
window->scr_id = DefaultScreen(window->dpy);
/* Fiddle about determining the best fbconfig. */
{
int num = 0, i, best = -1, best_samples = -1;
GLXFBConfig *const config = glXChooseFBConfig(window->dpy,
window->scr_id, glow_attribs, &num);
if(config == NULL || num == 0){
fputs("Could not get glX framebuffer configuration\n", stderr);
XCloseDisplay(window->dpy);
window->dpy = NULL;
return;
}
for(i = 0; i < num; i++){
XVisualInfo *const info =
glXGetVisualFromFBConfig(window->dpy, config[i]);
if(info != NULL){
int sample_buffers, samples;
glXGetFBConfigAttrib(window->dpy, config[i],
GLX_SAMPLE_BUFFERS, &sample_buffers);
glXGetFBConfigAttrib(window->dpy, config[i],
GLX_SAMPLES, &samples);
if(best < 0 || (sample_buffers > 0 && samples > best_samples)){
best = i;
best_samples = samples;
}
XFree(info);
}
}
assert(best != -1);
assert(best < num);
window->fbconfig = config[best];
XFree(config);
}
/* Get a glX visual info for the fbconfig */
window->vis = glXGetVisualFromFBConfig(window->dpy, window->fbconfig);
if(window->vis == NULL){
fputs("Could not create a glX visual\n", stderr);
XCloseDisplay(window->dpy);
window->dpy = NULL;
return;
}
if(window->scr_id != window->vis->screen){
fputs("Screen does not match a given visual\n", stderr);
XCloseDisplay(window->dpy);
window->dpy = NULL;
return;
}
/* Open the window. */
{
XSetWindowAttributes winAttr;
Window root = RootWindow(window->dpy, window->scr_id);
winAttr.border_pixel = BlackPixel(window->dpy, window->scr_id);
winAttr.background_pixel = WhitePixel(window->dpy, window->scr_id);
winAttr.override_redirect = True;
winAttr.colormap = window->cmap =
XCreateColormap(window->dpy, root, window->vis->visual, AllocNone);
winAttr.event_mask = ExposureMask;
window->wnd = XCreateWindow(window->dpy, root, 0, 0, w, h, 0,
window->vis->depth, InputOutput, window->vis->visual,
CWBackPixel | CWColormap | CWBorderPixel | CWEventMask, &winAttr);
}
XStoreName(window->dpy, window->wnd, title);
XSelectInput(window->dpy, window->wnd, GLOW_X_EVENT_MASK);
XSync(window->dpy, False);
}
/******************************************************************************/
void Glow_DestroyWindow(struct Glow_Window *window){
XSync(window->dpy, False);
if(window->ctx)
glXDestroyContext(window->dpy, window->ctx->ctx);
XFreeColormap(window->dpy, window->cmap);
XDestroyWindow(window->dpy, window->wnd);
XSync(window->dpy, False);
XCloseDisplay(window->dpy);
}
/******************************************************************************/
void Glow_SetTitle(struct Glow_Window *window, const char *title){
XStoreName(window->dpy, window->wnd, title);
}
/******************************************************************************/
void Glow_ShowWindow(struct Glow_Window *window){
XClearWindow(window->dpy, window->wnd);
XMapRaised(window->dpy, window->wnd);
{
XEvent event;
do{
XNextEvent(window->dpy, &event);
} while(event.type != MapNotify);
}
}
/******************************************************************************/
void Glow_HideWindow(struct Glow_Window *window){
XUnmapWindow(window->dpy, window->wnd);
}
/******************************************************************************/
void Glow_GetWindowSize(const struct Glow_Window *window,
unsigned *out_w, unsigned *out_h){
out_w[0] = window->w;
out_h[0] = window->h;
}
/******************************************************************************/
void Glow_FlipScreen(struct Glow_Window *window){
Glow_MakeCurrent(window->ctx);
glFlush();
glXSwapBuffers(window->dpy, window->wnd);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
/******************************************************************************/
static unsigned glow_get_event(struct Glow_Window *window,
unsigned block, struct Glow_Event *out){
glow_get_event_start:
if(block || XPending(window->dpy) > 0){
XEvent event;
unsigned char press = 0u;
XNextEvent(window->dpy, &event);
switch(event.type){
case KeyPress:
press = 1u;
case KeyRelease:
{
KeySym sym;
XComposeStatus compose;
XLookupString(&event.xkey, out->value.key,
GLOW_MAX_KEY_NAME_SIZE, &sym, &compose);
#define GLOW_KEYSM(SYM, STR) case XK_ ## SYM: strcpy(out->value.key, ("" GLOW_ ## STR)); break;
switch(sym){
GLOW_KEYSM(Escape, ESCAPE);
GLOW_KEYSM(Up, UP_ARROW);
GLOW_KEYSM(Down, DOWN_ARROW);
GLOW_KEYSM(Left, LEFT_ARROW);
GLOW_KEYSM(Right, RIGHT_ARROW);
GLOW_KEYSM(Return, RETURN);
GLOW_KEYSM(Tab, TAB);
case XK_Control_L: /* FALLTHROUGH */
GLOW_KEYSM(Control_R, CONTROL);
case XK_Shift_R: /* FALLTHROUGH */
GLOW_KEYSM(Shift_L, SHIFT);
}
}
out->type = press ?
eGlowKeyboardPressed : eGlowKeyboardReleased;
return 1;
case ButtonPress:
press = 1u;
case ButtonRelease:
out->type = press ?
eGlowMousePressed : eGlowMouseReleased;
out->value.mouse.xy[0] = event.xbutton.x;
out->value.mouse.xy[1] = event.xbutton.y;
switch(event.xbutton.button){
case Button1: /* Left click */
out->value.mouse.button = eGlowLeft;
break;
case Button2: /* Middle click */
out->value.mouse.button = eGlowMiddle;
break;
case Button3: /* Right click */
out->value.mouse.button = eGlowRight;
break;
default: /*
fputs("Unexpected ", stdout);
fputs(press ? "Pressed " : "Released ", stdout);
printf("%i\n", event.xbutton.button); */
if(block) /* TCO doesn't work here for all compilers :( */
goto glow_get_event_start;
else
return 0;
}
return 1;
case MotionNotify:
out->type = eGlowMouseMoved;
out->value.mouse.xy[0] = event.xmotion.x;
out->value.mouse.xy[1] = event.xmotion.y;
return 1;
case UnmapNotify:
case DestroyNotify:
out->type = eGlowQuit;
return 1;
default:
/* printf("Unexpected event %i\n", event.type); */
if(block) /* TCO doesn't work here for all compilers :( */
goto glow_get_event_start;
}
}
return 0;
}
/******************************************************************************/
unsigned Glow_GetEvent(struct Glow_Window *window,
struct Glow_Event *out_event){
return glow_get_event(window, 0, out_event);
}
/******************************************************************************/
void Glow_WaitEvent(struct Glow_Window *window, struct Glow_Event *out_event){
glow_get_event(window, 1, out_event);
}
/******************************************************************************/
unsigned Glow_ContextStructSize(){
return sizeof(struct Glow_Context);
}
/******************************************************************************/
int Glow_CreateContext(struct Glow_Window *window,
struct Glow_Context *opt_share,
unsigned major, unsigned minor,
struct Glow_Context *out){
int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 0,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
None, None
};
context_attribs[1] = major;
context_attribs[3] = minor;
out->gl[0] = major;
out->gl[1] = minor;
out->dpy = window->dpy;
out->wnd = window->wnd;
typedef GLXContext (*glXCreateContextAttribsARB_t)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
const glXCreateContextAttribsARB_t glXCreateContextAttribsARB =
(glXCreateContextAttribsARB_t)glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
const GLXContext share_ctx = opt_share != NULL ? opt_share->ctx : 0;
if(glXCreateContextAttribsARB == NULL){
fputs("Could not use glXCreateContextAttribsARB, expect the wrong version of OpenGL\n", stderr);
if(major > 2)
return -1;
out->ctx = glXCreateNewContext(out->dpy, window->fbconfig, GLX_RGBA_TYPE, share_ctx, True);
}
else{
out->ctx = glXCreateContextAttribsARB(out->dpy, window->fbconfig, share_ctx, True, context_attribs);
}
if(window->ctx != NULL)
return -1;
XSync(window->dpy, False);
window->ctx = out;
Glow_MakeCurrent(window->ctx);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
/* glClearColor(0.75f, 0.333f, 0.0f, 1.0f); */
glFinish();
return 0;
}
/******************************************************************************/
struct Glow_Context *Glow_GetContext(
struct Glow_Window *window){
return window->ctx;
}
/******************************************************************************/
void Glow_CreateLegacyContext(struct Glow_Window *window,
struct Glow_Context *out){
Glow_CreateContext(window, NULL, 2, 1, out);
}
/******************************************************************************/
void Glow_MakeCurrent(struct Glow_Context *ctx){
glXMakeCurrent(ctx->dpy, ctx->wnd, ctx->ctx);
}
/******************************************************************************/
struct Glow_Window *Glow_CreateLegacyWindow(unsigned w, unsigned h,
const char *title){
/* Put the window and CTX in one location so that free() will get them both. */
struct Glow_Window *const window =
malloc(Glow_WindowStructSize() + Glow_ContextStructSize());
struct Glow_Context *const ctx = (struct Glow_Context *)(window + 1);
Glow_CreateWindow(window, w, h, title, 0);
Glow_CreateContext(window, NULL, 2, 1, ctx);
Glow_MakeCurrent(ctx);
return window;
}
/******************************************************************************/