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