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.20 by greg, Tue Jan 9 09:07:03 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   int  x_close(), x_clear(), x_paintr(), x_errout(),
70                  x_getcur(), x_comout(), x_comin();
71  
72   static struct driver  x_driver = {
73          x_close, x_clear, x_paintr, x_getcur,
74 <        x_comout, x_comin,
84 <        MAXRES, MAXRES
74 >        x_comout, x_comin, 1.0
75   };
76  
77  
78   struct driver *
79 < x_init(name)                    /* initialize driver */
80 < char  *name;
79 > x_init(name, id)                /* initialize driver */
80 > char  *name, *id;
81   {
82 <        struct sgttyb  ttymode;
83 <        
84 <        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 <        }
82 >        char  defgeom[32];
83 >        OpaqueFrame  mainframe;
84 >
85          ourdisplay = XOpenDisplay(NULL);
86          if (ourdisplay == NULL) {
87                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 108 | Line 91 | char  *name;
91                  stderr_v("not enough colors\n");
92                  return(NULL);
93          }
94 <        if (getmap() < 0)                       /* not fatal */
112 <                stderr_v("cannot allocate colors\n");
94 >        make_gmap(GAMMA);                       /* make color map */
95  
96          pickcursor = XCreateCursor(bcross_width, bcross_height,
97                          bcross_bits, bcross_mask_bits,
98                          bcross_x_hot, bcross_y_hot,
99                          BlackPixel, WhitePixel, GXcopy);
100 <        clientname = name;
100 >        mainframe.bdrwidth = BORWIDTH;
101 >        mainframe.border = BlackPixmap;
102 >        mainframe.background = BlackPixmap;
103 >        sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH),
104 >                        DisplayHeight()-(2*BORWIDTH+22));
105 >        gwind = XCreate("X10 driver", name, NULL, defgeom,
106 >                        &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
107 >        if (gwind == 0) {
108 >                stderr_v("can't create window\n");
109 >                return(NULL);
110 >        }
111 >        XStoreName(gwind, id);
112 >        XMapWindow(gwind);
113 >        XSelectInput(gwind, KeyPressed|ButtonPressed|
114 >                        ExposeWindow|ExposeRegion|UnmapWindow);
115 >        x_driver.xsiz = mainframe.width;
116 >        x_driver.ysiz = mainframe.height-COMHEIGHT;
117          x_driver.inpready = 0;
118          cmdvec = x_comout;                      /* set error vectors */
119          if (wrnvec != NULL)
# Line 142 | Line 140 | x_close()                      /* close our display */
140                  gwidth = gheight = 0;
141          }
142          XFreeCursor(pickcursor);
143 <        freemap();
143 >        freepixels();
144          XCloseDisplay(ourdisplay);
145          ourdisplay = NULL;
146   }
# Line 152 | Line 150 | static
150   x_clear(xres, yres)                     /* clear our display */
151   int  xres, yres;
152   {
153 +        if (xres < MINWIDTH)
154 +                xres = MINWIDTH;
155 +        if (yres < MINHEIGHT)
156 +                yres = MINHEIGHT;
157          if (xres != gwidth || yres != gheight) {        /* new window */
158                  if (comline != NULL)
159                          xt_close(comline);
160 <                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;
160 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
161                  comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
162 <                if (comline == NULL)
163 <                        goto fail;
164 <                XMapWindow(gwind);
165 <                XSelectInput(gwind,
170 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
171 <                XStoreName(gwind, clientname);
162 >                if (comline == NULL) {
163 >                        stderr_v("Cannot open command line window\n");
164 >                        quit(1);
165 >                }
166                  gwidth = xres;
167                  gheight = yres;
168          } else                                          /* just clear */
169                  XClear(gwind);
170 <        newmap();
171 <        flush();
170 >                                                /* reinitialize color table */
171 >        if (ncolors == 0 && getpixels() == 0)
172 >                stderr_v("cannot allocate colors\n");
173 >        else
174 >                new_ctab(ncolors);
175 >        XSync(1);                               /* discard input */
176          return;
179 fail:
180        stderr_v("Failure opening window in x_clear\n");
181        quit(1);
177   }
178  
179  
# Line 188 | Line 183 | COLOR  col;
183   int  xmin, ymin, xmax, ymax;
184   {
185          extern long  nrays;             /* global ray count */
186 +        extern int  xnewcolr();         /* pixel assignment routine */
187          static long  lastflush = 0;     /* ray count at last flush */
192        int  ndx;
188  
189          if (ncolors > 0) {
195                if ((ndx = colindex(col)) < 0) {
196                        colres >>= 1;
197                        redraw();
198                        return;
199                }
190                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
191 <                                pixval[ndx]);
191 >                                pixval[get_pixel(col, xnewcolr)]);
192          }
193          if (nrays - lastflush >= WFLUSH) {
194 <                flush();                /* also checks for input */
194 >                if (ncolors <= 0)       /* output necessary for death */
195 >                        XPixSet(gwind,0,0,1,1,BlackPixel);
196 >                checkinp();
197                  lastflush = nrays;
198          }
199   }
# Line 213 | Line 205 | char  *inp;
205   {
206          int  x_getc(), x_comout();
207  
208 +        if (fromcombuf(inp, &x_driver))
209 +                return;
210          xt_cursor(comline, TBLKCURS);
211 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
211 >        editline(inp, x_getc, x_comout);
212          xt_cursor(comline, TNOCURS);
213   }
214  
# Line 225 | Line 219 | char  *out;
219   {
220          if (comline != NULL)
221                  xt_puts(out, comline);
222 <        flush();
222 >        XFlush();
223   }
224  
225  
# Line 252 | Line 246 | int  *xp, *yp;
246          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
247          XFocusKeyboard(RootWindow);
248          XUngrabMouse();
249 <        flush();                                /* insure release */
249 >        XFlush();                               /* insure release */
250          if (c_last > c_first)                   /* key pressed */
251                  return(x_getc());
252                                                  /* button pressed */
# Line 268 | Line 262 | int  *xp, *yp;
262   }
263  
264  
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
265   static
266 < newcolr(ndx, clr)               /* enter a color into hardware table */
266 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
267   int  ndx;
268 < COLR  clr;
268 > int  r, g, b;
269   {
270          Color  xcolor;
271  
272          xcolor.pixel = pixval[ndx];
273 <        xcolor.red = clr[RED] << 8;
274 <        xcolor.green = clr[GRN] << 8;
275 <        xcolor.blue = clr[BLU] << 8;
273 >        xcolor.red = r << 8;
274 >        xcolor.green = g << 8;
275 >        xcolor.blue = b << 8;
276  
277          XStoreColor(&xcolor);
278   }
279  
280  
281 < static
282 < mapcolor(clr, col)                      /* map to our color space */
320 < COLR  clr;
321 < COLOR  col;
281 > static int
282 > getpixels()                             /* get the color map */
283   {
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 {
284          int  planes;
285  
286 +        freepixels();
287          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
341                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
288                  pixval = (int *)malloc(ncolors*sizeof(int));
289 <                if (colrtbl == NULL || pixval == NULL)
290 <                        return(-1);
289 >                if (pixval == NULL)
290 >                        break;
291                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
292 <                        return(0);
347 <                free((char *)colrtbl);
292 >                        return(ncolors);
293                  free((char *)pixval);
294          }
295 <        ncolors = 0;
351 <        return(-1);
295 >        return(ncolors = 0);
296   }
297  
298  
299   static
300 < freemap()                               /* free our color map */
300 > freepixels()                            /* free our pixels */
301   {
302          if (ncolors == 0)
303                  return;
304          XFreeColors(pixval, ncolors, 0);
361        free((char *)colrtbl);
305          free((char *)pixval);
306          ncolors = 0;
307   }
308  
309  
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
310   static int
311   x_getc()                        /* get a command character */
312   {
# Line 403 | Line 328 | getevent()                     /* get next event */
328                  getkey(levptr(XKeyPressedEvent));
329                  break;
330          case ExposeWindow:
331 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
332 <                        if (getmap() < 0)
408 <                                stderr_v("cannot grab colors\n");
409 <                        else
410 <                                newmap();
411 <                /* fall through */
331 >                windowchange(levptr(XExposeEvent));
332 >                break;
333          case ExposeRegion:
334                  fixwindow(levptr(XExposeEvent));
335                  break;
336          case UnmapWindow:
337                  if (levptr(XUnmapEvent)->subwindow == 0)
338 <                        freemap();
338 >                        freepixels();
339                  break;
340          case ButtonPressed:             /* handled in x_getcur() */
341                  break;
# Line 423 | Line 344 | getevent()                     /* get next event */
344  
345  
346   static
347 + windowchange(eexp)                      /* process window change event */
348 + register XExposeEvent  *eexp;
349 + {
350 +        if (eexp->subwindow != 0) {
351 +                fixwindow(eexp);
352 +                return;
353 +        }
354 +                                        /* check for change in size */
355 +        if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
356 +                x_driver.xsiz = eexp->width;
357 +                x_driver.ysiz = eexp->height-COMHEIGHT;
358 +                strcpy(getcombuf(&x_driver), "new\n");
359 +                return;
360 +        }
361 +                                        /* remap colors */
362 +        if (ncolors == 0 && getpixels() == 0) {
363 +                stderr_v("cannot allocate colors\n");
364 +                return;
365 +        }
366 +        new_ctab(ncolors);
367 +                                        /* redraw */
368 +        fixwindow(eexp);
369 + }
370 +
371 +
372 + static
373   getkey(ekey)                            /* get input key */
374   register XKeyPressedEvent  *ekey;
375   {
# Line 430 | Line 377 | register XKeyPressedEvent  *ekey;
377          register char  *str;
378  
379          str = XLookupMapping(ekey, &n);
380 <        while (n-- > 0 && c_last < sizeof(c_queue))
380 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
381                  c_queue[c_last++] = *str++;
382 <        x_driver.inpready = c_last - c_first;
382 >                x_driver.inpready++;
383 >        }
384   }
385  
386  
# Line 441 | Line 389 | fixwindow(eexp)                                /* repair damage to window */
389   register XExposeEvent  *eexp;
390   {
391          if (eexp->subwindow == 0)
392 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
392 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
393 >                        eexp->x, gheight - eexp->y - eexp->height,
394                          eexp->x + eexp->width, gheight - eexp->y);
395          else if (eexp->subwindow == comline->w)
396                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines