Fix more compiler warnings
[freeglut] / src / x11 / fg_state_x11.c
1 /*
2  * freeglut_state_x11.c
3  *
4  * X11-specific freeglut state query methods.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Creation date: Sat Feb 4 2012
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
30 #ifdef EGL_VERSION_1_0
31 #include "egl/fg_state_egl.h"
32 #endif
33
34 int fgPlatformGlutDeviceGet ( GLenum eWhat )
35 {
36     switch( eWhat )
37     {
38     case GLUT_HAS_KEYBOARD:
39         /*
40          * X11 has a core keyboard by definition, although it can
41          * be present as a virtual/dummy keyboard. For now, there
42          * is no reliable way to tell if a real keyboard is present.
43          */
44         return 1;
45
46     /* X11 has a mouse by definition */
47     case GLUT_HAS_MOUSE:
48         return 1 ;
49
50     case GLUT_NUM_MOUSE_BUTTONS:
51         /* We should be able to pass NULL when the last argument is zero,
52          * but at least one X server has a bug where this causes a segfault.
53          *
54          * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
55          * rather than an Axis; "freeglut_main.c" expects this when
56          * checking for a wheel event.
57          */
58         {
59             unsigned char map;
60             int nbuttons = XGetPointerMapping(fgDisplay.pDisplay.Display, &map,0);
61             return nbuttons;
62         }
63
64     default:
65         fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
66         break;
67     }
68
69     /* And now -- the failure. */
70     return -1;
71 }
72
73
74 int fgPlatformGlutGet ( GLenum eWhat )
75 {
76     switch( eWhat )
77     {
78
79     /*
80      * Those calls are somewhat similiar, as they use XGetWindowAttributes()
81      * function
82      */
83     case GLUT_WINDOW_X:
84     case GLUT_WINDOW_Y:
85     case GLUT_WINDOW_BORDER_WIDTH:
86     case GLUT_WINDOW_HEADER_HEIGHT:
87     {
88         int x, y;
89         Window w;
90
91         if( fgStructure.CurrentWindow == NULL )
92             return 0;
93
94         XTranslateCoordinates(
95             fgDisplay.pDisplay.Display,
96             fgStructure.CurrentWindow->Window.Handle,
97             fgDisplay.pDisplay.RootWindow,
98             0, 0, &x, &y, &w);
99
100         switch ( eWhat )
101         {
102         case GLUT_WINDOW_X: return x;
103         case GLUT_WINDOW_Y: return y;
104         }
105
106         if ( w == 0 )
107             return 0;
108         XTranslateCoordinates(
109             fgDisplay.pDisplay.Display,
110             fgStructure.CurrentWindow->Window.Handle,
111             w, 0, 0, &x, &y, &w);
112
113         switch ( eWhat )
114         {
115         case GLUT_WINDOW_BORDER_WIDTH:  return x;
116         case GLUT_WINDOW_HEADER_HEIGHT: return y;
117         }
118     }
119
120     case GLUT_WINDOW_WIDTH:
121     case GLUT_WINDOW_HEIGHT:
122     {
123         XWindowAttributes winAttributes;
124
125         if( fgStructure.CurrentWindow == NULL )
126             return 0;
127         XGetWindowAttributes(
128             fgDisplay.pDisplay.Display,
129             fgStructure.CurrentWindow->Window.Handle,
130             &winAttributes
131         );
132         switch ( eWhat )
133         {
134         case GLUT_WINDOW_WIDTH:            return winAttributes.width ;
135         case GLUT_WINDOW_HEIGHT:           return winAttributes.height ;
136         }
137     }
138     
139     /* Colormap size is handled in a bit different way than all the rest */
140     case GLUT_WINDOW_COLORMAP_SIZE:
141         if(
142 #ifndef EGL_VERSION_1_0
143            fgPlatformGetConfig( GLX_RGBA ) ||
144 #endif
145            fgStructure.CurrentWindow == NULL)
146         {
147             /*
148              * We've got a RGBA visual, so there is no colormap at all.
149              * The other possibility is that we have no current window set.
150              */
151             return 0;
152         }
153         else
154         {
155           XVisualInfo * visualInfo;
156 #ifdef EGL_VERSION_1_0
157           EGLint vid = 0;
158           XVisualInfo visualTemplate;
159           int num_visuals;
160           if (!eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display,
161                                   fgStructure.CurrentWindow->Window.pContext.egl.Config,
162                                   EGL_NATIVE_VISUAL_ID, &vid))
163             fgError("eglGetConfigAttrib(EGL_NATIVE_VISUAL_ID) failed");
164           visualTemplate.visualid = vid;
165           visualInfo = XGetVisualInfo(fgDisplay.pDisplay.Display, VisualIDMask, &visualTemplate, &num_visuals);
166 #else
167           const GLXFBConfig fbconfig =
168                 fgStructure.CurrentWindow->Window.pContext.FBConfig;
169
170           visualInfo =
171                 glXGetVisualFromFBConfig( fgDisplay.pDisplay.Display, fbconfig );
172 #endif
173           const int result = visualInfo->visual->map_entries;
174
175           XFree(visualInfo);
176
177           return result;
178         }
179
180     default:
181 #ifdef EGL_VERSION_1_0
182       return fghPlatformGlutGetEGL(eWhat);
183 #else
184       return fghPlatformGlutGetGLX(eWhat);
185 #endif
186     }
187 }