Files
PojavLauncher_iOS/Natives/utils.m
T

131 lines
3.8 KiB
Objective-C

#include "jni.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "log.h"
#include "utils.h"
BOOL getEntitlementValue(NSString *key);
@implementation UIWindow(ext)
// Simulate safe area on iPhones without notch
/*
- (UIEdgeInsets)safeAreaInsets {
return UIEdgeInsetsMake(0, 44, 21, 44);
}
*/
- (UIViewController *)visibleViewController {
UIViewController *current = self.rootViewController;
while (current.presentedViewController) {
if ([current.presentedViewController isKindOfClass:UIAlertController.class] || [current.presentedViewController isKindOfClass:NSClassFromString(@"UIInputWindowController")]) {
break;
}
current = current.presentedViewController;
}
if ([current isKindOfClass:UINavigationController.class]) {
return [(UINavigationController *)self.rootViewController visibleViewController];
} else {
return current;
}
}
@end
// This forces the navigation bar to keep its height (44dp) in landscape
@implementation UINavigationBar(forceFullHeightInLandscape)
- (BOOL)forceFullHeightInLandscape {
return UIScreen.mainScreen.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPhone;
}
@end
UIWindow* currentWindow() {
id delegate = UIApplication.sharedApplication.delegate;
if (@available(iOS 13.0, *)) {
delegate = UIApplication.sharedApplication.connectedScenes.anyObject.delegate;
}
return [delegate window];
}
UIViewController* currentVC() {
return currentWindow().visibleViewController;
}
BOOL isJITEnabled() {
if (getEntitlementValue(@"dynamic-codesigning")) {
return YES;
}
int flags;
csops(getpid(), 0, &flags, sizeof(flags));
return (flags & CS_DEBUGGED) != 0;
}
NSMutableDictionary* parseJSONFromFile(NSString *path) {
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (content == nil) {
NSLog(@"[ParseJSON] Error: could not read %@: %@", path, error.localizedDescription);
return [@{@"error": error} mutableCopy];
}
NSData* data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error) {
NSLog(@"[ParseJSON] Error: could not parse JSON: %@", error.localizedDescription);
return [@{@"error": error} mutableCopy];
}
return dict;
}
NSError* saveJSONToFile(NSDictionary *dict, NSString *path) {
// TODO: handle rename
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (jsonData == nil) {
return error;
}
BOOL success = [jsonData writeToFile:path options:NSDataWritingAtomic error:&error];
if (!success) {
return error;
}
return nil;
}
CGFloat MathUtils_dist(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2) {
const CGFloat x = (x2 - x1);
const CGFloat y = (y2 - y1);
return (CGFloat) hypot(x, y);
}
//Ported from https://www.arduino.cc/reference/en/language/functions/math/map/
CGFloat MathUtils_map(CGFloat x, CGFloat in_min, CGFloat in_max, CGFloat out_min, CGFloat out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void _CGDataProviderReleaseBytePointerCallback(void *info,const void *pointer) {
}
CGFloat dpToPx(CGFloat dp) {
CGFloat screenScale = [[UIScreen mainScreen] scale];
return dp * screenScale;
}
CGFloat pxToDp(CGFloat px) {
CGFloat screenScale = [[UIScreen mainScreen] scale];
return px / screenScale;
}
void setViewBackgroundColor(UIView* view) {
if(@available(iOS 13.0, *)) {
view.backgroundColor = UIColor.systemBackgroundColor;
} else {
view.backgroundColor = UIColor.whiteColor;
}
}