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