prepared to release 0.6 version
[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-scene.h"
30 #include <sys/time.h>
31 #include <stdlib.h>
32
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"
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 static void
42 lw_applet_realize (GtkWidget *widget)
43 {
44     GdkScreen *screen;
45     gchar * strid; 
46     gint id; 
47     AWallpaperPlugin *desktop_plugin = widget;
48
49     strid = g_strdup(hd_plugin_item_get_plugin_id (HD_PLUGIN_ITEM (widget)));
50     id = strid[strlen(strid)-1] - '0';
51     desktop_plugin->priv->view = id;
52     g_free(strid); 
53     
54     /* Load config */
55     read_config(desktop_plugin->priv);
56
57     init_scene_theme(desktop_plugin);
58         
59     desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
60
61     screen = gtk_widget_get_screen (widget);
62     gtk_widget_set_colormap (widget,
63                                 gdk_screen_get_rgba_colormap (screen));
64     gtk_widget_set_app_paintable (widget,
65                                 TRUE);
66     GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
67 }
68
69
70 static gboolean
71 lw_applet_expose_event(GtkWidget      *widget,
72                                         GdkEventExpose *event)
73 {
74   cairo_t *cr;
75
76   /* Create cairo context */
77   cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
78   gdk_cairo_region (cr, event->region);
79   cairo_clip (cr);
80
81   /* Draw alpha background */
82   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
83   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
84   cairo_paint (cr);
85
86   /* Free context */
87   cairo_destroy (cr);
88
89   return GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->expose_event (widget,
90                                                                                   event);
91 }
92
93 static gboolean
94 expose_event (GtkWidget *widget,GdkEventExpose *event,
95      gpointer data)
96 {
97     cairo_t *cr;
98     GdkPixbuf *pixbuf = (GdkPixbuf *) data;
99         
100     cr = gdk_cairo_create(widget->window);
101     if (cr){
102         gdk_cairo_region(cr, event->region);
103         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
104         gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.0, 0.0);
105         cairo_paint(cr);
106         cairo_destroy(cr);
107     }
108     return TRUE;
109 }
110
111 static void
112 realize (GtkWidget *widget)
113 {
114     GdkScreen *screen;
115     screen = gtk_widget_get_screen (widget);
116     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
117 }
118 /* Set position of widget on desktop */
119 static void
120 init_applet_position(gint *xapplet, gint *yapplet)
121 {
122   GSList *position = NULL;
123   gchar *position_key;
124   gchar *modified_key;
125   gchar *modified;
126   GError *error = NULL;
127   gint i;
128   GConfClient   *gconf_client = gconf_client_get_default ();
129   for (i=0;i<4;i++){
130       position_key = g_strdup_printf (GCONF_KEY_POSITION, i);
131       position = gconf_client_get_list (gconf_client,
132                                         position_key,
133                                         GCONF_VALUE_INT,
134                                         NULL);
135       if (position && position->data && position->next->data){
136         *xapplet = GPOINTER_TO_INT (position->data);
137         *yapplet = GPOINTER_TO_INT (position->next->data);
138       }else{
139         position = g_slist_prepend (g_slist_prepend (NULL,
140                                           GINT_TO_POINTER (Ystartposition)),
141                               GINT_TO_POINTER (Xstartposition));
142         gconf_client_set_list (gconf_client,
143                                    position_key,
144                                    GCONF_VALUE_INT,
145                                position,
146                        &error);
147         *xapplet = Xstartposition;
148         *yapplet = Ystartposition;
149       }
150       g_free (position_key);
151       modified = g_strdup_printf ("%i", 0);
152       modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, i);
153       gconf_client_set_string (gconf_client,
154                                modified_key,
155                                modified,
156                                &error);
157       g_free(modified);
158       g_free(modified_key);
159       modified_key = g_strdup_printf (GCONF_KEY_VIEW, i);
160       gconf_client_set_int (gconf_client,
161                                modified_key,
162                                (i+1),
163                                &error);
164       g_free(modified_key);
165
166   }
167   gconf_client_clear_cache(gconf_client);
168   g_object_unref(gconf_client);
169 }
170 #if 0
171 void
172 actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
173 {
174 /*  fprintf(stderr, "actor_set_position_full z=%d\n", z); */
175     hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-0, y-0, z);
176 }
177 #endif
178
179 void
180 destroy_hildon_actor(Actor *actor)
181 {
182     //fprintf(stderr, "destroy_hildon_actor %s\n",actor->name);
183     gtk_widget_destroy(actor->widget);
184     actor->widget = NULL;
185 }
186
187 void
188 create_hildon_actor_text(Actor *actor, AWallpaperPlugin *desktop_plugin) 
189 {
190   GtkWidget *ha = NULL;
191   GdkPixbuf *pixbuf = NULL;
192   GtkWidget *image = NULL;
193   GtkWidget *label = NULL;
194
195   ha = hildon_animation_actor_new();
196   label = gtk_label_new(NULL);  
197
198   if (label){
199     //g_signal_connect(G_OBJECT(label), "expose_event", G_CALLBACK(expose_event), NULL);
200
201     gtk_container_add (GTK_CONTAINER (ha), label);
202   }  
203   realize(ha);
204   gtk_widget_show(label);
205   gtk_widget_show_all(ha);
206   
207   /* TO DO check it */
208   /*  gdk_flush (); */
209
210   //g_object_set_data(G_OBJECT(ha), "image", image);
211   actor->image = label;
212   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
213   actor->widget = ha;
214   set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
215   set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
216   set_actor_visible(actor, actor->visible);
217 }
218
219
220
221 void
222 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
223 {
224   GtkWidget *ha = NULL;
225   GdkPixbuf *pixbuf = NULL;
226   GtkWidget *image = NULL;
227   gchar     *str = NULL;
228
229   ha = hildon_animation_actor_new();
230   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
231                         desktop_plugin->priv->theme, actor->filename);
232   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
233                                              actor->width, 
234                                              actor->height, 
235                                              NULL);
236   /*fprintf(stderr, "create_hildon_actor %s %s\n", actor->name, str);*/
237   if (str)
238       g_free(str);
239   if (pixbuf){
240       image = gtk_image_new_from_pixbuf (pixbuf);
241       g_object_unref(G_OBJECT(pixbuf));
242   }
243   if (image){
244     g_signal_connect(G_OBJECT(image), "expose_event",
245                            G_CALLBACK(expose_event), pixbuf);
246     gtk_container_add (GTK_CONTAINER (ha), image);
247   }  
248   realize(ha);
249   gtk_widget_show_all(ha);
250   
251   /* TO DO check it */
252   /*  gdk_flush (); */
253
254   //g_object_set_data(G_OBJECT(ha), "image", image);
255   actor->image = image;
256   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
257
258   actor->widget = ha;
259   set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
260   set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
261   set_actor_visible(actor, actor->visible);
262 }
263
264
265 void
266 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
267 {
268     GtkWidget *image = NULL;
269     GdkPixbuf *pixbuf = NULL;
270     gchar     *str = NULL;
271
272     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
273                             desktop_plugin->priv->theme, actor->filename);
274  
275     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
276                                                actor->width, 
277                                                actor->height, 
278                                                NULL);
279     if(str)
280         g_free(str);
281     if (pixbuf){
282         image = gtk_image_new_from_pixbuf (pixbuf);
283         g_object_unref(G_OBJECT(pixbuf));
284     }
285     if (image){ 
286         g_signal_connect(G_OBJECT(image), "expose_event",
287                                        G_CALLBACK(expose_event), pixbuf);
288         //if (g_object_get_data(G_OBJECT(actor->widget), "image")){
289         if (actor->image){
290             gtk_container_remove(GTK_CONTAINER(actor->widget), actor->image);  
291         }
292         //g_object_set_data(G_OBJECT(actor->widget), "image", image);
293         actor->image = image;
294         gtk_container_add (GTK_CONTAINER (actor->widget), image);
295         realize(actor->widget);
296         gtk_widget_show_all(actor->widget);
297         /* TO DO check it */
298        /*  gdk_flush (); */
299
300
301     }
302
303 }
304
305
306 static gboolean
307 short_timeout (AWallpaperPlugin *desktop_plugin)
308 {
309     //gint daytime = get_daytime();
310     GSList * tmp;
311     void (*pfunc)(gpointer, gpointer);
312     time_t now;
313     Actor *actor;
314     gboolean stop_flag = TRUE;
315
316 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
317         desktop_plugin->priv->short_timer = 0;
318         return FALSE;
319     }
320
321     now = time(NULL);
322     //scene.daytime = daytime;
323     /* fprintf(stderr, "Short timer %d\n", now); */
324     tmp = desktop_plugin->priv->scene->actors;
325     while (tmp != NULL){
326            actor = tmp->data;
327            if (now >= actor->time_start_animation  
328                && actor->time_start_animation > 0
329                /* && now - actor->time_start_animation <= actor->duration_animation*/){
330                 pfunc = actor->func_change;
331                 if (pfunc){ 
332                     (*pfunc)(actor, desktop_plugin);
333                     stop_flag = FALSE;
334                 }
335             }
336             tmp = g_slist_next(tmp);
337     }
338     if (stop_flag){
339          desktop_plugin->priv->short_timer = 0;
340          return FALSE;
341     }else
342          return TRUE; /* keep running this event */
343 }
344
345 void
346 run_long_timeout(AWallpaperPlugin *desktop_plugin)
347 {
348
349     gint daytime = get_daytime();
350     GSList * tmp;
351     void (*pfunc)(gpointer, gpointer);
352     time_t now;
353     Actor *actor;
354
355     if (!desktop_plugin->priv->scene)
356         return;
357     //fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer);
358     if (desktop_plugin->priv->scene->daytime != daytime){
359         desktop_plugin->priv->scene->daytime = daytime;
360         tmp = desktop_plugin->priv->scene->actors;
361         while (tmp != NULL){
362             //change_actor(tmp->data);
363             pfunc =((Actor*)tmp->data)->func_change;
364             if (pfunc){
365                 (*pfunc)(tmp->data, desktop_plugin);
366             }
367             tmp = g_slist_next(tmp);
368         }
369     }
370    
371     now = time(NULL);
372     //fprintf(stderr, "Now  %d\n", now);
373     tmp = desktop_plugin->priv->scene->actors;
374     while (tmp != NULL){
375         actor = tmp->data;
376         if (now >= actor->time_start_animation  
377             && actor->time_start_animation > 0
378             && desktop_plugin->priv->short_timer == 0){
379             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
380             if (desktop_plugin->priv->rich_animation){
381                 actor->time_start_animation = now;
382                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
383             }
384             else {
385                 (*actor->func_change)(actor, desktop_plugin);
386             }
387         }
388         tmp = g_slist_next(tmp);
389     }
390  
391 }
392
393 static gboolean
394 long_timeout (AWallpaperPlugin *desktop_plugin)
395 {
396     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
397     if (desktop_plugin->priv->long_timer == 0 )
398         return FALSE;
399     if (!desktop_plugin->priv->visible){
400         if(desktop_plugin->priv->short_timer != 0){
401             g_source_remove(desktop_plugin->priv->short_timer);
402             desktop_plugin->priv->short_timer = 0;
403         }
404         desktop_plugin->priv->long_timer = 0;
405         return FALSE;
406     }
407   
408
409     run_long_timeout(desktop_plugin);
410     return TRUE; /* keep running this event */
411 }
412
413 static void
414 desktop_plugin_visible_notify (GObject    *object,
415                                           GParamSpec *spec,
416                                           AWallpaperPlugin *desktop_plugin)
417 {
418     gboolean visible;
419     g_object_get (object, "is-on-current-desktop", &visible, NULL);
420     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
421     if (visible){
422         desktop_plugin->priv->visible = TRUE;
423         if (desktop_plugin->priv->long_timer == 0 ){
424             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
425             run_long_timeout(desktop_plugin);
426         }
427     }else{
428         desktop_plugin->priv->visible = FALSE;
429         if (desktop_plugin->priv->long_timer != 0 ){
430             g_source_remove(desktop_plugin->priv->long_timer);
431             desktop_plugin->priv->long_timer = 0;
432         }
433     }
434 }
435 gboolean
436 rich_animation_press(GtkWidget *widget, GdkEvent *event,
437                                             gpointer user_data){
438     fprintf(stderr,"gggggggggggggggggggg2222\n");
439     return FALSE;
440 }    
441 static void
442 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
443 {
444     GtkWidget *rich_animation;
445     gchar           buffer[2048];
446     gchar str[128];
447
448     //fprintf(stderr, "!!!!!!!plugin init \n");
449     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
450     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
451     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
452
453         /* Initialize DBUS */
454     livewp_initialize_dbus(priv);
455
456     priv->desktop_plugin = desktop_plugin;
457     priv->visible = TRUE;
458     priv->short_timer = 0;
459     priv->xapplet = 0;
460     priv->yapplet = 0;
461     priv->scene = NULL;
462 //    priv->theme = g_strdup("Modern");
463 //#if 0 
464     priv->hash_scene_func = g_hash_table_new(g_str_hash, g_str_equal);
465     
466     g_hash_table_insert(priv->hash_scene_func, g_strdup("Berlin"), (gpointer)&init_scene_Berlin);
467     g_hash_table_insert(priv->hash_scene_func, g_strdup("Modern"), (gpointer)&init_scene_Modern);
468     g_hash_table_insert(priv->hash_scene_func, g_strdup("Matrix"), (gpointer)&init_scene_Matrix);
469 //#endif
470     //priv->theme = g_strdup("Modern");
471     desktop_plugin->priv->main_widget = gtk_fixed_new();
472
473     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
474     desktop_plugin->priv->right_corner = NULL;
475     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
476     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
477     if (desktop_plugin->priv->right_corner){
478         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
479         gtk_widget_show (desktop_plugin->priv->right_corner);
480     }
481     /* Create rich animation event */
482     rich_animation = gtk_event_box_new();
483     if(rich_animation){
484         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
485         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
486         gtk_widget_set_size_request(rich_animation, 100, 32);
487         gtk_widget_show (rich_animation);
488         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
489         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
490     }
491 #if 0
492     GtkWidget *label = gtk_label_new ("1111ddddddddd");
493     gtk_widget_set_size_request(label, 95, 30);
494     gtk_widget_show (label);
495     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
496 #endif
497     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
498     g_signal_connect (desktop_plugin, "show-settings",
499                              G_CALLBACK (show_settings), priv);
500     gtk_widget_show (desktop_plugin->priv->main_widget);
501     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
502     init_applet_position(&(priv->xapplet), &(priv->yapplet));
503
504     
505     //fprintf(stderr, "!!!theme = %s\n", priv->theme);
506     //snprintf(str, sizeof(str) - 1, "%s", "init_scene2");
507     //fprintf(stderr, " str = %s\n", str);
508     priv->scene = NULL;
509 #if 0
510     if (!strcmp(priv->theme,"Modern"))
511         init_scene_Modern(desktop_plugin);
512     else if (!strcmp(priv->theme,"Berlin")) 
513         init_scene_Berlin(desktop_plugin);
514     else if (!strcmp(priv->theme, "Matrix"))
515         init_scene_Matrix(desktop_plugin);
516 #endif
517 //    init_scene_theme(desktop_plugin);
518         
519 //    priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
520     /* TODO Move scene to priv */
521     //scene.timer_type = LONG_TIMER_TYPE;
522     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
523                    G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
524
525    
526     //sleep(2);
527 }
528
529 static void
530 lw_applet_finalize (GObject *object)
531 {
532 fprintf(stderr,"lw_applet_finalize\n");
533 /*
534      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
535      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
536      
537      if (priv){
538              livewp_deinitialize_dbus(priv);
539              fprintf(stderr,"finalaze %i\n", priv->long_timer);
540              if (priv->long_timer){
541                 g_source_remove(priv->long_timer);
542                 priv->long_timer = 0;
543              }
544              if (priv->short_timer){
545                 g_source_remove(priv->short_timer);
546                 priv->short_timer = 0;
547              }
548
549              destroy_scene(desktop_plugin);
550      }
551 */
552 }
553
554 static void
555 desktop_widget_finalize (GObject *object)
556 {
557      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
558      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
559      
560      if (priv){
561              livewp_deinitialize_dbus(priv);
562              fprintf(stderr,"finalaze %i\n", priv->long_timer);
563              if (priv->long_timer){
564                 g_source_remove(priv->long_timer);
565                 priv->long_timer = 0;
566              }
567              if (priv->short_timer){
568                 g_source_remove(priv->short_timer);
569                 priv->short_timer = 0;
570              }
571
572              destroy_scene(desktop_plugin);
573      }
574       /* Call the base class's implementation: */
575       G_OBJECT_CLASS (animation_wallpaper_plugin_parent_class)->finalize (object);
576 }
577
578 static void
579 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
580
581     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
582     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
583     GObjectClass *object_class = G_OBJECT_CLASS (klass);
584
585     /* gobject */
586     gobject_class->destroy = (gpointer)lw_applet_finalize;
587     object_class->finalize = desktop_widget_finalize;
588     widget_class->realize = lw_applet_realize;
589     widget_class->expose_event = lw_applet_expose_event;
590
591     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
592
593 }
594
595 static void
596 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
597 }
598
599 Animation_WallpaperPrivate*
600 animation_wallpaper_plugin_new (void)
601 {
602   return g_object_new (Animation_Wallpaper_TYPE_HOME_PLUGIN , NULL);
603 }
604