1 /* vim: set sw=4 ts=4 et: */
3 * This file is part of Live Wallpaper (livewp)
5 * Copyright (C) 2010 Vlad Vasiliev
6 * Copyright (C) 2010 Tanya Makova
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.
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.
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
24 /*******************************************************************************/
25 #include "livewp-common.h"
26 #include "livewp-home-widget.h"
27 #include <gconf/gconf-client.h>
28 #include "livewp-rules.h"
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"
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))
40 /* Position of plugin on desktop */
41 #define Xstartposition 700
42 #define Ystartposition 425
44 gint xapplet = 0, yapplet = 0;
45 GSList * objects_list = NULL;
50 lw_applet_realize (GtkWidget *widget)
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,
59 GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
64 lw_applet_expose_event(GtkWidget *widget,
65 GdkEventExpose *event)
69 /* Create cairo context */
70 cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
71 gdk_cairo_region (cr, event->region);
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);
82 return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
87 expose_event (GtkWidget *widget,GdkEventExpose *event,
91 GdkPixbuf *pixbuf = (GdkPixbuf *) data;
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);
103 realize (GtkWidget *widget)
106 screen = gtk_widget_get_screen (widget);
107 gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
110 /* Set position of widget on desktop */
112 init_applet_position(void)
114 GSList *position = NULL;
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,
125 if (position && position->data && position->next->data){
126 xapplet = GPOINTER_TO_INT (position->data);
127 yapplet = GPOINTER_TO_INT (position->next->data);
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,
137 xapplet = Xstartposition;
138 yapplet = Ystartposition;
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,
147 gconf_client_clear_cache(gconf_client);
148 g_object_unref(gconf_client);
152 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
154 // fprintf(stderr, "actor_set_position_full\n");
155 hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
159 init_object(gchar * name, gchar * filename, gint x, gint y, gint z, gint width, gint height, gboolean visible, gint scale, gint opacity, void (*pfunc)(GtkWidget*, gpointer))
165 actor = hildon_animation_actor_new();
167 snprintf(str, 255, "/usr/share/livewp/theme/Modern/%s", filename);
168 //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
169 pixbuf = gdk_pixbuf_new_from_file_at_size (str,
174 image = gtk_image_new_from_pixbuf (pixbuf);
175 g_object_unref(G_OBJECT(pixbuf));
177 g_signal_connect(G_OBJECT(image), "expose_event",
178 G_CALLBACK(expose_event), pixbuf);
179 gtk_container_add (GTK_CONTAINER (actor), image);
181 actor_set_position_full(actor, x, y, z);
182 hildon_animation_actor_set_show (actor, visible);
184 gtk_widget_show_all(actor);
185 g_object_set_data(G_OBJECT(actor), "name", name);
186 g_object_set_data(G_OBJECT(actor), "filename", filename);
187 g_object_set_data(G_OBJECT(actor), "image", image);
188 g_object_set_data(G_OBJECT(actor), "x", x);
189 g_object_set_data(G_OBJECT(actor), "y", y);
190 g_object_set_data(G_OBJECT(actor), "z", z);
191 g_object_set_data(G_OBJECT(actor), "scale", scale);
192 g_object_set_data(G_OBJECT(actor), "visible", visible);
193 g_object_set_data(G_OBJECT(actor), "opacity", opacity);
194 g_object_set_data(G_OBJECT(actor), "func", pfunc);
196 hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), scene.window);
204 //objects_list = g_slist_append(objects_list, G_OBJECT(actor));
205 //objects_list = g_slist_append(objects_list, G_OBJECT(a));
209 void change_sun(GtkWidget * actor, gpointer data)
212 gint x, y, z, daytime;
213 //fprintf(stderr, "change sun \n");
215 daytime = get_daytime();
216 if (daytime != TIME_NIGHT){
217 hildon_animation_actor_set_show(actor, 1);
218 get_sun_pos(&alt, &azm);
219 get_sun_screen_pos(alt, azm, &x, &y);
220 actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
221 g_object_set_data(G_OBJECT(actor), "visible", 1);
222 g_object_set_data(G_OBJECT(actor), "x", x);
223 g_object_set_data(G_OBJECT(actor), "y", y);
225 hildon_animation_actor_set_show(actor, 0);
226 g_object_set_data(G_OBJECT(actor), "visible", 0);
230 void change_tram(GtkWidget * actor, gpointer data)
233 gint x, y, z, scale, sc, daytime;
234 //fprintf(stderr, "change sun \n");
236 x = g_object_get_data(G_OBJECT(actor), "x");
237 y = g_object_get_data(G_OBJECT(actor), "y");
238 scale = g_object_get_data(G_OBJECT(actor), "scale");
240 if (x > 480+350) x = -350;
241 scale = (x + 400) * 50 / 880 + 100;
242 y = y - 240 * (scale-100) / 100;
244 //fprintf(stderr, "scale =%i y=%i\n", y,scale);
245 actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
246 g_object_set_data(G_OBJECT(actor), "x", x);
247 //g_object_set_data(G_OBJECT(actor), "y", y);
248 hildon_animation_actor_set_scale(actor, (double)scale/100, (double)scale/100);
249 g_object_set_data(G_OBJECT(actor), "scale", scale);
253 void change_sky(GtkWidget * actor, gpointer data)
255 gint daytime, opacity;
256 //fprintf(stderr, "change sky \n");
258 daytime = get_daytime();
259 opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 10;
260 if (opacity < 0) opacity = 255;
261 hildon_animation_actor_set_show_full(actor, 1, opacity);
262 g_object_set_data(G_OBJECT(actor), "opacity", opacity);
265 //fprintf(stderr, "change sky night\n");
268 //fprintf(stderr, "change sky day\n");
274 void change_background(GtkWidget * actor, gpointer data)
278 opacity = g_object_get_data(G_OBJECT(actor), "opacity") - 1;
279 if (opacity < 0) opacity = 255;
280 hildon_animation_actor_set_show_full(actor, 1, opacity);
281 g_object_set_data(G_OBJECT(actor), "opacity", opacity);
287 init_scene(GtkWidget *window)
294 GSList * list = NULL;
296 scene.window = window;
297 scene.daytime = get_daytime();
298 scene.dynamic_actors = NULL;
299 scene.static_actors = NULL;
301 get_sun_pos(&alt, &azm);
302 get_sun_screen_pos(alt, azm, &x, &y);
303 actor = init_object("sun", "sun.png", x, y, 11, 88, 88, 1, 100, 255, &change_sun);
304 //scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
305 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
306 //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
308 actor = init_object("sky", "sky.png", 0, 0, 5, 800, 480, 1, 100, 255, NULL);
309 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
311 actor = init_object("town", "town.png", 0, 480-374, 10, 800, 374, 1, 100, 255, NULL);
312 scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
314 actor = init_object("border", "border.png", 0, 480-79, 30, 800, 79, 1, 100, 255, NULL);
315 scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
317 actor = init_object("tram", "tram.png", -350, 60+480-374+34, 25, 350, 274, 1, 100, 255, &change_tram);
318 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
321 actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
322 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
324 actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
325 list = g_slist_append(list, G_OBJECT(actor));
326 actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
327 list = g_slist_append(list, G_OBJECT(actor));
329 ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
330 //objects_list = g_slist_append(objects_list, G_OBJECT(ma));
335 init_scene1(GtkWidget *window)
342 GSList * list = NULL;
344 scene.window = window;
345 scene.daytime = get_daytime();
346 scene.dynamic_actors = NULL;
347 scene.static_actors = NULL;
349 get_sun_pos(&alt, &azm);
350 get_sun_screen_pos(alt, azm, &x, &y);
351 actor = init_object("sun", "sun.png", x, y, 20, 88, 88, 1, 100, 255, &change_sun);
352 //scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
353 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
354 //ma1->multiactor_init("sun", g_slist_append(list, G_OBJECT(actor)));
356 actor = init_object("background", "sky_dark.png", 0, 0, 4, 800, 480, 1, 100, 255, NULL);
357 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
359 actor = init_object("sky", "sky.png", 0, 0, 5, 800, 480, 1, 100, 255, &change_sky);
360 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
362 actor = init_object("town", "town.png", 0, 0, 10, 800, 480, 1, 100, 255, NULL);
363 scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
366 actor = init_object("cloud1", 400, 150, 2, 200, 150, NULL);
367 scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
369 actor = init_object("sun", 10, 10, 50, 88, 88, NULL);
370 list = g_slist_append(list, G_OBJECT(actor));
371 actor = init_object("cloud1", 50, 50, 49, 150, 100, NULL);
372 list = g_slist_append(list, G_OBJECT(actor));
374 ma1 = multiactor_init("multi", list, 0, 0, 50, 1.0, TRUE);
375 //objects_list = g_slist_append(objects_list, G_OBJECT(ma));
382 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
384 gint y0 = 400;// - уровень горизонта
387 *x = (int)(azm * 800) - 64;
388 *y = (int)((1 - alt) * y0) - 64;
391 void change_multiactor()
396 if (ma1->visible) fl = FALSE;
398 //multiactor_set_visible(ma1, fl);
402 if (scale == 0) scale = 1;
403 //multiactor_set_scale(ma1, scale);
407 //multiactor_set_position(ma1, x, y, 0);
412 change_actor(GtkWidget * actor)
415 gint x, y, daytime, scale;
422 void (*pfunc)(gpointer, gpointer);
424 name = g_object_get_data(G_OBJECT(actor), "name");
425 fprintf(stderr, "change actor %s\n", name);
427 pfunc = g_object_get_data(G_OBJECT(actor), "func");
429 (*pfunc)(actor, g_strdup(name));
430 daytime = get_daytime();
431 if (daytime != TIME_NIGHT){
432 hildon_animation_actor_set_show(actor, 1);
433 get_sun_pos(&alt, &azm);
434 get_sun_screen_pos(alt, azm, &x, &y);
435 actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
439 if (name == "cloud1"){
440 x = g_object_get_data(G_OBJECT(actor), "x");
441 y = g_object_get_data(G_OBJECT(actor), "y");
442 scale = g_object_get_data(G_OBJECT(actor), "scale");
445 image = g_object_get_data(G_OBJECT(actor), "image");
447 gtk_container_remove(actor, image);
448 pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png",
453 image = gtk_image_new_from_pixbuf (pixbuf);
454 g_object_unref(G_OBJECT(pixbuf));
456 g_signal_connect(G_OBJECT(image), "expose_event",
457 G_CALLBACK(expose_event), pixbuf);
458 gtk_container_add (GTK_CONTAINER (actor), image);
460 gtk_widget_show_all(actor);
472 sc = (double)scale / 100;
473 hildon_animation_actor_set_scale(actor, sc, sc);
474 fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
475 actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
476 g_object_set_data(G_OBJECT(actor), "x", x);
477 g_object_set_data(G_OBJECT(actor), "y", y);
478 g_object_set_data(G_OBJECT(actor), "scale", scale);
484 plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
486 gint daytime = get_daytime();
488 void (*pfunc)(gpointer, gpointer);
489 //fprintf(stderr, "on timeout\n");
491 if (scene.daytime == daytime){
492 /* Change dynamic actors */
493 tmp = scene.dynamic_actors;
495 /* Change static actors */
496 tmp = scene.static_actors;
499 //change_actor(tmp->data);
500 pfunc = g_object_get_data(G_OBJECT(tmp->data), "func");
502 (*pfunc)(tmp->data, NULL);
504 tmp = g_slist_next(tmp);
507 scene.daytime = daytime;
510 //get_sun_pos(&alt, &azm);
512 GSList * tmp = objects_list;
514 //processing(tmp->data);
516 str = g_object_get_data(G_OBJECT(tmp->data), "name");
517 fprintf(stderr, "object: %s\n", str);
520 //get_sun_screen_pos(alt, azm, &x, &y);
522 //actor_set_position_full(tmp->data, x, y, 20);
523 //g_object_set_data(G_OBJECT(tmp->data), "posX", x);
524 //g_object_set_data(G_OBJECT(tmp->data), "posY", y);
525 //fprintf(stderr, "x = %d y = %d\n", x, y);
526 child = gtk_container_get_children(GTK_CONTAINER (tmp->data));
527 while (child != NULL) {
528 gtk_container_remove(GTK_CONTAINER (tmp->data), child->data);
532 //snprintf(str, 255, "/usr/share/anwall/%s.png", name);
533 //fprintf(stderr, "!!!init object !!!!\nname = %s file = %s\n", name, str);
538 //hildon_animation_actor_set_show(tmp->data, 0);
541 //fprintf(stderr, "--timeout %s\n", a->name);
542 tmp = g_slist_next(tmp);
545 return TRUE; /* keep running this event */
549 desktop_plugin_visible_notify (GObject *object,
551 AWallpaperPlugin *desktop_plugin)
554 g_object_get (object, "is-on-current-desktop", &visible, NULL);
555 fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible);
559 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
562 /* This code doesn't work */
563 // fprintf(stderr,"ddddddddddddd\n");
564 // desktop_plugin->priv = Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(desktop_plugin);
565 // fprintf(stderr,"ddddddddddddd\n");
566 label = gtk_label_new ("");
567 //desktop_plugin->priv->main_widget = label;
568 gtk_widget_set_size_request(label, 95, 30);
569 gtk_widget_show (label);
570 hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
571 g_signal_connect (desktop_plugin, "show-settings",
572 G_CALLBACK (live_wallpaper_settings), NULL);
573 g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
574 G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
576 gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
577 init_applet_position();
578 init_scene(desktop_plugin);
579 desktop_plugin->timer = g_timeout_add(50, plugin_on_timeout, desktop_plugin);
583 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
584 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
586 widget_class->realize = lw_applet_realize;
587 widget_class->expose_event = lw_applet_expose_event;
589 g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
594 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {}