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

Comparing ray/src/rt/x10.c (file contents):
Revision 1.7 by greg, Sat May 27 09:18:45 1989 UTC vs.
Revision 1.18 by greg, Mon Jan 8 13:38:41 1990 UTC

# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24  
25   #include  "xtwind.h"
26  
27 < #define GAMMA           2.0             /* exponent for color correction */
27 > #define GAMMA           2.2             /* exponent for color correction */
28  
29   #define BORWIDTH        5               /* border width */
30 #define BARHEIGHT       25              /* menu bar size */
30   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
31 + #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
32 + #define MINHEIGHT       64              /* minimum graphics window height */
33  
34   #define COMFN           "8x13"          /* command line font name */
35   #define COMLH           3               /* number of command lines */
# Line 39 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40   #define WFLUSH          30              /* flush after this many rays */
41   #endif
42  
43 < #define  hashcolr(c)    ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors)
43 > #define  checkinp()     while (XPending() > 0) getevent()
44  
44 #define  flush()        XFlush()
45
46 #define  checkinp()     while (QLength() > 0) getevent()
47
45   #define  levptr(etype)  ((etype *)&thisevent)
46  
47   static char  *clientname;               /* calling client's name */
48  
49   static XEvent  thisevent;               /* current event */
50  
54 static int  colres = 128;               /* color resolution */
55 static COLR  colrmap[256];              /* our color mapping */
56
51   static int  ncolors = 0;                /* color table size */
52 < static COLR  *colrtbl;                  /* our color table */
59 < static int  *pixval;                    /* associated pixel values */
52 > static int  *pixval;                    /* allocated pixel values */
53  
54   static Display  *ourdisplay = NULL;     /* our display */
55  
# Line 69 | Line 62 | static int  gheight = 0;               /* graphics window height */
62  
63   static TEXTWIND  *comline = NULL;       /* our command line */
64  
72 static int  c_erase, c_kill;            /* erase and kill characters */
73
65   static char  c_queue[64];               /* input queue */
66   static int  c_first = 0;                /* first character in queue */
67   static int  c_last = 0;                 /* last character in queue */
68  
69 < extern char  *malloc();
69 > extern char  *malloc(), *getcombuf();
70  
71   int  x_close(), x_clear(), x_paintr(), x_errout(),
72                  x_getcur(), x_comout(), x_comin();
73  
74   static struct driver  x_driver = {
75          x_close, x_clear, x_paintr, x_getcur,
76 <        x_comout, x_comin,
86 <        MAXRES, MAXRES
76 >        x_comout, x_comin, 1.0
77   };
78  
79  
80   struct driver *
81 < x_init(name)                    /* initialize driver */
82 < char  *name;
81 > x_init(name, id)                /* initialize driver */
82 > char  *name, *id;
83   {
94        struct sgttyb  ttymode;
95        
96        if (isatty(0)) {
97                ioctl(0, TIOCGETP, &ttymode);
98                c_erase = ttymode.sg_erase;
99                c_kill = ttymode.sg_kill;
100        } else {
101                c_erase = 'H'-'@';
102                c_kill = 'U'-'@';
103        }
84          ourdisplay = XOpenDisplay(NULL);
85          if (ourdisplay == NULL) {
86                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 110 | Line 90 | char  *name;
90                  stderr_v("not enough colors\n");
91                  return(NULL);
92          }
93 <        if (getmap() < 0)                       /* not fatal */
114 <                stderr_v("cannot allocate colors\n");
93 >        make_gmap(GAMMA);                       /* make color map */
94  
95          pickcursor = XCreateCursor(bcross_width, bcross_height,
96                          bcross_bits, bcross_mask_bits,
97                          bcross_x_hot, bcross_y_hot,
98                          BlackPixel, WhitePixel, GXcopy);
99 <        clientname = name;
99 >        clientname = id;
100 >        x_driver.xsiz = DisplayWidth()-(2*BORWIDTH);
101 >        x_driver.ysiz = DisplayHeight()-(COMHEIGHT+2*BORWIDTH);
102          x_driver.inpready = 0;
103          cmdvec = x_comout;                      /* set error vectors */
104          if (wrnvec != NULL)
# Line 144 | Line 125 | x_close()                      /* close our display */
125                  gwidth = gheight = 0;
126          }
127          XFreeCursor(pickcursor);
128 <        freemap();
128 >        freepixels();
129          XCloseDisplay(ourdisplay);
130          ourdisplay = NULL;
131   }
# Line 154 | Line 135 | static
135   x_clear(xres, yres)                     /* clear our display */
136   int  xres, yres;
137   {
138 +        if (xres < MINWIDTH)
139 +                xres = MINWIDTH;
140 +        if (yres < MINHEIGHT)
141 +                yres = MINHEIGHT;
142          if (xres != gwidth || yres != gheight) {        /* new window */
143                  if (comline != NULL)
144                          xt_close(comline);
145 <                if (gwind != 0)
146 <                        XDestroyWindow(gwind);
147 <                gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
148 <                                xres, yres+COMHEIGHT, BORWIDTH,
149 <                                BlackPixmap, BlackPixmap);
150 <                if (gwind == 0)
151 <                        goto fail;
145 >                if (gwind == 0) {
146 >                        gwind = XCreateWindow(RootWindow, 0, 0,
147 >                                        xres, yres+COMHEIGHT, BORWIDTH,
148 >                                        BlackPixmap, BlackPixmap);
149 >                        if (gwind == 0)
150 >                                goto fail;
151 >                        XStoreName(gwind, clientname);
152 >                        XSelectInput(gwind, KeyPressed|ButtonPressed|
153 >                                        ExposeWindow|ExposeRegion|UnmapWindow);
154 >                        XMapWindow(gwind);
155 >                } else
156 >                        XChangeWindow(gwind, xres, yres+COMHEIGHT);
157                  comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
158                  if (comline == NULL)
159                          goto fail;
170                XMapWindow(gwind);
171                XSelectInput(gwind,
172        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
173                XStoreName(gwind, clientname);
160                  gwidth = xres;
161                  gheight = yres;
162          } else                                          /* just clear */
163                  XClear(gwind);
164 <        newmap();
165 <        flush();
164 >                                                /* reinitialize color table */
165 >        if (ncolors == 0 && getpixels() == 0)
166 >                stderr_v("cannot allocate colors\n");
167 >        else
168 >                new_ctab(ncolors);
169 >        XSync(1);                               /* discard input */
170          return;
171   fail:
172          stderr_v("Failure opening window in x_clear\n");
# Line 190 | Line 180 | COLOR  col;
180   int  xmin, ymin, xmax, ymax;
181   {
182          extern long  nrays;             /* global ray count */
183 +        extern int  xnewcolr();         /* pixel assignment routine */
184          static long  lastflush = 0;     /* ray count at last flush */
194        int  ndx;
185  
186          if (ncolors > 0) {
197                if ((ndx = colindex(col)) < 0) {
198                        colres >>= 1;
199                        redraw();
200                        return;
201                }
187                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
188 <                                pixval[ndx]);
188 >                                pixval[get_pixel(col, xnewcolr)]);
189          }
190          if (nrays - lastflush >= WFLUSH) {
191 <                flush();
191 >                if (ncolors <= 0)       /* output necessary for death */
192 >                        XPixSet(gwind,0,0,1,1,BlackPixel);
193 >                checkinp();
194                  lastflush = nrays;
195          }
209        checkinp();
196   }
197  
198  
# Line 216 | Line 202 | char  *inp;
202   {
203          int  x_getc(), x_comout();
204  
205 +        if (fromcombuf(inp, &x_driver))
206 +                return;
207          xt_cursor(comline, TBLKCURS);
208 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
208 >        editline(inp, x_getc, x_comout);
209          xt_cursor(comline, TNOCURS);
210   }
211  
# Line 228 | Line 216 | char  *out;
216   {
217          if (comline != NULL)
218                  xt_puts(out, comline);
219 <        flush();
219 >        XFlush();
220   }
221  
222  
# Line 255 | Line 243 | int  *xp, *yp;
243          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
244          XFocusKeyboard(RootWindow);
245          XUngrabMouse();
246 <        flush();                                /* insure release */
246 >        XFlush();                               /* insure release */
247          if (c_last > c_first)                   /* key pressed */
248                  return(x_getc());
249                                                  /* button pressed */
# Line 271 | Line 259 | int  *xp, *yp;
259   }
260  
261  
274 static int
275 colindex(col)                   /* return index for color */
276 COLOR  col;
277 {
278        static COLR  clr;
279        int  hval;
280        register int  ndx, i;
281
282        mapcolor(clr, col);
283
284        hval = ndx = hashcolr(clr);
285        
286        for (i = 1; i < ncolors; i++) {
287                if (colrtbl[ndx][EXP] == 0) {
288                        colrtbl[ndx][RED] = clr[RED];
289                        colrtbl[ndx][GRN] = clr[GRN];
290                        colrtbl[ndx][BLU] = clr[BLU];
291                        colrtbl[ndx][EXP] = COLXS;
292                        newcolr(ndx, clr);
293                        return(ndx);
294                }
295                if (            colrtbl[ndx][RED] == clr[RED] &&
296                                colrtbl[ndx][GRN] == clr[GRN] &&
297                                colrtbl[ndx][BLU] == clr[BLU]   )
298                        return(ndx);
299                ndx = (hval + i*i) % ncolors;
300        }
301        return(-1);
302 }
303
304
262   static
263 < newcolr(ndx, clr)               /* enter a color into hardware table */
263 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
264   int  ndx;
265 < COLR  clr;
265 > int  r, g, b;
266   {
267          Color  xcolor;
268  
269          xcolor.pixel = pixval[ndx];
270 <        xcolor.red = clr[RED] << 8;
271 <        xcolor.green = clr[GRN] << 8;
272 <        xcolor.blue = clr[BLU] << 8;
270 >        xcolor.red = r << 8;
271 >        xcolor.green = g << 8;
272 >        xcolor.blue = b << 8;
273  
274          XStoreColor(&xcolor);
275   }
276  
277  
278 < static
279 < mapcolor(clr, col)                      /* map to our color space */
323 < COLR  clr;
324 < COLOR  col;
278 > static int
279 > getpixels()                             /* get the color map */
280   {
326        register int  i, p;
327                                        /* compute color table value */
328        for (i = 0; i < 3; i++) {
329                p = colval(col,i) * 255.0 + 0.5;
330                if (p < 0) p = 0;
331                else if (p > 255) p = 255;
332                clr[i] = colrmap[p][i];
333        }
334        clr[EXP] = COLXS;
335 }
336
337
338 static
339 getmap()                                /* get the color map */
340 {
281          int  planes;
282  
283 +        freepixels();
284          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
344                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
285                  pixval = (int *)malloc(ncolors*sizeof(int));
286 <                if (colrtbl == NULL || pixval == NULL)
287 <                        return(-1);
286 >                if (pixval == NULL)
287 >                        break;
288                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
289 <                        return(0);
350 <                free((char *)colrtbl);
289 >                        return(ncolors);
290                  free((char *)pixval);
291          }
292 <        ncolors = 0;
354 <        return(-1);
292 >        return(ncolors = 0);
293   }
294  
295  
296   static
297 < freemap()                               /* free our color map */
297 > freepixels()                            /* free our pixels */
298   {
299          if (ncolors == 0)
300                  return;
301          XFreeColors(pixval, ncolors, 0);
364        free((char *)colrtbl);
302          free((char *)pixval);
303          ncolors = 0;
304   }
305  
306  
370 static
371 newmap()                                /* initialize the color map */
372 {
373        double  pow();
374        int  val;
375        register int  i;
376
377        for (i = 0; i < 256; i++) {
378                val = pow(i/256.0, 1.0/GAMMA) * colres;
379                val = (val*256 + 128) / colres;
380                colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
381                colrmap[i][EXP] = COLXS;
382        }
383        for (i = 0; i < ncolors; i++)
384                colrtbl[i][EXP] = 0;
385 }
386
387
307   static int
308   x_getc()                        /* get a command character */
309   {
# Line 406 | Line 325 | getevent()                     /* get next event */
325                  getkey(levptr(XKeyPressedEvent));
326                  break;
327          case ExposeWindow:
328 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
329 <                        if (getmap() < 0)
411 <                                stderr_v("cannot grab colors\n");
412 <                        else
413 <                                newmap();
414 <                /* fall through */
328 >                windowchange(levptr(XExposeEvent));
329 >                break;
330          case ExposeRegion:
331                  fixwindow(levptr(XExposeEvent));
332                  break;
333          case UnmapWindow:
334                  if (levptr(XUnmapEvent)->subwindow == 0)
335 <                        freemap();
335 >                        freepixels();
336                  break;
337          case ButtonPressed:             /* handled in x_getcur() */
338                  break;
# Line 426 | Line 341 | getevent()                     /* get next event */
341  
342  
343   static
344 + windowchange(eexp)                      /* process window change event */
345 + register XExposeEvent  *eexp;
346 + {
347 +        if (eexp->subwindow != 0) {
348 +                fixwindow(eexp);
349 +                return;
350 +        }
351 +                                        /* check for change in size */
352 +        if (eexp->width != gwidth || eexp->height != gheight+COMHEIGHT) {
353 +                x_driver.xsiz = eexp->width;
354 +                x_driver.ysiz = eexp->height;
355 +                strcpy(getcombuf(&x_driver), "new\n");
356 +                return;
357 +        }
358 +                                        /* remap colors */
359 +        if (ncolors == 0 && getpixels() == 0) {
360 +                stderr_v("cannot allocate colors\n");
361 +                return;
362 +        }
363 +        new_ctab(ncolors);
364 +                                        /* redraw */
365 +        fixwindow(eexp);
366 + }
367 +
368 +
369 + static
370   getkey(ekey)                            /* get input key */
371   register XKeyPressedEvent  *ekey;
372   {
# Line 444 | Line 385 | fixwindow(eexp)                                /* repair damage to window */
385   register XExposeEvent  *eexp;
386   {
387          if (eexp->subwindow == 0)
388 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
388 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
389 >                        eexp->x, gheight - eexp->y - eexp->height,
390                          eexp->x + eexp->width, gheight - eexp->y);
391          else if (eexp->subwindow == comline->w)
392                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines