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.16 by greg, Mon Sep 12 14:41:54 1994 UTC vs.
Revision 2.29 by greg, Sat Feb 22 02:07:29 2003 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
6 + */
7 +
8 + /* ====================================================================
9 + * The Radiance Software License, Version 1.0
10   *
11 < *     Jan 1990
11 > * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 > * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 > *
14 > * Redistribution and use in source and binary forms, with or without
15 > * modification, are permitted provided that the following conditions
16 > * are met:
17 > *
18 > * 1. Redistributions of source code must retain the above copyright
19 > *         notice, this list of conditions and the following disclaimer.
20 > *
21 > * 2. Redistributions in binary form must reproduce the above copyright
22 > *       notice, this list of conditions and the following disclaimer in
23 > *       the documentation and/or other materials provided with the
24 > *       distribution.
25 > *
26 > * 3. The end-user documentation included with the redistribution,
27 > *           if any, must include the following acknowledgment:
28 > *             "This product includes Radiance software
29 > *                 (http://radsite.lbl.gov/)
30 > *                 developed by the Lawrence Berkeley National Laboratory
31 > *               (http://www.lbl.gov/)."
32 > *       Alternately, this acknowledgment may appear in the software itself,
33 > *       if and wherever such third-party acknowledgments normally appear.
34 > *
35 > * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 > *       and "The Regents of the University of California" must
37 > *       not be used to endorse or promote products derived from this
38 > *       software without prior written permission. For written
39 > *       permission, please contact [email protected].
40 > *
41 > * 5. Products derived from this software may not be called "Radiance",
42 > *       nor may "Radiance" appear in their name, without prior written
43 > *       permission of Lawrence Berkeley National Laboratory.
44 > *
45 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 > * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 > * SUCH DAMAGE.
57 > * ====================================================================
58 > *
59 > * This software consists of voluntary contributions made by many
60 > * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 > * information on Lawrence Berkeley National Laboratory, please see
62 > * <http://www.lbl.gov/>.
63   */
64  
65 < #include  <stdio.h>
14 < #include  <math.h>
65 > #include  "standard.h"
66   #include  <sys/ioctl.h>
67 + #ifdef sparc
68 + #include  <sys/conf.h>
69 + #include  <sys/file.h>
70 + #include  <sys/filio.h>
71 + #endif
72 + #if  !defined(FNDELAY) && defined(O_NONBLOCK)
73 + #define  FNDELAY  O_NONBLOCK
74 + #endif
75  
76   #include  <X11/Xlib.h>
77   #include  <X11/cursorfont.h>
# Line 58 | Line 117 | static Cursor  pickcursor = 0;         /* cursor used for pic
117  
118   static int  gwidth, gheight;            /* graphics window size */
119  
120 + static int  comheight;                  /* desired comline height */
121   static TEXTWIND  *comline = NULL;       /* our command line */
122  
123   static char  c_queue[64];               /* input queue */
# Line 68 | Line 128 | static GC  ourgc = 0;                  /* our graphics context for dr
128  
129   static Colormap ourmap = 0;             /* our color map */
130  
131 < extern char  *malloc(), *getcombuf();
131 > #define IC_X11          0
132 > #define IC_IOCTL        1
133 > #define IC_READ         2
134  
135 < static int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
74 <                x11_getcur(), x11_comout(), x11_comin(), x11_flush();
135 > static int  inpcheck;                   /* whence to check input */
136  
137 + static int      x11_getcur();
138 +
139 + static void  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
140 +                x11_comout(), x11_comin(), x11_flush();
141 +
142 + static void  std_comin(), std_comout();
143 +
144   static struct driver  x11_driver = {
145          x11_close, x11_clear, x11_paintr, x11_getcur,
146 <        x11_comout, x11_comin, x11_flush, 1.0
146 >        NULL, NULL, x11_flush, 1.0
147   };
148  
149 < static int  getpixels(), xnewcolr(), freepixels(), resizewindow(),
150 <                getevent(), getkey(), fixwindow(), x11_getc();
149 > static int  getpixels(), x11_getc();
150 > static void  xnewcolr(), freepixels(), resizewindow(),
151 >                getevent(), getkey(), fixwindow();
152   static unsigned long  true_pixel();
153  
154  
# Line 87 | Line 156 | struct driver *
156   x11_init(name, id)              /* initialize driver */
157   char  *name, *id;
158   {
90        extern char  *getenv();
159          char  *gv;
160          int  nplanes;
161          XSetWindowAttributes    ourwinattr;
162          XWMHints  ourxwmhints;
163          XSizeHints      oursizhints;
164 <
164 >                                        /* open display server */
165          ourdisplay = XOpenDisplay(NULL);
166          if (ourdisplay == NULL) {
167 <                stderr_v("cannot open X-windows; DISPLAY variable set?\n");
167 >                eputs("cannot open X-windows; DISPLAY variable set?\n");
168                  return(NULL);
169          }
170                                          /* find a usable visual */
171          nplanes = DisplayPlanes(ourdisplay, ourscreen);
172          if (XMatchVisualInfo(ourdisplay,ourscreen,
173 <                                24,TrueColor,&ourvinfo) ||
173 >                                nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
174                          XMatchVisualInfo(ourdisplay,ourscreen,
175 <                                24,DirectColor,&ourvinfo)) {
175 >                                nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
176                  ourblack = 0;
177                  ourwhite = ourvinfo.red_mask |
178                                  ourvinfo.green_mask |
179                                  ourvinfo.blue_mask ;
180          } else {
181                  if (nplanes < 4) {
182 <                        stderr_v("not enough colors\n");
182 >                        eputs("not enough colors\n");
183                          return(NULL);
184                  }
185                  if (!XMatchVisualInfo(ourdisplay,ourscreen,
186                                          nplanes,PseudoColor,&ourvinfo) &&
187                                  !XMatchVisualInfo(ourdisplay,ourscreen,
188                                          nplanes,GrayScale,&ourvinfo)) {
189 <                        stderr_v("unsupported visual type\n");
189 >                        eputs("unsupported visual type\n");
190                          return(NULL);
191                  }
192                  ourblack = BlackPixel(ourdisplay,ourscreen);
# Line 126 | Line 194 | char  *name, *id;
194          }
195                                          /* set gamma */
196          if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
197 <                        || (gv = getenv("GAMMA")) != NULL)
197 >                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
198                  make_gmap(atof(gv));
199          else
200                  make_gmap(GAMMA);
201 +                                        /* X11 command line or no? */
202 +        if (!strcmp(name, "x11"))
203 +                comheight = COMHEIGHT;
204 +        else /* "x11d" */ {
205 +                comheight = 0;
206 + #ifndef  FNDELAY
207 +                eputs("warning: x11d driver not fully functional on this machine\n");
208 + #endif
209 +        }
210                                          /* open window */
211          ourwinattr.background_pixel = ourblack;
212          ourwinattr.border_pixel = ourblack;
# Line 142 | Line 219 | char  *name, *id;
219                  BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
220                  CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
221          if (gwind == 0) {
222 <                stderr_v("cannot create window\n");
222 >                eputs("cannot create window\n");
223                  return(NULL);
224          }
225          XStoreName(ourdisplay, gwind, id);
# Line 155 | Line 232 | char  *name, *id;
232                          gwind, x11icon_bits, x11icon_width, x11icon_height);
233          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
234          oursizhints.min_width = MINWIDTH;
235 <        oursizhints.min_height = MINHEIGHT+COMHEIGHT;
235 >        oursizhints.min_height = MINHEIGHT+comheight;
236          oursizhints.flags = PMinSize;
237          XSetNormalHints(ourdisplay, gwind, &oursizhints);
238          XSelectInput(ourdisplay, gwind, ExposureMask);
239          XMapWindow(ourdisplay, gwind);
240          XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
241          gwidth = levptr(XExposeEvent)->width;
242 <        gheight = levptr(XExposeEvent)->height - COMHEIGHT;
242 >        gheight = levptr(XExposeEvent)->height - comheight;
243          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
244          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
245          x11_driver.inpready = 0;
246          mapped = 1;
247 <        cmdvec = x11_comout;                    /* set error vectors */
248 <        if (wrnvec != NULL)
249 <                wrnvec = x11_errout;
247 >                                        /* set i/o vectors */
248 >        if (comheight) {
249 >                x11_driver.comin = x11_comin;
250 >                x11_driver.comout = x11_comout;
251 >                erract[COMMAND].pf = x11_comout;
252 >                if (erract[WARNING].pf != NULL)
253 >                        erract[WARNING].pf = x11_errout;
254 >                inpcheck = IC_X11;
255 >        } else {
256 >                x11_driver.comin = std_comin;
257 >                x11_driver.comout = std_comout;
258 >                erract[COMMAND].pf = std_comout;
259 >                inpcheck = IC_IOCTL;
260 >        }
261          return(&x11_driver);
262   }
263  
264  
265 < static
265 > static void
266   x11_close()                     /* close our display */
267   {
268 <        cmdvec = NULL;                          /* reset error vectors */
269 <        if (wrnvec != NULL)
270 <                wrnvec = stderr_v;
268 >        erract[COMMAND].pf = NULL;              /* reset error vectors */
269 >        if (erract[WARNING].pf != NULL)
270 >                erract[WARNING].pf = wputs;
271          if (ourdisplay == NULL)
272                  return;
273          if (comline != NULL) {
# Line 197 | Line 285 | x11_close()                    /* close our display */
285   }
286  
287  
288 < static
288 > static void
289   x11_clear(xres, yres)                   /* clear our display */
290   int  xres, yres;
291   {
# Line 209 | Line 297 | int  xres, yres;
297                                                  /* resize window */
298          if (xres != gwidth || yres != gheight) {
299                  XSelectInput(ourdisplay, gwind, 0);
300 <                XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
300 >                XResizeWindow(ourdisplay, gwind, xres, yres+comheight);
301                  gwidth = xres;
302                  gheight = yres;
303                  XFlush(ourdisplay);
# Line 220 | Line 308 | int  xres, yres;
308                                                  /* reinitialize color table */
309          if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale)
310                  if (getpixels() == 0)
311 <                        stderr_v("cannot allocate colors\n");
311 >                        eputs("cannot allocate colors\n");
312                  else
313                          new_ctab(ncolors);
314                                                  /* get new command line */
315          if (comline != NULL)
316                  xt_close(comline);
317 <        comline = xt_open(ourdisplay, gwind, 0, gheight,
318 <                        gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN);
319 <        if (comline == NULL) {
320 <                stderr_v("Cannot open command line window\n");
321 <                quit(1);
322 <        }
323 <        XSelectInput(ourdisplay, comline->w, ExposureMask);
317 >        if (comheight) {
318 >                comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth,
319 >                                comheight, 0, ourblack, ourwhite, COMFN);
320 >                if (comline == NULL) {
321 >                        eputs("cannot open command line window\n");
322 >                        quit(1);
323 >                }
324 >                XSelectInput(ourdisplay, comline->w, ExposureMask);
325                                                  /* remove earmuffs */
326 <        XSelectInput(ourdisplay, gwind,
326 >                XSelectInput(ourdisplay, gwind,
327                  StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
328 +        } else                                  /* remove earmuffs */
329 +                XSelectInput(ourdisplay, gwind,
330 +                        StructureNotifyMask|ExposureMask|ButtonPressMask);
331   }
332  
333  
334 < static
334 > static void
335   x11_paintr(col, xmin, ymin, xmax, ymax)         /* fill a rectangle */
336   COLOR  col;
337   int  xmin, ymin, xmax, ymax;
# Line 258 | Line 350 | int  xmin, ymin, xmax, ymax;
350   }
351  
352  
353 < static
353 > static void
354   x11_flush()                     /* flush output */
355   {
356 +        char    buf[256];
357          int     n;
265        char    *buf;
358                                                  /* check for input */
359          XNoOp(ourdisplay);
360          n = XPending(ourdisplay);                       /* from X server */
361          while (n-- > 0)
362                  getevent();
363 <        if (ioctl(0, FIONREAD, &n) == 0 && n > 0) {     /* from stdin */
364 <                buf = getcombuf(&x11_driver);
365 <                n = read(0, buf, n);
366 <                if (n > 0)
363 > #ifdef FNDELAY
364 >        if (inpcheck == IC_IOCTL) {                     /* from stdin */
365 > #ifdef FIONREAD
366 >                if (ioctl(fileno(stdin), FIONREAD, &n) < 0) {
367 > #else
368 >                if (1) {
369 > #endif
370 >                        if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) {
371 >                                eputs("cannot change input mode\n");
372 >                                quit(1);
373 >                        }
374 >                        inpcheck = IC_READ;
375 >                } else
376 >                        x11_driver.inpready += n;
377 >        }
378 >        if (inpcheck == IC_READ) {
379 >                n = read(fileno(stdin), buf, sizeof(buf)-1);
380 >                if (n > 0) {
381                          buf[n] = '\0';
382 +                        tocombuf(buf, &x11_driver);
383 +                }
384          }
385 + #endif
386   }
387  
388  
389 < static
389 > static void
390   x11_comin(inp, prompt)          /* read in a command line */
391   char  *inp, *prompt;
392   {
# Line 293 | Line 402 | char  *inp, *prompt;
402   }
403  
404  
405 < static
406 < x11_comout(out)                 /* output a string to command line */
407 < char  *out;
405 > static void
406 > x11_comout(outp)                /* output a string to command line */
407 > char  *outp;
408   {
409 <        if (comline == NULL)
409 >        if (comline == NULL || outp == NULL || !outp[0])
410                  return;
411 <        xt_puts(out, comline);
412 <        if (out[strlen(out)-1] == '\n')
411 >        xt_puts(outp, comline);
412 >        if (outp[strlen(outp)-1] == '\n')
413                  XFlush(ourdisplay);
414   }
415  
416  
417 < static
417 > static void
418   x11_errout(msg)                 /* output an error message */
419   char  *msg;
420   {
421 <        stderr_v(msg);          /* send to stderr also! */
421 >        eputs(msg);             /* send to stderr also! */
422          x11_comout(msg);
423   }
424  
425  
426 + static void
427 + std_comin(inp, prompt)          /* read in command line from stdin */
428 + char  *inp, *prompt;
429 + {
430 +        if (prompt != NULL) {
431 +                if (fromcombuf(inp, &x11_driver))
432 +                        return;
433 +                if (!x11_driver.inpready)
434 +                        std_comout(prompt);
435 +        }
436 + #ifdef FNDELAY
437 +        if (inpcheck == IC_READ) {      /* turn off FNDELAY */
438 +                if (fcntl(fileno(stdin), F_SETFL, 0) < 0) {
439 +                        eputs("cannot change input mode\n");
440 +                        quit(1);
441 +                }
442 +                inpcheck = IC_IOCTL;
443 +        }
444 + #endif
445 +        if (gets(inp) == NULL) {
446 +                strcpy(inp, "quit");
447 +                return;
448 +        }
449 +        x11_driver.inpready -= strlen(inp) + 1;
450 +        if (x11_driver.inpready < 0)
451 +                x11_driver.inpready = 0;
452 + }
453 +
454 +
455 + static void
456 + std_comout(outp)                /* write out string to stdout */
457 + char    *outp;
458 + {
459 +        fputs(outp, stdout);
460 +        fflush(stdout);
461 + }
462 +
463 +
464   static int
465   x11_getcur(xp, yp)              /* get cursor position */
466   int  *xp, *yp;
# Line 343 | Line 490 | int  *xp, *yp;
490   }
491  
492  
493 < static
493 > static void
494   xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
495   int  ndx;
496   int  r, g, b;
# Line 383 | Line 530 | loop:
530                          return(ncolors = 0);
531                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
532                          break;
533 <                free((char *)pixval);
533 >                free((void *)pixval);
534                  pixval = NULL;
535          }
536          if (pixval == NULL) {
# Line 412 | Line 559 | loop:
559   }
560  
561  
562 < static
562 > static void
563   freepixels()                            /* free our pixels */
564   {
565          if (ncolors == 0)
566                  return;
567          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
568 <        free((char *)pixval);
568 >        free((void *)pixval);
569          pixval = NULL;
570          ncolors = 0;
571          if (ourmap != DefaultColormap(ourdisplay,ourscreen))
# Line 454 | Line 601 | x11_getc()                     /* get a command character */
601   }
602  
603  
604 < static
604 > static void
605   getevent()                      /* get next event */
606   {
607          XNextEvent(ourdisplay, levptr(XEvent));
# Line 470 | Line 617 | getevent()                     /* get next event */
617                  if (ourvinfo.class == PseudoColor ||
618                                  ourvinfo.class == GrayScale)
619                          if (getpixels() == 0)
620 <                                stderr_v("Cannot allocate colors\n");
620 >                                eputs("cannot allocate colors\n");
621                          else
622                                  new_ctab(ncolors);
623                  mapped = 1;
# Line 487 | Line 634 | getevent()                     /* get next event */
634   }
635  
636  
637 < static
637 > static void
638   getkey(ekey)                            /* get input key */
639   register XKeyPressedEvent  *ekey;
640   {
# Line 500 | Line 647 | register XKeyPressedEvent  *ekey;
647   }
648  
649  
650 < static
650 > static void
651   fixwindow(eexp)                         /* repair damage to window */
652   register XExposeEvent  *eexp;
653   {
654 +        char  buf[80];
655 +
656          if (eexp->window == gwind) {
657 <                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
657 >                sprintf(buf, "repaint %d %d %d %d\n",
658                          eexp->x, gheight - eexp->y - eexp->height,
659                          eexp->x + eexp->width, gheight - eexp->y);
660 +                tocombuf(buf, &x11_driver);
661          } else if (eexp->window == comline->w) {
662                  if (eexp->count == 0)
663                          xt_redraw(comline);
# Line 515 | Line 665 | register XExposeEvent  *eexp;
665   }
666  
667  
668 < static
668 > static void
669   resizewindow(ersz)                      /* resize window */
670   register XConfigureEvent  *ersz;
671   {
672 <        if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
672 >        if (ersz->width == gwidth && ersz->height-comheight == gheight)
673                  return;
674  
675          gwidth = ersz->width;
676 <        gheight = ersz->height-COMHEIGHT;
676 >        gheight = ersz->height-comheight;
677          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
678          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
679  
680 <        strcpy(getcombuf(&x11_driver), "new\n");
680 >        tocombuf("new\n", &x11_driver);
681   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines