1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.47 2003/05/05 22:04:11 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 /* no MacOS X intf, unable to communicate with MT */
104 msg_Err( p_vout, "no MacOS X interface 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 )
184 vlc_value_t val, text;
187 int i_option = config_GetInt( p_vout, "macosx-vdev" );
189 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
191 text.psz_string = _("Video device");
192 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
194 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
196 while( (o_screen = [o_enumerator nextObject]) != NULL )
199 NSRect s_rect = [o_screen frame];
201 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
202 "%s %d (%dx%d)", _("Screen"), i,
203 (int)s_rect.size.width, (int)s_rect.size.height );
205 text.psz_string = psz_temp;
207 var_Change( p_vout, "video-device",
208 VLC_VAR_ADDCHOICE, &val, &text );
210 if( ( i - 1 ) == i_option )
212 var_Set( p_vout, "video-device", val );
217 var_AddCallback( p_vout, "video-device", vout_VarCallback,
220 val.b_bool = VLC_TRUE;
221 var_Set( p_vout, "intf-change", val );
225 if( CoCreateWindow( p_vout ) )
227 msg_Err( p_vout, "unable to create window" );
228 free( p_vout->p_sys->p_matrix );
229 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
230 free( p_vout->p_sys );
234 p_vout->pf_init = vout_Init;
235 p_vout->pf_end = vout_End;
236 p_vout->pf_manage = vout_Manage;
237 p_vout->pf_render = NULL;
238 p_vout->pf_display = vout_Display;
243 /*****************************************************************************
244 * vout_Init: initialize video thread output method
245 *****************************************************************************/
246 static int vout_Init( vout_thread_t *p_vout )
251 I_OUTPUTPICTURES = 0;
253 /* Initialize the output structure; we already found a codec,
254 * and the corresponding chroma we will be using. Since we can
255 * arbitrary scale, stick to the coordinates and aspect. */
256 p_vout->output.i_width = p_vout->render.i_width;
257 p_vout->output.i_height = p_vout->render.i_height;
258 p_vout->output.i_aspect = p_vout->render.i_aspect;
260 SetPort( p_vout->p_sys->p_qdport );
261 QTScaleMatrix( p_vout );
263 if( QTCreateSequence( p_vout ) )
265 msg_Err( p_vout, "unable to create sequence" );
269 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
270 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
274 /* Find an empty picture slot */
275 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
277 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
279 p_pic = p_vout->p_picture + i_index;
284 /* Allocate the picture */
285 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
290 p_pic->i_status = DESTROYED_PICTURE;
291 p_pic->i_type = DIRECT_PICTURE;
293 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
301 /*****************************************************************************
302 * vout_End: terminate video thread output method
303 *****************************************************************************/
304 static void vout_End( vout_thread_t *p_vout )
308 QTDestroySequence( p_vout );
310 /* Free the direct buffers we allocated */
311 for( i_index = I_OUTPUTPICTURES; i_index; )
314 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
318 /*****************************************************************************
319 * CloseVideo: destroy video thread output method
320 *****************************************************************************/
321 void E_(CloseVideo) ( vlc_object_t *p_this )
323 vout_thread_t * p_vout = (vout_thread_t *)p_this;
325 if( CoDestroyWindow( p_vout ) )
327 msg_Err( p_vout, "unable to destroy window" );
330 if ( p_vout->p_sys->p_fullscreen_state != NULL )
331 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
335 free( p_vout->p_sys->p_matrix );
336 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
338 free( p_vout->p_sys );
341 /*****************************************************************************
342 * vout_Manage: handle events
343 *****************************************************************************
344 * This function should be called regularly by video output thread. It manages
345 * console events. It returns a non null value on error.
346 *****************************************************************************/
347 static int vout_Manage( vout_thread_t *p_vout )
349 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
351 if( CoToggleFullscreen( p_vout ) )
356 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
359 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
361 QTScaleMatrix( p_vout );
362 SetDSequenceMatrix( p_vout->p_sys->i_seq,
363 p_vout->p_sys->p_matrix );
365 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
368 /* hide/show mouse cursor
369 * this code looks unnecessarily complicated, but is necessary like this.
370 * it has to deal with multiple monitors and therefore checks a lot */
371 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
373 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
374 p_vout->p_sys->b_mouse_pointer_visible )
376 VLCHideMouse( p_vout, YES );
378 else if ( !p_vout->p_sys->b_mouse_pointer_visible )
380 vlc_bool_t b_playing = NO;
381 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
384 if ( p_playlist != nil )
386 vlc_mutex_lock( &p_playlist->object_lock );
387 if( p_playlist->p_input != NULL )
389 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
390 b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
391 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
393 vlc_mutex_unlock( &p_playlist->object_lock );
394 vlc_object_release( p_playlist );
398 VLCHideMouse( p_vout, NO );
402 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
404 if( !p_vout->p_sys->b_mouse_pointer_visible )
406 VLCHideMouse( p_vout, NO );
410 p_vout->p_sys->b_mouse_moved = NO;
417 /*****************************************************************************
418 * vout_Display: displays previously rendered output
419 *****************************************************************************
420 * This function sends the currently rendered image to the display.
421 *****************************************************************************/
422 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
427 if( ( err = DecompressSequenceFrameS(
428 p_vout->p_sys->i_seq,
429 p_pic->p_sys->p_info,
430 p_pic->p_sys->i_size,
431 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
433 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
437 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
441 /*****************************************************************************
442 * CoSendRequest: send request to interface thread
443 *****************************************************************************
444 * Returns 0 on success, 1 otherwise
445 *****************************************************************************/
446 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
450 VLCVout * o_vlv = [[VLCVout alloc] init];
452 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
454 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
462 /*****************************************************************************
463 * CoCreateWindow: create new window
464 *****************************************************************************
465 * Returns 0 on success, 1 otherwise
466 *****************************************************************************/
467 static int CoCreateWindow( vout_thread_t *p_vout )
469 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
471 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
478 /*****************************************************************************
479 * CoDestroyWindow: destroy window
480 *****************************************************************************
481 * Returns 0 on success, 1 otherwise
482 *****************************************************************************/
483 static int CoDestroyWindow( vout_thread_t *p_vout )
485 if( !p_vout->p_sys->b_mouse_pointer_visible )
487 VLCHideMouse( p_vout, NO );
490 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
492 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
499 /*****************************************************************************
500 * CoToggleFullscreen: toggle fullscreen
501 *****************************************************************************
502 * Returns 0 on success, 1 otherwise
503 *****************************************************************************/
504 static int CoToggleFullscreen( vout_thread_t *p_vout )
506 QTDestroySequence( p_vout );
508 if( CoDestroyWindow( p_vout ) )
510 msg_Err( p_vout, "unable to destroy window" );
514 p_vout->b_fullscreen = !p_vout->b_fullscreen;
516 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
518 if( CoCreateWindow( p_vout ) )
520 msg_Err( p_vout, "unable to create window" );
524 SetPort( p_vout->p_sys->p_qdport );
525 QTScaleMatrix( p_vout );
527 if( QTCreateSequence( p_vout ) )
529 msg_Err( p_vout, "unable to create sequence" );
536 /*****************************************************************************
537 * VLCHideMouse: if b_hide then hide the cursor
538 *****************************************************************************/
539 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
544 NSWindow *o_window = p_vout->p_sys->o_window;
545 NSView *o_contents = [o_window contentView];
547 s_rect = [o_contents bounds];
548 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
549 ml = [o_contents convertPoint:ml fromView:nil];
550 b_inside = [o_contents mouse: ml inRect: s_rect];
552 if ( b_hide && b_inside )
554 /* only hide if mouse over VLCView */
556 p_vout->p_sys->b_mouse_pointer_visible = 0;
561 p_vout->p_sys->b_mouse_pointer_visible = 1;
563 p_vout->p_sys->b_mouse_moved = NO;
564 p_vout->p_sys->i_time_mouse_last_moved = mdate();
568 /*****************************************************************************
569 * QTScaleMatrix: scale matrix
570 *****************************************************************************/
571 static void QTScaleMatrix( vout_thread_t *p_vout )
574 unsigned int i_width, i_height;
575 Fixed factor_x, factor_y;
576 unsigned int i_offset_x = 0;
577 unsigned int i_offset_y = 0;
579 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
581 i_width = s_rect.right - s_rect.left;
582 i_height = s_rect.bottom - s_rect.top;
584 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
586 int i_adj_width = i_height * p_vout->output.i_aspect /
589 factor_x = FixDiv( Long2Fix( i_adj_width ),
590 Long2Fix( p_vout->output.i_width ) );
591 factor_y = FixDiv( Long2Fix( i_height ),
592 Long2Fix( p_vout->output.i_height ) );
594 i_offset_x = (i_width - i_adj_width) / 2;
598 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
599 p_vout->output.i_aspect;
601 factor_x = FixDiv( Long2Fix( i_width ),
602 Long2Fix( p_vout->output.i_width ) );
603 factor_y = FixDiv( Long2Fix( i_adj_height ),
604 Long2Fix( p_vout->output.i_height ) );
606 i_offset_y = (i_height - i_adj_height) / 2;
609 SetIdentityMatrix( p_vout->p_sys->p_matrix );
611 ScaleMatrix( p_vout->p_sys->p_matrix,
613 Long2Fix(0), Long2Fix(0) );
615 TranslateMatrix( p_vout->p_sys->p_matrix,
616 Long2Fix(i_offset_x),
617 Long2Fix(i_offset_y) );
620 /*****************************************************************************
621 * QTCreateSequence: create a new sequence
622 *****************************************************************************
623 * Returns 0 on success, 1 otherwise
624 *****************************************************************************/
625 static int QTCreateSequence( vout_thread_t *p_vout )
628 ImageDescriptionPtr p_descr;
630 HLock( (Handle)p_vout->p_sys->h_img_descr );
631 p_descr = *p_vout->p_sys->h_img_descr;
633 p_descr->idSize = sizeof(ImageDescription);
634 p_descr->cType = p_vout->p_sys->i_codec;
635 p_descr->version = 1;
636 p_descr->revisionLevel = 0;
637 p_descr->vendor = 'appl';
638 p_descr->width = p_vout->output.i_width;
639 p_descr->height = p_vout->output.i_height;
640 p_descr->hRes = Long2Fix(72);
641 p_descr->vRes = Long2Fix(72);
642 p_descr->spatialQuality = codecLosslessQuality;
643 p_descr->frameCount = 1;
644 p_descr->clutID = -1;
645 p_descr->dataSize = 0;
648 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
650 if( ( err = DecompressSequenceBeginS(
651 &p_vout->p_sys->i_seq,
652 p_vout->p_sys->h_img_descr,
654 p_vout->p_sys->p_qdport,
656 p_vout->p_sys->p_matrix,
658 codecFlagUseImageBuffer,
659 codecLosslessQuality,
660 p_vout->p_sys->img_dc ) ) )
662 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
669 /*****************************************************************************
670 * QTDestroySequence: destroy sequence
671 *****************************************************************************/
672 static void QTDestroySequence( vout_thread_t *p_vout )
674 CDSequenceEnd( p_vout->p_sys->i_seq );
677 /*****************************************************************************
678 * QTNewPicture: allocate a picture
679 *****************************************************************************
680 * Returns 0 on success, 1 otherwise
681 *****************************************************************************/
682 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
684 int i_width = p_vout->output.i_width;
685 int i_height = p_vout->output.i_height;
687 /* We know the chroma, allocate a buffer which will be used
688 * directly by the decoder */
689 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
691 if( p_pic->p_sys == NULL )
696 switch( p_vout->output.i_chroma )
698 case VLC_FOURCC('I','4','2','0'):
700 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
701 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
703 /* Allocate the memory buffer */
704 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
705 16, i_width * i_height * 3 / 2 );
708 p_pic->Y_PIXELS = p_pic->p_data;
709 p_pic->p[Y_PLANE].i_lines = i_height;
710 p_pic->p[Y_PLANE].i_pitch = i_width;
711 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
712 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
715 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
716 p_pic->p[U_PLANE].i_lines = i_height / 2;
717 p_pic->p[U_PLANE].i_pitch = i_width / 2;
718 p_pic->p[U_PLANE].i_pixel_pitch = 1;
719 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
722 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
723 p_pic->p[V_PLANE].i_lines = i_height / 2;
724 p_pic->p[V_PLANE].i_pitch = i_width / 2;
725 p_pic->p[V_PLANE].i_pixel_pitch = 1;
726 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
728 /* We allocated 3 planes */
731 #define P p_pic->p_sys->pixmap_i420
732 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
733 - p_pic->p_sys->p_info;
734 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
735 - p_pic->p_sys->p_info;
736 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
737 - p_pic->p_sys->p_info;
739 P.componentInfoY.rowBytes = i_width;
740 P.componentInfoCb.rowBytes = i_width / 2;
741 P.componentInfoCr.rowBytes = i_width / 2;
747 /* Unknown chroma, tell the guy to get lost */
748 free( p_pic->p_sys );
749 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
750 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
758 /*****************************************************************************
759 * QTFreePicture: destroy a picture allocated with QTNewPicture
760 *****************************************************************************/
761 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
763 switch( p_vout->output.i_chroma )
765 case VLC_FOURCC('I','4','2','0'):
766 free( p_pic->p_data_orig );
770 free( p_pic->p_sys );
773 /*****************************************************************************
774 * VLCWindow implementation
775 *****************************************************************************/
776 @implementation VLCWindow
778 - (void)setVout:(vout_thread_t *)_p_vout
783 - (vout_thread_t *)getVout
788 - (void)scaleWindowWithFactor: (float)factor
791 int i_corrected_height, i_corrected_width;
793 NSPoint topleftscreen;
795 if ( !p_vout->b_fullscreen )
798 topleftbase.y = [self frame].size.height;
799 topleftscreen = [self convertBaseToScreen: topleftbase];
801 if( p_vout->output.i_height * p_vout->output.i_aspect >
802 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
804 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
806 newsize.width = (int) ( i_corrected_width * factor );
807 newsize.height = (int) ( p_vout->render.i_height * factor );
811 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
812 p_vout->output.i_aspect;
813 newsize.width = (int) ( p_vout->render.i_width * factor );
814 newsize.height = (int) ( i_corrected_height * factor );
817 [self setContentSize: newsize];
819 [self setFrameTopLeftPoint: topleftscreen];
820 p_vout->i_changes |= VOUT_SIZE_CHANGE;
824 - (void)toggleFloatOnTop
826 if( config_GetInt( p_vout, "macosx-float" ) )
828 config_PutInt( p_vout, "macosx-float", 0 );
829 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
833 config_PutInt( p_vout, "macosx-float", 1 );
834 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
838 - (void)toggleFullscreen
840 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
845 return( p_vout->b_fullscreen );
848 - (BOOL)canBecomeKeyWindow
853 - (void)keyDown:(NSEvent *)o_event
857 if( [[o_event characters] length] )
859 key = [[o_event characters] characterAtIndex: 0];
865 [self toggleFullscreen];
868 case (unichar)0x1b: /* escape */
869 if( [self isFullscreen] )
871 [self toggleFullscreen];
876 p_vout->p_vlc->b_die = VLC_TRUE;
880 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
884 [super keyDown: o_event];
891 NSMutableString * o_title;
892 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
895 if( p_playlist == NULL )
900 vlc_mutex_lock( &p_playlist->object_lock );
901 o_title = [NSMutableString stringWithUTF8String:
902 p_playlist->pp_items[p_playlist->i_index]->psz_name];
903 vlc_mutex_unlock( &p_playlist->object_lock );
905 vlc_object_release( p_playlist );
909 NSRange prefix_range = [o_title rangeOfString: @"file:"];
910 if( prefix_range.location != NSNotFound )
912 [o_title deleteCharactersInRange: prefix_range];
915 [self setTitleWithRepresentedFilename: o_title];
920 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
924 /* This is actually the same as VLCControls::stop. */
925 - (BOOL)windowShouldClose:(id)sender
927 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
929 if( p_playlist == NULL )
934 playlist_Stop( p_playlist );
935 vlc_object_release( p_playlist );
937 /* The window will be closed by the intf later. */
943 /*****************************************************************************
944 * VLCView implementation
945 *****************************************************************************/
946 @implementation VLCView
948 - (void)drawRect:(NSRect)rect
950 vout_thread_t * p_vout;
951 id o_window = [self window];
952 p_vout = (vout_thread_t *)[o_window getVout];
954 [[NSColor blackColor] set];
956 [super drawRect: rect];
958 p_vout->i_changes |= VOUT_SIZE_CHANGE;
961 - (BOOL)acceptsFirstResponder
966 - (BOOL)becomeFirstResponder
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: YES];
976 - (BOOL)resignFirstResponder
978 vout_thread_t * p_vout;
979 id o_window = [self window];
980 p_vout = (vout_thread_t *)[o_window getVout];
982 [o_window setAcceptsMouseMovedEvents: NO];
983 VLCHideMouse( p_vout, NO );
987 - (void)mouseDown:(NSEvent *)o_event
989 vout_thread_t * p_vout;
990 id o_window = [self window];
991 p_vout = (vout_thread_t *)[o_window getVout];
994 switch( [o_event type] )
996 case NSLeftMouseDown:
998 var_Get( p_vout, "mouse-button-down", &val );
1000 var_Set( p_vout, "mouse-button-down", val );
1005 [super mouseDown: o_event];
1010 - (void)otherMouseDown:(NSEvent *)o_event
1012 /* This is not the the wheel button. you need to poll the
1013 * mouseWheel event for that. other is a third, forth or fifth button */
1014 vout_thread_t * p_vout;
1015 id o_window = [self window];
1016 p_vout = (vout_thread_t *)[o_window getVout];
1019 switch( [o_event type] )
1021 case NSOtherMouseDown:
1023 var_Get( p_vout, "mouse-button-down", &val );
1025 var_Set( p_vout, "mouse-button-down", val );
1030 [super mouseDown: o_event];
1035 - (void)rightMouseDown:(NSEvent *)o_event
1037 vout_thread_t * p_vout;
1038 id o_window = [self window];
1039 p_vout = (vout_thread_t *)[o_window getVout];
1042 switch( [o_event type] )
1044 case NSRightMouseDown:
1046 var_Get( p_vout, "mouse-button-down", &val );
1048 var_Set( p_vout, "mouse-button-down", val );
1053 [super mouseDown: o_event];
1058 - (void)mouseUp:(NSEvent *)o_event
1060 vout_thread_t * p_vout;
1061 id o_window = [self window];
1062 p_vout = (vout_thread_t *)[o_window getVout];
1065 switch( [o_event type] )
1070 b_val.b_bool = VLC_TRUE;
1071 var_Set( p_vout, "mouse-clicked", b_val );
1073 var_Get( p_vout, "mouse-button-down", &val );
1075 var_Set( p_vout, "mouse-button-down", val );
1080 [super mouseUp: o_event];
1085 - (void)otherMouseUp:(NSEvent *)o_event
1087 vout_thread_t * p_vout;
1088 id o_window = [self window];
1089 p_vout = (vout_thread_t *)[o_window getVout];
1092 switch( [o_event type] )
1094 case NSOtherMouseUp:
1096 var_Get( p_vout, "mouse-button-down", &val );
1098 var_Set( p_vout, "mouse-button-down", val );
1103 [super mouseUp: o_event];
1108 - (void)rightMouseUp:(NSEvent *)o_event
1110 vout_thread_t * p_vout;
1111 id o_window = [self window];
1112 p_vout = (vout_thread_t *)[o_window getVout];
1115 switch( [o_event type] )
1117 case NSRightMouseUp:
1119 var_Get( p_vout, "mouse-button-down", &val );
1121 var_Set( p_vout, "mouse-button-down", val );
1126 [super mouseUp: o_event];
1131 - (void)mouseDragged:(NSEvent *)o_event
1133 [self mouseMoved:o_event];
1136 - (void)otherMouseDragged:(NSEvent *)o_event
1138 [self mouseMoved:o_event];
1141 - (void)rightMouseDragged:(NSEvent *)o_event
1143 [self mouseMoved:o_event];
1146 - (void)mouseMoved:(NSEvent *)o_event
1152 vout_thread_t * p_vout;
1153 id o_window = [self window];
1154 p_vout = (vout_thread_t *)[o_window getVout];
1156 s_rect = [self bounds];
1157 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1158 b_inside = [self mouse: ml inRect: s_rect];
1163 int i_width, i_height, i_x, i_y;
1165 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1166 (unsigned int)s_rect.size.height,
1167 &i_x, &i_y, &i_width, &i_height );
1169 val.i_int = ( ((int)ml.x) - i_x ) *
1170 p_vout->render.i_width / i_width;
1171 var_Set( p_vout, "mouse-x", val );
1173 val.i_int = ( ((int)ml.y) - i_y ) *
1174 p_vout->render.i_height / i_height;
1175 var_Set( p_vout, "mouse-y", val );
1177 val.b_bool = VLC_TRUE;
1178 var_Set( p_vout, "mouse-moved", val );
1179 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1180 p_vout->p_sys->b_mouse_moved = YES;
1182 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1184 /* people with multiple monitors need their mouse,
1185 * even if VLCView in fullscreen. */
1186 VLCHideMouse( p_vout, NO );
1189 [super mouseMoved: o_event];
1194 /*****************************************************************************
1195 * VLCVout implementation
1196 *****************************************************************************/
1197 @implementation VLCVout
1199 - (void)createWindow:(NSValue *)o_value
1203 NSScreen * o_screen;
1204 vout_thread_t * p_vout;
1205 vlc_bool_t b_main_screen;
1207 p_vout = (vout_thread_t *)[o_value pointerValue];
1209 p_vout->p_sys->o_window = [VLCWindow alloc];
1210 [p_vout->p_sys->o_window setVout: p_vout];
1211 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1213 if( var_Get( p_vout, "video-device", &val ) < 0 )
1215 o_screen = [NSScreen mainScreen];
1220 NSArray *o_screens = [NSScreen screens];
1221 unsigned int i_index = val.i_int;
1223 if( [o_screens count] < i_index )
1225 o_screen = [NSScreen mainScreen];
1231 o_screen = [o_screens objectAtIndex: i_index];
1232 config_PutInt( p_vout, "macosx-vdev", i_index );
1233 b_main_screen = (i_index == 0);
1237 if( p_vout->b_fullscreen )
1239 NSRect screen_rect = [o_screen frame];
1240 screen_rect.origin.x = screen_rect.origin.y = 0;
1242 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1243 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1244 NULL, NULL, fullScreenAllowEvents );
1246 [p_vout->p_sys->o_window
1247 initWithContentRect: screen_rect
1248 styleMask: NSBorderlessWindowMask
1249 backing: NSBackingStoreBuffered
1250 defer: NO screen: o_screen];
1252 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1253 p_vout->p_sys->b_mouse_moved = YES;
1254 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1258 unsigned int i_stylemask = NSTitledWindowMask |
1259 NSMiniaturizableWindowMask |
1260 NSClosableWindowMask |
1261 NSResizableWindowMask;
1263 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1264 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1265 p_vout->p_sys->p_fullscreen_state = NULL;
1267 [p_vout->p_sys->o_window
1268 initWithContentRect: p_vout->p_sys->s_rect
1269 styleMask: i_stylemask
1270 backing: NSBackingStoreBuffered
1271 defer: NO screen: o_screen];
1273 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1275 if( config_GetInt( p_vout, "macosx-float" ) )
1277 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1280 if( !p_vout->p_sys->b_pos_saved )
1282 [p_vout->p_sys->o_window center];
1286 o_view = [[VLCView alloc] init];
1287 /* FIXME: [o_view setMenu:] */
1288 [p_vout->p_sys->o_window setContentView: o_view];
1289 [o_view autorelease];
1292 p_vout->p_sys->p_qdport = [o_view qdPort];
1293 [o_view unlockFocus];
1295 [p_vout->p_sys->o_window updateTitle];
1296 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1299 - (void)destroyWindow:(NSValue *)o_value
1301 vout_thread_t * p_vout;
1303 p_vout = (vout_thread_t *)[o_value pointerValue];
1305 if( !p_vout->b_fullscreen )
1309 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1310 p_vout->p_sys->s_rect.size = s_rect.size;
1312 s_rect = [p_vout->p_sys->o_window frame];
1313 p_vout->p_sys->s_rect.origin = s_rect.origin;
1315 p_vout->p_sys->b_pos_saved = YES;
1318 p_vout->p_sys->p_qdport = nil;
1319 [p_vout->p_sys->o_window close];
1320 p_vout->p_sys->o_window = nil;