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