try both single and double-buffered visuals
[mesa.git] / progs / xdemos / glxinfo.c
1 /* $Id: glxinfo.c,v 1.4 2000/02/02 20:57:51 brianp Exp $ */
2
3 /*
4 * Copyright (C) 1999 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /*
26 * This program is a work-alike of the IRIX glxinfo program.
27 * Command line options:
28 * -t print wide table
29 * -v print verbose information
30 * -display DisplayName specify the X display to interogate
31 *
32 * Brian Paul 26 January 2000
33 */
34
35
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #include <GL/gl.h>
39 #include <GL/glu.h>
40 #include <GL/glx.h>
41 #include <stdio.h>
42 #include <string.h>
43
44
45 typedef enum
46 {
47 Normal,
48 Wide,
49 Verbose
50 } InfoMode;
51
52
53 struct visual_attribs
54 {
55 /* X visual attribs */
56 int id;
57 int klass;
58 int depth;
59 int redMask, greenMask, blueMask;
60 int colormapSize;
61 int bitsPerRGB;
62
63 /* GL visual attribs */
64 int supportsGL;
65 int transparent;
66 int bufferSize;
67 int level;
68 int rgba;
69 int doubleBuffer;
70 int stereo;
71 int auxBuffers;
72 int redSize, greenSize, blueSize, alphaSize;
73 int depthSize;
74 int stencilSize;
75 int accumRedSize, accumGreenSize, accumBlueSize, accumAlphaSize;
76 int numSamples, numMultisample;
77 };
78
79
80 /*
81 * Print a list of extensions, with word-wrapping.
82 */
83 static void
84 print_extension_list(const char *ext)
85 {
86 const char *indentString = " ";
87 const int indent = 4;
88 const int max = 79;
89 int width, i, j;
90
91 if (!ext || !ext[0])
92 return;
93
94 width = indent;
95 printf(indentString);
96 i = j = 0;
97 while (1) {
98 if (ext[j] == ' ' || ext[j] == 0) {
99 /* found end of an extension name */
100 const int len = j - i;
101 if (width + len > max) {
102 /* start a new line */
103 printf("\n");
104 width = indent;
105 printf(indentString);
106 }
107 /* print the extension name between ext[i] and ext[j] */
108 while (i < j) {
109 printf("%c", ext[i]);
110 i++;
111 }
112 /* either we're all done, or we'll continue with next extension */
113 width += len + 1;
114 if (ext[j] == 0) {
115 break;
116 }
117 else {
118 i++;
119 j++;
120 printf(", ");
121 width += 2;
122 }
123 }
124 j++;
125 }
126 printf("\n");
127 }
128
129
130 static void
131 print_screen_info(Display *dpy, int scrnum)
132 {
133 Window win;
134 int attribSingle[] = {
135 GLX_RGBA,
136 GLX_RED_SIZE, 1,
137 GLX_GREEN_SIZE, 1,
138 GLX_BLUE_SIZE, 1,
139 None };
140 int attribDouble[] = {
141 GLX_RGBA,
142 GLX_RED_SIZE, 1,
143 GLX_GREEN_SIZE, 1,
144 GLX_BLUE_SIZE, 1,
145 GLX_DOUBLEBUFFER,
146 None };
147
148 XSetWindowAttributes attr;
149 unsigned long mask;
150 Window root;
151 GLXContext ctx;
152 XVisualInfo *visinfo;
153 int width = 100, height = 100;
154
155 root = RootWindow(dpy, scrnum);
156
157 visinfo = glXChooseVisual(dpy, scrnum, attribSingle);
158 if (!visinfo) {
159 visinfo = glXChooseVisual(dpy, scrnum, attribDouble);
160 if (!visinfo) {
161 fprintf(stderr, "Error: couldn't find RGB GLX visual!\n");
162 return;
163 }
164 }
165
166 attr.background_pixel = 0;
167 attr.border_pixel = 0;
168 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
169 attr.event_mask = StructureNotifyMask | ExposureMask;
170 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
171 win = XCreateWindow(dpy, root, 0, 0, width, height,
172 0, visinfo->depth, InputOutput,
173 visinfo->visual, mask, &attr);
174
175 ctx = glXCreateContext( dpy, visinfo, NULL, True );
176
177 glXMakeCurrent( dpy, win, ctx );
178
179
180 {
181 const char *serverVendor = glXQueryServerString(dpy, scrnum, GLX_VENDOR);
182 const char *serverVersion = glXQueryServerString(dpy, scrnum, GLX_VERSION);
183 const char *serverExtensions = glXQueryServerString(dpy, scrnum, GLX_EXTENSIONS);
184 const char *clientVersion = glXGetClientString(dpy, GLX_VERSION);
185 const char *clientExtensions = glXGetClientString(dpy, GLX_EXTENSIONS);
186 const char *glxExtensions = glXQueryExtensionsString(dpy, scrnum);
187 const char *glVendor = (const char *) glGetString(GL_VENDOR);
188 const char *glRenderer = (const char *) glGetString(GL_RENDERER);
189 const char *glVersion = (const char *) glGetString(GL_VERSION);
190 const char *glExtensions = (const char *) glGetString(GL_EXTENSIONS);
191 const char *gluVersion = (const char *) gluGetString(GLU_VERSION);
192 const char *gluExtensions = (const char *) gluGetString(GLU_EXTENSIONS);
193 printf("display: %s screen:%d\n", DisplayString(dpy), scrnum);
194 printf("server glx vendor string: %s\n", serverVendor);
195 printf("server glx version string: %s\n", serverVersion);
196 printf("server glx extensions:\n");
197 print_extension_list(serverExtensions);
198 printf("client glx version: %s\n", clientVersion);
199 printf("client glx extensions:\n");
200 print_extension_list(clientExtensions);
201 printf("GLX extensions:\n");
202 print_extension_list(glxExtensions);
203 printf("OpenGL vendor string: %s\n", glVendor);
204 printf("OpenGL renderer string: %s\n", glRenderer);
205 printf("OpenGL version string: %s\n", glVersion);
206 printf("OpenGL extensions:\n");
207 print_extension_list(glExtensions);
208 printf("glu version: %s\n", gluVersion);
209 printf("glu extensions:\n");
210 print_extension_list(gluExtensions);
211 }
212
213 glXDestroyContext(dpy, ctx);
214 XDestroyWindow(dpy, win);
215 }
216
217
218 static const char *
219 visual_class_name(int cls)
220 {
221 switch (cls) {
222 case StaticColor:
223 return "StaticColor";
224 case PseudoColor:
225 return "PseudoColor";
226 case StaticGray:
227 return "StaticGray";
228 case GrayScale:
229 return "GrayScale";
230 case TrueColor:
231 return "TrueColor";
232 case DirectColor:
233 return "DirectColor";
234 default:
235 return "";
236 }
237 }
238
239
240 static const char *
241 visual_class_abbrev(int cls)
242 {
243 switch (cls) {
244 case StaticColor:
245 return "sc";
246 case PseudoColor:
247 return "pc";
248 case StaticGray:
249 return "sg";
250 case GrayScale:
251 return "gs";
252 case TrueColor:
253 return "tc";
254 case DirectColor:
255 return "dc";
256 default:
257 return "";
258 }
259 }
260
261
262 static void
263 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
264 struct visual_attribs *attribs)
265 {
266 attribs->id = vInfo->visualid;
267 #if defined(__cplusplus) || defined(c_plusplus)
268 attribs->klass = vInfo->c_class;
269 #else
270 attribs->klass = vInfo->class;
271 #endif
272 attribs->depth = vInfo->depth;
273 attribs->redMask = vInfo->red_mask;
274 attribs->greenMask = vInfo->green_mask;
275 attribs->blueMask = vInfo->blue_mask;
276 attribs->colormapSize = vInfo->colormap_size;
277 attribs->bitsPerRGB = vInfo->bits_per_rgb;
278
279 glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL);
280 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
281 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
282 glXGetConfig(dpy, vInfo, GLX_RGBA, &attribs->rgba);
283 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
284 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
285 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
286 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
287 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
288 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
289 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
290 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
291 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
292 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
293 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
294 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
295 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
296
297 /* transparent pixel value not implemented yet */
298 attribs->transparent = 0;
299
300 /* multisample tests not implemented yet */
301 attribs->numSamples = 0;
302 attribs->numMultisample = 0;
303 }
304
305
306 static void
307 print_visual_attribs_verbose(const struct visual_attribs *attribs)
308 {
309 printf("Visual ID: %x depth=%d class=%s\n",
310 attribs->id, attribs->depth, visual_class_name(attribs->klass));
311 printf(" bufferSize=%d level=%d renderType=%s doubleBuffer=%d stereo=%d\n",
312 attribs->bufferSize, attribs->level, attribs->rgba ? "rgba" : "ci",
313 attribs->doubleBuffer, attribs->stereo);
314 printf(" rgba: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
315 attribs->redSize, attribs->greenSize,
316 attribs->blueSize, attribs->alphaSize);
317 printf(" auxBuffers=%d depthSize=%d stencilSize=%d\n",
318 attribs->auxBuffers, attribs->depthSize, attribs->stencilSize);
319 printf(" accum: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
320 attribs->accumRedSize, attribs->accumGreenSize,
321 attribs->accumBlueSize, attribs->accumAlphaSize);
322 printf(" multiSample=%d multiSampleBuffers=%d\n",
323 attribs->numSamples, attribs->numMultisample);
324 printf(" %s\n", attribs->transparent ? "Transparent." : "Opaque.");
325 }
326
327
328 static void
329 print_visual_attribs_short_header(void)
330 {
331 printf(" visual x bf lv rg d st r g b a ax dp st accum buffs ms \n");
332 printf(" id dep cl sp sz l ci b ro sz sz sz sz bf th cl r g b a ns b\n");
333 printf("-----------------------------------------------------------------\n");
334 }
335
336
337 static void
338 print_visual_attribs_short(const struct visual_attribs *attribs)
339 {
340 printf("0x%2x %2d %2s %2d %2d %2d %1s %2s %2s %2d %2d %2d %2d %2d %2d %2d",
341 attribs->id,
342 attribs->depth,
343 visual_class_abbrev(attribs->klass),
344 attribs->transparent,
345 attribs->bufferSize,
346 attribs->level,
347 attribs->rgba ? "r" : "c",
348 attribs->doubleBuffer ? "y" : ".",
349 attribs->stereo ? "y" : ".",
350 attribs->redSize, attribs->greenSize,
351 attribs->blueSize, attribs->alphaSize,
352 attribs->auxBuffers,
353 attribs->depthSize,
354 attribs->stencilSize
355 );
356
357 printf(" %2d %2d %2d %2d %2d %1d\n",
358 attribs->accumRedSize, attribs->accumGreenSize,
359 attribs->accumBlueSize, attribs->accumAlphaSize,
360 attribs->numSamples, attribs->numMultisample
361 );
362 }
363
364
365 static void
366 print_visual_attribs_long_header(void)
367 {
368 printf("Vis Vis Visual Trans buff lev render DB ste r g b a aux dep ste accum buffers MS MS\n");
369 printf(" ID Depth Type parent size el type reo sz sz sz sz buf th ncl r g b a num bufs\n");
370 printf("----------------------------------------------------------------------------------------------------\n");
371 }
372
373
374 static void
375 print_visual_attribs_long(const struct visual_attribs *attribs)
376 {
377 printf("0x%2x %2d %-11s %2d %2d %2d %4s %3d %3d %3d %3d %3d %3d",
378 attribs->id,
379 attribs->depth,
380 visual_class_name(attribs->klass),
381 attribs->transparent,
382 attribs->bufferSize,
383 attribs->level,
384 attribs->rgba ? "rgba" : "ci ",
385 attribs->doubleBuffer,
386 attribs->stereo,
387 attribs->redSize, attribs->greenSize,
388 attribs->blueSize, attribs->alphaSize
389 );
390
391 printf(" %3d %4d %2d %3d %3d %3d %3d %2d %2d\n",
392 attribs->auxBuffers,
393 attribs->depthSize,
394 attribs->stencilSize,
395 attribs->accumRedSize, attribs->accumGreenSize,
396 attribs->accumBlueSize, attribs->accumAlphaSize,
397 attribs->numSamples, attribs->numMultisample
398 );
399 }
400
401
402 static void
403 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
404 {
405 XVisualInfo template;
406 XVisualInfo *visuals;
407 int numVisuals;
408 long mask;
409 int i;
410
411 /* get list of all visuals on this screen */
412 template.screen = scrnum;
413 mask = VisualScreenMask;
414 visuals = XGetVisualInfo(dpy, mask, &template, &numVisuals);
415
416 if (mode == Verbose) {
417 for (i = 0; i < numVisuals; i++) {
418 struct visual_attribs attribs;
419 get_visual_attribs(dpy, &visuals[i], &attribs);
420 print_visual_attribs_verbose(&attribs);
421 }
422 }
423 else if (mode == Normal) {
424 print_visual_attribs_short_header();
425 for (i = 0; i < numVisuals; i++) {
426 struct visual_attribs attribs;
427 get_visual_attribs(dpy, &visuals[i], &attribs);
428 print_visual_attribs_short(&attribs);
429 }
430 }
431 else if (mode == Wide) {
432 print_visual_attribs_long_header();
433 for (i = 0; i < numVisuals; i++) {
434 struct visual_attribs attribs;
435 get_visual_attribs(dpy, &visuals[i], &attribs);
436 print_visual_attribs_long(&attribs);
437 }
438 }
439
440 XFree(visuals);
441 }
442
443
444 int
445 main(int argc, char *argv[])
446 {
447 char *displayName = ":0";
448 Display *dpy;
449 int numScreens, scrnum;
450 InfoMode mode = Normal;
451 int i;
452
453 for (i = 1; i < argc; i++) {
454 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
455 displayName = argv[i + 1];
456 i++;
457 }
458 else if (strcmp(argv[i], "-t") == 0) {
459 mode = Wide;
460 }
461 else if (strcmp(argv[i], "-v") == 0) {
462 mode = Verbose;
463 }
464 }
465
466 dpy = XOpenDisplay(displayName);
467 if (!dpy) {
468 fprintf(stderr, "Error: unable to open display %s\n", displayName);
469 return -1;
470 }
471
472 numScreens = ScreenCount(dpy);
473 for (scrnum = 0; scrnum < numScreens; scrnum++) {
474 print_screen_info(dpy, 0);
475 printf("\n");
476 print_visual_info(dpy, 0, mode);
477 if (scrnum + 1 < numScreens)
478 printf("\n\n");
479 }
480
481 XCloseDisplay(dpy);
482
483 return 0;
484 }