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