b7b4fafa17f7fab18bbdb2d43573a36e5d6658a5
[mesa.git] / progs / xdemos / glxinfo.c
1 /* $Id: glxinfo.c,v 1.2 2000/01/27 16:43:56 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 attrib[] = { GLX_RGBA,
135 GLX_RED_SIZE, 1,
136 GLX_GREEN_SIZE, 1,
137 GLX_BLUE_SIZE, 1,
138 None };
139 XSetWindowAttributes attr;
140 unsigned long mask;
141 Window root;
142 GLXContext ctx;
143 XVisualInfo *visinfo;
144 int width = 100, height = 100;
145
146 root = RootWindow(dpy, scrnum);
147
148 visinfo = glXChooseVisual(dpy, scrnum, attrib);
149 if (!visinfo) {
150 fprintf(stderr, "Error: couldn't find RGB GLX visual!\n");
151 return;
152 }
153
154 attr.background_pixel = 0;
155 attr.border_pixel = 0;
156 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
157 attr.event_mask = StructureNotifyMask | ExposureMask;
158 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
159 win = XCreateWindow(dpy, root, 0, 0, width, height,
160 0, visinfo->depth, InputOutput,
161 visinfo->visual, mask, &attr);
162
163 ctx = glXCreateContext( dpy, visinfo, NULL, True );
164
165 glXMakeCurrent( dpy, win, ctx );
166
167
168 {
169 const char *serverVendor = glXQueryServerString(dpy, scrnum, GLX_VENDOR);
170 const char *serverVersion = glXQueryServerString(dpy, scrnum, GLX_VERSION);
171 const char *serverExtensions = glXQueryServerString(dpy, scrnum, GLX_EXTENSIONS);
172 const char *clientVersion = glXGetClientString(dpy, GLX_VERSION);
173 const char *clientExtensions = glXGetClientString(dpy, GLX_EXTENSIONS);
174 const char *glxExtensions = glXQueryExtensionsString(dpy, scrnum);
175 const char *glVendor = (const char *) glGetString(GL_VENDOR);
176 const char *glRenderer = (const char *) glGetString(GL_RENDERER);
177 const char *glVersion = (const char *) glGetString(GL_VERSION);
178 const char *glExtensions = (const char *) glGetString(GL_EXTENSIONS);
179 const char *gluVersion = (const char *) gluGetString(GLU_VERSION);
180 const char *gluExtensions = (const char *) gluGetString(GLU_EXTENSIONS);
181 printf("display: %s screen:%d\n", DisplayString(dpy), scrnum);
182 printf("server glx vendor string: %s\n", serverVendor);
183 printf("server glx version string: %s\n", serverVersion);
184 printf("server glx extensions:\n");
185 print_extension_list(serverExtensions);
186 printf("client glx version: %s\n", clientVersion);
187 printf("client glx extensions:\n");
188 print_extension_list(clientExtensions);
189 printf("GLX extensions:\n");
190 print_extension_list(glxExtensions);
191 printf("OpenGL vendor string: %s\n", glVendor);
192 printf("OpenGL renderer string: %s\n", glRenderer);
193 printf("OpenGL version string: %s\n", glVersion);
194 printf("OpenGL extensions:\n");
195 print_extension_list(glExtensions);
196 printf("glu version: %s\n", gluVersion);
197 printf("glu extensions:\n");
198 print_extension_list(gluExtensions);
199 }
200
201 glXDestroyContext(dpy, ctx);
202 XDestroyWindow(dpy, win);
203 }
204
205
206 static const char *
207 visual_class_name(int cls)
208 {
209 switch (cls) {
210 case StaticColor:
211 return "StaticColor";
212 case PseudoColor:
213 return "PseudoColor";
214 case StaticGray:
215 return "StaticGray";
216 case GrayScale:
217 return "GrayScale";
218 case TrueColor:
219 return "TrueColor";
220 case DirectColor:
221 return "DirectColor";
222 default:
223 return "";
224 }
225 }
226
227
228 static const char *
229 visual_class_abbrev(int cls)
230 {
231 switch (cls) {
232 case StaticColor:
233 return "sc";
234 case PseudoColor:
235 return "pc";
236 case StaticGray:
237 return "sg";
238 case GrayScale:
239 return "gs";
240 case TrueColor:
241 return "tc";
242 case DirectColor:
243 return "dc";
244 default:
245 return "";
246 }
247 }
248
249
250 static void
251 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
252 struct visual_attribs *attribs)
253 {
254 attribs->id = vInfo->visualid;
255 #if defined(__cplusplus) || defined(c_plusplus)
256 attribs->klass = vInfo->c_class;
257 #else
258 attribs->klass = vInfo->class;
259 #endif
260 attribs->depth = vInfo->depth;
261 attribs->redMask = vInfo->red_mask;
262 attribs->greenMask = vInfo->green_mask;
263 attribs->blueMask = vInfo->blue_mask;
264 attribs->colormapSize = vInfo->colormap_size;
265 attribs->bitsPerRGB = vInfo->bits_per_rgb;
266
267 attribs->transparent = 0;
268 glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL);
269 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
270 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
271 glXGetConfig(dpy, vInfo, GLX_RGBA, &attribs->rgba);
272 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
273 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
274 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
275 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
276 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
277 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
278 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
279 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
280 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
281 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
282 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
283 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
284 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
285
286 attribs->numSamples = 0;
287 attribs->numMultisample = 0;
288 }
289
290
291 static void
292 print_visual_attribs_verbose(const struct visual_attribs *attribs)
293 {
294 printf("Visual ID: %x depth=%d class=%s\n",
295 attribs->id, attribs->depth, visual_class_name(attribs->klass));
296 printf(" bufferSize=%d level=%d renderType=%s doubleBuffer=%d stereo=%d\n",
297 attribs->bufferSize, attribs->level, attribs->rgba ? "rgba" : "ci",
298 attribs->doubleBuffer, attribs->stereo);
299 printf(" rgba: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
300 attribs->redSize, attribs->greenSize,
301 attribs->blueSize, attribs->alphaSize);
302 printf(" auxBuffers=%d depthSize=%d stencilSize=%d\n",
303 attribs->auxBuffers, attribs->depthSize, attribs->stencilSize);
304 printf(" accum: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
305 attribs->accumRedSize, attribs->accumGreenSize,
306 attribs->accumBlueSize, attribs->accumAlphaSize);
307 printf(" multiSample=%d multiSampleBuffers=%d\n",
308 attribs->numSamples, attribs->numMultisample);
309 printf(" %s\n", attribs->transparent ? "Transparent." : "Opaque.");
310 }
311
312
313 static void
314 print_visual_attribs_short_header(void)
315 {
316 printf(" visual x bf lv rg d st r g b a ax dp st accum buffs ms \n");
317 printf(" id dep cl sp sz l ci b ro sz sz sz sz bf th cl r g b a ns b\n");
318 printf("-----------------------------------------------------------------\n");
319 }
320
321
322 static void
323 print_visual_attribs_short(const struct visual_attribs *attribs)
324 {
325 printf("0x%2x %2d %2s %2d %2d %2d %1s %2s %2s %2d %2d %2d %2d %2d %2d %2d",
326 attribs->id,
327 attribs->depth,
328 visual_class_abbrev(attribs->klass),
329 attribs->transparent,
330 attribs->bufferSize,
331 attribs->level,
332 attribs->rgba ? "r" : "c",
333 attribs->doubleBuffer ? "y" : ".",
334 attribs->stereo ? "y" : ".",
335 attribs->redSize, attribs->greenSize,
336 attribs->blueSize, attribs->alphaSize,
337 attribs->auxBuffers,
338 attribs->depthSize,
339 attribs->stencilSize
340 );
341
342 printf(" %2d %2d %2d %2d %2d %1d\n",
343 attribs->accumRedSize, attribs->accumGreenSize,
344 attribs->accumBlueSize, attribs->accumAlphaSize,
345 attribs->numSamples, attribs->numMultisample
346 );
347 }
348
349
350 static void
351 print_visual_attribs_long_header(void)
352 {
353 printf("Vis Vis Visual Trans buff lev render DB ste r g b a aux dep ste accum buffers MS MS\n");
354 printf(" ID Depth Type parent size el type reo sz sz sz sz buf th ncl r g b a num bufs\n");
355 printf("----------------------------------------------------------------------------------------------------\n");
356 }
357
358
359 static void
360 print_visual_attribs_long(const struct visual_attribs *attribs)
361 {
362 printf("0x%2x %2d %-11s %2d %2d %2d %4s %3d %3d %3d %3d %3d %3d",
363 attribs->id,
364 attribs->depth,
365 visual_class_name(attribs->klass),
366 attribs->transparent,
367 attribs->bufferSize,
368 attribs->level,
369 attribs->rgba ? "rgba" : "ci ",
370 attribs->doubleBuffer,
371 attribs->stereo,
372 attribs->redSize, attribs->greenSize,
373 attribs->blueSize, attribs->alphaSize
374 );
375
376 printf(" %3d %4d %2d %3d %3d %3d %3d %2d %2d\n",
377 attribs->auxBuffers,
378 attribs->depthSize,
379 attribs->stencilSize,
380 attribs->accumRedSize, attribs->accumGreenSize,
381 attribs->accumBlueSize, attribs->accumAlphaSize,
382 attribs->numSamples, attribs->numMultisample
383 );
384 }
385
386
387 static void
388 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
389 {
390 XVisualInfo template;
391 XVisualInfo *visuals;
392 int numVisuals;
393 long mask;
394 int i;
395
396 /* get list of all visuals on this screen */
397 template.screen = scrnum;
398 mask = VisualScreenMask;
399 visuals = XGetVisualInfo(dpy, mask, &template, &numVisuals);
400
401 if (mode == Verbose) {
402 for (i = 0; i < numVisuals; i++) {
403 struct visual_attribs attribs;
404 get_visual_attribs(dpy, &visuals[i], &attribs);
405 print_visual_attribs_verbose(&attribs);
406 }
407 }
408 else if (mode == Normal) {
409 print_visual_attribs_short_header();
410 for (i = 0; i < numVisuals; i++) {
411 struct visual_attribs attribs;
412 get_visual_attribs(dpy, &visuals[i], &attribs);
413 print_visual_attribs_short(&attribs);
414 }
415 }
416 else if (mode == Wide) {
417 print_visual_attribs_long_header();
418 for (i = 0; i < numVisuals; i++) {
419 struct visual_attribs attribs;
420 get_visual_attribs(dpy, &visuals[i], &attribs);
421 print_visual_attribs_long(&attribs);
422 }
423 }
424
425 XFree(visuals);
426 }
427
428
429 int
430 main(int argc, char *argv[])
431 {
432 char *displayName = ":0";
433 Display *dpy;
434 int numScreens, scrnum;
435 InfoMode mode = Normal;
436 int i;
437
438 for (i = 1; i < argc; i++) {
439 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
440 displayName = argv[i + 1];
441 i++;
442 }
443 else if (strcmp(argv[i], "-t") == 0) {
444 mode = Wide;
445 }
446 else if (strcmp(argv[i], "-v") == 0) {
447 mode = Verbose;
448 }
449 }
450
451 dpy = XOpenDisplay(displayName);
452 if (!dpy) {
453 fprintf(stderr, "Error: unable to open display %s\n", displayName);
454 return -1;
455 }
456
457 numScreens = ScreenCount(dpy);
458 for (scrnum = 0; scrnum < numScreens; scrnum++) {
459 print_screen_info(dpy, 0);
460 printf("\n");
461 print_visual_info(dpy, 0, mode);
462 if (scrnum + 1 < numScreens)
463 printf("\n\n");
464 }
465
466 XCloseDisplay(dpy);
467
468 return 0;
469 }