1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.40 2003/03/18 04:07:23 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() */
34 #include <QuickTime/QuickTime.h>
39 #define QT_MAX_DIRECTBUFFERS 10
40 #define VL_MAX_DISPLAYS 16
47 /* When using I420 output */
48 PlanarPixmapInfoYUV420 pixmap_i420;
51 /*****************************************************************************
53 *****************************************************************************/
54 static int vout_Init ( vout_thread_t * );
55 static void vout_End ( vout_thread_t * );
56 static int vout_Manage ( vout_thread_t * );
57 static void vout_Display ( vout_thread_t *, picture_t * );
59 static int CoSendRequest ( vout_thread_t *, SEL );
60 static int CoCreateWindow ( vout_thread_t * );
61 static int CoDestroyWindow ( vout_thread_t * );
62 static int CoToggleFullscreen ( vout_thread_t * );
64 static void VLShowHideCursors ( vout_thread_t *, BOOL );
66 static void QTScaleMatrix ( vout_thread_t * );
67 static int QTCreateSequence ( vout_thread_t * );
68 static void QTDestroySequence ( vout_thread_t * );
69 static int QTNewPicture ( vout_thread_t *, picture_t * );
70 static void QTFreePicture ( vout_thread_t *, picture_t * );
72 /*****************************************************************************
73 * OpenVideo: allocates MacOS X video thread output method
74 *****************************************************************************
75 * This function allocates and initializes a MacOS X vout method.
76 *****************************************************************************/
77 int E_(OpenVideo) ( vlc_object_t *p_this )
79 vout_thread_t * p_vout = (vout_thread_t *)p_this;
83 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
84 if( p_vout->p_sys == NULL )
86 msg_Err( p_vout, "out of memory" );
90 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
92 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
93 for( i_timeout = 20 ; i_timeout-- ; )
97 msleep( INTF_IDLE_SLEEP );
103 msg_Err( p_vout, "no MacOS X interface present" );
104 free( p_vout->p_sys );
108 if( [NSApp respondsToSelector: @selector(getIntf)] )
110 intf_thread_t * p_intf;
112 for( i_timeout = 10 ; i_timeout-- ; )
114 if( ( p_intf = [NSApp getIntf] ) == NULL )
116 msleep( INTF_IDLE_SLEEP );
122 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
123 free( p_vout->p_sys );
128 p_vout->p_sys->h_img_descr =
129 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
130 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
131 p_vout->p_sys->p_fullscreen_state = NULL;
133 p_vout->p_sys->b_mouse_pointer_visible = 1;
135 /* set window size */
136 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
137 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
139 if( ( err = EnterMovies() ) != noErr )
141 msg_Err( p_vout, "EnterMovies failed: %d", err );
142 free( p_vout->p_sys->p_matrix );
143 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
144 free( p_vout->p_sys );
148 if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
150 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
151 nil, &p_vout->p_sys->img_dc );
152 if( err == noErr && p_vout->p_sys->img_dc != 0 )
154 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
155 p_vout->p_sys->i_codec = kYUV420CodecType;
159 msg_Err( p_vout, "failed to find an appropriate codec" );
164 msg_Err( p_vout, "chroma 0x%08x not supported",
165 p_vout->render.i_chroma );
168 if( p_vout->p_sys->img_dc == 0 )
170 free( p_vout->p_sys->p_matrix );
171 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172 free( p_vout->p_sys );
176 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
177 NSArray * o_screens = [NSScreen screens];
178 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
184 int i_option = config_GetInt( p_vout, "macosx-vdev" );
186 var_Create( p_vout, "video-device", VLC_VAR_STRING |
189 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
191 while( (o_screen = [o_enumerator nextObject]) != NULL )
194 NSRect s_rect = [o_screen frame];
196 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
197 "%s %d (%dx%d)", _("Screen"), i,
198 (int)s_rect.size.width, (int)s_rect.size.height );
200 val.psz_string = psz_temp;
201 var_Change( p_vout, "video-device", VLC_VAR_ADDCHOICE, &val );
203 if( ( i - 1 ) == i_option )
205 var_Set( p_vout, "video-device", val );
211 var_AddCallback( p_vout, "video-device", vout_VarCallback,
214 val.b_bool = VLC_TRUE;
215 var_Set( p_vout, "intf-change", val );
219 if( CoCreateWindow( p_vout ) )
221 msg_Err( p_vout, "unable to create window" );
222 free( p_vout->p_sys->p_matrix );
223 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
224 free( p_vout->p_sys );
228 p_vout->pf_init = vout_Init;
229 p_vout->pf_end = vout_End;
230 p_vout->pf_manage = vout_Manage;
231 p_vout->pf_render = NULL;
232 p_vout->pf_display = vout_Display;
237 /*****************************************************************************
238 * vout_Init: initialize video thread output method
239 *****************************************************************************/
240 static int vout_Init( vout_thread_t *p_vout )
245 I_OUTPUTPICTURES = 0;
247 /* Initialize the output structure; we already found a codec,
248 * and the corresponding chroma we will be using. Since we can
249 * arbitrary scale, stick to the coordinates and aspect. */
250 p_vout->output.i_width = p_vout->render.i_width;
251 p_vout->output.i_height = p_vout->render.i_height;
252 p_vout->output.i_aspect = p_vout->render.i_aspect;
254 SetPort( p_vout->p_sys->p_qdport );
255 QTScaleMatrix( p_vout );
257 if( QTCreateSequence( p_vout ) )
259 msg_Err( p_vout, "unable to create sequence" );
263 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
264 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
268 /* Find an empty picture slot */
269 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
271 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
273 p_pic = p_vout->p_picture + i_index;
278 /* Allocate the picture */
279 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
284 p_pic->i_status = DESTROYED_PICTURE;
285 p_pic->i_type = DIRECT_PICTURE;
287 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
295 /*****************************************************************************
296 * vout_End: terminate video thread output method
297 *****************************************************************************/
298 static void vout_End( vout_thread_t *p_vout )
302 QTDestroySequence( p_vout );
304 /* Free the direct buffers we allocated */
305 for( i_index = I_OUTPUTPICTURES; i_index; )
308 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
312 /*****************************************************************************
313 * CloseVideo: destroy video thread output method
314 *****************************************************************************/
315 void E_(CloseVideo) ( vlc_object_t *p_this )
317 vout_thread_t * p_vout = (vout_thread_t *)p_this;
319 if( CoDestroyWindow( p_vout ) )
321 msg_Err( p_vout, "unable to destroy window" );
324 if ( p_vout->p_sys->p_fullscreen_state != NULL )
325 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
329 free( p_vout->p_sys->p_matrix );
330 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
332 free( p_vout->p_sys );
335 /*****************************************************************************
336 * vout_Manage: handle events
337 *****************************************************************************
338 * This function should be called regularly by video output thread. It manages
339 * console events. It returns a non null value on error.
340 *****************************************************************************/
341 static int vout_Manage( vout_thread_t *p_vout )
343 vlc_bool_t b_change = 0;
344 intf_thread_t * p_intf = [NSApp getIntf];
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 && p_vout->b_fullscreen &&
367 p_intf->p_sys->b_play_status )
369 if( !p_vout->p_sys->b_mouse_pointer_visible )
371 VLShowHideCursors( p_vout, NO );
374 else if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
375 p_vout->p_sys->b_mouse_pointer_visible )
377 VLShowHideCursors( p_vout, YES );
382 else if ( p_vout->b_fullscreen && !p_intf->p_sys->b_play_status )
384 if( !p_vout->p_sys->b_mouse_pointer_visible )
386 VLShowHideCursors( p_vout, NO );
389 else if( p_vout->p_sys->b_mouse_pointer_visible )
391 p_vout->p_sys->i_time_mouse_last_moved = mdate();
392 p_vout->p_sys->b_mouse_moved = 1;
398 p_vout->p_sys->b_mouse_moved = 0;
399 p_vout->p_sys->i_time_mouse_last_moved = 0;
405 /*****************************************************************************
406 * vout_Display: displays previously rendered output
407 *****************************************************************************
408 * This function sends the currently rendered image to the display.
409 *****************************************************************************/
410 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
415 if( ( err = DecompressSequenceFrameS(
416 p_vout->p_sys->i_seq,
417 p_pic->p_sys->p_info,
418 p_pic->p_sys->i_size,
419 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
421 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
425 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
429 /*****************************************************************************
430 * CoSendRequest: send request to interface thread
431 *****************************************************************************
432 * Returns 0 on success, 1 otherwise
433 *****************************************************************************/
434 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
438 VLCVout * o_vlv = [[VLCVout alloc] init];
440 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
442 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
450 /*****************************************************************************
451 * CoCreateWindow: create new window
452 *****************************************************************************
453 * Returns 0 on success, 1 otherwise
454 *****************************************************************************/
455 static int CoCreateWindow( vout_thread_t *p_vout )
457 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
459 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
466 /*****************************************************************************
467 * CoDestroyWindow: destroy window
468 *****************************************************************************
469 * Returns 0 on success, 1 otherwise
470 *****************************************************************************/
471 static int CoDestroyWindow( vout_thread_t *p_vout )
473 if( !p_vout->p_sys->b_mouse_pointer_visible )
475 VLShowHideCursors( p_vout, NO );
476 p_vout->p_sys->b_mouse_pointer_visible = 1;
479 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
481 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
488 /*****************************************************************************
489 * CoToggleFullscreen: toggle fullscreen
490 *****************************************************************************
491 * Returns 0 on success, 1 otherwise
492 *****************************************************************************/
493 static int CoToggleFullscreen( vout_thread_t *p_vout )
495 QTDestroySequence( p_vout );
497 if( CoDestroyWindow( p_vout ) )
499 msg_Err( p_vout, "unable to destroy window" );
503 p_vout->b_fullscreen = !p_vout->b_fullscreen;
505 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
507 if( CoCreateWindow( p_vout ) )
509 msg_Err( p_vout, "unable to create window" );
513 SetPort( p_vout->p_sys->p_qdport );
514 QTScaleMatrix( p_vout );
516 if( QTCreateSequence( p_vout ) )
518 msg_Err( p_vout, "unable to create sequence" );
525 /*****************************************************************************
526 * VLShowHideCursors: if b_hide then hide the cursors on every display
527 * that contains p_vout, else show the cursors instead.
528 *****************************************************************************
529 * We cannot use kCGDirectMainDisplay, because this is always the display with
531 *****************************************************************************/
532 static void VLShowHideCursors ( vout_thread_t *p_vout, BOOL b_hide )
536 CGDirectDisplayID displays[VL_MAX_DISPLAYS];
537 CGDisplayCount displayCount;
539 o_screen = [p_vout->p_sys->o_window screen];
540 frame = [o_screen frame];
542 int err = CGGetDisplaysWithRect( CGRectMake( NSMinX( frame ), NSMinY( frame ),
543 NSWidth( frame ), NSHeight( frame ) ), VL_MAX_DISPLAYS, displays, &displayCount );
545 if ( displayCount > 0 && !err )
548 /* multiple displays are possible, because of mirroring.
549 * mirroring is essential one screen on mult. displays. */
550 for ( i=0 ; i < displayCount ; i++ )
554 CGDisplayHideCursor( displays[i] );
555 p_vout->p_sys->b_mouse_pointer_visible = 0;
559 CGDisplayShowCursor( displays[i] );
560 p_vout->p_sys->b_mouse_pointer_visible = 1;
567 /*****************************************************************************
568 * QTScaleMatrix: scale matrix
569 *****************************************************************************/
570 static void QTScaleMatrix( vout_thread_t *p_vout )
573 unsigned int i_width, i_height;
574 Fixed factor_x, factor_y;
575 unsigned int i_offset_x = 0;
576 unsigned int i_offset_y = 0;
578 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
580 i_width = s_rect.right - s_rect.left;
581 i_height = s_rect.bottom - s_rect.top;
583 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
585 int i_adj_width = i_height * p_vout->output.i_aspect /
588 factor_x = FixDiv( Long2Fix( i_adj_width ),
589 Long2Fix( p_vout->output.i_width ) );
590 factor_y = FixDiv( Long2Fix( i_height ),
591 Long2Fix( p_vout->output.i_height ) );
593 i_offset_x = (i_width - i_adj_width) / 2;
597 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
598 p_vout->output.i_aspect;
600 factor_x = FixDiv( Long2Fix( i_width ),
601 Long2Fix( p_vout->output.i_width ) );
602 factor_y = FixDiv( Long2Fix( i_adj_height ),
603 Long2Fix( p_vout->output.i_height ) );
605 i_offset_y = (i_height - i_adj_height) / 2;
608 SetIdentityMatrix( p_vout->p_sys->p_matrix );
610 ScaleMatrix( p_vout->p_sys->p_matrix,
612 Long2Fix(0), Long2Fix(0) );
614 TranslateMatrix( p_vout->p_sys->p_matrix,
615 Long2Fix(i_offset_x),
616 Long2Fix(i_offset_y) );
619 /*****************************************************************************
620 * QTCreateSequence: create a new sequence
621 *****************************************************************************
622 * Returns 0 on success, 1 otherwise
623 *****************************************************************************/
624 static int QTCreateSequence( vout_thread_t *p_vout )
627 ImageDescriptionPtr p_descr;
629 HLock( (Handle)p_vout->p_sys->h_img_descr );
630 p_descr = *p_vout->p_sys->h_img_descr;
632 p_descr->idSize = sizeof(ImageDescription);
633 p_descr->cType = p_vout->p_sys->i_codec;
634 p_descr->version = 1;
635 p_descr->revisionLevel = 0;
636 p_descr->vendor = 'appl';
637 p_descr->width = p_vout->output.i_width;
638 p_descr->height = p_vout->output.i_height;
639 p_descr->hRes = Long2Fix(72);
640 p_descr->vRes = Long2Fix(72);
641 p_descr->spatialQuality = codecLosslessQuality;
642 p_descr->frameCount = 1;
643 p_descr->clutID = -1;
644 p_descr->dataSize = 0;
647 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
649 if( ( err = DecompressSequenceBeginS(
650 &p_vout->p_sys->i_seq,
651 p_vout->p_sys->h_img_descr,
653 p_vout->p_sys->p_qdport,
655 p_vout->p_sys->p_matrix,
657 codecFlagUseImageBuffer,
658 codecLosslessQuality,
659 p_vout->p_sys->img_dc ) ) )
661 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
668 /*****************************************************************************
669 * QTDestroySequence: destroy sequence
670 *****************************************************************************/
671 static void QTDestroySequence( vout_thread_t *p_vout )
673 CDSequenceEnd( p_vout->p_sys->i_seq );
676 /*****************************************************************************
677 * QTNewPicture: allocate a picture
678 *****************************************************************************
679 * Returns 0 on success, 1 otherwise
680 *****************************************************************************/
681 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
683 int i_width = p_vout->output.i_width;
684 int i_height = p_vout->output.i_height;
686 /* We know the chroma, allocate a buffer which will be used
687 * directly by the decoder */
688 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
690 if( p_pic->p_sys == NULL )
695 switch( p_vout->output.i_chroma )
697 case VLC_FOURCC('I','4','2','0'):
699 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
700 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
702 /* Allocate the memory buffer */
703 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
704 16, i_width * i_height * 3 / 2 );
707 p_pic->Y_PIXELS = p_pic->p_data;
708 p_pic->p[Y_PLANE].i_lines = i_height;
709 p_pic->p[Y_PLANE].i_pitch = i_width;
710 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
711 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
714 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
715 p_pic->p[U_PLANE].i_lines = i_height / 2;
716 p_pic->p[U_PLANE].i_pitch = i_width / 2;
717 p_pic->p[U_PLANE].i_pixel_pitch = 1;
718 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
721 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
722 p_pic->p[V_PLANE].i_lines = i_height / 2;
723 p_pic->p[V_PLANE].i_pitch = i_width / 2;
724 p_pic->p[V_PLANE].i_pixel_pitch = 1;
725 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
727 /* We allocated 3 planes */
730 #define P p_pic->p_sys->pixmap_i420
731 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
732 - p_pic->p_sys->p_info;
733 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
734 - p_pic->p_sys->p_info;
735 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
736 - p_pic->p_sys->p_info;
738 P.componentInfoY.rowBytes = i_width;
739 P.componentInfoCb.rowBytes = i_width / 2;
740 P.componentInfoCr.rowBytes = i_width / 2;
746 /* Unknown chroma, tell the guy to get lost */
747 free( p_pic->p_sys );
748 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
749 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
757 /*****************************************************************************
758 * QTFreePicture: destroy a picture allocated with QTNewPicture
759 *****************************************************************************/
760 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
762 switch( p_vout->output.i_chroma )
764 case VLC_FOURCC('I','4','2','0'):
765 free( p_pic->p_data_orig );
769 free( p_pic->p_sys );
772 /*****************************************************************************
773 * VLCWindow implementation
774 *****************************************************************************/
775 @implementation VLCWindow
777 - (void)setVout:(vout_thread_t *)_p_vout
782 - (vout_thread_t *)getVout
787 - (void)scaleWindowWithFactor: (float)factor
790 int i_corrected_height, i_corrected_width;
792 NSPoint topleftscreen;
794 if ( !p_vout->b_fullscreen )
797 topleftbase.y = [self frame].size.height;
798 topleftscreen = [self convertBaseToScreen: topleftbase];
800 if( p_vout->output.i_height * p_vout->output.i_aspect >
801 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
803 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
805 newsize.width = (int) ( i_corrected_width * factor );
806 newsize.height = (int) ( p_vout->render.i_height * factor );
810 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
811 p_vout->output.i_aspect;
812 newsize.width = (int) ( p_vout->render.i_width * factor );
813 newsize.height = (int) ( i_corrected_height * factor );
816 [self setContentSize: newsize];
818 [self setFrameTopLeftPoint: topleftscreen];
819 p_vout->i_changes |= VOUT_SIZE_CHANGE;
823 - (void)toggleFullscreen
825 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
830 return( p_vout->b_fullscreen );
833 - (BOOL)canBecomeKeyWindow
838 - (void)keyDown:(NSEvent *)o_event
842 if( [[o_event characters] length] )
844 key = [[o_event characters] characterAtIndex: 0];
850 [self toggleFullscreen];
853 case (unichar)0x1b: /* escape */
854 if( [self isFullscreen] )
856 [self toggleFullscreen];
861 p_vout->p_vlc->b_die = VLC_TRUE;
865 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
869 [super keyDown: o_event];
876 NSMutableString * o_title;
878 intf_thread_t * p_intf = [NSApp getIntf];
879 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
882 if( p_playlist == NULL )
887 vlc_mutex_lock( &p_playlist->object_lock );
888 o_title = [NSMutableString stringWithUTF8String:
889 p_playlist->pp_items[p_playlist->i_index]->psz_name];
890 vlc_mutex_unlock( &p_playlist->object_lock );
892 vlc_object_release( p_playlist );
896 NSRange prefix_range = [o_title rangeOfString: @"file:"];
897 if( prefix_range.location != NSNotFound )
899 [o_title deleteCharactersInRange: prefix_range];
902 [self setTitleWithRepresentedFilename: o_title];
907 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
911 /* This is actually the same as VLCControls::stop. */
912 - (BOOL)windowShouldClose:(id)sender
914 intf_thread_t * p_intf = [NSApp getIntf];
915 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
917 if( p_playlist == NULL )
922 playlist_Stop( p_playlist );
923 vlc_object_release( p_playlist );
925 /* The window will be closed by the intf later. */
931 /*****************************************************************************
932 * VLCView implementation
933 *****************************************************************************/
934 @implementation VLCView
936 - (void)drawRect:(NSRect)rect
938 vout_thread_t * p_vout;
939 id o_window = [self window];
940 p_vout = (vout_thread_t *)[o_window getVout];
942 [[NSColor blackColor] set];
944 [super drawRect: rect];
946 p_vout->i_changes |= VOUT_SIZE_CHANGE;
949 - (BOOL)acceptsFirstResponder
954 - (BOOL)becomeFirstResponder
956 [[self window] setAcceptsMouseMovedEvents: YES];
960 - (BOOL)resignFirstResponder
962 [[self window] setAcceptsMouseMovedEvents: NO];
966 - (void)mouseDown:(NSEvent *)o_event
968 vout_thread_t * p_vout;
969 id o_window = [self window];
970 p_vout = (vout_thread_t *)[o_window getVout];
973 switch( [o_event type] )
975 case NSLeftMouseDown:
977 var_Get( p_vout, "mouse-button-down", &val );
979 var_Set( p_vout, "mouse-button-down", val );
984 [super mouseDown: o_event];
989 - (void)otherMouseDown:(NSEvent *)o_event
991 /* This is not the the wheel button. you need to poll the
992 * mouseWheel event for that. other is a third, forth or fifth button */
993 vout_thread_t * p_vout;
994 id o_window = [self window];
995 p_vout = (vout_thread_t *)[o_window getVout];
998 switch( [o_event type] )
1000 case NSOtherMouseDown:
1002 var_Get( p_vout, "mouse-button-down", &val );
1004 var_Set( p_vout, "mouse-button-down", val );
1009 [super mouseDown: o_event];
1014 - (void)rightMouseDown:(NSEvent *)o_event
1016 vout_thread_t * p_vout;
1017 id o_window = [self window];
1018 p_vout = (vout_thread_t *)[o_window getVout];
1021 switch( [o_event type] )
1023 case NSRightMouseDown:
1025 var_Get( p_vout, "mouse-button-down", &val );
1027 var_Set( p_vout, "mouse-button-down", val );
1032 [super mouseDown: o_event];
1037 - (void)mouseUp:(NSEvent *)o_event
1039 vout_thread_t * p_vout;
1040 id o_window = [self window];
1041 p_vout = (vout_thread_t *)[o_window getVout];
1044 switch( [o_event type] )
1049 b_val.b_bool = VLC_TRUE;
1050 var_Set( p_vout, "mouse-clicked", b_val );
1052 var_Get( p_vout, "mouse-button-down", &val );
1054 var_Set( p_vout, "mouse-button-down", val );
1059 [super mouseUp: o_event];
1064 - (void)otherMouseUp:(NSEvent *)o_event
1066 vout_thread_t * p_vout;
1067 id o_window = [self window];
1068 p_vout = (vout_thread_t *)[o_window getVout];
1071 switch( [o_event type] )
1073 case NSOtherMouseUp:
1075 var_Get( p_vout, "mouse-button-down", &val );
1077 var_Set( p_vout, "mouse-button-down", val );
1082 [super mouseUp: o_event];
1087 - (void)rightMouseUp:(NSEvent *)o_event
1089 vout_thread_t * p_vout;
1090 id o_window = [self window];
1091 p_vout = (vout_thread_t *)[o_window getVout];
1094 switch( [o_event type] )
1096 case NSRightMouseUp:
1098 var_Get( p_vout, "mouse-button-down", &val );
1100 var_Set( p_vout, "mouse-button-down", val );
1105 [super mouseUp: o_event];
1110 - (void)mouseDragged:(NSEvent *)o_event
1112 [self mouseMoved:o_event];
1115 - (void)otherMouseDragged:(NSEvent *)o_event
1117 [self mouseMoved:o_event];
1120 - (void)rightMouseDragged:(NSEvent *)o_event
1122 [self mouseMoved:o_event];
1125 - (void)mouseMoved:(NSEvent *)o_event
1131 vout_thread_t * p_vout;
1132 id o_window = [self window];
1133 p_vout = (vout_thread_t *)[o_window getVout];
1135 s_rect = [self bounds];
1136 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1137 b_inside = [self mouse: ml inRect: s_rect];
1142 int i_width, i_height, i_x, i_y;
1144 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1145 (unsigned int)s_rect.size.height,
1146 &i_x, &i_y, &i_width, &i_height );
1148 val.i_int = ( ((int)ml.x) - i_x ) *
1149 p_vout->render.i_width / i_width;
1150 var_Set( p_vout, "mouse-x", val );
1152 val.i_int = ( ((int)ml.y) - i_y ) *
1153 p_vout->render.i_height / i_height;
1154 var_Set( p_vout, "mouse-y", val );
1156 val.b_bool = VLC_TRUE;
1157 var_Set( p_vout, "mouse-moved", val );
1159 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1160 p_vout->p_sys->b_mouse_moved = 1;
1161 [super mouseMoved: o_event];
1166 /*****************************************************************************
1167 * VLCVout implementation
1168 *****************************************************************************/
1169 @implementation VLCVout
1171 - (void)createWindow:(NSValue *)o_value
1175 NSScreen * o_screen;
1176 vout_thread_t * p_vout;
1177 vlc_bool_t b_main_screen;
1179 p_vout = (vout_thread_t *)[o_value pointerValue];
1181 p_vout->p_sys->o_window = [VLCWindow alloc];
1182 [p_vout->p_sys->o_window setVout: p_vout];
1183 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1185 if( var_Get( p_vout, "video-device", &val ) < 0 )
1187 o_screen = [NSScreen mainScreen];
1192 unsigned int i_index = 0;
1193 NSArray *o_screens = [NSScreen screens];
1195 if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
1196 [o_screens count] < i_index )
1198 o_screen = [NSScreen mainScreen];
1204 o_screen = [o_screens objectAtIndex: i_index];
1205 config_PutInt( p_vout, "macosx-vdev", i_index );
1206 b_main_screen = (i_index == 0);
1209 free( val.psz_string );
1212 if( p_vout->b_fullscreen )
1214 NSRect screen_rect = [o_screen frame];
1215 screen_rect.origin.x = screen_rect.origin.y = 0;
1217 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1218 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1219 NULL, NULL, fullScreenAllowEvents );
1221 [p_vout->p_sys->o_window
1222 initWithContentRect: screen_rect
1223 styleMask: NSBorderlessWindowMask
1224 backing: NSBackingStoreBuffered
1225 defer: NO screen: o_screen];
1227 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1228 p_vout->p_sys->b_mouse_moved = 1;
1229 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1233 unsigned int i_stylemask = NSTitledWindowMask |
1234 NSMiniaturizableWindowMask |
1235 NSClosableWindowMask |
1236 NSResizableWindowMask;
1238 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1239 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1240 p_vout->p_sys->p_fullscreen_state = NULL;
1242 [p_vout->p_sys->o_window
1243 initWithContentRect: p_vout->p_sys->s_rect
1244 styleMask: i_stylemask
1245 backing: NSBackingStoreBuffered
1246 defer: NO screen: o_screen];
1248 if( !p_vout->p_sys->b_pos_saved )
1250 [p_vout->p_sys->o_window center];
1254 o_view = [[VLCView alloc] init];
1255 /* FIXME: [o_view setMenu:] */
1256 [p_vout->p_sys->o_window setContentView: o_view];
1257 [o_view autorelease];
1260 p_vout->p_sys->p_qdport = [o_view qdPort];
1261 [o_view unlockFocus];
1263 [p_vout->p_sys->o_window updateTitle];
1264 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1267 - (void)destroyWindow:(NSValue *)o_value
1269 vout_thread_t * p_vout;
1271 p_vout = (vout_thread_t *)[o_value pointerValue];
1273 if( !p_vout->b_fullscreen )
1277 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1278 p_vout->p_sys->s_rect.size = s_rect.size;
1280 s_rect = [p_vout->p_sys->o_window frame];
1281 p_vout->p_sys->s_rect.origin = s_rect.origin;
1283 p_vout->p_sys->b_pos_saved = 1;
1286 p_vout->p_sys->p_qdport = nil;
1287 [p_vout->p_sys->o_window close];
1288 p_vout->p_sys->o_window = nil;