]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* not < but > ;)
[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.39 2003/03/06 14:40:43 hartman 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     vlc_bool_t b_change = 0;
341     intf_thread_t * p_intf = [NSApp getIntf];
342     
343     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
344     {
345         if( CoToggleFullscreen( p_vout ) )  
346         {
347             return( 1 );
348         }
349
350         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
351     }
352
353     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
354     {
355         QTScaleMatrix( p_vout );
356         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
357                             p_vout->p_sys->p_matrix );
358  
359         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
360     }
361
362     /* hide/show mouse cursor */
363     if( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen && 
364                         p_intf->p_sys->b_play_status )
365     {
366         if( !p_vout->p_sys->b_mouse_pointer_visible )
367         {
368             CGDisplayShowCursor( kCGDirectMainDisplay );
369             p_vout->p_sys->b_mouse_pointer_visible = 1;
370             b_change = 1;
371         }
372         else if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 && 
373                    p_vout->p_sys->b_mouse_pointer_visible )
374         {
375             CGDisplayHideCursor( kCGDirectMainDisplay );
376             p_vout->p_sys->b_mouse_pointer_visible = 0;
377             b_change = 1;
378         }
379
380     }
381     else if ( p_vout->b_fullscreen && !p_intf->p_sys->b_play_status )
382     {
383         if( !p_vout->p_sys->b_mouse_pointer_visible )
384         {
385             CGDisplayShowCursor( kCGDirectMainDisplay );
386             p_vout->p_sys->b_mouse_pointer_visible = 1;
387             b_change = 1;
388         }
389         else if( p_vout->p_sys->b_mouse_pointer_visible )
390         {
391             p_vout->p_sys->i_time_mouse_last_moved = mdate();
392             p_vout->p_sys->b_mouse_moved = 1;
393         }
394     }
395     
396     if( b_change )
397     {
398         p_vout->p_sys->b_mouse_moved = 0;
399         p_vout->p_sys->i_time_mouse_last_moved = 0;
400     }
401
402     return( 0 );
403 }
404
405 /*****************************************************************************
406  * vout_Display: displays previously rendered output
407  *****************************************************************************
408  * This function sends the currently rendered image to the display.
409  *****************************************************************************/
410 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
411 {
412     OSErr err;
413     CodecFlags flags;
414
415     if( ( err = DecompressSequenceFrameS( 
416                     p_vout->p_sys->i_seq,
417                     p_pic->p_sys->p_info,
418                     p_pic->p_sys->i_size,                    
419                     codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
420     {
421         msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
422     }
423     else
424     {
425         QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
426     }
427 }
428
429 /*****************************************************************************
430  * CoSendRequest: send request to interface thread
431  *****************************************************************************
432  * Returns 0 on success, 1 otherwise
433  *****************************************************************************/
434 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
435 {
436     int i_ret = 0;
437
438     VLCVout * o_vlv = [[VLCVout alloc] init];
439
440     if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
441     {
442         msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
443     }
444
445     [o_vlv release];
446
447     return( i_ret );
448 }
449
450 /*****************************************************************************
451  * CoCreateWindow: create new window 
452  *****************************************************************************
453  * Returns 0 on success, 1 otherwise
454  *****************************************************************************/
455 static int CoCreateWindow( vout_thread_t *p_vout )
456 {
457     if( CoSendRequest( p_vout, @selector(createWindow:) ) )
458     {
459         msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
460         return( 1 );
461     }
462
463     return( 0 );
464 }
465
466 /*****************************************************************************
467  * CoDestroyWindow: destroy window 
468  *****************************************************************************
469  * Returns 0 on success, 1 otherwise
470  *****************************************************************************/
471 static int CoDestroyWindow( vout_thread_t *p_vout )
472 {
473     if( !p_vout->p_sys->b_mouse_pointer_visible )
474     {
475         CGDisplayShowCursor( kCGDirectMainDisplay );
476         p_vout->p_sys->b_mouse_pointer_visible = 1;
477     }
478
479     if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
480     {
481         msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
482         return( 1 );
483     }
484
485     return( 0 );
486 }
487
488 /*****************************************************************************
489  * CoToggleFullscreen: toggle fullscreen 
490  *****************************************************************************
491  * Returns 0 on success, 1 otherwise
492  *****************************************************************************/
493 static int CoToggleFullscreen( vout_thread_t *p_vout )
494 {
495     QTDestroySequence( p_vout );
496
497     if( CoDestroyWindow( p_vout ) )
498     {
499         msg_Err( p_vout, "unable to destroy window" );
500         return( 1 );
501     }
502     
503     p_vout->b_fullscreen = !p_vout->b_fullscreen;
504
505     config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
506
507     if( CoCreateWindow( p_vout ) )
508     {
509         msg_Err( p_vout, "unable to create window" );
510         return( 1 );
511     }
512
513     SetPort( p_vout->p_sys->p_qdport );
514     QTScaleMatrix( p_vout );
515
516     if( QTCreateSequence( p_vout ) )
517     {
518         msg_Err( p_vout, "unable to create sequence" );
519         return( 1 ); 
520     } 
521
522     return( 0 );
523 }
524
525 /*****************************************************************************
526  * QTScaleMatrix: scale matrix 
527  *****************************************************************************/
528 static void QTScaleMatrix( vout_thread_t *p_vout )
529 {
530     Rect s_rect;
531     unsigned int i_width, i_height;
532     Fixed factor_x, factor_y;
533     unsigned int i_offset_x = 0;
534     unsigned int i_offset_y = 0;
535
536     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
537
538     i_width = s_rect.right - s_rect.left;
539     i_height = s_rect.bottom - s_rect.top;
540
541     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
542     {
543         int i_adj_width = i_height * p_vout->output.i_aspect /
544                           VOUT_ASPECT_FACTOR;
545
546         factor_x = FixDiv( Long2Fix( i_adj_width ),
547                            Long2Fix( p_vout->output.i_width ) );
548         factor_y = FixDiv( Long2Fix( i_height ),
549                            Long2Fix( p_vout->output.i_height ) );
550
551         i_offset_x = (i_width - i_adj_width) / 2;
552     }
553     else
554     {
555         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
556                            p_vout->output.i_aspect;
557
558         factor_x = FixDiv( Long2Fix( i_width ),
559                            Long2Fix( p_vout->output.i_width ) );
560         factor_y = FixDiv( Long2Fix( i_adj_height ),
561                            Long2Fix( p_vout->output.i_height ) );
562
563         i_offset_y = (i_height - i_adj_height) / 2;
564     }
565     
566     SetIdentityMatrix( p_vout->p_sys->p_matrix );
567
568     ScaleMatrix( p_vout->p_sys->p_matrix,
569                  factor_x, factor_y,
570                  Long2Fix(0), Long2Fix(0) );            
571
572     TranslateMatrix( p_vout->p_sys->p_matrix, 
573                      Long2Fix(i_offset_x), 
574                      Long2Fix(i_offset_y) );
575 }
576
577 /*****************************************************************************
578  * QTCreateSequence: create a new sequence 
579  *****************************************************************************
580  * Returns 0 on success, 1 otherwise
581  *****************************************************************************/
582 static int QTCreateSequence( vout_thread_t *p_vout )
583 {
584     OSErr err;
585     ImageDescriptionPtr p_descr;
586
587     HLock( (Handle)p_vout->p_sys->h_img_descr );
588     p_descr = *p_vout->p_sys->h_img_descr;
589
590     p_descr->idSize = sizeof(ImageDescription);
591     p_descr->cType = p_vout->p_sys->i_codec;
592     p_descr->version = 1;
593     p_descr->revisionLevel = 0;
594     p_descr->vendor = 'appl';
595     p_descr->width = p_vout->output.i_width;
596     p_descr->height = p_vout->output.i_height;
597     p_descr->hRes = Long2Fix(72);
598     p_descr->vRes = Long2Fix(72);
599     p_descr->spatialQuality = codecLosslessQuality;
600     p_descr->frameCount = 1;
601     p_descr->clutID = -1;
602     p_descr->dataSize = 0;
603     p_descr->depth = 24;
604
605     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
606
607     if( ( err = DecompressSequenceBeginS( 
608                               &p_vout->p_sys->i_seq,
609                               p_vout->p_sys->h_img_descr,
610                               NULL, 0,
611                               p_vout->p_sys->p_qdport,
612                               NULL, NULL,
613                               p_vout->p_sys->p_matrix,
614                               0, NULL,
615                               codecFlagUseImageBuffer,
616                               codecLosslessQuality,
617                               p_vout->p_sys->img_dc ) ) )
618     {
619         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
620         return( 1 );
621     }
622
623     return( 0 );
624 }
625
626 /*****************************************************************************
627  * QTDestroySequence: destroy sequence 
628  *****************************************************************************/
629 static void QTDestroySequence( vout_thread_t *p_vout )
630 {
631     CDSequenceEnd( p_vout->p_sys->i_seq );
632 }
633
634 /*****************************************************************************
635  * QTNewPicture: allocate a picture
636  *****************************************************************************
637  * Returns 0 on success, 1 otherwise
638  *****************************************************************************/
639 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
640 {
641     int i_width  = p_vout->output.i_width;
642     int i_height = p_vout->output.i_height;
643
644     /* We know the chroma, allocate a buffer which will be used
645      * directly by the decoder */
646     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
647
648     if( p_pic->p_sys == NULL )
649     {
650         return( -1 );
651     }
652
653     switch( p_vout->output.i_chroma )
654     {
655         case VLC_FOURCC('I','4','2','0'):
656
657             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
658             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
659
660             /* Allocate the memory buffer */
661             p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
662                                           16, i_width * i_height * 3 / 2 );
663
664             /* Y buffer */
665             p_pic->Y_PIXELS = p_pic->p_data; 
666             p_pic->p[Y_PLANE].i_lines = i_height;
667             p_pic->p[Y_PLANE].i_pitch = i_width;
668             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
669             p_pic->p[Y_PLANE].i_visible_pitch = i_width;
670
671             /* U buffer */
672             p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
673             p_pic->p[U_PLANE].i_lines = i_height / 2;
674             p_pic->p[U_PLANE].i_pitch = i_width / 2;
675             p_pic->p[U_PLANE].i_pixel_pitch = 1;
676             p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
677
678             /* V buffer */
679             p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
680             p_pic->p[V_PLANE].i_lines = i_height / 2;
681             p_pic->p[V_PLANE].i_pitch = i_width / 2;
682             p_pic->p[V_PLANE].i_pixel_pitch = 1;
683             p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
684
685             /* We allocated 3 planes */
686             p_pic->i_planes = 3;
687
688 #define P p_pic->p_sys->pixmap_i420
689             P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
690                                        - p_pic->p_sys->p_info;
691             P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
692                                         - p_pic->p_sys->p_info;
693             P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
694                                         - p_pic->p_sys->p_info;
695
696             P.componentInfoY.rowBytes = i_width;
697             P.componentInfoCb.rowBytes = i_width / 2;
698             P.componentInfoCr.rowBytes = i_width / 2;
699 #undef P
700
701             break;
702
703     default:
704         /* Unknown chroma, tell the guy to get lost */
705         free( p_pic->p_sys );
706         msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
707                  p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
708         p_pic->i_planes = 0;
709         return( -1 );
710     }
711
712     return( 0 );
713 }
714
715 /*****************************************************************************
716  * QTFreePicture: destroy a picture allocated with QTNewPicture
717  *****************************************************************************/
718 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
719 {
720     switch( p_vout->output.i_chroma )
721     {
722         case VLC_FOURCC('I','4','2','0'):
723             free( p_pic->p_data_orig );
724             break;
725     }
726
727     free( p_pic->p_sys );
728 }
729
730 /*****************************************************************************
731  * VLCWindow implementation
732  *****************************************************************************/
733 @implementation VLCWindow
734
735 - (void)setVout:(vout_thread_t *)_p_vout
736 {
737     p_vout = _p_vout;
738 }
739
740 - (vout_thread_t *)getVout
741 {
742     return( p_vout );
743 }
744
745 - (void)scaleWindowWithFactor: (float)factor
746 {
747     NSSize newsize;
748     int i_corrected_height, i_corrected_width;
749     NSPoint topleftbase;
750     NSPoint topleftscreen;
751     
752     if ( !p_vout->b_fullscreen )
753     {
754         topleftbase.x = 0;
755         topleftbase.y = [self frame].size.height;
756         topleftscreen = [self convertBaseToScreen: topleftbase];
757         
758         if( p_vout->output.i_height * p_vout->output.i_aspect > 
759                         p_vout->output.i_width * VOUT_ASPECT_FACTOR )
760         {
761             i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
762                                             VOUT_ASPECT_FACTOR;
763             newsize.width = (int) ( i_corrected_width * factor );
764             newsize.height = (int) ( p_vout->render.i_height * factor );
765         }
766         else
767         {
768             i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
769                                             p_vout->output.i_aspect;
770             newsize.width = (int) ( p_vout->render.i_width * factor );
771             newsize.height = (int) ( i_corrected_height * factor );
772         }
773     
774         [self setContentSize: newsize];
775         
776         [self setFrameTopLeftPoint: topleftscreen];
777         p_vout->i_changes |= VOUT_SIZE_CHANGE;
778     }
779 }
780
781 - (void)toggleFullscreen
782 {
783     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
784 }
785
786 - (BOOL)isFullscreen
787 {
788     return( p_vout->b_fullscreen );
789 }
790
791 - (BOOL)canBecomeKeyWindow
792 {
793     return( YES );
794 }
795
796 - (void)keyDown:(NSEvent *)o_event
797 {
798     unichar key = 0;
799
800     if( [[o_event characters] length] )
801     {
802         key = [[o_event characters] characterAtIndex: 0];
803     }
804
805     switch( key )
806     {
807         case 'f': case 'F':
808             [self toggleFullscreen];
809             break;
810
811         case (unichar)0x1b: /* escape */
812             if( [self isFullscreen] )
813             {
814                 [self toggleFullscreen];
815             }
816             break;
817
818         case 'q': case 'Q':
819             p_vout->p_vlc->b_die = VLC_TRUE;
820             break;
821
822         case ' ':
823             input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
824             break;
825
826         default:
827             [super keyDown: o_event];
828             break;
829     }
830 }
831
832 - (void)updateTitle
833 {
834     NSMutableString * o_title;
835
836     intf_thread_t * p_intf = [NSApp getIntf];
837     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
838                                                        FIND_ANYWHERE );
839     
840     if( p_playlist == NULL )
841     {
842         return;
843     }
844
845     vlc_mutex_lock( &p_playlist->object_lock );
846     o_title = [NSMutableString stringWithUTF8String: 
847         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
848     vlc_mutex_unlock( &p_playlist->object_lock );
849
850     vlc_object_release( p_playlist );
851
852     if( o_title != nil )
853     {
854         NSRange prefix_range = [o_title rangeOfString: @"file:"];
855         if( prefix_range.location != NSNotFound )
856         {
857             [o_title deleteCharactersInRange: prefix_range];
858         }
859
860         [self setTitleWithRepresentedFilename: o_title];
861     }
862     else
863     {
864         [self setTitle:
865             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
866     }
867 }
868
869 /* This is actually the same as VLCControls::stop. */
870 - (BOOL)windowShouldClose:(id)sender
871 {
872     intf_thread_t * p_intf = [NSApp getIntf];
873     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
874                                                        FIND_ANYWHERE );
875     if( p_playlist == NULL )      
876     {
877         return NO;
878     }
879
880     playlist_Stop( p_playlist );
881     vlc_object_release( p_playlist );
882
883     /* The window will be closed by the intf later. */
884     return NO;
885 }
886
887 @end
888
889 /*****************************************************************************
890  * VLCView implementation
891  *****************************************************************************/
892 @implementation VLCView
893
894 - (void)drawRect:(NSRect)rect
895 {
896     vout_thread_t * p_vout;
897     id o_window = [self window];
898     p_vout = (vout_thread_t *)[o_window getVout];
899     
900     [[NSColor blackColor] set];
901     NSRectFill( rect );
902     [super drawRect: rect];
903
904     p_vout->i_changes |= VOUT_SIZE_CHANGE;
905 }
906
907 - (BOOL)acceptsFirstResponder
908 {
909     return( YES );
910 }
911
912 - (BOOL)becomeFirstResponder
913 {
914     [[self window] setAcceptsMouseMovedEvents: YES];
915     return( YES );
916 }
917
918 - (BOOL)resignFirstResponder
919 {
920     [[self window] setAcceptsMouseMovedEvents: NO];
921     return( YES );
922 }
923
924 - (void)mouseDown:(NSEvent *)o_event
925 {
926     vout_thread_t * p_vout;
927     id o_window = [self window];
928     p_vout = (vout_thread_t *)[o_window getVout];
929     vlc_value_t val;
930
931     switch( [o_event type] )
932     {        
933         case NSLeftMouseDown:
934         {
935             var_Get( p_vout, "mouse-button-down", &val );
936             val.i_int |= 1;
937             var_Set( p_vout, "mouse-button-down", val );
938         }
939         break;
940         
941         default:
942             [super mouseDown: o_event];
943         break;
944     }
945 }
946
947 - (void)otherMouseDown:(NSEvent *)o_event
948 {
949     /* This is not the the wheel button. you need to poll the
950      * mouseWheel event for that. other is a third, forth or fifth button */
951     vout_thread_t * p_vout;
952     id o_window = [self window];
953     p_vout = (vout_thread_t *)[o_window getVout];
954     vlc_value_t val;
955
956     switch( [o_event type] )
957     {
958         case NSOtherMouseDown:
959         {
960             var_Get( p_vout, "mouse-button-down", &val );
961             val.i_int |= 2;
962             var_Set( p_vout, "mouse-button-down", val );
963         }
964         break;
965         
966         default:
967             [super mouseDown: o_event];
968         break;
969     }
970 }
971
972 - (void)rightMouseDown:(NSEvent *)o_event
973 {
974     vout_thread_t * p_vout;
975     id o_window = [self window];
976     p_vout = (vout_thread_t *)[o_window getVout];
977     vlc_value_t val;
978
979     switch( [o_event type] )
980     {
981         case NSRightMouseDown:
982         {
983             var_Get( p_vout, "mouse-button-down", &val );
984             val.i_int |= 4;
985             var_Set( p_vout, "mouse-button-down", val );
986         }
987         break;
988         
989         default:
990             [super mouseDown: o_event];
991         break;
992     }
993 }
994
995 - (void)mouseUp:(NSEvent *)o_event
996 {
997     vout_thread_t * p_vout;
998     id o_window = [self window];
999     p_vout = (vout_thread_t *)[o_window getVout];
1000     vlc_value_t val;
1001
1002     switch( [o_event type] )
1003     {
1004         case NSLeftMouseUp:
1005         {
1006             vlc_value_t b_val;
1007             b_val.b_bool = VLC_TRUE;
1008             var_Set( p_vout, "mouse-clicked", b_val );
1009             
1010             var_Get( p_vout, "mouse-button-down", &val );
1011             val.i_int &= ~1;
1012             var_Set( p_vout, "mouse-button-down", val );
1013         }
1014         break;
1015                 
1016         default:
1017             [super mouseUp: o_event];
1018         break;
1019     }
1020 }
1021
1022 - (void)otherMouseUp:(NSEvent *)o_event
1023 {
1024     vout_thread_t * p_vout;
1025     id o_window = [self window];
1026     p_vout = (vout_thread_t *)[o_window getVout];
1027     vlc_value_t val;
1028
1029     switch( [o_event type] )
1030     {
1031         case NSOtherMouseUp:
1032         {
1033             var_Get( p_vout, "mouse-button-down", &val );
1034             val.i_int &= ~2;
1035             var_Set( p_vout, "mouse-button-down", val );
1036         }
1037         break;
1038                 
1039         default:
1040             [super mouseUp: o_event];
1041         break;
1042     }
1043 }
1044
1045 - (void)rightMouseUp:(NSEvent *)o_event
1046 {
1047     vout_thread_t * p_vout;
1048     id o_window = [self window];
1049     p_vout = (vout_thread_t *)[o_window getVout];
1050     vlc_value_t val;
1051
1052     switch( [o_event type] )
1053     {
1054         case NSRightMouseUp:
1055         {
1056             var_Get( p_vout, "mouse-button-down", &val );
1057             val.i_int &= ~4;
1058             var_Set( p_vout, "mouse-button-down", val );
1059         }
1060         break;
1061         
1062         default:
1063             [super mouseUp: o_event];
1064         break;
1065     }
1066 }
1067
1068 - (void)mouseDragged:(NSEvent *)o_event
1069 {
1070     [self mouseMoved:o_event];
1071 }
1072
1073 - (void)otherMouseDragged:(NSEvent *)o_event
1074 {
1075     [self mouseMoved:o_event];
1076 }
1077
1078 - (void)rightMouseDragged:(NSEvent *)o_event
1079 {
1080     [self mouseMoved:o_event];
1081 }
1082
1083 - (void)mouseMoved:(NSEvent *)o_event
1084 {
1085     NSPoint ml;
1086     NSRect s_rect;
1087     BOOL b_inside;
1088
1089     vout_thread_t * p_vout;
1090     id o_window = [self window];
1091     p_vout = (vout_thread_t *)[o_window getVout];
1092
1093     s_rect = [self bounds];
1094     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1095     b_inside = [self mouse: ml inRect: s_rect];
1096
1097     if( b_inside )
1098     {
1099         vlc_value_t val;
1100         int i_width, i_height, i_x, i_y;
1101         
1102         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1103                                    (unsigned int)s_rect.size.height,
1104                                    &i_x, &i_y, &i_width, &i_height );
1105
1106         val.i_int = ( ((int)ml.x) - i_x ) * 
1107                     p_vout->render.i_width / i_width;
1108         var_Set( p_vout, "mouse-x", val );
1109
1110         val.i_int = ( ((int)ml.y) - i_y ) * 
1111                     p_vout->render.i_height / i_height;
1112         var_Set( p_vout, "mouse-y", val );
1113             
1114         val.b_bool = VLC_TRUE;
1115         var_Set( p_vout, "mouse-moved", val );
1116     }
1117     p_vout->p_sys->i_time_mouse_last_moved = mdate();
1118     p_vout->p_sys->b_mouse_moved = 1;
1119     [super mouseMoved: o_event];
1120 }
1121
1122 @end
1123
1124 /*****************************************************************************
1125  * VLCVout implementation
1126  *****************************************************************************/
1127 @implementation VLCVout
1128
1129 - (void)createWindow:(NSValue *)o_value
1130 {
1131     vlc_value_t val;
1132     VLCView * o_view;
1133     NSScreen * o_screen;
1134     vout_thread_t * p_vout;
1135     vlc_bool_t b_main_screen;
1136     
1137     p_vout = (vout_thread_t *)[o_value pointerValue];
1138
1139     p_vout->p_sys->o_window = [VLCWindow alloc];
1140     [p_vout->p_sys->o_window setVout: p_vout];
1141     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1142
1143     if( var_Get( p_vout, "video-device", &val ) < 0 )
1144     {
1145         o_screen = [NSScreen mainScreen];
1146         b_main_screen = 1;
1147     }
1148     else
1149     {
1150         unsigned int i_index = 0;
1151         NSArray *o_screens = [NSScreen screens];
1152
1153         if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
1154             [o_screens count] < i_index )
1155         {
1156             o_screen = [NSScreen mainScreen];
1157             b_main_screen = 1;
1158         }
1159         else
1160         {
1161             i_index--;
1162             o_screen = [o_screens objectAtIndex: i_index];
1163             config_PutInt( p_vout, "macosx-vdev", i_index );
1164             b_main_screen = (i_index == 0);
1165         } 
1166
1167         free( val.psz_string );
1168     } 
1169
1170     if( p_vout->b_fullscreen )
1171     {
1172         NSRect screen_rect = [o_screen frame];
1173         screen_rect.origin.x = screen_rect.origin.y = 0;
1174
1175         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1176             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1177                              NULL, NULL, fullScreenAllowEvents );
1178
1179         [p_vout->p_sys->o_window 
1180             initWithContentRect: screen_rect
1181             styleMask: NSBorderlessWindowMask
1182             backing: NSBackingStoreBuffered
1183             defer: NO screen: o_screen];
1184
1185         [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1186         p_vout->p_sys->b_mouse_moved = 1;
1187         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1188     }
1189     else
1190     {
1191         unsigned int i_stylemask = NSTitledWindowMask |
1192                                    NSMiniaturizableWindowMask |
1193                                    NSClosableWindowMask |
1194                                    NSResizableWindowMask;
1195         
1196         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1197             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1198         p_vout->p_sys->p_fullscreen_state = NULL;
1199
1200         [p_vout->p_sys->o_window 
1201             initWithContentRect: p_vout->p_sys->s_rect
1202             styleMask: i_stylemask
1203             backing: NSBackingStoreBuffered
1204             defer: NO screen: o_screen];
1205
1206         if( !p_vout->p_sys->b_pos_saved )   
1207         {
1208             [p_vout->p_sys->o_window center];
1209         }
1210     }
1211
1212     o_view = [[VLCView alloc] init];
1213     /* FIXME: [o_view setMenu:] */
1214     [p_vout->p_sys->o_window setContentView: o_view];
1215     [o_view autorelease];
1216
1217     [o_view lockFocus];
1218     p_vout->p_sys->p_qdport = [o_view qdPort];
1219     [o_view unlockFocus];
1220     
1221     [p_vout->p_sys->o_window updateTitle];
1222     [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1223 }
1224
1225 - (void)destroyWindow:(NSValue *)o_value
1226 {
1227     vout_thread_t * p_vout;
1228
1229     p_vout = (vout_thread_t *)[o_value pointerValue];
1230
1231     if( !p_vout->b_fullscreen )
1232     {
1233         NSRect s_rect;
1234
1235         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1236         p_vout->p_sys->s_rect.size = s_rect.size;
1237
1238         s_rect = [p_vout->p_sys->o_window frame];
1239         p_vout->p_sys->s_rect.origin = s_rect.origin;
1240
1241         p_vout->p_sys->b_pos_saved = 1;
1242     }
1243     
1244     p_vout->p_sys->p_qdport = nil;
1245     [p_vout->p_sys->o_window close];
1246     p_vout->p_sys->o_window = nil;
1247 }
1248
1249 @end