wmsun: Remove unused variables.
[dockapps.git] / wmsun / wmSun.c
blob7979181f81e1d9df14591be5ed9e7068597294f0
1 /*
3 * wmSun-1.03 (C) 1999 Mike Henderson (mghenderson@lanl.gov)
4 *
5 * - Shows Sun Rise/Set Times....
6 *
7 *
8 *
9 *
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...
42 * Changes:
44 * Version 1.03 - released February 4, 1999.
45 * cosmetic for AfterStep users. removed spurious black line at RHS edge an mask.
48 * Version 1.02 - released January 12, 1999.
49 * Added support for User-specified date and Time difference so that
50 * you can have the display be correct in local time even for remote
51 * lat/lons. (I am in Hawaii right now. I dont want to reset the time on
52 * my laptop, so with these new options I can still get the correct
53 * local times of rise/set. Sunset is awesome here in Kona!!! And the calcs
54 * seem to be quite good -- it's a good test here because the Sun sets over
55 * the Pacific (no mountains are in the way)).
57 * Version 1.01 - released January 6, 1999.
58 * Fixed stupid bug in Date change montior.
60 * Version 1.0 - released January 5, 1999.
69 /*
70 * Includes
72 #include <stdio.h>
73 #include <math.h>
74 #include <unistd.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <time.h>
78 #include <X11/X.h>
79 #include <X11/xpm.h>
80 #include "wmgeneral/wmgeneral.h"
81 #include "wmSun_master.xpm"
82 #include "wmSun_mask.xbm"
86 /*
87 * Delay between refreshes (in microseconds)
89 #define DELAY 10000L
90 #define WMSUN_VERSION "1.03"
92 #define DegPerRad 57.29577951308232087680
93 #define RadPerDeg 0.01745329251994329576
99 void ParseCMDLine(int argc, char *argv[]);
100 void pressEvent(XButtonEvent *xev);
101 SunRise(int year, int month, int day, double LocalHour, double *UTRise,
102 double *UTSet);
104 int ToggleWindow = 0;
105 int nMAX = 1;
106 int Flag = 1;
107 int UseUserTimeDiff = 0;
108 int UseUserDate = 0;
109 long UserDate;
110 double Glat, Glon, SinGlat, CosGlat, TimeZone, UserTimeDiff;
113 int xDigit[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};
119 * main
121 int main(int argc, char *argv[]) {
127 struct tm *GMTTime, *LocalTime;
128 XEvent event;
129 int n;
130 int Year, Month, OldLocalDayOfMonth;
131 int LocalDayOfMonth, DayOfMonth;
132 long CurrentLocalTime, CurrentGMTTime, date;
133 double UT, val, LTRise, LTSet, LocalHour, hour24();
134 int H, M;
142 * Parse any command line arguments.
144 Glat = Glon = 0.0;
145 ParseCMDLine(argc, argv);
146 Glat *= RadPerDeg; SinGlat = sin( Glat ); CosGlat = cos( Glat );
150 openXwindow(argc, argv, wmSun_master, wmSun_mask_bits, wmSun_mask_width, wmSun_mask_height);
157 * Loop until we die
159 n = 32000;
160 OldLocalDayOfMonth = -999;
161 while(1) {
164 if (Flag) {
165 n = 32000;
166 Flag = 0;
173 * The Moon Ephemeris calculations are somewhat costly (the Moon is one of the most
174 * difficult objects to compute position for). So only process every nMAXth cycle of this
175 * loop. We run outer loop it faster to catch expose events, button presses, etc...
178 if (n>nMAX){
180 n = 0;
181 nMAX = 1000;
184 CurrentGMTTime = time(CurrentTime); GMTTime = gmtime(&CurrentGMTTime);
185 DayOfMonth = GMTTime->tm_mday;
187 UT = GMTTime->tm_hour + GMTTime->tm_min/60.0 + GMTTime->tm_sec/3600.0;
188 Year = GMTTime->tm_year+1900;
189 Month = GMTTime->tm_mon+1;
192 CurrentLocalTime = CurrentGMTTime; LocalTime = localtime(&CurrentLocalTime);
193 LocalDayOfMonth = LocalTime->tm_mday;
195 if ((OldLocalDayOfMonth != LocalDayOfMonth)||(Flag)){
197 Flag = 0;
199 if (UseUserDate){
200 date = UserDate;
201 Year = date/10000;
202 date -= Year*10000;
203 Month = date/100;
204 date -= Month*100;
205 DayOfMonth = date;
206 date = UserDate;
207 } else {
208 date = Year*10000 + Month*100 + DayOfMonth;
210 LocalHour = LocalTime->tm_hour + LocalTime->tm_min/60.0 + LocalTime->tm_sec/3600.0;
211 TimeZone = (UseUserTimeDiff) ? UserTimeDiff : UT - LocalHour;
214 * Clear Plotting area
216 copyXPMArea(65, 5, 54, 54, 5, 5);
221 * Compute Sun Rise/Set Times in Local Time
223 SunRise(Year, Month, DayOfMonth, LocalHour, &LTRise, &LTSet);
225 if (LTRise > 0.0){
226 val = LTRise;
227 H = (int)val; val = (val-H)*60.0;
228 M = (int)val;
229 copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 13);
230 copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 13);
231 copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 15);
232 copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 13);
233 copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 13);
234 } else {
235 copyXPMArea(10, 84, 28, 7, 19, 15);
239 if (LTSet > 0.0){
240 val = LTSet;
241 H = (int)val; val = (val-H)*60.0;
242 M = (int)val;
243 copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 40);
244 copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 40);
245 copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 42);
246 copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 40);
247 copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 40);
248 } else {
249 copyXPMArea(10, 84, 28, 7, 19, 40);
254 OldLocalDayOfMonth = LocalDayOfMonth;
257 } else {
260 * Update the counter.
262 ++n;
274 * Process any pending X events.
276 while(XPending(display)){
277 XNextEvent(display, &event);
278 switch(event.type){
279 case Expose:
280 RedrawWindow();
281 break;
282 case ButtonPress:
283 pressEvent(&event.xbutton);
284 break;
285 case ButtonRelease:
286 break;
296 * Redraw and wait for next update
298 RedrawWindow();
299 usleep(DELAY);
316 * ParseCMDLine()
318 void ParseCMDLine(int argc, char *argv[]) {
320 int i;
322 for (i = 1; i < argc; i++) {
324 if (!strcmp(argv[i], "-display")){
326 ++i;
328 } else if (!strcmp(argv[i], "-lat")){
330 Glat = atof(argv[++i]);
332 } else if (!strcmp(argv[i], "-lon")){
334 Glon = atof(argv[++i]);
336 } else if (!strcmp(argv[i], "-td")){
338 UseUserTimeDiff = 1;
339 UserTimeDiff = atof(argv[++i]);
341 } else if (!strcmp(argv[i], "-date")){
343 UseUserDate = 1;
344 UserDate = atoi(argv[++i]);
346 } else {
347 printf("\nwmSun version: %s\n", WMSUN_VERSION);
348 printf("\nusage: wmSun [-display <Display>] [-lat <Latitude>] [-lon <Longitude>] [-h]\n\n");
349 printf("\t-display <Display>\tUse alternate X display.\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 (hours).\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 ++ToggleWindow;
369 if (ToggleWindow > 4) ToggleWindow = 0;
370 Flag = 1;
372 return;