]>
Commit | Line | Data |
---|---|---|
6a5fa4e0 MG |
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"); | |
37a7c2a1 MG |
444 | fprintf(stderr, |
445 | "Disabled SHM, because it is currently broken!\n"); | |
446 | xd->shared = 0; | |
6a5fa4e0 MG |
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 | GetPixmaps(view->x); | |
588 | view->pixels = view->x->pixels; | |
589 | ||
590 | if (w == EDITOR_W) w = 256; /* XXX */ | |
591 | if (h == EDITOR_H) h = 256; /* XXX */ | |
592 | ||
593 | view->pan_x = w / 2; view->pan_y = h / 2; | |
594 | DoResizeView(view, w, h); | |
595 | ||
596 | GetViewTiles(view); | |
597 | ||
598 | return (view); | |
599 | } | |
600 | ||
601 | ||
602 | DestroyView(SimView *view) | |
603 | { | |
604 | SimView **vp; | |
605 | ||
606 | CancelRedrawView(view); | |
607 | ||
608 | for (vp = ((view->class == Editor_Class) ? | |
609 | (&sim->editor) : (&sim->map)); | |
610 | (*vp) != NULL; | |
611 | vp = &((*vp)->next)) { | |
612 | if ((*vp) == view) { | |
613 | (*vp) = view->next; | |
614 | if (view->class == Editor_Class) | |
615 | sim->editors--; | |
616 | else | |
617 | sim->maps--; | |
618 | ||
619 | break; | |
620 | } | |
621 | } | |
622 | ||
623 | if (view->title != NULL) { | |
624 | ckfree (view->title); | |
625 | view->title = NULL; | |
626 | } | |
627 | ||
628 | if (view->pixmap != None) { | |
629 | XFreePixmap(view->x->dpy, view->pixmap); | |
630 | view->pixmap = None; | |
631 | } | |
632 | ||
633 | if (view->pixmap2 != None) { | |
634 | XFreePixmap(view->x->dpy, view->pixmap2); | |
635 | view->pixmap2 = None; | |
636 | } | |
637 | ||
638 | if (view->overlay_pixmap != None) { | |
639 | XFreePixmap(view->x->dpy, view->overlay_pixmap); | |
640 | view->overlay_pixmap = None; | |
641 | } | |
642 | ||
643 | if (view->auto_scroll_token) { | |
644 | Tk_DeleteTimerHandler(view->auto_scroll_token); | |
645 | view->auto_scroll_token = 0; | |
646 | } | |
647 | ||
648 | #ifndef MSDOS | |
649 | if (view->shminfo) { | |
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; | |
655 | if (view->image) { | |
656 | view->image->data = NULL; | |
657 | view->data = NULL; | |
658 | XDestroyImage(view->image); | |
659 | view->image = NULL; | |
660 | } | |
661 | } else { | |
662 | #endif | |
663 | if (view->image) { | |
664 | if (view->image->data) { | |
665 | ckfree(view->image->data); | |
666 | view->image->data = NULL; | |
667 | } | |
668 | view->data = NULL; | |
669 | XDestroyImage(view->image); | |
670 | view->image = NULL; | |
671 | } | |
672 | #ifndef MSDOS | |
673 | } | |
674 | #endif | |
675 | ||
676 | if (view->other_image) { | |
677 | if (view->other_image->data) { | |
678 | ckfree(view->other_image->data); | |
679 | view->other_image->data = NULL; | |
680 | } | |
681 | view->other_data = NULL; | |
682 | XDestroyImage(view->other_image); | |
683 | view->other_image = NULL; | |
684 | } | |
685 | ||
686 | if (view->tiles) | |
687 | FreeTiles(view); | |
688 | ||
689 | DecRefDisplay(view->x); | |
690 | ||
691 | ckfree((char *) view); | |
692 | } | |
693 | ||
694 | ||
695 | unsigned char * | |
696 | AllocPixels(int len, unsigned char pixel) | |
697 | { | |
698 | int i; | |
699 | unsigned char *data, *cp; | |
700 | ||
701 | cp = data = (unsigned char *)ckalloc(len); | |
702 | for (i = len; i > 0; i--) { | |
703 | *(cp++) = pixel; | |
704 | } | |
705 | ||
706 | return (data); | |
707 | } | |
708 | ||
709 | ||
710 | DoResizeView(SimView *view, int w, int h) | |
711 | { | |
712 | int resize = 0; | |
713 | ||
714 | view->w_width = w; | |
715 | view->w_height = h; | |
716 | ||
717 | if (view->class == Map_Class) { /* Map_Class */ | |
718 | view->m_width = w; | |
719 | view->m_height = h; | |
720 | ||
721 | if (view->pixmap2 == None) { | |
722 | ||
723 | view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root, | |
724 | w, h, view->x->depth); | |
725 | if (view->pixmap2 == None) { | |
726 | fprintf(stderr, | |
727 | "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n", | |
728 | view->x->display); | |
729 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
730 | return; | |
731 | } | |
732 | } | |
733 | ||
734 | } else { /* Editor_Class */ | |
735 | ||
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++; | |
740 | ||
741 | if (resize || (view->pixmap2 == None)) { | |
742 | if (view->pixmap2 != None) { | |
743 | XFreePixmap(view->x->dpy, view->pixmap2); | |
744 | view->pixmap2 = None; | |
745 | } | |
746 | view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root, | |
747 | view->m_width, view->m_height, | |
748 | view->x->depth); | |
749 | if (view->pixmap2 == None) { | |
750 | fprintf(stderr, | |
751 | "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n", | |
752 | view->x->display); | |
753 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
754 | return; | |
755 | } | |
756 | } | |
757 | ||
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; | |
763 | } | |
764 | view->overlay_pixmap = XCreatePixmap(view->x->dpy, view->x->root, | |
765 | view->m_width, view->m_height, | |
766 | 1); | |
767 | if (view->overlay_pixmap == None) { | |
768 | fprintf(stderr, | |
769 | "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n", | |
770 | view->x->display); | |
771 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
772 | return; | |
773 | } | |
774 | if (view->x->overlay_gc == NULL) { | |
775 | unsigned long valuemask = 0; | |
776 | XGCValues values; | |
777 | ||
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); | |
785 | } | |
786 | } | |
787 | ||
788 | } | |
789 | ||
790 | #ifndef MSDOS | |
791 | if (view->type != X_Mem_View) { | |
792 | goto SPRING_FORWARD; | |
793 | } | |
794 | ||
795 | if (resize || (view->image == NULL)) { | |
796 | if (view->shminfo && view->image) { | |
797 | if (view->pixmap != None) { | |
798 | XFreePixmap(view->x->dpy, view->pixmap); | |
799 | view->pixmap = None; | |
800 | } | |
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) | |
806 | view->data8 = NULL; | |
807 | view->data = NULL; | |
808 | XDestroyImage(view->image); | |
809 | view->image = NULL; | |
810 | } | |
811 | ||
812 | #if 0 | |
813 | /* XShmPixmapFormat is documented but does not exist !!! */ | |
814 | if (XShmPixmapFormat(view->x->dpy) != ZPixmap) { | |
815 | fprintf(stderr, | |
816 | "Darn, display \"%s\" has the wrong shared memory format.\n", | |
817 | view->x->display); | |
818 | goto FALL_BACK; | |
819 | } | |
820 | #endif | |
821 | ||
822 | if (!view->shminfo) { | |
823 | view->shminfo = (XShmSegmentInfo *)ckalloc(sizeof (XShmSegmentInfo)); | |
824 | } | |
825 | ||
826 | view->image = | |
827 | XShmCreateImage(view->x->dpy, view->x->visual, view->x->depth, | |
828 | view->x->color ? ZPixmap : XYBitmap, | |
829 | NULL, view->shminfo, | |
830 | view->m_width, view->m_height); | |
831 | ||
832 | view->line_bytes = view->image->bytes_per_line; | |
833 | ||
834 | switch (view->x->depth) { | |
835 | ||
836 | case 1: | |
837 | view->pixel_bytes = 0; | |
838 | view->depth = 1; | |
839 | break; | |
840 | ||
841 | case 8: | |
842 | view->pixel_bytes = 1; | |
843 | view->depth = 8; | |
844 | break; | |
845 | ||
846 | case 15: | |
847 | view->pixel_bytes = 2; | |
848 | view->depth = 15; | |
849 | break; | |
850 | ||
851 | case 16: | |
852 | view->pixel_bytes = 2; | |
853 | view->depth = 16; | |
854 | break; | |
855 | ||
856 | case 24: | |
857 | /* XXX: TODO: 24 and 32 bit support */ | |
858 | view->pixel_bytes = 4; | |
859 | //view->pixel_bytes = 3; | |
860 | view->depth = 24; | |
861 | break; | |
862 | ||
863 | case 32: | |
864 | /* XXX: TODO: 24 and 32 bit support */ | |
865 | view->pixel_bytes = 4; | |
866 | view->depth = 32; | |
867 | break; | |
868 | ||
869 | default: | |
870 | view->pixel_bytes = 0; | |
871 | view->depth = 0; | |
872 | break; | |
873 | ||
874 | } // switch | |
875 | ||
876 | view->shminfo->shmid = shmget(IPC_PRIVATE, | |
877 | (view->line_bytes * | |
878 | view->m_height), | |
879 | (IPC_CREAT | 0777)); | |
880 | if (view->shminfo->shmid < 0) { | |
881 | perror("shmget"); | |
882 | fprintf(stderr, | |
883 | "Darn, Micropolis can't share memory with X display \"%s\".\n", | |
884 | view->x->display); | |
885 | goto FALL_BACK; | |
886 | } | |
887 | ||
888 | view->data = (unsigned char *)shmat(view->shminfo->shmid, 0, 0); | |
889 | if ((int)view->data == -1) { | |
890 | perror("shmat"); | |
891 | fprintf(stderr, | |
892 | "Darn, Micropolis can't find any memory to share with display \"%s\".\n", | |
893 | view->x->display); | |
894 | goto FALL_BACK; | |
895 | } | |
896 | ||
897 | view->image->data = (char *)view->data; | |
898 | view->shminfo->shmaddr = (char *)view->data; | |
899 | view->shminfo->readOnly = False; | |
900 | ||
901 | { int (*old)(); | |
902 | int result; | |
903 | int attached = 0; | |
904 | GotXError = 0; | |
905 | old = XSetErrorHandler(CatchXError); | |
906 | ||
907 | result = | |
908 | XShmAttach(view->x->dpy, view->shminfo); | |
909 | if (result == 0) { | |
910 | fprintf(stderr, | |
911 | "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n", | |
912 | view->x->display); | |
913 | GotXError = 1; | |
914 | } | |
915 | ||
916 | XSync(view->x->dpy, False); | |
917 | ||
918 | if (!GotXError) { | |
919 | attached = 1; | |
920 | view->pixmap = XShmCreatePixmap(view->x->dpy, view->x->root, | |
921 | view->data, view->shminfo, | |
922 | view->m_width, view->m_height, | |
923 | view->x->depth); | |
924 | XSync(view->x->dpy, False); | |
925 | ||
926 | if (GotXError || | |
927 | (view->pixmap == None)) { | |
928 | fprintf(stderr, | |
929 | "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n", | |
930 | view->x->display); | |
931 | GotXError = 1; | |
932 | } | |
933 | } | |
934 | ||
935 | XSetErrorHandler(old); | |
936 | ||
937 | if (GotXError) { | |
938 | view->pixmap = None; | |
939 | if (attached) { | |
940 | XShmDetach(view->x->dpy, view->shminfo); | |
941 | } // if | |
942 | result = shmdt(view->shminfo->shmaddr); | |
943 | result = shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
944 | ckfree(view->shminfo); | |
945 | view->shminfo = NULL; | |
946 | if (view->image) { | |
947 | view->image->data = NULL; | |
948 | view->data = NULL; | |
949 | XDestroyImage(view->image); | |
950 | view->image = NULL; | |
951 | } | |
952 | goto FALL_BACK; | |
953 | } | |
954 | ||
955 | if (view->x->color) { | |
956 | XSetForeground(view->x->dpy, view->x->gc, | |
957 | view->pixels[COLOR_LIGHTBROWN]); | |
958 | } else { | |
959 | XSetForeground(view->x->dpy, view->x->gc, | |
960 | view->pixels[COLOR_WHITE]); | |
961 | } | |
962 | ||
963 | XFillRectangle(view->x->dpy, view->pixmap, view->x->gc, | |
964 | 0, 0, view->m_width, view->m_height); | |
965 | } | |
966 | } | |
967 | ||
968 | goto FINISH; | |
969 | ||
970 | FALL_BACK: | |
971 | ||
972 | fprintf(stderr, | |
973 | "Falling back to the X network protocol on display \"%s\"...\n", | |
974 | view->x->display); | |
975 | #endif | |
976 | ||
977 | view->x->shared = 0; | |
978 | view->type = X_Wire_View; | |
979 | if (view->pixmap != None) { | |
980 | XFreePixmap(view->x->dpy, view->pixmap); | |
981 | view->pixmap = None; | |
982 | } | |
983 | #ifndef MSDOS | |
984 | if (view->shminfo) { | |
985 | if (view->shminfo->shmid >= 0) { | |
986 | if (view->shminfo->shmaddr) { | |
987 | shmdt(view->shminfo->shmaddr); | |
988 | } | |
989 | shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
990 | } | |
991 | ckfree((char *)view->shminfo); | |
992 | view->shminfo = NULL; | |
993 | } | |
994 | #endif | |
995 | if (view->image) { | |
996 | view->image->data = NULL; | |
997 | XDestroyImage(view->image); | |
998 | view->image = NULL; | |
999 | } | |
1000 | view->data = NULL; | |
1001 | view->line_bytes = 0; | |
1002 | view->pixel_bytes = 0; | |
1003 | view->depth = 0; | |
1004 | ||
1005 | SPRING_FORWARD: | |
1006 | ||
1007 | if (resize || (view->pixmap == None)) { | |
1008 | if (view->pixmap != None) { | |
1009 | XFreePixmap(view->x->dpy, view->pixmap); | |
1010 | view->pixmap = None; | |
1011 | } | |
1012 | view->pixmap = XCreatePixmap(view->x->dpy, view->x->root, | |
1013 | view->m_width, view->m_height, | |
1014 | view->x->depth); | |
1015 | if (view->pixmap == None) { | |
1016 | fprintf(stderr, | |
1017 | "Sorry, Micropolis can't create pixmap on X display \"%s\".\n", | |
1018 | view->x->display); | |
1019 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
1020 | return; | |
1021 | } | |
1022 | if (view->x->color) { | |
1023 | XSetForeground(view->x->dpy, view->x->gc, | |
1024 | view->pixels[COLOR_LIGHTBROWN]); | |
1025 | } else { | |
1026 | XSetForeground(view->x->dpy, view->x->gc, | |
1027 | view->pixels[COLOR_WHITE]); | |
1028 | } | |
1029 | XFillRectangle(view->x->dpy, view->pixmap, view->x->gc, | |
1030 | 0, 0, view->m_width, view->m_height); | |
1031 | } | |
1032 | ||
1033 | FINISH: | |
1034 | ||
1035 | if (view->class == Editor_Class) { | |
1036 | ||
1037 | AllocTiles(view); | |
1038 | DoAdjustPan(view); | |
1039 | ||
1040 | } else if (view->class == Map_Class) { | |
1041 | ||
1042 | if (view->type == X_Mem_View) { /* Memory Map */ | |
1043 | ||
1044 | if (view->x->color) { | |
1045 | ||
1046 | /* Color, Shared Memory */ | |
1047 | ||
1048 | view->data8 = view->data; | |
1049 | view->line_bytes8 = view->line_bytes; /* XXX: ??? */ | |
1050 | ||
1051 | switch (view->x->depth) { | |
1052 | ||
1053 | case 8: | |
1054 | view->pixel_bytes = 1; | |
1055 | view->depth = 8; | |
1056 | break; | |
1057 | ||
1058 | case 15: | |
1059 | view->pixel_bytes = 2; | |
1060 | view->depth = 15; | |
1061 | break; | |
1062 | ||
1063 | case 16: | |
1064 | view->pixel_bytes = 2; | |
1065 | view->depth = 16; | |
1066 | break; | |
1067 | ||
1068 | case 24: | |
1069 | /* XXX: TODO: 24 and 32 bit support */ | |
1070 | view->pixel_bytes = 4; | |
1071 | //view->pixel_bytes = 3; | |
1072 | view->depth = 24; | |
1073 | break; | |
1074 | ||
1075 | case 32: | |
1076 | /* XXX: TODO: 24 and 32 bit support */ | |
1077 | view->pixel_bytes = 4; | |
1078 | view->depth = 32; | |
1079 | break; | |
1080 | ||
1081 | default: | |
1082 | view->pixel_bytes = 0; | |
1083 | view->depth = 0; | |
1084 | break; | |
1085 | ||
1086 | } // switch | |
1087 | ||
1088 | } else { | |
1089 | ||
1090 | /* Black and White, Shared Memory */ | |
1091 | ||
1092 | if (view->other_image != NULL) { | |
1093 | XDestroyImage(view->other_image); | |
1094 | } | |
1095 | ||
1096 | view->line_bytes8 = view->m_width; /* XXX: fix depth */ | |
1097 | view->pixel_bytes = 0; | |
1098 | view->depth = 1; | |
1099 | ||
1100 | view->other_data = view->data8 = | |
1101 | AllocPixels(view->m_height * view->line_bytes8, /* XXX: fix depth */ | |
1102 | view->pixels[COLOR_WHITE]); | |
1103 | view->other_image = | |
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 */ | |
1108 | } | |
1109 | ||
1110 | } else { /* Wire Map */ | |
1111 | int bitmap_pad; | |
1112 | int bitmap_depth; | |
1113 | ||
1114 | if (view->image != NULL) { | |
1115 | XDestroyImage(view->image); | |
1116 | view->image = NULL; | |
1117 | } | |
1118 | ||
1119 | if (view->other_image != NULL) { | |
1120 | XDestroyImage(view->other_image); | |
1121 | view->other_image = NULL; | |
1122 | } | |
1123 | ||
1124 | if (view->x->color) { | |
1125 | ||
1126 | /* Color, Wire */ | |
1127 | ||
1128 | switch (view->x->depth) { | |
1129 | ||
1130 | case 8: | |
1131 | view->pixel_bytes = 1; | |
1132 | view->depth = 8; | |
1133 | bitmap_pad = 8; | |
1134 | bitmap_depth = 8; | |
1135 | view->line_bytes8 = | |
1136 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1137 | break; | |
1138 | ||
1139 | case 15: | |
1140 | view->pixel_bytes = 2; | |
1141 | view->depth = 15; | |
1142 | bitmap_pad = 16; | |
1143 | bitmap_depth = 16; | |
1144 | view->line_bytes8 = | |
1145 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1146 | break; | |
1147 | ||
1148 | case 16: | |
1149 | view->pixel_bytes = 2; | |
1150 | view->depth = 16; | |
1151 | bitmap_pad = 16; | |
1152 | bitmap_depth = 16; | |
1153 | view->line_bytes8 = | |
1154 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1155 | break; | |
1156 | ||
1157 | case 24: | |
1158 | view->pixel_bytes = 4; | |
1159 | //view->pixel_bytes = 3; | |
1160 | view->depth = 24; | |
93200beb | 1161 | bitmap_depth = 24; |
6a5fa4e0 MG |
1162 | bitmap_pad = 32; |
1163 | view->line_bytes8 = | |
1164 | ((view->m_width * 4) + 3) & (~3); | |
1165 | break; | |
1166 | ||
1167 | case 32: | |
1168 | view->pixel_bytes = 4; | |
1169 | view->depth = 32; | |
1170 | bitmap_pad = 32; | |
1171 | bitmap_depth = 32; | |
1172 | view->line_bytes8 = | |
1173 | ((view->m_width * 4) + 3) & (~3); | |
1174 | break; | |
1175 | ||
1176 | default: | |
1177 | assert(0); /* Unknown depth */ | |
1178 | break; | |
1179 | ||
1180 | } // switch | |
1181 | ||
1182 | view->line_bytes = | |
1183 | view->line_bytes8; | |
1184 | ||
1185 | } else { | |
1186 | ||
1187 | /* Black and White, Wire */ | |
1188 | ||
1189 | view->pixel_bytes = 0; | |
1190 | view->depth = 1; | |
1191 | view->line_bytes8 = | |
1192 | (view->m_width + 3) & (~3); /* XXX: handle depth */ | |
1193 | view->line_bytes = | |
1194 | (view->m_width + 7) >>3; | |
1195 | bitmap_pad = 8; | |
1196 | bitmap_depth = 8; | |
1197 | ||
1198 | } | |
1199 | ||
1200 | view->data = | |
1201 | AllocPixels(view->m_height * view->line_bytes, 0); | |
1202 | view->image = | |
1203 | XCreateImage(view->x->dpy, view->x->visual, | |
1204 | bitmap_depth, | |
1205 | view->x->color ? ZPixmap : XYBitmap, | |
1206 | 0, (char *)view->data, | |
1207 | view->m_width, view->m_height, | |
1208 | bitmap_pad, | |
1209 | view->line_bytes); | |
1210 | ||
1211 | view->other_data = | |
1212 | AllocPixels(view->m_height * view->line_bytes8, 0); | |
1213 | view->other_image = | |
1214 | XCreateImage(view->x->dpy, view->x->visual, | |
1215 | bitmap_depth, | |
1216 | ZPixmap, | |
1217 | 0, (char *)view->other_data, | |
1218 | view->m_width, view->m_height, | |
1219 | bitmap_pad, | |
1220 | view->line_bytes); | |
1221 | ||
1222 | if (view->x->color) { | |
1223 | view->data8 = view->data; | |
1224 | } else { | |
1225 | view->data8 = view->other_data; | |
1226 | } | |
1227 | } | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | ||
1232 | DoPanBy(struct SimView *view, int dx, int dy) | |
1233 | { | |
1234 | DoPanTo(view, view->pan_x + dx, view->pan_y + dy); | |
1235 | } | |
1236 | ||
1237 | ||
1238 | DoPanTo(struct SimView *view, int x, int y) | |
1239 | { | |
1240 | if (view->class != Editor_Class) { | |
1241 | return; | |
1242 | } | |
1243 | ||
1244 | if (x < 0) x = 0; | |
1245 | if (y < 0) y = 0; | |
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)) { | |
1250 | view->pan_x = x; | |
1251 | view->pan_y = y; | |
1252 | DoAdjustPan(view); | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | /* #define DEBUG_PAN */ | |
1257 | ||
1258 | DoAdjustPan(struct SimView *view) | |
1259 | { | |
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"); | |
1266 | ||
1267 | #ifdef DEBUG_PAN | |
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); | |
1272 | #endif | |
1273 | ||
1274 | if ((view->tile_x = ((px - ww2) >>4)) < 0) | |
1275 | view->tile_x = 0; | |
1276 | if ((view->tile_y = ((py - wh2) >>4)) < 0) | |
1277 | view->tile_y = 0; | |
1278 | ||
1279 | #ifdef DEBUG_PAN | |
1280 | printf(" now tile %d %d\n", view->tile_x, view->tile_y); | |
1281 | #endif | |
1282 | ||
1283 | view->tile_width = ((15 + px + ww2) >>4); | |
1284 | view->tile_height = ((15 + py + wh2) >>4); | |
1285 | ||
1286 | #ifdef DEBUG_PAN | |
1287 | printf(" outer tile %d %d\n", view->tile_width, view->tile_height); | |
1288 | #endif | |
1289 | ||
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; | |
1296 | ||
1297 | #ifdef DEBUG_PAN | |
1298 | printf(" tile size %d %d\n", view->tile_width, view->tile_height); | |
1299 | #endif | |
1300 | ||
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); | |
1305 | ||
1306 | #ifdef DEBUG_PAN | |
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); | |
1309 | #endif | |
1310 | ||
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); | |
1315 | ||
1316 | #ifdef DEBUG_PAN | |
1317 | printf(" screen %d %d %d %d\n", | |
1318 | view->screen_x, view->screen_y, | |
1319 | view->screen_width, view->screen_height); | |
1320 | #endif | |
1321 | ||
1322 | view->overlay_mode = 0; | |
1323 | // view->skip = 0; | |
1324 | view->invalid = 1; | |
1325 | if (SimSpeed == 0) { | |
1326 | EventuallyRedrawView(view); | |
1327 | } | |
1328 | /* InvalidateEditors(); */ | |
1329 | if (view->show_me) { | |
1330 | RedrawMaps(); | |
1331 | } | |
1332 | /* FixMicropolisTimer(); */ | |
1333 | ||
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; | |
1338 | ||
1339 | #ifdef DEBUG_PAN | |
1340 | printf("scrolling %d %d\n", dx, dy); | |
1341 | #endif | |
1342 | ||
1343 | if ((dx != 0) || (dy != 0)) { | |
1344 | int row, col, x, y, | |
1345 | width = view->tile_width, | |
1346 | height = view->tile_height; | |
1347 | ||
1348 | for (col = 0; col < width; col++) | |
1349 | memcpy(want[col], have[col], (height * sizeof(short))); | |
1350 | ||
1351 | for (col = 0; col < total_width; col++) { | |
1352 | x = col - dx; | |
1353 | for (row = 0; row < total_height; row++) { | |
1354 | y = row - dy; | |
1355 | if ((x >= 0) && (x < width) && | |
1356 | (y >= 0) && (y < height)) { | |
1357 | have[col][row] = want[x][y]; | |
1358 | } else { | |
1359 | have[col][row] = -1; | |
1360 | } | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | XCopyArea(view->x->dpy, view->pixmap, view->pixmap, view->x->gc, | |
1365 | 0, 0, view->tile_width <<4, view->tile_height <<4, | |
1366 | dx <<4, dy <<4); | |
1367 | ||
1368 | if (view->type == X_Mem_View) { | |
1369 | XSync(view->x->dpy, False); | |
1370 | } | |
1371 | } | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | ||
1376 | AllocTiles(SimView *view) | |
1377 | { | |
1378 | int row, col; | |
1379 | short **have, **want; | |
1380 | int w = view->m_width / 16, h = view->m_height / 16; | |
1381 | int n = (w + 1) * sizeof (short *); | |
1382 | ||
1383 | if (view->tiles) | |
1384 | FreeTiles(view); | |
1385 | ||
1386 | have = view->tiles = | |
1387 | (short **)ckalloc(n); | |
1388 | ||
1389 | want = view->other_tiles = | |
1390 | (short **)ckalloc(n); | |
1391 | ||
1392 | have[w] = want[w] = NULL; | |
1393 | ||
1394 | n = h * sizeof(short); | |
1395 | for (col = 0; col < w; col++) { | |
1396 | ||
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; | |
1402 | } | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | ||
1407 | FreeTiles(SimView *view) | |
1408 | { | |
1409 | int col; | |
1410 | ||
1411 | for (col = 0; view->tiles[col] != NULL; col++) { | |
1412 | ckfree ((char *)view->tiles[col]); | |
1413 | ckfree ((char *)view->other_tiles[col]); | |
1414 | } | |
1415 | ckfree ((char *)view->tiles); | |
1416 | view->tiles = NULL; | |
1417 | ckfree ((char *)view->other_tiles); | |
1418 | view->other_tiles = NULL; | |
1419 | } | |
1420 | ||
1421 | ||
1422 | #define POINT_BATCH 32 | |
1423 | ||
1424 | Ink *OldInk = NULL; | |
1425 | ||
1426 | ||
1427 | /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */ | |
1428 | ||
1429 | Ink * | |
1430 | NewInk() | |
1431 | { | |
1432 | Ink *ink; | |
1433 | ||
1434 | if (OldInk) { | |
1435 | ink = OldInk; | |
1436 | OldInk = OldInk->next; | |
1437 | } else { | |
1438 | ink = (Ink *)ckalloc(sizeof(Ink)); | |
1439 | ink->maxlength = POINT_BATCH; | |
1440 | ink->points = (XPoint *)ckalloc(POINT_BATCH * sizeof(XPoint)); | |
1441 | } | |
1442 | ink->length = 0; | |
1443 | ink->color = COLOR_WHITE; | |
1444 | ink->next = NULL; | |
1445 | ink->left = ink->right = ink->top = ink->bottom = | |
1446 | ink->last_x = ink->last_y = -1; | |
1447 | return (ink); | |
1448 | } | |
1449 | ||
1450 | ||
1451 | FreeInk(Ink *ink) | |
1452 | { | |
1453 | ink->next = OldInk; | |
1454 | OldInk = ink; | |
1455 | } | |
1456 | ||
1457 | ||
1458 | StartInk(Ink *ink, int x, int y) | |
1459 | { | |
1460 | ink->length = 1; | |
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; | |
1463 | } | |
1464 | ||
1465 | ||
1466 | AddInk(Ink *ink, int x, int y) | |
1467 | { | |
1468 | int dx = x - ink->last_x; | |
1469 | int dy = y - ink->last_y; | |
1470 | ||
1471 | if ((dx != 0) || (dy != 0)) { | |
1472 | /* | |
1473 | if (ink->length > 1) { | |
1474 | if ((dx == 0) && | |
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; | |
1479 | goto ADJUST; | |
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; | |
1485 | goto ADJUST; | |
1486 | } | |
1487 | } | |
1488 | */ | |
1489 | ||
1490 | if (ink->length >= ink->maxlength) { | |
1491 | ink->maxlength += POINT_BATCH; | |
1492 | ink->points = (XPoint *)realloc((void *)ink->points, | |
1493 | ink->maxlength * sizeof(XPoint)); | |
1494 | } | |
1495 | ink->points[ink->length].x = dx; | |
1496 | ink->points[ink->length].y = dy; | |
1497 | ink->length++; | |
1498 | ||
1499 | ADJUST: | |
1500 | if (x < ink->left) | |
1501 | ink->left = x; | |
1502 | if (x > ink->right) | |
1503 | ink->right = x; | |
1504 | if (y < ink->top) | |
1505 | ink->top = y; | |
1506 | if (y > ink->bottom) | |
1507 | ink->bottom = y; | |
1508 | ||
1509 | { int left, right, top, bottom; | |
1510 | SimView *view; | |
1511 | ||
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; } | |
1516 | ||
1517 | left -= 5; right += 5; top -= 5; bottom += 5; | |
1518 | ||
1519 | for (view = sim->editor; view != NULL; view = view->next) { | |
1520 | int vleft, vtop; | |
1521 | ||
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); | |
1529 | } | |
1530 | } | |
1531 | } | |
1532 | ink->last_x = x; ink->last_y = y; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | ||
1537 | EraseOverlay() | |
1538 | { | |
1539 | Ink *ink; | |
1540 | ||
1541 | while (sim->overlay) { | |
1542 | ink = sim->overlay; | |
1543 | sim->overlay = ink->next; | |
1544 | FreeInk(ink); | |
1545 | } | |
1546 | } |