rewrote actors objects
[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     *lat = 55.166666;
53     *lon = 30.166666;
54 }
55
56 void get_sun_pos(double * alt, double * azm)
57 {
58     int year, month, day, hour, min, zone;
59     double lat, lon;
60     get_localtime(&year, &month, &day, &hour, &min, &zone);
61     get_coord(&lat, &lon);
62     *alt = altitude(lon, lat, year, month, day, hour, min, zone);
63     *alt = *alt / 70; // sun height in percent
64
65     *azm = azimuth(lon, lat, year, month, day, hour, min, zone);
66     //if south latitude
67     if (lat < 0){
68         *azm = 90 + (90 - *azm);
69         *azm = fmod(*azm, 360);
70     }
71     *azm = (*azm - 90) / 180; // sun azimuth in percent
72 }
73 time_t get_next_sunrise()
74 {
75     return time(NULL) + 60*60*8;
76 }
77 int get_daytime()
78 {
79     double alt, azm;
80     get_sun_pos(&alt, &azm);
81     alt = alt * 100;
82     azm = azm * 100;
83     //fprintf("alt = %f azm=%f\n", alt, azm);
84     if (alt <= -7) return TIME_NIGHT;
85     if (alt > -7 && alt < 7 && azm < 50) return TIME_SUNRISE;
86     if (alt > 7 && alt < azm > 50) return TIME_SUNSET;
87     if (alt >= 7) return TIME_DAY;
88     
89 }
90 int get_moon_phase()
91
92     int year, month, day, hour, min, zone;
93     double phase;
94     get_localtime(&year, &month, &day, &hour, &min, &zone);
95     phase = moon_phase(year, month, day) * 100;
96     printf("ph = %f\n", phase);
97     if (phase <= 3 || phase >=97) return MOON_NONE;
98     if (phase > 5 && phase <= 25) return MOON_GROWS;
99     if (phase > 25 && phase <= 45) return MOON_GROWSHALF;
100     if (phase > 45 && phase <= 55) return MOON_FULL;
101     if (phase > 55 && phase <= 75) return MOON_DECREASHALF;
102     if (phase > 75 && phase < 97) return MOON_DECREAS;
103 }