mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 22:58:51 +00:00
Support drag & drop a file to transfer it to device
Signed-off-by: npes87184 <npes87184@gmail.com>
This commit is contained in:
parent
d0f8e425d9
commit
ddc9f583cc
4 changed files with 90 additions and 41 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
#define DEVICE_NAME_FIELD_LENGTH 64
|
#define DEVICE_NAME_FIELD_LENGTH 64
|
||||||
|
#define DEVICE_SDCARD_PATH "/sdcard/"
|
||||||
|
|
||||||
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
|
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
|
||||||
SDL_bool device_read_info(socket_t device_socket, char *name, struct size *frame_size);
|
SDL_bool device_read_info(socket_t device_socket, char *name, struct size *frame_size);
|
||||||
|
|
|
@ -2,57 +2,62 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
#include "device.h"
|
||||||
#include "lockutil.h"
|
#include "lockutil.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
// NOTE(adopi) this can be more generic:
|
void request_free(struct request *req) {
|
||||||
// it could be used with a command queue instead of a filename queue
|
if (!req) {
|
||||||
// then we would have a generic invoker (useful if we want to handle more async commands)
|
return;
|
||||||
|
}
|
||||||
|
SDL_free(req->file);
|
||||||
|
free(req);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_bool file_queue_is_empty(const struct file_queue *queue) {
|
SDL_bool request_queue_is_empty(const struct request_queue *queue) {
|
||||||
return queue->head == queue->tail;
|
return queue->head == queue->tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_queue_is_full(const struct file_queue *queue) {
|
SDL_bool request_queue_is_full(const struct request_queue *queue) {
|
||||||
return (queue->head + 1) % FILE_QUEUE_SIZE == queue->tail;
|
return (queue->head + 1) % REQUEST_QUEUE_SIZE == queue->tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_queue_init(struct file_queue *queue) {
|
SDL_bool request_queue_init(struct request_queue *queue) {
|
||||||
queue->head = 0;
|
queue->head = 0;
|
||||||
queue->tail = 0;
|
queue->tail = 0;
|
||||||
return SDL_TRUE;
|
return SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_queue_destroy(struct file_queue *queue) {
|
void request_queue_destroy(struct request_queue *queue) {
|
||||||
int i = queue->tail;
|
int i = queue->tail;
|
||||||
while (i != queue->head) {
|
while (i != queue->head) {
|
||||||
SDL_free(queue->data[i]);
|
request_free(queue->reqs[i]);
|
||||||
i = (i + 1) % FILE_QUEUE_SIZE;
|
i = (i + 1) % REQUEST_QUEUE_SIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_queue_push(struct file_queue *queue, const char *file) {
|
SDL_bool request_queue_push(struct request_queue *queue, struct request *req) {
|
||||||
if (file_queue_is_full(queue)) {
|
if (request_queue_is_full(queue)) {
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
}
|
}
|
||||||
queue->data[queue->head] = SDL_strdup(file);
|
queue->reqs[queue->head] = req;
|
||||||
queue->head = (queue->head + 1) % FILE_QUEUE_SIZE;
|
queue->head = (queue->head + 1) % REQUEST_QUEUE_SIZE;
|
||||||
return SDL_TRUE;
|
return SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_queue_take(struct file_queue *queue, char **file) {
|
SDL_bool request_queue_take(struct request_queue *queue, struct request **req) {
|
||||||
if (file_queue_is_empty(queue)) {
|
if (request_queue_is_empty(queue)) {
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
}
|
}
|
||||||
// transfer ownership
|
// transfer ownership
|
||||||
*file = queue->data[queue->tail];
|
*req = queue->reqs[queue->tail];
|
||||||
queue->tail = (queue->tail + 1) % FILE_QUEUE_SIZE;
|
queue->tail = (queue->tail + 1) % REQUEST_QUEUE_SIZE;
|
||||||
return SDL_TRUE;
|
return SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_handler_init(struct file_handler *file_handler, const char *serial) {
|
SDL_bool file_handler_init(struct file_handler *file_handler, const char *serial) {
|
||||||
|
|
||||||
if (!file_queue_init(&file_handler->queue)) {
|
if (!request_queue_init(&file_handler->queue)) {
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,12 +93,21 @@ SDL_bool file_handler_init(struct file_handler *file_handler, const char *serial
|
||||||
void file_handler_destroy(struct file_handler *file_handler) {
|
void file_handler_destroy(struct file_handler *file_handler) {
|
||||||
SDL_DestroyCond(file_handler->event_cond);
|
SDL_DestroyCond(file_handler->event_cond);
|
||||||
SDL_DestroyMutex(file_handler->mutex);
|
SDL_DestroyMutex(file_handler->mutex);
|
||||||
file_queue_destroy(&file_handler->queue);
|
request_queue_destroy(&file_handler->queue);
|
||||||
SDL_free((void *) file_handler->serial);
|
SDL_free((void *) file_handler->serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool file_handler_do(struct file_handler *file_handler, const char *file) {
|
static process_t install_apk(const char *serial, const char *file) {
|
||||||
|
return adb_install(serial, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static process_t push_file(const char *serial, const char *file) {
|
||||||
|
return adb_push(serial, file, DEVICE_SDCARD_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_bool file_handler_add_request(struct file_handler *file_handler, const char *file, req_action_t action) {
|
||||||
SDL_bool res;
|
SDL_bool res;
|
||||||
|
struct request *req = (struct request *) malloc(sizeof(struct request));
|
||||||
|
|
||||||
// start file_handler if it's used for the first time
|
// start file_handler if it's used for the first time
|
||||||
if (!file_handler->initialized) {
|
if (!file_handler->initialized) {
|
||||||
|
@ -103,9 +117,18 @@ SDL_bool file_handler_do(struct file_handler *file_handler, const char *file) {
|
||||||
file_handler->initialized = SDL_TRUE;
|
file_handler->initialized = SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGI("Adding request %s", file);
|
||||||
|
req->file = SDL_strdup(file);
|
||||||
|
req->action = action;
|
||||||
|
if (action == INSTALL_APK) {
|
||||||
|
req->func = install_apk;
|
||||||
|
} else {
|
||||||
|
req->func = push_file;
|
||||||
|
}
|
||||||
|
|
||||||
mutex_lock(file_handler->mutex);
|
mutex_lock(file_handler->mutex);
|
||||||
SDL_bool was_empty = file_queue_is_empty(&file_handler->queue);
|
SDL_bool was_empty = request_queue_is_empty(&file_handler->queue);
|
||||||
res = file_queue_push(&file_handler->queue, file);
|
res = request_queue_push(&file_handler->queue, req);
|
||||||
if (was_empty) {
|
if (was_empty) {
|
||||||
cond_signal(file_handler->event_cond);
|
cond_signal(file_handler->event_cond);
|
||||||
}
|
}
|
||||||
|
@ -118,7 +141,7 @@ static int run_file_handler(void *data) {
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
mutex_lock(file_handler->mutex);
|
mutex_lock(file_handler->mutex);
|
||||||
while (!file_handler->stopped && file_queue_is_empty(&file_handler->queue)) {
|
while (!file_handler->stopped && request_queue_is_empty(&file_handler->queue)) {
|
||||||
cond_wait(file_handler->event_cond, file_handler->mutex);
|
cond_wait(file_handler->event_cond, file_handler->mutex);
|
||||||
}
|
}
|
||||||
if (file_handler->stopped) {
|
if (file_handler->stopped) {
|
||||||
|
@ -126,25 +149,32 @@ static int run_file_handler(void *data) {
|
||||||
mutex_unlock(file_handler->mutex);
|
mutex_unlock(file_handler->mutex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
char *current_apk;
|
struct request *req;
|
||||||
|
process_t process;
|
||||||
|
const char *cmd;
|
||||||
#ifdef BUILD_DEBUG
|
#ifdef BUILD_DEBUG
|
||||||
bool non_empty = file_queue_take(&file_handler->queue, ¤t_apk);
|
bool non_empty = request_queue_take(&file_handler->queue, &req);
|
||||||
SDL_assert(non_empty);
|
SDL_assert(non_empty);
|
||||||
#else
|
#else
|
||||||
file_queue_take(&file_handler->queue, ¤t_apk);
|
request_queue_take(&file_handler->queue, &req);
|
||||||
#endif
|
#endif
|
||||||
LOGI("Installing %s...", current_apk);
|
LOGI("Processing %s...", req->file);
|
||||||
process_t process = adb_install(file_handler->serial, current_apk);
|
process = req->func(file_handler->serial, req->file);
|
||||||
file_handler->current_process = process;
|
file_handler->current_process = process;
|
||||||
|
|
||||||
mutex_unlock(file_handler->mutex);
|
mutex_unlock(file_handler->mutex);
|
||||||
|
|
||||||
if (process_check_success(process, "adb install")) {
|
if (req->action == INSTALL_APK) {
|
||||||
LOGI("%s installed successfully", current_apk);
|
cmd = "adb install";
|
||||||
} else {
|
} else {
|
||||||
LOGE("Failed to install %s", current_apk);
|
cmd = "adb push";
|
||||||
}
|
}
|
||||||
SDL_free(current_apk);
|
|
||||||
|
if (process_check_success(process, cmd)) {
|
||||||
|
LOGI("Process %s successfully", req->file);
|
||||||
|
} else {
|
||||||
|
LOGE("Failed to process %s", req->file);
|
||||||
|
}
|
||||||
|
request_free(req);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,21 @@
|
||||||
#include <SDL2/SDL_thread.h>
|
#include <SDL2/SDL_thread.h>
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#define FILE_QUEUE_SIZE 16
|
#define REQUEST_QUEUE_SIZE 16
|
||||||
|
|
||||||
// NOTE(AdoPi) file_queue and control_event can use a generic queue
|
typedef enum {
|
||||||
|
INSTALL_APK = 0,
|
||||||
|
PUSH_FILE,
|
||||||
|
} req_action_t;
|
||||||
|
|
||||||
struct file_queue {
|
struct request {
|
||||||
char *data[FILE_QUEUE_SIZE];
|
process_t (*func)(const char *, const char *);
|
||||||
|
char *file;
|
||||||
|
req_action_t action;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct request_queue {
|
||||||
|
struct request *reqs[REQUEST_QUEUE_SIZE];
|
||||||
int tail;
|
int tail;
|
||||||
int head;
|
int head;
|
||||||
};
|
};
|
||||||
|
@ -24,7 +33,7 @@ struct file_handler {
|
||||||
SDL_bool stopped;
|
SDL_bool stopped;
|
||||||
SDL_bool initialized;
|
SDL_bool initialized;
|
||||||
process_t current_process;
|
process_t current_process;
|
||||||
struct file_queue queue;
|
struct request_queue queue;
|
||||||
};
|
};
|
||||||
|
|
||||||
SDL_bool file_handler_init(struct file_handler *file_handler, const char *serial);
|
SDL_bool file_handler_init(struct file_handler *file_handler, const char *serial);
|
||||||
|
@ -34,6 +43,6 @@ SDL_bool file_handler_start(struct file_handler *file_handler);
|
||||||
void file_handler_stop(struct file_handler *file_handler);
|
void file_handler_stop(struct file_handler *file_handler);
|
||||||
void file_handler_join(struct file_handler *file_handler);
|
void file_handler_join(struct file_handler *file_handler);
|
||||||
|
|
||||||
SDL_bool file_handler_do(struct file_handler *file_handler, const char *filename);
|
SDL_bool file_handler_add_request(struct file_handler *file_handler, const char *filename, req_action_t action);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -56,6 +56,11 @@ static int event_watcher(void *data, SDL_Event *event) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int is_apk(const char *file) {
|
||||||
|
const char *ext = strrchr(file, '.');
|
||||||
|
return ext && !strcmp(ext, ".apk");
|
||||||
|
}
|
||||||
|
|
||||||
static void event_loop(void) {
|
static void event_loop(void) {
|
||||||
#ifdef CONTINUOUS_RESIZING_WORKAROUND
|
#ifdef CONTINUOUS_RESIZING_WORKAROUND
|
||||||
SDL_AddEventWatch(event_watcher, NULL);
|
SDL_AddEventWatch(event_watcher, NULL);
|
||||||
|
@ -105,7 +110,11 @@ static void event_loop(void) {
|
||||||
input_manager_process_mouse_button(&input_manager, &event.button);
|
input_manager_process_mouse_button(&input_manager, &event.button);
|
||||||
break;
|
break;
|
||||||
case SDL_DROPFILE:
|
case SDL_DROPFILE:
|
||||||
file_handler_do(&file_handler, event.drop.file);
|
if (is_apk(event.drop.file)) {
|
||||||
|
file_handler_add_request(&file_handler, event.drop.file, INSTALL_APK);
|
||||||
|
} else {
|
||||||
|
file_handler_add_request(&file_handler, event.drop.file, PUSH_FILE);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue