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.
7 * Copyright (C) 2000 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
38 * Each display/window/context:
41 char DisplayName
[1000];
53 static struct head Heads
[MAX_HEADS
];
54 static int NumHeads
= 0;
58 Error(const char *display
, const char *msg
)
60 fprintf(stderr
, "Error on display %s - %s\n", XDisplayName(display
), msg
);
66 AddHead(const char *displayName
)
71 int attrib
[] = { GLX_RGBA
,
78 XSetWindowAttributes attr
;
82 int width
= 300, height
= 300;
83 int xpos
= 10, ypos
= 10;
85 if (NumHeads
>= MAX_HEADS
)
88 dpy
= XOpenDisplay(displayName
);
90 Error(displayName
, "Unable to open display");
94 scrnum
= DefaultScreen(dpy
);
95 root
= RootWindow(dpy
, scrnum
);
97 visinfo
= glXChooseVisual(dpy
, scrnum
, attrib
);
99 Error(displayName
, "Unable to find RGB, double-buffered visual");
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
;
110 win
= XCreateWindow(dpy
, root
, 0, 0, width
, height
,
111 0, visinfo
->depth
, InputOutput
,
112 visinfo
->visual
, mask
, &attr
);
114 Error(displayName
, "Couldn't create window");
119 XSizeHints sizehints
;
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
);
131 ctx
= glXCreateContext(dpy
, visinfo
, NULL
, True
);
133 Error(displayName
, "Couldn't create GLX context");
137 XMapWindow(dpy
, win
);
139 if (!glXMakeCurrent(dpy
, win
, ctx
)) {
140 Error(displayName
, "glXMakeCurrent failed");
141 printf("glXMakeCurrent failed in Redraw()\n");
145 /* save the info for this head */
147 struct head
*h
= &Heads
[NumHeads
];
148 strcpy(h
->DisplayName
, displayName
);
153 strcpy(h
->Version
, (char *) glGetString(GL_VERSION
));
154 strcpy(h
->Vendor
, (char *) glGetString(GL_VENDOR
));
155 strcpy(h
->Renderer
, (char *) glGetString(GL_RENDERER
));
157 return &Heads
[NumHeads
-1];
164 Redraw(struct head
*h
)
166 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
167 Error(h
->DisplayName
, "glXMakeCurrent failed");
168 printf("glXMakeCurrent failed in Redraw()\n");
174 glShadeModel(GL_FLAT
);
175 glClearColor(0.5, 0.5, 0.5, 1.0);
176 glClear(GL_COLOR_BUFFER_BIT
);
178 /* draw green triangle */
179 glColor3f(0.0, 1.0, 0.0);
181 glRotatef(h
->Angle
, 0, 0, 1);
182 glBegin(GL_TRIANGLES
);
184 glVertex2f(-0.8, -0.7);
185 glVertex2f(0.8, -0.7);
189 glXSwapBuffers(h
->Dpy
, h
->Win
);
195 Resize(const struct head
*h
, unsigned int width
, unsigned int height
)
197 if (!glXMakeCurrent(h
->Dpy
, h
->Win
, h
->Context
)) {
198 Error(h
->DisplayName
, "glXMakeCurrent failed in Resize()");
202 glViewport(0, 0, width
, height
);
203 glMatrixMode(GL_PROJECTION
);
205 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
215 for (i
= 0; i
< NumHeads
; i
++) {
216 struct head
*h
= &Heads
[i
];
217 while (XPending(h
->Dpy
) > 0) {
219 XNextEvent(h
->Dpy
, &event
);
220 if (event
.xany
.window
== h
->Win
) {
221 switch (event
.type
) {
225 case ConfigureNotify
:
226 Resize(h
, event
.xconfigure
.width
, event
.xconfigure
.height
);
235 printf("window mismatch\n");
247 PrintInfo(const struct head
*h
)
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
);
260 main(int argc
, char *argv
[])
265 printf("glxheads: exercise multiple GLX connections (any key = exit)\n");
267 printf(" glxheads xdisplayname ...\n");
268 printf("Example:\n");
269 printf(" glxheads :0 mars:0 venus:1\n");
275 for (i
= 1; i
< argc
; i
++) {
276 const char *name
= argv
[i
];
277 struct head
*h
= AddHead(name
);