| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "_cgo_export.h" |
| 8 |
#include <EGL/egl.h> |
| 9 |
#include <X11/Xlib.h> |
| 10 |
#include <X11/Xutil.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
Atom net_wm_name; |
| 16 |
Atom utf8_string; |
| 17 |
Atom wm_delete_window; |
| 18 |
Atom wm_protocols; |
| 19 |
Atom wm_take_focus; |
| 20 |
|
| 21 |
EGLConfig e_config; |
| 22 |
EGLContext e_ctx; |
| 23 |
EGLDisplay e_dpy; |
| 24 |
Colormap x_colormap; |
| 25 |
Display *x_dpy; |
| 26 |
XVisualInfo *x_visual_info; |
| 27 |
Window x_root; |
| 28 |
|
| 29 |
|
| 30 |
char * |
| 31 |
eglGetErrorStr() { |
| 32 |
switch (eglGetError()) { |
| 33 |
case EGL_SUCCESS: |
| 34 |
return "EGL_SUCCESS"; |
| 35 |
case EGL_NOT_INITIALIZED: |
| 36 |
return "EGL_NOT_INITIALIZED"; |
| 37 |
case EGL_BAD_ACCESS: |
| 38 |
return "EGL_BAD_ACCESS"; |
| 39 |
case EGL_BAD_ALLOC: |
| 40 |
return "EGL_BAD_ALLOC"; |
| 41 |
case EGL_BAD_ATTRIBUTE: |
| 42 |
return "EGL_BAD_ATTRIBUTE"; |
| 43 |
case EGL_BAD_CONFIG: |
| 44 |
return "EGL_BAD_CONFIG"; |
| 45 |
case EGL_BAD_CONTEXT: |
| 46 |
return "EGL_BAD_CONTEXT"; |
| 47 |
case EGL_BAD_CURRENT_SURFACE: |
| 48 |
return "EGL_BAD_CURRENT_SURFACE"; |
| 49 |
case EGL_BAD_DISPLAY: |
| 50 |
return "EGL_BAD_DISPLAY"; |
| 51 |
case EGL_BAD_MATCH: |
| 52 |
return "EGL_BAD_MATCH"; |
| 53 |
case EGL_BAD_NATIVE_PIXMAP: |
| 54 |
return "EGL_BAD_NATIVE_PIXMAP"; |
| 55 |
case EGL_BAD_NATIVE_WINDOW: |
| 56 |
return "EGL_BAD_NATIVE_WINDOW"; |
| 57 |
case EGL_BAD_PARAMETER: |
| 58 |
return "EGL_BAD_PARAMETER"; |
| 59 |
case EGL_BAD_SURFACE: |
| 60 |
return "EGL_BAD_SURFACE"; |
| 61 |
case EGL_CONTEXT_LOST: |
| 62 |
return "EGL_CONTEXT_LOST"; |
| 63 |
} |
| 64 |
return "unknown EGL error"; |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
startDriver() { |
| 69 |
x_dpy = XOpenDisplay(NULL); |
| 70 |
if (!x_dpy) { |
| 71 |
fprintf(stderr, "XOpenDisplay failed\n"); |
| 72 |
exit(1); |
| 73 |
} |
| 74 |
e_dpy = eglGetDisplay(x_dpy); |
| 75 |
if (!e_dpy) { |
| 76 |
fprintf(stderr, "eglGetDisplay failed: %s\n", eglGetErrorStr()); |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
EGLint e_major, e_minor; |
| 80 |
if (!eglInitialize(e_dpy, &e_major, &e_minor)) { |
| 81 |
fprintf(stderr, "eglInitialize failed: %s\n", eglGetErrorStr()); |
| 82 |
exit(1); |
| 83 |
} |
| 84 |
if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 85 |
fprintf(stderr, "eglBindAPI failed: %s\n", eglGetErrorStr()); |
| 86 |
exit(1); |
| 87 |
} |
| 88 |
|
| 89 |
static const EGLint attribs[] = { |
| 90 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 91 |
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 92 |
EGL_BLUE_SIZE, 8, |
| 93 |
EGL_GREEN_SIZE, 8, |
| 94 |
EGL_RED_SIZE, 8, |
| 95 |
EGL_DEPTH_SIZE, 16, |
| 96 |
EGL_CONFIG_CAVEAT, EGL_NONE, |
| 97 |
EGL_NONE |
| 98 |
}; |
| 99 |
EGLint num_configs; |
| 100 |
if (!eglChooseConfig(e_dpy, attribs, &e_config, 1, &num_configs)) { |
| 101 |
fprintf(stderr, "eglChooseConfig failed: %s\n", eglGetErrorStr()); |
| 102 |
exit(1); |
| 103 |
} |
| 104 |
EGLint vid; |
| 105 |
if (!eglGetConfigAttrib(e_dpy, e_config, EGL_NATIVE_VISUAL_ID, &vid)) { |
| 106 |
fprintf(stderr, "eglGetConfigAttrib failed: %s\n", eglGetErrorStr()); |
| 107 |
exit(1); |
| 108 |
} |
| 109 |
|
| 110 |
XVisualInfo visTemplate; |
| 111 |
visTemplate.visualid = vid; |
| 112 |
int num_visuals; |
| 113 |
x_visual_info = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); |
| 114 |
if (!x_visual_info) { |
| 115 |
fprintf(stderr, "XGetVisualInfo failed\n"); |
| 116 |
exit(1); |
| 117 |
} |
| 118 |
|
| 119 |
x_root = RootWindow(x_dpy, DefaultScreen(x_dpy)); |
| 120 |
x_colormap = XCreateColormap(x_dpy, x_root, x_visual_info->visual, AllocNone); |
| 121 |
if (!x_colormap) { |
| 122 |
fprintf(stderr, "XCreateColormap failed\n"); |
| 123 |
exit(1); |
| 124 |
} |
| 125 |
|
| 126 |
static const EGLint ctx_attribs[] = { |
| 127 |
EGL_CONTEXT_CLIENT_VERSION, 3, |
| 128 |
EGL_NONE |
| 129 |
}; |
| 130 |
e_ctx = eglCreateContext(e_dpy, e_config, EGL_NO_CONTEXT, ctx_attribs); |
| 131 |
if (!e_ctx) { |
| 132 |
fprintf(stderr, "eglCreateContext failed: %s\n", eglGetErrorStr()); |
| 133 |
exit(1); |
| 134 |
} |
| 135 |
|
| 136 |
net_wm_name = XInternAtom(x_dpy, "_NET_WM_NAME", False); |
| 137 |
utf8_string = XInternAtom(x_dpy, "UTF8_STRING", False); |
| 138 |
wm_delete_window = XInternAtom(x_dpy, "WM_DELETE_WINDOW", False); |
| 139 |
wm_protocols = XInternAtom(x_dpy, "WM_PROTOCOLS", False); |
| 140 |
wm_take_focus = XInternAtom(x_dpy, "WM_TAKE_FOCUS", False); |
| 141 |
|
| 142 |
const int key_lo = 8; |
| 143 |
const int key_hi = 255; |
| 144 |
int keysyms_per_keycode; |
| 145 |
KeySym *keysyms = XGetKeyboardMapping(x_dpy, key_lo, key_hi-key_lo+1, &keysyms_per_keycode); |
| 146 |
if (keysyms_per_keycode < 2) { |
| 147 |
fprintf(stderr, "XGetKeyboardMapping returned too few keysyms per keycode: %d\n", keysyms_per_keycode); |
| 148 |
exit(1); |
| 149 |
} |
| 150 |
int k; |
| 151 |
for (k = key_lo; k <= key_hi; k++) { |
| 152 |
onKeysym(k, |
| 153 |
keysyms[(k-key_lo)*keysyms_per_keycode + 0], |
| 154 |
keysyms[(k-key_lo)*keysyms_per_keycode + 1]); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
processEvents() { |
| 161 |
while (XPending(x_dpy)) { |
| 162 |
XEvent ev; |
| 163 |
XNextEvent(x_dpy, &ev); |
| 164 |
switch (ev.type) { |
| 165 |
case KeyPress: |
| 166 |
case KeyRelease: |
| 167 |
onKey(ev.xkey.window, ev.xkey.state, ev.xkey.keycode, ev.type == KeyPress ? 1 : 2); |
| 168 |
break; |
| 169 |
case ButtonPress: |
| 170 |
case ButtonRelease: |
| 171 |
onMouse(ev.xbutton.window, ev.xbutton.x, ev.xbutton.y, ev.xbutton.state, ev.xbutton.button, |
| 172 |
ev.type == ButtonPress ? 1 : 2); |
| 173 |
break; |
| 174 |
case MotionNotify: |
| 175 |
onMouse(ev.xmotion.window, ev.xmotion.x, ev.xmotion.y, ev.xmotion.state, 0, 0); |
| 176 |
break; |
| 177 |
case FocusIn: |
| 178 |
case FocusOut: |
| 179 |
onFocus(ev.xmotion.window, ev.type == FocusIn); |
| 180 |
break; |
| 181 |
case Expose: |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (ev.xexpose.count == 0) { |
| 188 |
onExpose(ev.xexpose.window); |
| 189 |
} |
| 190 |
break; |
| 191 |
case ConfigureNotify: |
| 192 |
onConfigure(ev.xconfigure.window, ev.xconfigure.x, ev.xconfigure.y, |
| 193 |
ev.xconfigure.width, ev.xconfigure.height, |
| 194 |
DisplayWidth(x_dpy, DefaultScreen(x_dpy)), |
| 195 |
DisplayWidthMM(x_dpy, DefaultScreen(x_dpy))); |
| 196 |
break; |
| 197 |
case ClientMessage: |
| 198 |
if ((ev.xclient.message_type != wm_protocols) || (ev.xclient.format != 32)) { |
| 199 |
break; |
| 200 |
} |
| 201 |
Atom a = ev.xclient.data.l[0]; |
| 202 |
if (a == wm_delete_window) { |
| 203 |
onDeleteWindow(ev.xclient.window); |
| 204 |
} else if (a == wm_take_focus) { |
| 205 |
XSetInputFocus(x_dpy, ev.xclient.window, RevertToParent, ev.xclient.data.l[1]); |
| 206 |
} |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
makeCurrent(uintptr_t surface) { |
| 214 |
EGLSurface surf = (EGLSurface)(surface); |
| 215 |
if (!eglMakeCurrent(e_dpy, surf, surf, e_ctx)) { |
| 216 |
fprintf(stderr, "eglMakeCurrent failed: %s\n", eglGetErrorStr()); |
| 217 |
exit(1); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void |
| 222 |
swapBuffers(uintptr_t surface) { |
| 223 |
EGLSurface surf = (EGLSurface)(surface); |
| 224 |
if (!eglSwapBuffers(e_dpy, surf)) { |
| 225 |
fprintf(stderr, "eglSwapBuffers failed: %s\n", eglGetErrorStr()); |
| 226 |
exit(1); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
doCloseWindow(uintptr_t id) { |
| 232 |
Window win = (Window)(id); |
| 233 |
XDestroyWindow(x_dpy, win); |
| 234 |
} |
| 235 |
|
| 236 |
uintptr_t |
| 237 |
doNewWindow(int width, int height, char* title, int title_len) { |
| 238 |
XSetWindowAttributes attr; |
| 239 |
attr.colormap = x_colormap; |
| 240 |
attr.event_mask = |
| 241 |
KeyPressMask | |
| 242 |
KeyReleaseMask | |
| 243 |
ButtonPressMask | |
| 244 |
ButtonReleaseMask | |
| 245 |
PointerMotionMask | |
| 246 |
ExposureMask | |
| 247 |
StructureNotifyMask | |
| 248 |
FocusChangeMask; |
| 249 |
|
| 250 |
Window win = XCreateWindow( |
| 251 |
x_dpy, x_root, 0, 0, width, height, 0, x_visual_info->depth, InputOutput, |
| 252 |
x_visual_info->visual, CWColormap | CWEventMask, &attr); |
| 253 |
|
| 254 |
XSizeHints sizehints; |
| 255 |
sizehints.width = width; |
| 256 |
sizehints.height = height; |
| 257 |
sizehints.flags = USSize; |
| 258 |
XSetNormalHints(x_dpy, win, &sizehints); |
| 259 |
|
| 260 |
Atom atoms[2]; |
| 261 |
atoms[0] = wm_delete_window; |
| 262 |
atoms[1] = wm_take_focus; |
| 263 |
XSetWMProtocols(x_dpy, win, atoms, 2); |
| 264 |
|
| 265 |
XSetStandardProperties(x_dpy, win, "", "App", None, (char **)NULL, 0, &sizehints); |
| 266 |
XChangeProperty(x_dpy, win, net_wm_name, utf8_string, 8, PropModeReplace, title, title_len); |
| 267 |
|
| 268 |
return win; |
| 269 |
} |
| 270 |
|
| 271 |
uintptr_t |
| 272 |
doShowWindow(uintptr_t id) { |
| 273 |
Window win = (Window)(id); |
| 274 |
XMapWindow(x_dpy, win); |
| 275 |
EGLSurface surf = eglCreateWindowSurface(e_dpy, e_config, win, NULL); |
| 276 |
if (!surf) { |
| 277 |
fprintf(stderr, "eglCreateWindowSurface failed: %s\n", eglGetErrorStr()); |
| 278 |
exit(1); |
| 279 |
} |
| 280 |
return (uintptr_t)(surf); |
| 281 |
} |
| 282 |
|
| 283 |
uintptr_t |
| 284 |
surfaceCreate() { |
| 285 |
static const EGLint ctx_attribs[] = { |
| 286 |
EGL_CONTEXT_CLIENT_VERSION, 3, |
| 287 |
EGL_NONE |
| 288 |
}; |
| 289 |
EGLContext ctx = eglCreateContext(e_dpy, e_config, EGL_NO_CONTEXT, ctx_attribs); |
| 290 |
if (!ctx) { |
| 291 |
fprintf(stderr, "surface eglCreateContext failed: %s\n", eglGetErrorStr()); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
static const EGLint cfg_attribs[] = { |
| 296 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 297 |
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 298 |
EGL_BLUE_SIZE, 8, |
| 299 |
EGL_GREEN_SIZE, 8, |
| 300 |
EGL_RED_SIZE, 8, |
| 301 |
EGL_DEPTH_SIZE, 16, |
| 302 |
EGL_CONFIG_CAVEAT, EGL_NONE, |
| 303 |
EGL_NONE |
| 304 |
}; |
| 305 |
EGLConfig cfg; |
| 306 |
EGLint num_configs; |
| 307 |
if (!eglChooseConfig(e_dpy, cfg_attribs, &cfg, 1, &num_configs)) { |
| 308 |
fprintf(stderr, "gldriver: surface eglChooseConfig failed: %s\n", eglGetErrorStr()); |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
static const EGLint attribs[] = { |
| 314 |
EGL_WIDTH, 4096, |
| 315 |
EGL_HEIGHT, 3072, |
| 316 |
EGL_NONE |
| 317 |
}; |
| 318 |
EGLSurface surface = eglCreatePbufferSurface(e_dpy, cfg, attribs); |
| 319 |
if (!surface) { |
| 320 |
fprintf(stderr, "gldriver: surface eglCreatePbufferSurface failed: %s\n", eglGetErrorStr()); |
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
if (!eglMakeCurrent(e_dpy, surface, surface, ctx)) { |
| 325 |
fprintf(stderr, "gldriver: surface eglMakeCurrent failed: %s\n", eglGetErrorStr()); |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
return (uintptr_t)surface; |
| 330 |
} |