79a7ac93fa3993415a6f97e28ebb81f12626fbb1
[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 p,w;
92
93         if( fgStructure.CurrentWindow == NULL )
94             return 0;
95
96         if (fgStructure.CurrentWindow->Parent)
97             /* For child window, we should return relative to upper-left
98              * of parent's client area.
99              */
100             p = fgStructure.CurrentWindow->Parent->Window.Handle;
101         else
102             p = fgDisplay.pDisplay.RootWindow;
103             
104         XTranslateCoordinates(
105             fgDisplay.pDisplay.Display,
106             fgStructure.CurrentWindow->Window.Handle,
107             p,
108             0, 0, &x, &y, &w);
109
110         switch ( eWhat )
111         {
112         case GLUT_WINDOW_X: return x;
113         case GLUT_WINDOW_Y: return y;
114         }
115
116         if ( w == 0 || fgStructure.CurrentWindow->Parent)
117             /* logic below needs w, and child windows don't have borders */
118             return 0;
119         XTranslateCoordinates(
120             fgDisplay.pDisplay.Display,
121             fgStructure.CurrentWindow->Window.Handle,
122             w, 0, 0, &x, &y, &w);
123
124         switch ( eWhat )
125         {
126         case GLUT_WINDOW_BORDER_WIDTH:  return x;
127         case GLUT_WINDOW_HEADER_HEIGHT: return y;
128         }
129     }
130
131     case GLUT_WINDOW_WIDTH:
132     case GLUT_WINDOW_HEIGHT:
133     {
134         XWindowAttributes winAttributes;
135
136         if( fgStructure.CurrentWindow == NULL )
137             return 0;
138         XGetWindowAttributes(
139             fgDisplay.pDisplay.Display,
140             fgStructure.CurrentWindow->Window.Handle,
141             &winAttributes
142         );
143         switch ( eWhat )
144         {
145         case GLUT_WINDOW_WIDTH:            return winAttributes.width ;
146         case GLUT_WINDOW_HEIGHT:           return winAttributes.height ;
147         }
148     }
149     
150     /* Colormap size is handled in a bit different way than all the rest */
151     case GLUT_WINDOW_COLORMAP_SIZE:
152         if(
153 #ifndef EGL_VERSION_1_0
154            fgPlatformGetConfig( GLX_RGBA ) ||
155 #endif
156            fgStructure.CurrentWindow == NULL)
157         {
158             /*
159              * We've got a RGBA visual, so there is no colormap at all.
160              * The other possibility is that we have no current window set.
161              */
162             return 0;
163         }
164         else
165         {
166           XVisualInfo * visualInfo;
167                   int result;
168 #ifdef EGL_VERSION_1_0
169           EGLint vid = 0;
170           XVisualInfo visualTemplate;
171           int num_visuals;
172           if (!eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display,
173                                   fgStructure.CurrentWindow->Window.pContext.egl.Config,
174                                   EGL_NATIVE_VISUAL_ID, &vid))
175             fgError("eglGetConfigAttrib(EGL_NATIVE_VISUAL_ID) failed");
176           visualTemplate.visualid = vid;
177           visualInfo = XGetVisualInfo(fgDisplay.pDisplay.Display, VisualIDMask, &visualTemplate, &num_visuals);
178 #else
179           {
180           const GLXFBConfig fbconfig =
181                 fgStructure.CurrentWindow->Window.pContext.FBConfig;
182
183           visualInfo =
184                 glXGetVisualFromFBConfig( fgDisplay.pDisplay.Display, fbconfig );
185           }
186 #endif
187           result = visualInfo->visual->map_entries;
188
189           XFree(visualInfo);
190
191           return result;
192         }
193
194     default:
195 #ifdef EGL_VERSION_1_0
196       return fghPlatformGlutGetEGL(eWhat);
197 #else
198       return fghPlatformGlutGetGLX(eWhat);
199 #endif
200     }
201 }