1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.67 2003/12/15 19:25:56 bigben 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 = p_vout->b_fullscreen;
439 var_Create(p_intf,"fullscreen",VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
440 var_Set(p_intf,"fullscreen",val);
442 val.b_bool = VLC_TRUE;
443 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
444 var_Set(p_intf, "intf-change",val);
450 /*****************************************************************************
451 * CoCreateWindow: create new window
452 *****************************************************************************
453 * Returns 0 on success, 1 otherwise
454 *****************************************************************************/
455 static int CoCreateWindow( vout_thread_t *p_vout )
457 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
459 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
466 /*****************************************************************************
467 * CoDestroyWindow: destroy window
468 *****************************************************************************
469 * Returns 0 on success, 1 otherwise
470 *****************************************************************************/
471 static int CoDestroyWindow( vout_thread_t *p_vout )
474 VLCHideMouse( p_vout, NO );
476 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
478 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
485 /*****************************************************************************
486 * CoToggleFullscreen: toggle fullscreen
487 *****************************************************************************
488 * Returns 0 on success, 1 otherwise
489 *****************************************************************************/
490 static int CoToggleFullscreen( vout_thread_t *p_vout )
492 QTDestroySequence( p_vout );
494 if( CoDestroyWindow( p_vout ) )
496 msg_Err( p_vout, "unable to destroy window" );
500 p_vout->b_fullscreen = !p_vout->b_fullscreen;
502 if( CoCreateWindow( p_vout ) )
504 msg_Err( p_vout, "unable to create window" );
508 SetPort( p_vout->p_sys->p_qdport );
509 QTScaleMatrix( p_vout );
511 if( QTCreateSequence( p_vout ) )
513 msg_Err( p_vout, "unable to create sequence" );
520 /*****************************************************************************
521 * VLCHideMouse: if b_hide then hide the cursor
522 *****************************************************************************/
523 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
528 NSWindow *o_window = p_vout->p_sys->o_window;
529 NSView *o_contents = [o_window contentView];
531 s_rect = [o_contents bounds];
532 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
533 ml = [o_contents convertPoint:ml fromView:nil];
534 b_inside = [o_contents mouse: ml inRect: s_rect];
536 if ( b_hide && b_inside )
538 /* only hide if mouse over VLCView */
539 [NSCursor setHiddenUntilMouseMoves: YES];
543 [NSCursor setHiddenUntilMouseMoves: NO];
545 p_vout->p_sys->b_mouse_moved = NO;
546 p_vout->p_sys->i_time_mouse_last_moved = mdate();
550 /*****************************************************************************
551 * QTScaleMatrix: scale matrix
552 *****************************************************************************/
553 static void QTScaleMatrix( vout_thread_t *p_vout )
556 unsigned int i_width, i_height;
557 Fixed factor_x, factor_y;
558 unsigned int i_offset_x = 0;
559 unsigned int i_offset_y = 0;
561 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
563 i_width = s_rect.right - s_rect.left;
564 i_height = s_rect.bottom - s_rect.top;
566 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
568 int i_adj_width = i_height * p_vout->output.i_aspect /
571 factor_x = FixDiv( Long2Fix( i_adj_width ),
572 Long2Fix( p_vout->output.i_width ) );
573 factor_y = FixDiv( Long2Fix( i_height ),
574 Long2Fix( p_vout->output.i_height ) );
576 i_offset_x = (i_width - i_adj_width) / 2;
580 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
581 p_vout->output.i_aspect;
583 factor_x = FixDiv( Long2Fix( i_width ),
584 Long2Fix( p_vout->output.i_width ) );
585 factor_y = FixDiv( Long2Fix( i_adj_height ),
586 Long2Fix( p_vout->output.i_height ) );
588 i_offset_y = (i_height - i_adj_height) / 2;
591 SetIdentityMatrix( p_vout->p_sys->p_matrix );
593 ScaleMatrix( p_vout->p_sys->p_matrix,
595 Long2Fix(0), Long2Fix(0) );
597 TranslateMatrix( p_vout->p_sys->p_matrix,
598 Long2Fix(i_offset_x),
599 Long2Fix(i_offset_y) );
603 /*****************************************************************************
604 * QTCreateSequence: create a new sequence
605 *****************************************************************************
606 * Returns 0 on success, 1 otherwise
607 *****************************************************************************/
608 static int QTCreateSequence( vout_thread_t *p_vout )
611 ImageDescriptionPtr p_descr;
613 HLock( (Handle)p_vout->p_sys->h_img_descr );
614 p_descr = *p_vout->p_sys->h_img_descr;
616 p_descr->idSize = sizeof(ImageDescription);
617 p_descr->cType = p_vout->p_sys->i_codec;
618 p_descr->version = 1;
619 p_descr->revisionLevel = 0;
620 p_descr->vendor = 'appl';
621 p_descr->width = p_vout->output.i_width;
622 p_descr->height = p_vout->output.i_height;
623 p_descr->hRes = Long2Fix(72);
624 p_descr->vRes = Long2Fix(72);
625 p_descr->spatialQuality = codecLosslessQuality;
626 p_descr->frameCount = 1;
627 p_descr->clutID = -1;
628 p_descr->dataSize = 0;
631 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
633 if( ( err = DecompressSequenceBeginS(
634 &p_vout->p_sys->i_seq,
635 p_vout->p_sys->h_img_descr,
637 p_vout->p_sys->p_qdport,
639 p_vout->p_sys->p_matrix,
641 codecFlagUseImageBuffer,
642 codecLosslessQuality,
643 p_vout->p_sys->img_dc ) ) )
645 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
652 /*****************************************************************************
653 * QTDestroySequence: destroy sequence
654 *****************************************************************************/
655 static void QTDestroySequence( vout_thread_t *p_vout )
657 CDSequenceEnd( p_vout->p_sys->i_seq );
660 /*****************************************************************************
661 * QTNewPicture: allocate a picture
662 *****************************************************************************
663 * Returns 0 on success, 1 otherwise
664 *****************************************************************************/
665 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
667 int i_width = p_vout->output.i_width;
668 int i_height = p_vout->output.i_height;
670 /* We know the chroma, allocate a buffer which will be used
671 * directly by the decoder */
672 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
674 if( p_pic->p_sys == NULL )
679 switch( p_vout->output.i_chroma )
681 case VLC_FOURCC('I','4','2','0'):
683 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
684 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
686 /* Allocate the memory buffer */
687 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
688 16, i_width * i_height * 3 / 2 );
691 p_pic->Y_PIXELS = p_pic->p_data;
692 p_pic->p[Y_PLANE].i_lines = i_height;
693 p_pic->p[Y_PLANE].i_pitch = i_width;
694 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
695 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
698 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
699 p_pic->p[U_PLANE].i_lines = i_height / 2;
700 p_pic->p[U_PLANE].i_pitch = i_width / 2;
701 p_pic->p[U_PLANE].i_pixel_pitch = 1;
702 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
705 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
706 p_pic->p[V_PLANE].i_lines = i_height / 2;
707 p_pic->p[V_PLANE].i_pitch = i_width / 2;
708 p_pic->p[V_PLANE].i_pixel_pitch = 1;
709 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
711 /* We allocated 3 planes */
714 #define P p_pic->p_sys->pixmap_i420
715 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
716 - p_pic->p_sys->p_info;
717 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
718 - p_pic->p_sys->p_info;
719 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
720 - p_pic->p_sys->p_info;
722 P.componentInfoY.rowBytes = i_width;
723 P.componentInfoCb.rowBytes = i_width / 2;
724 P.componentInfoCr.rowBytes = i_width / 2;
730 /* Unknown chroma, tell the guy to get lost */
731 free( p_pic->p_sys );
732 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
733 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
741 /*****************************************************************************
742 * QTFreePicture: destroy a picture allocated with QTNewPicture
743 *****************************************************************************/
744 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
746 switch( p_vout->output.i_chroma )
748 case VLC_FOURCC('I','4','2','0'):
749 free( p_pic->p_data_orig );
753 free( p_pic->p_sys );
756 /*****************************************************************************
757 * VLCWindow implementation
758 *****************************************************************************/
759 @implementation VLCWindow
761 - (void)setVout:(vout_thread_t *)_p_vout
766 - (vout_thread_t *)getVout
771 - (void)scaleWindowWithFactor: (float)factor
774 int i_corrected_height, i_corrected_width;
776 NSPoint topleftscreen;
778 if ( !p_vout->b_fullscreen )
781 topleftbase.y = [self frame].size.height;
782 topleftscreen = [self convertBaseToScreen: topleftbase];
784 if( p_vout->output.i_height * p_vout->output.i_aspect >
785 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
787 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
789 newsize.width = (int) ( i_corrected_width * factor );
790 newsize.height = (int) ( p_vout->render.i_height * factor );
794 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
795 p_vout->output.i_aspect;
796 newsize.width = (int) ( p_vout->render.i_width * factor );
797 newsize.height = (int) ( i_corrected_height * factor );
800 [self setContentSize: newsize];
802 [self setFrameTopLeftPoint: topleftscreen];
803 p_vout->i_changes |= VOUT_SIZE_CHANGE;
807 - (void)toggleFloatOnTop
809 if( config_GetInt( p_vout, "video-on-top" ) )
811 config_PutInt( p_vout, "video-on-top", 0 );
812 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
816 config_PutInt( p_vout, "video-on-top", 1 );
817 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
821 - (void)toggleFullscreen
823 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
828 return( p_vout->b_fullscreen );
831 - (BOOL)canBecomeKeyWindow
836 - (void)keyDown:(NSEvent *)o_event
840 unsigned int i_pressed_modifiers = 0;
843 i_pressed_modifiers = [o_event modifierFlags];
845 if( i_pressed_modifiers & NSShiftKeyMask )
846 val.i_int |= KEY_MODIFIER_SHIFT;
847 if( i_pressed_modifiers & NSControlKeyMask )
848 val.i_int |= KEY_MODIFIER_CTRL;
849 if( i_pressed_modifiers & NSAlternateKeyMask )
850 val.i_int |= KEY_MODIFIER_ALT;
851 if( i_pressed_modifiers & NSCommandKeyMask )
852 val.i_int |= KEY_MODIFIER_COMMAND;
854 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
858 /* Escape should always get you out of fullscreen */
859 if( key == (unichar) 0x1b )
861 if( [self isFullscreen] )
863 [self toggleFullscreen];
866 else if ( key == ' ' )
868 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
870 if ( p_playlist != NULL )
872 playlist_Pause( p_playlist );
873 vlc_object_release( p_playlist);
878 val.i_int |= CocoaKeyToVLC( key );
879 var_Set( p_vout->p_vlc, "key-pressed", val );
884 [super keyDown: o_event];
890 NSMutableString * o_title;
891 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
894 if( p_playlist == NULL )
899 vlc_mutex_lock( &p_playlist->object_lock );
900 o_title = [NSMutableString stringWithUTF8String:
901 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
902 vlc_mutex_unlock( &p_playlist->object_lock );
904 vlc_object_release( p_playlist );
908 NSRange prefix_range = [o_title rangeOfString: @"file:"];
909 if( prefix_range.location != NSNotFound )
911 [o_title deleteCharactersInRange: prefix_range];
914 [self setTitleWithRepresentedFilename: o_title];
919 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
923 /* This is actually the same as VLCControls::stop. */
924 - (BOOL)windowShouldClose:(id)sender
926 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
928 if( p_playlist == NULL )
933 playlist_Stop( p_playlist );
934 vlc_object_release( p_playlist );
936 /* The window will be closed by the intf later. */
942 /*****************************************************************************
943 * VLCView implementation
944 *****************************************************************************/
945 @implementation VLCView
947 - (void)drawRect:(NSRect)rect
949 vout_thread_t * p_vout;
950 id o_window = [self window];
951 p_vout = (vout_thread_t *)[o_window getVout];
953 [[NSColor blackColor] set];
955 [super drawRect: rect];
957 p_vout->i_changes |= VOUT_SIZE_CHANGE;
960 - (BOOL)acceptsFirstResponder
965 - (BOOL)becomeFirstResponder
967 vout_thread_t * p_vout;
968 id o_window = [self window];
969 p_vout = (vout_thread_t *)[o_window getVout];
971 [o_window setAcceptsMouseMovedEvents: YES];
975 - (BOOL)resignFirstResponder
977 vout_thread_t * p_vout;
978 id o_window = [self window];
979 p_vout = (vout_thread_t *)[o_window getVout];
981 [o_window setAcceptsMouseMovedEvents: NO];
982 VLCHideMouse( p_vout, NO );
986 - (void)mouseDown:(NSEvent *)o_event
988 vout_thread_t * p_vout;
989 id o_window = [self window];
990 p_vout = (vout_thread_t *)[o_window getVout];
993 switch( [o_event type] )
995 case NSLeftMouseDown:
997 var_Get( p_vout, "mouse-button-down", &val );
999 var_Set( p_vout, "mouse-button-down", val );
1004 [super mouseDown: o_event];
1009 - (void)otherMouseDown:(NSEvent *)o_event
1011 /* This is not the the wheel button. you need to poll the
1012 * mouseWheel event for that. other is a third, forth or fifth button */
1013 vout_thread_t * p_vout;
1014 id o_window = [self window];
1015 p_vout = (vout_thread_t *)[o_window getVout];
1018 switch( [o_event type] )
1020 case NSOtherMouseDown:
1022 var_Get( p_vout, "mouse-button-down", &val );
1024 var_Set( p_vout, "mouse-button-down", val );
1029 [super mouseDown: o_event];
1034 - (void)rightMouseDown:(NSEvent *)o_event
1036 vout_thread_t * p_vout;
1037 id o_window = [self window];
1038 p_vout = (vout_thread_t *)[o_window getVout];
1041 switch( [o_event type] )
1043 case NSRightMouseDown:
1045 var_Get( p_vout, "mouse-button-down", &val );
1047 var_Set( p_vout, "mouse-button-down", val );
1052 [super mouseDown: o_event];
1057 - (void)mouseUp:(NSEvent *)o_event
1059 vout_thread_t * p_vout;
1060 id o_window = [self window];
1061 p_vout = (vout_thread_t *)[o_window getVout];
1064 switch( [o_event type] )
1069 b_val.b_bool = VLC_TRUE;
1070 var_Set( p_vout, "mouse-clicked", b_val );
1072 var_Get( p_vout, "mouse-button-down", &val );
1074 var_Set( p_vout, "mouse-button-down", val );
1079 [super mouseUp: o_event];
1084 - (void)otherMouseUp:(NSEvent *)o_event
1086 vout_thread_t * p_vout;
1087 id o_window = [self window];
1088 p_vout = (vout_thread_t *)[o_window getVout];
1091 switch( [o_event type] )
1093 case NSOtherMouseUp:
1095 var_Get( p_vout, "mouse-button-down", &val );
1097 var_Set( p_vout, "mouse-button-down", val );
1102 [super mouseUp: o_event];
1107 - (void)rightMouseUp:(NSEvent *)o_event
1109 vout_thread_t * p_vout;
1110 id o_window = [self window];
1111 p_vout = (vout_thread_t *)[o_window getVout];
1114 switch( [o_event type] )
1116 case NSRightMouseUp:
1118 var_Get( p_vout, "mouse-button-down", &val );
1120 var_Set( p_vout, "mouse-button-down", val );
1125 [super mouseUp: o_event];
1130 - (void)mouseDragged:(NSEvent *)o_event
1132 [self mouseMoved:o_event];
1135 - (void)otherMouseDragged:(NSEvent *)o_event
1137 [self mouseMoved:o_event];
1140 - (void)rightMouseDragged:(NSEvent *)o_event
1142 [self mouseMoved:o_event];
1145 - (void)mouseMoved:(NSEvent *)o_event
1151 vout_thread_t * p_vout;
1152 id o_window = [self window];
1153 p_vout = (vout_thread_t *)[o_window getVout];
1155 s_rect = [self bounds];
1156 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1157 b_inside = [self mouse: ml inRect: s_rect];
1162 int i_width, i_height, i_x, i_y;
1164 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1165 (unsigned int)s_rect.size.height,
1166 &i_x, &i_y, &i_width, &i_height );
1168 val.i_int = ( ((int)ml.x) - i_x ) *
1169 p_vout->render.i_width / i_width;
1170 var_Set( p_vout, "mouse-x", val );
1172 val.i_int = ( ((int)ml.y) - i_y ) *
1173 p_vout->render.i_height / i_height;
1174 var_Set( p_vout, "mouse-y", val );
1176 val.b_bool = VLC_TRUE;
1177 var_Set( p_vout, "mouse-moved", val );
1178 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1179 p_vout->p_sys->b_mouse_moved = YES;
1182 [super mouseMoved: o_event];
1187 /*****************************************************************************
1188 * VLCVout implementation
1189 *****************************************************************************/
1190 @implementation VLCVout
1192 - (void)createWindow:(NSValue *)o_value
1196 NSScreen * o_screen;
1197 vout_thread_t * p_vout;
1198 vlc_bool_t b_main_screen;
1200 p_vout = (vout_thread_t *)[o_value pointerValue];
1202 p_vout->p_sys->o_window = [VLCWindow alloc];
1203 [p_vout->p_sys->o_window setVout: p_vout];
1204 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1206 if( var_Get( p_vout, "video-device", &val ) < 0 )
1208 o_screen = [NSScreen mainScreen];
1213 NSArray *o_screens = [NSScreen screens];
1214 unsigned int i_index = val.i_int;
1216 if( [o_screens count] < i_index )
1218 o_screen = [NSScreen mainScreen];
1224 o_screen = [o_screens objectAtIndex: i_index];
1225 config_PutInt( p_vout, "macosx-vdev", i_index );
1226 b_main_screen = (i_index == 0);
1230 if( p_vout->b_fullscreen )
1232 NSRect screen_rect = [o_screen frame];
1233 screen_rect.origin.x = screen_rect.origin.y = 0;
1235 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1236 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1237 NULL, NULL, fullScreenAllowEvents );
1239 [p_vout->p_sys->o_window
1240 initWithContentRect: screen_rect
1241 styleMask: NSBorderlessWindowMask
1242 backing: NSBackingStoreBuffered
1243 defer: NO screen: o_screen];
1245 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1246 p_vout->p_sys->b_mouse_moved = YES;
1247 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1251 unsigned int i_stylemask = NSTitledWindowMask |
1252 NSMiniaturizableWindowMask |
1253 NSClosableWindowMask |
1254 NSResizableWindowMask;
1256 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1257 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1258 p_vout->p_sys->p_fullscreen_state = NULL;
1260 [p_vout->p_sys->o_window
1261 initWithContentRect: p_vout->p_sys->s_rect
1262 styleMask: i_stylemask
1263 backing: NSBackingStoreBuffered
1264 defer: NO screen: o_screen];
1266 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1268 if( config_GetInt( p_vout, "video-on-top" ) )
1270 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1273 if( !p_vout->p_sys->b_pos_saved )
1275 [p_vout->p_sys->o_window center];
1279 o_view = [[VLCView alloc] init];
1280 /* FIXME: [o_view setMenu:] */
1281 [p_vout->p_sys->o_window setContentView: o_view];
1282 [o_view autorelease];
1285 p_vout->p_sys->p_qdport = [o_view qdPort];
1287 [o_view unlockFocus];
1289 [p_vout->p_sys->o_window updateTitle];
1290 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1294 - (void)destroyWindow:(NSValue *)o_value
1296 vout_thread_t * p_vout;
1298 p_vout = (vout_thread_t *)[o_value pointerValue];
1300 if( !p_vout->b_fullscreen )
1304 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1305 p_vout->p_sys->s_rect.size = s_rect.size;
1307 s_rect = [p_vout->p_sys->o_window frame];
1308 p_vout->p_sys->s_rect.origin = s_rect.origin;
1310 p_vout->p_sys->b_pos_saved = YES;
1313 p_vout->p_sys->p_qdport = nil;
1314 [p_vout->p_sys->o_window close];
1315 p_vout->p_sys->o_window = nil;