4 * This file is part of mplayer2.
6 * mplayer2 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * mplayer2 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with mplayer2. If not, see <http://www.gnu.org/licenses/>.
20 #import <Cocoa/Cocoa.h>
21 #import <OpenGL/OpenGL.h>
22 #import <QuartzCore/QuartzCore.h>
23 #import <CoreServices/CoreServices.h> // for CGDisplayHideCursor
26 #include "cocoa_common.h"
29 #include "video_out.h"
35 #include "input/input.h"
36 #include "input/keycodes.h"
37 #include "osx_common.h"
40 #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
41 #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
43 @interface GLMPlayerWindow : NSWindow <NSWindowDelegate>
44 - (BOOL) canBecomeKeyWindow;
45 - (BOOL) canBecomeMainWindow;
47 - (void) mouseEvent:(NSEvent *)theEvent;
48 - (void) mulSize:(float)multiplier;
49 - (void) setContentSize:(NSSize)newSize keepCentered:(BOOL)keepCentered;
52 @interface GLMPlayerOpenGLView : NSView
55 struct vo_cocoa_state {
56 NSAutoreleasePool *pool;
57 GLMPlayerWindow *window;
58 NSOpenGLContext *glContext;
59 NSOpenGLPixelFormat *pixelFormat;
61 NSSize current_video_size;
62 NSSize previous_video_size;
65 NSScreen *screen_handle;
66 NSArray *screen_array;
68 NSInteger windowed_mask;
69 NSInteger fullscreen_mask;
71 NSRect windowed_frame;
73 NSString *window_title;
75 NSInteger windowed_window_level;
76 NSInteger fullscreen_window_level;
78 int last_screensaver_update;
82 int cursor_autohide_delay;
88 struct vo_cocoa_state *s;
92 // local function definitions
93 struct vo_cocoa_state *vo_cocoa_init_state(void);
94 void vo_set_level(int ontop);
95 void update_screen_info(void);
96 void resize_window(struct vo *vo);
97 void vo_cocoa_display_cursor(int requested_state);
98 void create_menu(void);
100 struct vo_cocoa_state *vo_cocoa_init_state(void)
102 struct vo_cocoa_state *s = talloc_ptrtype(NULL, s);
103 *s = (struct vo_cocoa_state){
105 .current_video_size = {0,0},
106 .previous_video_size = {0,0},
107 .windowed_mask = NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
108 .fullscreen_mask = NSBorderlessWindowMask,
109 .fullscreen_window_level = NSNormalWindowLevel + 1,
110 .windowed_frame = {{0,0},{0,0}},
117 bool vo_cocoa_gui_running(void)
122 void *vo_cocoa_glgetaddr(const char *s)
125 void *handle = dlopen(
126 "/System/Library/Frameworks/OpenGL.framework/OpenGL",
127 RTLD_LAZY | RTLD_LOCAL);
130 ret = dlsym(handle, s);
135 int vo_cocoa_init(struct vo *vo)
137 s = vo_cocoa_init_state();
138 s->pool = [[NSAutoreleasePool alloc] init];
139 s->cursor_autohide_delay = vo->opts->cursor_autohide_delay;
141 NSApp = [NSApplication sharedApplication];
142 [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];
147 void vo_cocoa_uninit(struct vo *vo)
149 CGDisplayShowCursor(kCGDirectMainDisplay);
150 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
154 [s->glContext release];
163 static int current_screen_has_dock_or_menubar(void)
165 NSRect f = s->screen_frame;
166 NSRect vf = [s->screen_handle visibleFrame];
167 return f.size.height > vf.size.height || f.size.width > vf.size.width;
170 void update_screen_info(void)
172 s->screen_array = [NSScreen screens];
173 if (xinerama_screen >= (int)[s->screen_array count]) {
174 mp_msg(MSGT_VO, MSGL_INFO, "[cocoa] Device ID %d does not exist, falling back to main device\n", xinerama_screen);
175 xinerama_screen = -1;
178 if (xinerama_screen < 0) { // default behaviour
179 if (! (s->screen_handle = [s->window screen]) )
180 s->screen_handle = [s->screen_array objectAtIndex:0];
182 s->screen_handle = [s->screen_array objectAtIndex:(xinerama_screen)];
185 s->screen_frame = [s->screen_handle frame];
188 void vo_cocoa_update_xinerama_info(struct vo *vo)
190 update_screen_info();
191 aspect_save_screenres(vo, s->screen_frame.size.width, s->screen_frame.size.height);
194 int vo_cocoa_change_attributes(struct vo *vo)
199 void resize_window(struct vo *vo)
201 vo->dwidth = [[s->window contentView] frame].size.width;
202 vo->dheight = [[s->window contentView] frame].size.height;
203 [s->glContext update];
206 void vo_set_level(int ontop)
209 s->windowed_window_level = NSNormalWindowLevel + 1;
211 s->windowed_window_level = NSNormalWindowLevel;
215 [s->window setLevel:s->windowed_window_level];
218 void vo_cocoa_ontop(struct vo *vo)
220 struct MPOpts *opts = vo->opts;
221 opts->vo_ontop = !opts->vo_ontop;
222 vo_set_level(opts->vo_ontop);
225 int vo_cocoa_create_window(struct vo *vo, uint32_t d_width,
226 uint32_t d_height, uint32_t flags)
228 struct MPOpts *opts = vo->opts;
229 if (s->current_video_size.width > 0 || s->current_video_size.height > 0)
230 s->previous_video_size = s->current_video_size;
231 s->current_video_size = NSMakeSize(d_width, d_height);
233 if (!(s->window || s->glContext)) { // keep using the same window
234 s->window = [[GLMPlayerWindow alloc] initWithContentRect:NSMakeRect(0, 0, d_width, d_height)
235 styleMask:s->windowed_mask
236 backing:NSBackingStoreBuffered defer:NO];
238 GLMPlayerOpenGLView *glView = [[GLMPlayerOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
240 NSOpenGLPixelFormatAttribute attrs[] = {
241 NSOpenGLPFADoubleBuffer, // double buffered
242 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer
243 (NSOpenGLPixelFormatAttribute)0
246 s->pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
247 s->glContext = [[NSOpenGLContext alloc] initWithFormat:s->pixelFormat shareContext:nil];
251 [s->window setContentView:glView];
253 [s->window setAcceptsMouseMovedEvents:YES];
254 [s->glContext setView:glView];
255 [s->glContext makeCurrentContext];
257 [NSApp setDelegate:s->window];
258 [s->window setDelegate:s->window];
259 [s->window setContentSize:s->current_video_size];
260 [s->window setContentAspectRatio:s->current_video_size];
263 if (flags & VOFLAG_HIDDEN) {
264 [s->window orderOut:nil];
266 [s->window makeKeyAndOrderFront:nil];
267 [NSApp activateIgnoringOtherApps:YES];
270 if (flags & VOFLAG_FULLSCREEN)
271 vo_cocoa_fullscreen(vo);
273 vo_set_level(opts->vo_ontop);
275 if (s->current_video_size.width != s->previous_video_size.width ||
276 s->current_video_size.height != s->previous_video_size.height) {
278 // we will resize as soon as we get out of fullscreen
279 s->out_fs_resize = YES;
281 // only if we are not in fullscreen and the video size did change
282 // we actually resize the window and set a new aspect ratio
283 [s->window setContentSize:s->current_video_size keepCentered:YES];
284 [s->window setContentAspectRatio:s->current_video_size];
292 [s->window_title release];
294 s->window_title = [[NSString alloc] initWithUTF8String:vo_get_window_title(vo)];
295 [s->window setTitle: s->window_title];
300 void vo_cocoa_swap_buffers()
302 [s->glContext flushBuffer];
305 void vo_cocoa_display_cursor(int requested_state)
307 if (requested_state) {
308 if (!vo_fs || s->cursor_autohide_delay > -2) {
309 s->display_cursor = requested_state;
310 CGDisplayShowCursor(kCGDirectMainDisplay);
313 if (s->cursor_autohide_delay != -1) {
314 s->display_cursor = requested_state;
315 CGDisplayHideCursor(kCGDirectMainDisplay);
320 int vo_cocoa_check_events(struct vo *vo)
323 float curTime = TickCount()/60;
324 int msCurTime = (int) (curTime * 1000);
326 // automatically hide mouse cursor
327 if (vo_fs && s->display_cursor &&
328 (msCurTime - s->cursor_timer >= s->cursor_autohide_delay)) {
329 vo_cocoa_display_cursor(0);
330 s->cursor_timer = msCurTime;
333 //update activity every 30 seconds to prevent
334 //screensaver from starting up.
335 if ((int)curTime - s->last_screensaver_update >= 30 || s->last_screensaver_update == 0)
337 UpdateSystemActivity(UsrActivity);
338 s->last_screensaver_update = (int)curTime;
341 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil
342 inMode:NSEventTrackingRunLoopMode dequeue:YES];
346 [NSApp sendEvent:event];
352 return VO_EVENT_RESIZE;
354 // Without SDL's bootstrap code (include SDL.h in mplayer.c),
355 // on Leopard, we have trouble to get the play window automatically focused
356 // when the app is actived. The Following code fix this problem.
358 if ([event type] == NSAppKitDefined
359 && [event subtype] == NSApplicationActivatedEventType) {
360 [s->window makeMainWindow];
361 [s->window makeKeyAndOrderFront:nil];
367 void vo_cocoa_fullscreen(struct vo *vo)
369 [s->window fullscreen];
373 int vo_cocoa_swap_interval(int enabled)
375 [s->glContext setValues:&enabled forParameter:NSOpenGLCPSwapInterval];
379 void *vo_cocoa_cgl_context(void)
381 return [s->glContext CGLContextObj];
384 void *vo_cocoa_cgl_pixel_format(void)
386 return [s->pixelFormat CGLPixelFormatObj];
392 NSMenuItem *menuItem;
394 menu = [[NSMenu new] autorelease];
395 menuItem = [[NSMenuItem new] autorelease];
396 [menu addItem: menuItem];
397 [NSApp setMainMenu: menu];
399 menu = [[NSMenu alloc] initWithTitle:@"Movie"];
400 menuItem = [[NSMenuItem alloc] initWithTitle:@"Half Size" action:@selector(halfSize) keyEquivalent:@"0"]; [menu addItem:menuItem];
401 menuItem = [[NSMenuItem alloc] initWithTitle:@"Normal Size" action:@selector(normalSize) keyEquivalent:@"1"]; [menu addItem:menuItem];
402 menuItem = [[NSMenuItem alloc] initWithTitle:@"Double Size" action:@selector(doubleSize) keyEquivalent:@"2"]; [menu addItem:menuItem];
404 menuItem = [[NSMenuItem alloc] initWithTitle:@"Movie" action:nil keyEquivalent:@""];
405 [menuItem setSubmenu:menu];
406 [[NSApp mainMenu] addItem:menuItem];
412 @implementation GLMPlayerWindow
414 - (void) windowDidResize:(NSNotification *) notification
423 update_screen_info();
424 if (current_screen_has_dock_or_menubar())
425 [NSApp setPresentationOptions:NSApplicationPresentationHideDock|NSApplicationPresentationHideMenuBar];
426 s->windowed_frame = [self frame];
427 [self setHasShadow:NO];
428 [self setStyleMask:s->fullscreen_mask];
429 [self setFrame:s->screen_frame display:YES animate:NO];
430 [self setLevel:s->fullscreen_window_level];
432 vo_cocoa_display_cursor(0);
433 [self setMovableByWindowBackground: NO];
435 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
436 [self setHasShadow:YES];
437 [self setStyleMask:s->windowed_mask];
438 [self setTitle:s->window_title];
439 [self setFrame:s->windowed_frame display:YES animate:NO];
440 if (s->out_fs_resize) {
441 [self setContentSize:s->current_video_size keepCentered:YES];
442 s->out_fs_resize = NO;
444 [self setContentAspectRatio:s->current_video_size];
445 [self setLevel:s->windowed_window_level];
447 vo_cocoa_display_cursor(1);
448 [self setMovableByWindowBackground: YES];
452 - (BOOL) canBecomeMainWindow { return YES; }
453 - (BOOL) canBecomeKeyWindow { return YES; }
454 - (BOOL) acceptsFirstResponder { return YES; }
455 - (BOOL) becomeFirstResponder { return YES; }
456 - (BOOL) resignFirstResponder { return YES; }
457 - (BOOL) windowShouldClose:(id)sender
459 mplayer_put_key(l_vo->key_fifo, KEY_CLOSE_WIN);
460 // We have to wait for MPlayer to handle this,
461 // otherwise we are in trouble if the
462 // KEY_CLOSE_WIN handler is disabled
466 - (BOOL) isMovableByWindowBackground
468 // this is only valid as a starting value. it will be rewritten in the -fullscreen method.
472 - (void) handleQuitEvent:(NSAppleEventDescriptor*)e withReplyEvent:(NSAppleEventDescriptor*)r
474 mplayer_put_key(l_vo->key_fifo, KEY_CLOSE_WIN);
477 - (void) keyDown:(NSEvent *)theEvent
479 unsigned char charcode;
480 if (([theEvent modifierFlags] & NSRightAlternateKeyMask) == NSRightAlternateKeyMask)
481 charcode = *[[theEvent characters] UTF8String];
483 charcode = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
485 int key = convert_key([theEvent keyCode], charcode);
488 if ([theEvent modifierFlags] & NSShiftKeyMask)
489 key |= KEY_MODIFIER_SHIFT;
490 if ([theEvent modifierFlags] & NSControlKeyMask)
491 key |= KEY_MODIFIER_CTRL;
492 if (([theEvent modifierFlags] & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask)
493 key |= KEY_MODIFIER_ALT;
494 if ([theEvent modifierFlags] & NSCommandKeyMask)
495 key |= KEY_MODIFIER_META;
496 mplayer_put_key(l_vo->key_fifo, key);
500 - (void) mouseMoved: (NSEvent *) theEvent
503 vo_cocoa_display_cursor(1);
506 - (void) mouseDragged:(NSEvent *)theEvent
508 [self mouseEvent: theEvent];
511 - (void) mouseDown:(NSEvent *)theEvent
513 [self mouseEvent: theEvent];
516 - (void) mouseUp:(NSEvent *)theEvent
518 [self mouseEvent: theEvent];
521 - (void) rightMouseDown:(NSEvent *)theEvent
523 [self mouseEvent: theEvent];
526 - (void) rightMouseUp:(NSEvent *)theEvent
528 [self mouseEvent: theEvent];
531 - (void) otherMouseDown:(NSEvent *)theEvent
533 [self mouseEvent: theEvent];
536 - (void) otherMouseUp:(NSEvent *)theEvent
538 [self mouseEvent: theEvent];
541 - (void) scrollWheel:(NSEvent *)theEvent
543 if ([theEvent deltaY] > 0)
544 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN3);
546 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN4);
549 - (void) mouseEvent:(NSEvent *)theEvent
551 if ([theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9) {
552 int buttonNumber = [theEvent buttonNumber];
553 // Fix to mplayer defined button order: left, middle, right
554 if (buttonNumber == 1) buttonNumber = 2;
555 else if (buttonNumber == 2) buttonNumber = 1;
556 switch ([theEvent type]) {
557 case NSLeftMouseDown:
558 case NSRightMouseDown:
559 case NSOtherMouseDown:
560 mplayer_put_key(l_vo->key_fifo, (MOUSE_BTN0 + buttonNumber) | MP_KEY_DOWN);
561 // Looks like Cocoa doesn't create MouseUp events when we are
562 // doing the second click in a double click. Put in the key_fifo
563 // the key that would be put from the MouseUp handling code.
564 if([theEvent clickCount] == 2)
565 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN0 + buttonNumber);
570 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN0 + buttonNumber);
576 - (void) applicationWillBecomeActive:(NSNotification *)aNotification
579 [s->window makeKeyAndOrderFront:s->window];
580 [s->window setLevel:s->fullscreen_window_level];
581 [NSApp setPresentationOptions:NSApplicationPresentationHideDock|
582 NSApplicationPresentationHideMenuBar];
586 - (void) applicationWillResignActive:(NSNotification *)aNotification
589 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
590 [s->window setLevel:s->windowed_window_level];
591 [s->window orderBack:s->window];
595 - (void) applicationDidFinishLaunching:(NSNotification*)notification
597 // Install an event handler so the Quit menu entry works
598 // The proper way using NSApp setDelegate: and
599 // applicationShouldTerminate: does not work,
600 // probably NSApplication never installs its handler.
601 [[NSAppleEventManager sharedAppleEventManager]
603 andSelector:@selector(handleQuitEvent:withReplyEvent:)
604 forEventClass:kCoreEventClass
605 andEventID:kAEQuitApplication];
611 [self setContentSize:s->current_video_size keepCentered:YES];
614 - (void) halfSize { [self mulSize:0.5f];}
616 - (void) doubleSize { [self mulSize:2.0f];}
618 - (void) mulSize:(float)multiplier
621 NSSize size = [[self contentView] frame].size;
622 size.width = s->current_video_size.width * (multiplier);
623 size.height = s->current_video_size.height * (multiplier);
624 [self setContentSize:size keepCentered:YES];
628 - (void) setContentSize:(NSSize)ns keepCentered:(BOOL)keepCentered
631 NSRect nf = [self frame];
632 NSRect vf = [[self screen] visibleFrame];
633 int title_height = nf.size.height - [[self contentView] bounds].size.height;
634 double ratio = (double)ns.width / (double)ns.height;
636 // clip the new size to the visibleFrame's size if needed
637 if (ns.width > vf.size.width || ns.height + title_height > vf.size.height) {
639 ns.height -= title_height; // make space for the title bar
641 if (ns.width > ns.height) {
642 ns.height = ((double)ns.width * 1/ratio + 0.5);
644 ns.width = ((double)ns.height * ratio + 0.5);
648 int dw = nf.size.width - ns.width;
649 int dh = nf.size.height - ns.height - title_height;
651 nf.origin.x += dw / 2;
652 nf.origin.y += dh / 2;
654 [self setFrame: NSMakeRect(nf.origin.x, nf.origin.y, ns.width, ns.height + title_height) display:YES animate:NO];
656 [self setContentSize:ns];
661 @implementation GLMPlayerOpenGLView
662 - (void) drawRect: (NSRect)rect
664 [[NSColor clearColor] set];
665 NSRectFill([self bounds]);