1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.17 2003/01/24 00:17:20 hartman Exp $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <thedj@users.sourceforge.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
39 #include <Cocoa/Cocoa.h>
40 #include <QuickTime/QuickTime.h>
45 #define QT_MAX_DIRECTBUFFERS 10
52 /* When using I420 output */
53 PlanarPixmapInfoYUV420 pixmap_i420;
56 /*****************************************************************************
58 *****************************************************************************/
59 static int vout_Init ( vout_thread_t * );
60 static void vout_End ( vout_thread_t * );
61 static int vout_Manage ( vout_thread_t * );
62 static void vout_Display ( vout_thread_t *, picture_t * );
64 static int CoSendRequest ( vout_thread_t *, SEL );
65 static int CoCreateWindow ( vout_thread_t * );
66 static int CoDestroyWindow ( vout_thread_t * );
67 static int CoToggleFullscreen ( vout_thread_t * );
69 static void QTScaleMatrix ( vout_thread_t * );
70 static int QTCreateSequence ( vout_thread_t * );
71 static void QTDestroySequence ( vout_thread_t * );
72 static int QTNewPicture ( vout_thread_t *, picture_t * );
73 static void QTFreePicture ( vout_thread_t *, picture_t * );
75 /*****************************************************************************
76 * OpenVideo: allocates MacOS X video thread output method
77 *****************************************************************************
78 * This function allocates and initializes a MacOS X vout method.
79 *****************************************************************************/
80 int E_(OpenVideo) ( vlc_object_t *p_this )
82 vout_thread_t * p_vout = (vout_thread_t *)p_this;
86 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
87 if( p_vout->p_sys == NULL )
89 msg_Err( p_vout, "out of memory" );
93 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
95 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
96 for( i_timeout = 20 ; i_timeout-- ; )
100 msleep( INTF_IDLE_SLEEP );
106 msg_Err( p_vout, "no MacOS X interface present" );
107 free( p_vout->p_sys );
111 if( [NSApp respondsToSelector: @selector(getIntf)] )
113 intf_thread_t * p_intf;
115 for( i_timeout = 10 ; i_timeout-- ; )
117 if( ( p_intf = [NSApp getIntf] ) == NULL )
119 msleep( INTF_IDLE_SLEEP );
125 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
126 free( p_vout->p_sys );
131 p_vout->p_sys->h_img_descr =
132 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
133 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
134 p_vout->p_sys->p_fullscreen_state = NULL;
136 p_vout->p_sys->b_mouse_pointer_visible = 1;
138 /* set window size */
139 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
140 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
142 if( ( err = EnterMovies() ) != noErr )
144 msg_Err( p_vout, "EnterMovies failed: %d", err );
145 free( p_vout->p_sys->p_matrix );
146 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
147 free( p_vout->p_sys );
151 if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
153 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
154 nil, &p_vout->p_sys->img_dc );
155 if( err == noErr && p_vout->p_sys->img_dc != 0 )
157 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
158 p_vout->p_sys->i_codec = kYUV420CodecType;
162 msg_Err( p_vout, "failed to find an appropriate codec" );
167 msg_Err( p_vout, "chroma 0x%08x not supported",
168 p_vout->render.i_chroma );
171 if( p_vout->p_sys->img_dc == 0 )
173 free( p_vout->p_sys->p_matrix );
174 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
175 free( p_vout->p_sys );
179 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
180 NSArray * o_screens = [NSScreen screens];
181 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
187 int i_option = config_GetInt( p_vout, "macosx-vdev" );
189 var_Create( p_vout, "video-device", VLC_VAR_STRING |
192 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
194 while( (o_screen = [o_enumerator nextObject]) != NULL )
197 NSRect s_rect = [o_screen frame];
199 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
200 "%s %d (%dx%d)", _("Screen"), i,
201 (int)s_rect.size.width, (int)s_rect.size.height );
203 val.psz_string = psz_temp;
204 var_Change( p_vout, "video-device", VLC_VAR_ADDCHOICE, &val );
206 if( ( i - 1 ) == i_option )
208 var_Set( p_vout, "video-device", val );
214 var_AddCallback( p_vout, "video-device", vout_VarCallback,
217 val.b_bool = VLC_TRUE;
218 var_Set( p_vout, "intf-change", val );
222 if( CoCreateWindow( p_vout ) )
224 msg_Err( p_vout, "unable to create window" );
225 free( p_vout->p_sys->p_matrix );
226 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
227 free( p_vout->p_sys );
231 p_vout->pf_init = vout_Init;
232 p_vout->pf_end = vout_End;
233 p_vout->pf_manage = vout_Manage;
234 p_vout->pf_render = NULL;
235 p_vout->pf_display = vout_Display;
240 /*****************************************************************************
241 * vout_Init: initialize video thread output method
242 *****************************************************************************/
243 static int vout_Init( vout_thread_t *p_vout )
248 I_OUTPUTPICTURES = 0;
250 /* Initialize the output structure; we already found a codec,
251 * and the corresponding chroma we will be using. Since we can
252 * arbitrary scale, stick to the coordinates and aspect. */
253 p_vout->output.i_width = p_vout->render.i_width;
254 p_vout->output.i_height = p_vout->render.i_height;
255 p_vout->output.i_aspect = p_vout->render.i_aspect;
257 SetPort( p_vout->p_sys->p_qdport );
258 QTScaleMatrix( p_vout );
260 if( QTCreateSequence( p_vout ) )
262 msg_Err( p_vout, "unable to create sequence" );
266 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
267 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
271 /* Find an empty picture slot */
272 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
274 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
276 p_pic = p_vout->p_picture + i_index;
281 /* Allocate the picture */
282 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
287 p_pic->i_status = DESTROYED_PICTURE;
288 p_pic->i_type = DIRECT_PICTURE;
290 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
298 /*****************************************************************************
299 * vout_End: terminate video thread output method
300 *****************************************************************************/
301 static void vout_End( vout_thread_t *p_vout )
305 QTDestroySequence( p_vout );
307 /* Free the direct buffers we allocated */
308 for( i_index = I_OUTPUTPICTURES; i_index; )
311 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
315 /*****************************************************************************
316 * CloseVideo: destroy video thread output method
317 *****************************************************************************/
318 void E_(CloseVideo) ( vlc_object_t *p_this )
320 vout_thread_t * p_vout = (vout_thread_t *)p_this;
322 if( CoDestroyWindow( p_vout ) )
324 msg_Err( p_vout, "unable to destroy window" );
327 if ( p_vout->p_sys->p_fullscreen_state != NULL )
328 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
332 free( p_vout->p_sys->p_matrix );
333 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
335 free( p_vout->p_sys );
338 /*****************************************************************************
339 * vout_Manage: handle events
340 *****************************************************************************
341 * This function should be called regularly by video output thread. It manages
342 * console events. It returns a non null value on error.
343 *****************************************************************************/
344 static int vout_Manage( vout_thread_t *p_vout )
346 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
348 if( CoToggleFullscreen( p_vout ) )
353 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
356 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
358 QTScaleMatrix( p_vout );
359 SetDSequenceMatrix( p_vout->p_sys->i_seq,
360 p_vout->p_sys->p_matrix );
362 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
365 /* hide/show mouse cursor */
366 if( p_vout->p_sys->b_mouse_moved ||
367 p_vout->p_sys->i_time_mouse_last_moved )
369 vlc_bool_t b_change = 0;
371 if( !p_vout->p_sys->b_mouse_pointer_visible )
373 CGDisplayShowCursor( kCGDirectMainDisplay );
377 else if( !p_vout->p_sys->b_mouse_moved &&
378 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
379 p_vout->p_sys->b_mouse_pointer_visible )
381 CGDisplayHideCursor( kCGDirectMainDisplay );
388 p_vout->p_sys->i_time_mouse_last_moved = 0;
389 p_vout->p_sys->b_mouse_moved = 0;
390 p_vout->p_sys->b_mouse_pointer_visible =
391 !p_vout->p_sys->b_mouse_pointer_visible;
398 /*****************************************************************************
399 * vout_Display: displays previously rendered output
400 *****************************************************************************
401 * This function sends the currently rendered image to the display.
402 *****************************************************************************/
403 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
408 if( ( err = DecompressSequenceFrameS(
409 p_vout->p_sys->i_seq,
410 p_pic->p_sys->p_info,
411 p_pic->p_sys->i_size,
412 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
414 msg_Err( p_vout, "DecompressSequenceFrameS failed: %d", err );
418 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
422 /*****************************************************************************
423 * CoSendRequest: send request to interface thread
424 *****************************************************************************
425 * Returns 0 on success, 1 otherwise
426 *****************************************************************************/
427 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
431 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
432 VLCVout * o_vlv = [[[VLCVout alloc] init] autorelease];
434 if( [o_vlv respondsToSelector: @selector(performSelectorOnMainThread:
435 withObject:waitUntilDone:)] )
437 [o_vlv performSelectorOnMainThread: sel
438 withObject: [NSValue valueWithPointer: p_vout]
441 else if( [NSApp respondsToSelector: @selector(getIntf)] )
445 NSPort * o_recv_port;
446 NSInvocation * o_inv;
447 NSPortMessage * o_msg;
448 intf_thread_t * p_intf;
449 NSMethodSignature * o_sig;
451 p_intf = (intf_thread_t *)[NSApp getIntf];
453 o_recv_port = [[NSPort port] retain];
454 o_value = [NSValue valueWithPointer: p_vout];
456 o_sig = [VLCVout instanceMethodSignatureForSelector: sel];
457 o_inv = [NSInvocation invocationWithMethodSignature: o_sig];
458 [o_inv setArgument: &o_value atIndex: 2];
459 [o_inv setTarget: o_vlv];
460 [o_inv setSelector: sel];
462 o_array = [NSArray arrayWithObject:
463 [NSData dataWithBytes: &o_inv length: sizeof(o_inv)]];
464 o_msg = [[NSPortMessage alloc]
465 initWithSendPort: p_intf->p_sys->o_sendport
466 receivePort: o_recv_port components: o_array];
468 p_vout->p_sys->o_lock =
469 [[NSConditionLock alloc] initWithCondition: 0];
470 [o_msg sendBeforeDate: [NSDate distantPast]];
471 [p_vout->p_sys->o_lock lockWhenCondition: 1];
472 [p_vout->p_sys->o_lock unlock];
473 [p_vout->p_sys->o_lock release];
474 p_vout->p_sys->o_lock = nil;
477 [o_recv_port release];
481 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
490 /*****************************************************************************
491 * CoCreateWindow: create new window
492 *****************************************************************************
493 * Returns 0 on success, 1 otherwise
494 *****************************************************************************/
495 static int CoCreateWindow( vout_thread_t *p_vout )
497 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
499 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
506 /*****************************************************************************
507 * CoDestroyWindow: destroy window
508 *****************************************************************************
509 * Returns 0 on success, 1 otherwise
510 *****************************************************************************/
511 static int CoDestroyWindow( vout_thread_t *p_vout )
513 if( !p_vout->p_sys->b_mouse_pointer_visible )
515 CGDisplayShowCursor( kCGDirectMainDisplay );
516 p_vout->p_sys->b_mouse_pointer_visible = 1;
519 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
521 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
528 /*****************************************************************************
529 * CoToggleFullscreen: toggle fullscreen
530 *****************************************************************************
531 * Returns 0 on success, 1 otherwise
532 *****************************************************************************/
533 static int CoToggleFullscreen( vout_thread_t *p_vout )
535 QTDestroySequence( p_vout );
537 if( CoDestroyWindow( p_vout ) )
539 msg_Err( p_vout, "unable to destroy window" );
543 p_vout->b_fullscreen = !p_vout->b_fullscreen;
545 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
547 if( CoCreateWindow( p_vout ) )
549 msg_Err( p_vout, "unable to create window" );
553 SetPort( p_vout->p_sys->p_qdport );
554 QTScaleMatrix( p_vout );
556 if( QTCreateSequence( p_vout ) )
558 msg_Err( p_vout, "unable to create sequence" );
565 /*****************************************************************************
566 * QTScaleMatrix: scale matrix
567 *****************************************************************************/
568 static void QTScaleMatrix( vout_thread_t *p_vout )
571 unsigned int i_width, i_height;
572 Fixed factor_x, factor_y;
573 unsigned int i_offset_x = 0;
574 unsigned int i_offset_y = 0;
576 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
578 i_width = s_rect.right - s_rect.left;
579 i_height = s_rect.bottom - s_rect.top;
581 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
583 int i_adj_width = i_height * p_vout->output.i_aspect /
586 factor_x = FixDiv( Long2Fix( i_adj_width ),
587 Long2Fix( p_vout->output.i_width ) );
588 factor_y = FixDiv( Long2Fix( i_height ),
589 Long2Fix( p_vout->output.i_height ) );
591 i_offset_x = (i_width - i_adj_width) / 2;
595 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
596 p_vout->output.i_aspect;
598 factor_x = FixDiv( Long2Fix( i_width ),
599 Long2Fix( p_vout->output.i_width ) );
600 factor_y = FixDiv( Long2Fix( i_adj_height ),
601 Long2Fix( p_vout->output.i_height ) );
603 i_offset_y = (i_height - i_adj_height) / 2;
605 /* the following is for the resize bar. Dirty hack (hartman) */
606 if ( !p_vout->b_fullscreen)
608 i_offset_y = i_offset_y - 12;
611 SetIdentityMatrix( p_vout->p_sys->p_matrix );
613 ScaleMatrix( p_vout->p_sys->p_matrix,
615 Long2Fix(0), Long2Fix(0) );
617 TranslateMatrix( p_vout->p_sys->p_matrix,
618 Long2Fix(i_offset_x),
619 Long2Fix(i_offset_y) );
622 /*****************************************************************************
623 * QTCreateSequence: create a new sequence
624 *****************************************************************************
625 * Returns 0 on success, 1 otherwise
626 *****************************************************************************/
627 static int QTCreateSequence( vout_thread_t *p_vout )
630 ImageDescriptionPtr p_descr;
632 HLock( (Handle)p_vout->p_sys->h_img_descr );
633 p_descr = *p_vout->p_sys->h_img_descr;
635 p_descr->idSize = sizeof(ImageDescription);
636 p_descr->cType = p_vout->p_sys->i_codec;
637 p_descr->version = 1;
638 p_descr->revisionLevel = 0;
639 p_descr->vendor = 'appl';
640 p_descr->width = p_vout->output.i_width;
641 p_descr->height = p_vout->output.i_height;
642 p_descr->hRes = Long2Fix(72);
643 p_descr->vRes = Long2Fix(72);
644 p_descr->spatialQuality = codecLosslessQuality;
645 p_descr->frameCount = 1;
646 p_descr->clutID = -1;
647 p_descr->dataSize = 0;
650 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
652 if( ( err = DecompressSequenceBeginS(
653 &p_vout->p_sys->i_seq,
654 p_vout->p_sys->h_img_descr,
656 p_vout->p_sys->p_qdport,
658 p_vout->p_sys->p_matrix,
660 codecFlagUseImageBuffer,
661 codecLosslessQuality,
662 p_vout->p_sys->img_dc ) ) )
664 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
671 /*****************************************************************************
672 * QTDestroySequence: destroy sequence
673 *****************************************************************************/
674 static void QTDestroySequence( vout_thread_t *p_vout )
676 CDSequenceEnd( p_vout->p_sys->i_seq );
679 /*****************************************************************************
680 * QTNewPicture: allocate a picture
681 *****************************************************************************
682 * Returns 0 on success, 1 otherwise
683 *****************************************************************************/
684 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
686 int i_width = p_vout->output.i_width;
687 int i_height = p_vout->output.i_height;
689 /* We know the chroma, allocate a buffer which will be used
690 * directly by the decoder */
691 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
693 if( p_pic->p_sys == NULL )
698 switch( p_vout->output.i_chroma )
700 case VLC_FOURCC('I','4','2','0'):
702 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
703 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
705 /* Allocate the memory buffer */
706 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
707 16, i_width * i_height * 3 / 2 );
710 p_pic->Y_PIXELS = p_pic->p_data;
711 p_pic->p[Y_PLANE].i_lines = i_height;
712 p_pic->p[Y_PLANE].i_pitch = i_width;
713 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
714 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
717 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
718 p_pic->p[U_PLANE].i_lines = i_height / 2;
719 p_pic->p[U_PLANE].i_pitch = i_width / 2;
720 p_pic->p[U_PLANE].i_pixel_pitch = 1;
721 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
724 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
725 p_pic->p[V_PLANE].i_lines = i_height / 2;
726 p_pic->p[V_PLANE].i_pitch = i_width / 2;
727 p_pic->p[V_PLANE].i_pixel_pitch = 1;
728 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
730 /* We allocated 3 planes */
733 #define P p_pic->p_sys->pixmap_i420
734 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
735 - p_pic->p_sys->p_info;
736 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
737 - p_pic->p_sys->p_info;
738 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
739 - p_pic->p_sys->p_info;
741 P.componentInfoY.rowBytes = i_width;
742 P.componentInfoCb.rowBytes = i_width / 2;
743 P.componentInfoCr.rowBytes = i_width / 2;
749 /* Unknown chroma, tell the guy to get lost */
750 free( p_pic->p_sys );
751 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
752 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
760 /*****************************************************************************
761 * QTFreePicture: destroy a picture allocated with QTNewPicture
762 *****************************************************************************/
763 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
765 switch( p_vout->output.i_chroma )
767 case VLC_FOURCC('I','4','2','0'):
768 free( p_pic->p_data_orig );
772 free( p_pic->p_sys );
775 /*****************************************************************************
776 * VLCWindow implementation
777 *****************************************************************************/
778 @implementation VLCWindow
780 - (void)setVout:(vout_thread_t *)_p_vout
785 - (vout_thread_t *)getVout
790 - (void)toggleFullscreen
792 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
797 return( p_vout->b_fullscreen );
800 - (BOOL)canBecomeKeyWindow
805 - (void)keyDown:(NSEvent *)o_event
809 if( [[o_event characters] length] )
811 key = [[o_event characters] characterAtIndex: 0];
817 [self toggleFullscreen];
820 case (unichar)0x1b: /* escape */
821 if( [self isFullscreen] )
823 [self toggleFullscreen];
828 p_vout->p_vlc->b_die = VLC_TRUE;
832 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
836 [super keyDown: o_event];
841 /* This is actually the same as VLCControls::stop. */
842 - (BOOL)windowShouldClose:(id)sender
844 intf_thread_t * p_intf = [NSApp getIntf];
845 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
847 if( p_playlist == NULL )
852 playlist_Stop( p_playlist );
853 vlc_object_release( p_playlist );
854 p_intf->p_sys->b_stopping = 1;
856 /* The window will be closed by the intf later. */
862 /*****************************************************************************
863 * VLCView implementation
864 *****************************************************************************/
865 @implementation VLCView
867 - (void)drawRect:(NSRect)rect
869 vout_thread_t * p_vout;
870 id o_window = [self window];
871 p_vout = (vout_thread_t *)[o_window getVout];
873 if ( p_vout->b_fullscreen )
875 [[NSColor blackColor] set];
878 [super drawRect: rect];
880 p_vout->i_changes |= VOUT_SIZE_CHANGE;
883 - (BOOL)acceptsFirstResponder
888 - (BOOL)becomeFirstResponder
890 [[self window] setAcceptsMouseMovedEvents: YES];
894 - (BOOL)resignFirstResponder
896 [[self window] setAcceptsMouseMovedEvents: NO];
900 - (void)mouseUp:(NSEvent *)o_event
902 vout_thread_t * p_vout;
903 id o_window = [self window];
904 p_vout = (vout_thread_t *)[o_window getVout];
906 switch( [o_event type] )
911 val.b_bool = VLC_TRUE;
912 var_Set( p_vout, "mouse-clicked", val );
917 [super mouseUp: o_event];
922 - (void)mouseMoved:(NSEvent *)o_event
928 vout_thread_t * p_vout;
929 id o_window = [self window];
930 p_vout = (vout_thread_t *)[o_window getVout];
932 s_rect = [self bounds];
933 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
934 b_inside = [self mouse: ml inRect: s_rect];
939 int i_width, i_height, i_x, i_y;
941 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
942 (unsigned int)s_rect.size.height,
943 &i_x, &i_y, &i_width, &i_height );
945 val.i_int = ( ((int)ml.x) - i_x ) *
946 p_vout->render.i_width / i_width;
947 var_Set( p_vout, "mouse-x", val );
949 val.i_int = ( ((int)ml.y) - i_y ) *
950 p_vout->render.i_height / i_height;
951 var_Set( p_vout, "mouse-y", val );
953 val.b_bool = VLC_TRUE;
954 var_Set( p_vout, "mouse-moved", val );
958 [super mouseMoved: o_event];
964 /*****************************************************************************
965 * VLCVout implementation
966 *****************************************************************************/
967 @implementation VLCVout
969 - (void)createWindow:(NSValue *)o_value
974 vout_thread_t * p_vout;
976 vlc_bool_t b_main_screen;
978 intf_thread_t * p_intf = [NSApp getIntf];
979 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
982 p_vout = (vout_thread_t *)[o_value pointerValue];
984 p_vout->p_sys->o_window = [VLCWindow alloc];
985 [p_vout->p_sys->o_window setVout: p_vout];
986 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
988 if( var_Get( p_vout, "video-device", &val ) < 0 )
990 o_screen = [NSScreen mainScreen];
995 unsigned int i_index = 0;
996 NSArray *o_screens = [NSScreen screens];
998 if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
999 [o_screens count] < i_index )
1001 o_screen = [NSScreen mainScreen];
1007 o_screen = [o_screens objectAtIndex: i_index];
1008 config_PutInt( p_vout, "macosx-vdev", i_index );
1009 b_main_screen = (i_index == 0);
1012 free( val.psz_string );
1015 if( p_vout->b_fullscreen )
1017 NSRect screen_rect = [o_screen frame];
1018 screen_rect.origin.x = screen_rect.origin.y = 0;
1020 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1021 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1022 NULL, NULL, fullScreenHideCursor | fullScreenAllowEvents );
1024 [p_vout->p_sys->o_window
1025 initWithContentRect: screen_rect
1026 styleMask: NSBorderlessWindowMask
1027 backing: NSBackingStoreBuffered
1028 defer: NO screen: o_screen];
1030 [p_vout->p_sys->o_window setLevel: NSModalPanelWindowLevel];
1034 unsigned int i_stylemask = NSTitledWindowMask |
1035 NSMiniaturizableWindowMask |
1036 NSClosableWindowMask |
1037 NSResizableWindowMask |
1038 NSTexturedBackgroundWindowMask;
1040 /* the following is for the resize bar. Dirty hack (hartman) */
1041 p_vout->p_sys->s_rect.size.height = p_vout->p_sys->s_rect.size.height + 24;
1043 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1044 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1045 p_vout->p_sys->p_fullscreen_state = NULL;
1047 [p_vout->p_sys->o_window
1048 initWithContentRect: p_vout->p_sys->s_rect
1049 styleMask: i_stylemask
1050 backing: NSBackingStoreBuffered
1051 defer: NO screen: o_screen];
1053 if( !p_vout->p_sys->b_pos_saved )
1055 [p_vout->p_sys->o_window center];
1059 o_view = [[VLCView alloc] init];
1060 /* FIXME: [o_view setMenu:] */
1061 [p_vout->p_sys->o_window setContentView: o_view];
1062 [o_view autorelease];
1065 p_vout->p_sys->p_qdport = [o_view qdPort];
1066 [o_view unlockFocus];
1069 if( p_playlist == NULL )
1074 vlc_mutex_lock( &p_playlist->object_lock );
1075 o_title = [NSString stringWithUTF8String:
1076 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1077 vlc_mutex_unlock( &p_playlist->object_lock );
1079 vlc_object_release( p_playlist );
1083 [p_vout->p_sys->o_window setTitle: o_title];
1084 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1088 [p_vout->p_sys->o_window setTitle:
1089 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1090 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1094 - (void)destroyWindow:(NSValue *)o_value
1096 vout_thread_t * p_vout;
1098 p_vout = (vout_thread_t *)[o_value pointerValue];
1100 if( !p_vout->b_fullscreen )
1104 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1105 p_vout->p_sys->s_rect.size = s_rect.size;
1107 s_rect = [p_vout->p_sys->o_window frame];
1108 p_vout->p_sys->s_rect.origin = s_rect.origin;
1110 p_vout->p_sys->b_pos_saved = 1;
1112 else /* the following is for the resize bar. Dirty hack (hartman) */
1114 p_vout->p_sys->s_rect.size.height = p_vout->p_sys->s_rect.size.height - 24;
1117 p_vout->p_sys->p_qdport = nil;
1118 [p_vout->p_sys->o_window close];
1119 p_vout->p_sys->o_window = nil;