ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 2.4
Committed: Fri Sep 9 15:10:25 1994 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +0 -1 lines
Log Message:
removed unneeded reference to ioctl.h

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