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