blob: 797029cc4cb5a6e4a3c917c307ac5986a2cdbea9 [file] [log] [blame]
Brian Paulfac5fd22000-12-02 20:33:05 +00001/* $Id: manywin.c,v 1.2 2000/12/02 20:33: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
167Redraw(struct head *h)
168{
169 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
170 Error(h->DisplayName, "glXMakeCurrent failed");
171 printf("glXMakeCurrent failed in Redraw()\n");
172 return;
173 }
174
175 h->Angle += 1.0;
176
177 glShadeModel(GL_FLAT);
178 glClearColor(0.5, 0.5, 0.5, 1.0);
179 glClear(GL_COLOR_BUFFER_BIT);
180
181 /* draw green triangle */
182 glColor3f(0.0, 1.0, 0.0);
183 glPushMatrix();
184 glRotatef(h->Angle, 0, 0, 1);
185 glBegin(GL_TRIANGLES);
186 glVertex2f(0, 0.8);
187 glVertex2f(-0.8, -0.7);
188 glVertex2f(0.8, -0.7);
189 glEnd();
190 glPopMatrix();
191
Brian Paulfac5fd22000-12-02 20:33:05 +0000192 if (!SwapSeparate)
193 glXSwapBuffers(h->Dpy, h->Win);
Brian Paul0852d0b2000-06-13 19:41:30 +0000194}
195
196
Brian Paulfac5fd22000-12-02 20:33:05 +0000197static void
198Swap(struct head *h)
199{
200 glXSwapBuffers(h->Dpy, h->Win);
201}
202
Brian Paul0852d0b2000-06-13 19:41:30 +0000203
204static void
205Resize(const struct head *h, unsigned int width, unsigned int height)
206{
207 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
208 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
209 return;
210 }
211 glFlush();
212 glViewport(0, 0, width, height);
213 glMatrixMode(GL_PROJECTION);
214 glLoadIdentity();
215 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
216}
217
218
219
220static void
221EventLoop(void)
222{
223 while (1) {
224 int i;
225 for (i = 0; i < NumHeads; i++) {
226 struct head *h = &Heads[i];
227 while (XPending(h->Dpy) > 0) {
228 XEvent event;
229 XNextEvent(h->Dpy, &event);
230 if (event.xany.window == h->Win) {
231 switch (event.type) {
232 case Expose:
233 Redraw(h);
Brian Paulfac5fd22000-12-02 20:33:05 +0000234 if (SwapSeparate)
235 Swap(h);
Brian Paul0852d0b2000-06-13 19:41:30 +0000236 break;
237 case ConfigureNotify:
238 Resize(h, event.xconfigure.width, event.xconfigure.height);
239 break;
240 case KeyPress:
241 return;
242 default:
243 /*no-op*/ ;
244 }
245 }
246 else {
247 printf("window mismatch\n");
248 }
249 }
Brian Paulfac5fd22000-12-02 20:33:05 +0000250 }
251
252 /* redraw all windows */
253 for (i = 0; i < NumHeads; i++) {
254 Redraw(&Heads[i]);
255 }
256 /* swapbuffers on all windows, if not already done */
257 if (SwapSeparate) {
258 for (i = 0; i < NumHeads; i++) {
259 Swap(&Heads[i]);
260 }
Brian Paul0852d0b2000-06-13 19:41:30 +0000261 }
262 usleep(1);
263 }
264}
265
266
267
268static void
269PrintInfo(const struct head *h)
270{
271 printf("Name: %s\n", h->DisplayName);
272 printf(" Display: 0x%x\n", h->Dpy);
273 printf(" Window: 0x%x\n", h->Win);
274 printf(" Context: 0x%x\n", h->Context);
275 printf(" GL_VERSION: %s\n", h->Version);
276 printf(" GL_VENDOR: %s\n", h->Vendor);
277 printf(" GL_RENDERER: %s\n", h->Renderer);
278}
279
280
281int
282main(int argc, char *argv[])
283{
284 int i;
285 if (argc == 1) {
286 struct head *h;
287 printf("manywin: open N simultaneous glx windows\n");
288 printf("Usage:\n");
Brian Paulfac5fd22000-12-02 20:33:05 +0000289 printf(" manywin [-s] numWindows\n");
290 printf("Options:\n");
291 printf(" -s = swap immediately after drawing (see src code)\n");
Brian Paul0852d0b2000-06-13 19:41:30 +0000292 printf("Example:\n");
293 printf(" manywin 10\n");
294 return 0;
295 }
296 else {
Brian Paulfac5fd22000-12-02 20:33:05 +0000297 int n = 3;
298 for (i = 1; i < argc; i++) {
299 if (strcmp(argv[i], "-s") == 0) {
300 SwapSeparate = GL_FALSE;
301 }
302 else {
303 n = atoi(argv[i]);
304 }
305 }
306 if (n < 1)
307 n = 1;
308
Brian Paul0852d0b2000-06-13 19:41:30 +0000309 printf("%d windows\n", n);
310 for (i = 0; i < n; i++) {
311 char name[100];
312 struct head *h;
313 sprintf(name, "%d", i);
314 h = AddHead(":0", name);
315 if (h) {
316 PrintInfo(h);
317 }
318 }
319 }
320
321 EventLoop();
322 return 0;
323}