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 #ifdef EGL_VERSION_1_0
180 fghPlatformInitializeEGL();
182 if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
183 fgError( "OpenGL GLX extension not supported by display '%s'",
184 XDisplayName( displayName ) );
187 fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
188 fgDisplay.pDisplay.RootWindow = RootWindow(
189 fgDisplay.pDisplay.Display,
190 fgDisplay.pDisplay.Screen
193 fgDisplay.ScreenWidth = DisplayWidth(
194 fgDisplay.pDisplay.Display,
195 fgDisplay.pDisplay.Screen
197 fgDisplay.ScreenHeight = DisplayHeight(
198 fgDisplay.pDisplay.Display,
199 fgDisplay.pDisplay.Screen
202 fgDisplay.ScreenWidthMM = DisplayWidthMM(
203 fgDisplay.pDisplay.Display,
204 fgDisplay.pDisplay.Screen
206 fgDisplay.ScreenHeightMM = DisplayHeightMM(
207 fgDisplay.pDisplay.Display,
208 fgDisplay.pDisplay.Screen
211 fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
213 /* Create the window deletion atom */
214 fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
216 /* Create the state and full screen atoms */
217 fgDisplay.pDisplay.State = None;
218 fgDisplay.pDisplay.StateFullScreen = None;
220 if (fghNetWMSupported())
222 const Atom supported = fghGetAtom("_NET_SUPPORTED");
223 const Atom state = fghGetAtom("_NET_WM_STATE");
225 /* Check if the state hint is supported. */
226 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
228 const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
230 fgDisplay.pDisplay.State = state;
232 /* Check if the window manager supports full screen. */
233 /** Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
234 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
236 fgDisplay.pDisplay.StateFullScreen = full_screen;
242 fgState.Initialised = GL_TRUE;
244 atexit(fgDeinitialize);
246 /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
247 fgInitialiseInputDevices();
250 void fgPlatformDeinitialiseInputDevices ( void )
252 fghCloseInputDevices ();
254 fgState.JoysticksInitialised = GL_FALSE;
255 fgState.InputDevsInitialised = GL_FALSE;
259 void fgPlatformCloseDisplay ( void )
262 * Make sure all X-client data we have created will be destroyed on
265 XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
268 * Close the display connection, destroying all windows we have
271 XCloseDisplay( fgDisplay.pDisplay.Display );
275 #ifndef EGL_VERSION_1_0
276 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
278 /* Note that the MVisualInfo is not owned by the MenuContext! */
279 glXDestroyContext( pDisplay.Display, MContext );