]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* modules/gui/macosx/vout.m:
[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.40 2003/03/18 04:07:23 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 #define VL_MAX_DISPLAYS 16
41
42 struct picture_sys_t
43 {
44     void *p_info;
45     unsigned int i_size;
46
47     /* When using I420 output */
48     PlanarPixmapInfoYUV420 pixmap_i420;
49 };
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  vout_Init      ( vout_thread_t * );
55 static void vout_End       ( vout_thread_t * );
56 static int  vout_Manage    ( vout_thread_t * );
57 static void vout_Display   ( vout_thread_t *, picture_t * );
58
59 static int  CoSendRequest      ( vout_thread_t *, SEL );
60 static int  CoCreateWindow     ( vout_thread_t * );
61 static int  CoDestroyWindow    ( vout_thread_t * );
62 static int  CoToggleFullscreen ( vout_thread_t * );
63
64 static void VLShowHideCursors  ( vout_thread_t *, BOOL );
65
66 static void QTScaleMatrix      ( vout_thread_t * );
67 static int  QTCreateSequence   ( vout_thread_t * );
68 static void QTDestroySequence  ( vout_thread_t * );
69 static int  QTNewPicture       ( vout_thread_t *, picture_t * );
70 static void QTFreePicture      ( vout_thread_t *, picture_t * );
71
72 /*****************************************************************************
73  * OpenVideo: allocates MacOS X video thread output method
74  *****************************************************************************
75  * This function allocates and initializes a MacOS X vout method.
76  *****************************************************************************/
77 int E_(OpenVideo) ( vlc_object_t *p_this )
78 {   
79     vout_thread_t * p_vout = (vout_thread_t *)p_this;
80     OSErr err;
81     int i_timeout;
82
83     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
84     if( p_vout->p_sys == NULL )
85     {
86         msg_Err( p_vout, "out of memory" );
87         return( 1 );
88     }
89
90     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
91
92     /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
93     for( i_timeout = 20 ; i_timeout-- ; )
94     {
95         if( NSApp == NULL )
96         {
97             msleep( INTF_IDLE_SLEEP );
98         }
99     }
100
101     if( NSApp == NULL )
102     {
103         msg_Err( p_vout, "no MacOS X interface present" );
104         free( p_vout->p_sys );
105         return( 1 );
106     }
107
108     if( [NSApp respondsToSelector: @selector(getIntf)] )
109     {
110         intf_thread_t * p_intf;
111
112         for( i_timeout = 10 ; i_timeout-- ; )
113         {
114             if( ( p_intf = [NSApp getIntf] ) == NULL )
115             {
116                 msleep( INTF_IDLE_SLEEP );
117             }
118         }
119
120         if( p_intf == NULL )
121         {
122             msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
123             free( p_vout->p_sys );
124             return( 1 );
125         }
126     }
127
128     p_vout->p_sys->h_img_descr = 
129         (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
130     p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
131     p_vout->p_sys->p_fullscreen_state = NULL;
132
133     p_vout->p_sys->b_mouse_pointer_visible = 1;
134
135     /* set window size */
136     p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
137     p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
138
139     if( ( err = EnterMovies() ) != noErr )
140     {
141         msg_Err( p_vout, "EnterMovies failed: %d", err );
142         free( p_vout->p_sys->p_matrix );
143         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
144         free( p_vout->p_sys );
145         return( 1 );
146     } 
147
148     if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
149     {
150         err = FindCodec( kYUV420CodecType, bestSpeedCodec,
151                          nil, &p_vout->p_sys->img_dc );
152         if( err == noErr && p_vout->p_sys->img_dc != 0 )
153         {
154             p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
155             p_vout->p_sys->i_codec = kYUV420CodecType;
156         }
157         else
158         {
159             msg_Err( p_vout, "failed to find an appropriate codec" );
160         }
161     }
162     else
163     {
164         msg_Err( p_vout, "chroma 0x%08x not supported",
165                          p_vout->render.i_chroma );
166     }
167
168     if( p_vout->p_sys->img_dc == 0 )
169     {
170         free( p_vout->p_sys->p_matrix );
171         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172         free( p_vout->p_sys );
173         return( 1 );        
174     }
175
176     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
177     NSArray * o_screens = [NSScreen screens];
178     if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
179     {
180         int i = 1;
181         vlc_value_t val;
182         NSScreen * o_screen;
183
184         int i_option = config_GetInt( p_vout, "macosx-vdev" );
185
186         var_Create( p_vout, "video-device", VLC_VAR_STRING |
187                                             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             if( ( i - 1 ) == i_option )
204             {
205                 var_Set( p_vout, "video-device", val );
206             }
207
208             i++;
209         }
210
211         var_AddCallback( p_vout, "video-device", vout_VarCallback,
212                          NULL );
213
214         val.b_bool = VLC_TRUE;
215         var_Set( p_vout, "intf-change", val );
216     }
217     [o_pool release];
218
219     if( CoCreateWindow( p_vout ) )
220     {
221         msg_Err( p_vout, "unable to create window" );
222         free( p_vout->p_sys->p_matrix );
223         DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
224         free( p_vout->p_sys ); 
225         return( 1 );
226     }
227
228     p_vout->pf_init = vout_Init;
229     p_vout->pf_end = vout_End;
230     p_vout->pf_manage = vout_Manage;
231     p_vout->pf_render = NULL;
232     p_vout->pf_display = vout_Display;
233
234     return( 0 );
235 }
236
237 /*****************************************************************************
238  * vout_Init: initialize video thread output method
239  *****************************************************************************/
240 static int vout_Init( vout_thread_t *p_vout )
241 {
242     int i_index;
243     picture_t *p_pic;
244
245     I_OUTPUTPICTURES = 0;
246
247     /* Initialize the output structure; we already found a codec,
248      * and the corresponding chroma we will be using. Since we can
249      * arbitrary scale, stick to the coordinates and aspect. */
250     p_vout->output.i_width  = p_vout->render.i_width;
251     p_vout->output.i_height = p_vout->render.i_height;
252     p_vout->output.i_aspect = p_vout->render.i_aspect;
253
254     SetPort( p_vout->p_sys->p_qdport );
255     QTScaleMatrix( p_vout );
256
257     if( QTCreateSequence( p_vout ) )
258     {
259         msg_Err( p_vout, "unable to create sequence" );
260         return( 1 );
261     }
262
263     /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
264     while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
265     {
266         p_pic = NULL;
267
268         /* Find an empty picture slot */
269         for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
270         {
271             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
272             {
273                 p_pic = p_vout->p_picture + i_index;
274                 break;
275             }
276         }
277
278         /* Allocate the picture */
279         if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
280         {
281             break;
282         }
283
284         p_pic->i_status = DESTROYED_PICTURE;
285         p_pic->i_type   = DIRECT_PICTURE;
286
287         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
288
289         I_OUTPUTPICTURES++;
290     }
291
292     return( 0 );
293 }
294
295 /*****************************************************************************
296  * vout_End: terminate video thread output method
297  *****************************************************************************/
298 static void vout_End( vout_thread_t *p_vout )
299 {
300     int i_index;
301
302     QTDestroySequence( p_vout );
303
304     /* Free the direct buffers we allocated */
305     for( i_index = I_OUTPUTPICTURES; i_index; )
306     {
307         i_index--;
308         QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
309     }
310 }
311
312 /*****************************************************************************
313  * CloseVideo: destroy video thread output method
314  *****************************************************************************/
315 void E_(CloseVideo) ( vlc_object_t *p_this )
316 {       
317     vout_thread_t * p_vout = (vout_thread_t *)p_this;     
318
319     if( CoDestroyWindow( p_vout ) )
320     {
321         msg_Err( p_vout, "unable to destroy window" );
322     }
323
324     if ( p_vout->p_sys->p_fullscreen_state != NULL )
325         EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
326
327     ExitMovies();
328
329     free( p_vout->p_sys->p_matrix );
330     DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
331
332     free( p_vout->p_sys );
333 }
334
335 /*****************************************************************************
336  * vout_Manage: handle events
337  *****************************************************************************
338  * This function should be called regularly by video output thread. It manages
339  * console events. It returns a non null value on error.
340  *****************************************************************************/
341 static int vout_Manage( vout_thread_t *p_vout )
342 {    
343     vlc_bool_t b_change = 0;
344     intf_thread_t * p_intf = [NSApp getIntf];
345     
346     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
347     {
348         if( CoToggleFullscreen( p_vout ) )  
349         {
350             return( 1 );
351         }
352
353         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
354     }
355
356     if( p_vout->i_changes & VOUT_SIZE_CHANGE ) 
357     {
358         QTScaleMatrix( p_vout );
359         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
360                             p_vout->p_sys->p_matrix );
361  
362         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
363     }
364
365     /* hide/show mouse cursor */
366     if( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen && 
367                         p_intf->p_sys->b_play_status )
368     {
369         if( !p_vout->p_sys->b_mouse_pointer_visible )
370         {
371             VLShowHideCursors( p_vout, NO );
372             b_change = 1;
373         }
374         else if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 && 
375                    p_vout->p_sys->b_mouse_pointer_visible )
376         {
377             VLShowHideCursors( p_vout, YES );
378             b_change = 1;
379         }
380
381     }
382     else if ( p_vout->b_fullscreen && !p_intf->p_sys->b_play_status )
383     {
384         if( !p_vout->p_sys->b_mouse_pointer_visible )
385         {
386             VLShowHideCursors( p_vout, NO );
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         VLShowHideCursors( p_vout, NO );
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  * VLShowHideCursors: if b_hide then hide the cursors on every display
527  * that contains p_vout, else show the cursors instead.
528  *****************************************************************************
529  * We cannot use kCGDirectMainDisplay, because this is always the display with
530  * the menubar.
531  *****************************************************************************/
532 static void VLShowHideCursors ( vout_thread_t *p_vout, BOOL b_hide )
533 {
534     NSRect frame;
535     NSScreen *o_screen;
536     CGDirectDisplayID displays[VL_MAX_DISPLAYS];
537     CGDisplayCount displayCount;
538     
539     o_screen = [p_vout->p_sys->o_window screen];
540     frame = [o_screen frame];
541     
542     int err = CGGetDisplaysWithRect( CGRectMake( NSMinX( frame ), NSMinY( frame ), 
543     NSWidth( frame ), NSHeight( frame ) ), VL_MAX_DISPLAYS, displays, &displayCount );
544     
545     if ( displayCount > 0 && !err )
546     {
547         unsigned int i;
548         /* multiple displays are possible, because of mirroring.
549          * mirroring is essential one screen on mult. displays. */
550         for ( i=0 ; i < displayCount ; i++ )
551         {
552             if ( b_hide )
553             {
554                 CGDisplayHideCursor( displays[i] );
555                 p_vout->p_sys->b_mouse_pointer_visible = 0;
556             }
557             else
558             {
559                 CGDisplayShowCursor( displays[i] );
560                 p_vout->p_sys->b_mouse_pointer_visible = 1;
561             }
562         }
563     }
564     return;
565 }
566
567 /*****************************************************************************
568  * QTScaleMatrix: scale matrix 
569  *****************************************************************************/
570 static void QTScaleMatrix( vout_thread_t *p_vout )
571 {
572     Rect s_rect;
573     unsigned int i_width, i_height;
574     Fixed factor_x, factor_y;
575     unsigned int i_offset_x = 0;
576     unsigned int i_offset_y = 0;
577
578     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
579
580     i_width = s_rect.right - s_rect.left;
581     i_height = s_rect.bottom - s_rect.top;
582
583     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
584     {
585         int i_adj_width = i_height * p_vout->output.i_aspect /
586                           VOUT_ASPECT_FACTOR;
587
588         factor_x = FixDiv( Long2Fix( i_adj_width ),
589                            Long2Fix( p_vout->output.i_width ) );
590         factor_y = FixDiv( Long2Fix( i_height ),
591                            Long2Fix( p_vout->output.i_height ) );
592
593         i_offset_x = (i_width - i_adj_width) / 2;
594     }
595     else
596     {
597         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
598                            p_vout->output.i_aspect;
599
600         factor_x = FixDiv( Long2Fix( i_width ),
601                            Long2Fix( p_vout->output.i_width ) );
602         factor_y = FixDiv( Long2Fix( i_adj_height ),
603                            Long2Fix( p_vout->output.i_height ) );
604
605         i_offset_y = (i_height - i_adj_height) / 2;
606     }
607     
608     SetIdentityMatrix( p_vout->p_sys->p_matrix );
609
610     ScaleMatrix( p_vout->p_sys->p_matrix,
611                  factor_x, factor_y,
612                  Long2Fix(0), Long2Fix(0) );            
613
614     TranslateMatrix( p_vout->p_sys->p_matrix, 
615                      Long2Fix(i_offset_x), 
616                      Long2Fix(i_offset_y) );
617 }
618
619 /*****************************************************************************
620  * QTCreateSequence: create a new sequence 
621  *****************************************************************************
622  * Returns 0 on success, 1 otherwise
623  *****************************************************************************/
624 static int QTCreateSequence( vout_thread_t *p_vout )
625 {
626     OSErr err;
627     ImageDescriptionPtr p_descr;
628
629     HLock( (Handle)p_vout->p_sys->h_img_descr );
630     p_descr = *p_vout->p_sys->h_img_descr;
631
632     p_descr->idSize = sizeof(ImageDescription);
633     p_descr->cType = p_vout->p_sys->i_codec;
634     p_descr->version = 1;
635     p_descr->revisionLevel = 0;
636     p_descr->vendor = 'appl';
637     p_descr->width = p_vout->output.i_width;
638     p_descr->height = p_vout->output.i_height;
639     p_descr->hRes = Long2Fix(72);
640     p_descr->vRes = Long2Fix(72);
641     p_descr->spatialQuality = codecLosslessQuality;
642     p_descr->frameCount = 1;
643     p_descr->clutID = -1;
644     p_descr->dataSize = 0;
645     p_descr->depth = 24;
646
647     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
648
649     if( ( err = DecompressSequenceBeginS( 
650                               &p_vout->p_sys->i_seq,
651                               p_vout->p_sys->h_img_descr,
652                               NULL, 0,
653                               p_vout->p_sys->p_qdport,
654                               NULL, NULL,
655                               p_vout->p_sys->p_matrix,
656                               0, NULL,
657                               codecFlagUseImageBuffer,
658                               codecLosslessQuality,
659                               p_vout->p_sys->img_dc ) ) )
660     {
661         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
662         return( 1 );
663     }
664
665     return( 0 );
666 }
667
668 /*****************************************************************************
669  * QTDestroySequence: destroy sequence 
670  *****************************************************************************/
671 static void QTDestroySequence( vout_thread_t *p_vout )
672 {
673     CDSequenceEnd( p_vout->p_sys->i_seq );
674 }
675
676 /*****************************************************************************
677  * QTNewPicture: allocate a picture
678  *****************************************************************************
679  * Returns 0 on success, 1 otherwise
680  *****************************************************************************/
681 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
682 {
683     int i_width  = p_vout->output.i_width;
684     int i_height = p_vout->output.i_height;
685
686     /* We know the chroma, allocate a buffer which will be used
687      * directly by the decoder */
688     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
689
690     if( p_pic->p_sys == NULL )
691     {
692         return( -1 );
693     }
694
695     switch( p_vout->output.i_chroma )
696     {
697         case VLC_FOURCC('I','4','2','0'):
698
699             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
700             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
701
702             /* Allocate the memory buffer */
703             p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
704                                           16, i_width * i_height * 3 / 2 );
705
706             /* Y buffer */
707             p_pic->Y_PIXELS = p_pic->p_data; 
708             p_pic->p[Y_PLANE].i_lines = i_height;
709             p_pic->p[Y_PLANE].i_pitch = i_width;
710             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
711             p_pic->p[Y_PLANE].i_visible_pitch = i_width;
712
713             /* U buffer */
714             p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
715             p_pic->p[U_PLANE].i_lines = i_height / 2;
716             p_pic->p[U_PLANE].i_pitch = i_width / 2;
717             p_pic->p[U_PLANE].i_pixel_pitch = 1;
718             p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
719
720             /* V buffer */
721             p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
722             p_pic->p[V_PLANE].i_lines = i_height / 2;
723             p_pic->p[V_PLANE].i_pitch = i_width / 2;
724             p_pic->p[V_PLANE].i_pixel_pitch = 1;
725             p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
726
727             /* We allocated 3 planes */
728             p_pic->i_planes = 3;
729
730 #define P p_pic->p_sys->pixmap_i420
731             P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
732                                        - p_pic->p_sys->p_info;
733             P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
734                                         - p_pic->p_sys->p_info;
735             P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
736                                         - p_pic->p_sys->p_info;
737
738             P.componentInfoY.rowBytes = i_width;
739             P.componentInfoCb.rowBytes = i_width / 2;
740             P.componentInfoCr.rowBytes = i_width / 2;
741 #undef P
742
743             break;
744
745     default:
746         /* Unknown chroma, tell the guy to get lost */
747         free( p_pic->p_sys );
748         msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
749                  p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
750         p_pic->i_planes = 0;
751         return( -1 );
752     }
753
754     return( 0 );
755 }
756
757 /*****************************************************************************
758  * QTFreePicture: destroy a picture allocated with QTNewPicture
759  *****************************************************************************/
760 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
761 {
762     switch( p_vout->output.i_chroma )
763     {
764         case VLC_FOURCC('I','4','2','0'):
765             free( p_pic->p_data_orig );
766             break;
767     }
768
769     free( p_pic->p_sys );
770 }
771
772 /*****************************************************************************
773  * VLCWindow implementation
774  *****************************************************************************/
775 @implementation VLCWindow
776
777 - (void)setVout:(vout_thread_t *)_p_vout
778 {
779     p_vout = _p_vout;
780 }
781
782 - (vout_thread_t *)getVout
783 {
784     return( p_vout );
785 }
786
787 - (void)scaleWindowWithFactor: (float)factor
788 {
789     NSSize newsize;
790     int i_corrected_height, i_corrected_width;
791     NSPoint topleftbase;
792     NSPoint topleftscreen;
793     
794     if ( !p_vout->b_fullscreen )
795     {
796         topleftbase.x = 0;
797         topleftbase.y = [self frame].size.height;
798         topleftscreen = [self convertBaseToScreen: topleftbase];
799         
800         if( p_vout->output.i_height * p_vout->output.i_aspect > 
801                         p_vout->output.i_width * VOUT_ASPECT_FACTOR )
802         {
803             i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
804                                             VOUT_ASPECT_FACTOR;
805             newsize.width = (int) ( i_corrected_width * factor );
806             newsize.height = (int) ( p_vout->render.i_height * factor );
807         }
808         else
809         {
810             i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
811                                             p_vout->output.i_aspect;
812             newsize.width = (int) ( p_vout->render.i_width * factor );
813             newsize.height = (int) ( i_corrected_height * factor );
814         }
815     
816         [self setContentSize: newsize];
817         
818         [self setFrameTopLeftPoint: topleftscreen];
819         p_vout->i_changes |= VOUT_SIZE_CHANGE;
820     }
821 }
822
823 - (void)toggleFullscreen
824 {
825     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
826 }
827
828 - (BOOL)isFullscreen
829 {
830     return( p_vout->b_fullscreen );
831 }
832
833 - (BOOL)canBecomeKeyWindow
834 {
835     return( YES );
836 }
837
838 - (void)keyDown:(NSEvent *)o_event
839 {
840     unichar key = 0;
841
842     if( [[o_event characters] length] )
843     {
844         key = [[o_event characters] characterAtIndex: 0];
845     }
846
847     switch( key )
848     {
849         case 'f': case 'F':
850             [self toggleFullscreen];
851             break;
852
853         case (unichar)0x1b: /* escape */
854             if( [self isFullscreen] )
855             {
856                 [self toggleFullscreen];
857             }
858             break;
859
860         case 'q': case 'Q':
861             p_vout->p_vlc->b_die = VLC_TRUE;
862             break;
863
864         case ' ':
865             input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
866             break;
867
868         default:
869             [super keyDown: o_event];
870             break;
871     }
872 }
873
874 - (void)updateTitle
875 {
876     NSMutableString * o_title;
877
878     intf_thread_t * p_intf = [NSApp getIntf];
879     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
880                                                        FIND_ANYWHERE );
881     
882     if( p_playlist == NULL )
883     {
884         return;
885     }
886
887     vlc_mutex_lock( &p_playlist->object_lock );
888     o_title = [NSMutableString stringWithUTF8String: 
889         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
890     vlc_mutex_unlock( &p_playlist->object_lock );
891
892     vlc_object_release( p_playlist );
893
894     if( o_title != nil )
895     {
896         NSRange prefix_range = [o_title rangeOfString: @"file:"];
897         if( prefix_range.location != NSNotFound )
898         {
899             [o_title deleteCharactersInRange: prefix_range];
900         }
901
902         [self setTitleWithRepresentedFilename: o_title];
903     }
904     else
905     {
906         [self setTitle:
907             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
908     }
909 }
910
911 /* This is actually the same as VLCControls::stop. */
912 - (BOOL)windowShouldClose:(id)sender
913 {
914     intf_thread_t * p_intf = [NSApp getIntf];
915     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
916                                                        FIND_ANYWHERE );
917     if( p_playlist == NULL )      
918     {
919         return NO;
920     }
921
922     playlist_Stop( p_playlist );
923     vlc_object_release( p_playlist );
924
925     /* The window will be closed by the intf later. */
926     return NO;
927 }
928
929 @end
930
931 /*****************************************************************************
932  * VLCView implementation
933  *****************************************************************************/
934 @implementation VLCView
935
936 - (void)drawRect:(NSRect)rect
937 {
938     vout_thread_t * p_vout;
939     id o_window = [self window];
940     p_vout = (vout_thread_t *)[o_window getVout];
941     
942     [[NSColor blackColor] set];
943     NSRectFill( rect );
944     [super drawRect: rect];
945
946     p_vout->i_changes |= VOUT_SIZE_CHANGE;
947 }
948
949 - (BOOL)acceptsFirstResponder
950 {
951     return( YES );
952 }
953
954 - (BOOL)becomeFirstResponder
955 {
956     [[self window] setAcceptsMouseMovedEvents: YES];
957     return( YES );
958 }
959
960 - (BOOL)resignFirstResponder
961 {
962     [[self window] setAcceptsMouseMovedEvents: NO];
963     return( YES );
964 }
965
966 - (void)mouseDown:(NSEvent *)o_event
967 {
968     vout_thread_t * p_vout;
969     id o_window = [self window];
970     p_vout = (vout_thread_t *)[o_window getVout];
971     vlc_value_t val;
972
973     switch( [o_event type] )
974     {        
975         case NSLeftMouseDown:
976         {
977             var_Get( p_vout, "mouse-button-down", &val );
978             val.i_int |= 1;
979             var_Set( p_vout, "mouse-button-down", val );
980         }
981         break;
982         
983         default:
984             [super mouseDown: o_event];
985         break;
986     }
987 }
988
989 - (void)otherMouseDown:(NSEvent *)o_event
990 {
991     /* This is not the the wheel button. you need to poll the
992      * mouseWheel event for that. other is a third, forth or fifth button */
993     vout_thread_t * p_vout;
994     id o_window = [self window];
995     p_vout = (vout_thread_t *)[o_window getVout];
996     vlc_value_t val;
997
998     switch( [o_event type] )
999     {
1000         case NSOtherMouseDown:
1001         {
1002             var_Get( p_vout, "mouse-button-down", &val );
1003             val.i_int |= 2;
1004             var_Set( p_vout, "mouse-button-down", val );
1005         }
1006         break;
1007         
1008         default:
1009             [super mouseDown: o_event];
1010         break;
1011     }
1012 }
1013
1014 - (void)rightMouseDown:(NSEvent *)o_event
1015 {
1016     vout_thread_t * p_vout;
1017     id o_window = [self window];
1018     p_vout = (vout_thread_t *)[o_window getVout];
1019     vlc_value_t val;
1020
1021     switch( [o_event type] )
1022     {
1023         case NSRightMouseDown:
1024         {
1025             var_Get( p_vout, "mouse-button-down", &val );
1026             val.i_int |= 4;
1027             var_Set( p_vout, "mouse-button-down", val );
1028         }
1029         break;
1030         
1031         default:
1032             [super mouseDown: o_event];
1033         break;
1034     }
1035 }
1036
1037 - (void)mouseUp:(NSEvent *)o_event
1038 {
1039     vout_thread_t * p_vout;
1040     id o_window = [self window];
1041     p_vout = (vout_thread_t *)[o_window getVout];
1042     vlc_value_t val;
1043
1044     switch( [o_event type] )
1045     {
1046         case NSLeftMouseUp:
1047         {
1048             vlc_value_t b_val;
1049             b_val.b_bool = VLC_TRUE;
1050             var_Set( p_vout, "mouse-clicked", b_val );
1051             
1052             var_Get( p_vout, "mouse-button-down", &val );
1053             val.i_int &= ~1;
1054             var_Set( p_vout, "mouse-button-down", val );
1055         }
1056         break;
1057                 
1058         default:
1059             [super mouseUp: o_event];
1060         break;
1061     }
1062 }
1063
1064 - (void)otherMouseUp:(NSEvent *)o_event
1065 {
1066     vout_thread_t * p_vout;
1067     id o_window = [self window];
1068     p_vout = (vout_thread_t *)[o_window getVout];
1069     vlc_value_t val;
1070
1071     switch( [o_event type] )
1072     {
1073         case NSOtherMouseUp:
1074         {
1075             var_Get( p_vout, "mouse-button-down", &val );
1076             val.i_int &= ~2;
1077             var_Set( p_vout, "mouse-button-down", val );
1078         }
1079         break;
1080                 
1081         default:
1082             [super mouseUp: o_event];
1083         break;
1084     }
1085 }
1086
1087 - (void)rightMouseUp:(NSEvent *)o_event
1088 {
1089     vout_thread_t * p_vout;
1090     id o_window = [self window];
1091     p_vout = (vout_thread_t *)[o_window getVout];
1092     vlc_value_t val;
1093
1094     switch( [o_event type] )
1095     {
1096         case NSRightMouseUp:
1097         {
1098             var_Get( p_vout, "mouse-button-down", &val );
1099             val.i_int &= ~4;
1100             var_Set( p_vout, "mouse-button-down", val );
1101         }
1102         break;
1103         
1104         default:
1105             [super mouseUp: o_event];
1106         break;
1107     }
1108 }
1109
1110 - (void)mouseDragged:(NSEvent *)o_event
1111 {
1112     [self mouseMoved:o_event];
1113 }
1114
1115 - (void)otherMouseDragged:(NSEvent *)o_event
1116 {
1117     [self mouseMoved:o_event];
1118 }
1119
1120 - (void)rightMouseDragged:(NSEvent *)o_event
1121 {
1122     [self mouseMoved:o_event];
1123 }
1124
1125 - (void)mouseMoved:(NSEvent *)o_event
1126 {
1127     NSPoint ml;
1128     NSRect s_rect;
1129     BOOL b_inside;
1130
1131     vout_thread_t * p_vout;
1132     id o_window = [self window];
1133     p_vout = (vout_thread_t *)[o_window getVout];
1134
1135     s_rect = [self bounds];
1136     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1137     b_inside = [self mouse: ml inRect: s_rect];
1138
1139     if( b_inside )
1140     {
1141         vlc_value_t val;
1142         int i_width, i_height, i_x, i_y;
1143         
1144         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1145                                    (unsigned int)s_rect.size.height,
1146                                    &i_x, &i_y, &i_width, &i_height );
1147
1148         val.i_int = ( ((int)ml.x) - i_x ) * 
1149                     p_vout->render.i_width / i_width;
1150         var_Set( p_vout, "mouse-x", val );
1151
1152         val.i_int = ( ((int)ml.y) - i_y ) * 
1153                     p_vout->render.i_height / i_height;
1154         var_Set( p_vout, "mouse-y", val );
1155             
1156         val.b_bool = VLC_TRUE;
1157         var_Set( p_vout, "mouse-moved", val );
1158     }
1159     p_vout->p_sys->i_time_mouse_last_moved = mdate();
1160     p_vout->p_sys->b_mouse_moved = 1;
1161     [super mouseMoved: o_event];
1162 }
1163
1164 @end
1165
1166 /*****************************************************************************
1167  * VLCVout implementation
1168  *****************************************************************************/
1169 @implementation VLCVout
1170
1171 - (void)createWindow:(NSValue *)o_value
1172 {
1173     vlc_value_t val;
1174     VLCView * o_view;
1175     NSScreen * o_screen;
1176     vout_thread_t * p_vout;
1177     vlc_bool_t b_main_screen;
1178     
1179     p_vout = (vout_thread_t *)[o_value pointerValue];
1180
1181     p_vout->p_sys->o_window = [VLCWindow alloc];
1182     [p_vout->p_sys->o_window setVout: p_vout];
1183     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1184
1185     if( var_Get( p_vout, "video-device", &val ) < 0 )
1186     {
1187         o_screen = [NSScreen mainScreen];
1188         b_main_screen = 1;
1189     }
1190     else
1191     {
1192         unsigned int i_index = 0;
1193         NSArray *o_screens = [NSScreen screens];
1194
1195         if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
1196             [o_screens count] < i_index )
1197         {
1198             o_screen = [NSScreen mainScreen];
1199             b_main_screen = 1;
1200         }
1201         else
1202         {
1203             i_index--;
1204             o_screen = [o_screens objectAtIndex: i_index];
1205             config_PutInt( p_vout, "macosx-vdev", i_index );
1206             b_main_screen = (i_index == 0);
1207         } 
1208
1209         free( val.psz_string );
1210     } 
1211
1212     if( p_vout->b_fullscreen )
1213     {
1214         NSRect screen_rect = [o_screen frame];
1215         screen_rect.origin.x = screen_rect.origin.y = 0;
1216
1217         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1218             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1219                              NULL, NULL, fullScreenAllowEvents );
1220
1221         [p_vout->p_sys->o_window 
1222             initWithContentRect: screen_rect
1223             styleMask: NSBorderlessWindowMask
1224             backing: NSBackingStoreBuffered
1225             defer: NO screen: o_screen];
1226
1227         [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1228         p_vout->p_sys->b_mouse_moved = 1;
1229         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1230     }
1231     else
1232     {
1233         unsigned int i_stylemask = NSTitledWindowMask |
1234                                    NSMiniaturizableWindowMask |
1235                                    NSClosableWindowMask |
1236                                    NSResizableWindowMask;
1237         
1238         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1239             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1240         p_vout->p_sys->p_fullscreen_state = NULL;
1241
1242         [p_vout->p_sys->o_window 
1243             initWithContentRect: p_vout->p_sys->s_rect
1244             styleMask: i_stylemask
1245             backing: NSBackingStoreBuffered
1246             defer: NO screen: o_screen];
1247
1248         if( !p_vout->p_sys->b_pos_saved )   
1249         {
1250             [p_vout->p_sys->o_window center];
1251         }
1252     }
1253
1254     o_view = [[VLCView alloc] init];
1255     /* FIXME: [o_view setMenu:] */
1256     [p_vout->p_sys->o_window setContentView: o_view];
1257     [o_view autorelease];
1258
1259     [o_view lockFocus];
1260     p_vout->p_sys->p_qdport = [o_view qdPort];
1261     [o_view unlockFocus];
1262     
1263     [p_vout->p_sys->o_window updateTitle];
1264     [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1265 }
1266
1267 - (void)destroyWindow:(NSValue *)o_value
1268 {
1269     vout_thread_t * p_vout;
1270
1271     p_vout = (vout_thread_t *)[o_value pointerValue];
1272
1273     if( !p_vout->b_fullscreen )
1274     {
1275         NSRect s_rect;
1276
1277         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1278         p_vout->p_sys->s_rect.size = s_rect.size;
1279
1280         s_rect = [p_vout->p_sys->o_window frame];
1281         p_vout->p_sys->s_rect.origin = s_rect.origin;
1282
1283         p_vout->p_sys->b_pos_saved = 1;
1284     }
1285     
1286     p_vout->p_sys->p_qdport = nil;
1287     [p_vout->p_sys->o_window close];
1288     p_vout->p_sys->o_window = nil;
1289 }
1290
1291 @end