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 /* Return the atom associated with "name". */
35 static Atom fghGetAtom(const char * name)
37 return XInternAtom(fgDisplay.pDisplay.Display, name, False);
41 * Check if "property" is set on "window". The property's values are returned
42 * through "data". If the property is set and is of type "type", return the
43 * number of elements in "data". Return zero otherwise. In both cases, use
44 * "Xfree()" to free "data".
46 static int fghGetWindowProperty(Window window,
49 unsigned char ** data)
52 * Caller always has to use "Xfree()" to free "data", since
53 * "XGetWindowProperty() always allocates one extra byte in prop_return
54 * [i.e. "data"] (even if the property is zero length) [..]".
57 int status; /* Returned by "XGetWindowProperty". */
60 int temp_format; /* Not used. */
61 unsigned long number_of_elements;
62 unsigned long temp_bytes_after; /* Not used. */
65 status = XGetWindowProperty(fgDisplay.pDisplay.Display,
78 FREEGLUT_INTERNAL_ERROR_EXIT(status == Success,
79 "XGetWindowProperty failled",
80 "fghGetWindowProperty");
82 if (type_returned != type)
84 number_of_elements = 0;
87 return number_of_elements;
90 /* Check if the window manager is NET WM compliant. */
91 static int fghNetWMSupported(void)
94 Window ** window_ptr_1;
96 int number_of_windows;
100 net_wm_supported = 0;
102 wm_check = fghGetAtom("_NET_SUPPORTING_WM_CHECK");
103 window_ptr_1 = malloc(sizeof(Window *));
106 * Check that the window manager has set this property on the root window.
107 * The property must be the ID of a child window.
109 number_of_windows = fghGetWindowProperty(fgDisplay.pDisplay.RootWindow,
112 (unsigned char **) window_ptr_1);
113 if (number_of_windows == 1)
115 Window ** window_ptr_2;
117 window_ptr_2 = malloc(sizeof(Window *));
119 /* Check that the window has the same property set to the same value. */
120 number_of_windows = fghGetWindowProperty(**window_ptr_1,
123 (unsigned char **) window_ptr_2);
124 if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
126 /* NET WM compliant */
127 net_wm_supported = 1;
130 XFree(*window_ptr_2);
134 XFree(*window_ptr_1);
137 return net_wm_supported;
140 /* Check if "hint" is present in "property" for "window". */
141 int fgHintPresent(Window window, Atom property, Atom hint)
150 number_of_atoms = fghGetWindowProperty(window,
153 (unsigned char **) &atoms);
154 for (i = 0; i < number_of_atoms; i++)
156 if (atoms[i] == hint)
168 * A call to this function should initialize all the display stuff...
170 void fgPlatformInitialize( const char* displayName )
172 fgDisplay.pDisplay.Display = XOpenDisplay( displayName );
174 if( fgDisplay.pDisplay.Display == NULL )
175 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
177 #ifdef EGL_VERSION_1_0
178 fghPlatformInitializeEGL();
180 if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
181 fgError( "OpenGL GLX extension not supported by display '%s'",
182 XDisplayName( displayName ) );
185 fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
186 fgDisplay.pDisplay.RootWindow = RootWindow(
187 fgDisplay.pDisplay.Display,
188 fgDisplay.pDisplay.Screen
191 fgDisplay.ScreenWidth = DisplayWidth(
192 fgDisplay.pDisplay.Display,
193 fgDisplay.pDisplay.Screen
195 fgDisplay.ScreenHeight = DisplayHeight(
196 fgDisplay.pDisplay.Display,
197 fgDisplay.pDisplay.Screen
200 fgDisplay.ScreenWidthMM = DisplayWidthMM(
201 fgDisplay.pDisplay.Display,
202 fgDisplay.pDisplay.Screen
204 fgDisplay.ScreenHeightMM = DisplayHeightMM(
205 fgDisplay.pDisplay.Display,
206 fgDisplay.pDisplay.Screen
209 fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
211 /* Create the window deletion atom */
212 fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
214 /* Create the state and full screen atoms */
215 fgDisplay.pDisplay.State = None;
216 fgDisplay.pDisplay.StateFullScreen = None;
218 if (fghNetWMSupported())
220 const Atom supported = fghGetAtom("_NET_SUPPORTED");
221 const Atom state = fghGetAtom("_NET_WM_STATE");
223 /* Check if the state hint is supported. */
224 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
226 const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
228 fgDisplay.pDisplay.State = state;
230 /* Check if the window manager supports full screen. */
231 /** Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
232 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
234 fgDisplay.pDisplay.StateFullScreen = full_screen;
240 fgState.Initialised = GL_TRUE;
242 atexit(fgDeinitialize);
244 /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
245 fgInitialiseInputDevices();
248 void fgPlatformDeinitialiseInputDevices ( void )
250 fghCloseInputDevices ();
252 fgState.JoysticksInitialised = GL_FALSE;
253 fgState.InputDevsInitialised = GL_FALSE;
257 void fgPlatformCloseDisplay ( void )
260 * Make sure all X-client data we have created will be destroyed on
263 XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
266 * Close the display connection, destroying all windows we have
269 XCloseDisplay( fgDisplay.pDisplay.Display );
273 #ifndef EGL_VERSION_1_0
274 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
276 /* Note that the MVisualInfo is not owned by the MenuContext! */
277 glXDestroyContext( pDisplay.Display, MContext );