e25acb1829cd64ae7f3167396a158068258c81d7
[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
31 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
32 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
33 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
34 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
35
36 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
37 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
38                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
39                                                           Animation_WallpaperPrivate))
40
41 /* Position of plugin on desktop */
42 #define Xstartposition 700 
43 #define Ystartposition 425 
44
45 gint xapplet = 0, yapplet = 0;
46 GSList * objects_list = NULL;
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, "estroy_hildon_actor %p\n",actor->widget);
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 void 
227 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
228 {
229     gint daytime, phase;
230     char *newfile;
231
232     if (actor){
233         daytime = get_daytime();
234         if (daytime == TIME_NIGHT){
235             if (!actor->visible){
236                 actor->visible = TRUE;
237                 phase = get_moon_phase();
238                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
239                 actor->filename = newfile;
240                 create_hildon_actor(actor, desktop_plugin);
241
242             }
243             //actor->x = 400;
244             //actor->y = 10;
245            // actor_set_position_full(actor->widget, x, y, actor->z);
246             //probability_sun(actor);
247             //fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
248          }else if (actor->visible){
249             actor->visible = FALSE;
250             fprintf(stderr, "destroy moon \n");
251             destroy_hildon_actor(actor);
252             /* TO DO make moonrise*/
253             actor->time_start_animation = get_next_sunset();
254         } 
255     }
256     
257 }
258
259 void 
260 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
261 {
262     double alt, azm;
263     gint x, y, daytime;
264
265     if (actor){
266         daytime = get_daytime();
267         if (daytime != TIME_NIGHT){
268             if (!actor->visible){
269                 actor->visible = TRUE;
270
271                 create_hildon_actor(actor, desktop_plugin);
272             }
273             get_sun_pos(&alt, &azm);
274             get_sun_screen_pos(alt, azm, &x, &y);
275             actor->x = x;
276             actor->y = y;
277             actor_set_position_full(actor->widget, x, y, actor->z);
278             probability_sun(actor);
279          }else if (actor->visible){
280             actor->visible = FALSE;
281             destroy_hildon_actor(actor);
282             actor->time_start_animation = get_next_sunrise();
283         } 
284     }
285     
286 }
287 static void 
288 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
289 {
290     gint x0 = -300, y0 = 225, scale0 = 100,
291          x1 = 800, y1 = 162, scale1 = 130, 
292          x, y, scale;
293     struct timeval tvb;     
294     suseconds_t ms;
295     long sec;
296     double t;
297     gint daytime = get_daytime();
298
299     gettimeofday(&tvb, NULL);
300     
301     ms = tvb.tv_usec;
302     sec = tvb.tv_sec;
303 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
304    
305     if (!actor->visible){
306         actor->visible = TRUE;
307         if (daytime == TIME_NIGHT)
308             actor->filename = g_strdup("tram_dark.png");
309         else
310             actor->filename = g_strdup("tram.png");
311         create_hildon_actor(actor, desktop_plugin);
312     }
313     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
314     x = path_line(x0, x1, t);
315     y = path_line(y0, y1, t);
316     scale = path_line(scale0, scale1, t);
317     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
318     actor_set_position_full(actor->widget, x, y, actor->z);
319     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
320     if (t >= 1){
321         /* stop animation */
322         actor->visible = FALSE;
323         destroy_hildon_actor(actor);
324         actor->time_start_animation = sec + 5*60;
325     }
326 }
327
328 void
329 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
330 {
331     gint x0 = 620, y0 = 233, scale0 = 100,
332          x1 = 79, y1 = -146, scale1 = 100, 
333          x, y, scale;
334     struct timeval tvb;     
335     suseconds_t ms;
336     long sec;
337     double t;
338     gint daytime = get_daytime();
339
340     gettimeofday(&tvb, NULL);
341     
342     ms = tvb.tv_usec;
343     sec = tvb.tv_sec;
344 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
345    
346     if (!actor->visible){
347         actor->visible = TRUE;
348         if (daytime == TIME_NIGHT)
349             actor->filename = g_strdup("tu154.png");
350         else
351             actor->filename = g_strdup("tu154.png");
352         create_hildon_actor(actor, desktop_plugin);
353     }
354     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
355     x = path_line(x0, x1, t);
356     y = path_line(y0, y1, t);
357     //scale = path_line(scale0, scale1, t);
358     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
359     actor_set_position_full(actor->widget, x, y, actor->z);
360     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
361     if (t >= 1){
362         /* stop animation */
363         actor->visible = FALSE;
364         destroy_hildon_actor(actor);
365         actor->time_start_animation = sec + 5*60;
366     }
367
368 }
369
370 void
371 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
372 {
373     gint x0 = -actor->width, y0 = 45, scale0 = 100,
374          x1 = 800, y1 = 20, scale1 = 100, 
375          x, y, scale;
376     struct timeval tvb;     
377     suseconds_t ms;
378     long sec;
379     double t;
380     gint daytime = get_daytime();
381
382     gettimeofday(&tvb, NULL);
383     
384     ms = tvb.tv_usec;
385     sec = tvb.tv_sec;
386 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
387    
388     if (!actor->visible){
389         actor->visible = TRUE;
390         if (daytime == TIME_NIGHT)
391             actor->filename = g_strdup("plane3.png");
392         else
393             actor->filename = g_strdup("plane3.png");
394         create_hildon_actor(actor, desktop_plugin);
395     }
396     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
397     x = path_line(x0, x1, t);
398     y = path_line(y0, y1, t);
399     //scale = path_line(scale0, scale1, t);
400     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
401     actor_set_position_full(actor->widget, x, y, actor->z);
402     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
403     if (t >= 1){
404         /* stop animation */
405         actor->visible = FALSE;
406         destroy_hildon_actor(actor);
407         actor->time_start_animation = sec + 5*60;
408     }
409
410 }
411
412 void
413 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
414 {
415   GtkWidget *ha = NULL;
416   GdkPixbuf *pixbuf = NULL;
417   GtkWidget *image = NULL;
418   gchar *str;
419
420   /* fprintf(stderr, "create_hildon_actor %s\n", actor->name);*/
421   ha = hildon_animation_actor_new();
422   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
423                         desktop_plugin->priv->theme, actor->filename);
424   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
425                                              actor->width, 
426                                              actor->height, 
427                                              NULL);
428   if (pixbuf){
429       image = gtk_image_new_from_pixbuf (pixbuf);
430       g_object_unref(G_OBJECT(pixbuf));
431   }
432   if (image){
433     g_signal_connect(G_OBJECT(image), "expose_event",
434                            G_CALLBACK(expose_event), pixbuf);
435     gtk_container_add (GTK_CONTAINER (ha), image);
436   }  
437   actor_set_position_full(ha, actor->x, actor->y, actor->z);
438   hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(ha), actor->visible);
439   hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(ha), (double)actor->scale/100, (double)actor->scale/100);
440   realize(ha);
441   gtk_widget_show_all(ha);
442
443   //g_object_set_data(G_OBJECT(ha), "name", name);
444   //g_object_set_data(G_OBJECT(ha), "filename", filename);
445   g_object_set_data(G_OBJECT(ha), "image", image);
446   /*
447   g_object_set_data(G_OBJECT(ha), "x", x);
448   g_object_set_data(G_OBJECT(ha), "y", y);
449   g_object_set_data(G_OBJECT(ha), "z", z);
450   g_object_set_data(G_OBJECT(ha), "width", width);
451   g_object_set_data(G_OBJECT(ha), "height", height);
452   g_object_set_data(G_OBJECT(ha), "scale", scale);
453   g_object_set_data(G_OBJECT(ha), "visible", visible);
454   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
455   g_object_set_data(G_OBJECT(ha), "func", pfunc);
456   */
457   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
458   actor->widget = ha;
459 }
460
461
462 void
463 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
464 {
465     GtkWidget *image = NULL;
466     GdkPixbuf *pixbuf = NULL;
467     gchar *str;
468
469     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
470                             desktop_plugin->priv->theme, actor->filename);
471  
472     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
473                                                actor->width, 
474                                                actor->height, 
475                                                NULL);
476     if (pixbuf){
477         image = gtk_image_new_from_pixbuf (pixbuf);
478         g_object_unref(G_OBJECT(pixbuf));
479     }
480     if (image){ 
481         g_signal_connect(G_OBJECT(image), "expose_event",
482                                        G_CALLBACK(expose_event), pixbuf);
483         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
484             gtk_container_remove(GTK_CONTAINER(actor->widget), g_object_get_data(G_OBJECT(actor->widget), "image"));  
485         }
486         g_object_set_data(G_OBJECT(actor->widget), "image", image);
487         gtk_container_add (GTK_CONTAINER (actor->widget), image);
488         realize(actor->widget);
489         gtk_widget_show_all(actor->widget);
490     }
491
492 }
493
494
495 void 
496 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
497 {
498     gint daytime;
499     gchar *newfile;
500     if (!actor) return;
501     daytime = get_daytime();
502     newfile = g_strdup_printf("%s%d.png", actor->name, daytime); 
503     actor->filename = newfile;
504     change_hildon_actor(actor, desktop_plugin);
505 }
506
507
508
509 /*
510 static gint 
511 get_time(gint t){
512     // уравнение изменения времени
513     return t*1.1;
514 }
515 */
516 static void 
517 destroy_scene(void){
518     GSList * tmp = scene.actors;
519     Actor *actor;
520     while (tmp != NULL){
521         actor = tmp->data;
522         if (actor){
523             gtk_widget_destroy(actor->widget);
524             //actor->widget = NULL;
525             g_free(actor);
526         }
527         tmp = g_slist_next(tmp);
528     }
529     g_slist_free(tmp);
530
531 }
532
533 void
534 reload_scene(AWallpaperPlugin *desktop_plugin)
535 {
536     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
537     destroy_scene();
538     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
539         init_scene(desktop_plugin);
540     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
541         init_scene1(desktop_plugin);
542 }
543
544 void 
545 probability_sun(Actor *actor)
546 {
547     actor->time_start_animation += 20 * 1;
548     actor->duration_animation = G_MAXINT;
549 }
550 /* Init Modern Scene */
551 static void
552 init_scene(AWallpaperPlugin *desktop_plugin)
553 {
554   Actor *actor;
555
556   /* fprintf(stderr, "init scene \n");*/
557   scene.daytime = get_daytime();
558   scene.actors = NULL;
559
560   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 
561                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
562   change_static_actor(actor, desktop_plugin);
563   scene.actors = g_slist_append(scene.actors, actor);
564
565   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
566                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
567   actor->time_start_animation = time(NULL);
568   actor->duration_animation = G_MAXINT;
569   change_sun(actor, desktop_plugin);
570   scene.actors = g_slist_append(scene.actors, actor);
571
572   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
573                       TRUE, 100, 255, NULL, NULL);
574   scene.actors = g_slist_append(scene.actors, actor);
575
576
577   actor = init_object(desktop_plugin, "town", "town0.png", 0, 0, 7, 800, 480, 
578                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
579   change_static_actor(actor, desktop_plugin);
580   scene.actors = g_slist_append(scene.actors, actor);
581
582
583   
584   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, 8, 350, 210, 
585                       FALSE, 100, 255, (gpointer)&change_tram, NULL);
586   actor->time_start_animation = time(NULL) + 10;
587   actor->duration_animation = 60;
588   scene.actors = g_slist_append(scene.actors, actor);
589
590   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 9, 800, 79,
591                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
592   change_static_actor(actor, desktop_plugin);
593   scene.actors = g_slist_append(scene.actors, actor);
594   
595   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, 10, 60, 60, 
596                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
597   change_moon(actor, desktop_plugin);
598   scene.actors = g_slist_append(scene.actors, actor);
599
600 #if 0    
601   anim = g_new0(Animation, 1);
602   anim->count = 1;
603   anim->actor = actor;
604   anim->func_change = &change_tram;
605   anim->func_time = NULL;
606   anim->timestart = time(NULL); 
607   anim->timeall = 10;
608   
609   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
610 #endif  
611  }
612
613 /* Init Berlin Scene */
614 static void
615 init_scene1(AWallpaperPlugin *desktop_plugin)
616 {
617   Actor *actor;
618
619   scene.daytime = get_daytime();
620   scene.actors = NULL;
621   
622   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
623                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
624   change_static_actor(actor, desktop_plugin);
625   scene.actors = g_slist_append(scene.actors, actor);
626
627   
628   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
629                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
630   actor->time_start_animation = time(NULL);
631   actor->duration_animation = G_MAXINT;
632   change_sun(actor, desktop_plugin);
633   scene.actors = g_slist_append(scene.actors, actor);
634
635
636   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 10, 50, 50, 
637                       TRUE, 100, 255, NULL, NULL);
638   scene.actors = g_slist_append(scene.actors, actor);
639
640
641   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 9, 800, 480, 
642                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
643   change_static_actor(actor, desktop_plugin);
644   scene.actors = g_slist_append(scene.actors, actor);
645
646   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
647                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
648   change_moon(actor, desktop_plugin);
649   scene.actors = g_slist_append(scene.actors, actor);
650   
651   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 7, 160, 50, 
652                       FALSE, 100, 255, (gpointer)&change_plane2, NULL);
653   actor->time_start_animation = time(NULL) + 5;
654   actor->duration_animation = 60;
655   scene.actors = g_slist_append(scene.actors, actor);
656   
657   actor = init_object(desktop_plugin, "plane1", "tu154_small.png", 620, 233, 8, 300, 116, 
658                       FALSE, 100, 255, (gpointer)&change_plane1, NULL);
659   actor->time_start_animation = time(NULL) + 60;
660   actor->duration_animation = 30;
661   scene.actors = g_slist_append(scene.actors, actor);
662
663 }
664
665
666 void 
667 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
668 {
669     gint y0 = 365;// - уровень горизонта
670     *x = (int)(azm * 800) - 64;
671     *y = (int)((1 - alt) * y0) - 64;
672     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
673 }
674 #if 0
675 static void 
676 change_actor(GtkWidget * actor)
677 {
678     char * name;
679     gint x, y, daytime, scale;
680     gdouble sc;
681     double alt, azm;
682
683     GtkWidget *image;
684     GdkPixbuf *pixbuf;
685
686     void (*pfunc)(gpointer, gpointer);
687
688     name = g_object_get_data(G_OBJECT(actor), "name");
689     fprintf(stderr, "change actor %s\n", name);
690     if (name == "sun"){
691         pfunc = g_object_get_data(G_OBJECT(actor), "func");
692         if (pfunc)
693             (*pfunc)(actor, g_strdup(name));
694         daytime = get_daytime();
695         if (daytime != TIME_NIGHT){
696             hildon_animation_actor_set_show(actor, 1);
697             get_sun_pos(&alt, &azm);
698             get_sun_screen_pos(alt, azm, &x, &y);
699             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
700         }
701     }
702     
703     if (name == "cloud1"){
704         x = g_object_get_data(G_OBJECT(actor), "x");
705         y = g_object_get_data(G_OBJECT(actor), "y");
706         scale = g_object_get_data(G_OBJECT(actor), "scale");
707
708         /* Start */
709         image = g_object_get_data(G_OBJECT(actor), "image");
710         
711         gtk_container_remove(actor, image);  
712         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
713                                              200, 
714                                              200, 
715                                              NULL);
716         if (pixbuf){
717               image = gtk_image_new_from_pixbuf (pixbuf);
718               g_object_unref(G_OBJECT(pixbuf));
719         }
720         g_signal_connect(G_OBJECT(image), "expose_event",
721                                    G_CALLBACK(expose_event), pixbuf);
722         gtk_container_add (GTK_CONTAINER (actor), image);
723         realize(actor);
724         gtk_widget_show_all(actor);
725         /* End*/
726
727             
728         x += 40;
729         y -= 20;
730         scale -= 10;
731         if (x > 500){
732             x = 400;
733             y = 150;
734             sc = 1;
735         }
736         sc = (double)scale / 100;
737         hildon_animation_actor_set_scale(actor, sc, sc);
738         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
739         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
740         g_object_set_data(G_OBJECT(actor), "x", x);
741         g_object_set_data(G_OBJECT(actor), "y", y);
742         g_object_set_data(G_OBJECT(actor), "scale", scale);
743     }
744
745 }
746 #endif
747 static gboolean
748 short_timeout (AWallpaperPlugin *desktop_plugin)
749 {
750       gint daytime = get_daytime();
751       GSList * tmp;
752       void (*pfunc)(gpointer, gpointer);
753       time_t now;
754       Actor *actor;
755       gboolean stop_flag = TRUE;
756
757     if (!desktop_plugin->priv->visible)
758         return TRUE;
759
760     now = time(NULL);
761     /* fprintf(stderr, "Short timer %d\n", now); */
762     tmp = scene.actors;
763     while (tmp != NULL){
764            actor = tmp->data;
765            if (now >= actor->time_start_animation  
766                && actor->time_start_animation > 0
767                /* && now - actor->time_start_animation <= actor->duration_animation*/){
768                 pfunc = actor->func_change;
769                 if (pfunc){ 
770                     (*pfunc)(actor, desktop_plugin);
771                     stop_flag = FALSE;
772                 }
773             }
774             tmp = g_slist_next(tmp);
775     }
776     scene.daytime = daytime;
777     if (stop_flag){
778          desktop_plugin->priv->short_timer = 0;
779          return FALSE;
780     }else
781          return TRUE; /* keep running this event */
782 }
783
784
785 static gboolean
786 long_timeout (AWallpaperPlugin *desktop_plugin)
787 {
788       gint daytime = get_daytime();
789       GSList * tmp;
790       void (*pfunc)(gpointer, gpointer);
791       time_t now;
792       Actor *actor;
793
794     /* TODO  remove timeout */
795     if (!desktop_plugin->priv->visible)
796         return TRUE;
797   //fprintf(stderr, "timer daytime=%d\n", daytime);
798   if (scene.daytime != daytime){
799       tmp = scene.actors;
800       while (tmp != NULL){
801           //change_actor(tmp->data);
802           pfunc =((Actor*)tmp->data)->func_change;
803           if (pfunc){
804               (*pfunc)(tmp->data, desktop_plugin);
805           }
806           tmp = g_slist_next(tmp);
807       }
808    }
809    
810    now = time(NULL);
811 //fprintf(stderr, "Now  %d\n", now);
812    tmp = scene.actors;
813    while (tmp != NULL){
814        actor = tmp->data;
815        if (now >= actor->time_start_animation  
816            && actor->time_start_animation > 0
817            && desktop_plugin->priv->short_timer == 0){
818             actor->time_start_animation = now;
819             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
820             desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
821         }
822         tmp = g_slist_next(tmp);
823    }
824    
825  scene.daytime = daytime;
826
827  return TRUE; /* keep running this event */
828 }
829
830 static void
831 desktop_plugin_visible_notify (GObject    *object,
832                                           GParamSpec *spec,
833                                           AWallpaperPlugin *desktop_plugin)
834 {
835       gboolean visible;
836       g_object_get (object, "is-on-current-desktop", &visible, NULL);
837       if (visible)
838         desktop_plugin->priv->visible = TRUE;
839       else
840         desktop_plugin->priv->visible = FALSE;
841    /*   fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
842 }
843
844 static void
845 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
846 {
847     GtkWidget *label;
848
849     fprintf(stderr, "!!!!!!!plugin init \n");
850     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
851     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
852     /* Load config */
853     read_config(priv);
854     priv->desktop_plugin = desktop_plugin;
855     label = gtk_label_new (""); 
856     gtk_widget_set_size_request(label, 95, 30);
857     gtk_widget_show (label);
858     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
859     g_signal_connect (desktop_plugin, "show-settings",
860                              G_CALLBACK (lw_settings), priv);
861     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
862                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
863
864     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
865     init_applet_position();
866     fprintf(stderr, "!!!theme = %s\n", priv->theme);
867     if (!strcmp(priv->theme,"Modern"))
868         init_scene(desktop_plugin);
869     else if (!strcmp(priv->theme,"Berlin")) 
870         init_scene1(desktop_plugin);
871     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
872     priv->short_timer = 0;
873     /* TODO Move scene to priv */
874     scene.timer_type = LONG_TIMER_TYPE;
875
876     //sleep(2);
877 }
878
879 static void
880 lw_applet_finalize (GObject *object)
881 {
882      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
883      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
884
885      if (priv->long_timer){
886         g_source_remove(priv->long_timer);
887         priv->long_timer = 0;
888      }
889      if (priv->short_timer){
890         g_source_remove(priv->short_timer);
891         priv->short_timer = 0;
892      }
893
894      destroy_scene();
895 }
896
897 static void
898 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
899
900     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
901     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
902
903     /* gobject */
904     gobject_class->destroy = (gpointer)lw_applet_finalize;
905     widget_class->realize = lw_applet_realize;
906     widget_class->expose_event = lw_applet_expose_event;
907
908     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
909
910 }
911
912 static void
913 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}