ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.21
Committed: Wed Jan 10 10:04:30 1990 UTC (35 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.20: +3 -1 lines
Log Message:
changed call to XCreate()

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 XEvent thisevent; /* current event */
48    
49     static int ncolors = 0; /* color table size */
50 greg 1.12 static int *pixval; /* allocated pixel values */
51 greg 1.1
52     static Display *ourdisplay = NULL; /* our display */
53    
54     static Window gwind = 0; /* our graphics window */
55    
56     static Cursor pickcursor = 0; /* cursor used for picking */
57    
58     static int gwidth = 0; /* graphics window width */
59     static int gheight = 0; /* graphics window height */
60    
61     static TEXTWIND *comline = NULL; /* our command line */
62    
63     static char c_queue[64]; /* input queue */
64     static int c_first = 0; /* first character in queue */
65     static int c_last = 0; /* last character in queue */
66    
67 greg 1.18 extern char *malloc(), *getcombuf();
68 greg 1.1
69 greg 1.21 extern char *progname;
70    
71 greg 1.1 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 greg 1.19 char defgeom[32];
85     OpaqueFrame mainframe;
86    
87 greg 1.1 ourdisplay = XOpenDisplay(NULL);
88     if (ourdisplay == NULL) {
89 greg 1.2 stderr_v("cannot open X-windows; DISPLAY variable set?\n");
90 greg 1.1 return(NULL);
91     }
92     if (DisplayPlanes() < 4) {
93     stderr_v("not enough colors\n");
94     return(NULL);
95     }
96 greg 1.14 make_gmap(GAMMA); /* make color map */
97 greg 1.1
98     pickcursor = XCreateCursor(bcross_width, bcross_height,
99     bcross_bits, bcross_mask_bits,
100     bcross_x_hot, bcross_y_hot,
101     BlackPixel, WhitePixel, GXcopy);
102 greg 1.19 mainframe.bdrwidth = BORWIDTH;
103     mainframe.border = BlackPixmap;
104     mainframe.background = BlackPixmap;
105     sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH),
106     DisplayHeight()-(2*BORWIDTH+22));
107 greg 1.21 gwind = XCreate("X10 display driver", progname, NULL, defgeom,
108 greg 1.19 &mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT);
109     if (gwind == 0) {
110     stderr_v("can't create window\n");
111     return(NULL);
112     }
113     XStoreName(gwind, id);
114     XMapWindow(gwind);
115     XSelectInput(gwind, KeyPressed|ButtonPressed|
116     ExposeWindow|ExposeRegion|UnmapWindow);
117     x_driver.xsiz = mainframe.width;
118     x_driver.ysiz = mainframe.height-COMHEIGHT;
119 greg 1.1 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 greg 1.12 freepixels();
146 greg 1.1 XCloseDisplay(ourdisplay);
147     ourdisplay = NULL;
148     }
149    
150    
151     static
152     x_clear(xres, yres) /* clear our display */
153     int xres, yres;
154     {
155 greg 1.18 if (xres < MINWIDTH)
156     xres = MINWIDTH;
157     if (yres < MINHEIGHT)
158     yres = MINHEIGHT;
159 greg 1.1 if (xres != gwidth || yres != gheight) { /* new window */
160     if (comline != NULL)
161     xt_close(comline);
162 greg 1.19 XChangeWindow(gwind, xres, yres+COMHEIGHT);
163 greg 1.1 comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
164 greg 1.19 if (comline == NULL) {
165     stderr_v("Cannot open command line window\n");
166     quit(1);
167     }
168 greg 1.1 gwidth = xres;
169     gheight = yres;
170     } else /* just clear */
171     XClear(gwind);
172 greg 1.17 /* reinitialize color table */
173     if (ncolors == 0 && getpixels() == 0)
174     stderr_v("cannot allocate colors\n");
175     else
176     new_ctab(ncolors);
177     XSync(1); /* discard input */
178 greg 1.1 return;
179     }
180    
181    
182     static
183     x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
184     COLOR col;
185     int xmin, ymin, xmax, ymax;
186     {
187     extern long nrays; /* global ray count */
188 greg 1.14 extern int xnewcolr(); /* pixel assignment routine */
189 greg 1.1 static long lastflush = 0; /* ray count at last flush */
190    
191     if (ncolors > 0) {
192     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
193 greg 1.14 pixval[get_pixel(col, xnewcolr)]);
194 greg 1.1 }
195     if (nrays - lastflush >= WFLUSH) {
196 greg 1.9 if (ncolors <= 0) /* output necessary for death */
197     XPixSet(gwind,0,0,1,1,BlackPixel);
198 greg 1.8 checkinp();
199 greg 1.1 lastflush = nrays;
200     }
201     }
202    
203    
204     static
205     x_comin(inp) /* read in a command line */
206     char *inp;
207     {
208     int x_getc(), x_comout();
209    
210 greg 1.18 if (fromcombuf(inp, &x_driver))
211     return;
212 greg 1.1 xt_cursor(comline, TBLKCURS);
213 greg 1.11 editline(inp, x_getc, x_comout);
214 greg 1.1 xt_cursor(comline, TNOCURS);
215     }
216    
217    
218     static
219     x_comout(out) /* output a string to command line */
220     char *out;
221     {
222     if (comline != NULL)
223     xt_puts(out, comline);
224 greg 1.8 XFlush();
225 greg 1.1 }
226    
227    
228     static
229     x_errout(msg) /* output an error message */
230     char *msg;
231     {
232     x_comout(msg);
233     stderr_v(msg); /* send to stderr also! */
234     }
235    
236    
237     static int
238     x_getcur(xp, yp) /* get cursor position */
239     int *xp, *yp;
240     {
241     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
242     sleep(1);
243     XFocusKeyboard(gwind);
244     do
245     getevent();
246     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
247     *xp = levptr(XKeyOrButtonEvent)->x;
248     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
249     XFocusKeyboard(RootWindow);
250     XUngrabMouse();
251 greg 1.8 XFlush(); /* insure release */
252 greg 1.1 if (c_last > c_first) /* key pressed */
253     return(x_getc());
254     /* button pressed */
255     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
256     case LeftButton:
257     return(MB1);
258     case MiddleButton:
259     return(MB2);
260     case RightButton:
261     return(MB3);
262     }
263     return(ABORT);
264     }
265    
266    
267     static
268 greg 1.13 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
269 greg 1.1 int ndx;
270 greg 1.13 int r, g, b;
271 greg 1.1 {
272     Color xcolor;
273    
274     xcolor.pixel = pixval[ndx];
275 greg 1.13 xcolor.red = r << 8;
276     xcolor.green = g << 8;
277     xcolor.blue = b << 8;
278 greg 1.1
279     XStoreColor(&xcolor);
280     }
281    
282    
283 greg 1.17 static int
284 greg 1.12 getpixels() /* get the color map */
285 greg 1.1 {
286     int planes;
287    
288 greg 1.17 freepixels();
289 greg 1.1 for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
290     pixval = (int *)malloc(ncolors*sizeof(int));
291 greg 1.12 if (pixval == NULL)
292     break;
293 greg 1.1 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
294 greg 1.17 return(ncolors);
295 greg 1.1 free((char *)pixval);
296     }
297 greg 1.17 return(ncolors = 0);
298 greg 1.1 }
299    
300    
301     static
302 greg 1.12 freepixels() /* free our pixels */
303 greg 1.1 {
304     if (ncolors == 0)
305     return;
306     XFreeColors(pixval, ncolors, 0);
307     free((char *)pixval);
308     ncolors = 0;
309     }
310    
311    
312     static int
313     x_getc() /* get a command character */
314     {
315     while (c_last <= c_first) {
316     c_first = c_last = 0; /* reset */
317     getevent(); /* wait for key */
318     }
319     x_driver.inpready--;
320     return(c_queue[c_first++]);
321     }
322    
323    
324     static
325     getevent() /* get next event */
326     {
327     XNextEvent(levptr(XEvent));
328     switch (levptr(XEvent)->type) {
329     case KeyPressed:
330     getkey(levptr(XKeyPressedEvent));
331     break;
332     case ExposeWindow:
333 greg 1.18 windowchange(levptr(XExposeEvent));
334     break;
335 greg 1.1 case ExposeRegion:
336     fixwindow(levptr(XExposeEvent));
337     break;
338     case UnmapWindow:
339     if (levptr(XUnmapEvent)->subwindow == 0)
340 greg 1.12 freepixels();
341 greg 1.1 break;
342     case ButtonPressed: /* handled in x_getcur() */
343     break;
344     }
345     }
346    
347    
348     static
349 greg 1.18 windowchange(eexp) /* process window change event */
350     register XExposeEvent *eexp;
351     {
352     if (eexp->subwindow != 0) {
353     fixwindow(eexp);
354     return;
355     }
356     /* check for change in size */
357 greg 1.19 if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
358 greg 1.18 x_driver.xsiz = eexp->width;
359 greg 1.19 x_driver.ysiz = eexp->height-COMHEIGHT;
360 greg 1.18 strcpy(getcombuf(&x_driver), "new\n");
361     return;
362     }
363     /* remap colors */
364     if (ncolors == 0 && getpixels() == 0) {
365     stderr_v("cannot allocate colors\n");
366     return;
367     }
368     new_ctab(ncolors);
369     /* redraw */
370     fixwindow(eexp);
371     }
372    
373    
374     static
375 greg 1.1 getkey(ekey) /* get input key */
376     register XKeyPressedEvent *ekey;
377     {
378     int n;
379     register char *str;
380    
381     str = XLookupMapping(ekey, &n);
382 greg 1.20 while (n-- > 0 && c_last < sizeof(c_queue)) {
383 greg 1.1 c_queue[c_last++] = *str++;
384 greg 1.20 x_driver.inpready++;
385     }
386 greg 1.1 }
387    
388    
389     static
390     fixwindow(eexp) /* repair damage to window */
391     register XExposeEvent *eexp;
392     {
393     if (eexp->subwindow == 0)
394 greg 1.18 sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
395     eexp->x, gheight - eexp->y - eexp->height,
396 greg 1.1 eexp->x + eexp->width, gheight - eexp->y);
397     else if (eexp->subwindow == comline->w)
398     xt_redraw(comline);
399     }