aboutsummaryrefslogtreecommitdiffstats
path: root/progs/samples/oglinfo.c
blob: 4fe51efb3cb8394bd9f82ebfdbd24aee42828c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* oglinfo.c */

/* This demo modified by BrianP to accomodate Mesa and test
 * the GLX 1.1 functions.
 */



#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <string.h>

int visual_request0[] = { None }; /* don't need much of a visual */
int visual_request1[] = { GLX_RGBA, None }; /* in case CI failed */

int main(int argc, char **argv)
{
  char *display_name = NULL;
  char *string;
  Display       *dpy;
  int           screen_num;
  int           major, minor;
  XVisualInfo   *vis;
  GLXContext    ctx;
  Window        root,  win;
  Colormap      cmap;
  XSetWindowAttributes swa;
  int dontcare;

  /* parse arguments */
  if(argc > 1) {
    if(!strcmp(argv[1],"-display"))
      display_name = argv[2];
    else {
      fprintf(stderr, "Usage: %s [-display <display>]\n",argv[0]);
      return 0;
    }
  }

  /* get display */
  if (!(dpy = XOpenDisplay(display_name))) {
    fprintf(stderr,"Error: XOpenDisplay() failed.\n");
    return 1;
  }

  /* does the server know about OpenGL & GLX? */
#ifndef MESA
  if(!XQueryExtension(dpy, "GLX", &dontcare, &dontcare, &dontcare)) {
    fprintf(stderr,"This system doesn't appear to support OpenGL\n");
    return 1;
  }
#else
  (void) dontcare;
#endif

  /* find the glx version */
  if(glXQueryVersion(dpy, &major, &minor))
    printf("GLX Version: %d.%d\n", major, minor);
  else {
    fprintf(stderr, "Error: glXQueryVersion() failed.\n");
    return 1;
  }

  /* get screen number */
  screen_num = DefaultScreen(dpy);

/* This #ifdef isn't redundant. It keeps the build from breaking
** if you are building on a machine that has an old (1.0) version
** of glx.
**
** This program could still be *run* on a machine that has an old 
** version of glx, even if it was *compiled* on a version that has
** a new version.
**
** If compiled on a system with an old version of glx, then it will 
** never recognize glx extensions, since that code would have been
** #ifdef'ed out.
*/
#ifdef GLX_VERSION_1_1

  /*
  ** This test guarantees that glx, on the display you are inquiring,
  ** suppports glXQueryExtensionsString().
  */
  if(minor > 0 || major > 1)
    string = (char *) glXQueryExtensionsString(dpy, screen_num);
  else
    string = "";

  if(string)
    printf("GLX Extensions (client & server): %s\n",
	   string);
  else {
    fprintf(stderr, "Error: glXQueryExtensionsString() failed.\n");
    return 1;
  }

  if (minor>0 || major>1) {
     printf("glXGetClientString(GLX_VENDOR): %s\n", glXGetClientString(dpy,GLX_VENDOR));
     printf("glXGetClientString(GLX_VERSION): %s\n", glXGetClientString(dpy,GLX_VERSION));
     printf("glXGetClientString(GLX_EXTENSIONS): %s\n", glXGetClientString(dpy,GLX_EXTENSIONS));
     printf("glXQueryServerString(GLX_VENDOR): %s\n", glXQueryServerString(dpy,screen_num,GLX_VENDOR));
     printf("glXQueryServerString(GLX_VERSION): %s\n", glXQueryServerString(dpy,screen_num,GLX_VERSION));
     printf("glXQueryServerString(GLX_EXTENSIONS): %s\n", glXQueryServerString(dpy,screen_num,GLX_EXTENSIONS));
  }


#endif

   /* get any valid OpenGL visual */
   if (!(vis = glXChooseVisual(dpy, screen_num, visual_request0)))  {
      if (!(vis = glXChooseVisual(dpy, screen_num, visual_request1)))  {
         fprintf(stderr,"Error: glXChooseVisual() failed.\n");
         return 1;
      }
   }

   /* get context */
   ctx = glXCreateContext(dpy,vis,0,GL_TRUE);

   /* root window */
   root = RootWindow(dpy,vis->screen);

   /* get RGBA colormap */
   cmap = XCreateColormap(dpy, root, vis->visual, AllocNone);

   /* get window */
   swa.colormap = cmap;
   swa.border_pixel = 0;
   swa.event_mask = StructureNotifyMask;
   win = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, vis->depth,
		       InputOutput,vis->visual,
		       CWBorderPixel|CWColormap|CWEventMask,
		       &swa);

   glXMakeCurrent(dpy,win,ctx);

  string = (char *) glGetString(GL_VERSION);
  if(string)
#ifdef MESA
    printf("Mesa Version: %s\n", string);
#else
    printf("OpenGL Version: %s\n", string);
#endif
  else {
    fprintf(stderr, "Error: glGetString(GL_VERSION) failed.\n");
    return 1;
  }

  string = (char *) glGetString(GL_EXTENSIONS);

  if(string)
#ifdef MESA
    printf("Mesa Extensions: %s\n", string);
#else
    printf("OpenGL Extensions: %s\n", string);
#endif
  else {
    fprintf(stderr, "Error: glGetString(GL_EXTENSIONS) failed.\n");
    return 1;
  }

  string = (char *) glGetString(GL_RENDERER);

  if(string)
#ifdef MESA
    printf("Mesa Renderer: %s\n", string);
#else
    printf("OpenGL renderer: %s\n", string);
#endif
  else {
    fprintf(stderr, "Error: glGetString(GL_RENDERER) failed.\n");
    return 1;
  }

/*
** This #ifdef prevents a build failure if you compile on an a
** machine with an old GLU library. 
**
** If you build on a pre GLU 1.1 machine, you will never be able
** to get glu info, even if you run on a GLU 1.1 or latter machine,
** since the code has been #ifdef'ed out.
*/
#ifdef GLU_VERSION_1_1

  /*
  ** If the glx version is 1.1 or latter, gluGetString() is guaranteed
  ** to exist.
  */
  if(minor > 0 || major > 1)
    string = (char *) gluGetString(GLU_VERSION);
  else
    string = "1.0";

  if(string)
    printf("GLU Version: %s\n", string);
  else {
    fprintf(stderr, "Error: gluGetString(GLU_VERSION) failed.\n");
    return 1;
  }
  
  if(minor > 0 || major > 1)
    string = (char *) gluGetString(GLU_EXTENSIONS);
  else
    string = "";

  if(string)
    printf("GLU Extensions: %s\n", string);
  else {
    fprintf(stderr, "Error: gluGetString(GLU_EXTENSIONS) failed.\n");
    return 1;
  }

#endif
  return 0;
}