Debian Codegraph @

app/x11.c

raw
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build linux && !android
6 // +build linux,!android
7
8 #include "_cgo_export.h"
9 #include <EGL/egl.h>
10 #include <GLES2/gl2.h>
11 #include <X11/Xlib.h>
12 #include <X11/Xutil.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 static Atom wm_delete_window;
17
18 static Window
19 new_window(Display *x_dpy, EGLDisplay e_dpy, int w, int h, EGLContext *ctx, EGLSurface *surf) {
20 static const EGLint attribs[] = {
21 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
22 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
23 EGL_BLUE_SIZE, 8,
24 EGL_GREEN_SIZE, 8,
25 EGL_RED_SIZE, 8,
26 EGL_DEPTH_SIZE, 16,
27 EGL_CONFIG_CAVEAT, EGL_NONE,
28 EGL_NONE
29 };
30 EGLConfig config;
31 EGLint num_configs;
32 if (!eglChooseConfig(e_dpy, attribs, &config, 1, &num_configs)) {
33 fprintf(stderr, "eglChooseConfig failed\n");
34 exit(1);
35 }
36 EGLint vid;
37 if (!eglGetConfigAttrib(e_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
38 fprintf(stderr, "eglGetConfigAttrib failed\n");
39 exit(1);
40 }
41
42 XVisualInfo visTemplate;
43 visTemplate.visualid = vid;
44 int num_visuals;
45 XVisualInfo *visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
46 if (!visInfo) {
47 fprintf(stderr, "XGetVisualInfo failed\n");
48 exit(1);
49 }
50
51 Window root = RootWindow(x_dpy, DefaultScreen(x_dpy));
52 XSetWindowAttributes attr;
53
54 attr.colormap = XCreateColormap(x_dpy, root, visInfo->visual, AllocNone);
55 if (!attr.colormap) {
56 fprintf(stderr, "XCreateColormap failed\n");
57 exit(1);
58 }
59
60 attr.event_mask = StructureNotifyMask | ExposureMask |
61 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask;
62 Window win = XCreateWindow(
63 x_dpy, root, 0, 0, w, h, 0, visInfo->depth, InputOutput,
64 visInfo->visual, CWColormap | CWEventMask, &attr);
65 XFree(visInfo);
66
67 XSizeHints sizehints;
68 sizehints.width = w;
69 sizehints.height = h;
70 sizehints.flags = USSize;
71 XSetNormalHints(x_dpy, win, &sizehints);
72 XSetStandardProperties(x_dpy, win, "App", "App", None, (char **)NULL, 0, &sizehints);
73
74 static const EGLint ctx_attribs[] = {
75 EGL_CONTEXT_CLIENT_VERSION, 2,
76 EGL_NONE
77 };
78 *ctx = eglCreateContext(e_dpy, config, EGL_NO_CONTEXT, ctx_attribs);
79 if (!*ctx) {
80 fprintf(stderr, "eglCreateContext failed\n");
81 exit(1);
82 }
83 *surf = eglCreateWindowSurface(e_dpy, config, win, NULL);
84 if (!*surf) {
85 fprintf(stderr, "eglCreateWindowSurface failed\n");
86 exit(1);
87 }
88 return win;
89 }
90
91 Display *x_dpy;
92 EGLDisplay e_dpy;
93 EGLContext e_ctx;
94 EGLSurface e_surf;
95 Window win;
96
97 void
98 createWindow(void) {
99 x_dpy = XOpenDisplay(NULL);
100 if (!x_dpy) {
101 fprintf(stderr, "XOpenDisplay failed\n");
102 exit(1);
103 }
104 e_dpy = eglGetDisplay(x_dpy);
105 if (!e_dpy) {
106 fprintf(stderr, "eglGetDisplay failed\n");
107 exit(1);
108 }
109 EGLint e_major, e_minor;
110 if (!eglInitialize(e_dpy, &e_major, &e_minor)) {
111 fprintf(stderr, "eglInitialize failed\n");
112 exit(1);
113 }
114 eglBindAPI(EGL_OPENGL_ES_API);
115 win = new_window(x_dpy, e_dpy, 600, 800, &e_ctx, &e_surf);
116
117 wm_delete_window = XInternAtom(x_dpy, "WM_DELETE_WINDOW", True);
118 if (wm_delete_window != None) {
119 XSetWMProtocols(x_dpy, win, &wm_delete_window, 1);
120 }
121
122 XMapWindow(x_dpy, win);
123 if (!eglMakeCurrent(e_dpy, e_surf, e_surf, e_ctx)) {
124 fprintf(stderr, "eglMakeCurrent failed\n");
125 exit(1);
126 }
127
128 // Window size and DPI should be initialized before starting app.
129 XEvent ev;
130 while (1) {
131 if (XCheckMaskEvent(x_dpy, StructureNotifyMask, &ev) == False) {
132 continue;
133 }
134 if (ev.type == ConfigureNotify) {
135 onResize(ev.xconfigure.width, ev.xconfigure.height);
136 break;
137 }
138 }
139 }
140
141 void
142 processEvents(void) {
143 while (XPending(x_dpy)) {
144 XEvent ev;
145 XNextEvent(x_dpy, &ev);
146 switch (ev.type) {
147 case ButtonPress:
148 onTouchBegin((float)ev.xbutton.x, (float)ev.xbutton.y);
149 break;
150 case ButtonRelease:
151 onTouchEnd((float)ev.xbutton.x, (float)ev.xbutton.y);
152 break;
153 case MotionNotify:
154 onTouchMove((float)ev.xmotion.x, (float)ev.xmotion.y);
155 break;
156 case ConfigureNotify:
157 onResize(ev.xconfigure.width, ev.xconfigure.height);
158 break;
159 case ClientMessage:
160 if (wm_delete_window != None && (Atom)ev.xclient.data.l[0] == wm_delete_window) {
161 onStop();
162 return;
163 }
164 break;
165 }
166 }
167 }
168
169 void
170 swapBuffers(void) {
171 if (eglSwapBuffers(e_dpy, e_surf) == EGL_FALSE) {
172 fprintf(stderr, "eglSwapBuffer failed\n");
173 exit(1);
174 }
175 }