changed matrix behavior when rich_animation off
[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     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     if (!desktop_plugin->priv->rich_animation) return;
759
760     a = g_ptr_array_index(actor->child, 0);
761     y = a->y + 10;
762     if (y > 480) y = -480;
763     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
764     actor_set_position_full(a->widget, a->x, y, a->z);
765     a->y = y;
766     
767     a = g_ptr_array_index(actor->child, 1);
768     y = a->y + 10;
769     if (y > 480) y = -480;
770     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
771     actor_set_position_full(a->widget, a->x, y, a->z);
772     a->y = y;
773
774     a = g_ptr_array_index(actor->child, 2);
775     y = a->y + 20;
776     if (y > 480) y = -480;
777     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
778     actor_set_position_full(a->widget, a->x, y, a->z);
779     a->y = y;
780
781     a = g_ptr_array_index(actor->child, 3);
782     y = a->y + 20;
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 }
789
790 void
791 change_layer1(Actor * actor, AWallpaperPlugin *desktop_plugin)
792 {
793     gint y;
794     y = actor->y + 10;
795     if (y > 480) y = -480;
796     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
797     actor_set_position_full(actor->widget, actor->x, y, actor->z);
798     actor->y = y;
799 }
800
801 void
802 change_layer2(Actor * actor, AWallpaperPlugin *desktop_plugin)
803 {
804     gint y;
805     y = actor->y + 15;
806     if (y >= 480) y = -480;
807     //fprintf(stderr, "!! %s - %d\n", actor->name, y);
808     actor_set_position_full(actor->widget, actor->x, y, actor->z);
809     actor->y = y;
810 }
811
812 /*
813 static gint 
814 get_time(gint t){
815     // уравнение изменения времени
816     return t*1.1;
817 }
818 */
819 static void 
820 destroy_scene(void){
821     GSList * tmp = scene.actors;
822     Actor *actor;
823     while (tmp != NULL){
824         actor = tmp->data;
825         if (actor){
826             if (actor->child){
827                 g_ptr_array_free(actor->child, TRUE);
828             }
829             if (actor->filename)
830                 g_free(actor->filename);
831             if (actor->name)
832                 g_free(actor->name);
833             gtk_widget_destroy(actor->widget);
834             //actor->widget = NULL;
835             g_free(actor);
836         }
837         tmp = g_slist_next(tmp);
838     }
839     g_slist_free(tmp);
840
841 }
842
843 void
844 reload_scene(AWallpaperPlugin *desktop_plugin)
845 {
846     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
847     destroy_scene();
848     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
849         init_scene(desktop_plugin);
850     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
851         init_scene1(desktop_plugin);
852     else if (!strcmp(desktop_plugin->priv->theme,"Matrix")) 
853         init_scene2(desktop_plugin);
854
855 }
856
857 static void
858 init_scene2(AWallpaperPlugin *desktop_plugin)
859 {
860   Actor *actor;
861   GPtrArray *child;
862   gint now = time(NULL);
863   gint y1, y2;
864
865   if (desktop_plugin->priv->rich_animation){
866       y1 = -480;
867       y2 = -480-480;
868   }else {
869       y1 = 0;
870       y2 = -480;
871   }
872
873   fprintf(stderr, "init scene2 \n");
874   //scene.daytime = get_daytime();
875   scene.actors = NULL;
876
877   
878   actor = init_object(desktop_plugin, "background", "bg.png", 
879                       0, 0, 5, 800, 480, 
880                       TRUE, TRUE, 100, 255, 
881                       NULL, NULL, NULL);
882   scene.actors = g_slist_append(scene.actors, actor);
883
884   actor = init_object(desktop_plugin, "symbols", "symbols.png", 
885                       0, 0, 10, 800, 480, 
886                       TRUE, TRUE, 100, 255, 
887                       NULL, NULL, NULL);
888   scene.actors = g_slist_append(scene.actors, actor);
889
890   child = g_ptr_array_sized_new(4);
891   actor = init_object(desktop_plugin, "layer1", "layer1_2.png", 
892                       0, y1, 6, 800, 960, 
893                       TRUE, TRUE, 100, 255, 
894                       NULL, NULL, NULL);
895   //actor->time_start_animation = now;
896   //actor->duration_animation = G_MAXINT;
897   scene.actors = g_slist_append(scene.actors, actor);
898   g_ptr_array_add(child, actor);
899
900   actor = init_object(desktop_plugin, "layer1", "layer1_1.png", 
901                       0, y2, 7, 800, 960, 
902                       TRUE, TRUE, 100, 255, 
903                       NULL, NULL, NULL);
904   //actor->time_start_animation = now;
905   //actor->duration_animation = G_MAXINT;
906   scene.actors = g_slist_append(scene.actors, actor);
907   g_ptr_array_add(child, actor);
908
909   actor = init_object(desktop_plugin, "layer2", "layer2_2.png", 
910                       0, y1, 8, 800, 960, 
911                       TRUE, TRUE, 100, 255, 
912                       NULL, NULL, NULL);
913   //actor->time_start_animation = now;
914   //actor->duration_animation = G_MAXINT;
915   scene.actors = g_slist_append(scene.actors, actor);
916   g_ptr_array_add(child, actor);
917
918   actor = init_object(desktop_plugin, "layer2", "layer2_1.png", 
919                       0, y2, 9, 800, 960, 
920                       TRUE, TRUE, 100, 255, 
921                       NULL, NULL, NULL);
922   //actor->time_start_animation = now;
923   //actor->duration_animation = G_MAXINT;
924   scene.actors = g_slist_append(scene.actors, actor);
925   g_ptr_array_add(child, actor);
926
927   actor = init_object(desktop_plugin, "layers", "", 
928                       0, y2, 9, 800, 960, 
929                       FALSE, FALSE, 100, 255, 
930                       (gpointer)&change_layer, NULL, child);
931   actor->time_start_animation = now;
932   actor->duration_animation = G_MAXINT;
933   scene.actors = g_slist_append(scene.actors, actor);
934
935   run_long_timeout(desktop_plugin);
936
937 }
938 /* Init Modern Scene */
939 static void
940 init_scene(AWallpaperPlugin *desktop_plugin)
941 {
942   Actor *actor;
943   gint now = time(NULL);
944   gint i;
945   gint winds[13][2];
946
947   /* fprintf(stderr, "init scene \n");*/
948   scene.daytime = get_daytime();
949   scene.actors = NULL;
950   scene.wind_orientation = -1;
951   scene.wind_angle = 0.3;
952   /* init value for random */
953   scene.seed = time(NULL);
954
955   actor = init_object(desktop_plugin, "sky", "sky0.png", 
956                       0, 0, 5, 800, 480, 
957                       TRUE , TRUE, 100, 255, 
958                       (gpointer)&change_static_actor, NULL, NULL);
959   change_static_actor(actor, desktop_plugin);
960   scene.actors = g_slist_append(scene.actors, actor);
961
962   actor = init_object(desktop_plugin, "sun", "sun.png", 
963                       0, 0, 6, 88, 88, 
964                       FALSE, FALSE, 100, 255, 
965                       (gpointer)&change_sun, (gpointer)&probability_sun, NULL);
966   actor->time_start_animation = now;
967   actor->duration_animation = G_MAXINT;
968   change_sun(actor, desktop_plugin);
969   scene.actors = g_slist_append(scene.actors, actor);
970
971   //actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
972     //                  TRUE, 100, 255, NULL, NULL);
973   //scene.actors = g_slist_append(scene.actors, actor);
974   
975   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 
976                       0, fast_rnd(300)-97, 7, 150, 97, 
977                       FALSE, FALSE, 100, 255, 
978                       (gpointer)&change_cloud, NULL, NULL);
979   actor->time_start_animation = now + fast_rnd(20);
980   actor->duration_animation = 3*60;
981   scene.actors = g_slist_append(scene.actors, actor);
982   
983   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 
984                       0, fast_rnd(300)-75, 7, 188, 75, 
985                       FALSE, FALSE, 100, 255, 
986                       (gpointer)&change_cloud, NULL, NULL);
987   actor->time_start_animation = now + fast_rnd(40)+10;
988   actor->duration_animation = 3*60;
989   scene.actors = g_slist_append(scene.actors, actor);
990
991   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 
992                       0, fast_rnd(300)-75, 7, 150, 75, 
993                       FALSE, FALSE, 100, 255, 
994                       (gpointer)&change_cloud, NULL, NULL);
995   actor->time_start_animation = now + fast_rnd(60) + 20;
996   actor->duration_animation = 5*60;
997   scene.actors = g_slist_append(scene.actors, actor);
998
999
1000   actor = init_object(desktop_plugin, "town", "town0.png", 
1001                       0, 0, 8, 800, 480, 
1002                       TRUE, TRUE, 100, 255, 
1003                       (gpointer)&change_static_actor, NULL, NULL);
1004   change_static_actor(actor, desktop_plugin);
1005   scene.actors = g_slist_append(scene.actors, actor);
1006
1007   actor = init_object(desktop_plugin, "tram", "tram.png", 
1008                       -300, 225, 9, 350, 210, 
1009                       FALSE, FALSE, 100, 255, 
1010                       (gpointer)&change_tram, NULL, NULL);
1011   actor->time_start_animation = time(NULL) + fast_rnd(10); 
1012   actor->duration_animation = 60;
1013   scene.actors = g_slist_append(scene.actors, actor);
1014
1015   actor = init_object(desktop_plugin, "border", "border0.png", 
1016                       0, 480-79, 10, 800, 79,
1017                       TRUE, TRUE, 100, 255, 
1018                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1019   change_static_actor_with_corner(actor, desktop_plugin);
1020   scene.actors = g_slist_append(scene.actors, actor);
1021   
1022   actor = init_object(desktop_plugin, "moon", "moon1.png", 
1023                       400, 20, 6, 60, 60, 
1024                       FALSE, FALSE, 100, 255, 
1025                       (gpointer)&change_moon, NULL, NULL);
1026   change_moon(actor, desktop_plugin);
1027   scene.actors = g_slist_append(scene.actors, actor);
1028
1029   actor = init_object(desktop_plugin, "wind", "", 
1030                       0, 0, 5, 0, 0, 
1031                       FALSE, FALSE, 100, 255, 
1032                       (gpointer)&change_wind, NULL, NULL);
1033   change_wind(actor, desktop_plugin);
1034   scene.actors = g_slist_append(scene.actors, actor);
1035
1036     /* windows in 4-th house  */
1037
1038     winds[0][0] = 482;
1039     winds[0][1] = 180;
1040
1041     winds[1][0] = 495;
1042     winds[1][1] = 179;
1043
1044     winds[2][0] = 482;
1045     winds[2][1] = 191;
1046
1047     winds[3][0] = 495;
1048     winds[3][1] = 190;
1049     
1050     winds[4][0] = 482;
1051     winds[4][1] = 201;
1052     
1053     winds[5][0] = 495;
1054     winds[5][1] = 210;
1055     
1056     winds[6][0] = 482;
1057     winds[6][1] = 222;
1058     
1059     winds[7][0] = 495;
1060     winds[7][1] = 221;
1061     
1062     winds[8][0] = 459;
1063     winds[8][1] = 203;
1064     
1065     winds[9][0] = 495;
1066     winds[9][1] = 241;
1067     
1068     winds[10][0] = 495;
1069     winds[10][1] = 252;
1070     
1071     winds[11][0] = 482;
1072     winds[11][1] = 273;
1073     
1074     winds[12][0] = 495;
1075     winds[12][1] = 303;
1076     for (i=0; i<13; i++){
1077         actor = init_object(desktop_plugin, "window1", "window1.png", 
1078                             winds[i][0], winds[i][1], 8, 8, 10, 
1079                             FALSE, FALSE, 100, 255, 
1080                             (gpointer)&change_window1, NULL, NULL);
1081         //change_window1(actor, desktop_plugin);
1082         actor->time_start_animation = now + fast_rnd(30);
1083         scene.actors = g_slist_append(scene.actors, actor);
1084
1085     }
1086     
1087     /* windows in 1-th house  */
1088     
1089     winds[0][0] = 86;
1090     winds[0][1] = 321;
1091
1092     winds[1][0] = 86;
1093     winds[1][1] = 363;
1094
1095     winds[2][0] = 86;
1096     winds[2][1] = 385;
1097
1098     winds[3][0] = 86;
1099     winds[3][1] = 286;
1100     
1101     winds[4][0] = 94;
1102     winds[4][1] = 232;
1103     
1104     winds[5][0] = 94;
1105     winds[5][1] = 243;
1106     
1107     winds[6][0] = 94;
1108     winds[6][1] = 265;
1109     
1110     winds[7][0] = 94;
1111     winds[7][1] = 331;
1112     for (i=0; i<8; i++){
1113         actor = init_object(desktop_plugin, "window2", "window2.png", 
1114                             winds[i][0], winds[i][1], 8, 8, 10, 
1115                             FALSE, FALSE, 100, 255, 
1116                             (gpointer)&change_window1, NULL, NULL);
1117         //change_window1(actor, desktop_plugin);
1118         actor->time_start_animation = now + fast_rnd(30);
1119         scene.actors = g_slist_append(scene.actors, actor);
1120
1121     }
1122     
1123     /* windows in 3-th house  */
1124     
1125     winds[0][0] = 251;
1126     winds[0][1] = 162;
1127
1128     winds[1][0] = 251;
1129     winds[1][1] = 196;
1130
1131     winds[2][0] = 251;
1132     winds[2][1] = 278;
1133
1134     winds[3][0] = 251;
1135     winds[3][1] = 289;
1136     
1137     winds[4][0] = 313;
1138     winds[4][1] = 173;
1139     
1140     winds[5][0] = 322;
1141     winds[5][1] = 160;
1142     
1143     winds[6][0] = 303;
1144     winds[6][1] = 217;
1145     
1146     winds[7][0] = 322;
1147     winds[7][1] = 224;
1148     
1149     winds[8][0] = 323;
1150     winds[8][1] = 217;
1151     
1152     winds[9][0] = 322;
1153     winds[9][1] = 288;
1154     
1155     for (i=0; i<10; i++){
1156         actor = init_object(desktop_plugin, "window3", "window3.png", 
1157                             winds[i][0], winds[i][1], 8, 8, 10, 
1158                             FALSE, FALSE, 100, 255, 
1159                             (gpointer)&change_window1, NULL, NULL);
1160         //change_window1(actor, desktop_plugin);
1161         actor->time_start_animation = now + fast_rnd(30);
1162         scene.actors = g_slist_append(scene.actors, actor);
1163
1164     }
1165
1166     /* windows in 5-th house  */
1167     
1168     winds[0][0] = 610;
1169     winds[0][1] = 224;
1170
1171     winds[1][0] = 602;
1172     winds[1][1] = 245;
1173
1174     winds[2][0] = 602;
1175     winds[2][1] = 264;
1176
1177     winds[3][0] = 610;
1178     winds[3][1] = 301;
1179     
1180     winds[4][0] = 610;
1181     winds[4][1] = 320;
1182     
1183     winds[5][0] = 593;
1184     winds[5][1] = 352;
1185     
1186     winds[6][0] = 610;
1187     winds[6][1] = 368;
1188     
1189     for (i=0; i<7; i++){
1190         actor = init_object(desktop_plugin, "window4", "window4.png", 
1191                             winds[i][0], winds[i][1], 8, 8, 10, 
1192                             FALSE, FALSE, 100, 255, 
1193                             (gpointer)&change_window1, NULL, NULL);
1194         //change_window1(actor, desktop_plugin);
1195         actor->time_start_animation = now + fast_rnd(30);
1196         scene.actors = g_slist_append(scene.actors, actor);
1197
1198     }
1199
1200     /* windows in 6-th house  */
1201     
1202     winds[0][0] = 717;
1203     winds[0][1] = 283;
1204
1205     winds[1][0] = 698;
1206     winds[1][1] = 293;
1207
1208     winds[2][0] = 717;
1209     winds[2][1] = 315;
1210
1211     winds[3][0] = 717;
1212     winds[3][1] = 323;
1213     
1214     winds[4][0] = 698;
1215     winds[4][1] = 362;
1216     
1217     winds[5][0] = 698;
1218     winds[5][1] = 400;
1219     
1220     for (i=0; i<6; i++){
1221         actor = init_object(desktop_plugin, "window5", "window5.png", 
1222                             winds[i][0], winds[i][1], 8, 8, 10, 
1223                             FALSE, FALSE, 100, 255, 
1224                             (gpointer)&change_window1, NULL, NULL);
1225         //change_window1(actor, desktop_plugin);
1226         actor->time_start_animation = now + fast_rnd(30);
1227         scene.actors = g_slist_append(scene.actors, actor);
1228
1229     }
1230
1231     run_long_timeout(desktop_plugin);
1232
1233 #if 0    
1234   anim = g_new0(Animation, 1);
1235   anim->count = 1;
1236   anim->actor = actor;
1237   anim->func_change = &change_tram;
1238   anim->func_time = NULL;
1239   anim->timestart = time(NULL); 
1240   anim->timeall = 10;
1241   
1242   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
1243 #endif  
1244  }
1245
1246 /* Init Berlin Scene */
1247 static void
1248 init_scene1(AWallpaperPlugin *desktop_plugin)
1249 {
1250   Actor *actor, *actor1, *actor2;
1251   gint now = time(NULL);
1252   gint i; 
1253   gint winds[13][2];
1254   GPtrArray *child = NULL;
1255
1256   scene.daytime = get_daytime();
1257   scene.actors = NULL;
1258   scene.wind_orientation = -1;
1259   scene.wind_angle = 0.3;
1260   /* init value for random */
1261   scene.seed = time(NULL);
1262
1263   
1264   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
1265                       TRUE, TRUE, 100, 255, 
1266                       (gpointer)&change_static_actor, NULL, NULL);
1267   change_static_actor(actor, desktop_plugin);
1268   scene.actors = g_slist_append(scene.actors, actor);
1269
1270   
1271   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
1272                       FALSE, FALSE, 100, 255, 
1273                       (gpointer)&change_sun, (gpointer)&probability_sun, NULL);
1274   actor->time_start_animation = time(NULL);
1275   actor->duration_animation = G_MAXINT;
1276   change_sun(actor, desktop_plugin);
1277   scene.actors = g_slist_append(scene.actors, actor);
1278
1279 #if 0
1280   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
1281                       TRUE, 100, 255, NULL, NULL);
1282   scene.actors = g_slist_append(scene.actors, actor);
1283 #endif
1284
1285   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
1286                       FALSE, FALSE, 100, 255, 
1287                       (gpointer)&change_moon, NULL, NULL);
1288   change_moon(actor, desktop_plugin);
1289   scene.actors = g_slist_append(scene.actors, actor);
1290   
1291   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, fast_rnd(300)-97, 7, 150, 97, 
1292                       FALSE, FALSE, 100, 255, 
1293                       (gpointer)&change_cloud, NULL, NULL);
1294   actor->time_start_animation = now + fast_rnd(30) + 10;
1295   actor->duration_animation = 3*60;
1296   scene.actors = g_slist_append(scene.actors, actor);
1297   
1298   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, fast_rnd(300)-75, 7, 188, 75, 
1299                       FALSE, FALSE, 100, 255, 
1300                       (gpointer)&change_cloud, NULL, NULL);
1301   actor->time_start_animation = now + fast_rnd(10);
1302   actor->duration_animation = 3*60;
1303   scene.actors = g_slist_append(scene.actors, actor);
1304
1305   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, fast_rnd(300)-75, 7, 150, 75, 
1306                       FALSE, FALSE, 100, 255, 
1307                       (gpointer)&change_cloud, NULL, NULL);
1308   actor->time_start_animation = now + fast_rnd(60) + 20;
1309   actor->duration_animation = 5*60;
1310   scene.actors = g_slist_append(scene.actors, actor);
1311
1312  
1313   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 8, 160, 50, 
1314                       FALSE, FALSE, 100, 255, 
1315                       (gpointer)&change_plane2, NULL, NULL);
1316   actor->time_start_animation = now + fast_rnd(40) + 20;
1317   actor->duration_animation = 60;
1318   scene.actors = g_slist_append(scene.actors, actor);
1319   
1320   actor = init_object(desktop_plugin, "plane1", "tu154.png", 620, 233, 9, 300, 116, 
1321                       FALSE, FALSE, 100, 255, 
1322                       (gpointer)&change_plane1, NULL, NULL);
1323   actor->time_start_animation = now + fast_rnd(20);
1324   actor->duration_animation = 30;
1325   scene.actors = g_slist_append(scene.actors, actor);
1326
1327   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
1328                       TRUE, TRUE, 100, 255, 
1329                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1330   change_static_actor_with_corner(actor, desktop_plugin);
1331   scene.actors = g_slist_append(scene.actors, actor);
1332
1333   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
1334                       FALSE, FALSE, 100, 255, 
1335                       (gpointer)&change_wind, NULL, NULL);
1336   change_wind(actor, desktop_plugin);
1337   scene.actors = g_slist_append(scene.actors, actor);
1338
1339   actor1 = init_object(desktop_plugin, "signal_red", "red.png", 
1340                       486, 425, 10, 18, 38, 
1341                       FALSE, TRUE, 100, 255, NULL, NULL, NULL);
1342   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1343   scene.actors = g_slist_append(scene.actors, actor1);
1344    
1345   actor2 = init_object(desktop_plugin, "signal_green", "green.png", 
1346                       486, 425, 10, 18, 38, 
1347                       TRUE, TRUE, 100, 255, NULL, NULL, NULL);
1348   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1349   scene.actors = g_slist_append(scene.actors, actor2);
1350   child = g_ptr_array_sized_new(2);
1351   g_ptr_array_add(child, actor1);
1352   g_ptr_array_add(child, actor2);
1353   actor = init_object(desktop_plugin, "signal", "",
1354                       486, 425, 10, 18, 38,
1355                       FALSE, FALSE, 100, 255, 
1356                       (gpointer)&change_signal, NULL, child);
1357   actor->time_start_animation = now + fast_rnd(30) + 10;
1358   scene.actors = g_slist_append(scene.actors, actor);
1359     
1360     winds[0][0] = 389;
1361     winds[0][1] = 305;
1362
1363     winds[1][0] = 373;
1364     winds[1][1] = 306;
1365
1366     winds[2][0] = 355;
1367     winds[2][1] = 306;
1368
1369     winds[3][0] = 356;
1370     winds[3][1] = 288;
1371     
1372     winds[4][0] = 337;
1373     winds[4][1] = 269;
1374     
1375     winds[5][0] = 372;
1376     winds[5][1] = 268;
1377   
1378     winds[6][0] = 372;
1379     winds[6][1] = 249;
1380     
1381     winds[7][0] = 388;
1382     winds[7][1] = 249;
1383     
1384     winds[8][0] = 387;
1385     winds[8][1] = 230;
1386     
1387     winds[9][0] = 372;
1388     winds[9][1] = 211;
1389     
1390     winds[10][0] = 355;
1391     winds[10][1] = 159;
1392     
1393     winds[11][0] = 335;
1394     winds[11][1] = 158;
1395     
1396     winds[12][0] = 386;
1397     winds[12][1] = 119;
1398   
1399     for (i=0; i<13; i++){
1400         actor = init_object(desktop_plugin, "window", "window.png", 
1401                             winds[i][0], winds[i][1], 10, 8, 9, 
1402                             FALSE, TRUE, 100, 255, 
1403                             (gpointer)&change_window1, NULL, NULL);
1404         //change_window1(actor, desktop_plugin);
1405         actor->time_start_animation = now + fast_rnd(30);
1406         scene.actors = g_slist_append(scene.actors, actor);
1407
1408     }
1409     
1410     run_long_timeout(desktop_plugin);
1411
1412 }
1413
1414
1415 void 
1416 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
1417 {
1418     gint y0 = 365;// - уровень горизонта
1419     *x = (int)(azm * 800) - 64;
1420     *y = (int)((1 - alt) * y0) - 64;
1421     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
1422 }
1423 #if 0
1424 static void 
1425 change_actor(GtkWidget * actor)
1426 {
1427     char * name;
1428     gint x, y, daytime, scale;
1429     gdouble sc;
1430     double alt, azm;
1431
1432     GtkWidget *image;
1433     GdkPixbuf *pixbuf;
1434
1435     void (*pfunc)(gpointer, gpointer);
1436
1437     name = g_object_get_data(G_OBJECT(actor), "name");
1438     fprintf(stderr, "change actor %s\n", name);
1439     if (name == "sun"){
1440         pfunc = g_object_get_data(G_OBJECT(actor), "func");
1441         if (pfunc)
1442             (*pfunc)(actor, g_strdup(name));
1443         daytime = get_daytime();
1444         if (daytime != TIME_NIGHT){
1445             hildon_animation_actor_set_show(actor, 1);
1446             get_sun_pos(&alt, &azm);
1447             get_sun_screen_pos(alt, azm, &x, &y);
1448             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1449         }
1450     }
1451     
1452     if (name == "cloud1"){
1453         x = g_object_get_data(G_OBJECT(actor), "x");
1454         y = g_object_get_data(G_OBJECT(actor), "y");
1455         scale = g_object_get_data(G_OBJECT(actor), "scale");
1456
1457         /* Start */
1458         image = g_object_get_data(G_OBJECT(actor), "image");
1459         
1460         gtk_container_remove(actor, image);  
1461         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
1462                                              200, 
1463                                              200, 
1464                                              NULL);
1465         if (pixbuf){
1466               image = gtk_image_new_from_pixbuf (pixbuf);
1467               g_object_unref(G_OBJECT(pixbuf));
1468         }
1469         g_signal_connect(G_OBJECT(image), "expose_event",
1470                                    G_CALLBACK(expose_event), pixbuf);
1471         gtk_container_add (GTK_CONTAINER (actor), image);
1472         realize(actor);
1473         gtk_widget_show_all(actor);
1474         /* End*/
1475
1476             
1477         x += 40;
1478         y -= 20;
1479         scale -= 10;
1480         if (x > 500){
1481             x = 400;
1482             y = 150;
1483             sc = 1;
1484         }
1485         sc = (double)scale / 100;
1486         hildon_animation_actor_set_scale(actor, sc, sc);
1487         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
1488         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1489         g_object_set_data(G_OBJECT(actor), "x", x);
1490         g_object_set_data(G_OBJECT(actor), "y", y);
1491         g_object_set_data(G_OBJECT(actor), "scale", scale);
1492     }
1493
1494 }
1495 #endif
1496 static gboolean
1497 short_timeout (AWallpaperPlugin *desktop_plugin)
1498 {
1499     //gint daytime = get_daytime();
1500     GSList * tmp;
1501     void (*pfunc)(gpointer, gpointer);
1502     time_t now;
1503     Actor *actor;
1504     gboolean stop_flag = TRUE;
1505
1506 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
1507         desktop_plugin->priv->short_timer = 0;
1508         return FALSE;
1509     }
1510
1511     now = time(NULL);
1512     //scene.daytime = daytime;
1513     /* fprintf(stderr, "Short timer %d\n", now); */
1514     tmp = scene.actors;
1515     while (tmp != NULL){
1516            actor = tmp->data;
1517            if (now >= actor->time_start_animation  
1518                && actor->time_start_animation > 0
1519                /* && now - actor->time_start_animation <= actor->duration_animation*/){
1520                 pfunc = actor->func_change;
1521                 if (pfunc){ 
1522                     (*pfunc)(actor, desktop_plugin);
1523                     stop_flag = FALSE;
1524                 }
1525             }
1526             tmp = g_slist_next(tmp);
1527     }
1528     if (stop_flag){
1529          desktop_plugin->priv->short_timer = 0;
1530          return FALSE;
1531     }else
1532          return TRUE; /* keep running this event */
1533 }
1534
1535 void
1536 run_long_timeout(AWallpaperPlugin *desktop_plugin)
1537 {
1538
1539     gint daytime = get_daytime();
1540     GSList * tmp;
1541     void (*pfunc)(gpointer, gpointer);
1542     time_t now;
1543     Actor *actor;
1544
1545
1546     //fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer);
1547     if (scene.daytime != daytime){
1548         scene.daytime = daytime;
1549         tmp = scene.actors;
1550         while (tmp != NULL){
1551             //change_actor(tmp->data);
1552             pfunc =((Actor*)tmp->data)->func_change;
1553             if (pfunc){
1554                 (*pfunc)(tmp->data, desktop_plugin);
1555             }
1556             tmp = g_slist_next(tmp);
1557         }
1558     }
1559    
1560     now = time(NULL);
1561     //fprintf(stderr, "Now  %d\n", now);
1562     tmp = scene.actors;
1563     while (tmp != NULL){
1564         actor = tmp->data;
1565         if (now >= actor->time_start_animation  
1566             && actor->time_start_animation > 0
1567             && desktop_plugin->priv->short_timer == 0){
1568             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
1569             if (desktop_plugin->priv->rich_animation){
1570                 actor->time_start_animation = now;
1571                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
1572             }
1573             else {
1574                 (*actor->func_change)(actor, desktop_plugin);
1575             }
1576         }
1577         tmp = g_slist_next(tmp);
1578     }
1579  
1580 }
1581
1582 static gboolean
1583 long_timeout (AWallpaperPlugin *desktop_plugin)
1584 {
1585     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
1586     if (desktop_plugin->priv->long_timer == 0 )
1587         return FALSE;
1588     if (!desktop_plugin->priv->visible){
1589         if(desktop_plugin->priv->short_timer != 0){
1590             g_source_remove(desktop_plugin->priv->short_timer);
1591             desktop_plugin->priv->short_timer = 0;
1592         }
1593         desktop_plugin->priv->long_timer = 0;
1594         return FALSE;
1595     }
1596   
1597
1598     run_long_timeout(desktop_plugin);
1599     return TRUE; /* keep running this event */
1600 }
1601
1602 static void
1603 desktop_plugin_visible_notify (GObject    *object,
1604                                           GParamSpec *spec,
1605                                           AWallpaperPlugin *desktop_plugin)
1606 {
1607     gboolean visible;
1608     g_object_get (object, "is-on-current-desktop", &visible, NULL);
1609     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
1610     if (visible){
1611         desktop_plugin->priv->visible = TRUE;
1612         if (desktop_plugin->priv->long_timer == 0 ){
1613             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1614             run_long_timeout(desktop_plugin);
1615         }
1616     }else{
1617         desktop_plugin->priv->visible = FALSE;
1618         if (desktop_plugin->priv->long_timer != 0 ){
1619             g_source_remove(desktop_plugin->priv->long_timer);
1620             desktop_plugin->priv->long_timer = 0;
1621         }
1622     }
1623 }
1624
1625 gboolean
1626 rich_animation_press(GtkWidget *widget, GdkEvent *event,
1627                                             gpointer user_data){
1628     fprintf(stderr,"gggggggggggggggggggg2222\n");
1629     return FALSE;
1630 }    
1631 static void
1632 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
1633 {
1634     GtkWidget *rich_animation;
1635     gchar           buffer[2048];
1636
1637
1638     fprintf(stderr, "!!!!!!!plugin init \n");
1639     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1640     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1641     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
1642
1643     /* Load config */
1644     read_config(priv);
1645     /* Initialize DBUS */
1646     livewp_initialize_dbus(priv);
1647
1648     priv->desktop_plugin = desktop_plugin;
1649     priv->visible = TRUE;
1650     priv->short_timer = 0;
1651     //priv->theme = g_strdup("Modern");
1652     desktop_plugin->priv->main_widget = gtk_fixed_new();
1653
1654     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
1655     desktop_plugin->priv->right_corner = NULL;
1656     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
1657     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
1658     if (desktop_plugin->priv->right_corner){
1659         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
1660         gtk_widget_show (desktop_plugin->priv->right_corner);
1661     }
1662     /* Create rich animation event */
1663     rich_animation = gtk_event_box_new();
1664     if(rich_animation){
1665         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
1666         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
1667         gtk_widget_set_size_request(rich_animation, 100, 32);
1668         gtk_widget_show (rich_animation);
1669         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
1670         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
1671     }
1672 /*
1673     GtkWidget *label = gtk_label_new ("ddddddddddddd"); 
1674     gtk_widget_set_size_request(label, 95, 30);
1675     gtk_widget_show (label);
1676 //    gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1677     gtk_fixed_put(GTK_FIXED(widget), label, 0, 0);
1678 */
1679     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1680     g_signal_connect (desktop_plugin, "show-settings",
1681                              G_CALLBACK (show_settings), priv);
1682     gtk_widget_show (desktop_plugin->priv->main_widget);
1683     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
1684     init_applet_position();
1685
1686     
1687     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1688     if (!strcmp(priv->theme,"Modern"))
1689         init_scene(desktop_plugin);
1690     else if (!strcmp(priv->theme,"Berlin")) 
1691         init_scene1(desktop_plugin);
1692     else if (!strcmp(priv->theme, "Matrix"))
1693         init_scene2(desktop_plugin);
1694     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1695     /* TODO Move scene to priv */
1696     scene.timer_type = LONG_TIMER_TYPE;
1697     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1698                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1699
1700    
1701     //sleep(2);
1702 }
1703
1704 static void
1705 lw_applet_finalize (GObject *object)
1706 {
1707      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1708      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1709      
1710      fprintf(stderr,"finalaze %i\n", priv->long_timer);
1711      if (priv->long_timer){
1712         g_source_remove(priv->long_timer);
1713         priv->long_timer = 0;
1714      }
1715      if (priv->short_timer){
1716         g_source_remove(priv->short_timer);
1717         priv->short_timer = 0;
1718      }
1719
1720      destroy_scene();
1721 }
1722
1723 static void
1724 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1725
1726     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1727     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1728
1729     /* gobject */
1730     gobject_class->destroy = (gpointer)lw_applet_finalize;
1731     widget_class->realize = lw_applet_realize;
1732     widget_class->expose_event = lw_applet_expose_event;
1733
1734     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1735
1736 }
1737
1738 static void
1739 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
1740 }