2a28bc326b28a926e3ab4eba9fb49a81881dea62
[mesa.git] / progs / xdemos / manywin.c
1 /* $Id: manywin.c,v 1.5 2001/11/26 17:21:46 brianp Exp $ */
2
3 /*
4 * Create N GLX windows/contexts and render to them in round-robin
5 * order.
6 *
7 * Copyright (C) 2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #include <GL/gl.h>
29 #include <GL/glx.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35
36 /*
37 * Each display/window/context:
38 */
39 struct head {
40 char DisplayName[1000];
41 Display *Dpy;
42 Window Win;
43 GLXContext Context;
44 float Angle;
45 char Renderer[1000];
46 char Vendor[1000];
47 char Version[1000];
48 };
49
50
51 #define MAX_HEADS 200
52 static struct head Heads[MAX_HEADS];
53 static int NumHeads = 0;
54 static GLboolean SwapSeparate = GL_TRUE;
55
56
57
58 static void
59 Error(const char *display, const char *msg)
60 {
61 fprintf(stderr, "Error on display %s - %s\n", display, msg);
62 exit(1);
63 }
64
65
66 static struct head *
67 AddHead(const char *displayName, const char *name)
68 {
69 Display *dpy;
70 Window win;
71 GLXContext ctx;
72 int attrib[] = { GLX_RGBA,
73 GLX_RED_SIZE, 1,
74 GLX_GREEN_SIZE, 1,
75 GLX_BLUE_SIZE, 1,
76 GLX_DOUBLEBUFFER,
77 None };
78 int scrnum;
79 XSetWindowAttributes attr;
80 unsigned long mask;
81 Window root;
82 XVisualInfo *visinfo;
83 int width = 90, height = 90;
84 int xpos = 0, ypos = 0;
85
86 if (NumHeads >= MAX_HEADS)
87 return NULL;
88
89 dpy = XOpenDisplay(displayName);
90 if (!dpy) {
91 Error(displayName, "Unable to open display");
92 return NULL;
93 }
94
95 scrnum = DefaultScreen(dpy);
96 root = RootWindow(dpy, scrnum);
97
98 visinfo = glXChooseVisual(dpy, scrnum, attrib);
99 if (!visinfo) {
100 Error(displayName, "Unable to find RGB, double-buffered visual");
101 return NULL;
102 }
103
104 /* window attributes */
105 xpos = (NumHeads % 10) * 100;
106 ypos = (NumHeads / 10) * 100;
107 printf("%d, %d\n", xpos, ypos);
108 attr.background_pixel = 0;
109 attr.border_pixel = 0;
110 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
111 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
112 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
113
114 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
115 0, visinfo->depth, InputOutput,
116 visinfo->visual, mask, &attr);
117 if (!win) {
118 Error(displayName, "Couldn't create window");
119 return NULL;
120 }
121
122 {
123 XSizeHints sizehints;
124 sizehints.x = xpos;
125 sizehints.y = ypos;
126 sizehints.width = width;
127 sizehints.height = height;
128 sizehints.flags = USSize | USPosition;
129 XSetNormalHints(dpy, win, &sizehints);
130 XSetStandardProperties(dpy, win, name, name,
131 None, (char **)NULL, 0, &sizehints);
132 }
133
134
135 ctx = glXCreateContext(dpy, visinfo, NULL, True);
136 if (!ctx) {
137 Error(displayName, "Couldn't create GLX context");
138 return NULL;
139 }
140
141 XMapWindow(dpy, win);
142
143 if (!glXMakeCurrent(dpy, win, ctx)) {
144 Error(displayName, "glXMakeCurrent failed");
145 printf("glXMakeCurrent failed in Redraw()\n");
146 return NULL;
147 }
148
149 /* save the info for this head */
150 {
151 struct head *h = &Heads[NumHeads];
152 strcpy(h->DisplayName, name);
153 h->Dpy = dpy;
154 h->Win = win;
155 h->Context = ctx;
156 h->Angle = 0.0;
157 strcpy(h->Version, (char *) glGetString(GL_VERSION));
158 strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
159 strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
160 NumHeads++;
161 return &Heads[NumHeads-1];
162 }
163
164 }
165
166
167 static void
168 DestroyHeads(void)
169 {
170 int i;
171 for (i = 0; i < NumHeads; i++) {
172 XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
173 glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
174 XCloseDisplay(Heads[i].Dpy);
175 }
176 }
177
178
179 static void
180 Redraw(struct head *h)
181 {
182 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
183 Error(h->DisplayName, "glXMakeCurrent failed");
184 printf("glXMakeCurrent failed in Redraw()\n");
185 return;
186 }
187
188 h->Angle += 1.0;
189
190 glShadeModel(GL_FLAT);
191 glClearColor(0.5, 0.5, 0.5, 1.0);
192 glClear(GL_COLOR_BUFFER_BIT);
193
194 /* draw green triangle */
195 glColor3f(0.0, 1.0, 0.0);
196 glPushMatrix();
197 glRotatef(h->Angle, 0, 0, 1);
198 glBegin(GL_TRIANGLES);
199 glVertex2f(0, 0.8);
200 glVertex2f(-0.8, -0.7);
201 glVertex2f(0.8, -0.7);
202 glEnd();
203 glPopMatrix();
204
205 if (!SwapSeparate)
206 glXSwapBuffers(h->Dpy, h->Win);
207 }
208
209
210 static void
211 Swap(struct head *h)
212 {
213 glXSwapBuffers(h->Dpy, h->Win);
214 }
215
216
217 static void
218 Resize(const struct head *h, unsigned int width, unsigned int height)
219 {
220 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
221 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
222 return;
223 }
224 glFlush();
225 glViewport(0, 0, width, height);
226 glMatrixMode(GL_PROJECTION);
227 glLoadIdentity();
228 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
229 }
230
231
232
233 static void
234 EventLoop(void)
235 {
236 while (1) {
237 int i;
238 for (i = 0; i < NumHeads; i++) {
239 struct head *h = &Heads[i];
240 while (XPending(h->Dpy) > 0) {
241 XEvent event;
242 XNextEvent(h->Dpy, &event);
243 if (event.xany.window == h->Win) {
244 switch (event.type) {
245 case Expose:
246 Redraw(h);
247 if (SwapSeparate)
248 Swap(h);
249 break;
250 case ConfigureNotify:
251 Resize(h, event.xconfigure.width, event.xconfigure.height);
252 break;
253 case KeyPress:
254 return;
255 default:
256 /*no-op*/ ;
257 }
258 }
259 else {
260 printf("window mismatch\n");
261 }
262 }
263 }
264
265 /* redraw all windows */
266 for (i = 0; i < NumHeads; i++) {
267 Redraw(&Heads[i]);
268 }
269 /* swapbuffers on all windows, if not already done */
270 if (SwapSeparate) {
271 for (i = 0; i < NumHeads; i++) {
272 Swap(&Heads[i]);
273 }
274 }
275 usleep(1);
276 }
277 }
278
279
280
281 static void
282 PrintInfo(const struct head *h)
283 {
284 printf("Name: %s\n", h->DisplayName);
285 printf(" Display: %p\n", (void *) h->Dpy);
286 printf(" Window: 0x%x\n", (int) h->Win);
287 printf(" Context: 0x%x\n", (int) h->Context);
288 printf(" GL_VERSION: %s\n", h->Version);
289 printf(" GL_VENDOR: %s\n", h->Vendor);
290 printf(" GL_RENDERER: %s\n", h->Renderer);
291 }
292
293
294 int
295 main(int argc, char *argv[])
296 {
297 char *dpyName = NULL;
298 int i;
299
300 if (argc == 1) {
301 printf("manywin: open N simultaneous glx windows\n");
302 printf("Usage:\n");
303 printf(" manywin [-s] numWindows\n");
304 printf("Options:\n");
305 printf(" -s = swap immediately after drawing (see src code)\n");
306 printf("Example:\n");
307 printf(" manywin 10\n");
308 return 0;
309 }
310 else {
311 int n = 3;
312 for (i = 1; i < argc; i++) {
313 if (strcmp(argv[i], "-s") == 0) {
314 SwapSeparate = GL_FALSE;
315 }
316 else if (strcmp(argv[i], "-display") == 0 && i < argc) {
317 dpyName = argv[i+1];
318 i++;
319 }
320 else {
321 n = atoi(argv[i]);
322 }
323 }
324 if (n < 1)
325 n = 1;
326
327 printf("%d windows\n", n);
328 for (i = 0; i < n; i++) {
329 char name[100];
330 struct head *h;
331 sprintf(name, "%d", i);
332 h = AddHead(dpyName, name);
333 if (h) {
334 PrintInfo(h);
335 }
336 }
337 }
338
339 EventLoop();
340 DestroyHeads();
341 return 0;
342 }