git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

commit d8905f57bf8c21bb6b062dc18c793eca29d61d51
parent 8b2f683c20edd7245a31584f014b084040c1d428
Author: Lior Halphon <LIJI32@gmail.com>
Date:   Fri, 15 Dec 2023 14:28:19 +0200

iOS alarm support

Diffstat:
MCore/gb.c5+++++
MCore/gb.h1+
MMakefile2+-
MiOS/GBViewController.h5++++-
MiOS/GBViewController.m50++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/Core/gb.c b/Core/gb.c @@ -2018,6 +2018,11 @@ unsigned GB_time_to_alarm(GB_gameboy_t *gb) return alarm_time - current_time; } +bool GB_rom_supports_alarms(GB_gameboy_t *gb) +{ + return gb->cartridge_type->mbc_type == GB_HUC3; +} + bool GB_has_accelerometer(GB_gameboy_t *gb) { return gb->cartridge_type->mbc_type == GB_MBC7; diff --git a/Core/gb.h b/Core/gb.h @@ -969,6 +969,7 @@ void GB_disconnect_serial(GB_gameboy_t *gb); GB_accessory_t GB_get_built_in_accessory(GB_gameboy_t *gb); /* For cartridges with an alarm clock */ +bool GB_rom_supports_alarms(GB_gameboy_t *gb); unsigned GB_time_to_alarm(GB_gameboy_t *gb); // 0 if no alarm /* For cartridges motion controls */ diff --git a/Makefile b/Makefile @@ -240,7 +240,7 @@ LDFLAGS += -arch arm64 OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) LDFLAGS += -miphoneos-version-min=11.0 -isysroot $(SYSROOT) REREGISTER_LDFLAGS := $(LDFLAGS) -lobjc -framework CoreServices -framework Foundation -LDFLAGS += -lobjc -framework UIKit -framework Foundation -framework CoreGraphics -framework Metal -framework MetalKit -framework AudioToolbox -framework AVFoundation -framework QuartzCore -framework CoreMotion -framework CoreVideo -framework CoreMedia -framework CoreImage -weak_framework CoreHaptics +LDFLAGS += -lobjc -framework UIKit -framework Foundation -framework CoreGraphics -framework Metal -framework MetalKit -framework AudioToolbox -framework AVFoundation -framework QuartzCore -framework CoreMotion -framework CoreVideo -framework CoreMedia -framework CoreImage -framework UserNotifications -weak_framework CoreHaptics CODESIGN := codesign -fs - else ifeq ($(PLATFORM),Darwin) diff --git a/iOS/GBViewController.h b/iOS/GBViewController.h @@ -1,5 +1,6 @@ #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> +#import <UserNotifications/UserNotifications.h> typedef enum { GBRunModeNormal, @@ -8,7 +9,9 @@ typedef enum { GBRunModePaused, } GBRunMode; -@interface GBViewController : UIViewController <UIApplicationDelegate, AVCaptureVideoDataOutputSampleBufferDelegate> +@interface GBViewController : UIViewController <UIApplicationDelegate, + AVCaptureVideoDataOutputSampleBufferDelegate, + UNUserNotificationCenterDelegate> @property (nonatomic, strong) UIWindow *window; - (void)reset; - (void)openLibrary; diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m @@ -244,6 +244,8 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) action:@selector(rotateCamera) forControlEvents:UIControlEventTouchUpInside]; [_backgroundView addSubview:_cameraPositionButton]; + + [UNUserNotificationCenter currentNotificationCenter].delegate = self; return true; } @@ -532,6 +534,18 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) GB_set_accelerometer_values(&_gb, -data.x, data.y); }]; } + + /* Clear pending alarms, don't play alarms while playing */ + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBNotificationsUsed"]) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center removeDeliveredNotificationsWithIdentifiers:@[[GBROMManager sharedManager].romFile]]; + [center removePendingNotificationRequestsWithIdentifiers:@[[GBROMManager sharedManager].romFile]]; + } + + if (GB_rom_supports_alarms(&_gb)) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:nil]; + } } - (void)run @@ -564,6 +578,18 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) _stopping = false; } +- (void)userNotificationCenter:(UNUserNotificationCenter *)center +didReceiveNotificationResponse:(UNNotificationResponse *)response + withCompletionHandler:(void (^)(void))completionHandler +{ + if (![response.notification.request.identifier isEqual:[GBROMManager sharedManager].currentROM]) { + [self application:[UIApplication sharedApplication] + openURL:[NSURL fileURLWithPath:response.notification.request.identifier] + options:@{}]; + } + completionHandler(); +} + - (UIImage *)imageFromData:(NSData *)data width:(unsigned)width height:(unsigned)height { /* Convert the screenshot to a CGImageRef */ @@ -605,6 +631,30 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [self saveStateToFile:[GBROMManager sharedManager].autosaveStateFile]; [[GBHapticManager sharedManager] setRumbleStrength:0]; [_motionManager stopAccelerometerUpdates]; + + unsigned timeToAlarm = GB_time_to_alarm(&_gb); + + if (timeToAlarm) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + + UNMutableNotificationContent *notificationContent = [[UNMutableNotificationContent alloc] init]; + NSString *friendlyName = [[[GBROMManager sharedManager].romFile lastPathComponent] stringByDeletingPathExtension]; + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^)]+\\)|\\[[^\\]]+\\]" options:0 error:nil]; + friendlyName = [regex stringByReplacingMatchesInString:friendlyName options:0 range:NSMakeRange(0, [friendlyName length]) withTemplate:@""]; + friendlyName = [friendlyName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + notificationContent.title = [NSString stringWithFormat:@"%@ Played an Alarm", friendlyName]; + notificationContent.body = [NSString stringWithFormat:@"%@ requested your attention by playing a scheduled alarm", friendlyName]; + notificationContent.sound = UNNotificationSound.defaultSound; + + UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[GBROMManager sharedManager].romFile + content:notificationContent + trigger:[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeToAlarm repeats:false]]; + + + [center addNotificationRequest:request withCompletionHandler:nil]; + [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"GBNotificationsUsed"]; + } } - (void)start

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.