version 0.1 is released
[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 z=%d\n", z); */
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                 actor->filename = newfile;
240                 create_hildon_actor(actor, desktop_plugin);
241
242             }
243             //actor->x = 400;
244             //actor->y = 10;
245            // actor_set_position_full(actor->widget, x, y, actor->z);
246             //probability_sun(actor);
247             //fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
248          }else if (actor->visible){
249             actor->visible = FALSE;
250             fprintf(stderr, "destroy moon \n");
251             destroy_hildon_actor(actor);
252             /* TO DO make moonrise*/
253             actor->time_start_animation = get_next_sunset();
254         } 
255     }
256     
257 }
258
259 void 
260 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
261 {
262     double alt, azm;
263     gint x, y, z, daytime;
264
265     if (actor){
266         daytime = get_daytime();
267         if (daytime != TIME_NIGHT){
268             if (!actor->visible){
269                 actor->visible = TRUE;
270
271                 create_hildon_actor(actor, desktop_plugin);
272             }
273             get_sun_pos(&alt, &azm);
274             get_sun_screen_pos(alt, azm, &x, &y);
275             actor->x = x;
276             actor->y = y;
277             actor_set_position_full(actor->widget, x, y, actor->z);
278             probability_sun(actor);
279          }else if (actor->visible){
280             actor->visible = FALSE;
281             destroy_hildon_actor(actor);
282             actor->time_start_animation = get_next_sunrise();
283         } 
284     }
285     
286 }
287 static void 
288 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
289 {
290     gint x0 = -300, y0 = 225, scale0 = 100,
291          x1 = 800, y1 = 162, scale1 = 130, 
292          x, y, scale;
293     struct timeval tvb;     
294     suseconds_t ms;
295     long sec;
296
297     gettimeofday(&tvb, NULL);
298     
299     gint daytime = get_daytime();
300     ms = tvb.tv_usec;
301     sec = tvb.tv_sec;
302     time_t now = time(NULL);
303     double t;
304 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
305    
306     if (!actor->visible){
307         actor->visible = TRUE;
308         if (daytime == TIME_NIGHT)
309             actor->filename = g_strdup("tram_dark.png");
310         else
311             actor->filename = g_strdup("tram.png");
312         create_hildon_actor(actor, desktop_plugin);
313     }
314     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
315     x = path_line(x0, x1, t);
316     y = path_line(y0, y1, t);
317     scale = path_line(scale0, scale1, t);
318     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
319     actor_set_position_full(actor->widget, x, y, actor->z);
320     hildon_animation_actor_set_scale(actor->widget, (double)scale/100, (double)scale/100);
321     if (t >= 1){
322         /* stop animation */
323         actor->visible = FALSE;
324         destroy_hildon_actor(actor);
325         actor->time_start_animation = now + 5*60;
326     }
327 }
328 void
329 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
330 {
331   GtkWidget *ha = NULL;
332   GdkPixbuf *pixbuf = NULL;
333   GtkWidget *image = NULL;
334   gchar *str;
335
336   /* fprintf(stderr, "create_hildon_actor %s\n", actor->name);*/
337   ha = hildon_animation_actor_new();
338   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
339                         desktop_plugin->priv->theme, actor->filename);
340   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
341                                              actor->width, 
342                                              actor->height, 
343                                              NULL);
344   if (pixbuf){
345       image = gtk_image_new_from_pixbuf (pixbuf);
346       g_object_unref(G_OBJECT(pixbuf));
347   }
348   if (image){
349     g_signal_connect(G_OBJECT(image), "expose_event",
350                            G_CALLBACK(expose_event), pixbuf);
351     gtk_container_add (GTK_CONTAINER (ha), image);
352   }  
353   actor_set_position_full(ha, actor->x, actor->y, actor->z);
354   hildon_animation_actor_set_show (ha, actor->visible);
355   hildon_animation_actor_set_scale(ha, (double)actor->scale/100, (double)actor->scale/100);
356   realize(ha);
357   gtk_widget_show_all(ha);
358
359   //g_object_set_data(G_OBJECT(ha), "name", name);
360   //g_object_set_data(G_OBJECT(ha), "filename", filename);
361   g_object_set_data(G_OBJECT(ha), "image", image);
362   /*
363   g_object_set_data(G_OBJECT(ha), "x", x);
364   g_object_set_data(G_OBJECT(ha), "y", y);
365   g_object_set_data(G_OBJECT(ha), "z", z);
366   g_object_set_data(G_OBJECT(ha), "width", width);
367   g_object_set_data(G_OBJECT(ha), "height", height);
368   g_object_set_data(G_OBJECT(ha), "scale", scale);
369   g_object_set_data(G_OBJECT(ha), "visible", visible);
370   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
371   g_object_set_data(G_OBJECT(ha), "func", pfunc);
372   */
373   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), desktop_plugin);
374   actor->widget = ha;
375 }
376
377
378 void
379 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
380 {
381     GtkWidget *image = NULL;
382     GdkPixbuf *pixbuf = NULL;
383     gchar *str;
384
385     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
386                             desktop_plugin->priv->theme, actor->filename);
387  
388     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
389                                                actor->width, 
390                                                actor->height, 
391                                                NULL);
392     if (pixbuf){
393         image = gtk_image_new_from_pixbuf (pixbuf);
394         g_object_unref(G_OBJECT(pixbuf));
395     }
396     if (image){ 
397         g_signal_connect(G_OBJECT(image), "expose_event",
398                                        G_CALLBACK(expose_event), pixbuf);
399         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
400             gtk_container_remove(actor->widget, g_object_get_data(G_OBJECT(actor->widget), "image"));  
401         }
402         g_object_set_data(G_OBJECT(actor->widget), "image", image);
403         gtk_container_add (GTK_CONTAINER (actor->widget), image);
404         realize(actor->widget);
405         gtk_widget_show_all(actor->widget);
406     }
407
408 }
409
410
411 void 
412 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
413 {
414     gint daytime;
415     gchar *newfile;
416     if (!actor) return;
417     daytime = get_daytime();
418     newfile = g_strdup_printf("%s%d.png", actor->name, daytime); 
419     actor->filename = newfile;
420     change_hildon_actor(actor, desktop_plugin);
421 }
422
423
424
425
426 static gint 
427 get_time(gint t){
428     // уравнение изменения времени
429     return t*1.1;
430 }
431
432 static void 
433 destroy_scene(void){
434     GSList * tmp = scene.actors;
435     Actor *actor;
436     while (tmp != NULL){
437         actor = tmp->data;
438         if (actor){
439             gtk_widget_destroy(actor->widget);
440             //actor->widget = NULL;
441             g_free(actor);
442         }
443         tmp = g_slist_next(tmp);
444     }
445     g_slist_free(tmp);
446
447 }
448
449 void
450 reload_scene(AWallpaperPlugin *desktop_plugin)
451 {
452     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
453     destroy_scene();
454     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
455         init_scene(desktop_plugin);
456     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
457         init_scene1(desktop_plugin);
458 }
459
460 void 
461 probability_sun(Actor *actor)
462 {
463     actor->time_start_animation += 20 * 1;
464     actor->duration_animation = G_MAXINT;
465 }
466 /* Init Modern Scene */
467 static void
468 init_scene(AWallpaperPlugin *desktop_plugin)
469 {
470   Actor *actor;
471
472   /* fprintf(stderr, "init scene \n");*/
473   scene.daytime = get_daytime();
474   scene.actors = NULL;
475
476   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 
477                       TRUE, 100, 255, &change_static_actor, NULL);
478   change_static_actor(actor, desktop_plugin);
479   scene.actors = g_slist_append(scene.actors, actor);
480
481   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
482                       TRUE, 100, 255, &change_sun, &probability_sun);
483   actor->time_start_animation = time(NULL);
484   actor->duration_animation = G_MAXINT;
485   change_sun(actor, desktop_plugin);
486   scene.actors = g_slist_append(scene.actors, actor);
487
488   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
489                       TRUE, 100, 255, NULL, NULL);
490   scene.actors = g_slist_append(scene.actors, actor);
491
492
493   actor = init_object(desktop_plugin, "town", "town0.png", 0, 0, 7, 800, 480, 
494                       TRUE, 100, 255, &change_static_actor, NULL);
495   change_static_actor(actor, desktop_plugin);
496   scene.actors = g_slist_append(scene.actors, actor);
497
498
499   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, 10, 60, 60, 
500                       FALSE, 100, 255, &change_moon, NULL);
501   change_moon(actor, desktop_plugin);
502   scene.actors = g_slist_append(scene.actors, actor);
503
504   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, 8, 350, 210, 
505                       FALSE, 100, 255, &change_tram, NULL);
506   actor->time_start_animation = time(NULL) + 10;
507   actor->duration_animation = 60;
508   scene.actors = g_slist_append(scene.actors, actor);
509
510   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 9, 800, 79,
511                       TRUE, 100, 255, &change_static_actor, NULL);
512   change_static_actor(actor, desktop_plugin);
513   scene.actors = g_slist_append(scene.actors, actor);
514 #if 0    
515   anim = g_new0(Animation, 1);
516   anim->count = 1;
517   anim->actor = actor;
518   anim->func_change = &change_tram;
519   anim->func_time = NULL;
520   anim->timestart = time(NULL); 
521   anim->timeall = 10;
522   
523   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
524 #endif  
525  }
526
527 /* Init Berlin Scene */
528 static void
529 init_scene1(AWallpaperPlugin *desktop_plugin)
530 {
531   Actor *actor;
532
533   scene.daytime = get_daytime();
534   scene.actors = NULL;
535   
536   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
537                       TRUE, 100, 255, &change_static_actor, NULL);
538   change_static_actor(actor, desktop_plugin);
539   scene.actors = g_slist_append(scene.actors, actor);
540
541   
542   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
543                       TRUE, 100, 255, &change_sun, &probability_sun);
544   actor->time_start_animation = time(NULL);
545   actor->duration_animation = G_MAXINT;
546   change_sun(actor, desktop_plugin);
547   scene.actors = g_slist_append(scene.actors, actor);
548  
549   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 9, 50, 50, 
550                       TRUE, 100, 255, NULL, NULL);
551   scene.actors = g_slist_append(scene.actors, actor);
552
553
554   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 7, 800, 480, 
555                       TRUE, 100, 255, &change_static_actor, NULL);
556   change_static_actor(actor, desktop_plugin);
557   scene.actors = g_slist_append(scene.actors, actor);
558
559   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 8, 60, 60, 
560                       FALSE, 100, 255, &change_moon, NULL);
561   change_moon(actor, desktop_plugin);
562   scene.actors = g_slist_append(scene.actors, actor);
563   //scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
564  
565    /*
566   actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
567   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
568
569   actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
570   list = g_slist_append(list, G_OBJECT(actor));
571   actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
572   list = g_slist_append(list, G_OBJECT(actor));
573
574   ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
575   //objects_list = g_slist_append(objects_list, G_OBJECT(ma)); 
576   */
577
578 }
579
580
581 void 
582 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
583 {
584     gint y0 = 365;// - уровень горизонта
585     gint o_width = 128,
586          o_height = 128; 
587     *x = (int)(azm * 800) - 64;
588     *y = (int)((1 - alt) * y0) - 64;
589     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
590 }
591 #if 0
592 static void 
593 change_actor(GtkWidget * actor)
594 {
595     char * name;
596     gint x, y, daytime, scale;
597     gdouble sc;
598     double alt, azm;
599
600     GtkWidget *image;
601     GdkPixbuf *pixbuf;
602
603     void (*pfunc)(gpointer, gpointer);
604
605     name = g_object_get_data(G_OBJECT(actor), "name");
606     fprintf(stderr, "change actor %s\n", name);
607     if (name == "sun"){
608         pfunc = g_object_get_data(G_OBJECT(actor), "func");
609         if (pfunc)
610             (*pfunc)(actor, g_strdup(name));
611         daytime = get_daytime();
612         if (daytime != TIME_NIGHT){
613             hildon_animation_actor_set_show(actor, 1);
614             get_sun_pos(&alt, &azm);
615             get_sun_screen_pos(alt, azm, &x, &y);
616             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
617         }
618     }
619     
620     if (name == "cloud1"){
621         x = g_object_get_data(G_OBJECT(actor), "x");
622         y = g_object_get_data(G_OBJECT(actor), "y");
623         scale = g_object_get_data(G_OBJECT(actor), "scale");
624
625         /* Start */
626         image = g_object_get_data(G_OBJECT(actor), "image");
627         
628         gtk_container_remove(actor, image);  
629         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
630                                              200, 
631                                              200, 
632                                              NULL);
633         if (pixbuf){
634               image = gtk_image_new_from_pixbuf (pixbuf);
635               g_object_unref(G_OBJECT(pixbuf));
636         }
637         g_signal_connect(G_OBJECT(image), "expose_event",
638                                    G_CALLBACK(expose_event), pixbuf);
639         gtk_container_add (GTK_CONTAINER (actor), image);
640         realize(actor);
641         gtk_widget_show_all(actor);
642         /* End*/
643
644             
645         x += 40;
646         y -= 20;
647         scale -= 10;
648         if (x > 500){
649             x = 400;
650             y = 150;
651             sc = 1;
652         }
653         sc = (double)scale / 100;
654         hildon_animation_actor_set_scale(actor, sc, sc);
655         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
656         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
657         g_object_set_data(G_OBJECT(actor), "x", x);
658         g_object_set_data(G_OBJECT(actor), "y", y);
659         g_object_set_data(G_OBJECT(actor), "scale", scale);
660     }
661
662 }
663 #endif
664 static gboolean
665 short_timeout (AWallpaperPlugin *desktop_plugin)
666 {
667       gint daytime = get_daytime();
668       GSList * tmp;
669       gchar * name;
670       void (*pfunc)(gpointer, gpointer);
671       time_t now;
672       gint t;
673       Actor *actor;
674       gboolean stop_flag = TRUE;
675
676     if (!desktop_plugin->priv->visible)
677         return TRUE;
678
679     now = time(NULL);
680     /* fprintf(stderr, "Short timer %d\n", now); */
681     tmp = scene.actors;
682     while (tmp != NULL){
683            actor = tmp->data;
684            if (now >= actor->time_start_animation  
685                && actor->time_start_animation > 0
686                /* && now - actor->time_start_animation <= actor->duration_animation*/){
687                 pfunc = actor->func_change;
688                 if (pfunc){ 
689                     (*pfunc)(actor, desktop_plugin);
690                     stop_flag = FALSE;
691                 }
692             }
693             tmp = g_slist_next(tmp);
694     }
695     scene.daytime = daytime;
696     if (stop_flag){
697          desktop_plugin->priv->short_timer = 0;
698          return FALSE;
699     }else
700          return TRUE; /* keep running this event */
701 }
702
703
704 static gboolean
705 long_timeout (AWallpaperPlugin *desktop_plugin)
706 {
707       gint daytime = get_daytime();
708       GSList * tmp;
709       gchar * name;
710       void (*pfunc)(gpointer, gpointer);
711       time_t now;
712       gint t;
713       Actor *actor;
714
715     /* TODO  remove timeout */
716     if (!desktop_plugin->priv->visible)
717         return TRUE;
718   //fprintf(stderr, "timer daytime=%d\n", daytime);
719   if (scene.daytime != daytime){
720       tmp = scene.actors;
721       while (tmp != NULL){
722           //change_actor(tmp->data);
723           pfunc =((Actor*)tmp->data)->func_change;
724           if (pfunc){
725               (*pfunc)(tmp->data, desktop_plugin);
726           }
727           tmp = g_slist_next(tmp);
728       }
729    }
730    
731    now = time(NULL);
732 //fprintf(stderr, "Now  %d\n", now);
733    tmp = scene.actors;
734    while (tmp != NULL){
735        actor = tmp->data;
736        if (now >= actor->time_start_animation  
737            && actor->time_start_animation > 0
738            && desktop_plugin->priv->short_timer == 0){
739             actor->time_start_animation = now;
740             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
741             desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, short_timeout, desktop_plugin);
742         }
743         tmp = g_slist_next(tmp);
744    }
745    
746  scene.daytime = daytime;
747
748  return TRUE; /* keep running this event */
749 }
750
751 static void
752 desktop_plugin_visible_notify (GObject    *object,
753                                           GParamSpec *spec,
754                                           AWallpaperPlugin *desktop_plugin)
755 {
756       gboolean visible;
757       g_object_get (object, "is-on-current-desktop", &visible, NULL);
758       if (visible)
759         desktop_plugin->priv->visible = TRUE;
760       else
761         desktop_plugin->priv->visible = FALSE;
762    /*   fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
763 }
764
765 static void
766 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
767 {
768     GtkWidget *label;
769
770     fprintf(stderr, "!!!!!!!plugin init \n");
771     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
772     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
773     /* Load config */
774     read_config(priv);
775     priv->desktop_plugin = desktop_plugin;
776     label = gtk_label_new (""); 
777     gtk_widget_set_size_request(label, 95, 30);
778     gtk_widget_show (label);
779     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
780     g_signal_connect (desktop_plugin, "show-settings",
781                              G_CALLBACK (lw_settings), priv);
782     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
783                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
784
785     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
786     init_applet_position();
787     fprintf(stderr, "!!!theme = %s\n", priv->theme);
788     if (!strcmp(priv->theme,"Modern"))
789         init_scene(desktop_plugin);
790     else if (!strcmp(priv->theme,"Berlin")) 
791         init_scene1(desktop_plugin);
792     priv->long_timer = g_timeout_add(LONG_TIMER, long_timeout, desktop_plugin);
793     priv->short_timer = 0;
794     /* TODO Move scene to priv */
795     scene.timer_type = LONG_TIMER_TYPE;
796 }
797
798 static void
799 lw_applet_finalize (GObject *object)
800 {
801      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
802      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
803
804      if (priv->long_timer){
805         g_source_remove(priv->long_timer);
806         priv->long_timer = 0;
807      }
808      if (priv->short_timer){
809         g_source_remove(priv->short_timer);
810         priv->short_timer = 0;
811      }
812
813      destroy_scene();
814 }
815
816 static void
817 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
818
819     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
820     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
821
822     /* gobject */
823     gobject_class->destroy = lw_applet_finalize;
824     widget_class->realize = lw_applet_realize;
825     widget_class->expose_event = lw_applet_expose_event;
826
827     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
828
829 }
830
831 static void
832 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}