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