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