tuned animation for demo
[livewp] / applet / src / livewp-rules.c
1 /*
2  * This file is part of Live Wallpaper (livewp)
3  * 
4  * Copyright (C) 2010 Vlad Vasiliev
5  * Copyright (C) 2010 Tanya Makova
6  *       for the code
7  * 
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  * 
13  * This software is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  * 
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this software; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22 */
23 /*******************************************************************************/
24
25 #include "livewp-rules.h"
26
27 /*******************************************************************************/
28
29 void get_localtime(int * year, int * month, int * day, int * hour, int * min, int * zone)
30 {
31     time_t timet;
32     struct tm nt;
33     time(&timet);
34     localtime_r(&timet, &nt);
35     *year = nt.tm_year + 1900;
36     *month = nt.tm_mon + 1;
37     *day = nt.tm_mday;
38     *hour = nt.tm_hour;
39     *min = nt.tm_min;
40     *zone = nt.tm_gmtoff/3600;
41   /* 
42  *year = 2010;
43     *month = 3;
44     *day = 24;
45     *hour = 16;
46     *min = 20;
47     *zone = 2;*/
48 }
49
50 void get_coord(double * lat, double * lon)
51 {
52     /* coordinates St.-Petersburg */
53     *lat = 59.883333;
54     *lon = 30.25;
55
56     /* coordinates Vitebsk */
57     //*lat = 55.166666;
58     //*lon = 30.133333;
59
60
61 }
62
63 double
64 get_max_sun_alt(double lon, double lat, int year, int month, int day, int zone)
65 {
66     int h1, m1, h2, m2, h, m;
67     struct tm;
68     double alt, hour1, hour2, hh, x;
69
70     sun_rise_set(lon, lat,
71                  year, month, day, 
72                  zone,
73                  &h1, &m1, &h2, &m2);
74     //printf("h1=%d m1=%d h2=%d m2=%d\n", h1, m1, h2, m2);
75     
76     hour1 = (double)h1 + (double)m1/60;
77     hour2 = (double)h2 + (double)m2/60;
78
79     hh = (hour2 - hour1) / 2 + hour1;
80     h = floor(hh);
81     m = (modf(hh, &x) * 60);
82     
83     alt = altitude(lon, lat, year, month, day, h, m, zone);
84     //printf("alt = %f \n", alt);
85     return alt;
86 }
87 void get_sun_pos(int h, int m, double * alt, double * azm)
88 {
89     int year, month, day, hour, min, zone;
90     double lat, lon, alt_max;
91     //get_localtime(&year, &month, &day, &hour, &min, &zone);
92     year = 2010;
93     month = 4;
94     day = 14;
95     hour = h;
96     min = m;
97     zone = 3;
98     get_coord(&lat, &lon);
99     alt_max = get_max_sun_alt(lon, lat, year, month, day, zone);
100     *alt = altitude(lon, lat, year, month, day, hour, min, zone);
101     //printf("max = %f alt = %f ",alt_max, *alt);
102     *alt = *alt / (alt_max + 10); // sun height in percent
103     //printf("alt1 = %f \n", *alt);
104
105     *azm = azimuth(lon, lat, year, month, day, hour, min, zone);
106     //printf("azm = %f ", *azm);
107     //if south latitude
108     if (lat < 0){
109         *azm = 75 + (75 - *azm);
110         *azm = fmod(*azm, 380);
111     }
112     *azm = (*azm - 75) / 210; // sun azimuth in percent
113     //printf("azm1 = %f \n", *azm);
114 }
115
116 void get_sun_pos1(double * alt, double * azm)
117 {
118     int year, month, day, hour, min, zone;
119     double lat, lon, alt_max;
120     get_localtime(&year, &month, &day, &hour, &min, &zone);
121     get_coord(&lat, &lon);
122     alt_max = get_max_sun_alt(lon, lat, year, month, day, zone);
123     *alt = altitude(lon, lat, year, month, day, hour, min, zone);
124     //printf("max = %f alt = %f ",alt_max, *alt);
125     *alt = *alt / (alt_max + 10); // sun height in percent
126     //printf("alt1 = %f \n", *alt);
127
128     *azm = azimuth(lon, lat, year, month, day, hour, min, zone);
129     //printf("azm = %f ", *azm);
130     //if south latitude
131     if (lat < 0){
132         *azm = 75 + (75 - *azm);
133         *azm = fmod(*azm, 380);
134     }
135     *azm = (*azm - 75) / 210; // sun azimuth in percent
136     //printf("azm1 = %f \n", *azm);
137 }
138 time_t get_next_sunrise()
139 {
140     return time(NULL) + 60*60*8;
141 }
142 time_t get_next_sunset()
143 {
144         return time(NULL) + 60*60*12;
145 }
146
147 int get_daytime(int h, int m)
148 {
149     double alt, azm;
150     get_sun_pos(h, m, &alt, &azm);
151     alt = alt * 100;
152     azm = azm * 100;
153     //printf("alt = %f azm=%f\n", alt, azm);
154     if (alt <= -12) return TIME_NIGHT;
155     if (alt > -12 && alt < 12 && azm < 50) return TIME_SUNRISE;
156     if (alt > -12 && alt < 12 && azm > 50) return TIME_SUNSET;
157     if (alt >= 12) return TIME_DAY;
158     return TIME_DAY;
159 }
160 int get_daytime1()
161 {
162     double alt, azm;
163     get_sun_pos1(&alt, &azm);
164     alt = alt * 100;
165     azm = azm * 100;
166     //printf("alt = %f azm=%f\n", alt, azm);
167     if (alt <= -7) return TIME_NIGHT;
168     if (alt > -7 && alt < 7 && azm < 50) return TIME_SUNRISE;
169     if (alt > -7 && alt < 7 && azm > 50) return TIME_SUNSET;
170     if (alt >= 7) return TIME_DAY;
171     return TIME_DAY;
172 }
173 int get_moon_phase()
174
175     int year, month, day, hour, min, zone;
176     double phase;
177     get_localtime(&year, &month, &day, &hour, &min, &zone);
178     phase = moon_phase(year, month, day) * 100;
179     /* printf("ph = %f\n", phase); */
180     if (phase <= 3 || phase >=97) return MOON_NONE;
181     if (phase > 5 && phase <= 25) return MOON_GROWS;
182     if (phase > 25 && phase <= 45) return MOON_GROWSHALF;
183     if (phase > 45 && phase <= 55) return MOON_FULL;
184     if (phase > 55 && phase <= 75) return MOON_DECREASHALF;
185     if (phase > 75 && phase < 97) return MOON_DECREAS;
186     return MOON_NONE;
187
188 }