1 /*****************************************************************************
2 * pda.c : PDA Gtk2 plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
7 * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <errno.h> /* ENOMEM */
30 #include <string.h> /* strerror() */
38 #include "pda_callbacks.h"
39 #include "pda_interface.h"
40 #include "pda_support.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
48 static void Run ( intf_thread_t * );
50 void GtkAutoPlayFile ( vlc_object_t * );
51 static int Manage ( intf_thread_t *p_intf );
52 void E_(GtkDisplayDate) ( GtkAdjustment *p_adj, gpointer userdata );
53 gint E_(GtkModeManage) ( intf_thread_t * p_intf );
55 /*****************************************************************************
57 *****************************************************************************/
58 #define AUTOPLAYFILE_TEXT N_("Autoplay selected file")
59 #define AUTOPLAYFILE_LONGTEXT N_("Automatically play a file when selected in the "\
60 "file selection list")
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( N_("PDA Linux Gtk2+ interface") );
67 set_category( CAT_INTERFACE );
68 set_subcategory( SUBCAT_INTERFACE_GENERAL );
69 // add_bool( "pda-autoplayfile", 1, GtkAutoPlayFile, AUTOPLAYFILE_TEXT, AUTOPLAYFILE_LONGTEXT, VLC_TRUE );
70 set_capability( "interface", 70 );
71 set_callbacks( Open, Close );
72 add_shortcut( "pda" );
75 /*****************************************************************************
76 * Open: initialize and create window
77 *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
80 intf_thread_t *p_intf = (intf_thread_t *)p_this;
82 /* Allocate instance and initialize some members */
83 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
84 if( p_intf->p_sys == NULL )
86 msg_Err( p_intf, "out of memory" );
91 msg_Dbg( p_intf, "Using gui-helper" );
92 p_intf->p_sys->p_gtk_main =
93 module_Need( p_this, "gui-helper", "gtk2", VLC_TRUE );
94 if( p_intf->p_sys->p_gtk_main == NULL )
96 free( p_intf->p_sys );
101 /* Initialize Gtk+ thread */
102 p_intf->p_sys->p_input = NULL;
104 p_intf->p_sys->b_autoplayfile = 1;
105 p_intf->p_sys->b_playing = 0;
106 p_intf->p_sys->b_slider_free = 1;
108 p_intf->pf_run = Run;
113 /*****************************************************************************
114 * Close: destroy interface window
115 *****************************************************************************/
116 static void Close( vlc_object_t *p_this )
118 intf_thread_t *p_intf = (intf_thread_t *)p_this;
120 if( p_intf->p_sys->p_input )
122 vlc_object_release( p_intf->p_sys->p_input );
125 #ifdef NEED_GTK2_MAIN
126 msg_Dbg( p_intf, "Releasing gui-helper" );
127 module_Unneed( p_intf, p_intf->p_sys->p_gtk_main );
130 /* Destroy structure */
131 free( p_intf->p_sys );
134 /*****************************************************************************
136 *****************************************************************************
137 * this part of the interface is in a separate thread so that we can call
138 * gtk_main() from within it without annoying the rest of the program.
139 *****************************************************************************/
140 static void Run( intf_thread_t *p_intf )
142 #ifndef NEED_GTK2_MAIN
143 /* gtk_init needs to know the command line. We don't care, so we
144 * give it an empty one */
145 char *p_args[] = { "", NULL };
146 char **pp_args = p_args;
150 playlist_t *p_playlist;
151 GtkCellRenderer *p_renderer = NULL;
152 GtkTreeViewColumn *p_column = NULL;
153 GtkListStore *p_filelist = NULL;
154 GtkListStore *p_playlist_store = NULL;
156 #ifndef NEED_GTK2_MAIN
158 msg_Dbg( p_intf, "Starting pda GTK2+ interface" );
159 gtk_init( &i_args, &pp_args );
161 /* Initialize Gtk+ */
162 msg_Dbg( p_intf, "Starting pda GTK2+ interface thread" );
166 /* Create some useful widgets that will certainly be used */
167 /* FIXME: magic path */
168 add_pixmap_directory("share");
169 add_pixmap_directory("/usr/share/vlc");
171 /* Path for pixmaps under linupy 1.4 */
172 add_pixmap_directory("/usr/local/share/pixmaps/vlc");
173 /* Path for pixmaps under linupy 2.0 */
174 add_pixmap_directory("/usr/share/pixmaps/vlc");
176 p_intf->p_sys->p_window = create_pda();
177 if (p_intf->p_sys->p_window == NULL)
179 msg_Err( p_intf, "unable to create pda interface" );
182 /* Store p_intf to keep an eye on it */
183 gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
186 /* Set the title of the main window */
187 gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
188 VOUT_TITLE " (PDA Linux interface)");
190 /* Get the notebook object */
191 p_intf->p_sys->p_notebook = GTK_NOTEBOOK( gtk_object_get_data(
192 GTK_OBJECT( p_intf->p_sys->p_window ), "notebook" ) );
194 /* Get the slider object */
195 p_intf->p_sys->p_slider = (GtkHScale*) lookup_widget( p_intf->p_sys->p_window, "timeSlider" );
196 p_intf->p_sys->p_slider_label = (GtkLabel*) lookup_widget( p_intf->p_sys->p_window, "timeLabel" );
197 if (p_intf->p_sys->p_slider == NULL)
198 msg_Err( p_intf, "Time slider widget not found." );
199 if (p_intf->p_sys->p_slider_label == NULL)
200 msg_Err( p_intf, "Time label widget not found." );
202 /* Connect the date display to the slider */
203 p_intf->p_sys->p_adj = gtk_range_get_adjustment( GTK_RANGE(p_intf->p_sys->p_slider) );
204 if (p_intf->p_sys->p_adj == NULL)
205 msg_Err( p_intf, "Adjustment range not found." );
206 g_signal_connect( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
207 G_CALLBACK( E_(GtkDisplayDate) ), p_intf );
208 p_intf->p_sys->f_adj_oldvalue = 0;
209 p_intf->p_sys->i_adj_oldvalue = 0;
211 /* BEGIN OF FILEVIEW GTK_TREE_VIEW */
212 p_intf->p_sys->p_tvfile = NULL;
213 p_intf->p_sys->p_tvfile = (GtkTreeView *) lookup_widget( p_intf->p_sys->p_window,
215 if (NULL == p_intf->p_sys->p_tvfile)
216 msg_Err(p_intf, "Error obtaining pointer to File List");
218 /* Insert columns 0 */
219 p_renderer = gtk_cell_renderer_text_new ();
220 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 0, (gchar *) N_("Filename"), p_renderer, NULL);
221 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 0 );
222 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 0 );
223 gtk_tree_view_column_set_sort_column_id(p_column, 0);
224 /* Insert columns 1 */
225 p_renderer = gtk_cell_renderer_text_new ();
226 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 1, (gchar *) N_("Permissions"), p_renderer, NULL);
227 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 1 );
228 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 1 );
229 gtk_tree_view_column_set_sort_column_id(p_column, 1);
230 /* Insert columns 2 */
231 p_renderer = gtk_cell_renderer_text_new ();
232 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 2, (gchar *) N_("Size"), p_renderer, NULL);
233 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 2 );
234 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 2 );
235 gtk_tree_view_column_set_sort_column_id(p_column, 2);
236 /* Insert columns 3 */
237 p_renderer = gtk_cell_renderer_text_new ();
238 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 3, (gchar *) N_("Owner"), p_renderer, NULL);
239 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 3 );
240 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 3 );
241 gtk_tree_view_column_set_sort_column_id(p_column, 3);
242 /* Insert columns 4 */
243 p_renderer = gtk_cell_renderer_text_new ();
244 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvfile, 4, (gchar *) N_("Group"), p_renderer, NULL);
245 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvfile, 4 );
246 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 4 );
247 gtk_tree_view_column_set_sort_column_id(p_column, 4);
249 /* Get new directory listing */
250 p_filelist = gtk_list_store_new (5,
251 G_TYPE_STRING, /* Filename */
252 G_TYPE_STRING, /* permissions */
253 G_TYPE_UINT64, /* File size */
254 G_TYPE_STRING, /* Owner */
255 G_TYPE_STRING);/* Group */
256 ReadDirectory(p_intf, p_filelist, ".");
257 gtk_tree_view_set_model(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile), GTK_TREE_MODEL(p_filelist));
258 g_object_unref(p_filelist); /* Model will be released by GtkTreeView */
259 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile)),GTK_SELECTION_MULTIPLE);
261 /* Column properties */
262 gtk_tree_view_set_headers_visible(p_intf->p_sys->p_tvfile, TRUE);
263 gtk_tree_view_columns_autosize(p_intf->p_sys->p_tvfile);
264 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(p_intf->p_sys->p_tvfile),TRUE);
265 /* END OF FILEVIEW GTK_TREE_VIEW */
267 /* BEGIN OF PLAYLIST GTK_TREE_VIEW */
268 p_intf->p_sys->p_tvplaylist = NULL;
269 p_intf->p_sys->p_tvplaylist = (GtkTreeView *) lookup_widget( p_intf->p_sys->p_window, "tvPlaylist");
270 if (NULL == p_intf->p_sys->p_tvplaylist)
271 msg_Err(p_intf, "Error obtaining pointer to Play List");
274 p_renderer = gtk_cell_renderer_text_new ();
275 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 0, (gchar *) N_("Filename"), p_renderer, NULL);
276 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 0 );
277 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 0 );
278 gtk_tree_view_column_set_sort_column_id(p_column, 0);
280 p_renderer = gtk_cell_renderer_text_new ();
281 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 1, (gchar *) N_("Time"), p_renderer, NULL);
282 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 1 );
283 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 1 );
284 gtk_tree_view_column_set_sort_column_id(p_column, 1);
286 /* Column 3 - is a hidden column used for reliable deleting items from the underlying playlist */
287 p_renderer = gtk_cell_renderer_text_new ();
288 gtk_tree_view_insert_column_with_attributes(p_intf->p_sys->p_tvplaylist, 2, (gchar *) N_("Index"), p_renderer, NULL);
289 p_column = gtk_tree_view_get_column(p_intf->p_sys->p_tvplaylist, 2 );
290 gtk_tree_view_column_add_attribute(p_column, p_renderer, "text", 2 );
291 gtk_tree_view_column_set_sort_column_id(p_column, 2);
293 /* update the playlist */
294 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
295 p_playlist_store = gtk_list_store_new (3,
296 G_TYPE_STRING, /* Filename */
297 G_TYPE_STRING, /* Time */
298 G_TYPE_UINT); /* Hidden index */
299 PlaylistRebuildListStore(p_playlist_store, p_playlist);
300 gtk_tree_view_set_model(GTK_TREE_VIEW(p_intf->p_sys->p_tvplaylist), GTK_TREE_MODEL(p_playlist_store));
301 g_object_unref(p_playlist_store);
302 vlc_object_release(p_playlist); /* Free the playlist */
303 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(p_intf->p_sys->p_tvplaylist)),GTK_SELECTION_MULTIPLE);
305 /* Column properties */
306 gtk_tree_view_set_headers_visible(p_intf->p_sys->p_tvplaylist, TRUE);
307 gtk_tree_view_columns_autosize(p_intf->p_sys->p_tvplaylist);
308 gtk_tree_view_set_headers_clickable(p_intf->p_sys->p_tvplaylist, TRUE);
309 /* END OF PLAYLIST GTK_TREE_VIEW */
311 /* Hide the Preference TAB for now. */
312 GtkWidget *p_preference_tab = NULL;
313 p_preference_tab = gtk_notebook_get_nth_page(p_intf->p_sys->p_notebook,5);
314 if (p_preference_tab != NULL)
315 gtk_widget_hide(p_preference_tab);
317 /* Show the control window */
318 gtk_widget_show( p_intf->p_sys->p_window );
320 #ifdef NEED_GTK2_MAIN
321 msg_Dbg( p_intf, "Manage GTK keyboard events using threads" );
322 while( !p_intf->b_die )
326 /* Sleep to avoid using all CPU - since some interfaces need to
327 * access keyboard events, a 100ms delay is a good compromise */
329 if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
330 msleep( INTF_IDLE_SLEEP );
336 msg_Dbg( p_intf, "Manage GTK keyboard events using timeouts" );
337 /* Sleep to avoid using all CPU - since some interfaces needs to access
338 * keyboard events, a 1000ms delay is a good compromise */
339 if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
340 i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage, p_intf );
342 i_dummy = gtk_timeout_add( 1000, (GtkFunction)Manage, p_intf );
346 /* Remove the timeout */
347 gtk_timeout_remove( i_dummy );
350 gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
351 #ifdef NEED_GTK2_MAIN
356 /*****************************************************************************
357 * GtkAutoplayFile: Autoplay file depending on configuration settings
358 *****************************************************************************/
359 void GtkAutoPlayFile( vlc_object_t *p_this )
361 GtkWidget *cbautoplay;
362 intf_thread_t *p_intf;
364 vlc_list_t *p_list = vlc_list_find( p_this, VLC_OBJECT_INTF,
367 for( i_index = 0; i_index < p_list->i_count; i_index++ )
369 p_intf = (intf_thread_t *)p_list->p_values[i_index].p_object ;
371 if( strcmp( MODULE_STRING, p_intf->p_module->psz_object_name ) )
375 cbautoplay = GTK_WIDGET( gtk_object_get_data(
376 GTK_OBJECT( p_intf->p_sys->p_window ),
379 if( !config_GetInt( p_this, "pda-autoplayfile" ) )
381 p_intf->p_sys->b_autoplayfile = VLC_FALSE;
385 p_intf->p_sys->b_autoplayfile = VLC_TRUE;
387 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( cbautoplay ),
388 p_intf->p_sys->b_autoplayfile );
390 vlc_list_release( p_list );
393 /* following functions are local */
395 /*****************************************************************************
396 * Manage: manage main thread messages
397 *****************************************************************************
398 * In this function, called approx. 10 times a second, we check what the
399 * main program wanted to tell us.
400 *****************************************************************************/
401 static int Manage( intf_thread_t *p_intf )
403 GtkListStore *p_liststore;
404 vlc_mutex_lock( &p_intf->change_lock );
406 /* Update the input */
407 if( p_intf->p_sys->p_input == NULL )
409 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
412 else if( p_intf->p_sys->p_input->b_dead )
414 vlc_object_release( p_intf->p_sys->p_input );
415 p_intf->p_sys->p_input = NULL;
418 if( p_intf->p_sys->p_input )
420 input_thread_t *p_input = p_intf->p_sys->p_input;
422 vlc_mutex_lock( &p_input->object_lock );
423 if( !p_input->b_die )
426 playlist_t *p_playlist;
428 E_(GtkModeManage)( p_intf );
429 p_intf->p_sys->b_playing = 1;
431 /* update playlist interface */
432 p_playlist = (playlist_t *) vlc_object_find(
433 p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
434 if (p_playlist != NULL)
436 p_liststore = gtk_list_store_new (3,
439 G_TYPE_UINT); /* Hidden index */
440 PlaylistRebuildListStore(p_liststore, p_playlist);
441 gtk_tree_view_set_model(p_intf->p_sys->p_tvplaylist, (GtkTreeModel*) p_liststore);
442 g_object_unref(p_liststore);
443 vlc_object_release( p_playlist );
447 /* Manage the slider */
449 #define p_area p_input->p_selected_area
450 if (p_intf->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
452 /* Manage the slider for CPU_CAPABILITY_FPU hardware */
453 if( p_intf->p_sys->b_playing )
455 float newvalue = p_intf->p_sys->p_adj->value;
457 /* If the user hasn't touched the slider since the last time,
458 * then the input can safely change it */
459 if( newvalue == p_intf->p_sys->f_adj_oldvalue )
461 /* Update the value */
462 p_intf->p_sys->p_adj->value =
463 p_intf->p_sys->f_adj_oldvalue =
464 ( 100. * p_area->i_tell ) / p_area->i_size;
465 g_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
468 /* Otherwise, send message to the input if the user has
469 * finished dragging the slider */
470 else if( p_intf->p_sys->b_slider_free )
472 double f_pos = (double)newvalue / 100.0;
474 /* release the lock to be able to seek */
475 vlc_mutex_unlock( &p_input->object_lock );
476 var_SetFloat( p_input, "position", f_pos );
477 vlc_mutex_lock( &p_input->object_lock );
479 /* Update the old value */
480 p_intf->p_sys->f_adj_oldvalue = newvalue;
486 /* Manage the slider without CPU_CAPABILITY_FPU hardware */
487 if( p_intf->p_sys->b_playing )
489 off_t newvalue = p_intf->p_sys->p_adj->value;
491 /* If the user hasn't touched the slider since the last time,
492 * then the input can safely change it */
493 if( newvalue == p_intf->p_sys->i_adj_oldvalue )
495 /* Update the value */
496 p_intf->p_sys->p_adj->value =
497 p_intf->p_sys->i_adj_oldvalue =
498 ( 100 * p_area->i_tell ) / p_area->i_size;
499 g_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
502 /* Otherwise, send message to the input if the user has
503 * finished dragging the slider */
504 else if( p_intf->p_sys->b_slider_free )
506 double f_pos = (double)newvalue / 100.0;
508 /* release the lock to be able to seek */
509 vlc_mutex_unlock( &p_input->object_lock );
510 var_SetFloat( p_input, "position", f_pos );
511 vlc_mutex_lock( &p_input->object_lock );
513 /* Update the old value */
514 p_intf->p_sys->i_adj_oldvalue = newvalue;
521 vlc_mutex_unlock( &p_input->object_lock );
523 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
525 E_(GtkModeManage)( p_intf );
526 p_intf->p_sys->b_playing = 0;
529 #ifndef NEED_GTK2_MAIN
532 vlc_mutex_unlock( &p_intf->change_lock );
534 /* Prepare to die, young Skywalker */
541 vlc_mutex_unlock( &p_intf->change_lock );
546 /*****************************************************************************
547 * GtkDisplayDate: display stream date
548 *****************************************************************************
549 * This function displays the current date related to the position in
550 * the stream. It is called whenever the slider changes its value.
551 * The lock has to be taken before you call the function.
552 *****************************************************************************/
553 void E_(GtkDisplayDate)( GtkAdjustment *p_adj, gpointer userdata )
555 intf_thread_t *p_intf;
557 p_intf = (intf_thread_t*) userdata;
561 if( p_intf->p_sys->p_input )
563 char psz_time[ MSTRTIME_MAX_SIZE ];
566 i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / I64C(1000000 );
567 secstotimestr( psz_time, i_seconds );
569 gtk_label_set_text( GTK_LABEL( p_intf->p_sys->p_slider_label ),
574 /*****************************************************************************
575 * GtkModeManage: actualize the aspect of the interface whenever the input
577 *****************************************************************************
578 * The lock has to be taken before you call the function.
579 *****************************************************************************/
580 gint E_(GtkModeManage)( intf_thread_t * p_intf )
582 GtkWidget * p_slider = NULL;
583 vlc_bool_t b_control;
585 if ( p_intf->p_sys->p_window == NULL )
586 msg_Err( p_intf, "Main widget not found" );
588 p_slider = lookup_widget( p_intf->p_sys->p_window, "timeSlider");
589 if (p_slider == NULL)
590 msg_Err( p_intf, "Slider widget not found" );
592 /* controls unavailable */
595 /* show the box related to current input mode */
596 if( p_intf->p_sys->p_input )
598 /* initialize and show slider for seekable streams */
600 gtk_widget_show( GTK_WIDGET( p_slider ) );
603 /* control buttons for free pace streams */
604 b_control = p_intf->p_sys->p_input->b_can_pace_control;
606 msg_Dbg( p_intf, "stream has changed, refreshing interface" );
609 /* set control items */
610 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbRewind"), b_control );
611 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbPause"), b_control );
612 gtk_widget_set_sensitive( lookup_widget( p_intf->p_sys->p_window, "tbForward"), b_control );