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