Skip to content

Commit

Permalink
- remote callbacks and improved music player remote setup for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Feb 10, 2024
1 parent dca633b commit 77118a6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
11 changes: 9 additions & 2 deletions core/pen/include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ namespace pen
void os_show_on_screen_keyboard(bool show);
bool os_set_keychain_item(const Str& identifier, const Str& key, const Str& value);
Str os_get_keychain_item(const Str& identifier, const Str& key);

bool os_is_backgrounded();

// music
struct music_item
{
Expand All @@ -65,6 +66,12 @@ namespace pen
void* internal;
};

struct music_player_remote {
void (*pause)(bool) = nullptr;
void (*next)(bool) = nullptr;
void (*tick)(void) = nullptr;
};

struct music_file
{
f32* pcm_data; // interleaved stereo or mono channel pcm
Expand All @@ -76,7 +83,7 @@ namespace pen
const music_item* music_get_items(); // returns stretchy buffer use sb_count for num items
music_file music_open_file(const music_item& item);
void music_close_file(const music_file& file);
void music_enable_remote_control();
void music_enable_remote_control(const music_player_remote& fns);
void music_set_now_playing(const Str& artist, const Str& album, const Str& track);
void music_set_now_playing_artwork(void* data, u32 w, u32 h, u32 bpp, u32 row_pitch);
void music_set_now_playing_time_info(u32 position_ms, u32 duration_ms);
Expand Down
1 change: 1 addition & 0 deletions core/pen/include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ namespace pen
void renderer_release_sampler(u32 sampler);
void renderer_release_depth_stencil_state(u32 depth_stencil_state);
void renderer_consume_cmd_buffer();
void renderer_consume_cmd_buffer_non_blocking();
void renderer_update_queries();
void renderer_get_present_time(f32& cpu_ms, f32& gpu_ms);

Expand Down
81 changes: 66 additions & 15 deletions core/pen/source/ios/os.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ -(MPRemoteCommandHandlerStatus)next;
pen_text_field_delegate* text_field_delegate = nullptr;
pen_text_field* text_field = nullptr;
bool show_on_screen_keyboard = false;
pen::music_player_remote music_remote;
};
os_context s_context;

Expand All @@ -105,6 +106,18 @@ void update_on_screen_keyboard()
s_open = false;
}
}

void enable_remote_control_internal()
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.pauseCommand addTarget:s_context.app_delegate action:@selector(pause)];
[commandCenter.playCommand addTarget:s_context.app_delegate action:@selector(play)];
[commandCenter.nextTrackCommand addTarget:s_context.app_delegate action:@selector(next)];
[commandCenter.previousTrackCommand addTarget:s_context.app_delegate action:@selector(prev)];
}
}

@implementation pen_app_delegate
Expand Down Expand Up @@ -158,20 +171,39 @@ - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(N
}

-(MPRemoteCommandHandlerStatus)play {
PEN_LOG("Press Play!");
return MPRemoteCommandHandlerStatusSuccess;
if(s_context.music_remote.pause)
{
s_context.music_remote.pause(false);
return MPRemoteCommandHandlerStatusSuccess;
}
return MPRemoteCommandHandlerStatusNoSuchContent;
}

-(MPRemoteCommandHandlerStatus)pause {
PEN_LOG("Press Pause!");
return MPRemoteCommandHandlerStatusSuccess;
if(s_context.music_remote.pause)
{
s_context.music_remote.pause(true);
return MPRemoteCommandHandlerStatusSuccess;
}
return MPRemoteCommandHandlerStatusNoSuchContent;
}

-(MPRemoteCommandHandlerStatus)prev {
PEN_LOG("Press Prev!");
return MPRemoteCommandHandlerStatusSuccess;
if(s_context.music_remote.next)
{
s_context.music_remote.next(true);
return MPRemoteCommandHandlerStatusSuccess;
}
return MPRemoteCommandHandlerStatusNoSuchContent;
}

-(MPRemoteCommandHandlerStatus)next {
PEN_LOG("Press Next!");
return MPRemoteCommandHandlerStatusSuccess;
if(s_context.music_remote.next)
{
s_context.music_remote.next(false);
return MPRemoteCommandHandlerStatusSuccess;
}
return MPRemoteCommandHandlerStatusNoSuchContent;
}
@end

Expand Down Expand Up @@ -351,6 +383,26 @@ bool os_update()
}

update_on_screen_keyboard();

// init audio player main thread
static bool music_remote_init = true;
if(music_remote_init)
{
// next and prev
if(s_context.music_remote.pause && s_context.music_remote.next) {
enable_remote_control_internal();
music_remote_init = false;
}

// tick
if(s_context.music_remote.tick) {
[NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
s_context.music_remote.tick();
}
}];
}
}

return true;
}
Expand Down Expand Up @@ -667,14 +719,9 @@ Str os_get_keychain_item(const Str& identifier, const Str& key)
return "";
}

void music_enable_remote_control()
void music_enable_remote_control(const music_player_remote& fns)
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.pauseCommand addTarget:s_context.app_delegate action:@selector(pause)];
//[commandCenter.pauseCommand addTarget:self action:@selector(pauseAudio)];
s_context.music_remote = fns;
}

void music_set_now_playing(const Str& artist, const Str& album, const Str& track)
Expand Down Expand Up @@ -737,4 +784,8 @@ void music_set_now_playing_time_info(u32 position_ms, u32 duration_ms)
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}
}

bool os_is_backgrounded() {
return [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground;
}
}
17 changes: 17 additions & 0 deletions core/pen/source/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,23 @@ namespace pen
#endif
}

void renderer_consume_cmd_buffer_non_blocking()
{
for (;;)
{
renderer_cmd* cmd = _ctx->cmd_buffer.check();
if(cmd) {
cmd = _ctx->cmd_buffer.get();
if (cmd)
exec_cmd(*cmd);
}
else
{
break;
}
}
}

void new_frame_internal()
{
// free slots we have now deleted the resources for
Expand Down

0 comments on commit 77118a6

Please sign in to comment.