1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.69 2004/01/25 17:01:57 murray 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>
41 #define QT_MAX_DIRECTBUFFERS 10
42 #define VL_MAX_DISPLAYS 16
49 /* When using I420 output */
50 PlanarPixmapInfoYUV420 pixmap_i420;
53 /*****************************************************************************
55 *****************************************************************************/
57 static int vout_Init ( vout_thread_t * );
58 static void vout_End ( vout_thread_t * );
59 static int vout_Manage ( vout_thread_t * );
60 static void vout_Display ( vout_thread_t *, picture_t * );
62 static int CoSendRequest ( vout_thread_t *, SEL );
63 static int CoCreateWindow ( vout_thread_t * );
64 static int CoDestroyWindow ( vout_thread_t * );
65 static int CoToggleFullscreen ( vout_thread_t * );
67 static void VLCHideMouse ( vout_thread_t *, BOOL );
69 static void QTScaleMatrix ( vout_thread_t * );
70 static int QTCreateSequence ( vout_thread_t * );
71 static void QTDestroySequence ( vout_thread_t * );
72 static int QTNewPicture ( vout_thread_t *, picture_t * );
73 static void QTFreePicture ( vout_thread_t *, picture_t * );
75 /*****************************************************************************
76 * OpenVideo: allocates MacOS X video thread output method
77 *****************************************************************************
78 * This function allocates and initializes a MacOS X vout method.
79 *****************************************************************************/
80 int E_(OpenVideo) ( vlc_object_t *p_this )
82 vout_thread_t * p_vout = (vout_thread_t *)p_this;
86 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
87 if( p_vout->p_sys == NULL )
89 msg_Err( p_vout, "out of memory" );
93 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
95 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
96 for( i_timeout = 20 ; i_timeout-- ; )
100 msleep( INTF_IDLE_SLEEP );
106 /* no MacOS X intf, unable to communicate with MT */
107 msg_Err( p_vout, "no MacOS X interface present" );
108 free( p_vout->p_sys );
112 if( [NSApp respondsToSelector: @selector(getIntf)] )
114 intf_thread_t * p_intf;
116 for( i_timeout = 10 ; i_timeout-- ; )
118 if( ( p_intf = [NSApp getIntf] ) == NULL )
120 msleep( INTF_IDLE_SLEEP );
126 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
127 free( p_vout->p_sys );
132 p_vout->p_sys->h_img_descr =
133 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
134 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
135 p_vout->p_sys->p_fullscreen_state = NULL;
137 p_vout->p_sys->b_mouse_moved = VLC_TRUE;
138 p_vout->p_sys->i_time_mouse_last_moved = mdate();
140 /* set window size */
141 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
142 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
144 if( ( err = EnterMovies() ) != noErr )
146 msg_Err( p_vout, "EnterMovies failed: %d", err );
147 free( p_vout->p_sys->p_matrix );
148 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
149 free( p_vout->p_sys );
153 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
154 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
156 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
157 nil, &p_vout->p_sys->img_dc );
159 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
160 if( err == noErr && p_vout->p_sys->img_dc != 0 )
162 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
163 p_vout->p_sys->i_codec = kYUV420CodecType;
167 msg_Err( p_vout, "failed to find an appropriate codec" );
170 if( p_vout->p_sys->img_dc == 0 )
172 free( p_vout->p_sys->p_matrix );
173 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
174 free( p_vout->p_sys );
178 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
179 NSArray * o_screens = [NSScreen screens];
180 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
183 vlc_value_t val, text;
186 int i_option = config_GetInt( p_vout, "macosx-vdev" );
188 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
190 text.psz_string = _("Video device");
191 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
193 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
195 while( (o_screen = [o_enumerator nextObject]) != NULL )
198 NSRect s_rect = [o_screen frame];
200 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
201 "%s %d (%dx%d)", _("Screen"), i,
202 (int)s_rect.size.width, (int)s_rect.size.height );
204 text.psz_string = psz_temp;
206 var_Change( p_vout, "video-device",
207 VLC_VAR_ADDCHOICE, &val, &text );
209 if( ( i - 1 ) == i_option )
211 var_Set( p_vout, "video-device", val );
216 var_AddCallback( p_vout, "video-device", vout_VarCallback,
219 val.b_bool = VLC_TRUE;
220 var_Set( p_vout, "intf-change", val );
224 if( CoCreateWindow( p_vout ) )
226 msg_Err( p_vout, "unable to create window" );
227 free( p_vout->p_sys->p_matrix );
228 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
229 free( p_vout->p_sys );
233 p_vout->pf_init = vout_Init;
234 p_vout->pf_end = vout_End;
235 p_vout->pf_manage = vout_Manage;
236 p_vout->pf_render = NULL;
237 p_vout->pf_display = vout_Display;
242 /*****************************************************************************
243 * vout_Init: initialize video thread output method
244 *****************************************************************************/
245 static int vout_Init( vout_thread_t *p_vout )
250 I_OUTPUTPICTURES = 0;
252 /* Initialize the output structure; we already found a codec,
253 * and the corresponding chroma we will be using. Since we can
254 * arbitrary scale, stick to the coordinates and aspect. */
255 p_vout->output.i_width = p_vout->render.i_width;
256 p_vout->output.i_height = p_vout->render.i_height;
257 p_vout->output.i_aspect = p_vout->render.i_aspect;
259 SetPort( p_vout->p_sys->p_qdport );
260 QTScaleMatrix( p_vout );
262 if( QTCreateSequence( p_vout ) )
264 msg_Err( p_vout, "unable to create sequence" );
268 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
269 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
273 /* Find an empty picture slot */
274 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
276 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
278 p_pic = p_vout->p_picture + i_index;
283 /* Allocate the picture */
284 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
289 p_pic->i_status = DESTROYED_PICTURE;
290 p_pic->i_type = DIRECT_PICTURE;
292 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
300 /*****************************************************************************
301 * vout_End: terminate video thread output method
302 *****************************************************************************/
303 static void vout_End( vout_thread_t *p_vout )
307 QTDestroySequence( p_vout );
309 /* Free the direct buffers we allocated */
310 for( i_index = I_OUTPUTPICTURES; i_index; )
313 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
317 /*****************************************************************************
318 * CloseVideo: destroy video thread output method
319 *****************************************************************************/
320 void E_(CloseVideo) ( vlc_object_t *p_this )
322 vout_thread_t * p_vout = (vout_thread_t *)p_this;
324 if( CoDestroyWindow( p_vout ) )
326 msg_Err( p_vout, "unable to destroy window" );
329 if ( p_vout->p_sys->p_fullscreen_state != NULL )
330 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
334 free( p_vout->p_sys->p_matrix );
335 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
337 free( p_vout->p_sys );
340 /*****************************************************************************
341 * vout_Manage: handle events
342 *****************************************************************************
343 * This function should be called regularly by video output thread. It manages
344 * console events. It returns a non null value on error.
345 *****************************************************************************/
346 static int vout_Manage( vout_thread_t *p_vout )
348 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
350 if( CoToggleFullscreen( p_vout ) )
355 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
358 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
360 QTScaleMatrix( p_vout );
361 SetDSequenceMatrix( p_vout->p_sys->i_seq,
362 p_vout->p_sys->p_matrix );
364 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
367 /* hide/show mouse cursor
368 * this code looks unnecessarily complicated, but is necessary like this.
369 * it has to deal with multiple monitors and therefore checks a lot */
370 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
372 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 3000000 )
374 VLCHideMouse( p_vout, YES );
377 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
379 VLCHideMouse( p_vout, NO );
382 /* disable screen saver */
383 UpdateSystemActivity( UsrActivity );
388 /*****************************************************************************
389 * vout_Display: displays previously rendered output
390 *****************************************************************************
391 * This function sends the currently rendered image to the display.
392 *****************************************************************************/
393 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
398 if( ( err = DecompressSequenceFrameS(
399 p_vout->p_sys->i_seq,
400 p_pic->p_sys->p_info,
401 p_pic->p_sys->i_size,
402 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
404 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
408 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
412 /*****************************************************************************
413 * CoSendRequest: send request to interface thread
414 *****************************************************************************
415 * Returns 0 on success, 1 otherwise
416 *****************************************************************************/
417 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
421 intf_thread_t * p_intf;
423 VLCVout * o_vlv = [[VLCVout alloc] init];
425 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
427 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
432 /*This makes this function dependant of the presence of a macosx
433 interface. We do not check if this interface exists, since it has
434 already been done before.*/
436 p_intf = [NSApp getIntf];
438 val.b_bool = VLC_TRUE;
439 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
440 var_Set(p_intf, "intf-change",val);
445 /*****************************************************************************
446 * CoCreateWindow: create new window
447 *****************************************************************************
448 * Returns 0 on success, 1 otherwise
449 *****************************************************************************/
450 static int CoCreateWindow( vout_thread_t *p_vout )
452 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
454 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
461 /*****************************************************************************
462 * CoDestroyWindow: destroy window
463 *****************************************************************************
464 * Returns 0 on success, 1 otherwise
465 *****************************************************************************/
466 static int CoDestroyWindow( vout_thread_t *p_vout )
469 VLCHideMouse( p_vout, NO );
471 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
473 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
480 /*****************************************************************************
481 * CoToggleFullscreen: toggle fullscreen
482 *****************************************************************************
483 * Returns 0 on success, 1 otherwise
484 *****************************************************************************/
485 static int CoToggleFullscreen( vout_thread_t *p_vout )
487 QTDestroySequence( p_vout );
489 if( CoDestroyWindow( p_vout ) )
491 msg_Err( p_vout, "unable to destroy window" );
495 p_vout->b_fullscreen = !p_vout->b_fullscreen;
497 if( CoCreateWindow( p_vout ) )
499 msg_Err( p_vout, "unable to create window" );
503 SetPort( p_vout->p_sys->p_qdport );
504 QTScaleMatrix( p_vout );
506 if( QTCreateSequence( p_vout ) )
508 msg_Err( p_vout, "unable to create sequence" );
515 /*****************************************************************************
516 * VLCHideMouse: if b_hide then hide the cursor
517 *****************************************************************************/
518 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
523 NSWindow *o_window = p_vout->p_sys->o_window;
524 NSView *o_contents = [o_window contentView];
526 s_rect = [o_contents bounds];
527 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
528 ml = [o_contents convertPoint:ml fromView:nil];
529 b_inside = [o_contents mouse: ml inRect: s_rect];
531 if ( b_hide && b_inside )
533 /* only hide if mouse over VLCView */
534 [NSCursor setHiddenUntilMouseMoves: YES];
538 [NSCursor setHiddenUntilMouseMoves: NO];
540 p_vout->p_sys->b_mouse_moved = NO;
541 p_vout->p_sys->i_time_mouse_last_moved = mdate();
545 /*****************************************************************************
546 * QTScaleMatrix: scale matrix
547 *****************************************************************************/
548 static void QTScaleMatrix( vout_thread_t *p_vout )
551 unsigned int i_width, i_height;
552 Fixed factor_x, factor_y;
553 unsigned int i_offset_x = 0;
554 unsigned int i_offset_y = 0;
556 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
558 i_width = s_rect.right - s_rect.left;
559 i_height = s_rect.bottom - s_rect.top;
561 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
563 int i_adj_width = i_height * p_vout->output.i_aspect /
566 factor_x = FixDiv( Long2Fix( i_adj_width ),
567 Long2Fix( p_vout->output.i_width ) );
568 factor_y = FixDiv( Long2Fix( i_height ),
569 Long2Fix( p_vout->output.i_height ) );
571 i_offset_x = (i_width - i_adj_width) / 2;
575 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
576 p_vout->output.i_aspect;
578 factor_x = FixDiv( Long2Fix( i_width ),
579 Long2Fix( p_vout->output.i_width ) );
580 factor_y = FixDiv( Long2Fix( i_adj_height ),
581 Long2Fix( p_vout->output.i_height ) );
583 i_offset_y = (i_height - i_adj_height) / 2;
586 SetIdentityMatrix( p_vout->p_sys->p_matrix );
588 ScaleMatrix( p_vout->p_sys->p_matrix,
590 Long2Fix(0), Long2Fix(0) );
592 TranslateMatrix( p_vout->p_sys->p_matrix,
593 Long2Fix(i_offset_x),
594 Long2Fix(i_offset_y) );
598 /*****************************************************************************
599 * QTCreateSequence: create a new sequence
600 *****************************************************************************
601 * Returns 0 on success, 1 otherwise
602 *****************************************************************************/
603 static int QTCreateSequence( vout_thread_t *p_vout )
606 ImageDescriptionPtr p_descr;
608 HLock( (Handle)p_vout->p_sys->h_img_descr );
609 p_descr = *p_vout->p_sys->h_img_descr;
611 p_descr->idSize = sizeof(ImageDescription);
612 p_descr->cType = p_vout->p_sys->i_codec;
613 p_descr->version = 1;
614 p_descr->revisionLevel = 0;
615 p_descr->vendor = 'appl';
616 p_descr->width = p_vout->output.i_width;
617 p_descr->height = p_vout->output.i_height;
618 p_descr->hRes = Long2Fix(72);
619 p_descr->vRes = Long2Fix(72);
620 p_descr->spatialQuality = codecLosslessQuality;
621 p_descr->frameCount = 1;
622 p_descr->clutID = -1;
623 p_descr->dataSize = 0;
626 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
628 if( ( err = DecompressSequenceBeginS(
629 &p_vout->p_sys->i_seq,
630 p_vout->p_sys->h_img_descr,
632 p_vout->p_sys->p_qdport,
634 p_vout->p_sys->p_matrix,
636 codecFlagUseImageBuffer,
637 codecLosslessQuality,
638 p_vout->p_sys->img_dc ) ) )
640 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
647 /*****************************************************************************
648 * QTDestroySequence: destroy sequence
649 *****************************************************************************/
650 static void QTDestroySequence( vout_thread_t *p_vout )
652 CDSequenceEnd( p_vout->p_sys->i_seq );
655 /*****************************************************************************
656 * QTNewPicture: allocate a picture
657 *****************************************************************************
658 * Returns 0 on success, 1 otherwise
659 *****************************************************************************/
660 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
662 int i_width = p_vout->output.i_width;
663 int i_height = p_vout->output.i_height;
665 /* We know the chroma, allocate a buffer which will be used
666 * directly by the decoder */
667 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
669 if( p_pic->p_sys == NULL )
674 switch( p_vout->output.i_chroma )
676 case VLC_FOURCC('I','4','2','0'):
678 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
679 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
681 /* Allocate the memory buffer */
682 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
683 16, i_width * i_height * 3 / 2 );
686 p_pic->Y_PIXELS = p_pic->p_data;
687 p_pic->p[Y_PLANE].i_lines = i_height;
688 p_pic->p[Y_PLANE].i_pitch = i_width;
689 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
690 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
693 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
694 p_pic->p[U_PLANE].i_lines = i_height / 2;
695 p_pic->p[U_PLANE].i_pitch = i_width / 2;
696 p_pic->p[U_PLANE].i_pixel_pitch = 1;
697 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
700 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
701 p_pic->p[V_PLANE].i_lines = i_height / 2;
702 p_pic->p[V_PLANE].i_pitch = i_width / 2;
703 p_pic->p[V_PLANE].i_pixel_pitch = 1;
704 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
706 /* We allocated 3 planes */
709 #define P p_pic->p_sys->pixmap_i420
710 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
711 - p_pic->p_sys->p_info;
712 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
713 - p_pic->p_sys->p_info;
714 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
715 - p_pic->p_sys->p_info;
717 P.componentInfoY.rowBytes = i_width;
718 P.componentInfoCb.rowBytes = i_width / 2;
719 P.componentInfoCr.rowBytes = i_width / 2;
725 /* Unknown chroma, tell the guy to get lost */
726 free( p_pic->p_sys );
727 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
728 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
736 /*****************************************************************************
737 * QTFreePicture: destroy a picture allocated with QTNewPicture
738 *****************************************************************************/
739 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
741 switch( p_vout->output.i_chroma )
743 case VLC_FOURCC('I','4','2','0'):
744 free( p_pic->p_data_orig );
748 free( p_pic->p_sys );
751 /*****************************************************************************
752 * VLCWindow implementation
753 *****************************************************************************/
754 @implementation VLCWindow
756 - (void)setVout:(vout_thread_t *)_p_vout
761 - (vout_thread_t *)getVout
766 - (void)scaleWindowWithFactor: (float)factor
769 int i_corrected_height, i_corrected_width;
771 NSPoint topleftscreen;
773 if ( !p_vout->b_fullscreen )
776 topleftbase.y = [self frame].size.height;
777 topleftscreen = [self convertBaseToScreen: topleftbase];
779 if( p_vout->output.i_height * p_vout->output.i_aspect >
780 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
782 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
784 newsize.width = (int) ( i_corrected_width * factor );
785 newsize.height = (int) ( p_vout->render.i_height * factor );
789 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
790 p_vout->output.i_aspect;
791 newsize.width = (int) ( p_vout->render.i_width * factor );
792 newsize.height = (int) ( i_corrected_height * factor );
795 [self setContentSize: newsize];
797 [self setFrameTopLeftPoint: topleftscreen];
798 p_vout->i_changes |= VOUT_SIZE_CHANGE;
802 - (void)toggleFloatOnTop
804 if( config_GetInt( p_vout, "video-on-top" ) )
806 config_PutInt( p_vout, "video-on-top", 0 );
807 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
811 config_PutInt( p_vout, "video-on-top", 1 );
812 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
816 - (void)toggleFullscreen
818 config_PutInt(p_vout, "fullscreen", !p_vout->b_fullscreen);
819 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
824 return( p_vout->b_fullscreen );
827 - (BOOL)canBecomeKeyWindow
832 - (void)keyDown:(NSEvent *)o_event
836 unsigned int i_pressed_modifiers = 0;
839 i_pressed_modifiers = [o_event modifierFlags];
841 if( i_pressed_modifiers & NSShiftKeyMask )
842 val.i_int |= KEY_MODIFIER_SHIFT;
843 if( i_pressed_modifiers & NSControlKeyMask )
844 val.i_int |= KEY_MODIFIER_CTRL;
845 if( i_pressed_modifiers & NSAlternateKeyMask )
846 val.i_int |= KEY_MODIFIER_ALT;
847 if( i_pressed_modifiers & NSCommandKeyMask )
848 val.i_int |= KEY_MODIFIER_COMMAND;
850 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
854 /* Escape should always get you out of fullscreen */
855 if( key == (unichar) 0x1b )
857 if( [self isFullscreen] )
859 [self toggleFullscreen];
862 else if ( key == ' ' )
864 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
866 if ( p_playlist != NULL )
868 playlist_Pause( p_playlist );
869 vlc_object_release( p_playlist);
874 val.i_int |= CocoaKeyToVLC( key );
875 var_Set( p_vout->p_vlc, "key-pressed", val );
880 [super keyDown: o_event];
886 NSMutableString * o_title;
887 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
890 if( p_playlist == NULL )
895 vlc_mutex_lock( &p_playlist->object_lock );
896 o_title = [NSMutableString stringWithUTF8String:
897 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
898 vlc_mutex_unlock( &p_playlist->object_lock );
900 vlc_object_release( p_playlist );
904 NSRange prefix_range = [o_title rangeOfString: @"file:"];
905 if( prefix_range.location != NSNotFound )
907 [o_title deleteCharactersInRange: prefix_range];
910 [self setTitleWithRepresentedFilename: o_title];
915 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
919 /* This is actually the same as VLCControls::stop. */
920 - (BOOL)windowShouldClose:(id)sender
922 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
924 if( p_playlist == NULL )
929 playlist_Stop( p_playlist );
930 vlc_object_release( p_playlist );
932 /* The window will be closed by the intf later. */
938 /*****************************************************************************
939 * VLCView implementation
940 *****************************************************************************/
941 @implementation VLCView
943 - (void)drawRect:(NSRect)rect
945 vout_thread_t * p_vout;
946 id o_window = [self window];
947 p_vout = (vout_thread_t *)[o_window getVout];
949 [[NSColor blackColor] set];
951 [super drawRect: rect];
953 p_vout->i_changes |= VOUT_SIZE_CHANGE;
956 - (BOOL)acceptsFirstResponder
961 - (BOOL)becomeFirstResponder
963 vout_thread_t * p_vout;
964 id o_window = [self window];
965 p_vout = (vout_thread_t *)[o_window getVout];
967 [o_window setAcceptsMouseMovedEvents: YES];
971 - (BOOL)resignFirstResponder
973 vout_thread_t * p_vout;
974 id o_window = [self window];
975 p_vout = (vout_thread_t *)[o_window getVout];
977 [o_window setAcceptsMouseMovedEvents: NO];
978 VLCHideMouse( p_vout, NO );
982 - (void)mouseDown:(NSEvent *)o_event
984 vout_thread_t * p_vout;
985 id o_window = [self window];
986 p_vout = (vout_thread_t *)[o_window getVout];
989 switch( [o_event type] )
991 case NSLeftMouseDown:
993 var_Get( p_vout, "mouse-button-down", &val );
995 var_Set( p_vout, "mouse-button-down", val );
1000 [super mouseDown: o_event];
1005 - (void)otherMouseDown:(NSEvent *)o_event
1007 /* This is not the the wheel button. you need to poll the
1008 * mouseWheel event for that. other is a third, forth or fifth button */
1009 vout_thread_t * p_vout;
1010 id o_window = [self window];
1011 p_vout = (vout_thread_t *)[o_window getVout];
1014 switch( [o_event type] )
1016 case NSOtherMouseDown:
1018 var_Get( p_vout, "mouse-button-down", &val );
1020 var_Set( p_vout, "mouse-button-down", val );
1025 [super mouseDown: o_event];
1030 - (void)rightMouseDown:(NSEvent *)o_event
1032 vout_thread_t * p_vout;
1033 id o_window = [self window];
1034 p_vout = (vout_thread_t *)[o_window getVout];
1037 switch( [o_event type] )
1039 case NSRightMouseDown:
1041 var_Get( p_vout, "mouse-button-down", &val );
1043 var_Set( p_vout, "mouse-button-down", val );
1048 [super mouseDown: o_event];
1053 - (void)mouseUp:(NSEvent *)o_event
1055 vout_thread_t * p_vout;
1056 id o_window = [self window];
1057 p_vout = (vout_thread_t *)[o_window getVout];
1060 switch( [o_event type] )
1065 b_val.b_bool = VLC_TRUE;
1066 var_Set( p_vout, "mouse-clicked", b_val );
1068 var_Get( p_vout, "mouse-button-down", &val );
1070 var_Set( p_vout, "mouse-button-down", val );
1075 [super mouseUp: o_event];
1080 - (void)otherMouseUp:(NSEvent *)o_event
1082 vout_thread_t * p_vout;
1083 id o_window = [self window];
1084 p_vout = (vout_thread_t *)[o_window getVout];
1087 switch( [o_event type] )
1089 case NSOtherMouseUp:
1091 var_Get( p_vout, "mouse-button-down", &val );
1093 var_Set( p_vout, "mouse-button-down", val );
1098 [super mouseUp: o_event];
1103 - (void)rightMouseUp:(NSEvent *)o_event
1105 vout_thread_t * p_vout;
1106 id o_window = [self window];
1107 p_vout = (vout_thread_t *)[o_window getVout];
1110 switch( [o_event type] )
1112 case NSRightMouseUp:
1114 var_Get( p_vout, "mouse-button-down", &val );
1116 var_Set( p_vout, "mouse-button-down", val );
1121 [super mouseUp: o_event];
1126 - (void)mouseDragged:(NSEvent *)o_event
1128 [self mouseMoved:o_event];
1131 - (void)otherMouseDragged:(NSEvent *)o_event
1133 [self mouseMoved:o_event];
1136 - (void)rightMouseDragged:(NSEvent *)o_event
1138 [self mouseMoved:o_event];
1141 - (void)mouseMoved:(NSEvent *)o_event
1147 vout_thread_t * p_vout;
1148 id o_window = [self window];
1149 p_vout = (vout_thread_t *)[o_window getVout];
1151 s_rect = [self bounds];
1152 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1153 b_inside = [self mouse: ml inRect: s_rect];
1158 int i_width, i_height, i_x, i_y;
1160 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1161 (unsigned int)s_rect.size.height,
1162 &i_x, &i_y, &i_width, &i_height );
1164 val.i_int = ( ((int)ml.x) - i_x ) *
1165 p_vout->render.i_width / i_width;
1166 var_Set( p_vout, "mouse-x", val );
1168 val.i_int = ( ((int)ml.y) - i_y ) *
1169 p_vout->render.i_height / i_height;
1170 var_Set( p_vout, "mouse-y", val );
1172 val.b_bool = VLC_TRUE;
1173 var_Set( p_vout, "mouse-moved", val );
1174 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1175 p_vout->p_sys->b_mouse_moved = YES;
1178 [super mouseMoved: o_event];
1183 /*****************************************************************************
1184 * VLCVout implementation
1185 *****************************************************************************/
1186 @implementation VLCVout
1188 - (void)createWindow:(NSValue *)o_value
1192 NSScreen * o_screen;
1193 vout_thread_t * p_vout;
1194 vlc_bool_t b_main_screen;
1196 p_vout = (vout_thread_t *)[o_value pointerValue];
1198 p_vout->p_sys->o_window = [VLCWindow alloc];
1199 [p_vout->p_sys->o_window setVout: p_vout];
1200 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1202 if( var_Get( p_vout, "video-device", &val ) < 0 )
1204 o_screen = [NSScreen mainScreen];
1209 NSArray *o_screens = [NSScreen screens];
1210 unsigned int i_index = val.i_int;
1212 if( [o_screens count] < i_index )
1214 o_screen = [NSScreen mainScreen];
1220 o_screen = [o_screens objectAtIndex: i_index];
1221 config_PutInt( p_vout, "macosx-vdev", i_index );
1222 b_main_screen = (i_index == 0);
1226 if( p_vout->b_fullscreen )
1228 NSRect screen_rect = [o_screen frame];
1229 screen_rect.origin.x = screen_rect.origin.y = 0;
1231 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1232 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1233 NULL, NULL, fullScreenAllowEvents );
1235 [p_vout->p_sys->o_window
1236 initWithContentRect: screen_rect
1237 styleMask: NSBorderlessWindowMask
1238 backing: NSBackingStoreBuffered
1239 defer: NO screen: o_screen];
1241 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1242 p_vout->p_sys->b_mouse_moved = YES;
1243 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1247 unsigned int i_stylemask = NSTitledWindowMask |
1248 NSMiniaturizableWindowMask |
1249 NSClosableWindowMask |
1250 NSResizableWindowMask;
1252 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1253 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1254 p_vout->p_sys->p_fullscreen_state = NULL;
1256 [p_vout->p_sys->o_window
1257 initWithContentRect: p_vout->p_sys->s_rect
1258 styleMask: i_stylemask
1259 backing: NSBackingStoreBuffered
1260 defer: NO screen: o_screen];
1262 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1264 if( config_GetInt( p_vout, "video-on-top" ) )
1266 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1269 if( !p_vout->p_sys->b_pos_saved )
1271 [p_vout->p_sys->o_window center];
1275 o_view = [[VLCView alloc] init];
1276 /* FIXME: [o_view setMenu:] */
1277 [p_vout->p_sys->o_window setContentView: o_view];
1278 [o_view autorelease];
1281 p_vout->p_sys->p_qdport = [o_view qdPort];
1283 [o_view unlockFocus];
1285 [p_vout->p_sys->o_window updateTitle];
1286 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1290 - (void)destroyWindow:(NSValue *)o_value
1292 vout_thread_t * p_vout;
1294 p_vout = (vout_thread_t *)[o_value pointerValue];
1296 if( !p_vout->b_fullscreen )
1300 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1301 p_vout->p_sys->s_rect.size = s_rect.size;
1303 s_rect = [p_vout->p_sys->o_window frame];
1304 p_vout->p_sys->s_rect.origin = s_rect.origin;
1306 p_vout->p_sys->b_pos_saved = YES;
1309 p_vout->p_sys->p_qdport = nil;
1310 [p_vout->p_sys->o_window close];
1311 p_vout->p_sys->o_window = nil;