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