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
24 #include "cocoa_common.h"
27 #include "video_out.h"
33 #include "input/input.h"
34 #include "input/keycodes.h"
35 #include "osx_common.h"
38 #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
39 #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
41 @interface GLMPlayerWindow : NSWindow <NSWindowDelegate>
42 - (BOOL) canBecomeKeyWindow;
43 - (BOOL) canBecomeMainWindow;
45 - (void) mouseEvent:(NSEvent *)theEvent;
46 - (void) mulSize:(float)multiplier;
47 - (void) setContentSize:(NSSize)newSize keepCentered:(BOOL)keepCentered;
50 @interface GLMPlayerOpenGLView : NSView
53 struct vo_cocoa_state {
54 NSAutoreleasePool *pool;
55 GLMPlayerWindow *window;
56 NSOpenGLContext *glContext;
57 NSOpenGLPixelFormat *pixelFormat;
59 NSSize current_video_size;
60 NSSize previous_video_size;
63 NSScreen *screen_handle;
64 NSArray *screen_array;
66 NSInteger windowed_mask;
67 NSInteger fullscreen_mask;
69 NSRect windowed_frame;
71 NSString *window_title;
73 NSInteger windowed_window_level;
74 NSInteger fullscreen_window_level;
76 int last_screensaver_update;
80 int cursor_autohide_delay;
86 struct vo_cocoa_state *s;
90 // local function definitions
91 struct vo_cocoa_state *vo_cocoa_init_state(void);
92 void vo_set_level(int ontop);
93 void update_screen_info(void);
94 void resize_window(struct vo *vo);
95 void vo_cocoa_display_cursor(int requested_state);
96 void create_menu(void);
98 struct vo_cocoa_state *vo_cocoa_init_state(void)
100 struct vo_cocoa_state *s = talloc_ptrtype(NULL, s);
101 *s = (struct vo_cocoa_state){
103 .current_video_size = {0,0},
104 .previous_video_size = {0,0},
105 .windowed_mask = NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
106 .fullscreen_mask = NSBorderlessWindowMask,
107 .fullscreen_window_level = NSNormalWindowLevel + 1,
108 .windowed_frame = {{0,0},{0,0}},
115 bool vo_cocoa_gui_running(void)
120 int vo_cocoa_init(struct vo *vo)
122 s = vo_cocoa_init_state();
123 s->pool = [[NSAutoreleasePool alloc] init];
124 s->cursor_autohide_delay = vo->opts->cursor_autohide_delay;
126 NSApp = [NSApplication sharedApplication];
127 [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];
132 void vo_cocoa_uninit(struct vo *vo)
134 CGDisplayShowCursor(kCGDirectMainDisplay);
135 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
139 [s->glContext release];
148 void update_screen_info(void)
150 s->screen_array = [NSScreen screens];
151 if (xinerama_screen >= (int)[s->screen_array count]) {
152 mp_msg(MSGT_VO, MSGL_INFO, "[cocoa] Device ID %d does not exist, falling back to main device\n", xinerama_screen);
153 xinerama_screen = -1;
156 if (xinerama_screen < 0) { // default behaviour
157 if (! (s->screen_handle = [s->window screen]) )
158 s->screen_handle = [s->screen_array objectAtIndex:0];
160 s->screen_handle = [s->screen_array objectAtIndex:(xinerama_screen)];
163 s->screen_frame = [s->screen_handle frame];
166 void vo_cocoa_update_xinerama_info(struct vo *vo)
168 update_screen_info();
169 aspect_save_screenres(vo, s->screen_frame.size.width, s->screen_frame.size.height);
172 int vo_cocoa_change_attributes(struct vo *vo)
177 void resize_window(struct vo *vo)
179 vo->dwidth = [[s->window contentView] frame].size.width;
180 vo->dheight = [[s->window contentView] frame].size.height;
181 [s->glContext update];
184 void vo_set_level(int ontop)
187 s->windowed_window_level = NSNormalWindowLevel + 1;
189 s->windowed_window_level = NSNormalWindowLevel;
193 [s->window setLevel:s->windowed_window_level];
196 void vo_cocoa_ontop(struct vo *vo)
198 struct MPOpts *opts = vo->opts;
199 opts->vo_ontop = !opts->vo_ontop;
200 vo_set_level(opts->vo_ontop);
203 int vo_cocoa_create_window(struct vo *vo, uint32_t d_width,
204 uint32_t d_height, uint32_t flags)
206 struct MPOpts *opts = vo->opts;
207 if (s->current_video_size.width > 0 || s->current_video_size.height > 0)
208 s->previous_video_size = s->current_video_size;
209 s->current_video_size = NSMakeSize(d_width, d_height);
211 if (!(s->window || s->glContext)) { // keep using the same window
212 s->window = [[GLMPlayerWindow alloc] initWithContentRect:NSMakeRect(0, 0, d_width, d_height)
213 styleMask:s->windowed_mask
214 backing:NSBackingStoreBuffered defer:NO];
216 GLMPlayerOpenGLView *glView = [[GLMPlayerOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
218 NSOpenGLPixelFormatAttribute attrs[] = {
219 NSOpenGLPFADoubleBuffer, // double buffered
220 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer
221 (NSOpenGLPixelFormatAttribute)0
224 s->pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
225 s->glContext = [[NSOpenGLContext alloc] initWithFormat:s->pixelFormat shareContext:nil];
229 [s->window setContentView:glView];
231 [s->window setAcceptsMouseMovedEvents:YES];
232 [s->glContext setView:glView];
233 [s->glContext makeCurrentContext];
235 [NSApp setDelegate:s->window];
236 [s->window setDelegate:s->window];
237 [s->window setContentSize:s->current_video_size];
238 [s->window setContentAspectRatio:s->current_video_size];
241 if (flags & VOFLAG_HIDDEN) {
242 [s->window orderOut:nil];
244 [s->window makeKeyAndOrderFront:nil];
245 [NSApp activateIgnoringOtherApps:YES];
248 if (flags & VOFLAG_FULLSCREEN)
249 vo_cocoa_fullscreen(vo);
251 vo_set_level(opts->vo_ontop);
253 if (s->current_video_size.width != s->previous_video_size.width ||
254 s->current_video_size.height != s->previous_video_size.height) {
256 // we will resize as soon as we get out of fullscreen
257 s->out_fs_resize = YES;
259 // only if we are not in fullscreen and the video size did change
260 // we actually resize the window and set a new aspect ratio
261 [s->window setContentSize:s->current_video_size keepCentered:YES];
262 [s->window setContentAspectRatio:s->current_video_size];
270 [s->window_title release];
272 s->window_title = [[NSString alloc] initWithUTF8String:vo_get_window_title(vo)];
273 [s->window setTitle: s->window_title];
278 void vo_cocoa_swap_buffers()
280 [s->glContext flushBuffer];
283 void vo_cocoa_display_cursor(int requested_state)
285 if (requested_state) {
286 if (!vo_fs || s->cursor_autohide_delay > -2) {
287 s->display_cursor = requested_state;
288 CGDisplayShowCursor(kCGDirectMainDisplay);
291 if (s->cursor_autohide_delay != -1) {
292 s->display_cursor = requested_state;
293 CGDisplayHideCursor(kCGDirectMainDisplay);
298 int vo_cocoa_check_events(struct vo *vo)
301 float curTime = TickCount()/60;
302 int msCurTime = (int) (curTime * 1000);
304 // automatically hide mouse cursor
305 if (vo_fs && s->display_cursor &&
306 (msCurTime - s->cursor_timer >= s->cursor_autohide_delay)) {
307 vo_cocoa_display_cursor(0);
308 s->cursor_timer = msCurTime;
311 //update activity every 30 seconds to prevent
312 //screensaver from starting up.
313 if ((int)curTime - s->last_screensaver_update >= 30 || s->last_screensaver_update == 0)
315 UpdateSystemActivity(UsrActivity);
316 s->last_screensaver_update = (int)curTime;
319 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil
320 inMode:NSEventTrackingRunLoopMode dequeue:YES];
324 [NSApp sendEvent:event];
330 return VO_EVENT_RESIZE;
332 // Without SDL's bootstrap code (include SDL.h in mplayer.c),
333 // on Leopard, we have trouble to get the play window automatically focused
334 // when the app is actived. The Following code fix this problem.
336 if ([event type] == NSAppKitDefined
337 && [event subtype] == NSApplicationActivatedEventType) {
338 [s->window makeMainWindow];
339 [s->window makeKeyAndOrderFront:nil];
345 void vo_cocoa_fullscreen(struct vo *vo)
347 [s->window fullscreen];
351 int vo_cocoa_swap_interval(int enabled)
353 [s->glContext setValues:&enabled forParameter:NSOpenGLCPSwapInterval];
357 void *vo_cocoa_cgl_context(void)
359 return [s->glContext CGLContextObj];
362 void *vo_cocoa_cgl_pixel_format(void)
364 return [s->pixelFormat CGLPixelFormatObj];
370 NSMenuItem *menuItem;
372 menu = [[NSMenu new] autorelease];
373 menuItem = [[NSMenuItem new] autorelease];
374 [menu addItem: menuItem];
375 [NSApp setMainMenu: menu];
377 menu = [[NSMenu alloc] initWithTitle:@"Movie"];
378 menuItem = [[NSMenuItem alloc] initWithTitle:@"Half Size" action:@selector(halfSize) keyEquivalent:@"0"]; [menu addItem:menuItem];
379 menuItem = [[NSMenuItem alloc] initWithTitle:@"Normal Size" action:@selector(normalSize) keyEquivalent:@"1"]; [menu addItem:menuItem];
380 menuItem = [[NSMenuItem alloc] initWithTitle:@"Double Size" action:@selector(doubleSize) keyEquivalent:@"2"]; [menu addItem:menuItem];
382 menuItem = [[NSMenuItem alloc] initWithTitle:@"Movie" action:nil keyEquivalent:@""];
383 [menuItem setSubmenu:menu];
384 [[NSApp mainMenu] addItem:menuItem];
390 @implementation GLMPlayerWindow
392 - (void) windowDidResize:(NSNotification *) notification
401 update_screen_info();
402 [NSApp setPresentationOptions:NSApplicationPresentationHideDock|NSApplicationPresentationHideMenuBar];
403 s->windowed_frame = [self frame];
404 [self setHasShadow:NO];
405 [self setStyleMask:s->fullscreen_mask];
406 [self setFrame:s->screen_frame display:YES animate:NO];
407 [self setLevel:s->fullscreen_window_level];
409 vo_cocoa_display_cursor(0);
410 [self setMovableByWindowBackground: NO];
412 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
413 [self setHasShadow:YES];
414 [self setStyleMask:s->windowed_mask];
415 [self setTitle:s->window_title];
416 [self setFrame:s->windowed_frame display:YES animate:NO];
417 if (s->out_fs_resize) {
418 [self setContentSize:s->current_video_size keepCentered:YES];
419 s->out_fs_resize = NO;
421 [self setContentAspectRatio:s->current_video_size];
422 [self setLevel:s->windowed_window_level];
424 vo_cocoa_display_cursor(1);
425 [self setMovableByWindowBackground: YES];
429 - (BOOL) canBecomeMainWindow { return YES; }
430 - (BOOL) canBecomeKeyWindow { return YES; }
431 - (BOOL) acceptsFirstResponder { return YES; }
432 - (BOOL) becomeFirstResponder { return YES; }
433 - (BOOL) resignFirstResponder { return YES; }
434 - (BOOL) windowShouldClose:(id)sender
436 mplayer_put_key(l_vo->key_fifo, KEY_CLOSE_WIN);
437 // We have to wait for MPlayer to handle this,
438 // otherwise we are in trouble if the
439 // KEY_CLOSE_WIN handler is disabled
443 - (BOOL) isMovableByWindowBackground
445 // this is only valid as a starting value. it will be rewritten in the -fullscreen method.
449 - (void) handleQuitEvent:(NSAppleEventDescriptor*)e withReplyEvent:(NSAppleEventDescriptor*)r
451 mplayer_put_key(l_vo->key_fifo, KEY_CLOSE_WIN);
454 - (void) keyDown:(NSEvent *)theEvent
456 unsigned char charcode;
457 if (([theEvent modifierFlags] & NSRightAlternateKeyMask) == NSRightAlternateKeyMask)
458 charcode = *[[theEvent characters] UTF8String];
460 charcode = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
462 int key = convert_key([theEvent keyCode], charcode);
465 if ([theEvent modifierFlags] & NSShiftKeyMask)
466 key |= KEY_MODIFIER_SHIFT;
467 if ([theEvent modifierFlags] & NSControlKeyMask)
468 key |= KEY_MODIFIER_CTRL;
469 if (([theEvent modifierFlags] & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask)
470 key |= KEY_MODIFIER_ALT;
471 if ([theEvent modifierFlags] & NSCommandKeyMask)
472 key |= KEY_MODIFIER_META;
473 mplayer_put_key(l_vo->key_fifo, key);
477 - (void) mouseMoved: (NSEvent *) theEvent
480 vo_cocoa_display_cursor(1);
483 - (void) mouseDragged:(NSEvent *)theEvent
485 [self mouseEvent: theEvent];
488 - (void) mouseDown:(NSEvent *)theEvent
490 [self mouseEvent: theEvent];
493 - (void) mouseUp:(NSEvent *)theEvent
495 [self mouseEvent: theEvent];
498 - (void) rightMouseDown:(NSEvent *)theEvent
500 [self mouseEvent: theEvent];
503 - (void) rightMouseUp:(NSEvent *)theEvent
505 [self mouseEvent: theEvent];
508 - (void) otherMouseDown:(NSEvent *)theEvent
510 [self mouseEvent: theEvent];
513 - (void) otherMouseUp:(NSEvent *)theEvent
515 [self mouseEvent: theEvent];
518 - (void) scrollWheel:(NSEvent *)theEvent
520 if ([theEvent deltaY] > 0)
521 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN3);
523 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN4);
526 - (void) mouseEvent:(NSEvent *)theEvent
528 if ([theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9) {
529 int buttonNumber = [theEvent buttonNumber];
530 // Fix to mplayer defined button order: left, middle, right
531 if (buttonNumber == 1) buttonNumber = 2;
532 else if (buttonNumber == 2) buttonNumber = 1;
533 switch ([theEvent type]) {
534 case NSLeftMouseDown:
535 case NSRightMouseDown:
536 case NSOtherMouseDown:
537 mplayer_put_key(l_vo->key_fifo, (MOUSE_BTN0 + buttonNumber) | MP_KEY_DOWN);
538 // Looks like Cocoa doesn't create MouseUp events when we are
539 // doing the second click in a double click. Put in the key_fifo
540 // the key that would be put from the MouseUp handling code.
541 if([theEvent clickCount] == 2)
542 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN0 + buttonNumber);
547 mplayer_put_key(l_vo->key_fifo, MOUSE_BTN0 + buttonNumber);
553 - (void) applicationWillBecomeActive:(NSNotification *)aNotification
556 [s->window setLevel:s->fullscreen_window_level];
557 [NSApp setPresentationOptions:NSApplicationPresentationHideDock|NSApplicationPresentationHideMenuBar];
558 [s->window makeKeyAndOrderFront:nil];
559 [NSApp activateIgnoringOtherApps: YES];
563 - (void) applicationWillResignActive:(NSNotification *)aNotification
566 [s->window setLevel:s->windowed_window_level];
567 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
571 - (void) applicationDidFinishLaunching:(NSNotification*)notification
573 // Install an event handler so the Quit menu entry works
574 // The proper way using NSApp setDelegate: and
575 // applicationShouldTerminate: does not work,
576 // probably NSApplication never installs its handler.
577 [[NSAppleEventManager sharedAppleEventManager]
579 andSelector:@selector(handleQuitEvent:withReplyEvent:)
580 forEventClass:kCoreEventClass
581 andEventID:kAEQuitApplication];
587 [self setContentSize:s->current_video_size keepCentered:YES];
590 - (void) halfSize { [self mulSize:0.5f];}
592 - (void) doubleSize { [self mulSize:2.0f];}
594 - (void) mulSize:(float)multiplier
597 NSSize size = [[self contentView] frame].size;
598 size.width = s->current_video_size.width * (multiplier);
599 size.height = s->current_video_size.height * (multiplier);
600 [self setContentSize:size keepCentered:YES];
604 - (void) setContentSize:(NSSize)ns keepCentered:(BOOL)keepCentered
607 NSRect nf = [self frame];
608 NSRect vf = [[self screen] visibleFrame];
609 int title_height = nf.size.height - [[self contentView] bounds].size.height;
610 double ratio = (double)ns.width / (double)ns.height;
612 // clip the new size to the visibleFrame's size if needed
613 if (ns.width > vf.size.width || ns.height + title_height > vf.size.height) {
615 ns.height -= title_height; // make space for the title bar
617 if (ns.width > ns.height) {
618 ns.height = ((double)ns.width * 1/ratio + 0.5);
620 ns.width = ((double)ns.height * ratio + 0.5);
624 int dw = nf.size.width - ns.width;
625 int dh = nf.size.height - ns.height - title_height;
627 nf.origin.x += dw / 2;
628 nf.origin.y += dh / 2;
630 [self setFrame: NSMakeRect(nf.origin.x, nf.origin.y, ns.width, ns.height + title_height) display:YES animate:NO];
632 [self setContentSize:ns];
637 @implementation GLMPlayerOpenGLView
638 - (void) drawRect: (NSRect)rect
640 [[NSColor clearColor] set];
641 NSRectFill([self bounds]);