Implement initial Wayland support
[freeglut] / src / wayland / fg_state_wl.c
1 /*
2  * fg_state_wl.c
3  *
4  * Wayland-specific freeglut state query methods.
5  *
6  * Copyright (c) 2015 Manuel Bachmann. All Rights Reserved.
7  * Written by Manuel Bachmann, <tarnyko@tarnyko.net>
8  * Creation date: Sun Mar 23 2015
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  * MANUEL BACHMANN 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 #include "egl/fg_state_egl.h"
31
32 int fgPlatformGlutDeviceGet ( GLenum eWhat )
33 {
34     switch( eWhat )
35     {
36     case GLUT_HAS_KEYBOARD:
37         if( fgDisplay.pDisplay.keyboard )
38           return 1;
39         else
40           return 0;
41
42     case GLUT_HAS_MOUSE:
43         /* we want the touchscreen to behave like a mouse,
44          * so let us pretend it is one.
45          */
46         if( fgDisplay.pDisplay.pointer ||
47             fgDisplay.pDisplay.touch )
48           return 1;
49         else
50           return 0;
51
52     case GLUT_NUM_MOUSE_BUTTONS:
53         /* Wayland has no way of telling us how much buttons
54          * a mouse has, unless the actual event gets sent to
55          * the client. As we are only handling 3 buttons
56          * currently, return this fixed number for now.
57          */
58         if( fgDisplay.pDisplay.pointer )
59           return 3;
60         /* touchscreen is considered as having one button */
61         else if( fgDisplay.pDisplay.touch )
62           return 1;
63         else
64           return 0;
65
66     default:
67         fgWarning( "glutDeviceGet(): missing enum handle %d", eWhat );
68         return -1;
69     }
70 }
71
72
73 int fgPlatformGlutGet ( GLenum eWhat )
74 {
75     switch( eWhat )
76     {
77
78     /*
79      * Those calls are pointless under Wayland, so inform the user
80      */
81     case GLUT_WINDOW_X:
82     case GLUT_WINDOW_Y:
83     {
84         if( fgStructure.CurrentWindow == NULL )
85         {
86             return 0;
87         }
88         else
89         {
90             fgWarning( "glutGet(): GLUT_WINDOW_X/Y properties "
91                                    "unsupported under Wayland" );
92             return -1;
93         }
94     }
95
96     /*
97      * TODO : support this correctly once we will start drawing
98      *  client-side decorations
99      */
100     case GLUT_WINDOW_BORDER_WIDTH:
101     case GLUT_WINDOW_HEADER_HEIGHT:
102     {
103         if( fgStructure.CurrentWindow == NULL ||
104             fgStructure.CurrentWindow->Parent )
105             /* can't get widths/heights if no current window
106              * and child windows don't have borders */
107             return 0;
108
109         return 0;
110     }
111
112     case GLUT_WINDOW_WIDTH:
113     case GLUT_WINDOW_HEIGHT:
114     {
115         if( fgStructure.CurrentWindow == NULL )
116             return 0;
117
118         switch ( eWhat )
119         {
120         case GLUT_WINDOW_WIDTH:
121             return fgStructure.CurrentWindow->State.Width;
122         case GLUT_WINDOW_HEIGHT:
123             return fgStructure.CurrentWindow->State.Height;
124         }
125     }
126
127     /* Colormap size is handled in a bit different way than all the rest */
128     case GLUT_WINDOW_COLORMAP_SIZE:
129     {
130         if( fgStructure.CurrentWindow == NULL )
131         {
132             return 0;
133         }
134         else
135         {
136             int result = 0;
137             if ( ! eglGetConfigAttrib( fgDisplay.pDisplay.egl.Display,
138                      fgStructure.CurrentWindow->Window.pContext.egl.Config,
139                      EGL_BUFFER_SIZE, &result ) )
140               fgError( "eglGetConfigAttrib(EGL_BUFFER_SIZE) failed" );
141
142             return result;
143         }
144     }
145
146     default:
147       return fghPlatformGlutGetEGL( eWhat );
148     }
149 }
150