| 1 |
/**************************************************************************/ |
| 2 |
/* display_server_ios.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 "display_server_ios.h" |
| 32 |
|
| 33 |
#import "device_metrics.h" |
| 34 |
|
| 35 |
#import <UIKit/UIKit.h> |
| 36 |
#import <sys/utsname.h> |
| 37 |
|
| 38 |
DisplayServerIOS *DisplayServerIOS::get_singleton() { |
| 39 |
return (DisplayServerIOS *)DisplayServerAppleEmbedded::get_singleton(); |
| 40 |
} |
| 41 |
|
| 42 |
DisplayServerIOS::DisplayServerIOS(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) : |
| 43 |
DisplayServerAppleEmbedded(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error) { |
| 44 |
} |
| 45 |
|
| 46 |
DisplayServerIOS::~DisplayServerIOS() { |
| 47 |
} |
| 48 |
|
| 49 |
DisplayServer *DisplayServerIOS::create_func(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) { |
| 50 |
return memnew(DisplayServerIOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error)); |
| 51 |
} |
| 52 |
|
| 53 |
void DisplayServerIOS::register_ios_driver() { |
| 54 |
register_create_function("iOS", create_func, get_rendering_drivers_func); |
| 55 |
} |
| 56 |
|
| 57 |
String DisplayServerIOS::get_name() const { |
| 58 |
return "iOS"; |
| 59 |
} |
| 60 |
|
| 61 |
int DisplayServerIOS::screen_get_dpi(int p_screen) const { |
| 62 |
p_screen = _get_screen_index(p_screen); |
| 63 |
int screen_count = get_screen_count(); |
| 64 |
ERR_FAIL_INDEX_V(p_screen, screen_count, 72); |
| 65 |
|
| 66 |
struct utsname systemInfo; |
| 67 |
uname(&systemInfo); |
| 68 |
|
| 69 |
NSString *string = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; |
| 70 |
|
| 71 |
NSDictionary *iOSModelToDPI = [GDTDeviceMetrics dpiList]; |
| 72 |
|
| 73 |
for (NSArray *keyArray in iOSModelToDPI) { |
| 74 |
if ([keyArray containsObject:string]) { |
| 75 |
NSNumber *value = iOSModelToDPI[keyArray]; |
| 76 |
return [value intValue]; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// If device wasn't found in dictionary |
| 81 |
// make a best guess from device metrics. |
| 82 |
CGFloat scale = [UIScreen mainScreen].scale; |
| 83 |
|
| 84 |
UIUserInterfaceIdiom idiom = [UIDevice currentDevice].userInterfaceIdiom; |
| 85 |
|
| 86 |
switch (idiom) { |
| 87 |
case UIUserInterfaceIdiomPad: |
| 88 |
return scale == 2 ? 264 : 132; |
| 89 |
case UIUserInterfaceIdiomPhone: { |
| 90 |
if (scale == 3) { |
| 91 |
CGFloat nativeScale = [UIScreen mainScreen].nativeScale; |
| 92 |
return nativeScale == 3 ? 458 : 401; |
| 93 |
} |
| 94 |
|
| 95 |
return 326; |
| 96 |
} |
| 97 |
default: |
| 98 |
return 72; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
float DisplayServerIOS::screen_get_refresh_rate(int p_screen) const { |
| 103 |
p_screen = _get_screen_index(p_screen); |
| 104 |
int screen_count = get_screen_count(); |
| 105 |
ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 106 |
|
| 107 |
float fps = [UIScreen mainScreen].maximumFramesPerSecond; |
| 108 |
if ([NSProcessInfo processInfo].lowPowerModeEnabled) { |
| 109 |
fps = 60; |
| 110 |
} |
| 111 |
return fps; |
| 112 |
} |
| 113 |
|
| 114 |
float DisplayServerIOS::screen_get_scale(int p_screen) const { |
| 115 |
p_screen = _get_screen_index(p_screen); |
| 116 |
int screen_count = get_screen_count(); |
| 117 |
ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f); |
| 118 |
|
| 119 |
return [UIScreen mainScreen].scale; |
| 120 |
} |