1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.41 2003/03/18 22:14:42 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 VLCHideMouse ( 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 /* we need an NSApp to create a window from */
104 msg_Err( p_vout, "no MacOS X application present" );
105 free( p_vout->p_sys );
109 if( [NSApp respondsToSelector: @selector(getIntf)] )
111 intf_thread_t * p_intf;
113 for( i_timeout = 10 ; i_timeout-- ; )
115 if( ( p_intf = [NSApp getIntf] ) == NULL )
117 msleep( INTF_IDLE_SLEEP );
123 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
124 free( p_vout->p_sys );
129 p_vout->p_sys->h_img_descr =
130 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
131 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
132 p_vout->p_sys->p_fullscreen_state = NULL;
134 p_vout->p_sys->b_mouse_pointer_visible = YES;
135 p_vout->p_sys->b_mouse_moved = YES;
136 p_vout->p_sys->i_time_mouse_last_moved = mdate();
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 * this code looks unnecessarily complicated, but is necessary like this.
367 * it has to deal with multiple monitors and therefore checks a lot */
368 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
370 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
371 p_vout->p_sys->b_mouse_pointer_visible )
373 VLCHideMouse( p_vout, YES );
375 else if ( !p_vout->p_sys->b_mouse_pointer_visible )
377 vlc_bool_t b_playing = NO;
378 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
381 if ( p_playlist != nil )
383 vlc_mutex_lock( &p_playlist->object_lock );
384 if( p_playlist->p_input != NULL )
386 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
387 b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
388 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
390 vlc_mutex_unlock( &p_playlist->object_lock );
391 vlc_object_release( p_playlist );
393 if ( ![p_vout->p_sys->o_window isKeyWindow] || !b_playing )
395 VLCHideMouse( p_vout, NO );
399 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
401 if( !p_vout->p_sys->b_mouse_pointer_visible )
403 VLCHideMouse( p_vout, NO );
407 p_vout->p_sys->b_mouse_moved = NO;
414 /*****************************************************************************
415 * vout_Display: displays previously rendered output
416 *****************************************************************************
417 * This function sends the currently rendered image to the display.
418 *****************************************************************************/
419 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
424 if( ( err = DecompressSequenceFrameS(
425 p_vout->p_sys->i_seq,
426 p_pic->p_sys->p_info,
427 p_pic->p_sys->i_size,
428 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
430 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
434 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
438 /*****************************************************************************
439 * CoSendRequest: send request to interface thread
440 *****************************************************************************
441 * Returns 0 on success, 1 otherwise
442 *****************************************************************************/
443 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
447 VLCVout * o_vlv = [[VLCVout alloc] init];
449 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
451 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
459 /*****************************************************************************
460 * CoCreateWindow: create new window
461 *****************************************************************************
462 * Returns 0 on success, 1 otherwise
463 *****************************************************************************/
464 static int CoCreateWindow( vout_thread_t *p_vout )
466 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
468 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
475 /*****************************************************************************
476 * CoDestroyWindow: destroy window
477 *****************************************************************************
478 * Returns 0 on success, 1 otherwise
479 *****************************************************************************/
480 static int CoDestroyWindow( vout_thread_t *p_vout )
482 if( !p_vout->p_sys->b_mouse_pointer_visible )
484 VLCHideMouse( p_vout, NO );
487 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
489 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
496 /*****************************************************************************
497 * CoToggleFullscreen: toggle fullscreen
498 *****************************************************************************
499 * Returns 0 on success, 1 otherwise
500 *****************************************************************************/
501 static int CoToggleFullscreen( vout_thread_t *p_vout )
503 QTDestroySequence( p_vout );
505 if( CoDestroyWindow( p_vout ) )
507 msg_Err( p_vout, "unable to destroy window" );
511 p_vout->b_fullscreen = !p_vout->b_fullscreen;
513 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
515 if( CoCreateWindow( p_vout ) )
517 msg_Err( p_vout, "unable to create window" );
521 SetPort( p_vout->p_sys->p_qdport );
522 QTScaleMatrix( p_vout );
524 if( QTCreateSequence( p_vout ) )
526 msg_Err( p_vout, "unable to create sequence" );
533 /*****************************************************************************
534 * VLCHideMouse: if b_hide then hide the cursor
535 *****************************************************************************/
536 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
541 NSWindow *o_window = p_vout->p_sys->o_window;
542 NSView *o_contents = [o_window contentView];
544 s_rect = [o_contents bounds];
545 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
546 ml = [o_contents convertPoint:ml fromView:nil];
547 b_inside = [o_contents mouse: ml inRect: s_rect];
549 if ( b_hide && b_inside )
551 /* only hide if mouse over VLCView */
553 p_vout->p_sys->b_mouse_pointer_visible = 0;
557 if ( ![o_window isKeyWindow] && b_inside )
559 /* be nice for ppl with multi monitors */
560 p_vout->p_sys->b_mouse_moved = NO;
561 p_vout->p_sys->i_time_mouse_last_moved = mdate();
565 p_vout->p_sys->b_mouse_pointer_visible = 1;
567 p_vout->p_sys->b_mouse_moved = NO;
568 p_vout->p_sys->i_time_mouse_last_moved = mdate();
572 /*****************************************************************************
573 * QTScaleMatrix: scale matrix
574 *****************************************************************************/
575 static void QTScaleMatrix( vout_thread_t *p_vout )
578 unsigned int i_width, i_height;
579 Fixed factor_x, factor_y;
580 unsigned int i_offset_x = 0;
581 unsigned int i_offset_y = 0;
583 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
585 i_width = s_rect.right - s_rect.left;
586 i_height = s_rect.bottom - s_rect.top;
588 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
590 int i_adj_width = i_height * p_vout->output.i_aspect /
593 factor_x = FixDiv( Long2Fix( i_adj_width ),
594 Long2Fix( p_vout->output.i_width ) );
595 factor_y = FixDiv( Long2Fix( i_height ),
596 Long2Fix( p_vout->output.i_height ) );
598 i_offset_x = (i_width - i_adj_width) / 2;
602 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
603 p_vout->output.i_aspect;
605 factor_x = FixDiv( Long2Fix( i_width ),
606 Long2Fix( p_vout->output.i_width ) );
607 factor_y = FixDiv( Long2Fix( i_adj_height ),
608 Long2Fix( p_vout->output.i_height ) );
610 i_offset_y = (i_height - i_adj_height) / 2;
613 SetIdentityMatrix( p_vout->p_sys->p_matrix );
615 ScaleMatrix( p_vout->p_sys->p_matrix,
617 Long2Fix(0), Long2Fix(0) );
619 TranslateMatrix( p_vout->p_sys->p_matrix,
620 Long2Fix(i_offset_x),
621 Long2Fix(i_offset_y) );
624 /*****************************************************************************
625 * QTCreateSequence: create a new sequence
626 *****************************************************************************
627 * Returns 0 on success, 1 otherwise
628 *****************************************************************************/
629 static int QTCreateSequence( vout_thread_t *p_vout )
632 ImageDescriptionPtr p_descr;
634 HLock( (Handle)p_vout->p_sys->h_img_descr );
635 p_descr = *p_vout->p_sys->h_img_descr;
637 p_descr->idSize = sizeof(ImageDescription);
638 p_descr->cType = p_vout->p_sys->i_codec;
639 p_descr->version = 1;
640 p_descr->revisionLevel = 0;
641 p_descr->vendor = 'appl';
642 p_descr->width = p_vout->output.i_width;
643 p_descr->height = p_vout->output.i_height;
644 p_descr->hRes = Long2Fix(72);
645 p_descr->vRes = Long2Fix(72);
646 p_descr->spatialQuality = codecLosslessQuality;
647 p_descr->frameCount = 1;
648 p_descr->clutID = -1;
649 p_descr->dataSize = 0;
652 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
654 if( ( err = DecompressSequenceBeginS(
655 &p_vout->p_sys->i_seq,
656 p_vout->p_sys->h_img_descr,
658 p_vout->p_sys->p_qdport,
660 p_vout->p_sys->p_matrix,
662 codecFlagUseImageBuffer,
663 codecLosslessQuality,
664 p_vout->p_sys->img_dc ) ) )
666 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
673 /*****************************************************************************
674 * QTDestroySequence: destroy sequence
675 *****************************************************************************/
676 static void QTDestroySequence( vout_thread_t *p_vout )
678 CDSequenceEnd( p_vout->p_sys->i_seq );
681 /*****************************************************************************
682 * QTNewPicture: allocate a picture
683 *****************************************************************************
684 * Returns 0 on success, 1 otherwise
685 *****************************************************************************/
686 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
688 int i_width = p_vout->output.i_width;
689 int i_height = p_vout->output.i_height;
691 /* We know the chroma, allocate a buffer which will be used
692 * directly by the decoder */
693 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
695 if( p_pic->p_sys == NULL )
700 switch( p_vout->output.i_chroma )
702 case VLC_FOURCC('I','4','2','0'):
704 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
705 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
707 /* Allocate the memory buffer */
708 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
709 16, i_width * i_height * 3 / 2 );
712 p_pic->Y_PIXELS = p_pic->p_data;
713 p_pic->p[Y_PLANE].i_lines = i_height;
714 p_pic->p[Y_PLANE].i_pitch = i_width;
715 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
716 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
719 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
720 p_pic->p[U_PLANE].i_lines = i_height / 2;
721 p_pic->p[U_PLANE].i_pitch = i_width / 2;
722 p_pic->p[U_PLANE].i_pixel_pitch = 1;
723 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
726 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
727 p_pic->p[V_PLANE].i_lines = i_height / 2;
728 p_pic->p[V_PLANE].i_pitch = i_width / 2;
729 p_pic->p[V_PLANE].i_pixel_pitch = 1;
730 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
732 /* We allocated 3 planes */
735 #define P p_pic->p_sys->pixmap_i420
736 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
737 - p_pic->p_sys->p_info;
738 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
739 - p_pic->p_sys->p_info;
740 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
741 - p_pic->p_sys->p_info;
743 P.componentInfoY.rowBytes = i_width;
744 P.componentInfoCb.rowBytes = i_width / 2;
745 P.componentInfoCr.rowBytes = i_width / 2;
751 /* Unknown chroma, tell the guy to get lost */
752 free( p_pic->p_sys );
753 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
754 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
762 /*****************************************************************************
763 * QTFreePicture: destroy a picture allocated with QTNewPicture
764 *****************************************************************************/
765 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
767 switch( p_vout->output.i_chroma )
769 case VLC_FOURCC('I','4','2','0'):
770 free( p_pic->p_data_orig );
774 free( p_pic->p_sys );
777 /*****************************************************************************
778 * VLCWindow implementation
779 *****************************************************************************/
780 @implementation VLCWindow
782 - (void)setVout:(vout_thread_t *)_p_vout
787 - (vout_thread_t *)getVout
792 - (void)scaleWindowWithFactor: (float)factor
795 int i_corrected_height, i_corrected_width;
797 NSPoint topleftscreen;
799 if ( !p_vout->b_fullscreen )
802 topleftbase.y = [self frame].size.height;
803 topleftscreen = [self convertBaseToScreen: topleftbase];
805 if( p_vout->output.i_height * p_vout->output.i_aspect >
806 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
808 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
810 newsize.width = (int) ( i_corrected_width * factor );
811 newsize.height = (int) ( p_vout->render.i_height * factor );
815 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
816 p_vout->output.i_aspect;
817 newsize.width = (int) ( p_vout->render.i_width * factor );
818 newsize.height = (int) ( i_corrected_height * factor );
821 [self setContentSize: newsize];
823 [self setFrameTopLeftPoint: topleftscreen];
824 p_vout->i_changes |= VOUT_SIZE_CHANGE;
828 - (void)toggleFullscreen
830 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
835 return( p_vout->b_fullscreen );
838 - (BOOL)canBecomeKeyWindow
843 - (void)keyDown:(NSEvent *)o_event
847 if( [[o_event characters] length] )
849 key = [[o_event characters] characterAtIndex: 0];
855 [self toggleFullscreen];
858 case (unichar)0x1b: /* escape */
859 if( [self isFullscreen] )
861 [self toggleFullscreen];
866 p_vout->p_vlc->b_die = VLC_TRUE;
870 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
874 [super keyDown: o_event];
881 NSMutableString * o_title;
882 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
885 if( p_playlist == NULL )
890 vlc_mutex_lock( &p_playlist->object_lock );
891 o_title = [NSMutableString stringWithUTF8String:
892 p_playlist->pp_items[p_playlist->i_index]->psz_name];
893 vlc_mutex_unlock( &p_playlist->object_lock );
895 vlc_object_release( p_playlist );
899 NSRange prefix_range = [o_title rangeOfString: @"file:"];
900 if( prefix_range.location != NSNotFound )
902 [o_title deleteCharactersInRange: prefix_range];
905 [self setTitleWithRepresentedFilename: o_title];
910 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
914 /* This is actually the same as VLCControls::stop. */
915 - (BOOL)windowShouldClose:(id)sender
917 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
919 if( p_playlist == NULL )
924 playlist_Stop( p_playlist );
925 vlc_object_release( p_playlist );
927 /* The window will be closed by the intf later. */
933 /*****************************************************************************
934 * VLCView implementation
935 *****************************************************************************/
936 @implementation VLCView
938 - (void)drawRect:(NSRect)rect
940 vout_thread_t * p_vout;
941 id o_window = [self window];
942 p_vout = (vout_thread_t *)[o_window getVout];
944 [[NSColor blackColor] set];
946 [super drawRect: rect];
948 p_vout->i_changes |= VOUT_SIZE_CHANGE;
951 - (BOOL)acceptsFirstResponder
956 - (BOOL)becomeFirstResponder
958 vout_thread_t * p_vout;
959 id o_window = [self window];
960 p_vout = (vout_thread_t *)[o_window getVout];
962 [o_window setAcceptsMouseMovedEvents: YES];
966 - (BOOL)resignFirstResponder
968 vout_thread_t * p_vout;
969 id o_window = [self window];
970 p_vout = (vout_thread_t *)[o_window getVout];
972 [o_window setAcceptsMouseMovedEvents: NO];
973 VLCHideMouse( p_vout, NO );
977 - (void)mouseDown:(NSEvent *)o_event
979 vout_thread_t * p_vout;
980 id o_window = [self window];
981 p_vout = (vout_thread_t *)[o_window getVout];
984 switch( [o_event type] )
986 case NSLeftMouseDown:
988 var_Get( p_vout, "mouse-button-down", &val );
990 var_Set( p_vout, "mouse-button-down", val );
995 [super mouseDown: o_event];
1000 - (void)otherMouseDown:(NSEvent *)o_event
1002 /* This is not the the wheel button. you need to poll the
1003 * mouseWheel event for that. other is a third, forth or fifth button */
1004 vout_thread_t * p_vout;
1005 id o_window = [self window];
1006 p_vout = (vout_thread_t *)[o_window getVout];
1009 switch( [o_event type] )
1011 case NSOtherMouseDown:
1013 var_Get( p_vout, "mouse-button-down", &val );
1015 var_Set( p_vout, "mouse-button-down", val );
1020 [super mouseDown: o_event];
1025 - (void)rightMouseDown:(NSEvent *)o_event
1027 vout_thread_t * p_vout;
1028 id o_window = [self window];
1029 p_vout = (vout_thread_t *)[o_window getVout];
1032 switch( [o_event type] )
1034 case NSRightMouseDown:
1036 var_Get( p_vout, "mouse-button-down", &val );
1038 var_Set( p_vout, "mouse-button-down", val );
1043 [super mouseDown: o_event];
1048 - (void)mouseUp:(NSEvent *)o_event
1050 vout_thread_t * p_vout;
1051 id o_window = [self window];
1052 p_vout = (vout_thread_t *)[o_window getVout];
1055 switch( [o_event type] )
1060 b_val.b_bool = VLC_TRUE;
1061 var_Set( p_vout, "mouse-clicked", b_val );
1063 var_Get( p_vout, "mouse-button-down", &val );
1065 var_Set( p_vout, "mouse-button-down", val );
1070 [super mouseUp: o_event];
1075 - (void)otherMouseUp:(NSEvent *)o_event
1077 vout_thread_t * p_vout;
1078 id o_window = [self window];
1079 p_vout = (vout_thread_t *)[o_window getVout];
1082 switch( [o_event type] )
1084 case NSOtherMouseUp:
1086 var_Get( p_vout, "mouse-button-down", &val );
1088 var_Set( p_vout, "mouse-button-down", val );
1093 [super mouseUp: o_event];
1098 - (void)rightMouseUp:(NSEvent *)o_event
1100 vout_thread_t * p_vout;
1101 id o_window = [self window];
1102 p_vout = (vout_thread_t *)[o_window getVout];
1105 switch( [o_event type] )
1107 case NSRightMouseUp:
1109 var_Get( p_vout, "mouse-button-down", &val );
1111 var_Set( p_vout, "mouse-button-down", val );
1116 [super mouseUp: o_event];
1121 - (void)mouseDragged:(NSEvent *)o_event
1123 [self mouseMoved:o_event];
1126 - (void)otherMouseDragged:(NSEvent *)o_event
1128 [self mouseMoved:o_event];
1131 - (void)rightMouseDragged:(NSEvent *)o_event
1133 [self mouseMoved:o_event];
1136 - (void)mouseMoved:(NSEvent *)o_event
1142 vout_thread_t * p_vout;
1143 id o_window = [self window];
1144 p_vout = (vout_thread_t *)[o_window getVout];
1146 s_rect = [self bounds];
1147 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1148 b_inside = [self mouse: ml inRect: s_rect];
1153 int i_width, i_height, i_x, i_y;
1155 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1156 (unsigned int)s_rect.size.height,
1157 &i_x, &i_y, &i_width, &i_height );
1159 val.i_int = ( ((int)ml.x) - i_x ) *
1160 p_vout->render.i_width / i_width;
1161 var_Set( p_vout, "mouse-x", val );
1163 val.i_int = ( ((int)ml.y) - i_y ) *
1164 p_vout->render.i_height / i_height;
1165 var_Set( p_vout, "mouse-y", val );
1167 val.b_bool = VLC_TRUE;
1168 var_Set( p_vout, "mouse-moved", val );
1169 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1170 p_vout->p_sys->b_mouse_moved = YES;
1172 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1174 /* people with multiple monitors need their mouse,
1175 * even if VLCView in fullscreen. */
1176 VLCHideMouse( p_vout, NO );
1179 [super mouseMoved: o_event];
1184 /*****************************************************************************
1185 * VLCVout implementation
1186 *****************************************************************************/
1187 @implementation VLCVout
1189 - (void)createWindow:(NSValue *)o_value
1193 NSScreen * o_screen;
1194 vout_thread_t * p_vout;
1195 vlc_bool_t b_main_screen;
1197 p_vout = (vout_thread_t *)[o_value pointerValue];
1199 p_vout->p_sys->o_window = [VLCWindow alloc];
1200 [p_vout->p_sys->o_window setVout: p_vout];
1201 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1203 if( var_Get( p_vout, "video-device", &val ) < 0 )
1205 o_screen = [NSScreen mainScreen];
1210 unsigned int i_index = 0;
1211 NSArray *o_screens = [NSScreen screens];
1213 if( !sscanf( val.psz_string, _("Screen %d"), &i_index ) ||
1214 [o_screens count] < i_index )
1216 o_screen = [NSScreen mainScreen];
1222 o_screen = [o_screens objectAtIndex: i_index];
1223 config_PutInt( p_vout, "macosx-vdev", i_index );
1224 b_main_screen = (i_index == 0);
1227 free( val.psz_string );
1230 if( p_vout->b_fullscreen )
1232 NSRect screen_rect = [o_screen frame];
1233 screen_rect.origin.x = screen_rect.origin.y = 0;
1235 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1236 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1237 NULL, NULL, fullScreenAllowEvents );
1239 [p_vout->p_sys->o_window
1240 initWithContentRect: screen_rect
1241 styleMask: NSBorderlessWindowMask
1242 backing: NSBackingStoreBuffered
1243 defer: NO screen: o_screen];
1245 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1246 p_vout->p_sys->b_mouse_moved = 1;
1247 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1251 unsigned int i_stylemask = NSTitledWindowMask |
1252 NSMiniaturizableWindowMask |
1253 NSClosableWindowMask |
1254 NSResizableWindowMask;
1256 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1257 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1258 p_vout->p_sys->p_fullscreen_state = NULL;
1260 [p_vout->p_sys->o_window
1261 initWithContentRect: p_vout->p_sys->s_rect
1262 styleMask: i_stylemask
1263 backing: NSBackingStoreBuffered
1264 defer: NO screen: o_screen];
1266 if( !p_vout->p_sys->b_pos_saved )
1268 [p_vout->p_sys->o_window center];
1272 o_view = [[VLCView alloc] init];
1273 /* FIXME: [o_view setMenu:] */
1274 [p_vout->p_sys->o_window setContentView: o_view];
1275 [o_view autorelease];
1278 p_vout->p_sys->p_qdport = [o_view qdPort];
1279 [o_view unlockFocus];
1281 [p_vout->p_sys->o_window updateTitle];
1282 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1285 - (void)destroyWindow:(NSValue *)o_value
1287 vout_thread_t * p_vout;
1289 p_vout = (vout_thread_t *)[o_value pointerValue];
1291 if( !p_vout->b_fullscreen )
1295 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1296 p_vout->p_sys->s_rect.size = s_rect.size;
1298 s_rect = [p_vout->p_sys->o_window frame];
1299 p_vout->p_sys->s_rect.origin = s_rect.origin;
1301 p_vout->p_sys->b_pos_saved = 1;
1304 p_vout->p_sys->p_qdport = nil;
1305 [p_vout->p_sys->o_window close];
1306 p_vout->p_sys->o_window = nil;