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