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.6 by greg, Fri May 26 10:27:18 1989 UTC vs.
Revision 1.21 by greg, Wed Jan 10 10:04:30 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()        while (XPending() > 0) getevent()
45
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 67 | Line 60 | static int  gheight = 0;               /* graphics window height */
60  
61   static TEXTWIND  *comline = NULL;       /* our command line */
62  
70 static int  c_erase, c_kill;            /* erase and kill characters */
71
63   static char  c_queue[64];               /* input queue */
64   static int  c_first = 0;                /* first character in queue */
65   static int  c_last = 0;                 /* last character in queue */
66  
67 < extern char  *malloc();
67 > extern char  *malloc(), *getcombuf();
68  
69 + extern char  *progname;
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,
84 <        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   {
84 <        struct sgttyb  ttymode;
85 <        
86 <        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 <        }
84 >        char  defgeom[32];
85 >        OpaqueFrame  mainframe;
86 >
87          ourdisplay = XOpenDisplay(NULL);
88          if (ourdisplay == NULL) {
89                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 108 | Line 93 | char  *name;
93                  stderr_v("not enough colors\n");
94                  return(NULL);
95          }
96 <        if (getmap() < 0)                       /* not fatal */
112 <                stderr_v("cannot allocate colors\n");
96 >        make_gmap(GAMMA);                       /* make color map */
97  
98          pickcursor = XCreateCursor(bcross_width, bcross_height,
99                          bcross_bits, bcross_mask_bits,
100                          bcross_x_hot, bcross_y_hot,
101                          BlackPixel, WhitePixel, GXcopy);
102 <        clientname = name;
102 >        mainframe.bdrwidth = BORWIDTH;
103 >        mainframe.border = BlackPixmap;
104 >        mainframe.background = BlackPixmap;
105 >        sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH),
106 >                        DisplayHeight()-(2*BORWIDTH+22));
107 >        gwind = XCreate("X10 display driver", progname, NULL, defgeom,
108 >                        &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
109 >        if (gwind == 0) {
110 >                stderr_v("can't create window\n");
111 >                return(NULL);
112 >        }
113 >        XStoreName(gwind, id);
114 >        XMapWindow(gwind);
115 >        XSelectInput(gwind, KeyPressed|ButtonPressed|
116 >                        ExposeWindow|ExposeRegion|UnmapWindow);
117 >        x_driver.xsiz = mainframe.width;
118 >        x_driver.ysiz = mainframe.height-COMHEIGHT;
119          x_driver.inpready = 0;
120          cmdvec = x_comout;                      /* set error vectors */
121          if (wrnvec != NULL)
# Line 142 | Line 142 | x_close()                      /* close our display */
142                  gwidth = gheight = 0;
143          }
144          XFreeCursor(pickcursor);
145 <        freemap();
145 >        freepixels();
146          XCloseDisplay(ourdisplay);
147          ourdisplay = NULL;
148   }
# Line 152 | Line 152 | static
152   x_clear(xres, yres)                     /* clear our display */
153   int  xres, yres;
154   {
155 +        if (xres < MINWIDTH)
156 +                xres = MINWIDTH;
157 +        if (yres < MINHEIGHT)
158 +                yres = MINHEIGHT;
159          if (xres != gwidth || yres != gheight) {        /* new window */
160                  if (comline != NULL)
161                          xt_close(comline);
162 <                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;
162 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
163                  comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
164 <                if (comline == NULL)
165 <                        goto fail;
166 <                XMapWindow(gwind);
167 <                XSelectInput(gwind,
170 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
171 <                XStoreName(gwind, clientname);
164 >                if (comline == NULL) {
165 >                        stderr_v("Cannot open command line window\n");
166 >                        quit(1);
167 >                }
168                  gwidth = xres;
169                  gheight = yres;
170          } else                                          /* just clear */
171                  XClear(gwind);
172 <        newmap();
173 <        flush();
172 >                                                /* reinitialize color table */
173 >        if (ncolors == 0 && getpixels() == 0)
174 >                stderr_v("cannot allocate colors\n");
175 >        else
176 >                new_ctab(ncolors);
177 >        XSync(1);                               /* discard input */
178          return;
179 fail:
180        stderr_v("Failure opening window in x_clear\n");
181        quit(1);
179   }
180  
181  
# Line 188 | Line 185 | COLOR  col;
185   int  xmin, ymin, xmax, ymax;
186   {
187          extern long  nrays;             /* global ray count */
188 +        extern int  xnewcolr();         /* pixel assignment routine */
189          static long  lastflush = 0;     /* ray count at last flush */
192        int  ndx;
190  
191          if (ncolors > 0) {
195                if ((ndx = colindex(col)) < 0) {
196                        colres >>= 1;
197                        redraw();
198                        return;
199                }
192                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
193 <                                pixval[ndx]);
193 >                                pixval[get_pixel(col, xnewcolr)]);
194          }
195          if (nrays - lastflush >= WFLUSH) {
196 <                flush();                /* also checks for input */
196 >                if (ncolors <= 0)       /* output necessary for death */
197 >                        XPixSet(gwind,0,0,1,1,BlackPixel);
198 >                checkinp();
199                  lastflush = nrays;
200          }
201   }
# Line 213 | Line 207 | char  *inp;
207   {
208          int  x_getc(), x_comout();
209  
210 +        if (fromcombuf(inp, &x_driver))
211 +                return;
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 225 | Line 221 | char  *out;
221   {
222          if (comline != NULL)
223                  xt_puts(out, comline);
224 <        flush();
224 >        XFlush();
225   }
226  
227  
# Line 252 | Line 248 | int  *xp, *yp;
248          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
249          XFocusKeyboard(RootWindow);
250          XUngrabMouse();
251 <        flush();                                /* insure release */
251 >        XFlush();                               /* insure release */
252          if (c_last > c_first)                   /* key pressed */
253                  return(x_getc());
254                                                  /* button pressed */
# Line 268 | Line 264 | int  *xp, *yp;
264   }
265  
266  
271 static int
272 colindex(col)                   /* return index for color */
273 COLOR  col;
274 {
275        static COLR  clr;
276        int  hval;
277        register int  ndx, i;
278
279        mapcolor(clr, col);
280
281        hval = ndx = hashcolr(clr);
282        
283        for (i = 1; i < ncolors; i++) {
284                if (colrtbl[ndx][EXP] == 0) {
285                        colrtbl[ndx][RED] = clr[RED];
286                        colrtbl[ndx][GRN] = clr[GRN];
287                        colrtbl[ndx][BLU] = clr[BLU];
288                        colrtbl[ndx][EXP] = COLXS;
289                        newcolr(ndx, clr);
290                        return(ndx);
291                }
292                if (            colrtbl[ndx][RED] == clr[RED] &&
293                                colrtbl[ndx][GRN] == clr[GRN] &&
294                                colrtbl[ndx][BLU] == clr[BLU]   )
295                        return(ndx);
296                ndx = (hval + i*i) % ncolors;
297        }
298        return(-1);
299 }
300
301
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 */
320 < COLR  clr;
321 < COLOR  col;
283 > static int
284 > getpixels()                             /* get the color map */
285   {
323        register int  i, p;
324                                        /* compute color table value */
325        for (i = 0; i < 3; i++) {
326                p = colval(col,i) * 255.0 + 0.5;
327                if (p < 0) p = 0;
328                else if (p > 255) p = 255;
329                clr[i] = colrmap[p][i];
330        }
331        clr[EXP] = COLXS;
332 }
333
334
335 static
336 getmap()                                /* get the color map */
337 {
286          int  planes;
287  
288 +        freepixels();
289          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
341                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
290                  pixval = (int *)malloc(ncolors*sizeof(int));
291 <                if (colrtbl == NULL || pixval == NULL)
292 <                        return(-1);
291 >                if (pixval == NULL)
292 >                        break;
293                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
294 <                        return(0);
347 <                free((char *)colrtbl);
294 >                        return(ncolors);
295                  free((char *)pixval);
296          }
297 <        ncolors = 0;
351 <        return(-1);
297 >        return(ncolors = 0);
298   }
299  
300  
301   static
302 < freemap()                               /* free our color map */
302 > freepixels()                            /* free our pixels */
303   {
304          if (ncolors == 0)
305                  return;
306          XFreeColors(pixval, ncolors, 0);
361        free((char *)colrtbl);
307          free((char *)pixval);
308          ncolors = 0;
309   }
310  
311  
367 static
368 newmap()                                /* initialize the color map */
369 {
370        double  pow();
371        int  val;
372        register int  i;
373
374        for (i = 0; i < 256; i++) {
375                val = pow(i/256.0, 1.0/GAMMA) * colres;
376                val = (val*256 + 128) / colres;
377                colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
378                colrmap[i][EXP] = COLXS;
379        }
380        for (i = 0; i < ncolors; i++)
381                colrtbl[i][EXP] = 0;
382 }
383
384
312   static int
313   x_getc()                        /* get a command character */
314   {
# Line 403 | Line 330 | getevent()                     /* get next event */
330                  getkey(levptr(XKeyPressedEvent));
331                  break;
332          case ExposeWindow:
333 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
334 <                        if (getmap() < 0)
408 <                                stderr_v("cannot grab colors\n");
409 <                        else
410 <                                newmap();
411 <                /* fall through */
333 >                windowchange(levptr(XExposeEvent));
334 >                break;
335          case ExposeRegion:
336                  fixwindow(levptr(XExposeEvent));
337                  break;
338          case UnmapWindow:
339                  if (levptr(XUnmapEvent)->subwindow == 0)
340 <                        freemap();
340 >                        freepixels();
341                  break;
342          case ButtonPressed:             /* handled in x_getcur() */
343                  break;
# Line 423 | Line 346 | getevent()                     /* get next event */
346  
347  
348   static
349 + windowchange(eexp)                      /* process window change event */
350 + register XExposeEvent  *eexp;
351 + {
352 +        if (eexp->subwindow != 0) {
353 +                fixwindow(eexp);
354 +                return;
355 +        }
356 +                                        /* check for change in size */
357 +        if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
358 +                x_driver.xsiz = eexp->width;
359 +                x_driver.ysiz = eexp->height-COMHEIGHT;
360 +                strcpy(getcombuf(&x_driver), "new\n");
361 +                return;
362 +        }
363 +                                        /* remap colors */
364 +        if (ncolors == 0 && getpixels() == 0) {
365 +                stderr_v("cannot allocate colors\n");
366 +                return;
367 +        }
368 +        new_ctab(ncolors);
369 +                                        /* redraw */
370 +        fixwindow(eexp);
371 + }
372 +
373 +
374 + static
375   getkey(ekey)                            /* get input key */
376   register XKeyPressedEvent  *ekey;
377   {
# Line 430 | Line 379 | register XKeyPressedEvent  *ekey;
379          register char  *str;
380  
381          str = XLookupMapping(ekey, &n);
382 <        while (n-- > 0 && c_last < sizeof(c_queue))
382 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
383                  c_queue[c_last++] = *str++;
384 <        x_driver.inpready = c_last - c_first;
384 >                x_driver.inpready++;
385 >        }
386   }
387  
388  
# Line 441 | Line 391 | fixwindow(eexp)                                /* repair damage to window */
391   register XExposeEvent  *eexp;
392   {
393          if (eexp->subwindow == 0)
394 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
394 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
395 >                        eexp->x, gheight - eexp->y - eexp->height,
396                          eexp->x + eexp->width, gheight - eexp->y);
397          else if (eexp->subwindow == comline->w)
398                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines