darking sky
[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, gint x, gint y, gint z, gint width, gint height, void (*pfunc)(GtkWidget*, gpointer))
160 {
161   GtkWidget *actor;
162   GdkPixbuf *pixbuf;
163   GtkWidget *image;
164   gdouble scale = 1.0;
165
166   actor = hildon_animation_actor_new();
167   gchar str[256];
168   snprintf(str, 255, "/usr/share/anwall/%s.png", name);
169   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
170   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
171                                              width, 
172                                              height, 
173                                              NULL);
174   if (pixbuf){
175       image = gtk_image_new_from_pixbuf (pixbuf);
176       g_object_unref(G_OBJECT(pixbuf));
177   }
178   g_signal_connect(G_OBJECT(image), "expose_event",
179                            G_CALLBACK(expose_event), pixbuf);
180   gtk_container_add (GTK_CONTAINER (actor), image);
181
182   actor_set_position_full(actor, x, y, z);
183   hildon_animation_actor_set_show (actor, 1);
184   realize(actor);
185   gtk_widget_show_all(actor);
186   g_object_set_data(G_OBJECT(actor), "name", name);
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), "scale", 10);
192   g_object_set_data(G_OBJECT(actor), "visible", 1);
193   g_object_set_data(G_OBJECT(actor), "opacity", 255);
194   g_object_set_data(G_OBJECT(actor), "func", pfunc);
195   
196   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), scene.window);
197 /*
198   a.widget = actor;
199   a.name = name;
200   a.x = x;
201   a.y = y;
202   a.z = z;
203   */
204   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
205   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
206   return actor;
207 }
208
209 void change_sun(GtkWidget * actor, gpointer data)
210 {
211     double alt, azm;
212     gint x, y, z, daytime;
213     fprintf(stderr, "change sun \n");
214     if (actor){
215         daytime = get_daytime();
216         if (daytime != TIME_NIGHT){
217              hildon_animation_actor_set_show(actor, 1);
218              get_sun_pos(&alt, &azm);
219              get_sun_screen_pos(alt, azm, &x, &y);
220              actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
221              g_object_set_data(G_OBJECT(actor), "visible", 1);
222              g_object_set_data(G_OBJECT(actor), "x", x);
223              g_object_set_data(G_OBJECT(actor), "y", y);
224         }else {
225             hildon_animation_actor_set_show(actor, 0);
226             g_object_set_data(G_OBJECT(actor), "visible", 0);
227         } 
228     }
229 }
230
231 void change_sky(GtkWidget * actor, gpointer data)
232 {
233     gint daytime, opacity;
234     fprintf(stderr, "change sky \n");
235     if (actor){
236         daytime = get_daytime();
237         opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 10;
238         if (opacity < 0) opacity = 255;
239         hildon_animation_actor_set_show_full(actor, 1, opacity);
240         g_object_set_data(G_OBJECT(actor), "opacity", opacity);
241         switch (daytime){
242             case TIME_NIGHT:
243                 fprintf(stderr, "change sky night\n");
244             break;
245             case TIME_DAY:
246                 fprintf(stderr, "change sky day\n");
247             break;
248
249         }
250     }
251 }
252
253 static void
254 init_scene(GtkWidget *window)
255 {
256   GtkWidget *actor;
257   GdkPixbuf *pixbuf;
258   GtkWidget *image;
259   double alt, azm;
260   gint x, y;
261   GSList * list = NULL;
262
263   scene.window = window;
264   scene.daytime = get_daytime();
265   scene.dynamic_actors = NULL;
266   scene.static_actors = NULL;
267
268   get_sun_pos(&alt, &azm);
269   get_sun_screen_pos(alt, azm, &x, &y);
270   actor = init_object("sun", x, y, 20, 88, 88, &change_sun);
271   //scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
272   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
273   //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
274
275   actor = init_object("sky_dark", 0, 0, 0, 800, 480, NULL);
276   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
277
278   actor = init_object("sky", 0, 0, 5, 800, 480, &change_sky);
279   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
280
281   actor = init_object("town", 0, 0, 10, 800, 480, NULL);
282   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
283 /*
284   actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
285   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
286
287   actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
288   list = g_slist_append(list, G_OBJECT(actor));
289   actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
290   list = g_slist_append(list, G_OBJECT(actor));
291
292   ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
293   //objects_list = g_slist_append(objects_list, G_OBJECT(ma)); 
294   */
295
296 }
297
298 void 
299 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
300 {
301     gint y0 = 400;// - уровень горизонта
302     gint o_width = 128,
303          o_height = 128; 
304     *x = (int)(azm * 800) - 64;
305     *y = (int)((1 - alt) * y0) - 64;
306 }
307
308 void change_multiactor()
309 {
310     gboolean fl;
311     double scale;
312     gint x, y, z;
313     if (ma1->visible) fl = FALSE;
314     else fl = TRUE;
315     //multiactor_set_visible(ma1, fl);
316
317     scale = ma1->scale;
318     scale -= 0.1;
319     if (scale == 0) scale = 1;
320     //multiactor_set_scale(ma1, scale);
321
322     x = ma1->x + 10;
323     y = ma1->y + 10;
324     //multiactor_set_position(ma1, x, y, 0);
325
326 }
327
328 static void 
329 change_actor(GtkWidget * actor)
330 {
331     char * name;
332     gint x, y, daytime, scale;
333     gdouble sc;
334     double alt, azm;
335
336     GtkWidget *image;
337     GdkPixbuf *pixbuf;
338
339     void (*pfunc)(gpointer, gpointer);
340
341     name = g_object_get_data(G_OBJECT(actor), "name");
342     fprintf(stderr, "change actor %s\n", name);
343     if (name == "sun"){
344         pfunc = g_object_get_data(G_OBJECT(actor), "func");
345         if (pfunc)
346             (*pfunc)(actor, g_strdup(name));
347         daytime = get_daytime();
348         if (daytime != TIME_NIGHT){
349             hildon_animation_actor_set_show(actor, 1);
350             get_sun_pos(&alt, &azm);
351             get_sun_screen_pos(alt, azm, &x, &y);
352             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
353         }
354     }
355     
356     if (name == "cloud1"){
357         x = g_object_get_data(G_OBJECT(actor), "x");
358         y = g_object_get_data(G_OBJECT(actor), "y");
359         scale = g_object_get_data(G_OBJECT(actor), "scale");
360
361         /* Start */
362         image = g_object_get_data(G_OBJECT(actor), "image");
363         
364         gtk_container_remove(actor, image);  
365         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/sun.png", 
366                                              200, 
367                                              200, 
368                                              NULL);
369         if (pixbuf){
370               image = gtk_image_new_from_pixbuf (pixbuf);
371               g_object_unref(G_OBJECT(pixbuf));
372         }
373         g_signal_connect(G_OBJECT(image), "expose_event",
374                                    G_CALLBACK(expose_event), pixbuf);
375         gtk_container_add (GTK_CONTAINER (actor), image);
376         realize(actor);
377         gtk_widget_show_all(actor);
378         /* End*/
379
380             
381         x += 40;
382         y -= 20;
383         scale -= 1;
384         if (x > 500){
385             x = 400;
386             y = 150;
387             sc = 1;
388         }
389         sc = (double)scale / 10;
390         hildon_animation_actor_set_scale(actor, sc, sc);
391         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
392         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
393         g_object_set_data(G_OBJECT(actor), "x", x);
394         g_object_set_data(G_OBJECT(actor), "y", y);
395         g_object_set_data(G_OBJECT(actor), "scale", scale);
396     }
397
398 }
399
400 static gboolean
401 plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
402 {
403   gint daytime = get_daytime();
404   GSList * tmp;
405   void (*pfunc)(gpointer, gpointer);
406   fprintf(stderr, "on timeout\n");
407
408   if (scene.daytime == daytime){
409       /* Change dynamic actors */
410       tmp = scene.dynamic_actors;
411   }else {
412       /* Change static actors */
413       tmp = scene.static_actors;
414   } 
415   while (tmp != NULL){
416       //change_actor(tmp->data);
417       pfunc = g_object_get_data(G_OBJECT(tmp->data), "func");
418       if (pfunc){
419           (*pfunc)(tmp->data, NULL);
420       }
421       tmp = g_slist_next(tmp);
422   }
423
424   scene.daytime = daytime;
425
426   //double azm, alt;
427   //get_sun_pos(&alt, &azm);
428 /*
429   GSList * tmp = objects_list;
430   while (tmp != NULL){
431       //processing(tmp->data);
432       
433       str = g_object_get_data(G_OBJECT(tmp->data), "name");
434       fprintf(stderr, "object: %s\n", str);
435       
436       if (str == "sun"){
437         //get_sun_screen_pos(alt, azm, &x, &y);
438         //x = tmp->data
439         //actor_set_position_full(tmp->data, x, y, 20);
440         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
441         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
442         //fprintf(stderr, "x = %d y = %d\n", x, y);
443         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
444         while (child != NULL) {
445             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
446             child = child->next;
447         }
448
449          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
450          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
451              
452       }
453       
454       if (str == "town"){
455           //hildon_animation_actor_set_show(tmp->data, 0);
456       }
457       //a = tmp->data;
458       //fprintf(stderr, "--timeout %s\n", a->name);
459       tmp = g_slist_next(tmp);
460   }
461   */
462   return TRUE; /* keep running this event */
463 }
464
465 static void
466 desktop_plugin_visible_notify (GObject    *object,
467                                           GParamSpec *spec,
468                                           AWallpaperPlugin *desktop_plugin)
469 {
470       gboolean visible;
471       g_object_get (object, "is-on-current-desktop", &visible, NULL);
472       fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible);
473 }
474
475 static void
476 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
477 {
478   GtkWidget *label;
479 /* This code doesn't work */
480 //  fprintf(stderr,"ddddddddddddd\n");
481 //  desktop_plugin->priv = Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(desktop_plugin);
482 //  fprintf(stderr,"ddddddddddddd\n");
483   label = gtk_label_new (""); 
484   //desktop_plugin->priv->main_widget = label;
485   gtk_widget_set_size_request(label, 95, 30);
486   gtk_widget_show (label);
487   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
488   g_signal_connect (desktop_plugin, "show-settings",
489                              G_CALLBACK (live_wallpaper_settings), NULL);
490   g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
491                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
492
493   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
494   init_applet_position();
495   init_scene(desktop_plugin);
496   desktop_plugin->timer = g_timeout_add(1000*2, plugin_on_timeout, desktop_plugin);
497 }
498
499 static void
500 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
501   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
502
503   widget_class->realize = lw_applet_realize;
504   widget_class->expose_event = lw_applet_expose_event;
505
506   g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
507
508 }
509
510 static void
511 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}