Debian Codegraph @

drivers/apple_embedded/apple_embedded.mm

raw
1 /**************************************************************************/
2 /* apple_embedded.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 "apple_embedded.h"
32
33 #import "app_delegate_service.h"
34 #import "godot_view_controller.h"
35
36 #import <CoreHaptics/CoreHaptics.h>
37 #import <UIKit/UIKit.h>
38 #include <sys/sysctl.h>
39
40 void AppleEmbedded::_bind_methods() {
41 ClassDB::bind_method(D_METHOD("get_rate_url", "app_id"), &AppleEmbedded::get_rate_url);
42 ClassDB::bind_method(D_METHOD("supports_haptic_engine"), &AppleEmbedded::supports_haptic_engine);
43 ClassDB::bind_method(D_METHOD("start_haptic_engine"), &AppleEmbedded::start_haptic_engine);
44 ClassDB::bind_method(D_METHOD("stop_haptic_engine"), &AppleEmbedded::stop_haptic_engine);
45 }
46
47 bool AppleEmbedded::supports_haptic_engine() {
48 if (@available(iOS 13, *)) {
49 id<CHHapticDeviceCapability> capabilities = [CHHapticEngine capabilitiesForHardware];
50 return capabilities.supportsHaptics;
51 }
52
53 return false;
54 }
55
56 CHHapticEngine *AppleEmbedded::get_haptic_engine_instance() API_AVAILABLE(ios(13)) {
57 if (haptic_engine == nullptr) {
58 NSError *error = nullptr;
59 haptic_engine = [[CHHapticEngine alloc] initAndReturnError:&error];
60
61 if (!error) {
62 [haptic_engine setAutoShutdownEnabled:true];
63 } else {
64 haptic_engine = nullptr;
65 NSLog(@"Could not initialize haptic engine: %@", error);
66 }
67 }
68
69 return haptic_engine;
70 }
71
72 void AppleEmbedded::vibrate_haptic_engine(float p_duration_seconds, float p_amplitude) API_AVAILABLE(ios(13)) {
73 if (@available(iOS 13, *)) { // We need the @available check every time to make the compiler happy...
74 if (supports_haptic_engine()) {
75 CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance();
76 if (cur_haptic_engine) {
77 NSDictionary *hapticDict;
78 if (p_amplitude < 0) {
79 hapticDict = @{
80 CHHapticPatternKeyPattern : @[
81 @{CHHapticPatternKeyEvent : @{
82 CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous,
83 CHHapticPatternKeyTime : @(CHHapticTimeImmediate),
84 CHHapticPatternKeyEventDuration : @(p_duration_seconds),
85 },
86 },
87 ],
88 };
89 } else {
90 hapticDict = @{
91 CHHapticPatternKeyPattern : @[
92 @{CHHapticPatternKeyEvent : @{
93 CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous,
94 CHHapticPatternKeyTime : @(CHHapticTimeImmediate),
95 CHHapticPatternKeyEventDuration : @(p_duration_seconds),
96 CHHapticPatternKeyEventParameters : @[
97 @{
98 CHHapticPatternKeyParameterID : @("HapticIntensity"),
99 CHHapticPatternKeyParameterValue : @(p_amplitude)
100 },
101 ],
102 },
103 },
104 ],
105 };
106 }
107
108 NSError *error;
109 CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error];
110
111 [[cur_haptic_engine createPlayerWithPattern:pattern error:&error] startAtTime:0 error:&error];
112
113 NSLog(@"Could not vibrate using haptic engine: %@", error);
114 }
115
116 return;
117 }
118 }
119
120 NSLog(@"Haptic engine is not supported");
121 }
122
123 void AppleEmbedded::start_haptic_engine() {
124 if (@available(iOS 13, *)) {
125 if (supports_haptic_engine()) {
126 CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance();
127 if (cur_haptic_engine) {
128 [cur_haptic_engine startWithCompletionHandler:^(NSError *returnedError) {
129 if (returnedError) {
130 NSLog(@"Could not start haptic engine: %@", returnedError);
131 }
132 }];
133 }
134
135 return;
136 }
137 }
138
139 NSLog(@"Haptic engine is not supported");
140 }
141
142 void AppleEmbedded::stop_haptic_engine() {
143 if (@available(iOS 13, *)) {
144 if (supports_haptic_engine()) {
145 CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance();
146 if (cur_haptic_engine) {
147 [cur_haptic_engine stopWithCompletionHandler:^(NSError *returnedError) {
148 if (returnedError) {
149 NSLog(@"Could not stop haptic engine: %@", returnedError);
150 }
151 }];
152 }
153
154 return;
155 }
156 }
157
158 NSLog(@"Haptic engine is not supported");
159 }
160
161 void AppleEmbedded::alert(const char *p_alert, const char *p_title) {
162 NSString *title = [NSString stringWithUTF8String:p_title];
163 NSString *message = [NSString stringWithUTF8String:p_alert];
164
165 UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
166 UIAlertAction *button = [UIAlertAction actionWithTitle:@"OK"
167 style:UIAlertActionStyleCancel
168 handler:^(id){
169 }];
170
171 [alert addAction:button];
172
173 [GDTAppDelegateService.viewController presentViewController:alert animated:YES completion:nil];
174 }
175
176 String AppleEmbedded::get_model() const {
177 // [[UIDevice currentDevice] model] only returns "iPad" or "iPhone".
178 size_t size;
179 sysctlbyname("hw.machine", nullptr, &size, nullptr, 0);
180 char *model = (char *)malloc(size);
181 if (model == nullptr) {
182 return "";
183 }
184 sysctlbyname("hw.machine", model, &size, nullptr, 0);
185 NSString *platform = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
186 free(model);
187 const char *str = [platform UTF8String];
188 return String::utf8(str != nullptr ? str : "");
189 }
190
191 String AppleEmbedded::get_rate_url(int p_app_id) const {
192 String app_url_path = "itms-apps://itunes.apple.com/app/idAPP_ID";
193
194 String ret = app_url_path.replace("APP_ID", String::num_int64(p_app_id));
195
196 print_verbose(vformat("Returning rate url %s", ret));
197 return ret;
198 }
199
200 AppleEmbedded::AppleEmbedded() {}