1 /*****************************************************************************
2 * pda.c : PDA Gtk2 plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
7 * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8 * Marc Ariberti <marcari@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
35 #include <vlc_input.h>
36 #include <vlc_interface.h>
37 #include <vlc_playlist.h>
41 #include "pda_callbacks.h"
42 #include "pda_interface.h"
43 #include "pda_support.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
51 static void Run ( intf_thread_t * );
53 void GtkAutoPlayFile ( vlc_object_t * );
54 static int Manage ( intf_thread_t *p_intf );
55 void E_(GtkDisplayDate) ( GtkAdjustment *p_adj, gpointer userdata );
56 gint E_(GtkModeManage) ( intf_thread_t * p_intf );
58 /*****************************************************************************
60 *****************************************************************************/
61 #define AUTOPLAYFILE_TEXT N_("Autoplay selected file")
62 #define AUTOPLAYFILE_LONGTEXT N_("Automatically play a file when selected in the "\
63 "file selection list")
65 /*****************************************************************************
67 *****************************************************************************/
69 set_description( _("PDA Linux Gtk2+ interface") );
70 set_category( CAT_INTERFACE );
71 set_subcategory( SUBCAT_INTERFACE_MAIN );
72 // add_bool( "pda-autoplayfile", 1, GtkAutoPlayFile, AUTOPLAYFILE_TEXT, AUTOPLAYFILE_LONGTEXT, true );
73 set_capability( "interface", 70 );
74 set_callbacks( Open, Close );
75 add_shortcut( "pda" );
78 /*****************************************************************************
79 * Open: initialize and create window
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 intf_thread_t *p_intf = (intf_thread_t *)p_this;
85 /* Allocate instance and initialize some members */
86 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
87 if( p_intf->p_sys == NULL )
89 msg_Err( p_intf, "out of memory" );
94 msg_Dbg( p_intf, "Using gui-helper" );
95 p_intf->p_sys->p_gtk_main =
96 module_Need( p_this, "gui-helper", "gtk2", true );
97 if( p_intf->p_sys->p_gtk_main == NULL )
99 free( p_intf->p_sys );
104 /* Initialize Gtk+ thread */
105 p_intf->p_sys->p_input = NULL;
107 p_intf->p_sys->b_autoplayfile = 1;
108 p_intf->p_sys->b_playing = 0;
109 p_intf->p_sys->b_slider_free = 1;
111 p_intf->pf_run = Run;
116 /*****************************************************************************
117 * Close: destroy interface window
118 *****************************************************************************/
119 static void Close( vlc_object_t *p_this )
121 intf_thread_t *p_intf = (intf_thread_t *)p_this;
123 if( p_intf->p_sys->p_input )
125 vlc_object_release( p_intf->p_sys->p_input );
128 #ifdef NEED_GTK2_MAIN
129 msg_Dbg( p_intf, "Releasing gui-helper" );
130 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
133 /* Destroy structure */
134 free( p_intf->p_sys );
137 /*****************************************************************************
139 *****************************************************************************
140 * this part of the interface is in a separate thread so that we can call
141 * gtk_main() from within it without annoying the rest of the program.
142 *****************************************************************************/
143 static void Run( intf_thread_t *p_intf )
145 #ifndef NEED_GTK2_MAIN
146 /* gtk_init needs to know the command line. We don't care, so we
147 * give it an empty one */
148 char *p_args[] = { "", NULL };
149 char **pp_args = p_args;
153 playlist_t *p_playlist;
154 GtkCellRenderer *p_renderer = NULL;
155 GtkTreeViewColumn *p_column = NULL;
156 GtkListStore *p_filelist = NULL;
157 GtkListStore *p_playlist_store = NULL;
159 #ifndef NEED_GTK2_MAIN
161 msg_Dbg( p_intf, "Starting pda GTK2+ interface" );
162 gtk_init( &i_args, &pp_args );
164 /* Initialize Gtk+ */
165 msg_Dbg( p_intf, "Starting pda GTK2+ interface thread" );
169 /* Create some useful widgets that will certainly be used */
170 /* FIXME: magic path */
171 add_pixmap_directory("share");
172 add_pixmap_directory("/usr/share/vlc");
174 /* Path for pixmaps under linupy 1.4 */
175 add_pixmap_directory("/usr/local/share/pixmaps/vlc");
176 /* Path for pixmaps under linupy 2.0 */
177 add_pixmap_directory("/usr/share/pixmaps/vlc");
179 p_intf->p_sys->p_window = create_pda();
180 if (p_intf->p_sys->p_window == NULL)
182 msg_Err( p_intf, "unable to create pda interface" );
185 /* Store p_intf to keep an eye on it */
186 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
189 /* Set the title of the main window */
190 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
191 VOUT_TITLE " (PDA Linux interface)");
193 /* Get the notebook object */
194 p_intf->p_sys->p_notebook = GTK_NOTEBOOK( gtk_object_get_data(
195 GTK_OBJECT( p_intf->p_sys->p_window ), "notebook" ) );
197 /* Get the slider object */
198 p_intf->p_sys->p_slider = (GtkHScale*) lookup_widget( p_intf->p_sys->p_window, "timeSlider" );
199 p_intf->p_sys->p_slider_label = (GtkLabel*) lookup_widget( p_intf->p_sys->p_window, "timeLabel" );
200 if (p_intf->p_sys->p_slider == NULL)
201 msg_Err( p_intf, "Time slider widget not found." );
202 if (p_intf->p_sys->p_slider_label == NULL)
203 msg_Err( p_intf, "Time label widget not found." );
205 /* Connect the date display to the slider */
206 p_intf->p_sys->p_adj = gtk_range_get_adjustment( GTK_RANGE(p_intf->p_sys->p_slider) );
207 if (p_intf->p_sys->p_adj == NULL)
208 msg_Err( p_intf, "Adjustment range not found." );
209 g_signal_connect( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
210 G_CALLBACK( E_(GtkDisplayDate) ), p_intf );
211 p_intf->p_sys->f_adj_oldvalue = 0;
212 p_intf->p_sys->i_adj_oldvalue = 0;
214 /* BEGIN OF FILEVIEW GTK_TREE_VIEW */
215 p_intf->p_sys->p_tvfile = NULL;
216 p_intf->p_sys->p_tvfile = (GtkTreeView *) lookup_widget( p_intf->p_sys->p_window,
218 if (NULL == p_intf->p_sys->p_tvfile)
219 msg_Err(p_intf, "Error obtaining pointer to File List");
221 /* Insert columns 0 */
222 p_renderer = gtk_cell_renderer_text_new ();
223 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 0, (gchar *) N_("Filename"), p_renderer, NULL);
224 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 0 );
225 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 0 );
226 gtk_tree_view_column_set_sort_column_id(p_column, 0);
227 /* Insert columns 1 */
228 p_renderer = gtk_cell_renderer_text_new ();
229 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 1, (gchar *) N_("Permissions"), p_renderer, NULL);
230 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 1 );
231 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 1 );
232 gtk_tree_view_column_set_sort_column_id(p_column, 1);
233 /* Insert columns 2 */
234 p_renderer = gtk_cell_renderer_text_new ();
235 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 2, (gchar *) N_("Size"), p_renderer, NULL);
236 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 2 );
237 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 2 );
238 gtk_tree_view_column_set_sort_column_id(p_column, 2);
239 /* Insert columns 3 */
240 p_renderer = gtk_cell_renderer_text_new ();
241 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 3, (gchar *) N_("Owner"), p_renderer, NULL);
242 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 3 );
243 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 3 );
244 gtk_tree_view_column_set_sort_column_id(p_column, 3);
245 /* Insert columns 4 */
246 p_renderer = gtk_cell_renderer_text_new ();
247 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 4, (gchar *) N_("Group"), p_renderer, NULL);
248 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 4 );
249 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 4 );
250 gtk_tree_view_column_set_sort_column_id(p_column, 4);
252 /* Get new directory listing */
253 p_filelist = gtk_list_store_new (5,
254 G_TYPE_STRING, /* Filename */
255 G_TYPE_STRING, /* permissions */
256 G_TYPE_UINT64, /* File size */
257 G_TYPE_STRING, /* Owner */
258 G_TYPE_STRING);/* Group */
259 ReadDirectory(p_intf, p_filelist, ".");
260 gtk_tree_view_set_model(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile), GTK_TREE_MODEL(p_filelist));
261 g_object_unref(p_filelist); /* Model will be released by GtkTreeView */
262 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile)),GTK_SELECTION_MULTIPLE);
264 /* Column properties */
265 gtk_tree_view_set_headers_visible(p_intf->p_sys->p_tvfile, TRUE);
266 gtk_tree_view_columns_autosize(p_intf->p_sys->p_tvfile);
267 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile),TRUE);
268 /* END OF FILEVIEW GTK_TREE_VIEW */
270 /* BEGIN OF PLAYLIST GTK_TREE_VIEW */
271 p_intf->p_sys->p_tvplaylist = NULL;
272 p_intf->p_sys->p_tvplaylist = (GtkTreeView *) lookup_widget( p_intf->p_sys->p_window, "tvPlaylist");
273 if (NULL == p_intf->p_sys->p_tvplaylist)
274 msg_Err(p_intf, "Error obtaining pointer to Play List");
277 p_renderer = gtk_cell_renderer_text_new ();
278 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 0, (gchar *) N_("Filename"), p_renderer, NULL);
279 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 0 );
280 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 0 );
281 gtk_tree_view_column_set_sort_column_id(p_column, 0);
283 p_renderer = gtk_cell_renderer_text_new ();
284 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 1, (gchar *) N_("Time"), p_renderer, NULL);
285 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 1 );
286 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 1 );
287 gtk_tree_view_column_set_sort_column_id(p_column, 1);
289 /* Column 3 - is a hidden column used for reliable deleting items from the underlying playlist */
290 p_renderer = gtk_cell_renderer_text_new ();
291 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 2, (gchar *) N_("Index"), p_renderer, NULL);
292 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 2 );
293 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 2 );
294 gtk_tree_view_column_set_sort_column_id(p_column, 2);
296 /* update the playlist */
297 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
298 p_playlist_store = gtk_list_store_new (3,
299 G_TYPE_STRING, /* Filename */
300 G_TYPE_STRING, /* Time */
301 G_TYPE_UINT); /* Hidden index */
302 PlaylistRebuildListStore(p_intf,p_playlist_store, p_playlist);
303 gtk_tree_view_set_model(GTK_TREE_VIEW(p_intf->p_sys->p_tvplaylist), GTK_TREE_MODEL(p_playlist_store));
304 g_object_unref(p_playlist_store);
305 vlc_object_release(p_playlist); /* Free the playlist */
306 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(p_intf->p_sys->p_tvplaylist)),GTK_SELECTION_MULTIPLE);
308 /* Column properties */
309 gtk_tree_view_set_headers_visible(p_intf->p_sys->p_tvplaylist, TRUE);
310 gtk_tree_view_columns_autosize(p_intf->p_sys->p_tvplaylist);
311 gtk_tree_view_set_headers_clickable(p_intf->p_sys->p_tvplaylist, TRUE);
312 /* END OF PLAYLIST GTK_TREE_VIEW */
314 /* Hide the Preference TAB for now. */
315 GtkWidget *p_preference_tab = NULL;
316 p_preference_tab = gtk_notebook_get_nth_page(p_intf->p_sys->p_notebook,5);
317 if (p_preference_tab != NULL)
318 gtk_widget_hide(p_preference_tab);
320 /* Show the control window */
321 gtk_widget_show( p_intf->p_sys->p_window );
323 #ifdef NEED_GTK2_MAIN
324 msg_Dbg( p_intf, "Manage GTK keyboard events using threads" );
325 while( !intf_ShouldDie( p_intf ) )
329 /* Sleep to avoid using all CPU - since some interfaces need to
330 * access keyboard events, a 100ms delay is a good compromise */
332 if (vlc_CPU() & CPU_CAPABILITY_FPU)
333 msleep( INTF_IDLE_SLEEP );
339 msg_Dbg( p_intf, "Manage GTK keyboard events using timeouts" );
340 /* Sleep to avoid using all CPU - since some interfaces needs to access
341 * keyboard events, a 1000ms delay is a good compromise */
342 if (vlc_CPU() & CPU_CAPABILITY_FPU)
343 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage, p_intf );
345 i_dummy = gtk_timeout_add( 1000, (GtkFunction)Manage, p_intf );
349 /* Remove the timeout */
350 gtk_timeout_remove( i_dummy );
353 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
354 #ifdef NEED_GTK2_MAIN
359 /*****************************************************************************
360 * GtkAutoplayFile: Autoplay file depending on configuration settings
361 *****************************************************************************/
362 void GtkAutoPlayFile( vlc_object_t *p_this )
364 GtkWidget *cbautoplay;
365 intf_thread_t *p_intf;
367 vlc_list_t *p_list = vlc_list_find( p_this, VLC_OBJECT_INTF,
370 for( i_index = 0; i_index < p_list->i_count; i_index++ )
372 p_intf = (intf_thread_t *)p_list->p_values[i_index].p_object ;
374 if( strcmp( MODULE_STRING, module_GetObjName(p_intf->p_module) ) )
378 cbautoplay = GTK_WIDGET( gtk_object_get_data(
379 GTK_OBJECT( p_intf->p_sys->p_window ),
382 if( !config_GetInt( p_this, "pda-autoplayfile" ) )
384 p_intf->p_sys->b_autoplayfile = false;
388 p_intf->p_sys->b_autoplayfile = true;
390 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( cbautoplay ),
391 p_intf->p_sys->b_autoplayfile );
393 vlc_list_release( p_list );
396 /* following functions are local */
398 /*****************************************************************************
399 * Manage: manage main thread messages
400 *****************************************************************************
401 * In this function, called approx. 10 times a second, we check what the
402 * main program wanted to tell us.
403 *****************************************************************************/
404 static int Manage( intf_thread_t *p_intf )
406 GtkListStore *p_liststore;
407 vlc_mutex_lock( &p_intf->change_lock );
409 /* Update the input */
410 if( p_intf->p_sys->p_input == NULL )
412 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
415 else if( p_intf->p_sys->p_input->b_dead )
417 vlc_object_release( p_intf->p_sys->p_input );
418 p_intf->p_sys->p_input = NULL;
421 if( p_intf->p_sys->p_input )
423 input_thread_t *p_input = p_intf->p_sys->p_input;
424 int64_t i_time = 0, i_length = 0;
426 vlc_mutex_lock( &p_input->object_lock );
427 if( !p_input->b_die )
429 playlist_t *p_playlist;
431 E_(GtkModeManage)( p_intf );
432 p_intf->p_sys->b_playing = 1;
434 /* update playlist interface */
435 p_playlist = (playlist_t *) vlc_object_find(
436 p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
437 if (p_playlist != NULL)
439 p_liststore = gtk_list_store_new (3,
442 G_TYPE_UINT); /* Hidden index */
443 PlaylistRebuildListStore(p_intf, p_liststore, p_playlist);
444 gtk_tree_view_set_model(p_intf->p_sys->p_tvplaylist, (GtkTreeModel*) p_liststore);
445 g_object_unref(p_liststore);
446 vlc_object_release( p_playlist );
449 /* Manage the slider */
450 i_time = var_GetTime( p_intf->p_sys->p_input, "time" );
451 i_length = var_GetTime( p_intf->p_sys->p_input, "length" );
453 if (vlc_CPU() & CPU_CAPABILITY_FPU)
455 /* Manage the slider for CPU_CAPABILITY_FPU hardware */
456 if( p_intf->p_sys->b_playing )
458 float newvalue = p_intf->p_sys->p_adj->value;
460 /* If the user hasn't touched the slider since the last time,
461 * then the input can safely change it */
462 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
464 /* Update the value */
465 p_intf->p_sys->p_adj->value =
466 p_intf->p_sys->f_adj_oldvalue =
467 ( 100 * i_time ) / i_length;
468 g_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
471 /* Otherwise, send message to the input if the user has
472 * finished dragging the slider */
473 else if( p_intf->p_sys->b_slider_free )
475 double f_pos = (double)newvalue / 100.0;
477 /* release the lock to be able to seek */
478 vlc_mutex_unlock( &p_input->object_lock );
479 var_SetFloat( p_input, "position", f_pos );
480 vlc_mutex_lock( &p_input->object_lock );
482 /* Update the old value */
483 p_intf->p_sys->f_adj_oldvalue = newvalue;
489 /* Manage the slider without CPU_CAPABILITY_FPU hardware */
490 if( p_intf->p_sys->b_playing )
492 off_t newvalue = p_intf->p_sys->p_adj->value;
494 /* If the user hasn't touched the slider since the last time,
495 * then the input can safely change it */
496 if( newvalue == p_intf->p_sys->i_adj_oldvalue )
498 /* Update the value */
499 p_intf->p_sys->p_adj->value =
500 p_intf->p_sys->i_adj_oldvalue =
501 ( 100 * i_time ) / i_length;
502 g_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
505 /* Otherwise, send message to the input if the user has
506 * finished dragging the slider */
507 else if( p_intf->p_sys->b_slider_free )
509 double f_pos = (double)newvalue / 100.0;
511 /* release the lock to be able to seek */
512 vlc_mutex_unlock( &p_input->object_lock );
513 var_SetFloat( p_input, "position", f_pos );
514 vlc_mutex_lock( &p_input->object_lock );
516 /* Update the old value */
517 p_intf->p_sys->i_adj_oldvalue = newvalue;
522 vlc_mutex_unlock( &p_input->object_lock );
524 else if( p_intf->p_sys->b_playing && !intf_ShouldDie( p_intf ) )
526 E_(GtkModeManage)( p_intf );
527 p_intf->p_sys->b_playing = 0;
530 #ifndef NEED_GTK2_MAIN
531 if( intf_ShouldDie( p_intf ) )
533 vlc_mutex_unlock( &p_intf->change_lock );
535 /* Prepare to die, young Skywalker */
542 vlc_mutex_unlock( &p_intf->change_lock );
547 /*****************************************************************************
548 * GtkDisplayDate: display stream date
549 *****************************************************************************
550 * This function displays the current date related to the position in
551 * the stream. It is called whenever the slider changes its value.
552 * The lock has to be taken before you call the function.
553 *****************************************************************************/
554 void E_(GtkDisplayDate)( GtkAdjustment *p_adj, gpointer userdata )
556 intf_thread_t *p_intf;
558 p_intf = (intf_thread_t*) userdata;
562 if( p_intf->p_sys->p_input )
564 char psz_time[ MSTRTIME_MAX_SIZE ];
567 i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / INT64_C(1000000 );
568 secstotimestr( psz_time, i_seconds );
570 gtk_label_set_text( GTK_LABEL( p_intf->p_sys->p_slider_label ),
575 /*****************************************************************************
576 * GtkModeManage: actualize the aspect of the interface whenever the input
578 *****************************************************************************
579 * The lock has to be taken before you call the function.
580 *****************************************************************************/
581 gint E_(GtkModeManage)( intf_thread_t * p_intf )
583 GtkWidget * p_slider = NULL;
586 if ( p_intf->p_sys->p_window == NULL )
587 msg_Err( p_intf, "Main widget not found" );
589 p_slider = lookup_widget( p_intf->p_sys->p_window, "timeSlider");
590 if (p_slider == NULL)
591 msg_Err( p_intf, "Slider widget not found" );
593 /* controls unavailable */
596 /* show the box related to current input mode */
597 if( p_intf->p_sys->p_input )
599 /* initialize and show slider for seekable streams */
601 gtk_widget_show( GTK_WIDGET( p_slider ) );
604 /* control buttons for free pace streams */
605 b_control = p_intf->p_sys->p_input->b_can_pace_control;
607 msg_Dbg( p_intf, "stream has changed, refreshing interface" );
610 /* set control items */
611 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbRewind"), b_control );
612 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbPause"), b_control );
613 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbForward"), b_control );