2 * CoreVideo video output driver
3 * Copyright (c) 2005 Nicolas Plourde <nicolasplourde@gmail.com>
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #import "vo_corevideo.h"
23 #include <sys/types.h>
27 #include <CoreServices/CoreServices.h>
28 //special workaround for Apple bug #6267445
29 //(OSServices Power API disabled in OSServices.h for 64bit systems)
31 #include <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
36 #include "fastmemcpy.h"
37 #include "video_out.h"
38 #include "video_out_internal.h"
44 #include "subopt-helper.h"
46 #include "input/input.h"
47 #include "input/mouse.h"
49 #include "osdep/keycodes.h"
50 #include "osx_common.h"
53 NSDistantObject *mplayerosxProxy;
54 id <MPlayerOSXVOProto> mplayerosxProto;
55 MPlayerOpenGLView *mpGLView;
56 NSAutoreleasePool *autoreleasepool;
60 BOOL shared_buffer = false;
61 #define DEFAULT_BUFFER_NAME "mplayerosx"
62 static char *buffer_name;
67 NSScreen *screen_handle;
68 NSArray *screen_array;
71 unsigned char *image_data;
72 // For double buffering
73 static uint8_t image_page = 0;
74 static unsigned char *image_datas[2];
76 static uint32_t image_width;
77 static uint32_t image_height;
78 static uint32_t image_depth;
79 static uint32_t image_bytes;
80 static uint32_t image_format;
83 static int isFullscreen;
87 static float winAlpha = 1;
88 static int int_pause = 0;
90 static BOOL isLeopardOrLater;
92 #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
93 #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
95 static vo_info_t info =
97 "Mac OS X Core Video",
99 "Nicolas Plourde <nicolas.plourde@gmail.com>",
103 LIBVO_EXTERN(corevideo)
105 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
107 switch (image_format)
110 vo_draw_alpha_rgb24(w,h,src,srca,stride,image_data+3*(y0*image_width+x0),3*image_width);
114 vo_draw_alpha_rgb32(w,h,src,srca,stride,image_data+4*(y0*image_width+x0),4*image_width);
117 vo_draw_alpha_yuy2(w,h,src,srca,stride,image_data + (x0 + y0 * image_width) * 2,image_width*2);
122 static void update_screen_info(void)
124 if (screen_id == -1 && xinerama_screen > -1)
125 screen_id = xinerama_screen;
127 screen_array = [NSScreen screens];
128 if(screen_id >= (int)[screen_array count])
130 mp_msg(MSGT_VO, MSGL_INFO, "[vo_corevideo] Device ID %d does not exist, falling back to main device\n", screen_id);
133 if (screen_id < 0 && [mpGLView window])
134 screen_handle = [[mpGLView window] screen];
136 screen_handle = [screen_array objectAtIndex:(screen_id < 0 ? 0 : screen_id)];
138 screen_frame = [screen_handle frame];
139 vo_screenwidth = screen_frame.size.width;
140 vo_screenheight = screen_frame.size.height;
141 xinerama_x = xinerama_y = 0;
142 aspect_save_screenres(vo_screenwidth, vo_screenheight);
145 static void free_file_specific(void)
149 [mplayerosxProto stop];
150 mplayerosxProto = nil;
151 [mplayerosxProxy release];
152 mplayerosxProxy = nil;
154 if (munmap(image_data, image_width*image_height*image_bytes) == -1)
155 mp_msg(MSGT_VO, MSGL_FATAL, "[vo_corevideo] uninit: munmap failed. Error: %s\n", strerror(errno));
157 if (shm_unlink(buffer_name) == -1)
158 mp_msg(MSGT_VO, MSGL_FATAL, "[vo_corevideo] uninit: shm_unlink failed. Error: %s\n", strerror(errno));
160 free(image_datas[0]);
161 if (vo_doublebuffering)
162 free(image_datas[1]);
163 image_datas[0] = NULL;
164 image_datas[1] = NULL;
169 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
171 free_file_specific();
175 image_height = height;
176 switch (image_format)
189 image_bytes = (image_depth + 7) / 8;
193 config_movie_aspect((float)d_width/d_height);
195 vo_dwidth = d_width *= mpGLView->winSizeMult;
196 vo_dheight = d_height *= mpGLView->winSizeMult;
198 image_data = malloc(image_width*image_height*image_bytes);
199 image_datas[0] = image_data;
200 if (vo_doublebuffering)
201 image_datas[1] = malloc(image_width*image_height*image_bytes);
204 vo_fs = flags & VOFLAG_FULLSCREEN;
213 mp_msg(MSGT_VO, MSGL_INFO, "[vo_corevideo] writing output to a shared buffer "
214 "named \"%s\"\n",buffer_name);
216 // create shared memory
217 shm_fd = shm_open(buffer_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
220 mp_msg(MSGT_VO, MSGL_FATAL,
221 "[vo_corevideo] failed to open shared memory. Error: %s\n", strerror(errno));
226 if (ftruncate(shm_fd, image_width*image_height*image_bytes) == -1)
228 mp_msg(MSGT_VO, MSGL_FATAL,
229 "[vo_corevideo] failed to size shared memory, possibly already in use. Error: %s\n", strerror(errno));
231 shm_unlink(buffer_name);
235 image_data = mmap(NULL, image_width*image_height*image_bytes,
236 PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
239 if (image_data == MAP_FAILED)
241 mp_msg(MSGT_VO, MSGL_FATAL,
242 "[vo_corevideo] failed to map shared memory. Error: %s\n", strerror(errno));
243 shm_unlink(buffer_name);
247 //connect to mplayerosx
248 mplayerosxProxy=[NSConnection rootProxyForConnectionWithRegisteredName:[NSString stringWithCString:buffer_name] host:nil];
249 if ([mplayerosxProxy conformsToProtocol:@protocol(MPlayerOSXVOProto)]) {
250 [mplayerosxProxy setProtocolForProxy:@protocol(MPlayerOSXVOProto)];
251 mplayerosxProto = (id <MPlayerOSXVOProto>)mplayerosxProxy;
252 [mplayerosxProto startWithWidth: image_width withHeight: image_height withBytes: image_bytes withAspect:d_width*100/d_height];
255 [mplayerosxProxy release];
256 mplayerosxProxy = nil;
257 mplayerosxProto = nil;
263 static void check_events(void)
266 [mpGLView check_events];
269 static void draw_osd(void)
271 vo_draw_text(image_width, image_height, draw_alpha);
274 static void flip_page(void)
277 NSAutoreleasePool *pool = [NSAutoreleasePool new];
278 [mplayerosxProto render];
281 [mpGLView setCurrentTexture];
283 if (vo_doublebuffering) {
284 image_page = 1 - image_page;
285 image_data = image_datas[image_page];
290 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
296 static int draw_frame(uint8_t *src[])
301 static uint32_t draw_image(mp_image_t *mpi)
303 memcpy_pic(image_data, mpi->planes[0], image_width*image_bytes, image_height, image_width*image_bytes, mpi->stride[0]);
308 static int query_format(uint32_t format)
310 const int supportflags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
311 image_format = format;
316 pixelFormat = kYUVSPixelFormat;
320 pixelFormat = k24RGBPixelFormat;
324 pixelFormat = k32ARGBPixelFormat;
328 pixelFormat = k32BGRAPixelFormat;
334 static void uninit(void)
336 SetSystemUIMode( kUIModeNormal, 0);
337 CGDisplayShowCursor(kCGDirectMainDisplay);
339 free_file_specific();
343 NSAutoreleasePool *finalPool;
345 [autoreleasepool release];
346 finalPool = [[NSAutoreleasePool alloc] init];
347 [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
355 static const opt_t subopts[] = {
356 {"device_id", OPT_ARG_INT, &screen_id, NULL},
357 {"shared_buffer", OPT_ARG_BOOL, &shared_buffer, NULL},
358 {"buffer_name", OPT_ARG_MSTRZ,&buffer_name, NULL},
362 static int preinit(const char *arg)
367 shared_buffer = false;
370 if (subopt_parse(arg, subopts) != 0) {
371 mp_msg(MSGT_VO, MSGL_FATAL,
372 "\n-vo corevideo command line help:\n"
373 "Example: mplayer -vo corevideo:device_id=1:shared_buffer:buffer_name=mybuff\n"
375 " device_id=<0-...>\n"
376 " Set screen device ID for fullscreen.\n"
378 " Write output to a shared memory buffer instead of displaying it.\n"
379 " buffer_name=<name>\n"
380 " Name of the shared buffer created with shm_open() as well as\n"
381 " the name of the NSConnection MPlayer will try to open.\n"
382 " Setting buffer_name implicitly enables shared_buffer.\n"
387 autoreleasepool = [[NSAutoreleasePool alloc] init];
390 buffer_name = strdup(DEFAULT_BUFFER_NAME);
392 shared_buffer = true;
397 NSApp = [NSApplication sharedApplication];
398 isLeopardOrLater = floor(NSAppKitVersionNumber) > 824;
400 osx_foreground_hack();
404 mpGLView = [[MPlayerOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat:[MPlayerOpenGLView defaultPixelFormat]];
405 [mpGLView autorelease];
407 // Install an event handler so the Quit menu entry works
408 // The proper way using NSApp setDelegate: and
409 // applicationShouldTerminate: does not work,
410 // probably NSApplication never installs its handler.
411 [[NSAppleEventManager sharedAppleEventManager]
412 setEventHandler:mpGLView
413 andSelector:@selector(handleQuitEvent:withReplyEvent:)
414 forEventClass:kCoreEventClass
415 andEventID:kAEQuitApplication];
424 static int control(uint32_t request, void *data)
428 case VOCTRL_DRAW_IMAGE: return draw_image(data);
429 case VOCTRL_PAUSE: return int_pause = 1;
430 case VOCTRL_RESUME: return int_pause = 0;
431 case VOCTRL_QUERY_FORMAT: return query_format(*(uint32_t*)data);
432 case VOCTRL_ONTOP: vo_ontop = !vo_ontop; if(!shared_buffer){ [mpGLView ontop]; } else { [mplayerosxProto ontop]; } return VO_TRUE;
433 case VOCTRL_ROOTWIN: vo_rootwin = !vo_rootwin; [mpGLView rootwin]; return VO_TRUE;
434 case VOCTRL_FULLSCREEN: vo_fs = !vo_fs; if(!shared_buffer){ [mpGLView fullscreen: NO]; } else { [mplayerosxProto toggleFullscreen]; } return VO_TRUE;
435 case VOCTRL_GET_PANSCAN: return VO_TRUE;
436 case VOCTRL_SET_PANSCAN: [mpGLView panscan]; return VO_TRUE;
437 case VOCTRL_UPDATE_SCREENINFO: update_screen_info(); return VO_TRUE;
442 //////////////////////////////////////////////////////////////////////////
443 // NSOpenGLView Subclass
444 //////////////////////////////////////////////////////////////////////////
445 @implementation MPlayerOpenGLView
448 NSOpenGLContext *glContext;
449 GLint swapInterval = 1;
456 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
457 styleMask:NSTitledWindowMask|NSTexturedBackgroundWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
458 backing:NSBackingStoreBuffered defer:NO];
460 [window autorelease];
461 [window setDelegate:mpGLView];
462 [window setContentView:mpGLView];
463 [window setInitialFirstResponder:mpGLView];
464 [window setAcceptsMouseMovedEvents:YES];
465 [window setTitle:@"MPlayer - The Movie Player"];
470 //create OpenGL Context
471 glContext = [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil];
473 [self setOpenGLContext:glContext];
474 [glContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
475 [glContext setView:self];
476 [glContext makeCurrentContext];
479 error = CVOpenGLTextureCacheCreate(NULL, 0, [glContext CGLContextObj], [[self pixelFormat] CGLPixelFormatObj], 0, &textureCache);
480 if(error != kCVReturnSuccess)
481 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture Cache(%d)\n", error);
484 - (void) releaseVideoSpecific
486 CVPixelBufferRelease(frameBuffers[0]);
487 frameBuffers[0] = NULL;
488 CVPixelBufferRelease(frameBuffers[1]);
489 frameBuffers[1] = NULL;
490 CVOpenGLTextureRelease(texture);
496 [self releaseVideoSpecific];
497 CVOpenGLTextureCacheRelease(textureCache);
499 [self setOpenGLContext:nil];
506 CVReturn error = kCVReturnSuccess;
509 [window setContentSize:NSMakeSize(vo_dwidth, vo_dheight)];
511 // Use visibleFrame to position the window taking the menu bar and dock into account.
512 // Also flip vo_dy since the screen origin is in the bottom left on OSX.
513 update_screen_info();
514 visibleFrame = [screen_handle visibleFrame];
515 [window setFrameTopLeftPoint:NSMakePoint(
516 visibleFrame.origin.x + vo_dx,
517 visibleFrame.origin.y + visibleFrame.size.height - vo_dy)];
519 [self releaseVideoSpecific];
520 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[0], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[0]);
521 if(error != kCVReturnSuccess)
522 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create Pixel Buffer(%d)\n", error);
523 if (vo_doublebuffering) {
524 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[1], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[1]);
525 if(error != kCVReturnSuccess)
526 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create Pixel Double Buffer(%d)\n", error);
529 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
530 if(error != kCVReturnSuccess)
531 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture(%d)\n", error);
534 [window makeKeyAndOrderFront:mpGLView];
540 [mpGLView fullscreen: NO];
551 NSMenu *menu, *aspectMenu;
552 NSMenuItem *menuItem;
554 menu = [[NSMenu new] autorelease];
555 menuItem = [[NSMenuItem new] autorelease];
556 [menu addItem: menuItem];
557 [NSApp setMainMenu: menu];
560 menu = [[NSMenu alloc] initWithTitle:@"Movie"];
561 menuItem = [[NSMenuItem alloc] initWithTitle:@"Half Size" action:@selector(menuAction:) keyEquivalent:@"0"]; [menu addItem:menuItem];
562 kHalfScreenCmd = menuItem;
563 menuItem = [[NSMenuItem alloc] initWithTitle:@"Normal Size" action:@selector(menuAction:) keyEquivalent:@"1"]; [menu addItem:menuItem];
564 kNormalScreenCmd = menuItem;
565 menuItem = [[NSMenuItem alloc] initWithTitle:@"Double Size" action:@selector(menuAction:) keyEquivalent:@"2"]; [menu addItem:menuItem];
566 kDoubleScreenCmd = menuItem;
567 menuItem = [[NSMenuItem alloc] initWithTitle:@"Full Size" action:@selector(menuAction:) keyEquivalent:@"f"]; [menu addItem:menuItem];
568 kFullScreenCmd = menuItem;
569 menuItem = [NSMenuItem separatorItem]; [menu addItem:menuItem];
571 aspectMenu = [[NSMenu alloc] initWithTitle:@"Aspect Ratio"];
572 menuItem = [[NSMenuItem alloc] initWithTitle:@"Keep" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
573 if(vo_keepaspect) [menuItem setState:NSOnState];
574 kKeepAspectCmd = menuItem;
575 menuItem = [[NSMenuItem alloc] initWithTitle:@"Pan-Scan" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
576 if(vo_panscan) [menuItem setState:NSOnState];
577 kPanScanCmd = menuItem;
578 menuItem = [NSMenuItem separatorItem]; [aspectMenu addItem:menuItem];
579 menuItem = [[NSMenuItem alloc] initWithTitle:@"Original" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
580 kAspectOrgCmd = menuItem;
581 menuItem = [[NSMenuItem alloc] initWithTitle:@"4:3" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
582 kAspectFullCmd = menuItem;
583 menuItem = [[NSMenuItem alloc] initWithTitle:@"16:9" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
584 kAspectWideCmd = menuItem;
585 menuItem = [[NSMenuItem alloc] initWithTitle:@"Aspect Ratio" action:nil keyEquivalent:@""];
586 [menuItem setSubmenu:aspectMenu];
587 [menu addItem:menuItem];
588 [aspectMenu release];
591 menuItem = [[NSMenuItem alloc] initWithTitle:@"Movie" action:nil keyEquivalent:@""];
592 [menuItem setSubmenu:menu];
593 [[NSApp mainMenu] addItem:menuItem];
596 menu = [[NSMenu alloc] initWithTitle:@"Window"];
598 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; [menu addItem:menuItem];
599 menuItem = [[NSMenuItem alloc] initWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""]; [menu addItem:menuItem];
602 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
603 [menuItem setSubmenu:menu];
604 [[NSApp mainMenu] addItem:menuItem];
605 [NSApp setWindowsMenu:menu];
611 - (void)set_winSizeMult:(float)mult
614 int d_width, d_height;
615 aspect(&d_width, &d_height, A_NOZOOM);
619 [self fullscreen:NO];
623 frame.size.width = d_width * mult;
624 frame.size.height = d_height * mult;
625 [window setContentSize: frame.size];
632 - (void)menuAction:(id)sender
634 if(sender == kHalfScreenCmd)
635 [self set_winSizeMult: 0.5];
636 if(sender == kNormalScreenCmd)
637 [self set_winSizeMult: 1];
638 if(sender == kDoubleScreenCmd)
639 [self set_winSizeMult: 2];
640 if(sender == kFullScreenCmd)
643 [self fullscreen:NO];
646 if(sender == kKeepAspectCmd)
648 vo_keepaspect = !vo_keepaspect;
650 [kKeepAspectCmd setState:NSOnState];
652 [kKeepAspectCmd setState:NSOffState];
657 if(sender == kPanScanCmd)
659 vo_panscan = !vo_panscan;
661 [kPanScanCmd setState:NSOnState];
663 [kPanScanCmd setState:NSOffState];
668 if(sender == kAspectOrgCmd)
669 change_movie_aspect(-1);
671 if(sender == kAspectFullCmd)
672 change_movie_aspect(4.0f/3.0f);
674 if(sender == kAspectWideCmd)
675 change_movie_aspect(16.0f/9.0f);
681 - (void)prepareOpenGL
684 glDisable(GL_DEPTH_TEST);
685 glDepthMask(GL_FALSE);
686 glDisable(GL_CULL_FACE);
691 reshape OpenGL viewport
695 int d_width, d_height;
697 NSRect frame = [self frame];
698 vo_dwidth = frame.size.width;
699 vo_dheight = frame.size.height;
701 glViewport(0, 0, frame.size.width, frame.size.height);
702 glMatrixMode(GL_PROJECTION);
704 glOrtho(0, frame.size.width, frame.size.height, 0, -1.0, 1.0);
705 glMatrixMode(GL_MODELVIEW);
711 aspect(&d_width, &d_height, A_WINZOOM);
713 textureFrame = NSMakeRect((vo_dwidth - d_width) / 2, (vo_dheight - d_height) / 2, d_width, d_height);
717 textureFrame = frame;
726 glClear(GL_COLOR_BUFFER_BIT);
728 glEnable(CVOpenGLTextureGetTarget(texture));
729 glBindTexture(CVOpenGLTextureGetTarget(texture), CVOpenGLTextureGetName(texture));
733 glTexCoord2f(upperLeft[0], upperLeft[1]); glVertex2i( textureFrame.origin.x-(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
734 glTexCoord2f(lowerLeft[0], lowerLeft[1]); glVertex2i(textureFrame.origin.x-(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
735 glTexCoord2f(lowerRight[0], lowerRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
736 glTexCoord2f(upperRight[0], upperRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
738 glDisable(CVOpenGLTextureGetTarget(texture));
743 NSRect frame = [self frame];
746 glColor4f(0.2, 0.2, 0.2, 0.5);
747 glVertex2i(frame.size.width-1, frame.size.height-1); glVertex2i(frame.size.width-1, frame.size.height-1);
748 glVertex2i(frame.size.width-1, frame.size.height-5); glVertex2i(frame.size.width-5, frame.size.height-1);
749 glVertex2i(frame.size.width-1, frame.size.height-9); glVertex2i(frame.size.width-9, frame.size.height-1);
751 glColor4f(0.4, 0.4, 0.4, 0.5);
752 glVertex2i(frame.size.width-1, frame.size.height-2); glVertex2i(frame.size.width-2, frame.size.height-1);
753 glVertex2i(frame.size.width-1, frame.size.height-6); glVertex2i(frame.size.width-6, frame.size.height-1);
754 glVertex2i(frame.size.width-1, frame.size.height-10); glVertex2i(frame.size.width-10, frame.size.height-1);
756 glColor4f(0.6, 0.6, 0.6, 0.5);
757 glVertex2i(frame.size.width-1, frame.size.height-3); glVertex2i(frame.size.width-3, frame.size.height-1);
758 glVertex2i(frame.size.width-1, frame.size.height-7); glVertex2i(frame.size.width-7, frame.size.height-1);
759 glVertex2i(frame.size.width-1, frame.size.height-11); glVertex2i(frame.size.width-11, frame.size.height-1);
767 Create OpenGL texture from current frame & set texco
769 - (void) setCurrentTexture
771 CVReturn error = kCVReturnSuccess;
773 CVOpenGLTextureRelease(texture);
774 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
775 if(error != kCVReturnSuccess)
776 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture(%d)\n", error);
778 CVOpenGLTextureGetCleanTexCoords(texture, lowerLeft, lowerRight, upperRight, upperLeft);
784 - (void) drawRect: (NSRect *) bounds
792 - (void) fullscreen: (BOOL) animate
794 static NSRect old_frame;
795 static NSRect old_view_frame;
804 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
805 CGDisplayHideCursor(kCGDirectMainDisplay);
809 old_frame = [window frame]; //save main window size & position
810 update_screen_info();
812 [window setFrame:screen_frame display:YES animate:animate]; //zoom-in window with nice useless sfx
813 old_view_frame = [self bounds];
815 //fix origin for multi screen setup
816 screen_frame.origin.x = 0;
817 screen_frame.origin.y = 0;
818 [self setFrame:screen_frame];
819 [self setNeedsDisplay:YES];
820 [window setHasShadow:NO];
825 SetSystemUIMode( kUIModeNormal, 0);
828 CGDisplayShowCursor(kCGDirectMainDisplay);
831 //revert window to previous setting
832 [self setFrame:old_view_frame];
833 [self setNeedsDisplay:YES];
834 [window setHasShadow:YES];
835 [window setFrame:old_frame display:YES animate:animate];//zoom-out window with nice useless sfx
846 [window setLevel:NSScreenSaverWindowLevel];
851 [window setLevel:NSNormalWindowLevel];
871 [window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey)];
872 [window orderBack:self];
877 [window setLevel:NSNormalWindowLevel];
883 Check event for new event
885 - (void) check_events
888 int curTime = TickCount()/60;
890 //automatically hide mouse cursor (and future on-screen control?)
891 if(isFullscreen && !mouseHide && !isRootwin)
893 if(curTime - lastMouseHide >= 5 || lastMouseHide == 0)
895 CGDisplayHideCursor(kCGDirectMainDisplay);
897 lastMouseHide = curTime;
901 //update activity every 30 seconds to prevent
902 //screensaver from starting up.
903 if(curTime - lastScreensaverUpdate >= 30 || lastScreensaverUpdate == 0)
905 UpdateSystemActivity(UsrActivity);
906 lastScreensaverUpdate = curTime;
909 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSEventTrackingRunLoopMode dequeue:YES];
912 [NSApp sendEvent:event];
913 // Without SDL's bootstrap code (include SDL.h in mplayer.c),
914 // on Leopard, we have trouble to get the play window automatically focused
915 // when the app is actived. The Following code fix this problem.
917 if (isLeopardOrLater && [event type] == NSAppKitDefined
918 && [event subtype] == NSApplicationActivatedEventType) {
919 [window makeMainWindow];
920 [window makeKeyAndOrderFront:mpGLView];
926 From NSView, respond to key equivalents.
928 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
930 switch([theEvent keyCode])
932 case 0x21: [window setAlphaValue: winAlpha-=0.05]; return YES;
933 case 0x1e: [window setAlphaValue: winAlpha+=0.05]; return YES;
941 - (void) keyDown: (NSEvent *) theEvent
943 int key = convert_key([theEvent keyCode], *[[theEvent characters] UTF8String]);
945 if([theEvent modifierFlags] & NSShiftKeyMask)
946 key |= KEY_MODIFIER_SHIFT;
947 if([theEvent modifierFlags] & NSControlKeyMask)
948 key |= KEY_MODIFIER_CTRL;
949 if(([theEvent modifierFlags] & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask)
950 key |= KEY_MODIFIER_ALT;
951 if([theEvent modifierFlags] & NSCommandKeyMask)
952 key |= KEY_MODIFIER_META;
953 mplayer_put_key(key);
958 Process mouse button event
960 - (void) mouseMoved: (NSEvent *) theEvent
962 if(isFullscreen && !isRootwin)
964 CGDisplayShowCursor(kCGDirectMainDisplay);
967 if (enable_mouse_movements && !isRootwin) {
968 NSPoint p =[self convertPoint:[theEvent locationInWindow] fromView:nil];
969 if ([self mouse:p inRect:textureFrame]) {
970 vo_mouse_movement(global_vo, vo_fs ? p.x : p.x - textureFrame.origin.x,
971 vo_fs ? [self frame].size.height - p.y : NSMaxY(textureFrame) - p.y);
976 - (void) mouseDown: (NSEvent *) theEvent
978 [self mouseEvent: theEvent];
981 - (void) mouseUp: (NSEvent *) theEvent
983 [self mouseEvent: theEvent];
986 - (void) rightMouseDown: (NSEvent *) theEvent
988 [self mouseEvent: theEvent];
991 - (void) rightMouseUp: (NSEvent *) theEvent
993 [self mouseEvent: theEvent];
996 - (void) otherMouseDown: (NSEvent *) theEvent
998 [self mouseEvent: theEvent];
1001 - (void) otherMouseUp: (NSEvent *) theEvent
1003 [self mouseEvent: theEvent];
1006 - (void) scrollWheel: (NSEvent *) theEvent
1008 if([theEvent deltaY] > 0)
1009 mplayer_put_key(MOUSE_BTN3);
1011 mplayer_put_key(MOUSE_BTN4);
1014 - (void) mouseEvent: (NSEvent *) theEvent
1016 if ( [theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9 )
1018 int buttonNumber = [theEvent buttonNumber];
1019 // Fix to mplayer defined button order: left, middle, right
1020 if (buttonNumber == 1)
1022 else if (buttonNumber == 2)
1024 switch([theEvent type])
1026 case NSLeftMouseDown:
1027 case NSRightMouseDown:
1028 case NSOtherMouseDown:
1029 mplayer_put_key((MOUSE_BTN0 + buttonNumber) | MP_KEY_DOWN);
1032 case NSRightMouseUp:
1033 case NSOtherMouseUp:
1034 mplayer_put_key(MOUSE_BTN0 + buttonNumber);
1043 - (BOOL) acceptsFirstResponder
1048 - (BOOL) becomeFirstResponder
1053 - (BOOL) resignFirstResponder
1058 - (BOOL)windowShouldClose:(id)sender
1060 mplayer_put_key(KEY_CLOSE_WIN);
1061 // We have to wait for MPlayer to handle this,
1062 // otherwise we are in trouble if the
1063 // KEY_CLOSE_WIN handler is disabled
1067 - (void)handleQuitEvent:(NSAppleEventDescriptor*)e withReplyEvent:(NSAppleEventDescriptor*)r
1069 mplayer_put_key(KEY_CLOSE_WIN);