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:
nanahi
2024-11-22 00:57:45 -05:00
committed by Kacper Michajłow
parent dcde47e15b
commit 1d36f7be05
3 changed files with 28 additions and 1 deletions

View File

@@ -22,12 +22,14 @@
struct clipboard_opts { struct clipboard_opts {
bool enabled; bool enabled;
bool monitor;
}; };
#define OPT_BASE_STRUCT struct clipboard_opts #define OPT_BASE_STRUCT struct clipboard_opts
const struct m_sub_options clipboard_conf = { const struct m_sub_options clipboard_conf = {
.opts = (const struct m_option[]) { .opts = (const struct m_option[]) {
{"enable", OPT_BOOL(enabled), .flags = UPDATE_CLIPBOARD}, {"enable", OPT_BOOL(enabled), .flags = UPDATE_CLIPBOARD},
{"monitor", OPT_BOOL(monitor), .flags = UPDATE_CLIPBOARD},
{0} {0}
}, },
.defaults = &(const struct clipboard_opts) { .defaults = &(const struct clipboard_opts) {
@@ -54,6 +56,7 @@ struct clipboard_ctx *mp_clipboard_create(struct clipboard_init_params *params,
*cl = (struct clipboard_ctx) { *cl = (struct clipboard_ctx) {
.log = mp_log_new(cl, global->log, "clipboard"), .log = mp_log_new(cl, global->log, "clipboard"),
.global = global, .global = global,
.monitor = params->flags & CLIPBOARD_INIT_ENABLE_MONITORING,
}; };
for (int i = 0; i < MP_ARRAY_SIZE(clipboard_backend_list); i++) { 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); 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, int mp_clipboard_get_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
struct clipboard_data *out, void *talloc_ctx) 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); struct clipboard_opts *opts = mp_get_config_group(NULL, mpctx->global, &clipboard_conf);
if (opts->enabled) { 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(&params, mpctx->global); mpctx->clipboard = mp_clipboard_create(&params, mpctx->global);
} }
talloc_free(opts); talloc_free(opts);

View File

@@ -24,6 +24,8 @@
struct clipboard_ctx; struct clipboard_ctx;
struct MPContext; struct MPContext;
#define CLIPBOARD_INIT_ENABLE_MONITORING 1
enum clipboard_data_type { enum clipboard_data_type {
CLIPBOARD_DATA_TEXT, CLIPBOARD_DATA_TEXT,
CLIPBOARD_DATA_IMAGE, CLIPBOARD_DATA_IMAGE,
@@ -65,6 +67,7 @@ struct clipboard_backend {
// Return 0 on success, otherwise -1 // Return 0 on success, otherwise -1
int (*init)(struct clipboard_ctx *cl, struct clipboard_init_params *params); int (*init)(struct clipboard_ctx *cl, struct clipboard_init_params *params);
void (*uninit)(struct clipboard_ctx *cl); 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, int (*get_data)(struct clipboard_ctx *cl, struct clipboard_access_params *params,
struct clipboard_data *out, void *talloc_ctx); struct clipboard_data *out, void *talloc_ctx);
int (*set_data)(struct clipboard_ctx *cl, struct clipboard_access_params *params, int (*set_data)(struct clipboard_ctx *cl, struct clipboard_access_params *params,
@@ -76,11 +79,13 @@ struct clipboard_ctx {
struct mp_log *log; struct mp_log *log;
void *priv; // backend-specific internal data void *priv; // backend-specific internal data
struct mpv_global *global; struct mpv_global *global;
bool monitor;
}; };
struct clipboard_ctx *mp_clipboard_create(struct clipboard_init_params *params, struct clipboard_ctx *mp_clipboard_create(struct clipboard_init_params *params,
struct mpv_global *global); struct mpv_global *global);
void mp_clipboard_destroy(struct clipboard_ctx *cl); 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, int mp_clipboard_get_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,
struct clipboard_data *out, void *talloc_ctx); struct clipboard_data *out, void *talloc_ctx);
int mp_clipboard_set_data(struct clipboard_ctx *cl, struct clipboard_access_params *params, int mp_clipboard_set_data(struct clipboard_ctx *cl, struct clipboard_access_params *params,

View File

@@ -1211,6 +1211,12 @@ static void handle_eof(struct MPContext *mpctx)
} }
} }
static void handle_clipboard_updates(struct MPContext *mpctx)
{
if (mp_clipboard_data_changed(mpctx->clipboard))
mp_notify_property(mpctx, "clipboard");
}
void run_playloop(struct MPContext *mpctx) void run_playloop(struct MPContext *mpctx)
{ {
if (encode_lavc_didfail(mpctx->encode_lavc_ctx)) { if (encode_lavc_didfail(mpctx->encode_lavc_ctx)) {
@@ -1236,6 +1242,8 @@ void run_playloop(struct MPContext *mpctx)
handle_dummy_ticks(mpctx); handle_dummy_ticks(mpctx);
handle_clipboard_updates(mpctx);
update_osd_msg(mpctx); update_osd_msg(mpctx);
handle_update_subtitles(mpctx); handle_update_subtitles(mpctx);
@@ -1276,6 +1284,7 @@ void run_playloop(struct MPContext *mpctx)
void mp_idle(struct MPContext *mpctx) void mp_idle(struct MPContext *mpctx)
{ {
handle_dummy_ticks(mpctx); handle_dummy_ticks(mpctx);
handle_clipboard_updates(mpctx);
mp_wait_events(mpctx); mp_wait_events(mpctx);
mp_process_input(mpctx); mp_process_input(mpctx);
handle_command_updates(mpctx); handle_command_updates(mpctx);