2020-07-15 20:59:57 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_iphone.mm */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-07-15 20:59:57 +02:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#ifdef IPHONE_ENABLED
|
|
|
|
|
|
|
|
#include "os_iphone.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-07-15 20:59:57 +02:00
|
|
|
#import "app_delegate.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2020-07-15 20:59:57 +02:00
|
|
|
#include "core/io/file_access_pack.h"
|
|
|
|
#include "display_server_iphone.h"
|
|
|
|
#include "drivers/unix/syslog_logger.h"
|
|
|
|
#import "godot_view.h"
|
|
|
|
#include "main/main.h"
|
|
|
|
#import "view_controller.h"
|
|
|
|
|
|
|
|
#import <AudioToolbox/AudioServices.h>
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import <dlfcn.h>
|
2020-12-27 01:50:21 +01:00
|
|
|
#include <sys/sysctl.h>
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
#if defined(VULKAN_ENABLED)
|
2020-12-04 19:26:24 +01:00
|
|
|
#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
|
2020-07-15 20:59:57 +02:00
|
|
|
#import <QuartzCore/CAMetalLayer.h>
|
2021-08-12 13:24:54 +02:00
|
|
|
#ifdef USE_VOLK
|
|
|
|
#include <volk.h>
|
|
|
|
#else
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
#endif
|
2020-07-15 20:59:57 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Initialization order between compilation units is not guaranteed,
|
|
|
|
// so we use this as a hack to ensure certain code is called before
|
|
|
|
// everything else, but after all units are initialized.
|
|
|
|
typedef void (*init_callback)();
|
|
|
|
static init_callback *ios_init_callbacks = nullptr;
|
|
|
|
static int ios_init_callbacks_count = 0;
|
|
|
|
static int ios_init_callbacks_capacity = 0;
|
|
|
|
HashMap<String, void *> OSIPhone::dynamic_symbol_lookup_table;
|
|
|
|
|
|
|
|
void add_ios_init_callback(init_callback cb) {
|
|
|
|
if (ios_init_callbacks_count == ios_init_callbacks_capacity) {
|
|
|
|
void *new_ptr = realloc(ios_init_callbacks, sizeof(cb) * 32);
|
|
|
|
if (new_ptr) {
|
|
|
|
ios_init_callbacks = (init_callback *)(new_ptr);
|
|
|
|
ios_init_callbacks_capacity += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ios_init_callbacks_capacity > ios_init_callbacks_count) {
|
|
|
|
ios_init_callbacks[ios_init_callbacks_count] = cb;
|
|
|
|
++ios_init_callbacks_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_dynamic_symbol(char *name, void *address) {
|
|
|
|
OSIPhone::dynamic_symbol_lookup_table[String(name)] = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSIPhone *OSIPhone::get_singleton() {
|
|
|
|
return (OSIPhone *)OS::get_singleton();
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:54:12 +01:00
|
|
|
OSIPhone::OSIPhone(String p_data_dir, String p_cache_dir) {
|
2020-07-15 20:59:57 +02:00
|
|
|
for (int i = 0; i < ios_init_callbacks_count; ++i) {
|
|
|
|
ios_init_callbacks[i]();
|
|
|
|
}
|
|
|
|
free(ios_init_callbacks);
|
|
|
|
ios_init_callbacks = nullptr;
|
|
|
|
ios_init_callbacks_count = 0;
|
|
|
|
ios_init_callbacks_capacity = 0;
|
|
|
|
|
|
|
|
main_loop = nullptr;
|
|
|
|
|
|
|
|
// can't call set_data_dir from here, since it requires DirAccess
|
|
|
|
// which is initialized in initialize_core
|
|
|
|
user_data_dir = p_data_dir;
|
2021-02-22 22:54:12 +01:00
|
|
|
cache_dir = p_cache_dir;
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
Vector<Logger *> loggers;
|
|
|
|
loggers.push_back(memnew(SyslogLogger));
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
// it seems iOS app's stdout/stderr is only obtainable if you launch it from
|
|
|
|
// Xcode
|
|
|
|
loggers.push_back(memnew(StdLogger));
|
|
|
|
#endif
|
|
|
|
_set_logger(memnew(CompositeLogger(loggers)));
|
|
|
|
|
|
|
|
AudioDriverManager::add_driver(&audio_driver);
|
|
|
|
|
|
|
|
DisplayServerIPhone::register_iphone_driver();
|
|
|
|
}
|
|
|
|
|
|
|
|
OSIPhone::~OSIPhone() {}
|
|
|
|
|
2021-07-22 18:23:48 +02:00
|
|
|
void OSIPhone::alert(const String &p_alert, const String &p_title) {
|
|
|
|
const CharString utf8_alert = p_alert.utf8();
|
|
|
|
const CharString utf8_title = p_title.utf8();
|
|
|
|
iOS::alert(utf8_alert.get_data(), utf8_title.get_data());
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:59:57 +02:00
|
|
|
void OSIPhone::initialize_core() {
|
|
|
|
OS_Unix::initialize_core();
|
|
|
|
|
|
|
|
set_user_data_dir(user_data_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::initialize() {
|
|
|
|
initialize_core();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::initialize_modules() {
|
|
|
|
ios = memnew(iOS);
|
|
|
|
Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", ios));
|
|
|
|
|
|
|
|
joypad_iphone = memnew(JoypadIPhone);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::deinitialize_modules() {
|
|
|
|
if (joypad_iphone) {
|
|
|
|
memdelete(joypad_iphone);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ios) {
|
|
|
|
memdelete(ios);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::set_main_loop(MainLoop *p_main_loop) {
|
|
|
|
main_loop = p_main_loop;
|
|
|
|
|
|
|
|
if (main_loop) {
|
2020-12-22 10:50:29 +01:00
|
|
|
main_loop->initialize();
|
2020-07-15 20:59:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OSIPhone::get_main_loop() const {
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::delete_main_loop() {
|
|
|
|
if (main_loop) {
|
2020-12-22 10:50:29 +01:00
|
|
|
main_loop->finalize();
|
2020-07-15 20:59:57 +02:00
|
|
|
memdelete(main_loop);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
main_loop = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OSIPhone::iterate() {
|
|
|
|
if (!main_loop) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DisplayServer::get_singleton()) {
|
|
|
|
DisplayServer::get_singleton()->process_events();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::start() {
|
|
|
|
Main::start();
|
|
|
|
|
|
|
|
if (joypad_iphone) {
|
|
|
|
joypad_iphone->start_processing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::finalize() {
|
|
|
|
deinitialize_modules();
|
|
|
|
|
|
|
|
// Already gets called
|
2022-02-16 13:56:32 +01:00
|
|
|
//delete_main_loop();
|
2020-07-15 20:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Dynamic Libraries
|
|
|
|
|
|
|
|
Error OSIPhone::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) {
|
|
|
|
if (p_path.length() == 0) {
|
|
|
|
p_library_handle = RTLD_SELF;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
return OS_Unix::open_dynamic_library(p_path, p_library_handle, p_also_set_library_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OSIPhone::close_dynamic_library(void *p_library_handle) {
|
|
|
|
if (p_library_handle == RTLD_SELF) {
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
return OS_Unix::close_dynamic_library(p_library_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OSIPhone::get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional) {
|
|
|
|
if (p_library_handle == RTLD_SELF) {
|
|
|
|
void **ptr = OSIPhone::dynamic_symbol_lookup_table.getptr(p_name);
|
|
|
|
if (ptr) {
|
|
|
|
p_symbol_handle = *ptr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OS_Unix::get_dynamic_library_symbol_handle(p_library_handle, p_name, p_symbol_handle, p_optional);
|
|
|
|
}
|
|
|
|
|
|
|
|
String OSIPhone::get_name() const {
|
|
|
|
return "iOS";
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
String OSIPhone::get_model_name() const {
|
|
|
|
String model = ios->get_model();
|
2022-02-16 13:56:32 +01:00
|
|
|
if (model != "") {
|
2020-07-15 20:59:57 +02:00
|
|
|
return model;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
return OS_Unix::get_model_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OSIPhone::shell_open(String p_uri) {
|
|
|
|
NSString *urlPath = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()];
|
|
|
|
NSURL *url = [NSURL URLWithString:urlPath];
|
|
|
|
|
|
|
|
if (![[UIApplication sharedApplication] canOpenURL:url]) {
|
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:43:20 +02:00
|
|
|
printf("opening url %s\n", p_uri.utf8().get_data());
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
|
|
|
|
|
|
|
|
return OK;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2020-07-15 20:59:57 +02:00
|
|
|
|
|
|
|
void OSIPhone::set_user_data_dir(String p_dir) {
|
|
|
|
DirAccess *da = DirAccess::open(p_dir);
|
|
|
|
|
|
|
|
user_data_dir = da->get_current_dir();
|
2020-07-27 12:43:20 +02:00
|
|
|
printf("setting data dir to %s from %s\n", user_data_dir.utf8().get_data(), p_dir.utf8().get_data());
|
2020-07-15 20:59:57 +02:00
|
|
|
memdelete(da);
|
|
|
|
}
|
|
|
|
|
|
|
|
String OSIPhone::get_user_data_dir() const {
|
|
|
|
return user_data_dir;
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:54:12 +01:00
|
|
|
String OSIPhone::get_cache_path() const {
|
|
|
|
return cache_dir;
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:59:57 +02:00
|
|
|
String OSIPhone::get_locale() const {
|
2020-08-10 14:46:45 +02:00
|
|
|
NSString *preferedLanguage = [NSLocale preferredLanguages].firstObject;
|
|
|
|
|
|
|
|
if (preferedLanguage) {
|
|
|
|
return String::utf8([preferedLanguage UTF8String]).replace("-", "_");
|
|
|
|
}
|
2020-07-15 20:59:57 +02:00
|
|
|
|
2020-08-10 14:46:45 +02:00
|
|
|
NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
|
|
|
|
return String::utf8([localeIdentifier UTF8String]).replace("-", "_");
|
2020-07-15 20:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String OSIPhone::get_unique_id() const {
|
2020-08-10 14:46:45 +02:00
|
|
|
NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
|
|
|
|
return String::utf8([uuid UTF8String]);
|
2020-07-15 20:59:57 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 01:50:21 +01:00
|
|
|
String OSIPhone::get_processor_name() const {
|
|
|
|
char buffer[256];
|
|
|
|
size_t buffer_len = 256;
|
|
|
|
if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, NULL, 0) == 0) {
|
|
|
|
return String::utf8(buffer, buffer_len);
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:59:57 +02:00
|
|
|
void OSIPhone::vibrate_handheld(int p_duration_ms) {
|
|
|
|
// iOS does not support duration for vibration
|
|
|
|
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OSIPhone::_check_internal_feature_support(const String &p_feature) {
|
|
|
|
return p_feature == "mobile";
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::on_focus_out() {
|
|
|
|
if (is_focused) {
|
|
|
|
is_focused = false;
|
|
|
|
|
|
|
|
if (DisplayServerIPhone::get_singleton()) {
|
|
|
|
DisplayServerIPhone::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
[AppDelegate.viewController.godotView stopRendering];
|
|
|
|
|
|
|
|
audio_driver.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSIPhone::on_focus_in() {
|
|
|
|
if (!is_focused) {
|
|
|
|
is_focused = true;
|
|
|
|
|
|
|
|
if (DisplayServerIPhone::get_singleton()) {
|
|
|
|
DisplayServerIPhone::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
[AppDelegate.viewController.godotView startRendering];
|
|
|
|
|
|
|
|
audio_driver.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // IPHONE_ENABLED
|