ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x10.c
Revision: 1.22
Committed: Tue Jan 30 09:47:21 1990 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +4 -3 lines
Log Message:
changed allocation and freeing of color table slightly

File Contents

# Content
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 #define GAMMA 2.2 /* exponent for color correction */
28
29 #define BORWIDTH 5 /* border width */
30 #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */
31 #define MINWIDTH (32*COMCW) /* minimum graphics window width */
32 #define MINHEIGHT 64 /* minimum graphics window height */
33
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 #define WFLUSH 30 /* flush after this many rays */
41 #endif
42
43 #define checkinp() while (XPending() > 0) getevent()
44
45 #define levptr(etype) ((etype *)&thisevent)
46
47 static XEvent thisevent; /* current event */
48
49 static int ncolors = 0; /* color table size */
50 static int *pixval; /* allocated pixel values */
51
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 extern char *malloc(), *getcombuf();
68
69 extern char *progname;
70
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 x_comout, x_comin, 1.0
77 };
78
79
80 struct driver *
81 x_init(name, id) /* initialize driver */
82 char *name, *id;
83 {
84 char defgeom[32];
85 OpaqueFrame mainframe;
86
87 ourdisplay = XOpenDisplay(NULL);
88 if (ourdisplay == NULL) {
89 stderr_v("cannot open X-windows; DISPLAY variable set?\n");
90 return(NULL);
91 }
92 if (DisplayPlanes() < 4) {
93 stderr_v("not enough colors\n");
94 return(NULL);
95 }
96 make_gmap(GAMMA); /* make color map */
97
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 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 gwind = XCreate("X10 display driver", progname, NULL, defgeom,
108 &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 x_driver.inpready = 0;
120 cmdvec = x_comout; /* set error vectors */
121 if (wrnvec != NULL)
122 wrnvec = x_errout;
123 return(&x_driver);
124 }
125
126
127 static
128 x_close() /* close our display */
129 {
130 cmdvec = NULL; /* reset error vectors */
131 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 freepixels();
146 XCloseDisplay(ourdisplay);
147 ourdisplay = NULL;
148 }
149
150
151 static
152 x_clear(xres, yres) /* clear our display */
153 int xres, yres;
154 {
155 if (xres < MINWIDTH)
156 xres = MINWIDTH;
157 if (yres < MINHEIGHT)
158 yres = MINHEIGHT;
159 if (xres != gwidth || yres != gheight) { /* new window */
160 if (comline != NULL)
161 xt_close(comline);
162 XChangeWindow(gwind, xres, yres+COMHEIGHT);
163 comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
164 if (comline == NULL) {
165 stderr_v("Cannot open command line window\n");
166 quit(1);
167 }
168 gwidth = xres;
169 gheight = yres;
170 } else /* just clear */
171 XClear(gwind);
172 /* reinitialize color table */
173 if (getpixels() == 0)
174 stderr_v("cannot allocate colors\n");
175 else
176 new_ctab(ncolors);
177 XSync(1); /* discard input */
178 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 extern int xnewcolr(); /* pixel assignment routine */
189 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 pixval[get_pixel(col, xnewcolr)]);
194 }
195 if (nrays - lastflush >= WFLUSH) {
196 if (ncolors <= 0) /* output necessary for death */
197 XPixSet(gwind,0,0,1,1,BlackPixel);
198 checkinp();
199 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 if (fromcombuf(inp, &x_driver))
211 return;
212 xt_cursor(comline, TBLKCURS);
213 editline(inp, x_getc, x_comout);
214 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 XFlush();
225 }
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 XFlush(); /* insure release */
252 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 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
269 int ndx;
270 int r, g, b;
271 {
272 Color xcolor;
273
274 xcolor.pixel = pixval[ndx];
275 xcolor.red = r << 8;
276 xcolor.green = g << 8;
277 xcolor.blue = b << 8;
278
279 XStoreColor(&xcolor);
280 }
281
282
283 static int
284 getpixels() /* get the color map */
285 {
286 int planes;
287
288 if (ncolors > 0)
289 return(ncolors);
290 for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
291 pixval = (int *)malloc(ncolors*sizeof(int));
292 if (pixval == NULL)
293 break;
294 if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
295 return(ncolors);
296 free((char *)pixval);
297 }
298 return(ncolors = 0);
299 }
300
301
302 static
303 freepixels() /* free our pixels */
304 {
305 if (ncolors == 0)
306 return;
307 XFreeColors(pixval, ncolors, 0);
308 free((char *)pixval);
309 ncolors = 0;
310 }
311
312
313 static int
314 x_getc() /* get a command character */
315 {
316 while (c_last <= c_first) {
317 c_first = c_last = 0; /* reset */
318 getevent(); /* wait for key */
319 }
320 x_driver.inpready--;
321 return(c_queue[c_first++]);
322 }
323
324
325 static
326 getevent() /* get next event */
327 {
328 XNextEvent(levptr(XEvent));
329 switch (levptr(XEvent)->type) {
330 case KeyPressed:
331 getkey(levptr(XKeyPressedEvent));
332 break;
333 case ExposeWindow:
334 windowchange(levptr(XExposeEvent));
335 break;
336 case ExposeRegion:
337 fixwindow(levptr(XExposeEvent));
338 break;
339 case UnmapWindow:
340 if (levptr(XUnmapEvent)->subwindow == 0)
341 freepixels();
342 break;
343 case ButtonPressed: /* handled in x_getcur() */
344 break;
345 }
346 }
347
348
349 static
350 windowchange(eexp) /* process window change event */
351 register XExposeEvent *eexp;
352 {
353 if (eexp->subwindow != 0) {
354 fixwindow(eexp);
355 return;
356 }
357 /* check for change in size */
358 if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) {
359 x_driver.xsiz = eexp->width;
360 x_driver.ysiz = eexp->height-COMHEIGHT;
361 strcpy(getcombuf(&x_driver), "new\n");
362 return;
363 }
364 /* remap colors */
365 if (getpixels() == 0) {
366 stderr_v("cannot allocate colors\n");
367 return;
368 }
369 new_ctab(ncolors);
370 /* redraw */
371 fixwindow(eexp);
372 }
373
374
375 static
376 getkey(ekey) /* get input key */
377 register XKeyPressedEvent *ekey;
378 {
379 int n;
380 register char *str;
381
382 str = XLookupMapping(ekey, &n);
383 while (n-- > 0 && c_last < sizeof(c_queue)) {
384 c_queue[c_last++] = *str++;
385 x_driver.inpready++;
386 }
387 }
388
389
390 static
391 fixwindow(eexp) /* repair damage to window */
392 register XExposeEvent *eexp;
393 {
394 if (eexp->subwindow == 0)
395 sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n",
396 eexp->x, gheight - eexp->y - eexp->height,
397 eexp->x + eexp->width, gheight - eexp->y);
398 else if (eexp->subwindow == comline->w)
399 xt_redraw(comline);
400 }