]>
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; | |
efad9d6d | 493 | int test = 1; |
6a5fa4e0 MG |
494 | int d = 8; |
495 | unsigned long valuemask = 0; | |
496 | char *t; | |
497 | struct XDisplay *xd; | |
498 | XGCValues values; | |
499 | XColor rgb, *color; | |
500 | ||
501 | t = (char *)ckalloc(strlen(title) + 1); | |
502 | strcpy(t, title); | |
503 | ||
504 | view->next = NULL; | |
505 | view->title = t; | |
506 | view->type = -1; | |
507 | view->class = class; | |
508 | view->bigtiles = view->smalltiles = NULL; | |
509 | view->pixels = NULL; | |
510 | view->line_bytes = 0; | |
511 | view->line_bytes8 = 0; | |
512 | view->pixel_bytes = 0; | |
513 | view->depth = 0; | |
514 | view->data = NULL; | |
515 | view->data8 = NULL; | |
516 | view->visible = 0; | |
517 | view->invalid = 0; | |
518 | view->skips = view->skip = 0; | |
519 | view->update = 0; | |
520 | view->map_state = ALMAP; | |
521 | view->show_editors = 1; | |
522 | view->tool_showing = 0; | |
523 | view->tool_mode = 0; | |
524 | view->tool_x = view->tool_y = 0; | |
525 | view->tool_x_const = view->tool_y_const = -1; | |
526 | view->tool_state = dozeState; | |
527 | view->tool_state_save = -1; | |
528 | view->super_user = 0; | |
529 | view->show_me = 1; | |
530 | view->dynamic_filter = 0; | |
531 | view->auto_scroll_token = 0; | |
532 | view->tool_event_time = 0; | |
533 | view->tool_last_event_time = 0; | |
534 | view->w_x = view->w_y = 0; | |
535 | view->w_width = view->w_height = 16; | |
536 | view->m_width = view->m_height = 0; | |
537 | view->i_width = w; view->i_height = h; | |
538 | view->pan_x = view->pan_y = 0; | |
539 | view->tile_x = view->tile_y = 0; | |
540 | view->tile_width = view->tile_height = 0; | |
541 | view->screen_x = view->screen_y = 0; | |
542 | view->screen_width = view->screen_height = 0; | |
543 | view->last_x = view->last_y = view->last_button = 0; | |
544 | view->track_info = NULL; | |
545 | view->message_var = NULL; | |
546 | ||
547 | /* This stuff was initialized in our caller (SimViewCmd) */ | |
548 | /* view->tkwin = NULL; */ | |
549 | /* view->interp = NULL; */ | |
550 | /* view->flags = 0; */ | |
551 | ||
552 | view->x = NULL; | |
553 | view->shminfo = NULL; | |
554 | view->tiles = NULL; | |
555 | view->other_tiles = NULL; | |
556 | view->image = NULL; | |
557 | view->other_image = NULL; | |
558 | view->other_data = NULL; | |
559 | view->pixmap = None; | |
560 | view->pixmap2 = None; | |
561 | view->overlay_pixmap = None; | |
562 | view->overlay_valid = 0; | |
563 | view->fontPtr = NULL; | |
564 | view->updates = 0; | |
565 | view->update_real = view->update_user = view->update_system = 0.0; | |
566 | view->update_context = 0; | |
567 | view->auto_goto = 0; | |
568 | view->auto_going = 0; | |
569 | view->auto_x_goal = view->auto_x_goal = 0; | |
570 | view->auto_speed = 75; | |
571 | view->follow = NULL; | |
572 | view->sound = 1; | |
573 | view->width = 0; view->height = 0; | |
574 | view->show_overlay = 1; | |
575 | view->overlay_mode = 0; | |
576 | ||
577 | view->x = FindXDisplay(view->tkwin); | |
578 | IncRefDisplay(view->x); | |
579 | ||
580 | /* view->x->shared is 1 if the shared memory extension is present and | |
581 | supports shared memory pixmaps, and -1 if it is present but doesn't. */ | |
582 | if (view->x->shared != 1) { | |
583 | view->type = X_Wire_View; | |
584 | } else { | |
585 | view->type = X_Mem_View; | |
586 | } | |
587 | ||
7d4d5431 MG |
588 | /* XXX: Find cases where transaltion is needed */ |
589 | view->x->needs_swap = 0; | |
efad9d6d | 590 | |
99c730ea | 591 | |
6a5fa4e0 MG |
592 | GetPixmaps(view->x); |
593 | view->pixels = view->x->pixels; | |
594 | ||
595 | if (w == EDITOR_W) w = 256; /* XXX */ | |
596 | if (h == EDITOR_H) h = 256; /* XXX */ | |
597 | ||
598 | view->pan_x = w / 2; view->pan_y = h / 2; | |
599 | DoResizeView(view, w, h); | |
600 | ||
601 | GetViewTiles(view); | |
602 | ||
603 | return (view); | |
604 | } | |
605 | ||
606 | ||
607 | DestroyView(SimView *view) | |
608 | { | |
609 | SimView **vp; | |
610 | ||
611 | CancelRedrawView(view); | |
612 | ||
613 | for (vp = ((view->class == Editor_Class) ? | |
614 | (&sim->editor) : (&sim->map)); | |
615 | (*vp) != NULL; | |
616 | vp = &((*vp)->next)) { | |
617 | if ((*vp) == view) { | |
618 | (*vp) = view->next; | |
619 | if (view->class == Editor_Class) | |
620 | sim->editors--; | |
621 | else | |
622 | sim->maps--; | |
623 | ||
624 | break; | |
625 | } | |
626 | } | |
627 | ||
628 | if (view->title != NULL) { | |
629 | ckfree (view->title); | |
630 | view->title = NULL; | |
631 | } | |
632 | ||
633 | if (view->pixmap != None) { | |
634 | XFreePixmap(view->x->dpy, view->pixmap); | |
635 | view->pixmap = None; | |
636 | } | |
637 | ||
638 | if (view->pixmap2 != None) { | |
639 | XFreePixmap(view->x->dpy, view->pixmap2); | |
640 | view->pixmap2 = None; | |
641 | } | |
642 | ||
643 | if (view->overlay_pixmap != None) { | |
644 | XFreePixmap(view->x->dpy, view->overlay_pixmap); | |
645 | view->overlay_pixmap = None; | |
646 | } | |
647 | ||
648 | if (view->auto_scroll_token) { | |
649 | Tk_DeleteTimerHandler(view->auto_scroll_token); | |
650 | view->auto_scroll_token = 0; | |
651 | } | |
652 | ||
653 | #ifndef MSDOS | |
654 | if (view->shminfo) { | |
655 | XShmDetach(view->x->dpy, view->shminfo); | |
656 | shmdt(view->shminfo->shmaddr); | |
657 | shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
658 | ckfree(view->shminfo); | |
659 | view->shminfo = NULL; | |
660 | if (view->image) { | |
661 | view->image->data = NULL; | |
662 | view->data = NULL; | |
663 | XDestroyImage(view->image); | |
664 | view->image = NULL; | |
665 | } | |
666 | } else { | |
667 | #endif | |
668 | if (view->image) { | |
669 | if (view->image->data) { | |
670 | ckfree(view->image->data); | |
671 | view->image->data = NULL; | |
672 | } | |
673 | view->data = NULL; | |
674 | XDestroyImage(view->image); | |
675 | view->image = NULL; | |
676 | } | |
677 | #ifndef MSDOS | |
678 | } | |
679 | #endif | |
680 | ||
681 | if (view->other_image) { | |
682 | if (view->other_image->data) { | |
683 | ckfree(view->other_image->data); | |
684 | view->other_image->data = NULL; | |
685 | } | |
686 | view->other_data = NULL; | |
687 | XDestroyImage(view->other_image); | |
688 | view->other_image = NULL; | |
689 | } | |
690 | ||
691 | if (view->tiles) | |
692 | FreeTiles(view); | |
693 | ||
694 | DecRefDisplay(view->x); | |
695 | ||
696 | ckfree((char *) view); | |
697 | } | |
698 | ||
699 | ||
700 | unsigned char * | |
701 | AllocPixels(int len, unsigned char pixel) | |
702 | { | |
703 | int i; | |
704 | unsigned char *data, *cp; | |
705 | ||
706 | cp = data = (unsigned char *)ckalloc(len); | |
707 | for (i = len; i > 0; i--) { | |
708 | *(cp++) = pixel; | |
709 | } | |
710 | ||
711 | return (data); | |
712 | } | |
713 | ||
714 | ||
715 | DoResizeView(SimView *view, int w, int h) | |
716 | { | |
717 | int resize = 0; | |
718 | ||
719 | view->w_width = w; | |
720 | view->w_height = h; | |
721 | ||
722 | if (view->class == Map_Class) { /* Map_Class */ | |
723 | view->m_width = w; | |
724 | view->m_height = h; | |
725 | ||
726 | if (view->pixmap2 == None) { | |
727 | ||
728 | view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root, | |
729 | w, h, view->x->depth); | |
730 | if (view->pixmap2 == None) { | |
731 | fprintf(stderr, | |
732 | "Sorry, Micropolis can't create a pixmap on X display \"%s\"!\n", | |
733 | view->x->display); | |
734 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
735 | return; | |
736 | } | |
737 | } | |
738 | ||
739 | } else { /* Editor_Class */ | |
740 | ||
741 | if ((w = (w + 31) & (~15)) > view->m_width) | |
742 | view->m_width = w, resize++; | |
743 | if ((h = (h + 31) & (~15)) > view->m_height) | |
744 | view->m_height = h, resize++; | |
745 | ||
746 | if (resize || (view->pixmap2 == None)) { | |
747 | if (view->pixmap2 != None) { | |
748 | XFreePixmap(view->x->dpy, view->pixmap2); | |
749 | view->pixmap2 = None; | |
750 | } | |
751 | view->pixmap2 = XCreatePixmap(view->x->dpy, view->x->root, | |
752 | view->m_width, view->m_height, | |
753 | view->x->depth); | |
754 | if (view->pixmap2 == None) { | |
755 | fprintf(stderr, | |
756 | "Sorry, Micropolis couldn't create a pixmap on X display \"%s\"!\n", | |
757 | view->x->display); | |
758 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
759 | return; | |
760 | } | |
761 | } | |
762 | ||
763 | if (resize || (view->overlay_pixmap == None)) { | |
764 | view->overlay_mode = 0; | |
765 | if (view->overlay_pixmap != None) { | |
766 | XFreePixmap(view->x->dpy, view->overlay_pixmap); | |
767 | view->overlay_pixmap = None; | |
768 | } | |
769 | view->overlay_pixmap = XCreatePixmap(view->x->dpy, view->x->root, | |
770 | view->m_width, view->m_height, | |
771 | 1); | |
772 | if (view->overlay_pixmap == None) { | |
773 | fprintf(stderr, | |
774 | "Sorry, Micropolis couldn't create another pixmap on X display \"%s\".\n", | |
775 | view->x->display); | |
776 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
777 | return; | |
778 | } | |
779 | if (view->x->overlay_gc == NULL) { | |
780 | unsigned long valuemask = 0; | |
781 | XGCValues values; | |
782 | ||
783 | view->x->overlay_gc = | |
784 | XCreateGC(view->x->dpy, view->overlay_pixmap, valuemask, &values); | |
785 | XSetForeground(view->x->dpy, view->x->overlay_gc, 0); | |
786 | XSetBackground(view->x->dpy, view->x->overlay_gc, 1); | |
787 | XSetLineAttributes(view->x->dpy, view->x->overlay_gc, | |
788 | 1, LineSolid, CapButt, JoinMiter); | |
789 | XSetGraphicsExposures(view->x->dpy, view->x->overlay_gc, False); | |
790 | } | |
791 | } | |
792 | ||
793 | } | |
794 | ||
795 | #ifndef MSDOS | |
796 | if (view->type != X_Mem_View) { | |
797 | goto SPRING_FORWARD; | |
798 | } | |
799 | ||
800 | if (resize || (view->image == NULL)) { | |
801 | if (view->shminfo && view->image) { | |
802 | if (view->pixmap != None) { | |
803 | XFreePixmap(view->x->dpy, view->pixmap); | |
804 | view->pixmap = None; | |
805 | } | |
806 | XShmDetach(view->x->dpy, view->shminfo); | |
807 | shmdt(view->shminfo->shmaddr); | |
808 | shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
809 | view->image->data = NULL; | |
810 | if (view->data == view->data8) | |
811 | view->data8 = NULL; | |
812 | view->data = NULL; | |
813 | XDestroyImage(view->image); | |
814 | view->image = NULL; | |
815 | } | |
816 | ||
817 | #if 0 | |
818 | /* XShmPixmapFormat is documented but does not exist !!! */ | |
819 | if (XShmPixmapFormat(view->x->dpy) != ZPixmap) { | |
820 | fprintf(stderr, | |
821 | "Darn, display \"%s\" has the wrong shared memory format.\n", | |
822 | view->x->display); | |
823 | goto FALL_BACK; | |
824 | } | |
825 | #endif | |
826 | ||
827 | if (!view->shminfo) { | |
828 | view->shminfo = (XShmSegmentInfo *)ckalloc(sizeof (XShmSegmentInfo)); | |
829 | } | |
830 | ||
831 | view->image = | |
832 | XShmCreateImage(view->x->dpy, view->x->visual, view->x->depth, | |
833 | view->x->color ? ZPixmap : XYBitmap, | |
834 | NULL, view->shminfo, | |
835 | view->m_width, view->m_height); | |
836 | ||
837 | view->line_bytes = view->image->bytes_per_line; | |
838 | ||
839 | switch (view->x->depth) { | |
840 | ||
841 | case 1: | |
842 | view->pixel_bytes = 0; | |
843 | view->depth = 1; | |
844 | break; | |
845 | ||
846 | case 8: | |
847 | view->pixel_bytes = 1; | |
848 | view->depth = 8; | |
849 | break; | |
850 | ||
851 | case 15: | |
852 | view->pixel_bytes = 2; | |
853 | view->depth = 15; | |
854 | break; | |
855 | ||
856 | case 16: | |
857 | view->pixel_bytes = 2; | |
858 | view->depth = 16; | |
859 | break; | |
860 | ||
861 | case 24: | |
862 | /* XXX: TODO: 24 and 32 bit support */ | |
863 | view->pixel_bytes = 4; | |
864 | //view->pixel_bytes = 3; | |
865 | view->depth = 24; | |
866 | break; | |
867 | ||
868 | case 32: | |
869 | /* XXX: TODO: 24 and 32 bit support */ | |
870 | view->pixel_bytes = 4; | |
871 | view->depth = 32; | |
872 | break; | |
873 | ||
874 | default: | |
875 | view->pixel_bytes = 0; | |
876 | view->depth = 0; | |
877 | break; | |
878 | ||
879 | } // switch | |
880 | ||
881 | view->shminfo->shmid = shmget(IPC_PRIVATE, | |
882 | (view->line_bytes * | |
883 | view->m_height), | |
884 | (IPC_CREAT | 0777)); | |
885 | if (view->shminfo->shmid < 0) { | |
886 | perror("shmget"); | |
887 | fprintf(stderr, | |
888 | "Darn, Micropolis can't share memory with X display \"%s\".\n", | |
889 | view->x->display); | |
890 | goto FALL_BACK; | |
891 | } | |
892 | ||
893 | view->data = (unsigned char *)shmat(view->shminfo->shmid, 0, 0); | |
894 | if ((int)view->data == -1) { | |
895 | perror("shmat"); | |
896 | fprintf(stderr, | |
897 | "Darn, Micropolis can't find any memory to share with display \"%s\".\n", | |
898 | view->x->display); | |
899 | goto FALL_BACK; | |
900 | } | |
901 | ||
902 | view->image->data = (char *)view->data; | |
903 | view->shminfo->shmaddr = (char *)view->data; | |
904 | view->shminfo->readOnly = False; | |
905 | ||
906 | { int (*old)(); | |
907 | int result; | |
908 | int attached = 0; | |
909 | GotXError = 0; | |
910 | old = XSetErrorHandler(CatchXError); | |
911 | ||
912 | result = | |
913 | XShmAttach(view->x->dpy, view->shminfo); | |
914 | if (result == 0) { | |
915 | fprintf(stderr, | |
916 | "Darn, the X display \"%s\" can't access Micropolis's shared memory.\n", | |
917 | view->x->display); | |
918 | GotXError = 1; | |
919 | } | |
920 | ||
921 | XSync(view->x->dpy, False); | |
922 | ||
923 | if (!GotXError) { | |
924 | attached = 1; | |
925 | view->pixmap = XShmCreatePixmap(view->x->dpy, view->x->root, | |
926 | view->data, view->shminfo, | |
927 | view->m_width, view->m_height, | |
928 | view->x->depth); | |
929 | XSync(view->x->dpy, False); | |
930 | ||
931 | if (GotXError || | |
932 | (view->pixmap == None)) { | |
933 | fprintf(stderr, | |
934 | "Darn, Micropolis couldn't get a shared memory pixmap on X display \"%s\".\n", | |
935 | view->x->display); | |
936 | GotXError = 1; | |
937 | } | |
938 | } | |
939 | ||
940 | XSetErrorHandler(old); | |
941 | ||
942 | if (GotXError) { | |
943 | view->pixmap = None; | |
944 | if (attached) { | |
945 | XShmDetach(view->x->dpy, view->shminfo); | |
946 | } // if | |
947 | result = shmdt(view->shminfo->shmaddr); | |
948 | result = shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
949 | ckfree(view->shminfo); | |
950 | view->shminfo = NULL; | |
951 | if (view->image) { | |
952 | view->image->data = NULL; | |
953 | view->data = NULL; | |
954 | XDestroyImage(view->image); | |
955 | view->image = NULL; | |
956 | } | |
957 | goto FALL_BACK; | |
958 | } | |
959 | ||
960 | if (view->x->color) { | |
961 | XSetForeground(view->x->dpy, view->x->gc, | |
962 | view->pixels[COLOR_LIGHTBROWN]); | |
963 | } else { | |
964 | XSetForeground(view->x->dpy, view->x->gc, | |
965 | view->pixels[COLOR_WHITE]); | |
966 | } | |
967 | ||
968 | XFillRectangle(view->x->dpy, view->pixmap, view->x->gc, | |
969 | 0, 0, view->m_width, view->m_height); | |
970 | } | |
971 | } | |
972 | ||
973 | goto FINISH; | |
974 | ||
975 | FALL_BACK: | |
976 | ||
977 | fprintf(stderr, | |
978 | "Falling back to the X network protocol on display \"%s\"...\n", | |
979 | view->x->display); | |
980 | #endif | |
981 | ||
982 | view->x->shared = 0; | |
983 | view->type = X_Wire_View; | |
984 | if (view->pixmap != None) { | |
985 | XFreePixmap(view->x->dpy, view->pixmap); | |
986 | view->pixmap = None; | |
987 | } | |
988 | #ifndef MSDOS | |
989 | if (view->shminfo) { | |
990 | if (view->shminfo->shmid >= 0) { | |
991 | if (view->shminfo->shmaddr) { | |
992 | shmdt(view->shminfo->shmaddr); | |
993 | } | |
994 | shmctl(view->shminfo->shmid, IPC_RMID, 0); | |
995 | } | |
996 | ckfree((char *)view->shminfo); | |
997 | view->shminfo = NULL; | |
998 | } | |
999 | #endif | |
1000 | if (view->image) { | |
1001 | view->image->data = NULL; | |
1002 | XDestroyImage(view->image); | |
1003 | view->image = NULL; | |
1004 | } | |
1005 | view->data = NULL; | |
1006 | view->line_bytes = 0; | |
1007 | view->pixel_bytes = 0; | |
1008 | view->depth = 0; | |
1009 | ||
1010 | SPRING_FORWARD: | |
1011 | ||
1012 | if (resize || (view->pixmap == None)) { | |
1013 | if (view->pixmap != None) { | |
1014 | XFreePixmap(view->x->dpy, view->pixmap); | |
1015 | view->pixmap = None; | |
1016 | } | |
1017 | view->pixmap = XCreatePixmap(view->x->dpy, view->x->root, | |
1018 | view->m_width, view->m_height, | |
1019 | view->x->depth); | |
1020 | if (view->pixmap == None) { | |
1021 | fprintf(stderr, | |
1022 | "Sorry, Micropolis can't create pixmap on X display \"%s\".\n", | |
1023 | view->x->display); | |
1024 | sim_exit(1); // Just sets tkMustExit and ExitReturn | |
1025 | return; | |
1026 | } | |
1027 | if (view->x->color) { | |
1028 | XSetForeground(view->x->dpy, view->x->gc, | |
1029 | view->pixels[COLOR_LIGHTBROWN]); | |
1030 | } else { | |
1031 | XSetForeground(view->x->dpy, view->x->gc, | |
1032 | view->pixels[COLOR_WHITE]); | |
1033 | } | |
1034 | XFillRectangle(view->x->dpy, view->pixmap, view->x->gc, | |
1035 | 0, 0, view->m_width, view->m_height); | |
1036 | } | |
1037 | ||
1038 | FINISH: | |
1039 | ||
1040 | if (view->class == Editor_Class) { | |
1041 | ||
1042 | AllocTiles(view); | |
1043 | DoAdjustPan(view); | |
1044 | ||
1045 | } else if (view->class == Map_Class) { | |
1046 | ||
1047 | if (view->type == X_Mem_View) { /* Memory Map */ | |
1048 | ||
1049 | if (view->x->color) { | |
1050 | ||
1051 | /* Color, Shared Memory */ | |
1052 | ||
1053 | view->data8 = view->data; | |
1054 | view->line_bytes8 = view->line_bytes; /* XXX: ??? */ | |
1055 | ||
1056 | switch (view->x->depth) { | |
1057 | ||
1058 | case 8: | |
1059 | view->pixel_bytes = 1; | |
1060 | view->depth = 8; | |
1061 | break; | |
1062 | ||
1063 | case 15: | |
1064 | view->pixel_bytes = 2; | |
1065 | view->depth = 15; | |
1066 | break; | |
1067 | ||
1068 | case 16: | |
1069 | view->pixel_bytes = 2; | |
1070 | view->depth = 16; | |
1071 | break; | |
1072 | ||
1073 | case 24: | |
1074 | /* XXX: TODO: 24 and 32 bit support */ | |
1075 | view->pixel_bytes = 4; | |
1076 | //view->pixel_bytes = 3; | |
1077 | view->depth = 24; | |
1078 | break; | |
1079 | ||
1080 | case 32: | |
1081 | /* XXX: TODO: 24 and 32 bit support */ | |
1082 | view->pixel_bytes = 4; | |
1083 | view->depth = 32; | |
1084 | break; | |
1085 | ||
1086 | default: | |
1087 | view->pixel_bytes = 0; | |
1088 | view->depth = 0; | |
1089 | break; | |
1090 | ||
1091 | } // switch | |
1092 | ||
1093 | } else { | |
1094 | ||
1095 | /* Black and White, Shared Memory */ | |
1096 | ||
1097 | if (view->other_image != NULL) { | |
1098 | XDestroyImage(view->other_image); | |
1099 | } | |
1100 | ||
1101 | view->line_bytes8 = view->m_width; /* XXX: fix depth */ | |
1102 | view->pixel_bytes = 0; | |
1103 | view->depth = 1; | |
1104 | ||
1105 | view->other_data = view->data8 = | |
1106 | AllocPixels(view->m_height * view->line_bytes8, /* XXX: fix depth */ | |
1107 | view->pixels[COLOR_WHITE]); | |
1108 | view->other_image = | |
1109 | XCreateImage(view->x->dpy, view->x->visual, 8, /* XXX: fix depth */ | |
1110 | ZPixmap, 0, (char *)view->other_data, | |
1111 | view->m_width, view->m_height, | |
1112 | 8, view->line_bytes8); /* XXX: fix depth */ | |
1113 | } | |
1114 | ||
1115 | } else { /* Wire Map */ | |
1116 | int bitmap_pad; | |
1117 | int bitmap_depth; | |
1118 | ||
1119 | if (view->image != NULL) { | |
1120 | XDestroyImage(view->image); | |
1121 | view->image = NULL; | |
1122 | } | |
1123 | ||
1124 | if (view->other_image != NULL) { | |
1125 | XDestroyImage(view->other_image); | |
1126 | view->other_image = NULL; | |
1127 | } | |
1128 | ||
1129 | if (view->x->color) { | |
1130 | ||
1131 | /* Color, Wire */ | |
1132 | ||
1133 | switch (view->x->depth) { | |
1134 | ||
1135 | case 8: | |
1136 | view->pixel_bytes = 1; | |
1137 | view->depth = 8; | |
1138 | bitmap_pad = 8; | |
1139 | bitmap_depth = 8; | |
1140 | view->line_bytes8 = | |
1141 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1142 | break; | |
1143 | ||
1144 | case 15: | |
1145 | view->pixel_bytes = 2; | |
1146 | view->depth = 15; | |
1147 | bitmap_pad = 16; | |
1148 | bitmap_depth = 16; | |
1149 | view->line_bytes8 = | |
1150 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1151 | break; | |
1152 | ||
1153 | case 16: | |
1154 | view->pixel_bytes = 2; | |
1155 | view->depth = 16; | |
1156 | bitmap_pad = 16; | |
1157 | bitmap_depth = 16; | |
1158 | view->line_bytes8 = | |
1159 | ((view->m_width * view->pixel_bytes) + 3) & (~3); | |
1160 | break; | |
1161 | ||
1162 | case 24: | |
1163 | view->pixel_bytes = 4; | |
1164 | //view->pixel_bytes = 3; | |
1165 | view->depth = 24; | |
93200beb | 1166 | bitmap_depth = 24; |
6a5fa4e0 MG |
1167 | bitmap_pad = 32; |
1168 | view->line_bytes8 = | |
1169 | ((view->m_width * 4) + 3) & (~3); | |
1170 | break; | |
1171 | ||
1172 | case 32: | |
1173 | view->pixel_bytes = 4; | |
1174 | view->depth = 32; | |
1175 | bitmap_pad = 32; | |
1176 | bitmap_depth = 32; | |
1177 | view->line_bytes8 = | |
1178 | ((view->m_width * 4) + 3) & (~3); | |
1179 | break; | |
1180 | ||
1181 | default: | |
1182 | assert(0); /* Unknown depth */ | |
1183 | break; | |
1184 | ||
1185 | } // switch | |
1186 | ||
1187 | view->line_bytes = | |
1188 | view->line_bytes8; | |
1189 | ||
1190 | } else { | |
1191 | ||
1192 | /* Black and White, Wire */ | |
1193 | ||
1194 | view->pixel_bytes = 0; | |
1195 | view->depth = 1; | |
1196 | view->line_bytes8 = | |
1197 | (view->m_width + 3) & (~3); /* XXX: handle depth */ | |
1198 | view->line_bytes = | |
1199 | (view->m_width + 7) >>3; | |
1200 | bitmap_pad = 8; | |
1201 | bitmap_depth = 8; | |
1202 | ||
1203 | } | |
1204 | ||
1205 | view->data = | |
1206 | AllocPixels(view->m_height * view->line_bytes, 0); | |
1207 | view->image = | |
1208 | XCreateImage(view->x->dpy, view->x->visual, | |
1209 | bitmap_depth, | |
1210 | view->x->color ? ZPixmap : XYBitmap, | |
1211 | 0, (char *)view->data, | |
1212 | view->m_width, view->m_height, | |
1213 | bitmap_pad, | |
1214 | view->line_bytes); | |
1215 | ||
1216 | view->other_data = | |
1217 | AllocPixels(view->m_height * view->line_bytes8, 0); | |
1218 | view->other_image = | |
1219 | XCreateImage(view->x->dpy, view->x->visual, | |
1220 | bitmap_depth, | |
1221 | ZPixmap, | |
1222 | 0, (char *)view->other_data, | |
1223 | view->m_width, view->m_height, | |
1224 | bitmap_pad, | |
1225 | view->line_bytes); | |
1226 | ||
1227 | if (view->x->color) { | |
1228 | view->data8 = view->data; | |
1229 | } else { | |
1230 | view->data8 = view->other_data; | |
1231 | } | |
1232 | } | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | ||
1237 | DoPanBy(struct SimView *view, int dx, int dy) | |
1238 | { | |
1239 | DoPanTo(view, view->pan_x + dx, view->pan_y + dy); | |
1240 | } | |
1241 | ||
1242 | ||
1243 | DoPanTo(struct SimView *view, int x, int y) | |
1244 | { | |
1245 | if (view->class != Editor_Class) { | |
1246 | return; | |
1247 | } | |
1248 | ||
1249 | if (x < 0) x = 0; | |
1250 | if (y < 0) y = 0; | |
1251 | if (x > view->i_width) x = view->i_width - 1; | |
1252 | if (y > view->i_height) y = view->i_height - 1; | |
1253 | if ((view->pan_x != x) || | |
1254 | (view->pan_y != y)) { | |
1255 | view->pan_x = x; | |
1256 | view->pan_y = y; | |
1257 | DoAdjustPan(view); | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | /* #define DEBUG_PAN */ | |
1262 | ||
1263 | DoAdjustPan(struct SimView *view) | |
1264 | { | |
1265 | int ww2 = view->w_width >>1, wh2 = view->w_height >>1; | |
1266 | int px = view->pan_x, py = view->pan_y; | |
1267 | int last_tile_x = view->tile_x, last_tile_y = view->tile_y; | |
1268 | int last_tile_width = view->tile_width, last_tile_height = view->tile_height; | |
1269 | int total_width = view->m_width >>4, total_height = view->m_height >>4; | |
1270 | //fprintf(stderr, "DoAdjustPan\n"); | |
1271 | ||
1272 | #ifdef DEBUG_PAN | |
1273 | printf("AdjustPan window %d %d ww2 %d wh2 %d pan %d %d\n", | |
1274 | view->w_width, view->w_height, ww2, wh2, px, py); | |
1275 | printf(" last tile %d %d %d %d\n", | |
1276 | last_tile_x, last_tile_y, last_tile_width, last_tile_height); | |
1277 | #endif | |
1278 | ||
1279 | if ((view->tile_x = ((px - ww2) >>4)) < 0) | |
1280 | view->tile_x = 0; | |
1281 | if ((view->tile_y = ((py - wh2) >>4)) < 0) | |
1282 | view->tile_y = 0; | |
1283 | ||
1284 | #ifdef DEBUG_PAN | |
1285 | printf(" now tile %d %d\n", view->tile_x, view->tile_y); | |
1286 | #endif | |
1287 | ||
1288 | view->tile_width = ((15 + px + ww2) >>4); | |
1289 | view->tile_height = ((15 + py + wh2) >>4); | |
1290 | ||
1291 | #ifdef DEBUG_PAN | |
1292 | printf(" outer tile %d %d\n", view->tile_width, view->tile_height); | |
1293 | #endif | |
1294 | ||
1295 | if (view->tile_width > (view->i_width >>4)) | |
1296 | view->tile_width = (view->i_width >>4); | |
1297 | view->tile_width -= view->tile_x; | |
1298 | if (view->tile_height > (view->i_height >>4)) | |
1299 | view->tile_height = (view->i_height >>4); | |
1300 | view->tile_height -= view->tile_y; | |
1301 | ||
1302 | #ifdef DEBUG_PAN | |
1303 | printf(" tile size %d %d\n", view->tile_width, view->tile_height); | |
1304 | #endif | |
1305 | ||
1306 | if (view->tile_width > (view->m_width >>4)) | |
1307 | view->tile_width = (view->m_width >>4); | |
1308 | if (view->tile_height > (view->m_height >>4)) | |
1309 | view->tile_height = (view->m_height >>4); | |
1310 | ||
1311 | #ifdef DEBUG_PAN | |
1312 | printf(" clipped size %d %d\n", view->tile_width, view->tile_height); | |
1313 | printf(" maximum size %d %d\n", view->m_width >>4, view->m_height >>4); | |
1314 | #endif | |
1315 | ||
1316 | view->screen_x = (ww2 - px) + (view->tile_x <<4); | |
1317 | view->screen_y = (wh2 - py) + (view->tile_y <<4); | |
1318 | view->screen_width = (view->tile_width <<4); | |
1319 | view->screen_height = (view->tile_height <<4); | |
1320 | ||
1321 | #ifdef DEBUG_PAN | |
1322 | printf(" screen %d %d %d %d\n", | |
1323 | view->screen_x, view->screen_y, | |
1324 | view->screen_width, view->screen_height); | |
1325 | #endif | |
1326 | ||
1327 | view->overlay_mode = 0; | |
1328 | // view->skip = 0; | |
1329 | view->invalid = 1; | |
1330 | if (SimSpeed == 0) { | |
1331 | EventuallyRedrawView(view); | |
1332 | } | |
1333 | /* InvalidateEditors(); */ | |
1334 | if (view->show_me) { | |
1335 | RedrawMaps(); | |
1336 | } | |
1337 | /* FixMicropolisTimer(); */ | |
1338 | ||
1339 | { int dx = last_tile_x - view->tile_x, | |
1340 | dy = last_tile_y - view->tile_y; | |
1341 | short **want = view->other_tiles, | |
1342 | **have = view->tiles; | |
1343 | ||
1344 | #ifdef DEBUG_PAN | |
1345 | printf("scrolling %d %d\n", dx, dy); | |
1346 | #endif | |
1347 | ||
1348 | if ((dx != 0) || (dy != 0)) { | |
1349 | int row, col, x, y, | |
1350 | width = view->tile_width, | |
1351 | height = view->tile_height; | |
1352 | ||
1353 | for (col = 0; col < width; col++) | |
1354 | memcpy(want[col], have[col], (height * sizeof(short))); | |
1355 | ||
1356 | for (col = 0; col < total_width; col++) { | |
1357 | x = col - dx; | |
1358 | for (row = 0; row < total_height; row++) { | |
1359 | y = row - dy; | |
1360 | if ((x >= 0) && (x < width) && | |
1361 | (y >= 0) && (y < height)) { | |
1362 | have[col][row] = want[x][y]; | |
1363 | } else { | |
1364 | have[col][row] = -1; | |
1365 | } | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | XCopyArea(view->x->dpy, view->pixmap, view->pixmap, view->x->gc, | |
1370 | 0, 0, view->tile_width <<4, view->tile_height <<4, | |
1371 | dx <<4, dy <<4); | |
1372 | ||
1373 | if (view->type == X_Mem_View) { | |
1374 | XSync(view->x->dpy, False); | |
1375 | } | |
1376 | } | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | ||
1381 | AllocTiles(SimView *view) | |
1382 | { | |
1383 | int row, col; | |
1384 | short **have, **want; | |
1385 | int w = view->m_width / 16, h = view->m_height / 16; | |
1386 | int n = (w + 1) * sizeof (short *); | |
1387 | ||
1388 | if (view->tiles) | |
1389 | FreeTiles(view); | |
1390 | ||
1391 | have = view->tiles = | |
1392 | (short **)ckalloc(n); | |
1393 | ||
1394 | want = view->other_tiles = | |
1395 | (short **)ckalloc(n); | |
1396 | ||
1397 | have[w] = want[w] = NULL; | |
1398 | ||
1399 | n = h * sizeof(short); | |
1400 | for (col = 0; col < w; col++) { | |
1401 | ||
1402 | have[col] = (short *)ckalloc(n); | |
1403 | want[col] = (short *)ckalloc(n); | |
1404 | for (row = 0; row < h; row++) { | |
1405 | have[col][row] = -1; | |
1406 | want[col][row] = -1; | |
1407 | } | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | ||
1412 | FreeTiles(SimView *view) | |
1413 | { | |
1414 | int col; | |
1415 | ||
1416 | for (col = 0; view->tiles[col] != NULL; col++) { | |
1417 | ckfree ((char *)view->tiles[col]); | |
1418 | ckfree ((char *)view->other_tiles[col]); | |
1419 | } | |
1420 | ckfree ((char *)view->tiles); | |
1421 | view->tiles = NULL; | |
1422 | ckfree ((char *)view->other_tiles); | |
1423 | view->other_tiles = NULL; | |
1424 | } | |
1425 | ||
1426 | ||
1427 | #define POINT_BATCH 32 | |
1428 | ||
1429 | Ink *OldInk = NULL; | |
1430 | ||
1431 | ||
1432 | /* XXX: todo: ink locking so someone doesn't erase ink that's being drawn */ | |
1433 | ||
1434 | Ink * | |
1435 | NewInk() | |
1436 | { | |
1437 | Ink *ink; | |
1438 | ||
1439 | if (OldInk) { | |
1440 | ink = OldInk; | |
1441 | OldInk = OldInk->next; | |
1442 | } else { | |
1443 | ink = (Ink *)ckalloc(sizeof(Ink)); | |
1444 | ink->maxlength = POINT_BATCH; | |
1445 | ink->points = (XPoint *)ckalloc(POINT_BATCH * sizeof(XPoint)); | |
1446 | } | |
1447 | ink->length = 0; | |
1448 | ink->color = COLOR_WHITE; | |
1449 | ink->next = NULL; | |
1450 | ink->left = ink->right = ink->top = ink->bottom = | |
1451 | ink->last_x = ink->last_y = -1; | |
1452 | return (ink); | |
1453 | } | |
1454 | ||
1455 | ||
1456 | FreeInk(Ink *ink) | |
1457 | { | |
1458 | ink->next = OldInk; | |
1459 | OldInk = ink; | |
1460 | } | |
1461 | ||
1462 | ||
1463 | StartInk(Ink *ink, int x, int y) | |
1464 | { | |
1465 | ink->length = 1; | |
1466 | ink->left = ink->right = ink->last_x = ink->points[0].x = x; | |
1467 | ink->top = ink->bottom = ink->last_y = ink->points[0].y = y; | |
1468 | } | |
1469 | ||
1470 | ||
1471 | AddInk(Ink *ink, int x, int y) | |
1472 | { | |
1473 | int dx = x - ink->last_x; | |
1474 | int dy = y - ink->last_y; | |
1475 | ||
1476 | if ((dx != 0) || (dy != 0)) { | |
1477 | /* | |
1478 | if (ink->length > 1) { | |
1479 | if ((dx == 0) && | |
1480 | (ink->points[ink->length - 1].x == 0) && | |
1481 | ((ink->points[ink->length - 1].y < 0) ? | |
1482 | (dy < 0) : (dy > 0))) { | |
1483 | ink->points[ink->length - 1].y += dy; | |
1484 | goto ADJUST; | |
1485 | } else if ((dy == 0) && | |
1486 | (ink->points[ink->length - 1].y == 0) && | |
1487 | ((ink->points[ink->length - 1].x < 0) ? | |
1488 | (dx < 0) : (dx > 0))) { | |
1489 | ink->points[ink->length - 1].x += dx; | |
1490 | goto ADJUST; | |
1491 | } | |
1492 | } | |
1493 | */ | |
1494 | ||
1495 | if (ink->length >= ink->maxlength) { | |
1496 | ink->maxlength += POINT_BATCH; | |
1497 | ink->points = (XPoint *)realloc((void *)ink->points, | |
1498 | ink->maxlength * sizeof(XPoint)); | |
1499 | } | |
1500 | ink->points[ink->length].x = dx; | |
1501 | ink->points[ink->length].y = dy; | |
1502 | ink->length++; | |
1503 | ||
1504 | ADJUST: | |
1505 | if (x < ink->left) | |
1506 | ink->left = x; | |
1507 | if (x > ink->right) | |
1508 | ink->right = x; | |
1509 | if (y < ink->top) | |
1510 | ink->top = y; | |
1511 | if (y > ink->bottom) | |
1512 | ink->bottom = y; | |
1513 | ||
1514 | { int left, right, top, bottom; | |
1515 | SimView *view; | |
1516 | ||
1517 | if (ink->last_x < x) { left = ink->last_x; right = x; } | |
1518 | else { left = x; right = ink->last_x; } | |
1519 | if (ink->last_y < y) { top = ink->last_y; bottom = y; } | |
1520 | else { top = y; bottom = ink->last_y; } | |
1521 | ||
1522 | left -= 5; right += 5; top -= 5; bottom += 5; | |
1523 | ||
1524 | for (view = sim->editor; view != NULL; view = view->next) { | |
1525 | int vleft, vtop; | |
1526 | ||
1527 | if ((right >= (vleft = view->pan_x - (view->w_width / 2))) && | |
1528 | (left <= vleft + view->w_width) && | |
1529 | (bottom >= (vtop = view->pan_y - (view->w_height / 2))) && | |
1530 | (top <= vtop + view->w_height)) { | |
1531 | /* XXX: do studly incremental update instead */ | |
1532 | view->overlay_mode = 0; | |
1533 | EventuallyRedrawView(view); | |
1534 | } | |
1535 | } | |
1536 | } | |
1537 | ink->last_x = x; ink->last_y = y; | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | ||
1542 | EraseOverlay() | |
1543 | { | |
1544 | Ink *ink; | |
1545 | ||
1546 | while (sim->overlay) { | |
1547 | ink = sim->overlay; | |
1548 | sim->overlay = ink->next; | |
1549 | FreeInk(ink); | |
1550 | } | |
1551 | } |