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