3b12943c063b3b95266600faee819ecf5cfc1575
[vlc.git] / modules / gui / maemo / maemo.c
1 /*****************************************************************************
2 * maemo.c : Maemo plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
5 * $Id$
6 *
7 * Authors: Antoine Lejeune <phytos@videolan.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_interface.h>
33 #include <vlc_vout_window.h>
34
35 #include <hildon/hildon-program.h>
36 #include <hildon/hildon-banner.h>
37 #include <gtk/gtk.h>
38 #include <stdio.h>
39 #include <inttypes.h>
40
41 #include "maemo.h"
42 #include "maemo_callbacks.h"
43 #include "maemo_input.h"
44 #include "maemo_interface.h"
45
46 /*****************************************************************************
47  * Local prototypes.
48  *****************************************************************************/
49 static int      Open               ( vlc_object_t * );
50 static void     Close              ( vlc_object_t * );
51 static void     Run                ( intf_thread_t * );
52 static gboolean should_die         ( gpointer );
53 static int      OpenWindow         ( vlc_object_t * );
54 static void     CloseWindow        ( vlc_object_t * );
55 static int      ControlWindow      ( vout_window_t *, int, va_list );
56 static uint32_t request_video      ( intf_thread_t *, vout_thread_t * );
57 static void     release_video      ( intf_thread_t * );
58 static gboolean video_widget_ready ( gpointer data );
59
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
63 vlc_module_begin();
64     set_shortname( "Maemo" );
65     set_description( N_("Maemo hildon interface") );
66     set_category( CAT_INTERFACE );
67     set_subcategory( SUBCAT_INTERFACE_MAIN );
68     set_capability( "interface", 70 );
69     set_callbacks( Open, Close );
70     add_shortcut( "maemo" );
71
72     add_submodule();
73         set_capability( "vout window xid", 50 );
74         set_callbacks( OpenWindow, CloseWindow );
75 vlc_module_end();
76
77 static struct
78 {
79     vlc_mutex_t    lock;
80     vlc_cond_t     wait;
81     intf_thread_t *intf;
82     bool           enabled;
83 } wnd_req = { VLC_STATIC_MUTEX, PTHREAD_COND_INITIALIZER, NULL, false };
84
85 /*****************************************************************************
86  * Module callbacks
87  *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
89 {
90     intf_thread_t *p_intf = (intf_thread_t *)p_this;
91
92     /* Allocate instance and initialize some members */
93     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
94     if( p_intf->p_sys == NULL )
95         return VLC_ENOMEM;
96
97     p_intf->pf_run = Run;
98
99     p_intf->p_sys->p_playlist = pl_Hold( p_intf );
100     p_intf->p_sys->p_input = NULL;
101     p_intf->p_sys->p_vout = NULL;
102
103     p_intf->p_sys->p_main_window = NULL;
104     p_intf->p_sys->p_video_window = NULL;
105
106     wnd_req.enabled = true;
107     /* ^no need to lock, interfacesare started before video outputs */
108     vlc_spin_init( &p_intf->p_sys->event_lock );
109
110     return VLC_SUCCESS;
111 }
112
113 static void Close( vlc_object_t *p_this )
114 {
115     intf_thread_t *p_intf = (intf_thread_t *)p_this;
116
117     vlc_object_release( p_intf->p_sys->p_playlist );
118
119     vlc_spin_destroy( &p_intf->p_sys->event_lock );
120
121     /* Destroy structure */
122     free( p_intf->p_sys );
123 }
124
125 /*****************************************************************************
126 * Initialize and launch the interface
127 *****************************************************************************/
128 static void Run( intf_thread_t *p_intf )
129 {
130     char  *p_args[] = { (char *)"vlc", NULL };
131     char **pp_args  = p_args;
132     int    i_args   = 1;
133
134     HildonProgram *program;
135     HildonWindow *window;
136     GtkWidget *main_vbox;
137
138     GtkWidget *tabs;
139     GtkWidget *video;
140     GtkWidget *bottom_hbox;
141     GtkWidget *play_button;
142     GtkWidget *prev_button;
143     GtkWidget *next_button;
144     GtkWidget *stop_button;
145     GtkWidget *seekbar;
146
147     gtk_init( &i_args, &pp_args );
148
149     program = HILDON_PROGRAM( hildon_program_get_instance() );
150     g_set_application_name( "VLC Media Player" );
151
152     window = HILDON_WINDOW( hildon_window_new() );
153     hildon_program_add_window( program, window );
154     gtk_object_set_data( GTK_OBJECT( window ),
155                          "p_intf", p_intf );
156     p_intf->p_sys->p_main_window = window;
157
158     // A little theming
159     char *psz_rc_file = NULL;
160     if( asprintf( &psz_rc_file, "%s/maemo/vlc_intf.rc",
161                   config_GetDataDir() ) != -1 )
162     {
163         gtk_rc_parse( psz_rc_file );
164         free( psz_rc_file );
165     }
166
167     // We create the main vertical box
168     main_vbox = gtk_vbox_new( FALSE, 0 );
169     gtk_container_add( GTK_CONTAINER( window ), main_vbox );
170
171     tabs = gtk_notebook_new();
172     p_intf->p_sys->p_tabs = tabs;
173     gtk_notebook_set_tab_pos( GTK_NOTEBOOK( tabs ), GTK_POS_LEFT );
174     gtk_notebook_set_show_border( GTK_NOTEBOOK( tabs ), FALSE );
175     gtk_box_pack_start( GTK_BOX( main_vbox ), tabs, TRUE, TRUE, 0 );
176
177     // We put first the embedded video
178     video = gtk_event_box_new();
179     gtk_notebook_append_page( GTK_NOTEBOOK( tabs ),
180                 video,
181                 gtk_image_new_from_stock( "vlc",
182                                           GTK_ICON_SIZE_DIALOG ) );
183     gtk_notebook_set_tab_label_packing( GTK_NOTEBOOK( tabs ),
184                                         video,
185                                         FALSE, FALSE, 0 );
186     create_playlist( p_intf );
187
188     // We put the horizontal box which contains all the buttons
189     bottom_hbox = gtk_hbox_new( FALSE, 0 );
190
191     // We create the buttons
192     play_button = gtk_button_new();
193     gtk_button_set_image( GTK_BUTTON( play_button ),
194                    gtk_image_new_from_stock( "vlc-play", GTK_ICON_SIZE_BUTTON ) );
195     gtk_widget_set_size_request( play_button, 60, 60);
196     p_intf->p_sys->p_play_button = play_button;
197     stop_button = gtk_button_new();
198     gtk_button_set_image( GTK_BUTTON( stop_button ),
199                           gtk_image_new_from_stock( "vlc-stop", GTK_ICON_SIZE_BUTTON ) );
200     prev_button = gtk_button_new();
201     gtk_button_set_image( GTK_BUTTON( prev_button ),
202                       gtk_image_new_from_stock( "vlc-previous", GTK_ICON_SIZE_BUTTON ) );
203     next_button = gtk_button_new();
204     gtk_button_set_image( GTK_BUTTON( next_button ),
205                       gtk_image_new_from_stock( "vlc-next", GTK_ICON_SIZE_BUTTON ) );
206     seekbar = hildon_seekbar_new();
207     p_intf->p_sys->p_seekbar = HILDON_SEEKBAR( seekbar );
208
209     // We add them to the hbox
210     gtk_box_pack_start( GTK_BOX( bottom_hbox ), play_button, FALSE, FALSE, 5 );
211     gtk_box_pack_start( GTK_BOX( bottom_hbox ), stop_button, FALSE, FALSE, 0 );
212     gtk_box_pack_start( GTK_BOX( bottom_hbox ), prev_button, FALSE, FALSE, 0 );
213     gtk_box_pack_start( GTK_BOX( bottom_hbox ), next_button, FALSE, FALSE, 0 );
214     gtk_box_pack_start( GTK_BOX( bottom_hbox ), seekbar    , TRUE , TRUE , 5 );
215     // We add the hbox to the main vbox
216     gtk_box_pack_start( GTK_BOX( main_vbox ), bottom_hbox, FALSE, FALSE, 0 );
217
218     g_signal_connect( window, "delete_event",
219                       G_CALLBACK( delete_event_cb ), NULL );
220     g_signal_connect( play_button, "clicked", G_CALLBACK( play_cb ), NULL );
221     g_signal_connect( stop_button, "clicked", G_CALLBACK( stop_cb ), NULL );
222     g_signal_connect( prev_button, "clicked", G_CALLBACK( prev_cb ), NULL );
223     g_signal_connect( next_button, "clicked", G_CALLBACK( next_cb ), NULL );
224     g_signal_connect( seekbar, "change-value",
225                       G_CALLBACK( seekbar_changed_cb ), NULL );
226
227     gtk_widget_show_all( GTK_WIDGET( window ) );
228
229     create_menu( p_intf );
230
231     // Set callback with the vlc core
232     g_timeout_add( INTF_IDLE_SLEEP / 1000, process_events, p_intf );
233     g_timeout_add( 150 /* miliseconds */, should_die, p_intf );
234     var_AddCallback( p_intf->p_sys->p_playlist, "item-change",
235                      item_changed_cb, p_intf );
236     var_AddCallback( p_intf->p_sys->p_playlist, "item-current",
237                      playlist_current_cb, p_intf );
238     var_AddCallback( p_intf->p_sys->p_playlist, "activity",
239                      activity_cb, p_intf );
240
241     // Look if the playlist is already started
242     item_changed_pl( p_intf );
243
244     // The embedded video is only ready after gtk_main and windows are shown
245     g_idle_add( video_widget_ready, video );
246
247     gtk_main();
248
249     delete_input( p_intf );
250     var_DelCallback( p_intf->p_sys->p_playlist, "item-change",
251                      item_changed_cb, p_intf );
252     var_DelCallback( p_intf->p_sys->p_playlist, "item-current",
253                      playlist_current_cb, p_intf );
254     var_DelCallback( p_intf->p_sys->p_playlist, "activity",
255                      activity_cb, p_intf );
256
257     /* FIXME: we need to wait for vout to clean up... */
258     assert( !p_intf->p_sys->p_vout ); /* too late */
259     gtk_object_destroy( GTK_OBJECT( window ) );
260 }
261
262 static gboolean should_die( gpointer data )
263 {
264     intf_thread_t *p_intf = (intf_thread_t *)data;
265     if( !vlc_object_alive( p_intf ) )
266         gtk_main_quit();
267     return TRUE;
268 }
269
270 /**
271 * Video output window provider
272 */
273 static int OpenWindow (vlc_object_t *obj)
274 {
275     vout_window_t *wnd = (vout_window_t *)obj;
276     intf_thread_t *intf;
277
278     if (wnd->cfg->is_standalone || !wnd_req.enabled)
279         return VLC_EGENERIC;
280
281     /* FIXME it should NOT be needed */
282     vout_thread_t *vout = vlc_object_find (obj, VLC_OBJECT_VOUT, FIND_PARENT);
283     if (!vout)
284         return VLC_EGENERIC;
285
286     vlc_mutex_lock (&wnd_req.lock);
287     while ((intf = wnd_req.intf) == NULL)
288         vlc_cond_wait (&wnd_req.wait, &wnd_req.lock);
289
290     wnd->handle.xid = request_video( intf, vout );
291     vlc_mutex_unlock (&wnd_req.lock);
292
293     vlc_object_release( vout );
294
295     if (!wnd->handle.xid)
296         return VLC_EGENERIC;
297
298     msg_Dbg( intf, "Using handle %"PRIu32, wnd->handle.xid );
299
300     wnd->control = ControlWindow;
301     wnd->sys = (vout_window_sys_t*)intf;
302
303     return VLC_SUCCESS;
304 }
305
306 static int ControlWindow (vout_window_t *wnd, int query, va_list args)
307 {
308     intf_thread_t *intf = (intf_thread_t *)wnd->sys;
309
310     switch( query )
311     {
312     case VOUT_WINDOW_SET_SIZE:
313     {
314         int i_width  = (int)va_arg( args, int );
315         int i_height = (int)va_arg( args, int );
316
317         int i_current_w, i_current_h;
318         gdk_drawable_get_size( GDK_DRAWABLE( intf->p_sys->p_video_window->window ),
319                                &i_current_w, &i_current_h );
320         if( i_width != i_current_w || i_height != i_current_h )
321             return VLC_EGENERIC;
322         return VLC_SUCCESS;
323     }
324     default:
325         return VLC_EGENERIC;
326     }
327 }
328
329 static void CloseWindow (vlc_object_t *obj)
330 {
331     vout_window_t *wnd = (vout_window_t *)obj;
332     intf_thread_t *intf = (intf_thread_t *)wnd->sys;
333
334     vlc_mutex_lock( &wnd_req.lock );
335     release_video( intf );
336     vlc_mutex_unlock( &wnd_req.lock );
337 }
338
339 static uint32_t request_video( intf_thread_t *p_intf, vout_thread_t *p_nvout )
340 {
341     if( p_intf->p_sys->p_vout )
342     {
343         msg_Dbg( p_intf, "Embedded video already in use" );
344         return 0;
345     }
346
347     p_intf->p_sys->p_vout = vlc_object_hold( p_nvout );
348     return GDK_WINDOW_XID( p_intf->p_sys->p_video_window->window );
349 }
350
351 static void release_video( intf_thread_t *p_intf )
352 {
353     msg_Dbg( p_intf, "Releasing embedded video" );
354
355     vlc_object_release( p_intf->p_sys->p_vout );
356     p_intf->p_sys->p_vout = NULL;
357 }
358
359 static gboolean video_widget_ready( gpointer data )
360 {
361     intf_thread_t *p_intf = NULL;
362     GtkWidget *top_window = NULL;
363     GtkWidget *video = (GtkWidget *)data;
364
365     top_window = gtk_widget_get_toplevel( GTK_WIDGET( video ) );
366     p_intf = (intf_thread_t *)gtk_object_get_data( GTK_OBJECT( top_window ),
367                                                    "p_intf" );
368     p_intf->p_sys->p_video_window = video;
369     gtk_widget_grab_focus( video );
370
371     vlc_mutex_lock( &wnd_req.lock );
372     wnd_req.intf = p_intf;
373     vlc_cond_signal( &wnd_req.wait );
374     vlc_mutex_unlock( &wnd_req.lock );
375
376     // We rewind the input
377     if( p_intf->p_sys->p_input )
378     {
379         input_Control( p_intf->p_sys->p_input, INPUT_SET_POSITION, 0.0 );
380     }
381
382     // We want it to be executed only one time
383     return FALSE;
384 }