01e93035c0d5c85d765025c91b85c701a6395f9d
[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 General Public License
11  * as published by the Free Software Foundation; either version 2 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     desktop_plugin->priv->one_in_all_view = get_one_in_all_views_from_config();
71     /* Check one theme in all view mode */
72     if (!desktop_plugin->priv->one_in_all_view || 
73         (desktop_plugin->priv->one_in_all_view && id == 0)){
74             /* Start a new process */
75             desktop_plugin->priv->pid = fork();
76             if (desktop_plugin->priv->pid == 0){
77                 execvp("/usr/bin/livewp", child_argv);
78                 fprintf(stderr,"Problem with new process");
79             }
80     }
81
82     desktop_plugin_visible_notify((GObject *)widget, NULL, desktop_plugin);
83
84     screen = gtk_widget_get_screen (widget);
85     gtk_widget_set_colormap (widget,
86                                 gdk_screen_get_rgba_colormap (screen));
87     gtk_widget_set_app_paintable (widget,
88                                 TRUE);
89     GTK_WIDGET_CLASS (animation_wallpaper_plugin_parent_class)->realize (widget);
90 }
91
92 /* Set position of widget on desktop */
93 static void
94 init_applet_position(gint *xapplet, gint *yapplet)
95 {
96   GSList *position = NULL;
97   gchar *position_key;
98   gchar *modified_key;
99   gchar *modified;
100   GError *error = NULL;
101   gint i;
102   GConfClient   *gconf_client = gconf_client_get_default ();
103   for (i=0;i<4;i++){
104       position_key = g_strdup_printf (GCONF_KEY_POSITION, i);
105       position = gconf_client_get_list (gconf_client,
106                                         position_key,
107                                         GCONF_VALUE_INT,
108                                         NULL);
109       if (position && position->data && position->next->data){
110         *xapplet = GPOINTER_TO_INT (position->data);
111         *yapplet = GPOINTER_TO_INT (position->next->data);
112       }else{
113         position = g_slist_prepend (g_slist_prepend (NULL,
114                                           GINT_TO_POINTER (Ystartposition)),
115                               GINT_TO_POINTER (Xstartposition));
116         gconf_client_set_list (gconf_client,
117                                    position_key,
118                                    GCONF_VALUE_INT,
119                                position,
120                        &error);
121         *xapplet = Xstartposition;
122         *yapplet = Ystartposition;
123       }
124       g_free (position_key);
125       modified = g_strdup_printf ("%i", 0);
126       modified_key = g_strdup_printf (GCONF_KEY_MODIFIED, i);
127       gconf_client_set_string (gconf_client,
128                                modified_key,
129                                modified,
130                                &error);
131       g_free(modified);
132       g_free(modified_key);
133       modified_key = g_strdup_printf (GCONF_KEY_VIEW, i);
134       gconf_client_set_int (gconf_client,
135                                modified_key,
136                                (i+1),
137                                &error);
138       g_free(modified_key);
139
140   }
141   gconf_client_clear_cache(gconf_client);
142   g_object_unref(gconf_client);
143 }
144
145 void
146 desktop_plugin_visible_notify (GObject    *object,
147                                           GParamSpec *spec,
148                                           AWallpaperPlugin *desktop_plugin)
149 {
150     gboolean visible;
151     Animation_WallpaperPrivate *priv = desktop_plugin->priv;
152     g_object_get (object, "is-on-current-desktop", &visible, NULL);
153     fprintf (stderr, "LWP is-on-current-desktop changed. visible: %u", visible); 
154     if (priv){
155              if (priv->one_in_all_view){
156                      if (visible)
157                         send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
158                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW1);
159                      else{
160                         /* start any application on current view */
161                         if ((priv->view + 1) == current_active_view())
162                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
163                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW1);
164                      }
165                      return;
166              }
167              switch (priv->view + 1){
168                 case 1: if (visible)
169                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
170                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW1);
171                         else
172                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
173                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW1);
174                         break;
175                 case 2: if (visible)
176                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
177                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW2);
178                         else
179                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
180                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW2);
181                         break;
182                 case 3: if (visible)
183                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
184                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW3);
185                         else
186                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
187                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW3);
188                         break;
189                 case 4: if (visible)
190                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
191                                           LIVEWP_SIGNAL_PATH, LIVEWP_PLAY_LIVEBG_ON_VIEW4);
192                         else
193                                 send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
194                                           LIVEWP_SIGNAL_PATH, LIVEWP_PAUSE_LIVEBG_ON_VIEW4);
195                         break;
196              }
197
198      }
199     
200 }
201
202 gboolean
203 send_alive_event(Animation_WallpaperPrivate *priv) 
204 {
205     switch (priv->view + 1){
206         case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
207                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW1);
208                 break;
209         case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
210                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW2);
211                 break;
212         case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
213                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW3);
214                 break;
215         case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
216                                   LIVEWP_SIGNAL_PATH, LIVEWP_ALIVE_LIVEBG_ON_VIEW4);
217                 break;
218     }
219
220     return TRUE;
221 }
222
223 static void
224 animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
225 {
226
227     //fprintf(stderr, "!!!!!!!plugin init \n");
228     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
229     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
230     priv->osso = osso_initialize(PACKAGE, VERSION, TRUE, NULL);
231
232         /* Initialize DBUS */
233     livewp_initialize_dbus(priv);
234
235     priv->desktop_plugin = desktop_plugin;
236     priv->window = (GtkWidget *)desktop_plugin;
237     priv->visible = FALSE;
238     priv->short_timer = 0;
239     priv->xapplet = 0;
240     priv->yapplet = 0;
241     priv->scene = NULL;
242     priv->pid = 0;
243     priv->theme = g_strdup("Modern");
244     //priv->theme = g_strdup("Accel");
245     desktop_plugin->priv->main_widget = gtk_fixed_new();
246
247     gtk_widget_set_size_request(desktop_plugin->priv->main_widget, 1, 1);
248     gtk_widget_show (desktop_plugin->priv->main_widget);
249     gtk_container_add (GTK_CONTAINER (desktop_plugin), desktop_plugin->priv->main_widget);
250     init_applet_position(&(priv->xapplet), &(priv->yapplet));
251
252     
253     priv->scene = NULL;
254     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
255                    G_CALLBACK (desktop_plugin_visible_notify), desktop_plugin);
256
257     priv->alive_timer = g_timeout_add(60000*3, (GtkFunction)send_alive_event, priv);  /* One per 3 minute */ 
258 }
259
260 static void
261 lw_applet_finalize (GObject *object)
262 {
263      int status = 0; int wpid; 
264 fprintf(stderr,"lw_applet_finalize\n");
265      AWallpaperPlugin *desktop_plugin = Animation_Wallpaper_HOME_PLUGIN (object);
266      Animation_WallpaperPrivate *priv = desktop_plugin->priv;
267 /*
268      
269      if (priv){
270              livewp_deinitialize_dbus(priv);
271              fprintf(stderr,"finalaze %i\n", priv->long_timer);
272              if (priv->long_timer){
273                 g_source_remove(priv->long_timer);
274                 priv->long_timer = 0;
275              }
276              if (priv->short_timer){
277                 g_source_remove(priv->short_timer);
278                 priv->short_timer = 0;
279              }
280
281              destroy_scene(desktop_plugin);
282      }
283 */    
284     fprintf(stderr,"sddddddddd1111111 %i\n", priv->view); 
285      if (priv){
286              switch (priv->view + 1){
287                 case 1: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
288                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW1);
289                         break;
290                 case 2: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
291                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW2);
292                         break;
293                 case 3: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
294                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW3);
295                         break;
296                 case 4: send_dbus_signal (priv, LIVEWP_SIGNAL_INTERFACE, 
297                                           LIVEWP_SIGNAL_PATH, LIVEWP_STOP_LIVEBG_ON_VIEW4);
298                         break;
299              }
300      }
301     fprintf(stderr,"sddddddddd222222222222\n"); 
302      if(priv->alive_timer > 0) 
303          g_source_remove(priv->alive_timer);
304
305     fprintf(stderr,"sddddddddd222333333222222222\n"); 
306      livewp_deinitialize_dbus(priv);
307
308     fprintf(stderr,"sddddddddd222334333222222222\n"); 
309      if (priv->pid >0){
310     fprintf(stderr,"sddddddddd22233433333333333333333333322222233222222222\n"); 
311              while (TRUE){
312                  wpid = wait(&status);
313                  if (wpid == priv->pid)
314                      break;
315              }
316      }
317 }
318
319 static void
320 desktop_widget_finalize (GObject *object)
321 {
322 fprintf(stderr,"desktop lw_applet_finalize\n");
323       /* Call the base class's implementation: */
324      G_OBJECT_CLASS (animation_wallpaper_plugin_parent_class)->finalize (object);
325 }
326
327 static void
328 animation_wallpaper_plugin_class_init (AWallpaperPluginClass *klass) {
329
330     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
331     GtkObjectClass *gobject_class = GTK_OBJECT_CLASS (klass);
332     GObjectClass *object_class = G_OBJECT_CLASS (klass);
333
334     /* gobject */
335     gobject_class->destroy = (gpointer)lw_applet_finalize;
336     object_class->finalize = desktop_widget_finalize;
337     widget_class->realize = lw_applet_realize;
338
339     g_type_class_add_private (klass, sizeof (Animation_WallpaperPrivate));
340
341 }
342
343 static void
344 animation_wallpaper_plugin_class_finalize (AWallpaperPluginClass *class) {
345 }
346
347 Animation_WallpaperPrivate*
348 animation_wallpaper_plugin_new (void)
349 {
350   return g_object_new (Animation_Wallpaper_TYPE_HOME_PLUGIN , NULL);
351 }
352