summaryrefslogtreecommitdiff
path: root/src/xdemos/glxsnoop.c
blob: 2215cfdaf6f89337b71721266509015eb5a40ff2 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * Display/snoop the z/stencil/back/front buffers of another app's window.
 * Also, an example of the need for shared ancillary renderbuffers.
 *
 * Hint: use 'xwininfo' to get a window's ID.
 *
 * Brian Paul
 * 11 Oct 2007
 */

#define GL_GLEXT_PROTOTYPES

#include <GL/gl.h>
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>


#define Z_BUFFER 1
#define STENCIL_BUFFER 2
#define BACK_BUFFER 3
#define FRONT_BUFFER 4


static int Buffer = BACK_BUFFER;
static int WindowID = 0;
static const char *DisplayName = NULL;
static GLXContext Context = 0;
static int Width, Height;


/**
 * Grab the z/stencil/back/front image from the srcWin and display it
 * (possibly converted to grayscale) in the dstWin.
 */
static void
redraw(Display *dpy, Window srcWin, Window dstWin )
{
   GLubyte *image = malloc(Width * Height * 4);

   glXMakeCurrent(dpy, srcWin, Context);
   glPixelStorei(GL_PACK_ALIGNMENT, 1);
   if (Buffer == BACK_BUFFER) {
      glReadBuffer(GL_BACK);
      glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, image);
   }
   else if (Buffer == FRONT_BUFFER) {
      glReadBuffer(GL_FRONT);
      glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, image);
   }
   else if (Buffer == Z_BUFFER) {
      GLfloat *z = malloc(Width * Height * sizeof(GLfloat));
      int i;
      glReadPixels(0, 0, Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
      for (i = 0; i < Width * Height; i++) {
         image[i*4+0] =
         image[i*4+1] =
         image[i*4+2] = (GLint) (255.0 * z[i]);
         image[i*4+3] = 255;
      }
      free(z);
   }
   else if (Buffer == STENCIL_BUFFER) {
      GLubyte *sten = malloc(Width * Height * sizeof(GLubyte));
      int i, min = 100, max = -1;
      float step;
      int sz;
      glGetIntegerv(GL_STENCIL_BITS, &sz);
      glReadPixels(0, 0, Width, Height,
                   GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, sten);
      /* find min/max for converting stencil to grayscale */
      for (i = 0; i < Width * Height; i++) {
         if (sten[i] < min)
            min = sten[i];
         if (sten[i] > max)
            max = sten[i];
      }
      if (min == max)
         step = 0;
      else
         step = 255.0 / (float) (max - min);
      for (i = 0; i < Width * Height; i++) {
         image[i*4+0] =
         image[i*4+1] =
         image[i*4+2] = (GLint) ((sten[i] - min) * step);
         image[i*4+3] = 255;
      }
      free(sten);
   }

   glXMakeCurrent(dpy, dstWin, Context);
   glWindowPos2iARB(0, 0);
   glDrawBuffer(GL_FRONT);
   glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, image);
   glFlush();

   free(image);
}


static void
set_window_title(Display *dpy, Window win, const char *title)
{
   XSizeHints sizehints;
   sizehints.flags = 0;
   XSetStandardProperties(dpy, win, title, title,
                          None, (char **)NULL, 0, &sizehints);
}


static Window
make_gl_window(Display *dpy, XVisualInfo *visinfo, int width, int height)
{
   int scrnum;
   XSetWindowAttributes attr;
   unsigned long mask;
   Window root;
   Window win;
   int x = 0, y = 0;
   char *name = NULL;

   scrnum = DefaultScreen( dpy );
   root = RootWindow( dpy, scrnum );

   /* window attributes */
   attr.background_pixel = 0;
   attr.border_pixel = 0;
   attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
   attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

   win = XCreateWindow( dpy, root, x, y, width, height,
		        0, visinfo->depth, InputOutput,
		        visinfo->visual, mask, &attr );

   /* set hints and properties */
   {
      XSizeHints sizehints;
      sizehints.x = x;
      sizehints.y = y;
      sizehints.width  = width;
      sizehints.height = height;
      sizehints.flags = USSize | USPosition;
      XSetNormalHints(dpy, win, &sizehints);
      XSetStandardProperties(dpy, win, name, name,
                              None, (char **)NULL, 0, &sizehints);
   }

   return win;
}


static void
update_window_title(Display *dpy, Window win)
{
   char title[1000], *buf;

   switch (Buffer) {
   case Z_BUFFER:
      buf = "Z";
      break;
   case STENCIL_BUFFER:
      buf = "Stencil";
      break;
   case BACK_BUFFER:
      buf = "Back";
      break;
   case FRONT_BUFFER:
      buf = "Front";
      break;
   default:
      buf = "";
   }

   sprintf(title, "glxsnoop window 0x%x (%s buffer)", (int) WindowID, buf);

   set_window_title(dpy, win, title);
}


static void
keypress(Display *dpy, Window win, char key)
{
   switch (key) {
   case 27:
      /* escape */
      exit(0);
      break;
   case 's':
      Buffer = STENCIL_BUFFER;
      break;
   case 'z':
      Buffer = Z_BUFFER;
      break;
   case 'f':
      Buffer = FRONT_BUFFER;
      break;
   case 'b':
      Buffer = BACK_BUFFER;
      break;
   default:
      return;
   }

   update_window_title(dpy, win);
   redraw(dpy, WindowID, win);
}


static void
event_loop(Display *dpy, Window win)
{
   XEvent event;

   while (1) {
      XNextEvent( dpy, &event );

      switch (event.type) {
      case Expose:
         redraw(dpy, WindowID, win);
         break;
      case ConfigureNotify:
         /*resize( event.xconfigure.width, event.xconfigure.height );*/
         break;
      case KeyPress:
         {
            char buffer[10];
            int code;
            code = XLookupKeysym(&event.xkey, 0);
            if (code == XK_Left) {
            }
            else {
               XLookupString(&event.xkey, buffer, sizeof(buffer),
                             NULL, NULL);
               keypress(dpy, win, buffer[0]);
            }
         }
      default:
         /* nothing */
         ;
      }
   }
}


static VisualID
get_window_visualid(Display *dpy, Window win)
{
   XWindowAttributes attr;

   if (XGetWindowAttributes(dpy, win, &attr)) {
      return attr.visual->visualid;
   }
   else {
      return 0;
   }
}


static void
get_window_size(Display *dpy, Window win, int *w, int *h)
{
   XWindowAttributes attr;

   if (XGetWindowAttributes(dpy, win, &attr)) {
      *w = attr.width;
      *h = attr.height;
   }
   else {
      *w = *h = 0;
   }
}


static XVisualInfo *
visualid_to_visualinfo(Display *dpy, VisualID vid)
{
   XVisualInfo *vinfo, templ;
   long mask;
   int n;

   templ.visualid = vid;
   mask = VisualIDMask;

   vinfo = XGetVisualInfo(dpy, mask, &templ, &n);
   return vinfo;
}


static void
key_usage(void)
{
   printf("Keyboard:\n");
   printf("  z - display Z buffer\n");
   printf("  s - display stencil buffer\n");
   printf("  f - display front color buffer\n");
   printf("  b - display back buffer\n");
}


static void
usage(void)
{
   printf("Usage: glxsnoop [-display dpy] windowID\n");
   key_usage();
}


static void
parse_opts(int argc, char *argv[])
{
   int i;

   for (i = 1; i < argc; i++) {
      if (strcmp(argv[i], "-h") == 0) {
         usage();
         exit(0);
      }
      else if (strcmp(argv[i], "-display") == 0) {
         DisplayName = argv[i + 1];
         i++;
      }
      else {
         if (argv[i][0] == '0' && argv[i][1] == 'x') {
            /* hex */
            WindowID = strtol(argv[i], NULL, 16);
         }
         else {
            WindowID = atoi(argv[i]);
         }
         break;
      }
   }

   if (!WindowID) {
      usage();
      exit(0);
   }
}


int
main( int argc, char *argv[] )
{
   Display *dpy;
   VisualID vid;
   XVisualInfo *visinfo;
   Window win;

   parse_opts(argc, argv);

   key_usage();

   dpy = XOpenDisplay(DisplayName);

   /* find the VisualID for the named window */
   vid = get_window_visualid(dpy, WindowID);
   get_window_size(dpy, WindowID, &Width, &Height);

   visinfo = visualid_to_visualinfo(dpy, vid);

   Context = glXCreateContext( dpy, visinfo, NULL, True );
   if (!Context) {
      printf("Error: glXCreateContext failed\n");
      exit(1);
   }

   win = make_gl_window(dpy, visinfo, Width, Height);
   XMapWindow(dpy, win);
   update_window_title(dpy, win);

   event_loop( dpy, win );

   return 0;
}