done hildon abstraction
[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
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, "destroy_hildon_actor %s\n",actor->name);
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             gboolean load_image,
189             gint scale, 
190             gint opacity, 
191             void (*pfunc_change)(Actor*),
192             void (*pfunc_probability)(Actor*),
193             GPtrArray *child
194            )
195 {
196   Actor *actor = NULL;
197   actor = g_new0(Actor, 1);
198   actor->x = x;
199   actor->y = y;
200   actor->z = z;
201   actor->width = width;
202   actor->height = height;
203   actor->visible = visible;
204   actor->scale = scale;
205   actor->opacity = opacity;
206   actor->filename = g_strdup(filename);
207   actor->name = g_strdup(name);
208   actor->func_change = (gpointer)pfunc_change; 
209   actor->func_probability = (gpointer)pfunc_probability;
210   actor->child = child;
211   if (load_image)
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(10) + 1) * 60;
256     return fast_rnd(180);
257 }
258
259 void 
260 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
261 {
262     gint phase;
263     char *newfile;
264     gint x0 = 150,
265          x1 = 650, 
266          x, y,
267          y0=150, y1 = 150, x2=400, y2=30;
268     double a, b, c;
269     struct timeval tvb;     
270     suseconds_t ms;
271     long sec;
272     double t;
273 #if 0
274     a = (double)(y2 - (double)(x2*(y1-y0) + x1*y0 - x0*y1)/(x1-x0))/(x2*(x2-x0-x1)+x0*x1);
275     b = (double)(y1-y0)/(x1-x0) - (double)a*(x0+x1);
276     c = (double)(x1*y0 - x0*y1)/(x1-x0) + (double)a*x0*x1;
277     fprintf(stderr, "a=%f, b=%f, c=%f\n", a, b, c);
278 #endif
279     gettimeofday(&tvb, NULL);
280     
281     ms = tvb.tv_usec;
282     sec = tvb.tv_sec;
283
284     if (actor){
285         if (scene.daytime == TIME_NIGHT){
286             if (!actor->visible){
287                 actor->visible = TRUE;
288                 phase = get_moon_phase();
289                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
290                 if (actor->filename)
291                     g_free(actor->filename);
292                 actor->filename = newfile;
293                 actor->time_start_animation = sec - fast_rnd(60 * 60);
294                 actor->duration_animation = 1 * 60 * 60;
295                 create_hildon_actor(actor, desktop_plugin);
296
297             }
298             t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
299             if (t <= 1)
300                 x = path_line(x0, x1, t);
301             else 
302                 x = path_line(x1, x0, t-1);
303             //y = path_line(y0, y1, t);
304             y = 0.001920*x*x - 1.536*x + 337.2;
305             //y = a*x*x + b*x + c;
306
307             actor_set_position_full(actor->widget, x, y, actor->z);
308
309             if (t>=2){
310                 actor->time_start_animation = sec;
311             }
312
313          }else if (actor->visible){
314             actor->visible = FALSE;
315             fprintf(stderr, "destroy moon \n");
316             destroy_hildon_actor(actor);
317             /* TO DO make moonrise*/
318             actor->time_start_animation = 0;
319         } 
320     }
321     
322 }
323
324 void 
325 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
326 {
327     double alt, azm;
328     gint x, y;
329
330     //fprintf(stderr, "change sun\n");
331     if (actor){
332         if (scene.daytime != TIME_NIGHT){
333             if (!actor->visible){
334                 actor->visible = TRUE;
335                 create_hildon_actor(actor, desktop_plugin);
336             }
337             get_sun_pos(&alt, &azm);
338             get_sun_screen_pos(alt, azm, &x, &y);
339             actor->x = x;
340             actor->y = y;
341             actor_set_position_full(actor->widget, x, y, actor->z);
342             //probability_sun(actor);
343             actor->time_start_animation = time(NULL) + probability_sun();
344          }else if (actor->visible){
345             actor->visible = FALSE;
346             destroy_hildon_actor(actor);
347             actor->time_start_animation = 0;
348         } 
349     }
350     
351 }
352
353 static void 
354 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
355 {
356     gint x0 = -300, y0 = 225, scale0 = 100,
357          x1 = 800, y1 = 162, scale1 = 130, 
358          x, y, scale;
359     struct timeval tvb;     
360     suseconds_t ms;
361     long sec;
362     double t;
363
364     //fprintf(stderr, "change tram\n");
365     gettimeofday(&tvb, NULL);
366     
367     ms = tvb.tv_usec;
368     sec = tvb.tv_sec;
369     
370     if (!actor->visible){
371         actor->visible = TRUE;
372         if (scene.daytime == TIME_NIGHT){
373             if (actor->filename)
374                 g_free(actor->filename);
375             actor->filename = g_strdup("tram_dark.png");
376         } else{
377             if (actor->filename)
378                 g_free(actor->filename);
379             actor->filename = g_strdup("tram.png");
380         }
381         create_hildon_actor(actor, desktop_plugin);
382     }
383     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
384     x = path_line(x0, x1, t);
385     y = path_line(y0, y1, t);
386     scale = path_line(scale0, scale1, t);
387     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
388     actor_set_position_full(actor->widget, x, y, actor->z);
389     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
390     set_actor_scale(actor, (double)scale/100, (double)scale/100);
391     if (t >= 1){
392         /* stop animation */
393         actor->visible = FALSE;
394         destroy_hildon_actor(actor);
395         actor->time_start_animation = sec + fast_rnd(60);
396     }
397 }
398
399 void
400 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
401 {
402     gint x0 = 620, y0 = 233,
403          x1 = 79, y1 = -146, 
404          x, y;
405     struct timeval tvb;     
406     suseconds_t ms;
407     long sec;
408     double t;
409
410     gettimeofday(&tvb, NULL);
411     
412     ms = tvb.tv_usec;
413     sec = tvb.tv_sec;
414 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
415    
416     if (scene.daytime != TIME_NIGHT){
417         if (actor->time_start_animation == 0){
418             actor->time_start_animation = sec + probability_plane();
419             return;
420         }
421     }
422     if (!actor->visible){
423         actor->visible = TRUE;
424         create_hildon_actor(actor, desktop_plugin);
425     }
426     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
427     x = path_line(x0, x1, t);
428     y = path_line(y0, y1, t);
429     //scale = path_line(scale0, scale1, t);
430     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
431     actor_set_position_full(actor->widget, x, y, actor->z);
432     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
433     if (t >= 1){
434         /* stop animation */
435         actor->visible = FALSE;
436         destroy_hildon_actor(actor);
437         if (scene.daytime == TIME_NIGHT) 
438             actor->time_start_animation = 0;
439         else 
440             actor->time_start_animation = sec + probability_plane();
441     }
442
443 }
444
445 void
446 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
447 {
448     gint x0 = -actor->width, y0 = 45,
449          x1 = 800, y1 = 20, 
450          x, y;
451     struct timeval tvb;     
452     suseconds_t ms;
453     long sec;
454     double t;
455
456     gettimeofday(&tvb, NULL);
457     
458     ms = tvb.tv_usec;
459     sec = tvb.tv_sec;
460 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
461     if (scene.daytime != TIME_NIGHT){
462         if (actor->time_start_animation == 0){
463             actor->time_start_animation = sec + probability_plane();
464             return;
465         }
466     }
467     if (!actor->visible){
468         actor->visible = TRUE;
469         create_hildon_actor(actor, desktop_plugin);
470     }
471
472     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
473     x = path_line(x0, x1, t);
474     y = path_line(y0, y1, t);
475     //scale = path_line(scale0, scale1, t);
476     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
477     actor_set_position_full(actor->widget, x, y, actor->z);
478     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
479     if (t >= 1){
480         /* stop animation */
481         actor->visible = FALSE;
482         destroy_hildon_actor(actor);
483         if (scene.daytime == TIME_NIGHT) 
484             actor->time_start_animation = 0;
485         else 
486             actor->time_start_animation = sec + probability_plane();
487     }
488
489 }
490
491 void
492 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
493 {
494     gint x0, y0 = 300, scale0 = 100,
495          x1, y1 = -actor->height, scale1 = 150, 
496          x, y, scale;
497     struct timeval tvb;     
498     suseconds_t ms;
499     long sec;
500     double t;
501     gchar *newfile;
502
503     //fprintf(stderr, "change cloud\n");
504     gettimeofday(&tvb, NULL);
505     
506     ms = tvb.tv_usec;
507     sec = tvb.tv_sec;
508     //fprintf(stderr, "c1oud %s - y0=%d\n", actor->name, actor->y);
509    
510     if (!actor->visible){
511         actor->visible = TRUE;
512         if (scene.daytime == TIME_NIGHT){
513             newfile = g_strdup_printf("%s_dark.png", actor->name);
514         }else{
515             newfile = g_strdup_printf("%s.png", actor->name);
516         } 
517         if (actor->filename)
518             g_free(actor->filename);
519         actor->filename = newfile;
520          
521         create_hildon_actor(actor, desktop_plugin);
522     }
523     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
524     
525     if (scene.wind_orientation == 1){
526         x0 = -actor->width;
527         x1 = 800;
528     }
529     else {
530         x0 = 800;
531         x1 = -actor->width;
532     }
533
534     x = path_line(x0, x1, t);    
535     y = -scene.wind_angle * (x - x0) + actor->y;
536     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
537
538     //fprintf(stderr, "change cloud t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
539     actor_set_position_full(actor->widget, x, y, actor->z);
540     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
541     set_actor_scale(actor, (double)scale/100, (double)scale/100);
542     if ((y < y1 || y > y0) || t >= 1){
543         /* stop animation */
544         actor->visible = FALSE;
545         destroy_hildon_actor(actor);
546         actor->time_start_animation = sec + fast_rnd(300);
547         actor->y = fast_rnd(300);
548     }
549
550 }
551
552 void
553 change_wind(Actor *actor, AWallpaperPlugin *desktop_plugin)
554 {
555     scene.wind_orientation = fast_rnd(2);
556     if (scene.wind_orientation == 0) scene.wind_orientation = -1;
557     scene.wind_angle = (double)(fast_rnd(200) - 100) / 100;
558     actor->time_start_animation = time(NULL) + (fast_rnd(10) + 10) * 60;
559     //fprintf(stderr, "change wind orient = %d angle = %f after = %d\n", scene.wind_orientation, scene.wind_angle, actor->time_start_animation-time(NULL));
560 }
561
562 void 
563 change_window1(Actor * actor, AWallpaperPlugin *desktop_plugin)
564 {
565     gint now = time(NULL);
566     if (scene.daytime == TIME_DAY){
567         if (actor->widget){
568             actor->visible = FALSE;
569             destroy_hildon_actor(actor);
570         }
571         actor->time_start_animation = 0;
572         return;
573     }else {
574         if (!actor->widget)
575             create_hildon_actor(actor, desktop_plugin);
576         if (actor->time_start_animation == 0){
577             actor->time_start_animation = now + fast_rnd(30);
578             return;
579         }
580     }
581
582     if (!actor->visible)
583         actor->visible = TRUE;
584     else 
585         actor->visible = FALSE;
586     //hildon_animation_actor_set_show(
587       //      HILDON_ANIMATION_ACTOR(actor->widget), 
588         //    actor->visible
589     //);
590     set_actor_visible(actor, actor->visible);
591     actor->time_start_animation = now + fast_rnd(60) + 10;
592
593 }
594
595 void 
596 change_signal(Actor * actor, AWallpaperPlugin *desktop_plugin)
597 {
598     gint now = time(NULL);
599     Actor *a;
600 #if 0
601     gchar *newfile;
602     gint now = time(NULL);
603     newfile = g_strdup_printf("%s%d.png", actor->name, scene.daytime); 
604     if (!strcmp(actor->filename, "red.png"))
605         newfile = g_strdup_printf("%s", "green.png");
606     else 
607         newfile = g_strdup_printf("%s", "red.png");
608     g_free(actor->filename);
609     actor->filename = newfile;
610     change_hildon_actor(actor, desktop_plugin);
611     actor->time_start_animation = now + fast_rnd(30) + 10;
612 #endif
613     a = g_ptr_array_index(actor->child, 0);
614     //fprintf(stderr, "actor name= %p \n", child->widget);
615     if (a->visible)
616         a->visible = FALSE;
617     else 
618         a->visible = TRUE;
619     //hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(a->widget), a->visible);
620     set_actor_visible(a, a->visible);
621     
622     a = g_ptr_array_index(actor->child, 1);
623     //fprintf(stderr, "actor name= %s \n", child->name);
624     if (a->visible)
625         a->visible = FALSE;
626     else 
627         a->visible = TRUE;
628     //hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(a->widget), a->visible);
629     set_actor_visible(a, a->visible);
630
631     actor->time_start_animation = now + fast_rnd(30) + 10;
632 }
633
634 void
635 set_actor_scale(Actor *actor, double scalex, double scaley)
636 {
637     hildon_animation_actor_set_scale(
638             HILDON_ANIMATION_ACTOR(actor->widget), 
639             scalex, 
640             scaley
641     );
642
643 }
644
645 void 
646 set_actor_visible(Actor *actor, gboolean visible)
647 {
648     hildon_animation_actor_set_show(HILDON_ANIMATION_ACTOR(actor->widget), visible);
649 }
650 void
651 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
652 {
653   GtkWidget *ha = NULL;
654   GdkPixbuf *pixbuf = NULL;
655   GtkWidget *image = NULL;
656   gchar     *str = NULL;
657
658   ha = hildon_animation_actor_new();
659   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
660                         desktop_plugin->priv->theme, actor->filename);
661   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
662                                              actor->width, 
663                                              actor->height, 
664                                              NULL);
665   /*fprintf(stderr, "create_hildon_actor %s %s\n", actor->name, str);*/
666   if (str)
667       g_free(str);
668   if (pixbuf){
669       image = gtk_image_new_from_pixbuf (pixbuf);
670       g_object_unref(G_OBJECT(pixbuf));
671   }
672   if (image){
673     g_signal_connect(G_OBJECT(image), "expose_event",
674                            G_CALLBACK(expose_event), pixbuf);
675     gtk_container_add (GTK_CONTAINER (ha), image);
676   }  
677   actor_set_position_full(ha, actor->x, actor->y, actor->z);
678   hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(ha), (double)actor->scale/100, (double)actor->scale/100);
679   realize(ha);
680   gtk_widget_show_all(ha);
681   hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(ha), actor->visible);
682   
683   /* TO DO check it */
684   /*  gdk_flush (); */
685
686   //g_object_set_data(G_OBJECT(ha), "name", name);
687   //g_object_set_data(G_OBJECT(ha), "filename", filename);
688   g_object_set_data(G_OBJECT(ha), "image", image);
689   /*
690   g_object_set_data(G_OBJECT(ha), "x", x);
691   g_object_set_data(G_OBJECT(ha), "y", y);
692   g_object_set_data(G_OBJECT(ha), "z", z);
693   g_object_set_data(G_OBJECT(ha), "width", width);
694   g_object_set_data(G_OBJECT(ha), "height", height);
695   g_object_set_data(G_OBJECT(ha), "scale", scale);
696   g_object_set_data(G_OBJECT(ha), "visible", visible);
697   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
698   g_object_set_data(G_OBJECT(ha), "func", pfunc);
699   */
700   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
701   actor->widget = ha;
702 }
703
704
705 void
706 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
707 {
708     GtkWidget *image = NULL;
709     GdkPixbuf *pixbuf = NULL;
710     gchar     *str = NULL;
711
712     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
713                             desktop_plugin->priv->theme, actor->filename);
714  
715     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
716                                                actor->width, 
717                                                actor->height, 
718                                                NULL);
719     if(str)
720         g_free(str);
721     if (pixbuf){
722         image = gtk_image_new_from_pixbuf (pixbuf);
723         g_object_unref(G_OBJECT(pixbuf));
724     }
725     if (image){ 
726         g_signal_connect(G_OBJECT(image), "expose_event",
727                                        G_CALLBACK(expose_event), pixbuf);
728         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
729             gtk_container_remove(GTK_CONTAINER(actor->widget), g_object_get_data(G_OBJECT(actor->widget), "image"));  
730         }
731         g_object_set_data(G_OBJECT(actor->widget), "image", image);
732         gtk_container_add (GTK_CONTAINER (actor->widget), image);
733         realize(actor->widget);
734         gtk_widget_show_all(actor->widget);
735         /* TO DO check it */
736        /*  gdk_flush (); */
737
738
739     }
740
741 }
742
743
744 void 
745 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
746 {
747     gchar *newfile;
748     newfile = g_strdup_printf("%s%d.png", actor->name, scene.daytime); 
749     if (actor->filename)
750             g_free(actor->filename);
751     actor->filename = newfile;
752     change_hildon_actor(actor, desktop_plugin);
753 }
754
755 void 
756 change_static_actor_with_corner(Actor * actor, AWallpaperPlugin *desktop_plugin)
757 {
758     gchar buffer[2048];
759
760     if (desktop_plugin->priv->right_corner)
761         gtk_widget_destroy(desktop_plugin->priv->right_corner);
762     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/town%i_right_corner.png", \
763                                   THEME_PATH, desktop_plugin->priv->theme, scene.daytime);
764     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
765     if (desktop_plugin->priv->right_corner){
766         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
767         gtk_widget_show (desktop_plugin->priv->right_corner);
768     }
769     change_static_actor(actor, desktop_plugin);
770
771 }
772
773 void
774 change_layer(Actor * actor, AWallpaperPlugin *desktop_plugin)
775 {
776     gint y;
777     Actor *a;
778
779     if (!desktop_plugin->priv->rich_animation) return;
780
781     a = g_ptr_array_index(actor->child, 0);
782     y = a->y + 10;
783     if (y > 480) y = -480;
784     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
785     actor_set_position_full(a->widget, a->x, y, a->z);
786     a->y = y;
787     
788     a = g_ptr_array_index(actor->child, 1);
789     y = a->y + 10;
790     if (y > 480) y = -480;
791     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
792     actor_set_position_full(a->widget, a->x, y, a->z);
793     a->y = y;
794
795     a = g_ptr_array_index(actor->child, 2);
796     y = a->y + 20;
797     if (y > 480) y = -480;
798     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
799     actor_set_position_full(a->widget, a->x, y, a->z);
800     a->y = y;
801
802     a = g_ptr_array_index(actor->child, 3);
803     y = a->y + 20;
804     if (y > 480) y = -480;
805     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
806     actor_set_position_full(a->widget, a->x, y, a->z);
807     a->y = y;
808
809 }
810
811 void
812 change_layer1(Actor * actor, AWallpaperPlugin *desktop_plugin)
813 {
814     gint y;
815     y = actor->y + 10;
816     if (y > 480) y = -480;
817     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
818     actor_set_position_full(actor->widget, actor->x, y, actor->z);
819     actor->y = y;
820 }
821
822 void
823 change_layer2(Actor * actor, AWallpaperPlugin *desktop_plugin)
824 {
825     gint y;
826     y = actor->y + 15;
827     if (y >= 480) y = -480;
828     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
829     actor_set_position_full(actor->widget, actor->x, y, actor->z);
830     actor->y = y;
831 }
832
833 /*
834 static gint 
835 get_time(gint t){
836     // уравнение изменения времени
837     return t*1.1;
838 }
839 */
840 static void 
841 destroy_scene(void){
842     GSList * tmp = scene.actors;
843     Actor *actor;
844     while (tmp != NULL){
845         actor = tmp->data;
846         if (actor){
847             if (actor->child){
848                 g_ptr_array_free(actor->child, TRUE);
849             }
850             if (actor->filename)
851                 g_free(actor->filename);
852             if (actor->name)
853                 g_free(actor->name);
854             gtk_widget_destroy(actor->widget);
855             //actor->widget = NULL;
856             g_free(actor);
857         }
858         tmp = g_slist_next(tmp);
859     }
860     g_slist_free(tmp);
861
862 }
863
864 void
865 reload_scene(AWallpaperPlugin *desktop_plugin)
866 {
867     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
868     destroy_scene();
869     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
870         init_scene(desktop_plugin);
871     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
872         init_scene1(desktop_plugin);
873     else if (!strcmp(desktop_plugin->priv->theme,"Matrix")) 
874         init_scene2(desktop_plugin);
875
876 }
877
878 static void
879 init_scene2(AWallpaperPlugin *desktop_plugin)
880 {
881   Actor *actor;
882   GPtrArray *child;
883   gint now = time(NULL);
884   gint y1, y2;
885
886   if (desktop_plugin->priv->rich_animation){
887       y1 = -480;
888       y2 = -480-480;
889   }else {
890       y1 = 0;
891       y2 = -480;
892   }
893
894   fprintf(stderr, "init scene2 \n");
895   //scene.daytime = get_daytime();
896   scene.actors = NULL;
897
898   
899   actor = init_object(desktop_plugin, "background", "bg.png", 
900                       0, 0, 5, 800, 480, 
901                       TRUE, TRUE, 100, 255, 
902                       NULL, NULL, NULL);
903   scene.actors = g_slist_append(scene.actors, actor);
904
905   actor = init_object(desktop_plugin, "symbols", "symbols.png", 
906                       0, 0, 10, 800, 480, 
907                       TRUE, TRUE, 100, 255, 
908                       NULL, NULL, NULL);
909   scene.actors = g_slist_append(scene.actors, actor);
910
911   child = g_ptr_array_sized_new(4);
912   actor = init_object(desktop_plugin, "layer1", "layer1_2.png", 
913                       0, y1, 6, 800, 960, 
914                       TRUE, TRUE, 100, 255, 
915                       NULL, NULL, NULL);
916   //actor->time_start_animation = now;
917   //actor->duration_animation = G_MAXINT;
918   scene.actors = g_slist_append(scene.actors, actor);
919   g_ptr_array_add(child, actor);
920
921   actor = init_object(desktop_plugin, "layer1", "layer1_1.png", 
922                       0, y2, 7, 800, 960, 
923                       TRUE, TRUE, 100, 255, 
924                       NULL, NULL, NULL);
925   //actor->time_start_animation = now;
926   //actor->duration_animation = G_MAXINT;
927   scene.actors = g_slist_append(scene.actors, actor);
928   g_ptr_array_add(child, actor);
929
930   actor = init_object(desktop_plugin, "layer2", "layer2_2.png", 
931                       0, y1, 8, 800, 960, 
932                       TRUE, TRUE, 100, 255, 
933                       NULL, NULL, NULL);
934   //actor->time_start_animation = now;
935   //actor->duration_animation = G_MAXINT;
936   scene.actors = g_slist_append(scene.actors, actor);
937   g_ptr_array_add(child, actor);
938
939   actor = init_object(desktop_plugin, "layer2", "layer2_1.png", 
940                       0, y2, 9, 800, 960, 
941                       TRUE, TRUE, 100, 255, 
942                       NULL, NULL, NULL);
943   //actor->time_start_animation = now;
944   //actor->duration_animation = G_MAXINT;
945   scene.actors = g_slist_append(scene.actors, actor);
946   g_ptr_array_add(child, actor);
947
948   actor = init_object(desktop_plugin, "layers", "", 
949                       0, y2, 9, 800, 960, 
950                       FALSE, FALSE, 100, 255, 
951                       (gpointer)&change_layer, NULL, child);
952   actor->time_start_animation = now;
953   actor->duration_animation = G_MAXINT;
954   scene.actors = g_slist_append(scene.actors, actor);
955
956   run_long_timeout(desktop_plugin);
957
958 }
959 /* Init Modern Scene */
960 static void
961 init_scene(AWallpaperPlugin *desktop_plugin)
962 {
963   Actor *actor;
964   gint now = time(NULL);
965   gint i;
966   gint winds[13][2];
967
968   /* fprintf(stderr, "init scene \n");*/
969   scene.daytime = get_daytime();
970   scene.actors = NULL;
971   scene.wind_orientation = -1;
972   scene.wind_angle = 0.3;
973   /* init value for random */
974   scene.seed = time(NULL);
975
976   actor = init_object(desktop_plugin, "sky", "sky0.png", 
977                       0, 0, 5, 800, 480, 
978                       TRUE , TRUE, 100, 255, 
979                       (gpointer)&change_static_actor, NULL, NULL);
980   change_static_actor(actor, desktop_plugin);
981   scene.actors = g_slist_append(scene.actors, actor);
982
983   actor = init_object(desktop_plugin, "sun", "sun.png", 
984                       0, 0, 6, 88, 88, 
985                       FALSE, FALSE, 100, 255, 
986                       (gpointer)&change_sun, (gpointer)&probability_sun, NULL);
987   actor->time_start_animation = now;
988   actor->duration_animation = G_MAXINT;
989   change_sun(actor, desktop_plugin);
990   scene.actors = g_slist_append(scene.actors, actor);
991
992   //actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
993     //                  TRUE, 100, 255, NULL, NULL);
994   //scene.actors = g_slist_append(scene.actors, actor);
995   
996   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 
997                       0, fast_rnd(300)-97, 7, 150, 97, 
998                       FALSE, FALSE, 100, 255, 
999                       (gpointer)&change_cloud, NULL, NULL);
1000   actor->time_start_animation = now + fast_rnd(20);
1001   actor->duration_animation = 3*60;
1002   scene.actors = g_slist_append(scene.actors, actor);
1003   
1004   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 
1005                       0, fast_rnd(300)-75, 7, 188, 75, 
1006                       FALSE, FALSE, 100, 255, 
1007                       (gpointer)&change_cloud, NULL, NULL);
1008   actor->time_start_animation = now + fast_rnd(40)+10;
1009   actor->duration_animation = 3*60;
1010   scene.actors = g_slist_append(scene.actors, actor);
1011
1012   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 
1013                       0, fast_rnd(300)-75, 7, 150, 75, 
1014                       FALSE, FALSE, 100, 255, 
1015                       (gpointer)&change_cloud, NULL, NULL);
1016   actor->time_start_animation = now + fast_rnd(60) + 20;
1017   actor->duration_animation = 5*60;
1018   scene.actors = g_slist_append(scene.actors, actor);
1019
1020
1021   actor = init_object(desktop_plugin, "town", "town0.png", 
1022                       0, 0, 8, 800, 480, 
1023                       TRUE, TRUE, 100, 255, 
1024                       (gpointer)&change_static_actor, NULL, NULL);
1025   change_static_actor(actor, desktop_plugin);
1026   scene.actors = g_slist_append(scene.actors, actor);
1027
1028   actor = init_object(desktop_plugin, "tram", "tram.png", 
1029                       -300, 225, 9, 350, 210, 
1030                       FALSE, FALSE, 100, 255, 
1031                       (gpointer)&change_tram, NULL, NULL);
1032   actor->time_start_animation = time(NULL) + fast_rnd(10); 
1033   actor->duration_animation = 60;
1034   scene.actors = g_slist_append(scene.actors, actor);
1035
1036   actor = init_object(desktop_plugin, "border", "border0.png", 
1037                       0, 480-79, 10, 800, 79,
1038                       TRUE, TRUE, 100, 255, 
1039                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1040   change_static_actor_with_corner(actor, desktop_plugin);
1041   scene.actors = g_slist_append(scene.actors, actor);
1042   
1043   actor = init_object(desktop_plugin, "moon", "moon1.png", 
1044                       400, 20, 6, 60, 60, 
1045                       FALSE, FALSE, 100, 255, 
1046                       (gpointer)&change_moon, NULL, NULL);
1047   change_moon(actor, desktop_plugin);
1048   scene.actors = g_slist_append(scene.actors, actor);
1049
1050   actor = init_object(desktop_plugin, "wind", "", 
1051                       0, 0, 5, 0, 0, 
1052                       FALSE, FALSE, 100, 255, 
1053                       (gpointer)&change_wind, NULL, NULL);
1054   change_wind(actor, desktop_plugin);
1055   scene.actors = g_slist_append(scene.actors, actor);
1056
1057     /* windows in 4-th house  */
1058
1059     winds[0][0] = 482;
1060     winds[0][1] = 180;
1061
1062     winds[1][0] = 495;
1063     winds[1][1] = 179;
1064
1065     winds[2][0] = 482;
1066     winds[2][1] = 191;
1067
1068     winds[3][0] = 495;
1069     winds[3][1] = 190;
1070     
1071     winds[4][0] = 482;
1072     winds[4][1] = 201;
1073     
1074     winds[5][0] = 495;
1075     winds[5][1] = 210;
1076     
1077     winds[6][0] = 482;
1078     winds[6][1] = 222;
1079     
1080     winds[7][0] = 495;
1081     winds[7][1] = 221;
1082     
1083     winds[8][0] = 459;
1084     winds[8][1] = 203;
1085     
1086     winds[9][0] = 495;
1087     winds[9][1] = 241;
1088     
1089     winds[10][0] = 495;
1090     winds[10][1] = 252;
1091     
1092     winds[11][0] = 482;
1093     winds[11][1] = 273;
1094     
1095     winds[12][0] = 495;
1096     winds[12][1] = 303;
1097     for (i=0; i<13; i++){
1098         actor = init_object(desktop_plugin, "window1", "window1.png", 
1099                             winds[i][0], winds[i][1], 8, 8, 10, 
1100                             FALSE, FALSE, 100, 255, 
1101                             (gpointer)&change_window1, NULL, NULL);
1102         //change_window1(actor, desktop_plugin);
1103         actor->time_start_animation = now + fast_rnd(30);
1104         scene.actors = g_slist_append(scene.actors, actor);
1105
1106     }
1107     
1108     /* windows in 1-th house  */
1109     
1110     winds[0][0] = 86;
1111     winds[0][1] = 321;
1112
1113     winds[1][0] = 86;
1114     winds[1][1] = 363;
1115
1116     winds[2][0] = 86;
1117     winds[2][1] = 385;
1118
1119     winds[3][0] = 86;
1120     winds[3][1] = 286;
1121     
1122     winds[4][0] = 94;
1123     winds[4][1] = 232;
1124     
1125     winds[5][0] = 94;
1126     winds[5][1] = 243;
1127     
1128     winds[6][0] = 94;
1129     winds[6][1] = 265;
1130     
1131     winds[7][0] = 94;
1132     winds[7][1] = 331;
1133     for (i=0; i<8; i++){
1134         actor = init_object(desktop_plugin, "window2", "window2.png", 
1135                             winds[i][0], winds[i][1], 8, 8, 10, 
1136                             FALSE, FALSE, 100, 255, 
1137                             (gpointer)&change_window1, NULL, NULL);
1138         //change_window1(actor, desktop_plugin);
1139         actor->time_start_animation = now + fast_rnd(30);
1140         scene.actors = g_slist_append(scene.actors, actor);
1141
1142     }
1143     
1144     /* windows in 3-th house  */
1145     
1146     winds[0][0] = 251;
1147     winds[0][1] = 162;
1148
1149     winds[1][0] = 251;
1150     winds[1][1] = 196;
1151
1152     winds[2][0] = 251;
1153     winds[2][1] = 278;
1154
1155     winds[3][0] = 251;
1156     winds[3][1] = 289;
1157     
1158     winds[4][0] = 313;
1159     winds[4][1] = 173;
1160     
1161     winds[5][0] = 322;
1162     winds[5][1] = 160;
1163     
1164     winds[6][0] = 303;
1165     winds[6][1] = 217;
1166     
1167     winds[7][0] = 322;
1168     winds[7][1] = 224;
1169     
1170     winds[8][0] = 323;
1171     winds[8][1] = 217;
1172     
1173     winds[9][0] = 322;
1174     winds[9][1] = 288;
1175     
1176     for (i=0; i<10; i++){
1177         actor = init_object(desktop_plugin, "window3", "window3.png", 
1178                             winds[i][0], winds[i][1], 8, 8, 10, 
1179                             FALSE, FALSE, 100, 255, 
1180                             (gpointer)&change_window1, NULL, NULL);
1181         //change_window1(actor, desktop_plugin);
1182         actor->time_start_animation = now + fast_rnd(30);
1183         scene.actors = g_slist_append(scene.actors, actor);
1184
1185     }
1186
1187     /* windows in 5-th house  */
1188     
1189     winds[0][0] = 610;
1190     winds[0][1] = 224;
1191
1192     winds[1][0] = 602;
1193     winds[1][1] = 245;
1194
1195     winds[2][0] = 602;
1196     winds[2][1] = 264;
1197
1198     winds[3][0] = 610;
1199     winds[3][1] = 301;
1200     
1201     winds[4][0] = 610;
1202     winds[4][1] = 320;
1203     
1204     winds[5][0] = 593;
1205     winds[5][1] = 352;
1206     
1207     winds[6][0] = 610;
1208     winds[6][1] = 368;
1209     
1210     for (i=0; i<7; i++){
1211         actor = init_object(desktop_plugin, "window4", "window4.png", 
1212                             winds[i][0], winds[i][1], 8, 8, 10, 
1213                             FALSE, FALSE, 100, 255, 
1214                             (gpointer)&change_window1, NULL, NULL);
1215         //change_window1(actor, desktop_plugin);
1216         actor->time_start_animation = now + fast_rnd(30);
1217         scene.actors = g_slist_append(scene.actors, actor);
1218
1219     }
1220
1221     /* windows in 6-th house  */
1222     
1223     winds[0][0] = 717;
1224     winds[0][1] = 283;
1225
1226     winds[1][0] = 698;
1227     winds[1][1] = 293;
1228
1229     winds[2][0] = 717;
1230     winds[2][1] = 315;
1231
1232     winds[3][0] = 717;
1233     winds[3][1] = 323;
1234     
1235     winds[4][0] = 698;
1236     winds[4][1] = 362;
1237     
1238     winds[5][0] = 698;
1239     winds[5][1] = 400;
1240     
1241     for (i=0; i<6; i++){
1242         actor = init_object(desktop_plugin, "window5", "window5.png", 
1243                             winds[i][0], winds[i][1], 8, 8, 10, 
1244                             FALSE, FALSE, 100, 255, 
1245                             (gpointer)&change_window1, NULL, NULL);
1246         //change_window1(actor, desktop_plugin);
1247         actor->time_start_animation = now + fast_rnd(30);
1248         scene.actors = g_slist_append(scene.actors, actor);
1249
1250     }
1251
1252     run_long_timeout(desktop_plugin);
1253
1254 #if 0    
1255   anim = g_new0(Animation, 1);
1256   anim->count = 1;
1257   anim->actor = actor;
1258   anim->func_change = &change_tram;
1259   anim->func_time = NULL;
1260   anim->timestart = time(NULL); 
1261   anim->timeall = 10;
1262   
1263   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
1264 #endif  
1265  }
1266
1267 /* Init Berlin Scene */
1268 static void
1269 init_scene1(AWallpaperPlugin *desktop_plugin)
1270 {
1271   Actor *actor, *actor1, *actor2;
1272   gint now = time(NULL);
1273   gint i; 
1274   gint winds[13][2];
1275   GPtrArray *child = NULL;
1276
1277   scene.daytime = get_daytime();
1278   scene.actors = NULL;
1279   scene.wind_orientation = -1;
1280   scene.wind_angle = 0.3;
1281   /* init value for random */
1282   scene.seed = time(NULL);
1283
1284   
1285   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
1286                       TRUE, TRUE, 100, 255, 
1287                       (gpointer)&change_static_actor, NULL, NULL);
1288   change_static_actor(actor, desktop_plugin);
1289   scene.actors = g_slist_append(scene.actors, actor);
1290
1291   
1292   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
1293                       FALSE, FALSE, 100, 255, 
1294                       (gpointer)&change_sun, (gpointer)&probability_sun, NULL);
1295   actor->time_start_animation = time(NULL);
1296   actor->duration_animation = G_MAXINT;
1297   change_sun(actor, desktop_plugin);
1298   scene.actors = g_slist_append(scene.actors, actor);
1299
1300 #if 0
1301   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
1302                       TRUE, 100, 255, NULL, NULL);
1303   scene.actors = g_slist_append(scene.actors, actor);
1304 #endif
1305
1306   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
1307                       FALSE, FALSE, 100, 255, 
1308                       (gpointer)&change_moon, NULL, NULL);
1309   change_moon(actor, desktop_plugin);
1310   scene.actors = g_slist_append(scene.actors, actor);
1311   
1312   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, fast_rnd(300)-97, 7, 150, 97, 
1313                       FALSE, FALSE, 100, 255, 
1314                       (gpointer)&change_cloud, NULL, NULL);
1315   actor->time_start_animation = now + fast_rnd(30) + 10;
1316   actor->duration_animation = 3*60;
1317   scene.actors = g_slist_append(scene.actors, actor);
1318   
1319   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, fast_rnd(300)-75, 7, 188, 75, 
1320                       FALSE, FALSE, 100, 255, 
1321                       (gpointer)&change_cloud, NULL, NULL);
1322   actor->time_start_animation = now + fast_rnd(10);
1323   actor->duration_animation = 3*60;
1324   scene.actors = g_slist_append(scene.actors, actor);
1325
1326   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, fast_rnd(300)-75, 7, 150, 75, 
1327                       FALSE, FALSE, 100, 255, 
1328                       (gpointer)&change_cloud, NULL, NULL);
1329   actor->time_start_animation = now + fast_rnd(60) + 20;
1330   actor->duration_animation = 5*60;
1331   scene.actors = g_slist_append(scene.actors, actor);
1332
1333  
1334   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 8, 160, 50, 
1335                       FALSE, FALSE, 100, 255, 
1336                       (gpointer)&change_plane2, NULL, NULL);
1337   actor->time_start_animation = now + fast_rnd(40) + 20;
1338   actor->duration_animation = 60;
1339   scene.actors = g_slist_append(scene.actors, actor);
1340   
1341   actor = init_object(desktop_plugin, "plane1", "tu154.png", 620, 233, 9, 300, 116, 
1342                       FALSE, FALSE, 100, 255, 
1343                       (gpointer)&change_plane1, NULL, NULL);
1344   actor->time_start_animation = now + fast_rnd(20);
1345   actor->duration_animation = 30;
1346   scene.actors = g_slist_append(scene.actors, actor);
1347
1348   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
1349                       TRUE, TRUE, 100, 255, 
1350                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1351   change_static_actor_with_corner(actor, desktop_plugin);
1352   scene.actors = g_slist_append(scene.actors, actor);
1353
1354   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
1355                       FALSE, FALSE, 100, 255, 
1356                       (gpointer)&change_wind, NULL, NULL);
1357   change_wind(actor, desktop_plugin);
1358   scene.actors = g_slist_append(scene.actors, actor);
1359
1360   actor1 = init_object(desktop_plugin, "signal_red", "red.png", 
1361                       486, 425, 10, 18, 38, 
1362                       FALSE, TRUE, 100, 255, NULL, NULL, NULL);
1363   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1364   scene.actors = g_slist_append(scene.actors, actor1);
1365    
1366   actor2 = init_object(desktop_plugin, "signal_green", "green.png", 
1367                       486, 425, 10, 18, 38, 
1368                       TRUE, TRUE, 100, 255, NULL, NULL, NULL);
1369   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1370   scene.actors = g_slist_append(scene.actors, actor2);
1371   child = g_ptr_array_sized_new(2);
1372   g_ptr_array_add(child, actor1);
1373   g_ptr_array_add(child, actor2);
1374   actor = init_object(desktop_plugin, "signal", "",
1375                       486, 425, 10, 18, 38,
1376                       FALSE, FALSE, 100, 255, 
1377                       (gpointer)&change_signal, NULL, child);
1378   actor->time_start_animation = now + fast_rnd(30) + 10;
1379   scene.actors = g_slist_append(scene.actors, actor);
1380     
1381     winds[0][0] = 389;
1382     winds[0][1] = 305;
1383
1384     winds[1][0] = 373;
1385     winds[1][1] = 306;
1386
1387     winds[2][0] = 355;
1388     winds[2][1] = 306;
1389
1390     winds[3][0] = 356;
1391     winds[3][1] = 288;
1392     
1393     winds[4][0] = 337;
1394     winds[4][1] = 269;
1395     
1396     winds[5][0] = 372;
1397     winds[5][1] = 268;
1398   
1399     winds[6][0] = 372;
1400     winds[6][1] = 249;
1401     
1402     winds[7][0] = 388;
1403     winds[7][1] = 249;
1404     
1405     winds[8][0] = 387;
1406     winds[8][1] = 230;
1407     
1408     winds[9][0] = 372;
1409     winds[9][1] = 211;
1410     
1411     winds[10][0] = 355;
1412     winds[10][1] = 159;
1413     
1414     winds[11][0] = 335;
1415     winds[11][1] = 158;
1416     
1417     winds[12][0] = 386;
1418     winds[12][1] = 119;
1419   
1420     for (i=0; i<13; i++){
1421         actor = init_object(desktop_plugin, "window", "window.png", 
1422                             winds[i][0], winds[i][1], 10, 8, 9, 
1423                             FALSE, TRUE, 100, 255, 
1424                             (gpointer)&change_window1, NULL, NULL);
1425         //change_window1(actor, desktop_plugin);
1426         actor->time_start_animation = now + fast_rnd(30);
1427         scene.actors = g_slist_append(scene.actors, actor);
1428
1429     }
1430     
1431     run_long_timeout(desktop_plugin);
1432
1433 }
1434
1435
1436 void 
1437 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
1438 {
1439     gint y0 = 365;// - уровень горизонта
1440     *x = (int)(azm * 800) - 64;
1441     *y = (int)((1 - alt) * y0) - 64;
1442     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
1443 }
1444 #if 0
1445 static void 
1446 change_actor(GtkWidget * actor)
1447 {
1448     char * name;
1449     gint x, y, daytime, scale;
1450     gdouble sc;
1451     double alt, azm;
1452
1453     GtkWidget *image;
1454     GdkPixbuf *pixbuf;
1455
1456     void (*pfunc)(gpointer, gpointer);
1457
1458     name = g_object_get_data(G_OBJECT(actor), "name");
1459     fprintf(stderr, "change actor %s\n", name);
1460     if (name == "sun"){
1461         pfunc = g_object_get_data(G_OBJECT(actor), "func");
1462         if (pfunc)
1463             (*pfunc)(actor, g_strdup(name));
1464         daytime = get_daytime();
1465         if (daytime != TIME_NIGHT){
1466             hildon_animation_actor_set_show(actor, 1);
1467             get_sun_pos(&alt, &azm);
1468             get_sun_screen_pos(alt, azm, &x, &y);
1469             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1470         }
1471     }
1472     
1473     if (name == "cloud1"){
1474         x = g_object_get_data(G_OBJECT(actor), "x");
1475         y = g_object_get_data(G_OBJECT(actor), "y");
1476         scale = g_object_get_data(G_OBJECT(actor), "scale");
1477
1478         /* Start */
1479         image = g_object_get_data(G_OBJECT(actor), "image");
1480         
1481         gtk_container_remove(actor, image);  
1482         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
1483                                              200, 
1484                                              200, 
1485                                              NULL);
1486         if (pixbuf){
1487               image = gtk_image_new_from_pixbuf (pixbuf);
1488               g_object_unref(G_OBJECT(pixbuf));
1489         }
1490         g_signal_connect(G_OBJECT(image), "expose_event",
1491                                    G_CALLBACK(expose_event), pixbuf);
1492         gtk_container_add (GTK_CONTAINER (actor), image);
1493         realize(actor);
1494         gtk_widget_show_all(actor);
1495         /* End*/
1496
1497             
1498         x += 40;
1499         y -= 20;
1500         scale -= 10;
1501         if (x > 500){
1502             x = 400;
1503             y = 150;
1504             sc = 1;
1505         }
1506         sc = (double)scale / 100;
1507         hildon_animation_actor_set_scale(actor, sc, sc);
1508         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
1509         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1510         g_object_set_data(G_OBJECT(actor), "x", x);
1511         g_object_set_data(G_OBJECT(actor), "y", y);
1512         g_object_set_data(G_OBJECT(actor), "scale", scale);
1513     }
1514
1515 }
1516 #endif
1517 static gboolean
1518 short_timeout (AWallpaperPlugin *desktop_plugin)
1519 {
1520     //gint daytime = get_daytime();
1521     GSList * tmp;
1522     void (*pfunc)(gpointer, gpointer);
1523     time_t now;
1524     Actor *actor;
1525     gboolean stop_flag = TRUE;
1526
1527 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
1528         desktop_plugin->priv->short_timer = 0;
1529         return FALSE;
1530     }
1531
1532     now = time(NULL);
1533     //scene.daytime = daytime;
1534     /* fprintf(stderr, "Short timer %d\n", now); */
1535     tmp = scene.actors;
1536     while (tmp != NULL){
1537            actor = tmp->data;
1538            if (now >= actor->time_start_animation  
1539                && actor->time_start_animation > 0
1540                /* && now - actor->time_start_animation <= actor->duration_animation*/){
1541                 pfunc = actor->func_change;
1542                 if (pfunc){ 
1543                     (*pfunc)(actor, desktop_plugin);
1544                     stop_flag = FALSE;
1545                 }
1546             }
1547             tmp = g_slist_next(tmp);
1548     }
1549     if (stop_flag){
1550          desktop_plugin->priv->short_timer = 0;
1551          return FALSE;
1552     }else
1553          return TRUE; /* keep running this event */
1554 }
1555
1556 void
1557 run_long_timeout(AWallpaperPlugin *desktop_plugin)
1558 {
1559
1560     gint daytime = get_daytime();
1561     GSList * tmp;
1562     void (*pfunc)(gpointer, gpointer);
1563     time_t now;
1564     Actor *actor;
1565
1566
1567     //fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer);
1568     if (scene.daytime != daytime){
1569         scene.daytime = daytime;
1570         tmp = scene.actors;
1571         while (tmp != NULL){
1572             //change_actor(tmp->data);
1573             pfunc =((Actor*)tmp->data)->func_change;
1574             if (pfunc){
1575                 (*pfunc)(tmp->data, desktop_plugin);
1576             }
1577             tmp = g_slist_next(tmp);
1578         }
1579     }
1580    
1581     now = time(NULL);
1582     //fprintf(stderr, "Now  %d\n", now);
1583     tmp = scene.actors;
1584     while (tmp != NULL){
1585         actor = tmp->data;
1586         if (now >= actor->time_start_animation  
1587             && actor->time_start_animation > 0
1588             && desktop_plugin->priv->short_timer == 0){
1589             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
1590             if (desktop_plugin->priv->rich_animation){
1591                 actor->time_start_animation = now;
1592                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
1593             }
1594             else {
1595                 (*actor->func_change)(actor, desktop_plugin);
1596             }
1597         }
1598         tmp = g_slist_next(tmp);
1599     }
1600  
1601 }
1602
1603 static gboolean
1604 long_timeout (AWallpaperPlugin *desktop_plugin)
1605 {
1606     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
1607     if (desktop_plugin->priv->long_timer == 0 )
1608         return FALSE;
1609     if (!desktop_plugin->priv->visible){
1610         if(desktop_plugin->priv->short_timer != 0){
1611             g_source_remove(desktop_plugin->priv->short_timer);
1612             desktop_plugin->priv->short_timer = 0;
1613         }
1614         desktop_plugin->priv->long_timer = 0;
1615         return FALSE;
1616     }
1617   
1618
1619     run_long_timeout(desktop_plugin);
1620     return TRUE; /* keep running this event */
1621 }
1622
1623 static void
1624 desktop_plugin_visible_notify (GObject    *object,
1625                                           GParamSpec *spec,
1626                                           AWallpaperPlugin *desktop_plugin)
1627 {
1628     gboolean visible;
1629     g_object_get (object, "is-on-current-desktop", &visible, NULL);
1630     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
1631     if (visible){
1632         desktop_plugin->priv->visible = TRUE;
1633         if (desktop_plugin->priv->long_timer == 0 ){
1634             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1635             run_long_timeout(desktop_plugin);
1636         }
1637     }else{
1638         desktop_plugin->priv->visible = FALSE;
1639         if (desktop_plugin->priv->long_timer != 0 ){
1640             g_source_remove(desktop_plugin->priv->long_timer);
1641             desktop_plugin->priv->long_timer = 0;
1642         }
1643     }
1644 }
1645
1646 gboolean
1647 rich_animation_press(GtkWidget *widget, GdkEvent *event,
1648                                             gpointer user_data){
1649     fprintf(stderr,"gggggggggggggggggggg2222\n");
1650     return FALSE;
1651 }    
1652 static void
1653 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
1654 {
1655     GtkWidget *rich_animation;
1656     gchar           buffer[2048];
1657
1658
1659     fprintf(stderr, "!!!!!!!plugin init \n");
1660     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1661     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1662     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
1663
1664     /* Load config */
1665     read_config(priv);
1666     /* Initialize DBUS */
1667     livewp_initialize_dbus(priv);
1668
1669     priv->desktop_plugin = desktop_plugin;
1670     priv->visible = TRUE;
1671     priv->short_timer = 0;
1672     //priv->theme = g_strdup("Modern");
1673     desktop_plugin->priv->main_widget = gtk_fixed_new();
1674
1675     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
1676     desktop_plugin->priv->right_corner = NULL;
1677     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
1678     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
1679     if (desktop_plugin->priv->right_corner){
1680         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
1681         gtk_widget_show (desktop_plugin->priv->right_corner);
1682     }
1683     /* Create rich animation event */
1684     rich_animation = gtk_event_box_new();
1685     if(rich_animation){
1686         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
1687         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
1688         gtk_widget_set_size_request(rich_animation, 100, 32);
1689         gtk_widget_show (rich_animation);
1690         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
1691         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
1692     }
1693 /*
1694     GtkWidget *label = gtk_label_new ("ddddddddddddd"); 
1695     gtk_widget_set_size_request(label, 95, 30);
1696     gtk_widget_show (label);
1697 //    gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1698     gtk_fixed_put(GTK_FIXED(widget), label, 0, 0);
1699 */
1700     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1701     g_signal_connect (desktop_plugin, "show-settings",
1702                              G_CALLBACK (show_settings), priv);
1703     gtk_widget_show (desktop_plugin->priv->main_widget);
1704     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
1705     init_applet_position();
1706
1707     
1708     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1709     if (!strcmp(priv->theme,"Modern"))
1710         init_scene(desktop_plugin);
1711     else if (!strcmp(priv->theme,"Berlin")) 
1712         init_scene1(desktop_plugin);
1713     else if (!strcmp(priv->theme, "Matrix"))
1714         init_scene2(desktop_plugin);
1715     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1716     /* TODO Move scene to priv */
1717     scene.timer_type = LONG_TIMER_TYPE;
1718     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1719                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1720
1721    
1722     //sleep(2);
1723 }
1724
1725 static void
1726 lw_applet_finalize (GObject *object)
1727 {
1728      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1729      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1730      
1731      fprintf(stderr,"finalaze %i\n", priv->long_timer);
1732      if (priv->long_timer){
1733         g_source_remove(priv->long_timer);
1734         priv->long_timer = 0;
1735      }
1736      if (priv->short_timer){
1737         g_source_remove(priv->short_timer);
1738         priv->short_timer = 0;
1739      }
1740
1741      destroy_scene();
1742 }
1743
1744 static void
1745 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1746
1747     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1748     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1749
1750     /* gobject */
1751     gobject_class->destroy = (gpointer)lw_applet_finalize;
1752     widget_class->realize = lw_applet_realize;
1753     widget_class->expose_event = lw_applet_expose_event;
1754
1755     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1756
1757 }
1758
1759 static void
1760 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
1761 }