| 1 |
/* |
| 2 |
* irisio.c - Nina Amenta, Aug. 1989 |
| 3 |
* kaliprint.c - Nina Amenta, Aug. 1989 * |
| 4 |
* |
| 5 |
* modified: merged kaliprint and irisio into io, |
| 6 |
* so can print directly from kali as well as kaliprint. |
| 7 |
* 7/94 Tamara Munzner |
| 8 |
* |
| 9 |
* entirely rewritten by Ed H. Chi |
| 10 |
* |
| 11 |
* $Id: xio.c,v 1.3 1996/10/07 15:56:06 slevy Exp $ |
| 12 |
* $Log: xio.c,v $ |
| 13 |
* Revision 1.3 1996/10/07 15:56:06 slevy |
| 14 |
* XTextProperties want unsigned chars. |
| 15 |
* |
| 16 |
* Revision 1.2 1996/10/05 02:13:04 slevy |
| 17 |
* Get int vs. unsigned int's correct for XGetGeometry() params. |
| 18 |
* |
| 19 |
* Revision 1.1 1996/10/05 01:32:41 slevy |
| 20 |
* Initial revision |
| 21 |
* |
| 22 |
* Revision 1.1 1996/06/11 04:57:54 slevy |
| 23 |
* Initial revision |
| 24 |
* |
| 25 |
* Revision 1.4 1994/09/12 23:14:41 chi |
| 26 |
* version 3.0 changes |
| 27 |
* header file recreated |
| 28 |
* |
| 29 |
* Revision 1.3 1994/08/26 16:29:02 chi |
| 30 |
* rewritten by Ed H. Chi (chi@geom) |
| 31 |
* start of a port to X11 |
| 32 |
* |
| 33 |
* Revision 1.2 1994/08/15 20:05:38 chi |
| 34 |
* significant code cleanup. |
| 35 |
* header files created. |
| 36 |
* main.h created |
| 37 |
* changed #defines in main.h from DRAW to KALIDRAW, CUT to .... etc |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <math.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <ctype.h> |
| 43 |
#include <stdlib.h> |
| 44 |
|
| 45 |
#define __EMX__ 1 /* Hack, to avoid conflict btwn gcc and Xlib wchar_t */ |
| 46 |
|
| 47 |
#include "main.h" |
| 48 |
#include "symmetry.h" |
| 49 |
#include "io.h" |
| 50 |
#include "kali.h" |
| 51 |
|
| 52 |
#include <X11/Xlib.h> |
| 53 |
#include <X11/Xutil.h> |
| 54 |
#include <X11/Xatom.h> |
| 55 |
|
| 56 |
#define MAXDEPTH 25 |
| 57 |
static POINT tstack[MAXDEPTH]; |
| 58 |
|
| 59 |
static float GXx0, GXy0; |
| 60 |
static int pushed = 0; |
| 61 |
|
| 62 |
struct GXdata { |
| 63 |
Display *dpy; |
| 64 |
Window w, wvis; |
| 65 |
GC gc; |
| 66 |
Colormap cmap; |
| 67 |
unsigned int depth; |
| 68 |
int xbase, ybase; |
| 69 |
unsigned int xsize, ysize; |
| 70 |
unsigned int screeny; |
| 71 |
float x0, xscale, y0, yscale; |
| 72 |
int dynvis; /* Dynamic Visual -- must we XFreeColor()? */ |
| 73 |
int abgr; /* Current color */ |
| 74 |
int hascpixel; |
| 75 |
unsigned long cpixel; |
| 76 |
Pixmap backpix; |
| 77 |
}; |
| 78 |
|
| 79 |
static struct GXdata *gxd; |
| 80 |
|
| 81 |
void GXinit(DRAWER *d, Display *dpy, Window w) |
| 82 |
{ |
| 83 |
Screen *s = DefaultScreenOfDisplay(dpy); |
| 84 |
if(d->data) |
| 85 |
free(d->data); |
| 86 |
|
| 87 |
d->data = malloc(sizeof(struct GXdata)); |
| 88 |
gxd = (struct GXdata *)(d->data); /* Save in static area */ |
| 89 |
gxd->dpy = dpy; |
| 90 |
gxd->w = gxd->wvis = w; |
| 91 |
gxd->x0 = gxd->y0 = 0; |
| 92 |
gxd->xscale = gxd->yscale = 1; |
| 93 |
gxd->screeny = 0; /* Not yet known */ |
| 94 |
gxd->hascpixel = 0; |
| 95 |
gxd->cmap = DefaultColormapOfScreen(s); |
| 96 |
gxd->dynvis = (DefaultVisualOfScreen(s)->class & 1) != 0; |
| 97 |
gxd->backpix = None; |
| 98 |
if(w == None) { |
| 99 |
gxd->xbase = gxd->ybase = 100; |
| 100 |
gxd->xsize = 640; gxd->ysize = 480; |
| 101 |
} else { |
| 102 |
gxd->gc = XCreateGC(dpy, w, 0, NULL); |
| 103 |
reshapeviewport(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
swapbuffers() |
| 108 |
{ |
| 109 |
if(gxd->backpix != None) |
| 110 |
XCopyArea(gxd->dpy, gxd->backpix, gxd->wvis, gxd->gc, |
| 111 |
0, 0, gxd->xsize, gxd->ysize, 0, 0); |
| 112 |
} |
| 113 |
|
| 114 |
winset(int w) |
| 115 |
{ /* Could do something useful, but for now just assume we have only 1 window */ |
| 116 |
} |
| 117 |
|
| 118 |
winconstraints() |
| 119 |
{} |
| 120 |
|
| 121 |
foreground() |
| 122 |
{} |
| 123 |
|
| 124 |
/* |
| 125 |
* Must be called after GXinit() |
| 126 |
*/ |
| 127 |
int |
| 128 |
winopen(char *title) |
| 129 |
{ |
| 130 |
Screen *s = DefaultScreenOfDisplay(gxd->dpy); |
| 131 |
XSetWindowAttributes swa; |
| 132 |
XTextProperty tp; |
| 133 |
XSizeHints szh; |
| 134 |
|
| 135 |
swa.backing_pixel = BlackPixelOfScreen(s); |
| 136 |
swa.event_mask = ButtonPressMask|ButtonReleaseMask|ButtonMotionMask |
| 137 |
|KeyPressMask|KeyReleaseMask|ExposureMask; |
| 138 |
gxd->w = gxd->wvis = |
| 139 |
XCreateWindow(gxd->dpy, RootWindowOfScreen(s), |
| 140 |
gxd->xbase, gxd->ybase, gxd->xsize, gxd->ysize, |
| 141 |
0, DefaultDepthOfScreen(s), InputOutput, |
| 142 |
DefaultVisualOfScreen(s), |
| 143 |
CWBackPixel|CWEventMask, &swa); |
| 144 |
gxd->gc = XCreateGC(gxd->dpy, gxd->w, 0, NULL); |
| 145 |
|
| 146 |
tp.value = (unsigned char *)title; |
| 147 |
tp.encoding = XA_STRING; |
| 148 |
tp.format = 8; |
| 149 |
tp.nitems = strlen(title); |
| 150 |
XSetWMName(gxd->dpy, gxd->wvis, &tp); |
| 151 |
|
| 152 |
szh.flags = USPosition; |
| 153 |
szh.x = gxd->xbase; |
| 154 |
szh.y = gxd->ybase; |
| 155 |
szh.width = gxd->xsize; |
| 156 |
szh.height = gxd->ysize; |
| 157 |
XSetWMSizeHints(gxd->dpy, gxd->wvis, &szh, XA_WM_NORMAL_HINTS); |
| 158 |
|
| 159 |
XMapWindow(gxd->dpy, gxd->wvis); |
| 160 |
reshapeviewport(); |
| 161 |
return gxd->wvis; |
| 162 |
} |
| 163 |
|
| 164 |
getsize(long *x, long *y) |
| 165 |
{ |
| 166 |
*x = gxd->xsize; |
| 167 |
*y = gxd->ysize; |
| 168 |
} |
| 169 |
|
| 170 |
getorigin(long *x, long *y) |
| 171 |
{ |
| 172 |
*x = gxd->xbase; |
| 173 |
*y = gxd->ybase; |
| 174 |
} |
| 175 |
|
| 176 |
doublebuffer() |
| 177 |
{ |
| 178 |
if(gxd->backpix != None) |
| 179 |
XFreePixmap(gxd->dpy, gxd->backpix); |
| 180 |
gxd->backpix = gxd->w = XCreatePixmap(gxd->dpy, gxd->wvis, |
| 181 |
gxd->xsize, gxd->ysize, gxd->depth); |
| 182 |
} |
| 183 |
|
| 184 |
gconfig() |
| 185 |
{ } |
| 186 |
|
| 187 |
RGBmode() |
| 188 |
{ } |
| 189 |
|
| 190 |
void |
| 191 |
prefposition(int x0, int x1, int y0, int y1) |
| 192 |
{ |
| 193 |
gxd->xbase = x0; |
| 194 |
gxd->ybase = y0; |
| 195 |
gxd->xsize = x1-x0+1; |
| 196 |
gxd->ysize = y1-y0+1; |
| 197 |
} |
| 198 |
|
| 199 |
reshapeviewport() |
| 200 |
{ |
| 201 |
Window rootw; |
| 202 |
int x0, y0; |
| 203 |
unsigned int screenx, border, depth; |
| 204 |
int oldx = gxd->xsize, oldy = gxd->ysize; |
| 205 |
|
| 206 |
XGetGeometry(gxd->dpy, gxd->wvis, &rootw, &gxd->xbase, &gxd->ybase, |
| 207 |
&gxd->xsize, &gxd->ysize, |
| 208 |
&border, &gxd->depth); |
| 209 |
if(gxd->backpix != None && (oldx != gxd->xsize || oldy != gxd->ysize)) |
| 210 |
doublebuffer(); |
| 211 |
if(gxd->screeny == 0) { |
| 212 |
XGetGeometry(gxd->dpy, rootw, &rootw, &x0,&y0, &screenx, |
| 213 |
&gxd->screeny, &border, &depth); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void |
| 218 |
ortho2(float left, float right, float bottom, float top) |
| 219 |
{ |
| 220 |
gxd->x0 = left; |
| 221 |
gxd->y0 = bottom; |
| 222 |
gxd->xscale = gxd->xsize / (right - left); |
| 223 |
gxd->yscale = gxd->ysize / (top - bottom); |
| 224 |
} |
| 225 |
|
| 226 |
#define MAXC 20 |
| 227 |
static int nc = 0; |
| 228 |
static struct knowncolor { |
| 229 |
unsigned long pixel; |
| 230 |
int abgr; |
| 231 |
} knowncolors[MAXC]; |
| 232 |
|
| 233 |
cpack(int abgr) |
| 234 |
{ |
| 235 |
XColor c; |
| 236 |
int i; |
| 237 |
|
| 238 |
if(gxd->abgr == abgr) |
| 239 |
return; |
| 240 |
|
| 241 |
for(i = nc; --i >= 0 && knowncolors[i].abgr != abgr; ) |
| 242 |
; |
| 243 |
if(i < 0) { |
| 244 |
if(nc >= MAXC) { |
| 245 |
if(gxd->dynvis) |
| 246 |
XFreeColors(gxd->dpy, gxd->cmap, &knowncolors[MAXC-1].pixel, 1, 0); |
| 247 |
nc--; |
| 248 |
} |
| 249 |
i = nc++; |
| 250 |
c.red = (abgr & 0xFF) * 257; |
| 251 |
c.green = ((abgr>>8)&0xFF) * 257; |
| 252 |
c.blue = ((abgr>>16)&0xFF) * 257; |
| 253 |
knowncolors[i].abgr = abgr; |
| 254 |
if(XAllocColor(gxd->dpy, gxd->cmap, &c)) { |
| 255 |
knowncolors[i].pixel = c.pixel; |
| 256 |
} else { |
| 257 |
knowncolors[i].pixel = (abgr == 0) |
| 258 |
? BlackPixelOfScreen(DefaultScreenOfDisplay(gxd->dpy)) |
| 259 |
: WhitePixelOfScreen(DefaultScreenOfDisplay(gxd->dpy)); |
| 260 |
} |
| 261 |
} |
| 262 |
gxd->abgr = abgr; |
| 263 |
XSetForeground(gxd->dpy, gxd->gc, knowncolors[i].pixel); |
| 264 |
} |
| 265 |
|
| 266 |
clear() |
| 267 |
{ |
| 268 |
XSetBackground(gxd->dpy, gxd->gc, gxd->cpixel); |
| 269 |
XFillRectangle(gxd->dpy, gxd->w, gxd->gc, 0, 0, gxd->xsize, gxd->ysize); |
| 270 |
} |
| 271 |
|
| 272 |
#define MAXP 16 |
| 273 |
static XPoint xp[MAXP]; |
| 274 |
static int xpc = 0; |
| 275 |
static int in_line = 0; |
| 276 |
|
| 277 |
v2f(float p[2]) |
| 278 |
{ |
| 279 |
if(xpc >= MAXP) { |
| 280 |
if(in_line) { |
| 281 |
endline(); |
| 282 |
bgnline(); |
| 283 |
} else { |
| 284 |
endpoint(); |
| 285 |
bgnpoint(); |
| 286 |
} |
| 287 |
} |
| 288 |
xp[xpc].x = (p[0] - gxd->x0)*gxd->xscale; |
| 289 |
xp[xpc].y = (p[1] - gxd->y0)*gxd->yscale; |
| 290 |
xpc++; |
| 291 |
} |
| 292 |
|
| 293 |
bgnline() |
| 294 |
{ |
| 295 |
xpc = 0; |
| 296 |
in_line = 1; |
| 297 |
} |
| 298 |
|
| 299 |
endline() |
| 300 |
{ |
| 301 |
XDrawLines(gxd->dpy, gxd->w, gxd->gc, xp, xpc, CoordModeOrigin); |
| 302 |
xpc = 0; |
| 303 |
} |
| 304 |
|
| 305 |
bgnpoint() |
| 306 |
{ |
| 307 |
xpc = 0; |
| 308 |
in_line = 0; |
| 309 |
} |
| 310 |
|
| 311 |
endpoint() |
| 312 |
{ |
| 313 |
XDrawPoints(gxd->dpy, gxd->w, gxd->gc, xp, xpc, CoordModeOrigin); |
| 314 |
xpc = 0; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
void GXPush(DRAWER *d) { |
| 319 |
tstack[pushed].x = GXx0; |
| 320 |
tstack[pushed].y = GXy0; |
| 321 |
if(++pushed>=MAXDEPTH) { |
| 322 |
pushed = MAXDEPTH-1; |
| 323 |
fprintf(stderr, "GXPush(%d)\n", pushed); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void GXTranslate(DRAWER *d, double x, double y) { |
| 328 |
GXx0 += x; |
| 329 |
GXy0 += y; |
| 330 |
} |
| 331 |
|
| 332 |
void GXPop(DRAWER *d) { |
| 333 |
pushed--; |
| 334 |
if(pushed < 0) { |
| 335 |
fprintf(stderr, "GXPop(%d)\n", pushed); |
| 336 |
pushed = 0; |
| 337 |
} |
| 338 |
GXx0 = tstack[pushed].x; |
| 339 |
GXy0 = tstack[pushed].y; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
void GXDrawLine(DRAWER *drawer, LINE *l) { |
| 344 |
float v1[2], v2[2]; |
| 345 |
v1[0] = l->m[SX] + GXx0; v1[1] = l->m[SY] + GXy0; |
| 346 |
v2[0] = l->m[EX] + GXx0; v2[1] = l->m[EY] + GXy0; |
| 347 |
bgnline(); |
| 348 |
v2f(v1); |
| 349 |
v2f(v2); |
| 350 |
endline(); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
void GXDrawLines(DRAWER *drawer, LINE *obj) { |
| 355 |
LINE *l; |
| 356 |
for(l=obj; l!=NULL; l=l->next) |
| 357 |
GXDrawLine(drawer, l); |
| 358 |
} |
| 359 |
|
| 360 |
void GXDrawPoints(DRAWER *drawer, POINT *obj, int count) { |
| 361 |
int i; |
| 362 |
bgnpoint(); |
| 363 |
for(i = 0; i < count; i++) |
| 364 |
v2f(&obj[i].x); |
| 365 |
endpoint(); |
| 366 |
} |
| 367 |
|
| 368 |
DRAWER GXDraw = { |
| 369 |
"GX", |
| 370 |
GXDrawLines, |
| 371 |
GXPush, /* ignore push */ |
| 372 |
GXTranslate, |
| 373 |
GXPop, /* ignore pop */ |
| 374 |
GXDrawLine, |
| 375 |
GXDrawPoints, |
| 376 |
NULL |
| 377 |
}; |