diff --git a/app/src/input_manager.c b/app/src/input_manager.c index e1e8738c..08531cac 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -128,6 +128,7 @@ sc_input_manager_init(struct sc_input_manager *im, assert(!params->control || (params->mp && params->mp->ops)); im->controller = params->controller; + im->fp = params->fp; im->screen = params->screen; im->kp = params->kp; im->mp = params->mp; @@ -834,6 +835,34 @@ sc_input_manager_process_mouse_wheel(struct sc_input_manager *im, im->mp->ops->process_mouse_scroll(im->mp, &evt); } +static bool +is_apk(const char *file) { + const char *ext = strrchr(file, '.'); + return ext && !strcmp(ext, ".apk"); +} + +static void +sc_input_manager_process_file(struct sc_input_manager *im, + const SDL_DropEvent *event) { + char *file = strdup(event->file); + SDL_free(event->file); + if (!file) { + LOG_OOM(); + return; + } + + enum sc_file_pusher_action action; + if (is_apk(file)) { + action = SC_FILE_PUSHER_ACTION_INSTALL_APK; + } else { + action = SC_FILE_PUSHER_ACTION_PUSH_FILE; + } + bool ok = sc_file_pusher_request(im->fp, action, file); + if (!ok) { + free(file); + } +} + void sc_input_manager_handle_event(struct sc_input_manager *im, SDL_Event *event) { switch (event->type) { @@ -872,5 +901,11 @@ sc_input_manager_handle_event(struct sc_input_manager *im, SDL_Event *event) { case SDL_FINGERUP: sc_input_manager_process_touch(im, &event->tfinger); break; + case SDL_DROPFILE: { + if (!im->control) { + break; + } + sc_input_manager_process_file(im, &event->drop); + } } } diff --git a/app/src/input_manager.h b/app/src/input_manager.h index b28b110f..377835bf 100644 --- a/app/src/input_manager.h +++ b/app/src/input_manager.h @@ -8,6 +8,7 @@ #include #include "controller.h" +#include "file_pusher.h" #include "fps_counter.h" #include "options.h" #include "trait/key_processor.h" @@ -15,6 +16,7 @@ struct sc_input_manager { struct sc_controller *controller; + struct sc_file_pusher *fp; struct sc_screen *screen; struct sc_key_processor *kp; @@ -44,6 +46,7 @@ struct sc_input_manager { struct sc_input_manager_params { struct sc_controller *controller; + struct sc_file_pusher *fp; struct sc_screen *screen; struct sc_key_processor *kp; struct sc_mouse_processor *mp; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 1c03b8c1..0552a057 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -148,12 +148,6 @@ sdl_configure(bool display, bool disable_screensaver) { } } -static bool -is_apk(const char *file) { - const char *ext = strrchr(file, '.'); - return ext && !strcmp(ext, ".apk"); -} - enum event_result { EVENT_RESULT_CONTINUE, EVENT_RESULT_STOPPED_BY_USER, @@ -161,8 +155,7 @@ enum event_result { }; static enum event_result -handle_event(struct scrcpy *s, const struct scrcpy_options *options, - SDL_Event *event) { +handle_event(struct scrcpy *s, SDL_Event *event) { switch (event->type) { case EVENT_STREAM_STOPPED: LOGD("Video stream stopped"); @@ -170,42 +163,17 @@ handle_event(struct scrcpy *s, const struct scrcpy_options *options, case SDL_QUIT: LOGD("User requested to quit"); return EVENT_RESULT_STOPPED_BY_USER; - case SDL_DROPFILE: { - if (!options->control) { - break; - } - char *file = strdup(event->drop.file); - SDL_free(event->drop.file); - if (!file) { - LOGW("Could not strdup drop filename\n"); - break; - } - - enum sc_file_pusher_action action; - if (is_apk(file)) { - action = SC_FILE_PUSHER_ACTION_INSTALL_APK; - } else { - action = SC_FILE_PUSHER_ACTION_PUSH_FILE; - } - bool ok = sc_file_pusher_request(&s->file_pusher, action, file); - if (!ok) { - free(file); - } - goto end; - } } sc_screen_handle_event(&s->screen, event); - -end: return EVENT_RESULT_CONTINUE; } static bool -event_loop(struct scrcpy *s, const struct scrcpy_options *options) { +event_loop(struct scrcpy *s) { SDL_Event event; while (SDL_WaitEvent(&event)) { - enum event_result result = handle_event(s, options, &event); + enum event_result result = handle_event(s, &event); switch (result) { case EVENT_RESULT_STOPPED_BY_USER: return true; @@ -409,11 +377,14 @@ scrcpy(struct scrcpy_options *options) { const char *serial = s->server.params.serial; assert(serial); + struct sc_file_pusher *fp = NULL; + if (options->display && options->control) { if (!sc_file_pusher_init(&s->file_pusher, serial, options->push_target)) { goto end; } + fp = &s->file_pusher; file_pusher_initialized = true; } @@ -578,6 +549,7 @@ aoa_hid_end: struct sc_screen_params screen_params = { .controller = &s->controller, + .fp = fp, .kp = kp, .mp = mp, .control = options->control, @@ -627,7 +599,7 @@ aoa_hid_end: } stream_started = true; - ret = event_loop(s, options); + ret = event_loop(s); LOGD("quit..."); // Close the window immediately on closing, because screen_destroy() may diff --git a/app/src/screen.c b/app/src/screen.c index b1abe985..92afafbb 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -485,6 +485,7 @@ sc_screen_init(struct sc_screen *screen, struct sc_input_manager_params im_params = { .controller = params->controller, + .fp = params->fp, .screen = screen, .kp = params->kp, .mp = params->mp, diff --git a/app/src/screen.h b/app/src/screen.h index 8af53a5e..677cf514 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -70,6 +70,7 @@ struct sc_screen { struct sc_screen_params { struct sc_controller *controller; + struct sc_file_pusher *fp; struct sc_key_processor *kp; struct sc_mouse_processor *mp;