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