silence warnings
[mesa.git] / progs / xdemos / glxheads.c
1 /* $Id: glxheads.c,v 1.4 2003/04/21 14:51:16 brianp Exp $ */
2
3 /*
4 * Exercise multiple GLX connections on multiple X displays.
5 * Direct GLX contexts are attempted first, then indirect.
6 * Each window will display a spinning green triangle.
7 *
8 * Copyright (C) 2000 Brian Paul All Rights Reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28
29 #include <GL/gl.h>
30 #include <GL/glx.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36
37
38 /*
39 * Each display/window/context:
40 */
41 struct head {
42 char DisplayName[1000];
43 Display *Dpy;
44 Window Win;
45 GLXContext Context;
46 float Angle;
47 char Renderer[1000];
48 char Vendor[1000];
49 char Version[1000];
50 };
51
52
53 #define MAX_HEADS 20
54 static struct head Heads[MAX_HEADS];
55 static int NumHeads = 0;
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)
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 = 300, height = 300;
84 int xpos = 10, ypos = 10;
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 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, 0, 0, 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, displayName, displayName,
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 NULL;
144 }
145
146 /* save the info for this head */
147 {
148 struct head *h = &Heads[NumHeads];
149 strcpy(h->DisplayName, displayName);
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
164 static void
165 Redraw(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
195 static void
196 Resize(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
211 static void
212 EventLoop(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
247 static void
248 PrintInfo(const struct head *h)
249 {
250 printf("Name: %s\n", h->DisplayName);
251 printf(" Display: %p\n", h->Dpy);
252 printf(" Window: 0x%x\n", (int) h->Win);
253 printf(" Context: 0x%lx\n", (long) 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
260 int
261 main(int argc, char *argv[])
262 {
263 int i;
264 if (argc == 1) {
265 struct head *h;
266 printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
267 printf("Usage:\n");
268 printf(" glxheads xdisplayname ...\n");
269 printf("Example:\n");
270 printf(" glxheads :0 mars:0 venus:1\n");
271 h = AddHead(":0");
272 if (h)
273 PrintInfo(h);
274 }
275 else {
276 for (i = 1; i < argc; i++) {
277 const char *name = argv[i];
278 struct head *h = AddHead(name);
279 if (h) {
280 PrintInfo(h);
281 }
282 }
283 }
284
285 EventLoop();
286 return 0;
287 }