8ad44f519ad1bf06100e9be0303c533b32908231
[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 #include <stdlib.h>
31
32 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
33 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
34 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
35 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
36
37 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
38 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
39                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
40                                                           Animation_WallpaperPrivate))
41
42 /* Position of plugin on desktop */
43 #define Xstartposition 700 
44 #define Ystartposition 448 
45
46 gint xapplet = 0, yapplet = 0;
47 Scene scene;
48 gint hour=8;
49 gint min=0;
50 gint ttt = 0;
51
52 static void
53 lw_applet_realize (GtkWidget *widget)
54 {
55       GdkScreen *screen;
56
57       screen = gtk_widget_get_screen (widget);
58       gtk_widget_set_colormap (widget,
59                                 gdk_screen_get_rgba_colormap (screen));
60       gtk_widget_set_app_paintable (widget,
61                                 TRUE);
62       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
63 }
64
65
66 static gboolean
67 lw_applet_expose_event(GtkWidget      *widget,
68                                         GdkEventExpose *event)
69 {
70   cairo_t *cr;
71
72   /* Create cairo context */
73   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
74   gdk_cairo_region (cr, event->region);
75   cairo_clip (cr);
76
77   /* Draw alpha background */
78   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
79   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
80   cairo_paint (cr);
81
82   /* Free context */
83   cairo_destroy (cr);
84
85   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
86                                                                                   event);
87 }
88
89 static gboolean
90 expose_event (GtkWidget *widget,GdkEventExpose *event,
91      gpointer data)
92 {
93     cairo_t *cr;
94     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
95         
96     cr = gdk_cairo_create(widget->window);
97     if (cr){
98         gdk_cairo_region(cr, event->region);
99         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
100         gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
101         cairo_paint(cr);
102         cairo_destroy(cr);
103     }
104     return TRUE;
105 }
106
107 static void
108 realize (GtkWidget *widget)
109 {
110     GdkScreen *screen;
111     screen = gtk_widget_get_screen (widget);
112     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
113 }
114
115 /* Set position of widget on desktop */
116 static void
117 init_applet_position(void)
118 {
119   GSList *position = NULL;
120   gchar *position_key;
121   gchar *modified_key;
122   gchar *modified;
123   GError *error = NULL;
124   GConfClient   *gconf_client = gconf_client_get_default ();
125   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
126   position = gconf_client_get_list (gconf_client,
127                                     position_key,
128                                     GCONF_VALUE_INT,
129                                     NULL);
130   if (position && position->data && position->next->data){
131         xapplet = GPOINTER_TO_INT (position->data);
132         yapplet = GPOINTER_TO_INT (position->next->data);
133   }else{
134         position = g_slist_prepend (g_slist_prepend (NULL,
135                                       GINT_TO_POINTER (Ystartposition)),
136                                       GINT_TO_POINTER (Xstartposition));
137         gconf_client_set_list (gconf_client,
138                                position_key,
139                                GCONF_VALUE_INT,
140                                position,
141                                &error);
142         xapplet = Xstartposition;
143         yapplet = Ystartposition;
144   }
145   g_free (position_key);
146   modified = g_strdup_printf ("%i", 0);
147   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
148   gconf_client_set_string (gconf_client,
149                            modified_key,
150                            modified,
151                            &error);
152   g_free(modified);
153   g_free(modified_key);
154   gconf_client_clear_cache(gconf_client);
155   g_object_unref(gconf_client);
156 }
157
158 void
159 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
160 {
161 /*  fprintf(stderr, "actor_set_position_full z=%d\n", z); */
162     hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
163 }
164
165 static gint 
166 path_line(gint x0, gint x1, double t)
167 {
168     // уравниение прямой
169     return ((x1 - x0) * t + x0);
170 }
171
172
173 void
174 destroy_hildon_actor(Actor *actor)
175 {
176     //fprintf(stderr, "destroy_hildon_actor %s\n",actor->name);
177     gtk_widget_destroy(actor->widget);
178     actor->widget = NULL;
179 }
180
181 static Actor* 
182 init_object(AWallpaperPlugin *desktop_plugin, 
183             gchar * name, 
184             gchar * filename, 
185             gint x, 
186             gint y, 
187             gint z, 
188             gint width, 
189             gint height, 
190             gboolean visible, 
191             gint scale, 
192             gint opacity, 
193             void (*pfunc_change)(Actor*),
194             void (*pfunc_probability)(Actor*)
195            )
196 {
197   Actor *actor = NULL;
198   actor = g_new0(Actor, 1);
199   actor->x = x;
200   actor->y = y;
201   actor->z = z;
202   actor->width = width;
203   actor->height = height;
204   actor->visible = visible;
205   actor->scale = scale;
206   actor->opacity = opacity;
207   actor->filename = g_strdup(filename);
208   actor->name = g_strdup(name);
209   actor->func_change = (gpointer)pfunc_change; 
210   actor->func_probability = (gpointer)pfunc_probability;
211   if (visible)
212     create_hildon_actor(actor, desktop_plugin);
213   else 
214     actor->widget = NULL;
215   actor->time_start_animation = 0;
216   actor->duration_animation = 0;
217   /*
218   a.widget = actor;
219   a.name = name;
220   a.x = x;
221   a.y = y;
222   a.z = z;
223   */
224   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
225   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
226   return actor;
227 }
228
229 gint 
230 rnd(gint max)
231 {
232     srand(time(NULL));
233     return rand() % max;
234 }
235 gint fast_rnd(gint max)
236 {
237     guint offset = 12923;
238     guint multiplier = 4079;
239     
240     scene.seed = scene.seed * multiplier + offset;
241     return (gint)(scene.seed % max);
242 }
243
244 gint 
245 probability_sun()
246 {
247     /* update sun position after ...  second */
248     return 60;
249 }
250
251
252 gint 
253 probability_plane()
254 {
255     return (fast_rnd(60));
256 //    return 0;
257 }
258
259 void 
260 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
261 {
262     gint phase;
263     char *newfile;
264
265     if (actor){
266         if (scene.daytime == TIME_NIGHT){
267             if (!actor->visible){
268                 actor->visible = TRUE;
269                 phase = get_moon_phase();
270                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
271                 if (actor->filename)
272                     g_free(actor->filename);
273                 actor->filename = newfile;
274                 create_hildon_actor(actor, desktop_plugin);
275
276             }
277             //actor->x = 400;
278             //actor->y = 10;
279            // actor_set_position_full(actor->widget, x, y, actor->z);
280             //probability_sun(actor);
281             //fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
282          }else if (actor->visible){
283             actor->visible = FALSE;
284             fprintf(stderr, "destroy moon \n");
285             destroy_hildon_actor(actor);
286             /* TO DO make moonrise*/
287             actor->time_start_animation = 0;
288         } 
289     }
290     
291 }
292
293 void 
294 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
295 {
296     double alt, azm;
297     gint x, y;
298
299     //fprintf(stderr, "change sun\n");
300     if (actor){
301         if (scene.daytime != TIME_NIGHT){
302             if (!actor->visible){
303                 actor->visible = TRUE;
304                 create_hildon_actor(actor, desktop_plugin);
305             }
306             get_sun_pos(hour, min, &alt, &azm);
307             get_sun_screen_pos(alt, azm, &x, &y);
308             actor->x = x;
309             actor->y = y;
310             actor_set_position_full(actor->widget, x, y, actor->z);
311             //probability_sun(actor);
312             actor->time_start_animation = time(NULL);
313          }else if (actor->visible){
314             actor->visible = FALSE;
315             destroy_hildon_actor(actor);
316             actor->time_start_animation = 0;
317         } 
318     }
319     
320 }
321
322 static void 
323 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
324 {
325     gint x0 = -300, y0 = 225, scale0 = 100,
326          x1 = 800, y1 = 162, scale1 = 130, 
327          x, y, scale;
328     struct timeval tvb;     
329     suseconds_t ms;
330     long sec;
331     double t;
332     gchar *newfile;
333
334     //fprintf(stderr, "change tram\n");
335     gettimeofday(&tvb, NULL);
336     
337     ms = tvb.tv_usec;
338     sec = tvb.tv_sec;
339     
340     if (!actor->visible){
341         actor->visible = TRUE;
342         if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
343             if (actor->filename)
344                 g_free(actor->filename);
345             actor->filename = g_strdup("tram_dark.png");
346         } else{
347             if (actor->filename)
348                 g_free(actor->filename);
349             actor->filename = g_strdup("tram.png");
350         }
351         create_hildon_actor(actor, desktop_plugin);
352     }
353     if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
354             newfile = g_strdup("tram_dark.png");
355         }else{
356             newfile = g_strdup("tram.png");
357         } 
358         if (strcmp(actor->filename, newfile)){
359         if (actor->filename)
360             g_free(actor->filename);
361         actor->filename = newfile;
362          
363
364         change_hildon_actor(actor, desktop_plugin);
365         }
366
367     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
368     x = path_line(x0, x1, t);
369     y = path_line(y0, y1, t);
370     scale = path_line(scale0, scale1, t);
371     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
372     actor_set_position_full(actor->widget, x, y, actor->z);
373     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
374     if (t >= 1){
375         /* stop animation */
376         actor->visible = FALSE;
377         destroy_hildon_actor(actor);
378         actor->time_start_animation = sec + 20;
379     }
380 }
381
382 void
383 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
384 {
385     gint x0 = 620, y0 = 233, scale0 = 100,
386          x1 = 79, y1 = -146, scale1 = 100, 
387          x, y, scale;
388     struct timeval tvb;     
389     suseconds_t ms;
390     long sec;
391     double t;
392     gchar *newfile;
393
394     gettimeofday(&tvb, NULL);
395     
396     ms = tvb.tv_usec;
397     sec = tvb.tv_sec;
398 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
399    
400     if (scene.daytime != TIME_NIGHT){
401         if (actor->time_start_animation == 0){
402             actor->time_start_animation = sec + probability_plane();
403             return;
404         }
405     }
406     if (!actor->visible){
407         actor->visible = TRUE;
408         create_hildon_actor(actor, desktop_plugin);
409     }
410 if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
411             newfile = g_strdup("dot1.png");
412         }else{
413             newfile = g_strdup("tu154.png");
414         } 
415         if (strcmp(actor->filename, newfile)){
416         if (actor->filename)
417             g_free(actor->filename);
418         actor->filename = newfile;
419          
420
421         change_hildon_actor(actor, desktop_plugin);
422         }
423
424     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
425     x = path_line(x0, x1, t);
426     y = path_line(y0, y1, t);
427     //scale line(scale0, scale1, t);
428     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
429     actor_set_position_full(actor->widget, x, y, actor->z);
430     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
431     if (t >= 1){
432         /* stop animation */
433         actor->visible = FALSE;
434         destroy_hildon_actor(actor);
435         if (scene.daytime == TIME_NIGHT) 
436             actor->time_start_animation = 0;
437         else 
438             actor->time_start_animation = sec + 10;
439     }
440
441 }
442
443 void
444 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
445 {
446     gint x0 = -actor->width, y0 = 45, scale0 = 100,
447          x1 = 800, y1 = 20, scale1 = 100, 
448          x, y, scale;
449     struct timeval tvb;     
450     suseconds_t ms;
451     long sec;
452     double t;
453     gchar * newfile;
454
455     gettimeofday(&tvb, NULL);
456     
457     ms = tvb.tv_usec;
458     sec = tvb.tv_sec;
459 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
460     if (scene.daytime != TIME_NIGHT){
461         if (actor->time_start_animation == 0){
462             actor->time_start_animation = sec + probability_plane();
463             return;
464         }
465     }
466     if (!actor->visible){
467         actor->visible = TRUE;
468         create_hildon_actor(actor, desktop_plugin);
469     }
470 if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
471             newfile = g_strdup("dot1.png");
472         }else{
473             newfile = g_strdup("plane3.png");
474         } 
475         if (strcmp(actor->filename, newfile)){
476         if (actor->filename)
477             g_free(actor->filename);
478         actor->filename = newfile;
479          
480
481         change_hildon_actor(actor, desktop_plugin);
482         }
483
484     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
485     x = path_line(x0, x1, t);
486     y = path_line(y0, y1, t);
487     //scale = path_line(scale0, scale1, t);
488     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
489     actor_set_position_full(actor->widget, x, y, actor->z);
490     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
491     if (t >= 1){
492         /* stop animation */
493         actor->visible = FALSE;
494         destroy_hildon_actor(actor);
495         if (scene.daytime == TIME_NIGHT) 
496             actor->time_start_animation = 0;
497         else 
498             actor->time_start_animation = sec + 15;
499     }
500
501 }
502
503 void
504 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
505 {
506     gint x0, y0 = 300, scale0 = 100,
507          x1, y1 = -actor->height, scale1 = 150, 
508          x, y, scale;
509     struct timeval tvb;     
510     suseconds_t ms;
511     long sec;
512     double t;
513     gchar *newfile;
514
515     //fprintf(stderr, "change cloud\n");
516     gettimeofday(&tvb, NULL);
517     
518     ms = tvb.tv_usec;
519     sec = tvb.tv_sec;
520     //fprintf(stderr, "c1oud %s - y0=%d\n", actor->name, actor->y);
521    
522     if (!actor->visible){
523         actor->visible = TRUE;
524         if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
525             newfile = g_strdup_printf("%s_dark.png", actor->name);
526         }else{
527             newfile = g_strdup_printf("%s.png", actor->name);
528         } 
529         if (actor->filename)
530             g_free(actor->filename);
531         actor->filename = newfile;
532          
533
534         create_hildon_actor(actor, desktop_plugin);
535     }
536     if (scene.daytime == TIME_NIGHT || (hour > 20 || hour < 6)){
537             newfile = g_strdup_printf("%s_dark.png", actor->name);
538         }else{
539             newfile = g_strdup_printf("%s.png", actor->name);
540         } 
541         if (strcmp(actor->filename, newfile)){
542         if (actor->filename)
543             g_free(actor->filename);
544         actor->filename = newfile;
545          
546
547         change_hildon_actor(actor, desktop_plugin);
548         }
549
550     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
551     
552     if (scene.wind_orientation == 1){
553         x0 = -actor->width;
554         x1 = 800;
555     }
556     else {
557         x0 = 800;
558         x1 = -actor->width;
559     }
560
561     x = path_line(x0, x1, t);    
562     y = -scene.wind_angle * (x - x0) + actor->y;
563     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
564
565     //fprintf(stderr, "change cloud t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
566     actor_set_position_full(actor->widget, x, y, actor->z);
567     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
568     if ((y < y1 || y > y0) || t >= 1){
569         /* stop animation */
570         actor->visible = FALSE;
571         destroy_hildon_actor(actor);
572         actor->time_start_animation = sec + 10;
573         actor->y = fast_rnd(300);
574     }
575
576 }
577
578 void
579 change_wind(Actor *actor, AWallpaperPlugin *desktop_plugin)
580 {
581     scene.wind_orientation = fast_rnd(2);
582     if (scene.wind_orientation == 0) scene.wind_orientation = -1;
583     scene.wind_angle = (double)(fast_rnd(200) - 100) / 100;
584     actor->time_start_animation = time(NULL) + (fast_rnd(10) + 10) * 60;
585     //fprintf(stderr, "change wind orient = %d angle = %f after = %d\n", scene.wind_orientation, scene.wind_angle, actor->time_start_animation-time(NULL));
586 }
587
588 void 
589 change_window1(Actor * actor, AWallpaperPlugin *desktop_plugin)
590 {
591     
592     gint now = time(NULL);
593     if (scene.daytime == TIME_DAY){
594         if (actor->visible){
595             actor->visible = FALSE;
596             destroy_hildon_actor(actor);
597         }
598         actor->time_start_animation = 0;
599         return;
600     }else if (actor->time_start_animation == 0){
601         actor->time_start_animation = now + fast_rnd(30);
602         return;
603     }
604
605     if (!actor->visible){
606         actor->visible = TRUE;
607         create_hildon_actor(actor, desktop_plugin);
608         actor->time_start_animation = now + 10;
609     }else {
610         actor->visible = FALSE;
611         destroy_hildon_actor(actor);
612         actor->time_start_animation = now + 10;
613     }
614    
615 }
616
617 void 
618 change_signal(Actor * actor, AWallpaperPlugin *desktop_plugin)
619 {
620     gchar *newfile;
621     gint now = time(NULL);
622     newfile = g_strdup_printf("%s%d.png", actor->name, scene.daytime); 
623     if (!strcmp(actor->filename, "red.png"))
624         newfile = g_strdup_printf("%s", "green.png");
625     else 
626         newfile = g_strdup_printf("%s", "red.png");
627     g_free(actor->filename);
628     actor->filename = newfile;
629     change_hildon_actor(actor, desktop_plugin);
630     actor->time_start_animation = now + 5;
631 }
632
633 void
634 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
635 {
636   GtkWidget *ha = NULL;
637   GdkPixbuf *pixbuf = NULL;
638   GtkWidget *image = NULL;
639   gchar     *str = NULL;
640
641   /* fprintf(stderr, "create_hildon_actor %s\n", actor->name);*/
642   ha = hildon_animation_actor_new();
643   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
644                         desktop_plugin->priv->theme, actor->filename);
645   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
646                                              actor->width, 
647                                              actor->height, 
648                                              NULL);
649   if (str)
650       g_free(str);
651   if (pixbuf){
652       image = gtk_image_new_from_pixbuf (pixbuf);
653       g_object_unref(G_OBJECT(pixbuf));
654   }
655   if (image){
656     g_signal_connect(G_OBJECT(image), "expose_event",
657                            G_CALLBACK(expose_event), pixbuf);
658     gtk_container_add (GTK_CONTAINER (ha), image);
659   }  
660   actor_set_position_full(ha, actor->x, actor->y, actor->z);
661   hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(ha), actor->visible);
662   hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(ha), (double)actor->scale/100, (double)actor->scale/100);
663   realize(ha);
664   gtk_widget_show_all(ha);
665
666   /* TO DO check it */
667   /*  gdk_flush (); */
668
669   //g_object_set_data(G_OBJECT(ha), "name", name);
670   //g_object_set_data(G_OBJECT(ha), "filename", filename);
671   g_object_set_data(G_OBJECT(ha), "image", image);
672   /*
673   g_object_set_data(G_OBJECT(ha), "x", x);
674   g_object_set_data(G_OBJECT(ha), "y", y);
675   g_object_set_data(G_OBJECT(ha), "z", z);
676   g_object_set_data(G_OBJECT(ha), "width", width);
677   g_object_set_data(G_OBJECT(ha), "height", height);
678   g_object_set_data(G_OBJECT(ha), "scale", scale);
679   g_object_set_data(G_OBJECT(ha), "visible", visible);
680   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
681   g_object_set_data(G_OBJECT(ha), "func", pfunc);
682   */
683   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
684   actor->widget = ha;
685 }
686
687
688 void
689 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
690 {
691     GtkWidget *image = NULL;
692     GdkPixbuf *pixbuf = NULL;
693     gchar     *str = NULL;
694
695     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
696                             desktop_plugin->priv->theme, actor->filename);
697  
698     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
699                                                actor->width, 
700                                                actor->height, 
701                                                NULL);
702     if(str)
703         g_free(str);
704     if (pixbuf){
705         image = gtk_image_new_from_pixbuf (pixbuf);
706         g_object_unref(G_OBJECT(pixbuf));
707     }
708     if (image){ 
709         g_signal_connect(G_OBJECT(image), "expose_event",
710                                        G_CALLBACK(expose_event), pixbuf);
711         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
712             gtk_container_remove(GTK_CONTAINER(actor->widget), g_object_get_data(G_OBJECT(actor->widget), "image"));  
713         }
714         g_object_set_data(G_OBJECT(actor->widget), "image", image);
715         gtk_container_add (GTK_CONTAINER (actor->widget), image);
716         realize(actor->widget);
717         gtk_widget_show_all(actor->widget);
718         /* TO DO check it */
719        /*  gdk_flush (); */
720
721
722     }
723
724 }
725
726
727 void 
728 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
729 {
730     gchar *newfile;
731     newfile = g_strdup_printf("%s%d.png", actor->name, scene.daytime); 
732     if (actor->filename)
733             g_free(actor->filename);
734     actor->filename = newfile;
735     change_hildon_actor(actor, desktop_plugin);
736 }
737
738 void 
739 change_static_actor_with_corner(Actor * actor, AWallpaperPlugin *desktop_plugin)
740 {
741     gchar *newfile;
742     gchar           buffer[2048];
743
744     if (desktop_plugin->priv->right_corner)
745         gtk_widget_destroy(desktop_plugin->priv->right_corner);
746     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/town%i_right_corner.png", \
747                                   THEME_PATH, desktop_plugin->priv->theme, scene.daytime);
748     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
749     if (desktop_plugin->priv->right_corner){
750         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
751         gtk_widget_show (desktop_plugin->priv->right_corner);
752     }
753     change_static_actor(actor, desktop_plugin);
754
755 }
756
757
758 /*
759 static gint 
760 get_time(gint t){
761     // уравнение изменения времени
762     return t*1.1;
763 }
764 */
765 static void 
766 destroy_scene(void){
767     GSList * tmp = scene.actors;
768     Actor *actor;
769     while (tmp != NULL){
770         actor = tmp->data;
771         if (actor){
772             if (actor->filename)
773                 g_free(actor->filename);
774             if (actor->name)
775                 g_free(actor->name);
776             gtk_widget_destroy(actor->widget);
777             //actor->widget = NULL;
778             g_free(actor);
779         }
780         tmp = g_slist_next(tmp);
781     }
782     g_slist_free(tmp);
783
784 }
785
786 void
787 reload_scene(AWallpaperPlugin *desktop_plugin)
788 {
789     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
790     destroy_scene();
791     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
792         init_scene(desktop_plugin);
793     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
794         init_scene1(desktop_plugin);
795 }
796
797
798 /* Init Modern Scene */
799 static void
800 init_scene(AWallpaperPlugin *desktop_plugin)
801 {
802   Actor *actor;
803   gint now = time(NULL);
804   gint i;
805   gint winds[13][2];
806
807   /* fprintf(stderr, "init scene \n");*/
808   scene.daytime = get_daytime(hour, min);
809   scene.actors = NULL;
810   scene.wind_orientation = -1;
811   scene.wind_angle = 0.3;
812   /* init value for random */
813   scene.seed = time(NULL);
814
815   
816
817   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 
818                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
819   change_static_actor(actor, desktop_plugin);
820   scene.actors = g_slist_append(scene.actors, actor);
821
822   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
823                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
824   actor->time_start_animation = now;
825   actor->duration_animation = G_MAXINT;
826   change_sun(actor, desktop_plugin);
827   scene.actors = g_slist_append(scene.actors, actor);
828
829   //actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
830     //                  TRUE, 100, 255, NULL, NULL);
831   //scene.actors = g_slist_append(scene.actors, actor);
832   
833   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, 0, 7, 150, 97, 
834                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
835   actor->time_start_animation = now;
836   actor->duration_animation = 1*60;
837   scene.actors = g_slist_append(scene.actors, actor);
838   
839   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, 80, 7, 188, 75, 
840                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
841   actor->time_start_animation = now + 20;
842   actor->duration_animation = 1*60;
843   scene.actors = g_slist_append(scene.actors, actor);
844
845   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, 30, 7, 150, 75, 
846                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
847   actor->time_start_animation = now + 40;
848   actor->duration_animation = 1*60;
849   scene.actors = g_slist_append(scene.actors, actor);
850
851
852   actor = init_object(desktop_plugin, "town", "town0.png", 0, 0, 8, 800, 480, 
853                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
854   change_static_actor(actor, desktop_plugin);
855   scene.actors = g_slist_append(scene.actors, actor);
856
857   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, 9, 350, 210, 
858                       FALSE, 100, 255, (gpointer)&change_tram, NULL);
859   actor->time_start_animation = now; 
860   actor->duration_animation = 40;
861   scene.actors = g_slist_append(scene.actors, actor);
862
863   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 10, 800, 79,
864                       TRUE, 100, 255, (gpointer)&change_static_actor_with_corner, NULL);
865   change_static_actor_with_corner(actor, desktop_plugin);
866   scene.actors = g_slist_append(scene.actors, actor);
867   
868   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, 6, 60, 60, 
869                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
870   change_moon(actor, desktop_plugin);
871   scene.actors = g_slist_append(scene.actors, actor);
872 /*
873   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
874                       FALSE, 100, 255, (gpointer)&change_wind, NULL);
875   change_wind(actor, desktop_plugin);
876   scene.actors = g_slist_append(scene.actors, actor);
877 */
878     /* windows in 4-th house  */
879
880     winds[0][0] = 482;
881     winds[0][1] = 180;
882
883     winds[1][0] = 495;
884     winds[1][1] = 179;
885
886     winds[2][0] = 482;
887     winds[2][1] = 191;
888
889     winds[3][0] = 495;
890     winds[3][1] = 190;
891     
892     winds[4][0] = 482;
893     winds[4][1] = 201;
894     
895     winds[5][0] = 495;
896     winds[5][1] = 210;
897     
898     winds[6][0] = 482;
899     winds[6][1] = 222;
900     
901     winds[7][0] = 495;
902     winds[7][1] = 221;
903     
904     winds[8][0] = 459;
905     winds[8][1] = 203;
906     
907     winds[9][0] = 495;
908     winds[9][1] = 241;
909     
910     winds[10][0] = 495;
911     winds[10][1] = 252;
912     
913     winds[11][0] = 482;
914     winds[11][1] = 273;
915     
916     winds[12][0] = 495;
917     winds[12][1] = 303;
918     for (i=0; i<13; i++){
919         actor = init_object(desktop_plugin, "window1", "window1.png", 
920                             winds[i][0], winds[i][1], 8, 8, 10, 
921                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
922         //change_window1(actor, desktop_plugin);
923         actor->time_start_animation = now + fast_rnd(30);
924         scene.actors = g_slist_append(scene.actors, actor);
925
926     }
927     
928     /* windows in 1-th house  */
929     
930     winds[0][0] = 86;
931     winds[0][1] = 321;
932
933     winds[1][0] = 86;
934     winds[1][1] = 363;
935
936     winds[2][0] = 86;
937     winds[2][1] = 385;
938
939     winds[3][0] = 86;
940     winds[3][1] = 286;
941     
942     winds[4][0] = 94;
943     winds[4][1] = 232;
944     
945     winds[5][0] = 94;
946     winds[5][1] = 243;
947     
948     winds[6][0] = 94;
949     winds[6][1] = 265;
950     
951     winds[7][0] = 94;
952     winds[7][1] = 331;
953     for (i=0; i<8; i++){
954         actor = init_object(desktop_plugin, "window2", "window2.png", 
955                             winds[i][0], winds[i][1], 8, 8, 10, 
956                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
957         //change_window1(actor, desktop_plugin);
958         actor->time_start_animation = now + fast_rnd(30);
959         scene.actors = g_slist_append(scene.actors, actor);
960
961     }
962     
963     /* windows in 3-th house  */
964     
965     winds[0][0] = 251;
966     winds[0][1] = 162;
967
968     winds[1][0] = 251;
969     winds[1][1] = 196;
970
971     winds[2][0] = 251;
972     winds[2][1] = 278;
973
974     winds[3][0] = 251;
975     winds[3][1] = 289;
976     
977     winds[4][0] = 313;
978     winds[4][1] = 173;
979     
980     winds[5][0] = 322;
981     winds[5][1] = 160;
982     
983     winds[6][0] = 303;
984     winds[6][1] = 217;
985     
986     winds[7][0] = 322;
987     winds[7][1] = 224;
988     
989     winds[8][0] = 323;
990     winds[8][1] = 217;
991     
992     winds[9][0] = 322;
993     winds[9][1] = 288;
994     
995     for (i=0; i<10; i++){
996         actor = init_object(desktop_plugin, "window3", "window3.png", 
997                             winds[i][0], winds[i][1], 8, 8, 10, 
998                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
999         //change_window1(actor, desktop_plugin);
1000         actor->time_start_animation = now + fast_rnd(30);
1001         scene.actors = g_slist_append(scene.actors, actor);
1002
1003     }
1004
1005     /* windows in 5-th house  */
1006     
1007     winds[0][0] = 610;
1008     winds[0][1] = 224;
1009
1010     winds[1][0] = 602;
1011     winds[1][1] = 245;
1012
1013     winds[2][0] = 602;
1014     winds[2][1] = 264;
1015
1016     winds[3][0] = 610;
1017     winds[3][1] = 301;
1018     
1019     winds[4][0] = 610;
1020     winds[4][1] = 320;
1021     
1022     winds[5][0] = 593;
1023     winds[5][1] = 352;
1024     
1025     winds[6][0] = 610;
1026     winds[6][1] = 368;
1027     
1028     for (i=0; i<7; i++){
1029         actor = init_object(desktop_plugin, "window4", "window4.png", 
1030                             winds[i][0], winds[i][1], 8, 8, 10, 
1031                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
1032         //change_window1(actor, desktop_plugin);
1033         actor->time_start_animation = now + fast_rnd(30);
1034         scene.actors = g_slist_append(scene.actors, actor);
1035
1036     }
1037
1038     /* windows in 6-th house  */
1039     
1040     winds[0][0] = 717;
1041     winds[0][1] = 283;
1042
1043     winds[1][0] = 698;
1044     winds[1][1] = 293;
1045
1046     winds[2][0] = 717;
1047     winds[2][1] = 315;
1048
1049     winds[3][0] = 717;
1050     winds[3][1] = 323;
1051     
1052     winds[4][0] = 698;
1053     winds[4][1] = 362;
1054     
1055     winds[5][0] = 698;
1056     winds[5][1] = 400;
1057     
1058     for (i=0; i<6; i++){
1059         actor = init_object(desktop_plugin, "window5", "window5.png", 
1060                             winds[i][0], winds[i][1], 8, 8, 10, 
1061                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
1062         //change_window1(actor, desktop_plugin);
1063         actor->time_start_animation = now + fast_rnd(30);
1064         scene.actors = g_slist_append(scene.actors, actor);
1065
1066     }
1067
1068     run_long_timeout(desktop_plugin);
1069 #if 0    
1070   anim = g_new0(Animation, 1);
1071   anim->count = 1;
1072   anim->actor = actor;
1073   anim->func_change = &change_tram;
1074   anim->func_time = NULL;
1075   anim->timestart = time(NULL); 
1076   anim->timeall = 10;
1077   
1078   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
1079 #endif  
1080  }
1081
1082 /* Init Berlin Scene */
1083 static void
1084 init_scene1(AWallpaperPlugin *desktop_plugin)
1085 {
1086   Actor *actor;
1087   gint now = time(NULL);
1088   gint i; 
1089   gint winds[13][2];
1090
1091   scene.daytime = get_daytime(hour, min);
1092   scene.actors = NULL;
1093   scene.wind_orientation = 1;
1094   scene.wind_angle = 0.3;
1095   /* init value for random */
1096   scene.seed = time(NULL);
1097
1098   
1099   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
1100                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
1101   change_static_actor(actor, desktop_plugin);
1102   scene.actors = g_slist_append(scene.actors, actor);
1103
1104   
1105   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
1106                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
1107   actor->time_start_animation = time(NULL);
1108   actor->duration_animation = G_MAXINT;
1109   change_sun(actor, desktop_plugin);
1110   scene.actors = g_slist_append(scene.actors, actor);
1111
1112 #if 0
1113   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
1114                       TRUE, 100, 255, NULL, NULL);
1115   scene.actors = g_slist_append(scene.actors, actor);
1116 #endif
1117
1118   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
1119                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
1120   change_moon(actor, desktop_plugin);
1121   scene.actors = g_slist_append(scene.actors, actor);
1122   
1123   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, 300, 7, 150, 97, 
1124                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
1125   actor->time_start_animation = now + 20;
1126   actor->duration_animation = 1*60;
1127   scene.actors = g_slist_append(scene.actors, actor);
1128   
1129   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, 100, 7, 188, 75, 
1130                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
1131   actor->time_start_animation = now;
1132   actor->duration_animation = 1*60;
1133   scene.actors = g_slist_append(scene.actors, actor);
1134
1135   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, 200, 7, 150, 75, 
1136                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
1137   actor->time_start_animation = now + 40;
1138   actor->duration_animation = 1*60;
1139   scene.actors = g_slist_append(scene.actors, actor);
1140
1141  
1142   actor = init_object(desktop_plugin, "plane3", "plane3.png", 0, 45, 8, 160, 50, 
1143                       FALSE, 100, 255, (gpointer)&change_plane2, NULL);
1144   actor->time_start_animation = now + 20;
1145   actor->duration_animation = 40;
1146   scene.actors = g_slist_append(scene.actors, actor);
1147   
1148   actor = init_object(desktop_plugin, "tu154", "tu154.png", 620, 233, 9, 300, 116, 
1149                       FALSE, 100, 255, (gpointer)&change_plane1, NULL);
1150   actor->time_start_animation = now;
1151   actor->duration_animation = 20;
1152   scene.actors = g_slist_append(scene.actors, actor);
1153
1154   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
1155                       TRUE, 100, 255, (gpointer)&change_static_actor_with_corner, NULL);
1156   change_static_actor_with_corner(actor, desktop_plugin);
1157   scene.actors = g_slist_append(scene.actors, actor);
1158 /*
1159   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
1160                       FALSE, 100, 255, (gpointer)&change_wind, NULL);
1161   change_wind(actor, desktop_plugin);
1162   scene.actors = g_slist_append(scene.actors, actor);
1163 */
1164   actor = init_object(desktop_plugin, "signal", "red.png", 486, 425, 10, 18, 38, 
1165                       TRUE, 100, 255, (gpointer)&change_signal, NULL);
1166   actor->time_start_animation = now;  
1167   scene.actors = g_slist_append(scene.actors, actor);winds[0][0] = 717;
1168     
1169     winds[0][0] = 389;
1170     winds[0][1] = 305;
1171
1172     winds[1][0] = 373;
1173     winds[1][1] = 306;
1174
1175     winds[2][0] = 355;
1176     winds[2][1] = 306;
1177
1178     winds[3][0] = 356;
1179     winds[3][1] = 288;
1180     
1181     winds[4][0] = 337;
1182     winds[4][1] = 269;
1183     
1184     winds[5][0] = 372;
1185     winds[5][1] = 268;
1186   
1187     winds[6][0] = 372;
1188     winds[6][1] = 249;
1189     
1190     winds[7][0] = 388;
1191     winds[7][1] = 249;
1192     
1193     winds[8][0] = 387;
1194     winds[8][1] = 230;
1195     
1196     winds[9][0] = 372;
1197     winds[9][1] = 211;
1198     
1199     winds[10][0] = 355;
1200     winds[10][1] = 159;
1201     
1202     winds[11][0] = 335;
1203     winds[11][1] = 158;
1204     
1205     winds[12][0] = 386;
1206     winds[12][1] = 119;
1207   
1208     for (i=0; i<13; i++){
1209         actor = init_object(desktop_plugin, "window", "window.png", 
1210                             winds[i][0], winds[i][1], 10, 8, 9, 
1211                             FALSE, 100, 255, (gpointer)&change_window1, NULL);
1212         //change_window1(actor, desktop_plugin);
1213         actor->time_start_animation = now + fast_rnd(30);
1214         scene.actors = g_slist_append(scene.actors, actor);
1215
1216     }
1217     
1218     run_long_timeout(desktop_plugin);
1219
1220 }
1221
1222
1223 void 
1224 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
1225 {
1226     gint y0 = 365;// - уровень горизонта
1227     *x = (int)(azm * 800) - 64;
1228     *y = (int)((1 - alt) * y0) - 64;
1229     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
1230 }
1231 #if 0
1232 static void 
1233 change_actor(GtkWidget * actor)
1234 {
1235     char * name;
1236     gint x, y, daytime, scale;
1237     gdouble sc;
1238     double alt, azm;
1239
1240     GtkWidget *image;
1241     GdkPixbuf *pixbuf;
1242
1243     void (*pfunc)(gpointer, gpointer);
1244
1245     name = g_object_get_data(G_OBJECT(actor), "name");
1246     fprintf(stderr, "change actor %s\n", name);
1247     if (name == "sun"){
1248         pfunc = g_object_get_data(G_OBJECT(actor), "func");
1249         if (pfunc)
1250             (*pfunc)(actor, g_strdup(name));
1251         daytime = get_daytime();
1252         if (daytime != TIME_NIGHT){
1253             hildon_animation_actor_set_show(actor, 1);
1254             get_sun_pos(&alt, &azm);
1255             get_sun_screen_pos(alt, azm, &x, &y);
1256             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1257         }
1258     }
1259     
1260     if (name == "cloud1"){
1261         x = g_object_get_data(G_OBJECT(actor), "x");
1262         y = g_object_get_data(G_OBJECT(actor), "y");
1263         scale = g_object_get_data(G_OBJECT(actor), "scale");
1264
1265         /* Start */
1266         image = g_object_get_data(G_OBJECT(actor), "image");
1267         
1268         gtk_container_remove(actor, image);  
1269         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
1270                                              200, 
1271                                              200, 
1272                                              NULL);
1273         if (pixbuf){
1274               image = gtk_image_new_from_pixbuf (pixbuf);
1275               g_object_unref(G_OBJECT(pixbuf));
1276         }
1277         g_signal_connect(G_OBJECT(image), "expose_event",
1278                                    G_CALLBACK(expose_event), pixbuf);
1279         gtk_container_add (GTK_CONTAINER (actor), image);
1280         realize(actor);
1281         gtk_widget_show_all(actor);
1282         /* End*/
1283
1284             
1285         x += 40;
1286         y -= 20;
1287         scale -= 10;
1288         if (x > 500){
1289             x = 400;
1290             y = 150;
1291             sc = 1;
1292         }
1293         sc = (double)scale / 100;
1294         hildon_animation_actor_set_scale(actor, sc, sc);
1295         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
1296         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1297         g_object_set_data(G_OBJECT(actor), "x", x);
1298         g_object_set_data(G_OBJECT(actor), "y", y);
1299         g_object_set_data(G_OBJECT(actor), "scale", scale);
1300     }
1301
1302 }
1303 #endif
1304 static gboolean
1305 short_timeout (AWallpaperPlugin *desktop_plugin)
1306 {
1307     //gint daytime = get_daytime();
1308     GSList * tmp;
1309     void (*pfunc)(gpointer, gpointer);
1310     time_t now;
1311     Actor *actor;
1312     gboolean stop_flag = TRUE;
1313     
1314     if (ttt == 1){
1315         min ++;
1316         if (min == 60){
1317             hour ++;
1318             min = 0;
1319             if (hour == 23) hour = 0;
1320         }
1321         ttt = 0;
1322     }else ttt ++;
1323     if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
1324         desktop_plugin->priv->short_timer = 0;
1325         return FALSE;
1326     }
1327
1328     now = time(NULL);
1329     //scene.daytime = daytime;
1330     /* fprintf(stderr, "Short timer %d\n", now); */
1331     tmp = scene.actors;
1332     while (tmp != NULL){
1333            actor = tmp->data;
1334            if (now >= actor->time_start_animation  
1335                && actor->time_start_animation > 0
1336                /* && now - actor->time_start_animation <= actor->duration_animation*/){
1337                 pfunc = actor->func_change;
1338                 if (pfunc){ 
1339                     (*pfunc)(actor, desktop_plugin);
1340                     stop_flag = FALSE;
1341                 }
1342             }
1343             tmp = g_slist_next(tmp);
1344     }
1345     if (stop_flag){
1346          desktop_plugin->priv->short_timer = 0;
1347          return FALSE;
1348     }else
1349          return TRUE; /* keep running this event */
1350 }
1351
1352 void
1353 run_long_timeout(AWallpaperPlugin *desktop_plugin){
1354
1355     gint daytime = get_daytime(hour, min);
1356     GSList * tmp;
1357     void (*pfunc)(gpointer, gpointer);
1358     time_t now;
1359     Actor *actor;
1360
1361     //hour = hour + 1;
1362     //min = 0;
1363     //if (hour == 24) hour = 0;
1364     fprintf(stderr, "---- %d:%d\n", hour, min);
1365
1366
1367     //fprintf(stderr, "timer daytime=%d\n", daytime);
1368     if (scene.daytime != daytime){
1369         scene.daytime = daytime;
1370         tmp = scene.actors;
1371         while (tmp != NULL){
1372             //change_actor(tmp->data);
1373             pfunc =((Actor*)tmp->data)->func_change;
1374             if (pfunc){
1375                 (*pfunc)(tmp->data, desktop_plugin);
1376             }
1377             tmp = g_slist_next(tmp);
1378         }
1379     }
1380    
1381     now = time(NULL);
1382     //fprintf(stderr, "Now  %d\n", now);
1383     tmp = scene.actors;
1384     while (tmp != NULL){
1385         actor = tmp->data;
1386         if (now >= actor->time_start_animation  
1387             && actor->time_start_animation > 0
1388             && desktop_plugin->priv->short_timer == 0){
1389             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
1390             if (desktop_plugin->priv->rich_animation){
1391                 actor->time_start_animation = now;
1392                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
1393             }
1394             else {
1395                 (*actor->func_change)(actor, desktop_plugin);
1396             }
1397         }
1398         tmp = g_slist_next(tmp);
1399     }
1400  
1401 }
1402
1403 static gboolean
1404 long_timeout (AWallpaperPlugin *desktop_plugin)
1405 {
1406     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
1407     if (desktop_plugin->priv->long_timer == 0 )
1408         return FALSE;
1409     if (!desktop_plugin->priv->visible){
1410         if(desktop_plugin->priv->short_timer != 0){
1411             g_source_remove(desktop_plugin->priv->short_timer);
1412             desktop_plugin->priv->short_timer = 0;
1413         }
1414         desktop_plugin->priv->long_timer = 0;
1415         return FALSE;
1416     }
1417   
1418
1419     run_long_timeout(desktop_plugin);
1420     return TRUE; /* keep running this event */
1421 }
1422
1423 static void
1424 desktop_plugin_visible_notify (GObject    *object,
1425                                           GParamSpec *spec,
1426                                           AWallpaperPlugin *desktop_plugin)
1427 {
1428     gboolean visible;
1429     g_object_get (object, "is-on-current-desktop", &visible, NULL);
1430     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
1431     if (visible){
1432         desktop_plugin->priv->visible = TRUE;
1433         if (desktop_plugin->priv->long_timer == 0 ){
1434             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1435             run_long_timeout(desktop_plugin);
1436         }
1437     }else{
1438         desktop_plugin->priv->visible = FALSE;
1439         if (desktop_plugin->priv->long_timer != 0 ){
1440             g_source_remove(desktop_plugin->priv->long_timer);
1441             desktop_plugin->priv->long_timer = 0;
1442         }
1443     }
1444 }
1445
1446 gboolean
1447 rich_animation_press(GtkWidget *widget, GdkEvent *event,
1448                                             gpointer user_data){
1449     fprintf(stderr,"gggggggggggggggggggg2222\n");
1450 }    
1451 static void
1452 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
1453 {
1454     GtkWidget *rich_animation;
1455     gchar           buffer[2048];
1456
1457
1458     fprintf(stderr, "!!!!!!!plugin init \n");
1459     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1460     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1461
1462     /* Load config */
1463     read_config(priv);
1464     priv->desktop_plugin = desktop_plugin;
1465     desktop_plugin->priv->main_widget = gtk_fixed_new();
1466
1467     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
1468     desktop_plugin->priv->right_corner = NULL;
1469     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
1470     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
1471     if (desktop_plugin->priv->right_corner){
1472         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
1473         gtk_widget_show (desktop_plugin->priv->right_corner);
1474     }
1475     /* Create rich animation event */
1476     rich_animation = gtk_event_box_new();
1477     if(rich_animation){
1478         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
1479         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
1480         gtk_widget_set_size_request(rich_animation, 100, 32);
1481         gtk_widget_show (rich_animation);
1482         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
1483         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
1484     }
1485 /*
1486     GtkWidget *label = gtk_label_new ("ddddddddddddd"); 
1487     gtk_widget_set_size_request(label, 95, 30);
1488     gtk_widget_show (label);
1489 //    gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1490     gtk_fixed_put(GTK_FIXED(widget), label, 0, 0);
1491 */
1492     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1493     g_signal_connect (desktop_plugin, "show-settings",
1494                              G_CALLBACK (lw_settings), priv);
1495     gtk_widget_show (desktop_plugin->priv->main_widget);
1496     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
1497     init_applet_position();
1498
1499     
1500     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1501     if (!strcmp(priv->theme,"Modern"))
1502         init_scene(desktop_plugin);
1503     else if (!strcmp(priv->theme,"Berlin")) 
1504         init_scene1(desktop_plugin);
1505     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1506     priv->short_timer = 0;
1507     /* TODO Move scene to priv */
1508     scene.timer_type = LONG_TIMER_TYPE;
1509     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1510                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1511
1512    
1513     //sleep(2);
1514 }
1515
1516 static void
1517 lw_applet_finalize (GObject *object)
1518 {
1519      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1520      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1521      
1522      fprintf(stderr,"finalaze %i\n", priv->long_timer);
1523      if (priv->long_timer){
1524         g_source_remove(priv->long_timer);
1525         priv->long_timer = 0;
1526      }
1527      if (priv->short_timer){
1528         g_source_remove(priv->short_timer);
1529         priv->short_timer = 0;
1530      }
1531
1532      destroy_scene();
1533 }
1534
1535 static void
1536 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1537
1538     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1539     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1540
1541     /* gobject */
1542     gobject_class->destroy = (gpointer)lw_applet_finalize;
1543     widget_class->realize = lw_applet_realize;
1544     widget_class->expose_event = lw_applet_expose_event;
1545
1546     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1547
1548 }
1549
1550 static void
1551 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
1552 }