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