ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.12
Committed: Mon Oct 2 17:12:39 1989 UTC (35 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +35 -88 lines
Log Message:
New adaptive color allocation scheme for rview

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 greg 1.12 #define GAMMA 2.2 /* exponent for color correction */
28 greg 1.1
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 greg 1.10 #define WFLUSH 30 /* flush after this many rays */
40 greg 1.1 #endif
41    
42 greg 1.8 #define checkinp() while (XPending() > 0) getevent()
43 greg 1.1
44     #define levptr(etype) ((etype *)&thisevent)
45    
46     static char *clientname; /* calling client's name */
47    
48     static XEvent thisevent; /* current event */
49    
50     static int ncolors = 0; /* color table size */
51 greg 1.12 static int *pixval; /* allocated pixel values */
52 greg 1.1
53     static Display *ourdisplay = NULL; /* our display */
54    
55     static Window gwind = 0; /* our graphics window */
56    
57     static Cursor pickcursor = 0; /* cursor used for picking */
58    
59     static int gwidth = 0; /* graphics window width */
60     static int gheight = 0; /* graphics window height */
61    
62     static TEXTWIND *comline = NULL; /* our command line */
63    
64     static char c_queue[64]; /* input queue */
65     static int c_first = 0; /* first character in queue */
66     static int c_last = 0; /* last character in queue */
67    
68     extern char *malloc();
69    
70     int x_close(), x_clear(), x_paintr(), x_errout(),
71     x_getcur(), x_comout(), x_comin();
72    
73     static struct driver x_driver = {
74     x_close, x_clear, x_paintr, x_getcur,
75     x_comout, x_comin,
76     MAXRES, MAXRES
77     };
78    
79    
80     struct driver *
81     x_init(name) /* initialize driver */
82     char *name;
83     {
84     ourdisplay = XOpenDisplay(NULL);
85     if (ourdisplay == NULL) {
86 greg 1.2 stderr_v("cannot open X-windows; DISPLAY variable set?\n");
87 greg 1.1 return(NULL);
88     }
89     if (DisplayPlanes() < 4) {
90     stderr_v("not enough colors\n");
91     return(NULL);
92     }
93 greg 1.12 make_cmap(GAMMA); /* make color map */
94     if (getpixels() < 0) /* get pixels */
95 greg 1.1 stderr_v("cannot allocate colors\n");
96    
97     pickcursor = XCreateCursor(bcross_width, bcross_height,
98     bcross_bits, bcross_mask_bits,
99     bcross_x_hot, bcross_y_hot,
100     BlackPixel, WhitePixel, GXcopy);
101     clientname = name;
102     x_driver.inpready = 0;
103 greg 1.5 cmdvec = x_comout; /* set error vectors */
104 greg 1.1 if (wrnvec != NULL)
105     wrnvec = x_errout;
106     return(&x_driver);
107     }
108    
109    
110     static
111     x_close() /* close our display */
112     {
113 greg 1.5 cmdvec = NULL; /* reset error vectors */
114 greg 1.1 if (wrnvec != NULL)
115     wrnvec = stderr_v;
116     if (ourdisplay == NULL)
117     return;
118     if (comline != NULL) {
119     xt_close(comline);
120     comline = NULL;
121     }
122     if (gwind != 0) {
123     XDestroyWindow(gwind);
124     gwind = 0;
125     gwidth = gheight = 0;
126     }
127     XFreeCursor(pickcursor);
128 greg 1.12 freepixels();
129 greg 1.1 XCloseDisplay(ourdisplay);
130     ourdisplay = NULL;
131     }
132    
133    
134     static
135     x_clear(xres, yres) /* clear our display */
136     int xres, yres;
137     {
138     if (xres != gwidth || yres != gheight) { /* new window */
139     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);
155     gwidth = xres;
156     gheight = yres;
157     } else /* just clear */
158     XClear(gwind);
159 greg 1.12 if (newtab() < 0) {
160     stderr_v("Color allocation error\n");
161     quit(1);
162     }
163 greg 1.8 checkinp();
164 greg 1.1 return;
165     fail:
166     stderr_v("Failure opening window in x_clear\n");
167     quit(1);
168     }
169    
170    
171     static
172     x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
173     COLOR col;
174     int xmin, ymin, xmax, ymax;
175     {
176     extern long nrays; /* global ray count */
177     static long lastflush = 0; /* ray count at last flush */
178    
179     if (ncolors > 0) {
180     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
181 greg 1.12 pixval[get_pixel(col)]);
182 greg 1.1 }
183     if (nrays - lastflush >= WFLUSH) {
184 greg 1.9 if (ncolors <= 0) /* output necessary for death */
185     XPixSet(gwind,0,0,1,1,BlackPixel);
186 greg 1.8 checkinp();
187 greg 1.1 lastflush = nrays;
188     }
189     }
190    
191    
192     static
193     x_comin(inp) /* read in a command line */
194     char *inp;
195     {
196     int x_getc(), x_comout();
197    
198     xt_cursor(comline, TBLKCURS);
199 greg 1.11 editline(inp, x_getc, x_comout);
200 greg 1.1 xt_cursor(comline, TNOCURS);
201     }
202    
203    
204     static
205     x_comout(out) /* output a string to command line */
206     char *out;
207     {
208     if (comline != NULL)
209     xt_puts(out, comline);
210 greg 1.8 XFlush();
211 greg 1.1 }
212    
213    
214     static
215     x_errout(msg) /* output an error message */
216     char *msg;
217     {
218     x_comout(msg);
219     stderr_v(msg); /* send to stderr also! */
220     }
221    
222    
223     static int
224     x_getcur(xp, yp) /* get cursor position */
225     int *xp, *yp;
226     {
227     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
228     sleep(1);
229     XFocusKeyboard(gwind);
230     do
231     getevent();
232     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
233     *xp = levptr(XKeyOrButtonEvent)->x;
234     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
235     XFocusKeyboard(RootWindow);
236     XUngrabMouse();
237 greg 1.8 XFlush(); /* insure release */
238 greg 1.1 if (c_last > c_first) /* key pressed */
239     return(x_getc());
240     /* button pressed */
241     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
242     case LeftButton:
243     return(MB1);
244     case MiddleButton:
245     return(MB2);
246     case RightButton:
247     return(MB3);
248     }
249     return(ABORT);
250     }
251    
252    
253     static
254     newcolr(ndx, clr) /* enter a color into hardware table */
255     int ndx;
256     COLR clr;
257     {
258     Color xcolor;
259    
260     xcolor.pixel = pixval[ndx];
261     xcolor.red = clr[RED] << 8;
262     xcolor.green = clr[GRN] << 8;
263     xcolor.blue = clr[BLU] << 8;
264    
265     XStoreColor(&xcolor);
266     }
267    
268    
269     static
270 greg 1.12 newtab() /* assign new color table */
271 greg 1.1 {
272 greg 1.12 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 greg 1.1 }
287 greg 1.12 XStoreColors(ncolors, defs);
288     free((char *)defs);
289     return(0);
290 greg 1.1 }
291    
292    
293     static
294 greg 1.12 getpixels() /* get the color map */
295 greg 1.1 {
296     int planes;
297    
298     for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
299     pixval = (int *)malloc(ncolors*sizeof(int));
300 greg 1.12 if (pixval == NULL)
301     break;
302 greg 1.1 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
303     return(0);
304     free((char *)pixval);
305     }
306     ncolors = 0;
307     return(-1);
308     }
309    
310    
311     static
312 greg 1.12 freepixels() /* free our pixels */
313 greg 1.1 {
314     if (ncolors == 0)
315     return;
316     XFreeColors(pixval, ncolors, 0);
317     free((char *)pixval);
318     ncolors = 0;
319     }
320    
321    
322     static int
323     x_getc() /* get a command character */
324     {
325     while (c_last <= c_first) {
326     c_first = c_last = 0; /* reset */
327     getevent(); /* wait for key */
328     }
329     x_driver.inpready--;
330     return(c_queue[c_first++]);
331     }
332    
333    
334     static
335     getevent() /* get next event */
336     {
337     XNextEvent(levptr(XEvent));
338     switch (levptr(XEvent)->type) {
339     case KeyPressed:
340     getkey(levptr(XKeyPressedEvent));
341     break;
342     case ExposeWindow:
343     if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
344 greg 1.12 if (getpixels() < 0)
345 greg 1.1 stderr_v("cannot grab colors\n");
346     else
347 greg 1.12 newtab();
348 greg 1.1 /* fall through */
349     case ExposeRegion:
350     fixwindow(levptr(XExposeEvent));
351     break;
352     case UnmapWindow:
353     if (levptr(XUnmapEvent)->subwindow == 0)
354 greg 1.12 freepixels();
355 greg 1.1 break;
356     case ButtonPressed: /* handled in x_getcur() */
357     break;
358     }
359     }
360    
361    
362     static
363     getkey(ekey) /* get input key */
364     register XKeyPressedEvent *ekey;
365     {
366     int n;
367     register char *str;
368    
369     str = XLookupMapping(ekey, &n);
370     while (n-- > 0 && c_last < sizeof(c_queue))
371     c_queue[c_last++] = *str++;
372     x_driver.inpready = c_last - c_first;
373     }
374    
375    
376     static
377     fixwindow(eexp) /* repair damage to window */
378     register XExposeEvent *eexp;
379     {
380     if (eexp->subwindow == 0)
381     repaint(eexp->x, gheight - eexp->y - eexp->height,
382     eexp->x + eexp->width, gheight - eexp->y);
383     else if (eexp->subwindow == comline->w)
384     xt_redraw(comline);
385     }