Make AddMouseRegion's index unsigned
[dockapps.git] / wmsun / wmsun.c
blob4ab00c657d83aa8cc662b554e12a7d42be2ff9bb
1 /*
3 * wmSun (C) 1999 Mike Henderson (mghenderson@lanl.gov)
5 * - Shows Sun Rise/Set Times....
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA
26 * Things TODO:
27 * - clean up code!
28 * - support for 8-bit displays.
29 * - more detailed documentation.
30 * - eclipses?
31 * - add buttons to play will date and lat lon...
32 * Could be something like this;
33 * First click brings up buttons to change date.
34 * Second click brings up buttons to change lat/lon.
35 * Third goes back to display
36 * Set time delay to go back to display if user doesnt do it...
44 * Includes
46 #define _POSIX_C_SOURCE 1
47 #include <X11/X.h> /* for ButtonPress, ButtonRelease, etc */
48 #include <X11/Xlib.h> /* for XEvent, ConnectionNumber, etc */
49 #include <math.h> /* for cos, sin */
50 #include <stdio.h> /* for printf, NULL */
51 #include <stdlib.h> /* for atof, atoi, exit */
52 #include <string.h> /* for strcmp */
53 #include <sys/select.h> /* for select, FD_SET, FD_ZERO, etc */
54 #include <sys/time.h> /* for timeval */
55 #include <time.h> /* for tm, gmtime_r, localtime_r, etc */
56 #include "wmSun_mask.xbm" /* for wmSun_mask_bits, etc */
57 #include "wmSun_master.xpm" /* for wmSun_master */
58 #include <libdockapp/wmgeneral.h> /* for copyXPMArea, display, etc */
63 * Delay between refreshes (in microseconds)
65 #define DELAY 1000000L
67 #define DegPerRad 57.29577951308232087680
68 #define RadPerDeg 0.01745329251994329576
74 void ParseCMDLine(int argc, char *argv[]);
75 void pressEvent(XButtonEvent *xev);
76 void SunRise(int year, int month, int day, double LocalHour, double *UTRise,
77 double *UTSet);
79 int ToggleWindow = 0;
80 int nMAX = 1;
81 int Flag = 1;
82 int UseUserTimeDiff = 0;
83 int UseUserDate = 0;
84 long UserDate;
85 double Glat, Glon, SinGlat, CosGlat, TimeZone, UserTimeDiff;
86 int TwelveHour = 0;
89 int xDigit[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};
95 * main
97 int main(int argc, char *argv[]) {
103 struct tm *GMTTime, *LocalTime;
104 XEvent event;
105 int n;
106 int Year, Month;
107 int DayOfMonth;
108 long CurrentLocalTime, CurrentGMTTime, date;
109 double UT, val, LTRise, LTSet, LocalHour, hour24();
110 int H, M;
111 struct timeval timeout;
112 fd_set xfdset;
120 * Parse any command line arguments.
122 Glat = Glon = 0.0;
123 ParseCMDLine(argc, argv);
124 Glat *= RadPerDeg; SinGlat = sin( Glat ); CosGlat = cos( Glat );
128 openXwindow(argc, argv, wmSun_master, (char *)wmSun_mask_bits, wmSun_mask_width, wmSun_mask_height);
135 * Loop until we die
137 n = 32000;
138 while(1) {
141 if (Flag) {
142 n = 32000;
143 Flag = 0;
150 * The Moon Ephemeris calculations are somewhat costly (the Moon is one of the most
151 * difficult objects to compute position for). So only process every nMAXth cycle of this
152 * loop. We run outer loop it faster to catch expose events, button presses, etc...
155 if (n>nMAX){
156 struct tm result;
158 n = 0;
159 nMAX = 60;
162 CurrentGMTTime = time(CurrentTime);
163 GMTTime = gmtime_r(&CurrentGMTTime, &result);
164 DayOfMonth = GMTTime->tm_mday;
166 UT = GMTTime->tm_hour + GMTTime->tm_min/60.0 + GMTTime->tm_sec/3600.0;
167 Year = GMTTime->tm_year+1900;
168 Month = GMTTime->tm_mon+1;
171 CurrentLocalTime = CurrentGMTTime;
172 LocalTime = localtime_r(&CurrentLocalTime, &result);
175 Flag = 0;
177 if (UseUserDate){
178 date = UserDate;
179 Year = date/10000;
180 date -= Year*10000;
181 Month = date/100;
182 date -= Month*100;
183 DayOfMonth = date;
184 date = UserDate;
185 } else {
186 date = Year*10000 + Month*100 + DayOfMonth;
188 LocalHour = LocalTime->tm_hour + LocalTime->tm_min/60.0 + LocalTime->tm_sec/3600.0;
189 TimeZone = (UseUserTimeDiff) ? UserTimeDiff : UT - LocalHour;
192 * Clear Plotting area
194 copyXPMArea(65, 5, 54, 54, 5, 5);
199 * Compute Sun Rise/Set Times in Local Time
201 SunRise(Year, Month, DayOfMonth, LocalHour, &LTRise, &LTSet);
203 if (LTRise > 0.0){
204 val = LTRise;
205 H = (int)val; val = (val-H)*60.0;
206 if (TwelveHour) {
207 H = H % 12;
208 if (H == 0)
209 H = 12;
211 M = (int)val;
212 copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 13);
213 copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 13);
214 copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 15);
215 copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 13);
216 copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 13);
217 } else {
218 copyXPMArea(10, 84, 28, 7, 19, 15);
222 if (LTSet > 0.0){
223 val = LTSet;
224 H = (int)val; val = (val-H)*60.0;
225 if (TwelveHour) {
226 H = H % 12;
227 if (H == 0)
228 H = 12;
230 M = (int)val;
231 copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 40);
232 copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 40);
233 copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 42);
234 copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 40);
235 copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 40);
236 } else {
237 copyXPMArea(10, 84, 28, 7, 19, 40);
240 } else {
243 * Update the counter.
245 ++n;
252 * Add X display to file descriptor set for polling.
254 FD_ZERO(&xfdset);
255 FD_SET(ConnectionNumber(display), &xfdset);
262 * Process any pending X events.
264 while(XPending(display)){
265 XNextEvent(display, &event);
266 switch(event.type){
267 case Expose:
268 RedrawWindow();
269 break;
270 case ButtonPress:
271 pressEvent(&event.xbutton);
272 break;
273 case ButtonRelease:
274 break;
284 * Redraw and wait for next update
286 RedrawWindow();
287 timeout.tv_sec = DELAY / 1000000L;
288 timeout.tv_usec = DELAY % 1000000L;
289 select(ConnectionNumber(display) + 1, &xfdset, NULL, NULL, &timeout);
306 * ParseCMDLine()
308 void ParseCMDLine(int argc, char *argv[]) {
310 int i;
312 for (i = 1; i < argc; i++) {
314 if (!strcmp(argv[i], "-display")){
316 ++i;
318 } else if (!strcmp(argv[i], "-geometry")){
320 ++i;
322 } else if (!strcmp(argv[i], "-lat")){
324 Glat = atof(argv[++i]);
326 } else if (!strcmp(argv[i], "-lon")){
328 Glon = atof(argv[++i]);
330 } else if (!strcmp(argv[i], "-td")){
332 UseUserTimeDiff = 1;
333 UserTimeDiff = atof(argv[++i]);
335 } else if (!strcmp(argv[i], "-date")){
337 UseUserDate = 1;
338 UserDate = atoi(argv[++i]);
340 } else if (!strcmp(argv[i], "-12")){
342 TwelveHour = 1;
344 } else {
345 printf("\nwmSun version: %s\n", WMSUN_VERSION);
346 printf("\nusage: wmsun [-display <Display>] [-lat <Latitude>] [-lon <Longitude>] [-h]\n\n");
347 printf("\t-display <Display>\tUse alternate X display.\n");
348 printf("\t-geometry <Geometry>\tSet window geometry.\n");
349 printf("\t-lat <Latitude>\t\tObservers Latitude. Positive to the west.\n");
350 printf("\t-lon <Longitude>\tObservers Longitude.\n");
351 printf("\t-td <Delta Time>\tUser defined difference between UT an LT (hrs).\n");
352 printf("\t-12\t\t\tUse 12-hour clock.\n");
353 printf("\t-h\t\t\tDisplay help screen.\n\n");
354 exit(1);
362 * This routine handles button presses. Clicking in the window
363 * toggles the display.
366 void pressEvent(XButtonEvent *xev){
368 (void) xev;
369 ++ToggleWindow;
370 if (ToggleWindow > 4) ToggleWindow = 0;
371 Flag = 1;
373 return;