3 by Nicolas Plourde <nicolasplourde@gmail.com>
5 MPlayer Mac OSX video out module.
6 Copyright (c) Nicolas Plourde - 2005
10 #include <sys/types.h>
14 #include <CoreServices/CoreServices.h>
15 //special workaround for Apple bug #6267445
16 //(OSServices Power API disabled in OSServices.h for 64bit systems)
18 #include <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
23 #include "fastmemcpy.h"
24 #include "video_out.h"
25 #include "video_out_internal.h"
30 #include "libvo/sub.h"
31 #include "subopt-helper.h"
33 #include "input/input.h"
34 #include "input/mouse.h"
36 #include "osdep/keycodes.h"
40 NSDistantObject *mplayerosxProxy;
41 id <MPlayerOSXVOProto> mplayerosxProto;
42 MPlayerOpenGLView *mpGLView;
43 NSAutoreleasePool *autoreleasepool;
48 BOOL shared_buffer = false;
49 #define DEFAULT_BUFFER_NAME "mplayerosx"
50 static char *buffer_name;
56 NSScreen *screen_handle;
57 NSArray *screen_array;
60 unsigned char *image_data;
61 // For double buffering
62 static uint8_t image_page = 0;
63 static unsigned char *image_datas[2];
65 static uint32_t image_width;
66 static uint32_t image_height;
67 static uint32_t image_depth;
68 static uint32_t image_bytes;
69 static uint32_t image_format;
72 static int isFullscreen;
75 static float old_movie_aspect;
76 extern int enable_mouse_movements;
78 static float winAlpha = 1;
79 static int int_pause = 0;
81 static BOOL isLeopardOrLater;
83 static vo_info_t info =
87 "Nicolas Plourde <nicolas.plourde@gmail.com>",
93 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride)
98 vo_draw_alpha_rgb32(w,h,src,srca,stride,image_data+4*(y0*image_width+x0),4*image_width);
101 vo_draw_alpha_yuy2(w,h,src,srca,stride,image_data + (x0 + y0 * image_width) * 2,image_width*2);
106 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)
110 screen_array = [NSScreen screens];
111 if(screen_id < [screen_array count])
113 screen_handle = [screen_array objectAtIndex:screen_id];
117 mp_msg(MSGT_VO, MSGL_FATAL, "Get device error: Device ID %d do not exist, falling back to main device.\n", screen_id);
118 screen_handle = [screen_array objectAtIndex:0];
121 screen_frame = [screen_handle frame];
122 vo_screenwidth = screen_frame.size.width;
123 vo_screenheight = screen_frame.size.height;
127 image_height = height;
128 switch (image_format)
138 image_bytes = (image_depth + 7) / 8;
142 image_data = malloc(image_width*image_height*image_bytes);
143 image_datas[0] = image_data;
144 if (vo_doublebuffering)
145 image_datas[1] = malloc(image_width*image_height*image_bytes);
148 monitor_aspect = (float)screen_frame.size.width/(float)screen_frame.size.height;
152 aspect_save_orig(width,height);
153 aspect_save_prescale(d_width,d_height);
154 aspect_save_screenres(screen_frame.size.width, screen_frame.size.height);
155 aspect((int *)&d_width,(int *)&d_height,A_NOZOOM);
157 movie_aspect = (float)d_width/(float)d_height;
158 old_movie_aspect = movie_aspect;
160 vo_fs = flags & VOFLAG_FULLSCREEN;
168 mp_msg(MSGT_VO, MSGL_INFO, "VO: [macosx] writing output to a shared buffer "
169 "named \"%s\".\n",buffer_name);
171 movie_aspect = (float)d_width/(float)d_height;
173 // create shared memory
174 shm_fd = shm_open(buffer_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
177 mp_msg(MSGT_VO, MSGL_FATAL,
178 "vo_macosx: failed to open shared memory. Error: %s\n", strerror(errno));
183 if (ftruncate(shm_fd, image_width*image_height*image_bytes) == -1)
185 mp_msg(MSGT_VO, MSGL_FATAL,
186 "vo_macosx: failed to size shared memory, possibly already in use. Error: %s\n", strerror(errno));
187 shm_unlink(buffer_name);
191 image_data = mmap(NULL, image_width*image_height*image_bytes,
192 PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
194 if (image_data == MAP_FAILED)
196 mp_msg(MSGT_VO, MSGL_FATAL,
197 "vo_macosx: failed to map shared memory. Error: %s\n", strerror(errno));
198 shm_unlink(buffer_name);
202 //connnect to mplayerosx
203 mplayerosxProxy=[NSConnection rootProxyForConnectionWithRegisteredName:[NSString stringWithCString:buffer_name] host:nil];
204 if ([mplayerosxProxy conformsToProtocol:@protocol(MPlayerOSXVOProto)]) {
205 [mplayerosxProxy setProtocolForProxy:@protocol(MPlayerOSXVOProto)];
206 mplayerosxProto = (id <MPlayerOSXVOProto>)mplayerosxProxy;
207 [mplayerosxProto startWithWidth: image_width withHeight: image_height withBytes: image_bytes withAspect:(int)(movie_aspect*100)];
210 [mplayerosxProxy release];
211 mplayerosxProxy = nil;
212 mplayerosxProto = nil;
218 static void check_events(void)
220 [mpGLView check_events];
223 static void draw_osd(void)
225 vo_draw_text(image_width, image_height, draw_alpha);
228 static void flip_page(void)
231 [mplayerosxProto render];
233 [mpGLView setCurrentTexture];
235 if (vo_doublebuffering) {
236 image_page = 1 - image_page;
237 image_data = image_datas[image_page];
242 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
248 static int draw_frame(uint8_t *src[])
250 switch (image_format)
254 fast_memcpy(image_data, src[0], image_width*image_height*image_bytes);
258 memcpy_pic(image_data, src[0], image_width * 2, image_height, image_width * 2, image_width * 2);
265 static int query_format(uint32_t format)
267 image_format = format;
272 pixelFormat = kYUVSPixelFormat;
273 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
277 pixelFormat = k32ARGBPixelFormat;
278 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
283 static void uninit(void)
287 [mplayerosxProto stop];
288 mplayerosxProto = nil;
289 [mplayerosxProxy release];
290 mplayerosxProxy = nil;
292 if (munmap(image_data, image_width*image_height*image_bytes) == -1)
293 mp_msg(MSGT_VO, MSGL_FATAL, "uninit: munmap failed. Error: %s\n", strerror(errno));
295 if (shm_unlink(buffer_name) == -1)
296 mp_msg(MSGT_VO, MSGL_FATAL, "uninit: shm_unlink failed. Error: %s\n", strerror(errno));
300 SetSystemUIMode( kUIModeNormal, 0);
301 CGDisplayShowCursor(kCGDirectMainDisplay);
305 NSAutoreleasePool *finalPool;
307 [autoreleasepool release];
308 finalPool = [[NSAutoreleasePool alloc] init];
309 [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
314 free(image_datas[0]);
315 if (vo_doublebuffering)
316 free(image_datas[1]);
317 image_datas[0] = NULL;
318 image_datas[1] = NULL;
322 if (buffer_name) free(buffer_name);
326 static opt_t subopts[] = {
327 {"device_id", OPT_ARG_INT, &screen_id, (opt_test_f)int_non_neg},
328 {"shared_buffer", OPT_ARG_BOOL, &shared_buffer, NULL},
329 {"buffer_name", OPT_ARG_MSTRZ,&buffer_name, NULL},
333 static int preinit(const char *arg)
338 shared_buffer = false;
341 if (subopt_parse(arg, subopts) != 0) {
342 mp_msg(MSGT_VO, MSGL_FATAL,
343 "\n-vo macosx command line help:\n"
344 "Example: mplayer -vo macosx:device_id=1:shared_buffer:buffer_name=mybuff\n"
346 " device_id=<0-...>\n"
347 " Set screen device id for fullscreen.\n"
349 " Write output to a shared memory buffer instead of displaying it.\n"
350 " buffer_name=<name>\n"
351 " Name of the shared buffer created with shm_open() as well as\n"
352 " the name of the NSConnection MPlayer will try to open.\n"
353 " Setting buffer_name implicitly enables shared_buffer.\n"
359 autoreleasepool = [[NSAutoreleasePool alloc] init];
360 NSApp = [NSApplication sharedApplication];
361 isLeopardOrLater = floor(NSAppKitVersionNumber) > 824;
364 buffer_name = strdup(DEFAULT_BUFFER_NAME);
366 shared_buffer = true;
370 #if !defined (CONFIG_MACOSX_FINDER) || !defined (CONFIG_SDL)
371 //this chunk of code is heavily based off SDL_macosx.m from SDL
372 ProcessSerialNumber myProc, frProc;
375 if (GetFrontProcess(&frProc) == noErr)
377 if (GetCurrentProcess(&myProc) == noErr)
379 if (SameProcess(&frProc, &myProc, &sameProc) == noErr && !sameProc)
381 TransformProcessType(&myProc, kProcessTransformToForegroundApplication);
383 SetFrontProcess(&myProc);
390 mpGLView = [[MPlayerOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat:[MPlayerOpenGLView defaultPixelFormat]];
391 [mpGLView autorelease];
401 static int control(uint32_t request, void *data)
405 case VOCTRL_PAUSE: return int_pause = 1;
406 case VOCTRL_RESUME: return int_pause = 0;
407 case VOCTRL_QUERY_FORMAT: return query_format(*((uint32_t*)data));
408 case VOCTRL_ONTOP: vo_ontop = (!(vo_ontop)); if(!shared_buffer){ [mpGLView ontop]; } else { [mplayerosxProto ontop]; } return VO_TRUE;
409 case VOCTRL_ROOTWIN: vo_rootwin = (!(vo_rootwin)); [mpGLView rootwin]; return VO_TRUE;
410 case VOCTRL_FULLSCREEN: vo_fs = (!(vo_fs)); if(!shared_buffer){ [mpGLView fullscreen: NO]; } else { [mplayerosxProto toggleFullscreen]; } return VO_TRUE;
411 case VOCTRL_GET_PANSCAN: return VO_TRUE;
412 case VOCTRL_SET_PANSCAN: [mpGLView panscan]; return VO_TRUE;
417 //////////////////////////////////////////////////////////////////////////
418 // NSOpenGLView Subclass
419 //////////////////////////////////////////////////////////////////////////
420 @implementation MPlayerOpenGLView
427 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
428 styleMask:NSTitledWindowMask|NSTexturedBackgroundWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
429 backing:NSBackingStoreBuffered defer:NO];
431 [window autorelease];
432 [window setDelegate:mpGLView];
433 [window setContentView:mpGLView];
434 [window setInitialFirstResponder:mpGLView];
435 [window setAcceptsMouseMovedEvents:YES];
436 [window setTitle:@"MPlayer - The Movie Player"];
447 GLint swapInterval = 1;
450 CVReturn error = kCVReturnSuccess;
453 aspect((int *)&d_width, (int *)&d_height,A_NOZOOM);
454 frame = NSMakeRect(0, 0, d_width, d_height);
455 [window setContentSize: frame.size];
457 //create OpenGL Context
458 glContext = [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil];
460 [self setOpenGLContext:glContext];
461 [glContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
462 [glContext setView:self];
463 [glContext makeCurrentContext];
465 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[0], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[0]);
466 if(error != kCVReturnSuccess)
467 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create Pixel Buffer(%d)\n", error);
468 if (vo_doublebuffering) {
469 error = CVPixelBufferCreateWithBytes(NULL, image_width, image_height, pixelFormat, image_datas[1], image_width*image_bytes, NULL, NULL, NULL, &frameBuffers[1]);
470 if(error != kCVReturnSuccess)
471 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create Pixel Double Buffer(%d)\n", error);
474 error = CVOpenGLTextureCacheCreate(NULL, 0, [glContext CGLContextObj], [[self pixelFormat] CGLPixelFormatObj], 0, &textureCache);
475 if(error != kCVReturnSuccess)
476 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture Cache(%d)\n", error);
478 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
479 if(error != kCVReturnSuccess)
480 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture(%d)\n", error);
484 [window makeKeyAndOrderFront:mpGLView];
490 [mpGLView fullscreen: NO];
501 NSMenu *menu, *aspectMenu;
502 NSMenuItem *menuItem;
504 [NSApp setMainMenu:[[NSMenu alloc] init]];
507 menu = [[NSMenu alloc] initWithTitle:@"Movie"];
508 menuItem = [[NSMenuItem alloc] initWithTitle:@"Half Size" action:@selector(menuAction:) keyEquivalent:@"0"]; [menu addItem:menuItem];
509 kHalfScreenCmd = menuItem;
510 menuItem = [[NSMenuItem alloc] initWithTitle:@"Normal Size" action:@selector(menuAction:) keyEquivalent:@"1"]; [menu addItem:menuItem];
511 kNormalScreenCmd = menuItem;
512 menuItem = [[NSMenuItem alloc] initWithTitle:@"Double Size" action:@selector(menuAction:) keyEquivalent:@"2"]; [menu addItem:menuItem];
513 kDoubleScreenCmd = menuItem;
514 menuItem = [[NSMenuItem alloc] initWithTitle:@"Full Size" action:@selector(menuAction:) keyEquivalent:@"f"]; [menu addItem:menuItem];
515 kFullScreenCmd = menuItem;
516 menuItem = (NSMenuItem *)[NSMenuItem separatorItem]; [menu addItem:menuItem];
518 aspectMenu = [[NSMenu alloc] initWithTitle:@"Aspect Ratio"];
519 menuItem = [[NSMenuItem alloc] initWithTitle:@"Keep" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
520 if(vo_keepaspect) [menuItem setState:NSOnState];
521 kKeepAspectCmd = menuItem;
522 menuItem = [[NSMenuItem alloc] initWithTitle:@"Pan-Scan" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
523 if(vo_panscan) [menuItem setState:NSOnState];
524 kPanScanCmd = menuItem;
525 menuItem = (NSMenuItem *)[NSMenuItem separatorItem]; [aspectMenu addItem:menuItem];
526 menuItem = [[NSMenuItem alloc] initWithTitle:@"Original" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
527 kAspectOrgCmd = menuItem;
528 menuItem = [[NSMenuItem alloc] initWithTitle:@"4:3" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
529 kAspectFullCmd = menuItem;
530 menuItem = [[NSMenuItem alloc] initWithTitle:@"16:9" action:@selector(menuAction:) keyEquivalent:@""]; [aspectMenu addItem:menuItem];
531 kAspectWideCmd = menuItem;
532 menuItem = [[NSMenuItem alloc] initWithTitle:@"Aspect Ratio" action:nil keyEquivalent:@""];
533 [menuItem setSubmenu:aspectMenu];
534 [menu addItem:menuItem];
535 [aspectMenu release];
538 menuItem = [[NSMenuItem alloc] initWithTitle:@"Movie" action:nil keyEquivalent:@""];
539 [menuItem setSubmenu:menu];
540 [[NSApp mainMenu] addItem:menuItem];
543 menu = [[NSMenu alloc] initWithTitle:@"Window"];
545 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; [menu addItem:menuItem];
546 menuItem = [[NSMenuItem alloc] initWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""]; [menu addItem:menuItem];
549 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
550 [menuItem setSubmenu:menu];
551 [[NSApp mainMenu] addItem:menuItem];
552 [NSApp setWindowsMenu:menu];
561 - (void)menuAction:(id)sender
567 aspect((int *)&d_width, (int *)&d_height,A_NOZOOM);
569 if(sender == kQuitCmd)
571 mplayer_put_key(KEY_ESC);
574 if(sender == kHalfScreenCmd)
577 vo_fs = (!(vo_fs)); [self fullscreen:NO];
581 frame.size.width = (d_width*winSizeMult);
582 frame.size.height = ((d_width/movie_aspect)*winSizeMult);
583 [window setContentSize: frame.size];
586 if(sender == kNormalScreenCmd)
589 vo_fs = (!(vo_fs)); [self fullscreen:NO];
593 frame.size.width = d_width;
594 frame.size.height = d_width/movie_aspect;
595 [window setContentSize: frame.size];
598 if(sender == kDoubleScreenCmd)
601 vo_fs = (!(vo_fs)); [self fullscreen:NO];
605 frame.size.width = d_width*winSizeMult;
606 frame.size.height = (d_width/movie_aspect)*winSizeMult;
607 [window setContentSize: frame.size];
610 if(sender == kFullScreenCmd)
613 [self fullscreen:NO];
616 if(sender == kKeepAspectCmd)
618 vo_keepaspect = (!(vo_keepaspect));
620 [kKeepAspectCmd setState:NSOnState];
622 [kKeepAspectCmd setState:NSOffState];
627 if(sender == kPanScanCmd)
629 vo_panscan = (!(vo_panscan));
631 [kPanScanCmd setState:NSOnState];
633 [kPanScanCmd setState:NSOffState];
638 if(sender == kAspectOrgCmd)
640 movie_aspect = old_movie_aspect;
648 frame.size.width = d_width*winSizeMult;
649 frame.size.height = (d_width/movie_aspect)*winSizeMult;
650 [window setContentSize: frame.size];
655 if(sender == kAspectFullCmd)
657 movie_aspect = 4.0f/3.0f;
665 frame.size.width = d_width*winSizeMult;
666 frame.size.height = (d_width/movie_aspect)*winSizeMult;
667 [window setContentSize: frame.size];
672 if(sender == kAspectWideCmd)
674 movie_aspect = 16.0f/9.0f;
682 frame.size.width = d_width*winSizeMult;
683 frame.size.height = (d_width/movie_aspect)*winSizeMult;
684 [window setContentSize: frame.size];
693 - (void)prepareOpenGL
696 glDisable(GL_DEPTH_TEST);
697 glDepthMask(GL_FALSE);
698 glDisable(GL_CULL_FACE);
703 reshape OpenGL viewport
713 NSRect frame = [self frame];
715 glViewport(0, 0, frame.size.width, frame.size.height);
716 glMatrixMode(GL_PROJECTION);
718 glOrtho(0, frame.size.width, frame.size.height, 0, -1.0, 1.0);
719 glMatrixMode(GL_MODELVIEW);
725 aspect( (int *)&d_width, (int *)&d_height, A_NOZOOM);
726 d_height = ((float)d_width/movie_aspect);
728 aspectX = (float)((float)frame.size.width/(float)d_width);
729 aspectY = (float)((float)(frame.size.height)/(float)d_height);
731 if((d_height*aspectX)>(frame.size.height))
733 padding = (frame.size.width - d_width*aspectY)/2;
734 textureFrame = NSMakeRect(padding, 0, d_width*aspectY, d_height*aspectY);
738 padding = ((frame.size.height) - d_height*aspectX)/2;
739 textureFrame = NSMakeRect(0, padding, d_width*aspectX, d_height*aspectX);
744 textureFrame = frame;
746 vo_dwidth = textureFrame.size.width;
747 vo_dheight = textureFrame.size.height;
757 glClear(GL_COLOR_BUFFER_BIT);
759 glEnable(CVOpenGLTextureGetTarget(texture));
760 glBindTexture(CVOpenGLTextureGetTarget(texture), CVOpenGLTextureGetName(texture));
764 glTexCoord2f(upperLeft[0], upperLeft[1]); glVertex2i( textureFrame.origin.x-(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
765 glTexCoord2f(lowerLeft[0], lowerLeft[1]); glVertex2i(textureFrame.origin.x-(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
766 glTexCoord2f(lowerRight[0], lowerRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), NSMaxY(textureFrame)+(vo_panscan_y >> 1));
767 glTexCoord2f(upperRight[0], upperRight[1]); glVertex2i(NSMaxX(textureFrame)+(vo_panscan_x >> 1), textureFrame.origin.y-(vo_panscan_y >> 1));
769 glDisable(CVOpenGLTextureGetTarget(texture));
774 NSRect frame = [self frame];
777 glColor4f(0.2, 0.2, 0.2, 0.5);
778 glVertex2i(frame.size.width-1, frame.size.height-1); glVertex2i(frame.size.width-1, frame.size.height-1);
779 glVertex2i(frame.size.width-1, frame.size.height-5); glVertex2i(frame.size.width-5, frame.size.height-1);
780 glVertex2i(frame.size.width-1, frame.size.height-9); glVertex2i(frame.size.width-9, frame.size.height-1);
782 glColor4f(0.4, 0.4, 0.4, 0.5);
783 glVertex2i(frame.size.width-1, frame.size.height-2); glVertex2i(frame.size.width-2, frame.size.height-1);
784 glVertex2i(frame.size.width-1, frame.size.height-6); glVertex2i(frame.size.width-6, frame.size.height-1);
785 glVertex2i(frame.size.width-1, frame.size.height-10); glVertex2i(frame.size.width-10, frame.size.height-1);
787 glColor4f(0.6, 0.6, 0.6, 0.5);
788 glVertex2i(frame.size.width-1, frame.size.height-3); glVertex2i(frame.size.width-3, frame.size.height-1);
789 glVertex2i(frame.size.width-1, frame.size.height-7); glVertex2i(frame.size.width-7, frame.size.height-1);
790 glVertex2i(frame.size.width-1, frame.size.height-11); glVertex2i(frame.size.width-11, frame.size.height-1);
796 curTime = TickCount()/60;
798 //auto hide mouse cursor (and future on-screen control?)
799 if(isFullscreen && !mouseHide && !isRootwin)
801 if( ((curTime - lastMouseHide) >= 5) || (lastMouseHide == 0) )
803 CGDisplayHideCursor(kCGDirectMainDisplay);
805 lastMouseHide = curTime;
809 //update activity every 30 seconds to prevent
810 //screensaver from starting up.
811 if( ((curTime - lastScreensaverUpdate) >= 30) || (lastScreensaverUpdate == 0) )
813 UpdateSystemActivity(UsrActivity);
814 lastScreensaverUpdate = curTime;
819 Create OpenGL texture from current frame & set texco
821 - (void) setCurrentTexture
823 CVReturn error = kCVReturnSuccess;
825 error = CVOpenGLTextureCacheCreateTextureFromImage(NULL, textureCache, frameBuffers[image_page], 0, &texture);
826 if(error != kCVReturnSuccess)
827 mp_msg(MSGT_VO, MSGL_ERR,"Failed to create OpenGL texture(%d)\n", error);
829 CVOpenGLTextureGetCleanTexCoords(texture, lowerLeft, lowerRight, upperRight, upperLeft);
835 - (void) drawRect: (NSRect *) bounds
843 - (void) fullscreen: (BOOL) animate
845 static NSRect old_frame;
846 static NSRect old_view_frame;
855 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
856 CGDisplayHideCursor(kCGDirectMainDisplay);
860 old_frame = [window frame]; //save main window size & position
862 screen_frame = [screen_handle frame];
864 screen_frame = [[window screen] frame];
865 vo_screenwidth = screen_frame.size.width;
866 vo_screenheight = screen_frame.size.height;
869 [window setFrame:screen_frame display:YES animate:animate]; //zoom-in window with nice useless sfx
870 old_view_frame = [self bounds];
872 //fix origin for multi screen setup
873 screen_frame.origin.x = 0;
874 screen_frame.origin.y = 0;
875 [self setFrame:screen_frame];
876 [self setNeedsDisplay:YES];
877 [window setHasShadow:NO];
882 SetSystemUIMode( kUIModeNormal, 0);
885 CGDisplayShowCursor(kCGDirectMainDisplay);
888 //revert window to previous setting
889 [self setFrame:old_view_frame];
890 [self setNeedsDisplay:YES];
891 [window setHasShadow:YES];
892 [window setFrame:old_frame display:YES animate:animate];//zoom-out window with nice useless sfx
903 [window setLevel:NSScreenSaverWindowLevel];
908 [window setLevel:NSNormalWindowLevel];
928 [window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey)];
929 [window orderBack:self];
934 [window setLevel:NSNormalWindowLevel];
940 Check event for new event
942 - (void) check_events
944 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate dateWithTimeIntervalSinceNow:0.0001] inMode:NSEventTrackingRunLoopMode dequeue:YES];
947 [NSApp sendEvent:event];
948 // Without SDL's bootstrap code (include SDL.h in mplayer.c),
949 // on Leopard, we got trouble to get the play window auto focused
950 // when app is actived. Following code fix this problem.
952 if (isLeopardOrLater && [event type] == NSAppKitDefined
953 && [event subtype] == NSApplicationActivatedEventType) {
954 [window makeMainWindow];
955 [window makeKeyAndOrderFront:mpGLView];
961 From NSView, respond to key equivalents.
963 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
965 switch([theEvent keyCode])
967 case 0x21: [window setAlphaValue: winAlpha-=0.05]; return YES;
968 case 0x1e: [window setAlphaValue: winAlpha+=0.05]; return YES;
976 - (void) keyDown: (NSEvent *) theEvent
980 switch([theEvent keyCode])
983 case 0x24: key = KEY_ENTER; break;
984 case 0x35: key = KEY_ESC; break;
985 case 0x33: key = KEY_BACKSPACE; break;
986 case 0x3A: key = KEY_BACKSPACE; break;
987 case 0x3B: key = KEY_BACKSPACE; break;
988 case 0x38: key = KEY_BACKSPACE; break;
989 case 0x7A: key = KEY_F+1; break;
990 case 0x78: key = KEY_F+2; break;
991 case 0x63: key = KEY_F+3; break;
992 case 0x76: key = KEY_F+4; break;
993 case 0x60: key = KEY_F+5; break;
994 case 0x61: key = KEY_F+6; break;
995 case 0x62: key = KEY_F+7; break;
996 case 0x64: key = KEY_F+8; break;
997 case 0x65: key = KEY_F+9; break;
998 case 0x6D: key = KEY_F+10; break;
999 case 0x67: key = KEY_F+11; break;
1000 case 0x6F: key = KEY_F+12; break;
1001 case 0x72: key = KEY_INSERT; break;
1002 case 0x75: key = KEY_DELETE; break;
1003 case 0x73: key = KEY_HOME; break;
1004 case 0x77: key = KEY_END; break;
1005 case 0x45: key = '+'; break;
1006 case 0x4E: key = '-'; break;
1007 case 0x30: key = KEY_TAB; break;
1008 case 0x74: key = KEY_PAGE_UP; break;
1009 case 0x79: key = KEY_PAGE_DOWN; break;
1010 case 0x7B: key = KEY_LEFT; break;
1011 case 0x7C: key = KEY_RIGHT; break;
1012 case 0x7D: key = KEY_DOWN; break;
1013 case 0x7E: key = KEY_UP; break;
1014 case 0x43: key = '*'; break;
1015 case 0x4B: key = '/'; break;
1016 case 0x4C: key = KEY_KPENTER; break;
1017 case 0x41: key = KEY_KPDEC; break;
1018 case 0x52: key = KEY_KP0; break;
1019 case 0x53: key = KEY_KP1; break;
1020 case 0x54: key = KEY_KP2; break;
1021 case 0x55: key = KEY_KP3; break;
1022 case 0x56: key = KEY_KP4; break;
1023 case 0x57: key = KEY_KP5; break;
1024 case 0x58: key = KEY_KP6; break;
1025 case 0x59: key = KEY_KP7; break;
1026 case 0x5B: key = KEY_KP8; break;
1027 case 0x5C: key = KEY_KP9; break;
1028 default: key = *[[theEvent characters] UTF8String]; break;
1030 mplayer_put_key(key);
1034 Process mouse button event
1036 - (void) mouseMoved: (NSEvent *) theEvent
1038 if(isFullscreen && !isRootwin)
1040 CGDisplayShowCursor(kCGDirectMainDisplay);
1043 if (enable_mouse_movements && !isRootwin) {
1044 NSPoint p =[self convertPoint:[theEvent locationInWindow] fromView:nil];
1045 if ([self mouse:p inRect:textureFrame]) {
1047 snprintf(cmdstr, sizeof(cmdstr), "set_mouse_pos %i %i",
1048 (int)(vo_fs ? p.x : (p.x - textureFrame.origin.x)),
1049 (int)(vo_fs ? [self frame].size.height - p.y: (NSMaxY(textureFrame) - p.y)));
1050 mp_input_queue_cmd(global_vo->input_ctx, mp_input_parse_cmd(cmdstr));
1055 - (void) mouseDown: (NSEvent *) theEvent
1057 [self mouseEvent: theEvent];
1060 - (void) mouseUp: (NSEvent *) theEvent
1062 [self mouseEvent: theEvent];
1065 - (void) rightMouseDown: (NSEvent *) theEvent
1067 [self mouseEvent: theEvent];
1070 - (void) rightMouseUp: (NSEvent *) theEvent
1072 [self mouseEvent: theEvent];
1075 - (void) otherMouseDown: (NSEvent *) theEvent
1077 [self mouseEvent: theEvent];
1080 - (void) otherMouseUp: (NSEvent *) theEvent
1082 [self mouseEvent: theEvent];
1085 - (void) scrollWheel: (NSEvent *) theEvent
1087 if([theEvent deltaY] > 0)
1088 mplayer_put_key(MOUSE_BTN3);
1090 mplayer_put_key(MOUSE_BTN4);
1093 - (void) mouseEvent: (NSEvent *) theEvent
1095 if ( [theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9 )
1097 int buttonNumber = [theEvent buttonNumber];
1098 // Fix to mplayer defined button order: left, middle, right
1099 if (buttonNumber == 1)
1101 else if (buttonNumber == 2)
1103 switch([theEvent type])
1105 case NSLeftMouseDown:
1106 case NSRightMouseDown:
1107 case NSOtherMouseDown:
1108 mplayer_put_key((MOUSE_BTN0 + buttonNumber) | MP_KEY_DOWN);
1111 case NSRightMouseUp:
1112 case NSOtherMouseUp:
1113 mplayer_put_key(MOUSE_BTN0 + buttonNumber);
1122 - (BOOL) acceptsFirstResponder
1127 - (BOOL) becomeFirstResponder
1132 - (BOOL) resignFirstResponder
1137 - (void)windowWillClose:(NSNotification *)aNotification
1140 mplayer_put_key(KEY_ESC);