1 /* $Id: glxheads.c,v 1.4 2003/04/21 14:51:16 brianp Exp $ */
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.
8 * Copyright (C) 2000 Brian Paul All Rights Reserved.
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
39 * Each display/window/context:
42 char DisplayName
[1000];
54 static struct head Heads
[MAX_HEADS
];
55 static int NumHeads
= 0;
59 Error(const char *display
, const char *msg
)
61 fprintf(stderr
, "Error on display %s - %s\n", display
, msg
);
67 AddHead(const char *displayName
)
72 int attrib
[] = { GLX_RGBA
,
79 XSetWindowAttributes attr
;
83 int width
= 300, height
= 300;
84 int xpos
= 10, ypos
= 10;
86 if (NumHeads
>= MAX_HEADS
)
89 dpy
= XOpenDisplay(displayName
);
91 Error(displayName
, "Unable to open display");
95 scrnum
= DefaultScreen(dpy
);
96 root
= RootWindow(dpy
, scrnum
);
98 visinfo
= glXChooseVisual(dpy
, scrnum
, attrib
);
100 Error(displayName
, "Unable to find RGB, double-buffered visual");
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
;
111 win
= XCreateWindow(dpy
, root
, 0, 0, width
, height
,
112 0, visinfo
->depth
, InputOutput
,
113 visinfo
->visual
, mask
, &attr
);
115 Error(displayName
, "Couldn't create window");
120 XSizeHints sizehints
;
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
);
132 ctx
= glXCreateContext(dpy
, visinfo
, NULL
, True
);
134 Error(displayName
, "Couldn't create GLX context");
138 XMapWindow(dpy
, win
);
140 if (!glXMakeCurrent(dpy
, win
, ctx
)) {
141 Error(displayName
, "glXMakeCurrent failed");
142 printf("glXMakeCurrent failed in Redraw()\n");
146 /* save the info for this head */
148 struct head
*h
= &Heads
[NumHeads
];
149 strcpy(h
->DisplayName
, displayName
);
154 strcpy(h
->Version
, (char *) glGetString(GL_VERSION
));
155 strcpy(h
->Vendor
, (char *) glGetString(GL_VENDOR
));
156 strcpy(h
->Renderer
, (char *) glGetString(GL_RENDERER
));
158 return &Heads
[NumHeads
-1];
165 Redraw(struct head
*h
)
167 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
168 Error(h
->DisplayName
, "glXMakeCurrent failed");
169 printf("glXMakeCurrent failed in Redraw()\n");
175 glShadeModel(GL_FLAT
);
176 glClearColor(0.5, 0.5, 0.5, 1.0);
177 glClear(GL_COLOR_BUFFER_BIT
);
179 /* draw green triangle */
180 glColor3f(0.0, 1.0, 0.0);
182 glRotatef(h
->Angle
, 0, 0, 1);
183 glBegin(GL_TRIANGLES
);
185 glVertex2f(-0.8, -0.7);
186 glVertex2f(0.8, -0.7);
190 glXSwapBuffers(h
->Dpy
, h
->Win
);
196 Resize(const struct head
*h
, unsigned int width
, unsigned int height
)
198 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
199 Error(h
->DisplayName
, "glXMakeCurrent failed in Resize()");
203 glViewport(0, 0, width
, height
);
204 glMatrixMode(GL_PROJECTION
);
206 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
216 for (i
= 0; i
< NumHeads
; i
++) {
217 struct head
*h
= &Heads
[i
];
218 while (XPending(h
->Dpy
) > 0) {
220 XNextEvent(h
->Dpy
, &event
);
221 if (event
.xany
.window
== h
->Win
) {
222 switch (event
.type
) {
226 case ConfigureNotify
:
227 Resize(h
, event
.xconfigure
.width
, event
.xconfigure
.height
);
236 printf("window mismatch\n");
248 PrintInfo(const struct head
*h
)
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
);
261 main(int argc
, char *argv
[])
266 printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
268 printf(" glxheads xdisplayname ...\n");
269 printf("Example:\n");
270 printf(" glxheads :0 mars:0 venus:1\n");
276 for (i
= 1; i
< argc
; i
++) {
277 const char *name
= argv
[i
];
278 struct head
*h
= AddHead(name
);