1 /* ---------------------------------------------------------------------- *
2  * picedit.c
3  * This file is part of lincity.
4  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
5  * Thanks to Gerben V for X true color support.
6  * ---------------------------------------------------------------------- */
7 #include "lcconfig.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include "lcstring.h"
12 #ifdef LC_X11
13 #include <X11/Xlib.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xutil.h>
16 #include "xpicedit.h"
17 #elif defined (SVGALIB)
18 #include <vga.h>
19 #include <vgagl.h>
20 #include <vgamouse.h>
21 #endif
22 
23 #define LIBDIR DEFAULT_LIBDIR
24 
25 #ifdef __EMX__
26 #include <X11/Xlibint.h>      /* required for __XOS2RedirRoot */
27 #define chown(x,y,z)
28 #ifdef LIBDIR
29 #undef LIBDIR   /* yes, I know I shouldn't ;-) */
30 #endif
31 char LIBDIR[256];
32 #define OS2_DEFAULT_LIBDIR "/XFree86/lib/X11/lincity"
33 #endif
34 
35 #define COLOUR_PAL_FILE "/colour.pal"
36 #define FONTFILE "/opening/iso8859-1-8x8.raw"
37 
38 #define WINSTARTX 240
39 #define WINSTARTY ((14*16)-1)
40 #define SWINSTARTX 56
41 #define SWINSTARTY 260
42 #define MAXIMAGESIZE 4
43 
44 #define SIZEBUTTONX1x1 540
45 #define SIZEBUTTONY1x1 260
46 #define SIZEBUTTONX2x2 540
47 #define SIZEBUTTONY2x2 276
48 #define SIZEBUTTONX3x3 540
49 #define SIZEBUTTONY3x3 292
50 #define SIZEBUTTONX4x4 540
51 #define SIZEBUTTONY4x4 308
52 #define SIZEBUTTONSIZEX 32
53 #define SIZEBUTTONSIZEY 16
54 
55 #define FOCUSWINX 40
56 #define FOCUSWINY 360
57 /* FOCUSWINSIZE must be a multiple of 4 */
58 #define FOCUSWINSIZE 56
59 
60 #define ZOOMBUTX 540
61 #define ZOOMBUTY 390
62 #define ZOOMBUTSIZEX 40
63 #define ZOOMBUTSIZEY 16
64 
65 #define HALFBUTX 540
66 #define HALFBUTY 370
67 #define HALFBUTSIZEX 40
68 #define HALFBUTSIZEY 16
69 
70 #define NORMBUTX 540
71 #define NORMBUTY 350
72 #define NORMBUTSIZEX 40
73 #define NORMBUTSIZEY 16
74 
75 #define DOTBUTX 150
76 #define DOTBUTY 350
77 #define DOTBUTSIZEX 40
78 #define DOTBUTSIZEY 16
79 
80 #define HFILBUTX 150
81 #define HFILBUTY 370
82 #define HFILBUTSIZEX 40
83 #define HFILBUTSIZEY 16
84 
85 #define VFILBUTX 150
86 #define VFILBUTY 390
87 #define VFILBUTSIZEX 40
88 #define VFILBUTSIZEY 16
89 
90 #define red(x) (32 + x)
91 #define green(x) (64 + x)
92 #define yellow(x) (96 + x)
93 #define blue(x) (128 + x)
94 #define magenta(x) (160 + x)
95 #define cyan(x) (192 + x)
96 #define white(x) (224 + x)
97 
98 #define FOCUSWINCOLOUR green(30)
99 #define UNFOCUSWINCOLOUR magenta(20)
100 #define XFOCUSWINCOLOUR red(15)
101 
102 unsigned char mouse_pointer[] =
103 {
104   255, 255, 255, 255, 1, 1, 1, 1,
105   255, 1, 1, 1, 255, 255, 1, 1,
106   255, 1, 255, 255, 255, 1, 255, 1,
107   255, 1, 255, 1, 1, 255, 1, 1,
108   1, 255, 255, 1, 1, 1, 255, 1,
109   1, 255, 1, 255, 1, 1, 1, 1,
110   1, 1, 255, 1, 255, 1, 1, 1,
111   1, 1, 1, 1, 1, 1, 1};
112 
113 unsigned char under_mouse_pointer[8 * 8], last16[16];
114 unsigned char image[16 * MAXIMAGESIZE][16 * MAXIMAGESIZE];
115 char imagefile[100];
116 
117 int colour = 0, mx, my, mox, moy, zoom_flag = 1, hfill_flag = 0, vfill_flag = 0;
118 int imagesize, newimagesize, focus, focusx, focusy;
119 int old_focusx = 0, old_focusy = 0, stopflag, lc_mouse_type;
120 
121 void do_buttons (int, int);
122 void draw_dot (void);
123 void draw_hfill (void);
124 void draw_vfill (void);
125 void full_zoom (void);
126 void half_zoom (void);
127 void norm_zoom (void);
128 void do_error (char *);
129 void choose_colour (int, int);
130 void fill_colour (int, int);
131 void hfill (int, int);
132 void vfill (int, int);
133 void fill_colourzoom (int, int);
134 void fill_colour1 (int, int);
135 void fill_colour2 (int, int);
136 void fill_colour3 (int, int);
137 void fill_colour4 (int, int);
138 void setcustompalette (void);
139 void initfont (void);
140 void save_image (void);
141 void load_image (void);
142 void setup (void);
143 void draw_focus (void);
144 void draw_swin (void);
145 void draw_big_image (void);
146 void draw_big_imagezoom (void);
147 void draw_big_image1 (void);
148 void draw_big_image2 (void);
149 void draw_big_image3 (void);
150 void draw_big_image4 (void);
151 void change_imagesize (int);
152 void change_focus (int, int);
153 void hide_mouse (void);
154 void reshow_mouse (void);
155 
156 #ifdef LC_X11
157 
158 void Fgl_hline (int, int, int, int);
159 void Fgl_line (int, int, int, int, int);
160 void Fgl_setpixel (int, int, int);
161 int Fgl_getpixel (int, int);
162 void Fgl_write (int, int, char *);
163 void Fgl_fillbox (int, int, int, int, int);
164 void Fgl_putbox (int, int, int, int, unsigned char *);
165 void Fgl_getbox (int, int, int, int, unsigned char *);
166 
167 #else
168 
169 void lincityrc ();
170 
171 #define Fgl_setpixel(a,b,c)    gl_setpixel(a,b,c)
172 #define Fgl_getpixel(a,b)      gl_getpixel(a,b)
173 #define Fgl_hline(a,b,c,d)     gl_hline(a,b,c,d)
174 #define Fgl_line(a,b,c,d,e)    gl_line(a,b,c,d,e)
175 #define Fgl_write(a,b,c)       gl_write(a,b,c)
176 #define Fgl_fillbox(a,b,c,d,e) gl_fillbox(a,b,c,d,e)
177 #define Fgl_putbox(a,b,c,d,e)  gl_putbox(a,b,c,d,e)
178 #define Fgl_getbox(a,b,c,d,e)  gl_getbox(a,b,c,d,e)
179 
180 #endif
181 
182 #ifdef LC_X11
183 
184 int winX, winY, mouse_button;
185 unsigned int winW, winH;
186 disp display;
187 
188 char *bg_color = NULL;
189 char pixmap[(640 + BORDERX * 2) * (480 + BORDERY * 2)];
190 char myfont[256 * 8];
191 int verbose = FALSE;
192 int text_bg = 0;
193 int text_fg = 255;
194 int borderx, bordery;
195 long unsigned int colour_table[256];
196 
197 void parse_args (int, char **, char **);
198 void Create_Window (char *);
199 void HandleError (char *, int);
200 void HandleEvent (XEvent *);
201 void refresh_screen (int, int, int, int);
202 void my_x_putchar (int, int, int);
203 
204 #endif
205 int
main(int argc,char * argv[])206 main (int argc, char *argv[])
207 {
208   int x, y, c, m, ox, oy;
209 #ifdef LC_X11
210   XEvent xev;
211   char *geometry = NULL;
212 #endif
213   c = 0;
214   stopflag = 0;
215   newimagesize = 1;
216 
217 #ifdef __EMX__
218   strcpy(LIBDIR, __XOS2RedirRoot(OS2_DEFAULT_LIBDIR));
219 #endif
220 
221 #ifdef LC_X11
222   borderx = BORDERX;
223   bordery = BORDERY;
224   parse_args (argc, argv, &geometry);
225   printf ("Got past parse args\n");
226   Create_Window (geometry);
227   XSync (display.dpy, FALSE);
228 
229   while (XPending (display.dpy) == 0);
230   XNextEvent (display.dpy, &xev);
231   HandleEvent (&xev);
232 
233   printf ("Got past create window\n");
234 #else
235   lincityrc ();
236   vga_init ();
237   vga_setmode (G640x480x256);
238   gl_setcontextvga (G640x480x256);
239 #endif
240 
241   setcustompalette ();
242   initfont ();
243   if (argc < 2)
244     strcpy (imagefile, "imagefile.default");
245   else
246     {
247       strcpy (imagefile, argv[1]);
248       load_image ();
249     }
250 #ifndef LC_X11
251   m = mouse_init ("/dev/mouse", lc_mouse_type, MOUSE_DEFAULTSAMPLERATE);
252   mouse_setxrange (0, 640 - 1);
253   mouse_setyrange (0, 480 - 1);
254   mouse_setwrap (MOUSE_NOWRAP);
255 #endif
256   setup ();
257 #ifdef LC_X11
258   XSync (display.dpy, FALSE);
259 #endif
260   printf ("Got past setup\n");
261   if (newimagesize != imagesize)
262     change_imagesize (newimagesize);
263   change_focus (0, 0);
264   printf ("Got to do loop\n");
265   do
266     {
267 #ifdef LC_X11
268       if (XPending (display.dpy))
269 	{
270 	  XNextEvent (display.dpy, &xev);
271 	  HandleEvent (&xev);
272 	}
273       if (mouse_button != 0)
274 	do_buttons (0, 0);
275 #else
276       c = vga_getkey ();
277       mouse_waitforupdate ();
278       mx = mouse_getx ();
279       my = mouse_gety ();
280       if (mx != mox || my != moy)
281 	{
282 	  Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
283 	  Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
284 	  Fgl_putbox (mx, my, 8, 8, mouse_pointer);
285 	  mox = mx;
286 	  moy = my;
287 	}
288       do_buttons (mx, my);
289 #endif
290     }
291   while (c == 0 && stopflag == 0);
292   printf ("Normal exit\n");
293 #ifndef LC_X11
294   mouse_close ();
295   vga_setmode (TEXT);
296   printf ("c=%d mouse %d\n", c, m);
297 #endif
298   save_image ();
299   exit (0);
300 }
301 
302 void
do_error(char * s)303 do_error (char *s)
304 {
305 #ifdef LC_X11
306   HandleError (s, FATAL);
307 #else
308   vga_setmode (TEXT);
309   printf ("%s\n", s);
310   exit (1);
311 #endif
312 }
313 
314 void
do_buttons(int x,int y)315 do_buttons (int x, int y)
316 {
317   int b, left = 0, right = 0;
318 #ifdef LC_X11
319   int dummy_int;
320   Window dummy_win;
321   if (mouse_button == Button1)
322     left = 1;
323   else if (mouse_button == Button3)
324     right = 1;
325   XQueryPointer (display.dpy, display.win, &dummy_win, &dummy_win
326 	      ,&dummy_int, &dummy_int, &x, &y, (unsigned int *) &dummy_int);
327   x -= borderx;
328   y -= bordery;
329 #else
330   b = mouse_getbutton ();
331   left = b & MOUSE_LEFTBUTTON;
332   right = b & MOUSE_RIGHTBUTTON;
333 #endif
334   if (right != 0 && x < 560)
335     choose_colour (x, y);
336   if (left != 0)
337     {
338       if (y < 16 * 14 - 2)
339 	choose_colour (x, y);
340       else if (x < WINSTARTX + 256 && x >= WINSTARTX
341 	       && y < WINSTARTY + 256 && y > WINSTARTY)
342 	fill_colour (x, y);
343       else if (x > SIZEBUTTONX1x1
344 	       && x < SIZEBUTTONX1x1 + SIZEBUTTONSIZEX
345 	       && y > SIZEBUTTONY1x1
346 	       && y < SIZEBUTTONY1x1 + SIZEBUTTONSIZEY
347 	       && imagesize != 1)
348 	change_imagesize (1);
349       else if (x > SIZEBUTTONX2x2
350 	       && x < SIZEBUTTONX2x2 + SIZEBUTTONSIZEX
351 	       && y > SIZEBUTTONY2x2
352 	       && y < SIZEBUTTONY2x2 + SIZEBUTTONSIZEY
353 	       && imagesize != 2)
354 	change_imagesize (2);
355       else if (x > SIZEBUTTONX3x3
356 	       && x < SIZEBUTTONX3x3 + SIZEBUTTONSIZEX
357 	       && y > SIZEBUTTONY3x3
358 	       && y < SIZEBUTTONY3x3 + SIZEBUTTONSIZEY
359 	       && imagesize != 3)
360 	change_imagesize (3);
361       else if (x > SIZEBUTTONX4x4
362 	       && x < SIZEBUTTONX4x4 + SIZEBUTTONSIZEX
363 	       && y > SIZEBUTTONY4x4
364 	       && y < SIZEBUTTONY4x4 + SIZEBUTTONSIZEY
365 	       && imagesize != 4)
366 	change_imagesize (4);
367       else if (x > FOCUSWINX && x < FOCUSWINX + FOCUSWINSIZE
368 	       && y > FOCUSWINY && FOCUSWINY + FOCUSWINSIZE
369 	       && zoom_flag == 1)
370 	change_focus ((x - FOCUSWINX) / (FOCUSWINSIZE / 4)
371 		      ,(y - FOCUSWINY) / (FOCUSWINSIZE / 4));
372       else if (x > ZOOMBUTX && x < ZOOMBUTX + ZOOMBUTSIZEX
373 	       && y > ZOOMBUTY && y < ZOOMBUTY + ZOOMBUTSIZEY)
374 	full_zoom ();
375       else if (x > HALFBUTX && x < HALFBUTX + HALFBUTSIZEX
376 	       && y > HALFBUTY && y < HALFBUTY + HALFBUTSIZEY)
377 	half_zoom ();
378       else if (x > NORMBUTX && x < NORMBUTX + NORMBUTSIZEX
379 	       && y > NORMBUTY && y < NORMBUTY + NORMBUTSIZEY)
380 	norm_zoom ();
381       else if (x > DOTBUTX && x < DOTBUTX + DOTBUTSIZEX
382 	       && y > DOTBUTY && y < DOTBUTY + DOTBUTSIZEY)
383 	draw_dot ();
384       else if (x > HFILBUTX && x < HFILBUTX + HFILBUTSIZEX
385 	       && y > HFILBUTY && y < HFILBUTY + HFILBUTSIZEY)
386 	draw_hfill ();
387       else if (x > VFILBUTX && x < VFILBUTX + VFILBUTSIZEX
388 	       && y > VFILBUTY && y < VFILBUTY + VFILBUTSIZEY)
389 	draw_vfill ();
390     }
391 }
392 
393 void
draw_dot(void)394 draw_dot (void)
395 {
396   Fgl_write (DOTBUTX - 10, DOTBUTY + 3, ">");
397   Fgl_write (HFILBUTX - 10, HFILBUTY + 3, " ");
398   Fgl_write (VFILBUTX - 10, VFILBUTY + 3, " ");
399   hfill_flag = 0;
400   vfill_flag = 0;
401 }
402 
403 void
draw_hfill(void)404 draw_hfill (void)
405 {
406   Fgl_write (DOTBUTX - 10, DOTBUTY + 3, " ");
407   Fgl_write (HFILBUTX - 10, HFILBUTY + 3, ">");
408   Fgl_write (VFILBUTX - 10, VFILBUTY + 3, " ");
409   hfill_flag = 1;
410   vfill_flag = 0;
411 }
412 
413 void
draw_vfill(void)414 draw_vfill (void)
415 {
416   Fgl_write (DOTBUTX - 10, DOTBUTY + 3, " ");
417   Fgl_write (HFILBUTX - 10, HFILBUTY + 3, " ");
418   Fgl_write (VFILBUTX - 10, VFILBUTY + 3, ">");
419   vfill_flag = 1;
420   hfill_flag = 0;
421 }
422 
423 
424 void
full_zoom(void)425 full_zoom (void)
426 {
427   int y;
428   if (zoom_flag == 0)
429     return;
430   Fgl_write (ZOOMBUTX - 10, ZOOMBUTY + 3, ">");
431   Fgl_write (HALFBUTX - 10, HALFBUTY + 3, " ");
432   Fgl_write (NORMBUTX - 10, NORMBUTY + 3, " ");
433   if (imagesize == 3)
434     {
435       Fgl_fillbox (WINSTARTX - 1, WINSTARTY - 1
436 		   ,258, 258, 0);
437       Fgl_hline (WINSTARTX - 1, WINSTARTY - 1
438 		 ,WINSTARTX + 240, 255);
439       Fgl_hline (WINSTARTX - 1, WINSTARTY + 240
440 		 ,WINSTARTX + 240, 255);
441       Fgl_line (WINSTARTX - 1, WINSTARTY - 1, WINSTARTX - 1
442 		,WINSTARTY + 240, 255);
443       Fgl_line (WINSTARTX + 240, WINSTARTY - 1, WINSTARTX + 240
444 		,WINSTARTY + 240, 255);
445     }
446   zoom_flag = 0;
447   old_focusx = focusx;
448   old_focusy = focusy;
449   change_focus (0, 0);
450 
451   draw_big_image ();
452 }
453 
454 void
half_zoom(void)455 half_zoom (void)
456 {
457   int y;
458   if (zoom_flag == 2 || imagesize < 2)
459     return;
460   zoom_flag = 2;
461   Fgl_write (ZOOMBUTX - 10, ZOOMBUTY + 3, " ");
462   Fgl_write (HALFBUTX - 10, HALFBUTY + 3, ">");
463   Fgl_write (NORMBUTX - 10, NORMBUTY + 3, " ");
464   if (zoom_flag != 0)
465     {
466       old_focusx = focusx;
467       old_focusy = focusy;
468     }
469   if (focusx >= (imagesize - 1))
470     focusx = imagesize - 2;
471   if (focusy >= (imagesize - 1))
472     focusy = imagesize - 2;
473   change_focus (focusx, focusy);
474   draw_big_image ();
475 }
476 
477 
478 void
norm_zoom(void)479 norm_zoom (void)
480 {
481   int y;
482   if (zoom_flag == 1)
483     return;
484   Fgl_write (ZOOMBUTX - 10, ZOOMBUTY + 3, " ");
485   Fgl_write (HALFBUTX - 10, HALFBUTY + 3, " ");
486   Fgl_write (NORMBUTX - 10, NORMBUTY + 3, ">");
487   Fgl_hline (WINSTARTX, WINSTARTY - 1, WINSTARTX + 256, 255);
488   Fgl_hline (WINSTARTX, WINSTARTY + 256, WINSTARTX + 256, 255);
489   Fgl_line (WINSTARTX - 1, WINSTARTY, WINSTARTX - 1, WINSTARTY + 256, 255);
490   Fgl_line (WINSTARTX + 256, WINSTARTY
491 	    ,WINSTARTX + 256, WINSTARTY + 256, 255);
492   for (y = 0; y < 16; y++)
493     {
494       Fgl_hline (WINSTARTX, WINSTARTY + y * 16 + 15
495 		 ,WINSTARTX + 255, 253);
496       Fgl_line (WINSTARTX + y * 16 + 15, WINSTARTY, WINSTARTX + y * 16 + 15
497 		,WINSTARTY + 255, 253);
498     }
499   zoom_flag = 1;
500   change_focus (old_focusx, old_focusy);
501   draw_big_image ();
502 }
503 
504 void
choose_colour(int x,int y)505 choose_colour (int x, int y)
506 {
507   char s[100];
508   int q, newcolour;
509   newcolour = Fgl_getpixel (x - 1, y - 1);
510   if (newcolour == colour)
511     return;
512   colour = newcolour;
513   for (q = 15; q > 0; q--)
514     last16[q] = last16[q - 1];
515   last16[0] = colour;
516   for (q = 0; q < 16; q++)
517     {
518       Fgl_fillbox (600, 14 * 16 + q * 16, 39, 15, (int) last16[q]);
519       sprintf (s, "%d", (int) last16[q]);
520       Fgl_write (600 + 3, 14 * 16 + q * 16 + 3, s);
521     }
522 }
523 
524 void
fill_colour(int x,int y)525 fill_colour (int x, int y)
526 {
527   if (hfill_flag != 0)
528     hfill (x, y);
529   else if (vfill_flag != 0)
530     vfill (x, y);
531   else if (zoom_flag == 1)
532     fill_colourzoom (x, y);
533   else if (zoom_flag == 2)
534     fill_colour2 (x, y);
535   else if (imagesize == 1)
536     fill_colour1 (x, y);
537   else if (imagesize == 2)
538     fill_colour2 (x, y);
539   else if (imagesize == 3)
540     fill_colour3 (x, y);
541   else if (imagesize == 4)
542     fill_colour4 (x, y);
543 }
544 
545 void
hfill(int x,int y)546 hfill (int x, int y)
547 {
548   int xx = 0, col, d;
549   if (zoom_flag == 1)
550     d = 16;
551   else if (zoom_flag == 2)
552     d = 8;
553   else if (imagesize == 1)
554     d = 16;
555   else if (imagesize == 2)
556     d = 8;
557   else if (imagesize == 3)
558     d = 5;
559   else if (imagesize == 4)
560     d = 4;
561   col = image[((x - WINSTARTX) / d) + (focusx * 16)]
562     [((y - (WINSTARTY)) / d) + (focusy * 16)];
563 
564   while (((x - WINSTARTX) / d) + (focusx * 16) + xx < 16 * MAXIMAGESIZE
565 	 && (image[((x - WINSTARTX) / d) + (focusx * 16) + xx]
566 	     [((y - (WINSTARTY)) / d) + (focusy * 16)] == col))
567     {
568       image[((x - WINSTARTX) / d) + (focusx * 16) + xx]
569 	[((y - (WINSTARTY)) / d) + (focusy * 16)] = colour;
570       xx++;
571     }
572   xx = -1;
573   while (((x - WINSTARTX) / d) + (focusx * 16) + xx >= 0
574 	 && (image[((x - WINSTARTX) / d) + (focusx * 16) + xx]
575 	     [((y - (WINSTARTY)) / d) + (focusy * 16)] == col))
576     {
577       image[((x - WINSTARTX) / d) + (focusx * 16) + xx]
578 	[((y - (WINSTARTY)) / d) + (focusy * 16)] = colour;
579       xx--;
580     }
581   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
582   draw_swin ();
583   draw_big_image ();
584   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
585   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
586 }
587 
588 void
vfill(int x,int y)589 vfill (int x, int y)
590 {
591   int yy = 0, col, d;
592   if (zoom_flag == 1)
593     d = 16;
594   else if (zoom_flag == 2)
595     d = 8;
596   else if (imagesize == 1)
597     d = 16;
598   else if (imagesize == 2)
599     d = 8;
600   else if (imagesize == 3)
601     d = 5;
602   else if (imagesize == 4)
603     d = 4;
604   col = image[((x - WINSTARTX) / d) + (focusx * 16)]
605     [((y - (WINSTARTY)) / d) + (focusy * 16)];
606 
607   while (((y - WINSTARTY) / d) + (focusy * 16) + yy < 16 * MAXIMAGESIZE
608 	 && (image[((x - WINSTARTX) / d) + (focusx * 16)]
609 	     [((y - (WINSTARTY)) / d) + (focusy * 16) + yy] == col))
610     {
611       image[((x - WINSTARTX) / d) + (focusx * 16)]
612 	[((y - (WINSTARTY)) / d) + (focusy * 16) + yy] = colour;
613       yy++;
614     }
615   yy = -1;
616   while (((y - WINSTARTY) / d) + (focusy * 16) + yy >= 0
617 	 && (image[((x - WINSTARTX) / d) + (focusx * 16)]
618 	     [((y - (WINSTARTY)) / d) + (focusy * 16) + yy] == col))
619     {
620       image[((x - WINSTARTX) / d) + (focusx * 16)]
621 	[((y - (WINSTARTY)) / d) + (focusy * 16) + yy] = colour;
622       yy--;
623     }
624   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
625   draw_swin ();
626   draw_big_image ();
627   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
628   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
629 }
630 
631 
632 void
fill_colourzoom(int x,int y)633 fill_colourzoom (int x, int y)
634 {
635   int xx, yy;
636   xx = (x - WINSTARTX) / 16;
637   xx = WINSTARTX + (xx * 16);
638   yy = (y - WINSTARTY) / 16;
639   yy = WINSTARTY + (yy * 16);
640   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
641   Fgl_fillbox (xx, yy, 15, 15, colour);
642   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
643   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
644 
645   Fgl_setpixel (((SWINSTARTX + 8) - imagesize * 8) + ((x - WINSTARTX) / 16)
646 		+ (focusx * 16)
647 		,((SWINSTARTY + 8) - imagesize * 8) + ((y - (WINSTARTY)) / 16) + (focusy * 16)
648 		,colour);
649   image[((x - WINSTARTX) / 16) + (focusx * 16)]
650     [((y - (WINSTARTY)) / 16) + (focusy * 16)] = colour;
651 }
652 
653 void
fill_colour1(int x,int y)654 fill_colour1 (int x, int y)
655 {
656   int xx, yy;
657   xx = (x - WINSTARTX) / 16;
658   xx = WINSTARTX + (xx * 16);
659   yy = (y - WINSTARTY) / 16;
660   yy = WINSTARTY + (yy * 16);
661   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
662   Fgl_fillbox (xx, yy, 16, 16, colour);
663   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
664   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
665 
666   Fgl_setpixel ((SWINSTARTX) + ((x - WINSTARTX) / 16)
667 		+ (focusx * 16)
668 		,(SWINSTARTY) + ((y - (WINSTARTY)) / 16) + (focusy * 16)
669 		,colour);
670   image[((x - WINSTARTX) / 16) + (focusx * 16)]
671     [((y - (WINSTARTY)) / 16) + (focusy * 16)] = colour;
672 }
673 
674 void
fill_colour2(int x,int y)675 fill_colour2 (int x, int y)
676 {
677   int xx, yy;
678   xx = (x - WINSTARTX) / 8;
679   xx = WINSTARTX + (xx * 8);
680   yy = (y - WINSTARTY) / 8;
681   yy = WINSTARTY + (yy * 8);
682   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
683   Fgl_fillbox (xx, yy, 8, 8, colour);
684   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
685   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
686 
687 
688   image[((x - WINSTARTX) / 8) + (focusx * 16)]
689     [((y - (WINSTARTY)) / 8) + (focusy * 16)] = colour;
690   draw_swin ();
691 }
692 
693 void
fill_colour3(int x,int y)694 fill_colour3 (int x, int y)
695 {
696   int xx, yy;
697   xx = (x - WINSTARTX) / 5;
698   if (xx > 47)
699     return;
700   xx = WINSTARTX + (xx * 5);
701   yy = (y - WINSTARTY) / 5;
702   if (yy > 47)
703     return;
704   yy = WINSTARTY + (yy * 5);
705   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
706   Fgl_fillbox (xx, yy, 5, 5, colour);
707   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
708   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
709 
710   Fgl_setpixel ((SWINSTARTX - 16) + ((x - WINSTARTX) / 5)
711 		+ (focusx * 5)
712 		,(SWINSTARTY - 16) + ((y - (WINSTARTY)) / 5) + (focusy * 5)
713 		,colour);
714   image[((x - WINSTARTX) / 5) + (focusx * 5)]
715     [((y - (WINSTARTY)) / 5) + (focusy * 5)] = colour;
716 }
717 
718 void
fill_colour4(int x,int y)719 fill_colour4 (int x, int y)
720 {
721   int xx, yy;
722   xx = (x - WINSTARTX) / 4;
723   xx = WINSTARTX + (xx * 4);
724   yy = (y - WINSTARTY) / 4;
725   yy = WINSTARTY + (yy * 4);
726   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
727   Fgl_fillbox (xx, yy, 4, 4, colour);
728   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
729   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
730 
731   Fgl_setpixel ((SWINSTARTX - 24) + ((x - WINSTARTX) / 4)
732 		+ (focusx * 4)
733 		,(SWINSTARTY - 24) + ((y - (WINSTARTY)) / 4) + (focusy * 4)
734 		,colour);
735   image[((x - WINSTARTX) / 4) + (focusx * 4)]
736     [((y - (WINSTARTY)) / 4) + (focusy * 4)] = colour;
737 }
738 
739 #ifdef LC_X11
740 /* fixed the problem with xpicedit by checking if the colormap is writable*/
741 void
setcustompalette(void)742 setcustompalette (void)
743 {
744   char s[100], cpf[100];
745   int i, n, r, g, b, me = 0, flag[256];
746   long unsigned int plane_masks[3];
747   FILE *inf;
748   XColor pal[256];
749   int is_writeable;
750 
751   /* See if the colormap is writable.
752    * Only PseudoColor, GrayScale and DirectColor are writable TrueColor is not.
753    */
754   Visual *visual = DefaultVisual (display.dpy, display.screen);
755   int visual_class = visual->class;
756   is_writeable = (visual_class == PseudoColor || visual_class == GrayScale || visual_class == DirectColor);
757 
758   printf ("In setcustompalette\n");
759   display.cmap = XDefaultColormap (display.dpy, display.screen);
760   if (is_writeable) {
761     if (XAllocColorCells (display.dpy, display.cmap, 0
762 			,plane_masks, 0, colour_table, 256) == 0)
763     {
764     me = (*DefaultVisual (display.dpy, display.screen)).map_entries;
765     printf ("DefaultVisual id=%d bp-rgb=%d map-entries=%d\n"
766 	      ,(*DefaultVisual (display.dpy, display.screen)).visualid
767 	      ,(*DefaultVisual (display.dpy, display.screen)).bits_per_rgb
768 	      ,(*DefaultVisual (display.dpy, display.screen)).map_entries);
769     display.cmap = XCreateColormap (display.dpy, display.win
770 				,DefaultVisual (display.dpy, display.screen)
771 				,AllocNone);
772       if (me == 256 && XAllocColorCells (display.dpy, display.cmap, 0
773 				   ,plane_masks, 0, colour_table, 256) != 0)
774 	printf ("Allocated 256 cells\n");
775       else
776 	for (i = 0; i < 256; i++)
777 	  colour_table[i] = i;
778       }
779     if (!display.cmap)
780       HandleError ("No default colour map", FATAL);
781   }
782   printf ("Got past create colourmap\n");
783 
784   for (i = 0; i < 256; i++)
785     flag[i] = 0;
786 
787   strcpy (cpf, LIBDIR);
788   strcat (cpf, COLOUR_PAL_FILE);
789   if ((inf = fopen (cpf, "r")) == 0)
790     HandleError ("Can't find the colour pallet file", FATAL);
791   while (feof (inf) == 0)
792     {
793       fgets (s, 99, inf);
794       if (sscanf (s, "%d %d %d %d", &n, &r, &g, &b) == 4)
795 	{
796 	  pal[n].red = r << 10;
797 	  pal[n].green = g << 10;
798 	  pal[n].blue = b << 10;
799 	  pal[n].flags = DoRed | DoGreen | DoBlue;
800 	  if (is_writeable) {
801 	    pal[n].pixel = colour_table[n];
802 	  }
803 	  else
804 	    {
805 	      if (XAllocColor (display.dpy
806 			       ,display.cmap, &(pal[n])) == 0)
807 		HandleError ("alloc colour failed"
808 			     ,FATAL);
809 	      colour_table[n] = pal[n].pixel;
810 	      XSetForeground (display.dpy
811 			      ,display.pixcolour_gc[n]
812 			      ,colour_table[n]);
813 	    }
814 	  flag[n] = 1;
815 	}
816     }
817   fclose (inf);
818 
819   for (i = 0; i < 256; i++)
820     if (flag[i] == 0)
821       {
822 	printf ("Colour %d not loaded\n", i);
823 	do_error ("Can't continue");
824       }
825   if(is_writeable) {
826     XStoreColors (display.dpy, display.cmap, pal, 256);
827     XFlush(display.dpy);
828   }
829   XSetWindowColormap (display.dpy, display.win, display.cmap);
830 }
831 
832 #else
833 void
setcustompalette(void)834 setcustompalette (void)
835 {
836   char s[100], cpf[100];
837   int i, n, r, g, b, flag[256];
838   FILE *inf;
839   Palette pal;
840   for (i = 0; i < 256; i++)
841     flag[i] = 0;
842   strcpy (cpf, LIBDIR);
843   strcat (cpf, COLOUR_PAL_FILE);
844   if ((inf = fopen (cpf, "r")) == 0)
845     do_error ("Can't find the colour pallet file :(");
846 
847   while (feof (inf) == 0)
848     {
849       fgets (s, 99, inf);
850       if (sscanf (s, "%d %d %d %d", &n, &r, &g, &b) == 4)
851 	{
852 	  pal.color[n].red = r;
853 	  pal.color[n].green = g;
854 	  pal.color[n].blue = b;
855 	  flag[n] = 1;
856 	}
857     }
858   fclose (inf);
859   for (i = 0; i < 256; i++)
860     if (flag[i] == 0)
861       {
862 	printf ("Colour %d not loaded\n", i);
863 	do_error ("Can't continue");
864       }
865   gl_setpalette (&pal);
866 }
867 #endif
868 
869 
870 void
initfont()871 initfont ()
872 {
873 #ifdef LC_X11
874   char cpf[100];
875   int i;
876   FILE *finf;
877   strcpy (cpf, LIBDIR);
878   strcat (cpf, FONTFILE);
879   if ((finf = fopen (cpf, "r")) == 0)
880     HandleError ("Can't open the font file", FATAL);
881   for (i = 0; i < 256 * 8; i++)
882     myfont[i] = fgetc (finf);
883   fclose (finf);
884 #else
885   void *font;
886   font = malloc (256 * 8 * 8 * BYTESPERPIXEL);
887   gl_expandfont (8, 8, white (24), gl_font8x8, font);
888   gl_setfont (8, 8, font);
889 #endif
890 }
891 
892 
893 void
save_image(void)894 save_image (void)
895 {
896   int x, y;
897   FILE *outf;
898   if ((outf = fopen (imagefile, "wb")) == NULL)
899     {
900       printf ("Can't write image file %s \n", imagefile);
901       return;
902     }
903   for (y = 0; y < 16 * imagesize; y++)
904     for (x = 0; x < 16 * imagesize; x++)
905       putc (image[x][y], outf);
906   fclose (outf);
907 }
908 
909 void
load_image(void)910 load_image (void)
911 {
912   int x, y, l;
913   FILE *inf;
914   if ((inf = fopen (imagefile, "rb")) == NULL)
915     return;
916   fseek (inf, 0L, SEEK_END);
917   l = ftell (inf);
918   fseek (inf, 0L, SEEK_SET);
919   if (l == 256)
920     newimagesize = 1;
921   else if (l == 1024)
922     newimagesize = 2;
923   else if (l == 2304)
924     newimagesize = 3;
925   else if (l == 4096)
926     newimagesize = 4;
927   else
928     {
929       printf ("Can't load file %s, maybe wrong size %d\n"
930 	      ,imagefile, l);
931       exit (1);
932     }
933   for (y = 0; y < 16 * newimagesize; y++)
934     for (x = 0; x < 16 * newimagesize; x++)
935       image[x][y] = fgetc (inf);
936   fclose (inf);
937 }
938 
939 
940 
941 
942 void
setup(void)943 setup (void)
944 {
945   char s[50];
946   short x, y;
947   Fgl_hline (WINSTARTX, WINSTARTY - 1, WINSTARTX + 256, 255);
948   Fgl_hline (WINSTARTX, WINSTARTY + 256, WINSTARTX + 256, 255);
949   Fgl_line (WINSTARTX - 1, WINSTARTY, WINSTARTX - 1, WINSTARTY + 256, 255);
950   Fgl_line (WINSTARTX + 256, WINSTARTY, WINSTARTX + 256, WINSTARTY + 256, 255);
951   for (y = 0; y < 16; y++)
952     {
953       Fgl_hline (WINSTARTX, WINSTARTY + y * 16 + 15, WINSTARTX + 255, 253);
954       Fgl_line (WINSTARTX + y * 16 + 15, WINSTARTY, WINSTARTX + y * 16 + 15
955 		,WINSTARTY + 255, 253);
956       for (x = 0; x < 16; x++)
957 	{
958 	  Fgl_fillbox (x * 40, y * 14, 38, 12, y * 16 + x);
959 	  sprintf (s, "%d", y * 16 + x);
960 	  Fgl_write (x * 40, y * 14, s);
961 	}
962     }
963   change_imagesize (1);
964   Fgl_fillbox (SIZEBUTTONX1x1, SIZEBUTTONY1x1, SIZEBUTTONSIZEX - 1
965 	       ,SIZEBUTTONSIZEY - 1, 240);
966   Fgl_write (SIZEBUTTONX1x1 + 3, SIZEBUTTONY1x1 + 3, "1x1");
967   Fgl_fillbox (SIZEBUTTONX2x2, SIZEBUTTONY2x2, SIZEBUTTONSIZEX - 1
968 	       ,SIZEBUTTONSIZEY - 1, 240);
969   Fgl_write (SIZEBUTTONX2x2 + 3, SIZEBUTTONY2x2 + 3, "2x2");
970   Fgl_fillbox (SIZEBUTTONX3x3, SIZEBUTTONY3x3, SIZEBUTTONSIZEX - 1
971 	       ,SIZEBUTTONSIZEY - 1, 240);
972   Fgl_write (SIZEBUTTONX3x3 + 3, SIZEBUTTONY3x3 + 3, "3x3");
973   Fgl_fillbox (SIZEBUTTONX4x4, SIZEBUTTONY4x4, SIZEBUTTONSIZEX - 1
974 	       ,SIZEBUTTONSIZEY - 1, 240);
975   Fgl_write (SIZEBUTTONX4x4 + 3, SIZEBUTTONY4x4 + 3, "4x4");
976   /* this is the focus change window */
977   Fgl_fillbox (FOCUSWINX - 1, FOCUSWINY - 1, FOCUSWINSIZE + 1, FOCUSWINSIZE + 1, 240);
978   draw_focus ();
979   /* this is the zoom button */
980   Fgl_fillbox (ZOOMBUTX, ZOOMBUTY, ZOOMBUTSIZEX, ZOOMBUTSIZEY, 240);
981   Fgl_write (ZOOMBUTX + 3, ZOOMBUTY + 3, "ZOOM");
982   /* this is the half zoom button */
983   Fgl_fillbox (HALFBUTX, HALFBUTY, HALFBUTSIZEX, HALFBUTSIZEY, 240);
984   Fgl_write (HALFBUTX + 3, HALFBUTY + 3, "HALF");
985   /* this is the normal zoom button */
986   Fgl_fillbox (NORMBUTX, NORMBUTY, NORMBUTSIZEX, NORMBUTSIZEY, 240);
987   Fgl_write (NORMBUTX + 3, NORMBUTY + 3, "NORM");
988   /* this is the dot button */
989   Fgl_fillbox (DOTBUTX, DOTBUTY, DOTBUTSIZEX, DOTBUTSIZEY, 240);
990   Fgl_write (DOTBUTX + 3, DOTBUTY + 3, "DOT");
991   /* this id the hline button */
992   Fgl_fillbox (HFILBUTX, HFILBUTY, HFILBUTSIZEX, HFILBUTSIZEY, 240);
993   Fgl_write (HFILBUTX + 3, HFILBUTY + 3, "HFIL");
994   /* this is the vline button */
995   Fgl_fillbox (VFILBUTX, VFILBUTY, VFILBUTSIZEX, VFILBUTSIZEY, 240);
996   Fgl_write (VFILBUTX + 3, VFILBUTY + 3, "VFIL");
997   Fgl_write (0, 480 - 9, imagefile);
998   Fgl_write (0, 423, " q - to save and quit...");
999   Fgl_write (0, 433, "   - (then move the mouse)");
1000   Fgl_write (0, 448, "^C - to quit without saving");
1001 }
1002 
1003 void
draw_focus(void)1004 draw_focus (void)
1005 {
1006   int x, y;
1007   hide_mouse ();
1008   for (y = 0; y < 4; y++)
1009     for (x = 0; x < 4; x++)
1010       {
1011 	if (x == focusx && y == focusy)
1012 	  Fgl_fillbox (FOCUSWINX + (FOCUSWINSIZE / 4) * focusx
1013 		       ,FOCUSWINY + (FOCUSWINSIZE / 4) * focusy
1014 		       ,(FOCUSWINSIZE / 4) - 1
1015 		       ,(FOCUSWINSIZE / 4) - 1, FOCUSWINCOLOUR);
1016 	else if (zoom_flag == 2)
1017 	  {
1018 	    if ((x == focusx + 1 && y == focusy)
1019 		|| (x == focusx && y == focusy + 1)
1020 		|| (x == focusx + 1 && y == focusy + 1))
1021 	      Fgl_fillbox (FOCUSWINX + (FOCUSWINSIZE / 4) * x
1022 			   ,FOCUSWINY + (FOCUSWINSIZE / 4) * y
1023 			   ,(FOCUSWINSIZE / 4) - 1
1024 			   ,(FOCUSWINSIZE / 4) - 1, FOCUSWINCOLOUR);
1025 	  }
1026 	else if (x < imagesize && y < imagesize)
1027 	  Fgl_fillbox (FOCUSWINX + (FOCUSWINSIZE / 4) * x
1028 		       ,FOCUSWINY + (FOCUSWINSIZE / 4) * y
1029 		       ,(FOCUSWINSIZE / 4) - 1
1030 		       ,(FOCUSWINSIZE / 4) - 1, UNFOCUSWINCOLOUR);
1031 	else
1032 	  Fgl_fillbox (FOCUSWINX + (FOCUSWINSIZE / 4) * x
1033 		       ,FOCUSWINY + (FOCUSWINSIZE / 4) * y
1034 		       ,(FOCUSWINSIZE / 4) - 1
1035 		       ,(FOCUSWINSIZE / 4) - 1, XFOCUSWINCOLOUR);
1036       }
1037   reshow_mouse ();
1038 }
1039 
1040 void
draw_swin(void)1041 draw_swin (void)
1042 {
1043   int x, y;
1044   Fgl_fillbox ((SWINSTARTX + 7) - 4 * 8, (SWINSTARTY + 7) - 4 * 8
1045 	       ,16 * 4 + 2, 16 * 4 + 2, 0);
1046   Fgl_hline ((SWINSTARTX + 7) - imagesize * 8, (SWINSTARTY + 7) - imagesize * 8
1047 	     ,((SWINSTARTX + 8) - imagesize * 8) + 16 * imagesize, 235);
1048   Fgl_hline ((SWINSTARTX + 7) - imagesize * 8
1049 	     ,((SWINSTARTY + 8) - imagesize * 8) + 16 * imagesize
1050 	     ,((SWINSTARTX + 8) - imagesize * 8) + 16 * imagesize, 235);
1051   Fgl_line ((SWINSTARTX + 7) - imagesize * 8, (SWINSTARTY + 7) - imagesize * 8
1052 	    ,(SWINSTARTX + 7) - imagesize * 8
1053 	    ,((SWINSTARTY + 8) - imagesize * 8) + 16 * imagesize, 235);
1054   Fgl_line (((SWINSTARTX + 8) - imagesize * 8) + 16 * imagesize
1055 	    ,(SWINSTARTY + 7) - imagesize * 8
1056 	    ,((SWINSTARTX + 8) - imagesize * 8) + 16 * imagesize
1057 	    ,((SWINSTARTY + 8) - imagesize * 8) + 16 * imagesize, 235);
1058   for (y = 0; y < 16 * imagesize; y++)
1059     for (x = 0; x < 16 * imagesize; x++)
1060       Fgl_setpixel (((SWINSTARTX + 8) - imagesize * 8) + x
1061 		    ,((SWINSTARTY + 8) - imagesize * 8) + y, image[x][y]);
1062 }
1063 
1064 void
draw_big_image(void)1065 draw_big_image (void)
1066 {
1067   if (zoom_flag == 1)
1068     draw_big_imagezoom ();
1069   else if (zoom_flag == 2)
1070     draw_big_image2 ();
1071   else if (imagesize == 1)
1072     draw_big_image1 ();
1073   else if (imagesize == 2)
1074     draw_big_image2 ();
1075   else if (imagesize == 3)
1076     draw_big_image3 ();
1077   else if (imagesize == 4)
1078     draw_big_image4 ();
1079 }
1080 
1081 void
draw_big_imagezoom(void)1082 draw_big_imagezoom (void)
1083 {
1084   int x, y;
1085   for (y = 0; y < 16; y++)
1086     for (x = 0; x < 16; x++)
1087       Fgl_fillbox (WINSTARTX + x * 16, (WINSTARTY) + y * 16, 15, 15
1088 		   ,image[x + focusx * 16][y + focusy * 16]);
1089 }
1090 
1091 void
draw_big_image1(void)1092 draw_big_image1 (void)
1093 {
1094   int x, y;
1095   for (y = 0; y < 16; y++)
1096     for (x = 0; x < 16; x++)
1097       Fgl_fillbox (WINSTARTX + x * 16, (WINSTARTY) + y * 16, 16, 16
1098 		   ,image[x + focusx * 16][y + focusy * 16]);
1099 }
1100 
1101 void
draw_big_image2(void)1102 draw_big_image2 (void)
1103 {
1104   int x, y;
1105   for (y = 0; y < 32; y++)
1106     for (x = 0; x < 32; x++)
1107       Fgl_fillbox (WINSTARTX + x * 8, (WINSTARTY) + y * 8, 8, 8
1108 		   ,image[x + focusx * 16][y + focusy * 16]);
1109 }
1110 
1111 void
draw_big_image3(void)1112 draw_big_image3 (void)
1113 {
1114   int x, y;
1115   for (y = 0; y < 48; y++)
1116     for (x = 0; x < 48; x++)
1117       Fgl_fillbox (WINSTARTX + x * 5, (WINSTARTY) + y * 5, 5, 5
1118 		   ,image[x + focusx * 5][y + focusy * 5]);
1119 }
1120 
1121 void
draw_big_image4(void)1122 draw_big_image4 (void)
1123 {
1124   int x, y;
1125   for (y = 0; y < 64; y++)
1126     for (x = 0; x < 64; x++)
1127       Fgl_fillbox (WINSTARTX + x * 4, (WINSTARTY) + y * 4, 4, 4
1128 		   ,image[x + focusx * 4][y + focusy * 4]);
1129 }
1130 
1131 void
change_imagesize(int n)1132 change_imagesize (int n)
1133 {
1134   char s[50];
1135   imagesize = n;
1136   focus = 0;
1137   focusx = 0;
1138   focusy = 0;
1139   draw_swin ();
1140   draw_focus ();
1141   sprintf (s, "Image size %dx%d", n, n);
1142   Fgl_write (0, 480 - 20, s);
1143 
1144 }
1145 
1146 void
change_focus(int x,int y)1147 change_focus (int x, int y)
1148 {
1149   if (x >= imagesize || y >= imagesize)
1150     return;
1151   focusx = x;
1152   focusy = y;
1153   draw_focus ();
1154   draw_big_image ();
1155 }
1156 
1157 void
hide_mouse(void)1158 hide_mouse (void)
1159 {
1160   Fgl_putbox (mox, moy, 8, 8, under_mouse_pointer);
1161 }
1162 
1163 void
reshow_mouse(void)1164 reshow_mouse (void)
1165 {
1166   Fgl_getbox (mx, my, 8, 8, under_mouse_pointer);
1167   Fgl_putbox (mx, my, 8, 8, mouse_pointer);
1168 }
1169 
1170 
1171 #ifdef LC_X11
1172 
1173 void
parse_args(int argc,char ** argv,char ** geometry)1174 parse_args (int argc, char **argv, char **geometry)
1175 {
1176   /* These variables are used to analyze the command line parameters. */
1177   int option;
1178   extern int optind;
1179   extern char *optarg;
1180 
1181   int bits;			/* Used to set window geometry. */
1182 
1183 
1184   /* Check the command line. */
1185   while ((option = getopt (argc, argv, "vbg:")) != EOF)
1186     {
1187       switch (option)
1188 	{
1189 	case 'v':
1190 	  verbose = TRUE;
1191 	  break;
1192 	case 'g':
1193 	  *geometry = optarg;
1194 	  break;
1195 	case 'b':
1196 	  borderx = 0;
1197 	  bordery = 0;
1198 	  break;
1199 	}
1200     }
1201   if (verbose)
1202     printf ("Version %s\n", VERSION);
1203   if (!(display.dpy = XOpenDisplay (display.dname)))
1204     {
1205       HandleError ("Cannot open display.\n", FATAL);
1206       exit (-1);
1207     }
1208 /* Record the screen number and root window. */
1209   display.screen = DefaultScreen (display.dpy);
1210   display.root = RootWindow (display.dpy, display.screen);
1211 
1212   winW = WINWIDTH + borderx * 2;
1213   winH = WINHEIGHT + bordery * 2;
1214   winX = (DisplayWidth (display.dpy, display.screen)
1215 	  - winW) / 2;
1216   winY = (DisplayHeight (display.dpy, display.screen)
1217 	  - winH) / 2;
1218   if (*geometry != NULL)
1219     XParseGeometry (*geometry, &winX, &winY, &winW, &winH);
1220 }
1221 
1222 void
Create_Window(char * geometry)1223 Create_Window (char *geometry)
1224 {
1225   short q;
1226   XSetWindowAttributes xswa;
1227   XSizeHints sizehint;
1228   XWMHints wmhints;
1229   unsigned char wname[256];	/* Window Name */
1230 
1231   xswa.event_mask = 0;
1232   xswa.background_pixel = display.bg;
1233   printf ("DefaultVisual id=%d bp-rgb=%d map-entries=%d\n"
1234 	  ,(*DefaultVisual (display.dpy, display.screen)).visualid
1235 	  ,(*DefaultVisual (display.dpy, display.screen)).bits_per_rgb
1236 	  ,(*DefaultVisual (display.dpy, display.screen)).map_entries);
1237   display.win = XCreateWindow (display.dpy, display.root,
1238 			       winX, winY,
1239 			       winW, winH, 0,
1240 			       DefaultDepth (display.dpy, display.screen),
1241 		   InputOutput, DefaultVisual (display.dpy, display.screen),
1242   /*      PseudoColor,  */
1243 			       CWEventMask | CWBackPixel, &xswa);
1244 
1245   sizehint.x = winX;
1246   sizehint.y = winY;
1247   sizehint.width = winW;
1248   sizehint.height = winH;
1249   sizehint.min_width = winW;
1250   sizehint.min_height = winH;
1251   if (geometry != NULL)
1252     sizehint.flags = USPosition | USSize | PMinSize;
1253   else
1254     sizehint.flags = PPosition | PSize | PMinSize;
1255   XSetNormalHints (display.dpy, display.win, &sizehint);
1256 
1257   display.protocol_atom = XInternAtom (display.dpy, "WM_PROTOCOLS",
1258 				       False);
1259   display.kill_atom = XInternAtom (display.dpy, "WM_DELETE_WINDOW",
1260 				   False);
1261 
1262   /* Title */
1263   sprintf ((char *) wname, "xpicedit, Version %s, by IJ Peters",
1264 	   VERSION);
1265   XChangeProperty (display.dpy, display.win,
1266 		   XA_WM_NAME, XA_STRING, 8, PropModeReplace, wname,
1267 		   strlen ((char *) wname));
1268 
1269   /* Window Manager Hints (This is supposed to make input work.) */
1270   wmhints.flags = InputHint;
1271   wmhints.input = True;
1272   XSetWMHints (display.dpy, display.win, &wmhints);
1273   XMapWindow (display.dpy, display.win);
1274   XSelectInput (display.dpy, display.win,
1275 		KeyPressMask | ButtonPressMask | ButtonReleaseMask
1276 		| ExposureMask | StructureNotifyMask);
1277   for (q = 0; q < 256; q++)
1278     {
1279       display.pixcolour_gc[q] = XCreateGC (display.dpy
1280 					   ,display.win, 0, NULL);
1281       XSetForeground (display.dpy, display.pixcolour_gc[q], q);
1282       XSetBackground (display.dpy, display.pixcolour_gc[q]
1283 		      ,display.bg);
1284     }
1285 }
1286 
1287 void
HandleError(char * description,int degree)1288 HandleError (char *description, int degree)
1289 {
1290   fprintf (stderr, "An error has occurred.  The description is below... \n");
1291   fprintf (stderr, "%s\n", description);
1292 
1293   if (degree == FATAL)
1294     {
1295       fprintf (stderr, "Program aborting...\n");
1296       exit (-1);
1297     }
1298 }
1299 
1300 void
Fgl_setpixel(int x,int y,int col)1301 Fgl_setpixel (int x, int y, int col)
1302 {
1303   int i;
1304   col &= 0xff;
1305   i = (y + bordery) * (640 + BORDERX) + x + borderx;
1306   if ((int) *(pixmap + i) != col)
1307     {
1308       *(pixmap + i) = (unsigned char) col;
1309       XDrawPoint (display.dpy, display.win
1310 		  ,display.pixcolour_gc[col], x + borderx, y + bordery);
1311     }
1312 }
1313 
1314 int
Fgl_getpixel(int x,int y)1315 Fgl_getpixel (int x, int y)
1316 {
1317   return (*(pixmap + (y + bordery) * (640 + BORDERX) + x + borderx));
1318 }
1319 
1320 void
Fgl_hline(int x1,int y1,int x2,int col)1321 Fgl_hline (int x1, int y1, int x2, int col)
1322 {
1323   int x, i;
1324   col &= 0xff;
1325   i = (y1 + bordery) * (640 + BORDERX);
1326   for (x = x1 + borderx; x < x2 + borderx; x++)
1327     *(pixmap + i + x) = col;
1328   XDrawLine (display.dpy, display.win
1329 	     ,display.pixcolour_gc[col], x1 + borderx
1330 	     ,y1 + bordery, x2 + borderx, y1 + bordery);
1331 }
1332 
1333 void
Fgl_line(int x1,int y1,int dummy,int y2,int col)1334 Fgl_line (int x1, int y1, int dummy, int y2, int col)
1335      /* vertical lines only. */
1336 {
1337   int y, i;
1338   col &= 0xff;
1339   for (y = y1 + bordery; y < y2 + bordery; y++)
1340     *(pixmap + x1 + borderx + (y * (640 + BORDERX))) = col;
1341   XDrawLine (display.dpy, display.win
1342 	     ,display.pixcolour_gc[col], x1 + borderx
1343 	     ,y1 + bordery, x1 + borderx, y2 + bordery);
1344 }
1345 
1346 void
Fgl_write(int x,int y,char * s)1347 Fgl_write (int x, int y, char *s)
1348 {
1349   int i;
1350   for (i = 0; i < strlen (s); i++)
1351     my_x_putchar (x + i * 8, y, s[i]);
1352 }
1353 
1354 void
my_x_putchar(int xx,int yy,int c)1355 my_x_putchar (int xx, int yy, int c)
1356 {
1357   int x, y, b;
1358   for (y = 0; y < 8; y++)
1359     {
1360       b = myfont[c * 8 + y];
1361       for (x = 0; x < 8; x++)
1362 	{
1363 	  if ((b & 0x80) == 0)
1364 	    Fgl_setpixel (xx + x, yy + y, text_bg);
1365 	  else
1366 	    Fgl_setpixel (xx + x, yy + y, text_fg);
1367 	  b = b << 1;
1368 	}
1369     }
1370 }
1371 
1372 
1373 void
Fgl_fillbox(int x1,int y1,int w,int h,int col)1374 Fgl_fillbox (int x1, int y1, int w, int h, int col)
1375 {
1376   int x, y;
1377   col &= 0xff;
1378   for (y = y1 + bordery; y < y1 + h + bordery; y++)
1379     {
1380       for (x = x1 + borderx; x < x1 + w + borderx; x++)
1381 	*(pixmap + y * (640 + BORDERX) + x) = col;
1382       XDrawLine (display.dpy, display.win, display.pixcolour_gc[col]
1383 		 ,x1 + borderx, y, x1 + w - 1 + borderx, y);
1384     }
1385 }
1386 
1387 void
Fgl_putbox(int x1,int y1,int x2,int y2,unsigned char * buf)1388 Fgl_putbox (int x1, int y1, int x2, int y2, unsigned char *buf)
1389 {
1390 }
1391 
1392 void
Fgl_getbox(int x1,int y1,int x2,int y2,unsigned char * buf)1393 Fgl_getbox (int x1, int y1, int x2, int y2, unsigned char *buf)
1394 {
1395 }
1396 
1397 void
HandleEvent(XEvent * event)1398 HandleEvent (XEvent * event)
1399 {
1400   switch (event->type)
1401     {
1402     case (KeyPress):
1403       {
1404 	XKeyEvent *key_event = (XKeyEvent *) event;
1405 	char buf[128];
1406 	KeySym ks;
1407 	XComposeStatus status;
1408 	XLookupString (key_event, buf, 128, &ks, &status);
1409 	if (buf[0] == 'q' || buf[0] == 'Q')
1410 	  stopflag = TRUE;
1411 	else if (buf[0] == 'r' || buf[0] == 'R')
1412 	  refresh_screen (0, 0, 640, 480);
1413       }
1414       break;
1415 
1416     case (ButtonPress):
1417       {
1418 	XButtonEvent *ev = (XButtonEvent *) event;
1419 	mouse_button = ev->button;
1420       }
1421       break;
1422 
1423     case (ButtonRelease):
1424       {
1425 	mouse_button = 0;
1426       }
1427       break;
1428 
1429     case (Expose):
1430       {
1431 	XExposeEvent *ev = (XExposeEvent *) event;
1432 	refresh_screen (ev->x, ev->y
1433 			,ev->x + ev->width, ev->y + ev->height);
1434       }
1435       break;
1436 
1437     default:
1438       printf ("Unknown event %d\n", event->type);
1439     }
1440 }
1441 
1442 void
refresh_screen(int x1,int y1,int x2,int y2)1443 refresh_screen (int x1, int y1, int x2, int y2)		/* bounds of refresh area */
1444 {
1445   int x, y;
1446   for (y = y1; y < y2; y++)
1447     for (x = x1; x < x2; x++)
1448       XDrawPoint (display.dpy, display.win
1449 		  ,display.pixcolour_gc[*(pixmap
1450 				  + x + y * (640 + BORDERX)) & 0xff], x, y);
1451 }
1452 
1453 #endif
1454 
1455 void
lincityrc(void)1456 lincityrc (void)
1457 {
1458   char s[256], s1[256];
1459   int i;
1460   FILE *rc;
1461 
1462   strcpy (s, getenv ("HOME"));
1463   strcat (s, "/");
1464   strcat (s, ".lincityrc");
1465   if ((rc = fopen (s, "r")) == 0)
1466     {
1467       do
1468 	{
1469 	  strcpy (s1, "cat ");
1470 	  strcat (s1, LIBDIR);
1471 	  strcat (s1, "/messages/mousetype.mes");
1472 	  system (s1);
1473 	  i = getchar ();
1474 	}
1475       while (i < '0' || i > '6');
1476       if ((rc = fopen (s, "w")) == 0)
1477 	{
1478 	  printf ("Can't open %s for writing, can't continue \n ", s);
1479 	  exit (1);
1480 	}
1481       fprintf (rc, "mouse=%d\n", i - '0');
1482       fclose (rc);
1483       chown (s, getuid (), getgid ());
1484       if ((rc = fopen (s, "r")) == 0)
1485 	{
1486 	  printf ("What!! can't open %s for reading after writing???\n", s);
1487 	  exit (1);
1488 	}
1489     }
1490   while (feof (rc) == 0)
1491     {
1492       fgets (s, 99, rc);
1493       if (sscanf (s, "mouse=%d", &i) != 0)
1494 	lc_mouse_type = i;
1495     }
1496   fclose (rc);
1497 }
1498