fixed segmentation fault
[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
41 /* Position of plugin on desktop */
42 #define Xstartposition 700 
43 #define Ystartposition 425
44
45 gint xapplet = 0, yapplet = 0;
46 GSList * objects_list = NULL;
47 MultiActor * ma1;
48 Scene scene;
49
50 static void
51 lw_applet_realize (GtkWidget *widget)
52 {
53       GdkScreen *screen;
54
55       screen = gtk_widget_get_screen (widget);
56       gtk_widget_set_colormap (widget,
57                                 gdk_screen_get_rgba_colormap (screen));
58       gtk_widget_set_app_paintable (widget,
59                                 TRUE);
60       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
61 }
62
63
64 static gboolean
65 lw_applet_expose_event(GtkWidget      *widget,
66                                         GdkEventExpose *event)
67 {
68   cairo_t *cr;
69
70   /* Create cairo context */
71   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
72   gdk_cairo_region (cr, event->region);
73   cairo_clip (cr);
74
75   /* Draw alpha background */
76   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
77   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
78   cairo_paint (cr);
79
80   /* Free context */
81   cairo_destroy (cr);
82
83   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
84                                                                                   event);
85 }
86
87 static gboolean
88 expose_event (GtkWidget *widget,GdkEventExpose *event,
89      gpointer data)
90 {
91     cairo_t *cr;
92     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
93         
94     cr = gdk_cairo_create(widget->window);
95     gdk_cairo_region(cr, event->region);
96     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
97     gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
98     cairo_paint(cr);
99     cairo_destroy(cr);
100     return TRUE;
101 }
102
103 static void
104 realize (GtkWidget *widget)
105 {
106     GdkScreen *screen;
107     screen = gtk_widget_get_screen (widget);
108     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
109 }
110
111 /* Set position of widget on desktop */
112 static void
113 init_applet_position(void)
114 {
115   GSList *position = NULL;
116   gchar *position_key;
117   gchar *modified_key;
118   gchar *modified;
119   GError *error = NULL;
120   GConfClient   *gconf_client = gconf_client_get_default ();
121   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
122   position = gconf_client_get_list (gconf_client,
123                                     position_key,
124                                     GCONF_VALUE_INT,
125                                     NULL);
126   if (position && position->data && position->next->data){
127         xapplet = GPOINTER_TO_INT (position->data);
128         yapplet = GPOINTER_TO_INT (position->next->data);
129   }else{
130         position = g_slist_prepend (g_slist_prepend (NULL,
131                                       GINT_TO_POINTER (Ystartposition)),
132                                       GINT_TO_POINTER (Xstartposition));
133         gconf_client_set_list (gconf_client,
134                                position_key,
135                                GCONF_VALUE_INT,
136                                position,
137                                &error);
138         xapplet = Xstartposition;
139         yapplet = Ystartposition;
140   }
141   g_free (position_key);
142   modified = g_strdup_printf ("%ld", 0);
143   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
144   gconf_client_set_string (gconf_client,
145                            modified_key,
146                            modified,
147                            &error);
148   gconf_client_clear_cache(gconf_client);
149   g_object_unref(gconf_client);
150 }
151
152 void
153 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
154 {
155  fprintf(stderr, "actor_set_position_full\n");
156  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
157 }
158
159 static GtkWidget* 
160 init_object(gchar * name, gint x, gint y, gint z, gint width, gint height, void (*pfunc)(GtkWidget*, gpointer))
161 {
162   GtkWidget *actor;
163   GdkPixbuf *pixbuf;
164   GtkWidget *image;
165   gdouble scale = 1.0;
166
167   actor = hildon_animation_actor_new();
168   gchar str[256];
169   snprintf(str, 255, "/usr/share/anwall/%s.png", name);
170   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
171   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
172                                              width, 
173                                              height, 
174                                              NULL);
175   if (pixbuf){
176       image = gtk_image_new_from_pixbuf (pixbuf);
177       g_object_unref(G_OBJECT(pixbuf));
178   }
179   g_signal_connect(G_OBJECT(image), "expose_event",
180                            G_CALLBACK(expose_event), pixbuf);
181   gtk_container_add (GTK_CONTAINER (actor), image);
182
183   actor_set_position_full(actor, x, y, z);
184   hildon_animation_actor_set_show (actor, 1);
185   realize(actor);
186   gtk_widget_show_all(actor);
187   g_object_set_data(G_OBJECT(actor), "name", name);
188   g_object_set_data(G_OBJECT(actor), "image", image);
189   g_object_set_data(G_OBJECT(actor), "x", x);
190   g_object_set_data(G_OBJECT(actor), "y", y);
191   g_object_set_data(G_OBJECT(actor), "z", z);
192   g_object_set_data(G_OBJECT(actor), "scale", 10);
193   g_object_set_data(G_OBJECT(actor), "visible", 1);
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     if (actor && g_object_get_data(G_OBJECT(actor), "name")){
214         fprintf(stderr, "111111111111 function %s\n", g_object_get_data(G_OBJECT(actor), "name"));
215     }
216     if (data){
217         fprintf(stderr, "11111111111111111 function %s\n", data);
218     }
219     daytime = get_daytime();
220     if (daytime != TIME_NIGHT){
221          hildon_animation_actor_set_show(actor, 1);
222          //multiactor_set_visible(ma, TRUE);
223          get_sun_pos(&alt, &azm);
224          get_sun_screen_pos(alt, azm, &x, &y);
225          actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
226          //multiactor_set_position(ma, x - ma->x, y - ma->y, 0);
227     } 
228 }
229
230
231 static void
232 init_scene(GtkWidget *window)
233 {
234   GtkWidget *actor;
235   GdkPixbuf *pixbuf;
236   GtkWidget *image;
237   double alt, azm;
238   gint x, y;
239   GSList * list = NULL;
240
241   scene.window = window;
242   scene.daytime = get_daytime();
243   scene.dynamic_actors = NULL;
244   scene.static_actors = NULL;
245
246   get_sun_pos(&alt, &azm);
247   get_sun_screen_pos(alt, azm, &x, &y);
248   actor = init_object("sun", x, y, 20, 88, 88, &change_sun);
249   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
250   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
251   //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
252
253   actor = init_object("sky", 0, 0, 0, 800, 480, NULL);
254   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
255
256   actor = init_object("town", 0, 0, 10, 800, 480, NULL);
257   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
258
259   actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
260   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
261 /*
262   actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
263   list = g_slist_append(list, G_OBJECT(actor));
264   actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
265   list = g_slist_append(list, G_OBJECT(actor));
266
267   ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
268   //objects_list = g_slist_append(objects_list, G_OBJECT(ma)); 
269   */
270
271 }
272
273 void 
274 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
275 {
276     gint y0 = 400;// - уровень горизонта
277     gint o_width = 128,
278          o_height = 
279     *x = (int)(azm * 800) - 64;
280     *y = (int)((1 - alt) * y0) - 64;
281 }
282
283 void change_multiactor()
284 {
285     gboolean fl;
286     double scale;
287     gint x, y, z;
288     if (ma1->visible) fl = FALSE;
289     else fl = TRUE;
290     //multiactor_set_visible(ma1, fl);
291
292     scale = ma1->scale;
293     scale -= 0.1;
294     if (scale == 0) scale = 1;
295     //multiactor_set_scale(ma1, scale);
296
297     x = ma1->x + 10;
298     y = ma1->y + 10;
299     multiactor_set_position(ma1, x, y, 0);
300
301 }
302
303 static void 
304 change_actor(GtkWidget * actor)
305 {
306     char * name;
307     gint x, y, daytime, scale;
308     gdouble sc;
309     double alt, azm;
310
311     GtkWidget *image;
312     GdkPixbuf *pixbuf;
313
314     void (*pfunc)(gpointer, gpointer);
315
316     name = g_object_get_data(G_OBJECT(actor), "name");
317     fprintf(stderr, "change actor %s\n", name);
318     if (name == "sun"){
319         pfunc = g_object_get_data(G_OBJECT(actor), "func");
320         if (pfunc)
321             (*pfunc)(actor, g_strdup(name));
322         daytime = get_daytime();
323         if (daytime != TIME_NIGHT){
324             hildon_animation_actor_set_show(actor, 1);
325             get_sun_pos(&alt, &azm);
326             get_sun_screen_pos(alt, azm, &x, &y);
327             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
328         }
329     }
330     
331     if (name == "cloud1"){
332         x = g_object_get_data(G_OBJECT(actor), "x");
333         y = g_object_get_data(G_OBJECT(actor), "y");
334         scale = g_object_get_data(G_OBJECT(actor), "scale");
335
336         /* Start */
337         image = g_object_get_data(G_OBJECT(actor), "image");
338         
339         gtk_container_remove(actor, image);  
340         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/sun.png", 
341                                              200, 
342                                              200, 
343                                              NULL);
344         if (pixbuf){
345               image = gtk_image_new_from_pixbuf (pixbuf);
346               g_object_unref(G_OBJECT(pixbuf));
347         }
348         g_signal_connect(G_OBJECT(image), "expose_event",
349                                    G_CALLBACK(expose_event), pixbuf);
350         gtk_container_add (GTK_CONTAINER (actor), image);
351         realize(actor);
352         gtk_widget_show_all(actor);
353         /* End*/
354
355             
356         x += 40;
357         y -= 20;
358         scale -= 1;
359         if (x > 500){
360             x = 400;
361             y = 150;
362             sc = 1;
363         }
364         sc = (double)scale / 10;
365         hildon_animation_actor_set_scale(actor, sc, sc);
366         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
367         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
368         g_object_set_data(G_OBJECT(actor), "x", x);
369         g_object_set_data(G_OBJECT(actor), "y", y);
370         g_object_set_data(G_OBJECT(actor), "scale", scale);
371     }
372
373 }
374
375 static gboolean
376 plugin_on_timeout (gpointer data)
377 {
378   gint daytime = get_daytime();
379   GSList * tmp;
380   fprintf(stderr, "on timeout\n");
381
382 //  change_multiactor();
383
384   if (scene.daytime == daytime){
385       /* Change dynamic actors */
386       tmp = scene.dynamic_actors;
387   }else {
388       /* Change static actors */
389       tmp = scene.dynamic_actors;
390   } 
391   while (tmp != NULL){
392       change_actor(tmp->data);
393       tmp = g_slist_next(tmp);
394   }
395
396   scene.daytime = daytime;
397
398   //double azm, alt;
399   //get_sun_pos(&alt, &azm);
400 /*
401   GSList * tmp = objects_list;
402   while (tmp != NULL){
403       //processing(tmp->data);
404       
405       str = g_object_get_data(G_OBJECT(tmp->data), "name");
406       fprintf(stderr, "object: %s\n", str);
407       
408       if (str == "sun"){
409         //get_sun_screen_pos(alt, azm, &x, &y);
410         //x = tmp->data
411         //actor_set_position_full(tmp->data, x, y, 20);
412         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
413         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
414         //fprintf(stderr, "x = %d y = %d\n", x, y);
415         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
416         while (child != NULL) {
417             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
418             child = child->next;
419         }
420
421          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
422          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
423              
424       }
425       
426       if (str == "town"){
427           //hildon_animation_actor_set_show(tmp->data, 0);
428       }
429       //a = tmp->data;
430       //fprintf(stderr, "--timeout %s\n", a->name);
431       tmp = g_slist_next(tmp);
432   }
433   */
434   return TRUE; /* keep running this event */
435 }
436
437 static void
438 desktop_plugin_visible_notify (GObject    *object,
439                                           GParamSpec *spec,
440                                           AWallpaperPlugin *desktop_plugin)
441 {
442       gboolean visible;
443       g_object_get (object, "is-on-current-desktop", &visible, NULL);
444       fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible);
445 }
446
447 static void
448 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
449 {
450   GtkWidget *label;
451   label = gtk_label_new (""); 
452   gtk_widget_set_size_request(label, 95, 30);
453   gtk_widget_show (label);
454   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
455   g_signal_connect (desktop_plugin, "show-settings",
456                              G_CALLBACK (live_wallpaper_settings), NULL);
457   g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
458                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
459
460   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
461   init_applet_position();
462   init_scene(desktop_plugin);
463   desktop_plugin->timer = g_timeout_add(1000*10, plugin_on_timeout, desktop_plugin);
464 }
465
466 static void
467 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
468   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
469
470   widget_class->realize = lw_applet_realize;
471   widget_class->expose_event = lw_applet_expose_event;
472
473   g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
474
475 }
476
477 static void
478 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}