]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
b6cff3a3c3dc328888c132ecb1a581d7a7664df6
[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.26 2003/02/08 17:26:00 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
41 struct picture_sys_t
42 {
43     void *p_info;
44     unsigned int i_size;
45
46     /* When using I420 output */
47     PlanarPixmapInfoYUV420 pixmap_i420;
48 };
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  vout_Init      ( vout_thread_t * );
54 static void vout_End       ( vout_thread_t * );
55 static int  vout_Manage    ( vout_thread_t * );
56 static void vout_Display   ( vout_thread_t *, picture_t * );
57
58 static int  CoSendRequest      ( vout_thread_t *, SEL );
59 static int  CoCreateWindow     ( vout_thread_t * );
60 static int  CoDestroyWindow    ( vout_thread_t * );
61 static int  CoToggleFullscreen ( vout_thread_t * );
62
63 static void QTScaleMatrix      ( vout_thread_t * );
64 static int  QTCreateSequence   ( vout_thread_t * );
65 static void QTDestroySequence  ( vout_thread_t * );
66 static int  QTNewPicture       ( vout_thread_t *, picture_t * );
67 static void QTFreePicture      ( vout_thread_t *, picture_t * );
68
69 /*****************************************************************************
70  * OpenVideo: allocates MacOS X video thread output method
71  *****************************************************************************
72  * This function allocates and initializes a MacOS X vout method.
73  *****************************************************************************/
74 int E_(OpenVideo) ( vlc_object_t *p_this )
75 {   
76     vout_thread_t * p_vout = (vout_thread_t *)p_this;
77     OSErr err;
78     int i_timeout;
79
80     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
81     if( p_vout->p_sys == NULL )
82     {
83         msg_Err( p_vout, "out of memory" );
84         return( 1 );
85     }
86
87     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
88
89     /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
90     for( i_timeout = 20 ; i_timeout-- ; )
91     {
92         if( NSApp == NULL )
93         {
94             msleep( INTF_IDLE_SLEEP );
95         }
96     }
97
98     if( NSApp == NULL )
99     {
100         msg_Err( p_vout, "no MacOS X interface present" );
101         free( p_vout->p_sys );
102         return( 1 );
103     }
104
105     if( [NSApp respondsToSelector: @selector(getIntf)] )
106     {
107         intf_thread_t * p_intf;
108
109         for( i_timeout = 10 ; i_timeout-- ; )
110         {
111             if( ( p_intf = [NSApp getIntf] ) == NULL )
112             {
113                 msleep( INTF_IDLE_SLEEP );
114             }
115         }
116
117         if( p_intf == NULL )
118         {
119             msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
120             free( p_vout->p_sys );
121             return( 1 );
122         }
123     }
124
125     p_vout->p_sys->h_img_descr = 
126         (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
127     p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
128     p_vout->p_sys->p_fullscreen_state = NULL;
129
130     p_vout->p_sys->b_mouse_pointer_visible = 1;
131
132     /* set window size */
133     p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
134     p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
135
136     if( ( err = EnterMovies() ) != noErr )
137     {
138         msg_Err( p_vout, "EnterMovies failed: %d", err );
139         free( p_vout->p_sys->p_matrix );
140         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
141         free( p_vout->p_sys );
142         return( 1 );
143     } 
144
145     if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
146     {
147         err = FindCodec( kYUV420CodecType, bestSpeedCodec,
148                          nil, &p_vout->p_sys->img_dc );
149         if( err == noErr && p_vout->p_sys->img_dc != 0 )
150         {
151             p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
152             p_vout->p_sys->i_codec = kYUV420CodecType;
153         }
154         else
155         {
156             msg_Err( p_vout, "failed to find an appropriate codec" );
157         }
158     }
159     else
160     {
161         msg_Err( p_vout, "chroma 0x%08x not supported",
162                          p_vout->render.i_chroma );
163     }
164
165     if( p_vout->p_sys->img_dc == 0 )
166     {
167         free( p_vout->p_sys->p_matrix );
168         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
169         free( p_vout->p_sys );
170         return( 1 );        
171     }
172
173     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
174     NSArray * o_screens = [NSScreen screens];
175     if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
176     {
177         int i = 1;
178         vlc_value_t val;
179         NSScreen * o_screen;
180
181         int i_option = config_GetInt( p_vout, "macosx-vdev" );
182
183         var_Create( p_vout, "video-device", VLC_VAR_STRING |
184                                             VLC_VAR_HASCHOICE ); 
185
186         NSEnumerator * o_enumerator = [o_screens objectEnumerator];
187
188         while( (o_screen = [o_enumerator nextObject]) != NULL )
189         {
190             char psz_temp[255];
191             NSRect s_rect = [o_screen frame];
192
193             snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1, 
194                       "%s %d (%dx%d)", _("Screen"), i,
195                       (int)s_rect.size.width, (int)s_rect.size.height ); 
196
197             val.psz_string = psz_temp;
198             var_Change( p_vout, "video-device", VLC_VAR_ADDCHOICE, &val );
199
200             if( ( i - 1 ) == i_option )
201             {
202                 var_Set( p_vout, "video-device", val );
203             }
204
205             i++;
206         }
207
208         var_AddCallback( p_vout, "video-device", vout_VarCallback,
209                          NULL );
210
211         val.b_bool = VLC_TRUE;
212         var_Set( p_vout, "intf-change", val );
213     }
214     [o_pool release];
215
216     if( CoCreateWindow( p_vout ) )
217     {
218         msg_Err( p_vout, "unable to create window" );
219         free( p_vout->p_sys->p_matrix );
220         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
221         free( p_vout->p_sys ); 
222         return( 1 );
223     }
224
225     p_vout->pf_init = vout_Init;
226     p_vout->pf_end = vout_End;
227     p_vout->pf_manage = vout_Manage;
228     p_vout->pf_render = NULL;
229     p_vout->pf_display = vout_Display;
230
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * vout_Init: initialize video thread output method
236  *****************************************************************************/
237 static int vout_Init( vout_thread_t *p_vout )
238 {
239     int i_index;
240     picture_t *p_pic;
241
242     I_OUTPUTPICTURES = 0;
243
244     /* Initialize the output structure; we already found a codec,
245      * and the corresponding chroma we will be using. Since we can
246      * arbitrary scale, stick to the coordinates and aspect. */
247     p_vout->output.i_width  = p_vout->render.i_width;
248     p_vout->output.i_height = p_vout->render.i_height;
249     p_vout->output.i_aspect = p_vout->render.i_aspect;
250
251     SetPort( p_vout->p_sys->p_qdport );
252     QTScaleMatrix( p_vout );
253
254     if( QTCreateSequence( p_vout ) )
255     {
256         msg_Err( p_vout, "unable to create sequence" );
257         return( 1 );
258     }
259
260     /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
261     while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
262     {
263         p_pic = NULL;
264
265         /* Find an empty picture slot */
266         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
267         {
268             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
269             {
270                 p_pic = p_vout->p_picture + i_index;
271                 break;
272             }
273         }
274
275         /* Allocate the picture */
276         if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
277         {
278             break;
279         }
280
281         p_pic->i_status = DESTROYED_PICTURE;
282         p_pic->i_type   = DIRECT_PICTURE;
283
284         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
285
286         I_OUTPUTPICTURES++;
287     }
288
289     return( 0 );
290 }
291
292 /*****************************************************************************
293  * vout_End: terminate video thread output method
294  *****************************************************************************/
295 static void vout_End( vout_thread_t *p_vout )
296 {
297     int i_index;
298
299     QTDestroySequence( p_vout );
300
301     /* Free the direct buffers we allocated */
302     for( i_index = I_OUTPUTPICTURES; i_index; )
303     {
304         i_index--;
305         QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
306     }
307 }
308
309 /*****************************************************************************
310  * CloseVideo: destroy video thread output method
311  *****************************************************************************/
312 void E_(CloseVideo) ( vlc_object_t *p_this )
313 {       
314     vout_thread_t * p_vout = (vout_thread_t *)p_this;     
315
316     if( CoDestroyWindow( p_vout ) )
317     {
318         msg_Err( p_vout, "unable to destroy window" );
319     }
320
321     if ( p_vout->p_sys->p_fullscreen_state != NULL )
322         EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
323
324     ExitMovies();
325
326     free( p_vout->p_sys->p_matrix );
327     DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
328
329     free( p_vout->p_sys );
330 }
331
332 /*****************************************************************************
333  * vout_Manage: handle events
334  *****************************************************************************
335  * This function should be called regularly by video output thread. It manages
336  * console events. It returns a non null value on error.
337  *****************************************************************************/
338 static int vout_Manage( vout_thread_t *p_vout )
339 {    
340     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
341     {
342         if( CoToggleFullscreen( p_vout ) )  
343         {
344             return( 1 );
345         }
346
347         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
348     }
349
350     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
351     {
352         QTScaleMatrix( p_vout );
353         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
354                             p_vout->p_sys->p_matrix );
355  
356         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
357     }
358
359     /* hide/show mouse cursor */
360     if( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
361     {
362         vlc_bool_t b_change = 0;
363
364         if( !p_vout->p_sys->b_mouse_pointer_visible )
365         {
366             CGDisplayShowCursor( kCGDirectMainDisplay );
367             p_vout->p_sys->b_mouse_pointer_visible = 1;
368             b_change = 1;
369         }
370         else if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 && 
371                    p_vout->p_sys->b_mouse_pointer_visible )
372         {
373             CGDisplayHideCursor( kCGDirectMainDisplay );
374             p_vout->p_sys->b_mouse_pointer_visible = 0;
375             b_change = 1;
376         }
377
378         if( b_change )
379         {
380             p_vout->p_sys->b_mouse_moved = 0;
381             p_vout->p_sys->i_time_mouse_last_moved = 0;
382         }
383     }
384
385     return( 0 );
386 }
387
388 /*****************************************************************************
389  * vout_Display: displays previously rendered output
390  *****************************************************************************
391  * This function sends the currently rendered image to the display.
392  *****************************************************************************/
393 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
394 {
395     OSErr err;
396     CodecFlags flags;
397
398     if( ( err = DecompressSequenceFrameS( 
399                     p_vout->p_sys->i_seq,
400                     p_pic->p_sys->p_info,
401                     p_pic->p_sys->i_size,                    
402                     codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
403     {
404         msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
405     }
406     else
407     {
408         QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
409     }
410 }
411
412 /*****************************************************************************
413  * CoSendRequest: send request to interface thread
414  *****************************************************************************
415  * Returns 0 on success, 1 otherwise
416  *****************************************************************************/
417 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
418 {
419     int i_ret = 0;
420
421     VLCVout * o_vlv = [[VLCVout alloc] init];
422
423     if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
424     {
425         msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
426     }
427
428     [o_vlv release];
429
430     return( i_ret );
431 }
432
433 /*****************************************************************************
434  * CoCreateWindow: create new window 
435  *****************************************************************************
436  * Returns 0 on success, 1 otherwise
437  *****************************************************************************/
438 static int CoCreateWindow( vout_thread_t *p_vout )
439 {
440     if( CoSendRequest( p_vout, @selector(createWindow:) ) )
441     {
442         msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
443         return( 1 );
444     }
445
446     return( 0 );
447 }
448
449 /*****************************************************************************
450  * CoDestroyWindow: destroy window 
451  *****************************************************************************
452  * Returns 0 on success, 1 otherwise
453  *****************************************************************************/
454 static int CoDestroyWindow( vout_thread_t *p_vout )
455 {
456     if( !p_vout->p_sys->b_mouse_pointer_visible )
457     {
458         CGDisplayShowCursor( kCGDirectMainDisplay );
459         p_vout->p_sys->b_mouse_pointer_visible = 1;
460     }
461
462     if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
463     {
464         msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
465         return( 1 );
466     }
467
468     return( 0 );
469 }
470
471 /*****************************************************************************
472  * CoToggleFullscreen: toggle fullscreen 
473  *****************************************************************************
474  * Returns 0 on success, 1 otherwise
475  *****************************************************************************/
476 static int CoToggleFullscreen( vout_thread_t *p_vout )
477 {
478     QTDestroySequence( p_vout );
479
480     if( CoDestroyWindow( p_vout ) )
481     {
482         msg_Err( p_vout, "unable to destroy window" );
483         return( 1 );
484     }
485     
486     p_vout->b_fullscreen = !p_vout->b_fullscreen;
487
488     config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
489
490     if( CoCreateWindow( p_vout ) )
491     {
492         msg_Err( p_vout, "unable to create window" );
493         return( 1 );
494     }
495
496     SetPort( p_vout->p_sys->p_qdport );
497     QTScaleMatrix( p_vout );
498
499     if( QTCreateSequence( p_vout ) )
500     {
501         msg_Err( p_vout, "unable to create sequence" );
502         return( 1 ); 
503     } 
504
505     return( 0 );
506 }
507
508 /*****************************************************************************
509  * QTScaleMatrix: scale matrix 
510  *****************************************************************************/
511 static void QTScaleMatrix( vout_thread_t *p_vout )
512 {
513     Rect s_rect;
514     unsigned int i_width, i_height;
515     Fixed factor_x, factor_y;
516     unsigned int i_offset_x = 0;
517     unsigned int i_offset_y = 0;
518
519     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
520
521     i_width = s_rect.right - s_rect.left;
522     i_height = s_rect.bottom - s_rect.top;
523
524     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
525     {
526         int i_adj_width = i_height * p_vout->output.i_aspect /
527                           VOUT_ASPECT_FACTOR;
528
529         factor_x = FixDiv( Long2Fix( i_adj_width ),
530                            Long2Fix( p_vout->output.i_width ) );
531         factor_y = FixDiv( Long2Fix( i_height ),
532                            Long2Fix( p_vout->output.i_height ) );
533
534         i_offset_x = (i_width - i_adj_width) / 2;
535     }
536     else
537     {
538         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
539                            p_vout->output.i_aspect;
540
541         factor_x = FixDiv( Long2Fix( i_width ),
542                            Long2Fix( p_vout->output.i_width ) );
543         factor_y = FixDiv( Long2Fix( i_adj_height ),
544                            Long2Fix( p_vout->output.i_height ) );
545
546         i_offset_y = (i_height - i_adj_height) / 2;
547     }
548     
549     SetIdentityMatrix( p_vout->p_sys->p_matrix );
550
551     ScaleMatrix( p_vout->p_sys->p_matrix,
552                  factor_x, factor_y,
553                  Long2Fix(0), Long2Fix(0) );            
554
555     TranslateMatrix( p_vout->p_sys->p_matrix, 
556                      Long2Fix(i_offset_x), 
557                      Long2Fix(i_offset_y) );
558 }
559
560 /*****************************************************************************
561  * QTCreateSequence: create a new sequence 
562  *****************************************************************************
563  * Returns 0 on success, 1 otherwise
564  *****************************************************************************/
565 static int QTCreateSequence( vout_thread_t *p_vout )
566 {
567     OSErr err;
568     ImageDescriptionPtr p_descr;
569
570     HLock( (Handle)p_vout->p_sys->h_img_descr );
571     p_descr = *p_vout->p_sys->h_img_descr;
572
573     p_descr->idSize = sizeof(ImageDescription);
574     p_descr->cType = p_vout->p_sys->i_codec;
575     p_descr->version = 1;
576     p_descr->revisionLevel = 0;
577     p_descr->vendor = 'appl';
578     p_descr->width = p_vout->output.i_width;
579     p_descr->height = p_vout->output.i_height;
580     p_descr->hRes = Long2Fix(72);
581     p_descr->vRes = Long2Fix(72);
582     p_descr->spatialQuality = codecLosslessQuality;
583     p_descr->frameCount = 1;
584     p_descr->clutID = -1;
585     p_descr->dataSize = 0;
586     p_descr->depth = 24;
587
588     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
589
590     if( ( err = DecompressSequenceBeginS( 
591                               &p_vout->p_sys->i_seq,
592                               p_vout->p_sys->h_img_descr,
593                               NULL, 0,
594                               p_vout->p_sys->p_qdport,
595                               NULL, NULL,
596                               p_vout->p_sys->p_matrix,
597                               0, NULL,
598                               codecFlagUseImageBuffer,
599                               codecLosslessQuality,
600                               p_vout->p_sys->img_dc ) ) )
601     {
602         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
603         return( 1 );
604     }
605
606     return( 0 );
607 }
608
609 /*****************************************************************************
610  * QTDestroySequence: destroy sequence 
611  *****************************************************************************/
612 static void QTDestroySequence( vout_thread_t *p_vout )
613 {
614     CDSequenceEnd( p_vout->p_sys->i_seq );
615 }
616
617 /*****************************************************************************
618  * QTNewPicture: allocate a picture
619  *****************************************************************************
620  * Returns 0 on success, 1 otherwise
621  *****************************************************************************/
622 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
623 {
624     int i_width  = p_vout->output.i_width;
625     int i_height = p_vout->output.i_height;
626
627     /* We know the chroma, allocate a buffer which will be used
628      * directly by the decoder */
629     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
630
631     if( p_pic->p_sys == NULL )
632     {
633         return( -1 );
634     }
635
636     switch( p_vout->output.i_chroma )
637     {
638         case VLC_FOURCC('I','4','2','0'):
639
640             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
641             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
642
643             /* Allocate the memory buffer */
644             p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
645                                           16, i_width * i_height * 3 / 2 );
646
647             /* Y buffer */
648             p_pic->Y_PIXELS = p_pic->p_data; 
649             p_pic->p[Y_PLANE].i_lines = i_height;
650             p_pic->p[Y_PLANE].i_pitch = i_width;
651             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
652             p_pic->p[Y_PLANE].i_visible_pitch = i_width;
653
654             /* U buffer */
655             p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
656             p_pic->p[U_PLANE].i_lines = i_height / 2;
657             p_pic->p[U_PLANE].i_pitch = i_width / 2;
658             p_pic->p[U_PLANE].i_pixel_pitch = 1;
659             p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
660
661             /* V buffer */
662             p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
663             p_pic->p[V_PLANE].i_lines = i_height / 2;
664             p_pic->p[V_PLANE].i_pitch = i_width / 2;
665             p_pic->p[V_PLANE].i_pixel_pitch = 1;
666             p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
667
668             /* We allocated 3 planes */
669             p_pic->i_planes = 3;
670
671 #define P p_pic->p_sys->pixmap_i420
672             P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
673                                        - p_pic->p_sys->p_info;
674             P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
675                                         - p_pic->p_sys->p_info;
676             P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
677                                         - p_pic->p_sys->p_info;
678
679             P.componentInfoY.rowBytes = i_width;
680             P.componentInfoCb.rowBytes = i_width / 2;
681             P.componentInfoCr.rowBytes = i_width / 2;
682 #undef P
683
684             break;
685
686     default:
687         /* Unknown chroma, tell the guy to get lost */
688         free( p_pic->p_sys );
689         msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
690                  p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
691         p_pic->i_planes = 0;
692         return( -1 );
693     }
694
695     return( 0 );
696 }
697
698 /*****************************************************************************
699  * QTFreePicture: destroy a picture allocated with QTNewPicture
700  *****************************************************************************/
701 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
702 {
703     switch( p_vout->output.i_chroma )
704     {
705         case VLC_FOURCC('I','4','2','0'):
706             free( p_pic->p_data_orig );
707             break;
708     }
709
710     free( p_pic->p_sys );
711 }
712
713 /*****************************************************************************
714  * VLCWindow implementation
715  *****************************************************************************/
716 @implementation VLCWindow
717
718 - (void)setVout:(vout_thread_t *)_p_vout
719 {
720     p_vout = _p_vout;
721 }
722
723 - (vout_thread_t *)getVout
724 {
725     return( p_vout );
726 }
727
728 - (void)scaleWindowWithFactor: (float)factor
729 {
730     NSSize newsize;
731     NSPoint topleftbase;
732     NSPoint topleftscreen;
733     
734     if ( !p_vout->b_fullscreen )
735     {
736         topleftbase.x = 0;
737         topleftbase.y = [self frame].size.height;
738         topleftscreen = [self convertBaseToScreen: topleftbase];
739         
740         newsize.width = (int) ( p_vout->render.i_width * factor );
741         newsize.height = (int) ( ( p_vout->render.i_height + WINDOW_TITLE_HEIGHT ) * factor );
742         [self setContentSize: newsize];
743         
744         [self setFrameTopLeftPoint: topleftscreen];
745         p_vout->i_changes |= VOUT_SIZE_CHANGE;
746     }
747 }
748
749 - (void)toggleFullscreen
750 {
751     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
752 }
753
754 - (BOOL)isFullscreen
755 {
756     return( p_vout->b_fullscreen );
757 }
758
759 - (BOOL)canBecomeKeyWindow
760 {
761     return( YES );
762 }
763
764 - (void)keyDown:(NSEvent *)o_event
765 {
766     unichar key = 0;
767
768     if( [[o_event characters] length] )
769     {
770         key = [[o_event characters] characterAtIndex: 0];
771     }
772
773     switch( key )
774     {
775         case 'f': case 'F':
776             [self toggleFullscreen];
777             break;
778
779         case (unichar)0x1b: /* escape */
780             if( [self isFullscreen] )
781             {
782                 [self toggleFullscreen];
783             }
784             break;
785
786         case 'q': case 'Q':
787             p_vout->p_vlc->b_die = VLC_TRUE;
788             break;
789
790         case ' ':
791             input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
792             break;
793
794         default:
795             [super keyDown: o_event];
796             break;
797     }
798 }
799
800 /* This is actually the same as VLCControls::stop. */
801 - (BOOL)windowShouldClose:(id)sender
802 {
803     intf_thread_t * p_intf = [NSApp getIntf];
804     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
805                                                        FIND_ANYWHERE );
806     if( p_playlist == NULL )      
807     {
808         return NO;
809     }
810
811     playlist_Stop( p_playlist );
812     vlc_object_release( p_playlist );
813
814     /* The window will be closed by the intf later. */
815     return NO;
816 }
817
818 @end
819
820 /*****************************************************************************
821  * VLCView implementation
822  *****************************************************************************/
823 @implementation VLCView
824
825 - (void)drawRect:(NSRect)rect
826 {
827     vout_thread_t * p_vout;
828     id o_window = [self window];
829     p_vout = (vout_thread_t *)[o_window getVout];
830     
831     [[NSColor blackColor] set];
832     NSRectFill( rect );
833     [super drawRect: rect];
834
835     p_vout->i_changes |= VOUT_SIZE_CHANGE;
836 }
837
838 - (BOOL)acceptsFirstResponder
839 {
840     return( YES );
841 }
842
843 - (BOOL)becomeFirstResponder
844 {
845     [[self window] setAcceptsMouseMovedEvents: YES];
846     return( YES );
847 }
848
849 - (BOOL)resignFirstResponder
850 {
851     [[self window] setAcceptsMouseMovedEvents: NO];
852     return( YES );
853 }
854
855 - (void)mouseUp:(NSEvent *)o_event
856 {
857     vout_thread_t * p_vout;
858     id o_window = [self window];
859     p_vout = (vout_thread_t *)[o_window getVout];
860
861     switch( [o_event type] )
862     {
863         case NSLeftMouseUp:
864         {
865             vlc_value_t val;
866             val.b_bool = VLC_TRUE;
867             var_Set( p_vout, "mouse-clicked", val );        
868         }
869         break;
870
871         default:
872             [super mouseUp: o_event];
873         break;
874     }
875 }
876
877 - (void)mouseMoved:(NSEvent *)o_event
878 {
879     NSPoint ml;
880     NSRect s_rect;
881     BOOL b_inside;
882
883     vout_thread_t * p_vout;
884     id o_window = [self window];
885     p_vout = (vout_thread_t *)[o_window getVout];
886
887     s_rect = [self bounds];
888     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
889     b_inside = [self mouse: ml inRect: s_rect];
890
891     if( b_inside )
892     {
893         vlc_value_t val;
894         int i_width, i_height, i_x, i_y;
895         
896         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
897                                    (unsigned int)s_rect.size.height,
898                                    &i_x, &i_y, &i_width, &i_height );
899
900         val.i_int = ( ((int)ml.x) - i_x ) * 
901                     p_vout->render.i_width / i_width;
902         var_Set( p_vout, "mouse-x", val );
903
904         val.i_int = ( ((int)ml.y) - i_y ) * 
905                     p_vout->render.i_height / i_height;
906         var_Set( p_vout, "mouse-y", val );
907             
908         val.b_bool = VLC_TRUE;
909         var_Set( p_vout, "mouse-moved", val );
910         
911         p_vout->p_sys->i_time_mouse_last_moved = mdate();
912         p_vout->p_sys->b_mouse_moved = 1;
913     }
914     else
915     {
916         [super mouseMoved: o_event];
917     }
918 }
919
920 @end
921
922 /*****************************************************************************
923  * VLCVout implementation
924  *****************************************************************************/
925 @implementation VLCVout
926
927 - (void)createWindow:(NSValue *)o_value
928 {
929     vlc_value_t val;
930     VLCView * o_view;
931     NSScreen * o_screen;
932     vout_thread_t * p_vout;
933     id o_title;
934     vlc_bool_t b_main_screen;
935
936     intf_thread_t * p_intf = [NSApp getIntf];
937     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
938                                                FIND_ANYWHERE );
939     
940     p_vout = (vout_thread_t *)[o_value pointerValue];
941
942     p_vout->p_sys->o_window = [VLCWindow alloc];
943     [p_vout->p_sys->o_window setVout: p_vout];
944     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
945
946     if( var_Get( p_vout, "video-device", &val ) < 0 )
947     {
948         o_screen = [NSScreen mainScreen];
949         b_main_screen = 1;
950     }
951     else
952     {
953         unsigned int i_index = 0;
954         NSArray *o_screens = [NSScreen screens];
955
956         if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
957             [o_screens count] < i_index )
958         {
959             o_screen = [NSScreen mainScreen];
960             b_main_screen = 1;
961         }
962         else
963         {
964             i_index--;
965             o_screen = [o_screens objectAtIndex: i_index];
966             config_PutInt( p_vout, "macosx-vdev", i_index );
967             b_main_screen = (i_index == 0);
968         } 
969
970         free( val.psz_string );
971     } 
972
973     if( p_vout->b_fullscreen )
974     {
975         NSRect screen_rect = [o_screen frame];
976         screen_rect.origin.x = screen_rect.origin.y = 0;
977
978         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
979             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
980                              NULL, NULL, fullScreenAllowEvents );
981
982         [p_vout->p_sys->o_window 
983             initWithContentRect: screen_rect
984             styleMask: NSBorderlessWindowMask
985             backing: NSBackingStoreBuffered
986             defer: NO screen: o_screen];
987
988         [p_vout->p_sys->o_window setLevel: NSModalPanelWindowLevel];
989         p_vout->p_sys->b_mouse_moved = 1;
990         p_vout->p_sys->i_time_mouse_last_moved = mdate();
991     }
992     else
993     {
994         unsigned int i_stylemask = NSTitledWindowMask |
995                                    NSMiniaturizableWindowMask |
996                                    NSClosableWindowMask |
997                                    NSResizableWindowMask;
998         
999         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1000             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1001         p_vout->p_sys->p_fullscreen_state = NULL;
1002
1003         [p_vout->p_sys->o_window 
1004             initWithContentRect: p_vout->p_sys->s_rect
1005             styleMask: i_stylemask
1006             backing: NSBackingStoreBuffered
1007             defer: NO screen: o_screen];
1008
1009         if( !p_vout->p_sys->b_pos_saved )   
1010         {
1011             [p_vout->p_sys->o_window center];
1012         }
1013     }
1014
1015     o_view = [[VLCView alloc] init];
1016     /* FIXME: [o_view setMenu:] */
1017     [p_vout->p_sys->o_window setContentView: o_view];
1018     [o_view autorelease];
1019
1020     [o_view lockFocus];
1021     p_vout->p_sys->p_qdport = [o_view qdPort];
1022     [o_view unlockFocus];
1023
1024
1025     if( p_playlist == NULL )
1026     {
1027         return;
1028     }
1029
1030     vlc_mutex_lock( &p_playlist->object_lock );
1031     o_title = [NSString stringWithUTF8String: 
1032         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
1033     vlc_mutex_unlock( &p_playlist->object_lock ); 
1034
1035     vlc_object_release( p_playlist );
1036
1037     if (o_title)
1038     {
1039         [p_vout->p_sys->o_window setTitle: o_title];
1040         [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1041     }
1042     else
1043     {
1044         [p_vout->p_sys->o_window setTitle:
1045             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1046         [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1047     }
1048 }
1049
1050 - (void)destroyWindow:(NSValue *)o_value
1051 {
1052     vout_thread_t * p_vout;
1053
1054     p_vout = (vout_thread_t *)[o_value pointerValue];
1055
1056     if( !p_vout->b_fullscreen )
1057     {
1058         NSRect s_rect;
1059
1060         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1061         p_vout->p_sys->s_rect.size = s_rect.size;
1062
1063         s_rect = [p_vout->p_sys->o_window frame];
1064         p_vout->p_sys->s_rect.origin = s_rect.origin;
1065
1066         p_vout->p_sys->b_pos_saved = 1;
1067     }
1068     
1069     p_vout->p_sys->p_qdport = nil;
1070     [p_vout->p_sys->o_window close];
1071     p_vout->p_sys->o_window = nil;
1072 }
1073
1074 @end