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"
43 #include "libvo/sub.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 static vo_info_t info =
94 "Mac OS X Core Video",
96 "Nicolas Plourde <nicolas.plourde@gmail.com>",
100 LIBVO_EXTERN(corevideo)
102 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
104 switch (image_format)
107 vo_draw_alpha_rgb24(w,h,src,srca,stride,image_data+3*(y0*image_width+x0),3*image_width);
111 vo_draw_alpha_rgb32(w,h,src,srca,stride,image_data+4*(y0*image_width+x0),4*image_width);
114 vo_draw_alpha_yuy2(w,h,src,srca,stride,image_data + (x0 + y0 * image_width) * 2,image_width*2);
119 static void update_screen_info(void)
121 if (screen_id == -1 && xinerama_screen > -1)
122 screen_id = xinerama_screen;
124 screen_array = [NSScreen screens];
125 if(screen_id >= (int)[screen_array count])
127 mp_msg(MSGT_VO, MSGL_INFO, "[vo_corevideo] Device ID %d does not exist, falling back to main device\n", screen_id);
130 if (screen_id < 0 && [mpGLView window])
131 screen_handle = [[mpGLView window] screen];
133 screen_handle = [screen_array objectAtIndex:(screen_id < 0 ? 0 : screen_id)];
135 screen_frame = [screen_handle frame];
136 vo_screenwidth = screen_frame.size.width;
137 vo_screenheight = screen_frame.size.height;
138 xinerama_x = xinerama_y = 0;
139 aspect_save_screenres(vo_screenwidth, vo_screenheight);
142 static void free_file_specific(void)
146 [mplayerosxProto stop];
147 mplayerosxProto = nil;
148 [mplayerosxProxy release];
149 mplayerosxProxy = nil;
151 if (munmap(image_data, image_width*image_height*image_bytes) == -1)
152 mp_msg(MSGT_VO, MSGL_FATAL, "[vo_corevideo] uninit: munmap failed. Error: %s\n", strerror(errno));
154 if (shm_unlink(buffer_name) == -1)
155 mp_msg(MSGT_VO, MSGL_FATAL, "[vo_corevideo] uninit: shm_unlink failed. Error: %s\n", strerror(errno));
157 free(image_datas[0]);
158 if (vo_doublebuffering)
159 free(image_datas[1]);
160 image_datas[0] = NULL;
161 image_datas[1] = NULL;
166 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)
168 free_file_specific();
172 image_height = height;
173 switch (image_format)
186 image_bytes = (image_depth + 7) / 8;
190 config_movie_aspect((float)d_width/d_height);
192 vo_dwidth = d_width *= mpGLView->winSizeMult;
193 vo_dheight = d_height *= mpGLView->winSizeMult;
195 image_data = malloc(image_width*image_height*image_bytes);
196 image_datas[0] = image_data;
197 if (vo_doublebuffering)
198 image_datas[1] = malloc(image_width*image_height*image_bytes);
201 vo_fs = flags & VOFLAG_FULLSCREEN;
210 mp_msg(MSGT_VO, MSGL_INFO, "[vo_corevideo] writing output to a shared buffer "
211 "named \"%s\"\n",buffer_name);
213 // create shared memory
214 shm_fd = shm_open(buffer_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
217 mp_msg(MSGT_VO, MSGL_FATAL,
218 "[vo_corevideo] failed to open shared memory. Error: %s\n", strerror(errno));
223 if (ftruncate(shm_fd, image_width*image_height*image_bytes) == -1)
225 mp_msg(MSGT_VO, MSGL_FATAL,
226 "[vo_corevideo] failed to size shared memory, possibly already in use. Error: %s\n", strerror(errno));
228 shm_unlink(buffer_name);
232 image_data = mmap(NULL, image_width*image_height*image_bytes,
233 PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
236 if (image_data == MAP_FAILED)
238 mp_msg(MSGT_VO, MSGL_FATAL,
239 "[vo_corevideo] failed to map shared memory. Error: %s\n", strerror(errno));
240 shm_unlink(buffer_name);
244 //connect to mplayerosx
245 mplayerosxProxy=[NSConnection rootProxyForConnectionWithRegisteredName:[NSString stringWithCString:buffer_name] host:nil];
246 if ([mplayerosxProxy conformsToProtocol:@protocol(MPlayerOSXVOProto)]) {
247 [mplayerosxProxy setProtocolForProxy:@protocol(MPlayerOSXVOProto)];
248 mplayerosxProto = (id <MPlayerOSXVOProto>)mplayerosxProxy;
249 [mplayerosxProto startWithWidth: image_width withHeight: image_height withBytes: image_bytes withAspect:d_width*100/d_height];
252 [mplayerosxProxy release];
253 mplayerosxProxy = nil;
254 mplayerosxProto = nil;
260 static void check_events(void)
263 [mpGLView check_events];
266 static void draw_osd(void)
268 vo_draw_text(image_width, image_height, draw_alpha);
271 static void flip_page(void)
274 NSAutoreleasePool *pool = [NSAutoreleasePool new];
275 [mplayerosxProto render];
278 [mpGLView setCurrentTexture];
280 if (vo_doublebuffering) {
281 image_page = 1 - image_page;
282 image_data = image_datas[image_page];
287 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
293 static int draw_frame(uint8_t *src[])
298 static uint32_t draw_image(mp_image_t *mpi)
300 memcpy_pic(image_data, mpi->planes[0], image_width*image_bytes, image_height, image_width*image_bytes, mpi->stride[0]);
305 static int query_format(uint32_t format)
307 const int supportflags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
308 image_format = format;
313 pixelFormat = kYUVSPixelFormat;
317 pixelFormat = k24RGBPixelFormat;
321 pixelFormat = k32ARGBPixelFormat;
325 pixelFormat = k32BGRAPixelFormat;
331 static void uninit(void)
333 SetSystemUIMode( kUIModeNormal, 0);
334 CGDisplayShowCursor(kCGDirectMainDisplay);
336 free_file_specific();
340 NSAutoreleasePool *finalPool;
342 [autoreleasepool release];
343 finalPool = [[NSAutoreleasePool alloc] init];
344 [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
348 if (buffer_name) free(buffer_name);
352 static const opt_t subopts[] = {
353 {"device_id", OPT_ARG_INT, &screen_id, NULL},
354 {"shared_buffer", OPT_ARG_BOOL, &shared_buffer, NULL},
355 {"buffer_name", OPT_ARG_MSTRZ,&buffer_name, NULL},
359 static int preinit(const char *arg)
364 shared_buffer = false;
367 if (subopt_parse(arg, subopts) != 0) {
368 mp_msg(MSGT_VO, MSGL_FATAL,
369 "\n-vo corevideo command line help:\n"
370 "Example: mplayer -vo corevideo:device_id=1:shared_buffer:buffer_name=mybuff\n"
372 " device_id=<0-...>\n"
373 " Set screen device ID for fullscreen.\n"
375 " Write output to a shared memory buffer instead of displaying it.\n"
376 " buffer_name=<name>\n"
377 " Name of the shared buffer created with shm_open() as well as\n"
378 " the name of the NSConnection MPlayer will try to open.\n"
379 " Setting buffer_name implicitly enables shared_buffer.\n"
384 autoreleasepool = [[NSAutoreleasePool alloc] init];
387 buffer_name = strdup(DEFAULT_BUFFER_NAME);
389 shared_buffer = true;
394 NSApp = [NSApplication sharedApplication];
395 isLeopardOrLater = floor(NSAppKitVersionNumber) > 824;
397 osx_foreground_hack();
401 mpGLView = [[MPlayerOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat:[MPlayerOpenGLView defaultPixelFormat]];
402 [mpGLView autorelease];
412 static int control(uint32_t request, void *data)
416 case VOCTRL_DRAW_IMAGE: return draw_image(data);
417 case VOCTRL_PAUSE: return int_pause = 1;
418 case VOCTRL_RESUME: return int_pause = 0;
419 case VOCTRL_QUERY_FORMAT: return query_format(*(uint32_t*)data);
420 case VOCTRL_ONTOP: vo_ontop = !vo_ontop; if(!shared_buffer){ [mpGLView ontop]; } else { [mplayerosxProto ontop]; } return VO_TRUE;
421 case VOCTRL_ROOTWIN: vo_rootwin = !vo_rootwin; [mpGLView rootwin]; return VO_TRUE;
422 case VOCTRL_FULLSCREEN: vo_fs = !vo_fs; if(!shared_buffer){ [mpGLView fullscreen: NO]; } else { [mplayerosxProto toggleFullscreen]; } return VO_TRUE;
423 case VOCTRL_GET_PANSCAN: return VO_TRUE;
424 case VOCTRL_SET_PANSCAN: [mpGLView panscan]; return VO_TRUE;
425 case VOCTRL_UPDATE_SCREENINFO: update_screen_info(); return VO_TRUE;
430 //////////////////////////////////////////////////////////////////////////
431 // NSOpenGLView Subclass
432 //////////////////////////////////////////////////////////////////////////
433 @implementation MPlayerOpenGLView
436 NSOpenGLContext *glContext;
437 GLint swapInterval = 1;
444 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
445 styleMask:NSTitledWindowMask|NSTexturedBackgroundWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
446 backing:NSBackingStoreBuffered defer:NO];
448 [window autorelease];
449 [window setDelegate:mpGLView];
450 [window setContentView:mpGLView];
451 [window setInitialFirstResponder:mpGLView];
452 [window setAcceptsMouseMovedEvents:YES];
453 [window setTitle:@"MPlayer - The Movie Player"];
458 //create OpenGL Context
459 glContext = [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil];
461 [self setOpenGLContext:glContext];
462 [glContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
463 [glContext setView:self];
464 [glContext makeCurrentContext];
467 error = CVOpenGLTextureCacheCreate(NULL, 0, [glContext CGLContextObj], [[self pixelFormat] CGLPixelFormatObj], 0, &textureCache);
468 if(error != kCVReturnSuccess)
469 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture Cache(%d)\n", error);
472 - (void) releaseVideoSpecific
474 CVPixelBufferRelease(frameBuffers[0]);
475 frameBuffers[0] = NULL;
476 CVPixelBufferRelease(frameBuffers[1]);
477 frameBuffers[1] = NULL;
478 CVOpenGLTextureRelease(texture);
484 [self releaseVideoSpecific];
485 CVOpenGLTextureCacheRelease(textureCache);
487 [self setOpenGLContext:nil];
494 CVReturn error = kCVReturnSuccess;
497 [window setContentSize:NSMakeSize(vo_dwidth, vo_dheight)];
499 // Use visibleFrame to position the window taking the menu bar and dock into account.
500 // Also flip vo_dy since the screen origin is in the bottom left on OSX.
501 update_screen_info();
502 visibleFrame = [screen_handle visibleFrame];
503 [window setFrameTopLeftPoint:NSMakePoint(
504 visibleFrame.origin.x + vo_dx,
505 visibleFrame.origin.y + visibleFrame.size.height - vo_dy)];
507 [self releaseVideoSpecific];
508 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[0], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[0]);
509 if(error != kCVReturnSuccess)
510 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create Pixel Buffer(%d)\n", error);
511 if (vo_doublebuffering) {
512 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[1], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[1]);
513 if(error != kCVReturnSuccess)
514 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create Pixel Double Buffer(%d)\n", error);
517 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
518 if(error != kCVReturnSuccess)
519 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture(%d)\n", error);
522 [window makeKeyAndOrderFront:mpGLView];
528 [mpGLView fullscreen: NO];
539 NSMenu *menu, *aspectMenu;
540 NSMenuItem *menuItem;
542 [NSApp setMainMenu:[[NSMenu alloc] init]];
545 menu = [[NSMenu alloc] initWithTitle:@"Movie"];
546 menuItem = [[NSMenuItem alloc] initWithTitle:@"Half Size" action:@selector(menuAction:) keyEquivalent:@"0"]; [menu addItem:menuItem];
547 kHalfScreenCmd = menuItem;
548 menuItem = [[NSMenuItem alloc] initWithTitle:@"Normal Size" action:@selector(menuAction:) keyEquivalent:@"1"]; [menu addItem:menuItem];
549 kNormalScreenCmd = menuItem;
550 menuItem = [[NSMenuItem alloc] initWithTitle:@"Double Size" action:@selector(menuAction:) keyEquivalent:@"2"]; [menu addItem:menuItem];
551 kDoubleScreenCmd = menuItem;
552 menuItem = [[NSMenuItem alloc] initWithTitle:@"Full Size" action:@selector(menuAction:) keyEquivalent:@"f"]; [menu addItem:menuItem];
553 kFullScreenCmd = menuItem;
554 menuItem = [NSMenuItem separatorItem]; [menu addItem:menuItem];
556 aspectMenu = [[NSMenu alloc] initWithTitle:@"Aspect Ratio"];
557 menuItem = [[NSMenuItem alloc] initWithTitle:@"Keep" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
558 if(vo_keepaspect) [menuItem setState:NSOnState];
559 kKeepAspectCmd = menuItem;
560 menuItem = [[NSMenuItem alloc] initWithTitle:@"Pan-Scan" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
561 if(vo_panscan) [menuItem setState:NSOnState];
562 kPanScanCmd = menuItem;
563 menuItem = [NSMenuItem separatorItem]; [aspectMenu addItem:menuItem];
564 menuItem = [[NSMenuItem alloc] initWithTitle:@"Original" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
565 kAspectOrgCmd = menuItem;
566 menuItem = [[NSMenuItem alloc] initWithTitle:@"4:3" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
567 kAspectFullCmd = menuItem;
568 menuItem = [[NSMenuItem alloc] initWithTitle:@"16:9" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
569 kAspectWideCmd = menuItem;
570 menuItem = [[NSMenuItem alloc] initWithTitle:@"Aspect Ratio" action:nil keyEquivalent:@""];
571 [menuItem setSubmenu:aspectMenu];
572 [menu addItem:menuItem];
573 [aspectMenu release];
576 menuItem = [[NSMenuItem alloc] initWithTitle:@"Movie" action:nil keyEquivalent:@""];
577 [menuItem setSubmenu:menu];
578 [[NSApp mainMenu] addItem:menuItem];
581 menu = [[NSMenu alloc] initWithTitle:@"Window"];
583 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; [menu addItem:menuItem];
584 menuItem = [[NSMenuItem alloc] initWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""]; [menu addItem:menuItem];
587 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
588 [menuItem setSubmenu:menu];
589 [[NSApp mainMenu] addItem:menuItem];
590 [NSApp setWindowsMenu:menu];
596 - (void)set_winSizeMult:(float)mult
599 int d_width, d_height;
600 aspect(&d_width, &d_height, A_NOZOOM);
604 [self fullscreen:NO];
608 frame.size.width = d_width * mult;
609 frame.size.height = d_height * mult;
610 [window setContentSize: frame.size];
617 - (void)menuAction:(id)sender
619 if(sender == kQuitCmd)
621 mplayer_put_key(KEY_CLOSE_WIN);
624 if(sender == kHalfScreenCmd)
625 [self set_winSizeMult: 0.5];
626 if(sender == kNormalScreenCmd)
627 [self set_winSizeMult: 1];
628 if(sender == kDoubleScreenCmd)
629 [self set_winSizeMult: 2];
630 if(sender == kFullScreenCmd)
633 [self fullscreen:NO];
636 if(sender == kKeepAspectCmd)
638 vo_keepaspect = !vo_keepaspect;
640 [kKeepAspectCmd setState:NSOnState];
642 [kKeepAspectCmd setState:NSOffState];
647 if(sender == kPanScanCmd)
649 vo_panscan = !vo_panscan;
651 [kPanScanCmd setState:NSOnState];
653 [kPanScanCmd setState:NSOffState];
658 if(sender == kAspectOrgCmd)
659 change_movie_aspect(-1);
661 if(sender == kAspectFullCmd)
662 change_movie_aspect(4.0f/3.0f);
664 if(sender == kAspectWideCmd)
665 change_movie_aspect(16.0f/9.0f);
671 - (void)prepareOpenGL
674 glDisable(GL_DEPTH_TEST);
675 glDepthMask(GL_FALSE);
676 glDisable(GL_CULL_FACE);
681 reshape OpenGL viewport
685 int d_width, d_height;
687 NSRect frame = [self frame];
688 vo_dwidth = frame.size.width;
689 vo_dheight = frame.size.height;
691 glViewport(0, 0, frame.size.width, frame.size.height);
692 glMatrixMode(GL_PROJECTION);
694 glOrtho(0, frame.size.width, frame.size.height, 0, -1.0, 1.0);
695 glMatrixMode(GL_MODELVIEW);
701 aspect(&d_width, &d_height, A_WINZOOM);
703 textureFrame = NSMakeRect((vo_dwidth - d_width) / 2, (vo_dheight - d_height) / 2, d_width, d_height);
707 textureFrame = frame;
718 glClear(GL_COLOR_BUFFER_BIT);
720 glEnable(CVOpenGLTextureGetTarget(texture));
721 glBindTexture(CVOpenGLTextureGetTarget(texture), CVOpenGLTextureGetName(texture));
725 glTexCoord2f(upperLeft[0], upperLeft[1]); glVertex2i( textureFrame.origin.x-(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
726 glTexCoord2f(lowerLeft[0], lowerLeft[1]); glVertex2i(textureFrame.origin.x-(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
727 glTexCoord2f(lowerRight[0], lowerRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
728 glTexCoord2f(upperRight[0], upperRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
730 glDisable(CVOpenGLTextureGetTarget(texture));
735 NSRect frame = [self frame];
738 glColor4f(0.2, 0.2, 0.2, 0.5);
739 glVertex2i(frame.size.width-1, frame.size.height-1); glVertex2i(frame.size.width-1, frame.size.height-1);
740 glVertex2i(frame.size.width-1, frame.size.height-5); glVertex2i(frame.size.width-5, frame.size.height-1);
741 glVertex2i(frame.size.width-1, frame.size.height-9); glVertex2i(frame.size.width-9, frame.size.height-1);
743 glColor4f(0.4, 0.4, 0.4, 0.5);
744 glVertex2i(frame.size.width-1, frame.size.height-2); glVertex2i(frame.size.width-2, frame.size.height-1);
745 glVertex2i(frame.size.width-1, frame.size.height-6); glVertex2i(frame.size.width-6, frame.size.height-1);
746 glVertex2i(frame.size.width-1, frame.size.height-10); glVertex2i(frame.size.width-10, frame.size.height-1);
748 glColor4f(0.6, 0.6, 0.6, 0.5);
749 glVertex2i(frame.size.width-1, frame.size.height-3); glVertex2i(frame.size.width-3, frame.size.height-1);
750 glVertex2i(frame.size.width-1, frame.size.height-7); glVertex2i(frame.size.width-7, frame.size.height-1);
751 glVertex2i(frame.size.width-1, frame.size.height-11); glVertex2i(frame.size.width-11, frame.size.height-1);
757 curTime = TickCount()/60;
759 //automatically hide mouse cursor (and future on-screen control?)
760 if(isFullscreen && !mouseHide && !isRootwin)
762 if(curTime - lastMouseHide >= 5 || lastMouseHide == 0)
764 CGDisplayHideCursor(kCGDirectMainDisplay);
766 lastMouseHide = curTime;
770 //update activity every 30 seconds to prevent
771 //screensaver from starting up.
772 if(curTime - lastScreensaverUpdate >= 30 || lastScreensaverUpdate == 0)
774 UpdateSystemActivity(UsrActivity);
775 lastScreensaverUpdate = curTime;
780 Create OpenGL texture from current frame & set texco
782 - (void) setCurrentTexture
784 CVReturn error = kCVReturnSuccess;
786 CVOpenGLTextureRelease(texture);
787 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
788 if(error != kCVReturnSuccess)
789 mp_msg(MSGT_VO, MSGL_ERR,"[vo_corevideo] Failed to create OpenGL texture(%d)\n", error);
791 CVOpenGLTextureGetCleanTexCoords(texture, lowerLeft, lowerRight, upperRight, upperLeft);
797 - (void) drawRect: (NSRect *) bounds
805 - (void) fullscreen: (BOOL) animate
807 static NSRect old_frame;
808 static NSRect old_view_frame;
817 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
818 CGDisplayHideCursor(kCGDirectMainDisplay);
822 old_frame = [window frame]; //save main window size & position
823 update_screen_info();
825 [window setFrame:screen_frame display:YES animate:animate]; //zoom-in window with nice useless sfx
826 old_view_frame = [self bounds];
828 //fix origin for multi screen setup
829 screen_frame.origin.x = 0;
830 screen_frame.origin.y = 0;
831 [self setFrame:screen_frame];
832 [self setNeedsDisplay:YES];
833 [window setHasShadow:NO];
838 SetSystemUIMode( kUIModeNormal, 0);
841 CGDisplayShowCursor(kCGDirectMainDisplay);
844 //revert window to previous setting
845 [self setFrame:old_view_frame];
846 [self setNeedsDisplay:YES];
847 [window setHasShadow:YES];
848 [window setFrame:old_frame display:YES animate:animate];//zoom-out window with nice useless sfx
859 [window setLevel:NSScreenSaverWindowLevel];
864 [window setLevel:NSNormalWindowLevel];
884 [window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey)];
885 [window orderBack:self];
890 [window setLevel:NSNormalWindowLevel];
896 Check event for new event
898 - (void) check_events
900 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate dateWithTimeIntervalSinceNow:0.0001] inMode:NSEventTrackingRunLoopMode dequeue:YES];
903 [NSApp sendEvent:event];
904 // Without SDL's bootstrap code (include SDL.h in mplayer.c),
905 // on Leopard, we have trouble to get the play window automatically focused
906 // when the app is actived. The Following code fix this problem.
908 if (isLeopardOrLater && [event type] == NSAppKitDefined
909 && [event subtype] == NSApplicationActivatedEventType) {
910 [window makeMainWindow];
911 [window makeKeyAndOrderFront:mpGLView];
917 From NSView, respond to key equivalents.
919 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
921 switch([theEvent keyCode])
923 case 0x21: [window setAlphaValue: winAlpha-=0.05]; return YES;
924 case 0x1e: [window setAlphaValue: winAlpha+=0.05]; return YES;
932 - (void) keyDown: (NSEvent *) theEvent
934 int key = convert_key([theEvent keyCode], *[[theEvent characters] UTF8String]);
936 mplayer_put_key(key);
940 Process mouse button event
942 - (void) mouseMoved: (NSEvent *) theEvent
944 if(isFullscreen && !isRootwin)
946 CGDisplayShowCursor(kCGDirectMainDisplay);
949 if (enable_mouse_movements && !isRootwin) {
950 NSPoint p =[self convertPoint:[theEvent locationInWindow] fromView:nil];
951 if ([self mouse:p inRect:textureFrame]) {
952 vo_mouse_movement(global_vo, vo_fs ? p.x : p.x - textureFrame.origin.x,
953 vo_fs ? [self frame].size.height - p.y : NSMaxY(textureFrame) - p.y);
958 - (void) mouseDown: (NSEvent *) theEvent
960 [self mouseEvent: theEvent];
963 - (void) mouseUp: (NSEvent *) theEvent
965 [self mouseEvent: theEvent];
968 - (void) rightMouseDown: (NSEvent *) theEvent
970 [self mouseEvent: theEvent];
973 - (void) rightMouseUp: (NSEvent *) theEvent
975 [self mouseEvent: theEvent];
978 - (void) otherMouseDown: (NSEvent *) theEvent
980 [self mouseEvent: theEvent];
983 - (void) otherMouseUp: (NSEvent *) theEvent
985 [self mouseEvent: theEvent];
988 - (void) scrollWheel: (NSEvent *) theEvent
990 if([theEvent deltaY] > 0)
991 mplayer_put_key(MOUSE_BTN3);
993 mplayer_put_key(MOUSE_BTN4);
996 - (void) mouseEvent: (NSEvent *) theEvent
998 if ( [theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9 )
1000 int buttonNumber = [theEvent buttonNumber];
1001 // Fix to mplayer defined button order: left, middle, right
1002 if (buttonNumber == 1)
1004 else if (buttonNumber == 2)
1006 switch([theEvent type])
1008 case NSLeftMouseDown:
1009 case NSRightMouseDown:
1010 case NSOtherMouseDown:
1011 mplayer_put_key((MOUSE_BTN0 + buttonNumber) | MP_KEY_DOWN);
1014 case NSRightMouseUp:
1015 case NSOtherMouseUp:
1016 mplayer_put_key(MOUSE_BTN0 + buttonNumber);
1025 - (BOOL) acceptsFirstResponder
1030 - (BOOL) becomeFirstResponder
1035 - (BOOL) resignFirstResponder
1040 - (void)windowWillClose:(NSNotification *)aNotification
1043 mplayer_put_key(KEY_CLOSE_WIN);