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.6 by gregl, Tue Nov 11 20:03:02 1997 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *     5/7/87
11   */
12  
13 < #include  <stdio.h>
13 > #include  "standard.h"
14  
15 #include  <sys/ioctl.h>
16
15   #include  <X/Xlib.h>
16   #include  <X/cursors/bcross.cursor>
17   #include  <X/cursors/bcross_mask.cursor>
# Line 24 | Line 22 | static char SCCSid[] = "$SunId$ LBL";
22  
23   #include  "xtwind.h"
24  
25 < #define GAMMA           2.0             /* exponent for color correction */
25 > #define GAMMA           2.2             /* exponent for color correction */
26  
27   #define BORWIDTH        5               /* border width */
30 #define BARHEIGHT       25              /* menu bar size */
28   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
29 + #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
30 + #define MINHEIGHT       MINWIDTH                /* minimum graphics window height */
31  
32   #define COMFN           "8x13"          /* command line font name */
33   #define COMLH           3               /* number of command lines */
34   #define COMCW           8               /* approx. character width (pixels) */
35   #define COMCH           13              /* approx. character height (pixels) */
36  
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
37   #define  levptr(etype)  ((etype *)&thisevent)
38  
50 static char  *clientname;               /* calling client's name */
51
39   static XEvent  thisevent;               /* current event */
40  
54 static int  colres = 128;               /* color resolution */
55 static COLR  colrmap[256];              /* our color mapping */
56
41   static int  ncolors = 0;                /* color table size */
42 < static COLR  *colrtbl;                  /* our color table */
59 < static int  *pixval;                    /* associated pixel values */
42 > static int  *pixval;                    /* allocated pixel values */
43  
44   static Display  *ourdisplay = NULL;     /* our display */
45  
# Line 64 | Line 47 | static Window  gwind = 0;              /* our graphics window */
47  
48   static Cursor  pickcursor = 0;          /* cursor used for picking */
49  
50 < static int  gwidth = 0;                 /* graphics window width */
68 < static int  gheight = 0;                /* graphics window height */
50 > static int  gwidth, gheight;            /* graphics window size */
51  
52   static TEXTWIND  *comline = NULL;       /* our command line */
53  
72 static int  c_erase, c_kill;            /* erase and kill characters */
73
54   static char  c_queue[64];               /* input queue */
55   static int  c_first = 0;                /* first character in queue */
56   static int  c_last = 0;                 /* last character in queue */
57  
58 < extern char  *malloc();
58 > extern char  *malloc(), *getcombuf();
59  
60 < int  x_close(), x_clear(), x_paintr(), x_errout(),
81 <                x_getcur(), x_comout(), x_comin();
60 > extern char  *progname;
61  
62 + static int  x_close(), x_clear(), x_paintr(), x_errout(),
63 +                x_getcur(), x_comout(), x_comin(), x_flush();
64 +
65   static struct driver  x_driver = {
66          x_close, x_clear, x_paintr, x_getcur,
67 <        x_comout, x_comin,
86 <        MAXRES, MAXRES
67 >        x_comout, x_comin, x_flush, 1.0
68   };
69  
70  
71   struct driver *
72 < x_init(name)                    /* initialize driver */
73 < char  *name;
72 > x_init(name, id)                /* initialize driver */
73 > char  *name, *id;
74   {
75 <        struct sgttyb  ttymode;
76 <        
77 <        if (isatty(0)) {
78 <                ioctl(0, TIOCGETP, &ttymode);
79 <                c_erase = ttymode.sg_erase;
99 <                c_kill = ttymode.sg_kill;
100 <        } else {
101 <                c_erase = 'H'-'@';
102 <                c_kill = 'U'-'@';
103 <        }
75 >        extern char  *getenv();
76 >        char  *gv;
77 >        char  defgeom[32];
78 >        OpaqueFrame  mainframe;
79 >
80          ourdisplay = XOpenDisplay(NULL);
81          if (ourdisplay == NULL) {
82 <                stderr_v("cannot open X-windows; DISPLAY variable set?\n");
82 >                eputs("cannot open X-windows; DISPLAY variable set?\n");
83                  return(NULL);
84          }
85          if (DisplayPlanes() < 4) {
86 <                stderr_v("not enough colors\n");
86 >                eputs("not enough colors\n");
87                  return(NULL);
88          }
89 <        if (getmap() < 0)                       /* not fatal */
90 <                stderr_v("cannot allocate colors\n");
89 >                                /* make color map */
90 >        if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
91 >                make_gmap(atof(gv));
92 >        else
93 >                make_gmap(GAMMA);
94  
95          pickcursor = XCreateCursor(bcross_width, bcross_height,
96                          bcross_bits, bcross_mask_bits,
97                          bcross_x_hot, bcross_y_hot,
98                          BlackPixel, WhitePixel, GXcopy);
99 <        clientname = name;
99 >        mainframe.bdrwidth = BORWIDTH;
100 >        mainframe.border = BlackPixmap;
101 >        mainframe.background = BlackPixmap;
102 >        sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH),
103 >                        DisplayHeight()-(2*BORWIDTH+22));
104 >        gwind = XCreate("X10 display driver", progname, NULL, defgeom,
105 >                        &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
106 >        if (gwind == 0) {
107 >                eputs("can't create window\n");
108 >                return(NULL);
109 >        }
110 >        XStoreName(gwind, id);
111 >        XSelectInput(gwind, KeyPressed|ButtonPressed|
112 >                        ExposeWindow|ExposeRegion|UnmapWindow);
113 >        gwidth = mainframe.width;
114 >        gheight = mainframe.height-COMHEIGHT;
115 >        x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
116 >        x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
117          x_driver.inpready = 0;
118 <        cmdvec = x_comout;                      /* set error vectors */
119 <        if (wrnvec != NULL)
120 <                wrnvec = x_errout;
118 >        erract[COMMAND].pf = x_comout;          /* set error vectors */
119 >        if (erract[WARNING].pf != NULL)
120 >                erract[WARNING].pf = x_errout;
121          return(&x_driver);
122   }
123  
# Line 129 | Line 125 | char  *name;
125   static
126   x_close()                       /* close our display */
127   {
128 <        cmdvec = NULL;                          /* reset error vectors */
129 <        if (wrnvec != NULL)
130 <                wrnvec = stderr_v;
128 >        erract[COMMAND].pf = NULL;              /* reset error vectors */
129 >        if (erract[WARNING].pf != NULL)
130 >                erract[WARNING].pf = wputs;
131          if (ourdisplay == NULL)
132                  return;
133          if (comline != NULL) {
# Line 141 | Line 137 | x_close()                      /* close our display */
137          if (gwind != 0) {
138                  XDestroyWindow(gwind);
139                  gwind = 0;
144                gwidth = gheight = 0;
140          }
141          XFreeCursor(pickcursor);
142 <        freemap();
142 >        freepixels();
143          XCloseDisplay(ourdisplay);
144          ourdisplay = NULL;
145   }
# Line 155 | Line 150 | x_clear(xres, yres)                    /* clear our display */
150   int  xres, yres;
151   {
152          if (xres != gwidth || yres != gheight) {        /* new window */
153 <                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);
153 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
154                  gwidth = xres;
155                  gheight = yres;
156          } else                                          /* just clear */
157                  XClear(gwind);
158 <        newmap();
159 <        flush();
158 >                                                /* reinitialize color table */
159 >        if (getpixels() == 0)
160 >                eputs("cannot allocate colors\n");
161 >        else
162 >                new_ctab(ncolors);
163 >                                                /* open new command line */
164 >        if (comline != NULL)
165 >                xt_close(comline);
166 >        comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
167 >        if (comline == NULL) {
168 >                eputs("Cannot open command line window\n");
169 >                quit(1);
170 >        }
171 >        XMapWindow(gwind);                      /* make sure it's mapped */
172 >        XSync(1);                               /* discard input */
173          return;
181 fail:
182        stderr_v("Failure opening window in x_clear\n");
183        quit(1);
174   }
175  
176  
# Line 189 | Line 179 | x_paintr(col, xmin, ymin, xmax, ymax)          /* fill a recta
179   COLOR  col;
180   int  xmin, ymin, xmax, ymax;
181   {
182 <        extern long  nrays;             /* global ray count */
193 <        static long  lastflush = 0;     /* ray count at last flush */
194 <        int  ndx;
182 >        extern int  xnewcolr();         /* pixel assignment routine */
183  
184          if (ncolors > 0) {
197                if ((ndx = colindex(col)) < 0) {
198                        colres >>= 1;
199                        redraw();
200                        return;
201                }
185                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
186 <                                pixval[ndx]);
186 >                                pixval[get_pixel(col, xnewcolr)]);
187          }
205        if (nrays - lastflush >= WFLUSH) {
206                flush();
207                lastflush = nrays;
208        }
209        checkinp();
188   }
189  
190  
191   static
192 < x_comin(inp)                    /* read in a command line */
215 < char  *inp;
192 > x_flush()                       /* flush output */
193   {
194 <        int  x_getc(), x_comout();
194 >        if (ncolors <= 0)       /* output necessary for death */
195 >                XPixSet(gwind,0,0,1,1,BlackPixel);
196 >        while (XPending() > 0)
197 >                getevent();
198  
199 + }
200 +
201 +
202 + static
203 + x_comin(inp, prompt)            /* read in a command line */
204 + char  *inp, *prompt;
205 + {
206 +        extern int  x_getc();
207 +
208 +        if (prompt != NULL)
209 +                if (fromcombuf(inp, &x_driver))
210 +                        return;
211 +                else
212 +                        xt_puts(prompt, comline);
213          xt_cursor(comline, TBLKCURS);
214 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
214 >        editline(inp, x_getc, x_comout);
215          xt_cursor(comline, TNOCURS);
216   }
217  
# Line 226 | Line 220 | static
220   x_comout(out)                   /* output a string to command line */
221   char  *out;
222   {
223 <        if (comline != NULL)
224 <                xt_puts(out, comline);
225 <        flush();
223 >        if (comline == NULL)
224 >                return;
225 >        xt_puts(out, comline);
226 >        if (out[strlen(out)-1] == '\n')
227 >                XFlush();
228   }
229  
230  
# Line 236 | Line 232 | static
232   x_errout(msg)                   /* output an error message */
233   char  *msg;
234   {
235 +        eputs(msg);             /* send to stderr also! */
236          x_comout(msg);
240        stderr_v(msg);          /* send to stderr also! */
237   }
238  
239  
# 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 +                gwidth = eexp->width;
363 +                gheight = eexp->height-COMHEIGHT;
364 +                x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
365 +                x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
366 +                strcpy(getcombuf(&x_driver), "new\n");
367 +                return;
368 +        }
369 +                                        /* remap colors */
370 +        if (getpixels() == 0) {
371 +                eputs("cannot allocate colors\n");
372 +                return;
373 +        }
374 +        new_ctab(ncolors);
375 +                                        /* redraw */
376 +        fixwindow(eexp);
377 + }
378 +
379 +
380 + static
381   getkey(ekey)                            /* get input key */
382   register XKeyPressedEvent  *ekey;
383   {
# Line 433 | Line 385 | register XKeyPressedEvent  *ekey;
385          register char  *str;
386  
387          str = XLookupMapping(ekey, &n);
388 <        while (n-- > 0 && c_last < sizeof(c_queue))
388 >        while (n-- > 0 && c_last < sizeof(c_queue)) {
389                  c_queue[c_last++] = *str++;
390 <        x_driver.inpready = c_last - c_first;
390 >                x_driver.inpready++;
391 >        }
392   }
393  
394  
# Line 444 | Line 397 | fixwindow(eexp)                                /* repair damage to window */
397   register XExposeEvent  *eexp;
398   {
399          if (eexp->subwindow == 0)
400 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
400 >                sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
401 >                        eexp->x, gheight - eexp->y - eexp->height,
402                          eexp->x + eexp->width, gheight - eexp->y);
403          else if (eexp->subwindow == comline->w)
404                  xt_redraw(comline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines