refactoried of code
[livewp] / applet / src / livewp-actor.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-actor.h"
26
27 static void
28 realize (GtkWidget *widget)
29 {
30     GdkScreen *screen;
31     screen = gtk_widget_get_screen (widget);
32     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
33 }
34
35 static gboolean
36 expose_event (GtkWidget *widget,GdkEventExpose *event,
37      gpointer data)
38 {
39     cairo_t *cr;
40     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
41         
42     cr = gdk_cairo_create(widget->window);
43     if (cr){
44         gdk_cairo_region(cr, event->region);
45         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
46         gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
47         cairo_paint(cr);
48         cairo_destroy(cr);
49     }
50     return TRUE;
51 }
52
53 void
54 destroy_hildon_actor(Actor *actor)
55 {
56     //fprintf(stderr, "destroy_hildon_actor %s\n",actor->name);
57     gtk_widget_destroy(actor->widget);
58     actor->widget = NULL;
59 }
60
61 void
62 create_hildon_actor_text(Actor *actor, AWallpaperPlugin *desktop_plugin) 
63 {
64   GtkWidget *ha = NULL;
65   GtkWidget *label = NULL;
66
67   ha = hildon_animation_actor_new();
68   label = gtk_label_new(NULL);  
69
70   if (label){
71     //g_signal_connect(G_OBJECT(label), "expose_event", G_CALLBACK(expose_event), NULL);
72
73     gtk_container_add (GTK_CONTAINER (ha), label);
74   }  
75   realize(ha);
76   gtk_widget_show(label);
77   gtk_widget_show_all(ha);
78   
79   /* TO DO check it */
80   /*  gdk_flush (); */
81
82   //g_object_set_data(G_OBJECT(ha), "image", image);
83   actor->image = label;
84   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin->priv->window));
85   actor->widget = ha;
86   set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
87   set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
88   set_actor_visible(actor, actor->visible);
89 }
90
91 void
92 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
93 {
94   GtkWidget *ha = NULL;
95   GdkPixbuf *pixbuf = NULL;
96   GtkWidget *image = NULL;
97   gchar     *str = NULL;
98
99   ha = hildon_animation_actor_new();
100   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
101                         desktop_plugin->priv->theme, actor->filename);
102   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
103                                              actor->width, 
104                                              actor->height, 
105                                              NULL);
106   /* fprintf(stderr, "create_hildon_actor %s %s\n", actor->name, str); */
107   if (str)
108       g_free(str);
109   if (pixbuf){
110       image = gtk_image_new_from_pixbuf (pixbuf);
111       g_object_unref(G_OBJECT(pixbuf));
112   }
113   if (image){
114     g_signal_connect(G_OBJECT(image), "expose_event",
115                            G_CALLBACK(expose_event), pixbuf);
116     gtk_container_add (GTK_CONTAINER (ha), image);
117   }  
118   realize(ha);
119   gtk_widget_show_all(ha);
120   
121   /* TO DO check it */
122   /*  gdk_flush (); */
123
124   //g_object_set_data(G_OBJECT(ha), "image", image);
125   actor->image = image;
126   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin->priv->window));
127
128   actor->widget = ha;
129   set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
130   set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
131   set_actor_visible(actor, actor->visible);
132 }
133
134 void
135 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
136 {
137     GtkWidget *image = NULL;
138     GdkPixbuf *pixbuf = NULL;
139     gchar     *str = NULL;
140
141     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
142                             desktop_plugin->priv->theme, actor->filename);
143  
144     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
145                                                actor->width, 
146                                                actor->height, 
147                                                NULL);
148     if(str)
149         g_free(str);
150     if (pixbuf){
151         image = gtk_image_new_from_pixbuf (pixbuf);
152         g_object_unref(G_OBJECT(pixbuf));
153     }
154     if (image){ 
155         g_signal_connect(G_OBJECT(image), "expose_event",
156                                        G_CALLBACK(expose_event), pixbuf);
157         //if (g_object_get_data(G_OBJECT(actor->widget), "image")){
158         if (actor->image){
159             gtk_container_remove(GTK_CONTAINER(actor->widget), actor->image);  
160         }
161         //g_object_set_data(G_OBJECT(actor->widget), "image", image);
162         actor->image = image;
163         gtk_container_add (GTK_CONTAINER (actor->widget), image);
164         realize(actor->widget);
165         gtk_widget_show_all(actor->widget);
166         /* TO DO check it */
167        /*  gdk_flush (); */
168
169
170     }
171 }
172
173 Actor* 
174 init_object(AWallpaperPlugin *desktop_plugin, 
175             gchar * name, 
176             gchar * filename, 
177             gint x, 
178             gint y, 
179             gint z, 
180             gint width, 
181             gint height, 
182             gboolean visible, 
183             gboolean load_image,
184             gint scale, 
185             gint opacity, 
186             void (*pfunc_change)(Actor*),
187             void (*pfunc_probability)(Actor*),
188             GPtrArray *child
189            )
190 {
191     Actor *actor = NULL;
192     actor = g_new0(Actor, 1);
193     actor->x = x;
194     actor->y = y;
195     actor->z = z;
196     actor->width = width;
197     actor->height = height;
198     actor->visible = visible;
199     actor->scale = scale;
200     actor->opacity = opacity;
201     actor->filename = g_strdup(filename);
202     actor->name = g_strdup(name);
203     actor->func_change = (gpointer)pfunc_change; 
204     actor->func_probability = (gpointer)pfunc_probability;
205     actor->child = child;
206     if (load_image)
207         create_hildon_actor(actor, desktop_plugin);
208     else 
209          actor->widget = NULL;
210     actor->time_start_animation = 0;
211     actor->duration_animation = 0;
212     return actor;
213 }
214
215 void 
216 destroy_actor(Actor *actor)
217 {
218     if (actor){
219         if (actor->child){
220             g_ptr_array_free(actor->child, TRUE);
221         }
222         if (actor->filename)
223             g_free(actor->filename);
224         if (actor->name)
225             g_free(actor->name);
226         gtk_widget_destroy(actor->widget);
227         //actor->widget = NULL;
228         g_free(actor);
229     }
230 }
231 static gint 
232 path_line(gint x0, gint x1, double t)
233 {
234     // уравниение прямой
235     return ((x1 - x0) * t + x0);
236 }
237 void
238 set_actor_scale(Actor *actor, double scalex, double scaley)
239 {
240     hildon_animation_actor_set_scale(
241             HILDON_ANIMATION_ACTOR(actor->widget), 
242             scalex, 
243             scaley
244     );
245
246 }
247 void
248 set_actor_rotation(Actor *actor, gint axis, double degrees, gint x, gint y, gint z)
249 {
250     hildon_animation_actor_set_rotation(
251             HILDON_ANIMATION_ACTOR(actor->widget),
252             axis,
253             degrees,
254             x,
255             y,
256             z
257     );
258 }
259 void 
260 set_actor_visible(Actor *actor, gboolean visible)
261 {
262     hildon_animation_actor_set_show(HILDON_ANIMATION_ACTOR(actor->widget), visible);
263 }
264
265 void
266 set_actor_position(Actor *actor, gint x, gint y, gint z, AWallpaperPlugin *desktop_plugin)
267 {
268     hildon_animation_actor_set_position_full(HILDON_ANIMATION_ACTOR (actor->widget), 
269                                              x-desktop_plugin->priv->xapplet, 
270                                              y-desktop_plugin->priv->yapplet, 
271                                              z);
272 }
273
274 int get_notify_count(gchar *notify_type)
275 {
276     sqlite3 *db = NULL;
277     sqlite3_stmt *res = NULL;
278     gint rc = 0, result = 0;
279     gchar sql[1024];
280
281     rc = sqlite3_open("/home/user/.config/hildon-desktop/notifications.db", &db);
282     if (rc){
283         fprintf(stderr, "error open db %d %s\n", rc, sqlite3_errmsg(db));
284     }else {
285         snprintf(sql, sizeof(sql)-1, "select count(id) from notifications where icon_name='%s'", notify_type);
286         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
287         if (rc != SQLITE_OK){
288             fprintf(stderr, "error prepare %d %s\n", rc, sql);
289         }
290         if (sqlite3_step(res) != SQLITE_ROW){
291             fprintf(stderr, "not sqlite_row\n");
292         }
293         result = sqlite3_column_int(res, 0);
294         //fprintf(stderr, "count missing calls = %d\n", call_count);
295         sqlite3_finalize(res);
296
297         sqlite3_close(db);
298     }
299     return result;
300 }
301 gchar * read_notification()
302 {
303     gchar *message = "";
304     gint count = 0;
305     
306     fprintf(stderr, "read notification \n");
307     count = get_notify_count("general_missed");
308     if (count > 0){
309         message = g_strdup_printf("%s: %d", _("Missed calls"), count);
310     }
311     count = get_notify_count("general_sms");
312     if (count > 0){
313         if (message){
314             message = g_strdup_printf("%s \n%s: %d", message, _("Missed sms"), count);
315         }else {
316             message = g_strdup_printf("%s: %d", _("Missed sms"), count);
317         }
318     }
319     count = get_notify_count("general_chat");
320     if (count > 0){
321         if (message){
322             message = g_strdup_printf("%s \n%s: %d", message, _("Missed chat"), count);
323         }else {
324             message = g_strdup_printf("%s: %d", _("Missed chat"), count);
325         }
326     }
327     count = get_notify_count("qgn_list_messagin");
328     if (count > 0){
329         if (message){
330             message = g_strdup_printf("%s \n%s: %d", message, _("Missed mail"), count);
331         }else {
332             message = g_strdup_printf("%s: %d", _("Missed mail"), count);
333         }
334     }
335     fprintf(stderr, "notify=%s\n", message);
336     return message;
337 }
338
339 void
340 change_obj(Actor *actor, AWallpaperPlugin *desktop_plugin)
341 {
342     char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
343     //char * accel_filename = "/home/tanya/coord";
344
345     FILE *fd = NULL;
346     int rs, ax, ay, az, dx, dy;
347     fd = fopen(accel_filename, "r");
348     if (fd == NULL){
349         fprintf(stderr, "cannot open file\n");
350         return;
351     }
352     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
353     fclose(fd);
354     if (rs != 3){
355         fprintf(stderr, "cannot read information from file\n");
356         return;
357     }
358
359     fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
360     dx = -ax / 100;
361     dy = -ay / 100;
362
363     actor->x = actor->x + dx;
364     actor->y = actor->y + dy;
365
366     if (actor->x > 800) actor->x = 0;
367     if (actor->x < 0) actor->x = 800;
368
369     if (actor->y > 480) actor->y = 0;
370     if (actor->y < 0) actor->y = 480;
371
372     set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
373
374
375 }
376
377 void 
378 change_billboard(Actor * actor, AWallpaperPlugin *desktop_plugin)
379 {
380     gint count = 0;
381     Actor *a = NULL;
382      
383     //fprintf(stderr, "change_billboard\n");   
384     
385     if (desktop_plugin->priv->scene->notification < time(NULL)){
386         count = get_notify_count("general_missed");
387         a = g_ptr_array_index(actor->child, 0);
388         if (count > 0){
389             set_actor_visible(a, TRUE);            
390         }else {
391             set_actor_visible(a, FALSE);
392         }
393         count = get_notify_count("general_sms");
394         a = g_ptr_array_index(actor->child, 3);
395         if (count > 0){
396             set_actor_visible(a, TRUE);            
397         }else {
398             set_actor_visible(a, FALSE);
399         }
400         count = get_notify_count("general_chat");
401         a = g_ptr_array_index(actor->child, 1);
402         if (count > 0){
403             set_actor_visible(a, TRUE);            
404         }else {
405             set_actor_visible(a, FALSE);
406         }
407         count = get_notify_count("qgn_list_messagin");
408         a = g_ptr_array_index(actor->child, 2);
409         if (count > 0){
410             set_actor_visible(a, TRUE);            
411         }else {
412             set_actor_visible(a, FALSE);
413         }
414
415         desktop_plugin->priv->scene->notification = FALSE;
416     }
417     actor->time_start_animation = time(NULL) + 20;    
418 }
419
420 #if 0
421 void 
422 change_billboard1(Actor * actor, AWallpaperPlugin *desktop_plugin)
423 {
424     GtkWidget *label;
425     sqlite3 *db = NULL;
426     sqlite3_stmt *res = NULL;
427     gchar *errMsg = NULL, *message;
428     gchar sql[1024];
429     gint call_count=0, sms_count=0, rc=0;
430     GtkListStore *list = NULL;
431     PangoFontDescription *pfd = NULL;
432     
433     rc = sqlite3_open("/home/user/.rtcom-eventlogger/el.db", &db);
434     if (rc){
435         fprintf(stderr, "error open db %d %s\n", rc, sqlite3_errmsg(db));
436     }else {
437         snprintf(sql, sizeof(sql)-1, "select count(id) from Events where event_type_id=%d", 3);
438
439         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
440         if (rc != SQLITE_OK){
441             fprintf(stderr, "error prepare %d %s\n", rc, sql);
442         }
443         if (sqlite3_step(res) != SQLITE_ROW){
444             fprintf(stderr, "not sqlite_row\n");
445         }
446         call_count = sqlite3_column_int(res, 0);
447         //fprintf(stderr, "count missing calls = %d\n", call_count);
448         sqlite3_finalize(res);
449
450         snprintf(sql, sizeof(sql)-1, "select count(id) from Events where event_type_id=%d and is_read=%d", 7, 0);
451         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
452         if (rc != SQLITE_OK){
453             fprintf(stderr, "error prepare %d %s\n", rc, sql);
454         }
455         if (sqlite3_step(res) != SQLITE_ROW){
456             fprintf(stderr, "not sqlite_row\n");
457         }
458         sms_count = sqlite3_column_int(res, 0);
459         //fprintf(stderr, "count sms = %d\n", sms_count);
460         sqlite3_finalize(res);
461
462
463         sqlite3_close(db);
464     }
465     label = actor->image;
466     message = g_markup_printf_escaped("<span bgcolor=\"%s\" foreground=\"%s\">Missed calls: %d Unread sms: %d</span>", "#FFFFFF", "#000000", call_count, sms_count);
467     gtk_label_set_markup(GTK_LABEL(label), message);
468     g_free(message);
469     pfd = pango_font_description_from_string("Sans 14");
470     gtk_widget_modify_font(GTK_WIDGET(label), NULL);
471     gtk_widget_modify_font(GTK_WIDGET(label), pfd);
472     pango_font_description_free(pfd);
473     actor->time_start_animation = time(NULL) + 20;    
474 }
475 #endif
476
477 void 
478 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
479 {
480     gint phase;
481     char *newfile;
482     gint x0 = 150,
483          x1 = 650, 
484          x, y;
485     struct timeval tvb;     
486     suseconds_t ms;
487     long sec;
488     double t;
489 #if 0
490     gint y0, y1, x2, y2;
491     double a, b, c;
492     a = (double)(y2 - (double)(x2*(y1-y0) + x1*y0 - x0*y1)/(x1-x0))/(x2*(x2-x0-x1)+x0*x1);
493     b = (double)(y1-y0)/(x1-x0) - (double)a*(x0+x1);
494     c = (double)(x1*y0 - x0*y1)/(x1-x0) + (double)a*x0*x1;
495     fprintf(stderr, "a=%f, b=%f, c=%f\n", a, b, c);
496 #endif
497     gettimeofday(&tvb, NULL);
498     
499     ms = tvb.tv_usec;
500     sec = tvb.tv_sec;
501
502     if (actor){
503         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
504             if (!actor->visible){
505                 actor->visible = TRUE;
506                 phase = get_moon_phase();
507                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
508                 if (actor->filename)
509                     g_free(actor->filename);
510                 actor->filename = newfile;
511                 actor->time_start_animation = sec - fast_rnd(60 * 60);
512                 actor->duration_animation = 1 * 60 * 60;
513                 create_hildon_actor(actor, desktop_plugin);
514
515             }
516             t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
517             if (t <= 1)
518                 x = path_line(x0, x1, t);
519             else 
520                 x = path_line(x1, x0, t-1);
521             y = 0.001920*x*x - 1.536*x + 337.2;
522             //y = a*x*x + b*x + c;
523
524             set_actor_position(actor, x, y, actor->z, desktop_plugin);
525
526             if (t>=2){
527                 actor->time_start_animation = sec;
528             }
529
530          }else if (actor->visible){
531             actor->visible = FALSE;
532             fprintf(stderr, "destroy moon \n");
533             destroy_hildon_actor(actor);
534             actor->time_start_animation = 0;
535         } 
536     }
537     
538 }
539
540 void 
541 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
542 {
543     double alt, azm;
544     gint x, y;
545
546     //fprintf(stderr, "change sun\n");
547     if (actor){
548         if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
549             if (!actor->visible){
550                 actor->visible = TRUE;
551                 create_hildon_actor(actor, desktop_plugin);
552             }
553             get_sun_pos(&alt, &azm);
554             get_sun_screen_pos(alt, azm, &x, &y);
555             actor->x = x;
556             actor->y = y;
557             set_actor_position(actor, x, y, actor->z, desktop_plugin);
558             actor->time_start_animation = time(NULL) + 60;
559          }else if (actor->visible){
560             actor->visible = FALSE;
561             destroy_hildon_actor(actor);
562             actor->time_start_animation = 0;
563         } 
564     }
565     
566 }
567
568 void 
569 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
570 {
571     gint x0 = -300, y0 = 225, scale0 = 100,
572          x1 = 800, y1 = 162, scale1 = 130, 
573          x, y, scale;
574     struct timeval tvb;     
575     suseconds_t ms;
576     long sec;
577     double t;
578
579     //fprintf(stderr, "change tram\n");
580     gettimeofday(&tvb, NULL);
581     
582     ms = tvb.tv_usec;
583     sec = tvb.tv_sec;
584     
585     if (!actor->visible){
586         actor->visible = TRUE;
587         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
588             if (actor->filename)
589                 g_free(actor->filename);
590             actor->filename = g_strdup("tram_dark.png");
591         } else{
592             if (actor->filename)
593                 g_free(actor->filename);
594             actor->filename = g_strdup("tram.png");
595         }
596         create_hildon_actor(actor, desktop_plugin);
597     }
598     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
599     x = path_line(x0, x1, t);
600     y = path_line(y0, y1, t);
601     scale = path_line(scale0, scale1, t);
602     set_actor_position(actor, x, y, actor->z, desktop_plugin);
603     set_actor_scale(actor, (double)scale/100, (double)scale/100);
604     if (t >= 1){
605         /* stop animation */
606         actor->visible = FALSE;
607         destroy_hildon_actor(actor);
608         actor->time_start_animation = sec + fast_rnd(60);
609     }
610 }
611
612 void
613 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
614 {
615     gint x0 = 620, y0 = 233,
616          x1 = 79, y1 = -146, 
617          x, y;
618     struct timeval tvb;     
619     suseconds_t ms;
620     long sec;
621     double t;
622
623     gettimeofday(&tvb, NULL);
624     
625     ms = tvb.tv_usec;
626     sec = tvb.tv_sec;
627 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
628    
629     if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
630         if (actor->time_start_animation == 0){
631             actor->time_start_animation = sec + fast_rnd(180);
632             return;
633         }
634     }
635     if (!actor->visible){
636         actor->visible = TRUE;
637         create_hildon_actor(actor, desktop_plugin);
638     }
639     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
640     x = path_line(x0, x1, t);
641     y = path_line(y0, y1, t);
642     //scale = path_line(scale0, scale1, t);
643     set_actor_position(actor, x, y, actor->z, desktop_plugin);
644     if (t >= 1){
645         /* stop animation */
646         actor->visible = FALSE;
647         destroy_hildon_actor(actor);
648         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT) 
649             actor->time_start_animation = 0;
650         else 
651             actor->time_start_animation = sec + fast_rnd(180);
652     }
653
654 }
655
656 void
657 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
658 {
659     gint x0 = -actor->width, y0 = 45,
660          x1 = 800, y1 = 20, 
661          x, y;
662     struct timeval tvb;     
663     suseconds_t ms;
664     long sec;
665     double t;
666
667     gettimeofday(&tvb, NULL);
668     
669     ms = tvb.tv_usec;
670     sec = tvb.tv_sec;
671 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
672     if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
673         if (actor->time_start_animation == 0){
674             actor->time_start_animation = sec + fast_rnd(180);
675             return;
676         }
677     }
678     if (!actor->visible){
679         actor->visible = TRUE;
680         create_hildon_actor(actor, desktop_plugin);
681     }
682
683     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
684     x = path_line(x0, x1, t);
685     y = path_line(y0, y1, t);
686     //scale = path_line(scale0, scale1, t);
687     set_actor_position(actor, x, y, actor->z, desktop_plugin);
688     if (t >= 1){
689         /* stop animation */
690         actor->visible = FALSE;
691         destroy_hildon_actor(actor);
692         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT) 
693             actor->time_start_animation = 0;
694         else 
695             actor->time_start_animation = sec + fast_rnd(180);
696     }
697
698 }
699
700 void
701 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
702 {
703     gint x0, y0 = 300, scale0 = 100,
704          x1, y1 = -actor->height, scale1 = 150, 
705          x, y, scale;
706     struct timeval tvb;     
707     suseconds_t ms;
708     long sec;
709     double t;
710     gchar *newfile;
711
712     //fprintf(stderr, "change cloud\n");
713     gettimeofday(&tvb, NULL);
714     
715     ms = tvb.tv_usec;
716     sec = tvb.tv_sec;
717    
718     if (!actor->visible){
719         actor->visible = TRUE;
720         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
721             newfile = g_strdup_printf("%s_dark.png", actor->name);
722         }else{
723             newfile = g_strdup_printf("%s.png", actor->name);
724         } 
725         if (actor->filename)
726             g_free(actor->filename);
727         actor->filename = newfile;
728          
729         create_hildon_actor(actor, desktop_plugin);
730     }
731     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
732     
733     if (desktop_plugin->priv->scene->wind_orientation == 1){
734         x0 = -actor->width;
735         x1 = 800;
736     }
737     else {
738         x0 = 800;
739         x1 = -actor->width;
740     }
741
742     x = path_line(x0, x1, t);    
743     y = -desktop_plugin->priv->scene->wind_angle * (x - x0) + actor->y;
744     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
745
746     set_actor_position(actor, x, y, actor->z, desktop_plugin);
747     set_actor_scale(actor, (double)scale/100, (double)scale/100);
748     if ((y < y1 || y > y0) || t >= 1){
749         /* stop animation */
750         actor->visible = FALSE;
751         destroy_hildon_actor(actor);
752         actor->time_start_animation = sec + fast_rnd(300);
753         actor->y = fast_rnd(300);
754     }
755
756 }
757
758 void
759 change_wind(Actor *actor, AWallpaperPlugin *desktop_plugin)
760 {
761     desktop_plugin->priv->scene->wind_orientation = fast_rnd(2);
762     if (desktop_plugin->priv->scene->wind_orientation == 0) desktop_plugin->priv->scene->wind_orientation = -1;
763     desktop_plugin->priv->scene->wind_angle = (double)(fast_rnd(200) - 100) / 100;
764     actor->time_start_animation = time(NULL) + (fast_rnd(10) + 10) * 60;
765     //fprintf(stderr, "change wind orient = %d angle = %f after = %d\n", scene.wind_orientation, scene.wind_angle, actor->time_start_animation-time(NULL));
766 }
767
768 void 
769 change_window1(Actor * actor, AWallpaperPlugin *desktop_plugin)
770 {
771     gint now = time(NULL);
772     if (desktop_plugin->priv->scene->daytime == TIME_DAY){
773         if (actor->widget){
774             actor->visible = FALSE;
775             destroy_hildon_actor(actor);
776         }
777         actor->time_start_animation = 0;
778         return;
779     }else {
780         if (!actor->widget)
781             create_hildon_actor(actor, desktop_plugin);
782         if (actor->time_start_animation == 0){
783             actor->time_start_animation = now + fast_rnd(30);
784             return;
785         }
786     }
787
788     if (!actor->visible)
789         actor->visible = TRUE;
790     else 
791         actor->visible = FALSE;
792     set_actor_visible(actor, actor->visible);
793     actor->time_start_animation = now + fast_rnd(60) + 10;
794
795 }
796
797 void 
798 change_signal(Actor * actor, AWallpaperPlugin *desktop_plugin)
799 {
800     gint now = time(NULL);
801     Actor *a;
802     a = g_ptr_array_index(actor->child, 0);
803     if (a->visible)
804         a->visible = FALSE;
805     else 
806         a->visible = TRUE;
807     set_actor_visible(a, a->visible);
808     
809     a = g_ptr_array_index(actor->child, 1);
810     if (a->visible)
811         a->visible = FALSE;
812     else 
813         a->visible = TRUE;
814     set_actor_visible(a, a->visible);
815
816     actor->time_start_animation = now + fast_rnd(30) + 10;
817 }
818
819 void
820 change_tape(Actor *actor, AWallpaperPlugin *desktop_plugin)
821 {
822     gint x, y, i;
823     Actor *a;
824
825     if (!desktop_plugin->priv->rich_animation) return;
826     
827     char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
828     //char * accel_filename = "/home/tanya/coord";
829
830     FILE *fd = NULL;
831     int rs, ax, ay, az;
832     fd = fopen(accel_filename, "r");
833     if (fd == NULL){
834         //fprintf(stderr, "cannot open file\n");
835         fd = fopen("/home/user/coord", "r"); 
836     }
837     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
838     fclose(fd);
839     if (rs != 3){
840         fprintf(stderr, "cannot read information from file\n");
841         return;
842
843     }
844
845     //fprintf(stderr, "change obj %i %i %i angle rad=%f, deg=%f\n", ax, ay, az, atan2(ax, -ay), atan2(ax, -ay)*180/M_PI);
846     int ang = (int)floor(atan2(ay, ax)*180/M_PI);
847     if (ang < 0) ang = 360+ang;
848
849     if (!desktop_plugin->priv->rich_animation) return;
850
851     for (i=0; i<16; i++){
852         a = g_ptr_array_index(actor->child, i);
853         if (a->scale == 100) a->scale = ang;
854         if (abs(a->scale - ang) > 10){
855             if (a->scale > ang){
856                 if ((a->scale - ang) < (ang + (360-a->scale))) a->scale--;
857                 else a->scale++;
858             }
859             if (a->scale < ang) {
860                 if (ang - a->scale < (a->scale+(360-ang))) a->scale++;
861                 else a->scale--;
862             }
863             if (a->scale > 360) a->scale = 0;
864             if (a->scale < 0) a->scale = 360;
865         }
866     
867         x = a->x - (float)cos(a->scale*M_PI/180)*a->z;
868         y = a->y - (float)sin(a->scale*M_PI/180)*a->z;
869         //x = round(a->x - (float)cos(a->scale*M_PI/180)*a->z);
870         //y = round(a->y - (float)sin(a->scale*M_PI/180)*a->z);
871         //x = a->x - cos(angle)*a->z;
872         //y = a->y - sin(angle)*a->z;
873         if ((a->scale > 270 || a->scale < 90) && x < -a->width*cos(a->scale*M_PI/180)){ 
874             x = 800; 
875             y = fast_rnd(480);
876         } 
877         if ((a->scale > 90 && a->scale < 270) && x > 800 - a->width*cos(a->scale*M_PI/180)){
878             x = 0;
879             y = fast_rnd(480);
880         }
881         if (a->scale > 0 && a->scale < 180 && y < -a->width*sin(a->scale*M_PI/180)){
882             y = 480;
883             x = fast_rnd(800);
884         }
885         if (a->scale < 360 && a->scale > 180 && y > 480 - a->width*sin(a->scale*M_PI/180)){
886             y = 0;
887             x = fast_rnd(800);
888         }
889         //if (i ==0) fprintf(stderr, "x=%d y=%d ang=%d speed=%d\n", x, y, a->scale, a->z);
890         set_actor_rotation(a, HILDON_AA_Z_AXIS, a->scale, 0, 0, 0);
891         set_actor_position(a, x, y, a->z, desktop_plugin);
892         a->x = x;
893         a->y = y;
894     }
895     
896 }
897
898 void
899 change_layer(Actor * actor, AWallpaperPlugin *desktop_plugin)
900 {
901     gint y, speed1 = 8, speed2 = 16;
902     Actor *a;
903
904     if (!desktop_plugin->priv->rich_animation) return;
905
906     a = g_ptr_array_index(actor->child, 0);
907     y = a->y + speed1;
908     if (y > 480) y = -480;
909     set_actor_position(a, a->x, y, a->z, desktop_plugin);
910     a->y = y;
911     
912     a = g_ptr_array_index(actor->child, 1);
913     y = a->y + speed1;
914     if (y > 480) y = -480;
915     set_actor_position(a, a->x, y, a->z, desktop_plugin);
916     a->y = y;
917
918     a = g_ptr_array_index(actor->child, 2);
919     y = a->y + speed2;
920     if (y > 480) y = -480;
921     set_actor_position(a, a->x, y, a->z, desktop_plugin);
922     a->y = y;
923
924     a = g_ptr_array_index(actor->child, 3);
925     y = a->y + speed2;
926     if (y > 480) y = -480;
927     set_actor_position(a, a->x, y, a->z, desktop_plugin);
928     a->y = y;
929 }
930
931 void 
932 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
933 {
934     gchar *newfile;
935     newfile = g_strdup_printf("%s%d.png", actor->name, desktop_plugin->priv->scene->daytime); 
936     if (actor->filename)
937             g_free(actor->filename);
938     actor->filename = newfile;
939     change_hildon_actor(actor, desktop_plugin);
940 }
941
942 void 
943 change_static_actor_with_corner(Actor * actor, AWallpaperPlugin *desktop_plugin)
944 {
945     gchar buffer[2048];
946
947     if (desktop_plugin->priv->right_corner)
948         gtk_widget_destroy(desktop_plugin->priv->right_corner);
949     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/town%i_right_corner.png", \
950                                   THEME_PATH, desktop_plugin->priv->theme, desktop_plugin->priv->scene->daytime);
951     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
952     if (desktop_plugin->priv->right_corner){
953         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
954         gtk_widget_show (desktop_plugin->priv->right_corner);
955     }
956     change_static_actor(actor, desktop_plugin);
957
958 }