mirror of
https://github.com/mpv-player/mpv.git
synced 2025-12-28 05:33:14 +00:00
clipboard: add clipboard monitoring API
This adds a clipboard monitoring API for backends which use polling to monitor clipboard updates. --clipboard-monitor option can turn clipboard monitoring on and off at runtime.
This commit is contained in:
@@ -22,12 +22,14 @@
|
||||
|
||||
struct clipboard_opts {
|
||||
bool enabled;
|
||||
bool monitor;
|
||||
};
|
||||
|
||||
#define OPT_BASE_STRUCT struct clipboard_opts
|
||||
const struct m_sub_options clipboard_conf = {
|
||||
.opts = (const struct m_option[]) {
|
||||
{"enable", OPT_BOOL(enabled), .flags = UPDATE_CLIPBOARD},
|
||||
{"monitor", OPT_BOOL(monitor), .flags = UPDATE_CLIPBOARD},
|
||||
{0}
|
||||
},
|
||||
.defaults = &(const struct clipboard_opts) {
|
||||
@@ -54,6 +56,7 @@ struct clipboard_ctx *mp_clipboard_create(struct clipboard_init_params *params,
|
||||
*cl = (struct clipboard_ctx) {
|
||||
.log = mp_log_new(cl, global->log, "clipboard"),
|
||||
.global = global,
|
||||
.monitor = params->flags & CLIPBOARD_INIT_ENABLE_MONITORING,
|
||||
};
|
||||
|
||||
for (int i = 0; i < MP_ARRAY_SIZE(clipboard_backend_list); i++) {
|
||||
@@ -79,6 +82,13 @@ void mp_clipboard_destroy(struct clipboard_ctx *cl)
|
||||
talloc_free(cl);
|
||||
}
|
||||
|
||||
bool mp_clipboard_data_changed(struct clipboard_ctx *cl)
|
||||
{
|
||||
if (cl && cl->backend->data_changed && cl->monitor)
|
||||
return cl->backend->data_changed(cl);
|
||||
return false;
|
||||
}
|
||||
|
||||
int mp_clipboard_get_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
|
||||
struct clipboard_data *out, void *talloc_ctx)
|
||||
{
|
||||
@@ -102,7 +112,10 @@ void reinit_clipboard(struct MPContext *mpctx)
|
||||
|
||||
struct clipboard_opts *opts = mp_get_config_group(NULL, mpctx->global, &clipboard_conf);
|
||||
if (opts->enabled) {
|
||||
struct clipboard_init_params params = {.mpctx = mpctx};
|
||||
struct clipboard_init_params params = {
|
||||
.mpctx = mpctx,
|
||||
.flags = opts->monitor ? CLIPBOARD_INIT_ENABLE_MONITORING : 0,
|
||||
};
|
||||
mpctx->clipboard = mp_clipboard_create(¶ms, mpctx->global);
|
||||
}
|
||||
talloc_free(opts);
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
struct clipboard_ctx;
|
||||
struct MPContext;
|
||||
|
||||
#define CLIPBOARD_INIT_ENABLE_MONITORING 1
|
||||
|
||||
enum clipboard_data_type {
|
||||
CLIPBOARD_DATA_TEXT,
|
||||
CLIPBOARD_DATA_IMAGE,
|
||||
@@ -65,6 +67,7 @@ struct clipboard_backend {
|
||||
// Return 0 on success, otherwise -1
|
||||
int (*init)(struct clipboard_ctx *cl, struct clipboard_init_params *params);
|
||||
void (*uninit)(struct clipboard_ctx *cl);
|
||||
bool (*data_changed)(struct clipboard_ctx *cl);
|
||||
int (*get_data)(struct clipboard_ctx *cl, struct clipboard_access_params *params,
|
||||
struct clipboard_data *out, void *talloc_ctx);
|
||||
int (*set_data)(struct clipboard_ctx *cl, struct clipboard_access_params *params,
|
||||
@@ -76,11 +79,13 @@ struct clipboard_ctx {
|
||||
struct mp_log *log;
|
||||
void *priv; // backend-specific internal data
|
||||
struct mpv_global *global;
|
||||
bool monitor;
|
||||
};
|
||||
|
||||
struct clipboard_ctx *mp_clipboard_create(struct clipboard_init_params *params,
|
||||
struct mpv_global *global);
|
||||
void mp_clipboard_destroy(struct clipboard_ctx *cl);
|
||||
bool mp_clipboard_data_changed(struct clipboard_ctx *cl);
|
||||
int mp_clipboard_get_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
|
||||
struct clipboard_data *out, void *talloc_ctx);
|
||||
int mp_clipboard_set_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
|
||||
|
||||
Reference in New Issue
Block a user