1ce9127e71deaf43a9cad94858066c3114e94613
[livewp] / applet / src / livewp-home-widget.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include "livewp-common.h" 
26 #include "livewp-home-widget.h"
27 #include <gconf/gconf-client.h>
28 #include "livewp-rules.h"
29 #include <sys/time.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 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
38                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
39                                                           Animation_WallpaperPrivate))
40
41 /* Position of plugin on desktop */
42 #define Xstartposition 700 
43 #define Ystartposition 425 
44
45 gint xapplet = 0, yapplet = 0;
46 GSList * objects_list = NULL;
47 Scene scene;
48
49 static void
50 lw_applet_realize (GtkWidget *widget)
51 {
52       GdkScreen *screen;
53
54       screen = gtk_widget_get_screen (widget);
55       gtk_widget_set_colormap (widget,
56                                 gdk_screen_get_rgba_colormap (screen));
57       gtk_widget_set_app_paintable (widget,
58                                 TRUE);
59       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
60 }
61
62
63 static gboolean
64 lw_applet_expose_event(GtkWidget      *widget,
65                                         GdkEventExpose *event)
66 {
67   cairo_t *cr;
68
69   /* Create cairo context */
70   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
71   gdk_cairo_region (cr, event->region);
72   cairo_clip (cr);
73
74   /* Draw alpha background */
75   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
76   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
77   cairo_paint (cr);
78
79   /* Free context */
80   cairo_destroy (cr);
81
82   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
83                                                                                   event);
84 }
85
86 static gboolean
87 expose_event (GtkWidget *widget,GdkEventExpose *event,
88      gpointer data)
89 {
90     cairo_t *cr;
91     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
92         
93     cr = gdk_cairo_create(widget->window);
94     if (cr){
95         gdk_cairo_region(cr, event->region);
96         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
97         gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
98         cairo_paint(cr);
99         cairo_destroy(cr);
100     }
101     return TRUE;
102 }
103
104 static void
105 realize (GtkWidget *widget)
106 {
107     GdkScreen *screen;
108     screen = gtk_widget_get_screen (widget);
109     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
110 }
111
112 /* Set position of widget on desktop */
113 static void
114 init_applet_position(void)
115 {
116   GSList *position = NULL;
117   gchar *position_key;
118   gchar *modified_key;
119   gchar *modified;
120   GError *error = NULL;
121   GConfClient   *gconf_client = gconf_client_get_default ();
122   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
123   position = gconf_client_get_list (gconf_client,
124                                     position_key,
125                                     GCONF_VALUE_INT,
126                                     NULL);
127   if (position && position->data && position->next->data){
128         xapplet = GPOINTER_TO_INT (position->data);
129         yapplet = GPOINTER_TO_INT (position->next->data);
130   }else{
131         position = g_slist_prepend (g_slist_prepend (NULL,
132                                       GINT_TO_POINTER (Ystartposition)),
133                                       GINT_TO_POINTER (Xstartposition));
134         gconf_client_set_list (gconf_client,
135                                position_key,
136                                GCONF_VALUE_INT,
137                                position,
138                                &error);
139         xapplet = Xstartposition;
140         yapplet = Ystartposition;
141   }
142   g_free (position_key);
143   modified = g_strdup_printf ("%ld", 0);
144   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
145   gconf_client_set_string (gconf_client,
146                            modified_key,
147                            modified,
148                            &error);
149   g_free(modified);
150   g_free(modified_key);
151   gconf_client_clear_cache(gconf_client);
152   g_object_unref(gconf_client);
153 }
154
155 void
156 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
157 {
158 // fprintf(stderr, "actor_set_position_full\n");
159  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
160 }
161
162 static gint 
163 path_line(gint x0, gint x1, double t)
164 {
165     // уравниение прямой
166     return ((x1 - x0) * t + x0);
167 }
168
169
170 void
171 destroy_hildon_actor(Actor *actor)
172 {
173     fprintf(stderr, "estroy_hildon_actor %p\n",actor->widget);
174     gtk_widget_destroy(actor->widget);
175     actor->widget = NULL;
176 }
177
178 static Actor* 
179 init_object(AWallpaperPlugin *desktop_plugin, 
180             gchar * name, 
181             gchar * filename, 
182             gint x, 
183             gint y, 
184             gint z, 
185             gint width, 
186             gint height, 
187             gboolean visible, 
188             gint scale, 
189             gint opacity, 
190             void (*pfunc_change)(Actor*),
191             void (*pfunc_probability)(Actor*)
192            )
193 {
194   Actor *actor = NULL;
195   actor = g_new0(Actor, 1);
196   actor->x = x;
197   actor->y = y;
198   actor->z = z;
199   actor->width = width;
200   actor->height = height;
201   actor->visible = visible;
202   actor->scale = scale;
203   actor->opacity = opacity;
204   actor->filename = g_strdup(filename);
205   actor->name = g_strdup(name);
206   actor->func_change = pfunc_change; 
207   actor->func_probability = pfunc_probability;
208   if (visible)
209     create_hildon_actor(actor, desktop_plugin);
210   else 
211     actor->widget = NULL;
212   actor->time_start_animation = 0;
213   actor->duration_animation = 0;
214   /*
215   a.widget = actor;
216   a.name = name;
217   a.x = x;
218   a.y = y;
219   a.z = z;
220   */
221   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
222   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
223   return actor;
224 }
225
226 void 
227 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
228 {
229     gint x, y, z, daytime, phase;
230     char *newfile;
231
232     if (actor){
233         daytime = get_daytime();
234         if (daytime == TIME_NIGHT){
235             if (!actor->visible){
236                 actor->visible = TRUE;
237                 phase = get_moon_phase();
238                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
239                 
240                 actor->filename = newfile;
241                 create_hildon_actor(actor, desktop_plugin);
242
243             }
244             //actor->x = 400;
245             //actor->y = 10;
246            // actor_set_position_full(actor->widget, x, y, actor->z);
247             //probability_sun(actor);
248             //fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
249          }else if (actor->visible){
250             actor->visible = FALSE;
251             fprintf(stderr, "destroy moon \n");
252             destroy_hildon_actor(actor);
253             /* TO DO make moonrise*/
254             actor->time_start_animation = get_next_sunset();
255         } 
256     }
257     
258 }
259
260 void 
261 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
262 {
263     double alt, azm;
264     gint x, y, z, daytime;
265
266     if (actor){
267         daytime = get_daytime();
268         if (daytime != TIME_NIGHT){
269             if (!actor->visible){
270                 actor->visible = TRUE;
271
272                 create_hildon_actor(actor, desktop_plugin);
273             }
274             get_sun_pos(&alt, &azm);
275             get_sun_screen_pos(alt, azm, &x, &y);
276             actor->x = x;
277             actor->y = y;
278             actor_set_position_full(actor->widget, x, y, actor->z);
279             probability_sun(actor);
280          }else if (actor->visible){
281             actor->visible = FALSE;
282             destroy_hildon_actor(actor);
283             actor->time_start_animation = get_next_sunrise();
284         } 
285     }
286     
287 }
288 static void 
289 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
290 {
291     gint x0 = -300, y0 = 225, scale0 = 100,
292          x1 = 800, y1 = 162, scale1 = 130, 
293          x, y, scale;
294     struct timeval tvb;     
295     suseconds_t ms;
296     long sec;
297
298     gettimeofday(&tvb, NULL);
299     
300     gint daytime = get_daytime();
301     ms = tvb.tv_usec;
302     sec = tvb.tv_sec;
303     time_t now = time(NULL);
304     double t;
305 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
306    
307     if (!actor->visible){
308         actor->visible = TRUE;
309         if (daytime == TIME_NIGHT)
310             actor->filename = g_strdup("tram_dark.png");
311         else
312             actor->filename = g_strdup("tram.png");
313         create_hildon_actor(actor, desktop_plugin);
314     }
315     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
316     x = path_line(x0, x1, t);
317     y = path_line(y0, y1, t);
318     scale = path_line(scale0, scale1, t);
319     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
320     actor_set_position_full(actor->widget, x, y, actor->z);
321     hildon_animation_actor_set_scale(actor->widget, (double)scale/100, (double)scale/100);
322     if (t >= 1){
323         /* stop animation */
324         actor->visible = FALSE;
325         destroy_hildon_actor(actor);
326         actor->time_start_animation = now + 5*60;
327     }
328 }
329 void
330 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
331 {
332   GtkWidget *ha = NULL;
333   GdkPixbuf *pixbuf = NULL;
334   GtkWidget *image = NULL;
335   gchar *str;
336
337   fprintf(stderr, "create_hildon_actor %s\n", actor->name);
338   ha = hildon_animation_actor_new();
339   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
340                         desktop_plugin->priv->theme, actor->filename);
341   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
342                                              actor->width, 
343                                              actor->height, 
344                                              NULL);
345   if (pixbuf){
346       image = gtk_image_new_from_pixbuf (pixbuf);
347       g_object_unref(G_OBJECT(pixbuf));
348   }
349   if (image){
350     g_signal_connect(G_OBJECT(image), "expose_event",
351                            G_CALLBACK(expose_event), pixbuf);
352     gtk_container_add (GTK_CONTAINER (ha), image);
353   }  
354   actor_set_position_full(ha, actor->x, actor->y, actor->z);
355   hildon_animation_actor_set_show (ha, actor->visible);
356   hildon_animation_actor_set_scale(ha, (double)actor->scale/100, (double)actor->scale/100);
357   realize(ha);
358   gtk_widget_show_all(ha);
359
360   //g_object_set_data(G_OBJECT(ha), "name", name);
361   //g_object_set_data(G_OBJECT(ha), "filename", filename);
362   g_object_set_data(G_OBJECT(ha), "image", image);
363   /*
364   g_object_set_data(G_OBJECT(ha), "x", x);
365   g_object_set_data(G_OBJECT(ha), "y", y);
366   g_object_set_data(G_OBJECT(ha), "z", z);
367   g_object_set_data(G_OBJECT(ha), "width", width);
368   g_object_set_data(G_OBJECT(ha), "height", height);
369   g_object_set_data(G_OBJECT(ha), "scale", scale);
370   g_object_set_data(G_OBJECT(ha), "visible", visible);
371   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
372   g_object_set_data(G_OBJECT(ha), "func", pfunc);
373   */
374   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), desktop_plugin);
375       fprintf(stderr, "ha %p\n", ha);
376   actor->widget = ha;
377 }
378
379
380 void
381 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
382 {
383     GtkWidget *image = NULL;
384     GdkPixbuf *pixbuf = NULL;
385     gchar *str;
386
387     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
388                             desktop_plugin->priv->theme, actor->filename);
389  
390     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
391                                                actor->width, 
392                                                actor->height, 
393                                                NULL);
394     if (pixbuf){
395         image = gtk_image_new_from_pixbuf (pixbuf);
396         g_object_unref(G_OBJECT(pixbuf));
397     }
398     if (image){ 
399         g_signal_connect(G_OBJECT(image), "expose_event",
400                                        G_CALLBACK(expose_event), pixbuf);
401         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
402             gtk_container_remove(actor->widget, g_object_get_data(G_OBJECT(actor->widget), "image"));  
403         }
404         g_object_set_data(G_OBJECT(actor->widget), "image", image);
405         gtk_container_add (GTK_CONTAINER (actor->widget), image);
406         realize(actor->widget);
407         gtk_widget_show_all(actor->widget);
408     }
409
410 }
411
412
413 void 
414 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
415 {
416     gint daytime;
417     gchar *newfile;
418     if (!actor) return;
419     daytime = get_daytime();
420     newfile = g_strdup_printf("%s%d.png", actor->name, daytime); 
421     actor->filename = newfile;
422     change_hildon_actor(actor, desktop_plugin);
423 }
424
425
426
427 void change_sky(GtkWidget * actor, gpointer data)
428 {
429     gint daytime, opacity;
430     //fprintf(stderr, "change sky \n");
431     if (actor){
432         daytime = get_daytime();
433         opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 10;
434         if (opacity < 0) opacity = 255;
435         hildon_animation_actor_set_show_full(actor, 1, opacity);
436         g_object_set_data(G_OBJECT(actor), "opacity", opacity);
437         switch (daytime){
438             case TIME_NIGHT:
439                 //fprintf(stderr, "change sky night\n");
440             break;
441             case TIME_DAY:
442                 //fprintf(stderr, "change sky day\n");
443             break;
444
445         }
446     }
447 }
448 void 
449 change_background(GtkWidget * actor, gpointer data)
450 {
451     gint opacity;
452     if (actor){
453         opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 1;
454         if (opacity < 0) opacity = 255;
455         hildon_animation_actor_set_show_full(actor, 1, opacity);
456         g_object_set_data(G_OBJECT(actor), "opacity", opacity);
457
458     }
459 }
460
461 static gint 
462 get_time(gint t){
463     // уравнение изменения времени
464     return t*1.1;
465 }
466
467 static void 
468 destroy_scene(void){
469     GSList * tmp = scene.actors;
470     Actor *actor;
471     while (tmp != NULL){
472         actor = tmp->data;
473         if (actor){
474             gtk_widget_destroy(actor->widget);
475             //actor->widget = NULL;
476             g_free(actor);
477         }
478         tmp = g_slist_next(tmp);
479     }
480     g_slist_free(tmp);
481
482 }
483
484 void
485 reload_scene(AWallpaperPlugin *desktop_plugin)
486 {
487     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
488     destroy_scene();
489     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
490         init_scene(desktop_plugin);
491     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
492         init_scene1(desktop_plugin);
493 }
494
495 void 
496 probability_sun(Actor *actor)
497 {
498     actor->time_start_animation += 20 * 1;
499     actor->duration_animation = G_MAXINT;
500 }
501
502 static void
503 init_scene(AWallpaperPlugin *desktop_plugin)
504 {
505   Actor *actor;
506
507 fprintf(stderr, "init scene \n");
508   scene.daytime = get_daytime();
509   scene.actors = NULL;
510   //get_sun_pos(&alt, &azm);
511   //get_sun_screen_pos(alt, azm, &x, &y);
512   
513  
514   //scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
515   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, SKY_LEVEL, 800, 480, 
516                       TRUE, 100, 255, &change_static_actor, NULL);
517   change_static_actor(actor, desktop_plugin);
518   scene.actors = g_slist_append(scene.actors, actor);
519
520   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, SUN_LEVEL, 88, 88, 
521                       TRUE, 100, 255, &change_sun, &probability_sun);
522   actor->time_start_animation = time(NULL);
523   actor->duration_animation = G_MAXINT;
524   change_sun(actor, desktop_plugin);
525   scene.actors = g_slist_append(scene.actors, actor);
526
527   actor = init_object(desktop_plugin, "town", "town0.png", 0, 480-374, TOWN_LEVEL, 800, 374, 
528                       TRUE, 100, 255, &change_static_actor, NULL);
529   change_static_actor(actor, desktop_plugin);
530   scene.actors = g_slist_append(scene.actors, actor);
531   
532   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, TOWN_LEVEL+5, 800, 79, 
533                       TRUE, 100, 255, &change_static_actor, NULL);
534   change_static_actor(actor, desktop_plugin);
535   scene.actors = g_slist_append(scene.actors, actor);
536
537   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, SUN_LEVEL, 60, 60, 
538                       FALSE, 100, 255, &change_moon, NULL);
539   change_moon(actor, desktop_plugin);
540   scene.actors = g_slist_append(scene.actors, actor);
541
542
543   //if (scene.daytime == TIME_DAY) snprintf(str, 255, "tram.png");
544   //else snprintf(str, 255, "tram_dark.png");
545   //actor = init_object("tram", "tram.png", -300, 191, 25, 350, 210, 1, 100, 255, NULL);
546   
547   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, TRAM_LEVEL, 350, 210, 
548                       FALSE, 100, 255, &change_tram, NULL);
549   actor->time_start_animation = time(NULL) + 10;
550   actor->duration_animation = 60;
551   scene.actors = g_slist_append(scene.actors, actor);
552 #if 0    
553   anim = g_new0(Animation, 1);
554   anim->count = 1;
555   anim->actor = actor;
556   anim->func_change = &change_tram;
557   anim->func_time = NULL;
558   anim->timestart = time(NULL); 
559   anim->timeall = 10;
560   
561   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
562 #endif  
563  }
564
565 static void
566 init_scene1(AWallpaperPlugin *desktop_plugin)
567 {
568   Actor *actor;
569
570   scene.daytime = get_daytime();
571   scene.actors = NULL;
572   
573   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, SUN_LEVEL, 88, 88, 
574                       TRUE, 100, 255, &change_sun, &probability_sun);
575   actor->time_start_animation = time(NULL);
576   actor->duration_animation = G_MAXINT;
577   change_sun(actor, desktop_plugin);
578   scene.actors = g_slist_append(scene.actors, actor);
579  
580   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, SKY_LEVEL, 800, 480, 
581                       TRUE, 100, 255, &change_static_actor, NULL);
582   change_static_actor(actor, desktop_plugin);
583   scene.actors = g_slist_append(scene.actors, actor);
584
585
586   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, TOWN_LEVEL, 800, 480, 
587                       TRUE, 100, 255, &change_static_actor, NULL);
588   change_static_actor(actor, desktop_plugin);
589   scene.actors = g_slist_append(scene.actors, actor);
590
591   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, SUN_LEVEL, 60, 60, 
592                       FALSE, 100, 255, &change_moon, NULL);
593   change_moon(actor, desktop_plugin);
594   scene.actors = g_slist_append(scene.actors, actor);
595
596   //scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
597  
598    /*
599   actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
600   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
601
602   actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
603   list = g_slist_append(list, G_OBJECT(actor));
604   actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
605   list = g_slist_append(list, G_OBJECT(actor));
606
607   ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
608   //objects_list = g_slist_append(objects_list, G_OBJECT(ma)); 
609   */
610
611 }
612
613
614 void 
615 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
616 {
617     gint y0 = 365;// - уровень горизонта
618     gint o_width = 128,
619          o_height = 128; 
620     *x = (int)(azm * 800) - 64;
621     *y = (int)((1 - alt) * y0) - 64;
622     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
623 }
624 #if 0
625 static void 
626 change_actor(GtkWidget * actor)
627 {
628     char * name;
629     gint x, y, daytime, scale;
630     gdouble sc;
631     double alt, azm;
632
633     GtkWidget *image;
634     GdkPixbuf *pixbuf;
635
636     void (*pfunc)(gpointer, gpointer);
637
638     name = g_object_get_data(G_OBJECT(actor), "name");
639     fprintf(stderr, "change actor %s\n", name);
640     if (name == "sun"){
641         pfunc = g_object_get_data(G_OBJECT(actor), "func");
642         if (pfunc)
643             (*pfunc)(actor, g_strdup(name));
644         daytime = get_daytime();
645         if (daytime != TIME_NIGHT){
646             hildon_animation_actor_set_show(actor, 1);
647             get_sun_pos(&alt, &azm);
648             get_sun_screen_pos(alt, azm, &x, &y);
649             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
650         }
651     }
652     
653     if (name == "cloud1"){
654         x = g_object_get_data(G_OBJECT(actor), "x");
655         y = g_object_get_data(G_OBJECT(actor), "y");
656         scale = g_object_get_data(G_OBJECT(actor), "scale");
657
658         /* Start */
659         image = g_object_get_data(G_OBJECT(actor), "image");
660         
661         gtk_container_remove(actor, image);  
662         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
663                                              200, 
664                                              200, 
665                                              NULL);
666         if (pixbuf){
667               image = gtk_image_new_from_pixbuf (pixbuf);
668               g_object_unref(G_OBJECT(pixbuf));
669         }
670         g_signal_connect(G_OBJECT(image), "expose_event",
671                                    G_CALLBACK(expose_event), pixbuf);
672         gtk_container_add (GTK_CONTAINER (actor), image);
673         realize(actor);
674         gtk_widget_show_all(actor);
675         /* End*/
676
677             
678         x += 40;
679         y -= 20;
680         scale -= 10;
681         if (x > 500){
682             x = 400;
683             y = 150;
684             sc = 1;
685         }
686         sc = (double)scale / 100;
687         hildon_animation_actor_set_scale(actor, sc, sc);
688         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
689         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
690         g_object_set_data(G_OBJECT(actor), "x", x);
691         g_object_set_data(G_OBJECT(actor), "y", y);
692         g_object_set_data(G_OBJECT(actor), "scale", scale);
693     }
694
695 }
696 #endif
697 static gboolean
698 short_timeout (AWallpaperPlugin *desktop_plugin)
699 {
700       gint daytime = get_daytime();
701       GSList * tmp;
702       gchar * name;
703       void (*pfunc)(gpointer, gpointer);
704       time_t now;
705       gint t;
706       Actor *actor;
707       gboolean stop_flag = TRUE;
708  
709     if (!desktop_plugin->priv->visible)
710         return TRUE;
711   
712     now = time(NULL);
713     //fprintf(stderr, "Short timer %d\n", now);
714     tmp = scene.actors;
715     while (tmp != NULL){
716            actor = tmp->data;
717            if (now >= actor->time_start_animation  
718                && actor->time_start_animation > 0
719                /* && now - actor->time_start_animation <= actor->duration_animation*/){
720                 pfunc = actor->func_change;
721                 if (pfunc){ 
722                     (*pfunc)(actor, desktop_plugin);
723                     stop_flag = FALSE;
724                 }
725             }
726             tmp = g_slist_next(tmp);
727     }
728        
729     scene.daytime = daytime;
730     if (stop_flag){
731          desktop_plugin->priv->short_timer = 0;
732          return FALSE;
733     }else
734          return TRUE; /* keep running this event */
735 }
736
737
738 static gboolean
739 long_timeout (AWallpaperPlugin *desktop_plugin)
740 {
741       gint daytime = get_daytime();
742       GSList * tmp;
743       gchar * name;
744       void (*pfunc)(gpointer, gpointer);
745       time_t now;
746       gint t;
747       Actor *actor;
748
749     /* TODO  remove timeout */
750     if (!desktop_plugin->priv->visible)
751         return TRUE;
752   //fprintf(stderr, "timer daytime=%d\n", daytime);
753   if (scene.daytime != daytime){
754       tmp = scene.actors;
755       while (tmp != NULL){
756           //change_actor(tmp->data);
757           pfunc =((Actor*)tmp->data)->func_change;
758           if (pfunc){
759               (*pfunc)(tmp->data, desktop_plugin);
760           }
761           tmp = g_slist_next(tmp);
762       }
763    }
764    
765    now = time(NULL);
766 //fprintf(stderr, "Now  %d\n", now);
767    tmp = scene.actors;
768    while (tmp != NULL){
769        actor = tmp->data;
770        if (now >= actor->time_start_animation  
771            && actor->time_start_animation > 0
772            && desktop_plugin->priv->short_timer == 0){
773             actor->time_start_animation = now;
774             fprintf(stderr, "start shor timer act = %s %d\n", actor->name, now);
775             desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, short_timeout, desktop_plugin);
776         }
777         tmp = g_slist_next(tmp);
778    }
779    
780  scene.daytime = daytime;
781
782  return TRUE; /* keep running this event */
783 }
784
785 static void
786 desktop_plugin_visible_notify (GObject    *object,
787                                           GParamSpec *spec,
788                                           AWallpaperPlugin *desktop_plugin)
789 {
790       gboolean visible;
791       g_object_get (object, "is-on-current-desktop", &visible, NULL);
792       if (visible)
793         desktop_plugin->priv->visible = TRUE;
794       else
795         desktop_plugin->priv->visible = FALSE;
796    /*   fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
797 }
798
799 static void
800 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
801 {
802     GtkWidget *label;
803
804     fprintf(stderr, "!!!!!!!plugin init \n");
805     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
806     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
807     /* Load config */
808     read_config(priv);
809     priv->desktop_plugin = desktop_plugin;
810     label = gtk_label_new (""); 
811     gtk_widget_set_size_request(label, 95, 30);
812     gtk_widget_show (label);
813     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
814     g_signal_connect (desktop_plugin, "show-settings",
815                              G_CALLBACK (lw_settings), priv);
816     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
817                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
818
819     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
820     init_applet_position();
821     fprintf(stderr, "!!!theme = %s\n", priv->theme);
822     if (!strcmp(priv->theme,"Modern"))
823         init_scene(desktop_plugin);
824     else if (!strcmp(priv->theme,"Berlin")) 
825         init_scene1(desktop_plugin);
826     priv->long_timer = g_timeout_add(LONG_TIMER, long_timeout, desktop_plugin);
827     priv->short_timer = 0;
828     /* TODO Move scene to priv */
829     scene.timer_type = LONG_TIMER_TYPE;
830 }
831
832 static void
833 lw_applet_finalize (GObject *object)
834 {
835      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
836      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
837
838      if (priv->long_timer){
839         g_source_remove(priv->long_timer);
840         priv->long_timer = 0;
841      }
842      if (priv->short_timer){
843         g_source_remove(priv->short_timer);
844         priv->short_timer = 0;
845      }
846
847      destroy_scene();
848 }
849
850 static void
851 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
852
853     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
854     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
855
856     /* gobject */
857     gobject_class->destroy = lw_applet_finalize;
858     widget_class->realize = lw_applet_realize;
859     widget_class->expose_event = lw_applet_expose_event;
860
861     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
862
863 }
864
865 static void
866 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}