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