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 2.5 by greg, Tue Sep 20 15:50:00 1994 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   */
12  
13   #include  <stdio.h>
14 + #include  <math.h>
15  
15 #include  <sys/ioctl.h>
16
16   #include  <X/Xlib.h>
17   #include  <X/cursors/bcross.cursor>
18   #include  <X/cursors/bcross_mask.cursor>
# Line 24 | Line 23 | static char SCCSid[] = "$SunId$ LBL";
23  
24   #include  "xtwind.h"
25  
26 < #define GAMMA           2.0             /* exponent for color correction */
26 > #define GAMMA           2.2             /* exponent for color correction */
27  
28   #define BORWIDTH        5               /* border width */
30 #define BARHEIGHT       25              /* menu bar size */
29   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
30 + #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
31 + #define MINHEIGHT       MINWIDTH                /* minimum graphics window height */
32  
33   #define COMFN           "8x13"          /* command line font name */
34   #define COMLH           3               /* number of command lines */
35   #define COMCW           8               /* approx. character width (pixels) */
36   #define COMCH           13              /* approx. character height (pixels) */
37  
38 #ifndef WFLUSH
39 #define WFLUSH          30              /* flush after this many rays */
40 #endif
41
42 #define  hashcolr(c)    ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors)
43
44 #define  flush()        XFlush()
45
46 #define  checkinp()     while (QLength() > 0) getevent()
47
38   #define  levptr(etype)  ((etype *)&thisevent)
39  
50 static char  *clientname;               /* calling client's name */
51
40   static XEvent  thisevent;               /* current event */
41  
54 static int  colres = 128;               /* color resolution */
55 static COLR  colrmap[256];              /* our color mapping */
56
42   static int  ncolors = 0;                /* color table size */
43 < static COLR  *colrtbl;                  /* our color table */
59 < static int  *pixval;                    /* associated pixel values */
43 > static int  *pixval;                    /* allocated pixel values */
44  
45   static Display  *ourdisplay = NULL;     /* our display */
46  
# Line 64 | Line 48 | static Window  gwind = 0;              /* our graphics window */
48  
49   static Cursor  pickcursor = 0;          /* cursor used for picking */
50  
51 < static int  gwidth = 0;                 /* graphics window width */
68 < static int  gheight = 0;                /* graphics window height */
51 > static int  gwidth, gheight;            /* graphics window size */
52  
53   static TEXTWIND  *comline = NULL;       /* our command line */
54  
72 static int  c_erase, c_kill;            /* erase and kill characters */
73
55   static char  c_queue[64];               /* input queue */
56   static int  c_first = 0;                /* first character in queue */
57   static int  c_last = 0;                 /* last character in queue */
58  
59 < extern char  *malloc();
59 > extern char  *malloc(), *getcombuf();
60  
61 < int  x_close(), x_clear(), x_paintr(), x_errout(),
81 <                x_getcur(), x_comout(), x_comin();
61 > extern char  *progname;
62  
63 + static int  x_close(), x_clear(), x_paintr(), x_errout(),
64 +                x_getcur(), x_comout(), x_comin(), x_flush();
65 +
66   static struct driver  x_driver = {
67          x_close, x_clear, x_paintr, x_getcur,
68 <        x_comout, x_comin,
86 <        MAXRES, MAXRES
68 >        x_comout, x_comin, x_flush, 1.0
69   };
70  
71  
72   struct driver *
73 < x_init(name)                    /* initialize driver */
74 < char  *name;
73 > x_init(name, id)                /* initialize driver */
74 > char  *name, *id;
75   {
76 <        struct sgttyb  ttymode;
77 <        
78 <        if (isatty(0)) {
79 <                ioctl(0, TIOCGETP, &ttymode);
80 <                c_erase = ttymode.sg_erase;
99 <                c_kill = ttymode.sg_kill;
100 <        } else {
101 <                c_erase = 'H'-'@';
102 <                c_kill = 'U'-'@';
103 <        }
76 >        extern char  *getenv();
77 >        char  *gv;
78 >        char  defgeom[32];
79 >        OpaqueFrame  mainframe;
80 >
81          ourdisplay = XOpenDisplay(NULL);
82          if (ourdisplay == NULL) {
83                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 110 | Line 87 | char  *name;
87                  stderr_v("not enough colors\n");
88                  return(NULL);
89          }
90 <        if (getmap() < 0)                       /* not fatal */
91 <                stderr_v("cannot allocate colors\n");
90 >                                /* make color map */
91 >        if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
92 >                make_gmap(atof(gv));
93 >        else
94 >                make_gmap(GAMMA);
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 display driver", progname, 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 >        XSelectInput(gwind, KeyPressed|ButtonPressed|
113 >                        ExposeWindow|ExposeRegion|UnmapWindow);
114 >        gwidth = mainframe.width;
115 >        gheight = mainframe.height-COMHEIGHT;
116 >        x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
117 >        x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
118          x_driver.inpready = 0;
119          cmdvec = x_comout;                      /* set error vectors */
120          if (wrnvec != NULL)
# Line 141 | Line 138 | x_close()                      /* close our display */
138          if (gwind != 0) {
139                  XDestroyWindow(gwind);
140                  gwind = 0;
144                gwidth = gheight = 0;
141          }
142          XFreeCursor(pickcursor);
143 <        freemap();
143 >        freepixels();
144          XCloseDisplay(ourdisplay);
145          ourdisplay = NULL;
146   }
# Line 155 | Line 151 | x_clear(xres, yres)                    /* clear our display */
151   int  xres, yres;
152   {
153          if (xres != gwidth || yres != gheight) {        /* new window */
154 <                if (comline != NULL)
159 <                        xt_close(comline);
160 <                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;
167 <                comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
168 <                if (comline == NULL)
169 <                        goto fail;
170 <                XMapWindow(gwind);
171 <                XSelectInput(gwind,
172 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
173 <                XStoreName(gwind, clientname);
154 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
155                  gwidth = xres;
156                  gheight = yres;
157          } else                                          /* just clear */
158                  XClear(gwind);
159 <        newmap();
160 <        flush();
159 >                                                /* reinitialize color table */
160 >        if (getpixels() == 0)
161 >                stderr_v("cannot allocate colors\n");
162 >        else
163 >                new_ctab(ncolors);
164 >                                                /* open new command line */
165 >        if (comline != NULL)
166 >                xt_close(comline);
167 >        comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
168 >        if (comline == NULL) {
169 >                stderr_v("Cannot open command line window\n");
170 >                quit(1);
171 >        }
172 >        XMapWindow(gwind);                      /* make sure it's mapped */
173 >        XSync(1);                               /* discard input */
174          return;
181 fail:
182        stderr_v("Failure opening window in x_clear\n");
183        quit(1);
175   }
176  
177  
# Line 189 | Line 180 | x_paintr(col, xmin, ymin, xmax, ymax)          /* fill a recta
180   COLOR  col;
181   int  xmin, ymin, xmax, ymax;
182   {
183 <        extern long  nrays;             /* global ray count */
193 <        static long  lastflush = 0;     /* ray count at last flush */
194 <        int  ndx;
183 >        extern int  xnewcolr();         /* pixel assignment routine */
184  
185          if (ncolors > 0) {
197                if ((ndx = colindex(col)) < 0) {
198                        colres >>= 1;
199                        redraw();
200                        return;
201                }
186                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
187 <                                pixval[ndx]);
187 >                                pixval[get_pixel(col, xnewcolr)]);
188          }
205        if (nrays - lastflush >= WFLUSH) {
206                flush();
207                lastflush = nrays;
208        }
209        checkinp();
189   }
190  
191  
192   static
193 < x_comin(inp)                    /* read in a command line */
215 < char  *inp;
193 > x_flush()                       /* flush output */
194   {
195 <        int  x_getc(), x_comout();
195 >        if (ncolors <= 0)       /* output necessary for death */
196 >                XPixSet(gwind,0,0,1,1,BlackPixel);
197 >        while (XPending() > 0)
198 >                getevent();
199  
200 + }
201 +
202 +
203 + static
204 + x_comin(inp, prompt)            /* read in a command line */
205 + char  *inp, *prompt;
206 + {
207 +        extern int  x_getc();
208 +
209 +        if (prompt != NULL)
210 +                if (fromcombuf(inp, &x_driver))
211 +                        return;
212 +                else
213 +                        xt_puts(prompt, comline);
214          xt_cursor(comline, TBLKCURS);
215 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
215 >        editline(inp, x_getc, x_comout);
216          xt_cursor(comline, TNOCURS);
217   }
218  
# Line 226 | Line 221 | static
221   x_comout(out)                   /* output a string to command line */
222   char  *out;
223   {
224 <        if (comline != NULL)
225 <                xt_puts(out, comline);
226 <        flush();
224 >        if (comline == NULL)
225 >                return;
226 >        xt_puts(out, comline);
227 >        if (out[strlen(out)-1] == '\n')
228 >                XFlush();
229   }
230  
231  
# Line 236 | Line 233 | static
233   x_errout(msg)                   /* output an error message */
234   char  *msg;
235   {
239        x_comout(msg);
236          stderr_v(msg);          /* send to stderr also! */
237 +        x_comout(msg);
238   }
239  
240  
# Line 255 | Line 252 | int  *xp, *yp;
252          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
253          XFocusKeyboard(RootWindow);
254          XUngrabMouse();
255 <        flush();                                /* insure release */
255 >        XFlush();                               /* insure release */
256          if (c_last > c_first)                   /* key pressed */
257                  return(x_getc());
258                                                  /* button pressed */
# Line 271 | Line 268 | int  *xp, *yp;
268   }
269  
270  
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
271   static
272 < newcolr(ndx, clr)               /* enter a color into hardware table */
272 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
273   int  ndx;
274 < COLR  clr;
274 > int  r, g, b;
275   {
276          Color  xcolor;
277  
278          xcolor.pixel = pixval[ndx];
279 <        xcolor.red = clr[RED] << 8;
280 <        xcolor.green = clr[GRN] << 8;
281 <        xcolor.blue = clr[BLU] << 8;
279 >        xcolor.red = r << 8;
280 >        xcolor.green = g << 8;
281 >        xcolor.blue = b << 8;
282  
283          XStoreColor(&xcolor);
284   }
285  
286  
287 < static
288 < mapcolor(clr, col)                      /* map to our color space */
323 < COLR  clr;
324 < COLOR  col;
287 > static int
288 > getpixels()                             /* get the color map */
289   {
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 {
290          int  planes;
291  
292 +        if (ncolors > 0)
293 +                return(ncolors);
294          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
344                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
295                  pixval = (int *)malloc(ncolors*sizeof(int));
296 <                if (colrtbl == NULL || pixval == NULL)
297 <                        return(-1);
296 >                if (pixval == NULL)
297 >                        break;
298                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
299 <                        return(0);
350 <                free((char *)colrtbl);
299 >                        return(ncolors);
300                  free((char *)pixval);
301          }
302 <        ncolors = 0;
354 <        return(-1);
302 >        return(ncolors = 0);
303   }
304  
305  
306   static
307 < freemap()                               /* free our color map */
307 > freepixels()                            /* free our pixels */
308   {
309          if (ncolors == 0)
310                  return;
311          XFreeColors(pixval, ncolors, 0);
364        free((char *)colrtbl);
312          free((char *)pixval);
313          ncolors = 0;
314   }
315  
316  
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
317   static int
318   x_getc()                        /* get a command character */
319   {
# Line 406 | Line 335 | getevent()                     /* get next event */
335                  getkey(levptr(XKeyPressedEvent));
336                  break;
337          case ExposeWindow:
338 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
339 <                        if (getmap() < 0)
411 <                                stderr_v("cannot grab colors\n");
412 <                        else
413 <                                newmap();
414 <                /* fall through */
338 >                windowchange(levptr(XExposeEvent));
339 >                break;
340          case ExposeRegion:
341                  fixwindow(levptr(XExposeEvent));
342                  break;
343          case UnmapWindow:
344                  if (levptr(XUnmapEvent)->subwindow == 0)
345 <                        freemap();
345 >                        freepixels();
346                  break;
347          case ButtonPressed:             /* handled in x_getcur() */
348                  break;
# Line 426 | Line 351 | getevent()                     /* get next event */
351  
352  
353   static
354 + windowchange(eexp)                      /* process window change event */
355 + register XExposeEvent  *eexp;
356 + {
357 +        if (eexp->subwindow != 0) {
358 +                fixwindow(eexp);
359 +                return;
360 +        }
361 +                                        /* check for change in size */
362 +        if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
363 +                gwidth = eexp->width;
364 +                gheight = eexp->height-COMHEIGHT;
365 +                x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
366 +                x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
367 +                strcpy(getcombuf(&x_driver), "new\n");
368 +                return;
369 +        }
370 +                                        /* remap colors */
371 +        if (getpixels() == 0) {
372 +                stderr_v("cannot allocate colors\n");
373 +                return;
374 +        }
375 +        new_ctab(ncolors);
376 +                                        /* redraw */
377 +        fixwindow(eexp);
378 + }
379 +
380 +
381 + static
382   getkey(ekey)                            /* get input key */
383   register XKeyPressedEvent  *ekey;
384   {
# Line 433 | Line 386 | register XKeyPressedEvent  *ekey;
386          register char  *str;
387  
388          str = XLookupMapping(ekey, &n);
389 <        while (n-- > 0 && c_last < sizeof(c_queue))
389 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
390                  c_queue[c_last++] = *str++;
391 <        x_driver.inpready = c_last - c_first;
391 >                x_driver.inpready++;
392 >        }
393   }
394  
395  
# Line 444 | Line 398 | fixwindow(eexp)                                /* repair damage to window */
398   register XExposeEvent  *eexp;
399   {
400          if (eexp->subwindow == 0)
401 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
401 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
402 >                        eexp->x, gheight - eexp->y - eexp->height,
403                          eexp->x + eexp->width, gheight - eexp->y);
404          else if (eexp->subwindow == comline->w)
405                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines