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 <gtk/gtk.h>
26 #include <hildon/hildon.h>
27 #include "livewp-home-widget.h"
28 #include <gconf/gconf-client.h>
29 #include "livewp-rules.h"
30
31 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
32 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
33 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
34 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
35
36 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
37 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
38                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
39                                                           Animation_WallpaperPrivate))
40
41
42 /* Position of plugin on desktop */
43 #define Xstartposition 700 
44 #define Ystartposition 425
45
46 gint xapplet = 0, yapplet = 0;
47 GSList * objects_list = NULL;
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 static 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)
161 {
162   Actor  a;  
163   GtkWidget *actor;
164   GdkPixbuf *pixbuf;
165   GtkWidget *image;
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    pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/cloud1.png", 
184                                              width, 
185                                              height, 
186                                              NULL);
187   if (pixbuf){
188       image = gtk_image_new_from_pixbuf (pixbuf);
189       g_object_unref(G_OBJECT(pixbuf));
190   }
191   g_signal_connect(G_OBJECT(image), "expose_event",
192                            G_CALLBACK(expose_event), pixbuf);
193
194   gtk_container_add (GTK_CONTAINER (actor), image);
195   actor_set_position_full(actor, x, y, z);
196   hildon_animation_actor_set_show (actor, 1);
197   realize(actor);
198   gtk_widget_show_all(actor);
199   g_object_set_data(G_OBJECT(actor), "name", name);
200   g_object_set_data(G_OBJECT(actor), "x", x);
201   g_object_set_data(G_OBJECT(actor), "y", y);
202   g_object_set_data(G_OBJECT(actor), "z", z);
203   g_object_set_data(G_OBJECT(actor), "scale", 10);
204
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 static void
212 init_scene(GtkWidget *window)
213 {
214   GtkWidget *actor;
215   GdkPixbuf *pixbuf;
216   GtkWidget *image;
217   double alt, azm;
218   gint x, y;
219
220   scene.window = window;
221   scene.daytime = get_daytime();
222   scene.dynamic_actors = NULL;
223   scene.static_actors = NULL;
224
225   get_sun_pos(&alt, &azm);
226   get_sun_screen_pos(alt, azm, &x, &y);
227   fprintf(stderr, "init scene\n");
228   actor = init_object("sun", x, y, 20, 88, 88);
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   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
232
233   actor = init_object("sky", 0, 0, 0, 800, 480);
234   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
235   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
236
237   actor = init_object("town", 0, 0, 10, 800, 480);
238   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
239   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
240
241   actor = init_object("cloud1", 400, 150, 2, 200, 150);
242   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
243   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
244
245 }
246
247 static void 
248 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
249 {
250     gint y0 = 400;// - уровень горизонта
251     gint o_width = 128,
252          o_height = 
253     *x = (int)(azm * 800) - 64;
254     *y = (int)((1 - alt) * y0) - 64;
255 }
256
257 static void 
258 change_actor(GtkWidget * actor)
259 {
260     char * name;
261     gint x, y, daytime, sc;
262     double scale;
263     double alt, azm;
264     name = g_object_get_data(G_OBJECT(actor), "name");
265     fprintf(stderr, "change actor %s\n", name);
266     if (name == "sun"){
267         daytime = get_daytime();
268         if (daytime != TIME_NIGHT){
269             hildon_animation_actor_set_show(actor, 1);
270             get_sun_pos(&alt, &azm);
271             get_sun_screen_pos(alt, azm, &x, &y);
272             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
273         }
274     }
275     
276     if (name == "cloud1"){
277         x = g_object_get_data(G_OBJECT(actor), "x");
278         y = g_object_get_data(G_OBJECT(actor), "y");
279         sc = g_object_get_data(G_OBJECT(actor), "scale");
280         
281         x += 20;
282         y -= 20;
283         sc -= 1;
284         if (x > 500){
285             x = 400;
286             y = 150;
287             sc = 10;
288         }
289         scale = (double)sc / 10;
290         hildon_animation_actor_set_scale(actor, scale, scale);
291         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, scale);
292         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
293         g_object_set_data(G_OBJECT(actor), "x", x);
294         g_object_set_data(G_OBJECT(actor), "y", y);
295         g_object_set_data(G_OBJECT(actor), "scale", sc);
296     }
297
298 }
299
300 static gboolean
301 plugin_on_timeout (gpointer data)
302 {
303   gint daytime = get_daytime();
304   GSList * tmp;
305   fprintf(stderr, "on timeout\n");
306
307   if (scene.daytime == daytime){
308       /* Change dynamic actors */
309       tmp = scene.dynamic_actors;
310   }else {
311       /* Change static actors */
312       tmp = scene.dynamic_actors;
313   } 
314   while (tmp != NULL){
315       change_actor(tmp->data);
316       tmp = g_slist_next(tmp);
317   }
318
319   scene.daytime = daytime;
320
321   //double azm, alt;
322   //get_sun_pos(&alt, &azm);
323 /*
324   GSList * tmp = objects_list;
325   while (tmp != NULL){
326       //processing(tmp->data);
327       
328       str = g_object_get_data(G_OBJECT(tmp->data), "name");
329       fprintf(stderr, "object: %s\n", str);
330       
331       if (str == "sun"){
332         //get_sun_screen_pos(alt, azm, &x, &y);
333         //x = tmp->data
334         //actor_set_position_full(tmp->data, x, y, 20);
335         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
336         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
337         //fprintf(stderr, "x = %d y = %d\n", x, y);
338         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
339         while (child != NULL) {
340             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
341             child = child->next;
342         }
343
344          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
345          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
346              
347       }
348       
349       if (str == "town"){
350           //hildon_animation_actor_set_show(tmp->data, 0);
351       }
352       //a = tmp->data;
353       //fprintf(stderr, "--timeout %s\n", a->name);
354       tmp = g_slist_next(tmp);
355   }
356   */
357   return TRUE; /* keep running this event */
358 }
359 void
360 live_wallpaper_settings(GtkWidget *widget, gpointer user_data){
361         fprintf(stderr,"test\n");
362 }
363 static void
364 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
365 {
366   GtkWidget *label;
367   label = gtk_label_new (""); 
368   gtk_widget_set_size_request(label, 95, 30);
369   gtk_widget_show (label);
370   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
371   g_signal_connect (desktop_plugin, "show-settings",
372                              G_CALLBACK (live_wallpaper_settings), NULL);
373   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
374   init_applet_position();
375   init_scene(desktop_plugin);
376   desktop_plugin->timer = g_timeout_add(1000*10, plugin_on_timeout, desktop_plugin);
377 }
378
379 static void
380 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
381   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
382
383   widget_class->realize = lw_applet_realize;
384   widget_class->expose_event = lw_applet_expose_event;
385
386   g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
387
388 }
389
390 static void
391 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}