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 <gtk/gtk.h>
26 #include <hildon/hildon.h>
27 #include "livewp-home-widget.h"
28 #include <libhildondesktop/libhildondesktop.h>
29 #include <gconf/gconf-client.h>
30 #include "livewp-rules.h"
31
32 #define PLUGIN_NAME "livewp-home-widget.desktop-0"
33 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/%s/position"
34 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/%s/modified"
35 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/%s/view"
36
37 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
38 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
39                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
40                                                           Animation_WallpaperPrivate))
41 struct _Animation_WallpaperPrivate
42 {
43       gpointer data;
44 };
45
46
47 /* Position of plugin on desktop */
48 #define Xstartposition 700 
49 #define Ystartposition 425
50
51 gint xapplet = 0, yapplet = 0;
52 GSList * objects_list = NULL;
53 Scene scene;
54
55 static void
56 lw_applet_realize (GtkWidget *widget)
57 {
58       GdkScreen *screen;
59
60       screen = gtk_widget_get_screen (widget);
61       gtk_widget_set_colormap (widget,
62                                 gdk_screen_get_rgba_colormap (screen));
63       gtk_widget_set_app_paintable (widget,
64                                 TRUE);
65       GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
66 }
67
68
69 static gboolean
70 lw_applet_expose_event(GtkWidget      *widget,
71                                         GdkEventExpose *event)
72 {
73   cairo_t *cr;
74
75   /* Create cairo context */
76   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
77   gdk_cairo_region (cr, event->region);
78   cairo_clip (cr);
79
80   /* Draw alpha background */
81   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
82   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
83   cairo_paint (cr);
84
85   /* Free context */
86   cairo_destroy (cr);
87
88   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
89                                                                                   event);
90 }
91
92 gboolean
93 expose_event (GtkWidget *widget,GdkEventExpose *event,
94      gpointer data)
95 {
96     cairo_t *cr;
97     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
98         
99     cr = gdk_cairo_create(widget->window);
100     gdk_cairo_region(cr, event->region);
101     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
102     gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
103     cairo_paint(cr);
104     cairo_destroy(cr);
105     return TRUE;
106 }
107
108 void
109 realize (GtkWidget *widget)
110 {
111     GdkScreen *screen;
112     screen = gtk_widget_get_screen (widget);
113     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
114 }
115
116 /* set position of widget on desktop */
117 void
118 init_applet_position(void)
119 {
120   GSList *position = NULL;
121   gchar *position_key;
122   gchar *modified_key;
123   gchar *modified;
124   GError *error = NULL;
125   GConfClient   *gconf_client = gconf_client_get_default ();
126   position_key = g_strdup_printf (GCONF_KEY_POSITION, PLUGIN_NAME);
127   position = gconf_client_get_list (gconf_client,
128                                     position_key,
129                                     GCONF_VALUE_INT,
130                                     NULL);
131   if (position && position->data && position->next->data){
132         xapplet = GPOINTER_TO_INT (position->data);
133         yapplet = GPOINTER_TO_INT (position->next->data);
134   }else{
135         position = g_slist_prepend (g_slist_prepend (NULL,
136                                       GINT_TO_POINTER (Ystartposition)),
137                                       GINT_TO_POINTER (Xstartposition));
138         gconf_client_set_list (gconf_client,
139                                position_key,
140                                GCONF_VALUE_INT,
141                                position,
142                                &error);
143         xapplet = Xstartposition;
144         yapplet = Ystartposition;
145   }
146   g_free (position_key);
147   modified = g_strdup_printf ("%ld", 0);
148   modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, PLUGIN_NAME);
149   gconf_client_set_string (gconf_client,
150                            modified_key,
151                            modified,
152                            &error);
153   gconf_client_clear_cache(gconf_client);
154   g_object_unref(gconf_client);
155 }
156
157 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
158 {
159  fprintf(stderr, "actor_set_position_full\n");
160  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
161 }
162
163 GtkWidget* init_object(gchar * name, gint x, gint y, gint z, gint width, gint height)
164 {
165   Actor  a;  
166   GtkWidget *actor;
167   GdkPixbuf *pixbuf;
168   GtkWidget *image;
169
170   actor = hildon_animation_actor_new();
171   gchar str[256];
172   snprintf(str, 255, "/usr/share/anwall/%s.png", name);
173   //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
174   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
175                                              width, 
176                                              height, 
177                                              NULL);
178   if (pixbuf){
179       image = gtk_image_new_from_pixbuf (pixbuf);
180       g_object_unref(G_OBJECT(pixbuf));
181   }
182   g_signal_connect(G_OBJECT(image), "expose_event",
183                            G_CALLBACK(expose_event), pixbuf);
184   gtk_container_add (GTK_CONTAINER (actor), image);
185
186    pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/anwall/cloud1.png", 
187                                              width, 
188                                              height, 
189                                              NULL);
190   if (pixbuf){
191       image = gtk_image_new_from_pixbuf (pixbuf);
192       g_object_unref(G_OBJECT(pixbuf));
193   }
194   g_signal_connect(G_OBJECT(image), "expose_event",
195                            G_CALLBACK(expose_event), pixbuf);
196
197   gtk_container_add (GTK_CONTAINER (actor), image);
198   actor_set_position_full(actor, x, y, z);
199   hildon_animation_actor_set_show (actor, 1);
200   realize(actor);
201   gtk_widget_show_all(actor);
202   g_object_set_data(G_OBJECT(actor), "name", name);
203   g_object_set_data(G_OBJECT(actor), "x", x);
204   g_object_set_data(G_OBJECT(actor), "y", y);
205   g_object_set_data(G_OBJECT(actor), "z", z);
206   g_object_set_data(G_OBJECT(actor), "scale", 10);
207
208   
209   //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
210   //objects_list = g_slist_append(objects_list, G_OBJECT(a));
211   return actor;
212 }
213
214 void
215 init_scene(GtkWidget *window)
216 {
217   GtkWidget *actor;
218   GdkPixbuf *pixbuf;
219   GtkWidget *image;
220   double alt, azm;
221   gint x, y;
222
223   scene.window = window;
224   scene.daytime = get_daytime();
225   scene.dynamic_actors = NULL;
226   scene.static_actors = NULL;
227
228   get_sun_pos(&alt, &azm);
229   get_sun_screen_pos(alt, azm, &x, &y);
230   fprintf(stderr, "init scene\n");
231   actor = init_object("sun", x, y, 20, 88, 88);
232   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
233   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
234   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
235
236   actor = init_object("sky", 0, 0, 0, 800, 480);
237   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
238   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
239
240   actor = init_object("town", 0, 0, 10, 800, 480);
241   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
242   scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
243
244   actor = init_object("cloud1", 400, 150, 2, 200, 150);
245   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), window);
246   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
247
248 }
249
250 static void
251 example_label_home_applet_realize (GtkWidget *widget)
252 {
253 }
254 void get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
255 {
256     gint y0 = 400;// - уровень горизонта
257     gint o_width = 128,
258          o_height = 
259     *x = (int)(azm * 800) - 64;
260     *y = (int)((1 - alt) * y0) - 64;
261 }
262
263 void change_actor(GtkWidget * actor)
264 {
265     char * name;
266     gint x, y, daytime, sc;
267     double scale;
268     double alt, azm;
269     name = g_object_get_data(G_OBJECT(actor), "name");
270     fprintf(stderr, "change actor %s\n", name);
271     if (name == "sun"){
272         daytime = get_daytime();
273         if (daytime != TIME_NIGHT){
274             hildon_animation_actor_set_show(actor, 1);
275             get_sun_pos(&alt, &azm);
276             get_sun_screen_pos(alt, azm, &x, &y);
277             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
278         }
279     }
280     
281     if (name == "cloud1"){
282         x = g_object_get_data(G_OBJECT(actor), "x");
283         y = g_object_get_data(G_OBJECT(actor), "y");
284         sc = g_object_get_data(G_OBJECT(actor), "scale");
285         
286         x += 20;
287         y -= 20;
288         sc -= 1;
289         if (x > 500){
290             x = 400;
291             y = 150;
292             sc = 10;
293         }
294         scale = (double)sc / 10;
295         hildon_animation_actor_set_scale(actor, scale, scale);
296         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, scale);
297         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
298         g_object_set_data(G_OBJECT(actor), "x", x);
299         g_object_set_data(G_OBJECT(actor), "y", y);
300         g_object_set_data(G_OBJECT(actor), "scale", sc);
301     }
302
303 }
304
305 static gboolean
306 plugin_on_timeout (gpointer data)
307 {
308   gint daytime = get_daytime();
309   GSList * tmp;
310   fprintf(stderr, "on timeout\n");
311
312   if (scene.daytime == daytime){
313       // change dynamic actors
314       tmp = scene.dynamic_actors;
315   }else {
316       // change static actors
317       tmp = scene.dynamic_actors;
318   } 
319   while (tmp != NULL){
320       change_actor(tmp->data);
321       tmp = g_slist_next(tmp);
322   }
323
324   scene.daytime = daytime;
325
326   //double azm, alt;
327   //get_sun_pos(&alt, &azm);
328 /*
329   GSList * tmp = objects_list;
330   while (tmp != NULL){
331       //processing(tmp->data);
332       
333       str = g_object_get_data(G_OBJECT(tmp->data), "name");
334       fprintf(stderr, "object: %s\n", str);
335       
336       if (str == "sun"){
337         //get_sun_screen_pos(alt, azm, &x, &y);
338         //x = tmp->data
339         //actor_set_position_full(tmp->data, x, y, 20);
340         //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
341         //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
342         //fprintf(stderr, "x = %d y = %d\n", x, y);
343         child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
344         while (child != NULL) {
345             gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
346             child = child->next;
347         }
348
349          //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
350          //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
351              
352       }
353       
354       if (str == "town"){
355           //hildon_animation_actor_set_show(tmp->data, 0);
356       }
357       //a = tmp->data;
358       //fprintf(stderr, "--timeout %s\n", a->name);
359       tmp = g_slist_next(tmp);
360   }
361   */
362   return TRUE; /* keep running this event */
363 }
364 void
365 live_wallpaper_settings(GtkWidget *widget, gpointer user_data){
366         fprintf(stderr,"test\n");
367 }
368 static void
369 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
370 {
371   GtkWidget *label;
372   label = gtk_label_new (""); 
373   gtk_widget_set_size_request(label, 95, 30);
374   gtk_widget_show (label);
375   hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
376   g_signal_connect (desktop_plugin, "show-settings",
377                              G_CALLBACK (live_wallpaper_settings), NULL);
378   gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
379   init_applet_position();
380   init_scene(desktop_plugin);
381   desktop_plugin->timer = g_timeout_add(1000*10, plugin_on_timeout, desktop_plugin);
382 }
383
384 static void
385 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
386   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
387
388   widget_class->realize = lw_applet_realize;
389   widget_class->expose_event = lw_applet_expose_event;
390
391   g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
392
393 }
394
395 static void
396 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}