Skip to content

Commit

Permalink
iOS alarm support
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Dec 15, 2023
1 parent 8b2f683 commit d8905f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Core/gb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Core/gb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion iOS/GBViewController.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <UserNotifications/UserNotifications.h>

typedef enum {
GBRunModeNormal,
Expand All @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions iOS/GBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
action:@selector(rotateCamera)
forControlEvents:UIControlEventTouchUpInside];
[_backgroundView addSubview:_cameraPositionButton];

[UNUserNotificationCenter currentNotificationCenter].delegate = self;

return true;
}
Expand Down Expand Up @@ -532,6 +534,18 @@ - (void)preRun
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
Expand Down Expand Up @@ -564,6 +578,18 @@ - (void)run
_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 */
Expand Down Expand Up @@ -605,6 +631,30 @@ - (void)postRun
[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
Expand Down

0 comments on commit d8905f5

Please sign in to comment.