2 * freeglut_init_x11.c
\r
4 * Various freeglut initialization functions.
\r
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
\r
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
\r
8 * Copied for Platform code by Evan Felix <karcaw at gmail.com>
\r
9 * Creation date: Thur Feb 2 2012
\r
11 * Permission is hereby granted, free of charge, to any person obtaining a
\r
12 * copy of this software and associated documentation files (the "Software"),
\r
13 * to deal in the Software without restriction, including without limitation
\r
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
15 * and/or sell copies of the Software, and to permit persons to whom the
\r
16 * Software is furnished to do so, subject to the following conditions:
\r
18 * The above copyright notice and this permission notice shall be included
\r
19 * in all copies or substantial portions of the Software.
\r
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
24 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
25 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
29 #define FREEGLUT_BUILDING_LIB
\r
30 #include <limits.h> /* LONG_MAX */
\r
31 #include <GL/freeglut.h>
\r
32 #include "../Common/freeglut_internal.h"
\r
34 /* Return the atom associated with "name". */
\r
35 static Atom fghGetAtom(const char * name)
\r
37 return XInternAtom(fgDisplay.pDisplay.Display, name, False);
\r
41 * Check if "property" is set on "window". The property's values are returned
\r
42 * through "data". If the property is set and is of type "type", return the
\r
43 * number of elements in "data". Return zero otherwise. In both cases, use
\r
44 * "Xfree()" to free "data".
\r
46 static int fghGetWindowProperty(Window window,
\r
49 unsigned char ** data)
\r
52 * Caller always has to use "Xfree()" to free "data", since
\r
53 * "XGetWindowProperty() always allocates one extra byte in prop_return
\r
54 * [i.e. "data"] (even if the property is zero length) [..]".
\r
57 int status; /* Returned by "XGetWindowProperty". */
\r
60 int temp_format; /* Not used. */
\r
61 unsigned long number_of_elements;
\r
62 unsigned long temp_bytes_after; /* Not used. */
\r
65 status = XGetWindowProperty(fgDisplay.pDisplay.Display,
\r
74 &number_of_elements,
\r
78 FREEGLUT_INTERNAL_ERROR_EXIT(status == Success,
\r
79 "XGetWindowProperty failled",
\r
80 "fghGetWindowProperty");
\r
82 if (type_returned != type)
\r
84 number_of_elements = 0;
\r
87 return number_of_elements;
\r
90 /* Check if the window manager is NET WM compliant. */
\r
91 static int fghNetWMSupported(void)
\r
94 Window ** window_ptr_1;
\r
96 int number_of_windows;
\r
97 int net_wm_supported;
\r
100 net_wm_supported = 0;
\r
102 wm_check = fghGetAtom("_NET_SUPPORTING_WM_CHECK");
\r
103 window_ptr_1 = malloc(sizeof(Window *));
\r
106 * Check that the window manager has set this property on the root window.
\r
107 * The property must be the ID of a child window.
\r
109 number_of_windows = fghGetWindowProperty(fgDisplay.pDisplay.RootWindow,
\r
112 (unsigned char **) window_ptr_1);
\r
113 if (number_of_windows == 1)
\r
115 Window ** window_ptr_2;
\r
117 window_ptr_2 = malloc(sizeof(Window *));
\r
119 /* Check that the window has the same property set to the same value. */
\r
120 number_of_windows = fghGetWindowProperty(**window_ptr_1,
\r
123 (unsigned char **) window_ptr_2);
\r
124 if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
\r
126 /* NET WM compliant */
\r
127 net_wm_supported = 1;
\r
130 XFree(*window_ptr_2);
\r
131 free(window_ptr_2);
\r
134 XFree(*window_ptr_1);
\r
135 free(window_ptr_1);
\r
137 return net_wm_supported;
\r
140 /* Check if "hint" is present in "property" for "window". */
\r
141 int fgHintPresent(Window window, Atom property, Atom hint)
\r
144 int number_of_atoms;
\r
150 number_of_atoms = fghGetWindowProperty(window,
\r
153 (unsigned char **) &atoms);
\r
154 for (i = 0; i < number_of_atoms; i++)
\r
156 if (atoms[i] == hint)
\r
168 * A call to this function should initialize all the display stuff...
\r
170 void fgPlatformInitialize( const char* displayName )
\r
172 fgDisplay.pDisplay.Display = XOpenDisplay( displayName );
\r
174 if( fgDisplay.pDisplay.Display == NULL )
\r
175 fgError( "failed to open display '%s'", XDisplayName( displayName ) );
\r
177 if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
\r
178 fgError( "OpenGL GLX extension not supported by display '%s'",
\r
179 XDisplayName( displayName ) );
\r
181 fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
\r
182 fgDisplay.pDisplay.RootWindow = RootWindow(
\r
183 fgDisplay.pDisplay.Display,
\r
184 fgDisplay.pDisplay.Screen
\r
187 fgDisplay.ScreenWidth = DisplayWidth(
\r
188 fgDisplay.pDisplay.Display,
\r
189 fgDisplay.pDisplay.Screen
\r
191 fgDisplay.ScreenHeight = DisplayHeight(
\r
192 fgDisplay.pDisplay.Display,
\r
193 fgDisplay.pDisplay.Screen
\r
196 fgDisplay.ScreenWidthMM = DisplayWidthMM(
\r
197 fgDisplay.pDisplay.Display,
\r
198 fgDisplay.pDisplay.Screen
\r
200 fgDisplay.ScreenHeightMM = DisplayHeightMM(
\r
201 fgDisplay.pDisplay.Display,
\r
202 fgDisplay.pDisplay.Screen
\r
205 fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
\r
207 /* Create the window deletion atom */
\r
208 fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
\r
210 /* Create the state and full screen atoms */
\r
211 fgDisplay.pDisplay.State = None;
\r
212 fgDisplay.pDisplay.StateFullScreen = None;
\r
214 if (fghNetWMSupported())
\r
216 const Atom supported = fghGetAtom("_NET_SUPPORTED");
\r
217 const Atom state = fghGetAtom("_NET_WM_STATE");
\r
219 /* Check if the state hint is supported. */
\r
220 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
\r
222 const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
\r
224 fgDisplay.pDisplay.State = state;
\r
226 /* Check if the window manager supports full screen. */
\r
227 /** Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
\r
228 if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
\r
230 fgDisplay.pDisplay.StateFullScreen = full_screen;
\r
236 fgState.Initialised = GL_TRUE;
\r
238 atexit(fgDeinitialize);
\r
240 /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
\r
241 fgInitialiseInputDevices();
\r
244 void fgPlatformDeinitialiseInputDevices ( void )
\r
246 fghCloseInputDevices ();
\r
248 fgState.JoysticksInitialised = GL_FALSE;
\r
249 fgState.InputDevsInitialised = GL_FALSE;
\r
253 void fgPlatformCloseDisplay ( void )
\r
256 * Make sure all X-client data we have created will be destroyed on
\r
259 XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
\r
262 * Close the display connection, destroying all windows we have
\r
265 XCloseDisplay( fgDisplay.pDisplay.Display );
\r
269 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
\r
271 /* Note that the MVisualInfo is not owned by the MenuContext! */
\r
272 glXDestroyContext( pDisplay.Display, MContext );
\r