ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
(Generate patch)

Comparing ray/src/rt/x11.c (file contents):
Revision 2.14 by greg, Mon Nov 22 11:33:02 1993 UTC vs.
Revision 2.32 by schorsch, Tue Mar 30 16:13:01 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  x11.c - driver for X-windows version 11
9 *
10 *     Jan 1990
6   */
7  
8 < #include  <stdio.h>
9 < #include  <math.h>
8 > #include "copyright.h"
9 >
10 > #include  "standard.h"
11   #include  <sys/ioctl.h>
12 + #ifdef sparc
13 + #include  <sys/conf.h>
14 + #include  <sys/file.h>
15 + #include  <sys/filio.h>
16 + #endif
17 + #if  !defined(FNDELAY) && defined(O_NONBLOCK)
18 + #define  FNDELAY  O_NONBLOCK
19 + #endif
20  
21   #include  <X11/Xlib.h>
22   #include  <X11/cursorfont.h>
23   #include  <X11/Xutil.h>
24  
25 + #include  "platform.h"
26   #include  "color.h"
27   #include  "driver.h"
28   #include  "x11twind.h"
# Line 58 | Line 63 | static Cursor  pickcursor = 0;         /* cursor used for pic
63  
64   static int  gwidth, gheight;            /* graphics window size */
65  
66 + static int  comheight;                  /* desired comline height */
67   static TEXTWIND  *comline = NULL;       /* our command line */
68  
69   static char  c_queue[64];               /* input queue */
# Line 68 | Line 74 | static GC  ourgc = 0;                  /* our graphics context for dr
74  
75   static Colormap ourmap = 0;             /* our color map */
76  
77 < extern char  *malloc(), *getcombuf();
77 > #define IC_X11          0
78 > #define IC_IOCTL        1
79 > #define IC_READ         2
80  
81 < static int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
74 <                x11_getcur(), x11_comout(), x11_comin(), x11_flush();
81 > static int  inpcheck;                   /* whence to check input */
82  
83 + static void x11_errout(char  *msg);
84 +
85 + static dr_closef_t x11_close;
86 + static dr_clearf_t x11_clear;
87 + static dr_paintrf_t x11_paintr;
88 + static dr_getcurf_t x11_getcur;
89 + static dr_comoutf_t x11_comout;
90 + static dr_cominf_t x11_comin;
91 + static dr_flushf_t x11_flush;
92 +
93 + static dr_cominf_t std_comin;
94 + static dr_comoutf_t std_comout;
95 +
96   static struct driver  x11_driver = {
97          x11_close, x11_clear, x11_paintr, x11_getcur,
98 <        x11_comout, x11_comin, x11_flush, 1.0
98 >        NULL, NULL, x11_flush, 1.0
99   };
100  
101 < static int  getpixels(), xnewcolr(), freepixels(), resizewindow(),
82 <                getevent(), getkey(), fixwindow(), x11_getc();
83 < static unsigned long  true_pixel();
101 > static dr_getchf_t x11_getc;
102  
103 + static void freepixels(void);
104 + static int getpixels(void);
105 + static dr_newcolrf_t xnewcolr;
106 + static unsigned long true_pixel(COLOR  col);
107 + static void getevent(void);
108 + static void getkey(XKeyPressedEvent  *ekey);
109 + static void fixwindow(XExposeEvent  *eexp);
110 + static void resizewindow(XConfigureEvent  *ersz);
111  
112 < struct driver *
113 < x11_init(name, id)              /* initialize driver */
114 < char  *name, *id;
112 > extern dr_initf_t x11_init; /* XXX this should be in a seperate header file */
113 >
114 >
115 > extern struct driver *
116 > x11_init(               /* initialize driver */
117 >        char  *name,
118 >        char  *id
119 > )
120   {
90        extern char  *getenv();
121          char  *gv;
122          int  nplanes;
123          XSetWindowAttributes    ourwinattr;
124          XWMHints  ourxwmhints;
125          XSizeHints      oursizhints;
126 <
126 >                                        /* open display server */
127          ourdisplay = XOpenDisplay(NULL);
128          if (ourdisplay == NULL) {
129 <                stderr_v("cannot open X-windows; DISPLAY variable set?\n");
129 >                eputs("cannot open X-windows; DISPLAY variable set?\n");
130                  return(NULL);
131          }
132                                          /* find a usable visual */
133          nplanes = DisplayPlanes(ourdisplay, ourscreen);
134          if (XMatchVisualInfo(ourdisplay,ourscreen,
135 <                                24,TrueColor,&ourvinfo) ||
135 >                                nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
136                          XMatchVisualInfo(ourdisplay,ourscreen,
137 <                                24,DirectColor,&ourvinfo)) {
137 >                                nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
138                  ourblack = 0;
139                  ourwhite = ourvinfo.red_mask |
140                                  ourvinfo.green_mask |
141                                  ourvinfo.blue_mask ;
142          } else {
143                  if (nplanes < 4) {
144 <                        stderr_v("not enough colors\n");
144 >                        eputs("not enough colors\n");
145                          return(NULL);
146                  }
147                  if (!XMatchVisualInfo(ourdisplay,ourscreen,
148                                          nplanes,PseudoColor,&ourvinfo) &&
149                                  !XMatchVisualInfo(ourdisplay,ourscreen,
150                                          nplanes,GrayScale,&ourvinfo)) {
151 <                        stderr_v("unsupported visual type\n");
151 >                        eputs("unsupported visual type\n");
152                          return(NULL);
153                  }
154                  ourblack = BlackPixel(ourdisplay,ourscreen);
# Line 126 | Line 156 | char  *name, *id;
156          }
157                                          /* set gamma */
158          if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
159 <                        || (gv = getenv("GAMMA")) != NULL)
159 >                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
160                  make_gmap(atof(gv));
161          else
162                  make_gmap(GAMMA);
163 +                                        /* X11 command line or no? */
164 +        if (!strcmp(name, "x11"))
165 +                comheight = COMHEIGHT;
166 +        else /* "x11d" */ {
167 +                comheight = 0;
168 + #ifndef  FNDELAY
169 +                eputs("warning: x11d driver not fully functional on this machine\n");
170 + #endif
171 +        }
172                                          /* open window */
173          ourwinattr.background_pixel = ourblack;
174          ourwinattr.border_pixel = ourblack;
# Line 142 | Line 181 | char  *name, *id;
181                  BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
182                  CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
183          if (gwind == 0) {
184 <                stderr_v("cannot create window\n");
184 >                eputs("cannot create window\n");
185                  return(NULL);
186          }
187          XStoreName(ourdisplay, gwind, id);
# Line 155 | Line 194 | char  *name, *id;
194                          gwind, x11icon_bits, x11icon_width, x11icon_height);
195          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
196          oursizhints.min_width = MINWIDTH;
197 <        oursizhints.min_height = MINHEIGHT+COMHEIGHT;
197 >        oursizhints.min_height = MINHEIGHT+comheight;
198          oursizhints.flags = PMinSize;
199          XSetNormalHints(ourdisplay, gwind, &oursizhints);
200          XSelectInput(ourdisplay, gwind, ExposureMask);
201          XMapWindow(ourdisplay, gwind);
202          XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
203          gwidth = levptr(XExposeEvent)->width;
204 <        gheight = levptr(XExposeEvent)->height - COMHEIGHT;
204 >        gheight = levptr(XExposeEvent)->height - comheight;
205          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
206          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
207          x11_driver.inpready = 0;
208          mapped = 1;
209 <        cmdvec = x11_comout;                    /* set error vectors */
210 <        if (wrnvec != NULL)
211 <                wrnvec = x11_errout;
209 >                                        /* set i/o vectors */
210 >        if (comheight) {
211 >                x11_driver.comin = x11_comin;
212 >                x11_driver.comout = x11_comout;
213 >                erract[COMMAND].pf = x11_comout;
214 >                if (erract[WARNING].pf != NULL)
215 >                        erract[WARNING].pf = x11_errout;
216 >                inpcheck = IC_X11;
217 >        } else {
218 >                x11_driver.comin = std_comin;
219 >                x11_driver.comout = std_comout;
220 >                erract[COMMAND].pf = std_comout;
221 >                inpcheck = IC_IOCTL;
222 >        }
223          return(&x11_driver);
224   }
225  
226  
227 < static
228 < x11_close()                     /* close our display */
227 > static void
228 > x11_close(void)                 /* close our display */
229   {
230 <        cmdvec = NULL;                          /* reset error vectors */
231 <        if (wrnvec != NULL)
232 <                wrnvec = stderr_v;
230 >        erract[COMMAND].pf = NULL;              /* reset error vectors */
231 >        if (erract[WARNING].pf != NULL)
232 >                erract[WARNING].pf = wputs;
233          if (ourdisplay == NULL)
234                  return;
235          if (comline != NULL) {
# Line 197 | Line 247 | x11_close()                    /* close our display */
247   }
248  
249  
250 < static
251 < x11_clear(xres, yres)                   /* clear our display */
252 < int  xres, yres;
250 > static void
251 > x11_clear(                      /* clear our display */
252 >        int  xres,
253 >        int  yres
254 > )
255   {
256                                                  /* check limits */
257          if (xres < MINWIDTH)
# Line 209 | Line 261 | int  xres, yres;
261                                                  /* resize window */
262          if (xres != gwidth || yres != gheight) {
263                  XSelectInput(ourdisplay, gwind, 0);
264 <                XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
264 >                XResizeWindow(ourdisplay, gwind, xres, yres+comheight);
265                  gwidth = xres;
266                  gheight = yres;
267                  XFlush(ourdisplay);
# Line 218 | Line 270 | int  xres, yres;
270          }
271          XClearWindow(ourdisplay, gwind);
272                                                  /* reinitialize color table */
273 <        if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale)
273 >        if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) {
274                  if (getpixels() == 0)
275 <                        stderr_v("cannot allocate colors\n");
275 >                        eputs("cannot allocate colors\n");
276                  else
277                          new_ctab(ncolors);
278 +        }
279                                                  /* get new command line */
280          if (comline != NULL)
281                  xt_close(comline);
282 <        comline = xt_open(ourdisplay, gwind, 0, gheight,
283 <                        gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN);
284 <        if (comline == NULL) {
285 <                stderr_v("Cannot open command line window\n");
286 <                quit(1);
287 <        }
288 <        XSelectInput(ourdisplay, comline->w, ExposureMask);
282 >        if (comheight) {
283 >                comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth,
284 >                                comheight, 0, ourblack, ourwhite, COMFN);
285 >                if (comline == NULL) {
286 >                        eputs("cannot open command line window\n");
287 >                        quit(1);
288 >                }
289 >                XSelectInput(ourdisplay, comline->w, ExposureMask);
290                                                  /* remove earmuffs */
291 <        XSelectInput(ourdisplay, gwind,
291 >                XSelectInput(ourdisplay, gwind,
292                  StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
293 +        } else                                  /* remove earmuffs */
294 +                XSelectInput(ourdisplay, gwind,
295 +                        StructureNotifyMask|ExposureMask|ButtonPressMask);
296   }
297  
298  
299 < static
300 < x11_paintr(col, xmin, ymin, xmax, ymax)         /* fill a rectangle */
301 < COLOR  col;
302 < int  xmin, ymin, xmax, ymax;
299 > static void
300 > x11_paintr(             /* fill a rectangle */
301 >        COLOR  col,
302 >        int  xmin,
303 >        int  ymin,
304 >        int  xmax,
305 >        int  ymax
306 > )
307   {
308          unsigned long  pixel;
309  
# Line 258 | Line 319 | int  xmin, ymin, xmax, ymax;
319   }
320  
321  
322 < static
323 < x11_flush()                     /* flush output */
322 > static void
323 > x11_flush(void)                 /* flush output */
324   {
325 +        char    buf[256];
326 +        int     n;
327 +                                                /* check for input */
328          XNoOp(ourdisplay);
329 <        while (XPending(ourdisplay) > 0)
329 >        n = XPending(ourdisplay);                       /* from X server */
330 >        while (n-- > 0)
331                  getevent();
332 + #ifdef FNDELAY
333 +        if (inpcheck == IC_IOCTL) {                     /* from stdin */
334 + #ifdef FIONREAD
335 +                if (ioctl(fileno(stdin), FIONREAD, &n) < 0) {
336 + #else
337 +                if (1) {
338 + #endif
339 +                        if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) {
340 +                                eputs("cannot change input mode\n");
341 +                                quit(1);
342 +                        }
343 +                        inpcheck = IC_READ;
344 +                } else
345 +                        x11_driver.inpready += n;
346 +        }
347 +        if (inpcheck == IC_READ) {
348 +                n = read(fileno(stdin), buf, sizeof(buf)-1);
349 +                if (n > 0) {
350 +                        buf[n] = '\0';
351 +                        tocombuf(buf, &x11_driver);
352 +                }
353 +        }
354 + #endif
355   }
356  
357  
358 < static
359 < x11_comin(inp, prompt)          /* read in a command line */
360 < char  *inp, *prompt;
358 > static void
359 > x11_comin(              /* read in a command line */
360 >        char  *inp,
361 >        char  *prompt
362 > )
363   {
364 <        if (prompt != NULL)
364 >        if (prompt != NULL) {
365 >                x11_flush();            /* make sure we get everything */
366                  if (fromcombuf(inp, &x11_driver))
367                          return;
368 <                else
369 <                        xt_puts(prompt, comline);
368 >                xt_puts(prompt, comline);
369 >        }
370          xt_cursor(comline, TBLKCURS);
371          editline(inp, x11_getc, x11_comout);
372          xt_cursor(comline, TNOCURS);
373   }
374  
375  
376 < static
377 < x11_comout(out)                 /* output a string to command line */
378 < char  *out;
376 > static void
377 > x11_comout(             /* output a string to command line */
378 >        char  *outp
379 > )
380   {
381 <        if (comline == NULL)
381 >        if (comline == NULL || outp == NULL || !outp[0])
382                  return;
383 <        xt_puts(out, comline);
384 <        if (out[strlen(out)-1] == '\n')
383 >        xt_puts(outp, comline);
384 >        if (outp[strlen(outp)-1] == '\n')
385                  XFlush(ourdisplay);
386   }
387  
388  
389 < static
390 < x11_errout(msg)                 /* output an error message */
391 < char  *msg;
389 > static void
390 > x11_errout(                     /* output an error message */
391 >        char  *msg
392 > )
393   {
394 <        stderr_v(msg);          /* send to stderr also! */
394 >        eputs(msg);             /* send to stderr also! */
395          x11_comout(msg);
396   }
397  
398  
399 + static void
400 + std_comin(              /* read in command line from stdin */
401 +        char  *inp,
402 +        char  *prompt
403 + )
404 + {
405 +        if (prompt != NULL) {
406 +                if (fromcombuf(inp, &x11_driver))
407 +                        return;
408 +                if (!x11_driver.inpready)
409 +                        std_comout(prompt);
410 +        }
411 + #ifdef FNDELAY
412 +        if (inpcheck == IC_READ) {      /* turn off FNDELAY */
413 +                if (fcntl(fileno(stdin), F_SETFL, 0) < 0) {
414 +                        eputs("cannot change input mode\n");
415 +                        quit(1);
416 +                }
417 +                inpcheck = IC_IOCTL;
418 +        }
419 + #endif
420 +        if (gets(inp) == NULL) {
421 +                strcpy(inp, "quit");
422 +                return;
423 +        }
424 +        x11_driver.inpready -= strlen(inp) + 1;
425 +        if (x11_driver.inpready < 0)
426 +                x11_driver.inpready = 0;
427 + }
428 +
429 +
430 + static void
431 + std_comout(             /* write out string to stdout */
432 +        char    *outp
433 + )
434 + {
435 +        fputs(outp, stdout);
436 +        fflush(stdout);
437 + }
438 +
439 +
440   static int
441 < x11_getcur(xp, yp)              /* get cursor position */
442 < int  *xp, *yp;
441 > x11_getcur(             /* get cursor position */
442 >        int  *xp,
443 >        int  *yp
444 > )
445   {
446          while (XGrabPointer(ourdisplay, gwind, True, ButtonPressMask,
447                          GrabModeAsync, GrabModeAsync, None, pickcursor,
# Line 332 | Line 468 | int  *xp, *yp;
468   }
469  
470  
471 < static
472 < xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
473 < int  ndx;
474 < int  r, g, b;
471 > static void
472 > xnewcolr(               /* enter a color into hardware table */
473 >        int  ndx,
474 >        int  r,
475 >        int  g,
476 >        int  b
477 > )
478   {
479          XColor  xcolor;
480  
# Line 350 | Line 489 | int  r, g, b;
489  
490  
491   static int
492 < getpixels()                             /* get the color map */
492 > getpixels(void)                         /* get the color map */
493   {
494          XColor  thiscolor;
495          register int  i, j;
# Line 372 | Line 511 | loop:
511                          return(ncolors = 0);
512                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
513                          break;
514 <                free((char *)pixval);
514 >                free((void *)pixval);
515                  pixval = NULL;
516          }
517          if (pixval == NULL) {
# Line 401 | Line 540 | loop:
540   }
541  
542  
543 < static
544 < freepixels()                            /* free our pixels */
543 > static void
544 > freepixels(void)                                /* free our pixels */
545   {
546          if (ncolors == 0)
547                  return;
548          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
549 <        free((char *)pixval);
549 >        free((void *)pixval);
550          pixval = NULL;
551          ncolors = 0;
552          if (ourmap != DefaultColormap(ourdisplay,ourscreen))
# Line 417 | Line 556 | freepixels()                           /* free our pixels */
556  
557  
558   static unsigned long
559 < true_pixel(col)                 /* return true pixel value for color */
560 < COLOR  col;
559 > true_pixel(                     /* return true pixel value for color */
560 >        COLOR  col
561 > )
562   {
563          unsigned long  rval;
564          BYTE  rgb[3];
# Line 432 | Line 572 | COLOR  col;
572  
573  
574   static int
575 < x11_getc()                      /* get a command character */
575 > x11_getc(void)                  /* get a command character */
576   {
577          while (c_last <= c_first) {
578                  c_first = c_last = 0;           /* reset */
# Line 443 | Line 583 | x11_getc()                     /* get a command character */
583   }
584  
585  
586 < static
587 < getevent()                      /* get next event */
586 > static void
587 > getevent(void)                  /* get next event */
588   {
589          XNextEvent(ourdisplay, levptr(XEvent));
590          switch (levptr(XEvent)->type) {
# Line 457 | Line 597 | getevent()                     /* get next event */
597                  break;
598          case MapNotify:
599                  if (ourvinfo.class == PseudoColor ||
600 <                                ourvinfo.class == GrayScale)
600 >                                ourvinfo.class == GrayScale) {
601                          if (getpixels() == 0)
602 <                                stderr_v("Cannot allocate colors\n");
602 >                                eputs("cannot allocate colors\n");
603                          else
604                                  new_ctab(ncolors);
605 +                }
606                  mapped = 1;
607                  break;
608          case Expose:
# Line 476 | Line 617 | getevent()                     /* get next event */
617   }
618  
619  
620 < static
621 < getkey(ekey)                            /* get input key */
622 < register XKeyPressedEvent  *ekey;
620 > static void
621 > getkey(                         /* get input key */
622 >        register XKeyPressedEvent  *ekey
623 > )
624   {
625          register int  n;
626  
# Line 489 | Line 631 | register XKeyPressedEvent  *ekey;
631   }
632  
633  
634 < static
635 < fixwindow(eexp)                         /* repair damage to window */
636 < register XExposeEvent  *eexp;
634 > static void
635 > fixwindow(                              /* repair damage to window */
636 >        register XExposeEvent  *eexp
637 > )
638   {
639 +        char  buf[80];
640 +
641          if (eexp->window == gwind) {
642 <                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
642 >                sprintf(buf, "repaint %d %d %d %d\n",
643                          eexp->x, gheight - eexp->y - eexp->height,
644                          eexp->x + eexp->width, gheight - eexp->y);
645 +                tocombuf(buf, &x11_driver);
646          } else if (eexp->window == comline->w) {
647                  if (eexp->count == 0)
648                          xt_redraw(comline);
# Line 504 | Line 650 | register XExposeEvent  *eexp;
650   }
651  
652  
653 < static
654 < resizewindow(ersz)                      /* resize window */
655 < register XConfigureEvent  *ersz;
653 > static void
654 > resizewindow(                   /* resize window */
655 >        register XConfigureEvent  *ersz
656 > )
657   {
658 <        if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
658 >        if (ersz->width == gwidth && ersz->height-comheight == gheight)
659                  return;
660  
661          gwidth = ersz->width;
662 <        gheight = ersz->height-COMHEIGHT;
662 >        gheight = ersz->height-comheight;
663          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
664          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
665  
666 <        strcpy(getcombuf(&x11_driver), "new\n");
666 >        tocombuf("new\n", &x11_driver);
667   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines