1 /*
2  * Sun clock definitions.
3  */
4 
5 #define XK_MISCELLANY
6 #define XK_LATIN1
7 
8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h>
10 #include <X11/keysymdef.h>
11 #include <X11/keysym.h>
12 #include <X11/xpm.h>
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <math.h>
18 #include <time.h>
19 #include "version.h"
20 
21 #define EDITORCOMMAND EMXBINDIR"/emx -edit 0 -fn 9x15"  /* Default text editor */
22 #define	FAILFONT	"fixed"
23 
24 /* num of bitmaps to accomodate 1 mark and 2 spatial objets (Sun, Moon) */
25 #define SPECIALBITMAPS 3
26 #define CITYBITMAPS 5
27 
28 #define MENU_WIDTH 38
29 #define SEL_WIDTH 20
30 #define SEL_HEIGHT 10
31 #define MAXSHORT 32767
32 #define TOPTITLEBARHEIGHT 40   /* usual height of top title bar for KDE
33 				  can put 0 instead if you don't use that! */
34 
35 #define abs(x) ((x) < 0 ? (-(x)) : x)			  /* Absolute value */
36 #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))	  /* Extract sign */
37 #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle	  */
38 #define torad(d) ((d) * (PI / 180.0))                     /* Deg->Rad     */
39 #define todeg(d) ((d) * (180.0 / PI))                     /* Rad->Deg     */
40 #define dsin(x) (sin(torad((x))))                         /* Sin from deg */
41 #define dcos(x) (cos(torad((x))))                         /* Cos from deg */
42 
43 
44 #define PI 3.14159265358979323846
45 #define ZFACT 1.2
46 
47 #define COLORLENGTH 48             /* maximum length of color names */
48 
49 #define TERMINC  100		   /* Circle segments for terminator */
50 
51 #define PROJINT  (60 * 10)	   /* Frequency of seasonal recalculation
52 				      in seconds. */
53 
54 #define EARTHRADIUS_KM  6378.160
55 #define EARTHRADIUS_ML  3963.202
56 #define SUN_APPRADIUS   0.266      /* Sun apparent radius, in degrees */
57 #define ATM_REFRACTION  0.100      /* Atmospheric refraction, in degrees */
58 #define ATM_DIFFUSION   3.0        /* Atmospheric diffusion, in degrees */
59 
60 #define COORDINATES 'c'
61 #define DISTANCES 'd'
62 #define EXTENSION 'e'
63 #define LEGALTIME 'l'
64 #define SOLARTIME 's'
65 
66 #define TIMECOUNT 25
67 #define TIMESTEP  10000
68 
69 enum {MONOCHROME=0, FEWCOLORS, MANYCOLORS, FULLCOLORS};
70 
71 enum {RESET=0, REMAP, REATTRIB, DESTROY, ICONIFY, DEICONIFY};
72 
73 enum {RANDOM=0, FIXED, CENTER, NW, NE, SW, SE};
74 
75 enum {READSYSRC = 0, READUSERRC, PARSECMDLINE, RUNNING, RUNTIMEOPTION,
76       IMAGERECYCLE, FAILEDOPTION};
77 
78 enum {NULL_INPUT = 0, OPTION_INPUT, URBAN_INPUT};
79 
80 enum {
81   CLOCKBGCOLOR=0, MAPBGCOLOR, MENUBGCOLOR, CLOCKSTRIPBGCOLOR, MAPSTRIPBGCOLOR,
82   ZOOMBGCOLOR, OPTIONBGCOLOR, BUTTONCOLOR, STARCOLOR,
83   CLOCKFGCOLOR, MAPFGCOLOR, MENUFGCOLOR,
84   BUTTONFGCOLOR1, BUTTONFGCOLOR2, BUTTONFGCOLOR3, BUTTONFGCOLOR4,
85   CLOCKSTRIPFGCOLOR, MAPSTRIPFGCOLOR,
86   ZOOMFGCOLOR, OPTIONFGCOLOR, WEAKCOLOR, ROOTCOLOR,
87   CARETCOLOR, CHANGECOLOR, CHOICECOLOR, DIRCOLOR, IMAGECOLOR, CITYNAMECOLOR,
88   CITYCOLOR0, CITYCOLOR1, CITYCOLOR2, MARKCOLOR1, MARKCOLOR2,
89   SUNCOLOR, MOONCOLOR, LINECOLOR, MERIDIANCOLOR, PARALLELCOLOR, TROPICCOLOR,
90   NUMPIXELS
91 };
92 
93 enum {
94   CLOCKSTRIPFONT, MAPSTRIPFONT, COORDFONT, CITYFONT, LABELFONT, MENUFONT, NUMFONTS
95 };
96 
97 /* Geometry structure */
98 
99 typedef struct Geometry {
100 	int	mask;
101 	int	x;
102 	int	y;
103 	unsigned int	width;
104 	unsigned int	height;
105         unsigned int    w_mini;
106         unsigned int    h_mini;
107 } Geometry;
108 
109 /* Behavioral flags */
110 typedef struct Flags {
111   /* Status values */
112         short colorlevel;               /* 0=mono 1=invert1 2=invert2 3=color*/
113         short fillmode;                 /* 0=coastlines 1=contour 2=landfill */
114         short vmfflags;                 /* flags for VMF format */
115         short dotted;                   /* use dotted lines ? */
116         short colorscale;               /* number of colors in shading */
117         short darkness;                 /* level of darkness in shading */
118         short map_mode;                 /* are we in C, D, E, L, S mode? */
119         short clock_mode;               /* clock mode */
120         short animate;                  /* start/stop animation */
121         short animperiod;               /* animation periodicity 0,1..5 sec */
122         short progress;                 /* special progress time ?*/
123         short shading;                  /* shading mode */
124         short dms;                      /* degree, minute, second mode */
125         short objectmode;               /* mode for showing Moon/Sun */
126         short objects;                  /* are Moon/Sun to be shown ? */
127         short citymode;                 /* cities mode */
128         short meridian;                 /* meridians mode */
129         short parallel;                 /* parallels/tropics mode */
130   /* Internal switches */
131         short mapped;                   /* is window mapped ? */
132         short update;                   /* update image (=-1 full update) */
133         short bottom;                   /* bottom strip to be cleaned */
134         short hours_shown;              /* hours in extension mode shown? */
135 } Flags;
136 
137 /* Zoom settings */
138 
139 typedef struct ZoomSettings {
140         double          fx;             /* zoom factor along width */
141         double          fy;             /* zoom factor along height */
142         double          fdx;            /* translation factor along width */
143         double          fdy;            /* translation factor along height */
144         double meridspacing;            /* meridian spacing (degrees) */
145         double paralspacing;            /* parallel spacing (degrees) */
146         int             mode;           /* zoom behaviour mode=0,1,2 */
147         int             width;          /* width of full extent zoomed area */
148         int             height;         /* height of full extent zoomed area */
149         int             dx;             /* translation along width */
150         int             dy;             /* translation along height */
151 } ZoomSettings;
152 
153 
154 /* Graphic Data */
155 
156 typedef struct GraphicData {
157         GC              wingc;          /* window gc */
158         GC              pixgc;          /* pixmap gc */
159         Pixel           pixel[NUMPIXELS];  /* list of pixels used */
160         int             precedence;     /* ordinal number of creation */
161         int             clockstrip;     /* height of strip for clock */
162         int             mapstrip;       /* height of strip for map */
163         int             menustrip;      /* height of strip for map */
164         int             charspace;      /* menu char spacing */
165         short           links;          /* how many other Windows linked ? */
166         short           usedcolors;     /* number of colors used */
167         Colormap        cmap;           /* window (private?) colormap */
168         XFontStruct *   font[NUMFONTS]; /* font structures */
169 } GraphicData;
170 
171 /* Records to hold cities */
172 
173 typedef struct City {
174     char *name;		/* Name of the city */
175     double lat, lon;	/* Latitude and longitude of city */
176     char *tz;		/* Timezone of city */
177     short size;
178     struct City *next;	/* Pointer to next record */
179 } City;
180 
181 /* Records to hold marks */
182 
183 typedef struct Mark {
184     City *city;
185     double save_lat, save_lon;
186     int  flags, pulse, full;
187     struct tm sr, ss, dl;
188 } Mark;
189 
190 /* Records to hold text labels */
191 
192 typedef struct TextLabel {
193     char *text;
194     double lat, lon;
195     int color;
196     int position;
197     struct TextLabel *next;
198 } TextLabel ;
199 
200 /* Records to hold text entries */
201 
202 typedef struct TextEntry {
203     char * string;
204     int maxlength;
205     int caret;
206     int oldcaret;
207     int oldlength;
208     char oldchar;
209 } TextEntry;
210 
211 /* Sundata structure */
212 typedef struct Sundata {
213         int             wintype;        /* is window map or clock ? */
214         Window          win;            /* window id */
215         Pixmap          mappix;         /* map pixmap */
216         XImage *        xim;            /* ximage of map */
217         char *          ximdata;        /* ximage data copy */
218         int             *spotsizes;     /* city spot sizes, by category */
219         int             *sizelimits;    /* city size limits */
220         GraphicData *   gdata;          /* associated graphical data */
221         Geometry        geom;           /* geometry */
222 	Geometry        prevgeom;       /* previous geometry */
223         ZoomSettings    zoom;           /* Zoom settings of window */
224         ZoomSettings    newzoom;        /* New zoom settings */
225         ZoomSettings    oldzoom;        /* Old zoom settings */
226 	Flags		flags;		/* window behavioral settings */
227         int             hstrip;         /* height of bottom strip */
228         char *          clock_img_file; /* name of clock xpm file */
229         char *          map_img_file;   /* name of map xpm file */
230 	char *		bits;           /* pointer to char * bitmap bits */
231         short *         tr1;            /* pointer to day/night transition 1 */
232         short *         tr2;            /* pointer to day/night transition 2 */
233         int             south;          /* color code (0 / -1) at South pole */
234         double *        daywave;        /* pointer to daywave values */
235         double *        cosval;         /* pointer to cosine values */
236         double *        sinval;         /* pointer to sine values */
237         unsigned char * daypixel;       /* pointer to day pixels */
238         unsigned char * nightpixel;     /* pointer to night pixels */
239         Pixel           *vmfpixels;     /* list of additional vmf pixels */
240         int             ncolors;        /* number of colors in day pixels */
241 	time_t		footime;		/* time - real or fake, see flags */
242 	long		projtime;	/* last time projected illumination */
243 	long		roottime;	/* last time written to root */
244 	long		animtime;	/* last time of animation */
245         long            jump;           /* time jump (in sec) */
246         double          sundec;         /* Sun declination */
247         double          sunlon;         /* Sun longitude */
248         double          moondec;        /* Moon declination */
249         double          moonlon;        /* Moon longitude */
250         double          shadefactor;    /* shading factor */
251         double          shadescale;     /* shading rescale factor */
252         double          fnoon;          /* position of noon, double float */
253 	int		noon;		/* position of noon, integer */
254         int             local_day;      /* previous local day */
255         int             solar_day;      /* previous solar day */
256 	int		count;	        /* number of time iterations */
257         struct City     pos1;           /* first position */
258         struct City     pos2;           /* second position */
259         struct City     *lastmarked;    /* last marked city */
260         struct Mark     mark1;          /* first mark */
261         struct Mark     mark2;          /* second mark */
262         struct TextLabel *label;        /* label structure if any */
263         struct Sundata  *next;          /* pointer to next structure */
264 } Sundata;
265 
266 /* setupCallback structure */
267 
268 typedef void (*setupCB)(int mode);
269 
270 typedef void (*popCB)(Sundata * Context);
271 
272 /* Which OS are we using ? */
273 
274 #if defined(linux) || defined(__linux) || defined(__linux__)
275 #define _OS_LINUX_
276 #elif defined(hpux) || defined(__hpux) || defined(__hpux__)
277 #define _OS_HPUX_
278 #endif
279