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