libvlc: Simplify get/set/toggle_fullscreen.
[vlc.git] / src / control / video.c
1 /*****************************************************************************
2  * video.c: libvlc new API video functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * $Id$
7  *
8  * Authors: Cl´┐Żent Stenac <zorglub@videolan.org>
9  *          Filippo Carone <littlejohn@videolan.org>
10  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
11  *          Damien Fouilleul <damienf a_t videolan dot org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #include "libvlc_internal.h"
29
30 #include <vlc/libvlc.h>
31 #include <vlc_input.h>
32 #include <vlc_vout.h>
33
34 /*
35  * Remember to release the returned vout_thread_t.
36  */
37 static vout_thread_t *GetVout( libvlc_media_player_t *p_mi,
38                                libvlc_exception_t *p_exception )
39 {
40     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_exception );
41     vout_thread_t *p_vout = NULL;
42
43     if( p_input_thread )
44     {
45         p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
46         if( !p_vout )
47         {
48             libvlc_exception_raise( p_exception, "No active video output" );
49         }
50         vlc_object_release( p_input_thread );
51     }
52     return p_vout;
53 }
54
55 /**********************************************************************
56  * Exported functions
57  **********************************************************************/
58
59 void libvlc_set_fullscreen( libvlc_media_player_t *p_mi, int b_fullscreen,
60                             libvlc_exception_t *p_e )
61 {
62     /* We only work on the first vout */
63     vout_thread_t *p_vout = GetVout( p_mi, p_e );
64
65     /* GetVout will raise the exception for us */
66     if( !p_vout ) return;
67
68     var_SetBool( p_vout, "fullscreen", b_fullscreen );
69
70     vlc_object_release( p_vout );
71 }
72
73 int libvlc_get_fullscreen( libvlc_media_player_t *p_mi,
74                             libvlc_exception_t *p_e )
75 {
76     /* We only work on the first vout */
77     vout_thread_t *p_vout = GetVout( p_mi, p_e );
78     int i_ret;
79
80     /* GetVout will raise the exception for us */
81     if( !p_vout )
82         return 0;
83
84     i_ret = var_GetBool( p_vout, "fullscreen", &val );
85
86     vlc_object_release( p_vout );
87
88     return i_ret;
89 }
90
91 void libvlc_toggle_fullscreen( libvlc_media_player_t *p_mi,
92                                libvlc_exception_t *p_e )
93 {
94     /* We only work on the first vout */
95     vout_thread_t *p_vout = GetVout( p_mi, p_e );
96     bool ret;
97
98     /* GetVout will raise the exception for us */
99     if( !p_vout ) return;
100
101     ret = var_GetBool( p_vout, "fullscreen", &val );
102     var_SetBool( p_vout1, "fullscreen", !ret );
103
104     vlc_object_release( p_vout );
105 }
106
107 void
108 libvlc_video_take_snapshot( libvlc_media_player_t *p_mi, char *psz_filepath,
109         unsigned int i_width, unsigned int i_height, libvlc_exception_t *p_e )
110 {
111     vout_thread_t *p_vout = GetVout( p_mi, p_e );
112     input_thread_t *p_input_thread;
113
114     /* GetVout will raise the exception for us */
115     if( !p_vout )
116     {
117         return;
118     }
119
120     if( !psz_filepath )
121     {
122         libvlc_exception_raise( p_e, "filepath is null" );
123         return;
124     }
125
126     var_SetInteger( p_vout, "snapshot-width", i_width );
127     var_SetInteger( p_vout, "snapshot-height", i_height );
128
129     p_input_thread = p_mi->p_input_thread;
130     if( !p_mi->p_input_thread )
131     {
132         libvlc_exception_raise( p_e, "Input does not exist" );
133         return;
134     }
135
136     var_SetString( p_vout, "snapshot-path", psz_filepath );
137     var_SetString( p_vout, "snapshot-format", "png" );
138
139     vout_Control( p_vout, VOUT_SNAPSHOT );
140     vlc_object_release( p_vout );
141 }
142
143 int libvlc_video_get_height( libvlc_media_player_t *p_mi,
144                              libvlc_exception_t *p_e )
145 {
146     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
147     if( !p_vout1 )
148         return 0;
149
150     vlc_object_release( p_vout1 );
151
152     return p_vout1->i_window_height;
153 }
154
155 int libvlc_video_get_width( libvlc_media_player_t *p_mi,
156                             libvlc_exception_t *p_e )
157 {
158     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
159     if( !p_vout1 )
160         return 0;
161
162     vlc_object_release( p_vout1 );
163
164     return p_vout1->i_window_width;
165 }
166
167 int libvlc_media_player_has_vout( libvlc_media_player_t *p_mi,
168                                      libvlc_exception_t *p_e )
169 {
170     input_thread_t *p_input_thread = libvlc_get_input_thread(p_mi, p_e);
171     bool has_vout = false;
172
173     if( p_input_thread )
174     {
175         vout_thread_t *p_vout;
176
177         p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
178         if( p_vout )
179         {
180             has_vout = true;
181             vlc_object_release( p_vout );
182         }
183         vlc_object_release( p_input_thread );
184     }
185     return has_vout;
186 }
187
188 int libvlc_video_reparent( libvlc_media_player_t *p_mi, libvlc_drawable_t d,
189                            libvlc_exception_t *p_e )
190 {
191     vout_thread_t *p_vout = GetVout( p_mi, p_e );
192
193     if( p_vout )
194     {
195         vout_Control( p_vout , VOUT_REPARENT, d);
196         vlc_object_release( p_vout );
197     }
198     return 0;
199 }
200
201 void libvlc_video_resize( libvlc_media_player_t *p_mi, int width, int height, libvlc_exception_t *p_e )
202 {
203     vout_thread_t *p_vout = GetVout( p_mi, p_e );
204     if( p_vout )
205     {
206         vout_Control( p_vout, VOUT_SET_SIZE, width, height );
207         vlc_object_release( p_vout );
208     }
209 }
210
211 void libvlc_video_redraw_rectangle( libvlc_media_player_t *p_mi,
212                            const libvlc_rectangle_t *area,
213                            libvlc_exception_t *p_e )
214 {
215     if( (NULL != area)
216      && ((area->bottom - area->top) > 0)
217      && ((area->right - area->left) > 0) )
218     {
219         vout_thread_t *p_vout = GetVout( p_mi, p_e );
220         if( p_vout )
221         {
222             /* tell running vout to redraw area */
223             vout_Control( p_vout , VOUT_REDRAW_RECT,
224                                area->top, area->left, area->bottom, area->right );
225             vlc_object_release( p_vout );
226         }
227     }
228 }
229
230 /* global video settings */
231
232 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
233                               libvlc_exception_t *p_e )
234 {
235     /* set as default for future vout instances */
236     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
237
238     if( libvlc_playlist_isplaying(p_instance, p_e) )
239     {
240         libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
241         if( p_mi )
242         {
243             vout_thread_t *p_vout = GetVout( p_mi, p_e );
244             if( p_vout )
245             {
246                 /* tell running vout to re-parent */
247                 vout_Control( p_vout , VOUT_REPARENT, d);
248                 vlc_object_release( p_vout );
249             }
250             libvlc_media_player_release(p_mi);
251         }
252     }
253 }
254
255 libvlc_drawable_t libvlc_video_get_parent( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
256 {
257     VLC_UNUSED(p_e);
258
259     libvlc_drawable_t result;
260  
261     result = var_GetInteger( p_instance->p_libvlc_int, "drawable" );
262  
263     return result;
264 }
265
266
267 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
268                            libvlc_exception_t *p_e )
269 {
270     /* set as default for future vout instances */
271     config_PutInt(p_instance->p_libvlc_int, "width", width);
272     config_PutInt(p_instance->p_libvlc_int, "height", height);
273
274     if( libvlc_playlist_isplaying(p_instance, p_e) )
275     {
276         libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
277         if( p_mi )
278         {
279             vout_thread_t *p_vout = GetVout( p_mi, p_e );
280             if( p_vout )
281             {
282                 /* tell running vout to re-size */
283                 vout_Control( p_vout , VOUT_SET_SIZE, width, height);
284                 vlc_object_release( p_vout );
285             }
286             libvlc_media_player_release(p_mi);
287         }
288     }
289 }
290
291 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
292                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
293                            libvlc_exception_t *p_e )
294 {
295     if( NULL == view )
296     {
297         libvlc_exception_raise( p_e, "viewport is NULL" );
298     }
299
300     /* if clip is NULL, then use view rectangle as clip */
301     if( NULL == clip )
302         clip = view;
303
304     /* set as default for future vout instances */
305     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
306     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
307     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
308     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
309     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
310     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
311     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
312     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
313
314     if( libvlc_playlist_isplaying(p_instance, p_e) )
315     {
316         libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
317         if( p_mi )
318         {
319             vout_thread_t *p_vout = GetVout( p_mi, p_e );
320             if( p_vout )
321             {
322                 /* change viewport for running vout */
323                 vout_Control( p_vout , VOUT_SET_VIEWPORT,
324                                    view->top, view->left, view->bottom, view->right,
325                                    clip->top, clip->left, clip->bottom, clip->right );
326                 vlc_object_release( p_vout );
327             }
328             libvlc_media_player_release(p_mi);
329         }
330     }
331 }
332
333 char *libvlc_video_get_aspect_ratio( libvlc_media_player_t *p_mi,
334                                      libvlc_exception_t *p_e )
335 {
336     char *psz_aspect = 0;
337     vout_thread_t *p_vout = GetVout( p_mi, p_e );
338
339     if( !p_vout )
340         return 0;
341
342     psz_aspect = var_GetNonEmptyString( p_vout, "aspect-ratio" );
343     vlc_object_release( p_vout );
344     return psz_aspect ? psz_aspect : strdup("");
345 }
346
347 void libvlc_video_set_aspect_ratio( libvlc_media_player_t *p_mi,
348                                     char *psz_aspect, libvlc_exception_t *p_e )
349 {
350     vout_thread_t *p_vout = GetVout( p_mi, p_e );
351     int i_ret = -1;
352
353     if( !p_vout )
354         return;
355
356     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
357     if( i_ret )
358         libvlc_exception_raise( p_e,
359                         "Unexpected error while setting aspect-ratio value" );
360
361     vlc_object_release( p_vout );
362 }
363
364 int libvlc_video_get_spu( libvlc_media_player_t *p_mi,
365                           libvlc_exception_t *p_e )
366 {
367     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
368     vlc_value_t val_list;
369     vlc_value_t val;
370     int i_spu = -1;
371     int i_ret = -1;
372     int i;
373
374     if( !p_input_thread )
375         return -1;
376
377     i_ret = var_Get( p_input_thread, "spu-es", &val );
378     if( i_ret < 0 )
379     {
380         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
381         vlc_object_release( p_input_thread );
382         return i_ret;
383     }
384
385     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
386     for( i = 0; i < val_list.p_list->i_count; i++ )
387     {
388         vlc_value_t spu_val = val_list.p_list->p_values[i];
389         if( val.i_int == spu_val.i_int )
390         {
391             i_spu = i;
392             break;
393         }
394     }
395     vlc_object_release( p_input_thread );
396     return i_spu;
397 }
398
399 void libvlc_video_set_spu( libvlc_media_player_t *p_mi, int i_spu,
400                            libvlc_exception_t *p_e )
401 {
402     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
403     vlc_value_t val_list;
404     int i_ret = -1;
405     int i;
406
407     if( !p_input_thread )
408         return;
409
410     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
411     for( i = 0; i < val_list.p_list->i_count; i++ )
412     {
413         vlc_value_t val = val_list.p_list->p_values[i];
414         if( i_spu == i )
415         {
416             vlc_value_t new_val;
417
418             new_val.i_int = val.i_int;
419             i_ret = var_Set( p_input_thread, "spu-es", new_val );
420             if( i_ret < 0 )
421             {
422                 libvlc_exception_raise( p_e, "Setting subtitle value failed" );
423             }
424             vlc_object_release( p_input_thread );
425             return;
426         }
427     }
428     libvlc_exception_raise( p_e, "Subtitle value out of range" );
429     vlc_object_release( p_input_thread );
430 }
431
432 int libvlc_video_set_subtitle_file( libvlc_media_player_t *p_mi,
433                                     char *psz_subtitle,
434                                     libvlc_exception_t *p_e )
435 {
436     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
437     bool b_ret = false;
438
439     if( p_input_thread )
440     {
441         if( input_AddSubtitles( p_input_thread, psz_subtitle, true ) )
442             b_ret = true;
443         vlc_object_release( p_input_thread );
444     }
445     return b_ret;
446 }
447
448 char *libvlc_video_get_crop_geometry( libvlc_media_player_t *p_mi,
449                                    libvlc_exception_t *p_e )
450 {
451     char *psz_geometry = 0;
452     vout_thread_t *p_vout = GetVout( p_mi, p_e );
453
454     if( !p_vout )
455         return 0;
456
457     psz_geometry = var_GetNonEmptyString( p_vout, "crop" );
458     vlc_object_release( p_vout );
459     return psz_geometry ? psz_geometry : strdup("");
460 }
461
462 void libvlc_video_set_crop_geometry( libvlc_media_player_t *p_mi,
463                                     char *psz_geometry, libvlc_exception_t *p_e )
464 {
465     vout_thread_t *p_vout = GetVout( p_mi, p_e );
466     int i_ret = -1;
467
468     if( !p_vout )
469         return;
470
471     i_ret = var_SetString( p_vout, "crop", psz_geometry );
472     if( i_ret )
473         libvlc_exception_raise( p_e,
474                         "Unexpected error while setting crop geometry" );
475
476     vlc_object_release( p_vout );
477 }
478
479 int libvlc_video_get_teletext( libvlc_media_player_t *p_mi,
480                                libvlc_exception_t *p_e )
481 {
482     vout_thread_t *p_vout = GetVout( p_mi, p_e );
483     vlc_object_t *p_vbi;
484     int i_ret = -1;
485
486     if( !p_vout )
487         return i_ret;
488
489     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
490                                                    FIND_ANYWHERE );
491     if( p_vbi )
492     {
493         i_ret = var_GetInteger( p_vbi, "vbi-page" );
494         vlc_object_release( p_vbi );
495     }
496
497     vlc_object_release( p_vout );
498     return i_ret;
499 }
500
501 void libvlc_video_set_teletext( libvlc_media_player_t *p_mi, int i_page,
502                                 libvlc_exception_t *p_e )
503 {
504     vout_thread_t *p_vout = GetVout( p_mi, p_e );
505     vlc_object_t *p_vbi;
506     int i_ret = -1;
507
508     if( !p_vout )
509         return;
510
511     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
512                                                    FIND_ANYWHERE );
513     if( p_vbi )
514     {
515         i_ret = var_SetInteger( p_vbi, "vbi-page", i_page );
516         vlc_object_release( p_vbi );
517     }
518     if( i_ret )
519         libvlc_exception_raise( p_e,
520                         "Unexpected error while setting teletext page" );
521     vlc_object_release( p_vout );
522 }
523
524 void libvlc_toggle_teletext( libvlc_media_player_t *p_mi,
525                              libvlc_exception_t *p_e )
526 {
527     /* We only work on the first vout */
528     vout_thread_t *p_vout = GetVout( p_mi, p_e );
529     vlc_value_t val; int i_ret;
530
531     /* GetVout will raise the exception for us */
532     if( !p_vout )
533         return;
534
535     i_ret = var_Get( p_vout, "vbi-opaque", &val );
536     if( i_ret )
537         libvlc_exception_raise( p_e,
538                         "Unexpected error while looking up teletext value" );
539
540     val.b_bool = !val.b_bool;
541     i_ret = var_Set( p_vout, "vbi-opaque", val );
542     if( i_ret )
543         libvlc_exception_raise( p_e,
544                         "Unexpected error while setting teletext value" );
545
546     vlc_object_release( p_vout );
547 }
548
549 int libvlc_video_destroy( libvlc_media_player_t *p_mi,
550                           libvlc_exception_t *p_e )
551 {
552     vout_thread_t *p_vout = GetVout( p_mi, p_e );
553     vlc_object_detach( p_vout );
554     vlc_object_release( p_vout );
555     vlc_object_release( p_vout );
556
557     return 0;
558 }