blob: f8ef80d426aefd199162c8c8f47dd995e2408727 [file] [log] [blame]
Brian Paul125fddc2001-01-23 23:45:05 +00001/* $Id: manywin.c,v 1.3 2001/01/23 23:45:05 brianp Exp $ */
Brian Paul0852d0b2000-06-13 19:41:30 +00002
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 <unistd.h>
33
34
35/*
36 * Each display/window/context:
37 */
38struct head {
39 char DisplayName[1000];
40 Display *Dpy;
41 Window Win;
42 GLXContext Context;
43 float Angle;
44 char Renderer[1000];
45 char Vendor[1000];
46 char Version[1000];
47};
48
49
50#define MAX_HEADS 200
51static struct head Heads[MAX_HEADS];
52static int NumHeads = 0;
Brian Paulfac5fd22000-12-02 20:33:05 +000053static GLboolean SwapSeparate = GL_TRUE;
54
Brian Paul0852d0b2000-06-13 19:41:30 +000055
56
57static void
58Error(const char *display, const char *msg)
59{
60 fprintf(stderr, "Error on display %s - %s\n", display, msg);
61 exit(1);
62}
63
64
65static struct head *
66AddHead(const char *displayName, const char *name)
67{
68 Display *dpy;
69 Window win;
70 GLXContext ctx;
71 int attrib[] = { GLX_RGBA,
72 GLX_RED_SIZE, 1,
73 GLX_GREEN_SIZE, 1,
74 GLX_BLUE_SIZE, 1,
75 GLX_DOUBLEBUFFER,
76 None };
77 int scrnum;
78 XSetWindowAttributes attr;
79 unsigned long mask;
80 Window root;
81 XVisualInfo *visinfo;
82 int width = 90, height = 90;
83 int xpos = 0, ypos = 0;
84
85 if (NumHeads >= MAX_HEADS)
86 return NULL;
87
88 dpy = XOpenDisplay(displayName);
89 if (!dpy) {
90 Error(displayName, "Unable to open display");
91 return NULL;
92 }
93
94 scrnum = DefaultScreen(dpy);
95 root = RootWindow(dpy, scrnum);
96
97 visinfo = glXChooseVisual(dpy, scrnum, attrib);
98 if (!visinfo) {
99 Error(displayName, "Unable to find RGB, double-buffered visual");
100 return NULL;
101 }
102
103 /* window attributes */
104 xpos = (NumHeads % 10) * 100;
105 ypos = (NumHeads / 10) * 100;
106 printf("%d, %d\n", xpos, ypos);
107 attr.background_pixel = 0;
108 attr.border_pixel = 0;
109 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
110 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
111 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
112
113 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
114 0, visinfo->depth, InputOutput,
115 visinfo->visual, mask, &attr);
116 if (!win) {
117 Error(displayName, "Couldn't create window");
118 return NULL;
119 }
120
121 {
122 XSizeHints sizehints;
123 sizehints.x = xpos;
124 sizehints.y = ypos;
125 sizehints.width = width;
126 sizehints.height = height;
127 sizehints.flags = USSize | USPosition;
128 XSetNormalHints(dpy, win, &sizehints);
129 XSetStandardProperties(dpy, win, name, name,
130 None, (char **)NULL, 0, &sizehints);
131 }
132
133
134 ctx = glXCreateContext(dpy, visinfo, NULL, True);
135 if (!ctx) {
136 Error(displayName, "Couldn't create GLX context");
137 return NULL;
138 }
139
140 XMapWindow(dpy, win);
141
142 if (!glXMakeCurrent(dpy, win, ctx)) {
143 Error(displayName, "glXMakeCurrent failed");
144 printf("glXMakeCurrent failed in Redraw()\n");
145 return;
146 }
147
148 /* save the info for this head */
149 {
150 struct head *h = &Heads[NumHeads];
151 strcpy(h->DisplayName, name);
152 h->Dpy = dpy;
153 h->Win = win;
154 h->Context = ctx;
155 h->Angle = 0.0;
156 strcpy(h->Version, (char *) glGetString(GL_VERSION));
157 strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
158 strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
159 NumHeads++;
160 return &Heads[NumHeads-1];
161 }
162
163}
164
165
166static void
Brian Paul125fddc2001-01-23 23:45:05 +0000167DestroyHeads(void)
168{
169 int i;
170 for (i = 0; i < NumHeads; i++) {
171 XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
172 glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
173 XCloseDisplay(Heads[i].Dpy);
174 }
175}
176
177
178static void
Brian Paul0852d0b2000-06-13 19:41:30 +0000179Redraw(struct head *h)
180{
181 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
182 Error(h->DisplayName, "glXMakeCurrent failed");
183 printf("glXMakeCurrent failed in Redraw()\n");
184 return;
185 }
186
187 h->Angle += 1.0;
188
189 glShadeModel(GL_FLAT);
190 glClearColor(0.5, 0.5, 0.5, 1.0);
191 glClear(GL_COLOR_BUFFER_BIT);
192
193 /* draw green triangle */
194 glColor3f(0.0, 1.0, 0.0);
195 glPushMatrix();
196 glRotatef(h->Angle, 0, 0, 1);
197 glBegin(GL_TRIANGLES);
198 glVertex2f(0, 0.8);
199 glVertex2f(-0.8, -0.7);
200 glVertex2f(0.8, -0.7);
201 glEnd();
202 glPopMatrix();
203
Brian Paulfac5fd22000-12-02 20:33:05 +0000204 if (!SwapSeparate)
205 glXSwapBuffers(h->Dpy, h->Win);
Brian Paul0852d0b2000-06-13 19:41:30 +0000206}
207
208
Brian Paulfac5fd22000-12-02 20:33:05 +0000209static void
210Swap(struct head *h)
211{
212 glXSwapBuffers(h->Dpy, h->Win);
213}
214
Brian Paul0852d0b2000-06-13 19:41:30 +0000215
216static void
217Resize(const struct head *h, unsigned int width, unsigned int height)
218{
219 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
220 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
221 return;
222 }
223 glFlush();
224 glViewport(0, 0, width, height);
225 glMatrixMode(GL_PROJECTION);
226 glLoadIdentity();
227 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
228}
229
230
231
232static void
233EventLoop(void)
234{
235 while (1) {
236 int i;
237 for (i = 0; i < NumHeads; i++) {
238 struct head *h = &Heads[i];
239 while (XPending(h->Dpy) > 0) {
240 XEvent event;
241 XNextEvent(h->Dpy, &event);
242 if (event.xany.window == h->Win) {
243 switch (event.type) {
244 case Expose:
245 Redraw(h);
Brian Paulfac5fd22000-12-02 20:33:05 +0000246 if (SwapSeparate)
247 Swap(h);
Brian Paul0852d0b2000-06-13 19:41:30 +0000248 break;
249 case ConfigureNotify:
250 Resize(h, event.xconfigure.width, event.xconfigure.height);
251 break;
252 case KeyPress:
253 return;
254 default:
255 /*no-op*/ ;
256 }
257 }
258 else {
259 printf("window mismatch\n");
260 }
261 }
Brian Paulfac5fd22000-12-02 20:33:05 +0000262 }
263
264 /* redraw all windows */
265 for (i = 0; i < NumHeads; i++) {
266 Redraw(&Heads[i]);
267 }
268 /* swapbuffers on all windows, if not already done */
269 if (SwapSeparate) {
270 for (i = 0; i < NumHeads; i++) {
271 Swap(&Heads[i]);
272 }
Brian Paul0852d0b2000-06-13 19:41:30 +0000273 }
274 usleep(1);
275 }
276}
277
278
279
280static void
281PrintInfo(const struct head *h)
282{
283 printf("Name: %s\n", h->DisplayName);
284 printf(" Display: 0x%x\n", h->Dpy);
285 printf(" Window: 0x%x\n", h->Win);
286 printf(" Context: 0x%x\n", h->Context);
287 printf(" GL_VERSION: %s\n", h->Version);
288 printf(" GL_VENDOR: %s\n", h->Vendor);
289 printf(" GL_RENDERER: %s\n", h->Renderer);
290}
291
292
293int
294main(int argc, char *argv[])
295{
296 int i;
297 if (argc == 1) {
298 struct head *h;
299 printf("manywin: open N simultaneous glx windows\n");
300 printf("Usage:\n");
Brian Paulfac5fd22000-12-02 20:33:05 +0000301 printf(" manywin [-s] numWindows\n");
302 printf("Options:\n");
303 printf(" -s = swap immediately after drawing (see src code)\n");
Brian Paul0852d0b2000-06-13 19:41:30 +0000304 printf("Example:\n");
305 printf(" manywin 10\n");
306 return 0;
307 }
308 else {
Brian Paulfac5fd22000-12-02 20:33:05 +0000309 int n = 3;
310 for (i = 1; i < argc; i++) {
311 if (strcmp(argv[i], "-s") == 0) {
312 SwapSeparate = GL_FALSE;
313 }
314 else {
315 n = atoi(argv[i]);
316 }
317 }
318 if (n < 1)
319 n = 1;
320
Brian Paul0852d0b2000-06-13 19:41:30 +0000321 printf("%d windows\n", n);
322 for (i = 0; i < n; i++) {
323 char name[100];
324 struct head *h;
325 sprintf(name, "%d", i);
Brian Paul125fddc2001-01-23 23:45:05 +0000326 h = AddHead(NULL, name);
Brian Paul0852d0b2000-06-13 19:41:30 +0000327 if (h) {
328 PrintInfo(h);
329 }
330 }
331 }
332
333 EventLoop();
Brian Paul125fddc2001-01-23 23:45:05 +0000334 DestroyHeads();
Brian Paul0852d0b2000-06-13 19:41:30 +0000335 return 0;
336}