VOUT_CLOSE is unsafe and pointless. Do not use it.
[vlc.git] / modules / gui / maemo / maemo_input.c
1 /*****************************************************************************
2 * maemo_input.c : Input handling for the maemo plugin
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 <vlc_common.h>
29
30 #include "maemo.h"
31 #include "maemo_input.h"
32
33 static int input_event_cb( vlc_object_t *p_this, const char *psz_var,
34                            vlc_value_t oldval, vlc_value_t newval, void *param );
35
36
37 gboolean process_events( gpointer data )
38 {
39     intf_thread_t *p_intf = (intf_thread_t *)data;
40     vlc_spin_lock( &p_intf->p_sys->event_lock );
41
42     int i_event = p_intf->p_sys->i_event;
43     p_intf->p_sys->i_event = 0;
44
45     vlc_spin_unlock( &p_intf->p_sys->event_lock );
46     if( i_event )
47     {
48         if( i_event & EVENT_PLAYLIST_CURRENT )
49             item_changed_pl( p_intf );
50         if( i_event & EVENT_ACTIVITY )
51             item_changed_pl( p_intf );
52         if( i_event & EVENT_ITEM_CHANGED )
53             item_changed( p_intf );
54         if( i_event & EVENT_INTF_CHANGED )
55             update_position( p_intf );
56     }
57
58     return TRUE;
59 }
60
61 void set_input( intf_thread_t *p_intf, input_thread_t *p_input )
62 {
63     if( p_input && !( p_input->b_die || p_input->b_dead ) )
64     {
65         p_intf->p_sys->p_input = p_input;
66         vlc_object_hold( p_input );
67         var_AddCallback( p_input, "intf-event", input_event_cb, p_intf );
68
69         // "Activate" the seekbar
70         gtk_widget_set_sensitive( GTK_WIDGET( p_intf->p_sys->p_seekbar ), TRUE );
71     }
72     else
73         p_intf->p_sys->p_input = NULL;
74 }
75
76 void delete_input( intf_thread_t *p_intf )
77 {
78     if( p_intf->p_sys->p_input )
79     {
80         var_DelCallback( p_intf->p_sys->p_input, "intf-event",
81                          input_event_cb, p_intf );
82         vlc_object_release( p_intf->p_sys->p_input );
83         p_intf->p_sys->p_input = NULL;
84
85         // Reset the seekbar
86         hildon_seekbar_set_position( p_intf->p_sys->p_seekbar, 0 );
87         gtk_widget_set_sensitive( GTK_WIDGET( p_intf->p_sys->p_seekbar ), FALSE );
88     }
89 }
90
91 void item_changed_pl( intf_thread_t *p_intf )
92 {
93     vlc_mutex_lock( &p_intf->change_lock );
94     if( p_intf->p_sys->p_input &&
95         ( p_intf->p_sys->p_input->b_dead || p_intf->p_sys->p_input->b_die ) )
96     {
97         delete_input( p_intf );
98         vlc_mutex_unlock( &p_intf->change_lock );
99         return;
100     }
101
102     if( !p_intf->p_sys->p_input )
103     {
104         set_input( p_intf, playlist_CurrentInput( p_intf->p_sys->p_playlist ) );
105     }
106     vlc_mutex_unlock( &p_intf->change_lock );
107     return;
108 }
109
110 int playlist_current_cb( vlc_object_t *p_this, const char *psz_var,
111                          vlc_value_t oldval, vlc_value_t newval, void *param )
112 {
113     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
114     intf_thread_t *p_intf = (intf_thread_t *)param;
115     vlc_spin_lock( &p_intf->p_sys->event_lock );
116
117     p_intf->p_sys->i_event |= EVENT_PLAYLIST_CURRENT;
118
119     vlc_spin_unlock( &p_intf->p_sys->event_lock );
120     return VLC_SUCCESS;
121 }
122
123 int activity_cb( vlc_object_t *p_this, const char *psz_var,
124                  vlc_value_t oldval, vlc_value_t newval, void *param )
125 {
126     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
127     intf_thread_t *p_intf = (intf_thread_t *)param;
128     vlc_spin_lock( &p_intf->p_sys->event_lock );
129
130     p_intf->p_sys->i_event |= EVENT_ACTIVITY;
131
132     vlc_spin_unlock( &p_intf->p_sys->event_lock );
133     return VLC_SUCCESS;
134 }
135
136 void item_changed( intf_thread_t *p_intf )
137 {
138     GtkButton *p_button = GTK_BUTTON( p_intf->p_sys->p_play_button );
139     vlc_value_t state;
140
141     if( !p_intf->p_sys->p_input )
142         return;
143
144     var_Get( p_intf->p_sys->p_input, "state", &state );
145
146     // We change the "play" button
147     if( state.i_int == PLAYING_S )
148         gtk_button_set_image( p_button, gtk_image_new_from_stock( "vlc-pause",
149                               GTK_ICON_SIZE_BUTTON ) );
150     else
151         gtk_button_set_image( p_button, gtk_image_new_from_stock( "vlc-play",
152                               GTK_ICON_SIZE_BUTTON ) );
153 }
154
155 int item_changed_cb( vlc_object_t *p_this, const char *psz_var,
156                      vlc_value_t oldval, vlc_value_t newval, void *param )
157 {
158     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
159     intf_thread_t *p_intf = (intf_thread_t *)param;
160     vlc_spin_lock( &p_intf->p_sys->event_lock );
161
162     p_intf->p_sys->i_event |= EVENT_ITEM_CHANGED;
163
164     vlc_spin_unlock( &p_intf->p_sys->event_lock );
165     return VLC_SUCCESS;
166 }
167
168 void update_position( intf_thread_t *p_intf )
169 {
170     if( p_intf->p_sys->p_input )
171     {
172         hildon_seekbar_set_total_time( p_intf->p_sys->p_seekbar,
173                     var_GetTime( p_intf->p_sys->p_input, "length" )/1000000 );
174         hildon_seekbar_set_position( p_intf->p_sys->p_seekbar,
175                     var_GetTime( p_intf->p_sys->p_input, "time" )/1000000 );
176     }
177 }
178
179 int interface_changed_cb( vlc_object_t *p_this, const char *psz_var,
180                           vlc_value_t oldval, vlc_value_t newval, void *param )
181 {
182     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
183     intf_thread_t *p_intf = (intf_thread_t *)param;
184     vlc_spin_lock( &p_intf->p_sys->event_lock );
185
186     p_intf->p_sys->i_event |= EVENT_INTF_CHANGED;
187
188     vlc_spin_unlock( &p_intf->p_sys->event_lock );
189     return VLC_SUCCESS;
190 }
191
192 static int input_event_cb( vlc_object_t *p_this, const char *psz_var,
193                            vlc_value_t oldval, vlc_value_t newval, void *param )
194 {
195     if( newval.i_int == INPUT_EVENT_STATE )
196         return item_changed_cb( p_this, psz_var, oldval, newval, param );
197     else
198         return interface_changed_cb( p_this, psz_var, oldval, newval, param );
199 }
200