added billboard in Modern
[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, "stend", "stend0.png", 
1037                       482, 146, 9, 300, 305, 
1038                       TRUE, TRUE, 100, 255, 
1039                       (gpointer)&change_static_actor, NULL, NULL);
1040   change_static_actor(actor, desktop_plugin);
1041   scene->actors = g_slist_append(scene->actors, actor);
1042
1043   actor = init_object(desktop_plugin, "tram", "tram.png", 
1044                       -300, 225, 10, 350, 210, 
1045                       FALSE, FALSE, 100, 255, 
1046                       (gpointer)&change_tram, NULL, NULL);
1047   actor->time_start_animation = time(NULL) + fast_rnd(10); 
1048   actor->duration_animation = 60;
1049   scene->actors = g_slist_append(scene->actors, actor);
1050
1051   actor = init_object(desktop_plugin, "border", "border0.png", 
1052                       0, 480-79, 11, 800, 79,
1053                       TRUE, TRUE, 100, 255, 
1054                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1055   change_static_actor_with_corner(actor, desktop_plugin);
1056   scene->actors = g_slist_append(scene->actors, actor);
1057   
1058   actor = init_object(desktop_plugin, "moon", "moon1.png", 
1059                       400, 20, 6, 60, 60, 
1060                       FALSE, FALSE, 100, 255, 
1061                       (gpointer)&change_moon, NULL, NULL);
1062   change_moon(actor, desktop_plugin);
1063   scene->actors = g_slist_append(scene->actors, actor);
1064
1065   actor = init_object(desktop_plugin, "wind", "", 
1066                       0, 0, 5, 0, 0, 
1067                       FALSE, FALSE, 100, 255, 
1068                       (gpointer)&change_wind, NULL, NULL);
1069   change_wind(actor, desktop_plugin);
1070   scene->actors = g_slist_append(scene->actors, actor);
1071
1072     /* windows in 4-th house  */
1073
1074     winds[0][0] = 482;
1075     winds[0][1] = 180;
1076
1077     winds[1][0] = 495;
1078     winds[1][1] = 179;
1079
1080     winds[2][0] = 482;
1081     winds[2][1] = 191;
1082
1083     winds[3][0] = 495;
1084     winds[3][1] = 190;
1085     
1086     winds[4][0] = 482;
1087     winds[4][1] = 201;
1088     
1089     winds[5][0] = 495;
1090     winds[5][1] = 210;
1091     
1092     winds[6][0] = 482;
1093     winds[6][1] = 222;
1094     
1095     winds[7][0] = 495;
1096     winds[7][1] = 221;
1097     
1098     winds[8][0] = 459;
1099     winds[8][1] = 203;
1100     
1101     winds[9][0] = 495;
1102     winds[9][1] = 241;
1103     
1104     winds[10][0] = 495;
1105     winds[10][1] = 252;
1106     
1107     winds[11][0] = 482;
1108     winds[11][1] = 273;
1109     
1110     winds[12][0] = 495;
1111     winds[12][1] = 303;
1112     for (i=0; i<13; i++){
1113         actor = init_object(desktop_plugin, "window1", "window1.png", 
1114                             winds[i][0], winds[i][1], 8, 8, 10, 
1115                             FALSE, FALSE, 100, 255, 
1116                             (gpointer)&change_window1, NULL, NULL);
1117         //change_window1(actor, desktop_plugin);
1118         actor->time_start_animation = now + fast_rnd(30);
1119         scene->actors = g_slist_append(scene->actors, actor);
1120
1121     }
1122     
1123     /* windows in 1-th house  */
1124     
1125     winds[0][0] = 86;
1126     winds[0][1] = 321;
1127
1128     winds[1][0] = 86;
1129     winds[1][1] = 363;
1130
1131     winds[2][0] = 86;
1132     winds[2][1] = 385;
1133
1134     winds[3][0] = 86;
1135     winds[3][1] = 286;
1136     
1137     winds[4][0] = 94;
1138     winds[4][1] = 232;
1139     
1140     winds[5][0] = 94;
1141     winds[5][1] = 243;
1142     
1143     winds[6][0] = 94;
1144     winds[6][1] = 265;
1145     
1146     winds[7][0] = 94;
1147     winds[7][1] = 331;
1148     for (i=0; i<8; i++){
1149         actor = init_object(desktop_plugin, "window2", "window2.png", 
1150                             winds[i][0], winds[i][1], 8, 8, 10, 
1151                             FALSE, FALSE, 100, 255, 
1152                             (gpointer)&change_window1, NULL, NULL);
1153         //change_window1(actor, desktop_plugin);
1154         actor->time_start_animation = now + fast_rnd(30);
1155         scene->actors = g_slist_append(scene->actors, actor);
1156
1157     }
1158     
1159     /* windows in 3-th house  */
1160     
1161     winds[0][0] = 251;
1162     winds[0][1] = 162;
1163
1164     winds[1][0] = 251;
1165     winds[1][1] = 196;
1166
1167     winds[2][0] = 251;
1168     winds[2][1] = 278;
1169
1170     winds[3][0] = 251;
1171     winds[3][1] = 289;
1172     
1173     winds[4][0] = 313;
1174     winds[4][1] = 173;
1175     
1176     winds[5][0] = 322;
1177     winds[5][1] = 160;
1178     
1179     winds[6][0] = 303;
1180     winds[6][1] = 217;
1181     
1182     winds[7][0] = 322;
1183     winds[7][1] = 224;
1184     
1185     winds[8][0] = 323;
1186     winds[8][1] = 217;
1187     
1188     winds[9][0] = 322;
1189     winds[9][1] = 288;
1190     
1191     for (i=0; i<10; i++){
1192         actor = init_object(desktop_plugin, "window3", "window3.png", 
1193                             winds[i][0], winds[i][1], 8, 8, 10, 
1194                             FALSE, FALSE, 100, 255, 
1195                             (gpointer)&change_window1, NULL, NULL);
1196         //change_window1(actor, desktop_plugin);
1197         actor->time_start_animation = now + fast_rnd(30);
1198         scene->actors = g_slist_append(scene->actors, actor);
1199
1200     }
1201
1202     /* windows in 5-th house  */
1203     
1204     winds[0][0] = 610;
1205     winds[0][1] = 224;
1206
1207     winds[1][0] = 602;
1208     winds[1][1] = 245;
1209
1210     winds[2][0] = 602;
1211     winds[2][1] = 264;
1212
1213     winds[3][0] = 610;
1214     winds[3][1] = 301;
1215     
1216     winds[4][0] = 610;
1217     winds[4][1] = 320;
1218     
1219     winds[5][0] = 593;
1220     winds[5][1] = 352;
1221     
1222     winds[6][0] = 610;
1223     winds[6][1] = 368;
1224     
1225     for (i=0; i<7; i++){
1226         actor = init_object(desktop_plugin, "window4", "window4.png", 
1227                             winds[i][0], winds[i][1], 8, 8, 10, 
1228                             FALSE, FALSE, 100, 255, 
1229                             (gpointer)&change_window1, NULL, NULL);
1230         //change_window1(actor, desktop_plugin);
1231         actor->time_start_animation = now + fast_rnd(30);
1232         scene->actors = g_slist_append(scene->actors, actor);
1233
1234     }
1235
1236     /* windows in 6-th house  */
1237     
1238     winds[0][0] = 717;
1239     winds[0][1] = 283;
1240
1241     winds[1][0] = 698;
1242     winds[1][1] = 293;
1243
1244     winds[2][0] = 717;
1245     winds[2][1] = 315;
1246
1247     winds[3][0] = 717;
1248     winds[3][1] = 323;
1249     
1250     winds[4][0] = 698;
1251     winds[4][1] = 362;
1252     
1253     winds[5][0] = 698;
1254     winds[5][1] = 400;
1255     
1256     for (i=0; i<6; i++){
1257         actor = init_object(desktop_plugin, "window5", "window5.png", 
1258                             winds[i][0], winds[i][1], 8, 8, 10, 
1259                             FALSE, FALSE, 100, 255, 
1260                             (gpointer)&change_window1, NULL, NULL);
1261         //change_window1(actor, desktop_plugin);
1262         actor->time_start_animation = now + fast_rnd(30);
1263         scene->actors = g_slist_append(scene->actors, actor);
1264
1265     }
1266     run_long_timeout(desktop_plugin);
1267
1268 #if 0    
1269   anim = g_new0(Animation, 1);
1270   anim->count = 1;
1271   anim->actor = actor;
1272   anim->func_change = &change_tram;
1273   anim->func_time = NULL;
1274   anim->timestart = time(NULL); 
1275   anim->timeall = 10;
1276   
1277   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
1278 #endif  
1279 }
1280
1281 /* Init Berlin Scene */
1282 static void
1283 init_scene1(AWallpaperPlugin *desktop_plugin)
1284 {
1285   Actor *actor, *actor1, *actor2;
1286   Scene *scene;
1287   gint now = time(NULL);
1288   gint i; 
1289   gint winds[13][2];
1290   GPtrArray *child = NULL;
1291
1292   scene = g_new0(Scene, 1);
1293   scene->daytime = get_daytime();
1294   scene->actors = NULL;
1295   scene->wind_orientation = -1;
1296   scene->wind_angle = 0.3;
1297   /* init value for random */
1298   scene->seed = time(NULL);
1299   desktop_plugin->priv->scene = scene;
1300   
1301   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
1302                       TRUE, TRUE, 100, 255, 
1303                       (gpointer)&change_static_actor, NULL, NULL);
1304   change_static_actor(actor, desktop_plugin);
1305   scene->actors = g_slist_append(scene->actors, actor);
1306
1307   
1308   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
1309                       FALSE, FALSE, 100, 255, 
1310                       (gpointer)&change_sun, (gpointer)&probability_sun, NULL);
1311   actor->time_start_animation = time(NULL);
1312   actor->duration_animation = G_MAXINT;
1313   change_sun(actor, desktop_plugin);
1314   scene->actors = g_slist_append(scene->actors, actor);
1315
1316 #if 0
1317   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
1318                       TRUE, 100, 255, NULL, NULL);
1319   scene.actors = g_slist_append(scene.actors, actor);
1320 #endif
1321
1322   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
1323                       FALSE, FALSE, 100, 255, 
1324                       (gpointer)&change_moon, NULL, NULL);
1325   change_moon(actor, desktop_plugin);
1326   scene->actors = g_slist_append(scene->actors, actor);
1327   
1328   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, fast_rnd(300)-97, 7, 150, 97, 
1329                       FALSE, FALSE, 100, 255, 
1330                       (gpointer)&change_cloud, NULL, NULL);
1331   actor->time_start_animation = now + fast_rnd(30) + 10;
1332   actor->duration_animation = 3*60;
1333   scene->actors = g_slist_append(scene->actors, actor);
1334   
1335   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, fast_rnd(300)-75, 7, 188, 75, 
1336                       FALSE, FALSE, 100, 255, 
1337                       (gpointer)&change_cloud, NULL, NULL);
1338   actor->time_start_animation = now + fast_rnd(10);
1339   actor->duration_animation = 3*60;
1340   scene->actors = g_slist_append(scene->actors, actor);
1341
1342   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, fast_rnd(300)-75, 7, 150, 75, 
1343                       FALSE, FALSE, 100, 255, 
1344                       (gpointer)&change_cloud, NULL, NULL);
1345   actor->time_start_animation = now + fast_rnd(60) + 20;
1346   actor->duration_animation = 5*60;
1347   scene->actors = g_slist_append(scene->actors, actor);
1348
1349  
1350   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 8, 160, 50, 
1351                       FALSE, FALSE, 100, 255, 
1352                       (gpointer)&change_plane2, NULL, NULL);
1353   actor->time_start_animation = now + fast_rnd(40) + 20;
1354   actor->duration_animation = 60;
1355   scene->actors = g_slist_append(scene->actors, actor);
1356   
1357   actor = init_object(desktop_plugin, "plane1", "tu154.png", 620, 233, 9, 300, 116, 
1358                       FALSE, FALSE, 100, 255, 
1359                       (gpointer)&change_plane1, NULL, NULL);
1360   actor->time_start_animation = now + fast_rnd(20);
1361   actor->duration_animation = 30;
1362   scene->actors = g_slist_append(scene->actors, actor);
1363
1364   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
1365                       TRUE, TRUE, 100, 255, 
1366                       (gpointer)&change_static_actor_with_corner, NULL, NULL);
1367   change_static_actor_with_corner(actor, desktop_plugin);
1368   scene->actors = g_slist_append(scene->actors, actor);
1369
1370   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
1371                       FALSE, FALSE, 100, 255, 
1372                       (gpointer)&change_wind, NULL, NULL);
1373   change_wind(actor, desktop_plugin);
1374   scene->actors = g_slist_append(scene->actors, actor);
1375
1376   actor1 = init_object(desktop_plugin, "signal_red", "red.png", 
1377                       486, 425, 10, 18, 38, 
1378                       FALSE, TRUE, 100, 255, NULL, NULL, NULL);
1379   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1380   scene->actors = g_slist_append(scene->actors, actor1);
1381    
1382   actor2 = init_object(desktop_plugin, "signal_green", "green.png", 
1383                       486, 425, 10, 18, 38, 
1384                       TRUE, TRUE, 100, 255, NULL, NULL, NULL);
1385   //actor->time_start_animation = now + fast_rnd(30) + 10;  
1386   scene->actors = g_slist_append(scene->actors, actor2);
1387   child = g_ptr_array_sized_new(2);
1388   g_ptr_array_add(child, actor1);
1389   g_ptr_array_add(child, actor2);
1390   actor = init_object(desktop_plugin, "signal", "",
1391                       486, 425, 10, 18, 38,
1392                       FALSE, FALSE, 100, 255, 
1393                       (gpointer)&change_signal, NULL, child);
1394   actor->time_start_animation = now + fast_rnd(30) + 10;
1395   scene->actors = g_slist_append(scene->actors, actor);
1396     
1397     winds[0][0] = 389;
1398     winds[0][1] = 305;
1399
1400     winds[1][0] = 373;
1401     winds[1][1] = 306;
1402
1403     winds[2][0] = 355;
1404     winds[2][1] = 306;
1405
1406     winds[3][0] = 356;
1407     winds[3][1] = 288;
1408     
1409     winds[4][0] = 337;
1410     winds[4][1] = 269;
1411     
1412     winds[5][0] = 372;
1413     winds[5][1] = 268;
1414   
1415     winds[6][0] = 372;
1416     winds[6][1] = 249;
1417     
1418     winds[7][0] = 388;
1419     winds[7][1] = 249;
1420     
1421     winds[8][0] = 387;
1422     winds[8][1] = 230;
1423     
1424     winds[9][0] = 372;
1425     winds[9][1] = 211;
1426     
1427     winds[10][0] = 355;
1428     winds[10][1] = 159;
1429     
1430     winds[11][0] = 335;
1431     winds[11][1] = 158;
1432     
1433     winds[12][0] = 386;
1434     winds[12][1] = 119;
1435   
1436     for (i=0; i<13; i++){
1437         actor = init_object(desktop_plugin, "window", "window.png", 
1438                             winds[i][0], winds[i][1], 10, 8, 9, 
1439                             FALSE, TRUE, 100, 255, 
1440                             (gpointer)&change_window1, NULL, NULL);
1441         //change_window1(actor, desktop_plugin);
1442         actor->time_start_animation = now + fast_rnd(30);
1443         scene->actors = g_slist_append(scene->actors, actor);
1444
1445     }
1446     
1447     run_long_timeout(desktop_plugin);
1448
1449 }
1450
1451 void 
1452 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
1453 {
1454     gint y0 = 365;// - уровень горизонта
1455     *x = (int)(azm * 800) - 64;
1456     *y = (int)((1 - alt) * y0) - 64;
1457     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
1458 }
1459 #if 0
1460 static void 
1461 change_actor(GtkWidget * actor)
1462 {
1463     char * name;
1464     gint x, y, daytime, scale;
1465     gdouble sc;
1466     double alt, azm;
1467
1468     GtkWidget *image;
1469     GdkPixbuf *pixbuf;
1470
1471     void (*pfunc)(gpointer, gpointer);
1472
1473     name = g_object_get_data(G_OBJECT(actor), "name");
1474     fprintf(stderr, "change actor %s\n", name);
1475     if (name == "sun"){
1476         pfunc = g_object_get_data(G_OBJECT(actor), "func");
1477         if (pfunc)
1478             (*pfunc)(actor, g_strdup(name));
1479         daytime = get_daytime();
1480         if (daytime != TIME_NIGHT){
1481             hildon_animation_actor_set_show(actor, 1);
1482             get_sun_pos(&alt, &azm);
1483             get_sun_screen_pos(alt, azm, &x, &y);
1484             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1485         }
1486     }
1487     
1488     if (name == "cloud1"){
1489         x = g_object_get_data(G_OBJECT(actor), "x");
1490         y = g_object_get_data(G_OBJECT(actor), "y");
1491         scale = g_object_get_data(G_OBJECT(actor), "scale");
1492
1493         /* Start */
1494         image = g_object_get_data(G_OBJECT(actor), "image");
1495         
1496         gtk_container_remove(actor, image);  
1497         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
1498                                              200, 
1499                                              200, 
1500                                              NULL);
1501         if (pixbuf){
1502               image = gtk_image_new_from_pixbuf (pixbuf);
1503               g_object_unref(G_OBJECT(pixbuf));
1504         }
1505         g_signal_connect(G_OBJECT(image), "expose_event",
1506                                    G_CALLBACK(expose_event), pixbuf);
1507         gtk_container_add (GTK_CONTAINER (actor), image);
1508         realize(actor);
1509         gtk_widget_show_all(actor);
1510         /* End*/
1511
1512             
1513         x += 40;
1514         y -= 20;
1515         scale -= 10;
1516         if (x > 500){
1517             x = 400;
1518             y = 150;
1519             sc = 1;
1520         }
1521         sc = (double)scale / 100;
1522         hildon_animation_actor_set_scale(actor, sc, sc);
1523         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
1524         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
1525         g_object_set_data(G_OBJECT(actor), "x", x);
1526         g_object_set_data(G_OBJECT(actor), "y", y);
1527         g_object_set_data(G_OBJECT(actor), "scale", scale);
1528     }
1529
1530 }
1531 #endif
1532 static gboolean
1533 short_timeout (AWallpaperPlugin *desktop_plugin)
1534 {
1535     //gint daytime = get_daytime();
1536     GSList * tmp;
1537     void (*pfunc)(gpointer, gpointer);
1538     time_t now;
1539     Actor *actor;
1540     gboolean stop_flag = TRUE;
1541
1542 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
1543         desktop_plugin->priv->short_timer = 0;
1544         return FALSE;
1545     }
1546
1547     now = time(NULL);
1548     //scene.daytime = daytime;
1549     /* fprintf(stderr, "Short timer %d\n", now); */
1550     tmp = desktop_plugin->priv->scene->actors;
1551     while (tmp != NULL){
1552            actor = tmp->data;
1553            if (now >= actor->time_start_animation  
1554                && actor->time_start_animation > 0
1555                /* && now - actor->time_start_animation <= actor->duration_animation*/){
1556                 pfunc = actor->func_change;
1557                 if (pfunc){ 
1558                     (*pfunc)(actor, desktop_plugin);
1559                     stop_flag = FALSE;
1560                 }
1561             }
1562             tmp = g_slist_next(tmp);
1563     }
1564     if (stop_flag){
1565          desktop_plugin->priv->short_timer = 0;
1566          return FALSE;
1567     }else
1568          return TRUE; /* keep running this event */
1569 }
1570
1571 void
1572 run_long_timeout(AWallpaperPlugin *desktop_plugin)
1573 {
1574
1575     gint daytime = get_daytime();
1576     GSList * tmp;
1577     void (*pfunc)(gpointer, gpointer);
1578     time_t now;
1579     Actor *actor;
1580
1581
1582     //fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer);
1583     if (desktop_plugin->priv->scene->daytime != daytime){
1584         desktop_plugin->priv->scene->daytime = daytime;
1585         tmp = desktop_plugin->priv->scene->actors;
1586         while (tmp != NULL){
1587             //change_actor(tmp->data);
1588             pfunc =((Actor*)tmp->data)->func_change;
1589             if (pfunc){
1590                 (*pfunc)(tmp->data, desktop_plugin);
1591             }
1592             tmp = g_slist_next(tmp);
1593         }
1594     }
1595    
1596     now = time(NULL);
1597     //fprintf(stderr, "Now  %d\n", now);
1598     tmp = desktop_plugin->priv->scene->actors;
1599     while (tmp != NULL){
1600         actor = tmp->data;
1601         if (now >= actor->time_start_animation  
1602             && actor->time_start_animation > 0
1603             && desktop_plugin->priv->short_timer == 0){
1604             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
1605             if (desktop_plugin->priv->rich_animation){
1606                 actor->time_start_animation = now;
1607                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
1608             }
1609             else {
1610                 (*actor->func_change)(actor, desktop_plugin);
1611             }
1612         }
1613         tmp = g_slist_next(tmp);
1614     }
1615  
1616 }
1617
1618 static gboolean
1619 long_timeout (AWallpaperPlugin *desktop_plugin)
1620 {
1621     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
1622     if (desktop_plugin->priv->long_timer == 0 )
1623         return FALSE;
1624     if (!desktop_plugin->priv->visible){
1625         if(desktop_plugin->priv->short_timer != 0){
1626             g_source_remove(desktop_plugin->priv->short_timer);
1627             desktop_plugin->priv->short_timer = 0;
1628         }
1629         desktop_plugin->priv->long_timer = 0;
1630         return FALSE;
1631     }
1632   
1633
1634     run_long_timeout(desktop_plugin);
1635     return TRUE; /* keep running this event */
1636 }
1637
1638 static void
1639 desktop_plugin_visible_notify (GObject    *object,
1640                                           GParamSpec *spec,
1641                                           AWallpaperPlugin *desktop_plugin)
1642 {
1643     gboolean visible;
1644     g_object_get (object, "is-on-current-desktop", &visible, NULL);
1645     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
1646     if (visible){
1647         desktop_plugin->priv->visible = TRUE;
1648         if (desktop_plugin->priv->long_timer == 0 ){
1649             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1650             run_long_timeout(desktop_plugin);
1651         }
1652     }else{
1653         desktop_plugin->priv->visible = FALSE;
1654         if (desktop_plugin->priv->long_timer != 0 ){
1655             g_source_remove(desktop_plugin->priv->long_timer);
1656             desktop_plugin->priv->long_timer = 0;
1657         }
1658     }
1659 }
1660
1661 gboolean
1662 rich_animation_press(GtkWidget *widget, GdkEvent *event,
1663                                             gpointer user_data){
1664     fprintf(stderr,"gggggggggggggggggggg2222\n");
1665     return FALSE;
1666 }    
1667 static void
1668 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
1669 {
1670     GtkWidget *rich_animation;
1671     gchar           buffer[2048];
1672
1673
1674     //fprintf(stderr, "!!!!!!!plugin init \n");
1675     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1676     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1677     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
1678
1679     /* Load config */
1680     read_config(priv);
1681     /* Initialize DBUS */
1682     livewp_initialize_dbus(priv);
1683
1684     priv->desktop_plugin = desktop_plugin;
1685     priv->visible = TRUE;
1686     priv->short_timer = 0;
1687     //priv->theme = g_strdup("Modern");
1688     desktop_plugin->priv->main_widget = gtk_fixed_new();
1689
1690     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
1691     desktop_plugin->priv->right_corner = NULL;
1692     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
1693     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
1694     if (desktop_plugin->priv->right_corner){
1695         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
1696         gtk_widget_show (desktop_plugin->priv->right_corner);
1697     }
1698     /* Create rich animation event */
1699     rich_animation = gtk_event_box_new();
1700     if(rich_animation){
1701         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
1702         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
1703         gtk_widget_set_size_request(rich_animation, 100, 32);
1704         gtk_widget_show (rich_animation);
1705         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
1706         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
1707     }
1708 /*
1709     GtkWidget *label = gtk_label_new ("ddddddddddddd"); 
1710     gtk_widget_set_size_request(label, 95, 30);
1711     gtk_widget_show (label);
1712 //    gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1713     gtk_fixed_put(GTK_FIXED(widget), label, 0, 0);
1714 */
1715     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1716     g_signal_connect (desktop_plugin, "show-settings",
1717                              G_CALLBACK (show_settings), priv);
1718     gtk_widget_show (desktop_plugin->priv->main_widget);
1719     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
1720     init_applet_position();
1721
1722     
1723     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1724     priv->scene = NULL;
1725     if (!strcmp(priv->theme,"Modern"))
1726         init_scene(desktop_plugin);
1727     else if (!strcmp(priv->theme,"Berlin")) 
1728         init_scene1(desktop_plugin);
1729     else if (!strcmp(priv->theme, "Matrix"))
1730         init_scene2(desktop_plugin);
1731     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1732     /* TODO Move scene to priv */
1733     //scene.timer_type = LONG_TIMER_TYPE;
1734     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1735                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1736
1737    
1738     //sleep(2);
1739 }
1740
1741 static void
1742 lw_applet_finalize (GObject *object)
1743 {
1744      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1745      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1746      
1747      fprintf(stderr,"finalaze %i\n", priv->long_timer);
1748      if (priv->long_timer){
1749         g_source_remove(priv->long_timer);
1750         priv->long_timer = 0;
1751      }
1752      if (priv->short_timer){
1753         g_source_remove(priv->short_timer);
1754         priv->short_timer = 0;
1755      }
1756
1757      destroy_scene(desktop_plugin);
1758 }
1759
1760 static void
1761 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1762
1763     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1764     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1765
1766     /* gobject */
1767     gobject_class->destroy = (gpointer)lw_applet_finalize;
1768     widget_class->realize = lw_applet_realize;
1769     widget_class->expose_event = lw_applet_expose_event;
1770
1771     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1772
1773 }
1774
1775 static void
1776 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
1777 }