ios.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) 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. CHHapticPatternKeyPattern : @[
  69. @{CHHapticPatternKeyEvent : @{
  70. CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous,
  71. CHHapticPatternKeyTime : @(CHHapticTimeImmediate),
  72. CHHapticPatternKeyEventDuration : @(p_duration_seconds)
  73. },
  74. },
  75. ],
  76. };
  77. NSError *error;
  78. CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error];
  79. [[cur_haptic_engine createPlayerWithPattern:pattern error:&error] startAtTime:0 error:&error];
  80. NSLog(@"Could not vibrate using haptic engine: %@", error);
  81. }
  82. return;
  83. }
  84. }
  85. NSLog(@"Haptic engine is not supported in this version of iOS");
  86. }
  87. void iOS::start_haptic_engine() {
  88. if (@available(iOS 13, *)) {
  89. if (supports_haptic_engine()) {
  90. CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance();
  91. if (cur_haptic_engine) {
  92. [cur_haptic_engine startWithCompletionHandler:^(NSError *returnedError) {
  93. if (returnedError) {
  94. NSLog(@"Could not start haptic engine: %@", returnedError);
  95. }
  96. }];
  97. }
  98. return;
  99. }
  100. }
  101. NSLog(@"Haptic engine is not supported in this version of iOS");
  102. }
  103. void iOS::stop_haptic_engine() {
  104. if (@available(iOS 13, *)) {
  105. if (supports_haptic_engine()) {
  106. CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance();
  107. if (cur_haptic_engine) {
  108. [cur_haptic_engine stopWithCompletionHandler:^(NSError *returnedError) {
  109. if (returnedError) {
  110. NSLog(@"Could not stop haptic engine: %@", returnedError);
  111. }
  112. }];
  113. }
  114. return;
  115. }
  116. }
  117. NSLog(@"Haptic engine is not supported in this version of iOS");
  118. }
  119. void iOS::alert(const char *p_alert, const char *p_title) {
  120. NSString *title = [NSString stringWithUTF8String:p_title];
  121. NSString *message = [NSString stringWithUTF8String:p_alert];
  122. UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  123. UIAlertAction *button = [UIAlertAction actionWithTitle:@"OK"
  124. style:UIAlertActionStyleCancel
  125. handler:^(id){
  126. }];
  127. [alert addAction:button];
  128. [AppDelegate.viewController presentViewController:alert animated:YES completion:nil];
  129. }
  130. String iOS::get_model() const {
  131. // [[UIDevice currentDevice] model] only returns "iPad" or "iPhone".
  132. size_t size;
  133. sysctlbyname("hw.machine", nullptr, &size, nullptr, 0);
  134. char *model = (char *)malloc(size);
  135. if (model == nullptr) {
  136. return "";
  137. }
  138. sysctlbyname("hw.machine", model, &size, nullptr, 0);
  139. NSString *platform = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
  140. free(model);
  141. const char *str = [platform UTF8String];
  142. return String::utf8(str != nullptr ? str : "");
  143. }
  144. String iOS::get_rate_url(int p_app_id) const {
  145. String app_url_path = "itms-apps://itunes.apple.com/app/idAPP_ID";
  146. String ret = app_url_path.replace("APP_ID", String::num(p_app_id));
  147. print_verbose(vformat("Returning rate url %s", ret));
  148. return ret;
  149. }
  150. iOS::iOS() {}