Daniel Borca's GLUT for DOS/DGJPP.
[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 v0.1 for Mesa 4.0
23 *
24 * Copyright (C) 2002 - Borca Daniel
25 * Email : dborca@yahoo.com
26 * Web : http://www.geocities.com/dborca
27 */
28
29
30 #include <signal.h>
31 #include "GL/glut.h"
32 #include "internal.h"
33
34
35 static void *old_sig_int = NULL;
36
37
38 static void signal_handler (int num)
39 {
40 signal(SIGINT, old_sig_int);
41
42 raise(num);
43 }
44
45
46 void APIENTRY glutInit (int *argcp, char **argv)
47 {
48 /* Hack alert:
49 only SIGINT (but not Ctrl-Break)
50 calls the destructors and will safely clean up
51 */
52 old_sig_int = signal(SIGINT, signal_handler);
53 }
54
55
56 void APIENTRY glutInitDisplayMode (unsigned int mode)
57 {
58 g_display_mode = mode;
59
60 pc_install_keyb();
61 g_mouse = pc_install_mouse();
62 }
63
64
65 void APIENTRY glutInitWindowPosition (int x, int y)
66 {
67 g_xpos = x;
68 g_ypos = y;
69 }
70
71
72 void APIENTRY glutInitWindowSize (int width, int height)
73 {
74 g_width = width;
75 g_height = height;
76 }
77
78
79 void APIENTRY glutMainLoop (void)
80 {
81 GLboolean idle;
82 static int old_mouse_x = 0;
83 static int old_mouse_y = 0;
84 static int old_mouse_b = 0;
85
86 glutPostRedisplay();
87 if (reshape_func) reshape_func(g_width, g_height);
88 if (visibility_func) visibility_func(GLUT_VISIBLE);
89 if (g_mouse) pc_show_mouse();
90
91 while (GL_TRUE) {
92 idle = GL_TRUE;
93
94 if (g_redisplay && display_func) {
95 idle = GL_FALSE;
96 g_redisplay = GL_FALSE;
97
98 if (g_mouse && !(g_display_mode & GLUT_DOUBLE)) pc_scare_mouse();
99 display_func();
100 if (g_mouse && !(g_display_mode & GLUT_DOUBLE)) pc_unscare_mouse();
101 }
102
103 if (pc_keypressed()) {
104 int key;
105
106 idle = GL_FALSE;
107 key = pc_readkey();
108
109 switch (key>>16) {
110 case KEY_F1: if (special_func) special_func(GLUT_KEY_F1, 0, 0); break;
111 case KEY_F2: if (special_func) special_func(GLUT_KEY_F2, 0, 0); break;
112 case KEY_F3: if (special_func) special_func(GLUT_KEY_F3, 0, 0); break;
113 case KEY_F4: if (special_func) special_func(GLUT_KEY_F4, 0, 0); break;
114 case KEY_F5: if (special_func) special_func(GLUT_KEY_F5, 0, 0); break;
115 case KEY_F6: if (special_func) special_func(GLUT_KEY_F6, 0, 0); break;
116 case KEY_F7: if (special_func) special_func(GLUT_KEY_F7, 0, 0); break;
117 case KEY_F8: if (special_func) special_func(GLUT_KEY_F8, 0, 0); break;
118 case KEY_F9: if (special_func) special_func(GLUT_KEY_F9, 0, 0); break;
119 case KEY_F10: if (special_func) special_func(GLUT_KEY_F10, 0, 0); break;
120 case KEY_F11: if (special_func) special_func(GLUT_KEY_F11, 0, 0); break;
121 case KEY_F12: if (special_func) special_func(GLUT_KEY_F12, 0, 0); break;
122 case KEY_LEFT: if (special_func) special_func(GLUT_KEY_LEFT, 0, 0); break;
123 case KEY_UP: if (special_func) special_func(GLUT_KEY_UP, 0, 0); break;
124 case KEY_RIGHT: if (special_func) special_func(GLUT_KEY_RIGHT, 0, 0); break;
125 case KEY_DOWN: if (special_func) special_func(GLUT_KEY_DOWN, 0, 0); break;
126 case KEY_PGUP: if (special_func) special_func(GLUT_KEY_PAGE_UP, 0, 0); break;
127 case KEY_PGDN: if (special_func) special_func(GLUT_KEY_PAGE_DOWN, 0, 0); break;
128 case KEY_HOME: if (special_func) special_func(GLUT_KEY_HOME, 0, 0); break;
129 case KEY_END: if (special_func) special_func(GLUT_KEY_END, 0, 0); break;
130 case KEY_INSERT: if (special_func) special_func(GLUT_KEY_INSERT, 0, 0); break;
131 default: if (keyboard_func) keyboard_func(key & 0xFF, 0, 0);
132 }
133 }
134
135 if (g_mouse && motion_func && ((pc_mouse_x != old_mouse_x) || (pc_mouse_y != old_mouse_y))) {
136 idle = GL_FALSE;
137 old_mouse_x = pc_mouse_x;
138 old_mouse_y = pc_mouse_y;
139
140 motion_func(old_mouse_x, old_mouse_y);
141 }
142
143 if (g_mouse && mouse_func && (pc_mouse_b != old_mouse_b)) {
144 int new_mouse_b = pc_mouse_b;
145
146 if ((old_mouse_b & 1) && !(new_mouse_b & 1))
147 mouse_func(GLUT_LEFT_BUTTON, GLUT_UP, pc_mouse_x, pc_mouse_y);
148 else if (!(old_mouse_b & 1) && (new_mouse_b & 1))
149 mouse_func(GLUT_LEFT_BUTTON, GLUT_DOWN, pc_mouse_x, pc_mouse_y);
150
151 if ((old_mouse_b & 2) && !(new_mouse_b & 2))
152 mouse_func(GLUT_RIGHT_BUTTON, GLUT_UP, pc_mouse_x, pc_mouse_y);
153 else if (!(old_mouse_b & 2) && (new_mouse_b & 2))
154 mouse_func(GLUT_RIGHT_BUTTON, GLUT_DOWN, pc_mouse_x, pc_mouse_y);
155
156 if ((old_mouse_b & 4) && !(new_mouse_b & 4))
157 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_UP, pc_mouse_x, pc_mouse_y);
158 else if (!(old_mouse_b & 3) && (new_mouse_b & 4))
159 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_DOWN, pc_mouse_x, pc_mouse_y);
160
161 idle = GL_FALSE;
162 old_mouse_b = new_mouse_b;
163 }
164
165 if (idle && idle_func)
166 idle_func();
167 }
168 }