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