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