refactoried of code
[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 #include <unistd.h>
33 #include <sys/wait.h>
34
35 #define GCONF_KEY_POSITION "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/position"
36 #define GCONF_KEY_MODIFIED "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/modified"
37 #define GCONF_KEY_VIEW     "/apps/osso/hildon-desktop/applets/livewp-home-widget.desktop-%i/view"
38
39 HD_DEFINE_PLUGIN_MODULE (AWallpaperPlugin, animation_wallpaper_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
40 #define Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
41                                                           Animation_Wallpaper_TYPE_HOME_PLUGIN,\
42                                                           Animation_WallpaperPrivate))
43 static void
44 lw_applet_realize (GtkWidget *widget)
45 {
46     GdkScreen *screen;
47     gchar * strid; 
48     gint id; 
49     AWallpaperPlugin *desktop_plugin = (AWallpaperPlugin *)widget;
50     char* child_argv[] =  {"/usr/bin/livewp", "0", (char *) 0 };
51
52
53     strid = g_strdup(hd_plugin_item_get_plugin_id (HD_PLUGIN_ITEM (widget)));
54
55     id = strid[strlen(strid)-1] - '0';
56     desktop_plugin->priv->view = id;
57     g_free(strid); 
58     
59
60     switch (id){
61             case 0: child_argv[1] = "1";
62                 break;
63             case 1: child_argv[1] = "2";
64                 break;
65             case 2: child_argv[1] = "3";
66                 break;
67             case 3: child_argv[1] = "4";
68                 break;
69     }
70
71     desktop_plugin->priv->pid = fork();
72     if (desktop_plugin->priv->pid == 0){
73         execvp("/usr/bin/livewp", child_argv);
74         fprintf(stderr,"Problem with new process");
75     }
76
77     desktop_plugin_visible_notify((GObject *)widget, NULL, desktop_plugin);
78
79 #if 0
80     /* Load config */
81     read_config(desktop_plugin->priv);
82
83     init_scene_theme(desktop_plugin);
84         
85     desktop_plugin->priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
86 #endif
87     screen = gtk_widget_get_screen (widget);
88     gtk_widget_set_colormap (widget,
89                                 gdk_screen_get_rgba_colormap (screen));
90     gtk_widget_set_app_paintable (widget,
91                                 TRUE);
92     GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
93 }
94
95 /* Set position of widget on desktop */
96 static void
97 init_applet_position(gint *xapplet, gint *yapplet)
98 {
99   GSList *position = NULL;
100   gchar *position_key;
101   gchar *modified_key;
102   gchar *modified;
103   GError *error = NULL;
104   gint i;
105   GConfClient   *gconf_client = gconf_client_get_default ();
106   for (i=0;i<4;i++){
107       position_key = g_strdup_printf (GCONF_KEY_POSITION, i);
108       position = gconf_client_get_list (gconf_client,
109                                         position_key,
110                                         GCONF_VALUE_INT,
111                                         NULL);
112       if (position && position->data && position->next->data){
113         *xapplet = GPOINTER_TO_INT (position->data);
114         *yapplet = GPOINTER_TO_INT (position->next->data);
115       }else{
116         position = g_slist_prepend (g_slist_prepend (NULL,
117                                           GINT_TO_POINTER (Ystartposition)),
118                               GINT_TO_POINTER (Xstartposition));
119         gconf_client_set_list (gconf_client,
120                                    position_key,
121                                    GCONF_VALUE_INT,
122                                position,
123                        &error);
124         *xapplet = Xstartposition;
125         *yapplet = Ystartposition;
126       }
127       g_free (position_key);
128       modified = g_strdup_printf ("%i", 0);
129       modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, i);
130       gconf_client_set_string (gconf_client,
131                                modified_key,
132                                modified,
133                                &error);
134       g_free(modified);
135       g_free(modified_key);
136       modified_key = g_strdup_printf (GCONF_KEY_VIEW, i);
137       gconf_client_set_int (gconf_client,
138                                modified_key,
139                                (i+1),
140                                &error);
141       g_free(modified_key);
142
143   }
144   gconf_client_clear_cache(gconf_client);
145   g_object_unref(gconf_client);
146 }
147
148 void
149 desktop_plugin_visible_notify (GObject    *object,
150                                           GParamSpec *spec,
151                                           AWallpaperPlugin *desktop_plugin)
152 {
153     gboolean visible;
154     Animation_WallpaperPrivate *priv = desktop_plugin->priv;
155     g_object_get (object, "is-on-current-desktop", &visible, NULL);
156     /* fprintf (stderr, "is-on-current-desktop changed. visible: %u", visible); */
157     if (priv){
158              switch (priv->view + 1){
159                 case 1: if (visible)
160                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
161                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW1);
162                         else
163                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
164                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW1);
165                         break;
166                 case 2: if (visible)
167                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
168                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW2);
169                         else
170                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
171                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW2);
172                         break;
173                 case 3: if (visible)
174                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
175                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW3);
176                         else
177                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
178                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW3);
179                         break;
180                 case 4: if (visible)
181                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
182                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW4);
183                         else
184                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
185                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW4);
186                         break;
187              }
188      }
189
190 }
191
192 gboolean
193 send_alive_event(Animation_WallpaperPrivate *priv) 
194 {
195     switch (priv->view + 1){
196         case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
197                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW1);
198                 break;
199         case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
200                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW2);
201                 break;
202         case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
203                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW3);
204                 break;
205         case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
206                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW4);
207                 break;
208     }
209
210     return TRUE;
211 }
212
213 static void
214 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
215 {
216
217     //fprintf(stderr, "!!!!!!!plugin init \n");
218     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
219     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
220     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
221
222         /* Initialize DBUS */
223     livewp_initialize_dbus(priv);
224
225     priv->desktop_plugin = desktop_plugin;
226     priv->window = (GtkWidget *)desktop_plugin;
227     priv->visible = FALSE;
228     priv->short_timer = 0;
229     priv->xapplet = 0;
230     priv->yapplet = 0;
231     priv->scene = NULL;
232     priv->theme = g_strdup("Modern");
233     //priv->theme = g_strdup("Accel");
234     desktop_plugin->priv->main_widget = gtk_fixed_new();
235
236     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 1, 1);
237     gtk_widget_show (desktop_plugin->priv->main_widget);
238     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
239     init_applet_position(&(priv->xapplet), &(priv->yapplet));
240
241     
242     priv->scene = NULL;
243     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
244                    G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
245
246     priv->alive_timer = g_timeout_add(60000*3, (GtkFunction)send_alive_event, priv);  /* One per 3 minute */ 
247 }
248
249 static void
250 lw_applet_finalize (GObject *object)
251 {
252      int status = 0; int wpid; 
253 fprintf(stderr,"lw_applet_finalize\n");
254      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
255      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
256 /*
257      
258      if (priv){
259              livewp_deinitialize_dbus(priv);
260              fprintf(stderr,"finalaze %i\n", priv->long_timer);
261              if (priv->long_timer){
262                 g_source_remove(priv->long_timer);
263                 priv->long_timer = 0;
264              }
265              if (priv->short_timer){
266                 g_source_remove(priv->short_timer);
267                 priv->short_timer = 0;
268              }
269
270              destroy_scene(desktop_plugin);
271      }
272 */    
273      
274      if (priv){
275              switch (priv->view + 1){
276                 case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
277                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW1);
278                         break;
279                 case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
280                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW2);
281                         break;
282                 case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
283                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW3);
284                         break;
285                 case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
286                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW4);
287                         break;
288              }
289      }
290      if(priv->alive_timer > 0) 
291          g_source_remove(priv->alive_timer);
292
293      livewp_deinitialize_dbus(priv);
294      while (TRUE){
295          wpid = wait(&status);
296          if (wpid == priv->pid)
297              break;
298      }
299 }
300
301 static void
302 desktop_widget_finalize (GObject *object)
303 {
304       /* Call the base class's implementation: */
305      G_OBJECT_CLASS (animation_wallpaper_plugin_parent_class)->finalize (object);
306 }
307
308 static void
309 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
310
311     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
312     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
313     GObjectClass *object_class = G_OBJECT_CLASS (klass);
314
315     /* gobject */
316     gobject_class->destroy = (gpointer)lw_applet_finalize;
317     object_class->finalize = desktop_widget_finalize;
318     widget_class->realize = lw_applet_realize;
319
320     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
321
322 }
323
324 static void
325 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
326 }
327
328 Animation_WallpaperPrivate*
329 animation_wallpaper_plugin_new (void)
330 {
331   return g_object_new (Animation_Wallpaper_TYPE_HOME_PLUGIN , NULL);
332 }
333