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