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)
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
28 * - support for 8-bit displays.
29 * - more detailed documentation.
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...
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
66 #define WMSUN_VERSION "1.05"
68 #define DegPerRad 57.29577951308232087680
69 #define RadPerDeg 0.01745329251994329576
75 void ParseCMDLine(int argc
, char *argv
[]);
76 void pressEvent(XButtonEvent
*xev
);
77 void SunRise(int year
, int month
, int day
, double LocalHour
, double *UTRise
,
83 int UseUserTimeDiff
= 0;
86 double Glat
, Glon
, SinGlat
, CosGlat
, TimeZone
, UserTimeDiff
;
90 int xDigit
[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};
98 int main(int argc
, char *argv
[]) {
104 struct tm
*GMTTime
, *LocalTime
;
109 long CurrentLocalTime
, CurrentGMTTime
, date
;
110 double UT
, val
, LTRise
, LTSet
, LocalHour
, hour24();
112 struct timeval timeout
;
121 * Parse any command line arguments.
124 ParseCMDLine(argc
, argv
);
125 Glat
*= RadPerDeg
; SinGlat
= sin( Glat
); CosGlat
= cos( Glat
);
129 openXwindow(argc
, argv
, wmSun_master
, (char *)wmSun_mask_bits
, wmSun_mask_width
, wmSun_mask_height
);
151 * The Moon Ephemeris calculations are somewhat costly (the Moon is one of the most
152 * difficult objects to compute position for). So only process every nMAXth cycle of this
153 * loop. We run outer loop it faster to catch expose events, button presses, etc...
163 CurrentGMTTime
= time(CurrentTime
);
164 GMTTime
= gmtime_r(&CurrentGMTTime
, &result
);
165 DayOfMonth
= GMTTime
->tm_mday
;
167 UT
= GMTTime
->tm_hour
+ GMTTime
->tm_min
/60.0 + GMTTime
->tm_sec
/3600.0;
168 Year
= GMTTime
->tm_year
+1900;
169 Month
= GMTTime
->tm_mon
+1;
172 CurrentLocalTime
= CurrentGMTTime
;
173 LocalTime
= localtime_r(&CurrentLocalTime
, &result
);
187 date
= Year
*10000 + Month
*100 + DayOfMonth
;
189 LocalHour
= LocalTime
->tm_hour
+ LocalTime
->tm_min
/60.0 + LocalTime
->tm_sec
/3600.0;
190 TimeZone
= (UseUserTimeDiff
) ? UserTimeDiff
: UT
- LocalHour
;
193 * Clear Plotting area
195 copyXPMArea(65, 5, 54, 54, 5, 5);
200 * Compute Sun Rise/Set Times in Local Time
202 SunRise(Year
, Month
, DayOfMonth
, LocalHour
, <Rise
, <Set
);
206 H
= (int)val
; val
= (val
-H
)*60.0;
213 copyXPMArea(xDigit
[H
/10], 73, 7, 9, 17, 13);
214 copyXPMArea(xDigit
[H
%10], 73, 7, 9, 17+7, 13);
215 copyXPMArea(xDigit
[10], 75, 3, 6, 17+15, 15);
216 copyXPMArea(xDigit
[M
/10], 73, 7, 9, 17+19, 13);
217 copyXPMArea(xDigit
[M
%10], 73, 7, 9, 17+26, 13);
219 copyXPMArea(10, 84, 28, 7, 19, 15);
225 H
= (int)val
; val
= (val
-H
)*60.0;
232 copyXPMArea(xDigit
[H
/10], 73, 7, 9, 17, 40);
233 copyXPMArea(xDigit
[H
%10], 73, 7, 9, 17+7, 40);
234 copyXPMArea(xDigit
[10], 75, 3, 6, 17+15, 42);
235 copyXPMArea(xDigit
[M
/10], 73, 7, 9, 17+19, 40);
236 copyXPMArea(xDigit
[M
%10], 73, 7, 9, 17+26, 40);
238 copyXPMArea(10, 84, 28, 7, 19, 40);
244 * Update the counter.
253 * Add X display to file descriptor set for polling.
256 FD_SET(ConnectionNumber(display
), &xfdset
);
263 * Process any pending X events.
265 while(XPending(display
)){
266 XNextEvent(display
, &event
);
272 pressEvent(&event
.xbutton
);
285 * Redraw and wait for next update
288 timeout
.tv_sec
= DELAY
/ 1000000L;
289 timeout
.tv_usec
= DELAY
% 1000000L;
290 select(ConnectionNumber(display
) + 1, &xfdset
, NULL
, NULL
, &timeout
);
309 void ParseCMDLine(int argc
, char *argv
[]) {
313 for (i
= 1; i
< argc
; i
++) {
315 if (!strcmp(argv
[i
], "-display")){
319 } else if (!strcmp(argv
[i
], "-geometry")){
323 } else if (!strcmp(argv
[i
], "-lat")){
325 Glat
= atof(argv
[++i
]);
327 } else if (!strcmp(argv
[i
], "-lon")){
329 Glon
= atof(argv
[++i
]);
331 } else if (!strcmp(argv
[i
], "-td")){
334 UserTimeDiff
= atof(argv
[++i
]);
336 } else if (!strcmp(argv
[i
], "-date")){
339 UserDate
= atoi(argv
[++i
]);
341 } else if (!strcmp(argv
[i
], "-12")){
346 printf("\nwmSun version: %s\n", WMSUN_VERSION
);
347 printf("\nusage: wmsun [-display <Display>] [-lat <Latitude>] [-lon <Longitude>] [-h]\n\n");
348 printf("\t-display <Display>\tUse alternate X display.\n");
349 printf("\t-geometry <Geometry>\tSet window geometry.\n");
350 printf("\t-lat <Latitude>\t\tObservers Latitude. Positive to the west.\n");
351 printf("\t-lon <Longitude>\tObservers Longitude.\n");
352 printf("\t-td <Delta Time>\tUser defined difference between UT an LT (hrs).\n");
353 printf("\t-12\t\t\tUse 12-hour clock.\n");
354 printf("\t-h\t\t\tDisplay help screen.\n\n");
363 * This routine handles button presses. Clicking in the window
364 * toggles the display.
367 void pressEvent(XButtonEvent
*xev
){
371 if (ToggleWindow
> 4) ToggleWindow
= 0;