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