1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.65 2003/11/15 22:42:16 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>
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 VLCVout * o_vlv = [[VLCVout alloc] init];
423 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
425 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
433 /*****************************************************************************
434 * CoCreateWindow: create new window
435 *****************************************************************************
436 * Returns 0 on success, 1 otherwise
437 *****************************************************************************/
438 static int CoCreateWindow( vout_thread_t *p_vout )
440 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
442 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
449 /*****************************************************************************
450 * CoDestroyWindow: destroy window
451 *****************************************************************************
452 * Returns 0 on success, 1 otherwise
453 *****************************************************************************/
454 static int CoDestroyWindow( vout_thread_t *p_vout )
457 VLCHideMouse( p_vout, NO );
459 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
461 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
468 /*****************************************************************************
469 * CoToggleFullscreen: toggle fullscreen
470 *****************************************************************************
471 * Returns 0 on success, 1 otherwise
472 *****************************************************************************/
473 static int CoToggleFullscreen( vout_thread_t *p_vout )
475 QTDestroySequence( p_vout );
477 if( CoDestroyWindow( p_vout ) )
479 msg_Err( p_vout, "unable to destroy window" );
483 p_vout->b_fullscreen = !p_vout->b_fullscreen;
485 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
487 if( CoCreateWindow( p_vout ) )
489 msg_Err( p_vout, "unable to create window" );
493 SetPort( p_vout->p_sys->p_qdport );
494 QTScaleMatrix( p_vout );
496 if( QTCreateSequence( p_vout ) )
498 msg_Err( p_vout, "unable to create sequence" );
505 /*****************************************************************************
506 * VLCHideMouse: if b_hide then hide the cursor
507 *****************************************************************************/
508 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
513 NSWindow *o_window = p_vout->p_sys->o_window;
514 NSView *o_contents = [o_window contentView];
516 s_rect = [o_contents bounds];
517 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
518 ml = [o_contents convertPoint:ml fromView:nil];
519 b_inside = [o_contents mouse: ml inRect: s_rect];
521 if ( b_hide && b_inside )
523 /* only hide if mouse over VLCView */
524 [NSCursor setHiddenUntilMouseMoves: YES];
528 [NSCursor setHiddenUntilMouseMoves: NO];
530 p_vout->p_sys->b_mouse_moved = NO;
531 p_vout->p_sys->i_time_mouse_last_moved = mdate();
535 /*****************************************************************************
536 * QTScaleMatrix: scale matrix
537 *****************************************************************************/
538 static void QTScaleMatrix( vout_thread_t *p_vout )
541 unsigned int i_width, i_height;
542 Fixed factor_x, factor_y;
543 unsigned int i_offset_x = 0;
544 unsigned int i_offset_y = 0;
546 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
548 i_width = s_rect.right - s_rect.left;
549 i_height = s_rect.bottom - s_rect.top;
551 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
553 int i_adj_width = i_height * p_vout->output.i_aspect /
556 factor_x = FixDiv( Long2Fix( i_adj_width ),
557 Long2Fix( p_vout->output.i_width ) );
558 factor_y = FixDiv( Long2Fix( i_height ),
559 Long2Fix( p_vout->output.i_height ) );
561 i_offset_x = (i_width - i_adj_width) / 2;
565 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
566 p_vout->output.i_aspect;
568 factor_x = FixDiv( Long2Fix( i_width ),
569 Long2Fix( p_vout->output.i_width ) );
570 factor_y = FixDiv( Long2Fix( i_adj_height ),
571 Long2Fix( p_vout->output.i_height ) );
573 i_offset_y = (i_height - i_adj_height) / 2;
576 SetIdentityMatrix( p_vout->p_sys->p_matrix );
578 ScaleMatrix( p_vout->p_sys->p_matrix,
580 Long2Fix(0), Long2Fix(0) );
582 TranslateMatrix( p_vout->p_sys->p_matrix,
583 Long2Fix(i_offset_x),
584 Long2Fix(i_offset_y) );
588 /*****************************************************************************
589 * QTCreateSequence: create a new sequence
590 *****************************************************************************
591 * Returns 0 on success, 1 otherwise
592 *****************************************************************************/
593 static int QTCreateSequence( vout_thread_t *p_vout )
596 ImageDescriptionPtr p_descr;
598 HLock( (Handle)p_vout->p_sys->h_img_descr );
599 p_descr = *p_vout->p_sys->h_img_descr;
601 p_descr->idSize = sizeof(ImageDescription);
602 p_descr->cType = p_vout->p_sys->i_codec;
603 p_descr->version = 1;
604 p_descr->revisionLevel = 0;
605 p_descr->vendor = 'appl';
606 p_descr->width = p_vout->output.i_width;
607 p_descr->height = p_vout->output.i_height;
608 p_descr->hRes = Long2Fix(72);
609 p_descr->vRes = Long2Fix(72);
610 p_descr->spatialQuality = codecLosslessQuality;
611 p_descr->frameCount = 1;
612 p_descr->clutID = -1;
613 p_descr->dataSize = 0;
616 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
618 if( ( err = DecompressSequenceBeginS(
619 &p_vout->p_sys->i_seq,
620 p_vout->p_sys->h_img_descr,
622 p_vout->p_sys->p_qdport,
624 p_vout->p_sys->p_matrix,
626 codecFlagUseImageBuffer,
627 codecLosslessQuality,
628 p_vout->p_sys->img_dc ) ) )
630 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
637 /*****************************************************************************
638 * QTDestroySequence: destroy sequence
639 *****************************************************************************/
640 static void QTDestroySequence( vout_thread_t *p_vout )
642 CDSequenceEnd( p_vout->p_sys->i_seq );
645 /*****************************************************************************
646 * QTNewPicture: allocate a picture
647 *****************************************************************************
648 * Returns 0 on success, 1 otherwise
649 *****************************************************************************/
650 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
652 int i_width = p_vout->output.i_width;
653 int i_height = p_vout->output.i_height;
655 /* We know the chroma, allocate a buffer which will be used
656 * directly by the decoder */
657 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
659 if( p_pic->p_sys == NULL )
664 switch( p_vout->output.i_chroma )
666 case VLC_FOURCC('I','4','2','0'):
668 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
669 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
671 /* Allocate the memory buffer */
672 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
673 16, i_width * i_height * 3 / 2 );
676 p_pic->Y_PIXELS = p_pic->p_data;
677 p_pic->p[Y_PLANE].i_lines = i_height;
678 p_pic->p[Y_PLANE].i_pitch = i_width;
679 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
680 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
683 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
684 p_pic->p[U_PLANE].i_lines = i_height / 2;
685 p_pic->p[U_PLANE].i_pitch = i_width / 2;
686 p_pic->p[U_PLANE].i_pixel_pitch = 1;
687 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
690 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
691 p_pic->p[V_PLANE].i_lines = i_height / 2;
692 p_pic->p[V_PLANE].i_pitch = i_width / 2;
693 p_pic->p[V_PLANE].i_pixel_pitch = 1;
694 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
696 /* We allocated 3 planes */
699 #define P p_pic->p_sys->pixmap_i420
700 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
701 - p_pic->p_sys->p_info;
702 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
703 - p_pic->p_sys->p_info;
704 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
705 - p_pic->p_sys->p_info;
707 P.componentInfoY.rowBytes = i_width;
708 P.componentInfoCb.rowBytes = i_width / 2;
709 P.componentInfoCr.rowBytes = i_width / 2;
715 /* Unknown chroma, tell the guy to get lost */
716 free( p_pic->p_sys );
717 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
718 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
726 /*****************************************************************************
727 * QTFreePicture: destroy a picture allocated with QTNewPicture
728 *****************************************************************************/
729 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
731 switch( p_vout->output.i_chroma )
733 case VLC_FOURCC('I','4','2','0'):
734 free( p_pic->p_data_orig );
738 free( p_pic->p_sys );
741 /*****************************************************************************
742 * VLCWindow implementation
743 *****************************************************************************/
744 @implementation VLCWindow
746 - (void)setVout:(vout_thread_t *)_p_vout
751 - (vout_thread_t *)getVout
756 - (void)scaleWindowWithFactor: (float)factor
759 int i_corrected_height, i_corrected_width;
761 NSPoint topleftscreen;
763 if ( !p_vout->b_fullscreen )
766 topleftbase.y = [self frame].size.height;
767 topleftscreen = [self convertBaseToScreen: topleftbase];
769 if( p_vout->output.i_height * p_vout->output.i_aspect >
770 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
772 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
774 newsize.width = (int) ( i_corrected_width * factor );
775 newsize.height = (int) ( p_vout->render.i_height * factor );
779 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
780 p_vout->output.i_aspect;
781 newsize.width = (int) ( p_vout->render.i_width * factor );
782 newsize.height = (int) ( i_corrected_height * factor );
785 [self setContentSize: newsize];
787 [self setFrameTopLeftPoint: topleftscreen];
788 p_vout->i_changes |= VOUT_SIZE_CHANGE;
792 - (void)toggleFloatOnTop
794 if( config_GetInt( p_vout, "macosx-float" ) )
796 config_PutInt( p_vout, "macosx-float", 0 );
797 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
801 config_PutInt( p_vout, "macosx-float", 1 );
802 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
806 - (void)toggleFullscreen
808 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
813 return( p_vout->b_fullscreen );
816 - (BOOL)canBecomeKeyWindow
821 - (void)keyDown:(NSEvent *)o_event
825 unsigned int i_pressed_modifiers = 0;
828 i_pressed_modifiers = [o_event modifierFlags];
830 if( i_pressed_modifiers & NSShiftKeyMask )
831 val.i_int |= KEY_MODIFIER_SHIFT;
832 if( i_pressed_modifiers & NSControlKeyMask )
833 val.i_int |= KEY_MODIFIER_CTRL;
834 if( i_pressed_modifiers & NSAlternateKeyMask )
835 val.i_int |= KEY_MODIFIER_ALT;
836 if( i_pressed_modifiers & NSCommandKeyMask )
837 val.i_int |= KEY_MODIFIER_COMMAND;
839 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
843 /* Escape should always get you out of fullscreen */
844 if( key == (unichar) 0x1b )
846 if( [self isFullscreen] )
848 [self toggleFullscreen];
851 else if ( key == ' ' )
853 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
855 if ( p_playlist != NULL )
857 playlist_Pause( p_playlist );
858 vlc_object_release( p_playlist);
863 val.i_int |= CocoaKeyToVLC( key );
864 var_Set( p_vout->p_vlc, "key-pressed", val );
869 [super keyDown: o_event];
875 NSMutableString * o_title;
876 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
879 if( p_playlist == NULL )
884 vlc_mutex_lock( &p_playlist->object_lock );
885 o_title = [NSMutableString stringWithUTF8String:
886 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
887 vlc_mutex_unlock( &p_playlist->object_lock );
889 vlc_object_release( p_playlist );
893 NSRange prefix_range = [o_title rangeOfString: @"file:"];
894 if( prefix_range.location != NSNotFound )
896 [o_title deleteCharactersInRange: prefix_range];
899 [self setTitleWithRepresentedFilename: o_title];
904 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
908 /* This is actually the same as VLCControls::stop. */
909 - (BOOL)windowShouldClose:(id)sender
911 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
913 if( p_playlist == NULL )
918 playlist_Stop( p_playlist );
919 vlc_object_release( p_playlist );
921 /* The window will be closed by the intf later. */
927 /*****************************************************************************
928 * VLCView implementation
929 *****************************************************************************/
930 @implementation VLCView
932 - (void)drawRect:(NSRect)rect
934 vout_thread_t * p_vout;
935 id o_window = [self window];
936 p_vout = (vout_thread_t *)[o_window getVout];
938 [[NSColor blackColor] set];
940 [super drawRect: rect];
942 p_vout->i_changes |= VOUT_SIZE_CHANGE;
945 - (BOOL)acceptsFirstResponder
950 - (BOOL)becomeFirstResponder
952 vout_thread_t * p_vout;
953 id o_window = [self window];
954 p_vout = (vout_thread_t *)[o_window getVout];
956 [o_window setAcceptsMouseMovedEvents: YES];
960 - (BOOL)resignFirstResponder
962 vout_thread_t * p_vout;
963 id o_window = [self window];
964 p_vout = (vout_thread_t *)[o_window getVout];
966 [o_window setAcceptsMouseMovedEvents: NO];
967 VLCHideMouse( p_vout, NO );
971 - (void)mouseDown:(NSEvent *)o_event
973 vout_thread_t * p_vout;
974 id o_window = [self window];
975 p_vout = (vout_thread_t *)[o_window getVout];
978 switch( [o_event type] )
980 case NSLeftMouseDown:
982 var_Get( p_vout, "mouse-button-down", &val );
984 var_Set( p_vout, "mouse-button-down", val );
989 [super mouseDown: o_event];
994 - (void)otherMouseDown:(NSEvent *)o_event
996 /* This is not the the wheel button. you need to poll the
997 * mouseWheel event for that. other is a third, forth or fifth button */
998 vout_thread_t * p_vout;
999 id o_window = [self window];
1000 p_vout = (vout_thread_t *)[o_window getVout];
1003 switch( [o_event type] )
1005 case NSOtherMouseDown:
1007 var_Get( p_vout, "mouse-button-down", &val );
1009 var_Set( p_vout, "mouse-button-down", val );
1014 [super mouseDown: o_event];
1019 - (void)rightMouseDown:(NSEvent *)o_event
1021 vout_thread_t * p_vout;
1022 id o_window = [self window];
1023 p_vout = (vout_thread_t *)[o_window getVout];
1026 switch( [o_event type] )
1028 case NSRightMouseDown:
1030 var_Get( p_vout, "mouse-button-down", &val );
1032 var_Set( p_vout, "mouse-button-down", val );
1037 [super mouseDown: o_event];
1042 - (void)mouseUp:(NSEvent *)o_event
1044 vout_thread_t * p_vout;
1045 id o_window = [self window];
1046 p_vout = (vout_thread_t *)[o_window getVout];
1049 switch( [o_event type] )
1054 b_val.b_bool = VLC_TRUE;
1055 var_Set( p_vout, "mouse-clicked", b_val );
1057 var_Get( p_vout, "mouse-button-down", &val );
1059 var_Set( p_vout, "mouse-button-down", val );
1064 [super mouseUp: o_event];
1069 - (void)otherMouseUp:(NSEvent *)o_event
1071 vout_thread_t * p_vout;
1072 id o_window = [self window];
1073 p_vout = (vout_thread_t *)[o_window getVout];
1076 switch( [o_event type] )
1078 case NSOtherMouseUp:
1080 var_Get( p_vout, "mouse-button-down", &val );
1082 var_Set( p_vout, "mouse-button-down", val );
1087 [super mouseUp: o_event];
1092 - (void)rightMouseUp:(NSEvent *)o_event
1094 vout_thread_t * p_vout;
1095 id o_window = [self window];
1096 p_vout = (vout_thread_t *)[o_window getVout];
1099 switch( [o_event type] )
1101 case NSRightMouseUp:
1103 var_Get( p_vout, "mouse-button-down", &val );
1105 var_Set( p_vout, "mouse-button-down", val );
1110 [super mouseUp: o_event];
1115 - (void)mouseDragged:(NSEvent *)o_event
1117 [self mouseMoved:o_event];
1120 - (void)otherMouseDragged:(NSEvent *)o_event
1122 [self mouseMoved:o_event];
1125 - (void)rightMouseDragged:(NSEvent *)o_event
1127 [self mouseMoved:o_event];
1130 - (void)mouseMoved:(NSEvent *)o_event
1136 vout_thread_t * p_vout;
1137 id o_window = [self window];
1138 p_vout = (vout_thread_t *)[o_window getVout];
1140 s_rect = [self bounds];
1141 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1142 b_inside = [self mouse: ml inRect: s_rect];
1147 int i_width, i_height, i_x, i_y;
1149 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1150 (unsigned int)s_rect.size.height,
1151 &i_x, &i_y, &i_width, &i_height );
1153 val.i_int = ( ((int)ml.x) - i_x ) *
1154 p_vout->render.i_width / i_width;
1155 var_Set( p_vout, "mouse-x", val );
1157 val.i_int = ( ((int)ml.y) - i_y ) *
1158 p_vout->render.i_height / i_height;
1159 var_Set( p_vout, "mouse-y", val );
1161 val.b_bool = VLC_TRUE;
1162 var_Set( p_vout, "mouse-moved", val );
1163 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1164 p_vout->p_sys->b_mouse_moved = YES;
1167 [super mouseMoved: o_event];
1172 /*****************************************************************************
1173 * VLCVout implementation
1174 *****************************************************************************/
1175 @implementation VLCVout
1177 - (void)createWindow:(NSValue *)o_value
1181 NSScreen * o_screen;
1182 vout_thread_t * p_vout;
1183 vlc_bool_t b_main_screen;
1185 p_vout = (vout_thread_t *)[o_value pointerValue];
1187 p_vout->p_sys->o_window = [VLCWindow alloc];
1188 [p_vout->p_sys->o_window setVout: p_vout];
1189 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1191 if( var_Get( p_vout, "video-device", &val ) < 0 )
1193 o_screen = [NSScreen mainScreen];
1198 NSArray *o_screens = [NSScreen screens];
1199 unsigned int i_index = val.i_int;
1201 if( [o_screens count] < i_index )
1203 o_screen = [NSScreen mainScreen];
1209 o_screen = [o_screens objectAtIndex: i_index];
1210 config_PutInt( p_vout, "macosx-vdev", i_index );
1211 b_main_screen = (i_index == 0);
1215 if( p_vout->b_fullscreen )
1217 NSRect screen_rect = [o_screen frame];
1218 screen_rect.origin.x = screen_rect.origin.y = 0;
1220 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1221 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1222 NULL, NULL, fullScreenAllowEvents );
1224 [p_vout->p_sys->o_window
1225 initWithContentRect: screen_rect
1226 styleMask: NSBorderlessWindowMask
1227 backing: NSBackingStoreBuffered
1228 defer: NO screen: o_screen];
1230 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1231 p_vout->p_sys->b_mouse_moved = YES;
1232 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1236 unsigned int i_stylemask = NSTitledWindowMask |
1237 NSMiniaturizableWindowMask |
1238 NSClosableWindowMask |
1239 NSResizableWindowMask;
1241 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1242 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1243 p_vout->p_sys->p_fullscreen_state = NULL;
1245 [p_vout->p_sys->o_window
1246 initWithContentRect: p_vout->p_sys->s_rect
1247 styleMask: i_stylemask
1248 backing: NSBackingStoreBuffered
1249 defer: NO screen: o_screen];
1251 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1253 if( config_GetInt( p_vout, "macosx-float" ) )
1255 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1258 if( !p_vout->p_sys->b_pos_saved )
1260 [p_vout->p_sys->o_window center];
1264 o_view = [[VLCView alloc] init];
1265 /* FIXME: [o_view setMenu:] */
1266 [p_vout->p_sys->o_window setContentView: o_view];
1267 [o_view autorelease];
1270 p_vout->p_sys->p_qdport = [o_view qdPort];
1272 [o_view unlockFocus];
1274 [p_vout->p_sys->o_window updateTitle];
1275 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1278 - (void)destroyWindow:(NSValue *)o_value
1280 vout_thread_t * p_vout;
1282 p_vout = (vout_thread_t *)[o_value pointerValue];
1284 if( !p_vout->b_fullscreen )
1288 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1289 p_vout->p_sys->s_rect.size = s_rect.size;
1291 s_rect = [p_vout->p_sys->o_window frame];
1292 p_vout->p_sys->s_rect.origin = s_rect.origin;
1294 p_vout->p_sys->b_pos_saved = YES;
1297 p_vout->p_sys->p_qdport = nil;
1298 [p_vout->p_sys->o_window close];
1299 p_vout->p_sys->o_window = nil;