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