blob: 29462257fb844e3c9894559406c40a34e5cac50f [file] [log] [blame]
Brian Paul0852d0b2000-06-13 19:41:30 +00001/* $Id: manywin.c,v 1.1 2000/06/13 19:41:30 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 <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;
53
54
55static void
56Error(const char *display, const char *msg)
57{
58 fprintf(stderr, "Error on display %s - %s\n", display, msg);
59 exit(1);
60}
61
62
63static struct head *
64AddHead(const char *displayName, const char *name)
65{
66 Display *dpy;
67 Window win;
68 GLXContext ctx;
69 int attrib[] = { GLX_RGBA,
70 GLX_RED_SIZE, 1,
71 GLX_GREEN_SIZE, 1,
72 GLX_BLUE_SIZE, 1,
73 GLX_DOUBLEBUFFER,
74 None };
75 int scrnum;
76 XSetWindowAttributes attr;
77 unsigned long mask;
78 Window root;
79 XVisualInfo *visinfo;
80 int width = 90, height = 90;
81 int xpos = 0, ypos = 0;
82
83 if (NumHeads >= MAX_HEADS)
84 return NULL;
85
86 dpy = XOpenDisplay(displayName);
87 if (!dpy) {
88 Error(displayName, "Unable to open display");
89 return NULL;
90 }
91
92 scrnum = DefaultScreen(dpy);
93 root = RootWindow(dpy, scrnum);
94
95 visinfo = glXChooseVisual(dpy, scrnum, attrib);
96 if (!visinfo) {
97 Error(displayName, "Unable to find RGB, double-buffered visual");
98 return NULL;
99 }
100
101 /* window attributes */
102 xpos = (NumHeads % 10) * 100;
103 ypos = (NumHeads / 10) * 100;
104 printf("%d, %d\n", xpos, ypos);
105 attr.background_pixel = 0;
106 attr.border_pixel = 0;
107 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
108 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
109 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
110
111 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
112 0, visinfo->depth, InputOutput,
113 visinfo->visual, mask, &attr);
114 if (!win) {
115 Error(displayName, "Couldn't create window");
116 return NULL;
117 }
118
119 {
120 XSizeHints sizehints;
121 sizehints.x = xpos;
122 sizehints.y = ypos;
123 sizehints.width = width;
124 sizehints.height = height;
125 sizehints.flags = USSize | USPosition;
126 XSetNormalHints(dpy, win, &sizehints);
127 XSetStandardProperties(dpy, win, name, name,
128 None, (char **)NULL, 0, &sizehints);
129 }
130
131
132 ctx = glXCreateContext(dpy, visinfo, NULL, True);
133 if (!ctx) {
134 Error(displayName, "Couldn't create GLX context");
135 return NULL;
136 }
137
138 XMapWindow(dpy, win);
139
140 if (!glXMakeCurrent(dpy, win, ctx)) {
141 Error(displayName, "glXMakeCurrent failed");
142 printf("glXMakeCurrent failed in Redraw()\n");
143 return;
144 }
145
146 /* save the info for this head */
147 {
148 struct head *h = &Heads[NumHeads];
149 strcpy(h->DisplayName, name);
150 h->Dpy = dpy;
151 h->Win = win;
152 h->Context = ctx;
153 h->Angle = 0.0;
154 strcpy(h->Version, (char *) glGetString(GL_VERSION));
155 strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
156 strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
157 NumHeads++;
158 return &Heads[NumHeads-1];
159 }
160
161}
162
163
164static void
165Redraw(struct head *h)
166{
167 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
168 Error(h->DisplayName, "glXMakeCurrent failed");
169 printf("glXMakeCurrent failed in Redraw()\n");
170 return;
171 }
172
173 h->Angle += 1.0;
174
175 glShadeModel(GL_FLAT);
176 glClearColor(0.5, 0.5, 0.5, 1.0);
177 glClear(GL_COLOR_BUFFER_BIT);
178
179 /* draw green triangle */
180 glColor3f(0.0, 1.0, 0.0);
181 glPushMatrix();
182 glRotatef(h->Angle, 0, 0, 1);
183 glBegin(GL_TRIANGLES);
184 glVertex2f(0, 0.8);
185 glVertex2f(-0.8, -0.7);
186 glVertex2f(0.8, -0.7);
187 glEnd();
188 glPopMatrix();
189
190 glXSwapBuffers(h->Dpy, h->Win);
191}
192
193
194
195static void
196Resize(const struct head *h, unsigned int width, unsigned int height)
197{
198 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
199 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
200 return;
201 }
202 glFlush();
203 glViewport(0, 0, width, height);
204 glMatrixMode(GL_PROJECTION);
205 glLoadIdentity();
206 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
207}
208
209
210
211static void
212EventLoop(void)
213{
214 while (1) {
215 int i;
216 for (i = 0; i < NumHeads; i++) {
217 struct head *h = &Heads[i];
218 while (XPending(h->Dpy) > 0) {
219 XEvent event;
220 XNextEvent(h->Dpy, &event);
221 if (event.xany.window == h->Win) {
222 switch (event.type) {
223 case Expose:
224 Redraw(h);
225 break;
226 case ConfigureNotify:
227 Resize(h, event.xconfigure.width, event.xconfigure.height);
228 break;
229 case KeyPress:
230 return;
231 default:
232 /*no-op*/ ;
233 }
234 }
235 else {
236 printf("window mismatch\n");
237 }
238 }
239 Redraw(h);
240 }
241 usleep(1);
242 }
243}
244
245
246
247static void
248PrintInfo(const struct head *h)
249{
250 printf("Name: %s\n", h->DisplayName);
251 printf(" Display: 0x%x\n", h->Dpy);
252 printf(" Window: 0x%x\n", h->Win);
253 printf(" Context: 0x%x\n", h->Context);
254 printf(" GL_VERSION: %s\n", h->Version);
255 printf(" GL_VENDOR: %s\n", h->Vendor);
256 printf(" GL_RENDERER: %s\n", h->Renderer);
257}
258
259
260int
261main(int argc, char *argv[])
262{
263 int i;
264 if (argc == 1) {
265 struct head *h;
266 printf("manywin: open N simultaneous glx windows\n");
267 printf("Usage:\n");
268 printf(" manywin numWindows\n");
269 printf("Example:\n");
270 printf(" manywin 10\n");
271 return 0;
272 }
273 else {
274 int n = atoi(argv[1]);
275 printf("%d windows\n", n);
276 for (i = 0; i < n; i++) {
277 char name[100];
278 struct head *h;
279 sprintf(name, "%d", i);
280 h = AddHead(":0", name);
281 if (h) {
282 PrintInfo(h);
283 }
284 }
285 }
286
287 EventLoop();
288 return 0;
289}