initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
8
drivers/apple/SCsub
Normal file
8
drivers/apple/SCsub
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
|
||||
# Driver source files
|
||||
env.add_source_files(env.drivers_sources, "*.mm")
|
||||
env.add_source_files(env.drivers_sources, "*.cpp")
|
56
drivers/apple/foundation_helpers.h
Normal file
56
drivers/apple/foundation_helpers.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**************************************************************************/
|
||||
/* foundation_helpers.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <Foundation/NSString.h>
|
||||
|
||||
class String;
|
||||
template <typename T>
|
||||
class CharStringT;
|
||||
|
||||
using CharString = CharStringT<char>;
|
||||
|
||||
namespace conv {
|
||||
|
||||
/**
|
||||
* Converts a Godot String to an NSString without allocating an intermediate UTF-8 buffer.
|
||||
* */
|
||||
NSString *to_nsstring(const String &p_str);
|
||||
/**
|
||||
* Converts a Godot CharString to an NSString without allocating an intermediate UTF-8 buffer.
|
||||
* */
|
||||
NSString *to_nsstring(const CharString &p_str);
|
||||
/**
|
||||
* Converts an NSString to a Godot String without allocating intermediate buffers.
|
||||
* */
|
||||
String to_string(NSString *p_str);
|
||||
|
||||
} //namespace conv
|
85
drivers/apple/foundation_helpers.mm
Normal file
85
drivers/apple/foundation_helpers.mm
Normal file
@@ -0,0 +1,85 @@
|
||||
/**************************************************************************/
|
||||
/* foundation_helpers.mm */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#import "foundation_helpers.h"
|
||||
|
||||
#import "core/string/ustring.h"
|
||||
|
||||
#import <CoreFoundation/CFString.h>
|
||||
|
||||
namespace conv {
|
||||
|
||||
NSString *to_nsstring(const String &p_str) {
|
||||
return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
|
||||
length:p_str.length() * sizeof(char32_t)
|
||||
encoding:NSUTF32LittleEndianStringEncoding];
|
||||
}
|
||||
|
||||
NSString *to_nsstring(const CharString &p_str) {
|
||||
return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
|
||||
length:p_str.length()
|
||||
encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
String to_string(NSString *p_str) {
|
||||
CFStringRef str = (__bridge CFStringRef)p_str;
|
||||
CFStringEncoding fastest = CFStringGetFastestEncoding(str);
|
||||
// Sometimes, CFString will return a pointer to it's encoded data,
|
||||
// so we can create the string without allocating intermediate buffers.
|
||||
const char *p = CFStringGetCStringPtr(str, fastest);
|
||||
if (p) {
|
||||
switch (fastest) {
|
||||
case kCFStringEncodingASCII:
|
||||
return String::ascii(Span(p, CFStringGetLength(str)));
|
||||
case kCFStringEncodingUTF8:
|
||||
return String::utf8(p);
|
||||
case kCFStringEncodingUTF32LE:
|
||||
return String::utf32(Span((char32_t *)p, CFStringGetLength(str)));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CFRange range = CFRangeMake(0, CFStringGetLength(str));
|
||||
CFIndex byte_len = 0;
|
||||
// Try to losslessly convert the string directly into a String's buffer to avoid intermediate allocations.
|
||||
CFIndex n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, nil, 0, &byte_len);
|
||||
if (n == range.length) {
|
||||
String res;
|
||||
res.resize_uninitialized((byte_len / sizeof(char32_t)) + 1);
|
||||
res[n] = 0;
|
||||
n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, (UInt8 *)res.ptrw(), res.length() * sizeof(char32_t), nil);
|
||||
return res;
|
||||
}
|
||||
|
||||
return String::utf8(p_str.UTF8String);
|
||||
}
|
||||
|
||||
} //namespace conv
|
80
drivers/apple/joypad_apple.h
Normal file
80
drivers/apple/joypad_apple.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/**************************************************************************/
|
||||
/* joypad_apple.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input.h"
|
||||
#include "core/input/input_enums.h"
|
||||
|
||||
#define Key _QKey
|
||||
#import <GameController/GameController.h>
|
||||
#undef Key
|
||||
|
||||
@class GCController;
|
||||
class RumbleContext;
|
||||
|
||||
struct GameController {
|
||||
int joy_id;
|
||||
GCController *controller;
|
||||
RumbleContext *rumble_context API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) = nil;
|
||||
NSInteger ff_effect_timestamp = 0;
|
||||
bool force_feedback = false;
|
||||
bool double_nintendo_joycon_layout = false;
|
||||
bool single_nintendo_joycon_layout = false;
|
||||
|
||||
uint32_t axis_changed_mask = 0;
|
||||
static_assert(static_cast<uint32_t>(JoyAxis::MAX) < 32, "JoyAxis::MAX must be less than 32");
|
||||
double axis_value[(int)JoyAxis::MAX];
|
||||
|
||||
GameController(int p_joy_id, GCController *p_controller);
|
||||
~GameController();
|
||||
};
|
||||
|
||||
class JoypadApple {
|
||||
private:
|
||||
id<NSObject> connect_observer = nil;
|
||||
id<NSObject> disconnect_observer = nil;
|
||||
HashMap<int, GameController *> joypads;
|
||||
HashMap<GCController *, int> controller_to_joy_id;
|
||||
|
||||
GCControllerPlayerIndex get_free_player_index();
|
||||
|
||||
void add_joypad(GCController *p_controller);
|
||||
void remove_joypad(GCController *p_controller);
|
||||
|
||||
public:
|
||||
JoypadApple();
|
||||
~JoypadApple();
|
||||
|
||||
void joypad_vibration_start(GameController &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0));
|
||||
void joypad_vibration_stop(GameController &p_joypad, uint64_t p_timestamp) API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0));
|
||||
|
||||
void process_joypads();
|
||||
};
|
634
drivers/apple/joypad_apple.mm
Normal file
634
drivers/apple/joypad_apple.mm
Normal file
@@ -0,0 +1,634 @@
|
||||
/**************************************************************************/
|
||||
/* joypad_apple.mm */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#import "joypad_apple.h"
|
||||
|
||||
#import <CoreHaptics/CoreHaptics.h>
|
||||
#import <os/log.h>
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "main/main.h"
|
||||
|
||||
class API_AVAILABLE(macos(11), ios(14.0), tvos(14.0)) RumbleMotor {
|
||||
CHHapticEngine *engine;
|
||||
id<CHHapticPatternPlayer> player;
|
||||
bool is_started;
|
||||
|
||||
RumbleMotor(GCController *p_controller, GCHapticsLocality p_locality) {
|
||||
engine = [p_controller.haptics createEngineWithLocality:p_locality];
|
||||
engine.autoShutdownEnabled = YES;
|
||||
}
|
||||
|
||||
public:
|
||||
static RumbleMotor *create(GCController *p_controller, GCHapticsLocality p_locality) {
|
||||
if ([p_controller.haptics.supportedLocalities containsObject:p_locality]) {
|
||||
return memnew(RumbleMotor(p_controller, p_locality));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ bool has_active_player() {
|
||||
return player != nil;
|
||||
}
|
||||
|
||||
void execute_pattern(CHHapticPattern *p_pattern) {
|
||||
NSError *error;
|
||||
if (!is_started) {
|
||||
ERR_FAIL_COND_MSG(![engine startAndReturnError:&error], "Couldn't start controller haptic engine: " + String::utf8(error.localizedDescription.UTF8String));
|
||||
is_started = YES;
|
||||
}
|
||||
|
||||
player = [engine createPlayerWithPattern:p_pattern error:&error];
|
||||
ERR_FAIL_COND_MSG(error, "Couldn't create controller haptic pattern player: " + String::utf8(error.localizedDescription.UTF8String));
|
||||
ERR_FAIL_COND_MSG(![player startAtTime:CHHapticTimeImmediate error:&error], "Couldn't execute controller haptic pattern: " + String::utf8(error.localizedDescription.UTF8String));
|
||||
}
|
||||
|
||||
void stop() {
|
||||
id<CHHapticPatternPlayer> old_player = player;
|
||||
player = nil;
|
||||
|
||||
NSError *error;
|
||||
ERR_FAIL_COND_MSG(![old_player stopAtTime:CHHapticTimeImmediate error:&error], "Couldn't stop controller haptic pattern: " + String::utf8(error.localizedDescription.UTF8String));
|
||||
}
|
||||
};
|
||||
|
||||
class API_AVAILABLE(macos(11), ios(14.0), tvos(14.0)) RumbleContext {
|
||||
RumbleMotor *weak_motor;
|
||||
RumbleMotor *strong_motor;
|
||||
|
||||
public:
|
||||
RumbleContext(GCController *p_controller) {
|
||||
weak_motor = RumbleMotor::create(p_controller, GCHapticsLocalityRightHandle);
|
||||
strong_motor = RumbleMotor::create(p_controller, GCHapticsLocalityLeftHandle);
|
||||
}
|
||||
|
||||
~RumbleContext() {
|
||||
if (weak_motor) {
|
||||
memdelete(weak_motor);
|
||||
}
|
||||
if (strong_motor) {
|
||||
memdelete(strong_motor);
|
||||
}
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ bool has_motors() {
|
||||
return weak_motor != nullptr && strong_motor != nullptr;
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ bool has_active_players() {
|
||||
if (!has_motors()) {
|
||||
return false;
|
||||
}
|
||||
return (weak_motor && weak_motor->has_active_player()) || (strong_motor && strong_motor->has_active_player());
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (weak_motor) {
|
||||
weak_motor->stop();
|
||||
}
|
||||
if (strong_motor) {
|
||||
strong_motor->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void play_weak_pattern(CHHapticPattern *p_pattern) {
|
||||
if (weak_motor) {
|
||||
weak_motor->execute_pattern(p_pattern);
|
||||
}
|
||||
}
|
||||
|
||||
void play_strong_pattern(CHHapticPattern *p_pattern) {
|
||||
if (strong_motor) {
|
||||
strong_motor->execute_pattern(p_pattern);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GameController::GameController(int p_joy_id, GCController *p_controller) :
|
||||
joy_id(p_joy_id), controller(p_controller) {
|
||||
force_feedback = NO;
|
||||
|
||||
for (int i = 0; i < (int)JoyAxis::MAX; i++) {
|
||||
axis_value[i] = 0.0;
|
||||
}
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
if (controller.haptics != nil) {
|
||||
// Create a rumble context for the controller.
|
||||
rumble_context = memnew(RumbleContext(p_controller));
|
||||
|
||||
// If the rumble motors aren't available, disable force feedback.
|
||||
force_feedback = rumble_context->has_motors();
|
||||
}
|
||||
}
|
||||
|
||||
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
|
||||
if ([controller.productCategory isEqualToString:@"Switch Pro Controller"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L/R)"]) {
|
||||
double_nintendo_joycon_layout = true;
|
||||
}
|
||||
|
||||
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L)"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (R)"]) {
|
||||
single_nintendo_joycon_layout = true;
|
||||
}
|
||||
}
|
||||
|
||||
int l_joy_id = joy_id;
|
||||
|
||||
auto BUTTON = [l_joy_id](JoyButton p_button) {
|
||||
return ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
Input::get_singleton()->joy_button(l_joy_id, p_button, pressed);
|
||||
};
|
||||
};
|
||||
|
||||
auto JOYSTICK_LEFT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
if (axis_value[(int)JoyAxis::LEFT_X] != xValue) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::LEFT_X);
|
||||
axis_value[(int)JoyAxis::LEFT_X] = xValue;
|
||||
}
|
||||
if (axis_value[(int)JoyAxis::LEFT_Y] != -yValue) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::LEFT_Y);
|
||||
axis_value[(int)JoyAxis::LEFT_Y] = -yValue;
|
||||
}
|
||||
};
|
||||
|
||||
auto JOYSTICK_RIGHT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
|
||||
if (axis_value[(int)JoyAxis::RIGHT_X] != xValue) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::RIGHT_X);
|
||||
axis_value[(int)JoyAxis::RIGHT_X] = xValue;
|
||||
}
|
||||
if (axis_value[(int)JoyAxis::RIGHT_Y] != -yValue) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::RIGHT_Y);
|
||||
axis_value[(int)JoyAxis::RIGHT_Y] = -yValue;
|
||||
}
|
||||
};
|
||||
|
||||
auto TRIGGER_LEFT = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
if (axis_value[(int)JoyAxis::TRIGGER_LEFT] != value) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::TRIGGER_LEFT);
|
||||
axis_value[(int)JoyAxis::TRIGGER_LEFT] = value;
|
||||
}
|
||||
};
|
||||
|
||||
auto TRIGGER_RIGHT = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
|
||||
if (axis_value[(int)JoyAxis::TRIGGER_RIGHT] != value) {
|
||||
axis_changed_mask |= (1 << (int)JoyAxis::TRIGGER_RIGHT);
|
||||
axis_value[(int)JoyAxis::TRIGGER_RIGHT] = value;
|
||||
}
|
||||
};
|
||||
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
if (controller.physicalInputProfile != nil) {
|
||||
GCPhysicalInputProfile *profile = controller.physicalInputProfile;
|
||||
|
||||
GCControllerButtonInput *buttonA = profile.buttons[GCInputButtonA];
|
||||
GCControllerButtonInput *buttonB = profile.buttons[GCInputButtonB];
|
||||
GCControllerButtonInput *buttonX = profile.buttons[GCInputButtonX];
|
||||
GCControllerButtonInput *buttonY = profile.buttons[GCInputButtonY];
|
||||
if (double_nintendo_joycon_layout) {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
} else if (single_nintendo_joycon_layout) {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
} else {
|
||||
if (buttonA) {
|
||||
buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
}
|
||||
if (buttonB) {
|
||||
buttonB.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
}
|
||||
if (buttonX) {
|
||||
buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
}
|
||||
if (buttonY) {
|
||||
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftThumbstickButton = profile.buttons[GCInputLeftThumbstickButton];
|
||||
GCControllerButtonInput *rightThumbstickButton = profile.buttons[GCInputRightThumbstickButton];
|
||||
if (leftThumbstickButton) {
|
||||
leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK);
|
||||
}
|
||||
if (rightThumbstickButton) {
|
||||
rightThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::RIGHT_STICK);
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftShoulder = profile.buttons[GCInputLeftShoulder];
|
||||
GCControllerButtonInput *rightShoulder = profile.buttons[GCInputRightShoulder];
|
||||
if (leftShoulder) {
|
||||
leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER);
|
||||
}
|
||||
if (rightShoulder) {
|
||||
rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER);
|
||||
}
|
||||
|
||||
GCControllerButtonInput *leftTrigger = profile.buttons[GCInputLeftTrigger];
|
||||
GCControllerButtonInput *rightTrigger = profile.buttons[GCInputRightTrigger];
|
||||
if (leftTrigger) {
|
||||
leftTrigger.valueChangedHandler = TRIGGER_LEFT;
|
||||
}
|
||||
if (rightTrigger) {
|
||||
rightTrigger.valueChangedHandler = TRIGGER_RIGHT;
|
||||
}
|
||||
|
||||
GCControllerButtonInput *buttonMenu = profile.buttons[GCInputButtonMenu];
|
||||
GCControllerButtonInput *buttonHome = profile.buttons[GCInputButtonHome];
|
||||
GCControllerButtonInput *buttonOptions = profile.buttons[GCInputButtonOptions];
|
||||
if (buttonMenu) {
|
||||
buttonMenu.pressedChangedHandler = BUTTON(JoyButton::START);
|
||||
}
|
||||
if (buttonHome) {
|
||||
buttonHome.pressedChangedHandler = BUTTON(JoyButton::GUIDE);
|
||||
}
|
||||
if (buttonOptions) {
|
||||
buttonOptions.pressedChangedHandler = BUTTON(JoyButton::BACK);
|
||||
}
|
||||
|
||||
// Xbox controller buttons.
|
||||
if (@available(macOS 12.0, iOS 15.0, tvOS 15.0, *)) {
|
||||
GCControllerButtonInput *buttonShare = profile.buttons[GCInputButtonShare];
|
||||
if (buttonShare) {
|
||||
buttonShare.pressedChangedHandler = BUTTON(JoyButton::MISC1);
|
||||
}
|
||||
}
|
||||
|
||||
GCControllerButtonInput *paddleButton1 = profile.buttons[GCInputXboxPaddleOne];
|
||||
GCControllerButtonInput *paddleButton2 = profile.buttons[GCInputXboxPaddleTwo];
|
||||
GCControllerButtonInput *paddleButton3 = profile.buttons[GCInputXboxPaddleThree];
|
||||
GCControllerButtonInput *paddleButton4 = profile.buttons[GCInputXboxPaddleFour];
|
||||
if (paddleButton1) {
|
||||
paddleButton1.pressedChangedHandler = BUTTON(JoyButton::PADDLE1);
|
||||
}
|
||||
if (paddleButton2) {
|
||||
paddleButton2.pressedChangedHandler = BUTTON(JoyButton::PADDLE2);
|
||||
}
|
||||
if (paddleButton3) {
|
||||
paddleButton3.pressedChangedHandler = BUTTON(JoyButton::PADDLE3);
|
||||
}
|
||||
if (paddleButton4) {
|
||||
paddleButton4.pressedChangedHandler = BUTTON(JoyButton::PADDLE4);
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *leftThumbstick = profile.dpads[GCInputLeftThumbstick];
|
||||
if (leftThumbstick) {
|
||||
leftThumbstick.valueChangedHandler = JOYSTICK_LEFT;
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *rightThumbstick = profile.dpads[GCInputRightThumbstick];
|
||||
if (rightThumbstick) {
|
||||
rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT;
|
||||
}
|
||||
|
||||
GCControllerDirectionPad *dpad = nil;
|
||||
if (controller.extendedGamepad != nil) {
|
||||
dpad = controller.extendedGamepad.dpad;
|
||||
} else if (controller.microGamepad != nil) {
|
||||
dpad = controller.microGamepad.dpad;
|
||||
}
|
||||
if (dpad) {
|
||||
dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP);
|
||||
dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN);
|
||||
dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT);
|
||||
dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT);
|
||||
}
|
||||
}
|
||||
} else if (controller.extendedGamepad != nil) {
|
||||
GCExtendedGamepad *gamepad = controller.extendedGamepad;
|
||||
|
||||
if (double_nintendo_joycon_layout) {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
} else if (single_nintendo_joycon_layout) {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
} else {
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::B);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y);
|
||||
}
|
||||
|
||||
gamepad.leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER);
|
||||
gamepad.rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER);
|
||||
gamepad.dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP);
|
||||
gamepad.dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN);
|
||||
gamepad.dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT);
|
||||
gamepad.dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT);
|
||||
|
||||
gamepad.leftThumbstick.valueChangedHandler = JOYSTICK_LEFT;
|
||||
gamepad.rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT;
|
||||
gamepad.leftTrigger.valueChangedHandler = TRIGGER_LEFT;
|
||||
gamepad.rightTrigger.valueChangedHandler = TRIGGER_RIGHT;
|
||||
|
||||
if (@available(macOS 10.14.1, iOS 12.1, tvOS 12.1, *)) {
|
||||
gamepad.leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK);
|
||||
gamepad.rightThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::RIGHT_STICK);
|
||||
}
|
||||
|
||||
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
|
||||
gamepad.buttonOptions.pressedChangedHandler = BUTTON(JoyButton::BACK);
|
||||
gamepad.buttonMenu.pressedChangedHandler = BUTTON(JoyButton::START);
|
||||
}
|
||||
|
||||
if (@available(macOS 11, iOS 14.0, tvOS 14.0, *)) {
|
||||
gamepad.buttonHome.pressedChangedHandler = BUTTON(JoyButton::GUIDE);
|
||||
if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
|
||||
GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
|
||||
xboxGamepad.paddleButton1.pressedChangedHandler = BUTTON(JoyButton::PADDLE1);
|
||||
xboxGamepad.paddleButton2.pressedChangedHandler = BUTTON(JoyButton::PADDLE2);
|
||||
xboxGamepad.paddleButton3.pressedChangedHandler = BUTTON(JoyButton::PADDLE3);
|
||||
xboxGamepad.paddleButton4.pressedChangedHandler = BUTTON(JoyButton::PADDLE4);
|
||||
}
|
||||
}
|
||||
|
||||
if (@available(macOS 12, iOS 15.0, tvOS 15.0, *)) {
|
||||
if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
|
||||
GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
|
||||
xboxGamepad.buttonShare.pressedChangedHandler = BUTTON(JoyButton::MISC1);
|
||||
}
|
||||
}
|
||||
} else if (controller.microGamepad != nil) {
|
||||
GCMicroGamepad *gamepad = controller.microGamepad;
|
||||
|
||||
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A);
|
||||
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X);
|
||||
gamepad.dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP);
|
||||
gamepad.dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN);
|
||||
gamepad.dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT);
|
||||
gamepad.dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT);
|
||||
}
|
||||
|
||||
// TODO: Need to add support for controller.motion which gives us access to
|
||||
// the orientation of the device (if supported).
|
||||
}
|
||||
|
||||
GameController::~GameController() {
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
if (rumble_context) {
|
||||
memdelete(rumble_context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JoypadApple::JoypadApple() {
|
||||
connect_observer = [NSNotificationCenter.defaultCenter
|
||||
addObserverForName:GCControllerDidConnectNotification
|
||||
object:nil
|
||||
queue:NSOperationQueue.mainQueue
|
||||
usingBlock:^(NSNotification *notification) {
|
||||
GCController *controller = notification.object;
|
||||
if (!controller) {
|
||||
return;
|
||||
}
|
||||
add_joypad(controller);
|
||||
}];
|
||||
|
||||
disconnect_observer = [NSNotificationCenter.defaultCenter
|
||||
addObserverForName:GCControllerDidDisconnectNotification
|
||||
object:nil
|
||||
queue:NSOperationQueue.mainQueue
|
||||
usingBlock:^(NSNotification *notification) {
|
||||
GCController *controller = notification.object;
|
||||
if (!controller) {
|
||||
return;
|
||||
}
|
||||
remove_joypad(controller);
|
||||
}];
|
||||
|
||||
if (@available(macOS 11.3, iOS 14.5, tvOS 14.5, *)) {
|
||||
GCController.shouldMonitorBackgroundEvents = YES;
|
||||
}
|
||||
}
|
||||
|
||||
JoypadApple::~JoypadApple() {
|
||||
for (KeyValue<int, GameController *> &E : joypads) {
|
||||
memdelete(E.value);
|
||||
E.value = nullptr;
|
||||
}
|
||||
|
||||
[NSNotificationCenter.defaultCenter removeObserver:connect_observer];
|
||||
[NSNotificationCenter.defaultCenter removeObserver:disconnect_observer];
|
||||
}
|
||||
|
||||
// Finds the rightmost set bit in a number, n.
|
||||
// variation of https://www.geeksforgeeks.org/position-of-rightmost-set-bit/
|
||||
int rightmost_one(int n) {
|
||||
return __builtin_ctz(n & -n) + 1;
|
||||
}
|
||||
|
||||
GCControllerPlayerIndex JoypadApple::get_free_player_index() {
|
||||
// player_set will be a bitfield where each bit represents a player index.
|
||||
__block uint32_t player_set = 0;
|
||||
for (const KeyValue<GCController *, int> &E : controller_to_joy_id) {
|
||||
player_set |= 1U << E.key.playerIndex;
|
||||
}
|
||||
|
||||
// invert, as we want to find the first unset player index.
|
||||
int n = rightmost_one((int)(~player_set));
|
||||
if (n >= 5) {
|
||||
return GCControllerPlayerIndexUnset;
|
||||
}
|
||||
|
||||
return (GCControllerPlayerIndex)(n - 1);
|
||||
}
|
||||
|
||||
void JoypadApple::add_joypad(GCController *p_controller) {
|
||||
if (controller_to_joy_id.has(p_controller)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get a new id for our controller.
|
||||
int joy_id = Input::get_singleton()->get_unused_joy_id();
|
||||
|
||||
if (joy_id == -1) {
|
||||
print_verbose("Couldn't retrieve new joy ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Assign our player index.
|
||||
if (p_controller.playerIndex == GCControllerPlayerIndexUnset) {
|
||||
p_controller.playerIndex = get_free_player_index();
|
||||
}
|
||||
|
||||
// Tell Godot about our new controller.
|
||||
char const *device_name;
|
||||
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
|
||||
device_name = p_controller.productCategory.UTF8String;
|
||||
} else {
|
||||
device_name = p_controller.vendorName.UTF8String;
|
||||
}
|
||||
Input::get_singleton()->joy_connection_changed(joy_id, true, String::utf8(device_name));
|
||||
|
||||
// Assign our player index.
|
||||
joypads.insert(joy_id, memnew(GameController(joy_id, p_controller)));
|
||||
controller_to_joy_id.insert(p_controller, joy_id);
|
||||
}
|
||||
|
||||
void JoypadApple::remove_joypad(GCController *p_controller) {
|
||||
if (!controller_to_joy_id.has(p_controller)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int joy_id = controller_to_joy_id[p_controller];
|
||||
controller_to_joy_id.erase(p_controller);
|
||||
|
||||
// Tell Godot this joystick is no longer there.
|
||||
Input::get_singleton()->joy_connection_changed(joy_id, false, "");
|
||||
|
||||
// And remove it from our dictionary.
|
||||
GameController **old = joypads.getptr(joy_id);
|
||||
memdelete(*old);
|
||||
*old = nullptr;
|
||||
joypads.erase(joy_id);
|
||||
}
|
||||
|
||||
API_AVAILABLE(macos(10.15), ios(13.0), tvos(14.0))
|
||||
CHHapticPattern *get_vibration_pattern(float p_magnitude, float p_duration) {
|
||||
// Creates a vibration pattern with an intensity and duration.
|
||||
NSDictionary *hapticDict = @{
|
||||
CHHapticPatternKeyPattern : @[
|
||||
@{
|
||||
CHHapticPatternKeyEvent : @{
|
||||
CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous,
|
||||
CHHapticPatternKeyTime : @(CHHapticTimeImmediate),
|
||||
CHHapticPatternKeyEventDuration : [NSNumber numberWithFloat:p_duration],
|
||||
|
||||
CHHapticPatternKeyEventParameters : @[
|
||||
@{
|
||||
CHHapticPatternKeyParameterID : CHHapticEventParameterIDHapticIntensity,
|
||||
CHHapticPatternKeyParameterValue : [NSNumber numberWithFloat:p_magnitude]
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
NSError *error;
|
||||
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error];
|
||||
return pattern;
|
||||
}
|
||||
|
||||
void JoypadApple::joypad_vibration_start(GameController &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
|
||||
if (!p_joypad.force_feedback || p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If there is active vibration players, stop them.
|
||||
if (p_joypad.rumble_context->has_active_players()) {
|
||||
joypad_vibration_stop(p_joypad, p_timestamp);
|
||||
}
|
||||
|
||||
// Gets the default vibration pattern and creates a player for each motor.
|
||||
CHHapticPattern *weak_pattern = get_vibration_pattern(p_weak_magnitude, p_duration);
|
||||
CHHapticPattern *strong_pattern = get_vibration_pattern(p_strong_magnitude, p_duration);
|
||||
|
||||
p_joypad.rumble_context->play_weak_pattern(weak_pattern);
|
||||
p_joypad.rumble_context->play_strong_pattern(strong_pattern);
|
||||
|
||||
p_joypad.ff_effect_timestamp = p_timestamp;
|
||||
}
|
||||
|
||||
void JoypadApple::joypad_vibration_stop(GameController &p_joypad, uint64_t p_timestamp) {
|
||||
if (!p_joypad.force_feedback) {
|
||||
return;
|
||||
}
|
||||
// If there is no active vibration players, exit.
|
||||
if (!p_joypad.rumble_context->has_active_players()) {
|
||||
return;
|
||||
}
|
||||
|
||||
p_joypad.rumble_context->stop();
|
||||
|
||||
p_joypad.ff_effect_timestamp = p_timestamp;
|
||||
}
|
||||
|
||||
void JoypadApple::process_joypads() {
|
||||
Input *input = Input::get_singleton();
|
||||
|
||||
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
|
||||
for (KeyValue<int, GameController *> &E : joypads) {
|
||||
int id = E.key;
|
||||
GameController &joypad = *E.value;
|
||||
|
||||
uint32_t changed = joypad.axis_changed_mask;
|
||||
joypad.axis_changed_mask = 0;
|
||||
// Loop over changed axes.
|
||||
while (changed) {
|
||||
// Find the index of the next set bit.
|
||||
uint32_t i = (uint32_t)__builtin_ctzll(changed);
|
||||
// Clear the set bit.
|
||||
changed &= (changed - 1);
|
||||
input->joy_axis(id, (JoyAxis)i, joypad.axis_value[i]);
|
||||
}
|
||||
|
||||
if (joypad.force_feedback) {
|
||||
uint64_t timestamp = input->get_joy_vibration_timestamp(id);
|
||||
|
||||
if (timestamp > (unsigned)joypad.ff_effect_timestamp) {
|
||||
Vector2 strength = input->get_joy_vibration_strength(id);
|
||||
float duration = input->get_joy_vibration_duration(id);
|
||||
if (duration == 0) {
|
||||
duration = GCHapticDurationInfinite;
|
||||
}
|
||||
|
||||
if (strength.x == 0 && strength.y == 0) {
|
||||
joypad_vibration_stop(joypad, timestamp);
|
||||
} else {
|
||||
joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
125
drivers/apple/os_log_logger.cpp
Normal file
125
drivers/apple/os_log_logger.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/**************************************************************************/
|
||||
/* os_log_logger.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "os_log_logger.h"
|
||||
|
||||
#include "core/string/print_string.h"
|
||||
|
||||
#include <cstdlib> // For malloc/free
|
||||
|
||||
OsLogLogger::OsLogLogger(const char *p_subsystem) {
|
||||
const char *subsystem = p_subsystem;
|
||||
if (!subsystem) {
|
||||
subsystem = "org.godotengine.godot";
|
||||
os_log_info(OS_LOG_DEFAULT, "Missing subsystem for os_log logging; using %{public}s", subsystem);
|
||||
}
|
||||
|
||||
log = os_log_create(subsystem, "engine");
|
||||
error_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_ERROR));
|
||||
warning_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_WARNING));
|
||||
script_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SCRIPT));
|
||||
shader_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SHADER));
|
||||
}
|
||||
|
||||
void OsLogLogger::logv(const char *p_format, va_list p_list, bool p_err) {
|
||||
constexpr int static_buf_size = 1024;
|
||||
char static_buf[static_buf_size] = { '\0' };
|
||||
char *buf = static_buf;
|
||||
va_list list_copy;
|
||||
va_copy(list_copy, p_list);
|
||||
int len = vsnprintf(buf, static_buf_size, p_format, p_list);
|
||||
if (len >= static_buf_size) {
|
||||
buf = (char *)Memory::alloc_static(len + 1);
|
||||
vsnprintf(buf, len + 1, p_format, list_copy);
|
||||
}
|
||||
va_end(list_copy);
|
||||
|
||||
// Choose appropriate log type based on error flag.
|
||||
os_log_type_t log_type = p_err ? OS_LOG_TYPE_ERROR : OS_LOG_TYPE_INFO;
|
||||
os_log_with_type(log, log_type, "%{public}s", buf);
|
||||
|
||||
if (len >= static_buf_size) {
|
||||
Memory::free_static(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void OsLogLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces) {
|
||||
os_log_t selected_log;
|
||||
switch (p_type) {
|
||||
case ERR_WARNING:
|
||||
selected_log = warning_log;
|
||||
break;
|
||||
case ERR_SCRIPT:
|
||||
selected_log = script_log;
|
||||
break;
|
||||
case ERR_SHADER:
|
||||
selected_log = shader_log;
|
||||
break;
|
||||
case ERR_ERROR:
|
||||
default:
|
||||
selected_log = error_log;
|
||||
break;
|
||||
}
|
||||
const char *err_details;
|
||||
if (p_rationale && *p_rationale) {
|
||||
err_details = p_rationale;
|
||||
} else {
|
||||
err_details = p_code;
|
||||
}
|
||||
|
||||
// Choose log level based on error type.
|
||||
os_log_type_t log_type;
|
||||
switch (p_type) {
|
||||
case ERR_WARNING:
|
||||
log_type = OS_LOG_TYPE_DEFAULT;
|
||||
break;
|
||||
case ERR_ERROR:
|
||||
case ERR_SCRIPT:
|
||||
case ERR_SHADER:
|
||||
default:
|
||||
log_type = OS_LOG_TYPE_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
// Append script backtraces, if any.
|
||||
String back_trace;
|
||||
for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
|
||||
if (backtrace.is_valid() && !backtrace->is_empty()) {
|
||||
back_trace += "\n";
|
||||
back_trace += backtrace->format(strlen(error_type_indent(p_type)));
|
||||
}
|
||||
}
|
||||
|
||||
if (back_trace.is_empty()) {
|
||||
os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s", p_file, p_line, p_function, err_details, p_code);
|
||||
} else {
|
||||
os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s%{public}s", p_file, p_line, p_function, err_details, p_code, back_trace.utf8().ptr());
|
||||
}
|
||||
}
|
55
drivers/apple/os_log_logger.h
Normal file
55
drivers/apple/os_log_logger.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/**************************************************************************/
|
||||
/* os_log_logger.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/logger.h"
|
||||
|
||||
#include <os/log.h>
|
||||
|
||||
/**
|
||||
* @brief Apple unified logging system integration for Godot Engine.
|
||||
*/
|
||||
class OsLogLogger : public Logger {
|
||||
os_log_t log;
|
||||
os_log_t error_log;
|
||||
os_log_t warning_log;
|
||||
os_log_t script_log;
|
||||
os_log_t shader_log;
|
||||
|
||||
public:
|
||||
void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
|
||||
void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, ErrorType p_type = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {}) override;
|
||||
|
||||
/**
|
||||
* @brief Constructs an OsLogLogger with the specified subsystem identifier, which is normally the bundle identifier.
|
||||
*/
|
||||
OsLogLogger(const char *p_subsystem);
|
||||
};
|
129
drivers/apple/thread_apple.cpp
Normal file
129
drivers/apple/thread_apple.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/**************************************************************************/
|
||||
/* thread_apple.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "thread_apple.h"
|
||||
|
||||
#include "core/error/error_macros.h"
|
||||
#include "core/object/script_language.h"
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
|
||||
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
|
||||
|
||||
struct ThreadData {
|
||||
Thread::Callback callback;
|
||||
void *userdata;
|
||||
Thread::ID caller_id;
|
||||
};
|
||||
|
||||
void *Thread::thread_callback(void *p_data) {
|
||||
ThreadData *thread_data = static_cast<ThreadData *>(p_data);
|
||||
|
||||
// Set the caller ID for this thread
|
||||
caller_id = thread_data->caller_id;
|
||||
|
||||
ScriptServer::thread_enter(); // Scripts may need to attach a stack.
|
||||
|
||||
// Call the actual callback
|
||||
thread_data->callback(thread_data->userdata);
|
||||
|
||||
ScriptServer::thread_exit();
|
||||
|
||||
// Clean up
|
||||
memdelete(thread_data);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Error Thread::set_name(const String &p_name) {
|
||||
int err = pthread_setname_np(p_name.utf8().get_data());
|
||||
return err == 0 ? OK : ERR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
|
||||
ERR_FAIL_COND_V_MSG(id != UNASSIGNED_ID, UNASSIGNED_ID, "A Thread object has been re-started without wait_to_finish() having been called on it.");
|
||||
id = id_counter.increment();
|
||||
|
||||
ThreadData *thread_data = memnew(ThreadData);
|
||||
thread_data->callback = p_callback;
|
||||
thread_data->userdata = p_user;
|
||||
thread_data->caller_id = id;
|
||||
|
||||
// Create the thread
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
switch (p_settings.priority) {
|
||||
case PRIORITY_LOW:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_UTILITY, 0);
|
||||
break;
|
||||
case PRIORITY_NORMAL:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
|
||||
break;
|
||||
case PRIORITY_HIGH:
|
||||
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (p_settings.stack_size > 0) {
|
||||
pthread_attr_setstacksize(&attr, p_settings.stack_size);
|
||||
}
|
||||
|
||||
// Create the thread
|
||||
pthread_create(&pthread, &attr, thread_callback, thread_data);
|
||||
|
||||
// Clean up attributes
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void Thread::wait_to_finish() {
|
||||
ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
|
||||
ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
|
||||
|
||||
int err = pthread_join(pthread, nullptr);
|
||||
if (err != 0) {
|
||||
ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread.");
|
||||
}
|
||||
pthread = pthread_t();
|
||||
id = UNASSIGNED_ID;
|
||||
}
|
||||
|
||||
Thread::~Thread() {
|
||||
if (id != UNASSIGNED_ID) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
WARN_PRINT(
|
||||
"A Thread object is being destroyed without its completion having been realized.\n"
|
||||
"Please call wait_to_finish() on it to ensure correct cleanup.");
|
||||
#endif
|
||||
pthread_detach(pthread);
|
||||
}
|
||||
}
|
110
drivers/apple/thread_apple.h
Normal file
110
drivers/apple/thread_apple.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/**************************************************************************/
|
||||
/* thread_apple.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/templates/safe_refcount.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <new> // For hardware interference size
|
||||
|
||||
class String;
|
||||
|
||||
class Thread {
|
||||
public:
|
||||
typedef void (*Callback)(void *p_userdata);
|
||||
|
||||
typedef uint64_t ID;
|
||||
|
||||
enum : ID {
|
||||
UNASSIGNED_ID = 0,
|
||||
MAIN_ID = 1
|
||||
};
|
||||
|
||||
enum Priority {
|
||||
PRIORITY_LOW,
|
||||
PRIORITY_NORMAL,
|
||||
PRIORITY_HIGH
|
||||
};
|
||||
|
||||
struct Settings {
|
||||
Priority priority;
|
||||
/// Override the default stack size (0 means default)
|
||||
uint64_t stack_size = 0;
|
||||
Settings() { priority = PRIORITY_NORMAL; }
|
||||
};
|
||||
|
||||
#if defined(__cpp_lib_hardware_interference_size)
|
||||
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
|
||||
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
|
||||
GODOT_GCC_WARNING_POP
|
||||
#else
|
||||
// At a negligible memory cost, we use a conservatively high value.
|
||||
static constexpr size_t CACHE_LINE_BYTES = 128;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class Main;
|
||||
|
||||
ID id = UNASSIGNED_ID;
|
||||
pthread_t pthread;
|
||||
|
||||
static SafeNumeric<uint64_t> id_counter;
|
||||
static thread_local ID caller_id;
|
||||
|
||||
static void *thread_callback(void *p_data);
|
||||
|
||||
static void make_main_thread() { caller_id = MAIN_ID; }
|
||||
static void release_main_thread() { caller_id = id_counter.increment(); }
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ static void yield() { pthread_yield_np(); }
|
||||
|
||||
_FORCE_INLINE_ ID get_id() const { return id; }
|
||||
// get the ID of the caller thread
|
||||
_FORCE_INLINE_ static ID get_caller_id() {
|
||||
return caller_id;
|
||||
}
|
||||
// get the ID of the main thread
|
||||
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
|
||||
|
||||
_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; }
|
||||
|
||||
static Error set_name(const String &p_name);
|
||||
|
||||
ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
|
||||
bool is_started() const { return id != UNASSIGNED_ID; }
|
||||
/// Waits until thread is finished, and deallocates it.
|
||||
void wait_to_finish();
|
||||
|
||||
Thread() = default;
|
||||
~Thread();
|
||||
};
|
Reference in New Issue
Block a user