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