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