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