continued restructuring
[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
166 void
167 create_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin) 
168 {
169   GtkWidget *ha = NULL;
170   GdkPixbuf *pixbuf = NULL;
171   GtkWidget *image = NULL;
172   gchar     *str = NULL;
173
174   ha = hildon_animation_actor_new();
175   str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
176                         desktop_plugin->priv->theme, actor->filename);
177   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
178                                              actor->width, 
179                                              actor->height, 
180                                              NULL);
181   /*fprintf(stderr, "create_hildon_actor %s %s\n", actor->name, str);*/
182   if (str)
183       g_free(str);
184   if (pixbuf){
185       image = gtk_image_new_from_pixbuf (pixbuf);
186       g_object_unref(G_OBJECT(pixbuf));
187   }
188   if (image){
189     g_signal_connect(G_OBJECT(image), "expose_event",
190                            G_CALLBACK(expose_event), pixbuf);
191     gtk_container_add (GTK_CONTAINER (ha), image);
192   }  
193   realize(ha);
194   gtk_widget_show_all(ha);
195   
196   /* TO DO check it */
197   /*  gdk_flush (); */
198
199   //g_object_set_data(G_OBJECT(ha), "image", image);
200   actor->image = image;
201   hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), GTK_WINDOW(desktop_plugin));
202   actor->widget = ha;
203   set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
204   set_actor_scale(actor, (double)actor->scale/100, (double)actor->scale/100);
205   set_actor_visible(actor, actor->visible);
206 }
207
208
209 void
210 change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
211 {
212     GtkWidget *image = NULL;
213     GdkPixbuf *pixbuf = NULL;
214     gchar     *str = NULL;
215
216     str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
217                             desktop_plugin->priv->theme, actor->filename);
218  
219     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
220                                                actor->width, 
221                                                actor->height, 
222                                                NULL);
223     if(str)
224         g_free(str);
225     if (pixbuf){
226         image = gtk_image_new_from_pixbuf (pixbuf);
227         g_object_unref(G_OBJECT(pixbuf));
228     }
229     if (image){ 
230         g_signal_connect(G_OBJECT(image), "expose_event",
231                                        G_CALLBACK(expose_event), pixbuf);
232         //if (g_object_get_data(G_OBJECT(actor->widget), "image")){
233         if (actor->image){
234             gtk_container_remove(GTK_CONTAINER(actor->widget), actor->image);  
235         }
236         //g_object_set_data(G_OBJECT(actor->widget), "image", image);
237         actor->image = image;
238         gtk_container_add (GTK_CONTAINER (actor->widget), image);
239         realize(actor->widget);
240         gtk_widget_show_all(actor->widget);
241         /* TO DO check it */
242        /*  gdk_flush (); */
243
244
245     }
246
247 }
248
249
250 void 
251 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
252 {
253     gint y0 = 365;// - ÑƒÑ€Ð¾Ð²ÐµÐ½ÑŒ Ð³Ð¾Ñ€Ð¸Ð·Ð¾Ð½Ñ‚а
254     *x = (int)(azm * 800) - 64;
255     *y = (int)((1 - alt) * y0) - 64;
256     //fprintf(stderr, "sun pos alt=%f azm=%f x=%d y=%d\n", alt, azm, *x, *y);
257 }
258 #if 0
259 static void 
260 change_actor(GtkWidget * actor)
261 {
262     char * name;
263     gint x, y, daytime, scale;
264     gdouble sc;
265     double alt, azm;
266
267     GtkWidget *image;
268     GdkPixbuf *pixbuf;
269
270     void (*pfunc)(gpointer, gpointer);
271
272     name = g_object_get_data(G_OBJECT(actor), "name");
273     fprintf(stderr, "change actor %s\n", name);
274     if (name == "sun"){
275         pfunc = g_object_get_data(G_OBJECT(actor), "func");
276         if (pfunc)
277             (*pfunc)(actor, g_strdup(name));
278         daytime = get_daytime();
279         if (daytime != TIME_NIGHT){
280             hildon_animation_actor_set_show(actor, 1);
281             get_sun_pos(&alt, &azm);
282             get_sun_screen_pos(alt, azm, &x, &y);
283             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
284         }
285     }
286     
287     if (name == "cloud1"){
288         x = g_object_get_data(G_OBJECT(actor), "x");
289         y = g_object_get_data(G_OBJECT(actor), "y");
290         scale = g_object_get_data(G_OBJECT(actor), "scale");
291
292         /* Start */
293         image = g_object_get_data(G_OBJECT(actor), "image");
294         
295         gtk_container_remove(actor, image);  
296         pixbuf = gdk_pixbuf_new_from_file_at_size ("/usr/share/livewp/theme/Modern/sun.png", 
297                                              200, 
298                                              200, 
299                                              NULL);
300         if (pixbuf){
301               image = gtk_image_new_from_pixbuf (pixbuf);
302               g_object_unref(G_OBJECT(pixbuf));
303         }
304         g_signal_connect(G_OBJECT(image), "expose_event",
305                                    G_CALLBACK(expose_event), pixbuf);
306         gtk_container_add (GTK_CONTAINER (actor), image);
307         realize(actor);
308         gtk_widget_show_all(actor);
309         /* End*/
310
311             
312         x += 40;
313         y -= 20;
314         scale -= 10;
315         if (x > 500){
316             x = 400;
317             y = 150;
318             sc = 1;
319         }
320         sc = (double)scale / 100;
321         hildon_animation_actor_set_scale(actor, sc, sc);
322         fprintf(stderr, "cloud x=%d y=%d scale=%f", x, y, sc);
323         actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
324         g_object_set_data(G_OBJECT(actor), "x", x);
325         g_object_set_data(G_OBJECT(actor), "y", y);
326         g_object_set_data(G_OBJECT(actor), "scale", scale);
327     }
328
329 }
330 #endif
331 static gboolean
332 short_timeout (AWallpaperPlugin *desktop_plugin)
333 {
334     //gint daytime = get_daytime();
335     GSList * tmp;
336     void (*pfunc)(gpointer, gpointer);
337     time_t now;
338     Actor *actor;
339     gboolean stop_flag = TRUE;
340
341 if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
342         desktop_plugin->priv->short_timer = 0;
343         return FALSE;
344     }
345
346     now = time(NULL);
347     //scene.daytime = daytime;
348     /* fprintf(stderr, "Short timer %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                /* && now - actor->time_start_animation <= actor->duration_animation*/){
355                 pfunc = actor->func_change;
356                 if (pfunc){ 
357                     (*pfunc)(actor, desktop_plugin);
358                     stop_flag = FALSE;
359                 }
360             }
361             tmp = g_slist_next(tmp);
362     }
363     if (stop_flag){
364          desktop_plugin->priv->short_timer = 0;
365          return FALSE;
366     }else
367          return TRUE; /* keep running this event */
368 }
369
370 void
371 run_long_timeout(AWallpaperPlugin *desktop_plugin)
372 {
373
374     gint daytime = get_daytime();
375     GSList * tmp;
376     void (*pfunc)(gpointer, gpointer);
377     time_t now;
378     Actor *actor;
379
380
381     //fprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer);
382     if (desktop_plugin->priv->scene->daytime != daytime){
383         desktop_plugin->priv->scene->daytime = daytime;
384         tmp = desktop_plugin->priv->scene->actors;
385         while (tmp != NULL){
386             //change_actor(tmp->data);
387             pfunc =((Actor*)tmp->data)->func_change;
388             if (pfunc){
389                 (*pfunc)(tmp->data, desktop_plugin);
390             }
391             tmp = g_slist_next(tmp);
392         }
393     }
394    
395     now = time(NULL);
396     //fprintf(stderr, "Now  %d\n", now);
397     tmp = desktop_plugin->priv->scene->actors;
398     while (tmp != NULL){
399         actor = tmp->data;
400         if (now >= actor->time_start_animation  
401             && actor->time_start_animation > 0
402             && desktop_plugin->priv->short_timer == 0){
403             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
404             if (desktop_plugin->priv->rich_animation){
405                 actor->time_start_animation = now;
406                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
407             }
408             else {
409                 (*actor->func_change)(actor, desktop_plugin);
410             }
411         }
412         tmp = g_slist_next(tmp);
413     }
414  
415 }
416
417 static gboolean
418 long_timeout (AWallpaperPlugin *desktop_plugin)
419 {
420     /* fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
421     if (desktop_plugin->priv->long_timer == 0 )
422         return FALSE;
423     if (!desktop_plugin->priv->visible){
424         if(desktop_plugin->priv->short_timer != 0){
425             g_source_remove(desktop_plugin->priv->short_timer);
426             desktop_plugin->priv->short_timer = 0;
427         }
428         desktop_plugin->priv->long_timer = 0;
429         return FALSE;
430     }
431   
432
433     run_long_timeout(desktop_plugin);
434     return TRUE; /* keep running this event */
435 }
436
437 static void
438 desktop_plugin_visible_notify (GObject    *object,
439                                           GParamSpec *spec,
440                                           AWallpaperPlugin *desktop_plugin)
441 {
442     gboolean visible;
443     g_object_get (object, "is-on-current-desktop", &visible, NULL);
444     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
445     if (visible){
446         desktop_plugin->priv->visible = TRUE;
447         if (desktop_plugin->priv->long_timer == 0 ){
448             desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
449             run_long_timeout(desktop_plugin);
450         }
451     }else{
452         desktop_plugin->priv->visible = FALSE;
453         if (desktop_plugin->priv->long_timer != 0 ){
454             g_source_remove(desktop_plugin->priv->long_timer);
455             desktop_plugin->priv->long_timer = 0;
456         }
457     }
458 }
459
460 gboolean
461 rich_animation_press(GtkWidget *widget, GdkEvent *event,
462                                             gpointer user_data){
463     fprintf(stderr,"gggggggggggggggggggg2222\n");
464     return FALSE;
465 }    
466 static void
467 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
468 {
469     GtkWidget *rich_animation;
470     gchar           buffer[2048];
471
472
473     //fprintf(stderr, "!!!!!!!plugin init \n");
474     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
475     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
476     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
477
478     /* Load config */
479     read_config(priv);
480     /* Initialize DBUS */
481     livewp_initialize_dbus(priv);
482
483     priv->desktop_plugin = desktop_plugin;
484     priv->visible = TRUE;
485     priv->short_timer = 0;
486     priv->xapplet = 0;
487     priv->yapplet = 0;
488     //priv->theme = g_strdup("Modern");
489     desktop_plugin->priv->main_widget = gtk_fixed_new();
490
491     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 100, 32);
492     desktop_plugin->priv->right_corner = NULL;
493     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, "town0_right_corner.png");
494     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
495     if (desktop_plugin->priv->right_corner){
496         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
497         gtk_widget_show (desktop_plugin->priv->right_corner);
498     }
499     /* Create rich animation event */
500     rich_animation = gtk_event_box_new();
501     if(rich_animation){
502         gtk_widget_set_events(rich_animation, GDK_BUTTON_PRESS_MASK);
503         gtk_event_box_set_visible_window(GTK_EVENT_BOX(rich_animation), FALSE);
504         gtk_widget_set_size_request(rich_animation, 100, 32);
505         gtk_widget_show (rich_animation);
506         g_signal_connect(rich_animation, "button-press-event", G_CALLBACK(rich_animation_press), desktop_plugin);
507         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), rich_animation, 0, 0);
508     }
509 /*
510     GtkWidget *label = gtk_label_new ("ddddddddddddd"); 
511     gtk_widget_set_size_request(label, 95, 30);
512     gtk_widget_show (label);
513     gtk_container_add (GTK_CONTAINER (desktop_plugin), label);
514     gtk_fixed_put(GTK_FIXED(widget), label, 0, 0);
515 */
516     hd_home_plugin_item_set_settings (HD_HOME_PLUGIN_ITEM (desktop_plugin), TRUE);
517     g_signal_connect (desktop_plugin, "show-settings",
518                              G_CALLBACK (show_settings), priv);
519     gtk_widget_show (desktop_plugin->priv->main_widget);
520     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
521     init_applet_position(&(priv->xapplet), &(priv->yapplet));
522
523     
524     fprintf(stderr, "!!!theme = %s\n", priv->theme);
525     priv->scene = NULL;
526     if (!strcmp(priv->theme,"Modern"))
527         init_scene(desktop_plugin);
528     else if (!strcmp(priv->theme,"Berlin")) 
529         init_scene1(desktop_plugin);
530     else if (!strcmp(priv->theme, "Matrix"))
531         init_scene2(desktop_plugin);
532     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
533     /* TODO Move scene to priv */
534     //scene.timer_type = LONG_TIMER_TYPE;
535     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
536                      G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
537
538    
539     //sleep(2);
540 }
541
542 static void
543 lw_applet_finalize (GObject *object)
544 {
545      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
546      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
547      
548      fprintf(stderr,"finalaze %i\n", priv->long_timer);
549      if (priv->long_timer){
550         g_source_remove(priv->long_timer);
551         priv->long_timer = 0;
552      }
553      if (priv->short_timer){
554         g_source_remove(priv->short_timer);
555         priv->short_timer = 0;
556      }
557
558      destroy_scene(desktop_plugin);
559 }
560
561 static void
562 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
563
564     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
565     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
566
567     /* gobject */
568     gobject_class->destroy = (gpointer)lw_applet_finalize;
569     widget_class->realize = lw_applet_realize;
570     widget_class->expose_event = lw_applet_expose_event;
571
572     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
573
574 }
575
576 static void
577 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
578 }
579
580