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