dc437593a82c30f428e05a9b94aa367322a98ca8
[livewp] / applet / livewp-home-widget.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 #include <gtk/gtk.h>
25 #include <hildon/hildon.h>
26 #include "livewp-home-widget.h"
27 #include <libhildondesktop/libhildondesktop.h>
28 #include <gconf/gconf-client.h>
29 #include "livewp-astro.h"
30
31 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
32 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
33 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
34 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
35
36 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
37
38 #define Xstartposition 795
39 #define Ystartposition 430
40
41 gint xapplet = 0, yapplet = 0;
42 GSList * objects_list = NULL;
43
44 gboolean
45 expose_event (GtkWidget *widget,GdkEventExpose *event,
46      gpointer data)
47 {
48     cairo_t *cr;
49     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
50         
51     cr = gdk_cairo_create(widget->window);
52     gdk_cairo_region(cr, event->region);
53     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
54     gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
55     cairo_paint(cr);
56     cairo_destroy(cr);
57     return TRUE;
58 }
59 void
60 realize (GtkWidget *widget)
61 {
62     GdkScreen *screen;
63     screen = gtk_widget_get_screen (widget);
64     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
65 }
66
67 void
68 init_applet_position(void)
69 {
70   /* To Do Must be Only 1 per secund */
71   GSList *position = NULL;
72   gchar *position_key;
73   gchar *modified_key;
74   gchar *modified;
75   GError *error = NULL;
76   GConfClient   *gconf_client = gconf_client_get_default ();
77   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
78   position = gconf_client_get_list (gconf_client,
79                                     position_key,
80                                     GCONF_VALUE_INT,
81                                     NULL);
82   if (position && position->data && position->next->data){
83         xapplet = GPOINTER_TO_INT (position->data);
84         yapplet = GPOINTER_TO_INT (position->next->data);
85   }else{
86         fprintf(stderr,"Not position \n");
87         position = g_slist_prepend (g_slist_prepend (NULL,
88                                       GINT_TO_POINTER (Ystartposition)),
89                                       GINT_TO_POINTER (Xstartposition));
90         gconf_client_set_list (gconf_client,
91                                position_key,
92                                GCONF_VALUE_INT,
93                                position,
94                                &error);
95         xapplet = Xstartposition;
96         yapplet = Ystartposition;
97   }
98   g_free (position_key);
99   modified = g_strdup_printf ("%ld", 0);
100   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
101   gconf_client_set_string (gconf_client,
102                            modified_key,
103                            modified,
104                            &error);
105   fprintf(stderr,"position %i %i\n",xapplet, yapplet);
106   gconf_client_clear_cache(gconf_client);
107   g_object_unref(gconf_client);
108 }
109
110 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
111 {
112  fprintf(stderr, "actor_set_position_full\n");
113  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
114 }
115
116 GtkWidget* init_object(gchar * name, gint x, gint y, gint z, gint width, gint height)
117 {
118   GtkWidget *actor;
119   GdkPixbuf *pixbuf;
120   GtkWidget *image;
121
122   actor = hildon_animation_actor_new();
123   gchar str[256];
124   snprintf(str, 255, "/usr/share/anwall/%s.png", name);
125   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
126   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
127                                              width, 
128                                              height, 
129                                              NULL);
130   if (pixbuf){
131       image = gtk_image_new_from_pixbuf (pixbuf);
132       g_object_unref(G_OBJECT(pixbuf));
133   }
134   g_signal_connect(G_OBJECT(image), "expose_event",
135                            G_CALLBACK(expose_event), pixbuf);
136
137   gtk_container_add (GTK_CONTAINER (actor), image);
138   actor_set_position_full(actor, x, y, z);
139   hildon_animation_actor_set_show (actor, 1);
140   realize(actor);
141   gtk_widget_show_all(actor);
142   g_object_set_data(G_OBJECT(actor), "name", name);
143   g_object_set_data(G_OBJECT(actor), "posX", x);
144   g_object_set_data(G_OBJECT(actor), "posY", y);
145
146   objects_list = g_slist_append(objects_list, G_OBJECT(actor));
147   return actor;
148 }
149
150 void
151 init_scene(GtkWidget *window)
152 {
153   GtkWidget *actor;
154   GdkPixbuf *pixbuf;
155   GtkWidget *image;
156   double alt, azm;
157   gint x, y;
158
159   get_sun_pos(&alt, &azm);
160   get_sun_screen_pos(alt, azm, &x, &y);
161   if (x > 800) x = 700;
162   if (y > 480) y = 400;
163   actor = init_object("sun", x, y, 1, 88, 88);
164   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
165   actor = init_object("sky", 0, 0, 0, 800, 480);
166   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
167   actor = init_object("town", 0, 0, 10, 800, 480);
168   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
169
170 //  actor = init_object("cloud1", 450, 100, 2, 200, 150);
171 //  hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
172
173
174 }
175
176 static void
177 example_label_home_applet_realize (GtkWidget *widget)
178 {
179 }
180 void get_coord(double * lat, double * lon)
181 {
182     *lat = 55.166666;
183     *lon = 30.166666;
184 }
185 void get_sun_pos(double * alt, double * azm)
186 {
187   double lon ,lat;
188   time_t timet;
189   struct tm nowtime;
190   get_coord(&lat, &lon);
191   time(&timet);
192   localtime_r(&timet, &nowtime);
193   //fprintf(stderr, "plugin on timeout %i.%i.%i (%i) %i:%i:%i\n", nowtime.tm_mday, nowtime.tm_mon+1, nowtime.tm_year+1900, abs(nowtime.tm_gmtoff/3600), nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec);
194  fprintf(stderr, "before altitude\n"); 
195     *alt = altitude(lon, lat, nowtime.tm_year+1900, nowtime.tm_mon+1, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_gmtoff/3600);
196     *alt = *alt / 70;
197     *azm = azimuth(lon, lat, nowtime.tm_year+1900, nowtime.tm_mon+1, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_gmtoff/3600);
198     //если южная широта
199     if (lat < 0){
200         *azm = 90 + (90 - *azm);
201         *azm = fmod(*azm, 360);
202     } 
203     *azm = (*azm - 90) / 180;
204     //fprintf(stderr, "alt=%5.6f azm=%5.6f\n", alt, azm);
205     
206 }
207
208 void get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
209 {
210     gint y0 = 400;// - уровень горизонта
211     gint o_width = 128,
212          o_height = 
213     *x = (int)(azm * 800) - 64;
214     *y = (int)((1 - alt) * y0) - 64;
215 }
216
217 static gboolean
218 plugin_on_timeout (gpointer data)
219 {
220   gchar * str;
221   gint x, y;
222   
223   double azm, alt;
224   get_sun_pos(&alt, &azm);
225
226   GSList * tmp = objects_list;
227   while (tmp != NULL){
228       str = g_object_get_data(G_OBJECT(tmp->data), "name");
229       fprintf(stderr, "object: %s\n", str);
230       if (str == "sun"){
231         get_sun_screen_pos(alt, azm, &x, &y);
232         actor_set_position_full(tmp->data, x, y, 0);
233         fprintf(stderr, "x = %i, y = %i\n", x, y);
234         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
235         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
236         fprintf(stderr, "x = %d y = %d\n", x, y);
237       }
238       tmp = g_slist_next(tmp);
239   }
240   
241   return TRUE; /* keep running this event */
242 }
243
244 static void
245 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
246 {
247   GtkWidget *label;
248   label = gtk_label_new (""); 
249   gtk_widget_show (label);
250   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
251   init_applet_position();
252   init_scene(desktop_plugin);
253   desktop_plugin->timer = g_timeout_add(1000*60*1, plugin_on_timeout, desktop_plugin);
254 }
255
256 static void
257 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {}
258
259 static void
260 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}