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.23 by greg, Tue Jan 30 11:37:48 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  
50 static char  *clientname;               /* calling client's name */
51
47   static XEvent  thisevent;               /* current event */
48  
54 static int  colres = 128;               /* color resolution */
55 static COLR  colrmap[256];              /* our color mapping */
56
49   static int  ncolors = 0;                /* color table size */
50 < static COLR  *colrtbl;                  /* our color table */
59 < static int  *pixval;                    /* associated pixel values */
50 > static int  *pixval;                    /* allocated pixel values */
51  
52   static Display  *ourdisplay = NULL;     /* our display */
53  
# Line 69 | Line 60 | static int  gheight = 0;               /* graphics window height */
60  
61   static TEXTWIND  *comline = NULL;       /* our command line */
62  
72 static int  c_erase, c_kill;            /* erase and kill characters */
73
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,
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   {
84 <        struct sgttyb  ttymode;
85 <        
86 <        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 >        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 110 | Line 93 | char  *name;
93                  stderr_v("not enough colors\n");
94                  return(NULL);
95          }
96 <        if (getmap() < 0)                       /* not fatal */
114 <                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 144 | 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 154 | 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)
161 <                        XDestroyWindow(gwind);
162 <                gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
163 <                                xres, yres+COMHEIGHT, BORWIDTH,
164 <                                BlackPixmap, BlackPixmap);
165 <                if (gwind == 0)
166 <                        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,
172 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
173 <                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 (getpixels() == 0)
174 >                stderr_v("cannot allocate colors\n");
175 >        else
176 >                new_ctab(ncolors);
177 >        XSync(1);                               /* discard input */
178          return;
181 fail:
182        stderr_v("Failure opening window in x_clear\n");
183        quit(1);
179   }
180  
181  
# Line 190 | 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 */
194        int  ndx;
190  
191          if (ncolors > 0) {
197                if ((ndx = colindex(col)) < 0) {
198                        colres >>= 1;
199                        redraw();
200                        return;
201                }
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();
196 >                if (ncolors <= 0)       /* output necessary for death */
197 >                        XPixSet(gwind,0,0,1,1,BlackPixel);
198 >                checkinp();
199                  lastflush = nrays;
200          }
209        checkinp();
201   }
202  
203  
204   static
205 < x_comin(inp)                    /* read in a command line */
206 < char  *inp;
205 > x_comin(inp, prompt)            /* read in a command line */
206 > char  *inp, *prompt;
207   {
208          int  x_getc(), x_comout();
209  
210 +        if (prompt != NULL)
211 +                if (fromcombuf(inp, &x_driver))
212 +                        return;
213 +                else
214 +                        xt_puts(prompt, comline);
215          xt_cursor(comline, TBLKCURS);
216 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
216 >        editline(inp, x_getc, x_comout);
217          xt_cursor(comline, TNOCURS);
218   }
219  
# Line 228 | Line 224 | char  *out;
224   {
225          if (comline != NULL)
226                  xt_puts(out, comline);
227 <        flush();
227 >        XFlush();
228   }
229  
230  
# Line 255 | Line 251 | int  *xp, *yp;
251          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
252          XFocusKeyboard(RootWindow);
253          XUngrabMouse();
254 <        flush();                                /* insure release */
254 >        XFlush();                               /* insure release */
255          if (c_last > c_first)                   /* key pressed */
256                  return(x_getc());
257                                                  /* button pressed */
# Line 271 | Line 267 | int  *xp, *yp;
267   }
268  
269  
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
270   static
271 < newcolr(ndx, clr)               /* enter a color into hardware table */
271 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
272   int  ndx;
273 < COLR  clr;
273 > int  r, g, b;
274   {
275          Color  xcolor;
276  
277          xcolor.pixel = pixval[ndx];
278 <        xcolor.red = clr[RED] << 8;
279 <        xcolor.green = clr[GRN] << 8;
280 <        xcolor.blue = clr[BLU] << 8;
278 >        xcolor.red = r << 8;
279 >        xcolor.green = g << 8;
280 >        xcolor.blue = b << 8;
281  
282          XStoreColor(&xcolor);
283   }
284  
285  
286 < static
287 < mapcolor(clr, col)                      /* map to our color space */
323 < COLR  clr;
324 < COLOR  col;
286 > static int
287 > getpixels()                             /* get the color map */
288   {
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 {
289          int  planes;
290  
291 +        if (ncolors > 0)
292 +                return(ncolors);
293          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
344                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
294                  pixval = (int *)malloc(ncolors*sizeof(int));
295 <                if (colrtbl == NULL || pixval == NULL)
296 <                        return(-1);
295 >                if (pixval == NULL)
296 >                        break;
297                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
298 <                        return(0);
350 <                free((char *)colrtbl);
298 >                        return(ncolors);
299                  free((char *)pixval);
300          }
301 <        ncolors = 0;
354 <        return(-1);
301 >        return(ncolors = 0);
302   }
303  
304  
305   static
306 < freemap()                               /* free our color map */
306 > freepixels()                            /* free our pixels */
307   {
308          if (ncolors == 0)
309                  return;
310          XFreeColors(pixval, ncolors, 0);
364        free((char *)colrtbl);
311          free((char *)pixval);
312          ncolors = 0;
313   }
314  
315  
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
316   static int
317   x_getc()                        /* get a command character */
318   {
# Line 406 | Line 334 | getevent()                     /* get next event */
334                  getkey(levptr(XKeyPressedEvent));
335                  break;
336          case ExposeWindow:
337 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
338 <                        if (getmap() < 0)
411 <                                stderr_v("cannot grab colors\n");
412 <                        else
413 <                                newmap();
414 <                /* fall through */
337 >                windowchange(levptr(XExposeEvent));
338 >                break;
339          case ExposeRegion:
340                  fixwindow(levptr(XExposeEvent));
341                  break;
342          case UnmapWindow:
343                  if (levptr(XUnmapEvent)->subwindow == 0)
344 <                        freemap();
344 >                        freepixels();
345                  break;
346          case ButtonPressed:             /* handled in x_getcur() */
347                  break;
# Line 426 | Line 350 | getevent()                     /* get next event */
350  
351  
352   static
353 + windowchange(eexp)                      /* process window change event */
354 + register XExposeEvent  *eexp;
355 + {
356 +        if (eexp->subwindow != 0) {
357 +                fixwindow(eexp);
358 +                return;
359 +        }
360 +                                        /* check for change in size */
361 +        if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
362 +                x_driver.xsiz = eexp->width;
363 +                x_driver.ysiz = eexp->height-COMHEIGHT;
364 +                strcpy(getcombuf(&x_driver), "new\n");
365 +                return;
366 +        }
367 +                                        /* remap colors */
368 +        if (getpixels() == 0) {
369 +                stderr_v("cannot allocate colors\n");
370 +                return;
371 +        }
372 +        new_ctab(ncolors);
373 +                                        /* redraw */
374 +        fixwindow(eexp);
375 + }
376 +
377 +
378 + static
379   getkey(ekey)                            /* get input key */
380   register XKeyPressedEvent  *ekey;
381   {
# Line 433 | Line 383 | register XKeyPressedEvent  *ekey;
383          register char  *str;
384  
385          str = XLookupMapping(ekey, &n);
386 <        while (n-- > 0 && c_last < sizeof(c_queue))
386 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
387                  c_queue[c_last++] = *str++;
388 <        x_driver.inpready = c_last - c_first;
388 >                x_driver.inpready++;
389 >        }
390   }
391  
392  
# Line 444 | Line 395 | fixwindow(eexp)                                /* repair damage to window */
395   register XExposeEvent  *eexp;
396   {
397          if (eexp->subwindow == 0)
398 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
398 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
399 >                        eexp->x, gheight - eexp->y - eexp->height,
400                          eexp->x + eexp->width, gheight - eexp->y);
401          else if (eexp->subwindow == comline->w)
402                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines