patch to import Jon Smirl's work from Bitkeeper
[mesa.git] / src / glut / mini / window.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.1
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.2 for Mesa 4.1
23 *
24 * Copyright (C) 2002 - Borca Daniel
25 * Email : dborca@yahoo.com
26 * Web : http://www.geocities.com/dborca
27 */
28
29
30 #include <stdio.h>
31 #include <GL/gl.h>
32 #include "GL/glut.h"
33 #include "internal.h"
34
35 #define USE_MINI_GLX 1
36 #if USE_MINI_GLX
37 #include "GL/miniglx.h"
38 #else
39 #include <GL/glx.h>
40 #endif
41
42
43
44 static GLXContext context = 0;
45 static Window win;
46 static XVisualInfo *visinfo = 0;
47 static Display *dpy = 0;
48
49
50 int APIENTRY glutCreateWindow (const char *title)
51 {
52 XSetWindowAttributes attr;
53 unsigned long mask;
54 GLXContext ctx;
55 int scrnum = 0;
56 Window root = RootWindow( dpy, scrnum );
57
58 if (win)
59 return 0;
60
61 if (!dpy) {
62 dpy = XOpenDisplay(NULL);
63 if (!dpy) {
64 printf("Error: XOpenDisplay failed\n");
65 exit(1);
66 }
67 }
68
69 if (!visinfo) {
70 int attrib[] = {GLX_RGBA,
71 GLX_RED_SIZE, 1,
72 GLX_GREEN_SIZE, 1,
73 GLX_BLUE_SIZE, 1,
74 GLX_DEPTH_SIZE, 1,
75 GLX_DOUBLEBUFFER,
76 None };
77
78
79 visinfo = glXChooseVisual( dpy, scrnum, attrib );
80 if (!visinfo) {
81 printf("Error: couldn't get an RGB, Double-buffered visual\n");
82 exit(1);
83 }
84 }
85
86 /* window attributes */
87 attr.background_pixel = 0;
88 attr.border_pixel = 0;
89 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
90 attr.event_mask = StructureNotifyMask | ExposureMask;
91 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
92
93 win = XCreateWindow( dpy, root, 0, 0, g_width, g_height,
94 0, visinfo->depth, InputOutput,
95 visinfo->visual, mask, &attr );
96 if (!win) {
97 printf("Error: XCreateWindow failed\n");
98 exit(1);
99 }
100
101 ctx = glXCreateContext( dpy, visinfo, NULL, True );
102 if (!ctx) {
103 printf("Error: glXCreateContext failed\n");
104 exit(1);
105 }
106
107 if (!glXMakeCurrent( dpy, win, ctx )) {
108 printf("Error: glXMakeCurrent failed\n");
109 exit(1);
110 }
111
112 if (!(g_display_mode & GLUT_DOUBLE))
113 glDrawBuffer( GL_FRONT );
114
115
116 XMapWindow( dpy, win );
117
118 #if !USE_MINI_GLX
119 {
120 XEvent e;
121 while (1) {
122 XNextEvent( dpy, &e );
123 if (e.type == MapNotify && e.xmap.window == win) {
124 break;
125 }
126 }
127 }
128 #endif
129
130 return 1;
131 }
132
133
134 int APIENTRY glutCreateSubWindow (int win, int x, int y, int width, int height)
135 {
136 return GL_FALSE;
137 }
138
139
140 void APIENTRY glutDestroyWindow (int idx)
141 {
142 if (dpy && win)
143 XDestroyWindow( dpy, win );
144
145 if (dpy)
146 XCloseDisplay( dpy );
147
148 win = 0;
149 dpy = 0;
150 }
151
152
153 void APIENTRY glutPostRedisplay (void)
154 {
155 g_redisplay = GL_TRUE;
156 }
157
158
159 void APIENTRY glutSwapBuffers (void)
160 {
161 /* if (g_mouse) pc_scare_mouse(); */
162 if (dpy && win) glXSwapBuffers( dpy, win );
163 /* if (g_mouse) pc_unscare_mouse(); */
164 }
165
166
167 int APIENTRY glutGetWindow (void)
168 {
169 return 0;
170 }
171
172
173 void APIENTRY glutSetWindow (int win)
174 {
175 }
176
177
178 void APIENTRY glutSetWindowTitle (const char *title)
179 {
180 }
181
182
183 void APIENTRY glutSetIconTitle (const char *title)
184 {
185 }
186
187
188 void APIENTRY glutPositionWindow (int x, int y)
189 {
190 }
191
192
193 void APIENTRY glutReshapeWindow (int width, int height)
194 {
195 }
196
197
198 void APIENTRY glutPopWindow (void)
199 {
200 }
201
202
203 void APIENTRY glutPushWindow (void)
204 {
205 }
206
207
208 void APIENTRY glutIconifyWindow (void)
209 {
210 }
211
212
213 void APIENTRY glutShowWindow (void)
214 {
215 }
216
217
218 void APIENTRY glutHideWindow (void)
219 {
220 }
221
222 void APIENTRY glutMainLoop (void)
223 {
224 GLboolean idle;
225 GLboolean have_event;
226 XEvent evt;
227 int visible = 0;
228
229 glutPostRedisplay();
230 if (reshape_func) reshape_func(g_width, g_height);
231
232 while (GL_TRUE) {
233 idle = GL_TRUE;
234
235
236 if (visible && idle_func)
237 have_event = XCheckMaskEvent( dpy, ~0, &evt );
238 else
239 have_event = XNextEvent( dpy, &evt );
240
241 if (have_event) {
242 idle = GL_FALSE;
243 switch(evt.type) {
244 case MapNotify:
245 if (visibility_func) {
246 visibility_func(GLUT_VISIBLE);
247 }
248 visible = 1;
249 break;
250 case UnmapNotify:
251 if (visibility_func) {
252 visibility_func(GLUT_NOT_VISIBLE);
253 }
254 visible = 0;
255 break;
256 case Expose:
257 g_redisplay = 1;
258 break;
259 }
260 }
261
262 if (visible && g_redisplay && display_func) {
263 idle = GL_FALSE;
264 g_redisplay = GL_FALSE;
265
266 display_func();
267 }
268
269 if (visible && idle && idle_func) {
270 idle_func();
271 }
272 }
273 }