added function kill to conky
[livewp] / applet / src / livewp-main.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-scene.h"
27 #include "livewp-settings.h"
28 #include "livewp-home-widget.h"
29 /*******************************************************************************/
30
31 void set_live_bg (Display *display, Window xwindow, int mode);
32 void quit_from_program (Animation_WallpaperPrivate *priv);
33 void view_state_changed (Animation_WallpaperPrivate *priv);
34 static gboolean short_timeout (AWallpaperPlugin *desktop_plugin);
35 void run_long_timeout(AWallpaperPlugin *desktop_plugin);
36 gboolean long_timeout (AWallpaperPlugin *desktop_plugin);
37 void conky_visible(AWallpaperPlugin *desktop_plugin);
38
39 void 
40 set_live_bg (Display *display, Window xwindow, int mode)
41 {
42         Atom atom;
43
44         atom = XInternAtom (display, "_HILDON_LIVE_DESKTOP_BACKGROUND", False);
45         fprintf (stderr, "XID: 0x%x\n", (unsigned)xwindow);
46
47         XChangeProperty (display,
48                       xwindow,
49                        atom,
50                        XA_INTEGER, 32, PropModeReplace,
51                        (unsigned char *) &mode, 1);
52 }
53 /*******************************************************************************/
54
55 void
56 view_state_changed (Animation_WallpaperPrivate *priv)
57 {
58    fprintf(stderr," VIew %i State %i\n", priv->view, priv->visible);
59    if (priv->visible){  
60        /* ToDo  make undependent function */
61        if (!strcmp(priv->theme,"Video")){
62            if (priv->pipeline){
63                gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
64            }else{
65                init_scene_Video(priv->desktop_plugin);
66            }
67        }
68        if (!priv->running){
69             if (g_hash_table_lookup(priv->hash_theme, "exec_path")){
70                 init_scene_External(priv->desktop_plugin);
71             }
72        }
73        if (!strcmp(priv->theme,"Conky")){
74            conky_visible(priv->desktop_plugin);
75        }
76
77        if (priv->long_timer == 0 ){
78             priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, priv->desktop_plugin);
79             run_long_timeout(priv->desktop_plugin);
80        }else {
81             run_long_timeout(priv->desktop_plugin);
82        }
83    }else{
84         if (priv->pipeline){
85            gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
86            return;
87         }
88         if (priv->long_timer != 0 ){
89             g_source_remove(priv->long_timer);
90             priv->long_timer = 0;
91         }
92    }
93 }
94 /*******************************************************************************/
95 gboolean
96 check_alive_event(Animation_WallpaperPrivate *priv) 
97 {
98     if ( time(NULL) - priv->last_alive_event > 10*60) /* If last event later 10 minute */
99         quit_from_program(priv);
100     return TRUE;
101 }
102
103 /*******************************************************************************/
104 void
105 view_changed (GConfClient *client, guint cnxn_id,
106                  GConfEntry *entry, Animation_WallpaperPrivate *priv)
107 {
108    if (entry->value->type == GCONF_VALUE_INT) {
109         if (priv->view == gconf_value_get_int(entry->value)){
110             if (priv->visible != TRUE){
111                 priv->visible = TRUE;
112                 view_state_changed(priv);
113             }
114         }else{
115             if (priv->visible != FALSE){
116                 priv->visible = FALSE;
117                 view_state_changed(priv);
118             }
119         }
120     }
121 }
122 /*******************************************************************************/
123 void
124 quit_from_program (Animation_WallpaperPrivate *priv)
125 {
126      fprintf(stderr,"quit_from_program\n");
127      if (priv){
128              livewp_deinitialize_dbus(priv);
129          if (priv->view_notify != 0) { 
130             gconf_client_notify_remove (gconf_client_get_default (), priv->view_notify);
131             priv->view_notify = 0;
132          }
133              if (priv->long_timer){
134                     g_source_remove(priv->long_timer);
135                 priv->long_timer = 0;
136              }
137              if (priv->short_timer){
138                     g_source_remove(priv->short_timer);
139                     priv->short_timer = 0;
140              }
141              destroy_scene(priv->desktop_plugin);
142          osso_deinitialize(priv->osso);
143          //g_hash_table_destroy(priv->hash_scene_func);
144          g_free(priv->desktop_plugin);
145          priv->desktop_plugin = NULL;
146          g_free(priv);
147          priv = NULL;
148      } 
149      fprintf(stderr,"end of quit_from_program\n");
150      gtk_main_quit();;
151 }
152 /*******************************************************************************/
153 static gboolean
154 short_timeout (AWallpaperPlugin *desktop_plugin)
155 {
156     //gint daytime = get_daytime();
157     GSList * tmp;
158     void (*pfunc)(gpointer, gpointer);
159     time_t now;
160     Actor *actor;
161     gboolean stop_flag = TRUE;
162
163     if (!desktop_plugin->priv->visible || !desktop_plugin->priv->rich_animation){
164         desktop_plugin->priv->short_timer = 0;
165         return FALSE;
166     }
167
168     now = time(NULL);
169     //scene.daytime = daytime;
170     /* fprintf(stderr, "Short timer %d\n", now); */
171     tmp = desktop_plugin->priv->scene->actors;
172     while (tmp != NULL){
173            actor = tmp->data;
174            if (now >= actor->time_start_animation  
175                && actor->time_start_animation > 0
176                /* && now - actor->time_start_animation <= actor->duration_animation*/){
177                 pfunc = actor->func_change;
178                 if (pfunc){ 
179                     (*pfunc)(actor, desktop_plugin);
180                     stop_flag = FALSE;
181                 }
182             }
183             tmp = g_slist_next(tmp);
184     }
185     if (stop_flag){
186          desktop_plugin->priv->short_timer = 0;
187          return FALSE;
188     }else
189          return TRUE; /* keep running this event */
190 }
191
192 void
193 run_long_timeout(AWallpaperPlugin *desktop_plugin)
194 {
195
196     gint daytime = get_daytime();
197     GSList * tmp;
198     void (*pfunc)(gpointer, gpointer);
199     time_t now;
200     Actor *actor;
201
202     if (!desktop_plugin->priv->scene)
203         return;
204     /*ifprintf(stderr, "!!!run long timeout short_timer=%d\n", desktop_plugin->priv->short_timer); */
205     if (desktop_plugin->priv->scene->daytime != daytime){
206         desktop_plugin->priv->scene->daytime = daytime;
207         tmp = desktop_plugin->priv->scene->actors;
208         while (tmp != NULL){
209             //change_actor(tmp->data);
210             pfunc =((Actor*)tmp->data)->func_change;
211             if (pfunc){
212                 (*pfunc)(tmp->data, desktop_plugin);
213             }
214             tmp = g_slist_next(tmp);
215         }
216     }
217    
218     now = time(NULL);
219     //fprintf(stderr, "Now  %d\n", now);
220     tmp = desktop_plugin->priv->scene->actors;
221     while (tmp != NULL){
222         actor = tmp->data;
223         if (now >= actor->time_start_animation  
224             && actor->time_start_animation > 0
225             && desktop_plugin->priv->short_timer == 0){
226             /* fprintf(stderr, "start short timer act = %s %d\n", actor->name, now); */
227             if (desktop_plugin->priv->rich_animation){
228                 actor->time_start_animation = now;
229                 desktop_plugin->priv->short_timer = g_timeout_add(SHORT_TIMER, (GtkFunction)short_timeout, desktop_plugin);
230             }
231             else {
232                 (*actor->func_change)(actor, desktop_plugin);
233             }
234         }
235         tmp = g_slist_next(tmp);
236     }
237  
238 }
239
240 gboolean
241 long_timeout (AWallpaperPlugin *desktop_plugin)
242 {
243     GstFormat fmt = GST_FORMAT_TIME;
244     gint64 p;
245
246     /*fprintf(stderr, "long_timeout %i\n", desktop_plugin->priv->long_timer); */
247     if (desktop_plugin->priv->pipeline){
248         if (!desktop_plugin->priv->visible){
249                 gst_element_query_position(desktop_plugin->priv->pipeline, &fmt, &p);
250                 desktop_plugin->priv->theme_int_parametr1 = p / GST_SECOND;
251                 gst_element_set_state (desktop_plugin->priv->pipeline, GST_STATE_NULL);
252                 gst_object_unref (GST_OBJECT (desktop_plugin->priv->pipeline));
253                 desktop_plugin->priv->pipeline = NULL;
254         }
255         return TRUE;  
256     }
257
258     if (desktop_plugin->priv->long_timer == 0 )
259         return FALSE;
260     if (!desktop_plugin->priv->visible){
261         if(desktop_plugin->priv->short_timer != 0){
262             g_source_remove(desktop_plugin->priv->short_timer);
263             desktop_plugin->priv->short_timer = 0;
264         }
265         desktop_plugin->priv->long_timer = 0;
266         return FALSE;
267     }
268
269     run_long_timeout(desktop_plugin);
270     return TRUE; /* keep running this event */
271 }
272
273 /*******************************************************************************/
274 void
275 create_xwindow (Animation_WallpaperPrivate *priv){
276     GtkWidget *main_widget = NULL;
277     /* Create Main GUI */
278     main_widget = hildon_window_new ();
279     priv->window = main_widget;
280     gtk_window_set_title(GTK_WINDOW(main_widget), PACKAGE);
281     gtk_window_fullscreen(GTK_WINDOW(main_widget));
282     gtk_widget_show_all(GTK_WIDGET(main_widget));
283     set_live_bg(GDK_WINDOW_XDISPLAY (main_widget->window), GDK_WINDOW_XID (main_widget->window), priv->view);
284 }
285 /*******************************************************************************/
286 int
287 main(int argc, char *argv[])
288 {
289     HildonProgram       *app;
290     int view = 1;
291
292      
293     if (argc == 2) 
294         view = atoi(argv[1]);
295     if (view < 1 || view > 4)
296         view = 1;
297
298     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
299     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
300     desktop_plugin->priv = priv; 
301
302     gst_init (NULL, NULL);
303
304     /* Ininitializing */
305     hildon_gtk_init (&argc, &argv);
306     app = HILDON_PROGRAM (hildon_program_get_instance());
307     g_set_application_name (PACKAGE);
308  
309     priv->osso = osso_initialize("org.maemo.livewp", VERSION, TRUE, NULL);
310     if(!priv->osso){
311         fprintf(stderr,"osso_initialize failed\n");
312         return 1;
313     }
314
315 #ifdef ENABLE_NLS
316     setlocale(LC_ALL, "");
317     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
318     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
319     textdomain(GETTEXT_PACKAGE);
320 #endif
321
322     priv->scene = NULL;
323     priv->pipeline = NULL;
324     priv->podpid = -1;
325     priv->running = FALSE;
326     /* fprintf(stderr,"XWINDOW %i\n",GDK_WINDOW_XID (main_widget->window)); */
327     priv->desktop_plugin = desktop_plugin;
328     priv->view = view;
329
330     if (current_active_view() == view) 
331         priv->visible = TRUE;
332     else 
333         priv->visible = FALSE;
334
335     fill_priv(priv);
336     
337     create_xwindow(priv);
338     /* Initialize DBUS */
339     livewp_initialize_dbus(priv);
340         
341     priv->view_notify = 0; 
342     /* fprintf(stderr,"VISIBLE %i %i\n",priv->visible, current_active_view()); */
343     gconf_client_add_dir(gconf_client_get_default (), "/apps/osso/hildon-desktop/views", GCONF_CLIENT_PRELOAD_NONE, NULL);
344     priv->view_notify = gconf_client_notify_add(gconf_client_get_default (),"/apps/osso/hildon-desktop/views/current",
345                                                 (GConfClientNotifyFunc)view_changed, priv, NULL, NULL);
346
347     /* Set start position of Video to zero */
348     if (!strcmp(priv->theme,"Video"))
349         priv->theme_int_parametr1 = 0;
350
351
352     init_scene_theme(desktop_plugin);
353         
354     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
355
356     priv->alive_timer = g_timeout_add(60000*10, (GtkFunction)check_alive_event, priv);  /* One per 10 minute */ 
357     gtk_main();
358     return 0;
359 }