something work
[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-rules.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 Scene scene;
44
45 gboolean
46 expose_event (GtkWidget *widget,GdkEventExpose *event,
47      gpointer data)
48 {
49     cairo_t *cr;
50     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
51         
52     cr = gdk_cairo_create(widget->window);
53     gdk_cairo_region(cr, event->region);
54     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
55     gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
56     cairo_paint(cr);
57     cairo_destroy(cr);
58     return TRUE;
59 }
60 void
61 realize (GtkWidget *widget)
62 {
63     GdkScreen *screen;
64     screen = gtk_widget_get_screen (widget);
65     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
66 }
67
68 void
69 init_applet_position(void)
70 {
71   /* To Do Must be Only 1 per secund */
72   GSList *position = NULL;
73   gchar *position_key;
74   gchar *modified_key;
75   gchar *modified;
76   GError *error = NULL;
77   GConfClient   *gconf_client = gconf_client_get_default ();
78   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
79   position = gconf_client_get_list (gconf_client,
80                                     position_key,
81                                     GCONF_VALUE_INT,
82                                     NULL);
83   if (position && position->data && position->next->data){
84         xapplet = GPOINTER_TO_INT (position->data);
85         yapplet = GPOINTER_TO_INT (position->next->data);
86   }else{
87         fprintf(stderr,"Not position \n");
88         position = g_slist_prepend (g_slist_prepend (NULL,
89                                       GINT_TO_POINTER (Ystartposition)),
90                                       GINT_TO_POINTER (Xstartposition));
91         gconf_client_set_list (gconf_client,
92                                position_key,
93                                GCONF_VALUE_INT,
94                                position,
95                                &error);
96         xapplet = Xstartposition;
97         yapplet = Ystartposition;
98   }
99   g_free (position_key);
100   modified = g_strdup_printf ("%ld", 0);
101   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
102   gconf_client_set_string (gconf_client,
103                            modified_key,
104                            modified,
105                            &error);
106   fprintf(stderr,"position %i %i\n",xapplet, yapplet);
107   gconf_client_clear_cache(gconf_client);
108   g_object_unref(gconf_client);
109 }
110
111 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
112 {
113  fprintf(stderr, "actor_set_position_full\n");
114  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
115 }
116
117 GtkWidget* init_object(gchar * name, gint x, gint y, gint z, gint width, gint height)
118 {
119   Actor  a;  
120   GtkWidget *actor;
121   GdkPixbuf *pixbuf;
122   GtkWidget *image;
123
124   actor = hildon_animation_actor_new();
125   gchar str[256];
126   snprintf(str, 255, "/usr/share/anwall/%s.png", name);
127   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
128   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
129                                              width, 
130                                              height, 
131                                              NULL);
132   if (pixbuf){
133       image = gtk_image_new_from_pixbuf (pixbuf);
134       g_object_unref(G_OBJECT(pixbuf));
135   }
136   g_signal_connect(G_OBJECT(image), "expose_event",
137                            G_CALLBACK(expose_event), pixbuf);
138   gtk_container_add (GTK_CONTAINER (actor), image);
139
140    pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/cloud1.png", 
141                                              width, 
142                                              height, 
143                                              NULL);
144   if (pixbuf){
145       image = gtk_image_new_from_pixbuf (pixbuf);
146       g_object_unref(G_OBJECT(pixbuf));
147   }
148   g_signal_connect(G_OBJECT(image), "expose_event",
149                            G_CALLBACK(expose_event), pixbuf);
150
151   gtk_container_add (GTK_CONTAINER (actor), image);
152   actor_set_position_full(actor, x, y, z);
153   hildon_animation_actor_set_show (actor, 1);
154   realize(actor);
155   gtk_widget_show_all(actor);
156   g_object_set_data(G_OBJECT(actor), "name", name);
157   g_object_set_data(G_OBJECT(actor), "x", x);
158   g_object_set_data(G_OBJECT(actor), "y", y);
159   g_object_set_data(G_OBJECT(actor), "z", z);
160   g_object_set_data(G_OBJECT(actor), "scale", 10);
161
162   
163   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
164   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
165   return actor;
166 }
167
168 void
169 init_scene(GtkWidget *window)
170 {
171   GtkWidget *actor;
172   GdkPixbuf *pixbuf;
173   GtkWidget *image;
174   double alt, azm;
175   gint x, y;
176
177   scene.window = window;
178   scene.daytime = get_daytime();
179   scene.dynamic_actors = NULL;
180   scene.static_actors = NULL;
181
182   get_sun_pos(&alt, &azm);
183   get_sun_screen_pos(alt, azm, &x, &y);
184   fprintf(stderr, "init scene\n");
185   actor = init_object("sun", x, y, 20, 88, 88);
186   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
187   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
188   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
189
190   actor = init_object("sky", 0, 0, 0, 800, 480);
191   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
192   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
193
194   actor = init_object("town", 0, 0, 10, 800, 480);
195   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
196   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
197
198   actor = init_object("cloud1", 400, 150, 2, 200, 150);
199   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
200   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
201
202 }
203
204 static void
205 example_label_home_applet_realize (GtkWidget *widget)
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 void change_actor(GtkWidget * actor)
218 {
219     char * name;
220     gint x, y, daytime, sc;
221     double scale;
222     double alt, azm;
223     name = g_object_get_data(G_OBJECT(actor), "name");
224     fprintf(stderr, "change actor %s\n", name);
225     if (name == "sun"){
226         daytime = get_daytime();
227         if (daytime != TIME_NIGHT){
228             hildon_animation_actor_set_show(actor, 1);
229             get_sun_pos(&alt, &azm);
230             get_sun_screen_pos(alt, azm, &x, &y);
231             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
232         }
233     }
234     
235     if (name == "cloud1"){
236         x = g_object_get_data(G_OBJECT(actor), "x");
237         y = g_object_get_data(G_OBJECT(actor), "y");
238         sc = g_object_get_data(G_OBJECT(actor), "scale");
239         
240         x += 40;
241         y -= 20;
242         sc -= 1;
243         if (x > 600){
244             x = 400;
245             y = 150;
246             sc = 10;
247         }
248         scale = (double)sc / 10;
249         hildon_animation_actor_set_scale(actor, scale, scale);
250         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, scale);
251         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
252         g_object_set_data(G_OBJECT(actor), "x", x);
253         g_object_set_data(G_OBJECT(actor), "y", y);
254         g_object_set_data(G_OBJECT(actor), "scale", sc);
255     }
256
257 }
258
259 static gboolean
260 plugin_on_timeout (gpointer data)
261 {
262   gint daytime = get_daytime();
263   GSList * tmp;
264   fprintf(stderr, "on timeout\n");
265
266   if (scene.daytime == daytime){
267       // change dynamic actors
268       tmp = scene.dynamic_actors;
269   }else {
270       // change static actors
271       tmp = scene.dynamic_actors;
272   } 
273   while (tmp != NULL){
274       change_actor(tmp->data);
275       tmp = g_slist_next(tmp);
276   }
277
278   scene.daytime = daytime;
279
280   //double azm, alt;
281   //get_sun_pos(&alt, &azm);
282 /*
283   GSList * tmp = objects_list;
284   while (tmp != NULL){
285       //processing(tmp->data);
286       
287       str = g_object_get_data(G_OBJECT(tmp->data), "name");
288       fprintf(stderr, "object: %s\n", str);
289       
290       if (str == "sun"){
291         //get_sun_screen_pos(alt, azm, &x, &y);
292         //x = tmp->data
293         //actor_set_position_full(tmp->data, x, y, 20);
294         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
295         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
296         //fprintf(stderr, "x = %d y = %d\n", x, y);
297         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
298         while (child != NULL) {
299             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
300             child = child->next;
301         }
302
303          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
304          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
305              
306       }
307       
308       if (str == "town"){
309           //hildon_animation_actor_set_show(tmp->data, 0);
310       }
311       //a = tmp->data;
312       //fprintf(stderr, "--timeout %s\n", a->name);
313       tmp = g_slist_next(tmp);
314   }
315   */
316   return TRUE; /* keep running this event */
317 }
318
319 static void
320 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
321 {
322   GtkWidget *label;
323   label = gtk_label_new (""); 
324   gtk_widget_show (label);
325   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
326   init_applet_position();
327   init_scene(desktop_plugin);
328   desktop_plugin->timer = g_timeout_add(1000*10, plugin_on_timeout, desktop_plugin);
329 }
330
331 static void
332 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {}
333
334 static void
335 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}