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.9 by greg, Fri Jun 2 17:21:26 1989 UTC vs.
Revision 1.24 by greg, Wed Feb 21 18:17:56 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 36 | Line 37 | static char SCCSid[] = "$SunId$ LBL";
37   #define COMCH           13              /* approx. character height (pixels) */
38  
39   #ifndef WFLUSH
40 < #define WFLUSH          50              /* flush after this many rays */
40 > #define WFLUSH          30              /* flush after this many rays */
41   #endif
42  
42 #define  hashcolr(c)    ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors)
43
43   #define  checkinp()     while (XPending() > 0) getevent()
44  
45   #define  levptr(etype)  ((etype *)&thisevent)
46  
48 static char  *clientname;               /* calling client's name */
49
47   static XEvent  thisevent;               /* current event */
48  
52 static int  colres = 128;               /* color resolution */
53 static COLR  colrmap[256];              /* our color mapping */
54
49   static int  ncolors = 0;                /* color table size */
50 < static COLR  *colrtbl;                  /* our color table */
57 < static int  *pixval;                    /* associated pixel values */
50 > static int  *pixval;                    /* allocated pixel values */
51  
52   static Display  *ourdisplay = NULL;     /* our display */
53  
# Line 62 | Line 55 | static Window  gwind = 0;              /* our graphics window */
55  
56   static Cursor  pickcursor = 0;          /* cursor used for picking */
57  
58 < static int  gwidth = 0;                 /* graphics window width */
66 < static int  gheight = 0;                /* graphics window height */
58 > static int  gwidth, gheight;            /* graphics window size */
59  
60   static TEXTWIND  *comline = NULL;       /* our command line */
61  
70 static int  c_erase, c_kill;            /* erase and kill characters */
71
62   static char  c_queue[64];               /* input queue */
63   static int  c_first = 0;                /* first character in queue */
64   static int  c_last = 0;                 /* last character in queue */
65  
66 < extern char  *malloc();
66 > extern char  *malloc(), *getcombuf();
67  
68 + extern char  *progname;
69 +
70   int  x_close(), x_clear(), x_paintr(), x_errout(),
71                  x_getcur(), x_comout(), x_comin();
72  
73   static struct driver  x_driver = {
74          x_close, x_clear, x_paintr, x_getcur,
75 <        x_comout, x_comin,
84 <        MAXRES, MAXRES
75 >        x_comout, x_comin, 1.0
76   };
77  
78  
79   struct driver *
80 < x_init(name)                    /* initialize driver */
81 < char  *name;
80 > x_init(name, id)                /* initialize driver */
81 > char  *name, *id;
82   {
83 <        struct sgttyb  ttymode;
84 <        
85 <        if (isatty(0)) {
95 <                ioctl(0, TIOCGETP, &ttymode);
96 <                c_erase = ttymode.sg_erase;
97 <                c_kill = ttymode.sg_kill;
98 <        } else {
99 <                c_erase = 'H'-'@';
100 <                c_kill = 'U'-'@';
101 <        }
83 >        char  defgeom[32];
84 >        OpaqueFrame  mainframe;
85 >
86          ourdisplay = XOpenDisplay(NULL);
87          if (ourdisplay == NULL) {
88                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 108 | Line 92 | char  *name;
92                  stderr_v("not enough colors\n");
93                  return(NULL);
94          }
95 <        if (getmap() < 0)                       /* not fatal */
112 <                stderr_v("cannot allocate colors\n");
95 >        make_gmap(GAMMA);                       /* make color map */
96  
97          pickcursor = XCreateCursor(bcross_width, bcross_height,
98                          bcross_bits, bcross_mask_bits,
99                          bcross_x_hot, bcross_y_hot,
100                          BlackPixel, WhitePixel, GXcopy);
101 <        clientname = name;
101 >        mainframe.bdrwidth = BORWIDTH;
102 >        mainframe.border = BlackPixmap;
103 >        mainframe.background = BlackPixmap;
104 >        sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH),
105 >                        DisplayHeight()-(2*BORWIDTH+22));
106 >        gwind = XCreate("X10 display driver", progname, NULL, defgeom,
107 >                        &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
108 >        if (gwind == 0) {
109 >                stderr_v("can't create window\n");
110 >                return(NULL);
111 >        }
112 >        XStoreName(gwind, id);
113 >        XSelectInput(gwind, KeyPressed|ButtonPressed|
114 >                        ExposeWindow|ExposeRegion|UnmapWindow);
115 >        gwidth = mainframe.width;
116 >        gheight = mainframe.height-COMHEIGHT;
117 >        x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
118 >        x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
119          x_driver.inpready = 0;
120          cmdvec = x_comout;                      /* set error vectors */
121          if (wrnvec != NULL)
# Line 139 | Line 139 | x_close()                      /* close our display */
139          if (gwind != 0) {
140                  XDestroyWindow(gwind);
141                  gwind = 0;
142                gwidth = gheight = 0;
142          }
143          XFreeCursor(pickcursor);
144 <        freemap();
144 >        freepixels();
145          XCloseDisplay(ourdisplay);
146          ourdisplay = NULL;
147   }
# Line 155 | Line 154 | int  xres, yres;
154          if (xres != gwidth || yres != gheight) {        /* new window */
155                  if (comline != NULL)
156                          xt_close(comline);
157 <                if (gwind != 0)
159 <                        XDestroyWindow(gwind);
160 <                gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
161 <                                xres, yres+COMHEIGHT, BORWIDTH,
162 <                                BlackPixmap, BlackPixmap);
163 <                if (gwind == 0)
164 <                        goto fail;
157 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
158                  comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
159 <                if (comline == NULL)
160 <                        goto fail;
161 <                XMapWindow(gwind);
162 <                XSelectInput(gwind,
170 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
171 <                XStoreName(gwind, clientname);
159 >                if (comline == NULL) {
160 >                        stderr_v("Cannot open command line window\n");
161 >                        quit(1);
162 >                }
163                  gwidth = xres;
164                  gheight = yres;
165          } else                                          /* just clear */
166                  XClear(gwind);
167 <        newmap();
168 <        checkinp();
167 >                                                /* reinitialize color table */
168 >        if (getpixels() == 0)
169 >                stderr_v("cannot allocate colors\n");
170 >        else
171 >                new_ctab(ncolors);
172 >
173 >        XMapWindow(gwind);                      /* make sure it's mapped */
174 >        XSync(1);                               /* discard input */
175          return;
179 fail:
180        stderr_v("Failure opening window in x_clear\n");
181        quit(1);
176   }
177  
178  
# Line 188 | Line 182 | COLOR  col;
182   int  xmin, ymin, xmax, ymax;
183   {
184          extern long  nrays;             /* global ray count */
185 +        extern int  xnewcolr();         /* pixel assignment routine */
186          static long  lastflush = 0;     /* ray count at last flush */
192        int  ndx;
187  
188          if (ncolors > 0) {
195                if ((ndx = colindex(col)) < 0) {
196                        colres >>= 1;
197                        redraw();
198                        return;
199                }
189                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
190 <                                pixval[ndx]);
190 >                                pixval[get_pixel(col, xnewcolr)]);
191          }
192          if (nrays - lastflush >= WFLUSH) {
193                  if (ncolors <= 0)       /* output necessary for death */
# Line 210 | Line 199 | int  xmin, ymin, xmax, ymax;
199  
200  
201   static
202 < x_comin(inp)                    /* read in a command line */
203 < char  *inp;
202 > x_comin(inp, prompt)            /* read in a command line */
203 > char  *inp, *prompt;
204   {
205          int  x_getc(), x_comout();
206  
207 +        if (prompt != NULL)
208 +                if (fromcombuf(inp, &x_driver))
209 +                        return;
210 +                else
211 +                        xt_puts(prompt, comline);
212          xt_cursor(comline, TBLKCURS);
213 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
213 >        editline(inp, x_getc, x_comout);
214          xt_cursor(comline, TNOCURS);
215   }
216  
# Line 270 | Line 264 | int  *xp, *yp;
264   }
265  
266  
273 static int
274 colindex(col)                   /* return index for color */
275 COLOR  col;
276 {
277        static COLR  clr;
278        int  hval;
279        register int  ndx, i;
280
281        mapcolor(clr, col);
282
283        hval = ndx = hashcolr(clr);
284        
285        for (i = 1; i < ncolors; i++) {
286                if (colrtbl[ndx][EXP] == 0) {
287                        colrtbl[ndx][RED] = clr[RED];
288                        colrtbl[ndx][GRN] = clr[GRN];
289                        colrtbl[ndx][BLU] = clr[BLU];
290                        colrtbl[ndx][EXP] = COLXS;
291                        newcolr(ndx, clr);
292                        return(ndx);
293                }
294                if (            colrtbl[ndx][RED] == clr[RED] &&
295                                colrtbl[ndx][GRN] == clr[GRN] &&
296                                colrtbl[ndx][BLU] == clr[BLU]   )
297                        return(ndx);
298                ndx = (hval + i*i) % ncolors;
299        }
300        return(-1);
301 }
302
303
267   static
268 < newcolr(ndx, clr)               /* enter a color into hardware table */
268 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
269   int  ndx;
270 < COLR  clr;
270 > int  r, g, b;
271   {
272          Color  xcolor;
273  
274          xcolor.pixel = pixval[ndx];
275 <        xcolor.red = clr[RED] << 8;
276 <        xcolor.green = clr[GRN] << 8;
277 <        xcolor.blue = clr[BLU] << 8;
275 >        xcolor.red = r << 8;
276 >        xcolor.green = g << 8;
277 >        xcolor.blue = b << 8;
278  
279          XStoreColor(&xcolor);
280   }
281  
282  
283 < static
284 < mapcolor(clr, col)                      /* map to our color space */
322 < COLR  clr;
323 < COLOR  col;
283 > static int
284 > getpixels()                             /* get the color map */
285   {
325        register int  i, p;
326                                        /* compute color table value */
327        for (i = 0; i < 3; i++) {
328                p = colval(col,i) * 255.0 + 0.5;
329                if (p < 0) p = 0;
330                else if (p > 255) p = 255;
331                clr[i] = colrmap[p][i];
332        }
333        clr[EXP] = COLXS;
334 }
335
336
337 static
338 getmap()                                /* get the color map */
339 {
286          int  planes;
287  
288 +        if (ncolors > 0)
289 +                return(ncolors);
290          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
343                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
291                  pixval = (int *)malloc(ncolors*sizeof(int));
292 <                if (colrtbl == NULL || pixval == NULL)
293 <                        return(-1);
292 >                if (pixval == NULL)
293 >                        break;
294                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
295 <                        return(0);
349 <                free((char *)colrtbl);
295 >                        return(ncolors);
296                  free((char *)pixval);
297          }
298 <        ncolors = 0;
353 <        return(-1);
298 >        return(ncolors = 0);
299   }
300  
301  
302   static
303 < freemap()                               /* free our color map */
303 > freepixels()                            /* free our pixels */
304   {
305          if (ncolors == 0)
306                  return;
307          XFreeColors(pixval, ncolors, 0);
363        free((char *)colrtbl);
308          free((char *)pixval);
309          ncolors = 0;
310   }
311  
312  
369 static
370 newmap()                                /* initialize the color map */
371 {
372        double  pow();
373        int  val;
374        register int  i;
375
376        for (i = 0; i < 256; i++) {
377                val = pow(i/256.0, 1.0/GAMMA) * colres;
378                val = (val*256 + 128) / colres;
379                colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
380                colrmap[i][EXP] = COLXS;
381        }
382        for (i = 0; i < ncolors; i++)
383                colrtbl[i][EXP] = 0;
384 }
385
386
313   static int
314   x_getc()                        /* get a command character */
315   {
# Line 405 | Line 331 | getevent()                     /* get next event */
331                  getkey(levptr(XKeyPressedEvent));
332                  break;
333          case ExposeWindow:
334 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
335 <                        if (getmap() < 0)
410 <                                stderr_v("cannot grab colors\n");
411 <                        else
412 <                                newmap();
413 <                /* fall through */
334 >                windowchange(levptr(XExposeEvent));
335 >                break;
336          case ExposeRegion:
337                  fixwindow(levptr(XExposeEvent));
338                  break;
339          case UnmapWindow:
340                  if (levptr(XUnmapEvent)->subwindow == 0)
341 <                        freemap();
341 >                        freepixels();
342                  break;
343          case ButtonPressed:             /* handled in x_getcur() */
344                  break;
# Line 425 | Line 347 | getevent()                     /* get next event */
347  
348  
349   static
350 + windowchange(eexp)                      /* process window change event */
351 + register XExposeEvent  *eexp;
352 + {
353 +        if (eexp->subwindow != 0) {
354 +                fixwindow(eexp);
355 +                return;
356 +        }
357 +                                        /* check for change in size */
358 +        if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
359 +                gwidth = eexp->width;
360 +                gheight = eexp->height-COMHEIGHT;
361 +                x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
362 +                x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
363 +                strcpy(getcombuf(&x_driver), "new\n");
364 +                return;
365 +        }
366 +                                        /* remap colors */
367 +        if (getpixels() == 0) {
368 +                stderr_v("cannot allocate colors\n");
369 +                return;
370 +        }
371 +        new_ctab(ncolors);
372 +                                        /* redraw */
373 +        fixwindow(eexp);
374 + }
375 +
376 +
377 + static
378   getkey(ekey)                            /* get input key */
379   register XKeyPressedEvent  *ekey;
380   {
# Line 432 | Line 382 | register XKeyPressedEvent  *ekey;
382          register char  *str;
383  
384          str = XLookupMapping(ekey, &n);
385 <        while (n-- > 0 && c_last < sizeof(c_queue))
385 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
386                  c_queue[c_last++] = *str++;
387 <        x_driver.inpready = c_last - c_first;
387 >                x_driver.inpready++;
388 >        }
389   }
390  
391  
# Line 443 | Line 394 | fixwindow(eexp)                                /* repair damage to window */
394   register XExposeEvent  *eexp;
395   {
396          if (eexp->subwindow == 0)
397 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
397 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
398 >                        eexp->x, gheight - eexp->y - eexp->height,
399                          eexp->x + eexp->width, gheight - eexp->y);
400          else if (eexp->subwindow == comline->w)
401                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines