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