Applied James DeLisle's patch adding EWMH _NET_WM_PID support.
[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     fgDisplay.pDisplay.NetWMPid        = None;
220     fgDisplay.pDisplay.ClientMachine   = None;
221
222     fgDisplay.pDisplay.NetWMSupported = fghNetWMSupported();
223
224     if (fgDisplay.pDisplay.NetWMSupported)
225     {
226       const Atom supported = fghGetAtom("_NET_SUPPORTED");
227       const Atom state     = fghGetAtom("_NET_WM_STATE");
228       
229       /* Check if the state hint is supported. */
230       if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, state))
231       {
232         const Atom full_screen = fghGetAtom("_NET_WM_STATE_FULLSCREEN");
233         
234         fgDisplay.pDisplay.State = state;
235         
236         /* Check if the window manager supports full screen. */
237         /**  Check "_NET_WM_ALLOWED_ACTIONS" on our window instead? **/
238         if (fgHintPresent(fgDisplay.pDisplay.RootWindow, supported, full_screen))
239         {
240           fgDisplay.pDisplay.StateFullScreen = full_screen;
241         }
242       }
243
244       fgDisplay.pDisplay.NetWMPid = fghGetAtom("_NET_WM_PID");
245       fgDisplay.pDisplay.ClientMachine = fghGetAtom("WM_CLIENT_MACHINE");
246     }
247
248     /* Get start time */
249     fgState.Time = fgSystemTime();
250     
251
252     fgState.Initialised = GL_TRUE;
253
254     atexit(fgDeinitialize);
255
256     /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
257     fgInitialiseInputDevices();
258 }
259
260 void fgPlatformDeinitialiseInputDevices ( void )
261 {
262         fghCloseInputDevices ();
263
264     fgState.JoysticksInitialised = GL_FALSE;
265     fgState.InputDevsInitialised = GL_FALSE;
266 }
267
268
269 void fgPlatformCloseDisplay ( void )
270 {
271     /*
272      * Make sure all X-client data we have created will be destroyed on
273      * display closing
274      */
275     XSetCloseDownMode( fgDisplay.pDisplay.Display, DestroyAll );
276
277     /*
278      * Close the display connection, destroying all windows we have
279      * created so far
280      */
281     XCloseDisplay( fgDisplay.pDisplay.Display );
282 }
283
284
285 #ifndef EGL_VERSION_1_0
286 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
287 {
288     /* Note that the MVisualInfo is not owned by the MenuContext! */
289     glXDestroyContext( pDisplay.Display, MContext );
290 }
291 #endif