diff --git a/app/src/apkinstaller.c b/app/src/apkinstaller.c index 4a4a8240..eb76c92d 100644 --- a/app/src/apkinstaller.c +++ b/app/src/apkinstaller.c @@ -70,8 +70,7 @@ SDL_bool installer_init(struct installer *installer, const char* serial) { installer->serial = SDL_strdup(serial); } -// TODO(adopi) -// installer->initialized = SDL_TRUE; + installer->initialized = SDL_TRUE; installer->stopped = SDL_FALSE; return SDL_TRUE; @@ -82,6 +81,8 @@ void installer_destroy(struct installer *installer) { SDL_DestroyMutex(installer->mutex); apk_queue_destroy(&installer->queue); SDL_free((void *) installer->serial); + installer->initialized = SDL_FALSE; + installer->stopped = SDL_FALSE; } SDL_bool installer_push_apk(struct installer *installer, const char* apk) { diff --git a/app/src/apkinstaller.h b/app/src/apkinstaller.h index fd04fb07..7e072963 100644 --- a/app/src/apkinstaller.h +++ b/app/src/apkinstaller.h @@ -25,9 +25,20 @@ struct installer { SDL_mutex *mutex; SDL_cond *event_cond; SDL_bool stopped; + SDL_bool initialized; struct apk_queue queue; }; +#define INSTALLER_INITIALIZER { \ + .serial = NULL, \ + .thread = NULL, \ + .mutex = NULL, \ + .event_cond = NULL, \ + .stopped = SDL_FALSE, \ + .initialized = SDL_FALSE \ +} + + SDL_bool installer_init(struct installer *installer, const char* serial); void installer_destroy(struct installer *installer); diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index f955a2d6..f7b79fd5 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -29,7 +29,7 @@ static struct screen screen = SCREEN_INITIALIZER; static struct frames frames; static struct decoder decoder; static struct controller controller; -static struct installer installer; +static struct installer installer = INSTALLER_INITIALIZER; static struct input_manager input_manager = { .controller = &controller, @@ -105,7 +105,15 @@ static void event_loop(void) { input_manager_process_mouse_button(&input_manager, &event.button); break; case SDL_DROPFILE: - // TODO(adopi) init here + + if (!installer.initialized) { + SDL_bool init_ok = installer_init(&installer, server.serial); + if (init_ok && installer_start(&installer)) { + goto push; + } + installer_destroy(&installer); + } + push: installer_push_apk(&installer, event.drop.file); break; } @@ -173,16 +181,6 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b goto finally_destroy_controller; } - // TODO(adopi) init the installer when we really want to use it - if (!installer_init(&installer, serial)) { - ret = SDL_FALSE; - goto finally_stop_and_join_installer; - } - - if (!installer_start(&installer)) { - ret = SDL_FALSE; - goto finally_destroy_installer; - } if (!screen_init_rendering(&screen, device_name, frame_size)) { ret = SDL_FALSE; @@ -198,13 +196,14 @@ finally_stop_and_join_controller: controller_join(&controller); finally_destroy_controller: controller_destroy(&controller); -finally_stop_and_join_installer: - installer_stop(&installer); - installer_join(&installer); -finally_destroy_installer: - installer_destroy(&installer); finally_stop_decoder: decoder_stop(&decoder); + // stop installer + if (installer.initialized) { + installer_stop(&installer); + installer_join(&installer); + installer_destroy(&installer); + } // stop the server before decoder_join() to wake up the decoder server_stop(&server); decoder_join(&decoder);