ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.8
Committed: Tue May 30 09:53:10 1989 UTC (36 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +5 -8 lines
Log Message:
QLength() macro doesn't seem to work, unfortunately

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 greg 1.8 #define checkinp() while (XPending() > 0) getevent()
45 greg 1.1
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 greg 1.8 checkinp();
178 greg 1.1 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.8 checkinp();
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 greg 1.8 XFlush();
229 greg 1.1 }
230    
231    
232     static
233     x_errout(msg) /* output an error message */
234     char *msg;
235     {
236     x_comout(msg);
237     stderr_v(msg); /* send to stderr also! */
238     }
239    
240    
241     static int
242     x_getcur(xp, yp) /* get cursor position */
243     int *xp, *yp;
244     {
245     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
246     sleep(1);
247     XFocusKeyboard(gwind);
248     do
249     getevent();
250     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
251     *xp = levptr(XKeyOrButtonEvent)->x;
252     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
253     XFocusKeyboard(RootWindow);
254     XUngrabMouse();
255 greg 1.8 XFlush(); /* insure release */
256 greg 1.1 if (c_last > c_first) /* key pressed */
257     return(x_getc());
258     /* button pressed */
259     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
260     case LeftButton:
261     return(MB1);
262     case MiddleButton:
263     return(MB2);
264     case RightButton:
265     return(MB3);
266     }
267     return(ABORT);
268     }
269    
270    
271     static int
272     colindex(col) /* return index for color */
273     COLOR col;
274     {
275     static COLR clr;
276     int hval;
277     register int ndx, i;
278    
279     mapcolor(clr, col);
280    
281     hval = ndx = hashcolr(clr);
282    
283     for (i = 1; i < ncolors; i++) {
284     if (colrtbl[ndx][EXP] == 0) {
285     colrtbl[ndx][RED] = clr[RED];
286     colrtbl[ndx][GRN] = clr[GRN];
287     colrtbl[ndx][BLU] = clr[BLU];
288     colrtbl[ndx][EXP] = COLXS;
289     newcolr(ndx, clr);
290     return(ndx);
291     }
292     if ( colrtbl[ndx][RED] == clr[RED] &&
293     colrtbl[ndx][GRN] == clr[GRN] &&
294     colrtbl[ndx][BLU] == clr[BLU] )
295     return(ndx);
296     ndx = (hval + i*i) % ncolors;
297     }
298     return(-1);
299     }
300    
301    
302     static
303     newcolr(ndx, clr) /* enter a color into hardware table */
304     int ndx;
305     COLR clr;
306     {
307     Color xcolor;
308    
309     xcolor.pixel = pixval[ndx];
310     xcolor.red = clr[RED] << 8;
311     xcolor.green = clr[GRN] << 8;
312     xcolor.blue = clr[BLU] << 8;
313    
314     XStoreColor(&xcolor);
315     }
316    
317    
318     static
319     mapcolor(clr, col) /* map to our color space */
320     COLR clr;
321     COLOR col;
322     {
323     register int i, p;
324     /* compute color table value */
325     for (i = 0; i < 3; i++) {
326     p = colval(col,i) * 255.0 + 0.5;
327     if (p < 0) p = 0;
328     else if (p > 255) p = 255;
329     clr[i] = colrmap[p][i];
330     }
331     clr[EXP] = COLXS;
332     }
333    
334    
335     static
336     getmap() /* get the color map */
337     {
338     int planes;
339    
340     for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
341     colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
342     pixval = (int *)malloc(ncolors*sizeof(int));
343     if (colrtbl == NULL || pixval == NULL)
344     return(-1);
345     if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
346     return(0);
347     free((char *)colrtbl);
348     free((char *)pixval);
349     }
350     ncolors = 0;
351     return(-1);
352     }
353    
354    
355     static
356     freemap() /* free our color map */
357     {
358     if (ncolors == 0)
359     return;
360     XFreeColors(pixval, ncolors, 0);
361     free((char *)colrtbl);
362     free((char *)pixval);
363     ncolors = 0;
364     }
365    
366    
367     static
368     newmap() /* initialize the color map */
369     {
370     double pow();
371     int val;
372     register int i;
373    
374     for (i = 0; i < 256; i++) {
375     val = pow(i/256.0, 1.0/GAMMA) * colres;
376     val = (val*256 + 128) / colres;
377     colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
378     colrmap[i][EXP] = COLXS;
379     }
380     for (i = 0; i < ncolors; i++)
381     colrtbl[i][EXP] = 0;
382     }
383    
384    
385     static int
386     x_getc() /* get a command character */
387     {
388     while (c_last <= c_first) {
389     c_first = c_last = 0; /* reset */
390     getevent(); /* wait for key */
391     }
392     x_driver.inpready--;
393     return(c_queue[c_first++]);
394     }
395    
396    
397     static
398     getevent() /* get next event */
399     {
400     XNextEvent(levptr(XEvent));
401     switch (levptr(XEvent)->type) {
402     case KeyPressed:
403     getkey(levptr(XKeyPressedEvent));
404     break;
405     case ExposeWindow:
406     if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
407     if (getmap() < 0)
408     stderr_v("cannot grab colors\n");
409     else
410     newmap();
411     /* fall through */
412     case ExposeRegion:
413     fixwindow(levptr(XExposeEvent));
414     break;
415     case UnmapWindow:
416     if (levptr(XUnmapEvent)->subwindow == 0)
417     freemap();
418     break;
419     case ButtonPressed: /* handled in x_getcur() */
420     break;
421     }
422     }
423    
424    
425     static
426     getkey(ekey) /* get input key */
427     register XKeyPressedEvent *ekey;
428     {
429     int n;
430     register char *str;
431    
432     str = XLookupMapping(ekey, &n);
433     while (n-- > 0 && c_last < sizeof(c_queue))
434     c_queue[c_last++] = *str++;
435     x_driver.inpready = c_last - c_first;
436     }
437    
438    
439     static
440     fixwindow(eexp) /* repair damage to window */
441     register XExposeEvent *eexp;
442     {
443     if (eexp->subwindow == 0)
444     repaint(eexp->x, gheight - eexp->y - eexp->height,
445     eexp->x + eexp->width, gheight - eexp->y);
446     else if (eexp->subwindow == comline->w)
447     xt_redraw(comline);
448     }