]>
git.zerfleddert.de Git - micropolis/blob - src/sim/w_x.c
ef42536588ef0d8d535c104d17f5fd017fbb8f9d
1 /* w_x.c: X Window System support
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.
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.
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/>.
21 * ADDITIONAL TERMS per GNU GPL Section 7
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.
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
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.
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.
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
65 struct XDisplay
*XDisplays
= NULL
;
75 unsigned char ColorIntensities
[] = {
76 /* COLOR_WHITE */ 255,
77 /* COLOR_YELLOW */ 170,
78 /* COLOR_ORANGE */ 127,
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,
95 ViewToTileCoords(SimView
*view
, int x
, int y
, int *outx
, int *outy
)
97 x
= (view
->pan_x
- ((view
->w_width
>>1) - x
)) >>4;
98 y
= (view
->pan_y
- ((view
->w_height
>>1) - y
)) >>4;
101 if (x
>= WORLD_X
) x
= WORLD_X
- 1;
103 if (y
>= WORLD_Y
) y
= WORLD_Y
- 1;
105 if (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
)
111 if (y
>= view
->tile_y
+ view
->tile_height
)
112 y
= view
->tile_y
+ view
->tile_height
- 1;
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
;
119 *outx
= x
; *outy
= y
;
123 ViewToPixelCoords(SimView
*view
, int x
, int y
, int *outx
, int *outy
)
125 x
= view
->pan_x
- ((view
->w_width
>>1) - x
);
126 y
= view
->pan_y
- ((view
->w_height
>>1) - y
);
129 if (x
>= (WORLD_X
<<4)) x
= (WORLD_X
<<4) - 1;
131 if (y
>= (WORLD_Y
<<4)) y
= (WORLD_Y
<<4) - 1;
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;
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;
147 *outx
= x
; *outy
= y
;
160 sim_skip
= sim_skips
;
163 switch (FlushStyle
) {
169 for (xd
= XDisplays
; xd
!= NULL
; xd
= xd
->next
)
174 for (xd
= XDisplays
; xd
!= NULL
; xd
= xd
->next
)
175 XSync(xd
->dpy
, False
);
179 if (XDisplays
&& XDisplays
->next
) {
180 for (xd
= XDisplays
; xd
!= NULL
; xd
= xd
->next
) {
184 for (xd
= XDisplays
; xd
!= NULL
; xd
= xd
->next
) {
185 XSync(xd
->dpy
, False
);
190 for (xd
= XDisplays
; xd
!= NULL
; xd
= xd
->next
) {
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
;
208 CatchXError(Display
*dpy
, XErrorEvent
*err
)
212 printf("GOT X ERROR code %d request code %d %d\n",
213 err
->error_code
, err
->request_code
, err
->minor_code
);
221 (void)XSetErrorHandler(CatchXError
);
226 while (sim
->editor
!= NULL
) {
227 DestroyView(sim
->editor
);
230 while (sim
->map
!= NULL
) {
231 DestroyView(sim
->map
);
234 while (sim
->graph
!= NULL
) {
235 DestroyGraph(sim
->graph
);
239 while (sim
->scam
!= NULL
) {
240 DestroyCam(sim
->scam
);
249 while (Tk_DoOneEvent(TK_DONT_WAIT
)) ;
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
;
264 sim
->scams
= 0; sim
->scam
= NULL
;
273 FindXDisplay(Tk_Window tkwin
)
277 unsigned long valuemask
= 0;
280 Display
*dpy
= Tk_Display(tkwin
);
281 Screen
*screen
= Tk_Screen(tkwin
);
283 char *display
= ":0"; /* XXX TODO: fix this for new x libs */
285 char *display
= dpy
->display_name
;
289 xd
&& (xd
->screen
!= screen
);
295 xd
= (struct XDisplay
*)ckalloc(sizeof (struct XDisplay
));
299 xd
->display
= (char *)ckalloc(strlen(display
) + 1);
300 xd
->tkDisplay
= ((TkWindow
*)tkwin
)->dispPtr
;
301 strcpy(xd
->display
, display
);
303 xd
->root
= RootWindowOfScreen(xd
->screen
);
305 xd
->visual
= Tk_DefaultVisual(xd
->screen
);
306 xd
->depth
= Tk_DefaultDepth(xd
->screen
);
307 xd
->colormap
= Tk_DefaultColormap(xd
->screen
);
309 xd
->color
= (xd
->depth
!= 1);
311 xd
->pixels
= (int *)ckalloc(16 * sizeof(int));
312 if (xd
->color
) { /* Color screen */
315 #define GETCOLOR(i, name) \
317 xd->pixels[i] = Rand16() & 255; \
319 if ((color = Tk_GetColor(tk_mainInterp, tkwin, \
320 None, name)) == NULL) { \
321 xd->pixels[i] = Rand16() & 255; \
324 switch (xd->depth) { \
330 if (xd->visual->red_mask == 0x7c00) { \
332 (((color->red >> (8 + 3)) & 0x1f) << (5 + 5)) | \
333 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
334 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
336 (((color->blue >> (8 + 3)) & 0x1f) << (5 + 5)) | \
337 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
338 (((color->red >> (8 + 3)) & 0x1f) << (0)); \
342 if (xd->visual->red_mask == 0xf800) { \
344 (((color->red >> (8 + 3)) & 0x1f) << (6 + 5)) | \
345 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
346 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
349 (((color->blue >> (8 + 3)) & 0x1f) << (6 + 5)) | \
350 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
351 (((color->red >> (8 + 3)) & 0x1f) << (0)); \
356 if (xd->visual->red_mask == 0xff0000) { \
358 ((color->red & 0xff) << 16) | \
359 ((color->green & 0xff) << 8) | \
360 ((color->blue & 0xff) << 0); \
363 ((color->blue & 0xff) << 16) | \
364 ((color->green & 0xff) << 8) | \
365 ((color->red & 0xff) << 0); \
372 if ((xd
->depth
== 8) &&
373 (Tk_DefaultColormap(xd
->screen
) ==
374 DefaultColormapOfScreen(xd
->screen
))) {
375 xd
->pixels
[COLOR_WHITE
] = WhitePixelOfScreen(xd
->screen
);
376 xd
->pixels
[COLOR_BLACK
] = BlackPixelOfScreen(xd
->screen
);
378 GETCOLOR(COLOR_WHITE
, "#ffffff");
379 GETCOLOR(COLOR_BLACK
, "#000000");
382 GETCOLOR(COLOR_YELLOW
, "#ffff00");
383 GETCOLOR(COLOR_ORANGE
, "#ff7f00");
384 GETCOLOR(COLOR_RED
, "#ff0000");
385 GETCOLOR(COLOR_DARKRED
, "#bf0000");
386 GETCOLOR(COLOR_DARKBLUE
, "#0000e6");
387 GETCOLOR(COLOR_LIGHTBLUE
, "#6666e6");
388 GETCOLOR(COLOR_BROWN
, "#cc4c4c");
389 GETCOLOR(COLOR_LIGHTGREEN
, "#00e600");
390 GETCOLOR(COLOR_DARKGREEN
, "#007f00");
391 GETCOLOR(COLOR_OLIVE
, "#997f4c");
392 GETCOLOR(COLOR_LIGHTBROWN
, "#cc7f66");
393 GETCOLOR(COLOR_LIGHTGRAY
, "#bfbfbf");
394 GETCOLOR(COLOR_MEDIUMGRAY
, "#7f7f7f");
395 GETCOLOR(COLOR_DARKGRAY
, "#3f3f3f");
399 "Oh, dear. There don't seem to be enough free colors on X display \"%s\".\n",
402 "Micropolis will try to run anyway, but might look pretty weird!\n");
404 } else { /* Black and white screen */
405 int white
= WhitePixelOfScreen(xd
->screen
);
406 int black
= BlackPixelOfScreen(xd
->screen
);
408 xd
->pixels
[COLOR_WHITE
] = white
;
409 xd
->pixels
[COLOR_BLACK
] = black
;
411 xd
->pixels
[COLOR_YELLOW
] = white
;
412 xd
->pixels
[COLOR_ORANGE
] = white
;
413 xd
->pixels
[COLOR_RED
] = white
;
414 xd
->pixels
[COLOR_DARKRED
] = black
;
415 xd
->pixels
[COLOR_DARKBLUE
] = black
;
416 xd
->pixels
[COLOR_LIGHTBLUE
] = white
;
417 xd
->pixels
[COLOR_BROWN
] = black
;
418 xd
->pixels
[COLOR_LIGHTGREEN
] = white
;
419 xd
->pixels
[COLOR_DARKGREEN
] = black
;
420 xd
->pixels
[COLOR_OLIVE
] = black
;
421 xd
->pixels
[COLOR_LIGHTBROWN
] = white
;
422 xd
->pixels
[COLOR_LIGHTGRAY
] = white
;
423 xd
->pixels
[COLOR_MEDIUMGRAY
] = white
;
424 xd
->pixels
[COLOR_DARKGRAY
] = black
;
427 xd
->gc
= Tk_DefaultGC(xd
->screen
);
428 XSetForeground(xd
->dpy
, xd
->gc
, xd
->pixels
[COLOR_BLACK
]);
429 XSetBackground(xd
->dpy
, xd
->gc
, xd
->pixels
[COLOR_WHITE
]);
430 XSetLineAttributes(xd
->dpy
, xd
->gc
,
431 1, LineSolid
, CapButt
, JoinMiter
);
432 XSetGraphicsExposures(xd
->dpy
, xd
->gc
, False
);
435 { int major
, minor
, event
, error
, pixmaps
;
437 (XQueryExtension(xd
->dpy
, "MIT-SHM", /* Jeez! */
438 &major
, &event
, &error
) != True
) ||
439 (XShmQueryVersion(xd
->dpy
,
440 &major
, &minor
, &pixmaps
) != True
)) {
442 "Darn, X display \"%s\" doesn't support the shared memory extension.\n",
448 "Darn, X display \"%s\" claims to support the shared memory extension,\n",
451 "but is too lame to support shared memory pixmaps, so Micropolis will run slower.\n");
453 "Please complain to your X server vendor, %s\n",
454 XServerVendor(xd
->dpy
));
458 "Cool, I found the shared memory extension!\n");
468 xd
->last_request_read
= -1;
469 xd
->big_tile_pixmap
= None
;
471 xd
->overlay_gc
= NULL
;
472 xd
->gray25_stipple
= None
;
473 xd
->gray50_stipple
= None
;
474 xd
->gray75_stipple
= None
;
475 xd
->vert_stipple
= None
;
476 xd
->horiz_stipple
= None
;
477 xd
->diag_stipple
= None
;
479 xd
->big_tile_image
= xd
->small_tile_image
= NULL
;
481 xd
->next
= XDisplays
; XDisplays
= xd
;
488 IncRefDisplay(XDisplay
*xd
)
494 DecRefDisplay(XDisplay
*xd
)
496 if ((--xd
->references
) == 0) {
497 /* I'd blow it away, but tk may still be using the display */
503 InitNewView(SimView
*view
, char *title
, int class, int w
, int h
)
508 unsigned long valuemask
= 0;
514 t
= (char *)ckalloc(strlen(title
) + 1);
521 view
->bigtiles
= view
->smalltiles
= NULL
;
523 view
->line_bytes
= 0;
524 view
->line_bytes8
= 0;
525 view
->pixel_bytes
= 0;
531 view
->skips
= view
->skip
= 0;
533 view
->map_state
= ALMAP
;
534 view
->show_editors
= 1;
535 view
->tool_showing
= 0;
537 view
->tool_x
= view
->tool_y
= 0;
538 view
->tool_x_const
= view
->tool_y_const
= -1;
539 view
->tool_state
= dozeState
;
540 view
->tool_state_save
= -1;
541 view
->super_user
= 0;
543 view
->dynamic_filter
= 0;
544 view
->auto_scroll_token
= 0;
545 view
->tool_event_time
= 0;
546 view
->tool_last_event_time
= 0;
547 view
->w_x
= view
->w_y
= 0;
548 view
->w_width
= view
->w_height
= 16;
549 view
->m_width
= view
->m_height
= 0;
550 view
->i_width
= w
; view
->i_height
= h
;
551 view
->pan_x
= view
->pan_y
= 0;
552 view
->tile_x
= view
->tile_y
= 0;
553 view
->tile_width
= view
->tile_height
= 0;
554 view
->screen_x
= view
->screen_y
= 0;
555 view
->screen_width
= view
->screen_height
= 0;
556 view
->last_x
= view
->last_y
= view
->last_button
= 0;
557 view
->track_info
= NULL
;
558 view
->message_var
= NULL
;
560 /* This stuff was initialized in our caller (SimViewCmd) */
561 /* view->tkwin = NULL; */
562 /* view->interp = NULL; */
563 /* view->flags = 0; */
566 view
->shminfo
= NULL
;
568 view
->other_tiles
= NULL
;
570 view
->other_image
= NULL
;
571 view
->other_data
= NULL
;
573 view
->pixmap2
= None
;
574 view
->overlay_pixmap
= None
;
575 view
->overlay_valid
= 0;
576 view
->fontPtr
= NULL
;
578 view
->update_real
= view
->update_user
= view
->update_system
= 0.0;
579 view
->update_context
= 0;
581 view
->auto_going
= 0;
582 view
->auto_x_goal
= view
->auto_x_goal
= 0;
583 view
->auto_speed
= 75;
586 view
->width
= 0; view
->height
= 0;
587 view
->show_overlay
= 1;
588 view
->overlay_mode
= 0;
590 view
->x
= FindXDisplay(view
->tkwin
);
591 IncRefDisplay(view
->x
);
593 /* view->x->shared is 1 if the shared memory extension is present and
594 supports shared memory pixmaps, and -1 if it is present but doesn't. */
595 if (view
->x
->shared
!= 1) {
596 view
->type
= X_Wire_View
;
598 view
->type
= X_Mem_View
;
601 view
->x
->needs_swap
= !(*(unsigned char*) (&test
));
602 view
->x
->x_big_endian
= (ImageByteOrder(view
->x
->dpy
) == MSBFirst
);
606 view
->pixels
= view
->x
->pixels
;
608 if (w
== EDITOR_W
) w
= 256; /* XXX */
609 if (h
== EDITOR_H
) h
= 256; /* XXX */
611 view
->pan_x
= w
/ 2; view
->pan_y
= h
/ 2;
612 DoResizeView(view
, w
, h
);
618 DestroyView(SimView
*view
)
622 CancelRedrawView(view
);
624 for (vp
= ((view
->class == Editor_Class
) ?
625 (&sim
->editor
) : (&sim
->map
));
627 vp
= &((*vp
)->next
)) {
630 if (view
->class == Editor_Class
)
639 if (view
->title
!= NULL
) {
640 ckfree (view
->title
);
644 if (view
->pixmap
!= None
) {
645 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
649 if (view
->pixmap2
!= None
) {
650 XFreePixmap(view
->x
->dpy
, view
->pixmap2
);
651 view
->pixmap2
= None
;
654 if (view
->overlay_pixmap
!= None
) {
655 XFreePixmap(view
->x
->dpy
, view
->overlay_pixmap
);
656 view
->overlay_pixmap
= None
;
659 if (view
->auto_scroll_token
) {
660 Tk_DeleteTimerHandler(view
->auto_scroll_token
);
661 view
->auto_scroll_token
= 0;
666 XShmDetach(view
->x
->dpy
, view
->shminfo
);
667 shmdt(view
->shminfo
->shmaddr
);
668 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
669 ckfree(view
->shminfo
);
670 view
->shminfo
= NULL
;
672 view
->image
->data
= NULL
;
674 XDestroyImage(view
->image
);
680 if (view
->image
->data
) {
681 ckfree(view
->image
->data
);
682 view
->image
->data
= NULL
;
685 XDestroyImage(view
->image
);
692 if (view
->other_image
) {
693 if (view
->other_image
->data
) {
694 ckfree(view
->other_image
->data
);
695 view
->other_image
->data
= NULL
;
697 view
->other_data
= NULL
;
698 XDestroyImage(view
->other_image
);
699 view
->other_image
= NULL
;
705 DecRefDisplay(view
->x
);
707 ckfree((char *) view
);
712 AllocPixels(int len
, unsigned char pixel
)
715 unsigned char *data
, *cp
;
717 cp
= data
= (unsigned char *)ckalloc(len
);
718 for (i
= len
; i
> 0; i
--) {
726 DoResizeView(SimView
*view
, int w
, int h
)
733 if (view
->class == Map_Class
) { /* Map_Class */
737 if (view
->pixmap2
== None
) {
739 view
->pixmap2
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
740 w
, h
, view
->x
->depth
);
741 if (view
->pixmap2
== None
) {
743 "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n",
745 sim_exit(1); // Just sets tkMustExit and ExitReturn
750 } else { /* Editor_Class */
752 if ((w
= (w
+ 31) & (~15)) > view
->m_width
)
753 view
->m_width
= w
, resize
++;
754 if ((h
= (h
+ 31) & (~15)) > view
->m_height
)
755 view
->m_height
= h
, resize
++;
757 if (resize
|| (view
->pixmap2
== None
)) {
758 if (view
->pixmap2
!= None
) {
759 XFreePixmap(view
->x
->dpy
, view
->pixmap2
);
760 view
->pixmap2
= None
;
762 view
->pixmap2
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
763 view
->m_width
, view
->m_height
,
765 if (view
->pixmap2
== None
) {
767 "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n",
769 sim_exit(1); // Just sets tkMustExit and ExitReturn
774 if (resize
|| (view
->overlay_pixmap
== None
)) {
775 view
->overlay_mode
= 0;
776 if (view
->overlay_pixmap
!= None
) {
777 XFreePixmap(view
->x
->dpy
, view
->overlay_pixmap
);
778 view
->overlay_pixmap
= None
;
780 view
->overlay_pixmap
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
781 view
->m_width
, view
->m_height
,
783 if (view
->overlay_pixmap
== None
) {
785 "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n",
787 sim_exit(1); // Just sets tkMustExit and ExitReturn
790 if (view
->x
->overlay_gc
== NULL
) {
791 unsigned long valuemask
= 0;
794 view
->x
->overlay_gc
=
795 XCreateGC(view
->x
->dpy
, view
->overlay_pixmap
, valuemask
, &values
);
796 XSetForeground(view
->x
->dpy
, view
->x
->overlay_gc
, 0);
797 XSetBackground(view
->x
->dpy
, view
->x
->overlay_gc
, 1);
798 XSetLineAttributes(view
->x
->dpy
, view
->x
->overlay_gc
,
799 1, LineSolid
, CapButt
, JoinMiter
);
800 XSetGraphicsExposures(view
->x
->dpy
, view
->x
->overlay_gc
, False
);
807 if (view
->type
!= X_Mem_View
) {
811 if (resize
|| (view
->image
== NULL
)) {
812 if (view
->shminfo
&& view
->image
) {
813 if (view
->pixmap
!= None
) {
814 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
817 XShmDetach(view
->x
->dpy
, view
->shminfo
);
818 shmdt(view
->shminfo
->shmaddr
);
819 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
820 view
->image
->data
= NULL
;
821 if (view
->data
== view
->data8
)
824 XDestroyImage(view
->image
);
829 /* XShmPixmapFormat is documented but does not exist !!! */
830 if (XShmPixmapFormat(view
->x
->dpy
) != ZPixmap
) {
832 "Darn, display \"%s\" has the wrong shared memory format.\n",
838 if (!view
->shminfo
) {
839 view
->shminfo
= (XShmSegmentInfo
*)ckalloc(sizeof (XShmSegmentInfo
));
843 XShmCreateImage(view
->x
->dpy
, view
->x
->visual
, view
->x
->depth
,
844 view
->x
->color
? ZPixmap
: XYBitmap
,
846 view
->m_width
, view
->m_height
);
848 view
->line_bytes
= view
->image
->bytes_per_line
;
850 switch (view
->x
->depth
) {
853 view
->pixel_bytes
= 0;
858 view
->pixel_bytes
= 1;
863 view
->pixel_bytes
= 2;
868 view
->pixel_bytes
= 2;
873 /* XXX: TODO: 24 and 32 bit support */
874 view
->pixel_bytes
= 4;
875 //view->pixel_bytes = 3;
880 /* XXX: TODO: 24 and 32 bit support */
881 view
->pixel_bytes
= 4;
886 view
->pixel_bytes
= 0;
892 view
->shminfo
->shmid
= shmget(IPC_PRIVATE
,
896 if (view
->shminfo
->shmid
< 0) {
899 "Darn, Micropolis can't share memory with X display \"%s\".\n",
904 view
->data
= (unsigned char *)shmat(view
->shminfo
->shmid
, 0, 0);
905 if ((int)view
->data
== -1) {
908 "Darn, Micropolis can't find any memory to share with display \"%s\".\n",
913 view
->image
->data
= (char *)view
->data
;
914 view
->shminfo
->shmaddr
= (char *)view
->data
;
915 view
->shminfo
->readOnly
= False
;
921 old
= XSetErrorHandler(CatchXError
);
924 XShmAttach(view
->x
->dpy
, view
->shminfo
);
927 "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n",
932 XSync(view
->x
->dpy
, False
);
936 view
->pixmap
= XShmCreatePixmap(view
->x
->dpy
, view
->x
->root
,
937 view
->data
, view
->shminfo
,
938 view
->m_width
, view
->m_height
,
940 XSync(view
->x
->dpy
, False
);
943 (view
->pixmap
== None
)) {
945 "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n",
951 XSetErrorHandler(old
);
956 XShmDetach(view
->x
->dpy
, view
->shminfo
);
958 result
= shmdt(view
->shminfo
->shmaddr
);
959 result
= shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
960 ckfree(view
->shminfo
);
961 view
->shminfo
= NULL
;
963 view
->image
->data
= NULL
;
965 XDestroyImage(view
->image
);
971 if (view
->x
->color
) {
972 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
973 view
->pixels
[COLOR_LIGHTBROWN
]);
975 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
976 view
->pixels
[COLOR_WHITE
]);
979 XFillRectangle(view
->x
->dpy
, view
->pixmap
, view
->x
->gc
,
980 0, 0, view
->m_width
, view
->m_height
);
989 "Falling back to the X network protocol on display \"%s\"...\n",
994 view
->type
= X_Wire_View
;
995 if (view
->pixmap
!= None
) {
996 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
1000 if (view
->shminfo
) {
1001 if (view
->shminfo
->shmid
>= 0) {
1002 if (view
->shminfo
->shmaddr
) {
1003 shmdt(view
->shminfo
->shmaddr
);
1005 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
1007 ckfree((char *)view
->shminfo
);
1008 view
->shminfo
= NULL
;
1012 view
->image
->data
= NULL
;
1013 XDestroyImage(view
->image
);
1017 view
->line_bytes
= 0;
1018 view
->pixel_bytes
= 0;
1023 if (resize
|| (view
->pixmap
== None
)) {
1024 if (view
->pixmap
!= None
) {
1025 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
1026 view
->pixmap
= None
;
1028 view
->pixmap
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
1029 view
->m_width
, view
->m_height
,
1031 if (view
->pixmap
== None
) {
1033 "Sorry, Micropolis can't create pixmap on X display \"%s\".\n",
1035 sim_exit(1); // Just sets tkMustExit and ExitReturn
1038 if (view
->x
->color
) {
1039 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
1040 view
->pixels
[COLOR_LIGHTBROWN
]);
1042 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
1043 view
->pixels
[COLOR_WHITE
]);
1045 XFillRectangle(view
->x
->dpy
, view
->pixmap
, view
->x
->gc
,
1046 0, 0, view
->m_width
, view
->m_height
);
1051 if (view
->class == Editor_Class
) {
1056 } else if (view
->class == Map_Class
) {
1058 if (view
->type
== X_Mem_View
) { /* Memory Map */
1060 if (view
->x
->color
) {
1062 /* Color, Shared Memory */
1064 view
->data8
= view
->data
;
1065 view
->line_bytes8
= view
->line_bytes
; /* XXX: ??? */
1067 switch (view
->x
->depth
) {
1070 view
->pixel_bytes
= 1;
1075 view
->pixel_bytes
= 2;
1080 view
->pixel_bytes
= 2;
1085 /* XXX: TODO: 24 and 32 bit support */
1086 view
->pixel_bytes
= 4;
1087 //view->pixel_bytes = 3;
1092 /* XXX: TODO: 24 and 32 bit support */
1093 view
->pixel_bytes
= 4;
1098 view
->pixel_bytes
= 0;
1106 /* Black and White, Shared Memory */
1108 if (view
->other_image
!= NULL
) {
1109 XDestroyImage(view
->other_image
);
1112 view
->line_bytes8
= view
->m_width
; /* XXX: fix depth */
1113 view
->pixel_bytes
= 0;
1116 view
->other_data
= view
->data8
=
1117 AllocPixels(view
->m_height
* view
->line_bytes8
, /* XXX: fix depth */
1118 view
->pixels
[COLOR_WHITE
]);
1120 XCreateImage(view
->x
->dpy
, view
->x
->visual
, 8, /* XXX: fix depth */
1121 ZPixmap
, 0, (char *)view
->other_data
,
1122 view
->m_width
, view
->m_height
,
1123 8, view
->line_bytes8
); /* XXX: fix depth */
1126 } else { /* Wire Map */
1130 if (view
->image
!= NULL
) {
1131 XDestroyImage(view
->image
);
1135 if (view
->other_image
!= NULL
) {
1136 XDestroyImage(view
->other_image
);
1137 view
->other_image
= NULL
;
1140 if (view
->x
->color
) {
1144 switch (view
->x
->depth
) {
1147 view
->pixel_bytes
= 1;
1152 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1156 view
->pixel_bytes
= 2;
1161 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1165 view
->pixel_bytes
= 2;
1170 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1174 view
->pixel_bytes
= 4;
1175 //view->pixel_bytes = 3;
1180 ((view
->m_width
* 4) + 3) & (~3);
1184 view
->pixel_bytes
= 4;
1189 ((view
->m_width
* 4) + 3) & (~3);
1193 assert(0); /* Unknown depth */
1203 /* Black and White, Wire */
1205 view
->pixel_bytes
= 0;
1208 (view
->m_width
+ 3) & (~3); /* XXX: handle depth */
1210 (view
->m_width
+ 7) >>3;
1217 AllocPixels(view
->m_height
* view
->line_bytes
, 0);
1219 XCreateImage(view
->x
->dpy
, view
->x
->visual
,
1221 view
->x
->color
? ZPixmap
: XYBitmap
,
1222 0, (char *)view
->data
,
1223 view
->m_width
, view
->m_height
,
1228 AllocPixels(view
->m_height
* view
->line_bytes8
, 0);
1230 XCreateImage(view
->x
->dpy
, view
->x
->visual
,
1233 0, (char *)view
->other_data
,
1234 view
->m_width
, view
->m_height
,
1238 if (view
->x
->color
) {
1239 view
->data8
= view
->data
;
1241 view
->data8
= view
->other_data
;
1251 DoPanBy(struct SimView
*view
, int dx
, int dy
)
1253 DoPanTo(view
, view
->pan_x
+ dx
, view
->pan_y
+ dy
);
1257 DoPanTo(struct SimView
*view
, int x
, int y
)
1259 if (view
->class != Editor_Class
) {
1265 if (x
> view
->i_width
) x
= view
->i_width
- 1;
1266 if (y
> view
->i_height
) y
= view
->i_height
- 1;
1267 if ((view
->pan_x
!= x
) ||
1268 (view
->pan_y
!= y
)) {
1275 /* #define DEBUG_PAN */
1277 DoAdjustPan(struct SimView
*view
)
1279 int ww2
= view
->w_width
>>1, wh2
= view
->w_height
>>1;
1280 int px
= view
->pan_x
, py
= view
->pan_y
;
1281 int last_tile_x
= view
->tile_x
, last_tile_y
= view
->tile_y
;
1282 int last_tile_width
= view
->tile_width
, last_tile_height
= view
->tile_height
;
1283 int total_width
= view
->m_width
>>4, total_height
= view
->m_height
>>4;
1284 //fprintf(stderr, "DoAdjustPan\n");
1287 printf("AdjustPan window %d %d ww2 %d wh2 %d pan %d %d\n",
1288 view
->w_width
, view
->w_height
, ww2
, wh2
, px
, py
);
1289 printf(" last tile %d %d %d %d\n",
1290 last_tile_x
, last_tile_y
, last_tile_width
, last_tile_height
);
1293 if ((view
->tile_x
= ((px
- ww2
) >>4)) < 0)
1295 if ((view
->tile_y
= ((py
- wh2
) >>4)) < 0)
1299 printf(" now tile %d %d\n", view
->tile_x
, view
->tile_y
);
1302 view
->tile_width
= ((15 + px
+ ww2
) >>4);
1303 view
->tile_height
= ((15 + py
+ wh2
) >>4);
1306 printf(" outer tile %d %d\n", view
->tile_width
, view
->tile_height
);
1309 if (view
->tile_width
> (view
->i_width
>>4))
1310 view
->tile_width
= (view
->i_width
>>4);
1311 view
->tile_width
-= view
->tile_x
;
1312 if (view
->tile_height
> (view
->i_height
>>4))
1313 view
->tile_height
= (view
->i_height
>>4);
1314 view
->tile_height
-= view
->tile_y
;
1317 printf(" tile size %d %d\n", view
->tile_width
, view
->tile_height
);
1320 if (view
->tile_width
> (view
->m_width
>>4))
1321 view
->tile_width
= (view
->m_width
>>4);
1322 if (view
->tile_height
> (view
->m_height
>>4))
1323 view
->tile_height
= (view
->m_height
>>4);
1326 printf(" clipped size %d %d\n", view
->tile_width
, view
->tile_height
);
1327 printf(" maximum size %d %d\n", view
->m_width
>>4, view
->m_height
>>4);
1330 view
->screen_x
= (ww2
- px
) + (view
->tile_x
<<4);
1331 view
->screen_y
= (wh2
- py
) + (view
->tile_y
<<4);
1332 view
->screen_width
= (view
->tile_width
<<4);
1333 view
->screen_height
= (view
->tile_height
<<4);
1336 printf(" screen %d %d %d %d\n",
1337 view
->screen_x
, view
->screen_y
,
1338 view
->screen_width
, view
->screen_height
);
1341 view
->overlay_mode
= 0;
1344 if (SimSpeed
== 0) {
1345 EventuallyRedrawView(view
);
1347 /* InvalidateEditors(); */
1348 if (view
->show_me
) {
1351 /* FixMicropolisTimer(); */
1353 { int dx
= last_tile_x
- view
->tile_x
,
1354 dy
= last_tile_y
- view
->tile_y
;
1355 short **want
= view
->other_tiles
,
1356 **have
= view
->tiles
;
1359 printf("scrolling %d %d\n", dx
, dy
);
1362 if ((dx
!= 0) || (dy
!= 0)) {
1364 width
= view
->tile_width
,
1365 height
= view
->tile_height
;
1367 for (col
= 0; col
< width
; col
++)
1368 memcpy(want
[col
], have
[col
], (height
* sizeof(short)));
1370 for (col
= 0; col
< total_width
; col
++) {
1372 for (row
= 0; row
< total_height
; row
++) {
1374 if ((x
>= 0) && (x
< width
) &&
1375 (y
>= 0) && (y
< height
)) {
1376 have
[col
][row
] = want
[x
][y
];
1378 have
[col
][row
] = -1;
1383 XCopyArea(view
->x
->dpy
, view
->pixmap
, view
->pixmap
, view
->x
->gc
,
1384 0, 0, view
->tile_width
<<4, view
->tile_height
<<4,
1387 if (view
->type
== X_Mem_View
) {
1388 XSync(view
->x
->dpy
, False
);
1395 AllocTiles(SimView
*view
)
1398 short **have
, **want
;
1399 int w
= view
->m_width
/ 16, h
= view
->m_height
/ 16;
1400 int n
= (w
+ 1) * sizeof (short *);
1405 have
= view
->tiles
=
1406 (short **)ckalloc(n
);
1408 want
= view
->other_tiles
=
1409 (short **)ckalloc(n
);
1411 have
[w
] = want
[w
] = NULL
;
1413 n
= h
* sizeof(short);
1414 for (col
= 0; col
< w
; col
++) {
1416 have
[col
] = (short *)ckalloc(n
);
1417 want
[col
] = (short *)ckalloc(n
);
1418 for (row
= 0; row
< h
; row
++) {
1419 have
[col
][row
] = -1;
1420 want
[col
][row
] = -1;
1426 FreeTiles(SimView
*view
)
1430 for (col
= 0; view
->tiles
[col
] != NULL
; col
++) {
1431 ckfree ((char *)view
->tiles
[col
]);
1432 ckfree ((char *)view
->other_tiles
[col
]);
1434 ckfree ((char *)view
->tiles
);
1436 ckfree ((char *)view
->other_tiles
);
1437 view
->other_tiles
= NULL
;
1441 #define POINT_BATCH 32
1446 /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */
1455 OldInk
= OldInk
->next
;
1457 ink
= (Ink
*)ckalloc(sizeof(Ink
));
1458 ink
->maxlength
= POINT_BATCH
;
1459 ink
->points
= (XPoint
*)ckalloc(POINT_BATCH
* sizeof(XPoint
));
1462 ink
->color
= COLOR_WHITE
;
1464 ink
->left
= ink
->right
= ink
->top
= ink
->bottom
=
1465 ink
->last_x
= ink
->last_y
= -1;
1477 StartInk(Ink
*ink
, int x
, int y
)
1480 ink
->left
= ink
->right
= ink
->last_x
= ink
->points
[0].x
= x
;
1481 ink
->top
= ink
->bottom
= ink
->last_y
= ink
->points
[0].y
= y
;
1485 AddInk(Ink
*ink
, int x
, int y
)
1487 int dx
= x
- ink
->last_x
;
1488 int dy
= y
- ink
->last_y
;
1490 if ((dx
!= 0) || (dy
!= 0)) {
1492 if (ink->length > 1) {
1494 (ink->points[ink->length - 1].x == 0) &&
1495 ((ink->points[ink->length - 1].y < 0) ?
1496 (dy < 0) : (dy > 0))) {
1497 ink->points[ink->length - 1].y += dy;
1499 } else if ((dy == 0) &&
1500 (ink->points[ink->length - 1].y == 0) &&
1501 ((ink->points[ink->length - 1].x < 0) ?
1502 (dx < 0) : (dx > 0))) {
1503 ink->points[ink->length - 1].x += dx;
1509 if (ink
->length
>= ink
->maxlength
) {
1510 ink
->maxlength
+= POINT_BATCH
;
1511 ink
->points
= (XPoint
*)realloc((void *)ink
->points
,
1512 ink
->maxlength
* sizeof(XPoint
));
1514 ink
->points
[ink
->length
].x
= dx
;
1515 ink
->points
[ink
->length
].y
= dy
;
1525 if (y
> ink
->bottom
)
1528 { int left
, right
, top
, bottom
;
1531 if (ink
->last_x
< x
) { left
= ink
->last_x
; right
= x
; }
1532 else { left
= x
; right
= ink
->last_x
; }
1533 if (ink
->last_y
< y
) { top
= ink
->last_y
; bottom
= y
; }
1534 else { top
= y
; bottom
= ink
->last_y
; }
1536 left
-= 5; right
+= 5; top
-= 5; bottom
+= 5;
1538 for (view
= sim
->editor
; view
!= NULL
; view
= view
->next
) {
1541 if ((right
>= (vleft
= view
->pan_x
- (view
->w_width
/ 2))) &&
1542 (left
<= vleft
+ view
->w_width
) &&
1543 (bottom
>= (vtop
= view
->pan_y
- (view
->w_height
/ 2))) &&
1544 (top
<= vtop
+ view
->w_height
)) {
1545 /* XXX: do studly incremental update instead */
1546 view
->overlay_mode
= 0;
1547 EventuallyRedrawView(view
);
1551 ink
->last_x
= x
; ink
->last_y
= y
;
1560 while (sim
->overlay
) {
1562 sim
->overlay
= ink
->next
;