2 * This file is part of Live Wallpaper (livewp)
4 * Copyright (C) 2010 Vlad Vasiliev
5 * Copyright (C) 2010 Tanya Makova
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.
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.
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
23 /*******************************************************************************/
25 #include "livewp-rules.h"
27 /*******************************************************************************/
29 void get_localtime(int * year, int * month, int * day, int * hour, int * min, int * zone)
34 localtime_r(&timet, &nt);
35 *year = nt.tm_year + 1900;
36 *month = nt.tm_mon + 1;
40 *zone = nt.tm_gmtoff/3600;
50 void get_coord(double * lat, double * lon)
52 /* coordinates St.-Petersburg */
56 /* coordinates Vitebsk */
64 get_max_sun_alt(double lon, double lat, int year, int month, int day, int zone)
66 int h1, m1, h2, m2, h, m;
68 double alt, hour1, hour2, hh, x;
70 sun_rise_set(lon, lat,
74 //printf("h1=%d m1=%d h2=%d m2=%d\n", h1, m1, h2, m2);
76 hour1 = (double)h1 + (double)m1/60;
77 hour2 = (double)h2 + (double)m2/60;
79 hh = (hour2 - hour1) / 2 + hour1;
81 m = (modf(hh, &x) * 60);
83 alt = altitude(lon, lat, year, month, day, h, m, zone);
84 //printf("alt = %f \n", alt);
88 void get_sun_pos(double * alt, double * azm)
90 int year, month, day, hour, min, zone;
91 double lat, lon, alt_max;
92 get_localtime(&year, &month, &day, &hour, &min, &zone);
93 get_coord(&lat, &lon);
94 alt_max = get_max_sun_alt(lon, lat, year, month, day, zone);
95 *alt = altitude(lon, lat, year, month, day, hour, min, zone);
96 //printf("max = %f alt = %f ",alt_max, *alt);
97 *alt = *alt / (alt_max + 10); // sun height in percent
98 //printf("alt1 = %f \n", *alt);
100 *azm = azimuth(lon, lat, year, month, day, hour, min, zone);
101 //printf("azm = %f ", *azm);
104 *azm = 75 + (75 - *azm);
105 *azm = fmod(*azm, 380);
107 *azm = (*azm - 75) / 210; // sun azimuth in percent
108 //printf("azm1 = %f \n", *azm);
110 time_t get_next_sunrise()
112 return time(NULL) + 60*60*8;
114 time_t get_next_sunset()
116 return time(NULL) + 60*60*12;
122 get_sun_pos(&alt, &azm);
125 //printf("alt = %f azm=%f\n", alt, azm);
126 if (alt <= -7) return TIME_NIGHT;
127 if (alt > -7 && alt < 7 && azm < 50) return TIME_SUNRISE;
128 if (alt > -7 && alt < 7 && azm > 50) return TIME_SUNSET;
129 if (alt >= 7) return TIME_DAY;
135 int year, month, day, hour, min, zone;
137 get_localtime(&year, &month, &day, &hour, &min, &zone);
138 phase = moon_phase(year, month, day) * 100;
139 /* printf("ph = %f\n", phase); */
140 if (phase <= 3 || phase >=97) return MOON_NONE;
141 if (phase > 5 && phase <= 25) return MOON_GROWS;
142 if (phase > 25 && phase <= 45) return MOON_GROWSHALF;
143 if (phase > 45 && phase <= 55) return MOON_FULL;
144 if (phase > 55 && phase <= 75) return MOON_DECREASHALF;
145 if (phase > 75 && phase < 97) return MOON_DECREAS;
153 guint offset = 12923;
154 guint multiplier = 4079;
155 static guint seed = 123;
157 seed = seed * multiplier + offset;
158 return (gint)(seed % max);
161 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
163 gint y0 = 365;// - уровень горизонта
164 *x = (int)(azm * 800) - 64;
165 *y = (int)((1 - alt) * y0) - 64;
166 //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);