4 * Various freeglut initialization functions.
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 <limits.h> /* LONG_MAX */
31 #include <GL/freeglut.h>
32 #include "fg_internal.h"
34 #include "egl/fg_init_egl.h"
36 /* Return the atom associated with "name". */
37 static Atom fghGetAtom(const char * name)
39 return XInternAtom(fgDisplay.pDisplay.Display, name, False);
43 * Check if "property" is set on "window". The property's values are returned
44 * through "data". If the property is set and is of type "type", return the
45 * number of elements in "data". Return zero otherwise. In both cases, use
46 * "Xfree()" to free "data".
48 static int fghGetWindowProperty(Window window,
51 unsigned char ** data)
54 * Caller always has to use "Xfree()" to free "data", since
55 * "XGetWindowProperty() always allocates one extra byte in prop_return
56 * [i.e. "data"] (even if the property is zero length) [..]".
59 int status; /* Returned by "XGetWindowProperty". */
62 int temp_format; /* Not used. */
63 unsigned long number_of_elements;
64 unsigned long temp_bytes_after; /* Not used. */
67 status = XGetWindowProperty(fgDisplay.pDisplay.Display,
80 FREEGLUT_INTERNAL_ERROR_EXIT(status == Success,
81 "XGetWindowProperty failled",
82 "fghGetWindowProperty");
84 if (type_returned != type)
86 number_of_elements = 0;
89 return number_of_elements;
92 /* Check if the window manager is NET WM compliant. */
93 static int fghNetWMSupported(void)
96 Window ** window_ptr_1;
98 int number_of_windows;
102 net_wm_supported = 0;
104 wm_check = fghGetAtom("_NET_SUPPORTING_WM_CHECK");
105 window_ptr_1 = malloc(sizeof(Window *));
108 * Check that the window manager has set this property on the root window.
109 * The property must be the ID of a child window.
111 number_of_windows = fghGetWindowProperty(fgDisplay.pDisplay.RootWindow,
114 (unsigned char **) window_ptr_1);
115 if (number_of_windows == 1)
117 Window ** window_ptr_2;
119 window_ptr_2 = malloc(sizeof(Window *));
121 /* Check that the window has the same property set to the same value. */
122 number_of_windows = fghGetWindowProperty(**window_ptr_1,
125 (unsigned char **) window_ptr_2);
126 if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
128 /* NET WM compliant */
129 net_wm_supported = 1;
132 XFree(*window_ptr_2);
136 XFree(*window_ptr_1);
139 return net_wm_supported;
142 /* Check if "hint" is present in "property" for "window". */
143 int fgHintPresent(Window window, Atom property, Atom hint)
152 number_of_atoms = fghGetWindowProperty(window,
155 (unsigned char **) &atoms);
156 for (i = 0; i < number_of_atoms; i++)
158 if (atoms[i] == hint)
170 * A call to this function should initialize all the display stuff...
172 void fgPlatformInitialize( const char* displayName )
174 fgDisplay.pDisplay.Display = XOpenDisplay( displayName );
176 if( fgDisplay.pDisplay.Display == NULL )
177 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
179 if ( fgState.XSyncSwitch )
180 XSynchronize(fgDisplay.pDisplay.Display, True);
182 #ifdef EGL_VERSION_1_0
183 fghPlatformInitializeEGL();
185 if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
186 fgError( "OpenGL GLX extension not supported by display '%s'",
187 XDisplayName( displayName ) );
189 /* This forces AMD Catalyst drivers to initialize and register a shutdown
190 * function, which must be done before our own call to atexit to prevent
191 * a crash if glutMainLoop is not called or is not exited cleanly.
194 glXQueryExtensionsString( fgDisplay.pDisplay.Display,
195 DefaultScreen( fgDisplay.pDisplay.Display ));
198 fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
199 fgDisplay.pDisplay.RootWindow = RootWindow(
200 fgDisplay.pDisplay.Display,
201 fgDisplay.pDisplay.Screen
204 fgDisplay.ScreenWidth = DisplayWidth(
205 fgDisplay.pDisplay.Display,
206 fgDisplay.pDisplay.Screen
208 fgDisplay.ScreenHeight = DisplayHeight(
209 fgDisplay.pDisplay.Display,
210 fgDisplay.pDisplay.Screen
213 fgDisplay.ScreenWidthMM = DisplayWidthMM(
214 fgDisplay.pDisplay.Display,
215 fgDisplay.pDisplay.Screen
217 fgDisplay.ScreenHeightMM = DisplayHeightMM(
218 fgDisplay.pDisplay.Display,
219 fgDisplay.pDisplay.Screen
222 fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
224 /* Create the window deletion atom */
225 fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
227 /* Create the state and full screen atoms */
228 fgDisplay.pDisplay.State = None;
229 fgDisplay.pDisplay.StateFullScreen = None;
230 fgDisplay.pDisplay.NetWMPid = None;
231 fgDisplay.pDisplay.ClientMachine = None;
233 fgDisplay.pDisplay.NetWMSupported = fghNetWMSupported();
235 if (fgDisplay.pDisplay.NetWMSupported)
237 const Atom supported = fghGetAtom("_NET_SUPPORTED");
238 const Atom state = fghGetAtom("_NET_WM_STATE");
240 /* Check if the state hint is supported. */
241 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
243 const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
245 fgDisplay.pDisplay.State = state;
247 /* Check if the window manager supports full screen. */
248 /** Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
249 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
251 fgDisplay.pDisplay.StateFullScreen = full_screen;
255 fgDisplay.pDisplay.NetWMPid = fghGetAtom("_NET_WM_PID");
256 fgDisplay.pDisplay.ClientMachine = fghGetAtom("WM_CLIENT_MACHINE");
260 fgState.Time = fgSystemTime();
263 fgState.Initialised = GL_TRUE;
265 atexit(fgDeinitialize);
267 /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
268 fgInitialiseInputDevices();
271 void fgPlatformDeinitialiseInputDevices ( void )
273 fghCloseInputDevices ();
275 fgState.JoysticksInitialised = GL_FALSE;
276 fgState.InputDevsInitialised = GL_FALSE;
280 void fgPlatformCloseDisplay ( void )
283 * Make sure all X-client data we have created will be destroyed on
286 XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
289 * Close the display connection, destroying all windows we have
292 XCloseDisplay( fgDisplay.pDisplay.Display );
296 #ifndef EGL_VERSION_1_0
297 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
299 /* Note that the MVisualInfo is not owned by the MenuContext! */
300 glXDestroyContext( pDisplay.Display, MContext );