File: wmsun.c

package info (click to toggle)
wmsun 1.06-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 156 kB
  • sloc: ansic: 327; makefile: 38
file content (376 lines) | stat: -rw-r--r-- 8,637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 *
 *  	wmSun (C) 1999 Mike Henderson (mghenderson@lanl.gov)
 *
 *  		- Shows Sun Rise/Set Times....
 *
 *
 *
 *
 *
 * 	This program is free software; you can redistribute it and/or modify
 * 	it under the terms of the GNU General Public License as published by
 * 	the Free Software Foundation; either version 2, or (at your option)
 * 	any later version.
 *
 * 	This program is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 *
 * 	You should have received a copy of the GNU General Public License
 * 	along with this program (see the file COPYING); if not, write to the
 * 	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *      Boston, MA  02111-1307, USA
 *
 *      Things TODO:
 *                  - clean up code!
 *                  - support for 8-bit displays.
 *                  - more detailed documentation.
 *                  - eclipses?
 *                  - add buttons to play will date and lat lon...
 *                    Could be something like this;
 *                       First click brings up buttons to change date.
 *                       Second click brings up buttons to change lat/lon.
 *                       Third goes back to display
 *                       Set time delay to go back to display if user doesnt do it...
 */





/*
 *   Includes
 */
#define _POSIX_C_SOURCE 1
#include <X11/X.h>                     /* for ButtonPress, ButtonRelease, etc */
#include <X11/Xlib.h>                  /* for XEvent, ConnectionNumber, etc */
#include <math.h>                      /* for cos, sin */
#include <stdio.h>                     /* for printf, NULL */
#include <stdlib.h>                    /* for atof, atoi, exit */
#include <string.h>                    /* for strcmp */
#include <sys/select.h>                /* for select, FD_SET, FD_ZERO, etc */
#include <sys/time.h>                  /* for timeval */
#include <time.h>                      /* for tm, gmtime_r, localtime_r, etc */
#include "wmSun_mask.xbm"              /* for wmSun_mask_bits, etc */
#include "wmSun_master.xpm"            /* for wmSun_master */
#include <libdockapp/wmgeneral.h>      /* for copyXPMArea, display, etc */



/*
 *  Delay between refreshes (in microseconds)
 */
#define DELAY 1000000L

#define DegPerRad       57.29577951308232087680
#define RadPerDeg        0.01745329251994329576





void ParseCMDLine(int argc, char *argv[]);
void pressEvent(XButtonEvent *xev);
void SunRise(int year, int month, int day, double LocalHour, double *UTRise,
	double *UTSet);

int	ToggleWindow = 0;
int	nMAX = 1;
int	Flag = 1;
int	UseUserTimeDiff = 0;
int	UseUserDate = 0;
long	UserDate;
double 	Glat, Glon, SinGlat, CosGlat, TimeZone, UserTimeDiff;
int	TwelveHour = 0;


int	xDigit[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};




/*
 *   main
 */
int main(int argc, char *argv[]) {





    struct tm		*GMTTime, *LocalTime;
    XEvent		event;
    int			n;
    int 		Year, Month;
    int			DayOfMonth;
    long		CurrentLocalTime, CurrentGMTTime, date;
    double		UT, val, LTRise, LTSet, LocalHour, hour24();
    int			H, M;
    struct timeval	timeout;
    fd_set		xfdset;






    /*
     *  Parse any command line arguments.
     */
    Glat = Glon = 0.0;
    ParseCMDLine(argc, argv);
    Glat *= RadPerDeg; SinGlat = sin( Glat ); CosGlat = cos( Glat );



    openXwindow(argc, argv, wmSun_master, (char *)wmSun_mask_bits, wmSun_mask_width, wmSun_mask_height);





    /*
     *  Loop until we die
     */
    n = 32000;
    while(1) {


	if (Flag) {
	    n = 32000;
	    Flag = 0;
	}




	/*
	 *  The Moon Ephemeris calculations are somewhat costly (the Moon is one of the most
	 *  difficult objects to compute position for). So only process every nMAXth cycle of this
	 *  loop. We run outer loop it faster to catch expose events, button presses, etc...
	 *
	 */
	if (n>nMAX){
	    struct tm result;

	    n = 0;
	    nMAX = 60;


	    CurrentGMTTime = time(CurrentTime);
	    GMTTime = gmtime_r(&CurrentGMTTime, &result);
	    DayOfMonth = GMTTime->tm_mday;

	    UT = GMTTime->tm_hour + GMTTime->tm_min/60.0 + GMTTime->tm_sec/3600.0;
	    Year = GMTTime->tm_year+1900;
	    Month = GMTTime->tm_mon+1;


	    CurrentLocalTime = CurrentGMTTime;
	    LocalTime = localtime_r(&CurrentLocalTime, &result);


		Flag = 0;

		if (UseUserDate){
	    	    date =  UserDate;
		    Year = date/10000;
		    date -= Year*10000;
		    Month = date/100;
		    date -= Month*100;
		    DayOfMonth = date;
		    date = UserDate;
		} else {
	    	    date = Year*10000 + Month*100 + DayOfMonth;
		}
	    	LocalHour = LocalTime->tm_hour + LocalTime->tm_min/60.0 + LocalTime->tm_sec/3600.0;
	    	TimeZone = (UseUserTimeDiff) ? UserTimeDiff : UT - LocalHour;

	    	/*
	    	 *  Clear Plotting area
	    	 */
	    	copyXPMArea(65, 5, 54, 54, 5, 5);



   	    	/*
	    	 *  Compute Sun Rise/Set Times in Local Time
	    	 */
	    	SunRise(Year, Month, DayOfMonth, LocalHour, &LTRise, &LTSet);

		if (LTRise > 0.0){
	    	    val = LTRise;
	    	    H = (int)val; val = (val-H)*60.0;
		    if (TwelveHour) {
			H = H % 12;
			if (H == 0)
			    H = 12;
		    }
	    	    M = (int)val;
	    	    copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 13);
	    	    copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 13);
	    	    copyXPMArea(xDigit[10],   75, 3, 6, 17+15, 15);
	    	    copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 13);
	    	    copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 13);
		} else {
	    	    copyXPMArea(10, 84, 28, 7, 19, 15);
		}


		if (LTSet > 0.0){
	    	    val = LTSet;
	    	    H = (int)val; val = (val-H)*60.0;
		    if (TwelveHour) {
			H = H % 12;
			if (H == 0)
			    H = 12;
		    }
	    	    M = (int)val;
	    	    copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 40);
	    	    copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 40);
	    	    copyXPMArea(xDigit[10],   75, 3, 6, 17+15, 42);
	    	    copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 40);
	    	    copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 40);
		} else {
	    	    copyXPMArea(10, 84, 28, 7, 19, 40);
		}

	} else {

	    /*
	     *  Update the counter.
	     */
	    ++n;

	}



	/*
	 *  Add X display to file descriptor set for polling.
	 */
	FD_ZERO(&xfdset);
	FD_SET(ConnectionNumber(display), &xfdset);





	/*
	 *   Process any pending X events.
	 */
        while(XPending(display)){
            XNextEvent(display, &event);
            switch(event.type){
                case Expose:
                        RedrawWindow();
                        break;
                case ButtonPress:
                        pressEvent(&event.xbutton);
                        break;
                case ButtonRelease:
                        break;
            }
        }






	/*
	 *  Redraw and wait for next update
	 */
	RedrawWindow();
	timeout.tv_sec = DELAY / 1000000L;
	timeout.tv_usec = DELAY % 1000000L;
	select(ConnectionNumber(display) + 1, &xfdset, NULL, NULL, &timeout);


     }



}








/*
 *   ParseCMDLine()
 */
void ParseCMDLine(int argc, char *argv[]) {

    int  i;

    for (i = 1; i < argc; i++) {

        if (!strcmp(argv[i], "-display")){

  	    ++i;

        } else if (!strcmp(argv[i], "-geometry")){

	    ++i;

        } else if (!strcmp(argv[i], "-lat")){

	    Glat = atof(argv[++i]);

        } else if (!strcmp(argv[i], "-lon")){

	    Glon = atof(argv[++i]);

        } else if (!strcmp(argv[i], "-td")){

	    UseUserTimeDiff = 1;
	    UserTimeDiff = atof(argv[++i]);

        } else if (!strcmp(argv[i], "-date")){

	    UseUserDate = 1;
	    UserDate = atoi(argv[++i]);

	} else if (!strcmp(argv[i], "-12")){

	    TwelveHour = 1;

	} else {
	    printf("\nwmSun version: %s\n", WMSUN_VERSION);
	    printf("\nusage: wmsun [-display <Display>] [-lat <Latitude>] [-lon <Longitude>] [-h]\n\n");
	    printf("\t-display <Display>\tUse alternate X display.\n");
	    printf("\t-geometry <Geometry>\tSet window geometry.\n");
	    printf("\t-lat <Latitude>\t\tObservers Latitude. Positive to the west.\n");
	    printf("\t-lon <Longitude>\tObservers Longitude.\n");
	    printf("\t-td <Delta Time>\tUser defined difference between UT an LT (hrs).\n");
	    printf("\t-12\t\t\tUse 12-hour clock.\n");
	    printf("\t-h\t\t\tDisplay help screen.\n\n");
	    exit(1);
	}
    }

}


/*
 *  This routine handles button presses. Clicking in the window
 *  toggles the display.
 *
 */
void pressEvent(XButtonEvent *xev){

   (void) xev;
   ++ToggleWindow;
   if (ToggleWindow > 4) ToggleWindow = 0;
   Flag = 1;

   return;

}