rich_animation option is done
[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     
336     if (!actor->visible){
337         actor->visible = TRUE;
338         if (scene.daytime == TIME_NIGHT){
339             if (actor->filename)
340                 g_free(actor->filename);
341             actor->filename = g_strdup("tram_dark.png");
342         } else{
343             if (actor->filename)
344                 g_free(actor->filename);
345             actor->filename = g_strdup("tram.png");
346         }
347         create_hildon_actor(actor, desktop_plugin);
348     }
349     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
350     x = path_line(x0, x1, t);
351     y = path_line(y0, y1, t);
352     scale = path_line(scale0, scale1, t);
353     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
354     actor_set_position_full(actor->widget, x, y, actor->z);
355     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
356     if (t >= 1){
357         /* stop animation */
358         actor->visible = FALSE;
359         destroy_hildon_actor(actor);
360         actor->time_start_animation = sec + 5*60;
361     }
362 }
363
364 void
365 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
366 {
367     gint x0 = 620, y0 = 233, scale0 = 100,
368          x1 = 79, y1 = -146, scale1 = 100, 
369          x, y, scale;
370     struct timeval tvb;     
371     suseconds_t ms;
372     long sec;
373     double t;
374
375     gettimeofday(&tvb, NULL);
376     
377     ms = tvb.tv_usec;
378     sec = tvb.tv_sec;
379 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
380    
381     if (!actor->visible){
382         actor->visible = TRUE;
383         if (scene.daytime == TIME_NIGHT){
384             if (actor->filename)
385                 g_free(actor->filename);
386             actor->filename = g_strdup("tu154.png");
387         }else{
388             if (actor->filename)
389                 g_free(actor->filename);
390             actor->filename = g_strdup("tu154.png");
391         }
392         create_hildon_actor(actor, desktop_plugin);
393     }
394     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
395     x = path_line(x0, x1, t);
396     y = path_line(y0, y1, t);
397     //scale = path_line(scale0, scale1, t);
398     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
399     actor_set_position_full(actor->widget, x, y, actor->z);
400     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
401     if (t >= 1){
402         /* stop animation */
403         actor->visible = FALSE;
404         destroy_hildon_actor(actor);
405         actor->time_start_animation = sec + probability_plane();
406     }
407
408 }
409
410 void
411 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
412 {
413     gint x0 = -actor->width, y0 = 45, scale0 = 100,
414          x1 = 800, y1 = 20, scale1 = 100, 
415          x, y, scale;
416     struct timeval tvb;     
417     suseconds_t ms;
418     long sec;
419     double t;
420
421     gettimeofday(&tvb, NULL);
422     
423     ms = tvb.tv_usec;
424     sec = tvb.tv_sec;
425 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
426    
427     if (!actor->visible){
428         actor->visible = TRUE;
429         if (scene.daytime == TIME_NIGHT){
430             if (actor->filename)
431                 g_free(actor->filename);
432         if (scene.daytime == TIME_NIGHT)
433             actor->filename = g_strdup("plane3.png");
434         }else{
435             if (actor->filename)
436                 g_free(actor->filename);
437             actor->filename = g_strdup("plane3.png");
438         }
439         create_hildon_actor(actor, desktop_plugin);
440     }
441     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
442     x = path_line(x0, x1, t);
443     y = path_line(y0, y1, t);
444     //scale = path_line(scale0, scale1, t);
445     //fprintf(stderr, "change tram t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
446     actor_set_position_full(actor->widget, x, y, actor->z);
447     //hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
448     if (t >= 1){
449         /* stop animation */
450         actor->visible = FALSE;
451         destroy_hildon_actor(actor);
452         actor->time_start_animation = sec + probability_plane();
453     }
454
455 }
456
457 void
458 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
459 {
460     gint x0, y0 = 300, scale0 = 100,
461          x1, y1 = -actor->height, scale1 = 150, 
462          x, y, scale;
463     struct timeval tvb;     
464     suseconds_t ms;
465     long sec;
466     double t;
467
468     gettimeofday(&tvb, NULL);
469     
470     ms = tvb.tv_usec;
471     sec = tvb.tv_sec;
472 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
473    
474     if (!actor->visible){
475         actor->visible = TRUE;
476         if (scene.daytime == TIME_NIGHT)
477             actor->filename = actor->filename;
478         else
479             actor->filename = actor->filename;
480         create_hildon_actor(actor, desktop_plugin);
481     }
482     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
483     
484     if (scene.wind_orientation == 1){
485         x0 = -actor->width;
486         x1 = 800;
487     }
488     else {
489         x0 = 800;
490         x1 = -actor->width;
491     }
492
493     x = path_line(x0, x1, t);    
494     y = -scene.wind_angle * (x - x0) + actor->y;
495     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
496
497     //fprintf(stderr, "change cloud t=%f x=%d y=%d scale=%d\n", t, x, y, scale);
498     actor_set_position_full(actor->widget, x, y, actor->z);
499     hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(actor->widget), (double)scale/100, (double)scale/100);
500     if ((y < y1 || y > y0) || t >= 1){
501         /* stop animation */
502         actor->visible = FALSE;
503         destroy_hildon_actor(actor);
504         actor->time_start_animation = sec + (fast_rnd(5) + 1)*60;
505     }
506
507 }
508
509 void
510 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
511 {
512   GtkWidget *ha = NULL;
513   GdkPixbuf *pixbuf = NULL;
514   GtkWidget *image = NULL;
515   gchar     *str = NULL;
516
517   /* fprintf(stderr, "create_hildon_actor %s\n", actor->name);*/
518   ha = hildon_animation_actor_new();
519   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
520                         desktop_plugin->priv->theme, actor->filename);
521   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
522                                              actor->width, 
523                                              actor->height, 
524                                              NULL);
525   if (str)
526       g_free(str);
527   if (pixbuf){
528       image = gtk_image_new_from_pixbuf (pixbuf);
529       g_object_unref(G_OBJECT(pixbuf));
530   }
531   if (image){
532     g_signal_connect(G_OBJECT(image), "expose_event",
533                            G_CALLBACK(expose_event), pixbuf);
534     gtk_container_add (GTK_CONTAINER (ha), image);
535   }  
536   actor_set_position_full(ha, actor->x, actor->y, actor->z);
537   hildon_animation_actor_set_show (HILDON_ANIMATION_ACTOR(ha), actor->visible);
538   hildon_animation_actor_set_scale(HILDON_ANIMATION_ACTOR(ha), (double)actor->scale/100, (double)actor->scale/100);
539   realize(ha);
540   gtk_widget_show_all(ha);
541
542   /* TO DO check it */
543   /*  gdk_flush (); */
544
545   //g_object_set_data(G_OBJECT(ha), "name", name);
546   //g_object_set_data(G_OBJECT(ha), "filename", filename);
547   g_object_set_data(G_OBJECT(ha), "image", image);
548   /*
549   g_object_set_data(G_OBJECT(ha), "x", x);
550   g_object_set_data(G_OBJECT(ha), "y", y);
551   g_object_set_data(G_OBJECT(ha), "z", z);
552   g_object_set_data(G_OBJECT(ha), "width", width);
553   g_object_set_data(G_OBJECT(ha), "height", height);
554   g_object_set_data(G_OBJECT(ha), "scale", scale);
555   g_object_set_data(G_OBJECT(ha), "visible", visible);
556   g_object_set_data(G_OBJECT(ha), "opacity", opacity);
557   g_object_set_data(G_OBJECT(ha), "func", pfunc);
558   */
559   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
560   actor->widget = ha;
561 }
562
563
564 void
565 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
566 {
567     GtkWidget *image = NULL;
568     GdkPixbuf *pixbuf = NULL;
569     gchar     *str = NULL;
570
571     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
572                             desktop_plugin->priv->theme, actor->filename);
573  
574     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
575                                                actor->width, 
576                                                actor->height, 
577                                                NULL);
578     if(str)
579         g_free(str);
580     if (pixbuf){
581         image = gtk_image_new_from_pixbuf (pixbuf);
582         g_object_unref(G_OBJECT(pixbuf));
583     }
584     if (image){ 
585         g_signal_connect(G_OBJECT(image), "expose_event",
586                                        G_CALLBACK(expose_event), pixbuf);
587         if (g_object_get_data(G_OBJECT(actor->widget), "image")){
588             gtk_container_remove(GTK_CONTAINER(actor->widget), g_object_get_data(G_OBJECT(actor->widget), "image"));  
589         }
590         g_object_set_data(G_OBJECT(actor->widget), "image", image);
591         gtk_container_add (GTK_CONTAINER (actor->widget), image);
592         realize(actor->widget);
593         gtk_widget_show_all(actor->widget);
594         /* TO DO check it */
595        /*  gdk_flush (); */
596
597
598     }
599
600 }
601
602
603 void 
604 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
605 {
606     gint daytime;
607     gchar *newfile;
608     if (!actor) return;
609     daytime = get_daytime();
610     newfile = g_strdup_printf("%s%d.png", actor->name, daytime); 
611     if (actor->filename)
612             g_free(actor->filename);
613     actor->filename = newfile;
614     change_hildon_actor(actor, desktop_plugin);
615 }
616
617
618
619 /*
620 static gint 
621 get_time(gint t){
622     // уравнение изменения времени
623     return t*1.1;
624 }
625 */
626 static void 
627 destroy_scene(void){
628     GSList * tmp = scene.actors;
629     Actor *actor;
630     while (tmp != NULL){
631         actor = tmp->data;
632         if (actor){
633             if (actor->filename)
634                 g_free(actor->filename);
635             if (actor->name)
636                 g_free(actor->name);
637             gtk_widget_destroy(actor->widget);
638             //actor->widget = NULL;
639             g_free(actor);
640         }
641         tmp = g_slist_next(tmp);
642     }
643     g_slist_free(tmp);
644
645 }
646
647 void
648 reload_scene(AWallpaperPlugin *desktop_plugin)
649 {
650     fprintf(stderr,"Reload scene %s\n", desktop_plugin->priv->theme);  
651     destroy_scene();
652     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
653         init_scene(desktop_plugin);
654     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
655         init_scene1(desktop_plugin);
656 }
657
658
659 /* Init Modern Scene */
660 static void
661 init_scene(AWallpaperPlugin *desktop_plugin)
662 {
663   Actor *actor;
664
665   /* fprintf(stderr, "init scene \n");*/
666   scene.daytime = get_daytime();
667   scene.actors = NULL;
668
669   actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 
670                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
671   change_static_actor(actor, desktop_plugin);
672   scene.actors = g_slist_append(scene.actors, actor);
673
674   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
675                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
676   actor->time_start_animation = time(NULL);
677   actor->duration_animation = G_MAXINT;
678   change_sun(actor, desktop_plugin);
679   scene.actors = g_slist_append(scene.actors, actor);
680
681   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
682                       TRUE, 100, 255, NULL, NULL);
683   scene.actors = g_slist_append(scene.actors, actor);
684
685
686   actor = init_object(desktop_plugin, "town", "town0.png", 0, 0, 7, 800, 480, 
687                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
688   change_static_actor(actor, desktop_plugin);
689   scene.actors = g_slist_append(scene.actors, actor);
690
691
692   
693   actor = init_object(desktop_plugin, "tram", "tram.png", -300, 225, 8, 350, 210, 
694                       FALSE, 100, 255, (gpointer)&change_tram, NULL);
695   actor->time_start_animation = time(NULL) + 10;
696   actor->duration_animation = 60;
697   scene.actors = g_slist_append(scene.actors, actor);
698
699   actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 9, 800, 79,
700                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
701   change_static_actor(actor, desktop_plugin);
702   scene.actors = g_slist_append(scene.actors, actor);
703   
704   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 20, 10, 60, 60, 
705                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
706   change_moon(actor, desktop_plugin);
707   scene.actors = g_slist_append(scene.actors, actor);
708
709 #if 0    
710   anim = g_new0(Animation, 1);
711   anim->count = 1;
712   anim->actor = actor;
713   anim->func_change = &change_tram;
714   anim->func_time = NULL;
715   anim->timestart = time(NULL); 
716   anim->timeall = 10;
717   
718   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
719 #endif  
720  }
721
722 /* Init Berlin Scene */
723 static void
724 init_scene1(AWallpaperPlugin *desktop_plugin)
725 {
726   Actor *actor;
727   gint now = time(NULL);
728   
729
730   scene.daytime = get_daytime();
731   scene.actors = NULL;
732   scene.wind_orientation = -1;
733   scene.wind_angle = 0.3;
734   
735   actor = init_object(desktop_plugin, "sky", "sky.png", 0, 0, 5, 800, 480, 
736                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
737   change_static_actor(actor, desktop_plugin);
738   scene.actors = g_slist_append(scene.actors, actor);
739
740   
741   actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 6, 88, 88, 
742                       TRUE, 100, 255, (gpointer)&change_sun, &probability_sun);
743   actor->time_start_animation = time(NULL);
744   actor->duration_animation = G_MAXINT;
745   change_sun(actor, desktop_plugin);
746   scene.actors = g_slist_append(scene.actors, actor);
747
748
749   actor = init_object(desktop_plugin, "dot", "dot1.png", 0, 0, 11, 50, 50, 
750                       TRUE, 100, 255, NULL, NULL);
751   scene.actors = g_slist_append(scene.actors, actor);
752
753
754   actor = init_object(desktop_plugin, "moon", "moon1.png", 400, 15, 6, 60, 60, 
755                       FALSE, 100, 255, (gpointer)&change_moon, NULL);
756   change_moon(actor, desktop_plugin);
757   scene.actors = g_slist_append(scene.actors, actor);
758   
759   actor = init_object(desktop_plugin, "cloud1", "cloud2.png", 0, fast_rnd(300), 11, 188, 75, 
760                       FALSE, 100, 255, (gpointer)&change_cloud, NULL);
761   actor->time_start_animation = now + fast_rnd(3) * 60;
762   actor->duration_animation = 5*60;
763   scene.actors = g_slist_append(scene.actors, actor);
764
765   actor = init_object(desktop_plugin, "plane2", "plane3.png", 0, 45, 8, 160, 50, 
766                       FALSE, 100, 255, (gpointer)&change_plane2, NULL);
767   actor->time_start_animation = now + probability_plane();
768   actor->duration_animation = 60;
769   scene.actors = g_slist_append(scene.actors, actor);
770   
771   actor = init_object(desktop_plugin, "plane1", "tu154_small.png", 620, 233, 9, 300, 116, 
772                       FALSE, 100, 255, (gpointer)&change_plane1, NULL);
773   actor->time_start_animation = now + probability_plane();
774   actor->duration_animation = 30;
775   scene.actors = g_slist_append(scene.actors, actor);
776
777   actor = init_object(desktop_plugin, "town", "town.png", 0, 0, 10, 800, 480, 
778                       TRUE, 100, 255, (gpointer)&change_static_actor, NULL);
779   change_static_actor(actor, desktop_plugin);
780   scene.actors = g_slist_append(scene.actors, actor);
781
782
783 }
784
785
786 void 
787 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
788 {
789     gint y0 = 365;// - уровень горизонта
790     *x = (int)(azm * 800) - 64;
791     *y = (int)((1 - alt) * y0) - 64;
792     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
793 }
794 #if 0
795 static void 
796 change_actor(GtkWidget * actor)
797 {
798     char * name;
799     gint x, y, daytime, scale;
800     gdouble sc;
801     double alt, azm;
802
803     GtkWidget *image;
804     GdkPixbuf *pixbuf;
805
806     void (*pfunc)(gpointer, gpointer);
807
808     name = g_object_get_data(G_OBJECT(actor), "name");
809     fprintf(stderr, "change actor %s\n", name);
810     if (name == "sun"){
811         pfunc = g_object_get_data(G_OBJECT(actor), "func");
812         if (pfunc)
813             (*pfunc)(actor, g_strdup(name));
814         daytime = get_daytime();
815         if (daytime != TIME_NIGHT){
816             hildon_animation_actor_set_show(actor, 1);
817             get_sun_pos(&alt, &azm);
818             get_sun_screen_pos(alt, azm, &x, &y);
819             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
820         }
821     }
822     
823     if (name == "cloud1"){
824         x = g_object_get_data(G_OBJECT(actor), "x");
825         y = g_object_get_data(G_OBJECT(actor), "y");
826         scale = g_object_get_data(G_OBJECT(actor), "scale");
827
828         /* Start */
829         image = g_object_get_data(G_OBJECT(actor), "image");
830         
831         gtk_container_remove(actor, image);  
832         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
833                                              200, 
834                                              200, 
835                                              NULL);
836         if (pixbuf){
837               image = gtk_image_new_from_pixbuf (pixbuf);
838               g_object_unref(G_OBJECT(pixbuf));
839         }
840         g_signal_connect(G_OBJECT(image), "expose_event",
841                                    G_CALLBACK(expose_event), pixbuf);
842         gtk_container_add (GTK_CONTAINER (actor), image);
843         realize(actor);
844         gtk_widget_show_all(actor);
845         /* End*/
846
847             
848         x += 40;
849         y -= 20;
850         scale -= 10;
851         if (x > 500){
852             x = 400;
853             y = 150;
854             sc = 1;
855         }
856         sc = (double)scale / 100;
857         hildon_animation_actor_set_scale(actor, sc, sc);
858         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
859         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
860         g_object_set_data(G_OBJECT(actor), "x", x);
861         g_object_set_data(G_OBJECT(actor), "y", y);
862         g_object_set_data(G_OBJECT(actor), "scale", scale);
863     }
864
865 }
866 #endif
867 static gboolean
868 short_timeout (AWallpaperPlugin *desktop_plugin)
869 {
870       gint daytime = get_daytime();
871       GSList * tmp;
872       void (*pfunc)(gpointer, gpointer);
873       time_t now;
874       Actor *actor;
875       gboolean stop_flag = TRUE;
876
877     if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
878         desktop_plugin->priv->short_timer = 0;
879         return FALSE;
880     }
881
882     now = time(NULL);
883     //scene.daytime = daytime;
884     /* fprintf(stderr, "Short timer %d\n", now); */
885     tmp = scene.actors;
886     while (tmp != NULL){
887            actor = tmp->data;
888            if (now >= actor->time_start_animation  
889                && actor->time_start_animation > 0
890                /* && now - actor->time_start_animation <= actor->duration_animation*/){
891                 pfunc = actor->func_change;
892                 if (pfunc){ 
893                     (*pfunc)(actor, desktop_plugin);
894                     stop_flag = FALSE;
895                 }
896             }
897             tmp = g_slist_next(tmp);
898     }
899     if (stop_flag){
900          desktop_plugin->priv->short_timer = 0;
901          return FALSE;
902     }else
903          return TRUE; /* keep running this event */
904 }
905
906
907 static gboolean
908 long_timeout (AWallpaperPlugin *desktop_plugin)
909 {
910     gint daytime = get_daytime();
911     GSList * tmp;
912     void (*pfunc)(gpointer, gpointer);
913     time_t now;
914     Actor *actor;
915
916     /* TODO  remove timeout */
917     if (!desktop_plugin->priv->visible){
918         desktop_plugin->priv->short_timer = 0;
919         return FALSE;
920     }
921     //fprintf(stderr, "timer daytime=%d\n", daytime);
922     if (scene.daytime != daytime){
923         tmp = scene.actors;
924         while (tmp != NULL){
925             //change_actor(tmp->data);
926             pfunc =((Actor*)tmp->data)->func_change;
927             if (pfunc){
928                 (*pfunc)(tmp->data, desktop_plugin);
929             }
930             tmp = g_slist_next(tmp);
931         }
932     }
933    
934     now = time(NULL);
935     //fprintf(stderr, "Now  %d\n", now);
936     tmp = scene.actors;
937     while (tmp != NULL){
938         actor = tmp->data;
939         if (now >= actor->time_start_animation  
940             && actor->time_start_animation > 0
941             && desktop_plugin->priv->short_timer == 0){
942             actor->time_start_animation = now;
943             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
944             if (desktop_plugin->priv->rich_animation)
945                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
946         }
947         tmp = g_slist_next(tmp);
948     }
949    
950     scene.daytime = daytime;
951
952     return TRUE; /* keep running this event */
953 }
954
955 static void
956 desktop_plugin_visible_notify (GObject    *object,
957                                           GParamSpec *spec,
958                                           AWallpaperPlugin *desktop_plugin)
959 {
960     gboolean visible;
961     g_object_get (object, "is-on-current-desktop", &visible, NULL);
962     if (visible){
963         desktop_plugin->priv->visible = TRUE;
964         desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
965     }else
966         desktop_plugin->priv->visible = FALSE;
967    /*   fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
968 }
969
970 gboolean
971 rich_animation_press(GtkWidget *widget, GdkEvent *event,
972                                             gpointer user_data){
973     fprintf(stderr,"gggggggggggggggggggg\n");
974 }    
975 static void
976 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
977 {
978     GtkWidget *label;
979     GtkWidget *rich_animation;
980     fprintf(stderr, "!!!!!!!plugin init \n");
981     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
982     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
983     /* Load config */
984     read_config(priv);
985     priv->desktop_plugin = desktop_plugin;
986     /* Create rich animation event */
987     rich_animation = gtk_event_box_new();
988     if(rich_animation){
989         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
990         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
991         gtk_widget_set_size_request(rich_animation, 95, 30);
992        gtk_widget_show (rich_animation);
993         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
994     }
995 /*
996     label = gtk_label_new (""); 
997     gtk_widget_set_size_request(label, 95, 30);
998     gtk_widget_show (label);
999     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
1000 */
1001     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
1002     g_signal_connect (desktop_plugin, "show-settings",
1003                              G_CALLBACK (lw_settings), priv);
1004     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
1005                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
1006
1007     gtk_container_add (GTK_CONTAINER (desktop_plugin), rich_animation);
1008     init_applet_position();
1009
1010     /* start value for random */
1011     seed = time(NULL);
1012
1013     fprintf(stderr, "!!!theme = %s\n", priv->theme);
1014     if (!strcmp(priv->theme,"Modern"))
1015         init_scene(desktop_plugin);
1016     else if (!strcmp(priv->theme,"Berlin")) 
1017         init_scene1(desktop_plugin);
1018     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
1019     priv->short_timer = 0;
1020     /* TODO Move scene to priv */
1021     scene.timer_type = LONG_TIMER_TYPE;
1022     
1023     //sleep(2);
1024 }
1025
1026 static void
1027 lw_applet_finalize (GObject *object)
1028 {
1029      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
1030      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
1031
1032      if (priv->long_timer){
1033         g_source_remove(priv->long_timer);
1034         priv->long_timer = 0;
1035      }
1036      if (priv->short_timer){
1037         g_source_remove(priv->short_timer);
1038         priv->short_timer = 0;
1039      }
1040
1041      destroy_scene();
1042 }
1043
1044 static void
1045 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
1046
1047     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1048     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
1049
1050     /* gobject */
1051     gobject_class->destroy = (gpointer)lw_applet_finalize;
1052     widget_class->realize = lw_applet_realize;
1053     widget_class->expose_event = lw_applet_expose_event;
1054
1055     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
1056
1057 }
1058
1059 static void
1060 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}