]> cvs.zerfleddert.de Git - micropolis/blob - src/sim/w_x.c
automatically detect byteorder of X server
[micropolis] / src / sim / w_x.c
1 /* w_x.c: X Window System support
2 *
3 * Micropolis, Unix Version. This game was released for the Unix platform
4 * in or about 1990 and has been modified for inclusion in the One Laptop
5 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6 * you need assistance with this program, you may contact:
7 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details. You should have received a
18 * copy of the GNU General Public License along with this program. If
19 * not, see <http://www.gnu.org/licenses/>.
20 *
21 * ADDITIONAL TERMS per GNU GPL Section 7
22 *
23 * No trademark or publicity rights are granted. This license does NOT
24 * give you any right, title or interest in the trademark SimCity or any
25 * other Electronic Arts trademark. You may not distribute any
26 * modification of this program using the trademark SimCity or claim any
27 * affliation or association with Electronic Arts Inc. or its employees.
28 *
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
31 *
32 * If you convey this program (or any modifications of it) and assume
33 * contractual liability for the program to recipients of it, you agree
34 * to indemnify Electronic Arts for any liability that those contractual
35 * assumptions impose on Electronic Arts.
36 *
37 * You may not misrepresent the origins of this program; modified
38 * versions of the program must be marked as such and not identified as
39 * the original program.
40 *
41 * This disclaimer supplements the one included in the General Public
42 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
60 * NOT APPLY TO YOU.
61 */
62 #include "sim.h"
63
64
65 struct XDisplay *XDisplays = NULL;
66 int DisplayCount = 0;
67 #ifdef IS_LINUX
68 int FlushStyle = 3;
69 #else
70 int FlushStyle = 4;
71 #endif
72 int GotXError;
73
74
75 unsigned char ColorIntensities[] = {
76 /* COLOR_WHITE */ 255,
77 /* COLOR_YELLOW */ 170,
78 /* COLOR_ORANGE */ 127,
79 /* COLOR_RED */ 85,
80 /* COLOR_DARKRED */ 63,
81 /* COLOR_DARKBLUE */ 76,
82 /* COLOR_LIGHTBLUE */ 144,
83 /* COLOR_BROWN */ 118,
84 /* COLOR_LIGHTGREEN */ 76,
85 /* COLOR_DARKGREEN */ 42,
86 /* COLOR_OLIVE */ 118,
87 /* COLOR_LIGHTBROWN */ 144,
88 /* COLOR_LIGHTGRAY */ 191,
89 /* COLOR_MEDIUMGRAY */ 127,
90 /* COLOR_DARKGRAY */ 63,
91 /* COLOR_BLACK */ 0,
92 };
93
94
95 ViewToTileCoords(SimView *view, int x, int y, int *outx, int *outy)
96 {
97 x = (view->pan_x - ((view->w_width >>1) - x)) >>4;
98 y = (view->pan_y - ((view->w_height >>1) - y)) >>4;
99
100 if (x < 0) x = 0;
101 if (x >= WORLD_X) x = WORLD_X - 1;
102 if (y < 0) y = 0;
103 if (y >= WORLD_Y) y = WORLD_Y - 1;
104
105 if (x < view->tile_x)
106 x = view->tile_x;
107 if (x >= view->tile_x + view->tile_width)
108 x = view->tile_x + view->tile_width - 1;
109 if (y < view->tile_y)
110 y = view->tile_y;
111 if (y >= view->tile_y + view->tile_height)
112 y = view->tile_y + view->tile_height - 1;
113
114 if (view->tool_x_const != -1)
115 x = view->tool_x_const;
116 if (view->tool_y_const != -1)
117 y = view->tool_y_const;
118
119 *outx = x; *outy = y;
120 }
121
122
123 ViewToPixelCoords(SimView *view, int x, int y, int *outx, int *outy)
124 {
125 x = view->pan_x - ((view->w_width >>1) - x);
126 y = view->pan_y - ((view->w_height >>1) - y);
127
128 if (x < 0) x = 0;
129 if (x >= (WORLD_X <<4)) x = (WORLD_X <<4) - 1;
130 if (y < 0) y = 0;
131 if (y >= (WORLD_Y <<4)) y = (WORLD_Y <<4) - 1;
132
133 if (x < (view->tile_x <<4))
134 x = (view->tile_x <<4);
135 if (x >= ((view->tile_x + view->tile_width) <<4))
136 x = ((view->tile_x + view->tile_width) <<4) - 1;
137 if (y < (view->tile_y <<4))
138 y = (view->tile_y <<4);
139 if (y >= ((view->tile_y + view->tile_height) <<4))
140 y = ((view->tile_y + view->tile_height) <<4) - 1;
141
142 if (view->tool_x_const != -1)
143 x = (view->tool_x_const <<4) + 8;
144 if (view->tool_y_const != -1)
145 y = (view->tool_y_const <<4) + 8;
146
147 *outx = x; *outy = y;
148 }
149
150
151 UpdateFlush()
152 {
153 struct XDisplay *xd;
154
155 if (sim_skips > 0) {
156 if (sim_skip > 0) {
157 sim_skip--;
158 return;
159 }
160 sim_skip = sim_skips;
161 }
162
163 switch (FlushStyle) {
164
165 case 0:
166 break;
167
168 case 1:
169 for (xd = XDisplays; xd != NULL; xd = xd->next)
170 XFlush(xd->dpy);
171 break;
172
173 case 2:
174 for (xd = XDisplays; xd != NULL; xd = xd->next)
175 XSync(xd->dpy, False);
176 break;
177
178 case 3:
179 if (XDisplays && XDisplays->next) {
180 for (xd = XDisplays; xd != NULL; xd = xd->next) {
181 XFlush(xd->dpy);
182 }
183 }
184 for (xd = XDisplays; xd != NULL; xd = xd->next) {
185 XSync(xd->dpy, False);
186 }
187 break;
188
189 case 4:
190 for (xd = XDisplays; xd != NULL; xd = xd->next) {
191 #ifndef IS_LINUX
192 /* XXX TODO: figure this out for linux and new x libs */
193 if ((xd->request != xd->dpy->request) ||
194 (xd->last_request_read != xd->dpy->last_request_read)) {
195 XSync(xd->dpy, False);
196 xd->request = xd->dpy->request;
197 xd->last_request_read = xd->dpy->last_request_read;
198 }
199 #endif
200 }
201 break;
202
203 }
204 }
205
206
207 int
208 CatchXError(Display *dpy, XErrorEvent *err)
209 {
210 GotXError = 1;
211 #if 0
212 printf("GOT X ERROR code %d request code %d %d\n",
213 err->error_code, err->request_code, err->minor_code);
214 #endif
215 return (0);
216 }
217
218
219 DoStopMicropolis()
220 {
221 (void)XSetErrorHandler(CatchXError);
222
223 StopToolkit();
224
225 if (sim) {
226 while (sim->editor != NULL) {
227 DestroyView(sim->editor);
228 }
229
230 while (sim->map != NULL) {
231 DestroyView(sim->map);
232 }
233
234 while (sim->graph != NULL) {
235 DestroyGraph(sim->graph);
236 }
237
238 #ifdef CAM
239 while (sim->scam != NULL) {
240 DestroyCam(sim->scam);
241 }
242 #endif
243 }
244 }
245
246
247 DoTimeoutListen()
248 {
249 while (Tk_DoOneEvent(TK_DONT_WAIT)) ;
250 }
251
252
253 Sim *
254 MakeNewSim()
255 {
256 Sim *sim;
257
258 sim = (Sim *)ckalloc(sizeof(Sim));
259 sim->editors = 0; sim->editor = NULL;
260 sim->maps = 0; sim->map = NULL;
261 sim->graphs = 0; sim->graph = NULL;
262 sim->sprites = 0; sim->sprite = NULL;
263 #ifdef CAM
264 sim->scams = 0; sim->scam = NULL;
265 #endif
266 sim->overlay = NULL;
267
268 return (sim);
269 }
270
271
272 XDisplay *
273 FindXDisplay(Tk_Window tkwin)
274 {
275 XDisplay *xd;
276 int d = 8;
277 unsigned long valuemask = 0;
278 XGCValues values;
279 XColor rgb, *color;
280 Display *dpy = Tk_Display(tkwin);
281 Screen *screen = Tk_Screen(tkwin);
282 #ifdef IS_LINUX
283 char *display = ":0"; /* XXX TODO: fix this for new x libs */
284 #else
285 char *display = dpy->display_name;
286 #endif
287
288 for (xd = XDisplays;
289 xd && (xd->screen != screen);
290 xd = xd->next) ;
291
292 if (xd != NULL) {
293 return (xd);
294 } else {
295 xd = (struct XDisplay *)ckalloc(sizeof (struct XDisplay));
296
297 xd->references = 0;
298 xd->dpy = dpy;
299 xd->display = (char *)ckalloc(strlen(display) + 1);
300 xd->tkDisplay = ((TkWindow *)tkwin)->dispPtr;
301 strcpy(xd->display, display);
302 xd->screen = screen;
303 xd->root = RootWindowOfScreen(xd->screen);
304
305 xd->visual = Tk_DefaultVisual(xd->screen);
306 xd->depth = Tk_DefaultDepth(xd->screen);
307 xd->colormap = Tk_DefaultColormap(xd->screen);
308
309 xd->color = (xd->depth != 1);
310
311 xd->pixels = (int *)ckalloc(16 * sizeof(int));
312 if (xd->color) { /* Color screen */
313 int GotColor = 1;
314
315 #define GETCOLOR(i, name) \
316 if (!GotColor) { \
317 xd->pixels[i] = Rand16() & 255; \
318 } else { \
319 if ((color = Tk_GetColor(tk_mainInterp, tkwin, \
320 None, name)) == NULL) { \
321 xd->pixels[i] = Rand16() & 255; \
322 GotColor = 0; \
323 } else { \
324 switch (xd->depth) { \
325 case 8: \
326 xd->pixels[i] = \
327 color->pixel; \
328 break; \
329 case 15: \
330 xd->pixels[i] = \
331 (((color->red >> (8 + 3)) & 0x1f) << (5 + 5)) | \
332 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
333 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
334 break; \
335 case 16: \
336 xd->pixels[i] = \
337 (((color->red >> (8 + 3)) & 0x1f) << (6 + 5)) | \
338 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
339 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
340 break; \
341 case 24: \
342 xd->pixels[i] = \
343 ((color->red & 0xff) << 16) | \
344 ((color->green & 0xff) << 8) | \
345 ((color->blue & 0xff) << 0); \
346 break; \
347 case 32: \
348 xd->pixels[i] = \
349 ((color->red & 0xff) << 16) | \
350 ((color->green & 0xff) << 8) | \
351 ((color->blue & 0xff) << 0); \
352 break; \
353 } \
354 } \
355 }
356
357 if ((xd->depth == 8) &&
358 (Tk_DefaultColormap(xd->screen) ==
359 DefaultColormapOfScreen(xd->screen))) {
360 xd->pixels[COLOR_WHITE] = WhitePixelOfScreen(xd->screen);
361 xd->pixels[COLOR_BLACK] = BlackPixelOfScreen(xd->screen);
362 } else {
363 GETCOLOR(COLOR_WHITE, "#ffffff");
364 GETCOLOR(COLOR_BLACK, "#000000");
365 }
366
367 GETCOLOR(COLOR_YELLOW, "#ffff00");
368 GETCOLOR(COLOR_ORANGE, "#ff7f00");
369 GETCOLOR(COLOR_RED, "#ff0000");
370 GETCOLOR(COLOR_DARKRED, "#bf0000");
371 GETCOLOR(COLOR_DARKBLUE, "#0000e6");
372 GETCOLOR(COLOR_LIGHTBLUE, "#6666e6");
373 GETCOLOR(COLOR_BROWN, "#cc4c4c");
374 GETCOLOR(COLOR_LIGHTGREEN, "#00e600");
375 GETCOLOR(COLOR_DARKGREEN, "#007f00");
376 GETCOLOR(COLOR_OLIVE, "#997f4c");
377 GETCOLOR(COLOR_LIGHTBROWN, "#cc7f66");
378 GETCOLOR(COLOR_LIGHTGRAY, "#bfbfbf");
379 GETCOLOR(COLOR_MEDIUMGRAY, "#7f7f7f");
380 GETCOLOR(COLOR_DARKGRAY, "#3f3f3f");
381
382 if (!GotColor) {
383 fprintf(stderr,
384 "Oh, dear. There don't seem to be enough free colors on X display \"%s\".\n",
385 xd->display);
386 fprintf(stderr,
387 "Micropolis will try to run anyway, but might look pretty weird!\n");
388 }
389 } else { /* Black and white screen */
390 int white = WhitePixelOfScreen(xd->screen);
391 int black = BlackPixelOfScreen(xd->screen);
392
393 xd->pixels[COLOR_WHITE] = white;
394 xd->pixels[COLOR_BLACK] = black;
395
396 xd->pixels[COLOR_YELLOW] = white;
397 xd->pixels[COLOR_ORANGE] = white;
398 xd->pixels[COLOR_RED] = white;
399 xd->pixels[COLOR_DARKRED] = black;
400 xd->pixels[COLOR_DARKBLUE] = black;
401 xd->pixels[COLOR_LIGHTBLUE] = white;
402 xd->pixels[COLOR_BROWN] = black;
403 xd->pixels[COLOR_LIGHTGREEN] = white;
404 xd->pixels[COLOR_DARKGREEN] = black;
405 xd->pixels[COLOR_OLIVE] = black;
406 xd->pixels[COLOR_LIGHTBROWN] = white;
407 xd->pixels[COLOR_LIGHTGRAY] = white;
408 xd->pixels[COLOR_MEDIUMGRAY] = white;
409 xd->pixels[COLOR_DARKGRAY] = black;
410 }
411
412 xd->gc = Tk_DefaultGC(xd->screen);
413 XSetForeground(xd->dpy, xd->gc, xd->pixels[COLOR_BLACK]);
414 XSetBackground(xd->dpy, xd->gc, xd->pixels[COLOR_WHITE]);
415 XSetLineAttributes(xd->dpy, xd->gc,
416 1, LineSolid, CapButt, JoinMiter);
417 XSetGraphicsExposures(xd->dpy, xd->gc, False);
418
419 #ifndef MSDOS
420 { int major, minor, event, error, pixmaps;
421 if (WireMode ||
422 (XQueryExtension(xd->dpy, "MIT-SHM", /* Jeez! */
423 &major, &event, &error) != True) ||
424 (XShmQueryVersion(xd->dpy,
425 &major, &minor, &pixmaps) != True)) {
426 fprintf(stderr,
427 "Darn, X display \"%s\" doesn't support the shared memory extension.\n",
428 xd->display);
429 xd->shared = 0;
430 } else {
431 if (!pixmaps) {
432 fprintf(stderr,
433 "Darn, X display \"%s\" claims to support the shared memory extension,\n",
434 xd->display);
435 fprintf(stderr,
436 "but is too lame to support shared memory pixmaps, so Micropolis will run slower.\n");
437 fprintf(stderr,
438 "Please complain to your X server vendor, %s\n",
439 XServerVendor(xd->dpy));
440 xd->shared = -1;
441 } else {
442 fprintf(stderr,
443 "Cool, I found the shared memory extension!\n");
444 fprintf(stderr,
445 "Disabled SHM, because it is currently broken!\n");
446 xd->shared = 0;
447 }
448 }
449 }
450 #else
451 xd->shared = 0;
452 #endif
453
454 xd->request = -1;
455 xd->last_request_read = -1;
456 xd->big_tile_pixmap = None;
457 xd->objects = NULL;
458 xd->overlay_gc = NULL;
459 xd->gray25_stipple = None;
460 xd->gray50_stipple = None;
461 xd->gray75_stipple = None;
462 xd->vert_stipple = None;
463 xd->horiz_stipple = None;
464 xd->diag_stipple = None;
465
466 xd->big_tile_image = xd->small_tile_image = NULL;
467
468 xd->next = XDisplays; XDisplays = xd;
469 }
470
471 return (xd);
472 }
473
474
475 IncRefDisplay(XDisplay *xd)
476 {
477 xd->references++;
478 }
479
480
481 DecRefDisplay(XDisplay *xd)
482 {
483 if ((--xd->references) == 0) {
484 /* I'd blow it away, but tk may still be using the display */
485 }
486 }
487
488
489 SimView *
490 InitNewView(SimView *view, char *title, int class, int w, int h)
491 {
492 int type, i;
493 int d = 8;
494 unsigned long valuemask = 0;
495 char *t;
496 struct XDisplay *xd;
497 XGCValues values;
498 XColor rgb, *color;
499
500 t = (char *)ckalloc(strlen(title) + 1);
501 strcpy(t, title);
502
503 view->next = NULL;
504 view->title = t;
505 view->type = -1;
506 view->class = class;
507 view->bigtiles = view->smalltiles = NULL;
508 view->pixels = NULL;
509 view->line_bytes = 0;
510 view->line_bytes8 = 0;
511 view->pixel_bytes = 0;
512 view->depth = 0;
513 view->data = NULL;
514 view->data8 = NULL;
515 view->visible = 0;
516 view->invalid = 0;
517 view->skips = view->skip = 0;
518 view->update = 0;
519 view->map_state = ALMAP;
520 view->show_editors = 1;
521 view->tool_showing = 0;
522 view->tool_mode = 0;
523 view->tool_x = view->tool_y = 0;
524 view->tool_x_const = view->tool_y_const = -1;
525 view->tool_state = dozeState;
526 view->tool_state_save = -1;
527 view->super_user = 0;
528 view->show_me = 1;
529 view->dynamic_filter = 0;
530 view->auto_scroll_token = 0;
531 view->tool_event_time = 0;
532 view->tool_last_event_time = 0;
533 view->w_x = view->w_y = 0;
534 view->w_width = view->w_height = 16;
535 view->m_width = view->m_height = 0;
536 view->i_width = w; view->i_height = h;
537 view->pan_x = view->pan_y = 0;
538 view->tile_x = view->tile_y = 0;
539 view->tile_width = view->tile_height = 0;
540 view->screen_x = view->screen_y = 0;
541 view->screen_width = view->screen_height = 0;
542 view->last_x = view->last_y = view->last_button = 0;
543 view->track_info = NULL;
544 view->message_var = NULL;
545
546 /* This stuff was initialized in our caller (SimViewCmd) */
547 /* view->tkwin = NULL; */
548 /* view->interp = NULL; */
549 /* view->flags = 0; */
550
551 view->x = NULL;
552 view->shminfo = NULL;
553 view->tiles = NULL;
554 view->other_tiles = NULL;
555 view->image = NULL;
556 view->other_image = NULL;
557 view->other_data = NULL;
558 view->pixmap = None;
559 view->pixmap2 = None;
560 view->overlay_pixmap = None;
561 view->overlay_valid = 0;
562 view->fontPtr = NULL;
563 view->updates = 0;
564 view->update_real = view->update_user = view->update_system = 0.0;
565 view->update_context = 0;
566 view->auto_goto = 0;
567 view->auto_going = 0;
568 view->auto_x_goal = view->auto_x_goal = 0;
569 view->auto_speed = 75;
570 view->follow = NULL;
571 view->sound = 1;
572 view->width = 0; view->height = 0;
573 view->show_overlay = 1;
574 view->overlay_mode = 0;
575
576 view->x = FindXDisplay(view->tkwin);
577 IncRefDisplay(view->x);
578
579 /* view->x->shared is 1 if the shared memory extension is present and
580 supports shared memory pixmaps, and -1 if it is present but doesn't. */
581 if (view->x->shared != 1) {
582 view->type = X_Wire_View;
583 } else {
584 view->type = X_Mem_View;
585 }
586
587 view->x->big_endian = (ImageByteOrder(view->x->dpy) == MSBFirst);
588
589 GetPixmaps(view->x);
590 view->pixels = view->x->pixels;
591
592 if (w == EDITOR_W) w = 256; /* XXX */
593 if (h == EDITOR_H) h = 256; /* XXX */
594
595 view->pan_x = w / 2; view->pan_y = h / 2;
596 DoResizeView(view, w, h);
597
598 GetViewTiles(view);
599
600 return (view);
601 }
602
603
604 DestroyView(SimView *view)
605 {
606 SimView **vp;
607
608 CancelRedrawView(view);
609
610 for (vp = ((view->class == Editor_Class) ?
611 (&sim->editor) : (&sim->map));
612 (*vp) != NULL;
613 vp = &((*vp)->next)) {
614 if ((*vp) == view) {
615 (*vp) = view->next;
616 if (view->class == Editor_Class)
617 sim->editors--;
618 else
619 sim->maps--;
620
621 break;
622 }
623 }
624
625 if (view->title != NULL) {
626 ckfree (view->title);
627 view->title = NULL;
628 }
629
630 if (view->pixmap != None) {
631 XFreePixmap(view->x->dpy, view->pixmap);
632 view->pixmap = None;
633 }
634
635 if (view->pixmap2 != None) {
636 XFreePixmap(view->x->dpy, view->pixmap2);
637 view->pixmap2 = None;
638 }
639
640 if (view->overlay_pixmap != None) {
641 XFreePixmap(view->x->dpy, view->overlay_pixmap);
642 view->overlay_pixmap = None;
643 }
644
645 if (view->auto_scroll_token) {
646 Tk_DeleteTimerHandler(view->auto_scroll_token);
647 view->auto_scroll_token = 0;
648 }
649
650 #ifndef MSDOS
651 if (view->shminfo) {
652 XShmDetach(view->x->dpy, view->shminfo);
653 shmdt(view->shminfo->shmaddr);
654 shmctl(view->shminfo->shmid, IPC_RMID, 0);
655 ckfree(view->shminfo);
656 view->shminfo = NULL;
657 if (view->image) {
658 view->image->data = NULL;
659 view->data = NULL;
660 XDestroyImage(view->image);
661 view->image = NULL;
662 }
663 } else {
664 #endif
665 if (view->image) {
666 if (view->image->data) {
667 ckfree(view->image->data);
668 view->image->data = NULL;
669 }
670 view->data = NULL;
671 XDestroyImage(view->image);
672 view->image = NULL;
673 }
674 #ifndef MSDOS
675 }
676 #endif
677
678 if (view->other_image) {
679 if (view->other_image->data) {
680 ckfree(view->other_image->data);
681 view->other_image->data = NULL;
682 }
683 view->other_data = NULL;
684 XDestroyImage(view->other_image);
685 view->other_image = NULL;
686 }
687
688 if (view->tiles)
689 FreeTiles(view);
690
691 DecRefDisplay(view->x);
692
693 ckfree((char *) view);
694 }
695
696
697 unsigned char *
698 AllocPixels(int len, unsigned char pixel)
699 {
700 int i;
701 unsigned char *data, *cp;
702
703 cp = data = (unsigned char *)ckalloc(len);
704 for (i = len; i > 0; i--) {
705 *(cp++) = pixel;
706 }
707
708 return (data);
709 }
710
711
712 DoResizeView(SimView *view, int w, int h)
713 {
714 int resize = 0;
715
716 view->w_width = w;
717 view->w_height = h;
718
719 if (view->class == Map_Class) { /* Map_Class */
720 view->m_width = w;
721 view->m_height = h;
722
723 if (view->pixmap2 == None) {
724
725 view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root,
726 w, h, view->x->depth);
727 if (view->pixmap2 == None) {
728 fprintf(stderr,
729 "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n",
730 view->x->display);
731 sim_exit(1); // Just sets tkMustExit and ExitReturn
732 return;
733 }
734 }
735
736 } else { /* Editor_Class */
737
738 if ((w = (w + 31) & (~15)) > view->m_width)
739 view->m_width = w, resize++;
740 if ((h = (h + 31) & (~15)) > view->m_height)
741 view->m_height = h, resize++;
742
743 if (resize || (view->pixmap2 == None)) {
744 if (view->pixmap2 != None) {
745 XFreePixmap(view->x->dpy, view->pixmap2);
746 view->pixmap2 = None;
747 }
748 view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root,
749 view->m_width, view->m_height,
750 view->x->depth);
751 if (view->pixmap2 == None) {
752 fprintf(stderr,
753 "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n",
754 view->x->display);
755 sim_exit(1); // Just sets tkMustExit and ExitReturn
756 return;
757 }
758 }
759
760 if (resize || (view->overlay_pixmap == None)) {
761 view->overlay_mode = 0;
762 if (view->overlay_pixmap != None) {
763 XFreePixmap(view->x->dpy, view->overlay_pixmap);
764 view->overlay_pixmap = None;
765 }
766 view->overlay_pixmap = XCreatePixmap(view->x->dpy, view->x->root,
767 view->m_width, view->m_height,
768 1);
769 if (view->overlay_pixmap == None) {
770 fprintf(stderr,
771 "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n",
772 view->x->display);
773 sim_exit(1); // Just sets tkMustExit and ExitReturn
774 return;
775 }
776 if (view->x->overlay_gc == NULL) {
777 unsigned long valuemask = 0;
778 XGCValues values;
779
780 view->x->overlay_gc =
781 XCreateGC(view->x->dpy, view->overlay_pixmap, valuemask, &values);
782 XSetForeground(view->x->dpy, view->x->overlay_gc, 0);
783 XSetBackground(view->x->dpy, view->x->overlay_gc, 1);
784 XSetLineAttributes(view->x->dpy, view->x->overlay_gc,
785 1, LineSolid, CapButt, JoinMiter);
786 XSetGraphicsExposures(view->x->dpy, view->x->overlay_gc, False);
787 }
788 }
789
790 }
791
792 #ifndef MSDOS
793 if (view->type != X_Mem_View) {
794 goto SPRING_FORWARD;
795 }
796
797 if (resize || (view->image == NULL)) {
798 if (view->shminfo && view->image) {
799 if (view->pixmap != None) {
800 XFreePixmap(view->x->dpy, view->pixmap);
801 view->pixmap = None;
802 }
803 XShmDetach(view->x->dpy, view->shminfo);
804 shmdt(view->shminfo->shmaddr);
805 shmctl(view->shminfo->shmid, IPC_RMID, 0);
806 view->image->data = NULL;
807 if (view->data == view->data8)
808 view->data8 = NULL;
809 view->data = NULL;
810 XDestroyImage(view->image);
811 view->image = NULL;
812 }
813
814 #if 0
815 /* XShmPixmapFormat is documented but does not exist !!! */
816 if (XShmPixmapFormat(view->x->dpy) != ZPixmap) {
817 fprintf(stderr,
818 "Darn, display \"%s\" has the wrong shared memory format.\n",
819 view->x->display);
820 goto FALL_BACK;
821 }
822 #endif
823
824 if (!view->shminfo) {
825 view->shminfo = (XShmSegmentInfo *)ckalloc(sizeof (XShmSegmentInfo));
826 }
827
828 view->image =
829 XShmCreateImage(view->x->dpy, view->x->visual, view->x->depth,
830 view->x->color ? ZPixmap : XYBitmap,
831 NULL, view->shminfo,
832 view->m_width, view->m_height);
833
834 view->line_bytes = view->image->bytes_per_line;
835
836 switch (view->x->depth) {
837
838 case 1:
839 view->pixel_bytes = 0;
840 view->depth = 1;
841 break;
842
843 case 8:
844 view->pixel_bytes = 1;
845 view->depth = 8;
846 break;
847
848 case 15:
849 view->pixel_bytes = 2;
850 view->depth = 15;
851 break;
852
853 case 16:
854 view->pixel_bytes = 2;
855 view->depth = 16;
856 break;
857
858 case 24:
859 /* XXX: TODO: 24 and 32 bit support */
860 view->pixel_bytes = 4;
861 //view->pixel_bytes = 3;
862 view->depth = 24;
863 break;
864
865 case 32:
866 /* XXX: TODO: 24 and 32 bit support */
867 view->pixel_bytes = 4;
868 view->depth = 32;
869 break;
870
871 default:
872 view->pixel_bytes = 0;
873 view->depth = 0;
874 break;
875
876 } // switch
877
878 view->shminfo->shmid = shmget(IPC_PRIVATE,
879 (view->line_bytes *
880 view->m_height),
881 (IPC_CREAT | 0777));
882 if (view->shminfo->shmid < 0) {
883 perror("shmget");
884 fprintf(stderr,
885 "Darn, Micropolis can't share memory with X display \"%s\".\n",
886 view->x->display);
887 goto FALL_BACK;
888 }
889
890 view->data = (unsigned char *)shmat(view->shminfo->shmid, 0, 0);
891 if ((int)view->data == -1) {
892 perror("shmat");
893 fprintf(stderr,
894 "Darn, Micropolis can't find any memory to share with display \"%s\".\n",
895 view->x->display);
896 goto FALL_BACK;
897 }
898
899 view->image->data = (char *)view->data;
900 view->shminfo->shmaddr = (char *)view->data;
901 view->shminfo->readOnly = False;
902
903 { int (*old)();
904 int result;
905 int attached = 0;
906 GotXError = 0;
907 old = XSetErrorHandler(CatchXError);
908
909 result =
910 XShmAttach(view->x->dpy, view->shminfo);
911 if (result == 0) {
912 fprintf(stderr,
913 "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n",
914 view->x->display);
915 GotXError = 1;
916 }
917
918 XSync(view->x->dpy, False);
919
920 if (!GotXError) {
921 attached = 1;
922 view->pixmap = XShmCreatePixmap(view->x->dpy, view->x->root,
923 view->data, view->shminfo,
924 view->m_width, view->m_height,
925 view->x->depth);
926 XSync(view->x->dpy, False);
927
928 if (GotXError ||
929 (view->pixmap == None)) {
930 fprintf(stderr,
931 "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n",
932 view->x->display);
933 GotXError = 1;
934 }
935 }
936
937 XSetErrorHandler(old);
938
939 if (GotXError) {
940 view->pixmap = None;
941 if (attached) {
942 XShmDetach(view->x->dpy, view->shminfo);
943 } // if
944 result = shmdt(view->shminfo->shmaddr);
945 result = shmctl(view->shminfo->shmid, IPC_RMID, 0);
946 ckfree(view->shminfo);
947 view->shminfo = NULL;
948 if (view->image) {
949 view->image->data = NULL;
950 view->data = NULL;
951 XDestroyImage(view->image);
952 view->image = NULL;
953 }
954 goto FALL_BACK;
955 }
956
957 if (view->x->color) {
958 XSetForeground(view->x->dpy, view->x->gc,
959 view->pixels[COLOR_LIGHTBROWN]);
960 } else {
961 XSetForeground(view->x->dpy, view->x->gc,
962 view->pixels[COLOR_WHITE]);
963 }
964
965 XFillRectangle(view->x->dpy, view->pixmap, view->x->gc,
966 0, 0, view->m_width, view->m_height);
967 }
968 }
969
970 goto FINISH;
971
972 FALL_BACK:
973
974 fprintf(stderr,
975 "Falling back to the X network protocol on display \"%s\"...\n",
976 view->x->display);
977 #endif
978
979 view->x->shared = 0;
980 view->type = X_Wire_View;
981 if (view->pixmap != None) {
982 XFreePixmap(view->x->dpy, view->pixmap);
983 view->pixmap = None;
984 }
985 #ifndef MSDOS
986 if (view->shminfo) {
987 if (view->shminfo->shmid >= 0) {
988 if (view->shminfo->shmaddr) {
989 shmdt(view->shminfo->shmaddr);
990 }
991 shmctl(view->shminfo->shmid, IPC_RMID, 0);
992 }
993 ckfree((char *)view->shminfo);
994 view->shminfo = NULL;
995 }
996 #endif
997 if (view->image) {
998 view->image->data = NULL;
999 XDestroyImage(view->image);
1000 view->image = NULL;
1001 }
1002 view->data = NULL;
1003 view->line_bytes = 0;
1004 view->pixel_bytes = 0;
1005 view->depth = 0;
1006
1007 SPRING_FORWARD:
1008
1009 if (resize || (view->pixmap == None)) {
1010 if (view->pixmap != None) {
1011 XFreePixmap(view->x->dpy, view->pixmap);
1012 view->pixmap = None;
1013 }
1014 view->pixmap = XCreatePixmap(view->x->dpy, view->x->root,
1015 view->m_width, view->m_height,
1016 view->x->depth);
1017 if (view->pixmap == None) {
1018 fprintf(stderr,
1019 "Sorry, Micropolis can't create pixmap on X display \"%s\".\n",
1020 view->x->display);
1021 sim_exit(1); // Just sets tkMustExit and ExitReturn
1022 return;
1023 }
1024 if (view->x->color) {
1025 XSetForeground(view->x->dpy, view->x->gc,
1026 view->pixels[COLOR_LIGHTBROWN]);
1027 } else {
1028 XSetForeground(view->x->dpy, view->x->gc,
1029 view->pixels[COLOR_WHITE]);
1030 }
1031 XFillRectangle(view->x->dpy, view->pixmap, view->x->gc,
1032 0, 0, view->m_width, view->m_height);
1033 }
1034
1035 FINISH:
1036
1037 if (view->class == Editor_Class) {
1038
1039 AllocTiles(view);
1040 DoAdjustPan(view);
1041
1042 } else if (view->class == Map_Class) {
1043
1044 if (view->type == X_Mem_View) { /* Memory Map */
1045
1046 if (view->x->color) {
1047
1048 /* Color, Shared Memory */
1049
1050 view->data8 = view->data;
1051 view->line_bytes8 = view->line_bytes; /* XXX: ??? */
1052
1053 switch (view->x->depth) {
1054
1055 case 8:
1056 view->pixel_bytes = 1;
1057 view->depth = 8;
1058 break;
1059
1060 case 15:
1061 view->pixel_bytes = 2;
1062 view->depth = 15;
1063 break;
1064
1065 case 16:
1066 view->pixel_bytes = 2;
1067 view->depth = 16;
1068 break;
1069
1070 case 24:
1071 /* XXX: TODO: 24 and 32 bit support */
1072 view->pixel_bytes = 4;
1073 //view->pixel_bytes = 3;
1074 view->depth = 24;
1075 break;
1076
1077 case 32:
1078 /* XXX: TODO: 24 and 32 bit support */
1079 view->pixel_bytes = 4;
1080 view->depth = 32;
1081 break;
1082
1083 default:
1084 view->pixel_bytes = 0;
1085 view->depth = 0;
1086 break;
1087
1088 } // switch
1089
1090 } else {
1091
1092 /* Black and White, Shared Memory */
1093
1094 if (view->other_image != NULL) {
1095 XDestroyImage(view->other_image);
1096 }
1097
1098 view->line_bytes8 = view->m_width; /* XXX: fix depth */
1099 view->pixel_bytes = 0;
1100 view->depth = 1;
1101
1102 view->other_data = view->data8 =
1103 AllocPixels(view->m_height * view->line_bytes8, /* XXX: fix depth */
1104 view->pixels[COLOR_WHITE]);
1105 view->other_image =
1106 XCreateImage(view->x->dpy, view->x->visual, 8, /* XXX: fix depth */
1107 ZPixmap, 0, (char *)view->other_data,
1108 view->m_width, view->m_height,
1109 8, view->line_bytes8); /* XXX: fix depth */
1110 }
1111
1112 } else { /* Wire Map */
1113 int bitmap_pad;
1114 int bitmap_depth;
1115
1116 if (view->image != NULL) {
1117 XDestroyImage(view->image);
1118 view->image = NULL;
1119 }
1120
1121 if (view->other_image != NULL) {
1122 XDestroyImage(view->other_image);
1123 view->other_image = NULL;
1124 }
1125
1126 if (view->x->color) {
1127
1128 /* Color, Wire */
1129
1130 switch (view->x->depth) {
1131
1132 case 8:
1133 view->pixel_bytes = 1;
1134 view->depth = 8;
1135 bitmap_pad = 8;
1136 bitmap_depth = 8;
1137 view->line_bytes8 =
1138 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1139 break;
1140
1141 case 15:
1142 view->pixel_bytes = 2;
1143 view->depth = 15;
1144 bitmap_pad = 16;
1145 bitmap_depth = 16;
1146 view->line_bytes8 =
1147 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1148 break;
1149
1150 case 16:
1151 view->pixel_bytes = 2;
1152 view->depth = 16;
1153 bitmap_pad = 16;
1154 bitmap_depth = 16;
1155 view->line_bytes8 =
1156 ((view->m_width * view->pixel_bytes) + 3) & (~3);
1157 break;
1158
1159 case 24:
1160 view->pixel_bytes = 4;
1161 //view->pixel_bytes = 3;
1162 view->depth = 24;
1163 bitmap_depth = 24;
1164 bitmap_pad = 32;
1165 view->line_bytes8 =
1166 ((view->m_width * 4) + 3) & (~3);
1167 break;
1168
1169 case 32:
1170 view->pixel_bytes = 4;
1171 view->depth = 32;
1172 bitmap_pad = 32;
1173 bitmap_depth = 32;
1174 view->line_bytes8 =
1175 ((view->m_width * 4) + 3) & (~3);
1176 break;
1177
1178 default:
1179 assert(0); /* Unknown depth */
1180 break;
1181
1182 } // switch
1183
1184 view->line_bytes =
1185 view->line_bytes8;
1186
1187 } else {
1188
1189 /* Black and White, Wire */
1190
1191 view->pixel_bytes = 0;
1192 view->depth = 1;
1193 view->line_bytes8 =
1194 (view->m_width + 3) & (~3); /* XXX: handle depth */
1195 view->line_bytes =
1196 (view->m_width + 7) >>3;
1197 bitmap_pad = 8;
1198 bitmap_depth = 8;
1199
1200 }
1201
1202 view->data =
1203 AllocPixels(view->m_height * view->line_bytes, 0);
1204 view->image =
1205 XCreateImage(view->x->dpy, view->x->visual,
1206 bitmap_depth,
1207 view->x->color ? ZPixmap : XYBitmap,
1208 0, (char *)view->data,
1209 view->m_width, view->m_height,
1210 bitmap_pad,
1211 view->line_bytes);
1212
1213 view->other_data =
1214 AllocPixels(view->m_height * view->line_bytes8, 0);
1215 view->other_image =
1216 XCreateImage(view->x->dpy, view->x->visual,
1217 bitmap_depth,
1218 ZPixmap,
1219 0, (char *)view->other_data,
1220 view->m_width, view->m_height,
1221 bitmap_pad,
1222 view->line_bytes);
1223
1224 if (view->x->color) {
1225 view->data8 = view->data;
1226 } else {
1227 view->data8 = view->other_data;
1228 }
1229 }
1230 }
1231 }
1232
1233
1234 DoPanBy(struct SimView *view, int dx, int dy)
1235 {
1236 DoPanTo(view, view->pan_x + dx, view->pan_y + dy);
1237 }
1238
1239
1240 DoPanTo(struct SimView *view, int x, int y)
1241 {
1242 if (view->class != Editor_Class) {
1243 return;
1244 }
1245
1246 if (x < 0) x = 0;
1247 if (y < 0) y = 0;
1248 if (x > view->i_width) x = view->i_width - 1;
1249 if (y > view->i_height) y = view->i_height - 1;
1250 if ((view->pan_x != x) ||
1251 (view->pan_y != y)) {
1252 view->pan_x = x;
1253 view->pan_y = y;
1254 DoAdjustPan(view);
1255 }
1256 }
1257
1258 /* #define DEBUG_PAN */
1259
1260 DoAdjustPan(struct SimView *view)
1261 {
1262 int ww2 = view->w_width >>1, wh2 = view->w_height >>1;
1263 int px = view->pan_x, py = view->pan_y;
1264 int last_tile_x = view->tile_x, last_tile_y = view->tile_y;
1265 int last_tile_width = view->tile_width, last_tile_height = view->tile_height;
1266 int total_width = view->m_width >>4, total_height = view->m_height >>4;
1267 //fprintf(stderr, "DoAdjustPan\n");
1268
1269 #ifdef DEBUG_PAN
1270 printf("AdjustPan window %d %d ww2 %d wh2 %d pan %d %d\n",
1271 view->w_width, view->w_height, ww2, wh2, px, py);
1272 printf(" last tile %d %d %d %d\n",
1273 last_tile_x, last_tile_y, last_tile_width, last_tile_height);
1274 #endif
1275
1276 if ((view->tile_x = ((px - ww2) >>4)) < 0)
1277 view->tile_x = 0;
1278 if ((view->tile_y = ((py - wh2) >>4)) < 0)
1279 view->tile_y = 0;
1280
1281 #ifdef DEBUG_PAN
1282 printf(" now tile %d %d\n", view->tile_x, view->tile_y);
1283 #endif
1284
1285 view->tile_width = ((15 + px + ww2) >>4);
1286 view->tile_height = ((15 + py + wh2) >>4);
1287
1288 #ifdef DEBUG_PAN
1289 printf(" outer tile %d %d\n", view->tile_width, view->tile_height);
1290 #endif
1291
1292 if (view->tile_width > (view->i_width >>4))
1293 view->tile_width = (view->i_width >>4);
1294 view->tile_width -= view->tile_x;
1295 if (view->tile_height > (view->i_height >>4))
1296 view->tile_height = (view->i_height >>4);
1297 view->tile_height -= view->tile_y;
1298
1299 #ifdef DEBUG_PAN
1300 printf(" tile size %d %d\n", view->tile_width, view->tile_height);
1301 #endif
1302
1303 if (view->tile_width > (view->m_width >>4))
1304 view->tile_width = (view->m_width >>4);
1305 if (view->tile_height > (view->m_height >>4))
1306 view->tile_height = (view->m_height >>4);
1307
1308 #ifdef DEBUG_PAN
1309 printf(" clipped size %d %d\n", view->tile_width, view->tile_height);
1310 printf(" maximum size %d %d\n", view->m_width >>4, view->m_height >>4);
1311 #endif
1312
1313 view->screen_x = (ww2 - px) + (view->tile_x <<4);
1314 view->screen_y = (wh2 - py) + (view->tile_y <<4);
1315 view->screen_width = (view->tile_width <<4);
1316 view->screen_height = (view->tile_height <<4);
1317
1318 #ifdef DEBUG_PAN
1319 printf(" screen %d %d %d %d\n",
1320 view->screen_x, view->screen_y,
1321 view->screen_width, view->screen_height);
1322 #endif
1323
1324 view->overlay_mode = 0;
1325 // view->skip = 0;
1326 view->invalid = 1;
1327 if (SimSpeed == 0) {
1328 EventuallyRedrawView(view);
1329 }
1330 /* InvalidateEditors(); */
1331 if (view->show_me) {
1332 RedrawMaps();
1333 }
1334 /* FixMicropolisTimer(); */
1335
1336 { int dx = last_tile_x - view->tile_x,
1337 dy = last_tile_y - view->tile_y;
1338 short **want = view->other_tiles,
1339 **have = view->tiles;
1340
1341 #ifdef DEBUG_PAN
1342 printf("scrolling %d %d\n", dx, dy);
1343 #endif
1344
1345 if ((dx != 0) || (dy != 0)) {
1346 int row, col, x, y,
1347 width = view->tile_width,
1348 height = view->tile_height;
1349
1350 for (col = 0; col < width; col++)
1351 memcpy(want[col], have[col], (height * sizeof(short)));
1352
1353 for (col = 0; col < total_width; col++) {
1354 x = col - dx;
1355 for (row = 0; row < total_height; row++) {
1356 y = row - dy;
1357 if ((x >= 0) && (x < width) &&
1358 (y >= 0) && (y < height)) {
1359 have[col][row] = want[x][y];
1360 } else {
1361 have[col][row] = -1;
1362 }
1363 }
1364 }
1365
1366 XCopyArea(view->x->dpy, view->pixmap, view->pixmap, view->x->gc,
1367 0, 0, view->tile_width <<4, view->tile_height <<4,
1368 dx <<4, dy <<4);
1369
1370 if (view->type == X_Mem_View) {
1371 XSync(view->x->dpy, False);
1372 }
1373 }
1374 }
1375 }
1376
1377
1378 AllocTiles(SimView *view)
1379 {
1380 int row, col;
1381 short **have, **want;
1382 int w = view->m_width / 16, h = view->m_height / 16;
1383 int n = (w + 1) * sizeof (short *);
1384
1385 if (view->tiles)
1386 FreeTiles(view);
1387
1388 have = view->tiles =
1389 (short **)ckalloc(n);
1390
1391 want = view->other_tiles =
1392 (short **)ckalloc(n);
1393
1394 have[w] = want[w] = NULL;
1395
1396 n = h * sizeof(short);
1397 for (col = 0; col < w; col++) {
1398
1399 have[col] = (short *)ckalloc(n);
1400 want[col] = (short *)ckalloc(n);
1401 for (row = 0; row < h; row++) {
1402 have[col][row] = -1;
1403 want[col][row] = -1;
1404 }
1405 }
1406 }
1407
1408
1409 FreeTiles(SimView *view)
1410 {
1411 int col;
1412
1413 for (col = 0; view->tiles[col] != NULL; col++) {
1414 ckfree ((char *)view->tiles[col]);
1415 ckfree ((char *)view->other_tiles[col]);
1416 }
1417 ckfree ((char *)view->tiles);
1418 view->tiles = NULL;
1419 ckfree ((char *)view->other_tiles);
1420 view->other_tiles = NULL;
1421 }
1422
1423
1424 #define POINT_BATCH 32
1425
1426 Ink *OldInk = NULL;
1427
1428
1429 /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */
1430
1431 Ink *
1432 NewInk()
1433 {
1434 Ink *ink;
1435
1436 if (OldInk) {
1437 ink = OldInk;
1438 OldInk = OldInk->next;
1439 } else {
1440 ink = (Ink *)ckalloc(sizeof(Ink));
1441 ink->maxlength = POINT_BATCH;
1442 ink->points = (XPoint *)ckalloc(POINT_BATCH * sizeof(XPoint));
1443 }
1444 ink->length = 0;
1445 ink->color = COLOR_WHITE;
1446 ink->next = NULL;
1447 ink->left = ink->right = ink->top = ink->bottom =
1448 ink->last_x = ink->last_y = -1;
1449 return (ink);
1450 }
1451
1452
1453 FreeInk(Ink *ink)
1454 {
1455 ink->next = OldInk;
1456 OldInk = ink;
1457 }
1458
1459
1460 StartInk(Ink *ink, int x, int y)
1461 {
1462 ink->length = 1;
1463 ink->left = ink->right = ink->last_x = ink->points[0].x = x;
1464 ink->top = ink->bottom = ink->last_y = ink->points[0].y = y;
1465 }
1466
1467
1468 AddInk(Ink *ink, int x, int y)
1469 {
1470 int dx = x - ink->last_x;
1471 int dy = y - ink->last_y;
1472
1473 if ((dx != 0) || (dy != 0)) {
1474 /*
1475 if (ink->length > 1) {
1476 if ((dx == 0) &&
1477 (ink->points[ink->length - 1].x == 0) &&
1478 ((ink->points[ink->length - 1].y < 0) ?
1479 (dy < 0) : (dy > 0))) {
1480 ink->points[ink->length - 1].y += dy;
1481 goto ADJUST;
1482 } else if ((dy == 0) &&
1483 (ink->points[ink->length - 1].y == 0) &&
1484 ((ink->points[ink->length - 1].x < 0) ?
1485 (dx < 0) : (dx > 0))) {
1486 ink->points[ink->length - 1].x += dx;
1487 goto ADJUST;
1488 }
1489 }
1490 */
1491
1492 if (ink->length >= ink->maxlength) {
1493 ink->maxlength += POINT_BATCH;
1494 ink->points = (XPoint *)realloc((void *)ink->points,
1495 ink->maxlength * sizeof(XPoint));
1496 }
1497 ink->points[ink->length].x = dx;
1498 ink->points[ink->length].y = dy;
1499 ink->length++;
1500
1501 ADJUST:
1502 if (x < ink->left)
1503 ink->left = x;
1504 if (x > ink->right)
1505 ink->right = x;
1506 if (y < ink->top)
1507 ink->top = y;
1508 if (y > ink->bottom)
1509 ink->bottom = y;
1510
1511 { int left, right, top, bottom;
1512 SimView *view;
1513
1514 if (ink->last_x < x) { left = ink->last_x; right = x; }
1515 else { left = x; right = ink->last_x; }
1516 if (ink->last_y < y) { top = ink->last_y; bottom = y; }
1517 else { top = y; bottom = ink->last_y; }
1518
1519 left -= 5; right += 5; top -= 5; bottom += 5;
1520
1521 for (view = sim->editor; view != NULL; view = view->next) {
1522 int vleft, vtop;
1523
1524 if ((right >= (vleft = view->pan_x - (view->w_width / 2))) &&
1525 (left <= vleft + view->w_width) &&
1526 (bottom >= (vtop = view->pan_y - (view->w_height / 2))) &&
1527 (top <= vtop + view->w_height)) {
1528 /* XXX: do studly incremental update instead */
1529 view->overlay_mode = 0;
1530 EventuallyRedrawView(view);
1531 }
1532 }
1533 }
1534 ink->last_x = x; ink->last_y = y;
1535 }
1536 }
1537
1538
1539 EraseOverlay()
1540 {
1541 Ink *ink;
1542
1543 while (sim->overlay) {
1544 ink = sim->overlay;
1545 sim->overlay = ink->next;
1546 FreeInk(ink);
1547 }
1548 }
Impressum, Datenschutz