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