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.12 by greg, Mon Oct 2 17:12:39 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 27 | Line 25 | static char SCCSid[] = "$SunId$ LBL";
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  checkinp()     while (XPending() > 0) getevent()
43
37   #define  levptr(etype)  ((etype *)&thisevent)
38  
46 static char  *clientname;               /* calling client's name */
47
39   static XEvent  thisevent;               /* current event */
40  
41   static int  ncolors = 0;                /* color table size */
# Line 56 | 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 */
60 < 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  
# Line 65 | Line 55 | 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(),
71 <                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,
76 <        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 +        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 <        make_cmap(GAMMA);                       /* make color map */
90 <        if (getpixels() < 0)                    /* get pixels */
91 <                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 110 | 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 122 | Line 137 | x_close()                      /* close our display */
137          if (gwind != 0) {
138                  XDestroyWindow(gwind);
139                  gwind = 0;
125                gwidth = gheight = 0;
140          }
141          XFreeCursor(pickcursor);
142          freepixels();
# Line 136 | 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)
140 <                        xt_close(comline);
141 <                if (gwind != 0)
142 <                        XDestroyWindow(gwind);
143 <                gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
144 <                                xres, yres+COMHEIGHT, BORWIDTH,
145 <                                BlackPixmap, BlackPixmap);
146 <                if (gwind == 0)
147 <                        goto fail;
148 <                comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
149 <                if (comline == NULL)
150 <                        goto fail;
151 <                XMapWindow(gwind);
152 <                XSelectInput(gwind,
153 <        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
154 <                XStoreName(gwind, clientname);
153 >                XChangeWindow(gwind, xres, yres+COMHEIGHT);
154                  gwidth = xres;
155                  gheight = yres;
156          } else                                          /* just clear */
157                  XClear(gwind);
158 <        if (newtab() < 0) {
159 <                stderr_v("Color allocation error\n");
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 <        checkinp();
171 >        XMapWindow(gwind);                      /* make sure it's mapped */
172 >        XSync(1);                               /* discard input */
173          return;
165 fail:
166        stderr_v("Failure opening window in x_clear\n");
167        quit(1);
174   }
175  
176  
# Line 173 | 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 */
177 <        static long  lastflush = 0;     /* ray count at last flush */
182 >        extern int  xnewcolr();         /* pixel assignment routine */
183  
184          if (ncolors > 0) {
185                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
186 <                                pixval[get_pixel(col)]);
186 >                                pixval[get_pixel(col, xnewcolr)]);
187          }
183        if (nrays - lastflush >= WFLUSH) {
184                if (ncolors <= 0)       /* output necessary for death */
185                        XPixSet(gwind,0,0,1,1,BlackPixel);
186                checkinp();
187                lastflush = nrays;
188        }
188   }
189  
190  
191   static
192 < x_comin(inp)                    /* read in a command line */
194 < 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);
215          xt_cursor(comline, TNOCURS);
# Line 205 | 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 <        XFlush();
223 >        if (comline == NULL)
224 >                return;
225 >        xt_puts(out, comline);
226 >        if (out[strlen(out)-1] == '\n')
227 >                XFlush();
228   }
229  
230  
# Line 215 | 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);
219        stderr_v(msg);          /* send to stderr also! */
237   }
238  
239  
# Line 251 | Line 268 | int  *xp, *yp;
268  
269  
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
270 < newtab()                        /* assign new color table */
271 < {
272 <        extern COLR     *get_ctab();
273 <        COLR    *ctab;
274 <        Color   *defs;
275 <        register int    i;
276 <
277 <        if ((ctab = get_ctab(ncolors)) == NULL)
278 <                return(-1);
279 <        if ((defs = (Color *)malloc(ncolors*sizeof(Color))) == NULL)
280 <                return(-1);
281 <        for (i = 0; i < ncolors; i++) {
282 <                defs[i].pixel = pixval[i];
283 <                defs[i].red = ctab[i][RED] << 8;
284 <                defs[i].green = ctab[i][GRN] << 8;
285 <                defs[i].blue = ctab[i][BLU] << 8;
286 <        }
287 <        XStoreColors(ncolors, defs);
288 <        free((char *)defs);
289 <        return(0);
290 < }
291 <
292 <
293 < static
286 > static int
287   getpixels()                             /* get the color map */
288   {
289          int  planes;
290  
291 +        if (ncolors > 0)
292 +                return(ncolors);
293          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
294                  pixval = (int *)malloc(ncolors*sizeof(int));
295                  if (pixval == NULL)
296                          break;
297                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
298 <                        return(0);
298 >                        return(ncolors);
299                  free((char *)pixval);
300          }
301 <        ncolors = 0;
307 <        return(-1);
301 >        return(ncolors = 0);
302   }
303  
304  
# Line 340 | 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 (getpixels() < 0)
345 <                                stderr_v("cannot grab colors\n");
346 <                        else
347 <                                newtab();
348 <                /* fall through */
337 >                windowchange(levptr(XExposeEvent));
338 >                break;
339          case ExposeRegion:
340                  fixwindow(levptr(XExposeEvent));
341                  break;
# Line 360 | 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 367 | 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 378 | 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