0df54f2573bfc5481fa7efe385fb9b9f5e3352a8
[freeglut] / src / x11 / fg_init_x11.c
1 /*
2  * freeglut_init_x11.c
3  *
4  * Various freeglut initialization functions.
5  *
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
10  *
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:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
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.
27  */
28
29 #define FREEGLUT_BUILDING_LIB
30 #include <limits.h>  /* LONG_MAX */
31 #include <GL/freeglut.h>
32 #include "fg_internal.h"
33 #include "fg_init.h"
34 #include "egl/fg_init_egl.h"
35
36 /* Return the atom associated with "name". */
37 static Atom fghGetAtom(const char * name)
38 {
39   return XInternAtom(fgDisplay.pDisplay.Display, name, False);
40 }
41
42 /*
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".
47  */
48 static int fghGetWindowProperty(Window window,
49                                 Atom property,
50                                 Atom type,
51                                 unsigned char ** data)
52 {
53   /*
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) [..]".
57    */
58
59   int status;  /*  Returned by "XGetWindowProperty". */
60
61   Atom          type_returned;
62   int           temp_format;             /*  Not used. */
63   unsigned long number_of_elements;
64   unsigned long temp_bytes_after;        /*  Not used. */
65
66
67   status = XGetWindowProperty(fgDisplay.pDisplay.Display,
68                               window,
69                               property,
70                               0,
71                               LONG_MAX,
72                               False,
73                               type,
74                               &type_returned,
75                               &temp_format,
76                               &number_of_elements,
77                               &temp_bytes_after,
78                               data);
79
80   FREEGLUT_INTERNAL_ERROR_EXIT(status == Success,
81                                "XGetWindowProperty failled",
82                                "fghGetWindowProperty");
83
84   if (type_returned != type)
85     {
86       number_of_elements = 0;
87     }
88
89   return number_of_elements;
90 }
91
92 /*  Check if the window manager is NET WM compliant. */
93 static int fghNetWMSupported(void)
94 {
95   Atom wm_check;
96   Window ** window_ptr_1;
97
98   int number_of_windows;
99   int net_wm_supported;
100
101
102   net_wm_supported = 0;
103
104   wm_check = fghGetAtom("_NET_SUPPORTING_WM_CHECK");
105   window_ptr_1 = malloc(sizeof(Window *));
106
107   /*
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.
110    */
111   number_of_windows = fghGetWindowProperty(fgDisplay.pDisplay.RootWindow,
112                                            wm_check,
113                                            XA_WINDOW,
114                                            (unsigned char **) window_ptr_1);
115   if (number_of_windows == 1)
116     {
117       Window ** window_ptr_2;
118
119       window_ptr_2 = malloc(sizeof(Window *));
120
121       /* Check that the window has the same property set to the same value. */
122       number_of_windows = fghGetWindowProperty(**window_ptr_1,
123                                                wm_check,
124                                                XA_WINDOW,
125                                                (unsigned char **) window_ptr_2);
126       if ((number_of_windows == 1) && (**window_ptr_1 == **window_ptr_2))
127       {
128         /* NET WM compliant */
129         net_wm_supported = 1;
130       }
131
132       XFree(*window_ptr_2);
133       free(window_ptr_2);
134     }
135
136         XFree(*window_ptr_1);
137         free(window_ptr_1);
138
139         return net_wm_supported;
140 }
141
142 /*  Check if "hint" is present in "property" for "window". */
143 int fgHintPresent(Window window, Atom property, Atom hint)
144 {
145   Atom *atoms;
146   int number_of_atoms;
147   int supported;
148   int i;
149
150   supported = 0;
151
152   number_of_atoms = fghGetWindowProperty(window,
153                                          property,
154                                          XA_ATOM,
155                                          (unsigned char **) &atoms);
156   for (i = 0; i < number_of_atoms; i++)
157   {
158       if (atoms[i] == hint)
159       {
160           supported = 1;
161           break;
162       }
163   }
164
165   XFree(atoms);
166   return supported;
167 }
168
169 /*
170  * A call to this function should initialize all the display stuff...
171  */
172 void fgPlatformInitialize( const char* displayName )
173 {
174     fgDisplay.pDisplay.Display = XOpenDisplay( displayName );
175
176     if( fgDisplay.pDisplay.Display == NULL )
177         fgError( "failed to open display '%s'", XDisplayName( displayName ) );
178
179 #ifdef EGL_VERSION_1_0
180     fghPlatformInitializeEGL();
181 #else
182     if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
183         fgError( "OpenGL GLX extension not supported by display '%s'",
184             XDisplayName( displayName ) );
185 #endif
186
187     fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
188     fgDisplay.pDisplay.RootWindow = RootWindow(
189         fgDisplay.pDisplay.Display,
190         fgDisplay.pDisplay.Screen
191     );
192
193     fgDisplay.ScreenWidth  = DisplayWidth(
194         fgDisplay.pDisplay.Display,
195         fgDisplay.pDisplay.Screen
196     );
197     fgDisplay.ScreenHeight = DisplayHeight(
198         fgDisplay.pDisplay.Display,
199         fgDisplay.pDisplay.Screen
200     );
201
202     fgDisplay.ScreenWidthMM = DisplayWidthMM(
203         fgDisplay.pDisplay.Display,
204         fgDisplay.pDisplay.Screen
205     );
206     fgDisplay.ScreenHeightMM = DisplayHeightMM(
207         fgDisplay.pDisplay.Display,
208         fgDisplay.pDisplay.Screen
209     );
210
211     fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
212
213     /* Create the window deletion atom */
214     fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
215
216     /* Create the state and full screen atoms */
217     fgDisplay.pDisplay.State           = None;
218     fgDisplay.pDisplay.StateFullScreen = None;
219
220     if (fghNetWMSupported())
221     {
222       const Atom supported = fghGetAtom("_NET_SUPPORTED");
223       const Atom state     = fghGetAtom("_NET_WM_STATE");
224       
225       /* Check if the state hint is supported. */
226       if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
227       {
228         const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
229         
230         fgDisplay.pDisplay.State = state;
231         
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))
235         {
236           fgDisplay.pDisplay.StateFullScreen = full_screen;
237         }
238       }
239     }
240
241     /* Get start time */
242     fgState.Time = fgSystemTime();
243     
244
245     fgState.Initialised = GL_TRUE;
246
247     atexit(fgDeinitialize);
248
249     /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
250     fgInitialiseInputDevices();
251 }
252
253 void fgPlatformDeinitialiseInputDevices ( void )
254 {
255         fghCloseInputDevices ();
256
257     fgState.JoysticksInitialised = GL_FALSE;
258     fgState.InputDevsInitialised = GL_FALSE;
259 }
260
261
262 void fgPlatformCloseDisplay ( void )
263 {
264     /*
265      * Make sure all X-client data we have created will be destroyed on
266      * display closing
267      */
268     XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
269
270     /*
271      * Close the display connection, destroying all windows we have
272      * created so far
273      */
274     XCloseDisplay( fgDisplay.pDisplay.Display );
275 }
276
277
278 #ifndef EGL_VERSION_1_0
279 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
280 {
281     /* Note that the MVisualInfo is not owned by the MenuContext! */
282     glXDestroyContext( pDisplay.Display, MContext );
283 }
284 #endif