| 1 |
/**************************************************************************/ |
| 2 |
/* godot_application.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "godot_application.h" |
| 32 |
|
| 33 |
#import "display_server_macos.h" |
| 34 |
#import "godot_application_delegate.h" |
| 35 |
#import "os_macos.h" |
| 36 |
|
| 37 |
GodotApplication *GodotApp = nil; |
| 38 |
|
| 39 |
@interface GodotApplication () |
| 40 |
- (void)forceUnbundledWindowActivationHackStep1; |
| 41 |
- (void)forceUnbundledWindowActivationHackStep2; |
| 42 |
- (void)forceUnbundledWindowActivationHackStep3; |
| 43 |
@end |
| 44 |
|
| 45 |
@implementation GodotApplication |
| 46 |
|
| 47 |
- (GodotApplication *)init { |
| 48 |
self = [super init]; |
| 49 |
|
| 50 |
GodotApp = self; |
| 51 |
|
| 52 |
return self; |
| 53 |
} |
| 54 |
|
| 55 |
- (GodotApplicationDelegate *)godotDelegate { |
| 56 |
return (GodotApplicationDelegate *)self.delegate; |
| 57 |
} |
| 58 |
|
| 59 |
- (void)activateApplication { |
| 60 |
[NSApp activateIgnoringOtherApps:YES]; |
| 61 |
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; |
| 62 |
const char *bundled_id = getenv("__CFBundleIdentifier"); |
| 63 |
NSString *nsbundleid_env = [NSString stringWithUTF8String:(bundled_id != nullptr) ? bundled_id : ""]; |
| 64 |
NSString *nsbundleid = [[NSBundle mainBundle] bundleIdentifier]; |
| 65 |
if (nsappname == nil || isatty(STDOUT_FILENO) || isatty(STDIN_FILENO) || isatty(STDERR_FILENO) || ![nsbundleid isEqualToString:nsbundleid_env]) { |
| 66 |
#ifdef TOOLS_ENABLED |
| 67 |
if (!OS_MacOS::is_debugger_attached()) { |
| 68 |
#else |
| 69 |
{ |
| 70 |
#endif |
| 71 |
// If the executable is started from terminal or is not bundled, macOS WindowServer won't register and activate app window correctly (menu and title bar are grayed out and input ignored). |
| 72 |
[self performSelector:@selector(forceUnbundledWindowActivationHackStep1) withObject:nil afterDelay:0.02]; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
- (void)mediaKeyEvent:(int)key state:(BOOL)state repeat:(BOOL)repeat { |
| 78 |
Key keycode = Key::NONE; |
| 79 |
switch (key) { |
| 80 |
case NX_KEYTYPE_SOUND_UP: { |
| 81 |
keycode = Key::VOLUMEUP; |
| 82 |
} break; |
| 83 |
case NX_KEYTYPE_SOUND_DOWN: { |
| 84 |
keycode = Key::VOLUMEUP; |
| 85 |
} break; |
| 86 |
//NX_KEYTYPE_BRIGHTNESS_UP |
| 87 |
//NX_KEYTYPE_BRIGHTNESS_DOWN |
| 88 |
case NX_KEYTYPE_CAPS_LOCK: { |
| 89 |
keycode = Key::CAPSLOCK; |
| 90 |
} break; |
| 91 |
case NX_KEYTYPE_HELP: { |
| 92 |
keycode = Key::HELP; |
| 93 |
} break; |
| 94 |
case NX_POWER_KEY: { |
| 95 |
keycode = Key::STANDBY; |
| 96 |
} break; |
| 97 |
case NX_KEYTYPE_MUTE: { |
| 98 |
keycode = Key::VOLUMEMUTE; |
| 99 |
} break; |
| 100 |
//NX_KEYTYPE_CONTRAST_UP |
| 101 |
//NX_KEYTYPE_CONTRAST_DOWN |
| 102 |
//NX_KEYTYPE_LAUNCH_PANEL |
| 103 |
//NX_KEYTYPE_EJECT |
| 104 |
//NX_KEYTYPE_VIDMIRROR |
| 105 |
//NX_KEYTYPE_FAST |
| 106 |
//NX_KEYTYPE_REWIND |
| 107 |
//NX_KEYTYPE_ILLUMINATION_UP |
| 108 |
//NX_KEYTYPE_ILLUMINATION_DOWN |
| 109 |
//NX_KEYTYPE_ILLUMINATION_TOGGLE |
| 110 |
case NX_KEYTYPE_PLAY: { |
| 111 |
keycode = Key::MEDIAPLAY; |
| 112 |
} break; |
| 113 |
case NX_KEYTYPE_NEXT: { |
| 114 |
keycode = Key::MEDIANEXT; |
| 115 |
} break; |
| 116 |
case NX_KEYTYPE_PREVIOUS: { |
| 117 |
keycode = Key::MEDIAPREVIOUS; |
| 118 |
} break; |
| 119 |
default: { |
| 120 |
keycode = Key::NONE; |
| 121 |
} break; |
| 122 |
} |
| 123 |
|
| 124 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 125 |
if (ds && keycode != Key::NONE) { |
| 126 |
DisplayServerMacOS::KeyEvent ke; |
| 127 |
|
| 128 |
ke.window_id = ds->_get_focused_window_or_popup(); |
| 129 |
ke.macos_state = 0; |
| 130 |
ke.pressed = state; |
| 131 |
ke.echo = repeat; |
| 132 |
ke.keycode = keycode; |
| 133 |
ke.physical_keycode = keycode; |
| 134 |
ke.key_label = keycode; |
| 135 |
ke.unicode = 0; |
| 136 |
ke.raw = true; |
| 137 |
|
| 138 |
ds->push_to_key_event_buffer(ke); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
- (void)sendEvent:(NSEvent *)event { |
| 143 |
if ([event type] == NSEventTypeSystemDefined && [event subtype] == 8) { |
| 144 |
int keyCode = (([event data1] & 0xFFFF0000) >> 16); |
| 145 |
int keyFlags = ([event data1] & 0x0000FFFF); |
| 146 |
int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA; |
| 147 |
int keyRepeat = (keyFlags & 0x1); |
| 148 |
|
| 149 |
[self mediaKeyEvent:keyCode state:keyState repeat:keyRepeat]; |
| 150 |
} |
| 151 |
|
| 152 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 153 |
if (ds) { |
| 154 |
if ([event type] == NSEventTypeLeftMouseDown || [event type] == NSEventTypeRightMouseDown || [event type] == NSEventTypeOtherMouseDown) { |
| 155 |
if (ds->mouse_process_popups()) { |
| 156 |
return; |
| 157 |
} |
| 158 |
} |
| 159 |
ds->send_event(event); |
| 160 |
} |
| 161 |
|
| 162 |
// From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost |
| 163 |
// This works around an AppKit bug, where key up events while holding |
| 164 |
// down the command key don't get sent to the key window. |
| 165 |
if ([event type] == NSEventTypeKeyUp && ([event modifierFlags] & NSEventModifierFlagCommand)) { |
| 166 |
[[self keyWindow] sendEvent:event]; |
| 167 |
} else { |
| 168 |
[super sendEvent:event]; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
- (void)forceUnbundledWindowActivationHackStep1 { |
| 173 |
// Step 1: Switch focus to macOS SystemUIServer process. |
| 174 |
// Required to perform step 2, TransformProcessType will fail if app is already the in focus. |
| 175 |
for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.systemuiserver"]) { |
| 176 |
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; |
| 177 |
break; |
| 178 |
} |
| 179 |
[self performSelector:@selector(forceUnbundledWindowActivationHackStep2) |
| 180 |
withObject:nil |
| 181 |
afterDelay:0.02]; |
| 182 |
} |
| 183 |
|
| 184 |
- (void)forceUnbundledWindowActivationHackStep2 { |
| 185 |
// Step 2: Register app as foreground process. |
| 186 |
ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 187 |
(void)TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
| 188 |
[self performSelector:@selector(forceUnbundledWindowActivationHackStep3) withObject:nil afterDelay:0.02]; |
| 189 |
} |
| 190 |
|
| 191 |
- (void)forceUnbundledWindowActivationHackStep3 { |
| 192 |
// Step 3: Switch focus back to app window. |
| 193 |
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps]; |
| 194 |
} |
| 195 |
|
| 196 |
@end |