From f8235ff5c155ff97488850e19c60e65b6712c3f2 Mon Sep 17 00:00:00 2001 From: Jean Guyomarc'h Date: Wed, 23 Jul 2014 16:56:16 +0200 Subject: [PATCH] ecore_cocoa: Lion fullscreen workaround + warning fixes Summary: Warnings and deprecated code fixes. I started to implement the OSX-Lion fullscreen style. Reviewers: cedric, naguirre, raster, raoulh Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D1175 --- src/lib/ecore_cocoa/ecore_cocoa.m | 181 +++++++++++++--------- src/lib/ecore_cocoa/ecore_cocoa_private.h | 2 +- src/lib/ecore_cocoa/ecore_cocoa_window.h | 4 + src/lib/ecore_cocoa/ecore_cocoa_window.m | 84 +++++----- 4 files changed, 154 insertions(+), 117 deletions(-) diff --git a/src/lib/ecore_cocoa/ecore_cocoa.m b/src/lib/ecore_cocoa/ecore_cocoa.m index d2303e7fb9..2b1bf08946 100644 --- a/src/lib/ecore_cocoa/ecore_cocoa.m +++ b/src/lib/ecore_cocoa/ecore_cocoa.m @@ -75,6 +75,20 @@ _ecore_cocoa_event_modifiers(unsigned int mod) return modifiers; } +static inline Eina_Bool +_nsevent_window_is_type_of(NSEvent *event, Class class) +{ + /* An NSPeriodic event has no window (undefined behaviour) */ + if ([event type] == NSPeriodic) return EINA_FALSE; + return [[[event window] class] isKindOfClass:class]; +} + +static inline Eina_Bool +_has_ecore_cocoa_window(NSEvent *event) +{ + return _nsevent_window_is_type_of(event, [EcoreCocoaWindow class]); +} + EAPI void ecore_cocoa_feed_events(void) { @@ -96,24 +110,31 @@ ecore_cocoa_feed_events(void) case NSRightMouseDragged: case NSOtherMouseDragged: { - Ecore_Event_Mouse_Move * ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)); - if (!ev) return; + if (_has_ecore_cocoa_window(event)) + { + Ecore_Event_Mouse_Move * ev = calloc(1, sizeof(Ecore_Event_Mouse_Move)); + if (!ev) return; - EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; - NSView *view = [window contentView]; - NSPoint pt = [event locationInWindow]; + EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; + NSView *view = [window contentView]; + NSPoint pt = [event locationInWindow]; - ev->x = pt.x; - ev->y = [view frame].size.height - pt.y; - ev->root.x = ev->x; - ev->root.y = ev->y; - ev->timestamp = time; - ev->window = window.ecore_window_data; - ev->event_window = ev->window; - ev->modifiers = 0; /* FIXME: keep modifier around. */ - - ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL); + ev->x = pt.x; + ev->y = [view frame].size.height - pt.y; + ev->root.x = ev->x; + ev->root.y = ev->y; + ev->timestamp = time; + ev->window = window.ecore_window_data; + ev->event_window = ev->window; + ev->modifiers = 0; /* FIXME: keep modifier around. */ + ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL); + } + else + { + // We might want to handle cases such as events on the menubar. + // If so, let's do it here. + } [NSApp sendEvent:event]; // pass along mouse events, for window manager break; } @@ -121,40 +142,47 @@ ecore_cocoa_feed_events(void) case NSRightMouseDown: case NSOtherMouseDown: { - Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)); - if (!ev) return; + if (_has_ecore_cocoa_window(event)) + { + Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)); + if (!ev) return; - EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; - NSView *view = [window contentView]; - NSPoint pt = [event locationInWindow]; + EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; + NSView *view = [window contentView]; + NSPoint pt = [event locationInWindow]; - ev->x = pt.x; - ev->y = [view frame].size.height - pt.y; - ev->root.x = ev->x; - ev->root.y = ev->y; - ev->timestamp = time; - switch ([event buttonNumber]) - { - case 0: ev->buttons = 1; break; - case 1: ev->buttons = 3; break; - case 2: ev->buttons = 2; break; - default: ev->buttons = 0; break; - } - ev->window = window.ecore_window_data; - ev->event_window = ev->window; + ev->x = pt.x; + ev->y = [view frame].size.height - pt.y; + ev->root.x = ev->x; + ev->root.y = ev->y; + ev->timestamp = time; + switch ([event buttonNumber]) + { + case 0: ev->buttons = 1; break; + case 1: ev->buttons = 3; break; + case 2: ev->buttons = 2; break; + default: ev->buttons = 0; break; + } + ev->window = window.ecore_window_data; + ev->event_window = ev->window; - if ([event clickCount] == 2) - ev->double_click = 1; + if ([event clickCount] == 2) + ev->double_click = 1; + else + ev->double_click = 0; + + if ([event clickCount] >= 3) + ev->triple_click = 1; + else + ev->triple_click = 0; + + ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL); + } else - ev->double_click = 0; - - if ([event clickCount] >= 3) - ev->triple_click = 1; - else - ev->triple_click = 0; - - ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL); - + { + // We might want to handle cases such as events on the menubar. + // If so, let's do it here. + } [NSApp sendEvent:event]; // pass along mouse events, for window manager break; } @@ -165,37 +193,44 @@ ecore_cocoa_feed_events(void) Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button)); if (!ev) return; - EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; - NSView *view = [window contentView]; - NSPoint pt = [event locationInWindow]; + if (_has_ecore_cocoa_window(event)) + { + EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window]; + NSView *view = [window contentView]; + NSPoint pt = [event locationInWindow]; - ev->x = pt.x; - ev->y = [view frame].size.height - pt.y; - ev->root.x = ev->x; - ev->root.y = ev->y; - ev->timestamp = time; - switch ([event buttonNumber]) - { - case 0: ev->buttons = 1; break; - case 1: ev->buttons = 3; break; - case 2: ev->buttons = 2; break; - default: ev->buttons = 0; break; - } - ev->window = window.ecore_window_data; - ev->event_window = ev->window; + ev->x = pt.x; + ev->y = [view frame].size.height - pt.y; + ev->root.x = ev->x; + ev->root.y = ev->y; + ev->timestamp = time; + switch ([event buttonNumber]) + { + case 0: ev->buttons = 1; break; + case 1: ev->buttons = 3; break; + case 2: ev->buttons = 2; break; + default: ev->buttons = 0; break; + } + ev->window = window.ecore_window_data; + ev->event_window = ev->window; - if ([event clickCount] == 2) - ev->double_click = 1; + if ([event clickCount] == 2) + ev->double_click = 1; + else + ev->double_click = 0; + + if ([event clickCount] >= 3) + ev->triple_click = 1; + else + ev->triple_click = 0; + + ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL); + } else - ev->double_click = 0; - - if ([event clickCount] >= 3) - ev->triple_click = 1; - else - ev->triple_click = 0; - - ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL); - + { + // We might want to handle cases such as events on the menubar. + // If so, let's do it here. + } [NSApp sendEvent:event]; // pass along mouse events, for window manager break; } diff --git a/src/lib/ecore_cocoa/ecore_cocoa_private.h b/src/lib/ecore_cocoa/ecore_cocoa_private.h index 7cd08be9d2..41310d43d5 100644 --- a/src/lib/ecore_cocoa/ecore_cocoa_private.h +++ b/src/lib/ecore_cocoa/ecore_cocoa_private.h @@ -3,7 +3,7 @@ struct _Ecore_Cocoa_Window { - NSWindow *window; + EcoreCocoaWindow *window; unsigned int borderless : 1; }; diff --git a/src/lib/ecore_cocoa/ecore_cocoa_window.h b/src/lib/ecore_cocoa/ecore_cocoa_window.h index 6778c70133..911c729ab8 100644 --- a/src/lib/ecore_cocoa/ecore_cocoa_window.h +++ b/src/lib/ecore_cocoa/ecore_cocoa_window.h @@ -11,4 +11,8 @@ styleMask: (unsigned int) aStyle backing: (NSBackingStoreType) bufferingType defer: (BOOL) flag; + +- (BOOL)isFullScreen; + @end + diff --git a/src/lib/ecore_cocoa/ecore_cocoa_window.m b/src/lib/ecore_cocoa/ecore_cocoa_window.m index 297a425e23..ab4a78ce13 100644 --- a/src/lib/ecore_cocoa/ecore_cocoa_window.m +++ b/src/lib/ecore_cocoa/ecore_cocoa_window.m @@ -15,9 +15,9 @@ backing: (NSBackingStoreType) bufferingType defer: (BOOL) flag { - if (![super initWithContentRect: contentRect - styleMask: aStyle - backing: bufferingType + if (![super initWithContentRect: contentRect + styleMask: aStyle + backing: bufferingType defer: flag]) return nil; [self setBackgroundColor: [NSColor whiteColor]]; @@ -25,9 +25,16 @@ [self setDelegate:self]; [self setAcceptsMouseMovedEvents:YES]; + [self setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; + return self; } +- (BOOL)isFullScreen +{ + return (([self styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask); +} + - (BOOL)acceptsFirstResponder { return YES; @@ -38,12 +45,12 @@ return YES; } -- (void)windowWillClose:(NSNotification *)notification +- (void)windowWillClose:(NSNotification *) EINA_UNUSED notification { NSLog(@"window is going to be closed"); } -- (void)windowDidResize:(NSNotification *)notif +- (void)windowDidResize:(NSNotification *) EINA_UNUSED notif { Ecore_Cocoa_Event_Video_Resize *event; NSSize size = self.frame.size; @@ -56,7 +63,9 @@ return; } event->w = size.width; - event->h = size.height - ecore_cocoa_titlebar_height_get(); + event->h = size.height - + (([self isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get()); + printf("Is fullscreen: %i\n", [self isFullScreen]); ecore_event_add(ECORE_COCOA_EVENT_RESIZE, event, NULL, NULL); } @@ -66,17 +75,6 @@ #include "Ecore_Cocoa.h" #include "ecore_cocoa_private.h" -static float _title_bar_height(void) -{ - NSRect frame = NSMakeRect (0, 0, 100, 100); - NSRect contentRect; - - contentRect = [NSWindow contentRectForFrameRect: frame - styleMask: NSTitledWindowMask]; - - return (frame.size.height - contentRect.size.height); -} - Ecore_Cocoa_Window * ecore_cocoa_window_new(int x, int y, @@ -85,27 +83,29 @@ ecore_cocoa_window_new(int x, { Ecore_Cocoa_Window *w; - EcoreCocoaWindow *window = [[EcoreCocoaWindow alloc] - initWithContentRect:NSMakeRect(x, y, width, height) - styleMask:(NSTitledWindowMask | - NSClosableWindowMask | - NSResizableWindowMask | - NSMiniaturizableWindowMask) - backing:NSBackingStoreBuffered - defer:NO]; + EcoreCocoaWindow *window = [[EcoreCocoaWindow alloc] initWithContentRect:NSMakeRect(x, y, width, height) + styleMask:(NSTitledWindowMask | + NSClosableWindowMask | + NSResizableWindowMask | + NSMiniaturizableWindowMask) + backing:NSBackingStoreBuffered + defer:NO]; if (!window) return NULL; //Set the process to be a foreground process, //without that it prevents the window to become the key window and - //receive all mouse mouve events. - ProcessSerialNumber psn; - GetCurrentProcess(&psn); - TransformProcessType(&psn, kProcessTransformToForegroundApplication); - SetFrontProcess(&psn); + //receive all mouse mouve events. + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; + [NSApp activateIgnoringOtherApps:YES]; + w = calloc(1, sizeof(Ecore_Cocoa_Window)); + if (w == NULL) + { + return NULL; + } w->window = window; w->borderless = 0; @@ -150,9 +150,9 @@ ecore_cocoa_window_resize(Ecore_Cocoa_Window *window, NSRect win_frame; - win_frame = [window->window frame]; - win_frame.size.height = height + _title_bar_height(); + win_frame.size.height = height + + (([window->window isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get()); win_frame.size.width = width; [window->window setFrame:win_frame display:YES]; @@ -165,16 +165,13 @@ ecore_cocoa_window_move_resize(Ecore_Cocoa_Window *window, int width, int height) { - if (!window) - return; + if (!window) return; NSRect win_frame; - if (!window) - return; - win_frame = [window->window frame]; - win_frame.size.height = height + _title_bar_height(); + win_frame.size.height = height + + (([window->window isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get()); win_frame.size.width = width; win_frame.origin.x = x; win_frame.origin.y = y; @@ -232,15 +229,16 @@ ecore_cocoa_window_view_set(Ecore_Cocoa_Window *window, return; //[[window->window contentView] addSubview:view]; + NSView *v = view; [window->window setContentView:view]; - NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[view frame] - options:NSTrackingMouseMoved | + NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[v frame] + options:NSTrackingMouseMoved | NSTrackingActiveInActiveApp | NSTrackingInVisibleRect - owner:view + owner:v userInfo:nil]; - [view addTrackingArea:area]; - + [v addTrackingArea:area]; + [area release]; }