ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.15
Committed: Wed Oct 4 09:18:54 1989 UTC (35 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +2 -4 lines
Log Message:
Changed new_ctab() error handling

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.14 make_gmap(GAMMA); /* make color map */
94 greg 1.12 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.15 /* redo color table */
160     new_ctab(ncolors);
161 greg 1.8 checkinp();
162 greg 1.1 return;
163     fail:
164     stderr_v("Failure opening window in x_clear\n");
165     quit(1);
166     }
167    
168    
169     static
170     x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
171     COLOR col;
172     int xmin, ymin, xmax, ymax;
173     {
174     extern long nrays; /* global ray count */
175 greg 1.14 extern int xnewcolr(); /* pixel assignment routine */
176 greg 1.1 static long lastflush = 0; /* ray count at last flush */
177    
178     if (ncolors > 0) {
179     XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
180 greg 1.14 pixval[get_pixel(col, xnewcolr)]);
181 greg 1.1 }
182     if (nrays - lastflush >= WFLUSH) {
183 greg 1.9 if (ncolors <= 0) /* output necessary for death */
184     XPixSet(gwind,0,0,1,1,BlackPixel);
185 greg 1.8 checkinp();
186 greg 1.1 lastflush = nrays;
187     }
188     }
189    
190    
191     static
192     x_comin(inp) /* read in a command line */
193     char *inp;
194     {
195     int x_getc(), x_comout();
196    
197     xt_cursor(comline, TBLKCURS);
198 greg 1.11 editline(inp, x_getc, x_comout);
199 greg 1.1 xt_cursor(comline, TNOCURS);
200     }
201    
202    
203     static
204     x_comout(out) /* output a string to command line */
205     char *out;
206     {
207     if (comline != NULL)
208     xt_puts(out, comline);
209 greg 1.8 XFlush();
210 greg 1.1 }
211    
212    
213     static
214     x_errout(msg) /* output an error message */
215     char *msg;
216     {
217     x_comout(msg);
218     stderr_v(msg); /* send to stderr also! */
219     }
220    
221    
222     static int
223     x_getcur(xp, yp) /* get cursor position */
224     int *xp, *yp;
225     {
226     while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
227     sleep(1);
228     XFocusKeyboard(gwind);
229     do
230     getevent();
231     while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
232     *xp = levptr(XKeyOrButtonEvent)->x;
233     *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
234     XFocusKeyboard(RootWindow);
235     XUngrabMouse();
236 greg 1.8 XFlush(); /* insure release */
237 greg 1.1 if (c_last > c_first) /* key pressed */
238     return(x_getc());
239     /* button pressed */
240     switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
241     case LeftButton:
242     return(MB1);
243     case MiddleButton:
244     return(MB2);
245     case RightButton:
246     return(MB3);
247     }
248     return(ABORT);
249     }
250    
251    
252     static
253 greg 1.13 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
254 greg 1.1 int ndx;
255 greg 1.13 int r, g, b;
256 greg 1.1 {
257     Color xcolor;
258    
259     xcolor.pixel = pixval[ndx];
260 greg 1.13 xcolor.red = r << 8;
261     xcolor.green = g << 8;
262     xcolor.blue = b << 8;
263 greg 1.1
264     XStoreColor(&xcolor);
265     }
266    
267    
268     static
269 greg 1.12 getpixels() /* get the color map */
270 greg 1.1 {
271     int planes;
272    
273     for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
274     pixval = (int *)malloc(ncolors*sizeof(int));
275 greg 1.12 if (pixval == NULL)
276     break;
277 greg 1.1 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
278     return(0);
279     free((char *)pixval);
280     }
281     ncolors = 0;
282     return(-1);
283     }
284    
285    
286     static
287 greg 1.12 freepixels() /* free our pixels */
288 greg 1.1 {
289     if (ncolors == 0)
290     return;
291     XFreeColors(pixval, ncolors, 0);
292     free((char *)pixval);
293     ncolors = 0;
294     }
295    
296    
297     static int
298     x_getc() /* get a command character */
299     {
300     while (c_last <= c_first) {
301     c_first = c_last = 0; /* reset */
302     getevent(); /* wait for key */
303     }
304     x_driver.inpready--;
305     return(c_queue[c_first++]);
306     }
307    
308    
309     static
310     getevent() /* get next event */
311     {
312     XNextEvent(levptr(XEvent));
313     switch (levptr(XEvent)->type) {
314     case KeyPressed:
315     getkey(levptr(XKeyPressedEvent));
316     break;
317     case ExposeWindow:
318 greg 1.13 if (levptr(XExposeEvent)->subwindow == 0) {
319     if (ncolors == 0 && getpixels() < 0) {
320     stderr_v("cannot allocate colors\n");
321     break;
322     }
323 greg 1.14 new_ctab(ncolors);
324 greg 1.13 }
325 greg 1.1 /* fall through */
326     case ExposeRegion:
327     fixwindow(levptr(XExposeEvent));
328     break;
329     case UnmapWindow:
330     if (levptr(XUnmapEvent)->subwindow == 0)
331 greg 1.12 freepixels();
332 greg 1.1 break;
333     case ButtonPressed: /* handled in x_getcur() */
334     break;
335     }
336     }
337    
338    
339     static
340     getkey(ekey) /* get input key */
341     register XKeyPressedEvent *ekey;
342     {
343     int n;
344     register char *str;
345    
346     str = XLookupMapping(ekey, &n);
347     while (n-- > 0 && c_last < sizeof(c_queue))
348     c_queue[c_last++] = *str++;
349     x_driver.inpready = c_last - c_first;
350     }
351    
352    
353     static
354     fixwindow(eexp) /* repair damage to window */
355     register XExposeEvent *eexp;
356     {
357     if (eexp->subwindow == 0)
358     repaint(eexp->x, gheight - eexp->y - eexp->height,
359     eexp->x + eexp->width, gheight - eexp->y);
360     else if (eexp->subwindow == comline->w)
361     xt_redraw(comline);
362     }