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