123
[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
30 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
31 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
32 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
33 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
34
35 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
36 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
37                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
38                                                           Animation_WallpaperPrivate))
39
40 /* Position of plugin on desktop */
41 #define Xstartposition 700 
42 #define Ystartposition 425 
43
44 gint xapplet = 0, yapplet = 0;
45 GSList * objects_list = NULL;
46 MultiActor * ma1;
47 Scene scene;
48
49 static void
50 lw_applet_realize (GtkWidget *widget)
51 {
52       GdkScreen *screen;
53
54       screen = gtk_widget_get_screen (widget);
55       gtk_widget_set_colormap (widget,
56                                 gdk_screen_get_rgba_colormap (screen));
57       gtk_widget_set_app_paintable (widget,
58                                 TRUE);
59       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
60 }
61
62
63 static gboolean
64 lw_applet_expose_event(GtkWidget      *widget,
65                                         GdkEventExpose *event)
66 {
67   cairo_t *cr;
68
69   /* Create cairo context */
70   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
71   gdk_cairo_region (cr, event->region);
72   cairo_clip (cr);
73
74   /* Draw alpha background */
75   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
76   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
77   cairo_paint (cr);
78
79   /* Free context */
80   cairo_destroy (cr);
81
82   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
83                                                                                   event);
84 }
85
86 static gboolean
87 expose_event (GtkWidget *widget,GdkEventExpose *event,
88      gpointer data)
89 {
90     cairo_t *cr;
91     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
92         
93     cr = gdk_cairo_create(widget->window);
94     gdk_cairo_region(cr, event->region);
95     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
96     gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
97     cairo_paint(cr);
98     cairo_destroy(cr);
99     return TRUE;
100 }
101
102 static void
103 realize (GtkWidget *widget)
104 {
105     GdkScreen *screen;
106     screen = gtk_widget_get_screen (widget);
107     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
108 }
109
110 /* Set position of widget on desktop */
111 static void
112 init_applet_position(void)
113 {
114   GSList *position = NULL;
115   gchar *position_key;
116   gchar *modified_key;
117   gchar *modified;
118   GError *error = NULL;
119   GConfClient   *gconf_client = gconf_client_get_default ();
120   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
121   position = gconf_client_get_list (gconf_client,
122                                     position_key,
123                                     GCONF_VALUE_INT,
124                                     NULL);
125   if (position && position->data && position->next->data){
126         xapplet = GPOINTER_TO_INT (position->data);
127         yapplet = GPOINTER_TO_INT (position->next->data);
128   }else{
129         position = g_slist_prepend (g_slist_prepend (NULL,
130                                       GINT_TO_POINTER (Ystartposition)),
131                                       GINT_TO_POINTER (Xstartposition));
132         gconf_client_set_list (gconf_client,
133                                position_key,
134                                GCONF_VALUE_INT,
135                                position,
136                                &error);
137         xapplet = Xstartposition;
138         yapplet = Ystartposition;
139   }
140   g_free (position_key);
141   modified = g_strdup_printf ("%ld", 0);
142   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
143   gconf_client_set_string (gconf_client,
144                            modified_key,
145                            modified,
146                            &error);
147   gconf_client_clear_cache(gconf_client);
148   g_object_unref(gconf_client);
149 }
150
151 void
152 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
153 {
154 // fprintf(stderr, "actor_set_position_full\n");
155  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
156 }
157
158 static GtkWidget* 
159 init_object(gchar * name, gchar * filename, gint x, gint y, gint z, gint width, gint height, gboolean visible, gint scale, gint opacity, void (*pfunc)(GtkWidget*, gpointer))
160 {
161   GtkWidget *actor;
162   GdkPixbuf *pixbuf;
163   GtkWidget *image;
164
165   actor = hildon_animation_actor_new();
166   gchar str[256];
167   snprintf(str, 255, "/usr/share/livewp/theme/Modern/%s", filename);
168   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
169   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
170                                              width, 
171                                              height, 
172                                              NULL);
173   if (pixbuf){
174       image = gtk_image_new_from_pixbuf (pixbuf);
175       g_object_unref(G_OBJECT(pixbuf));
176   }
177   g_signal_connect(G_OBJECT(image), "expose_event",
178                            G_CALLBACK(expose_event), pixbuf);
179   gtk_container_add (GTK_CONTAINER (actor), image);
180
181   actor_set_position_full(actor, x, y, z);
182   hildon_animation_actor_set_show (actor, visible);
183   realize(actor);
184   gtk_widget_show_all(actor);
185   g_object_set_data(G_OBJECT(actor), "name", name);
186   g_object_set_data(G_OBJECT(actor), "filename", filename);
187   g_object_set_data(G_OBJECT(actor), "image", image);
188   g_object_set_data(G_OBJECT(actor), "x", x);
189   g_object_set_data(G_OBJECT(actor), "y", y);
190   g_object_set_data(G_OBJECT(actor), "z", z);
191   g_object_set_data(G_OBJECT(actor), "width", width);
192   g_object_set_data(G_OBJECT(actor), "height", height);
193   g_object_set_data(G_OBJECT(actor), "scale", scale);
194   g_object_set_data(G_OBJECT(actor), "visible", visible);
195   g_object_set_data(G_OBJECT(actor), "opacity", opacity);
196   g_object_set_data(G_OBJECT(actor), "func", pfunc);
197   
198   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), scene.window);
199 /*
200   a.widget = actor;
201   a.name = name;
202   a.x = x;
203   a.y = y;
204   a.z = z;
205   */
206   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
207   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
208   return actor;
209 }
210
211 void change_sun(GtkWidget * actor, gpointer data)
212 {
213     double alt, azm;
214     gint x, y, z, daytime;
215     //fprintf(stderr, "change sun \n");
216     if (actor){
217         daytime = get_daytime();
218         if (daytime != TIME_NIGHT){
219              hildon_animation_actor_set_show(actor, 1);
220              get_sun_pos(&alt, &azm);
221              get_sun_screen_pos(alt, azm, &x, &y);
222              actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
223              g_object_set_data(G_OBJECT(actor), "visible", 1);
224              g_object_set_data(G_OBJECT(actor), "x", x);
225              g_object_set_data(G_OBJECT(actor), "y", y);
226         }else {
227             hildon_animation_actor_set_show(actor, 0);
228             g_object_set_data(G_OBJECT(actor), "visible", 0);
229         } 
230     }
231 }
232 void change_tram(GtkWidget * actor, gpointer data)
233 {
234     double alt, azm;
235     gint x, y, z, scale, width, height, daytime;
236     //fprintf(stderr, "change sun \n");
237     if (actor){
238         x = g_object_get_data(G_OBJECT(actor), "x");
239         y = g_object_get_data(G_OBJECT(actor), "y");
240         scale = g_object_get_data(G_OBJECT(actor), "scale");
241         width = g_object_get_data(G_OBJECT(actor), "width");
242         height = g_object_get_data(G_OBJECT(actor), "height");
243         x = x + 5;
244         if (x > 480+width) x = -width;
245         scale = (x + 400) * 50 / 880 + 100;
246         y = y - height * (scale-100) / 100;
247         
248         //fprintf(stderr, "scale =%i y=%i\n", y,scale);
249         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
250         g_object_set_data(G_OBJECT(actor), "x", x);
251         //g_object_set_data(G_OBJECT(actor), "y", y);
252         hildon_animation_actor_set_scale(actor, (double)scale/100, (double)scale/100);
253         g_object_set_data(G_OBJECT(actor), "scale", scale);
254     }
255 }
256
257 void change_sky(GtkWidget * actor, gpointer data)
258 {
259     gint daytime, opacity;
260     //fprintf(stderr, "change sky \n");
261     if (actor){
262         daytime = get_daytime();
263         opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 10;
264         if (opacity < 0) opacity = 255;
265         hildon_animation_actor_set_show_full(actor, 1, opacity);
266         g_object_set_data(G_OBJECT(actor), "opacity", opacity);
267         switch (daytime){
268             case TIME_NIGHT:
269                 //fprintf(stderr, "change sky night\n");
270             break;
271             case TIME_DAY:
272                 //fprintf(stderr, "change sky day\n");
273             break;
274
275         }
276     }
277 }
278 void change_background(GtkWidget * actor, gpointer data)
279 {
280     gint opacity;
281     if (actor){
282         opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 1;
283         if (opacity < 0) opacity = 255;
284         hildon_animation_actor_set_show_full(actor, 1, opacity);
285         g_object_set_data(G_OBJECT(actor), "opacity", opacity);
286
287     }
288 }
289
290 static void
291 init_scene(GtkWidget *window)
292 {
293   GtkWidget *actor;
294   GdkPixbuf *pixbuf;
295   GtkWidget *image;
296   double alt, azm;
297   gint x, y;
298   GSList * list = NULL;
299
300   scene.window = window;
301   scene.daytime = get_daytime();
302   scene.dynamic_actors = NULL;
303   scene.static_actors = NULL;
304
305   get_sun_pos(&alt, &azm);
306   get_sun_screen_pos(alt, azm, &x, &y);
307   actor = init_object("sun", "sun.png", x, y, 11, 88, 88, 1, 100, 255, &change_sun);
308   //scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
309   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
310   //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
311
312   actor = init_object("sky", "sky.png", 0, 0, 5, 800, 480, 1, 100, 255, NULL);
313   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
314
315   actor = init_object("town", "town.png", 0, 480-374, 10, 800, 374, 1, 100, 255, NULL);
316   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
317   
318   actor = init_object("border", "border.png", 0, 480-79, 30, 800, 79, 1, 100, 255, NULL);
319   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
320   
321   actor = init_object("tram", "tram.png", -300, 191, 25, 350, 240, 1, 100, 255, NULL);
322   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
323   
324 }
325 static void
326 init_scene1(GtkWidget *window)
327 {
328   GtkWidget *actor;
329   GdkPixbuf *pixbuf;
330   GtkWidget *image;
331   double alt, azm;
332   gint x, y;
333   GSList * list = NULL;
334
335   scene.window = window;
336   scene.daytime = get_daytime();
337   scene.dynamic_actors = NULL;
338   scene.static_actors = NULL;
339
340   get_sun_pos(&alt, &azm);
341   get_sun_screen_pos(alt, azm, &x, &y);
342   actor = init_object("sun", "sun.png", x, y, 20, 88, 88, 1, 100, 255, &change_sun);
343   //scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
344   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
345   //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
346
347   actor = init_object("background", "sky_dark.png", 0, 0, 4, 800, 480, 1, 100, 255, NULL);
348   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
349
350   actor = init_object("sky", "sky.png", 0, 0, 5, 800, 480, 1, 100, 255, &change_sky);
351   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
352
353   actor = init_object("town", "town.png", 0, 0, 10, 800, 480, 1, 100, 255, NULL);
354   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
355   
356 /*
357   actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
358   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
359
360   actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
361   list = g_slist_append(list, G_OBJECT(actor));
362   actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
363   list = g_slist_append(list, G_OBJECT(actor));
364
365   ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
366   //objects_list = g_slist_append(objects_list, G_OBJECT(ma)); 
367   */
368
369 }
370
371
372 void 
373 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
374 {
375     gint y0 = 400;// - уровень горизонта
376     gint o_width = 128,
377          o_height = 128; 
378     *x = (int)(azm * 800) - 64;
379     *y = (int)((1 - alt) * y0) - 64;
380 }
381
382 void change_multiactor()
383 {
384     gboolean fl;
385     double scale;
386     gint x, y, z;
387     if (ma1->visible) fl = FALSE;
388     else fl = TRUE;
389     //multiactor_set_visible(ma1, fl);
390
391     scale = ma1->scale;
392     scale -= 0.1;
393     if (scale == 0) scale = 1;
394     //multiactor_set_scale(ma1, scale);
395
396     x = ma1->x + 10;
397     y = ma1->y + 10;
398     //multiactor_set_position(ma1, x, y, 0);
399
400 }
401
402 static void 
403 change_actor(GtkWidget * actor)
404 {
405     char * name;
406     gint x, y, daytime, scale;
407     gdouble sc;
408     double alt, azm;
409
410     GtkWidget *image;
411     GdkPixbuf *pixbuf;
412
413     void (*pfunc)(gpointer, gpointer);
414
415     name = g_object_get_data(G_OBJECT(actor), "name");
416     fprintf(stderr, "change actor %s\n", name);
417     if (name == "sun"){
418         pfunc = g_object_get_data(G_OBJECT(actor), "func");
419         if (pfunc)
420             (*pfunc)(actor, g_strdup(name));
421         daytime = get_daytime();
422         if (daytime != TIME_NIGHT){
423             hildon_animation_actor_set_show(actor, 1);
424             get_sun_pos(&alt, &azm);
425             get_sun_screen_pos(alt, azm, &x, &y);
426             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
427         }
428     }
429     
430     if (name == "cloud1"){
431         x = g_object_get_data(G_OBJECT(actor), "x");
432         y = g_object_get_data(G_OBJECT(actor), "y");
433         scale = g_object_get_data(G_OBJECT(actor), "scale");
434
435         /* Start */
436         image = g_object_get_data(G_OBJECT(actor), "image");
437         
438         gtk_container_remove(actor, image);  
439         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
440                                              200, 
441                                              200, 
442                                              NULL);
443         if (pixbuf){
444               image = gtk_image_new_from_pixbuf (pixbuf);
445               g_object_unref(G_OBJECT(pixbuf));
446         }
447         g_signal_connect(G_OBJECT(image), "expose_event",
448                                    G_CALLBACK(expose_event), pixbuf);
449         gtk_container_add (GTK_CONTAINER (actor), image);
450         realize(actor);
451         gtk_widget_show_all(actor);
452         /* End*/
453
454             
455         x += 40;
456         y -= 20;
457         scale -= 10;
458         if (x > 500){
459             x = 400;
460             y = 150;
461             sc = 1;
462         }
463         sc = (double)scale / 100;
464         hildon_animation_actor_set_scale(actor, sc, sc);
465         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
466         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
467         g_object_set_data(G_OBJECT(actor), "x", x);
468         g_object_set_data(G_OBJECT(actor), "y", y);
469         g_object_set_data(G_OBJECT(actor), "scale", scale);
470     }
471
472 }
473
474 static gboolean
475 plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
476 {
477   gint daytime = get_daytime();
478   GSList * tmp;
479   void (*pfunc)(gpointer, gpointer);
480   //fprintf(stderr, "on timeout\n");
481
482   if (scene.daytime == daytime){
483       /* Change dynamic actors */
484       tmp = scene.dynamic_actors;
485   }else {
486       /* Change static actors */
487       tmp = scene.static_actors;
488   } 
489   while (tmp != NULL){
490       //change_actor(tmp->data);
491       pfunc = g_object_get_data(G_OBJECT(tmp->data), "func");
492       if (pfunc){
493           (*pfunc)(tmp->data, NULL);
494       }
495       tmp = g_slist_next(tmp);
496   }
497
498   scene.daytime = daytime;
499
500   //double azm, alt;
501   //get_sun_pos(&alt, &azm);
502 /*
503   GSList * tmp = objects_list;
504   while (tmp != NULL){
505       //processing(tmp->data);
506       
507       str = g_object_get_data(G_OBJECT(tmp->data), "name");
508       fprintf(stderr, "object: %s\n", str);
509       
510       if (str == "sun"){
511         //get_sun_screen_pos(alt, azm, &x, &y);
512         //x = tmp->data
513         //actor_set_position_full(tmp->data, x, y, 20);
514         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
515         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
516         //fprintf(stderr, "x = %d y = %d\n", x, y);
517         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
518         while (child != NULL) {
519             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
520             child = child->next;
521         }
522
523          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
524          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
525              
526       }
527       
528       if (str == "town"){
529           //hildon_animation_actor_set_show(tmp->data, 0);
530       }
531       //a = tmp->data;
532       //fprintf(stderr, "--timeout %s\n", a->name);
533       tmp = g_slist_next(tmp);
534   }
535   */
536   return TRUE; /* keep running this event */
537 }
538
539 static void
540 desktop_plugin_visible_notify (GObject    *object,
541                                           GParamSpec *spec,
542                                           AWallpaperPlugin *desktop_plugin)
543 {
544       gboolean visible;
545       g_object_get (object, "is-on-current-desktop", &visible, NULL);
546       fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible);
547 }
548
549 static void
550 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
551 {
552   GtkWidget *label;
553   Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
554   desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
555   label = gtk_label_new (""); 
556   //desktop_plugin->priv->main_widget = label;
557   gtk_widget_set_size_request(label, 95, 30);
558   gtk_widget_show (label);
559   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
560   g_signal_connect (desktop_plugin, "show-settings",
561                              G_CALLBACK (lw_settings), NULL);
562   g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
563                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
564
565   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
566   init_applet_position();
567   init_scene(desktop_plugin);
568   priv->timer = g_timeout_add(50, plugin_on_timeout, desktop_plugin);
569 }
570
571 static void
572 lw_applet_finalize (GObject *object)
573 {
574      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
575      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
576
577      if (priv->timer){
578         g_source_remove(priv->timer);
579         priv->timer = NULL;
580      }
581 }
582
583 static void
584 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
585
586     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
587     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
588
589     /* gobject */
590     gobject_class->destroy = lw_applet_finalize;
591     widget_class->realize = lw_applet_realize;
592     widget_class->expose_event = lw_applet_expose_event;
593
594     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
595
596 }
597
598 static void
599 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}