From 77118a6a7685c25b1ae3b94a2fe9512f9452cb05 Mon Sep 17 00:00:00 2001 From: polymonster Date: Sat, 10 Feb 2024 11:41:13 +0000 Subject: [PATCH] - remote callbacks and improved music player remote setup for ios --- core/pen/include/os.h | 11 ++++- core/pen/include/renderer.h | 1 + core/pen/source/ios/os.mm | 81 +++++++++++++++++++++++++++++------- core/pen/source/renderer.cpp | 17 ++++++++ 4 files changed, 93 insertions(+), 17 deletions(-) diff --git a/core/pen/include/os.h b/core/pen/include/os.h index fff37b8c..1f4654b6 100644 --- a/core/pen/include/os.h +++ b/core/pen/include/os.h @@ -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 { @@ -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 @@ -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); diff --git a/core/pen/include/renderer.h b/core/pen/include/renderer.h index b784cdad..6f3d72e6 100644 --- a/core/pen/include/renderer.h +++ b/core/pen/include/renderer.h @@ -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); diff --git a/core/pen/source/ios/os.mm b/core/pen/source/ios/os.mm index ad1e9b6d..92dd5361 100644 --- a/core/pen/source/ios/os.mm +++ b/core/pen/source/ios/os.mm @@ -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; @@ -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 @@ -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 @@ -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; } @@ -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) @@ -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; + } } diff --git a/core/pen/source/renderer.cpp b/core/pen/source/renderer.cpp index c931fe4f..e87cb3f3 100644 --- a/core/pen/source/renderer.cpp +++ b/core/pen/source/renderer.cpp @@ -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