3
* Copyright (c) 2003 by the XFree86 Project, Inc.
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:
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.
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
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.
42
#include <X11/Xutil.h>
43
#include <X11/Xmu/SysUtil.h>
44
#include <X11/extensions/dmxext.h>
46
static const char *FontName = "fixed";
50
EventLoop(Display *dpy, Window win, GC gc)
54
XNextEvent( dpy, &ev );
63
int numScreens, count, i;
64
DMXWindowAttributes *winInfo;
66
const char *msg = "DMX window info:";
68
DMXGetScreenCount(dpy, &numScreens);
70
= (DMXWindowAttributes *)
71
malloc(numScreens * sizeof(DMXWindowAttributes));
73
if (!DMXGetWindowAttributes(dpy, win, &count,
74
numScreens, winInfo)) {
75
printf("Could not get window information for 0x%08lx\n",
79
XClearWindow(dpy, win);
80
XDrawString(dpy, win, gc, x, y, msg, strlen(msg));
82
for (i = 0; i < count; i++) {
84
XmuSnprintf(str, sizeof(str),
85
"screen %d: pos: %dx%d+%d+%d visible: %dx%d+%d+%d",
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));
98
printf("Event type 0x%x\n", ev.type);
104
main(int argc, char *argv[])
106
const char *displayName = NULL;
108
int event_base, error_base;
110
long vinfoMask, attrMask;
111
XVisualInfo vinfoTemp, *visInfo;
114
XSetWindowAttributes attr;
115
XFontStruct *fontInfo;
119
displayName = argv[1];
122
dpy = XOpenDisplay(displayName);
124
fprintf(stderr, "Unable to open display %s\n", displayName);
128
if (!DMXQueryExtension(dpy, &event_base, &error_base)) {
129
fprintf(stderr, "DMX extension not available on this display.\n");
133
scr = DefaultScreen(dpy);
134
root = RootWindow(dpy, scr);
135
vis = DefaultVisual(dpy, scr);
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");
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;
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);
160
fprintf(stderr, "Unable to create window!\n");
165
fontInfo = XLoadQueryFont(dpy, FontName);
167
fprintf(stderr, "Error: font %s not found\n", FontName);
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);
176
XMapWindow(dpy, win);
178
EventLoop(dpy, win, gc);
180
XDestroyWindow(dpy, win);
187
static void make_window( char *title, int color_flag )
189
int x = 10, y = 10, width = 400, height = 300;
196
XVisualInfo *visinfo;
197
XSetWindowAttributes attr;
201
XMesaContext context;
207
* Do the usual X things to make a window.
210
dpy = XOpenDisplay(NULL);
212
printf("Couldn't open default display!\n");
216
scr = DefaultScreen(dpy);
217
root = RootWindow(dpy, scr);
219
/* alloc visinfo struct */
220
visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
222
/* Get a visual and colormap */
224
/* Open TrueColor window */
227
if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
228
printf("Couldn't get 24-bit TrueColor visual!\n");
232
if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
233
printf("Couldn't get 8-bit PseudoColor visual!\n");
237
cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
238
Black = Red = Green = Blue = 0;
241
/* Open color index window */
243
if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
244
printf("Couldn't get 8-bit PseudoColor visual\n");
248
cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
250
/* Allocate colors */
254
xcolor.flags = DoRed | DoGreen | DoBlue;
255
if (!XAllocColor( dpy, cmap, &xcolor )) {
256
printf("Couldn't allocate black!\n");
259
Black = xcolor.pixel;
264
xcolor.flags = DoRed | DoGreen | DoBlue;
265
if (!XAllocColor( dpy, cmap, &xcolor )) {
266
printf("Couldn't allocate red!\n");
272
xcolor.green = 0xffff;
274
xcolor.flags = DoRed | DoGreen | DoBlue;
275
if (!XAllocColor( dpy, cmap, &xcolor )) {
276
printf("Couldn't allocate green!\n");
279
Green = xcolor.pixel;
283
xcolor.blue = 0xffff;
284
xcolor.flags = DoRed | DoGreen | DoBlue;
285
if (!XAllocColor( dpy, cmap, &xcolor )) {
286
printf("Couldn't allocate blue!\n");
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;
299
/* Create the window */
300
win = XCreateWindow( dpy, root, x,y, width, height, 0,
301
visinfo->depth, InputOutput,
305
printf("Couldn't open window!\n");
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);
314
XNextEvent( dpy, &e );
315
if (e.type == MapNotify && e.xmap.window == win) {
322
* Now do the special Mesa/Xlib stuff!
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 */
332
0, /* stencil size */
333
0,0,0,0, /* accum_size */
339
printf("Couldn't create Mesa/X visual!\n");
343
/* Create a Mesa rendering context */
344
context = XMesaCreateContext( visual,
345
NULL /* share_list */
348
printf("Couldn't create Mesa/X context!\n");
352
buffer = XMesaCreateWindowBuffer( visual, win );
354
printf("Couldn't create Mesa/X buffer!\n");
359
XMesaMakeCurrent( context, buffer );
361
/* Ready to render! */
366
static void draw_cube( void )
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 );
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 );
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 );
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 );
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 );
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 );
423
static void display_loop( void )
425
GLfloat xrot, yrot, zrot;
427
xrot = yrot = zrot = 0.0;
429
glClearColor( 0.0, 0.0, 0.0, 0.0 );
430
glClearIndex( Black );
432
glMatrixMode( GL_PROJECTION );
434
glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
435
glTranslatef( 0.0, 0.0, -5.0 );
437
glMatrixMode( GL_MODELVIEW );
440
glCullFace( GL_BACK );
441
glEnable( GL_CULL_FACE );
443
glShadeModel( GL_FLAT );
446
glClear( GL_COLOR_BUFFER_BIT );
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 );
467
int main( int argc, char *argv[] )
473
if (strcmp(argv[1],"-ci")==0)
475
else if (strcmp(argv[1],"-rgb")==0)
479
printf("Bad flag: %s\n", argv[1]);
480
printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
486
printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
487
printf("Defaulting to 8-bit color index\n");
490
make_window( argv[0], mode );