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