| 1 | /* |
| 2 | * Copyright (c) 2003 by the XFree86 Project, Inc. |
| 3 | * |
| 4 | * All Rights Reserved. |
| 5 | * |
| 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: |
| 13 | * |
| 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. |
| 17 | * |
| 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 |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 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. |
| 31 | * |
| 32 | * Brian Paul |
| 33 | * 23 Jan 2003 |
| 34 | */ |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <X11/Xlib.h> |
| 41 | #include <X11/Xutil.h> |
| 42 | #include <X11/extensions/dmxext.h> |
| 43 | |
| 44 | static const char *FontName = "fixed"; |
| 45 | |
| 46 | static void |
| 47 | EventLoop(Display * dpy, Window win, GC gc) |
| 48 | { |
| 49 | XEvent ev; |
| 50 | |
| 51 | while (1) { |
| 52 | XNextEvent(dpy, &ev); |
| 53 | switch (ev.type) { |
| 54 | case ReparentNotify: |
| 55 | break; |
| 56 | case MapNotify: |
| 57 | break; |
| 58 | case ConfigureNotify: |
| 59 | case Expose: |
| 60 | { |
| 61 | int numScreens, count, i; |
| 62 | DMXWindowAttributes *winInfo; |
| 63 | int x, y; |
| 64 | const char *msg = "DMX window info:"; |
| 65 | |
| 66 | DMXGetScreenCount(dpy, &numScreens); |
| 67 | winInfo = (DMXWindowAttributes *) |
| 68 | malloc(numScreens * sizeof(DMXWindowAttributes)); |
| 69 | assert(winInfo); |
| 70 | if (!DMXGetWindowAttributes(dpy, win, &count, numScreens, winInfo)) { |
| 71 | printf("Could not get window information for 0x%08lx\n", |
| 72 | (long unsigned) win); |
| 73 | } |
| 74 | x = y = 50; |
| 75 | XClearWindow(dpy, win); |
| 76 | XDrawString(dpy, win, gc, x, y, msg, strlen(msg)); |
| 77 | y += 20; |
| 78 | for (i = 0; i < count; i++) { |
| 79 | char str[500]; |
| 80 | |
| 81 | snprintf(str, sizeof(str), |
| 82 | "screen %d: pos: %dx%d+%d+%d visible: %dx%d+%d+%d", |
| 83 | winInfo[i].screen, |
| 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)); |
| 89 | y += 20; |
| 90 | } |
| 91 | free(winInfo); |
| 92 | } |
| 93 | break; |
| 94 | default: |
| 95 | printf("Event type 0x%x\n", ev.type); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | main(int argc, char *argv[]) |
| 102 | { |
| 103 | const char *displayName = NULL; |
| 104 | Display *dpy; |
| 105 | int event_base, error_base; |
| 106 | int scr, n; |
| 107 | long vinfoMask, attrMask; |
| 108 | XVisualInfo vinfoTemp, *visInfo; |
| 109 | Visual *vis; |
| 110 | Window win, root; |
| 111 | XSetWindowAttributes attr; |
| 112 | XFontStruct *fontInfo; |
| 113 | GC gc; |
| 114 | |
| 115 | if (argc > 1) { |
| 116 | displayName = argv[1]; |
| 117 | } |
| 118 | |
| 119 | dpy = XOpenDisplay(displayName); |
| 120 | if (!dpy) { |
| 121 | fprintf(stderr, "Unable to open display %s\n", displayName); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (!DMXQueryExtension(dpy, &event_base, &error_base)) { |
| 126 | fprintf(stderr, "DMX extension not available on this display.\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | scr = DefaultScreen(dpy); |
| 131 | root = RootWindow(dpy, scr); |
| 132 | vis = DefaultVisual(dpy, scr); |
| 133 | |
| 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"); |
| 139 | XCloseDisplay(dpy); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 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; |
| 148 | |
| 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); |
| 153 | |
| 154 | if (!win) { |
| 155 | fprintf(stderr, "Unable to create window!\n"); |
| 156 | XCloseDisplay(dpy); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | fontInfo = XLoadQueryFont(dpy, FontName); |
| 161 | if (!fontInfo) { |
| 162 | fprintf(stderr, "Error: font %s not found\n", FontName); |
| 163 | exit(0); |
| 164 | } |
| 165 | |
| 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); |
| 170 | |
| 171 | XMapWindow(dpy, win); |
| 172 | |
| 173 | EventLoop(dpy, win, gc); |
| 174 | |
| 175 | XDestroyWindow(dpy, win); |
| 176 | XCloseDisplay(dpy); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | #if 00 |
| 181 | |
| 182 | static void |
| 183 | make_window(char *title, int color_flag) |
| 184 | { |
| 185 | int x = 10, y = 10, width = 400, height = 300; |
| 186 | Display *dpy; |
| 187 | int scr; |
| 188 | Window root, win; |
| 189 | Colormap cmap; |
| 190 | XColor xcolor; |
| 191 | int attr_flags; |
| 192 | XVisualInfo *visinfo; |
| 193 | XSetWindowAttributes attr; |
| 194 | XTextProperty tp; |
| 195 | XSizeHints sh; |
| 196 | XEvent e; |
| 197 | XMesaContext context; |
| 198 | XMesaVisual visual; |
| 199 | XMesaBuffer buffer; |
| 200 | |
| 201 | /* |
| 202 | * Do the usual X things to make a window. |
| 203 | */ |
| 204 | |
| 205 | dpy = XOpenDisplay(NULL); |
| 206 | if (!dpy) { |
| 207 | printf("Couldn't open default display!\n"); |
| 208 | exit(1); |
| 209 | } |
| 210 | |
| 211 | scr = DefaultScreen(dpy); |
| 212 | root = RootWindow(dpy, scr); |
| 213 | |
| 214 | /* alloc visinfo struct */ |
| 215 | visinfo = (XVisualInfo *) malloc(sizeof(XVisualInfo)); |
| 216 | |
| 217 | /* Get a visual and colormap */ |
| 218 | if (color_flag) { |
| 219 | /* Open TrueColor window */ |
| 220 | |
| 221 | /* |
| 222 | if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) { |
| 223 | printf("Couldn't get 24-bit TrueColor visual!\n"); |
| 224 | exit(1); |
| 225 | } |
| 226 | */ |
| 227 | if (!XMatchVisualInfo(dpy, scr, 8, PseudoColor, visinfo)) { |
| 228 | printf("Couldn't get 8-bit PseudoColor visual!\n"); |
| 229 | exit(1); |
| 230 | } |
| 231 | |
| 232 | cmap = XCreateColormap(dpy, root, visinfo->visual, AllocNone); |
| 233 | Black = Red = Green = Blue = 0; |
| 234 | } |
| 235 | else { |
| 236 | /* Open color index window */ |
| 237 | |
| 238 | if (!XMatchVisualInfo(dpy, scr, 8, PseudoColor, visinfo)) { |
| 239 | printf("Couldn't get 8-bit PseudoColor visual\n"); |
| 240 | exit(1); |
| 241 | } |
| 242 | |
| 243 | cmap = XCreateColormap(dpy, root, visinfo->visual, AllocNone); |
| 244 | |
| 245 | /* Allocate colors */ |
| 246 | xcolor.red = 0x0; |
| 247 | xcolor.green = 0x0; |
| 248 | xcolor.blue = 0x0; |
| 249 | xcolor.flags = DoRed | DoGreen | DoBlue; |
| 250 | if (!XAllocColor(dpy, cmap, &xcolor)) { |
| 251 | printf("Couldn't allocate black!\n"); |
| 252 | exit(1); |
| 253 | } |
| 254 | Black = xcolor.pixel; |
| 255 | |
| 256 | xcolor.red = 0xffff; |
| 257 | xcolor.green = 0x0; |
| 258 | xcolor.blue = 0x0; |
| 259 | xcolor.flags = DoRed | DoGreen | DoBlue; |
| 260 | if (!XAllocColor(dpy, cmap, &xcolor)) { |
| 261 | printf("Couldn't allocate red!\n"); |
| 262 | exit(1); |
| 263 | } |
| 264 | Red = xcolor.pixel; |
| 265 | |
| 266 | xcolor.red = 0x0; |
| 267 | xcolor.green = 0xffff; |
| 268 | xcolor.blue = 0x0; |
| 269 | xcolor.flags = DoRed | DoGreen | DoBlue; |
| 270 | if (!XAllocColor(dpy, cmap, &xcolor)) { |
| 271 | printf("Couldn't allocate green!\n"); |
| 272 | exit(1); |
| 273 | } |
| 274 | Green = xcolor.pixel; |
| 275 | |
| 276 | xcolor.red = 0x0; |
| 277 | xcolor.green = 0x0; |
| 278 | xcolor.blue = 0xffff; |
| 279 | xcolor.flags = DoRed | DoGreen | DoBlue; |
| 280 | if (!XAllocColor(dpy, cmap, &xcolor)) { |
| 281 | printf("Couldn't allocate blue!\n"); |
| 282 | exit(1); |
| 283 | } |
| 284 | Blue = xcolor.pixel; |
| 285 | } |
| 286 | |
| 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; |
| 293 | |
| 294 | /* Create the window */ |
| 295 | win = XCreateWindow(dpy, root, x, y, width, height, 0, |
| 296 | visinfo->depth, InputOutput, |
| 297 | visinfo->visual, attr_flags, &attr); |
| 298 | if (!win) { |
| 299 | printf("Couldn't open window!\n"); |
| 300 | exit(1); |
| 301 | } |
| 302 | |
| 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); |
| 307 | while (1) { |
| 308 | XNextEvent(dpy, &e); |
| 309 | if (e.type == MapNotify && e.xmap.window == win) { |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Now do the special Mesa/Xlib stuff! |
| 316 | */ |
| 317 | |
| 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 */ |
| 322 | 0, /* depth size */ |
| 323 | 0, /* stencil size */ |
| 324 | 0, 0, 0, 0, /* accum_size */ |
| 325 | 0, /* num samples */ |
| 326 | 0, /* level */ |
| 327 | 0 /* caveat */ |
| 328 | ); |
| 329 | if (!visual) { |
| 330 | printf("Couldn't create Mesa/X visual!\n"); |
| 331 | exit(1); |
| 332 | } |
| 333 | |
| 334 | /* Create a Mesa rendering context */ |
| 335 | context = XMesaCreateContext(visual, NULL /* share_list */ |
| 336 | ); |
| 337 | if (!context) { |
| 338 | printf("Couldn't create Mesa/X context!\n"); |
| 339 | exit(1); |
| 340 | } |
| 341 | |
| 342 | buffer = XMesaCreateWindowBuffer(visual, win); |
| 343 | if (!buffer) { |
| 344 | printf("Couldn't create Mesa/X buffer!\n"); |
| 345 | exit(1); |
| 346 | } |
| 347 | |
| 348 | XMesaMakeCurrent(context, buffer); |
| 349 | |
| 350 | /* Ready to render! */ |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | draw_cube(void) |
| 355 | { |
| 356 | /* X faces */ |
| 357 | glIndexi(Red); |
| 358 | glColor3f(1.0, 0.0, 0.0); |
| 359 | glBegin(GL_POLYGON); |
| 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); |
| 364 | glEnd(); |
| 365 | |
| 366 | glBegin(GL_POLYGON); |
| 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); |
| 371 | glEnd(); |
| 372 | |
| 373 | /* Y faces */ |
| 374 | glIndexi(Green); |
| 375 | glColor3f(0.0, 1.0, 0.0); |
| 376 | glBegin(GL_POLYGON); |
| 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); |
| 381 | glEnd(); |
| 382 | |
| 383 | glBegin(GL_POLYGON); |
| 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); |
| 388 | glEnd(); |
| 389 | |
| 390 | /* Z faces */ |
| 391 | glIndexi(Blue); |
| 392 | glColor3f(0.0, 0.0, 1.0); |
| 393 | glBegin(GL_POLYGON); |
| 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); |
| 398 | glEnd(); |
| 399 | |
| 400 | glBegin(GL_POLYGON); |
| 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); |
| 405 | glEnd(); |
| 406 | } |
| 407 | |
| 408 | static void |
| 409 | display_loop(void) |
| 410 | { |
| 411 | GLfloat xrot, yrot, zrot; |
| 412 | |
| 413 | xrot = yrot = zrot = 0.0; |
| 414 | |
| 415 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 416 | glClearIndex(Black); |
| 417 | |
| 418 | glMatrixMode(GL_PROJECTION); |
| 419 | glLoadIdentity(); |
| 420 | glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0); |
| 421 | glTranslatef(0.0, 0.0, -5.0); |
| 422 | |
| 423 | glMatrixMode(GL_MODELVIEW); |
| 424 | glLoadIdentity(); |
| 425 | |
| 426 | glCullFace(GL_BACK); |
| 427 | glEnable(GL_CULL_FACE); |
| 428 | |
| 429 | glShadeModel(GL_FLAT); |
| 430 | |
| 431 | while (1) { |
| 432 | glClear(GL_COLOR_BUFFER_BIT); |
| 433 | glPushMatrix(); |
| 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); |
| 437 | |
| 438 | draw_cube(); |
| 439 | |
| 440 | glPopMatrix(); |
| 441 | glFinish(); |
| 442 | |
| 443 | xrot += 10.0; |
| 444 | yrot += 7.0; |
| 445 | zrot -= 3.0; |
| 446 | } |
| 447 | |
| 448 | } |
| 449 | |
| 450 | int |
| 451 | main(int argc, char *argv[]) |
| 452 | { |
| 453 | int mode = 0; |
| 454 | |
| 455 | if (argc >= 2) { |
| 456 | if (strcmp(argv[1], "-ci") == 0) |
| 457 | mode = 0; |
| 458 | else if (strcmp(argv[1], "-rgb") == 0) |
| 459 | mode = 1; |
| 460 | else { |
| 461 | printf("Bad flag: %s\n", argv[1]); |
| 462 | printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n"); |
| 463 | exit(1); |
| 464 | } |
| 465 | } |
| 466 | else { |
| 467 | printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n"); |
| 468 | printf("Defaulting to 8-bit color index\n"); |
| 469 | } |
| 470 | |
| 471 | make_window(argv[0], mode); |
| 472 | |
| 473 | display_loop(); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | #endif |