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"
29 #include "livewp-scene.h"
33 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/position"
34 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/modified"
35 #define GCONF_KEY_VIEW "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/view"
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))
42 lw_applet_realize (GtkWidget *widget)
47 AWallpaperPlugin *desktop_plugin = widget;
48 char* child_argv[] = {"/usr/bin/livewp", "0", (char *) 0 };
51 strid = g_strdup(hd_plugin_item_get_plugin_id (HD_PLUGIN_ITEM (widget)));
52 id = strid[strlen(strid)-1] - '0';
53 desktop_plugin->priv->view = id;
58 case 0: child_argv[1] = "1";
60 case 1: child_argv[1] = "2";
62 case 2: child_argv[1] = "3";
64 case 3: child_argv[1] = "4";
67 desktop_plugin->priv->pid = fork();
68 if (desktop_plugin->priv->pid == 0){
69 execvp("/usr/bin/livewp", child_argv);
70 fprintf(stderr,"Problem with new process");
74 read_config(desktop_plugin->priv);
76 init_scene_theme(desktop_plugin);
78 desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
80 screen = gtk_widget_get_screen (widget);
81 gtk_widget_set_colormap (widget,
82 gdk_screen_get_rgba_colormap (screen));
83 gtk_widget_set_app_paintable (widget,
85 GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
90 lw_applet_expose_event(GtkWidget *widget,
91 GdkEventExpose *event)
95 /* Create cairo context */
96 cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
97 gdk_cairo_region (cr, event->region);
100 /* Draw alpha background */
101 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
102 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
108 return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
113 expose_event (GtkWidget *widget,GdkEventExpose *event,
117 GdkPixbuf *pixbuf = (GdkPixbuf *) data;
119 cr = gdk_cairo_create(widget->window);
121 gdk_cairo_region(cr, event->region);
122 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
123 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
131 realize (GtkWidget *widget)
134 screen = gtk_widget_get_screen (widget);
135 gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
137 /* Set position of widget on desktop */
139 init_applet_position(gint *xapplet, gint *yapplet)
141 GSList *position = NULL;
145 GError *error = NULL;
147 GConfClient *gconf_client = gconf_client_get_default ();
149 position_key = g_strdup_printf (GCONF_KEY_POSITION, i);
150 position = gconf_client_get_list (gconf_client,
154 if (position && position->data && position->next->data){
155 *xapplet = GPOINTER_TO_INT (position->data);
156 *yapplet = GPOINTER_TO_INT (position->next->data);
158 position = g_slist_prepend (g_slist_prepend (NULL,
159 GINT_TO_POINTER (Ystartposition)),
160 GINT_TO_POINTER (Xstartposition));
161 gconf_client_set_list (gconf_client,
166 *xapplet = Xstartposition;
167 *yapplet = Ystartposition;
169 g_free (position_key);
170 modified = g_strdup_printf ("%i", 0);
171 modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, i);
172 gconf_client_set_string (gconf_client,
177 g_free(modified_key);
178 modified_key = g_strdup_printf (GCONF_KEY_VIEW, i);
179 gconf_client_set_int (gconf_client,
183 g_free(modified_key);
186 gconf_client_clear_cache(gconf_client);
187 g_object_unref(gconf_client);
191 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
193 /* fprintf(stderr, "actor_set_position_full z=%d\n", z); */
194 hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-0, y-0, z);
199 destroy_hildon_actor(Actor *actor)
201 //fprintf(stderr, "destroy_hildon_actor %s\n",actor->name);
202 gtk_widget_destroy(actor->widget);
203 actor->widget = NULL;
207 create_hildon_actor_text(Actor *actor, AWallpaperPlugin *desktop_plugin)
209 GtkWidget *ha = NULL;
210 GdkPixbuf *pixbuf = NULL;
211 GtkWidget *image = NULL;
212 GtkWidget *label = NULL;
214 ha = hildon_animation_actor_new();
215 label = gtk_label_new(NULL);
218 //g_signal_connect(G_OBJECT(label), "expose_event", G_CALLBACK(expose_event), NULL);
220 gtk_container_add (GTK_CONTAINER (ha), label);
223 gtk_widget_show(label);
224 gtk_widget_show_all(ha);
229 //g_object_set_data(G_OBJECT(ha), "image", image);
230 actor->image = label;
231 hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin->priv->window));
233 set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
234 set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
235 set_actor_visible(actor, actor->visible);
241 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
243 GtkWidget *ha = NULL;
244 GdkPixbuf *pixbuf = NULL;
245 GtkWidget *image = NULL;
248 ha = hildon_animation_actor_new();
249 str = g_strdup_printf( "%s/%s/%s", THEME_PATH,
250 desktop_plugin->priv->theme, actor->filename);
251 pixbuf = gdk_pixbuf_new_from_file_at_size (str,
255 /* fprintf(stderr, "create_hildon_actor %s %s\n", actor->name, str); */
259 image = gtk_image_new_from_pixbuf (pixbuf);
260 g_object_unref(G_OBJECT(pixbuf));
263 g_signal_connect(G_OBJECT(image), "expose_event",
264 G_CALLBACK(expose_event), pixbuf);
265 gtk_container_add (GTK_CONTAINER (ha), image);
268 gtk_widget_show_all(ha);
273 //g_object_set_data(G_OBJECT(ha), "image", image);
274 actor->image = image;
275 hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin->priv->window));
278 set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
279 set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
280 set_actor_visible(actor, actor->visible);
285 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
287 GtkWidget *image = NULL;
288 GdkPixbuf *pixbuf = NULL;
291 str = g_strdup_printf( "%s/%s/%s", THEME_PATH,
292 desktop_plugin->priv->theme, actor->filename);
294 pixbuf = gdk_pixbuf_new_from_file_at_size (str,
301 image = gtk_image_new_from_pixbuf (pixbuf);
302 g_object_unref(G_OBJECT(pixbuf));
305 g_signal_connect(G_OBJECT(image), "expose_event",
306 G_CALLBACK(expose_event), pixbuf);
307 //if (g_object_get_data(G_OBJECT(actor->widget), "image")){
309 gtk_container_remove(GTK_CONTAINER(actor->widget), actor->image);
311 //g_object_set_data(G_OBJECT(actor->widget), "image", image);
312 actor->image = image;
313 gtk_container_add (GTK_CONTAINER (actor->widget), image);
314 realize(actor->widget);
315 gtk_widget_show_all(actor->widget);
326 short_timeout (AWallpaperPlugin *desktop_plugin)
328 //gint daytime = get_daytime();
330 void (*pfunc)(gpointer, gpointer);
333 gboolean stop_flag = TRUE;
335 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
336 desktop_plugin->priv->short_timer = 0;
341 //scene.daytime = daytime;
342 /* fprintf(stderr, "Short timer %d\n", now); */
343 tmp = desktop_plugin->priv->scene->actors;
346 if (now >= actor->time_start_animation
347 && actor->time_start_animation > 0
348 /* && now - actor->time_start_animation <= actor->duration_animation*/){
349 pfunc = actor->func_change;
351 (*pfunc)(actor, desktop_plugin);
355 tmp = g_slist_next(tmp);
358 desktop_plugin->priv->short_timer = 0;
361 return TRUE; /* keep running this event */
365 run_long_timeout(AWallpaperPlugin *desktop_plugin)
368 gint daytime = get_daytime();
370 void (*pfunc)(gpointer, gpointer);
374 if (!desktop_plugin->priv->scene)
376 /* fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer); */
377 if (desktop_plugin->priv->scene->daytime != daytime){
378 desktop_plugin->priv->scene->daytime = daytime;
379 tmp = desktop_plugin->priv->scene->actors;
381 //change_actor(tmp->data);
382 pfunc =((Actor*)tmp->data)->func_change;
384 (*pfunc)(tmp->data, desktop_plugin);
386 tmp = g_slist_next(tmp);
391 fprintf(stderr, "Now %d\n", now);
392 tmp = desktop_plugin->priv->scene->actors;
395 if (now >= actor->time_start_animation
396 && actor->time_start_animation > 0
397 && desktop_plugin->priv->short_timer == 0){
398 /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
399 if (desktop_plugin->priv->rich_animation){
400 actor->time_start_animation = now;
401 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
404 (*actor->func_change)(actor, desktop_plugin);
407 tmp = g_slist_next(tmp);
413 long_timeout (AWallpaperPlugin *desktop_plugin)
415 /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
416 if (desktop_plugin->priv->long_timer == 0 )
418 if (!desktop_plugin->priv->visible){
419 if(desktop_plugin->priv->short_timer != 0){
420 g_source_remove(desktop_plugin->priv->short_timer);
421 desktop_plugin->priv->short_timer = 0;
423 desktop_plugin->priv->long_timer = 0;
428 run_long_timeout(desktop_plugin);
429 return TRUE; /* keep running this event */
433 desktop_plugin_visible_notify (GObject *object,
435 AWallpaperPlugin *desktop_plugin)
438 Animation_WallpaperPrivate *priv = desktop_plugin->priv;
439 g_object_get (object, "is-on-current-desktop", &visible, NULL);
440 fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible);
442 switch (priv->view + 1){
444 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
445 LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW1);
447 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
448 LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW1);
451 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
452 LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW2);
454 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
455 LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW2);
458 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
459 LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW3);
461 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
462 LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW3);
465 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
466 LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW4);
468 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
469 LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW4);
477 rich_animation_press(GtkWidget *widget, GdkEvent *event,
479 fprintf(stderr,"gggggggggggggggggggg2222\n");
484 send_alive_event(Animation_WallpaperPrivate *priv)
486 fprintf(stderr, "send_alive_event\n");
487 switch (priv->view + 1){
488 case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
489 LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW1);
491 case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
492 LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW2);
494 case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
495 LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW3);
497 case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
498 LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW4);
506 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
508 GtkWidget *rich_animation;
512 //fprintf(stderr, "!!!!!!!plugin init \n");
513 Animation_WallpaperPrivate *priv = Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
514 desktop_plugin->priv = Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
515 priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
517 /* Initialize DBUS */
518 livewp_initialize_dbus(priv);
520 priv->desktop_plugin = desktop_plugin;
521 priv->window = desktop_plugin;
522 priv->visible = FALSE;
523 priv->short_timer = 0;
527 priv->theme = g_strdup("Modern");
529 priv->hash_scene_func = g_hash_table_new(g_str_hash, g_str_equal);
531 g_hash_table_insert(priv->hash_scene_func, g_strdup("Berlin"), (gpointer)&init_scene_Berlin);
532 g_hash_table_insert(priv->hash_scene_func, g_strdup("Modern"), (gpointer)&init_scene_Modern);
533 g_hash_table_insert(priv->hash_scene_func, g_strdup("Matrix"), (gpointer)&init_scene_Matrix);
534 g_hash_table_insert(priv->hash_scene_func, g_strdup("Accel"), (gpointer)&init_scene_Accel);
535 g_hash_table_insert(priv->hash_scene_func, g_strdup("Video"), (gpointer)&init_scene_Video);
537 //priv->theme = g_strdup("Accel");
538 desktop_plugin->priv->main_widget = gtk_fixed_new();
540 gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 1, 1);
541 gtk_widget_show (desktop_plugin->priv->main_widget);
542 gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
543 init_applet_position(&(priv->xapplet), &(priv->yapplet));
547 g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
548 G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
550 priv->alive_timer = g_timeout_add(60000*3, (GtkFunction)send_alive_event, priv); /* One per 3 minute */
554 lw_applet_finalize (GObject *object)
556 int status = 0; int wpid;
557 fprintf(stderr,"lw_applet_finalize\n");
558 AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
559 Animation_WallpaperPrivate *priv = desktop_plugin->priv;
563 livewp_deinitialize_dbus(priv);
564 fprintf(stderr,"finalaze %i\n", priv->long_timer);
565 if (priv->long_timer){
566 g_source_remove(priv->long_timer);
567 priv->long_timer = 0;
569 if (priv->short_timer){
570 g_source_remove(priv->short_timer);
571 priv->short_timer = 0;
574 destroy_scene(desktop_plugin);
579 switch (priv->view + 1){
580 case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
581 LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW1);
583 case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
584 LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW2);
586 case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
587 LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW3);
589 case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE,
590 LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW4);
594 if(priv->alive_timer > 0)
595 g_source_remove(priv->alive_timer);
597 livewp_deinitialize_dbus(priv);
599 wpid = wait(&status);
600 if (wpid == priv->pid)
606 desktop_widget_finalize (GObject *object)
608 AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
609 Animation_WallpaperPrivate *priv = desktop_plugin->priv;
611 /* Call the base class's implementation: */
612 G_OBJECT_CLASS (animation_wallpaper_plugin_parent_class)->finalize (object);
616 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
618 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
619 GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
620 GObjectClass *object_class = G_OBJECT_CLASS (klass);
623 gobject_class->destroy = (gpointer)lw_applet_finalize;
624 object_class->finalize = desktop_widget_finalize;
625 widget_class->realize = lw_applet_realize;
626 // widget_class->expose_event = lw_applet_expose_event;
628 g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
633 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
636 Animation_WallpaperPrivate*
637 animation_wallpaper_plugin_new (void)
639 return g_object_new (Animation_Wallpaper_TYPE_HOME_PLUGIN , NULL);