]>
git.zerfleddert.de Git - micropolis/blob - src/sim/w_x.c
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) { \
331 (((color->red >> (8 + 3)) & 0x1f) << (5 + 5)) | \
332 (((color->green >> (8 + 2)) & 0x1f) << (5)) | \
333 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
337 (((color->red >> (8 + 3)) & 0x1f) << (6 + 5)) | \
338 (((color->green >> (8 + 2)) & 0x3f) << (5)) | \
339 (((color->blue >> (8 + 3)) & 0x1f) << (0)); \
343 ((color->red & 0xff) << 16) | \
344 ((color->green & 0xff) << 8) | \
345 ((color->blue & 0xff) << 0); \
349 ((color->red & 0xff) << 16) | \
350 ((color->green & 0xff) << 8) | \
351 ((color->blue & 0xff) << 0); \
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
);
363 GETCOLOR(COLOR_WHITE
, "#ffffff");
364 GETCOLOR(COLOR_BLACK
, "#000000");
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");
384 "Oh, dear. There don't seem to be enough free colors on X display \"%s\".\n",
387 "Micropolis will try to run anyway, but might look pretty weird!\n");
389 } else { /* Black and white screen */
390 int white
= WhitePixelOfScreen(xd
->screen
);
391 int black
= BlackPixelOfScreen(xd
->screen
);
393 xd
->pixels
[COLOR_WHITE
] = white
;
394 xd
->pixels
[COLOR_BLACK
] = black
;
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
;
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
);
420 { int major
, minor
, event
, error
, pixmaps
;
422 (XQueryExtension(xd
->dpy
, "MIT-SHM", /* Jeez! */
423 &major
, &event
, &error
) != True
) ||
424 (XShmQueryVersion(xd
->dpy
,
425 &major
, &minor
, &pixmaps
) != True
)) {
427 "Darn, X display \"%s\" doesn't support the shared memory extension.\n",
433 "Darn, X display \"%s\" claims to support the shared memory extension,\n",
436 "but is too lame to support shared memory pixmaps, so Micropolis will run slower.\n");
438 "Please complain to your X server vendor, %s\n",
439 XServerVendor(xd
->dpy
));
443 "Cool, I found the shared memory extension!\n");
445 "Disabled SHM, because it is currently broken!\n");
455 xd
->last_request_read
= -1;
456 xd
->big_tile_pixmap
= None
;
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
;
466 xd
->big_tile_image
= xd
->small_tile_image
= NULL
;
468 xd
->next
= XDisplays
; XDisplays
= xd
;
475 IncRefDisplay(XDisplay
*xd
)
481 DecRefDisplay(XDisplay
*xd
)
483 if ((--xd
->references
) == 0) {
484 /* I'd blow it away, but tk may still be using the display */
490 InitNewView(SimView
*view
, char *title
, int class, int w
, int h
)
494 unsigned long valuemask
= 0;
500 t
= (char *)ckalloc(strlen(title
) + 1);
507 view
->bigtiles
= view
->smalltiles
= NULL
;
509 view
->line_bytes
= 0;
510 view
->line_bytes8
= 0;
511 view
->pixel_bytes
= 0;
517 view
->skips
= view
->skip
= 0;
519 view
->map_state
= ALMAP
;
520 view
->show_editors
= 1;
521 view
->tool_showing
= 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;
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
;
546 /* This stuff was initialized in our caller (SimViewCmd) */
547 /* view->tkwin = NULL; */
548 /* view->interp = NULL; */
549 /* view->flags = 0; */
552 view
->shminfo
= NULL
;
554 view
->other_tiles
= NULL
;
556 view
->other_image
= NULL
;
557 view
->other_data
= NULL
;
559 view
->pixmap2
= None
;
560 view
->overlay_pixmap
= None
;
561 view
->overlay_valid
= 0;
562 view
->fontPtr
= NULL
;
564 view
->update_real
= view
->update_user
= view
->update_system
= 0.0;
565 view
->update_context
= 0;
567 view
->auto_going
= 0;
568 view
->auto_x_goal
= view
->auto_x_goal
= 0;
569 view
->auto_speed
= 75;
572 view
->width
= 0; view
->height
= 0;
573 view
->show_overlay
= 1;
574 view
->overlay_mode
= 0;
576 view
->x
= FindXDisplay(view
->tkwin
);
577 IncRefDisplay(view
->x
);
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
;
584 view
->type
= X_Mem_View
;
588 view
->pixels
= view
->x
->pixels
;
590 if (w
== EDITOR_W
) w
= 256; /* XXX */
591 if (h
== EDITOR_H
) h
= 256; /* XXX */
593 view
->pan_x
= w
/ 2; view
->pan_y
= h
/ 2;
594 DoResizeView(view
, w
, h
);
602 DestroyView(SimView
*view
)
606 CancelRedrawView(view
);
608 for (vp
= ((view
->class == Editor_Class
) ?
609 (&sim
->editor
) : (&sim
->map
));
611 vp
= &((*vp
)->next
)) {
614 if (view
->class == Editor_Class
)
623 if (view
->title
!= NULL
) {
624 ckfree (view
->title
);
628 if (view
->pixmap
!= None
) {
629 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
633 if (view
->pixmap2
!= None
) {
634 XFreePixmap(view
->x
->dpy
, view
->pixmap2
);
635 view
->pixmap2
= None
;
638 if (view
->overlay_pixmap
!= None
) {
639 XFreePixmap(view
->x
->dpy
, view
->overlay_pixmap
);
640 view
->overlay_pixmap
= None
;
643 if (view
->auto_scroll_token
) {
644 Tk_DeleteTimerHandler(view
->auto_scroll_token
);
645 view
->auto_scroll_token
= 0;
650 XShmDetach(view
->x
->dpy
, view
->shminfo
);
651 shmdt(view
->shminfo
->shmaddr
);
652 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
653 ckfree(view
->shminfo
);
654 view
->shminfo
= NULL
;
656 view
->image
->data
= NULL
;
658 XDestroyImage(view
->image
);
664 if (view
->image
->data
) {
665 ckfree(view
->image
->data
);
666 view
->image
->data
= NULL
;
669 XDestroyImage(view
->image
);
676 if (view
->other_image
) {
677 if (view
->other_image
->data
) {
678 ckfree(view
->other_image
->data
);
679 view
->other_image
->data
= NULL
;
681 view
->other_data
= NULL
;
682 XDestroyImage(view
->other_image
);
683 view
->other_image
= NULL
;
689 DecRefDisplay(view
->x
);
691 ckfree((char *) view
);
696 AllocPixels(int len
, unsigned char pixel
)
699 unsigned char *data
, *cp
;
701 cp
= data
= (unsigned char *)ckalloc(len
);
702 for (i
= len
; i
> 0; i
--) {
710 DoResizeView(SimView
*view
, int w
, int h
)
717 if (view
->class == Map_Class
) { /* Map_Class */
721 if (view
->pixmap2
== None
) {
723 view
->pixmap2
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
724 w
, h
, view
->x
->depth
);
725 if (view
->pixmap2
== None
) {
727 "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n",
729 sim_exit(1); // Just sets tkMustExit and ExitReturn
734 } else { /* Editor_Class */
736 if ((w
= (w
+ 31) & (~15)) > view
->m_width
)
737 view
->m_width
= w
, resize
++;
738 if ((h
= (h
+ 31) & (~15)) > view
->m_height
)
739 view
->m_height
= h
, resize
++;
741 if (resize
|| (view
->pixmap2
== None
)) {
742 if (view
->pixmap2
!= None
) {
743 XFreePixmap(view
->x
->dpy
, view
->pixmap2
);
744 view
->pixmap2
= None
;
746 view
->pixmap2
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
747 view
->m_width
, view
->m_height
,
749 if (view
->pixmap2
== None
) {
751 "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n",
753 sim_exit(1); // Just sets tkMustExit and ExitReturn
758 if (resize
|| (view
->overlay_pixmap
== None
)) {
759 view
->overlay_mode
= 0;
760 if (view
->overlay_pixmap
!= None
) {
761 XFreePixmap(view
->x
->dpy
, view
->overlay_pixmap
);
762 view
->overlay_pixmap
= None
;
764 view
->overlay_pixmap
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
765 view
->m_width
, view
->m_height
,
767 if (view
->overlay_pixmap
== None
) {
769 "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n",
771 sim_exit(1); // Just sets tkMustExit and ExitReturn
774 if (view
->x
->overlay_gc
== NULL
) {
775 unsigned long valuemask
= 0;
778 view
->x
->overlay_gc
=
779 XCreateGC(view
->x
->dpy
, view
->overlay_pixmap
, valuemask
, &values
);
780 XSetForeground(view
->x
->dpy
, view
->x
->overlay_gc
, 0);
781 XSetBackground(view
->x
->dpy
, view
->x
->overlay_gc
, 1);
782 XSetLineAttributes(view
->x
->dpy
, view
->x
->overlay_gc
,
783 1, LineSolid
, CapButt
, JoinMiter
);
784 XSetGraphicsExposures(view
->x
->dpy
, view
->x
->overlay_gc
, False
);
791 if (view
->type
!= X_Mem_View
) {
795 if (resize
|| (view
->image
== NULL
)) {
796 if (view
->shminfo
&& view
->image
) {
797 if (view
->pixmap
!= None
) {
798 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
801 XShmDetach(view
->x
->dpy
, view
->shminfo
);
802 shmdt(view
->shminfo
->shmaddr
);
803 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
804 view
->image
->data
= NULL
;
805 if (view
->data
== view
->data8
)
808 XDestroyImage(view
->image
);
813 /* XShmPixmapFormat is documented but does not exist !!! */
814 if (XShmPixmapFormat(view
->x
->dpy
) != ZPixmap
) {
816 "Darn, display \"%s\" has the wrong shared memory format.\n",
822 if (!view
->shminfo
) {
823 view
->shminfo
= (XShmSegmentInfo
*)ckalloc(sizeof (XShmSegmentInfo
));
827 XShmCreateImage(view
->x
->dpy
, view
->x
->visual
, view
->x
->depth
,
828 view
->x
->color
? ZPixmap
: XYBitmap
,
830 view
->m_width
, view
->m_height
);
832 view
->line_bytes
= view
->image
->bytes_per_line
;
834 switch (view
->x
->depth
) {
837 view
->pixel_bytes
= 0;
842 view
->pixel_bytes
= 1;
847 view
->pixel_bytes
= 2;
852 view
->pixel_bytes
= 2;
857 /* XXX: TODO: 24 and 32 bit support */
858 view
->pixel_bytes
= 4;
859 //view->pixel_bytes = 3;
864 /* XXX: TODO: 24 and 32 bit support */
865 view
->pixel_bytes
= 4;
870 view
->pixel_bytes
= 0;
876 view
->shminfo
->shmid
= shmget(IPC_PRIVATE
,
880 if (view
->shminfo
->shmid
< 0) {
883 "Darn, Micropolis can't share memory with X display \"%s\".\n",
888 view
->data
= (unsigned char *)shmat(view
->shminfo
->shmid
, 0, 0);
889 if ((int)view
->data
== -1) {
892 "Darn, Micropolis can't find any memory to share with display \"%s\".\n",
897 view
->image
->data
= (char *)view
->data
;
898 view
->shminfo
->shmaddr
= (char *)view
->data
;
899 view
->shminfo
->readOnly
= False
;
905 old
= XSetErrorHandler(CatchXError
);
908 XShmAttach(view
->x
->dpy
, view
->shminfo
);
911 "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n",
916 XSync(view
->x
->dpy
, False
);
920 view
->pixmap
= XShmCreatePixmap(view
->x
->dpy
, view
->x
->root
,
921 view
->data
, view
->shminfo
,
922 view
->m_width
, view
->m_height
,
924 XSync(view
->x
->dpy
, False
);
927 (view
->pixmap
== None
)) {
929 "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n",
935 XSetErrorHandler(old
);
940 XShmDetach(view
->x
->dpy
, view
->shminfo
);
942 result
= shmdt(view
->shminfo
->shmaddr
);
943 result
= shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
944 ckfree(view
->shminfo
);
945 view
->shminfo
= NULL
;
947 view
->image
->data
= NULL
;
949 XDestroyImage(view
->image
);
955 if (view
->x
->color
) {
956 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
957 view
->pixels
[COLOR_LIGHTBROWN
]);
959 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
960 view
->pixels
[COLOR_WHITE
]);
963 XFillRectangle(view
->x
->dpy
, view
->pixmap
, view
->x
->gc
,
964 0, 0, view
->m_width
, view
->m_height
);
973 "Falling back to the X network protocol on display \"%s\"...\n",
978 view
->type
= X_Wire_View
;
979 if (view
->pixmap
!= None
) {
980 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
985 if (view
->shminfo
->shmid
>= 0) {
986 if (view
->shminfo
->shmaddr
) {
987 shmdt(view
->shminfo
->shmaddr
);
989 shmctl(view
->shminfo
->shmid
, IPC_RMID
, 0);
991 ckfree((char *)view
->shminfo
);
992 view
->shminfo
= NULL
;
996 view
->image
->data
= NULL
;
997 XDestroyImage(view
->image
);
1001 view
->line_bytes
= 0;
1002 view
->pixel_bytes
= 0;
1007 if (resize
|| (view
->pixmap
== None
)) {
1008 if (view
->pixmap
!= None
) {
1009 XFreePixmap(view
->x
->dpy
, view
->pixmap
);
1010 view
->pixmap
= None
;
1012 view
->pixmap
= XCreatePixmap(view
->x
->dpy
, view
->x
->root
,
1013 view
->m_width
, view
->m_height
,
1015 if (view
->pixmap
== None
) {
1017 "Sorry, Micropolis can't create pixmap on X display \"%s\".\n",
1019 sim_exit(1); // Just sets tkMustExit and ExitReturn
1022 if (view
->x
->color
) {
1023 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
1024 view
->pixels
[COLOR_LIGHTBROWN
]);
1026 XSetForeground(view
->x
->dpy
, view
->x
->gc
,
1027 view
->pixels
[COLOR_WHITE
]);
1029 XFillRectangle(view
->x
->dpy
, view
->pixmap
, view
->x
->gc
,
1030 0, 0, view
->m_width
, view
->m_height
);
1035 if (view
->class == Editor_Class
) {
1040 } else if (view
->class == Map_Class
) {
1042 if (view
->type
== X_Mem_View
) { /* Memory Map */
1044 if (view
->x
->color
) {
1046 /* Color, Shared Memory */
1048 view
->data8
= view
->data
;
1049 view
->line_bytes8
= view
->line_bytes
; /* XXX: ??? */
1051 switch (view
->x
->depth
) {
1054 view
->pixel_bytes
= 1;
1059 view
->pixel_bytes
= 2;
1064 view
->pixel_bytes
= 2;
1069 /* XXX: TODO: 24 and 32 bit support */
1070 view
->pixel_bytes
= 4;
1071 //view->pixel_bytes = 3;
1076 /* XXX: TODO: 24 and 32 bit support */
1077 view
->pixel_bytes
= 4;
1082 view
->pixel_bytes
= 0;
1090 /* Black and White, Shared Memory */
1092 if (view
->other_image
!= NULL
) {
1093 XDestroyImage(view
->other_image
);
1096 view
->line_bytes8
= view
->m_width
; /* XXX: fix depth */
1097 view
->pixel_bytes
= 0;
1100 view
->other_data
= view
->data8
=
1101 AllocPixels(view
->m_height
* view
->line_bytes8
, /* XXX: fix depth */
1102 view
->pixels
[COLOR_WHITE
]);
1104 XCreateImage(view
->x
->dpy
, view
->x
->visual
, 8, /* XXX: fix depth */
1105 ZPixmap
, 0, (char *)view
->other_data
,
1106 view
->m_width
, view
->m_height
,
1107 8, view
->line_bytes8
); /* XXX: fix depth */
1110 } else { /* Wire Map */
1114 if (view
->image
!= NULL
) {
1115 XDestroyImage(view
->image
);
1119 if (view
->other_image
!= NULL
) {
1120 XDestroyImage(view
->other_image
);
1121 view
->other_image
= NULL
;
1124 if (view
->x
->color
) {
1128 switch (view
->x
->depth
) {
1131 view
->pixel_bytes
= 1;
1136 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1140 view
->pixel_bytes
= 2;
1145 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1149 view
->pixel_bytes
= 2;
1154 ((view
->m_width
* view
->pixel_bytes
) + 3) & (~3);
1158 view
->pixel_bytes
= 4;
1159 //view->pixel_bytes = 3;
1164 ((view
->m_width
* 4) + 3) & (~3);
1168 view
->pixel_bytes
= 4;
1173 ((view
->m_width
* 4) + 3) & (~3);
1177 assert(0); /* Unknown depth */
1187 /* Black and White, Wire */
1189 view
->pixel_bytes
= 0;
1192 (view
->m_width
+ 3) & (~3); /* XXX: handle depth */
1194 (view
->m_width
+ 7) >>3;
1201 AllocPixels(view
->m_height
* view
->line_bytes
, 0);
1203 XCreateImage(view
->x
->dpy
, view
->x
->visual
,
1205 view
->x
->color
? ZPixmap
: XYBitmap
,
1206 0, (char *)view
->data
,
1207 view
->m_width
, view
->m_height
,
1212 AllocPixels(view
->m_height
* view
->line_bytes8
, 0);
1214 XCreateImage(view
->x
->dpy
, view
->x
->visual
,
1217 0, (char *)view
->other_data
,
1218 view
->m_width
, view
->m_height
,
1222 if (view
->x
->color
) {
1223 view
->data8
= view
->data
;
1225 view
->data8
= view
->other_data
;
1232 DoPanBy(struct SimView
*view
, int dx
, int dy
)
1234 DoPanTo(view
, view
->pan_x
+ dx
, view
->pan_y
+ dy
);
1238 DoPanTo(struct SimView
*view
, int x
, int y
)
1240 if (view
->class != Editor_Class
) {
1246 if (x
> view
->i_width
) x
= view
->i_width
- 1;
1247 if (y
> view
->i_height
) y
= view
->i_height
- 1;
1248 if ((view
->pan_x
!= x
) ||
1249 (view
->pan_y
!= y
)) {
1256 /* #define DEBUG_PAN */
1258 DoAdjustPan(struct SimView
*view
)
1260 int ww2
= view
->w_width
>>1, wh2
= view
->w_height
>>1;
1261 int px
= view
->pan_x
, py
= view
->pan_y
;
1262 int last_tile_x
= view
->tile_x
, last_tile_y
= view
->tile_y
;
1263 int last_tile_width
= view
->tile_width
, last_tile_height
= view
->tile_height
;
1264 int total_width
= view
->m_width
>>4, total_height
= view
->m_height
>>4;
1265 //fprintf(stderr, "DoAdjustPan\n");
1268 printf("AdjustPan window %d %d ww2 %d wh2 %d pan %d %d\n",
1269 view
->w_width
, view
->w_height
, ww2
, wh2
, px
, py
);
1270 printf(" last tile %d %d %d %d\n",
1271 last_tile_x
, last_tile_y
, last_tile_width
, last_tile_height
);
1274 if ((view
->tile_x
= ((px
- ww2
) >>4)) < 0)
1276 if ((view
->tile_y
= ((py
- wh2
) >>4)) < 0)
1280 printf(" now tile %d %d\n", view
->tile_x
, view
->tile_y
);
1283 view
->tile_width
= ((15 + px
+ ww2
) >>4);
1284 view
->tile_height
= ((15 + py
+ wh2
) >>4);
1287 printf(" outer tile %d %d\n", view
->tile_width
, view
->tile_height
);
1290 if (view
->tile_width
> (view
->i_width
>>4))
1291 view
->tile_width
= (view
->i_width
>>4);
1292 view
->tile_width
-= view
->tile_x
;
1293 if (view
->tile_height
> (view
->i_height
>>4))
1294 view
->tile_height
= (view
->i_height
>>4);
1295 view
->tile_height
-= view
->tile_y
;
1298 printf(" tile size %d %d\n", view
->tile_width
, view
->tile_height
);
1301 if (view
->tile_width
> (view
->m_width
>>4))
1302 view
->tile_width
= (view
->m_width
>>4);
1303 if (view
->tile_height
> (view
->m_height
>>4))
1304 view
->tile_height
= (view
->m_height
>>4);
1307 printf(" clipped size %d %d\n", view
->tile_width
, view
->tile_height
);
1308 printf(" maximum size %d %d\n", view
->m_width
>>4, view
->m_height
>>4);
1311 view
->screen_x
= (ww2
- px
) + (view
->tile_x
<<4);
1312 view
->screen_y
= (wh2
- py
) + (view
->tile_y
<<4);
1313 view
->screen_width
= (view
->tile_width
<<4);
1314 view
->screen_height
= (view
->tile_height
<<4);
1317 printf(" screen %d %d %d %d\n",
1318 view
->screen_x
, view
->screen_y
,
1319 view
->screen_width
, view
->screen_height
);
1322 view
->overlay_mode
= 0;
1325 if (SimSpeed
== 0) {
1326 EventuallyRedrawView(view
);
1328 /* InvalidateEditors(); */
1329 if (view
->show_me
) {
1332 /* FixMicropolisTimer(); */
1334 { int dx
= last_tile_x
- view
->tile_x
,
1335 dy
= last_tile_y
- view
->tile_y
;
1336 short **want
= view
->other_tiles
,
1337 **have
= view
->tiles
;
1340 printf("scrolling %d %d\n", dx
, dy
);
1343 if ((dx
!= 0) || (dy
!= 0)) {
1345 width
= view
->tile_width
,
1346 height
= view
->tile_height
;
1348 for (col
= 0; col
< width
; col
++)
1349 memcpy(want
[col
], have
[col
], (height
* sizeof(short)));
1351 for (col
= 0; col
< total_width
; col
++) {
1353 for (row
= 0; row
< total_height
; row
++) {
1355 if ((x
>= 0) && (x
< width
) &&
1356 (y
>= 0) && (y
< height
)) {
1357 have
[col
][row
] = want
[x
][y
];
1359 have
[col
][row
] = -1;
1364 XCopyArea(view
->x
->dpy
, view
->pixmap
, view
->pixmap
, view
->x
->gc
,
1365 0, 0, view
->tile_width
<<4, view
->tile_height
<<4,
1368 if (view
->type
== X_Mem_View
) {
1369 XSync(view
->x
->dpy
, False
);
1376 AllocTiles(SimView
*view
)
1379 short **have
, **want
;
1380 int w
= view
->m_width
/ 16, h
= view
->m_height
/ 16;
1381 int n
= (w
+ 1) * sizeof (short *);
1386 have
= view
->tiles
=
1387 (short **)ckalloc(n
);
1389 want
= view
->other_tiles
=
1390 (short **)ckalloc(n
);
1392 have
[w
] = want
[w
] = NULL
;
1394 n
= h
* sizeof(short);
1395 for (col
= 0; col
< w
; col
++) {
1397 have
[col
] = (short *)ckalloc(n
);
1398 want
[col
] = (short *)ckalloc(n
);
1399 for (row
= 0; row
< h
; row
++) {
1400 have
[col
][row
] = -1;
1401 want
[col
][row
] = -1;
1407 FreeTiles(SimView
*view
)
1411 for (col
= 0; view
->tiles
[col
] != NULL
; col
++) {
1412 ckfree ((char *)view
->tiles
[col
]);
1413 ckfree ((char *)view
->other_tiles
[col
]);
1415 ckfree ((char *)view
->tiles
);
1417 ckfree ((char *)view
->other_tiles
);
1418 view
->other_tiles
= NULL
;
1422 #define POINT_BATCH 32
1427 /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */
1436 OldInk
= OldInk
->next
;
1438 ink
= (Ink
*)ckalloc(sizeof(Ink
));
1439 ink
->maxlength
= POINT_BATCH
;
1440 ink
->points
= (XPoint
*)ckalloc(POINT_BATCH
* sizeof(XPoint
));
1443 ink
->color
= COLOR_WHITE
;
1445 ink
->left
= ink
->right
= ink
->top
= ink
->bottom
=
1446 ink
->last_x
= ink
->last_y
= -1;
1458 StartInk(Ink
*ink
, int x
, int y
)
1461 ink
->left
= ink
->right
= ink
->last_x
= ink
->points
[0].x
= x
;
1462 ink
->top
= ink
->bottom
= ink
->last_y
= ink
->points
[0].y
= y
;
1466 AddInk(Ink
*ink
, int x
, int y
)
1468 int dx
= x
- ink
->last_x
;
1469 int dy
= y
- ink
->last_y
;
1471 if ((dx
!= 0) || (dy
!= 0)) {
1473 if (ink->length > 1) {
1475 (ink->points[ink->length - 1].x == 0) &&
1476 ((ink->points[ink->length - 1].y < 0) ?
1477 (dy < 0) : (dy > 0))) {
1478 ink->points[ink->length - 1].y += dy;
1480 } else if ((dy == 0) &&
1481 (ink->points[ink->length - 1].y == 0) &&
1482 ((ink->points[ink->length - 1].x < 0) ?
1483 (dx < 0) : (dx > 0))) {
1484 ink->points[ink->length - 1].x += dx;
1490 if (ink
->length
>= ink
->maxlength
) {
1491 ink
->maxlength
+= POINT_BATCH
;
1492 ink
->points
= (XPoint
*)realloc((void *)ink
->points
,
1493 ink
->maxlength
* sizeof(XPoint
));
1495 ink
->points
[ink
->length
].x
= dx
;
1496 ink
->points
[ink
->length
].y
= dy
;
1506 if (y
> ink
->bottom
)
1509 { int left
, right
, top
, bottom
;
1512 if (ink
->last_x
< x
) { left
= ink
->last_x
; right
= x
; }
1513 else { left
= x
; right
= ink
->last_x
; }
1514 if (ink
->last_y
< y
) { top
= ink
->last_y
; bottom
= y
; }
1515 else { top
= y
; bottom
= ink
->last_y
; }
1517 left
-= 5; right
+= 5; top
-= 5; bottom
+= 5;
1519 for (view
= sim
->editor
; view
!= NULL
; view
= view
->next
) {
1522 if ((right
>= (vleft
= view
->pan_x
- (view
->w_width
/ 2))) &&
1523 (left
<= vleft
+ view
->w_width
) &&
1524 (bottom
>= (vtop
= view
->pan_y
- (view
->w_height
/ 2))) &&
1525 (top
<= vtop
+ view
->w_height
)) {
1526 /* XXX: do studly incremental update instead */
1527 view
->overlay_mode
= 0;
1528 EventuallyRedrawView(view
);
1532 ink
->last_x
= x
; ink
->last_y
= y
;
1541 while (sim
->overlay
) {
1543 sim
->overlay
= ink
->next
;