mesa: added new linux-gallium and linux-gallium-debug configs
[mesa.git] / progs / xdemos / glxheads.c
1
2 /*
3 * Exercise multiple GLX connections on multiple X displays.
4 * Direct GLX contexts are attempted first, then indirect.
5 * Each window will display a spinning green triangle.
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 /*
38 * Each display/window/context:
39 */
40 struct head {
41 char DisplayName[1000];
42 Display *Dpy;
43 Window Win;
44 GLXContext Context;
45 float Angle;
46 char Renderer[1000];
47 char Vendor[1000];
48 char Version[1000];
49 };
50
51
52 #define MAX_HEADS 20
53 static struct head Heads[MAX_HEADS];
54 static int NumHeads = 0;
55
56
57 static void
58 Error(const char *display, const char *msg)
59 {
60 fprintf(stderr, "Error on display %s - %s\n", XDisplayName(display), msg);
61 exit(1);
62 }
63
64
65 static struct head *
66 AddHead(const char *displayName)
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 = 300, height = 300;
83 int xpos = 10, ypos = 10;
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 attr.background_pixel = 0;
105 attr.border_pixel = 0;
106 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
107 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
108 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
109
110 win = XCreateWindow(dpy, root, 0, 0, width, height,
111 0, visinfo->depth, InputOutput,
112 visinfo->visual, mask, &attr);
113 if (!win) {
114 Error(displayName, "Couldn't create window");
115 return NULL;
116 }
117
118 {
119 XSizeHints sizehints;
120 sizehints.x = xpos;
121 sizehints.y = ypos;
122 sizehints.width = width;
123 sizehints.height = height;
124 sizehints.flags = USSize | USPosition;
125 XSetNormalHints(dpy, win, &sizehints);
126 XSetStandardProperties(dpy, win, displayName, displayName,
127 None, (char **)NULL, 0, &sizehints);
128 }
129
130
131 ctx = glXCreateContext(dpy, visinfo, NULL, True);
132 if (!ctx) {
133 Error(displayName, "Couldn't create GLX context");
134 return NULL;
135 }
136
137 XMapWindow(dpy, win);
138
139 if (!glXMakeCurrent(dpy, win, ctx)) {
140 Error(displayName, "glXMakeCurrent failed");
141 printf("glXMakeCurrent failed in Redraw()\n");
142 return NULL;
143 }
144
145 /* save the info for this head */
146 {
147 struct head *h = &Heads[NumHeads];
148 strcpy(h->DisplayName, displayName);
149 h->Dpy = dpy;
150 h->Win = win;
151 h->Context = ctx;
152 h->Angle = 0.0;
153 strcpy(h->Version, (char *) glGetString(GL_VERSION));
154 strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
155 strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
156 NumHeads++;
157 return &Heads[NumHeads-1];
158 }
159
160 }
161
162
163 static void
164 Redraw(struct head *h)
165 {
166 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
167 Error(h->DisplayName, "glXMakeCurrent failed");
168 printf("glXMakeCurrent failed in Redraw()\n");
169 return;
170 }
171
172 h->Angle += 1.0;
173
174 glShadeModel(GL_FLAT);
175 glClearColor(0.5, 0.5, 0.5, 1.0);
176 glClear(GL_COLOR_BUFFER_BIT);
177
178 /* draw green triangle */
179 glColor3f(0.0, 1.0, 0.0);
180 glPushMatrix();
181 glRotatef(h->Angle, 0, 0, 1);
182 glBegin(GL_TRIANGLES);
183 glVertex2f(0, 0.8);
184 glVertex2f(-0.8, -0.7);
185 glVertex2f(0.8, -0.7);
186 glEnd();
187 glPopMatrix();
188
189 glXSwapBuffers(h->Dpy, h->Win);
190 }
191
192
193
194 static void
195 Resize(const struct head *h, unsigned int width, unsigned int height)
196 {
197 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
198 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
199 return;
200 }
201 glFlush();
202 glViewport(0, 0, width, height);
203 glMatrixMode(GL_PROJECTION);
204 glLoadIdentity();
205 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
206 }
207
208
209
210 static void
211 EventLoop(void)
212 {
213 while (1) {
214 int i;
215 for (i = 0; i < NumHeads; i++) {
216 struct head *h = &Heads[i];
217 while (XPending(h->Dpy) > 0) {
218 XEvent event;
219 XNextEvent(h->Dpy, &event);
220 if (event.xany.window == h->Win) {
221 switch (event.type) {
222 case Expose:
223 Redraw(h);
224 break;
225 case ConfigureNotify:
226 Resize(h, event.xconfigure.width, event.xconfigure.height);
227 break;
228 case KeyPress:
229 return;
230 default:
231 /*no-op*/ ;
232 }
233 }
234 else {
235 printf("window mismatch\n");
236 }
237 }
238 Redraw(h);
239 }
240 usleep(1);
241 }
242 }
243
244
245
246 static void
247 PrintInfo(const struct head *h)
248 {
249 printf("Name: %s\n", h->DisplayName);
250 printf(" Display: %p\n", (void *) h->Dpy);
251 printf(" Window: 0x%x\n", (int) h->Win);
252 printf(" Context: 0x%lx\n", (long) h->Context);
253 printf(" GL_VERSION: %s\n", h->Version);
254 printf(" GL_VENDOR: %s\n", h->Vendor);
255 printf(" GL_RENDERER: %s\n", h->Renderer);
256 }
257
258
259 int
260 main(int argc, char *argv[])
261 {
262 int i;
263 if (argc == 1) {
264 struct head *h;
265 printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
266 printf("Usage:\n");
267 printf(" glxheads xdisplayname ...\n");
268 printf("Example:\n");
269 printf(" glxheads :0 mars:0 venus:1\n");
270 h = AddHead(":0");
271 if (h)
272 PrintInfo(h);
273 }
274 else {
275 for (i = 1; i < argc; i++) {
276 const char *name = argv[i];
277 struct head *h = AddHead(name);
278 if (h) {
279 PrintInfo(h);
280 }
281 }
282 }
283
284 EventLoop();
285 return 0;
286 }