Merge branch 'master' into gallium-0.2
[mesa.git] / src / glut / dos / state.c
1 /*
2 * DOS/DJGPP Mesa Utility Toolkit
3 * Version: 1.0
4 *
5 * Copyright (C) 2005 Daniel Borca All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * DANIEL BORCA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdio.h>
27
28 #include "internal.h"
29
30
31 #define FREQUENCY 100 /* set this to zero to use the default timer */
32
33
34 static int timer_installed;
35 #if FREQUENCY
36 static volatile int ticks;
37
38
39 static void
40 ticks_timer (void *p)
41 {
42 (void)p;
43 ticks++;
44 } ENDOFUNC(ticks_timer)
45 #else
46 #include <time.h>
47
48 static struct timeval then;
49 #endif
50
51
52 int APIENTRY
53 glutGet (GLenum type)
54 {
55 switch (type) {
56 case GLUT_WINDOW_X:
57 return _glut_current->xpos;
58 case GLUT_WINDOW_Y:
59 return _glut_current->ypos;
60 case GLUT_WINDOW_WIDTH:
61 return _glut_current->width;
62 case GLUT_WINDOW_HEIGHT:
63 return _glut_current->height;
64 case GLUT_WINDOW_STENCIL_SIZE:
65 return _glut_visual.stencil;
66 case GLUT_WINDOW_DEPTH_SIZE:
67 return _glut_visual.depth;
68 case GLUT_WINDOW_RGBA:
69 return !(_glut_default.mode & GLUT_INDEX);
70 case GLUT_WINDOW_COLORMAP_SIZE:
71 return (_glut_default.mode & GLUT_INDEX) ? (256 - RESERVED_COLORS) : 0;
72 case GLUT_SCREEN_WIDTH:
73 return _glut_visual.geometry[0];
74 case GLUT_SCREEN_HEIGHT:
75 return _glut_visual.geometry[1];
76 case GLUT_INIT_WINDOW_X:
77 return _glut_default.x;
78 case GLUT_INIT_WINDOW_Y:
79 return _glut_default.y;
80 case GLUT_INIT_WINDOW_WIDTH:
81 return _glut_default.width;
82 case GLUT_INIT_WINDOW_HEIGHT:
83 return _glut_default.height;
84 case GLUT_INIT_DISPLAY_MODE:
85 return _glut_default.mode;
86 case GLUT_ELAPSED_TIME:
87 #if FREQUENCY
88 if (!timer_installed) {
89 timer_installed = GL_TRUE;
90 LOCKDATA(ticks);
91 LOCKFUNC(ticks_timer);
92 pc_install_int(ticks_timer, NULL, FREQUENCY);
93 }
94 return ticks * 1000 / FREQUENCY;
95 #else
96 if (!timer_installed) {
97 timer_installed = GL_TRUE;
98 gettimeofday(&then, NULL);
99 return 0;
100 } else {
101 struct timeval now;
102 gettimeofday(&now, NULL);
103 return (now.tv_usec - then.tv_usec) / 1000 +
104 (now.tv_sec - then.tv_sec) * 1000;
105 }
106 #endif
107 default:
108 return -1;
109 }
110 }
111
112
113 int APIENTRY
114 glutDeviceGet (GLenum type)
115 {
116 switch (type) {
117 case GLUT_HAS_KEYBOARD:
118 return GL_TRUE;
119 case GLUT_HAS_MOUSE:
120 return (_glut_mouse != 0);
121 case GLUT_NUM_MOUSE_BUTTONS:
122 return _glut_mouse;
123 case GLUT_HAS_SPACEBALL:
124 case GLUT_HAS_DIAL_AND_BUTTON_BOX:
125 case GLUT_HAS_TABLET:
126 return GL_FALSE;
127 case GLUT_NUM_SPACEBALL_BUTTONS:
128 case GLUT_NUM_BUTTON_BOX_BUTTONS:
129 case GLUT_NUM_DIALS:
130 case GLUT_NUM_TABLET_BUTTONS:
131 return 0;
132 default:
133 return -1;
134 }
135 }
136
137
138 int APIENTRY
139 glutGetModifiers (void)
140 {
141 int mod = 0;
142 int shifts = pc_keyshifts();
143
144 if (shifts & (KB_SHIFT_FLAG | KB_CAPSLOCK_FLAG)) {
145 mod |= GLUT_ACTIVE_SHIFT;
146 }
147
148 if (shifts & KB_ALT_FLAG) {
149 mod |= GLUT_ACTIVE_ALT;
150 }
151
152 if (shifts & KB_CTRL_FLAG) {
153 mod |= GLUT_ACTIVE_CTRL;
154 }
155
156 return mod;
157 }
158
159
160 void APIENTRY
161 glutReportErrors (void)
162 {
163 /* reports all the OpenGL errors that happened till now */
164 }
165
166
167 /* GAME MODE
168 * Hack alert: incomplete... what is GameMode, anyway?
169 */
170 static GLint game;
171 static GLboolean game_possible;
172 static GLboolean game_active;
173 static GLuint game_width;
174 static GLuint game_height;
175 static GLuint game_bpp;
176 static GLuint game_refresh;
177
178
179 void APIENTRY
180 glutGameModeString (const char *string)
181 {
182 if (sscanf(string, "%ux%u:%u@%u", &game_width, &game_height, &game_bpp, &game_refresh) == 4) {
183 game_possible = GL_TRUE;
184 }
185 }
186
187
188 int APIENTRY
189 glutGameModeGet (GLenum mode)
190 {
191 switch (mode) {
192 case GLUT_GAME_MODE_ACTIVE:
193 return game_active;
194 case GLUT_GAME_MODE_POSSIBLE:
195 return game_possible && !_glut_current;
196 case GLUT_GAME_MODE_WIDTH:
197 return game_active ? (int)game_width : -1;
198 case GLUT_GAME_MODE_HEIGHT:
199 return game_active ? (int)game_height : -1;
200 case GLUT_GAME_MODE_PIXEL_DEPTH:
201 return game_active ? (int)game_bpp : -1;
202 case GLUT_GAME_MODE_REFRESH_RATE:
203 return game_active ? (int)game_refresh : -1;
204 default:
205 return -1;
206 }
207 }
208
209
210 int APIENTRY
211 glutEnterGameMode (void)
212 {
213 if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
214 _glut_visual.bpp = game_bpp;
215 _glut_visual.refresh = game_refresh;
216
217 glutInitWindowSize(game_width, game_height);
218
219 if ((game = glutCreateWindow("<game>")) > 0) {
220 game_active = GL_TRUE;
221 }
222
223 return game;
224 } else {
225 return 0;
226 }
227 }
228
229
230 void GLUTAPIENTRY
231 glutLeaveGameMode (void)
232 {
233 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE)) {
234 game_active = GL_FALSE;
235
236 glutDestroyWindow(game);
237 }
238 }