mirror of https://github.com/Genymobile/scrcpy
Add --time-limit
Add an option to stop scrcpy automatically after a given delay. PR #4052 <https://github.com/Genymobile/scrcpy/pull/4052> Fixes #3752 <https://github.com/Genymobile/scrcpy/issues/3752>pull/4052/head
parent
5042f8de93
commit
d3c2955fb9
@ -0,0 +1,77 @@
|
||||
#include "timeout.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
bool
|
||||
sc_timeout_init(struct sc_timeout *timeout) {
|
||||
bool ok = sc_mutex_init(&timeout->mutex);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ok = sc_cond_init(&timeout->cond);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
timeout->stopped = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int
|
||||
run_timeout(void *data) {
|
||||
struct sc_timeout *timeout = data;
|
||||
sc_tick deadline = timeout->deadline;
|
||||
|
||||
sc_mutex_lock(&timeout->mutex);
|
||||
bool timed_out = false;
|
||||
while (!timeout->stopped && !timed_out) {
|
||||
timed_out = !sc_cond_timedwait(&timeout->cond, &timeout->mutex,
|
||||
deadline);
|
||||
}
|
||||
sc_mutex_unlock(&timeout->mutex);
|
||||
|
||||
timeout->cbs->on_timeout(timeout, timeout->cbs_userdata);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
sc_timeout_start(struct sc_timeout *timeout, sc_tick deadline,
|
||||
const struct sc_timeout_callbacks *cbs, void *cbs_userdata) {
|
||||
bool ok = sc_thread_create(&timeout->thread, run_timeout, "scrcpy-timeout",
|
||||
timeout);
|
||||
if (!ok) {
|
||||
LOGE("Timeout: could not start thread");
|
||||
return false;
|
||||
}
|
||||
|
||||
timeout->deadline = deadline;
|
||||
|
||||
assert(cbs && cbs->on_timeout);
|
||||
timeout->cbs = cbs;
|
||||
timeout->cbs_userdata = cbs_userdata;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
sc_timeout_stop(struct sc_timeout *timeout) {
|
||||
sc_mutex_lock(&timeout->mutex);
|
||||
timeout->stopped = true;
|
||||
sc_mutex_unlock(&timeout->mutex);
|
||||
}
|
||||
|
||||
void
|
||||
sc_timeout_join(struct sc_timeout *timeout) {
|
||||
sc_thread_join(&timeout->thread, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
sc_timeout_destroy(struct sc_timeout *timeout) {
|
||||
sc_mutex_destroy(&timeout->mutex);
|
||||
sc_cond_destroy(&timeout->cond);
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
#ifndef SC_TIMEOUT_H
|
||||
#define SC_TIMEOUT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "tick.h"
|
||||
|
||||
struct sc_timeout {
|
||||
sc_thread thread;
|
||||
sc_tick deadline;
|
||||
|
||||
sc_mutex mutex;
|
||||
sc_cond cond;
|
||||
bool stopped;
|
||||
|
||||
const struct sc_timeout_callbacks *cbs;
|
||||
void *cbs_userdata;
|
||||
};
|
||||
|
||||
struct sc_timeout_callbacks {
|
||||
void (*on_timeout)(struct sc_timeout *timeout, void *userdata);
|
||||
};
|
||||
|
||||
bool
|
||||
sc_timeout_init(struct sc_timeout *timeout);
|
||||
|
||||
void
|
||||
sc_timeout_destroy(struct sc_timeout *timeout);
|
||||
|
||||
bool
|
||||
sc_timeout_start(struct sc_timeout *timeout, sc_tick deadline,
|
||||
const struct sc_timeout_callbacks *cbs, void *cbs_userdata);
|
||||
|
||||
void
|
||||
sc_timeout_stop(struct sc_timeout *timeout);
|
||||
|
||||
void
|
||||
sc_timeout_join(struct sc_timeout *timeout);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue