dd43f03eac077accc39d62f83aa39a763e49fc9d
[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
35 static 
36 void set_live_bg (Display *display, Window xwindow, int mode)
37 {
38         Atom atom;
39
40         atom = XInternAtom (display, "_HILDON_LIVE_DESKTOP_BACKGROUND", False);
41         fprintf (stderr, "XID: 0x%x\n", (unsigned)xwindow);
42
43         XChangeProperty (display,
44                        xwindow,
45                        atom,
46                        XA_INTEGER, 32, PropModeReplace,
47                        (unsigned char *) &mode, 1);
48 }
49 /*******************************************************************************/
50
51 void
52 view_state_changed (Animation_WallpaperPrivate *priv)
53 {
54    fprintf(stderr," VIew %i State %i\n", priv->view, priv->visible);
55    if (priv->visible){
56        if (!priv->running){
57             if (!strcmp(priv->theme, "Xsnow")){
58                 init_scene_Xsnow(priv->desktop_plugin);
59             }
60        }
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->long_timer == 0 ){
69             priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, priv->desktop_plugin);
70             run_long_timeout(priv->desktop_plugin);
71        }else {
72             run_long_timeout(priv->desktop_plugin);
73        }
74    }else{
75         if (priv->pipeline){
76            gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
77            return;
78         }
79         if (priv->long_timer != 0 ){
80             g_source_remove(priv->long_timer);
81             priv->long_timer = 0;
82         }
83    }
84 }
85 /*******************************************************************************/
86 gboolean
87 check_alive_event(Animation_WallpaperPrivate *priv) 
88 {
89     if ( time(NULL) - priv->last_alive_event > 10*60) /* If last event later 10 minute */
90         quit_from_program(priv);
91     return TRUE;
92 }
93
94 /*******************************************************************************/
95 void
96 view_changed (GConfClient *client, guint cnxn_id,
97                  GConfEntry *entry, Animation_WallpaperPrivate *priv)
98 {
99    if (entry->value->type == GCONF_VALUE_INT) {
100         if (priv->view == gconf_value_get_int(entry->value)){
101             if (priv->visible != TRUE){
102                 priv->visible = TRUE;
103                 view_state_changed(priv);
104             }
105         }else{
106             if (priv->visible != FALSE){
107                 priv->visible = FALSE;
108                 view_state_changed(priv);
109             }
110         }
111     }
112 }
113 /*******************************************************************************/
114 void
115 quit_from_program (Animation_WallpaperPrivate *priv)
116 {
117      fprintf(stderr,"quit_from_program\n");
118      if (priv){
119              livewp_deinitialize_dbus(priv);
120          if (priv->view_notify != 0) { 
121             gconf_client_notify_remove (gconf_client_get_default (), priv->view_notify);
122             priv->view_notify = 0;
123          }
124              if (priv->long_timer){
125                     g_source_remove(priv->long_timer);
126                 priv->long_timer = 0;
127              }
128              if (priv->short_timer){
129                     g_source_remove(priv->short_timer);
130                     priv->short_timer = 0;
131              }
132              destroy_scene(priv->desktop_plugin);
133          osso_deinitialize(priv->osso);
134          g_hash_table_destroy(priv->hash_scene_func);
135          g_free(priv->desktop_plugin);
136          priv->desktop_plugin = NULL;
137          g_free(priv);
138          priv = NULL;
139      } 
140      fprintf(stderr,"end of quit_from_program\n");
141      gtk_main_quit();;
142 }
143 /*******************************************************************************/
144 int
145 main(int argc, char *argv[])
146 {
147     GtkWidget *main_widget = NULL;
148     HildonProgram       *app;
149     int view = 1;
150
151      
152     if (argc == 2) 
153         view = atoi(argv[1]);
154     if (view < 1 || view > 4)
155         view = 1;
156
157     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
158     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
159     desktop_plugin->priv = priv; 
160
161     gst_init (NULL, NULL);
162
163     /* Ininitializing */
164     hildon_gtk_init (&argc, &argv);
165     app = HILDON_PROGRAM (hildon_program_get_instance());
166     g_set_application_name (PACKAGE);
167  
168     priv->osso = osso_initialize("org.maemo.livewp", VERSION, TRUE, NULL);
169     if(!priv->osso){
170         fprintf(stderr,"osso_initialize failed\n");
171         return 1;
172     }
173
174 #ifdef ENABLE_NLS
175     setlocale(LC_ALL, "");
176     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
177     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
178     textdomain(GETTEXT_PACKAGE);
179 #endif
180 /* Create Main GUI */
181     main_widget = hildon_window_new ();
182     gtk_window_set_title(GTK_WINDOW(main_widget), PACKAGE);
183     gtk_window_fullscreen(GTK_WINDOW(main_widget));
184     
185     gtk_widget_show_all(GTK_WIDGET(main_widget));
186
187     priv->hash_scene_func = g_hash_table_new(g_str_hash, g_str_equal);
188     g_hash_table_insert(priv->hash_scene_func, g_strdup("Accel"),  (gpointer)&init_scene_Accel);
189     g_hash_table_insert(priv->hash_scene_func, g_strdup("Berlin"), (gpointer)&init_scene_Berlin);
190     g_hash_table_insert(priv->hash_scene_func, g_strdup("Modern"), (gpointer)&init_scene_Modern);
191     g_hash_table_insert(priv->hash_scene_func, g_strdup("Matrix"), (gpointer)&init_scene_Matrix);
192     g_hash_table_insert(priv->hash_scene_func, g_strdup("Video"),  (gpointer)&init_scene_Video);
193     g_hash_table_insert(priv->hash_scene_func, g_strdup("Xsnow"),  (gpointer)&init_scene_Xsnow);
194
195     priv->scene = NULL;
196     priv->pipeline = NULL;
197     priv->podpid = -1;
198     priv->running = FALSE;
199     priv->window = main_widget;
200     /* fprintf(stderr,"XWINDOW %i\n",GDK_WINDOW_XID (main_widget->window)); */
201     priv->desktop_plugin = desktop_plugin;
202     priv->view = view;
203
204     if (current_active_view() == view) 
205         priv->visible = TRUE;
206     else 
207         priv->visible = FALSE;
208
209     /* Load config */
210     read_config(priv);
211
212     /* Initialize DBUS */
213     livewp_initialize_dbus(priv);
214     set_live_bg(GDK_WINDOW_XDISPLAY (main_widget->window), GDK_WINDOW_XID (main_widget->window), view);
215     
216     priv->view_notify = 0; 
217     /* fprintf(stderr,"VISIBLE %i %i\n",priv->visible, current_active_view()); */
218     gconf_client_add_dir(gconf_client_get_default (), "/apps/osso/hildon-desktop/views", GCONF_CLIENT_PRELOAD_NONE, NULL);
219     priv->view_notify = gconf_client_notify_add(gconf_client_get_default (),"/apps/osso/hildon-desktop/views/current",
220                                                 (GConfClientNotifyFunc)view_changed, priv, NULL, NULL);
221
222     /* Set start position of Video to zero */
223     if (!strcmp(priv->theme,"Video"))
224         priv->theme_int_parametr1 = 0;
225
226     init_scene_theme(desktop_plugin);
227         
228     priv->long_timer = g_timeout_add(LONG_TIMER, (GtkFunction)long_timeout, desktop_plugin);
229
230     priv->alive_timer = g_timeout_add(60000*10, (GtkFunction)check_alive_event, priv);  /* One per 10 minute */ 
231     gtk_main();
232     return 0;
233 }