]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* OS X users can now browse the DVD menus with the arrow keys.
[vlc] / modules / gui / macosx / vout.m
1 /*****************************************************************************
2  * vout.m: MacOS X video output plugin
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: vout.m,v 1.50 2003/06/03 23:17:43 massiot Exp $
6  *
7  * Authors: Colin Delacroix <colin@zoy.org>
8  *          Florian G. Pflug <fgp@phlo.org>
9  *          Jon Lech Johansen <jon-vl@nanocrew.net>
10  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <string.h>                                            /* strerror() */
33
34 #include <QuickTime/QuickTime.h>
35
36 #include "intf.h"
37 #include "vout.h"
38
39 #define QT_MAX_DIRECTBUFFERS 10
40 #define VL_MAX_DISPLAYS 16
41
42 struct picture_sys_t
43 {
44     void *p_info;
45     unsigned int i_size;
46
47     /* When using I420 output */
48     PlanarPixmapInfoYUV420 pixmap_i420;
49 };
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  vout_Init      ( vout_thread_t * );
55 static void vout_End       ( vout_thread_t * );
56 static int  vout_Manage    ( vout_thread_t * );
57 static void vout_Display   ( vout_thread_t *, picture_t * );
58
59 static int  CoSendRequest      ( vout_thread_t *, SEL );
60 static int  CoCreateWindow     ( vout_thread_t * );
61 static int  CoDestroyWindow    ( vout_thread_t * );
62 static int  CoToggleFullscreen ( vout_thread_t * );
63
64 static void VLCHideMouse       ( vout_thread_t *, BOOL );
65
66 static void QTScaleMatrix      ( vout_thread_t * );
67 static int  QTCreateSequence   ( vout_thread_t * );
68 static void QTDestroySequence  ( vout_thread_t * );
69 static int  QTNewPicture       ( vout_thread_t *, picture_t * );
70 static void QTFreePicture      ( vout_thread_t *, picture_t * );
71
72 /*****************************************************************************
73  * OpenVideo: allocates MacOS X video thread output method
74  *****************************************************************************
75  * This function allocates and initializes a MacOS X vout method.
76  *****************************************************************************/
77 int E_(OpenVideo) ( vlc_object_t *p_this )
78 {   
79     vout_thread_t * p_vout = (vout_thread_t *)p_this;
80     OSErr err;
81     int i_timeout;
82
83     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
84     if( p_vout->p_sys == NULL )
85     {
86         msg_Err( p_vout, "out of memory" );
87         return( 1 );
88     }
89
90     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
91
92     /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
93     for( i_timeout = 20 ; i_timeout-- ; )
94     {
95         if( NSApp == NULL )
96         {
97             msleep( INTF_IDLE_SLEEP );
98         }
99     }
100
101     if( NSApp == NULL )
102     {
103         /* no MacOS X intf, unable to communicate with MT */
104         msg_Err( p_vout, "no MacOS X interface present" );
105         free( p_vout->p_sys );
106         return( 1 );
107     }
108
109     if( [NSApp respondsToSelector: @selector(getIntf)] )
110     {
111         intf_thread_t * p_intf;
112
113         for( i_timeout = 10 ; i_timeout-- ; )
114         {
115             if( ( p_intf = [NSApp getIntf] ) == NULL )
116             {
117                 msleep( INTF_IDLE_SLEEP );
118             }
119         }
120
121         if( p_intf == NULL )
122         {
123             msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
124             free( p_vout->p_sys );
125             return( 1 );
126         }
127     }
128
129     p_vout->p_sys->h_img_descr = 
130         (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
131     p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
132     p_vout->p_sys->p_fullscreen_state = NULL;
133
134     p_vout->p_sys->b_mouse_pointer_visible = YES;
135     p_vout->p_sys->b_mouse_moved = YES;
136     p_vout->p_sys->i_time_mouse_last_moved = mdate();
137
138     /* set window size */
139     p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
140     p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
141
142     if( ( err = EnterMovies() ) != noErr )
143     {
144         msg_Err( p_vout, "EnterMovies failed: %d", err );
145         free( p_vout->p_sys->p_matrix );
146         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
147         free( p_vout->p_sys );
148         return( 1 );
149     } 
150
151     /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
152     vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
153
154     err = FindCodec( kYUV420CodecType, bestSpeedCodec,
155                         nil, &p_vout->p_sys->img_dc );
156     
157     vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
158     if( err == noErr && p_vout->p_sys->img_dc != 0 )
159     {
160         p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
161         p_vout->p_sys->i_codec = kYUV420CodecType;
162     }
163     else
164     {
165         msg_Err( p_vout, "failed to find an appropriate codec" );
166     }
167
168     if( p_vout->p_sys->img_dc == 0 )
169     {
170         free( p_vout->p_sys->p_matrix );
171         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172         free( p_vout->p_sys );
173         return VLC_EGENERIC;        
174     }
175
176     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
177     NSArray * o_screens = [NSScreen screens];
178     if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
179     {
180         int i = 1;
181         vlc_value_t val, text;
182         NSScreen * o_screen;
183
184         int i_option = config_GetInt( p_vout, "macosx-vdev" );
185
186         var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
187                                             VLC_VAR_HASCHOICE ); 
188         text.psz_string = _("Video device");
189         var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
190         
191         NSEnumerator * o_enumerator = [o_screens objectEnumerator];
192
193         while( (o_screen = [o_enumerator nextObject]) != NULL )
194         {
195             char psz_temp[255];
196             NSRect s_rect = [o_screen frame];
197
198             snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1, 
199                       "%s %d (%dx%d)", _("Screen"), i,
200                       (int)s_rect.size.width, (int)s_rect.size.height ); 
201
202             text.psz_string = psz_temp;
203             val.i_int = i;
204             var_Change( p_vout, "video-device",
205                         VLC_VAR_ADDCHOICE, &val, &text );
206
207             if( ( i - 1 ) == i_option )
208             {
209                 var_Set( p_vout, "video-device", val );
210             }
211             i++;
212         }
213
214         var_AddCallback( p_vout, "video-device", vout_VarCallback,
215                          NULL );
216
217         val.b_bool = VLC_TRUE;
218         var_Set( p_vout, "intf-change", val );
219     }
220     [o_pool release];
221
222     if( CoCreateWindow( p_vout ) )
223     {
224         msg_Err( p_vout, "unable to create window" );
225         free( p_vout->p_sys->p_matrix );
226         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
227         free( p_vout->p_sys ); 
228         return( 1 );
229     }
230
231     p_vout->pf_init = vout_Init;
232     p_vout->pf_end = vout_End;
233     p_vout->pf_manage = vout_Manage;
234     p_vout->pf_render = NULL;
235     p_vout->pf_display = vout_Display;
236
237     return( 0 );
238 }
239
240 /*****************************************************************************
241  * vout_Init: initialize video thread output method
242  *****************************************************************************/
243 static int vout_Init( vout_thread_t *p_vout )
244 {
245     int i_index;
246     picture_t *p_pic;
247
248     I_OUTPUTPICTURES = 0;
249
250     /* Initialize the output structure; we already found a codec,
251      * and the corresponding chroma we will be using. Since we can
252      * arbitrary scale, stick to the coordinates and aspect. */
253     p_vout->output.i_width  = p_vout->render.i_width;
254     p_vout->output.i_height = p_vout->render.i_height;
255     p_vout->output.i_aspect = p_vout->render.i_aspect;
256
257     SetPort( p_vout->p_sys->p_qdport );
258     QTScaleMatrix( p_vout );
259
260     if( QTCreateSequence( p_vout ) )
261     {
262         msg_Err( p_vout, "unable to create sequence" );
263         return( 1 );
264     }
265
266     /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
267     while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
268     {
269         p_pic = NULL;
270
271         /* Find an empty picture slot */
272         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
273         {
274             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
275             {
276                 p_pic = p_vout->p_picture + i_index;
277                 break;
278             }
279         }
280
281         /* Allocate the picture */
282         if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
283         {
284             break;
285         }
286
287         p_pic->i_status = DESTROYED_PICTURE;
288         p_pic->i_type   = DIRECT_PICTURE;
289
290         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
291
292         I_OUTPUTPICTURES++;
293     }
294
295     return( 0 );
296 }
297
298 /*****************************************************************************
299  * vout_End: terminate video thread output method
300  *****************************************************************************/
301 static void vout_End( vout_thread_t *p_vout )
302 {
303     int i_index;
304
305     QTDestroySequence( p_vout );
306
307     /* Free the direct buffers we allocated */
308     for( i_index = I_OUTPUTPICTURES; i_index; )
309     {
310         i_index--;
311         QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
312     }
313 }
314
315 /*****************************************************************************
316  * CloseVideo: destroy video thread output method
317  *****************************************************************************/
318 void E_(CloseVideo) ( vlc_object_t *p_this )
319 {       
320     vout_thread_t * p_vout = (vout_thread_t *)p_this;     
321
322     if( CoDestroyWindow( p_vout ) )
323     {
324         msg_Err( p_vout, "unable to destroy window" );
325     }
326
327     if ( p_vout->p_sys->p_fullscreen_state != NULL )
328         EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
329
330     ExitMovies();
331
332     free( p_vout->p_sys->p_matrix );
333     DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
334
335     free( p_vout->p_sys );
336 }
337
338 /*****************************************************************************
339  * vout_Manage: handle events
340  *****************************************************************************
341  * This function should be called regularly by video output thread. It manages
342  * console events. It returns a non null value on error.
343  *****************************************************************************/
344 static int vout_Manage( vout_thread_t *p_vout )
345 {
346     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
347     {
348         if( CoToggleFullscreen( p_vout ) )  
349         {
350             return( 1 );
351         }
352
353         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
354     }
355
356     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
357     {
358         QTScaleMatrix( p_vout );
359         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
360                             p_vout->p_sys->p_matrix );
361  
362         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
363     }
364
365     /* hide/show mouse cursor 
366      * this code looks unnecessarily complicated, but is necessary like this.
367      * it has to deal with multiple monitors and therefore checks a lot */
368     if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
369     {
370         if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 && 
371                    p_vout->p_sys->b_mouse_pointer_visible )
372         {
373             VLCHideMouse( p_vout, YES );
374         }
375         else if ( !p_vout->p_sys->b_mouse_pointer_visible )
376         {
377             vlc_bool_t b_playing = NO;
378             playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
379                                                                     FIND_ANYWHERE );
380
381             if ( p_playlist != nil )
382             {
383                 vlc_mutex_lock( &p_playlist->object_lock );
384                 if( p_playlist->p_input != NULL )
385                 {
386                     vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
387                     b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
388                     vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
389                 }
390                 vlc_mutex_unlock( &p_playlist->object_lock );
391                 vlc_object_release( p_playlist );
392             }
393             if ( !b_playing )
394             {
395                 VLCHideMouse( p_vout, NO );
396             }
397         }
398     }
399     else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
400     {
401         if( !p_vout->p_sys->b_mouse_pointer_visible )
402         {
403             VLCHideMouse( p_vout, NO );
404         }
405         else
406         {
407             p_vout->p_sys->b_mouse_moved = NO;
408         }
409     }
410     
411     return( 0 );
412 }
413
414 /*****************************************************************************
415  * vout_Display: displays previously rendered output
416  *****************************************************************************
417  * This function sends the currently rendered image to the display.
418  *****************************************************************************/
419 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
420 {
421     OSErr err;
422     CodecFlags flags;
423
424     if( ( err = DecompressSequenceFrameS( 
425                     p_vout->p_sys->i_seq,
426                     p_pic->p_sys->p_info,
427                     p_pic->p_sys->i_size,                    
428                     codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
429     {
430         msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
431     }
432     else
433     {
434         QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
435     }
436 }
437
438 /*****************************************************************************
439  * CoSendRequest: send request to interface thread
440  *****************************************************************************
441  * Returns 0 on success, 1 otherwise
442  *****************************************************************************/
443 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
444 {
445     int i_ret = 0;
446
447     VLCVout * o_vlv = [[VLCVout alloc] init];
448
449     if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
450     {
451         msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
452     }
453
454     [o_vlv release];
455
456     return( i_ret );
457 }
458
459 /*****************************************************************************
460  * CoCreateWindow: create new window 
461  *****************************************************************************
462  * Returns 0 on success, 1 otherwise
463  *****************************************************************************/
464 static int CoCreateWindow( vout_thread_t *p_vout )
465 {
466     if( CoSendRequest( p_vout, @selector(createWindow:) ) )
467     {
468         msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
469         return( 1 );
470     }
471
472     return( 0 );
473 }
474
475 /*****************************************************************************
476  * CoDestroyWindow: destroy window 
477  *****************************************************************************
478  * Returns 0 on success, 1 otherwise
479  *****************************************************************************/
480 static int CoDestroyWindow( vout_thread_t *p_vout )
481 {
482     if( !p_vout->p_sys->b_mouse_pointer_visible )
483     {
484         VLCHideMouse( p_vout, NO );
485     }
486
487     if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
488     {
489         msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
490         return( 1 );
491     }
492
493     return( 0 );
494 }
495
496 /*****************************************************************************
497  * CoToggleFullscreen: toggle fullscreen 
498  *****************************************************************************
499  * Returns 0 on success, 1 otherwise
500  *****************************************************************************/
501 static int CoToggleFullscreen( vout_thread_t *p_vout )
502 {
503     QTDestroySequence( p_vout );
504
505     if( CoDestroyWindow( p_vout ) )
506     {
507         msg_Err( p_vout, "unable to destroy window" );
508         return( 1 );
509     }
510     
511     p_vout->b_fullscreen = !p_vout->b_fullscreen;
512
513     config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
514
515     if( CoCreateWindow( p_vout ) )
516     {
517         msg_Err( p_vout, "unable to create window" );
518         return( 1 );
519     }
520
521     SetPort( p_vout->p_sys->p_qdport );
522     QTScaleMatrix( p_vout );
523
524     if( QTCreateSequence( p_vout ) )
525     {
526         msg_Err( p_vout, "unable to create sequence" );
527         return( 1 ); 
528     } 
529
530     return( 0 );
531 }
532
533 /*****************************************************************************
534  * VLCHideMouse: if b_hide then hide the cursor
535  *****************************************************************************/
536 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
537 {
538     BOOL b_inside;
539     NSRect s_rect;
540     NSPoint ml;
541     NSWindow *o_window = p_vout->p_sys->o_window;
542     NSView *o_contents = [o_window contentView];
543     
544     s_rect = [o_contents bounds];
545     ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
546     ml = [o_contents convertPoint:ml fromView:nil];
547     b_inside = [o_contents mouse: ml inRect: s_rect];
548     
549     if ( b_hide && b_inside )
550     {
551         /* only hide if mouse over VLCView */
552         [NSCursor hide];
553         p_vout->p_sys->b_mouse_pointer_visible = 0;
554     }
555     else if ( !b_hide )
556     {
557         [NSCursor unhide];
558         p_vout->p_sys->b_mouse_pointer_visible = 1;
559     }
560     p_vout->p_sys->b_mouse_moved = NO;
561     p_vout->p_sys->i_time_mouse_last_moved = mdate();
562     return;
563 }
564
565 /*****************************************************************************
566  * QTScaleMatrix: scale matrix 
567  *****************************************************************************/
568 static void QTScaleMatrix( vout_thread_t *p_vout )
569 {
570     Rect s_rect;
571     unsigned int i_width, i_height;
572     Fixed factor_x, factor_y;
573     unsigned int i_offset_x = 0;
574     unsigned int i_offset_y = 0;
575
576     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
577
578     i_width = s_rect.right - s_rect.left;
579     i_height = s_rect.bottom - s_rect.top;
580
581     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
582     {
583         int i_adj_width = i_height * p_vout->output.i_aspect /
584                           VOUT_ASPECT_FACTOR;
585
586         factor_x = FixDiv( Long2Fix( i_adj_width ),
587                            Long2Fix( p_vout->output.i_width ) );
588         factor_y = FixDiv( Long2Fix( i_height ),
589                            Long2Fix( p_vout->output.i_height ) );
590
591         i_offset_x = (i_width - i_adj_width) / 2;
592     }
593     else
594     {
595         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
596                            p_vout->output.i_aspect;
597
598         factor_x = FixDiv( Long2Fix( i_width ),
599                            Long2Fix( p_vout->output.i_width ) );
600         factor_y = FixDiv( Long2Fix( i_adj_height ),
601                            Long2Fix( p_vout->output.i_height ) );
602
603         i_offset_y = (i_height - i_adj_height) / 2;
604     }
605     
606     SetIdentityMatrix( p_vout->p_sys->p_matrix );
607
608     ScaleMatrix( p_vout->p_sys->p_matrix,
609                  factor_x, factor_y,
610                  Long2Fix(0), Long2Fix(0) );            
611
612     TranslateMatrix( p_vout->p_sys->p_matrix, 
613                      Long2Fix(i_offset_x), 
614                      Long2Fix(i_offset_y) );
615 }
616
617 /*****************************************************************************
618  * QTCreateSequence: create a new sequence 
619  *****************************************************************************
620  * Returns 0 on success, 1 otherwise
621  *****************************************************************************/
622 static int QTCreateSequence( vout_thread_t *p_vout )
623 {
624     OSErr err;
625     ImageDescriptionPtr p_descr;
626
627     HLock( (Handle)p_vout->p_sys->h_img_descr );
628     p_descr = *p_vout->p_sys->h_img_descr;
629
630     p_descr->idSize = sizeof(ImageDescription);
631     p_descr->cType = p_vout->p_sys->i_codec;
632     p_descr->version = 1;
633     p_descr->revisionLevel = 0;
634     p_descr->vendor = 'appl';
635     p_descr->width = p_vout->output.i_width;
636     p_descr->height = p_vout->output.i_height;
637     p_descr->hRes = Long2Fix(72);
638     p_descr->vRes = Long2Fix(72);
639     p_descr->spatialQuality = codecLosslessQuality;
640     p_descr->frameCount = 1;
641     p_descr->clutID = -1;
642     p_descr->dataSize = 0;
643     p_descr->depth = 24;
644
645     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
646
647     if( ( err = DecompressSequenceBeginS( 
648                               &p_vout->p_sys->i_seq,
649                               p_vout->p_sys->h_img_descr,
650                               NULL, 0,
651                               p_vout->p_sys->p_qdport,
652                               NULL, NULL,
653                               p_vout->p_sys->p_matrix,
654                               0, NULL,
655                               codecFlagUseImageBuffer,
656                               codecLosslessQuality,
657                               p_vout->p_sys->img_dc ) ) )
658     {
659         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
660         return( 1 );
661     }
662
663     return( 0 );
664 }
665
666 /*****************************************************************************
667  * QTDestroySequence: destroy sequence 
668  *****************************************************************************/
669 static void QTDestroySequence( vout_thread_t *p_vout )
670 {
671     CDSequenceEnd( p_vout->p_sys->i_seq );
672 }
673
674 /*****************************************************************************
675  * QTNewPicture: allocate a picture
676  *****************************************************************************
677  * Returns 0 on success, 1 otherwise
678  *****************************************************************************/
679 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
680 {
681     int i_width  = p_vout->output.i_width;
682     int i_height = p_vout->output.i_height;
683
684     /* We know the chroma, allocate a buffer which will be used
685      * directly by the decoder */
686     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
687
688     if( p_pic->p_sys == NULL )
689     {
690         return( -1 );
691     }
692
693     switch( p_vout->output.i_chroma )
694     {
695         case VLC_FOURCC('I','4','2','0'):
696
697             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
698             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
699
700             /* Allocate the memory buffer */
701             p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
702                                           16, i_width * i_height * 3 / 2 );
703
704             /* Y buffer */
705             p_pic->Y_PIXELS = p_pic->p_data; 
706             p_pic->p[Y_PLANE].i_lines = i_height;
707             p_pic->p[Y_PLANE].i_pitch = i_width;
708             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
709             p_pic->p[Y_PLANE].i_visible_pitch = i_width;
710
711             /* U buffer */
712             p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
713             p_pic->p[U_PLANE].i_lines = i_height / 2;
714             p_pic->p[U_PLANE].i_pitch = i_width / 2;
715             p_pic->p[U_PLANE].i_pixel_pitch = 1;
716             p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
717
718             /* V buffer */
719             p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
720             p_pic->p[V_PLANE].i_lines = i_height / 2;
721             p_pic->p[V_PLANE].i_pitch = i_width / 2;
722             p_pic->p[V_PLANE].i_pixel_pitch = 1;
723             p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
724
725             /* We allocated 3 planes */
726             p_pic->i_planes = 3;
727
728 #define P p_pic->p_sys->pixmap_i420
729             P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
730                                        - p_pic->p_sys->p_info;
731             P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
732                                         - p_pic->p_sys->p_info;
733             P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
734                                         - p_pic->p_sys->p_info;
735
736             P.componentInfoY.rowBytes = i_width;
737             P.componentInfoCb.rowBytes = i_width / 2;
738             P.componentInfoCr.rowBytes = i_width / 2;
739 #undef P
740
741             break;
742
743     default:
744         /* Unknown chroma, tell the guy to get lost */
745         free( p_pic->p_sys );
746         msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
747                  p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
748         p_pic->i_planes = 0;
749         return( -1 );
750     }
751
752     return( 0 );
753 }
754
755 /*****************************************************************************
756  * QTFreePicture: destroy a picture allocated with QTNewPicture
757  *****************************************************************************/
758 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
759 {
760     switch( p_vout->output.i_chroma )
761     {
762         case VLC_FOURCC('I','4','2','0'):
763             free( p_pic->p_data_orig );
764             break;
765     }
766
767     free( p_pic->p_sys );
768 }
769
770 /*****************************************************************************
771  * VLCWindow implementation
772  *****************************************************************************/
773 @implementation VLCWindow
774
775 - (void)setVout:(vout_thread_t *)_p_vout
776 {
777     p_vout = _p_vout;
778 }
779
780 - (vout_thread_t *)getVout
781 {
782     return( p_vout );
783 }
784
785 - (void)scaleWindowWithFactor: (float)factor
786 {
787     NSSize newsize;
788     int i_corrected_height, i_corrected_width;
789     NSPoint topleftbase;
790     NSPoint topleftscreen;
791     
792     if ( !p_vout->b_fullscreen )
793     {
794         topleftbase.x = 0;
795         topleftbase.y = [self frame].size.height;
796         topleftscreen = [self convertBaseToScreen: topleftbase];
797         
798         if( p_vout->output.i_height * p_vout->output.i_aspect > 
799                         p_vout->output.i_width * VOUT_ASPECT_FACTOR )
800         {
801             i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
802                                             VOUT_ASPECT_FACTOR;
803             newsize.width = (int) ( i_corrected_width * factor );
804             newsize.height = (int) ( p_vout->render.i_height * factor );
805         }
806         else
807         {
808             i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
809                                             p_vout->output.i_aspect;
810             newsize.width = (int) ( p_vout->render.i_width * factor );
811             newsize.height = (int) ( i_corrected_height * factor );
812         }
813     
814         [self setContentSize: newsize];
815         
816         [self setFrameTopLeftPoint: topleftscreen];
817         p_vout->i_changes |= VOUT_SIZE_CHANGE;
818     }
819 }
820
821 - (void)toggleFloatOnTop
822 {
823     if( config_GetInt( p_vout, "macosx-float" ) )
824     {
825         config_PutInt( p_vout, "macosx-float", 0 );
826         [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
827     }
828     else
829     {
830         config_PutInt( p_vout, "macosx-float", 1 );
831         [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
832     }
833 }
834
835 - (void)toggleFullscreen
836 {
837     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
838 }
839
840 - (BOOL)isFullscreen
841 {
842     return( p_vout->b_fullscreen );
843 }
844
845 - (BOOL)canBecomeKeyWindow
846 {
847     return( YES );
848 }
849
850 - (void)keyDown:(NSEvent *)o_event
851 {
852     unichar key = 0;
853     vlc_value_t val;
854
855     if( [[o_event characters] length] )
856     {
857         key = [[o_event characters] characterAtIndex: 0];
858     }
859
860     switch( key )
861     {
862         case 'f': case 'F':
863             [self toggleFullscreen];
864             break;
865
866         case (unichar)0x1b: /* escape */
867             if( [self isFullscreen] )
868             {
869                 [self toggleFullscreen];
870             }
871             break;
872
873         case 'q': case 'Q':
874             p_vout->p_vlc->b_die = VLC_TRUE;
875             break;
876
877         case ' ':
878             input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
879             break;
880
881         case (unichar)0xf700: /* arrow up */
882             val.psz_string = "UP";
883             var_Set( p_vout, "key-pressed", val );
884             break;
885
886         case (unichar)0xf701: /* arrow down */
887             val.psz_string = "DOWN";
888             var_Set( p_vout, "key-pressed", val );
889             break;
890
891         case (unichar)0xf702: /* arrow left */
892             val.psz_string = "LEFT";
893             var_Set( p_vout, "key-pressed", val );
894             break;
895
896         case (unichar)0xf703: /* arrow right */
897             val.psz_string = "RIGHT";
898             var_Set( p_vout, "key-pressed", val );
899             break;
900
901         case (unichar)0xd: /* return */
902         case (unichar)0x3: /* enter */
903             val.psz_string = "ENTER";
904             var_Set( p_vout, "key-pressed", val );
905             break;
906
907
908         default:
909             [super keyDown: o_event];
910             break;
911     }
912 }
913
914 - (void)updateTitle
915 {
916     NSMutableString * o_title;
917     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
918                                                        FIND_ANYWHERE );
919     
920     if( p_playlist == NULL )
921     {
922         return;
923     }
924
925     vlc_mutex_lock( &p_playlist->object_lock );
926     o_title = [NSMutableString stringWithUTF8String: 
927         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
928     vlc_mutex_unlock( &p_playlist->object_lock );
929
930     vlc_object_release( p_playlist );
931
932     if( o_title != nil )
933     {
934         NSRange prefix_range = [o_title rangeOfString: @"file:"];
935         if( prefix_range.location != NSNotFound )
936         {
937             [o_title deleteCharactersInRange: prefix_range];
938         }
939
940         [self setTitleWithRepresentedFilename: o_title];
941     }
942     else
943     {
944         [self setTitle:
945             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
946     }
947 }
948
949 /* This is actually the same as VLCControls::stop. */
950 - (BOOL)windowShouldClose:(id)sender
951 {
952     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
953                                                        FIND_ANYWHERE );
954     if( p_playlist == NULL )      
955     {
956         return NO;
957     }
958
959     playlist_Stop( p_playlist );
960     vlc_object_release( p_playlist );
961
962     /* The window will be closed by the intf later. */
963     return NO;
964 }
965
966 @end
967
968 /*****************************************************************************
969  * VLCView implementation
970  *****************************************************************************/
971 @implementation VLCView
972
973 - (void)drawRect:(NSRect)rect
974 {
975     vout_thread_t * p_vout;
976     id o_window = [self window];
977     p_vout = (vout_thread_t *)[o_window getVout];
978     
979     [[NSColor blackColor] set];
980     NSRectFill( rect );
981     [super drawRect: rect];
982
983     p_vout->i_changes |= VOUT_SIZE_CHANGE;
984 }
985
986 - (BOOL)acceptsFirstResponder
987 {
988     return( YES );
989 }
990
991 - (BOOL)becomeFirstResponder
992 {
993     vout_thread_t * p_vout;
994     id o_window = [self window];
995     p_vout = (vout_thread_t *)[o_window getVout];
996     
997     [o_window setAcceptsMouseMovedEvents: YES];
998     return( YES );
999 }
1000
1001 - (BOOL)resignFirstResponder
1002 {
1003     vout_thread_t * p_vout;
1004     id o_window = [self window];
1005     p_vout = (vout_thread_t *)[o_window getVout];
1006     
1007     [o_window setAcceptsMouseMovedEvents: NO];
1008     VLCHideMouse( p_vout, NO );
1009     return( YES );
1010 }
1011
1012 - (void)mouseDown:(NSEvent *)o_event
1013 {
1014     vout_thread_t * p_vout;
1015     id o_window = [self window];
1016     p_vout = (vout_thread_t *)[o_window getVout];
1017     vlc_value_t val;
1018
1019     switch( [o_event type] )
1020     {        
1021         case NSLeftMouseDown:
1022         {
1023             var_Get( p_vout, "mouse-button-down", &val );
1024             val.i_int |= 1;
1025             var_Set( p_vout, "mouse-button-down", val );
1026         }
1027         break;
1028         
1029         default:
1030             [super mouseDown: o_event];
1031         break;
1032     }
1033 }
1034
1035 - (void)otherMouseDown:(NSEvent *)o_event
1036 {
1037     /* This is not the the wheel button. you need to poll the
1038      * mouseWheel event for that. other is a third, forth or fifth button */
1039     vout_thread_t * p_vout;
1040     id o_window = [self window];
1041     p_vout = (vout_thread_t *)[o_window getVout];
1042     vlc_value_t val;
1043
1044     switch( [o_event type] )
1045     {
1046         case NSOtherMouseDown:
1047         {
1048             var_Get( p_vout, "mouse-button-down", &val );
1049             val.i_int |= 2;
1050             var_Set( p_vout, "mouse-button-down", val );
1051         }
1052         break;
1053         
1054         default:
1055             [super mouseDown: o_event];
1056         break;
1057     }
1058 }
1059
1060 - (void)rightMouseDown:(NSEvent *)o_event
1061 {
1062     vout_thread_t * p_vout;
1063     id o_window = [self window];
1064     p_vout = (vout_thread_t *)[o_window getVout];
1065     vlc_value_t val;
1066
1067     switch( [o_event type] )
1068     {
1069         case NSRightMouseDown:
1070         {
1071             var_Get( p_vout, "mouse-button-down", &val );
1072             val.i_int |= 4;
1073             var_Set( p_vout, "mouse-button-down", val );
1074         }
1075         break;
1076         
1077         default:
1078             [super mouseDown: o_event];
1079         break;
1080     }
1081 }
1082
1083 - (void)mouseUp:(NSEvent *)o_event
1084 {
1085     vout_thread_t * p_vout;
1086     id o_window = [self window];
1087     p_vout = (vout_thread_t *)[o_window getVout];
1088     vlc_value_t val;
1089
1090     switch( [o_event type] )
1091     {
1092         case NSLeftMouseUp:
1093         {
1094             vlc_value_t b_val;
1095             b_val.b_bool = VLC_TRUE;
1096             var_Set( p_vout, "mouse-clicked", b_val );
1097             
1098             var_Get( p_vout, "mouse-button-down", &val );
1099             val.i_int &= ~1;
1100             var_Set( p_vout, "mouse-button-down", val );
1101         }
1102         break;
1103                 
1104         default:
1105             [super mouseUp: o_event];
1106         break;
1107     }
1108 }
1109
1110 - (void)otherMouseUp:(NSEvent *)o_event
1111 {
1112     vout_thread_t * p_vout;
1113     id o_window = [self window];
1114     p_vout = (vout_thread_t *)[o_window getVout];
1115     vlc_value_t val;
1116
1117     switch( [o_event type] )
1118     {
1119         case NSOtherMouseUp:
1120         {
1121             var_Get( p_vout, "mouse-button-down", &val );
1122             val.i_int &= ~2;
1123             var_Set( p_vout, "mouse-button-down", val );
1124         }
1125         break;
1126                 
1127         default:
1128             [super mouseUp: o_event];
1129         break;
1130     }
1131 }
1132
1133 - (void)rightMouseUp:(NSEvent *)o_event
1134 {
1135     vout_thread_t * p_vout;
1136     id o_window = [self window];
1137     p_vout = (vout_thread_t *)[o_window getVout];
1138     vlc_value_t val;
1139
1140     switch( [o_event type] )
1141     {
1142         case NSRightMouseUp:
1143         {
1144             var_Get( p_vout, "mouse-button-down", &val );
1145             val.i_int &= ~4;
1146             var_Set( p_vout, "mouse-button-down", val );
1147         }
1148         break;
1149         
1150         default:
1151             [super mouseUp: o_event];
1152         break;
1153     }
1154 }
1155
1156 - (void)mouseDragged:(NSEvent *)o_event
1157 {
1158     [self mouseMoved:o_event];
1159 }
1160
1161 - (void)otherMouseDragged:(NSEvent *)o_event
1162 {
1163     [self mouseMoved:o_event];
1164 }
1165
1166 - (void)rightMouseDragged:(NSEvent *)o_event
1167 {
1168     [self mouseMoved:o_event];
1169 }
1170
1171 - (void)mouseMoved:(NSEvent *)o_event
1172 {
1173     NSPoint ml;
1174     NSRect s_rect;
1175     BOOL b_inside;
1176
1177     vout_thread_t * p_vout;
1178     id o_window = [self window];
1179     p_vout = (vout_thread_t *)[o_window getVout];
1180
1181     s_rect = [self bounds];
1182     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1183     b_inside = [self mouse: ml inRect: s_rect];
1184
1185     if( b_inside )
1186     {
1187         vlc_value_t val;
1188         int i_width, i_height, i_x, i_y;
1189         
1190         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1191                                    (unsigned int)s_rect.size.height,
1192                                    &i_x, &i_y, &i_width, &i_height );
1193
1194         val.i_int = ( ((int)ml.x) - i_x ) * 
1195                     p_vout->render.i_width / i_width;
1196         var_Set( p_vout, "mouse-x", val );
1197
1198         val.i_int = ( ((int)ml.y) - i_y ) * 
1199                     p_vout->render.i_height / i_height;
1200         var_Set( p_vout, "mouse-y", val );
1201             
1202         val.b_bool = VLC_TRUE;
1203         var_Set( p_vout, "mouse-moved", val );
1204         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1205         p_vout->p_sys->b_mouse_moved = YES;
1206     }
1207     else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1208     {
1209         /* people with multiple monitors need their mouse,
1210          * even if VLCView in fullscreen. */
1211         VLCHideMouse( p_vout, NO );
1212     }
1213     
1214     [super mouseMoved: o_event];
1215 }
1216
1217 @end
1218
1219 /*****************************************************************************
1220  * VLCVout implementation
1221  *****************************************************************************/
1222 @implementation VLCVout
1223
1224 - (void)createWindow:(NSValue *)o_value
1225 {
1226     vlc_value_t val;
1227     VLCView * o_view;
1228     NSScreen * o_screen;
1229     vout_thread_t * p_vout;
1230     vlc_bool_t b_main_screen;
1231     
1232     p_vout = (vout_thread_t *)[o_value pointerValue];
1233
1234     p_vout->p_sys->o_window = [VLCWindow alloc];
1235     [p_vout->p_sys->o_window setVout: p_vout];
1236     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1237
1238     if( var_Get( p_vout, "video-device", &val ) < 0 )
1239     {
1240         o_screen = [NSScreen mainScreen];
1241         b_main_screen = 1;
1242     }
1243     else
1244     {
1245         NSArray *o_screens = [NSScreen screens];
1246         unsigned int i_index = val.i_int;
1247         
1248         if( [o_screens count] < i_index )
1249         {
1250             o_screen = [NSScreen mainScreen];
1251             b_main_screen = 1;
1252         }
1253         else
1254         {
1255             i_index--;
1256             o_screen = [o_screens objectAtIndex: i_index];
1257             config_PutInt( p_vout, "macosx-vdev", i_index );
1258             b_main_screen = (i_index == 0);
1259         }
1260     } 
1261
1262     if( p_vout->b_fullscreen )
1263     {
1264         NSRect screen_rect = [o_screen frame];
1265         screen_rect.origin.x = screen_rect.origin.y = 0;
1266
1267         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1268             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1269                              NULL, NULL, fullScreenAllowEvents );
1270
1271         [p_vout->p_sys->o_window 
1272             initWithContentRect: screen_rect
1273             styleMask: NSBorderlessWindowMask
1274             backing: NSBackingStoreBuffered
1275             defer: NO screen: o_screen];
1276
1277         [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1278         p_vout->p_sys->b_mouse_moved = YES;
1279         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1280     }
1281     else
1282     {
1283         unsigned int i_stylemask = NSTitledWindowMask |
1284                                    NSMiniaturizableWindowMask |
1285                                    NSClosableWindowMask |
1286                                    NSResizableWindowMask;
1287         
1288         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1289             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1290         p_vout->p_sys->p_fullscreen_state = NULL;
1291
1292         [p_vout->p_sys->o_window 
1293             initWithContentRect: p_vout->p_sys->s_rect
1294             styleMask: i_stylemask
1295             backing: NSBackingStoreBuffered
1296             defer: NO screen: o_screen];
1297
1298         [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1299         
1300         if( config_GetInt( p_vout, "macosx-float" ) )
1301         {
1302             [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1303         }
1304         
1305         if( !p_vout->p_sys->b_pos_saved )   
1306         {
1307             [p_vout->p_sys->o_window center];
1308         }
1309     }
1310
1311     o_view = [[VLCView alloc] init];
1312     /* FIXME: [o_view setMenu:] */
1313     [p_vout->p_sys->o_window setContentView: o_view];
1314     [o_view autorelease];
1315
1316     [o_view lockFocus];
1317     p_vout->p_sys->p_qdport = [o_view qdPort];
1318     [o_view unlockFocus];
1319     
1320     [p_vout->p_sys->o_window updateTitle];
1321     [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1322 }
1323
1324 - (void)destroyWindow:(NSValue *)o_value
1325 {
1326     vout_thread_t * p_vout;
1327
1328     p_vout = (vout_thread_t *)[o_value pointerValue];
1329
1330     if( !p_vout->b_fullscreen )
1331     {
1332         NSRect s_rect;
1333
1334         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1335         p_vout->p_sys->s_rect.size = s_rect.size;
1336
1337         s_rect = [p_vout->p_sys->o_window frame];
1338         p_vout->p_sys->s_rect.origin = s_rect.origin;
1339
1340         p_vout->p_sys->b_pos_saved = YES;
1341     }
1342     
1343     p_vout->p_sys->p_qdport = nil;
1344     [p_vout->p_sys->o_window close];
1345     p_vout->p_sys->o_window = nil;
1346 }
1347
1348 @end