~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/dmx/examples/dmxwininfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86$ */
 
2
/*
 
3
 * Copyright (c) 2003 by the XFree86 Project, Inc.
 
4
 *
 
5
 * All Rights Reserved.
 
6
 *
 
7
 * Permission is hereby granted, free of charge, to any person obtaining
 
8
 * a copy of this software and associated documentation files (the
 
9
 * "Software"), to deal in the Software without restriction, including
 
10
 * without limitation on the rights to use, copy, modify, merge,
 
11
 * publish, distribute, sublicense, and/or sell copies of the Software,
 
12
 * and to permit persons to whom the Software is furnished to do so,
 
13
 * subject to the following conditions:
 
14
 *
 
15
 * The above copyright notice and this permission notice (including the
 
16
 * next paragraph) shall be included in all copies or substantial
 
17
 * portions of the Software.
 
18
 *
 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
22
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 
23
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
24
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
26
 * SOFTWARE.
 
27
 */
 
28
 
 
29
/*
 
30
 * Create a window and use the DMX extension to query the window's
 
31
 * back-end properties.  Display the info inside the window itself.
 
32
 *
 
33
 * Brian Paul
 
34
 * 23 Jan 2003
 
35
 */
 
36
 
 
37
#include <assert.h>
 
38
#include <stdio.h>
 
39
#include <stdlib.h>
 
40
#include <string.h>
 
41
#include <X11/Xlib.h>
 
42
#include <X11/Xutil.h>
 
43
#include <X11/Xmu/SysUtil.h>
 
44
#include <X11/extensions/dmxext.h>
 
45
 
 
46
static const char *FontName = "fixed";
 
47
 
 
48
 
 
49
static void
 
50
EventLoop(Display *dpy, Window win, GC gc)
 
51
{
 
52
   XEvent ev;
 
53
   while (1) {
 
54
      XNextEvent( dpy, &ev );
 
55
      switch (ev.type) {
 
56
      case ReparentNotify:
 
57
         break;
 
58
      case MapNotify:
 
59
         break;
 
60
      case ConfigureNotify:
 
61
      case Expose:
 
62
         {         
 
63
            int numScreens, count, i;
 
64
            DMXWindowAttributes *winInfo;
 
65
            int x, y;
 
66
            const char *msg = "DMX window info:";
 
67
 
 
68
            DMXGetScreenCount(dpy, &numScreens);
 
69
            winInfo
 
70
                = (DMXWindowAttributes *)
 
71
                malloc(numScreens * sizeof(DMXWindowAttributes));
 
72
            assert(winInfo);
 
73
            if (!DMXGetWindowAttributes(dpy, win, &count,
 
74
                                        numScreens, winInfo)) {
 
75
               printf("Could not get window information for 0x%08lx\n",
 
76
                      (long unsigned)win);
 
77
            }
 
78
            x = y = 50;
 
79
            XClearWindow(dpy, win);
 
80
            XDrawString(dpy, win, gc, x, y, msg, strlen(msg));
 
81
            y += 20;
 
82
            for (i = 0; i < count; i++) {
 
83
               char str[500];
 
84
               XmuSnprintf(str, sizeof(str),
 
85
                       "screen %d:  pos: %dx%d+%d+%d  visible: %dx%d+%d+%d",
 
86
                       winInfo[i].screen,
 
87
                       winInfo[i].pos.width, winInfo[i].pos.height,
 
88
                       winInfo[i].pos.x, winInfo[i].pos.y,
 
89
                       winInfo[i].vis.width, winInfo[i].vis.height, 
 
90
                       winInfo[i].vis.x, winInfo[i].vis.y);
 
91
               XDrawString(dpy, win, gc, x, y, str, strlen(str));
 
92
               y += 20;
 
93
            }
 
94
            free(winInfo);
 
95
         }
 
96
         break;
 
97
      default:
 
98
         printf("Event type 0x%x\n", ev.type);
 
99
      }
 
100
   }
 
101
}
 
102
 
 
103
int
 
104
main(int argc, char *argv[])
 
105
{
 
106
   const char *displayName = NULL;
 
107
   Display *dpy;
 
108
   int event_base, error_base;
 
109
   int scr, n;
 
110
   long vinfoMask, attrMask;
 
111
   XVisualInfo vinfoTemp, *visInfo;
 
112
   Visual *vis;
 
113
   Window win, root;
 
114
   XSetWindowAttributes attr;
 
115
   XFontStruct *fontInfo;
 
116
   GC gc;
 
117
 
 
118
   if (argc > 1) {
 
119
      displayName = argv[1];
 
120
   }
 
121
 
 
122
   dpy = XOpenDisplay(displayName);
 
123
   if (!dpy) {
 
124
      fprintf(stderr, "Unable to open display %s\n", displayName);
 
125
      return -1;
 
126
   }
 
127
 
 
128
   if (!DMXQueryExtension(dpy, &event_base, &error_base)) {
 
129
      fprintf(stderr, "DMX extension not available on this display.\n");
 
130
      return -1;
 
131
   }
 
132
 
 
133
   scr = DefaultScreen(dpy);
 
134
   root = RootWindow(dpy, scr);
 
135
   vis = DefaultVisual(dpy, scr);
 
136
 
 
137
   vinfoMask = VisualIDMask;
 
138
   vinfoTemp.visualid = vis->visualid;
 
139
   visInfo = XGetVisualInfo(dpy, vinfoMask, &vinfoTemp, &n);
 
140
   if (!visInfo || n != 1) {
 
141
      fprintf(stderr, "Unable to get visual!\n");
 
142
      XCloseDisplay(dpy);
 
143
      return -1;
 
144
   }
 
145
 
 
146
   attr.background_pixel = 0;
 
147
   attr.border_pixel = 0;
 
148
   attr.colormap = XCreateColormap(dpy, root, visInfo->visual, AllocNone);
 
149
   attr.event_mask = StructureNotifyMask | ExposureMask;
 
150
   attrMask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
 
151
 
 
152
   win = XCreateWindow(dpy, root,
 
153
                       500, 500, 600, 400,  /* x, y, w, h */
 
154
                       0, /* border_width */
 
155
                       visInfo->depth, InputOutput,
 
156
                       visInfo->visual, attrMask, &attr);
 
157
 
 
158
 
 
159
   if (!win) {
 
160
      fprintf(stderr, "Unable to create window!\n");
 
161
      XCloseDisplay(dpy);
 
162
      return -1;
 
163
   }
 
164
 
 
165
   fontInfo = XLoadQueryFont(dpy, FontName);
 
166
   if (!fontInfo) {
 
167
      fprintf(stderr, "Error: font %s not found\n", FontName);
 
168
      exit(0);
 
169
   }
 
170
 
 
171
   gc = XCreateGC(dpy, win, 0, NULL);
 
172
   XSetBackground(dpy, gc, BlackPixel(dpy, scr));
 
173
   XSetForeground(dpy, gc, WhitePixel(dpy, scr));
 
174
   XSetFont(dpy, gc, fontInfo->fid);
 
175
 
 
176
   XMapWindow(dpy, win);
 
177
 
 
178
   EventLoop(dpy, win, gc);
 
179
 
 
180
   XDestroyWindow(dpy, win);
 
181
   XCloseDisplay(dpy);
 
182
   return 0;
 
183
}
 
184
 
 
185
#if 00
 
186
 
 
187
static void make_window( char *title, int color_flag )
 
188
{
 
189
   int x = 10, y = 10, width = 400, height = 300;
 
190
   Display *dpy;
 
191
   int scr;
 
192
   Window root, win;
 
193
   Colormap cmap;
 
194
   XColor xcolor;
 
195
   int attr_flags;
 
196
   XVisualInfo *visinfo;
 
197
   XSetWindowAttributes attr;
 
198
   XTextProperty tp;
 
199
   XSizeHints sh;
 
200
   XEvent e;
 
201
   XMesaContext context;
 
202
   XMesaVisual visual;
 
203
   XMesaBuffer buffer;
 
204
 
 
205
 
 
206
   /*
 
207
    * Do the usual X things to make a window.
 
208
    */
 
209
 
 
210
   dpy = XOpenDisplay(NULL);
 
211
   if (!dpy) {
 
212
      printf("Couldn't open default display!\n");
 
213
      exit(1);
 
214
   }
 
215
 
 
216
   scr = DefaultScreen(dpy);
 
217
   root = RootWindow(dpy, scr);
 
218
 
 
219
   /* alloc visinfo struct */
 
220
   visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
 
221
 
 
222
   /* Get a visual and colormap */
 
223
   if (color_flag) {
 
224
      /* Open TrueColor window */
 
225
 
 
226
/*
 
227
      if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
 
228
         printf("Couldn't get 24-bit TrueColor visual!\n");
 
229
         exit(1);
 
230
      }
 
231
*/
 
232
      if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
 
233
         printf("Couldn't get 8-bit PseudoColor visual!\n");
 
234
         exit(1);
 
235
      }
 
236
 
 
237
      cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
 
238
      Black = Red = Green = Blue = 0;
 
239
   }
 
240
   else {
 
241
      /* Open color index window */
 
242
 
 
243
      if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
 
244
         printf("Couldn't get 8-bit PseudoColor visual\n");
 
245
         exit(1);
 
246
      }
 
247
 
 
248
      cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
 
249
 
 
250
      /* Allocate colors */
 
251
      xcolor.red   = 0x0;
 
252
      xcolor.green = 0x0;
 
253
      xcolor.blue  = 0x0;
 
254
      xcolor.flags = DoRed | DoGreen | DoBlue;
 
255
      if (!XAllocColor( dpy, cmap, &xcolor )) {
 
256
         printf("Couldn't allocate black!\n");
 
257
         exit(1);
 
258
      }
 
259
      Black = xcolor.pixel;
 
260
 
 
261
      xcolor.red   = 0xffff;
 
262
      xcolor.green = 0x0;
 
263
      xcolor.blue  = 0x0;
 
264
      xcolor.flags = DoRed | DoGreen | DoBlue;
 
265
      if (!XAllocColor( dpy, cmap, &xcolor )) {
 
266
         printf("Couldn't allocate red!\n");
 
267
         exit(1);
 
268
      }
 
269
      Red = xcolor.pixel;
 
270
 
 
271
      xcolor.red   = 0x0;
 
272
      xcolor.green = 0xffff;
 
273
      xcolor.blue  = 0x0;
 
274
      xcolor.flags = DoRed | DoGreen | DoBlue;
 
275
      if (!XAllocColor( dpy, cmap, &xcolor )) {
 
276
         printf("Couldn't allocate green!\n");
 
277
         exit(1);
 
278
      }
 
279
      Green = xcolor.pixel;
 
280
 
 
281
      xcolor.red   = 0x0;
 
282
      xcolor.green = 0x0;
 
283
      xcolor.blue  = 0xffff;
 
284
      xcolor.flags = DoRed | DoGreen | DoBlue;
 
285
      if (!XAllocColor( dpy, cmap, &xcolor )) {
 
286
         printf("Couldn't allocate blue!\n");
 
287
         exit(1);
 
288
      }
 
289
      Blue = xcolor.pixel;
 
290
   }
 
291
 
 
292
   /* set window attributes */
 
293
   attr.colormap = cmap;
 
294
   attr.event_mask = ExposureMask | StructureNotifyMask;
 
295
   attr.border_pixel = BlackPixel( dpy, scr );
 
296
   attr.background_pixel = BlackPixel( dpy, scr );
 
297
   attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
 
298
 
 
299
   /* Create the window */
 
300
   win = XCreateWindow( dpy, root, x,y, width, height, 0,
 
301
                            visinfo->depth, InputOutput,
 
302
                            visinfo->visual,
 
303
                            attr_flags, &attr);
 
304
   if (!win) {
 
305
      printf("Couldn't open window!\n");
 
306
      exit(1);
 
307
   }
 
308
 
 
309
   XStringListToTextProperty(&title, 1, &tp);
 
310
   sh.flags = USPosition | USSize;
 
311
   XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
 
312
   XMapWindow(dpy, win);
 
313
   while (1) {
 
314
      XNextEvent( dpy, &e );
 
315
      if (e.type == MapNotify && e.xmap.window == win) {
 
316
         break;
 
317
      }
 
318
   }
 
319
 
 
320
 
 
321
   /*
 
322
    * Now do the special Mesa/Xlib stuff!
 
323
    */
 
324
 
 
325
   visual = XMesaCreateVisual( dpy, visinfo,
 
326
                              (GLboolean) color_flag,
 
327
                               GL_FALSE,  /* alpha_flag */
 
328
                               GL_FALSE,  /* db_flag */
 
329
                               GL_FALSE,  /* stereo flag */
 
330
                               GL_FALSE,  /* ximage_flag */
 
331
                               0,         /* depth size */
 
332
                               0,         /* stencil size */
 
333
                               0,0,0,0,   /* accum_size */
 
334
                               0,         /* num samples */
 
335
                               0,         /* level */
 
336
                               0          /* caveat */
 
337
                              );
 
338
   if (!visual) {
 
339
      printf("Couldn't create Mesa/X visual!\n");
 
340
      exit(1);
 
341
   }
 
342
 
 
343
   /* Create a Mesa rendering context */
 
344
   context = XMesaCreateContext( visual,
 
345
                                 NULL       /* share_list */
 
346
                               );
 
347
   if (!context) {
 
348
      printf("Couldn't create Mesa/X context!\n");
 
349
      exit(1);
 
350
   }
 
351
 
 
352
   buffer = XMesaCreateWindowBuffer( visual, win );
 
353
   if (!buffer) {
 
354
      printf("Couldn't create Mesa/X buffer!\n");
 
355
      exit(1);
 
356
   }
 
357
 
 
358
 
 
359
   XMesaMakeCurrent( context, buffer );
 
360
 
 
361
   /* Ready to render! */
 
362
}
 
363
 
 
364
 
 
365
 
 
366
static void draw_cube( void )
 
367
{
 
368
   /* X faces */
 
369
   glIndexi( Red );
 
370
   glColor3f( 1.0, 0.0, 0.0 );
 
371
   glBegin( GL_POLYGON );
 
372
   glVertex3f( 1.0, 1.0, 1.0 );
 
373
   glVertex3f( 1.0, -1.0, 1.0 );
 
374
   glVertex3f( 1.0, -1.0, -1.0 );
 
375
   glVertex3f( 1.0, 1.0, -1.0 );
 
376
   glEnd();
 
377
 
 
378
   glBegin( GL_POLYGON );
 
379
   glVertex3f( -1.0, 1.0, 1.0 );
 
380
   glVertex3f( -1.0, 1.0, -1.0 );
 
381
   glVertex3f( -1.0, -1.0, -1.0 );
 
382
   glVertex3f( -1.0, -1.0, 1.0 );
 
383
   glEnd();
 
384
 
 
385
   /* Y faces */
 
386
   glIndexi( Green );
 
387
   glColor3f( 0.0, 1.0, 0.0 );
 
388
   glBegin( GL_POLYGON );
 
389
   glVertex3f(  1.0, 1.0,  1.0 );
 
390
   glVertex3f(  1.0, 1.0, -1.0 );
 
391
   glVertex3f( -1.0, 1.0, -1.0 );
 
392
   glVertex3f( -1.0, 1.0,  1.0 );
 
393
   glEnd();
 
394
 
 
395
   glBegin( GL_POLYGON );
 
396
   glVertex3f(  1.0, -1.0,  1.0 );
 
397
   glVertex3f( -1.0, -1.0,  1.0 );
 
398
   glVertex3f( -1.0, -1.0, -1.0 );
 
399
   glVertex3f(  1.0, -1.0, -1.0 );
 
400
   glEnd();
 
401
 
 
402
   /* Z faces */
 
403
   glIndexi( Blue );
 
404
   glColor3f( 0.0, 0.0, 1.0 );
 
405
   glBegin( GL_POLYGON );
 
406
   glVertex3f(  1.0,  1.0,  1.0 );
 
407
   glVertex3f( -1.0,  1.0,  1.0 );
 
408
   glVertex3f( -1.0, -1.0,  1.0 );
 
409
   glVertex3f(  1.0, -1.0,  1.0 );
 
410
   glEnd();
 
411
 
 
412
   glBegin( GL_POLYGON );
 
413
   glVertex3f(  1.0, 1.0, -1.0 );
 
414
   glVertex3f(  1.0,-1.0, -1.0 );
 
415
   glVertex3f( -1.0,-1.0, -1.0 );
 
416
   glVertex3f( -1.0, 1.0, -1.0 );
 
417
   glEnd();
 
418
}
 
419
 
 
420
 
 
421
 
 
422
 
 
423
static void display_loop( void )
 
424
{
 
425
   GLfloat xrot, yrot, zrot;
 
426
 
 
427
   xrot = yrot = zrot = 0.0;
 
428
 
 
429
   glClearColor( 0.0, 0.0, 0.0, 0.0 );
 
430
   glClearIndex( Black );
 
431
 
 
432
   glMatrixMode( GL_PROJECTION );
 
433
   glLoadIdentity();
 
434
   glFrustum( -1.0, 1.0,  -1.0, 1.0,  1.0, 10.0 );
 
435
   glTranslatef( 0.0, 0.0, -5.0 );
 
436
 
 
437
   glMatrixMode( GL_MODELVIEW );
 
438
   glLoadIdentity();
 
439
 
 
440
   glCullFace( GL_BACK );
 
441
   glEnable( GL_CULL_FACE );
 
442
 
 
443
   glShadeModel( GL_FLAT );
 
444
 
 
445
   while (1) {
 
446
      glClear( GL_COLOR_BUFFER_BIT );
 
447
      glPushMatrix();
 
448
      glRotatef( xrot, 1.0, 0.0, 0.0 );
 
449
      glRotatef( yrot, 0.0, 1.0, 0.0 );
 
450
      glRotatef( zrot, 0.0, 0.0, 1.0 );
 
451
 
 
452
      draw_cube();
 
453
 
 
454
      glPopMatrix();
 
455
      glFinish();
 
456
 
 
457
      xrot += 10.0;
 
458
      yrot += 7.0;
 
459
      zrot -= 3.0;
 
460
   }
 
461
 
 
462
}
 
463
 
 
464
 
 
465
 
 
466
 
 
467
int main( int argc, char *argv[] )
 
468
{
 
469
   int mode = 0;
 
470
 
 
471
   if (argc >= 2)
 
472
   {
 
473
        if (strcmp(argv[1],"-ci")==0)
 
474
           mode = 0;
 
475
        else if (strcmp(argv[1],"-rgb")==0)
 
476
           mode = 1;
 
477
        else
 
478
        {
 
479
           printf("Bad flag: %s\n", argv[1]);
 
480
           printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
 
481
           exit(1);
 
482
        }
 
483
   }
 
484
   else
 
485
   {
 
486
        printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
 
487
        printf("Defaulting to  8-bit color index\n");
 
488
   }
 
489
 
 
490
   make_window( argv[0], mode );
 
491
 
 
492
   display_loop();
 
493
   return 0;
 
494
}
 
495
 
 
496
 
 
497
#endif