2 * freeglut_window_x11.c
4 * Window management methods for X11
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9 * Creation date: Thur Feb 2 2012
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #define FREEGLUT_BUILDING_LIB
30 #include <GL/freeglut.h>
31 #include <limits.h> /* LONG_MAX */
32 #include <unistd.h> /* usleep, gethostname, getpid */
33 #include <sys/types.h> /* pid_t */
34 #include "../fg_internal.h"
36 #ifdef EGL_VERSION_1_0
37 #include "egl/fg_window_egl.h"
38 #define fghCreateNewContext fghCreateNewContextEGL
40 #include "x11/fg_window_x11_glx.h"
43 static int fghResizeFullscrToggle(void)
45 XWindowAttributes attributes;
46 SFG_Window *win = fgStructure.CurrentWindow;
48 if(glutGet(GLUT_FULL_SCREEN)) {
49 /* restore original window size */
50 fgStructure.CurrentWindow->State.WorkMask = GLUT_SIZE_WORK;
51 fgStructure.CurrentWindow->State.DesiredWidth = win->State.pWState.OldWidth;
52 fgStructure.CurrentWindow->State.DesiredHeight = win->State.pWState.OldHeight;
55 fgStructure.CurrentWindow->State.pWState.OldWidth = win->State.Width;
56 fgStructure.CurrentWindow->State.pWState.OldHeight = win->State.Height;
58 /* resize the window to cover the entire screen */
59 XGetWindowAttributes(fgDisplay.pDisplay.Display,
60 fgStructure.CurrentWindow->Window.Handle,
64 * The "x" and "y" members of "attributes" are the window's coordinates
65 * relative to its parent, i.e. to the decoration window.
67 XMoveResizeWindow(fgDisplay.pDisplay.Display,
68 fgStructure.CurrentWindow->Window.Handle,
71 fgDisplay.ScreenWidth,
72 fgDisplay.ScreenHeight);
77 #define _NET_WM_STATE_TOGGLE 2
78 static int fghEwmhFullscrToggle(void)
81 long evmask = SubstructureRedirectMask | SubstructureNotifyMask;
83 if(!fgDisplay.pDisplay.State || !fgDisplay.pDisplay.StateFullScreen) {
87 xev.type = ClientMessage;
88 xev.xclient.window = fgStructure.CurrentWindow->Window.Handle;
89 xev.xclient.message_type = fgDisplay.pDisplay.State;
90 xev.xclient.format = 32;
91 xev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;
92 xev.xclient.data.l[1] = fgDisplay.pDisplay.StateFullScreen;
93 xev.xclient.data.l[2] = 0; /* no second property to toggle */
94 xev.xclient.data.l[3] = 1; /* source indication: application */
95 xev.xclient.data.l[4] = 0; /* unused */
97 if(!XSendEvent(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow, 0, evmask, &xev)) {
103 static int fghToggleFullscreen(void)
105 /* first try the EWMH (_NET_WM_STATE) method ... */
106 if(fghEwmhFullscrToggle() != -1) {
110 /* fall back to resizing the window */
111 if(fghResizeFullscrToggle() != -1) {
117 static Bool fghWindowIsVisible( Display *display, XEvent *event, XPointer arg)
119 Window window = (Window)arg;
120 return (event->type == MapNotify) && (event->xmap.window == window);
124 * Opens a window. Requires a SFG_Window object created and attached
125 * to the freeglut structure. OpenGL context is created here.
127 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
128 GLboolean positionUse, int x, int y,
129 GLboolean sizeUse, int w, int h,
130 GLboolean gameMode, GLboolean isSubWindow )
132 XVisualInfo * visualInfo = NULL;
133 XSetWindowAttributes winAttr;
134 XTextProperty textProperty;
135 XSizeHints sizeHints;
137 XEvent eventReturnBuffer; /* return buffer required for a call */
139 unsigned int current_DisplayMode = fgState.DisplayMode ;
140 XEvent fakeEvent = {0};
142 /* Save the display mode if we are creating a menu window */
143 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
144 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
146 #ifdef EGL_VERSION_1_0
147 #define WINDOW_CONFIG window->Window.pContext.egl.Config
149 #define WINDOW_CONFIG window->Window.pContext.FBConfig
151 fghChooseConfig(&WINDOW_CONFIG);
153 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
154 fgState.DisplayMode = current_DisplayMode ;
156 if( ! WINDOW_CONFIG )
159 * The "fghChooseConfig" returned a null meaning that the visual
160 * context is not available.
161 * Try a couple of variations to see if they will work.
163 #ifndef EGL_VERSION_1_0
164 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
166 fgState.DisplayMode |= GLUT_DOUBLE ;
167 fghChooseConfig(&WINDOW_CONFIG);
168 fgState.DisplayMode &= ~GLUT_DOUBLE;
172 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
174 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
175 fghChooseConfig(&WINDOW_CONFIG);
176 fgState.DisplayMode |= GLUT_MULTISAMPLE;
180 FREEGLUT_INTERNAL_ERROR_EXIT( WINDOW_CONFIG != NULL,
181 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
183 /* Get the X visual. */
184 #ifdef EGL_VERSION_1_0
186 XVisualInfo visualTemplate;
188 if (!eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display, window->Window.pContext.egl.Config, EGL_NATIVE_VISUAL_ID, &vid))
189 fgError("eglGetConfigAttrib(EGL_NATIVE_VISUAL_ID) failed");
190 visualTemplate.visualid = vid;
191 visualInfo = XGetVisualInfo(fgDisplay.pDisplay.Display, VisualIDMask, &visualTemplate, &num_visuals);
193 visualInfo = glXGetVisualFromFBConfig( fgDisplay.pDisplay.Display,
194 window->Window.pContext.FBConfig );
197 FREEGLUT_INTERNAL_ERROR_EXIT( visualInfo != NULL,
198 "visualInfo could not be retrieved from FBConfig", "fgOpenWindow" );
201 * XXX HINT: the masks should be updated when adding/removing callbacks.
202 * XXX This might speed up message processing. Is that true?
204 * XXX A: Not appreciably, but it WILL make it easier to debug.
205 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
206 * XXX turns off events that it doesn't need and is a whole lot
207 * XXX more pleasant to trace. (Think mouse-motion! Tons of
208 * XXX ``bonus'' GUI events stream in.)
211 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
212 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
213 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
214 PointerMotionMask | ButtonMotionMask;
215 winAttr.background_pixmap = None;
216 winAttr.background_pixel = 0;
217 winAttr.border_pixel = 0;
219 winAttr.colormap = XCreateColormap(
220 fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow,
221 visualInfo->visual, AllocNone
224 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
226 if( window->IsMenu || ( gameMode == GL_TRUE ) )
228 winAttr.override_redirect = True;
229 mask |= CWOverrideRedirect;
233 x = y = -1; /* default window position */
235 w = h = 300; /* default window size */
237 window->Window.Handle = XCreateWindow(
238 fgDisplay.pDisplay.Display,
239 window->Parent == NULL ? fgDisplay.pDisplay.RootWindow :
240 window->Parent->Window.Handle,
242 visualInfo->depth, InputOutput,
243 visualInfo->visual, mask,
247 /* Fake configure event to force viewport setup
248 * even with no window manager.
250 fakeEvent.xconfigure.type = ConfigureNotify;
251 fakeEvent.xconfigure.display = fgDisplay.pDisplay.Display;
252 fakeEvent.xconfigure.window = window->Window.Handle;
253 fakeEvent.xconfigure.x = x;
254 fakeEvent.xconfigure.y = y;
255 fakeEvent.xconfigure.width = w;
256 fakeEvent.xconfigure.height = h;
257 XPutBackEvent(fgDisplay.pDisplay.Display, &fakeEvent);
260 * The GLX context creation, possibly trying the direct context rendering
261 * or else use the current context if the user has so specified
267 * If there isn't already an OpenGL rendering context for menu
270 if( !fgStructure.MenuContext )
272 fgStructure.MenuContext =
273 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
274 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
277 /* window->Window.Context = fgStructure.MenuContext->MContext; */
278 window->Window.Context = fghCreateNewContext( window );
280 else if( fgState.UseCurrentContext )
283 #ifdef EGL_VERSION_1_0
284 window->Window.Context = eglGetCurrentContext( );
286 window->Window.Context = glXGetCurrentContext( );
289 if( ! window->Window.Context )
290 window->Window.Context = fghCreateNewContext( window );
293 window->Window.Context = fghCreateNewContext( window );
295 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ ) && !defined(EGL_VERSION_1_0)
296 if( !glXIsDirect( fgDisplay.pDisplay.Display, window->Window.Context ) )
298 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
299 fgError( "Unable to force direct context rendering for window '%s'",
306 sizeHints.flags |= USPosition;
308 sizeHints.flags |= USSize;
311 * Fill in the size hints values now (the x, y, width and height
312 * settings are obsolete, are there any more WMs that support them?)
313 * Unless the X servers actually stop supporting these, we should
314 * continue to fill them in. It is *not* our place to tell the user
315 * that they should replace a window manager that they like, and which
316 * works, just because *we* think that it's not "modern" enough.
321 sizeHints.height = h;
323 wmHints.flags = StateHint;
324 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
325 /* Prepare the window and iconified window names... */
326 XStringListToTextProperty( (char **) &title, 1, &textProperty );
329 fgDisplay.pDisplay.Display,
330 window->Window.Handle,
339 XFree( textProperty.value );
341 XSetWMProtocols( fgDisplay.pDisplay.Display, window->Window.Handle,
342 &fgDisplay.pDisplay.DeleteWindow, 1 );
344 if (fgDisplay.pDisplay.NetWMSupported
345 && fgDisplay.pDisplay.NetWMPid != None
346 && fgDisplay.pDisplay.ClientMachine != None)
348 char hostname[HOST_NAME_MAX];
349 pid_t pid = getpid();
351 if (pid > 0 && gethostname(hostname, sizeof(hostname)) > -1)
353 hostname[sizeof(hostname) - 1] = '\0';
356 fgDisplay.pDisplay.Display,
357 window->Window.Handle,
358 fgDisplay.pDisplay.NetWMPid,
362 (unsigned char *) &pid,
367 fgDisplay.pDisplay.Display,
368 window->Window.Handle,
369 fgDisplay.pDisplay.ClientMachine,
373 (unsigned char *) hostname,
379 #ifdef EGL_VERSION_1_0
380 fghPlatformOpenWindowEGL(window);
382 glXMakeContextCurrent(
383 fgDisplay.pDisplay.Display,
384 window->Window.Handle,
385 window->Window.Handle,
386 window->Window.Context
390 /* register extension events _before_ window is mapped */
391 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
392 fgRegisterDevices( fgDisplay.pDisplay.Display, &(window->Window.Handle) );
395 if (!window->IsMenu) /* Don't show window after creation if its a menu */
397 XMapWindow( fgDisplay.pDisplay.Display, window->Window.Handle );
398 window->State.Visible = GL_TRUE;
403 /* wait till window visible */
404 if( !isSubWindow && !window->IsMenu)
405 XPeekIfEvent( fgDisplay.pDisplay.Display, &eventReturnBuffer, &fghWindowIsVisible, (XPointer)(window->Window.Handle) );
411 * Request a window resize
413 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
415 XResizeWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
417 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
422 * Closes a window, destroying the frame and OpenGL context
424 void fgPlatformCloseWindow( SFG_Window* window )
426 #ifdef EGL_VERSION_1_0
427 fghPlatformCloseWindowEGL(window);
429 if( window->Window.Context )
430 glXDestroyContext( fgDisplay.pDisplay.Display, window->Window.Context );
431 window->Window.pContext.FBConfig = NULL;
434 if( window->Window.Handle ) {
435 XDestroyWindow( fgDisplay.pDisplay.Display, window->Window.Handle );
437 /* XFlush( fgDisplay.pDisplay.Display ); */ /* XXX Shouldn't need this */
442 * This function makes the specified window visible
444 void fgPlatformShowWindow( SFG_Window *window )
446 XMapWindow( fgDisplay.pDisplay.Display, window->Window.Handle );
447 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
451 * This function hides the specified window
453 void fgPlatformHideWindow( SFG_Window *window )
455 if( window->Parent == NULL )
456 XWithdrawWindow( fgDisplay.pDisplay.Display,
457 window->Window.Handle,
458 fgDisplay.pDisplay.Screen );
460 XUnmapWindow( fgDisplay.pDisplay.Display,
461 window->Window.Handle );
462 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
466 * Iconify the specified window (top-level windows only)
468 void fgPlatformIconifyWindow( SFG_Window *window )
470 XIconifyWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
471 fgDisplay.pDisplay.Screen );
472 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
474 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
478 * Set the current window's title
480 void fgPlatformGlutSetWindowTitle( const char* title )
484 text.value = (unsigned char *) title;
485 text.encoding = XA_STRING;
487 text.nitems = strlen( title );
490 fgDisplay.pDisplay.Display,
491 fgStructure.CurrentWindow->Window.Handle,
495 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
499 * Set the current window's iconified title
501 void fgPlatformGlutSetIconTitle( const char* title )
505 text.value = (unsigned char *) title;
506 text.encoding = XA_STRING;
508 text.nitems = strlen( title );
511 fgDisplay.pDisplay.Display,
512 fgStructure.CurrentWindow->Window.Handle,
516 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
520 * Change the specified window's position
522 void fgPlatformPositionWindow( SFG_Window *window, int x, int y )
524 XMoveWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
526 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
530 * Lowers the specified window (by Z order change)
532 void fgPlatformPushWindow( SFG_Window *window )
534 XLowerWindow( fgDisplay.pDisplay.Display, window->Window.Handle );
538 * Raises the specified window (by Z order change)
540 void fgPlatformPopWindow( SFG_Window *window )
542 XRaiseWindow( fgDisplay.pDisplay.Display, window->Window.Handle );
546 * Toggle the window's full screen state.
548 void fgPlatformFullScreenToggle( SFG_Window *win )
550 if(fghToggleFullscreen() != -1) {
551 win->State.IsFullscreen = !win->State.IsFullscreen;