2 * Copyright (c) 2003 by the XFree86 Project, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * Create a window and use the DMX extension to query the window's
30 * back-end properties. Display the info inside the window itself.
41 #include <X11/Xutil.h>
42 #include <X11/extensions/dmxext.h>
44 static const char *FontName
= "fixed";
47 EventLoop(Display
* dpy
, Window win
, GC gc
)
61 int numScreens
, count
, i
;
62 DMXWindowAttributes
*winInfo
;
64 const char *msg
= "DMX window info:";
66 DMXGetScreenCount(dpy
, &numScreens
);
67 winInfo
= (DMXWindowAttributes
*)
68 malloc(numScreens
* sizeof(DMXWindowAttributes
));
70 if (!DMXGetWindowAttributes(dpy
, win
, &count
, numScreens
, winInfo
)) {
71 printf("Could not get window information for 0x%08lx\n",
75 XClearWindow(dpy
, win
);
76 XDrawString(dpy
, win
, gc
, x
, y
, msg
, strlen(msg
));
78 for (i
= 0; i
< count
; i
++) {
81 snprintf(str
, sizeof(str
),
82 "screen %d: pos: %dx%d+%d+%d visible: %dx%d+%d+%d",
84 winInfo
[i
].pos
.width
, winInfo
[i
].pos
.height
,
85 winInfo
[i
].pos
.x
, winInfo
[i
].pos
.y
,
86 winInfo
[i
].vis
.width
, winInfo
[i
].vis
.height
,
87 winInfo
[i
].vis
.x
, winInfo
[i
].vis
.y
);
88 XDrawString(dpy
, win
, gc
, x
, y
, str
, strlen(str
));
95 printf("Event type 0x%x\n", ev
.type
);
101 main(int argc
, char *argv
[])
103 const char *displayName
= NULL
;
105 int event_base
, error_base
;
107 long vinfoMask
, attrMask
;
108 XVisualInfo vinfoTemp
, *visInfo
;
111 XSetWindowAttributes attr
;
112 XFontStruct
*fontInfo
;
116 displayName
= argv
[1];
119 dpy
= XOpenDisplay(displayName
);
121 fprintf(stderr
, "Unable to open display %s\n", displayName
);
125 if (!DMXQueryExtension(dpy
, &event_base
, &error_base
)) {
126 fprintf(stderr
, "DMX extension not available on this display.\n");
130 scr
= DefaultScreen(dpy
);
131 root
= RootWindow(dpy
, scr
);
132 vis
= DefaultVisual(dpy
, scr
);
134 vinfoMask
= VisualIDMask
;
135 vinfoTemp
.visualid
= vis
->visualid
;
136 visInfo
= XGetVisualInfo(dpy
, vinfoMask
, &vinfoTemp
, &n
);
137 if (!visInfo
|| n
!= 1) {
138 fprintf(stderr
, "Unable to get visual!\n");
143 attr
.background_pixel
= 0;
144 attr
.border_pixel
= 0;
145 attr
.colormap
= XCreateColormap(dpy
, root
, visInfo
->visual
, AllocNone
);
146 attr
.event_mask
= StructureNotifyMask
| ExposureMask
;
147 attrMask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
149 win
= XCreateWindow(dpy
, root
, 500, 500, 600, 400, /* x, y, w, h */
150 0, /* border_width */
151 visInfo
->depth
, InputOutput
,
152 visInfo
->visual
, attrMask
, &attr
);
155 fprintf(stderr
, "Unable to create window!\n");
160 fontInfo
= XLoadQueryFont(dpy
, FontName
);
162 fprintf(stderr
, "Error: font %s not found\n", FontName
);
166 gc
= XCreateGC(dpy
, win
, 0, NULL
);
167 XSetBackground(dpy
, gc
, BlackPixel(dpy
, scr
));
168 XSetForeground(dpy
, gc
, WhitePixel(dpy
, scr
));
169 XSetFont(dpy
, gc
, fontInfo
->fid
);
171 XMapWindow(dpy
, win
);
173 EventLoop(dpy
, win
, gc
);
175 XDestroyWindow(dpy
, win
);
183 make_window(char *title
, int color_flag
)
185 int x
= 10, y
= 10, width
= 400, height
= 300;
192 XVisualInfo
*visinfo
;
193 XSetWindowAttributes attr
;
197 XMesaContext context
;
202 * Do the usual X things to make a window.
205 dpy
= XOpenDisplay(NULL
);
207 printf("Couldn't open default display!\n");
211 scr
= DefaultScreen(dpy
);
212 root
= RootWindow(dpy
, scr
);
214 /* alloc visinfo struct */
215 visinfo
= (XVisualInfo
*) malloc(sizeof(XVisualInfo
));
217 /* Get a visual and colormap */
219 /* Open TrueColor window */
222 if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
223 printf("Couldn't get 24-bit TrueColor visual!\n");
227 if (!XMatchVisualInfo(dpy
, scr
, 8, PseudoColor
, visinfo
)) {
228 printf("Couldn't get 8-bit PseudoColor visual!\n");
232 cmap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
233 Black
= Red
= Green
= Blue
= 0;
236 /* Open color index window */
238 if (!XMatchVisualInfo(dpy
, scr
, 8, PseudoColor
, visinfo
)) {
239 printf("Couldn't get 8-bit PseudoColor visual\n");
243 cmap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
245 /* Allocate colors */
249 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
250 if (!XAllocColor(dpy
, cmap
, &xcolor
)) {
251 printf("Couldn't allocate black!\n");
254 Black
= xcolor
.pixel
;
259 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
260 if (!XAllocColor(dpy
, cmap
, &xcolor
)) {
261 printf("Couldn't allocate red!\n");
267 xcolor
.green
= 0xffff;
269 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
270 if (!XAllocColor(dpy
, cmap
, &xcolor
)) {
271 printf("Couldn't allocate green!\n");
274 Green
= xcolor
.pixel
;
278 xcolor
.blue
= 0xffff;
279 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
280 if (!XAllocColor(dpy
, cmap
, &xcolor
)) {
281 printf("Couldn't allocate blue!\n");
287 /* set window attributes */
288 attr
.colormap
= cmap
;
289 attr
.event_mask
= ExposureMask
| StructureNotifyMask
;
290 attr
.border_pixel
= BlackPixel(dpy
, scr
);
291 attr
.background_pixel
= BlackPixel(dpy
, scr
);
292 attr_flags
= CWColormap
| CWEventMask
| CWBorderPixel
| CWBackPixel
;
294 /* Create the window */
295 win
= XCreateWindow(dpy
, root
, x
, y
, width
, height
, 0,
296 visinfo
->depth
, InputOutput
,
297 visinfo
->visual
, attr_flags
, &attr
);
299 printf("Couldn't open window!\n");
303 XStringListToTextProperty(&title
, 1, &tp
);
304 sh
.flags
= USPosition
| USSize
;
305 XSetWMProperties(dpy
, win
, &tp
, &tp
, 0, 0, &sh
, 0, 0);
306 XMapWindow(dpy
, win
);
309 if (e
.type
== MapNotify
&& e
.xmap
.window
== win
) {
315 * Now do the special Mesa/Xlib stuff!
318 visual
= XMesaCreateVisual(dpy
, visinfo
, (GLboolean
) color_flag
, GL_FALSE
, /* alpha_flag */
319 GL_FALSE
, /* db_flag */
320 GL_FALSE
, /* stereo flag */
321 GL_FALSE
, /* ximage_flag */
323 0, /* stencil size */
324 0, 0, 0, 0, /* accum_size */
330 printf("Couldn't create Mesa/X visual!\n");
334 /* Create a Mesa rendering context */
335 context
= XMesaCreateContext(visual
, NULL
/* share_list */
338 printf("Couldn't create Mesa/X context!\n");
342 buffer
= XMesaCreateWindowBuffer(visual
, win
);
344 printf("Couldn't create Mesa/X buffer!\n");
348 XMesaMakeCurrent(context
, buffer
);
350 /* Ready to render! */
358 glColor3f(1.0, 0.0, 0.0);
360 glVertex3f(1.0, 1.0, 1.0);
361 glVertex3f(1.0, -1.0, 1.0);
362 glVertex3f(1.0, -1.0, -1.0);
363 glVertex3f(1.0, 1.0, -1.0);
367 glVertex3f(-1.0, 1.0, 1.0);
368 glVertex3f(-1.0, 1.0, -1.0);
369 glVertex3f(-1.0, -1.0, -1.0);
370 glVertex3f(-1.0, -1.0, 1.0);
375 glColor3f(0.0, 1.0, 0.0);
377 glVertex3f(1.0, 1.0, 1.0);
378 glVertex3f(1.0, 1.0, -1.0);
379 glVertex3f(-1.0, 1.0, -1.0);
380 glVertex3f(-1.0, 1.0, 1.0);
384 glVertex3f(1.0, -1.0, 1.0);
385 glVertex3f(-1.0, -1.0, 1.0);
386 glVertex3f(-1.0, -1.0, -1.0);
387 glVertex3f(1.0, -1.0, -1.0);
392 glColor3f(0.0, 0.0, 1.0);
394 glVertex3f(1.0, 1.0, 1.0);
395 glVertex3f(-1.0, 1.0, 1.0);
396 glVertex3f(-1.0, -1.0, 1.0);
397 glVertex3f(1.0, -1.0, 1.0);
401 glVertex3f(1.0, 1.0, -1.0);
402 glVertex3f(1.0, -1.0, -1.0);
403 glVertex3f(-1.0, -1.0, -1.0);
404 glVertex3f(-1.0, 1.0, -1.0);
411 GLfloat xrot
, yrot
, zrot
;
413 xrot
= yrot
= zrot
= 0.0;
415 glClearColor(0.0, 0.0, 0.0, 0.0);
418 glMatrixMode(GL_PROJECTION
);
420 glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
421 glTranslatef(0.0, 0.0, -5.0);
423 glMatrixMode(GL_MODELVIEW
);
427 glEnable(GL_CULL_FACE
);
429 glShadeModel(GL_FLAT
);
432 glClear(GL_COLOR_BUFFER_BIT
);
434 glRotatef(xrot
, 1.0, 0.0, 0.0);
435 glRotatef(yrot
, 0.0, 1.0, 0.0);
436 glRotatef(zrot
, 0.0, 0.0, 1.0);
451 main(int argc
, char *argv
[])
456 if (strcmp(argv
[1], "-ci") == 0)
458 else if (strcmp(argv
[1], "-rgb") == 0)
461 printf("Bad flag: %s\n", argv
[1]);
462 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
467 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
468 printf("Defaulting to 8-bit color index\n");
471 make_window(argv
[0], mode
);