ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.18
Committed: Mon Jan 8 13:38:41 1990 UTC (35 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +43 -14 lines
Log Message:
Changed handling of view parameters

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 COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */
31 greg 1.18 #define MINWIDTH (32*COMCW) /* minimum graphics window width */
32     #define MINHEIGHT 64 /* minimum graphics window height */
33 greg 1.1
34     #define COMFN "8x13" /* command line font name */
35     #define COMLH 3 /* number of command lines */
36     #define COMCW 8 /* approx. character width (pixels) */
37     #define COMCH 13 /* approx. character height (pixels) */
38    
39     #ifndef WFLUSH
40 greg 1.10 #define WFLUSH 30 /* flush after this many rays */
41 greg 1.1 #endif
42    
43 greg 1.8 #define checkinp() while (XPending() > 0) getevent()
44 greg 1.1
45     #define levptr(etype) ((etype *)&thisevent)
46    
47     static char *clientname; /* calling client's name */
48    
49     static XEvent thisevent; /* current event */
50    
51     static int ncolors = 0; /* color table size */
52 greg 1.12 static int *pixval; /* allocated pixel values */
53 greg 1.1
54     static Display *ourdisplay = NULL; /* our display */
55    
56     static Window gwind = 0; /* our graphics window */
57    
58     static Cursor pickcursor = 0; /* cursor used for picking */
59    
60     static int gwidth = 0; /* graphics window width */
61     static int gheight = 0; /* graphics window height */
62    
63     static TEXTWIND *comline = NULL; /* our command line */
64    
65     static char c_queue[64]; /* input queue */
66     static int c_first = 0; /* first character in queue */
67     static int c_last = 0; /* last character in queue */
68    
69 greg 1.18 extern char *malloc(), *getcombuf();
70 greg 1.1
71     int x_close(), x_clear(), x_paintr(), x_errout(),
72     x_getcur(), x_comout(), x_comin();
73    
74     static struct driver x_driver = {
75     x_close, x_clear, x_paintr, x_getcur,
76 greg 1.18 x_comout, x_comin, 1.0
77 greg 1.1 };
78    
79    
80     struct driver *
81 greg 1.16 x_init(name, id) /* initialize driver */
82     char *name, *id;
83 greg 1.1 {
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.14 make_gmap(GAMMA); /* make color map */
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.16 clientname = id;
100 greg 1.18 x_driver.xsiz = DisplayWidth()-(2*BORWIDTH);
101     x_driver.ysiz = DisplayHeight()-(COMHEIGHT+2*BORWIDTH);
102 greg 1.1 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 greg 1.18 if (xres < MINWIDTH)
139     xres = MINWIDTH;
140     if (yres < MINHEIGHT)
141     yres = MINHEIGHT;
142 greg 1.1 if (xres != gwidth || yres != gheight) { /* new window */
143     if (comline != NULL)
144     xt_close(comline);
145 greg 1.17 if (gwind == 0) {
146 greg 1.18 gwind = XCreateWindow(RootWindow, 0, 0,
147 greg 1.17 xres, yres+COMHEIGHT, BORWIDTH,
148     BlackPixmap, BlackPixmap);
149     if (gwind == 0)
150     goto fail;
151     XStoreName(gwind, clientname);
152     XSelectInput(gwind, KeyPressed|ButtonPressed|
153     ExposeWindow|ExposeRegion|UnmapWindow);
154     XMapWindow(gwind);
155     } else
156     XChangeWindow(gwind, xres, yres+COMHEIGHT);
157 greg 1.1 comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
158     if (comline == NULL)
159     goto fail;
160     gwidth = xres;
161     gheight = yres;
162     } else /* just clear */
163     XClear(gwind);
164 greg 1.17 /* reinitialize color table */
165     if (ncolors == 0 && getpixels() == 0)
166     stderr_v("cannot allocate colors\n");
167     else
168     new_ctab(ncolors);
169     XSync(1); /* discard input */
170 greg 1.1 return;
171     fail:
172     stderr_v("Failure opening window in x_clear\n");
173     quit(1);
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     extern long nrays; /* global ray count */
183 greg 1.14 extern int xnewcolr(); /* pixel assignment routine */
184 greg 1.1 static long lastflush = 0; /* ray count at last flush */
185    
186     if (ncolors > 0) {
187     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
188 greg 1.14 pixval[get_pixel(col, xnewcolr)]);
189 greg 1.1 }
190     if (nrays - lastflush >= WFLUSH) {
191 greg 1.9 if (ncolors <= 0) /* output necessary for death */
192     XPixSet(gwind,0,0,1,1,BlackPixel);
193 greg 1.8 checkinp();
194 greg 1.1 lastflush = nrays;
195     }
196     }
197    
198    
199     static
200     x_comin(inp) /* read in a command line */
201     char *inp;
202     {
203     int x_getc(), x_comout();
204    
205 greg 1.18 if (fromcombuf(inp, &x_driver))
206     return;
207 greg 1.1 xt_cursor(comline, TBLKCURS);
208 greg 1.11 editline(inp, x_getc, x_comout);
209 greg 1.1 xt_cursor(comline, TNOCURS);
210     }
211    
212    
213     static
214     x_comout(out) /* output a string to command line */
215     char *out;
216     {
217     if (comline != NULL)
218     xt_puts(out, comline);
219 greg 1.8 XFlush();
220 greg 1.1 }
221    
222    
223     static
224     x_errout(msg) /* output an error message */
225     char *msg;
226     {
227     x_comout(msg);
228     stderr_v(msg); /* send to stderr also! */
229     }
230    
231    
232     static int
233     x_getcur(xp, yp) /* get cursor position */
234     int *xp, *yp;
235     {
236     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
237     sleep(1);
238     XFocusKeyboard(gwind);
239     do
240     getevent();
241     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
242     *xp = levptr(XKeyOrButtonEvent)->x;
243     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
244     XFocusKeyboard(RootWindow);
245     XUngrabMouse();
246 greg 1.8 XFlush(); /* insure release */
247 greg 1.1 if (c_last > c_first) /* key pressed */
248     return(x_getc());
249     /* button pressed */
250     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
251     case LeftButton:
252     return(MB1);
253     case MiddleButton:
254     return(MB2);
255     case RightButton:
256     return(MB3);
257     }
258     return(ABORT);
259     }
260    
261    
262     static
263 greg 1.13 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
264 greg 1.1 int ndx;
265 greg 1.13 int r, g, b;
266 greg 1.1 {
267     Color xcolor;
268    
269     xcolor.pixel = pixval[ndx];
270 greg 1.13 xcolor.red = r << 8;
271     xcolor.green = g << 8;
272     xcolor.blue = b << 8;
273 greg 1.1
274     XStoreColor(&xcolor);
275     }
276    
277    
278 greg 1.17 static int
279 greg 1.12 getpixels() /* get the color map */
280 greg 1.1 {
281     int planes;
282    
283 greg 1.17 freepixels();
284 greg 1.1 for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
285     pixval = (int *)malloc(ncolors*sizeof(int));
286 greg 1.12 if (pixval == NULL)
287     break;
288 greg 1.1 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
289 greg 1.17 return(ncolors);
290 greg 1.1 free((char *)pixval);
291     }
292 greg 1.17 return(ncolors = 0);
293 greg 1.1 }
294    
295    
296     static
297 greg 1.12 freepixels() /* free our pixels */
298 greg 1.1 {
299     if (ncolors == 0)
300     return;
301     XFreeColors(pixval, ncolors, 0);
302     free((char *)pixval);
303     ncolors = 0;
304     }
305    
306    
307     static int
308     x_getc() /* get a command character */
309     {
310     while (c_last <= c_first) {
311     c_first = c_last = 0; /* reset */
312     getevent(); /* wait for key */
313     }
314     x_driver.inpready--;
315     return(c_queue[c_first++]);
316     }
317    
318    
319     static
320     getevent() /* get next event */
321     {
322     XNextEvent(levptr(XEvent));
323     switch (levptr(XEvent)->type) {
324     case KeyPressed:
325     getkey(levptr(XKeyPressedEvent));
326     break;
327     case ExposeWindow:
328 greg 1.18 windowchange(levptr(XExposeEvent));
329     break;
330 greg 1.1 case ExposeRegion:
331     fixwindow(levptr(XExposeEvent));
332     break;
333     case UnmapWindow:
334     if (levptr(XUnmapEvent)->subwindow == 0)
335 greg 1.12 freepixels();
336 greg 1.1 break;
337     case ButtonPressed: /* handled in x_getcur() */
338     break;
339     }
340     }
341    
342    
343     static
344 greg 1.18 windowchange(eexp) /* process window change event */
345     register XExposeEvent *eexp;
346     {
347     if (eexp->subwindow != 0) {
348     fixwindow(eexp);
349     return;
350     }
351     /* check for change in size */
352     if (eexp->width != gwidth || eexp->height != gheight+COMHEIGHT) {
353     x_driver.xsiz = eexp->width;
354     x_driver.ysiz = eexp->height;
355     strcpy(getcombuf(&x_driver), "new\n");
356     return;
357     }
358     /* remap colors */
359     if (ncolors == 0 && getpixels() == 0) {
360     stderr_v("cannot allocate colors\n");
361     return;
362     }
363     new_ctab(ncolors);
364     /* redraw */
365     fixwindow(eexp);
366     }
367    
368    
369     static
370 greg 1.1 getkey(ekey) /* get input key */
371     register XKeyPressedEvent *ekey;
372     {
373     int n;
374     register char *str;
375    
376     str = XLookupMapping(ekey, &n);
377     while (n-- > 0 && c_last < sizeof(c_queue))
378     c_queue[c_last++] = *str++;
379     x_driver.inpready = c_last - c_first;
380     }
381    
382    
383     static
384     fixwindow(eexp) /* repair damage to window */
385     register XExposeEvent *eexp;
386     {
387     if (eexp->subwindow == 0)
388 greg 1.18 sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
389     eexp->x, gheight - eexp->y - eexp->height,
390 greg 1.1 eexp->x + eexp->width, gheight - eexp->y);
391     else if (eexp->subwindow == comline->w)
392     xt_redraw(comline);
393     }