ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 2.7
Committed: Sat Feb 22 02:07:29 2003 UTC (22 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.6: +0 -0 lines
State: FILE REMOVED
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

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 gregl 2.6 #include "standard.h"
14 greg 1.1
15     #include <X/Xlib.h>
16     #include <X/cursors/bcross.cursor>
17     #include <X/cursors/bcross_mask.cursor>
18    
19     #include "color.h"
20    
21     #include "driver.h"
22    
23     #include "xtwind.h"
24    
25 greg 1.12 #define GAMMA 2.2 /* exponent for color correction */
26 greg 1.1
27     #define BORWIDTH 5 /* border width */
28     #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */
29 greg 1.18 #define MINWIDTH (32*COMCW) /* minimum graphics window width */
30 greg 1.26 #define MINHEIGHT MINWIDTH /* minimum graphics window height */
31 greg 1.1
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    
37     #define levptr(etype) ((etype *)&thisevent)
38    
39     static XEvent thisevent; /* current event */
40    
41     static int ncolors = 0; /* color table size */
42 greg 1.12 static int *pixval; /* allocated pixel values */
43 greg 1.1
44     static Display *ourdisplay = NULL; /* our display */
45    
46     static Window gwind = 0; /* our graphics window */
47    
48     static Cursor pickcursor = 0; /* cursor used for picking */
49    
50 greg 1.24 static int gwidth, gheight; /* graphics window size */
51 greg 1.1
52     static TEXTWIND *comline = NULL; /* our command line */
53    
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 greg 1.18 extern char *malloc(), *getcombuf();
59 greg 1.1
60 greg 1.21 extern char *progname;
61    
62 greg 1.28 static int x_close(), x_clear(), x_paintr(), x_errout(),
63 greg 1.25 x_getcur(), x_comout(), x_comin(), x_flush();
64 greg 1.1
65     static struct driver x_driver = {
66     x_close, x_clear, x_paintr, x_getcur,
67 greg 1.25 x_comout, x_comin, x_flush, 1.0
68 greg 1.1 };
69    
70    
71     struct driver *
72 greg 1.16 x_init(name, id) /* initialize driver */
73     char *name, *id;
74 greg 1.1 {
75 greg 2.2 extern char *getenv();
76     char *gv;
77 greg 1.19 char defgeom[32];
78     OpaqueFrame mainframe;
79    
80 greg 1.1 ourdisplay = XOpenDisplay(NULL);
81     if (ourdisplay == NULL) {
82 gregl 2.6 eputs("cannot open X-windows; DISPLAY variable set?\n");
83 greg 1.1 return(NULL);
84     }
85     if (DisplayPlanes() < 4) {
86 gregl 2.6 eputs("not enough colors\n");
87 greg 1.1 return(NULL);
88     }
89 greg 2.2 /* make color map */
90 greg 2.5 if ((gv = getenv("DISPLAY_GAMMA")) != NULL)
91 greg 2.2 make_gmap(atof(gv));
92     else
93     make_gmap(GAMMA);
94 greg 1.1
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 greg 1.19 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 greg 1.21 gwind = XCreate("X10 display driver", progname, NULL, defgeom,
105 greg 1.19 &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
106     if (gwind == 0) {
107 gregl 2.6 eputs("can't create window\n");
108 greg 1.19 return(NULL);
109     }
110     XStoreName(gwind, id);
111     XSelectInput(gwind, KeyPressed|ButtonPressed|
112     ExposeWindow|ExposeRegion|UnmapWindow);
113 greg 1.24 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 greg 1.1 x_driver.inpready = 0;
118 gregl 2.6 erract[COMMAND].pf = x_comout; /* set error vectors */
119     if (erract[WARNING].pf != NULL)
120     erract[WARNING].pf = x_errout;
121 greg 1.1 return(&x_driver);
122     }
123    
124    
125     static
126     x_close() /* close our display */
127     {
128 gregl 2.6 erract[COMMAND].pf = NULL; /* reset error vectors */
129     if (erract[WARNING].pf != NULL)
130     erract[WARNING].pf = wputs;
131 greg 1.1 if (ourdisplay == NULL)
132     return;
133     if (comline != NULL) {
134     xt_close(comline);
135     comline = NULL;
136     }
137     if (gwind != 0) {
138     XDestroyWindow(gwind);
139     gwind = 0;
140     }
141     XFreeCursor(pickcursor);
142 greg 1.12 freepixels();
143 greg 1.1 XCloseDisplay(ourdisplay);
144     ourdisplay = NULL;
145     }
146    
147    
148     static
149     x_clear(xres, yres) /* clear our display */
150     int xres, yres;
151     {
152     if (xres != gwidth || yres != gheight) { /* new window */
153 greg 1.19 XChangeWindow(gwind, xres, yres+COMHEIGHT);
154 greg 1.1 gwidth = xres;
155     gheight = yres;
156     } else /* just clear */
157     XClear(gwind);
158 greg 1.17 /* reinitialize color table */
159 greg 1.22 if (getpixels() == 0)
160 gregl 2.6 eputs("cannot allocate colors\n");
161 greg 1.17 else
162     new_ctab(ncolors);
163 greg 1.29 /* 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 gregl 2.6 eputs("Cannot open command line window\n");
169 greg 1.29 quit(1);
170     }
171 greg 1.24 XMapWindow(gwind); /* make sure it's mapped */
172 greg 1.17 XSync(1); /* discard input */
173 greg 1.1 return;
174     }
175    
176    
177     static
178     x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
179     COLOR col;
180     int xmin, ymin, xmax, ymax;
181     {
182 greg 1.14 extern int xnewcolr(); /* pixel assignment routine */
183 greg 1.1
184     if (ncolors > 0) {
185     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
186 greg 1.14 pixval[get_pixel(col, xnewcolr)]);
187 greg 1.1 }
188 greg 1.25 }
189    
190    
191     static
192     x_flush() /* flush output */
193     {
194     if (ncolors <= 0) /* output necessary for death */
195     XPixSet(gwind,0,0,1,1,BlackPixel);
196     while (XPending() > 0)
197     getevent();
198    
199 greg 1.1 }
200    
201    
202     static
203 greg 1.23 x_comin(inp, prompt) /* read in a command line */
204     char *inp, *prompt;
205 greg 1.1 {
206 greg 1.28 extern int x_getc();
207 greg 1.1
208 greg 1.23 if (prompt != NULL)
209     if (fromcombuf(inp, &x_driver))
210     return;
211     else
212     xt_puts(prompt, comline);
213 greg 1.1 xt_cursor(comline, TBLKCURS);
214 greg 1.11 editline(inp, x_getc, x_comout);
215 greg 1.1 xt_cursor(comline, TNOCURS);
216     }
217    
218    
219     static
220     x_comout(out) /* output a string to command line */
221     char *out;
222     {
223 greg 1.27 if (comline == NULL)
224     return;
225     xt_puts(out, comline);
226     if (out[strlen(out)-1] == '\n')
227     XFlush();
228 greg 1.1 }
229    
230    
231     static
232     x_errout(msg) /* output an error message */
233     char *msg;
234     {
235 gregl 2.6 eputs(msg); /* send to stderr also! */
236 greg 1.1 x_comout(msg);
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 greg 1.8 XFlush(); /* insure release */
255 greg 1.1 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
271 greg 1.13 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
272 greg 1.1 int ndx;
273 greg 1.13 int r, g, b;
274 greg 1.1 {
275     Color xcolor;
276    
277     xcolor.pixel = pixval[ndx];
278 greg 1.13 xcolor.red = r << 8;
279     xcolor.green = g << 8;
280     xcolor.blue = b << 8;
281 greg 1.1
282     XStoreColor(&xcolor);
283     }
284    
285    
286 greg 1.17 static int
287 greg 1.12 getpixels() /* get the color map */
288 greg 1.1 {
289     int planes;
290    
291 greg 1.22 if (ncolors > 0)
292     return(ncolors);
293 greg 1.1 for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
294     pixval = (int *)malloc(ncolors*sizeof(int));
295 greg 1.12 if (pixval == NULL)
296     break;
297 greg 1.1 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
298 greg 1.17 return(ncolors);
299 greg 1.1 free((char *)pixval);
300     }
301 greg 1.17 return(ncolors = 0);
302 greg 1.1 }
303    
304    
305     static
306 greg 1.12 freepixels() /* free our pixels */
307 greg 1.1 {
308     if (ncolors == 0)
309     return;
310     XFreeColors(pixval, ncolors, 0);
311     free((char *)pixval);
312     ncolors = 0;
313     }
314    
315    
316     static int
317     x_getc() /* get a command character */
318     {
319     while (c_last <= c_first) {
320     c_first = c_last = 0; /* reset */
321     getevent(); /* wait for key */
322     }
323     x_driver.inpready--;
324     return(c_queue[c_first++]);
325     }
326    
327    
328     static
329     getevent() /* get next event */
330     {
331     XNextEvent(levptr(XEvent));
332     switch (levptr(XEvent)->type) {
333     case KeyPressed:
334     getkey(levptr(XKeyPressedEvent));
335     break;
336     case ExposeWindow:
337 greg 1.18 windowchange(levptr(XExposeEvent));
338     break;
339 greg 1.1 case ExposeRegion:
340     fixwindow(levptr(XExposeEvent));
341     break;
342     case UnmapWindow:
343     if (levptr(XUnmapEvent)->subwindow == 0)
344 greg 1.12 freepixels();
345 greg 1.1 break;
346     case ButtonPressed: /* handled in x_getcur() */
347     break;
348     }
349     }
350    
351    
352     static
353 greg 1.18 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 greg 1.19 if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
362 greg 1.24 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 greg 1.18 strcpy(getcombuf(&x_driver), "new\n");
367     return;
368     }
369     /* remap colors */
370 greg 1.22 if (getpixels() == 0) {
371 gregl 2.6 eputs("cannot allocate colors\n");
372 greg 1.18 return;
373     }
374     new_ctab(ncolors);
375     /* redraw */
376     fixwindow(eexp);
377     }
378    
379    
380     static
381 greg 1.1 getkey(ekey) /* get input key */
382     register XKeyPressedEvent *ekey;
383     {
384     int n;
385     register char *str;
386    
387     str = XLookupMapping(ekey, &n);
388 greg 1.20 while (n-- > 0 && c_last < sizeof(c_queue)) {
389 greg 1.1 c_queue[c_last++] = *str++;
390 greg 1.20 x_driver.inpready++;
391     }
392 greg 1.1 }
393    
394    
395     static
396     fixwindow(eexp) /* repair damage to window */
397     register XExposeEvent *eexp;
398     {
399     if (eexp->subwindow == 0)
400 greg 1.18 sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
401     eexp->x, gheight - eexp->y - eexp->height,
402 greg 1.1 eexp->x + eexp->width, gheight - eexp->y);
403     else if (eexp->subwindow == comline->w)
404     xt_redraw(comline);
405     }