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
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
,
82 int UseUserTimeDiff
= 0;
85 double Glat
, Glon
, SinGlat
, CosGlat
, TimeZone
, UserTimeDiff
;
89 int xDigit
[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};
97 int main(int argc
, char *argv
[]) {
103 struct tm
*GMTTime
, *LocalTime
;
108 long CurrentLocalTime
, CurrentGMTTime
, date
;
109 double UT
, val
, LTRise
, LTSet
, LocalHour
, hour24();
111 struct timeval timeout
;
120 * Parse any command line arguments.
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
);
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...
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
);
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
, <Rise
, <Set
);
205 H
= (int)val
; val
= (val
-H
)*60.0;
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);
218 copyXPMArea(10, 84, 28, 7, 19, 15);
224 H
= (int)val
; val
= (val
-H
)*60.0;
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);
237 copyXPMArea(10, 84, 28, 7, 19, 40);
243 * Update the counter.
252 * Add X display to file descriptor set for polling.
255 FD_SET(ConnectionNumber(display
), &xfdset
);
262 * Process any pending X events.
264 while(XPending(display
)){
265 XNextEvent(display
, &event
);
271 pressEvent(&event
.xbutton
);
284 * Redraw and wait for next update
287 timeout
.tv_sec
= DELAY
/ 1000000L;
288 timeout
.tv_usec
= DELAY
% 1000000L;
289 select(ConnectionNumber(display
) + 1, &xfdset
, NULL
, NULL
, &timeout
);
308 void ParseCMDLine(int argc
, char *argv
[]) {
312 for (i
= 1; i
< argc
; i
++) {
314 if (!strcmp(argv
[i
], "-display")){
318 } else if (!strcmp(argv
[i
], "-geometry")){
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")){
333 UserTimeDiff
= atof(argv
[++i
]);
335 } else if (!strcmp(argv
[i
], "-date")){
338 UserDate
= atoi(argv
[++i
]);
340 } else if (!strcmp(argv
[i
], "-12")){
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");
362 * This routine handles button presses. Clicking in the window
363 * toggles the display.
366 void pressEvent(XButtonEvent
*xev
){
370 if (ToggleWindow
> 4) ToggleWindow
= 0;