2 * Copyright (C) 2010 LunarG Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 * Chia-I Wu <olv@lunarg.com>
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
33 _eglutNativeInitDisplay(void)
35 _eglut->native_dpy = XOpenDisplay(_eglut->display_name);
36 if (!_eglut->native_dpy)
37 _eglutFatal("failed to initialize native display");
39 _eglut->surface_type = EGL_WINDOW_BIT;
43 _eglutNativeFiniDisplay(void)
45 XCloseDisplay(_eglut->native_dpy);
49 _eglutNativeInitWindow(struct eglut_window *win, const char *title,
50 int x, int y, int w, int h)
52 XVisualInfo *visInfo, visTemplate;
55 XSetWindowAttributes attr;
59 if (!eglGetConfigAttrib(_eglut->dpy,
60 win->config, EGL_NATIVE_VISUAL_ID, &vid))
61 _eglutFatal("failed to get visual id");
63 /* The X window visual must match the EGL config */
64 visTemplate.visualid = vid;
65 visInfo = XGetVisualInfo(_eglut->native_dpy,
66 VisualIDMask, &visTemplate, &num_visuals);
68 _eglutFatal("failed to get an visual of id 0x%x", vid);
70 root = RootWindow(_eglut->native_dpy, DefaultScreen(_eglut->native_dpy));
72 /* window attributes */
73 attr.background_pixel = 0;
74 attr.border_pixel = 0;
75 attr.colormap = XCreateColormap(_eglut->native_dpy,
76 root, visInfo->visual, AllocNone);
77 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
78 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
80 xwin = XCreateWindow(_eglut->native_dpy, root, x, y, w, h,
81 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr);
83 _eglutFatal("failed to create a window");
87 /* set hints and properties */
94 sizehints.flags = USSize | USPosition;
95 XSetNormalHints(_eglut->native_dpy, xwin, &sizehints);
96 XSetStandardProperties(_eglut->native_dpy, xwin,
97 title, title, None, (char **) NULL, 0, &sizehints);
100 XMapWindow(_eglut->native_dpy, xwin);
102 win->native.u.window = xwin;
103 win->native.width = w;
104 win->native.height = h;
108 _eglutNativeFiniWindow(struct eglut_window *win)
110 XDestroyWindow(_eglut->native_dpy, win->native.u.window);
114 lookup_keysym(KeySym sym)
120 special = EGLUT_KEY_F1;
123 special = EGLUT_KEY_F2;
126 special = EGLUT_KEY_F3;
129 special = EGLUT_KEY_F4;
132 special = EGLUT_KEY_F5;
135 special = EGLUT_KEY_F6;
138 special = EGLUT_KEY_F7;
141 special = EGLUT_KEY_F8;
144 special = EGLUT_KEY_F9;
147 special = EGLUT_KEY_F10;
150 special = EGLUT_KEY_F11;
153 special = EGLUT_KEY_F12;
157 special = EGLUT_KEY_LEFT;
161 special = EGLUT_KEY_UP;
165 special = EGLUT_KEY_RIGHT;
169 special = EGLUT_KEY_DOWN;
180 next_event(struct eglut_window *win)
185 if (!XPending(_eglut->native_dpy)) {
186 /* there is an idle callback */
187 if (_eglut->idle_cb) {
192 /* the app requests re-display */
193 if (_eglut->redisplay)
197 /* block for next event */
198 XNextEvent(_eglut->native_dpy, &event);
200 switch (event.type) {
204 case ConfigureNotify:
205 win->native.width = event.xconfigure.width;
206 win->native.height = event.xconfigure.height;
208 win->reshape_cb(win->native.width, win->native.height);
216 r = XLookupString(&event.xkey,
217 buffer, sizeof(buffer), &sym, NULL);
218 if (r && win->keyboard_cb) {
219 win->keyboard_cb(buffer[0]);
221 else if (!r && win->special_cb) {
222 r = lookup_keysym(sym);
233 _eglut->redisplay = redraw;
237 _eglutNativeEventLoop(void)
240 struct eglut_window *win = _eglut->current;
244 if (_eglut->redisplay) {
245 _eglut->redisplay = 0;
249 eglSwapBuffers(_eglut->dpy, win->surface);