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