ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.5
Committed: Thu May 25 19:33:19 1989 UTC (36 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -5 lines
Log Message:
eliminated duplication of standard error output and assoc. problems

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1987 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * x10.c - driver for X-windows version 10.4
9     *
10     * 5/7/87
11     */
12    
13     #include <stdio.h>
14    
15     #include <sys/ioctl.h>
16    
17     #include <X/Xlib.h>
18     #include <X/cursors/bcross.cursor>
19     #include <X/cursors/bcross_mask.cursor>
20    
21     #include "color.h"
22    
23     #include "driver.h"
24    
25     #include "xtwind.h"
26    
27     #define GAMMA 2.0 /* exponent for color correction */
28    
29     #define BORWIDTH 5 /* border width */
30     #define BARHEIGHT 25 /* menu bar size */
31     #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */
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() while (XPending() > 0) getevent()
45    
46     #define levptr(etype) ((etype *)&thisevent)
47    
48     static char *clientname; /* calling client's name */
49    
50     static XEvent thisevent; /* current event */
51    
52     static int colres = 128; /* color resolution */
53     static COLR colrmap[256]; /* our color mapping */
54    
55     static int ncolors = 0; /* color table size */
56     static COLR *colrtbl; /* our color table */
57     static int *pixval; /* associated pixel values */
58    
59     static Display *ourdisplay = NULL; /* our display */
60    
61     static Window gwind = 0; /* our graphics window */
62    
63     static Cursor pickcursor = 0; /* cursor used for picking */
64    
65     static int gwidth = 0; /* graphics window width */
66     static int gheight = 0; /* graphics window height */
67    
68     static TEXTWIND *comline = NULL; /* our command line */
69    
70     static int c_erase, c_kill; /* erase and kill characters */
71    
72     static char c_queue[64]; /* input queue */
73     static int c_first = 0; /* first character in queue */
74     static int c_last = 0; /* last character in queue */
75    
76     extern char *malloc();
77    
78     int x_close(), x_clear(), x_paintr(), x_errout(),
79     x_getcur(), x_comout(), x_comin();
80    
81     static struct driver x_driver = {
82     x_close, x_clear, x_paintr, x_getcur,
83     x_comout, x_comin,
84     MAXRES, MAXRES
85     };
86    
87    
88     struct driver *
89     x_init(name) /* initialize driver */
90     char *name;
91     {
92     struct sgttyb ttymode;
93    
94     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     }
102     ourdisplay = XOpenDisplay(NULL);
103     if (ourdisplay == NULL) {
104 greg 1.2 stderr_v("cannot open X-windows; DISPLAY variable set?\n");
105 greg 1.1 return(NULL);
106     }
107     if (DisplayPlanes() < 4) {
108     stderr_v("not enough colors\n");
109     return(NULL);
110     }
111     if (getmap() < 0) /* not fatal */
112     stderr_v("cannot allocate colors\n");
113    
114     pickcursor = XCreateCursor(bcross_width, bcross_height,
115     bcross_bits, bcross_mask_bits,
116     bcross_x_hot, bcross_y_hot,
117     BlackPixel, WhitePixel, GXcopy);
118     clientname = name;
119     x_driver.inpready = 0;
120 greg 1.5 cmdvec = x_comout; /* set error vectors */
121 greg 1.1 if (wrnvec != NULL)
122     wrnvec = x_errout;
123     return(&x_driver);
124     }
125    
126    
127     static
128     x_close() /* close our display */
129     {
130 greg 1.5 cmdvec = NULL; /* reset error vectors */
131 greg 1.1 if (wrnvec != NULL)
132     wrnvec = stderr_v;
133     if (ourdisplay == NULL)
134     return;
135     if (comline != NULL) {
136     xt_close(comline);
137     comline = NULL;
138     }
139     if (gwind != 0) {
140     XDestroyWindow(gwind);
141     gwind = 0;
142     gwidth = gheight = 0;
143     }
144     XFreeCursor(pickcursor);
145     freemap();
146     XCloseDisplay(ourdisplay);
147     ourdisplay = NULL;
148     }
149    
150    
151     static
152     x_clear(xres, yres) /* clear our display */
153     int xres, yres;
154     {
155     if (xres != gwidth || yres != gheight) { /* new window */
156     if (comline != NULL)
157     xt_close(comline);
158     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;
165     comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
166     if (comline == NULL)
167     goto fail;
168     XMapWindow(gwind);
169     XSelectInput(gwind,
170     KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
171     XStoreName(gwind, clientname);
172     gwidth = xres;
173     gheight = yres;
174     } else /* just clear */
175     XClear(gwind);
176     newmap();
177     flush();
178     return;
179     fail:
180     stderr_v("Failure opening window in x_clear\n");
181     quit(1);
182     }
183    
184    
185     static
186     x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
187     COLOR col;
188     int xmin, ymin, xmax, ymax;
189     {
190     extern long nrays; /* global ray count */
191     static long lastflush = 0; /* ray count at last flush */
192     int ndx;
193    
194     if (ncolors > 0) {
195     if ((ndx = colindex(col)) < 0) {
196     colres >>= 1;
197     redraw();
198     return;
199     }
200     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
201     pixval[ndx]);
202     }
203     if (nrays - lastflush >= WFLUSH) {
204 greg 1.4 flush(); /* also checks for input */
205 greg 1.1 lastflush = nrays;
206     }
207     }
208    
209    
210     static
211     x_comin(inp) /* read in a command line */
212     char *inp;
213     {
214     int x_getc(), x_comout();
215    
216     xt_cursor(comline, TBLKCURS);
217     editline(inp, x_getc, x_comout, c_erase, c_kill);
218     xt_cursor(comline, TNOCURS);
219     }
220    
221    
222     static
223     x_comout(out) /* output a string to command line */
224     char *out;
225     {
226     if (comline != NULL)
227     xt_puts(out, comline);
228     }
229    
230    
231     static
232     x_errout(msg) /* output an error message */
233     char *msg;
234     {
235     x_comout(msg);
236     stderr_v(msg); /* send to stderr also! */
237     }
238    
239    
240     static int
241     x_getcur(xp, yp) /* get cursor position */
242     int *xp, *yp;
243     {
244     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
245     sleep(1);
246     XFocusKeyboard(gwind);
247     do
248     getevent();
249     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
250     *xp = levptr(XKeyOrButtonEvent)->x;
251     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
252     XFocusKeyboard(RootWindow);
253     XUngrabMouse();
254     flush(); /* insure release */
255     if (c_last > c_first) /* key pressed */
256     return(x_getc());
257     /* button pressed */
258     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
259     case LeftButton:
260     return(MB1);
261     case MiddleButton:
262     return(MB2);
263     case RightButton:
264     return(MB3);
265     }
266     return(ABORT);
267     }
268    
269    
270     static int
271     colindex(col) /* return index for color */
272     COLOR col;
273     {
274     static COLR clr;
275     int hval;
276     register int ndx, i;
277    
278     mapcolor(clr, col);
279    
280     hval = ndx = hashcolr(clr);
281    
282     for (i = 1; i < ncolors; i++) {
283     if (colrtbl[ndx][EXP] == 0) {
284     colrtbl[ndx][RED] = clr[RED];
285     colrtbl[ndx][GRN] = clr[GRN];
286     colrtbl[ndx][BLU] = clr[BLU];
287     colrtbl[ndx][EXP] = COLXS;
288     newcolr(ndx, clr);
289     return(ndx);
290     }
291     if ( colrtbl[ndx][RED] == clr[RED] &&
292     colrtbl[ndx][GRN] == clr[GRN] &&
293     colrtbl[ndx][BLU] == clr[BLU] )
294     return(ndx);
295     ndx = (hval + i*i) % ncolors;
296     }
297     return(-1);
298     }
299    
300    
301     static
302     newcolr(ndx, clr) /* enter a color into hardware table */
303     int ndx;
304     COLR clr;
305     {
306     Color xcolor;
307    
308     xcolor.pixel = pixval[ndx];
309     xcolor.red = clr[RED] << 8;
310     xcolor.green = clr[GRN] << 8;
311     xcolor.blue = clr[BLU] << 8;
312    
313     XStoreColor(&xcolor);
314     }
315    
316    
317     static
318     mapcolor(clr, col) /* map to our color space */
319     COLR clr;
320     COLOR col;
321     {
322     register int i, p;
323     /* compute color table value */
324     for (i = 0; i < 3; i++) {
325     p = colval(col,i) * 255.0 + 0.5;
326     if (p < 0) p = 0;
327     else if (p > 255) p = 255;
328     clr[i] = colrmap[p][i];
329     }
330     clr[EXP] = COLXS;
331     }
332    
333    
334     static
335     getmap() /* get the color map */
336     {
337     int planes;
338    
339     for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
340     colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
341     pixval = (int *)malloc(ncolors*sizeof(int));
342     if (colrtbl == NULL || pixval == NULL)
343     return(-1);
344     if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
345     return(0);
346     free((char *)colrtbl);
347     free((char *)pixval);
348     }
349     ncolors = 0;
350     return(-1);
351     }
352    
353    
354     static
355     freemap() /* free our color map */
356     {
357     if (ncolors == 0)
358     return;
359     XFreeColors(pixval, ncolors, 0);
360     free((char *)colrtbl);
361     free((char *)pixval);
362     ncolors = 0;
363     }
364    
365    
366     static
367     newmap() /* initialize the color map */
368     {
369     double pow();
370     int val;
371     register int i;
372    
373     for (i = 0; i < 256; i++) {
374     val = pow(i/256.0, 1.0/GAMMA) * colres;
375     val = (val*256 + 128) / colres;
376     colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
377     colrmap[i][EXP] = COLXS;
378     }
379     for (i = 0; i < ncolors; i++)
380     colrtbl[i][EXP] = 0;
381     }
382    
383    
384     static int
385     x_getc() /* get a command character */
386     {
387     while (c_last <= c_first) {
388     c_first = c_last = 0; /* reset */
389     getevent(); /* wait for key */
390     }
391     x_driver.inpready--;
392     return(c_queue[c_first++]);
393     }
394    
395    
396     static
397     getevent() /* get next event */
398     {
399     XNextEvent(levptr(XEvent));
400     switch (levptr(XEvent)->type) {
401     case KeyPressed:
402     getkey(levptr(XKeyPressedEvent));
403     break;
404     case ExposeWindow:
405     if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
406     if (getmap() < 0)
407     stderr_v("cannot grab colors\n");
408     else
409     newmap();
410     /* fall through */
411     case ExposeRegion:
412     fixwindow(levptr(XExposeEvent));
413     break;
414     case UnmapWindow:
415     if (levptr(XUnmapEvent)->subwindow == 0)
416     freemap();
417     break;
418     case ButtonPressed: /* handled in x_getcur() */
419     break;
420     }
421     }
422    
423    
424     static
425     getkey(ekey) /* get input key */
426     register XKeyPressedEvent *ekey;
427     {
428     int n;
429     register char *str;
430    
431     str = XLookupMapping(ekey, &n);
432     while (n-- > 0 && c_last < sizeof(c_queue))
433     c_queue[c_last++] = *str++;
434     x_driver.inpready = c_last - c_first;
435     }
436    
437    
438     static
439     fixwindow(eexp) /* repair damage to window */
440     register XExposeEvent *eexp;
441     {
442     if (eexp->subwindow == 0)
443     repaint(eexp->x, gheight - eexp->y - eexp->height,
444     eexp->x + eexp->width, gheight - eexp->y);
445     else if (eexp->subwindow == comline->w)
446     xt_redraw(comline);
447     }