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