3aa19b1a7f9e56caa1bc3e9d57090e41d293aa86
[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 #else
33 #include "x11/fg_state_x11_glx.h"
34 #endif
35
36 int fgPlatformGlutDeviceGet ( GLenum eWhat )
37 {
38     switch( eWhat )
39     {
40     case GLUT_HAS_KEYBOARD:
41         /*
42          * X11 has a core keyboard by definition, although it can
43          * be present as a virtual/dummy keyboard. For now, there
44          * is no reliable way to tell if a real keyboard is present.
45          */
46         return 1;
47
48     /* X11 has a mouse by definition */
49     case GLUT_HAS_MOUSE:
50         return 1 ;
51
52     case GLUT_NUM_MOUSE_BUTTONS:
53         /* We should be able to pass NULL when the last argument is zero,
54          * but at least one X server has a bug where this causes a segfault.
55          *
56          * In XFree86/Xorg servers, a mouse wheel is seen as two buttons
57          * rather than an Axis; "freeglut_main.c" expects this when
58          * checking for a wheel event.
59          */
60         {
61             unsigned char map;
62             int nbuttons = XGetPointerMapping(fgDisplay.pDisplay.Display, &map,0);
63             return nbuttons;
64         }
65
66     default:
67         fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
68         break;
69     }
70
71     /* And now -- the failure. */
72     return -1;
73 }
74
75
76 int fgPlatformGlutGet ( GLenum eWhat )
77 {
78     switch( eWhat )
79     {
80
81     /*
82      * Those calls are somewhat similiar, as they use XGetWindowAttributes()
83      * function
84      */
85     case GLUT_WINDOW_X:
86     case GLUT_WINDOW_Y:
87     case GLUT_WINDOW_BORDER_WIDTH:
88     case GLUT_WINDOW_HEADER_HEIGHT:
89     {
90         int x, y;
91         Window w;
92
93         if( fgStructure.CurrentWindow == NULL )
94             return 0;
95
96         XTranslateCoordinates(
97             fgDisplay.pDisplay.Display,
98             fgStructure.CurrentWindow->Window.Handle,
99             fgDisplay.pDisplay.RootWindow,
100             0, 0, &x, &y, &w);
101
102         switch ( eWhat )
103         {
104         case GLUT_WINDOW_X: return x;
105         case GLUT_WINDOW_Y: return y;
106         }
107
108         if ( w == 0 )
109             return 0;
110         XTranslateCoordinates(
111             fgDisplay.pDisplay.Display,
112             fgStructure.CurrentWindow->Window.Handle,
113             w, 0, 0, &x, &y, &w);
114
115         switch ( eWhat )
116         {
117         case GLUT_WINDOW_BORDER_WIDTH:  return x;
118         case GLUT_WINDOW_HEADER_HEIGHT: return y;
119         }
120     }
121
122     case GLUT_WINDOW_WIDTH:
123     case GLUT_WINDOW_HEIGHT:
124     {
125         XWindowAttributes winAttributes;
126
127         if( fgStructure.CurrentWindow == NULL )
128             return 0;
129         XGetWindowAttributes(
130             fgDisplay.pDisplay.Display,
131             fgStructure.CurrentWindow->Window.Handle,
132             &winAttributes
133         );
134         switch ( eWhat )
135         {
136         case GLUT_WINDOW_WIDTH:            return winAttributes.width ;
137         case GLUT_WINDOW_HEIGHT:           return winAttributes.height ;
138         }
139     }
140     
141     /* Colormap size is handled in a bit different way than all the rest */
142     case GLUT_WINDOW_COLORMAP_SIZE:
143         if(
144 #ifndef EGL_VERSION_1_0
145            fgPlatformGetConfig( GLX_RGBA ) ||
146 #endif
147            fgStructure.CurrentWindow == NULL)
148         {
149             /*
150              * We've got a RGBA visual, so there is no colormap at all.
151              * The other possibility is that we have no current window set.
152              */
153             return 0;
154         }
155         else
156         {
157           XVisualInfo * visualInfo;
158 #ifdef EGL_VERSION_1_0
159           EGLint vid = 0;
160           XVisualInfo visualTemplate;
161           int num_visuals;
162           if (!eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display,
163                                   fgStructure.CurrentWindow->Window.pContext.egl.Config,
164                                   EGL_NATIVE_VISUAL_ID, &vid))
165             fgError("eglGetConfigAttrib(EGL_NATIVE_VISUAL_ID) failed");
166           visualTemplate.visualid = vid;
167           visualInfo = XGetVisualInfo(fgDisplay.pDisplay.Display, VisualIDMask, &visualTemplate, &num_visuals);
168 #else
169           const GLXFBConfig fbconfig =
170                 fgStructure.CurrentWindow->Window.pContext.FBConfig;
171
172           visualInfo =
173                 glXGetVisualFromFBConfig( fgDisplay.pDisplay.Display, fbconfig );
174 #endif
175           const int result = visualInfo->visual->map_entries;
176
177           XFree(visualInfo);
178
179           return result;
180         }
181
182     default:
183 #ifdef EGL_VERSION_1_0
184       return fghPlatformGlutGetEGL(eWhat);
185 #else
186       return fghPlatformGlutGetGLX(eWhat);
187 #endif
188     }
189 }