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