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