]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* put the beautiful "no picture" black screen.
[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.55 2003/08/25 14:51:47 garf 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             playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
431                                                                     FIND_ANYWHERE );
432
433             if ( p_playlist != nil )
434             {
435                 vlc_mutex_lock( &p_playlist->object_lock );
436                 if( p_playlist->p_input != NULL )
437                 {
438                     vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
439                     b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
440                     vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
441                 }
442                 vlc_mutex_unlock( &p_playlist->object_lock );
443                 vlc_object_release( p_playlist );
444             }
445             if ( !b_playing )
446             {
447                 VLCHideMouse( p_vout, NO );
448             }
449         }
450     }
451     else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
452     {
453         if( !p_vout->p_sys->b_mouse_pointer_visible )
454         {
455             VLCHideMouse( p_vout, NO );
456         }
457         else
458         {
459             p_vout->p_sys->b_mouse_moved = NO;
460         }
461     }
462     
463     return( 0 );
464 }
465
466 /*****************************************************************************
467  * vout_Display: displays previously rendered output
468  *****************************************************************************
469  * This function sends the currently rendered image to the display.
470  *****************************************************************************/
471 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
472 {
473     OSErr err;
474     CodecFlags flags;
475     Rect oldrect;
476     RgnHandle oldClip;
477
478     if( p_vout->p_sys->isplugin )
479     {
480         oldClip = NewRgn();
481
482         /* In mozilla plugin, mozilla browser also draws things in
483          * the windows. So we have to update the port/Origin for each
484          * picture. FIXME : the vout should lock something ! */
485         GetPort( &p_vout->p_sys->p_qdportold );
486         GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
487         GetClip( oldClip );
488
489         LockPortBits( p_vout->p_sys->p_qdport );
490
491         SetPort( p_vout->p_sys->p_qdport );
492         SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
493         ClipRect( &p_vout->p_sys->rect );
494    
495         
496         if( ( err = DecompressSequenceFrameS( 
497                         p_vout->p_sys->i_seq,
498                         p_pic->p_sys->p_info,
499                         p_pic->p_sys->i_size,                    
500                         codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
501         {
502             msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
503         }
504         else
505         {
506             QDFlushPortBuffer( p_vout->p_sys->p_qdport, p_vout->p_sys->mask );
507         }
508
509
510         SetOrigin( oldrect.left , oldrect.top );
511         SetClip( oldClip );
512         SetPort( p_vout->p_sys->p_qdportold );
513
514         UnlockPortBits( p_vout->p_sys->p_qdport );
515
516     }
517     else
518     { 
519         if( ( err = DecompressSequenceFrameS(
520                         p_vout->p_sys->i_seq,
521                         p_pic->p_sys->p_info,
522                         p_pic->p_sys->i_size,
523                         codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
524         {
525             msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
526         }
527         else
528         {
529             QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
530         }
531     }
532 }
533
534 /*****************************************************************************
535  * CoSendRequest: send request to interface thread
536  *****************************************************************************
537  * Returns 0 on success, 1 otherwise
538  *****************************************************************************/
539 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
540 {
541     int i_ret = 0;
542
543     VLCVout * o_vlv = [[VLCVout alloc] init];
544
545     if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
546     {
547         msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
548     }
549
550     [o_vlv release];
551
552     return( i_ret );
553 }
554
555 /*****************************************************************************
556  * CoCreateWindow: create new window 
557  *****************************************************************************
558  * Returns 0 on success, 1 otherwise
559  *****************************************************************************/
560 static int CoCreateWindow( vout_thread_t *p_vout )
561 {
562     if( CoSendRequest( p_vout, @selector(createWindow:) ) )
563     {
564         msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
565         return( 1 );
566     }
567
568     return( 0 );
569 }
570
571 /*****************************************************************************
572  * CoDestroyWindow: destroy window 
573  *****************************************************************************
574  * Returns 0 on success, 1 otherwise
575  *****************************************************************************/
576 static int CoDestroyWindow( vout_thread_t *p_vout )
577 {
578     if( !p_vout->p_sys->b_mouse_pointer_visible )
579     {
580         VLCHideMouse( p_vout, NO );
581     }
582
583     if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
584     {
585         msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
586         return( 1 );
587     }
588
589     return( 0 );
590 }
591
592 /*****************************************************************************
593  * CoToggleFullscreen: toggle fullscreen 
594  *****************************************************************************
595  * Returns 0 on success, 1 otherwise
596  *****************************************************************************/
597 static int CoToggleFullscreen( vout_thread_t *p_vout )
598 {
599     QTDestroySequence( p_vout );
600
601     if( CoDestroyWindow( p_vout ) )
602     {
603         msg_Err( p_vout, "unable to destroy window" );
604         return( 1 );
605     }
606     
607     p_vout->b_fullscreen = !p_vout->b_fullscreen;
608
609     config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
610
611     if( CoCreateWindow( p_vout ) )
612     {
613         msg_Err( p_vout, "unable to create window" );
614         return( 1 );
615     }
616
617     if( QTCreateSequence( p_vout ) )
618     {
619         msg_Err( p_vout, "unable to create sequence" );
620         return( 1 ); 
621     } 
622
623     return( 0 );
624 }
625
626 /*****************************************************************************
627  * VLCHideMouse: if b_hide then hide the cursor
628  *****************************************************************************/
629 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
630 {
631     BOOL b_inside;
632     NSRect s_rect;
633     NSPoint ml;
634     NSWindow *o_window = p_vout->p_sys->o_window;
635     NSView *o_contents = [o_window contentView];
636     
637     s_rect = [o_contents bounds];
638     ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
639     ml = [o_contents convertPoint:ml fromView:nil];
640     b_inside = [o_contents mouse: ml inRect: s_rect];
641     
642     if ( b_hide && b_inside )
643     {
644         /* only hide if mouse over VLCView */
645         [NSCursor hide];
646         p_vout->p_sys->b_mouse_pointer_visible = 0;
647     }
648     else if ( !b_hide )
649     {
650         [NSCursor unhide];
651         p_vout->p_sys->b_mouse_pointer_visible = 1;
652     }
653     p_vout->p_sys->b_mouse_moved = NO;
654     p_vout->p_sys->i_time_mouse_last_moved = mdate();
655     return;
656 }
657
658 /*****************************************************************************
659  * QTScaleMatrix: scale matrix 
660  *****************************************************************************/
661 static void QTScaleMatrix( vout_thread_t *p_vout )
662 {
663     Rect s_rect;
664     unsigned int i_width, i_height;
665     Fixed factor_x, factor_y;
666     unsigned int i_offset_x = 0;
667     unsigned int i_offset_y = 0;
668     vlc_value_t val;
669     vlc_value_t valt;
670     vlc_value_t vall;
671     vlc_value_t valb;
672     vlc_value_t valr;
673     vlc_value_t valx;
674     vlc_value_t valy;
675     vlc_value_t valw;
676     vlc_value_t valh;
677     vlc_value_t valportx;
678     vlc_value_t valporty;
679
680     GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
681     i_width = s_rect.right - s_rect.left;
682     i_height = s_rect.bottom - s_rect.top;
683
684     var_Get( p_vout->p_vlc, "drawable", &val );
685     var_Get( p_vout->p_vlc, "drawablet", &valt );
686     var_Get( p_vout->p_vlc, "drawablel", &vall );
687     var_Get( p_vout->p_vlc, "drawableb", &valb );
688     var_Get( p_vout->p_vlc, "drawabler", &valr );
689     var_Get( p_vout->p_vlc, "drawablex", &valx );
690     var_Get( p_vout->p_vlc, "drawabley", &valy );
691     var_Get( p_vout->p_vlc, "drawablew", &valw );
692     var_Get( p_vout->p_vlc, "drawableh", &valh );
693     var_Get( p_vout->p_vlc, "drawableportx", &valportx );
694     var_Get( p_vout->p_vlc, "drawableporty", &valporty );
695
696     if( p_vout->p_sys->isplugin )
697     {
698         p_vout->p_sys->portx = valportx.i_int;
699         p_vout->p_sys->porty = valporty.i_int;
700         p_vout->p_sys->p_qdport = val.i_int;
701         i_width = valw.i_int;
702         i_height = valh.i_int;
703
704         SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
705                     valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
706                     valb.i_int - valy.i_int );
707         p_vout->p_sys->rect.top = 0;
708         p_vout->p_sys->rect.left = 0;
709         p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
710         p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
711     }
712
713     if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
714     {
715         int i_adj_width = i_height * p_vout->output.i_aspect /
716                           VOUT_ASPECT_FACTOR;
717
718         factor_x = FixDiv( Long2Fix( i_adj_width ),
719                            Long2Fix( p_vout->output.i_width ) );
720         factor_y = FixDiv( Long2Fix( i_height ),
721                            Long2Fix( p_vout->output.i_height ) );
722
723         i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
724     }
725     else
726     {
727         int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
728                            p_vout->output.i_aspect;
729
730         factor_x = FixDiv( Long2Fix( i_width ),
731                            Long2Fix( p_vout->output.i_width ) );
732         factor_y = FixDiv( Long2Fix( i_adj_height ),
733                            Long2Fix( p_vout->output.i_height ) );
734
735         i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
736     }
737     
738     SetIdentityMatrix( p_vout->p_sys->p_matrix );
739
740     ScaleMatrix( p_vout->p_sys->p_matrix,
741                  factor_x, factor_y,
742                  Long2Fix(0), Long2Fix(0) );            
743
744     TranslateMatrix( p_vout->p_sys->p_matrix, 
745                      Long2Fix(i_offset_x), 
746                      Long2Fix(i_offset_y) );
747
748 }
749
750 /*****************************************************************************
751  * QTCreateSequence: create a new sequence 
752  *****************************************************************************
753  * Returns 0 on success, 1 otherwise
754  *****************************************************************************/
755 static int QTCreateSequence( vout_thread_t *p_vout )
756 {
757     OSErr err;
758     ImageDescriptionPtr p_descr;
759
760     HLock( (Handle)p_vout->p_sys->h_img_descr );
761     p_descr = *p_vout->p_sys->h_img_descr;
762
763     p_descr->idSize = sizeof(ImageDescription);
764     p_descr->cType = p_vout->p_sys->i_codec;
765     p_descr->version = 1;
766     p_descr->revisionLevel = 0;
767     p_descr->vendor = 'appl';
768     p_descr->width = p_vout->output.i_width;
769     p_descr->height = p_vout->output.i_height;
770     p_descr->hRes = Long2Fix(72);
771     p_descr->vRes = Long2Fix(72);
772     p_descr->spatialQuality = codecLosslessQuality;
773     p_descr->frameCount = 1;
774     p_descr->clutID = -1;
775     p_descr->dataSize = 0;
776     p_descr->depth = 24;
777
778
779     HUnlock( (Handle)p_vout->p_sys->h_img_descr );
780
781
782     if( ( err = DecompressSequenceBeginS( 
783                               &p_vout->p_sys->i_seq,
784                               p_vout->p_sys->h_img_descr,
785                               NULL, 0,
786                               p_vout->p_sys->p_qdport,
787                               NULL, NULL,
788                               p_vout->p_sys->p_matrix,
789                               0, p_vout->p_sys->mask,
790                               codecFlagUseImageBuffer,
791                               codecLosslessQuality,
792                               p_vout->p_sys->img_dc ) ) )
793     {
794         msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
795         return( 1 );
796     }
797
798
799     return( 0 );
800 }
801
802 /*****************************************************************************
803  * QTDestroySequence: destroy sequence 
804  *****************************************************************************/
805 static void QTDestroySequence( vout_thread_t *p_vout )
806 {
807     CDSequenceEnd( p_vout->p_sys->i_seq );
808 }
809
810 /*****************************************************************************
811  * QTNewPicture: allocate a picture
812  *****************************************************************************
813  * Returns 0 on success, 1 otherwise
814  *****************************************************************************/
815 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
816 {
817     int i_width  = p_vout->output.i_width;
818     int i_height = p_vout->output.i_height;
819
820     /* We know the chroma, allocate a buffer which will be used
821      * directly by the decoder */
822     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
823
824     if( p_pic->p_sys == NULL )
825     {
826         return( -1 );
827     }
828
829     switch( p_vout->output.i_chroma )
830     {
831         case VLC_FOURCC('I','4','2','0'):
832
833             p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
834             p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
835
836             /* Allocate the memory buffer */
837             p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
838                                           16, i_width * i_height * 3 / 2 );
839
840             /* Y buffer */
841             p_pic->Y_PIXELS = p_pic->p_data; 
842             p_pic->p[Y_PLANE].i_lines = i_height;
843             p_pic->p[Y_PLANE].i_pitch = i_width;
844             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
845             p_pic->p[Y_PLANE].i_visible_pitch = i_width;
846
847             /* U buffer */
848             p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
849             p_pic->p[U_PLANE].i_lines = i_height / 2;
850             p_pic->p[U_PLANE].i_pitch = i_width / 2;
851             p_pic->p[U_PLANE].i_pixel_pitch = 1;
852             p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
853
854             /* V buffer */
855             p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
856             p_pic->p[V_PLANE].i_lines = i_height / 2;
857             p_pic->p[V_PLANE].i_pitch = i_width / 2;
858             p_pic->p[V_PLANE].i_pixel_pitch = 1;
859             p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
860
861             /* We allocated 3 planes */
862             p_pic->i_planes = 3;
863
864 #define P p_pic->p_sys->pixmap_i420
865             P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
866                                        - p_pic->p_sys->p_info;
867             P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
868                                         - p_pic->p_sys->p_info;
869             P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
870                                         - p_pic->p_sys->p_info;
871
872             P.componentInfoY.rowBytes = i_width;
873             P.componentInfoCb.rowBytes = i_width / 2;
874             P.componentInfoCr.rowBytes = i_width / 2;
875 #undef P
876
877             break;
878
879     default:
880         /* Unknown chroma, tell the guy to get lost */
881         free( p_pic->p_sys );
882         msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
883                  p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
884         p_pic->i_planes = 0;
885         return( -1 );
886     }
887
888     return( 0 );
889 }
890
891 /*****************************************************************************
892  * QTFreePicture: destroy a picture allocated with QTNewPicture
893  *****************************************************************************/
894 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
895 {
896     switch( p_vout->output.i_chroma )
897     {
898         case VLC_FOURCC('I','4','2','0'):
899             free( p_pic->p_data_orig );
900             break;
901     }
902
903     free( p_pic->p_sys );
904 }
905
906 /*****************************************************************************
907  * VLCWindow implementation
908  *****************************************************************************/
909 @implementation VLCWindow
910
911 - (void)setVout:(vout_thread_t *)_p_vout
912 {
913     p_vout = _p_vout;
914 }
915
916 - (vout_thread_t *)getVout
917 {
918     return( p_vout );
919 }
920
921 - (void)scaleWindowWithFactor: (float)factor
922 {
923     NSSize newsize;
924     int i_corrected_height, i_corrected_width;
925     NSPoint topleftbase;
926     NSPoint topleftscreen;
927     
928     if ( !p_vout->b_fullscreen )
929     {
930         topleftbase.x = 0;
931         topleftbase.y = [self frame].size.height;
932         topleftscreen = [self convertBaseToScreen: topleftbase];
933         
934         if( p_vout->output.i_height * p_vout->output.i_aspect > 
935                         p_vout->output.i_width * VOUT_ASPECT_FACTOR )
936         {
937             i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
938                                             VOUT_ASPECT_FACTOR;
939             newsize.width = (int) ( i_corrected_width * factor );
940             newsize.height = (int) ( p_vout->render.i_height * factor );
941         }
942         else
943         {
944             i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
945                                             p_vout->output.i_aspect;
946             newsize.width = (int) ( p_vout->render.i_width * factor );
947             newsize.height = (int) ( i_corrected_height * factor );
948         }
949
950         [self setContentSize: newsize];
951         
952         [self setFrameTopLeftPoint: topleftscreen];
953         p_vout->i_changes |= VOUT_SIZE_CHANGE;
954     }
955 }
956
957 - (void)toggleFloatOnTop
958 {
959     if( config_GetInt( p_vout, "macosx-float" ) )
960     {
961         config_PutInt( p_vout, "macosx-float", 0 );
962         [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
963     }
964     else
965     {
966         config_PutInt( p_vout, "macosx-float", 1 );
967         [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
968     }
969 }
970
971 - (void)toggleFullscreen
972 {
973     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
974 }
975
976 - (BOOL)isFullscreen
977 {
978     return( p_vout->b_fullscreen );
979 }
980
981 - (BOOL)canBecomeKeyWindow
982 {
983     return( YES );
984 }
985
986 - (void)keyDown:(NSEvent *)o_event
987 {
988     unichar key = 0;
989     vlc_value_t val;
990
991     if( [[o_event characters] length] )
992     {
993         key = [[o_event characters] characterAtIndex: 0];
994     }
995
996     switch( key )
997     {
998         case 'f': case 'F':
999             [self toggleFullscreen];
1000             break;
1001
1002         case (unichar)0x1b: /* escape */
1003             if( [self isFullscreen] )
1004             {
1005                 [self toggleFullscreen];
1006             }
1007             break;
1008
1009         case 'q': case 'Q':
1010             p_vout->p_vlc->b_die = VLC_TRUE;
1011             break;
1012
1013         case ' ':
1014             input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
1015             break;
1016
1017         case (unichar)0xf700: /* arrow up */
1018             val.psz_string = "UP";
1019             var_Set( p_vout, "key-pressed", val );
1020             break;
1021
1022         case (unichar)0xf701: /* arrow down */
1023             val.psz_string = "DOWN";
1024             var_Set( p_vout, "key-pressed", val );
1025             break;
1026
1027         case (unichar)0xf702: /* arrow left */
1028             val.psz_string = "LEFT";
1029             var_Set( p_vout, "key-pressed", val );
1030             break;
1031
1032         case (unichar)0xf703: /* arrow right */
1033             val.psz_string = "RIGHT";
1034             var_Set( p_vout, "key-pressed", val );
1035             break;
1036
1037         case (unichar)0xd: /* return */
1038         case (unichar)0x3: /* enter */
1039             val.psz_string = "ENTER";
1040             var_Set( p_vout, "key-pressed", val );
1041             break;
1042
1043
1044         default:
1045             [super keyDown: o_event];
1046             break;
1047     }
1048 }
1049
1050 - (void)updateTitle
1051 {
1052     NSMutableString * o_title;
1053     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1054                                                        FIND_ANYWHERE );
1055     
1056     if( p_playlist == NULL )
1057     {
1058         return;
1059     }
1060
1061     vlc_mutex_lock( &p_playlist->object_lock );
1062     o_title = [NSMutableString stringWithUTF8String: 
1063         p_playlist->pp_items[p_playlist->i_index]->psz_name]; 
1064     vlc_mutex_unlock( &p_playlist->object_lock );
1065
1066     vlc_object_release( p_playlist );
1067
1068     if( o_title != nil )
1069     {
1070         NSRange prefix_range = [o_title rangeOfString: @"file:"];
1071         if( prefix_range.location != NSNotFound )
1072         {
1073             [o_title deleteCharactersInRange: prefix_range];
1074         }
1075
1076         [self setTitleWithRepresentedFilename: o_title];
1077     }
1078     else
1079     {
1080         [self setTitle:
1081             [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1082     }
1083 }
1084
1085 /* This is actually the same as VLCControls::stop. */
1086 - (BOOL)windowShouldClose:(id)sender
1087 {
1088     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1089                                                        FIND_ANYWHERE );
1090     if( p_playlist == NULL )      
1091     {
1092         return NO;
1093     }
1094
1095     playlist_Stop( p_playlist );
1096     vlc_object_release( p_playlist );
1097
1098     /* The window will be closed by the intf later. */
1099     return NO;
1100 }
1101
1102 @end
1103
1104 /*****************************************************************************
1105  * VLCView implementation
1106  *****************************************************************************/
1107 @implementation VLCView
1108
1109 - (void)drawRect:(NSRect)rect
1110 {
1111     vout_thread_t * p_vout;
1112     id o_window = [self window];
1113     p_vout = (vout_thread_t *)[o_window getVout];
1114     
1115     [[NSColor blackColor] set];
1116     NSRectFill( rect );
1117     [super drawRect: rect];
1118
1119     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1120 }
1121
1122 - (BOOL)acceptsFirstResponder
1123 {
1124     return( YES );
1125 }
1126
1127 - (BOOL)becomeFirstResponder
1128 {
1129     vout_thread_t * p_vout;
1130     id o_window = [self window];
1131     p_vout = (vout_thread_t *)[o_window getVout];
1132     
1133     [o_window setAcceptsMouseMovedEvents: YES];
1134     return( YES );
1135 }
1136
1137 - (BOOL)resignFirstResponder
1138 {
1139     vout_thread_t * p_vout;
1140     id o_window = [self window];
1141     p_vout = (vout_thread_t *)[o_window getVout];
1142     
1143     [o_window setAcceptsMouseMovedEvents: NO];
1144     VLCHideMouse( p_vout, NO );
1145     return( YES );
1146 }
1147
1148 - (void)mouseDown:(NSEvent *)o_event
1149 {
1150     vout_thread_t * p_vout;
1151     id o_window = [self window];
1152     p_vout = (vout_thread_t *)[o_window getVout];
1153     vlc_value_t val;
1154
1155     switch( [o_event type] )
1156     {        
1157         case NSLeftMouseDown:
1158         {
1159             var_Get( p_vout, "mouse-button-down", &val );
1160             val.i_int |= 1;
1161             var_Set( p_vout, "mouse-button-down", val );
1162         }
1163         break;
1164         
1165         default:
1166             [super mouseDown: o_event];
1167         break;
1168     }
1169 }
1170
1171 - (void)otherMouseDown:(NSEvent *)o_event
1172 {
1173     /* This is not the the wheel button. you need to poll the
1174      * mouseWheel event for that. other is a third, forth or fifth button */
1175     vout_thread_t * p_vout;
1176     id o_window = [self window];
1177     p_vout = (vout_thread_t *)[o_window getVout];
1178     vlc_value_t val;
1179
1180     switch( [o_event type] )
1181     {
1182         case NSOtherMouseDown:
1183         {
1184             var_Get( p_vout, "mouse-button-down", &val );
1185             val.i_int |= 2;
1186             var_Set( p_vout, "mouse-button-down", val );
1187         }
1188         break;
1189         
1190         default:
1191             [super mouseDown: o_event];
1192         break;
1193     }
1194 }
1195
1196 - (void)rightMouseDown:(NSEvent *)o_event
1197 {
1198     vout_thread_t * p_vout;
1199     id o_window = [self window];
1200     p_vout = (vout_thread_t *)[o_window getVout];
1201     vlc_value_t val;
1202
1203     switch( [o_event type] )
1204     {
1205         case NSRightMouseDown:
1206         {
1207             var_Get( p_vout, "mouse-button-down", &val );
1208             val.i_int |= 4;
1209             var_Set( p_vout, "mouse-button-down", val );
1210         }
1211         break;
1212         
1213         default:
1214             [super mouseDown: o_event];
1215         break;
1216     }
1217 }
1218
1219 - (void)mouseUp:(NSEvent *)o_event
1220 {
1221     vout_thread_t * p_vout;
1222     id o_window = [self window];
1223     p_vout = (vout_thread_t *)[o_window getVout];
1224     vlc_value_t val;
1225
1226     switch( [o_event type] )
1227     {
1228         case NSLeftMouseUp:
1229         {
1230             vlc_value_t b_val;
1231             b_val.b_bool = VLC_TRUE;
1232             var_Set( p_vout, "mouse-clicked", b_val );
1233             
1234             var_Get( p_vout, "mouse-button-down", &val );
1235             val.i_int &= ~1;
1236             var_Set( p_vout, "mouse-button-down", val );
1237         }
1238         break;
1239                 
1240         default:
1241             [super mouseUp: o_event];
1242         break;
1243     }
1244 }
1245
1246 - (void)otherMouseUp:(NSEvent *)o_event
1247 {
1248     vout_thread_t * p_vout;
1249     id o_window = [self window];
1250     p_vout = (vout_thread_t *)[o_window getVout];
1251     vlc_value_t val;
1252
1253     switch( [o_event type] )
1254     {
1255         case NSOtherMouseUp:
1256         {
1257             var_Get( p_vout, "mouse-button-down", &val );
1258             val.i_int &= ~2;
1259             var_Set( p_vout, "mouse-button-down", val );
1260         }
1261         break;
1262                 
1263         default:
1264             [super mouseUp: o_event];
1265         break;
1266     }
1267 }
1268
1269 - (void)rightMouseUp:(NSEvent *)o_event
1270 {
1271     vout_thread_t * p_vout;
1272     id o_window = [self window];
1273     p_vout = (vout_thread_t *)[o_window getVout];
1274     vlc_value_t val;
1275
1276     switch( [o_event type] )
1277     {
1278         case NSRightMouseUp:
1279         {
1280             var_Get( p_vout, "mouse-button-down", &val );
1281             val.i_int &= ~4;
1282             var_Set( p_vout, "mouse-button-down", val );
1283         }
1284         break;
1285         
1286         default:
1287             [super mouseUp: o_event];
1288         break;
1289     }
1290 }
1291
1292 - (void)mouseDragged:(NSEvent *)o_event
1293 {
1294     [self mouseMoved:o_event];
1295 }
1296
1297 - (void)otherMouseDragged:(NSEvent *)o_event
1298 {
1299     [self mouseMoved:o_event];
1300 }
1301
1302 - (void)rightMouseDragged:(NSEvent *)o_event
1303 {
1304     [self mouseMoved:o_event];
1305 }
1306
1307 - (void)mouseMoved:(NSEvent *)o_event
1308 {
1309     NSPoint ml;
1310     NSRect s_rect;
1311     BOOL b_inside;
1312
1313     vout_thread_t * p_vout;
1314     id o_window = [self window];
1315     p_vout = (vout_thread_t *)[o_window getVout];
1316
1317     s_rect = [self bounds];
1318     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1319     b_inside = [self mouse: ml inRect: s_rect];
1320
1321     if( b_inside )
1322     {
1323         vlc_value_t val;
1324         int i_width, i_height, i_x, i_y;
1325         
1326         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1327                                    (unsigned int)s_rect.size.height,
1328                                    &i_x, &i_y, &i_width, &i_height );
1329
1330         val.i_int = ( ((int)ml.x) - i_x ) * 
1331                     p_vout->render.i_width / i_width;
1332         var_Set( p_vout, "mouse-x", val );
1333
1334         val.i_int = ( ((int)ml.y) - i_y ) * 
1335                     p_vout->render.i_height / i_height;
1336         var_Set( p_vout, "mouse-y", val );
1337             
1338         val.b_bool = VLC_TRUE;
1339         var_Set( p_vout, "mouse-moved", val );
1340         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1341         p_vout->p_sys->b_mouse_moved = YES;
1342     }
1343     else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1344     {
1345         /* people with multiple monitors need their mouse,
1346          * even if VLCView in fullscreen. */
1347         VLCHideMouse( p_vout, NO );
1348     }
1349     
1350     [super mouseMoved: o_event];
1351 }
1352
1353 @end
1354
1355 /*****************************************************************************
1356  * VLCVout implementation
1357  *****************************************************************************/
1358 @implementation VLCVout
1359
1360 - (void)createWindow:(NSValue *)o_value
1361 {
1362     vlc_value_t val;
1363     VLCView * o_view;
1364     NSScreen * o_screen;
1365     vout_thread_t * p_vout;
1366     vlc_bool_t b_main_screen;
1367     
1368     p_vout = (vout_thread_t *)[o_value pointerValue];
1369
1370     p_vout->p_sys->o_window = [VLCWindow alloc];
1371     [p_vout->p_sys->o_window setVout: p_vout];
1372     [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1373
1374     if( var_Get( p_vout, "video-device", &val ) < 0 )
1375     {
1376         o_screen = [NSScreen mainScreen];
1377         b_main_screen = 1;
1378     }
1379     else
1380     {
1381         NSArray *o_screens = [NSScreen screens];
1382         unsigned int i_index = val.i_int;
1383         
1384         if( [o_screens count] < i_index )
1385         {
1386             o_screen = [NSScreen mainScreen];
1387             b_main_screen = 1;
1388         }
1389         else
1390         {
1391             i_index--;
1392             o_screen = [o_screens objectAtIndex: i_index];
1393             config_PutInt( p_vout, "macosx-vdev", i_index );
1394             b_main_screen = (i_index == 0);
1395         }
1396     } 
1397
1398     if( p_vout->b_fullscreen )
1399     {
1400         NSRect screen_rect = [o_screen frame];
1401         screen_rect.origin.x = screen_rect.origin.y = 0;
1402
1403         if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1404             BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1405                              NULL, NULL, fullScreenAllowEvents );
1406
1407         [p_vout->p_sys->o_window 
1408             initWithContentRect: screen_rect
1409             styleMask: NSBorderlessWindowMask
1410             backing: NSBackingStoreBuffered
1411             defer: NO screen: o_screen];
1412
1413         [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1414         p_vout->p_sys->b_mouse_moved = YES;
1415         p_vout->p_sys->i_time_mouse_last_moved = mdate();
1416     }
1417     else
1418     {
1419         unsigned int i_stylemask = NSTitledWindowMask |
1420                                    NSMiniaturizableWindowMask |
1421                                    NSClosableWindowMask |
1422                                    NSResizableWindowMask;
1423         
1424         if ( p_vout->p_sys->p_fullscreen_state != NULL )
1425             EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1426         p_vout->p_sys->p_fullscreen_state = NULL;
1427
1428         [p_vout->p_sys->o_window 
1429             initWithContentRect: p_vout->p_sys->s_rect
1430             styleMask: i_stylemask
1431             backing: NSBackingStoreBuffered
1432             defer: NO screen: o_screen];
1433
1434         [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1435         
1436         if( config_GetInt( p_vout, "macosx-float" ) )
1437         {
1438             [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1439         }
1440         
1441         if( !p_vout->p_sys->b_pos_saved )   
1442         {
1443             [p_vout->p_sys->o_window center];
1444         }
1445     }
1446
1447     o_view = [[VLCView alloc] init];
1448
1449     /* FIXME: [o_view setMenu:] */
1450     [p_vout->p_sys->o_window setContentView: o_view];
1451     [o_view autorelease];
1452
1453     [o_view lockFocus];
1454     p_vout->p_sys->p_qdport = [o_view qdPort];
1455
1456     [o_view unlockFocus];
1457     
1458     [p_vout->p_sys->o_window updateTitle];
1459     [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1460 }
1461
1462 - (void)destroyWindow:(NSValue *)o_value
1463 {
1464     vout_thread_t * p_vout;
1465
1466     p_vout = (vout_thread_t *)[o_value pointerValue];
1467
1468     if( !p_vout->b_fullscreen )
1469     {
1470         NSRect s_rect;
1471
1472         s_rect = [[p_vout->p_sys->o_window contentView] frame];
1473         p_vout->p_sys->s_rect.size = s_rect.size;
1474
1475         s_rect = [p_vout->p_sys->o_window frame];
1476         p_vout->p_sys->s_rect.origin = s_rect.origin;
1477
1478         p_vout->p_sys->b_pos_saved = YES;
1479     }
1480     
1481     p_vout->p_sys->p_qdport = nil;
1482     [p_vout->p_sys->o_window close];
1483     p_vout->p_sys->o_window = nil;
1484 }
1485
1486 @end