]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* the OSX vout has support for the new hotkeys now. no prefs for it yet.
[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.58 2003/10/29 02:13:04 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 = YES;
147     p_vout->p_sys->b_mouse_moved = YES;
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 = 1 ;
159     
160     } else
161     {
162         p_vout->p_sys->mask = NULL;
163         p_vout->p_sys->isplugin = 0 ;
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             SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
410         }
411         else
412         {
413             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
414         }
415     
416         QTScaleMatrix( p_vout );
417         SetDSequenceMatrix( p_vout->p_sys->i_seq, 
418                             p_vout->p_sys->p_matrix );
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
991     i_pressed_modifiers = GetCurrentKeyModifiers();
992
993     if( i_pressed_modifiers & NSShiftKeyMask )
994         val.i_int |= KEY_MODIFIER_SHIFT;
995     if( i_pressed_modifiers & NSControlKeyMask )
996         val.i_int |= KEY_MODIFIER_CTRL;
997     if( i_pressed_modifiers & NSAlternateKeyMask )
998         val.i_int |= KEY_MODIFIER_ALT;
999     if( i_pressed_modifiers & NSCommandKeyMask )
1000         val.i_int |= KEY_MODIFIER_COMMAND;
1001
1002     NSLog( @"detected the modifiers: %x", val.i_int );
1003
1004     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
1005
1006     if( key )
1007     {
1008         val.i_int |= CocoaConvertKey( key );
1009         var_Set( p_vout->p_vlc, "key-pressed", val );
1010         NSLog( @"detected the key: %x", key );
1011     }
1012     else
1013     {
1014         [super keyDown: o_event];
1015     }
1016 }
1017
1018 - (void)updateTitle
1019 {
1020     NSMutableString * o_title;
1021     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1022                                                        FIND_ANYWHERE );
1023     
1024     if( p_playlist == NULL )
1025     {
1026         return;
1027     }
1028
1029     vlc_mutex_lock( &p_playlist->object_lock );
1030     o_title = [NSMutableString stringWithUTF8String: 
1031         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
1032     vlc_mutex_unlock( &p_playlist->object_lock );
1033
1034     vlc_object_release( p_playlist );
1035
1036     if( o_title != nil )
1037     {
1038         NSRange prefix_range = [o_title rangeOfString: @"file:"];
1039         if( prefix_range.location != NSNotFound )
1040         {
1041             [o_title deleteCharactersInRange: prefix_range];
1042         }
1043
1044         [self setTitleWithRepresentedFilename: o_title];
1045     }
1046     else
1047     {
1048         [self setTitle:
1049             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1050     }
1051 }
1052
1053 /* This is actually the same as VLCControls::stop. */
1054 - (BOOL)windowShouldClose:(id)sender
1055 {
1056     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1057                                                        FIND_ANYWHERE );
1058     if( p_playlist == NULL )      
1059     {
1060         return NO;
1061     }
1062
1063     playlist_Stop( p_playlist );
1064     vlc_object_release( p_playlist );
1065
1066     /* The window will be closed by the intf later. */
1067     return NO;
1068 }
1069
1070 @end
1071
1072 /*****************************************************************************
1073  * VLCView implementation
1074  *****************************************************************************/
1075 @implementation VLCView
1076
1077 - (void)drawRect:(NSRect)rect
1078 {
1079     vout_thread_t * p_vout;
1080     id o_window = [self window];
1081     p_vout = (vout_thread_t *)[o_window getVout];
1082     
1083     [[NSColor blackColor] set];
1084     NSRectFill( rect );
1085     [super drawRect: rect];
1086
1087     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1088 }
1089
1090 - (BOOL)acceptsFirstResponder
1091 {
1092     return( YES );
1093 }
1094
1095 - (BOOL)becomeFirstResponder
1096 {
1097     vout_thread_t * p_vout;
1098     id o_window = [self window];
1099     p_vout = (vout_thread_t *)[o_window getVout];
1100     
1101     [o_window setAcceptsMouseMovedEvents: YES];
1102     return( YES );
1103 }
1104
1105 - (BOOL)resignFirstResponder
1106 {
1107     vout_thread_t * p_vout;
1108     id o_window = [self window];
1109     p_vout = (vout_thread_t *)[o_window getVout];
1110     
1111     [o_window setAcceptsMouseMovedEvents: NO];
1112     VLCHideMouse( p_vout, NO );
1113     return( YES );
1114 }
1115
1116 - (void)mouseDown:(NSEvent *)o_event
1117 {
1118     vout_thread_t * p_vout;
1119     id o_window = [self window];
1120     p_vout = (vout_thread_t *)[o_window getVout];
1121     vlc_value_t val;
1122
1123     switch( [o_event type] )
1124     {        
1125         case NSLeftMouseDown:
1126         {
1127             var_Get( p_vout, "mouse-button-down", &val );
1128             val.i_int |= 1;
1129             var_Set( p_vout, "mouse-button-down", val );
1130         }
1131         break;
1132         
1133         default:
1134             [super mouseDown: o_event];
1135         break;
1136     }
1137 }
1138
1139 - (void)otherMouseDown:(NSEvent *)o_event
1140 {
1141     /* This is not the the wheel button. you need to poll the
1142      * mouseWheel event for that. other is a third, forth or fifth button */
1143     vout_thread_t * p_vout;
1144     id o_window = [self window];
1145     p_vout = (vout_thread_t *)[o_window getVout];
1146     vlc_value_t val;
1147
1148     switch( [o_event type] )
1149     {
1150         case NSOtherMouseDown:
1151         {
1152             var_Get( p_vout, "mouse-button-down", &val );
1153             val.i_int |= 2;
1154             var_Set( p_vout, "mouse-button-down", val );
1155         }
1156         break;
1157         
1158         default:
1159             [super mouseDown: o_event];
1160         break;
1161     }
1162 }
1163
1164 - (void)rightMouseDown:(NSEvent *)o_event
1165 {
1166     vout_thread_t * p_vout;
1167     id o_window = [self window];
1168     p_vout = (vout_thread_t *)[o_window getVout];
1169     vlc_value_t val;
1170
1171     switch( [o_event type] )
1172     {
1173         case NSRightMouseDown:
1174         {
1175             var_Get( p_vout, "mouse-button-down", &val );
1176             val.i_int |= 4;
1177             var_Set( p_vout, "mouse-button-down", val );
1178         }
1179         break;
1180         
1181         default:
1182             [super mouseDown: o_event];
1183         break;
1184     }
1185 }
1186
1187 - (void)mouseUp:(NSEvent *)o_event
1188 {
1189     vout_thread_t * p_vout;
1190     id o_window = [self window];
1191     p_vout = (vout_thread_t *)[o_window getVout];
1192     vlc_value_t val;
1193
1194     switch( [o_event type] )
1195     {
1196         case NSLeftMouseUp:
1197         {
1198             vlc_value_t b_val;
1199             b_val.b_bool = VLC_TRUE;
1200             var_Set( p_vout, "mouse-clicked", b_val );
1201             
1202             var_Get( p_vout, "mouse-button-down", &val );
1203             val.i_int &= ~1;
1204             var_Set( p_vout, "mouse-button-down", val );
1205         }
1206         break;
1207                 
1208         default:
1209             [super mouseUp: o_event];
1210         break;
1211     }
1212 }
1213
1214 - (void)otherMouseUp:(NSEvent *)o_event
1215 {
1216     vout_thread_t * p_vout;
1217     id o_window = [self window];
1218     p_vout = (vout_thread_t *)[o_window getVout];
1219     vlc_value_t val;
1220
1221     switch( [o_event type] )
1222     {
1223         case NSOtherMouseUp:
1224         {
1225             var_Get( p_vout, "mouse-button-down", &val );
1226             val.i_int &= ~2;
1227             var_Set( p_vout, "mouse-button-down", val );
1228         }
1229         break;
1230                 
1231         default:
1232             [super mouseUp: o_event];
1233         break;
1234     }
1235 }
1236
1237 - (void)rightMouseUp:(NSEvent *)o_event
1238 {
1239     vout_thread_t * p_vout;
1240     id o_window = [self window];
1241     p_vout = (vout_thread_t *)[o_window getVout];
1242     vlc_value_t val;
1243
1244     switch( [o_event type] )
1245     {
1246         case NSRightMouseUp:
1247         {
1248             var_Get( p_vout, "mouse-button-down", &val );
1249             val.i_int &= ~4;
1250             var_Set( p_vout, "mouse-button-down", val );
1251         }
1252         break;
1253         
1254         default:
1255             [super mouseUp: o_event];
1256         break;
1257     }
1258 }
1259
1260 - (void)mouseDragged:(NSEvent *)o_event
1261 {
1262     [self mouseMoved:o_event];
1263 }
1264
1265 - (void)otherMouseDragged:(NSEvent *)o_event
1266 {
1267     [self mouseMoved:o_event];
1268 }
1269
1270 - (void)rightMouseDragged:(NSEvent *)o_event
1271 {
1272     [self mouseMoved:o_event];
1273 }
1274
1275 - (void)mouseMoved:(NSEvent *)o_event
1276 {
1277     NSPoint ml;
1278     NSRect s_rect;
1279     BOOL b_inside;
1280
1281     vout_thread_t * p_vout;
1282     id o_window = [self window];
1283     p_vout = (vout_thread_t *)[o_window getVout];
1284
1285     s_rect = [self bounds];
1286     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1287     b_inside = [self mouse: ml inRect: s_rect];
1288
1289     if( b_inside )
1290     {
1291         vlc_value_t val;
1292         int i_width, i_height, i_x, i_y;
1293         
1294         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1295                                    (unsigned int)s_rect.size.height,
1296                                    &i_x, &i_y, &i_width, &i_height );
1297
1298         val.i_int = ( ((int)ml.x) - i_x ) * 
1299                     p_vout->render.i_width / i_width;
1300         var_Set( p_vout, "mouse-x", val );
1301
1302         val.i_int = ( ((int)ml.y) - i_y ) * 
1303                     p_vout->render.i_height / i_height;
1304         var_Set( p_vout, "mouse-y", val );
1305             
1306         val.b_bool = VLC_TRUE;
1307         var_Set( p_vout, "mouse-moved", val );
1308         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1309         p_vout->p_sys->b_mouse_moved = YES;
1310     }
1311     else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1312     {
1313         /* people with multiple monitors need their mouse,
1314          * even if VLCView in fullscreen. */
1315         VLCHideMouse( p_vout, NO );
1316     }
1317     
1318     [super mouseMoved: o_event];
1319 }
1320
1321 @end
1322
1323 /*****************************************************************************
1324  * VLCVout implementation
1325  *****************************************************************************/
1326 @implementation VLCVout
1327
1328 - (void)createWindow:(NSValue *)o_value
1329 {
1330     vlc_value_t val;
1331     VLCView * o_view;
1332     NSScreen * o_screen;
1333     vout_thread_t * p_vout;
1334     vlc_bool_t b_main_screen;
1335     
1336     p_vout = (vout_thread_t *)[o_value pointerValue];
1337
1338     p_vout->p_sys->o_window = [VLCWindow alloc];
1339     [p_vout->p_sys->o_window setVout: p_vout];
1340     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1341
1342     if( var_Get( p_vout, "video-device", &val ) < 0 )
1343     {
1344         o_screen = [NSScreen mainScreen];
1345         b_main_screen = 1;
1346     }
1347     else
1348     {
1349         NSArray *o_screens = [NSScreen screens];
1350         unsigned int i_index = val.i_int;
1351         
1352         if( [o_screens count] < i_index )
1353         {
1354             o_screen = [NSScreen mainScreen];
1355             b_main_screen = 1;
1356         }
1357         else
1358         {
1359             i_index--;
1360             o_screen = [o_screens objectAtIndex: i_index];
1361             config_PutInt( p_vout, "macosx-vdev", i_index );
1362             b_main_screen = (i_index == 0);
1363         }
1364     } 
1365
1366     if( p_vout->b_fullscreen )
1367     {
1368         NSRect screen_rect = [o_screen frame];
1369         screen_rect.origin.x = screen_rect.origin.y = 0;
1370
1371         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1372             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1373                              NULL, NULL, fullScreenAllowEvents );
1374
1375         [p_vout->p_sys->o_window 
1376             initWithContentRect: screen_rect
1377             styleMask: NSBorderlessWindowMask
1378             backing: NSBackingStoreBuffered
1379             defer: NO screen: o_screen];
1380
1381         //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1382         p_vout->p_sys->b_mouse_moved = YES;
1383         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1384     }
1385     else
1386     {
1387         unsigned int i_stylemask = NSTitledWindowMask |
1388                                    NSMiniaturizableWindowMask |
1389                                    NSClosableWindowMask |
1390                                    NSResizableWindowMask;
1391         
1392         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1393             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1394         p_vout->p_sys->p_fullscreen_state = NULL;
1395
1396         [p_vout->p_sys->o_window 
1397             initWithContentRect: p_vout->p_sys->s_rect
1398             styleMask: i_stylemask
1399             backing: NSBackingStoreBuffered
1400             defer: NO screen: o_screen];
1401
1402         [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1403         
1404         if( config_GetInt( p_vout, "macosx-float" ) )
1405         {
1406             [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1407         }
1408         
1409         if( !p_vout->p_sys->b_pos_saved )   
1410         {
1411             [p_vout->p_sys->o_window center];
1412         }
1413     }
1414
1415     o_view = [[VLCView alloc] init];
1416
1417     /* FIXME: [o_view setMenu:] */
1418     [p_vout->p_sys->o_window setContentView: o_view];
1419     [o_view autorelease];
1420
1421     [o_view lockFocus];
1422     p_vout->p_sys->p_qdport = [o_view qdPort];
1423
1424     [o_view unlockFocus];
1425     
1426     [p_vout->p_sys->o_window updateTitle];
1427     [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1428 }
1429
1430 - (void)destroyWindow:(NSValue *)o_value
1431 {
1432     vout_thread_t * p_vout;
1433
1434     p_vout = (vout_thread_t *)[o_value pointerValue];
1435
1436     if( !p_vout->b_fullscreen )
1437     {
1438         NSRect s_rect;
1439
1440         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1441         p_vout->p_sys->s_rect.size = s_rect.size;
1442
1443         s_rect = [p_vout->p_sys->o_window frame];
1444         p_vout->p_sys->s_rect.origin = s_rect.origin;
1445
1446         p_vout->p_sys->b_pos_saved = YES;
1447     }
1448     
1449     p_vout->p_sys->p_qdport = nil;
1450     [p_vout->p_sys->o_window close];
1451     p_vout->p_sys->o_window = nil;
1452 }
1453
1454 @end