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