-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx11.c
122 lines (87 loc) · 3.52 KB
/
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
#include <err.h>
#include <stdlib.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <GL/glew.h>
#include <GL/glxew.h>
#include <GL/glx.h>
#include "x11.h"
Display* X11_display = 0;
Window X11_window;
XVisualInfo* X11_visual;
XIM X11_xim;
XIC X11_xic;
GLXContext X11_glx_context;
unsigned int X11_window_width = 800;
unsigned int X11_window_height = 600;
Atom xa_utf8_string;
Atom xa_clipboard;
Atom xa_targets;
static Bool x11_WaitForMapNotify(Display* X11_display, XEvent* event,
char* arg) {
return (event->type == MapNotify) && (event->xmap.window == (Window) arg);
}
void X11_Setup(void) {
int attributes[] = { GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8, GLX_DOUBLEBUFFER, None };
XWMHints* wmhints;
Colormap color_map;
XSetWindowAttributes attr;
XEvent event;
char* p;
XInitThreads();
const char* display_name = getenv("DISPLAY");
if (!display_name)
err(EXIT_FAILURE, "DISPLAY environment variable not set");
X11_display = XOpenDisplay(display_name);
if (!X11_display)
errx(EXIT_FAILURE, "Failed to open X11_display %s", display_name);
XSynchronize(X11_display, True);
if (!glXQueryExtension(X11_display, 0, 0))
errx(EXIT_FAILURE, "No GLX extension present");
if (!(X11_visual = glXChooseVisual(X11_display, DefaultScreen(X11_display),
attributes)))
errx(EXIT_FAILURE, "glXChooseVisual failed");
if (!(X11_glx_context =
glXCreateContext(X11_display, X11_visual, 0, GL_TRUE)))
errx(EXIT_FAILURE, "Failed creating OpenGL context");
color_map =
XCreateColormap(X11_display, RootWindow(X11_display, X11_visual->screen),
X11_visual->visual, AllocNone);
attr.backing_store = NotUseful;
attr.border_pixel = 0;
attr.colormap = color_map;
attr.event_mask =
ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
KeyPressMask | KeyReleaseMask | FocusChangeMask | EnterWindowMask |
LeaveWindowMask | StructureNotifyMask;
X11_window = XCreateWindow(
X11_display, RootWindow(X11_display, X11_visual->screen), 0, 0,
X11_window_width, X11_window_height, 0, X11_visual->depth, InputOutput,
X11_visual->visual,
CWBackingStore | CWBorderPixel | CWColormap | CWEventMask, &attr);
if (!(wmhints = XAllocWMHints())) errx(EXIT_FAILURE, "XAllocWMHints failed");
wmhints->input = True;
wmhints->flags = InputHint;
XSetWMHints(X11_display, X11_window, wmhints);
XFree(wmhints);
XMapRaised(X11_display, X11_window);
if ((p = XSetLocaleModifiers("")) && *p)
X11_xim = XOpenIM(X11_display, 0, 0, 0);
if (!X11_xim && (p = XSetLocaleModifiers("@im=none")) && *p)
X11_xim = XOpenIM(X11_display, 0, 0, 0);
if (!X11_xim) errx(EXIT_FAILURE, "Failed to open X Input Method");
X11_xic =
XCreateIC(X11_xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
XNClientWindow, X11_window, XNFocusWindow, X11_window, NULL);
if (!X11_xic) errx(EXIT_FAILURE, "Failed to create X Input Context");
XIfEvent(X11_display, &event, x11_WaitForMapNotify, (char*)X11_window);
if (!glXMakeCurrent(X11_display, X11_window, X11_glx_context))
errx(EXIT_FAILURE, "glXMakeCurrent returned false");
xa_utf8_string = XInternAtom(X11_display, "UTF8_STRING", False);
xa_clipboard = XInternAtom(X11_display, "CLIPBOARD", False);
xa_targets = XInternAtom(X11_display, "TARGETS", False);
XSynchronize(X11_display, False);
}