DOS and glide driver updates from Daniel Borca
[mesa.git] / src / glut / dos / init.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.0
4 * Copyright (C) 1995-1998 Brian Paul
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22 * DOS/DJGPP glut driver v1.4 for Mesa
23 *
24 * Copyright (C) 2002 - Borca Daniel
25 * Email : dborca@users.sourceforge.net
26 * Web : http://www.geocities.com/dborca
27 */
28
29
30 #include <string.h>
31
32 #include "glutint.h"
33
34 #define DEFAULT_WIDTH 300
35 #define DEFAULT_HEIGHT 300
36 #define DEFAULT_BPP 16
37
38 #define DEPTH_SIZE 16
39 #define STENCIL_SIZE 8
40 #define ACCUM_SIZE 16
41
42
43
44 GLuint g_bpp = DEFAULT_BPP;
45 GLuint g_depth = DEPTH_SIZE;
46 GLuint g_stencil = STENCIL_SIZE;
47 GLuint g_accum = ACCUM_SIZE;
48 GLuint g_refresh = 0;
49 GLuint g_screen_w, g_screen_h;
50 GLint g_driver_caps;
51
52 GLuint g_fps = 0;
53
54 GLuint g_display_mode = 0;
55 int g_init_x = 0, g_init_y = 0;
56 GLuint g_init_w = DEFAULT_WIDTH, g_init_h = DEFAULT_HEIGHT;
57
58 char *__glutProgramName = NULL;
59
60
61
62 void APIENTRY glutInit (int *argc, char **argv)
63 {
64 char *str;
65 const char *env;
66
67 if ((env = getenv("DMESA_GLUT_BPP")) != NULL) {
68 g_bpp = atoi(env);
69 }
70 if ((env = getenv("DMESA_GLUT_DEPTH")) != NULL) {
71 g_depth = atoi(env);
72 }
73 if ((env = getenv("DMESA_GLUT_STENCIL")) != NULL) {
74 g_stencil = atoi(env);
75 }
76 if ((env = getenv("DMESA_GLUT_ACCUM")) != NULL) {
77 g_accum = atoi(env);
78 }
79 if ((env = getenv("DMESA_GLUT_REFRESH")) != NULL) {
80 g_refresh = atoi(env);
81 }
82
83 /* Determine program name. */
84 str = strrchr(argv[0], '/');
85 if (str == NULL) {
86 str = argv[0];
87 } else {
88 str++;
89 }
90 __glutProgramName = __glutStrdup(str);
91
92 /* check if GLUT_FPS env var is set */
93 if ((env = getenv("GLUT_FPS")) != NULL) {
94 if ((g_fps = atoi(env)) <= 0) {
95 g_fps = 5000; /* 5000 milliseconds */
96 }
97 }
98
99 /* Initialize timer */
100 glutGet(GLUT_ELAPSED_TIME);
101 }
102
103
104
105 void APIENTRY glutInitDisplayMode (unsigned int mode)
106 {
107 g_display_mode = mode;
108 }
109
110
111
112 void APIENTRY glutInitWindowPosition (int x, int y)
113 {
114 g_init_x = x;
115 g_init_y = y;
116 }
117
118
119
120 void APIENTRY glutInitWindowSize (int width, int height)
121 {
122 g_init_w = width;
123 g_init_h = height;
124 }
125
126
127
128 void APIENTRY glutMainLoop (void)
129 {
130 int i;
131 GLboolean idle;
132 static int old_mouse_x = 0;
133 static int old_mouse_y = 0;
134 static int old_mouse_b = 0;
135
136 {
137 GLint screen_size[2];
138 DMesaGetIntegerv(DMESA_GET_SCREEN_SIZE, screen_size);
139 g_screen_w = screen_size[0];
140 g_screen_h = screen_size[1];
141 DMesaGetIntegerv(DMESA_GET_DRIVER_CAPS, &g_driver_caps);
142 }
143
144 pc_install_keyb();
145 __glutInitMouse();
146
147 for (i = 0; i < MAX_WINDOWS; i++) {
148 if (g_windows[i] != NULL) {
149 GLUTwindow *w = g_windows[i];
150 glutSetWindow(w->num);
151 glutPostRedisplay();
152 if (w->reshape) {
153 w->reshape(w->width, w->height);
154 }
155 if (w->visibility) {
156 w->visibility(GLUT_VISIBLE);
157 }
158 }
159 }
160
161 while (GL_TRUE) {
162 idle = GL_TRUE;
163
164 for (i = 0; i < MAX_WINDOWS; i++) {
165 if (g_windows[i] != NULL) {
166 GLUTwindow *w = g_windows[i];
167 if (w->redisplay && w->display) {
168 idle = GL_FALSE;
169 w->redisplay = GL_FALSE;
170
171 if (DMesaMakeCurrent(w->context, w->buffer)) {
172 if (w->show_mouse && !(g_display_mode & GLUT_DOUBLE)) {
173 /* XXX scare mouse */
174 w->display();
175 /* XXX unscare mouse */
176 } else {
177 w->display();
178 }
179 }
180 }
181 }
182 }
183 DMesaMakeCurrent(g_curwin->context, g_curwin->buffer);
184
185 if (g_mouse) {
186 int mouse_x;
187 int mouse_y;
188 int mouse_z;
189 int mouse_b;
190
191 /* query mouse */
192 mouse_b = pc_query_mouse(&mouse_x, &mouse_y, &mouse_z);
193
194 /* relative to window coordinates */
195 g_mouse_x = mouse_x - g_curwin->xpos;
196 g_mouse_y = mouse_y - g_curwin->ypos;
197
198 /* mouse was moved? */
199 if ((mouse_x != old_mouse_x) || (mouse_y != old_mouse_y)) {
200 idle = GL_FALSE;
201 old_mouse_x = mouse_x;
202 old_mouse_y = mouse_y;
203
204 if (mouse_b) {
205 /* any button pressed */
206 if (g_curwin->motion) {
207 g_curwin->motion(g_mouse_x, g_mouse_y);
208 }
209 } else {
210 /* no button pressed */
211 if (g_curwin->passive) {
212 g_curwin->passive(g_mouse_x, g_mouse_y);
213 }
214 }
215 }
216
217 /* button state changed? */
218 if (mouse_b != old_mouse_b) {
219 GLUTmouseCB mouse_func;
220
221 if ((mouse_func = g_curwin->mouse)) {
222 if ((old_mouse_b & 1) && !(mouse_b & 1))
223 mouse_func(GLUT_LEFT_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
224 else if (!(old_mouse_b & 1) && (mouse_b & 1))
225 mouse_func(GLUT_LEFT_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
226
227 if ((old_mouse_b & 2) && !(mouse_b & 2))
228 mouse_func(GLUT_RIGHT_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
229 else if (!(old_mouse_b & 2) && (mouse_b & 2))
230 mouse_func(GLUT_RIGHT_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
231
232 if ((old_mouse_b & 4) && !(mouse_b & 4))
233 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
234 else if (!(old_mouse_b & 3) && (mouse_b & 4))
235 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
236 }
237
238 idle = GL_FALSE;
239 old_mouse_b = mouse_b;
240 }
241 }
242
243 if (pc_keypressed()) {
244 int key;
245 int glut_key;
246
247 idle = GL_FALSE;
248 key = pc_readkey();
249
250 switch (key>>16) {
251 case KEY_F1: glut_key = GLUT_KEY_F1; goto special;
252 case KEY_F2: glut_key = GLUT_KEY_F2; goto special;
253 case KEY_F3: glut_key = GLUT_KEY_F3; goto special;
254 case KEY_F4: glut_key = GLUT_KEY_F4; goto special;
255 case KEY_F5: glut_key = GLUT_KEY_F5; goto special;
256 case KEY_F6: glut_key = GLUT_KEY_F6; goto special;
257 case KEY_F7: glut_key = GLUT_KEY_F7; goto special;
258 case KEY_F8: glut_key = GLUT_KEY_F8; goto special;
259 case KEY_F9: glut_key = GLUT_KEY_F9; goto special;
260 case KEY_F10: glut_key = GLUT_KEY_F10; goto special;
261 case KEY_F11: glut_key = GLUT_KEY_F11; goto special;
262 case KEY_F12: glut_key = GLUT_KEY_F12; goto special;
263 case KEY_LEFT: glut_key = GLUT_KEY_LEFT; goto special;
264 case KEY_UP: glut_key = GLUT_KEY_UP; goto special;
265 case KEY_RIGHT: glut_key = GLUT_KEY_RIGHT; goto special;
266 case KEY_DOWN: glut_key = GLUT_KEY_DOWN; goto special;
267 case KEY_PGUP: glut_key = GLUT_KEY_PAGE_UP; goto special;
268 case KEY_PGDN: glut_key = GLUT_KEY_PAGE_DOWN; goto special;
269 case KEY_HOME: glut_key = GLUT_KEY_HOME; goto special;
270 case KEY_END: glut_key = GLUT_KEY_END; goto special;
271 case KEY_INSERT: glut_key = GLUT_KEY_INSERT; goto special;
272 special:
273 if (g_curwin->special) {
274 g_curwin->special(glut_key, g_mouse_x, g_mouse_y);
275 }
276 break;
277 default:
278 if (g_curwin->keyboard) {
279 g_curwin->keyboard(key & 0xFF, g_mouse_x, g_mouse_y);
280 }
281 }
282 }
283
284 if (idle && g_idle_func)
285 g_idle_func();
286 }
287 }