renamed all references to freeglut_xxx files (their old names) to fg_xxx
[freeglut] / src / x11 / fg_init_x11.c
1 /*
2  * fg_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     if ( fgState.XSyncSwitch )
180         XSynchronize(fgDisplay.pDisplay.Display, True);
181
182 #ifdef EGL_VERSION_1_0
183     fghPlatformInitializeEGL();
184 #else
185     if( !glXQueryExtension( fgDisplay.pDisplay.Display, NULL, NULL ) )
186         fgError( "OpenGL GLX extension not supported by display '%s'",
187             XDisplayName( displayName ) );
188
189     /* This forces AMD Catalyst drivers to initialize and register a shutdown
190      * function, which must be done before our own call to atexit to prevent
191      * a crash if glutMainLoop is not called or is not exited cleanly.
192      * (see bug #206)
193      */
194     glXQueryExtensionsString( fgDisplay.pDisplay.Display,
195         DefaultScreen( fgDisplay.pDisplay.Display ));
196 #endif
197
198     fgDisplay.pDisplay.Screen = DefaultScreen( fgDisplay.pDisplay.Display );
199     fgDisplay.pDisplay.RootWindow = RootWindow(
200         fgDisplay.pDisplay.Display,
201         fgDisplay.pDisplay.Screen
202     );
203
204     fgDisplay.ScreenWidth  = DisplayWidth(
205         fgDisplay.pDisplay.Display,
206         fgDisplay.pDisplay.Screen
207     );
208     fgDisplay.ScreenHeight = DisplayHeight(
209         fgDisplay.pDisplay.Display,
210         fgDisplay.pDisplay.Screen
211     );
212
213     fgDisplay.ScreenWidthMM = DisplayWidthMM(
214         fgDisplay.pDisplay.Display,
215         fgDisplay.pDisplay.Screen
216     );
217     fgDisplay.ScreenHeightMM = DisplayHeightMM(
218         fgDisplay.pDisplay.Display,
219         fgDisplay.pDisplay.Screen
220     );
221
222     fgDisplay.pDisplay.Connection = ConnectionNumber( fgDisplay.pDisplay.Display );
223
224     /* Create the window deletion atom */
225     fgDisplay.pDisplay.DeleteWindow = fghGetAtom("WM_DELETE_WINDOW");
226
227     /* Create the state and full screen atoms */
228     fgDisplay.pDisplay.State           = None;
229     fgDisplay.pDisplay.StateFullScreen = None;
230     fgDisplay.pDisplay.NetWMPid        = None;
231     fgDisplay.pDisplay.ClientMachine   = None;
232
233     fgDisplay.pDisplay.NetWMSupported = fghNetWMSupported();
234
235     if (fgDisplay.pDisplay.NetWMSupported)
236     {
237       const Atom supported = fghGetAtom("_NET_SUPPORTED");
238       const Atom state     = fghGetAtom("_NET_WM_STATE");
239       
240       /* Check if the state hint is supported. */
241       if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
242       {
243         const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
244         
245         fgDisplay.pDisplay.State = state;
246         
247         /* Check if the window manager supports full screen. */
248         /**  Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
249         if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
250         {
251           fgDisplay.pDisplay.StateFullScreen = full_screen;
252         }
253       }
254
255       fgDisplay.pDisplay.NetWMPid = fghGetAtom("_NET_WM_PID");
256       fgDisplay.pDisplay.ClientMachine = fghGetAtom("WM_CLIENT_MACHINE");
257     }
258
259     /* Get start time */
260     fgState.Time = fgSystemTime();
261     
262
263     fgState.Initialised = GL_TRUE;
264
265     atexit(fgDeinitialize);
266
267     /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
268     fgInitialiseInputDevices();
269 }
270
271 void fgPlatformDeinitialiseInputDevices ( void )
272 {
273         fghCloseInputDevices ();
274
275     fgState.JoysticksInitialised = GL_FALSE;
276     fgState.InputDevsInitialised = GL_FALSE;
277 }
278
279
280 void fgPlatformCloseDisplay ( void )
281 {
282     /*
283      * Make sure all X-client data we have created will be destroyed on
284      * display closing
285      */
286     XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
287
288     /*
289      * Close the display connection, destroying all windows we have
290      * created so far
291      */
292     XCloseDisplay( fgDisplay.pDisplay.Display );
293 }
294
295
296 #ifndef EGL_VERSION_1_0
297 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
298 {
299     /* Note that the MVisualInfo is not owned by the MenuContext! */
300     glXDestroyContext( pDisplay.Display, MContext );
301 }
302 #endif