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