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