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