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