added clouds in Modern, added wind
[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 425 
45
46 gint xapplet = 0, yapplet = 0;
47 GSList * objects_list = NULL;
48 Scene scene;
49
50 static void
51 lw_applet_realize (GtkWidget *widget)
52 {
53       GdkScreen *screen;
54
55       screen = gtk_widget_get_screen (widget);
56       gtk_widget_set_colormap (widget,
57                                 gdk_screen_get_rgba_colormap (screen));
58       gtk_widget_set_app_paintable (widget,
59                                 TRUE);
60       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
61 }
62
63
64 static gboolean
65 lw_applet_expose_event(GtkWidget      *widget,
66                                         GdkEventExpose *event)
67 {
68   cairo_t *cr;
69
70   /* Create cairo context */
71   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
72   gdk_cairo_region (cr, event->region);
73   cairo_clip (cr);
74
75   /* Draw alpha background */
76   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
77   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
78   cairo_paint (cr);
79
80   /* Free context */
81   cairo_destroy (cr);
82
83   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
84                                                                                   event);
85 }
86
87 static gboolean
88 expose_event (GtkWidget *widget,GdkEventExpose *event,
89      gpointer data)
90 {
91     cairo_t *cr;
92     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
93         
94     cr = gdk_cairo_create(widget->window);
95     if (cr){
96         gdk_cairo_region(cr, event->region);
97         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
98         gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
99         cairo_paint(cr);
100         cairo_destroy(cr);
101     }
102     return TRUE;
103 }
104
105 static void
106 realize (GtkWidget *widget)
107 {
108     GdkScreen *screen;
109     screen = gtk_widget_get_screen (widget);
110     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
111 }
112
113 /* Set position of widget on desktop */
114 static void
115 init_applet_position(void)
116 {
117   GSList *position = NULL;
118   gchar *position_key;
119   gchar *modified_key;
120   gchar *modified;
121   GError *error = NULL;
122   GConfClient   *gconf_client = gconf_client_get_default ();
123   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
124   position = gconf_client_get_list (gconf_client,
125                                     position_key,
126                                     GCONF_VALUE_INT,
127                                     NULL);
128   if (position && position->data && position->next->data){
129         xapplet = GPOINTER_TO_INT (position->data);
130         yapplet = GPOINTER_TO_INT (position->next->data);
131   }else{
132         position = g_slist_prepend (g_slist_prepend (NULL,
133                                       GINT_TO_POINTER (Ystartposition)),
134                                       GINT_TO_POINTER (Xstartposition));
135         gconf_client_set_list (gconf_client,
136                                position_key,
137                                GCONF_VALUE_INT,
138                                position,
139                                &error);
140         xapplet = Xstartposition;
141         yapplet = Ystartposition;
142   }
143   g_free (position_key);
144   modified = g_strdup_printf ("%i", 0);
145   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
146   gconf_client_set_string (gconf_client,
147                            modified_key,
148                            modified,
149                            &error);
150   g_free(modified);
151   g_free(modified_key);
152   gconf_client_clear_cache(gconf_client);
153   g_object_unref(gconf_client);
154 }
155
156 void
157 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
158 {
159 /*  fprintf(stderr, "actor_set_position_full z=%d\n", z); */
160     hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
161 }
162
163 static gint 
164 path_line(gint x0, gint x1, double t)
165 {
166     // уравниение прямой
167     return ((x1 - x0) * t + x0);
168 }
169
170
171 void
172 destroy_hildon_actor(Actor *actor)
173 {
174     fprintf(stderr, "estroy_hildon_actor %p\n",actor->widget);
175     gtk_widget_destroy(actor->widget);
176     actor->widget = NULL;
177 }
178
179 static Actor* 
180 init_object(AWallpaperPlugin *desktop_plugin, 
181             gchar * name, 
182             gchar * filename, 
183             gint x, 
184             gint y, 
185             gint z, 
186             gint width, 
187             gint height, 
188             gboolean visible, 
189             gint scale, 
190             gint opacity, 
191             void (*pfunc_change)(Actor*),
192             void (*pfunc_probability)(Actor*)
193            )
194 {
195   Actor *actor = NULL;
196   actor = g_new0(Actor, 1);
197   actor->x = x;
198   actor->y = y;
199   actor->z = z;
200   actor->width = width;
201   actor->height = height;
202   actor->visible = visible;
203   actor->scale = scale;
204   actor->opacity = opacity;
205   actor->filename = g_strdup(filename);
206   actor->name = g_strdup(name);
207   actor->func_change = (gpointer)pfunc_change; 
208   actor->func_probability = (gpointer)pfunc_probability;
209   if (visible)
210     create_hildon_actor(actor, desktop_plugin);
211   else 
212     actor->widget = NULL;
213   actor->time_start_animation = 0;
214   actor->duration_animation = 0;
215   /*
216   a.widget = actor;
217   a.name = name;
218   a.x = x;
219   a.y = y;
220   a.z = z;
221   */
222   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
223   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
224   return actor;
225 }
226
227 gint 
228 rnd(gint max)
229 {
230     srand(time(NULL));
231     return rand() % max;
232 }
233 guint seed;
234 gint fast_rnd(gint max)
235 {
236     guint offset = 12923;
237     guint multiplier = 4079;
238     
239     seed = seed * multiplier + offset;
240     return (gint)(seed % max);
241 }
242
243 gint 
244 probability_sun()
245 {
246     /* update sun position after ...  second */
247     return 60;
248 }
249
250
251 gint 
252 probability_plane()
253 {
254     return (fast_rnd(10) + 1) * 60;
255 }
256
257 void 
258 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
259 {
260     gint phase;
261     char *newfile;
262
263     if (actor){
264         if (get_daytime() == TIME_NIGHT){
265             if (!actor->visible){
266                 actor->visible = TRUE;
267                 phase = get_moon_phase();
268                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
269                 if (actor->filename)
270                     g_free(actor->filename);
271                 actor->filename = newfile;
272                 create_hildon_actor(actor, desktop_plugin);
273
274             }
275             //actor->x = 400;
276             //actor->y = 10;
277            // actor_set_position_full(actor->widget, x, y, actor->z);
278             //probability_sun(actor);
279             //fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
280          }else if (actor->visible){
281             actor->visible = FALSE;
282             fprintf(stderr, "destroy moon \n");
283             destroy_hildon_actor(actor);
284             /* TO DO make moonrise*/
285             actor->time_start_animation = 0;
286         } 
287     }
288     
289 }
290
291 void 
292 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
293 {
294     double alt, azm;
295     gint x, y;
296
297     //fprintf(stderr, "change sun\n");
298     if (actor){
299         if (scene.daytime != TIME_NIGHT){
300             if (!actor->visible){
301                 actor->visible = TRUE;
302                 create_hildon_actor(actor, desktop_plugin);
303             }
304             get_sun_pos(&alt, &azm);
305             get_sun_screen_pos(alt, azm, &x, &y);
306             actor->x = x;
307             actor->y = y;
308             actor_set_position_full(actor->widget, x, y, actor->z);
309             //probability_sun(actor);
310             actor->time_start_animation = time(NULL) + probability_sun();
311          }else if (actor->visible){
312             actor->visible = FALSE;
313             destroy_hildon_actor(actor);
314             actor->time_start_animation = 0;
315         } 
316     }
317     
318 }
319
320 static void 
321 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
322 {
323     gint x0 = -300, y0 = 225, scale0 = 100,
324          x1 = 800, y1 = 162, scale1 = 130, 
325          x, y, scale;
326     struct timeval tvb;     
327     suseconds_t ms;
328     long sec;
329     double t;
330
331     gettimeofday(&tvb, NULL);
332     
333     ms = tvb.tv_usec;
334     sec = tvb.tv_sec;
335 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
336    
337     if (!actor->visible){
338         actor->visible = TRUE;
339         if (scene.daytime == TIME_NIGHT){
340             if (actor->filename)
341                 g_free(actor->filename);
342             actor->filename = g_strdup("tram_dark.png");
343         } else{
344             if (actor->filename)
345                 g_free(actor->filename);
346             actor->filename = g_strdup("tram.png");
347         }
348         create_hildon_actor(actor, desktop_plugin);
349     }
350     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
351     x = path_line(x0, x1, t);
352     y = path_line(y0, y1, t);
353     scale = path_line(scale0, scale1, t);
354     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
355     actor_set_position_full(actor->widget, x, y, actor->z);
356     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
357     if (t >= 1){
358         /* stop animation */
359         actor->visible = FALSE;
360         destroy_hildon_actor(actor);
361         actor->time_start_animation = sec + fast_rnd(300);
362     }
363 }
364
365 void
366 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
367 {
368     gint x0 = 620, y0 = 233, scale0 = 100,
369          x1 = 79, y1 = -146, scale1 = 100, 
370          x, y, scale;
371     struct timeval tvb;     
372     suseconds_t ms;
373     long sec;
374     double t;
375
376     gettimeofday(&tvb, NULL);
377     
378     ms = tvb.tv_usec;
379     sec = tvb.tv_sec;
380 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
381    
382     if (!actor->visible){
383         actor->visible = TRUE;
384         if (scene.daytime == TIME_NIGHT){
385             if (actor->filename)
386                 g_free(actor->filename);
387             actor->filename = g_strdup("tu154.png");
388         }else{
389             if (actor->filename)
390                 g_free(actor->filename);
391             actor->filename = g_strdup("tu154.png");
392         }
393         create_hildon_actor(actor, desktop_plugin);
394     }
395     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
396     x = path_line(x0, x1, t);
397     y = path_line(y0, y1, t);
398     //scale = path_line(scale0, scale1, t);
399     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
400     actor_set_position_full(actor->widget, x, y, actor->z);
401     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
402     if (t >= 1){
403         /* stop animation */
404         actor->visible = FALSE;
405         destroy_hildon_actor(actor);
406         actor->time_start_animation = sec + probability_plane();
407     }
408
409 }
410
411 void
412 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
413 {
414     gint x0 = -actor->width, y0 = 45, scale0 = 100,
415          x1 = 800, y1 = 20, scale1 = 100, 
416          x, y, scale;
417     struct timeval tvb;     
418     suseconds_t ms;
419     long sec;
420     double t;
421
422     gettimeofday(&tvb, NULL);
423     
424     ms = tvb.tv_usec;
425     sec = tvb.tv_sec;
426 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
427    
428     if (!actor->visible){
429         actor->visible = TRUE;
430         if (scene.daytime == TIME_NIGHT){
431             if (actor->filename)
432                 g_free(actor->filename);
433         if (scene.daytime == TIME_NIGHT)
434             actor->filename = g_strdup("plane3.png");
435         }else{
436             if (actor->filename)
437                 g_free(actor->filename);
438             actor->filename = g_strdup("plane3.png");
439         }
440         create_hildon_actor(actor, desktop_plugin);
441     }
442     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
443     x = path_line(x0, x1, t);
444     y = path_line(y0, y1, t);
445     //scale = path_line(scale0, scale1, t);
446     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
447     actor_set_position_full(actor->widget, x, y, actor->z);
448     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
449     if (t >= 1){
450         /* stop animation */
451         actor->visible = FALSE;
452         destroy_hildon_actor(actor);
453         actor->time_start_animation = sec + probability_plane();
454     }
455
456 }
457
458 void
459 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
460 {
461     gint x0, y0 = 300, scale0 = 100,
462          x1, y1 = -actor->height, scale1 = 150, 
463          x, y, scale;
464     struct timeval tvb;     
465     suseconds_t ms;
466     long sec;
467     double t;
468
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             actor->filename = actor->filename;
479         else
480             actor->filename = actor->filename;
481         create_hildon_actor(actor, desktop_plugin);
482     }
483     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
484     
485     if (scene.wind_orientation == 1){
486         x0 = -actor->width;
487         x1 = 800;
488     }
489     else {
490         x0 = 800;
491         x1 = -actor->width;
492     }
493
494     x = path_line(x0, x1, t);    
495     y = -scene.wind_angle * (x - x0) + actor->y;
496     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
497
498     //fprintf(stderr, "change cloud t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
499     actor_set_position_full(actor->widget, x, y, actor->z);
500     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
501     if ((y < y1 || y > y0) || t >= 1){
502         /* stop animation */
503         actor->visible = FALSE;
504         destroy_hildon_actor(actor);
505         actor->time_start_animation = sec + fast_rnd(300);
506         actor->y = fast_rnd(300);
507     }
508
509 }
510
511 void
512 change_wind(Actor *actor, AWallpaperPlugin *desktop_plugin)
513 {
514     scene.wind_orientation = fast_rnd(2) - 1;
515     scene.wind_angle = (double)(fast_rnd(400) - 200) / 100;
516     actor->time_start_animation = time(NULL) + (fast_rnd(20) + 10) * 60;
517     fprintf(stderr, "change wind orient = %d angle = %f after = %d\n", scene.wind_orientation, scene.wind_angle, actor->time_start_animation-time(NULL));
518 }
519
520 void
521 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
522 {
523   GtkWidget *ha = NULL;
524   GdkPixbuf *pixbuf = NULL;
525   GtkWidget *image = NULL;
526   gchar     *str = NULL;
527
528   /* fprintf(stderr, "create_hildon_actor %s\n", actor->name);*/
529   ha = hildon_animation_actor_new();
530   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
531                         desktop_plugin->priv->theme, actor->filename);
532   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
533                                              actor->width, 
534                                              actor->height, 
535                                              NULL);
536   if (str)
537       g_free(str);
538   if (pixbuf){
539       image = gtk_image_new_from_pixbuf (pixbuf);
540       g_object_unref(G_OBJECT(pixbuf));
541   }
542   if (image){
543     g_signal_connect(G_OBJECT(image), "expose_event",
544                            G_CALLBACK(expose_event), pixbuf);
545     gtk_container_add (GTK_CONTAINER (ha), image);
546   }  
547   actor_set_position_full(ha, actor->x, actor->y, actor->z);
548   hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(ha), actor->visible);
549   hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(ha), (double)actor->scale/100, (double)actor->scale/100);
550   realize(ha);
551   gtk_widget_show_all(ha);
552
553   /* TO DO check it */
554   /*  gdk_flush (); */
555
556   //g_object_set_data(G_OBJECT(ha), "name", name);
557   //g_object_set_data(G_OBJECT(ha), "filename", filename);
558   g_object_set_data(G_OBJECT(ha), "image", image);
559   /*
560   g_object_set_data(G_OBJECT(ha), "x", x);
561   g_object_set_data(G_OBJECT(ha), "y", y);
562   g_object_set_data(G_OBJECT(ha), "z", z);
563   g_object_set_data(G_OBJECT(ha), "width", width);
564   g_object_set_data(G_OBJECT(ha), "height", height);
565   g_object_set_data(G_OBJECT(ha), "scale", scale);
566   g_object_set_data(G_OBJECT(ha), "visible", visible);
567   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
568   g_object_set_data(G_OBJECT(ha), "func", pfunc);
569   */
570   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
571   actor->widget = ha;
572 }
573
574
575 void
576 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
577 {
578     GtkWidget *image = NULL;
579     GdkPixbuf *pixbuf = NULL;
580     gchar     *str = NULL;
581
582     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
583                             desktop_plugin->priv->theme, actor->filename);
584  
585     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
586                                                actor->width, 
587                                                actor->height, 
588                                                NULL);
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         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
599             gtk_container_remove(GTK_CONTAINER(actor->widget), g_object_get_data(G_OBJECT(actor->widget), "image"));  
600         }
601         g_object_set_data(G_OBJECT(actor->widget), "image", image);
602         gtk_container_add (GTK_CONTAINER (actor->widget), image);
603         realize(actor->widget);
604         gtk_widget_show_all(actor->widget);
605         /* TO DO check it */
606        /*  gdk_flush (); */
607
608
609     }
610
611 }
612
613
614 void 
615 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
616 {
617     gint daytime;
618     gchar *newfile;
619     if (!actor) return;
620     daytime = get_daytime();
621     newfile = g_strdup_printf("%s%d.png", actor->name, daytime); 
622     if (actor->filename)
623             g_free(actor->filename);
624     actor->filename = newfile;
625     change_hildon_actor(actor, desktop_plugin);
626 }
627
628
629
630 /*
631 static gint 
632 get_time(gint t){
633     // уравнение изменения времени
634     return t*1.1;
635 }
636 */
637 static void 
638 destroy_scene(void){
639     GSList * tmp = scene.actors;
640     Actor *actor;
641     while (tmp != NULL){
642         actor = tmp->data;
643         if (actor){
644             if (actor->filename)
645                 g_free(actor->filename);
646             if (actor->name)
647                 g_free(actor->name);
648             gtk_widget_destroy(actor->widget);
649             //actor->widget = NULL;
650             g_free(actor);
651         }
652         tmp = g_slist_next(tmp);
653     }
654     g_slist_free(tmp);
655
656 }
657
658 void
659 reload_scene(AWallpaperPlugin *desktop_plugin)
660 {
661     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
662     destroy_scene();
663     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
664         init_scene(desktop_plugin);
665     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
666         init_scene1(desktop_plugin);
667 }
668
669
670 /* Init Modern Scene */
671 static void
672 init_scene(AWallpaperPlugin *desktop_plugin)
673 {
674   Actor *actor;
675   gint now = time(NULL);
676
677   /* fprintf(stderr, "init scene \n");*/
678   scene.daytime = get_daytime();
679   scene.actors = NULL;
680   scene.wind_orientation = -1;
681   scene.wind_angle = 0.3;
682   
683
684   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 
685                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
686   change_static_actor(actor, desktop_plugin);
687   scene.actors = g_slist_append(scene.actors, actor);
688
689   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
690                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
691   actor->time_start_animation = time(NULL);
692   actor->duration_animation = G_MAXINT;
693   change_sun(actor, desktop_plugin);
694   scene.actors = g_slist_append(scene.actors, actor);
695
696   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
697                       TRUE, 100, 255, NULL, NULL);
698   scene.actors = g_slist_append(scene.actors, actor);
699   
700   actor = init_object(desktop_plugin, "cloud1", "cloud1.png", 0, fast_rnd(300)-97, 7, 150, 97, 
701                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
702   actor->time_start_animation = now + fast_rnd(180);
703   actor->duration_animation = 5*60;
704   scene.actors = g_slist_append(scene.actors, actor);
705   
706   actor = init_object(desktop_plugin, "cloud2", "cloud2.png", 0, fast_rnd(300)-75, 7, 188, 75, 
707                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
708   actor->time_start_animation = now + fast_rnd(180);
709   actor->duration_animation = 5*60;
710   scene.actors = g_slist_append(scene.actors, actor);
711
712   actor = init_object(desktop_plugin, "cloud4", "cloud4.png", 0, fast_rnd(300)-75, 7, 150, 75, 
713                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
714   actor->time_start_animation = now + fast_rnd(180);
715   actor->duration_animation = 5*60;
716   scene.actors = g_slist_append(scene.actors, actor);
717
718
719   actor = init_object(desktop_plugin, "town", "town0.png", 0, 0, 8, 800, 480, 
720                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
721   change_static_actor(actor, desktop_plugin);
722   scene.actors = g_slist_append(scene.actors, actor);
723
724   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, 9, 350, 210, 
725                       FALSE, 100, 255, (gpointer)&change_tram, NULL);
726   actor->time_start_animation = time(NULL) + fast_rnd(60);
727   actor->duration_animation = 60;
728   scene.actors = g_slist_append(scene.actors, actor);
729
730   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 10, 800, 79,
731                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
732   change_static_actor(actor, desktop_plugin);
733   scene.actors = g_slist_append(scene.actors, actor);
734   
735   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, 6, 60, 60, 
736                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
737   change_moon(actor, desktop_plugin);
738   scene.actors = g_slist_append(scene.actors, actor);
739
740   actor = init_object(desktop_plugin, "wind", "", 0, 0, 5, 0, 0, 
741                       FALSE, 100, 255, (gpointer)&change_wind, NULL);
742   change_wind(actor, desktop_plugin);
743   scene.actors = g_slist_append(scene.actors, actor);
744
745 #if 0    
746   anim = g_new0(Animation, 1);
747   anim->count = 1;
748   anim->actor = actor;
749   anim->func_change = &change_tram;
750   anim->func_time = NULL;
751   anim->timestart = time(NULL); 
752   anim->timeall = 10;
753   
754   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
755 #endif  
756  }
757
758 /* Init Berlin Scene */
759 static void
760 init_scene1(AWallpaperPlugin *desktop_plugin)
761 {
762   Actor *actor;
763   gint now = time(NULL);
764   
765
766   scene.daytime = get_daytime();
767   scene.actors = NULL;
768   scene.wind_orientation = -1;
769   scene.wind_angle = 0.3;
770   
771   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
772                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
773   change_static_actor(actor, desktop_plugin);
774   scene.actors = g_slist_append(scene.actors, actor);
775
776   
777   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
778                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
779   actor->time_start_animation = time(NULL);
780   actor->duration_animation = G_MAXINT;
781   change_sun(actor, desktop_plugin);
782   scene.actors = g_slist_append(scene.actors, actor);
783
784
785   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
786                       TRUE, 100, 255, NULL, NULL);
787   scene.actors = g_slist_append(scene.actors, actor);
788
789
790   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
791                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
792   change_moon(actor, desktop_plugin);
793   scene.actors = g_slist_append(scene.actors, actor);
794   
795   actor = init_object(desktop_plugin, "cloud1", "cloud2.png", 0, fast_rnd(300), 7, 188, 75, 
796                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
797   actor->time_start_animation = now + fast_rnd(3) * 60;
798   actor->duration_animation = 5*60;
799   scene.actors = g_slist_append(scene.actors, actor);
800
801   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 8, 160, 50, 
802                       FALSE, 100, 255, (gpointer)&change_plane2, NULL);
803   actor->time_start_animation = now + probability_plane();
804   actor->duration_animation = 60;
805   scene.actors = g_slist_append(scene.actors, actor);
806   
807   actor = init_object(desktop_plugin, "plane1", "tu154_small.png", 620, 233, 9, 300, 116, 
808                       FALSE, 100, 255, (gpointer)&change_plane1, NULL);
809   actor->time_start_animation = now + probability_plane();
810   actor->duration_animation = 30;
811   scene.actors = g_slist_append(scene.actors, actor);
812
813   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
814                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
815   change_static_actor(actor, desktop_plugin);
816   scene.actors = g_slist_append(scene.actors, actor);
817
818
819 }
820
821
822 void 
823 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
824 {
825     gint y0 = 365;// - уровень горизонта
826     *x = (int)(azm * 800) - 64;
827     *y = (int)((1 - alt) * y0) - 64;
828     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
829 }
830 #if 0
831 static void 
832 change_actor(GtkWidget * actor)
833 {
834     char * name;
835     gint x, y, daytime, scale;
836     gdouble sc;
837     double alt, azm;
838
839     GtkWidget *image;
840     GdkPixbuf *pixbuf;
841
842     void (*pfunc)(gpointer, gpointer);
843
844     name = g_object_get_data(G_OBJECT(actor), "name");
845     fprintf(stderr, "change actor %s\n", name);
846     if (name == "sun"){
847         pfunc = g_object_get_data(G_OBJECT(actor), "func");
848         if (pfunc)
849             (*pfunc)(actor, g_strdup(name));
850         daytime = get_daytime();
851         if (daytime != TIME_NIGHT){
852             hildon_animation_actor_set_show(actor, 1);
853             get_sun_pos(&alt, &azm);
854             get_sun_screen_pos(alt, azm, &x, &y);
855             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
856         }
857     }
858     
859     if (name == "cloud1"){
860         x = g_object_get_data(G_OBJECT(actor), "x");
861         y = g_object_get_data(G_OBJECT(actor), "y");
862         scale = g_object_get_data(G_OBJECT(actor), "scale");
863
864         /* Start */
865         image = g_object_get_data(G_OBJECT(actor), "image");
866         
867         gtk_container_remove(actor, image);  
868         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
869                                              200, 
870                                              200, 
871                                              NULL);
872         if (pixbuf){
873               image = gtk_image_new_from_pixbuf (pixbuf);
874               g_object_unref(G_OBJECT(pixbuf));
875         }
876         g_signal_connect(G_OBJECT(image), "expose_event",
877                                    G_CALLBACK(expose_event), pixbuf);
878         gtk_container_add (GTK_CONTAINER (actor), image);
879         realize(actor);
880         gtk_widget_show_all(actor);
881         /* End*/
882
883             
884         x += 40;
885         y -= 20;
886         scale -= 10;
887         if (x > 500){
888             x = 400;
889             y = 150;
890             sc = 1;
891         }
892         sc = (double)scale / 100;
893         hildon_animation_actor_set_scale(actor, sc, sc);
894         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
895         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
896         g_object_set_data(G_OBJECT(actor), "x", x);
897         g_object_set_data(G_OBJECT(actor), "y", y);
898         g_object_set_data(G_OBJECT(actor), "scale", scale);
899     }
900
901 }
902 #endif
903 static gboolean
904 short_timeout (AWallpaperPlugin *desktop_plugin)
905 {
906       gint daytime = get_daytime();
907       GSList * tmp;
908       void (*pfunc)(gpointer, gpointer);
909       time_t now;
910       Actor *actor;
911       gboolean stop_flag = TRUE;
912
913     if (!desktop_plugin->priv->visible)
914         return TRUE;
915
916     now = time(NULL);
917     //scene.daytime = daytime;
918     /* fprintf(stderr, "Short timer %d\n", now); */
919     tmp = scene.actors;
920     while (tmp != NULL){
921            actor = tmp->data;
922            if (now >= actor->time_start_animation  
923                && actor->time_start_animation > 0
924                /* && now - actor->time_start_animation <= actor->duration_animation*/){
925                 pfunc = actor->func_change;
926                 if (pfunc){ 
927                     (*pfunc)(actor, desktop_plugin);
928                     stop_flag = FALSE;
929                 }
930             }
931             tmp = g_slist_next(tmp);
932     }
933     if (stop_flag){
934          desktop_plugin->priv->short_timer = 0;
935          return FALSE;
936     }else
937          return TRUE; /* keep running this event */
938 }
939
940
941 static gboolean
942 long_timeout (AWallpaperPlugin *desktop_plugin)
943 {
944     gint daytime = get_daytime();
945     GSList * tmp;
946     void (*pfunc)(gpointer, gpointer);
947     time_t now;
948     Actor *actor;
949
950     /* TODO  remove timeout */
951     if (!desktop_plugin->priv->visible)
952         return TRUE;
953     //fprintf(stderr, "timer daytime=%d\n", daytime);
954     if (scene.daytime != daytime){
955         tmp = scene.actors;
956         while (tmp != NULL){
957             //change_actor(tmp->data);
958             pfunc =((Actor*)tmp->data)->func_change;
959             if (pfunc){
960                 (*pfunc)(tmp->data, desktop_plugin);
961             }
962             tmp = g_slist_next(tmp);
963         }
964     }
965    
966     now = time(NULL);
967     //fprintf(stderr, "Now  %d\n", now);
968     tmp = scene.actors;
969     while (tmp != NULL){
970         actor = tmp->data;
971         if (now >= actor->time_start_animation  
972             && actor->time_start_animation > 0
973             && desktop_plugin->priv->short_timer == 0){
974             actor->time_start_animation = now;
975             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
976             desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
977         }
978         tmp = g_slist_next(tmp);
979     }
980    
981     scene.daytime = daytime;
982
983     return TRUE; /* keep running this event */
984 }
985
986 static void
987 desktop_plugin_visible_notify (GObject    *object,
988                                           GParamSpec *spec,
989                                           AWallpaperPlugin *desktop_plugin)
990 {
991       gboolean visible;
992       g_object_get (object, "is-on-current-desktop", &visible, NULL);
993       if (visible)
994         desktop_plugin->priv->visible = TRUE;
995       else
996         desktop_plugin->priv->visible = FALSE;
997    /*   fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
998 }
999
1000 gboolean
1001 rich_animation_press(GtkWidget *widget, GdkEvent *event,
1002                                             gpointer user_data){
1003     fprintf(stderr,"gggggggggggggggggggg\n");
1004 }    
1005 static void
1006 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
1007 {
1008     GtkWidget *label;
1009     GtkWidget *rich_animation;
1010     fprintf(stderr, "!!!!!!!plugin init \n");
1011     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1012     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
1013     /* Load config */
1014     read_config(priv);
1015     priv->desktop_plugin = desktop_plugin;
1016     /* Create rich animation event */
1017     rich_animation = gtk_event_box_new();
1018     if(rich_animation){
1019         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
1020         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
1021         gtk_widget_set_size_request(rich_animation, 95, 30);
1022        gtk_widget_show (rich_animation);
1023         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
1024     }
1025 /*
1026     label = gtk_label_new (""); 
1027     gtk_widget_set_size_request(label, 95, 30);
1028     gtk_widget_show (label);
1029     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1030 */
1031     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1032     g_signal_connect (desktop_plugin, "show-settings",
1033                              G_CALLBACK (lw_settings), priv);
1034     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1035                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1036
1037     gtk_container_add (GTK_CONTAINER (desktop_plugin), rich_animation);
1038     init_applet_position();
1039
1040     /* start value for random */
1041     seed = time(NULL);
1042
1043     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1044     if (!strcmp(priv->theme,"Modern"))
1045         init_scene(desktop_plugin);
1046     else if (!strcmp(priv->theme,"Berlin")) 
1047         init_scene1(desktop_plugin);
1048     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1049     priv->short_timer = 0;
1050     /* TODO Move scene to priv */
1051     scene.timer_type = LONG_TIMER_TYPE;
1052     
1053     //sleep(2);
1054 }
1055
1056 static void
1057 lw_applet_finalize (GObject *object)
1058 {
1059      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1060      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1061
1062      if (priv->long_timer){
1063         g_source_remove(priv->long_timer);
1064         priv->long_timer = 0;
1065      }
1066      if (priv->short_timer){
1067         g_source_remove(priv->short_timer);
1068         priv->short_timer = 0;
1069      }
1070
1071      destroy_scene();
1072 }
1073
1074 static void
1075 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1076
1077     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1078     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1079
1080     /* gobject */
1081     gobject_class->destroy = (gpointer)lw_applet_finalize;
1082     widget_class->realize = lw_applet_realize;
1083     widget_class->expose_event = lw_applet_expose_event;
1084
1085     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1086
1087 }
1088
1089 static void
1090 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}