Merge branch 'master' of https://vcs.maemo.org/git/livewp
[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 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 static 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)
160 {
161   Actor  a;  
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   
188   g_object_set_data(G_OBJECT(actor), "name", name);
189   g_object_set_data(G_OBJECT(actor), "image", image);
190   g_object_set_data(G_OBJECT(actor), "x", x);
191   g_object_set_data(G_OBJECT(actor), "y", y);
192   g_object_set_data(G_OBJECT(actor), "z", z);
193   g_object_set_data(G_OBJECT(actor), "scale", 10);
194 /*
195   a.widget = actor;
196   a.name = name;
197   a.x = x;
198   a.y = y;
199   a.z = z;
200   */
201   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
202   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
203   return actor;
204 }
205
206 static void
207 init_scene(GtkWidget *window)
208 {
209   GtkWidget *actor;
210   GdkPixbuf *pixbuf;
211   GtkWidget *image;
212   double alt, azm;
213   gint x, y;
214
215   scene.window = window;
216   scene.daytime = get_daytime();
217   scene.dynamic_actors = NULL;
218   scene.static_actors = NULL;
219
220   get_sun_pos(&alt, &azm);
221   get_sun_screen_pos(alt, azm, &x, &y);
222   fprintf(stderr, "init scene\n");
223   actor = init_object("sun", x, y, 20, 88, 88);
224   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
225   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
226   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
227
228   actor = init_object("sky", 0, 0, 0, 800, 480);
229   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
230   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
231
232   actor = init_object("town", 0, 0, 10, 800, 480);
233   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
234   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
235
236   actor = init_object("cloud1", 400, 150, 2, 200, 150);
237   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
238   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
239
240 }
241
242 void 
243 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
244 {
245     gint y0 = 400;// - уровень горизонта
246     gint o_width = 128,
247          o_height = 
248     *x = (int)(azm * 800) - 64;
249     *y = (int)((1 - alt) * y0) - 64;
250 }
251
252 static void 
253 change_actor(GtkWidget * actor)
254 {
255     char * name;
256     gint x, y, daytime, scale;
257     gdouble sc;
258     double alt, azm;
259
260     GtkWidget *image;
261     GdkPixbuf *pixbuf;
262
263     name = g_object_get_data(G_OBJECT(actor), "name");
264     fprintf(stderr, "change actor %s\n", name);
265     if (name == "sun"){
266         daytime = get_daytime();
267         if (daytime != TIME_NIGHT){
268             hildon_animation_actor_set_show(actor, 1);
269             get_sun_pos(&alt, &azm);
270             get_sun_screen_pos(alt, azm, &x, &y);
271             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
272         }
273     }
274     
275     if (name == "cloud1"){
276         x = g_object_get_data(G_OBJECT(actor), "x");
277         y = g_object_get_data(G_OBJECT(actor), "y");
278         scale = g_object_get_data(G_OBJECT(actor), "scale");
279
280         /* Start */
281         image = g_object_get_data(G_OBJECT(actor), "image");
282         
283         gtk_container_remove(actor, image);  
284         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/sun.png", 
285                                              200, 
286                                              200, 
287                                              NULL);
288         if (pixbuf){
289               image = gtk_image_new_from_pixbuf (pixbuf);
290               g_object_unref(G_OBJECT(pixbuf));
291         }
292         g_signal_connect(G_OBJECT(image), "expose_event",
293                                    G_CALLBACK(expose_event), pixbuf);
294         gtk_container_add (GTK_CONTAINER (actor), image);
295         realize(actor);
296         gtk_widget_show_all(actor);
297         /* End*/
298
299             
300         x += 40;
301         y -= 20;
302         scale -= 1;
303         if (x > 500){
304             x = 400;
305             y = 150;
306             sc = 1;
307         }
308         sc = (double)scale / 10;
309         hildon_animation_actor_set_scale(actor, sc, sc);
310         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
311         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
312         g_object_set_data(G_OBJECT(actor), "x", x);
313         g_object_set_data(G_OBJECT(actor), "y", y);
314         g_object_set_data(G_OBJECT(actor), "scale", scale);
315     }
316
317 }
318
319 static gboolean
320 plugin_on_timeout (gpointer data)
321 {
322   gint daytime = get_daytime();
323   GSList * tmp;
324   fprintf(stderr, "on timeout\n");
325
326   if (scene.daytime == daytime){
327       /* Change dynamic actors */
328       tmp = scene.dynamic_actors;
329   }else {
330       /* Change static actors */
331       tmp = scene.dynamic_actors;
332   } 
333   while (tmp != NULL){
334       change_actor(tmp->data);
335       tmp = g_slist_next(tmp);
336   }
337
338   scene.daytime = daytime;
339
340   //double azm, alt;
341   //get_sun_pos(&alt, &azm);
342 /*
343   GSList * tmp = objects_list;
344   while (tmp != NULL){
345       //processing(tmp->data);
346       
347       str = g_object_get_data(G_OBJECT(tmp->data), "name");
348       fprintf(stderr, "object: %s\n", str);
349       
350       if (str == "sun"){
351         //get_sun_screen_pos(alt, azm, &x, &y);
352         //x = tmp->data
353         //actor_set_position_full(tmp->data, x, y, 20);
354         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
355         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
356         //fprintf(stderr, "x = %d y = %d\n", x, y);
357         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
358         while (child != NULL) {
359             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
360             child = child->next;
361         }
362
363          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
364          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
365              
366       }
367       
368       if (str == "town"){
369           //hildon_animation_actor_set_show(tmp->data, 0);
370       }
371       //a = tmp->data;
372       //fprintf(stderr, "--timeout %s\n", a->name);
373       tmp = g_slist_next(tmp);
374   }
375   */
376   return TRUE; /* keep running this event */
377 }
378
379 static void
380 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
381 {
382   GtkWidget *label;
383   label = gtk_label_new (""); 
384   gtk_widget_set_size_request(label, 95, 30);
385   gtk_widget_show (label);
386   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
387   g_signal_connect (desktop_plugin, "show-settings",
388                              G_CALLBACK (live_wallpaper_settings), NULL);
389   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
390   init_applet_position();
391   init_scene(desktop_plugin);
392   desktop_plugin->timer = g_timeout_add(1000*10, plugin_on_timeout, desktop_plugin);
393 }
394
395 static void
396 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
397   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
398
399   widget_class->realize = lw_applet_realize;
400   widget_class->expose_event = lw_applet_expose_event;
401
402   g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
403
404 }
405
406 static void
407 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}