ios.mm 7.2 KB

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