diff options
author | wm4 <wm4@nowhere> | 2019-12-23 11:40:27 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-12-23 11:44:24 +0100 |
commit | 07287262513c0d1ea46b7beaf100e73f2008295f (patch) | |
tree | 01548db64854e14caf2f0985fde8696488e57247 /sub/osd_libass.c | |
parent | 96932fe77c912f86d8fc51e073b4fd26a124a1fb (diff) | |
download | mpv-07287262513c0d1ea46b7beaf100e73f2008295f.tar.bz2 mpv-07287262513c0d1ea46b7beaf100e73f2008295f.tar.xz |
client API, lua: add new API for setting OSD overlays
Lua scripting has an undocumented mp.set_osd_ass() function, which is
used by osc.lua and console.lua. Apparently, 3rd party scripts also use
this. It's probably time to make this a public API.
The Lua implementation just bypassed the libmpv API. To make it usable
by any type of client, turn it into a command, "osd-overlay".
There's already a "overlay-add". Ignore it (although the manpage admits
guiltiness). I don't really want to deal with that old command. Its main
problem is that it uses global IDs, while I'd like to avoid that scripts
mess with each others overlays (whether that is accidentally or
intentionally). Maybe "overlay-add" can eventually be merged into
"osd-overlay", but I'm too lazy to do that now.
Scripting now uses the commands. There is a helper to manage OSD
overlays. The helper is very "thin"; I only want to force script authors
to use the ID allocation, which may help with putting multiple scripts
into a single .lua file without causing conflicts (basically, avoiding
singletons within a script's environment). The old set_osd_ass() is
emulated with the new API.
The JS scripting wrapper also provides a set_osd_ass() function, which
calls internal mpv API. Comment that part (to keep it compiling), but
I'm leaving it to @avih to finish the change.
Diffstat (limited to 'sub/osd_libass.c')
-rw-r--r-- | sub/osd_libass.c | 97 |
1 files changed, 66 insertions, 31 deletions
diff --git a/sub/osd_libass.c b/sub/osd_libass.c index d5f7bb82bf..01c0337eb4 100644 --- a/sub/osd_libass.c +++ b/sub/osd_libass.c @@ -80,8 +80,8 @@ static void destroy_ass_renderer(struct ass_state *ass) static void destroy_external(struct osd_external *ext) { - talloc_free(ext->text); destroy_ass_renderer(&ext->ass); + talloc_free(ext); } void osd_destroy_backend(struct osd_state *osd) @@ -90,7 +90,7 @@ void osd_destroy_backend(struct osd_state *osd) struct osd_object *obj = osd->objs[n]; destroy_ass_renderer(&obj->ass); for (int i = 0; i < obj->num_externals; i++) - destroy_external(&obj->externals[i]); + destroy_external(obj->externals[i]); obj->num_externals = 0; } } @@ -470,11 +470,9 @@ static void update_osd(struct osd_state *osd, struct osd_object *obj) static void update_external(struct osd_state *osd, struct osd_object *obj, struct osd_external *ext) { - bstr t = bstr0(ext->text); - if (!t.len) - return; - ext->ass.res_x = ext->res_x; - ext->ass.res_y = ext->res_y; + bstr t = bstr0(ext->ov.data); + ext->ass.res_x = ext->ov.res_x; + ext->ass.res_y = ext->ov.res_y; create_ass_track(osd, obj, &ext->ass); clear_ass(&ext->ass); @@ -497,29 +495,42 @@ static void update_external(struct osd_state *osd, struct osd_object *obj, } } -void osd_set_external(struct osd_state *osd, void *id, int res_x, int res_y, - char *text) +static int cmp_zorder(const void *pa, const void *pb) +{ + const struct osd_external *a = *(struct osd_external **)pa; + const struct osd_external *b = *(struct osd_external **)pb; + return a->ov.z == b->ov.z ? 0 : (a->ov.z > b->ov.z ? 1 : -1); +} + +void osd_set_external(struct osd_state *osd, struct osd_external_ass *ov) { pthread_mutex_lock(&osd->lock); struct osd_object *obj = osd->objs[OSDTYPE_EXTERNAL]; - struct osd_external *entry = 0; + bool zorder_changed = false; + int index = -1; + for (int n = 0; n < obj->num_externals; n++) { - if (obj->externals[n].id == id) { - entry = &obj->externals[n]; + struct osd_external *e = obj->externals[n]; + if (e->ov.id == ov->id && e->ov.owner == ov->owner) { + index = n; break; } } - if (!entry && !text) - goto done; - if (!entry) { - struct osd_external new = { .id = id }; + if (index < 0) { + if (!ov->format) + goto done; + struct osd_external *new = talloc_zero(NULL, struct osd_external); + new->ov.owner = ov->owner; + new->ov.id = ov->id; MP_TARRAY_APPEND(obj, obj->externals, obj->num_externals, new); - entry = &obj->externals[obj->num_externals - 1]; + index = obj->num_externals - 1; + zorder_changed = true; } - if (!text) { - int index = entry - &obj->externals[0]; + struct osd_external *entry = obj->externals[index]; + + if (!ov->format) { destroy_external(entry); MP_TARRAY_REMOVE_AT(obj->externals, obj->num_externals, index); obj->changed = true; @@ -527,22 +538,46 @@ void osd_set_external(struct osd_state *osd, void *id, int res_x, int res_y, goto done; } - if (!entry->text || strcmp(entry->text, text) != 0 || - entry->res_x != res_x || entry->res_y != res_y) - { - talloc_free(entry->text); - entry->text = talloc_strdup(NULL, text); - entry->res_x = res_x; - entry->res_y = res_y; - update_external(osd, obj, entry); - obj->changed = true; - osd->want_redraw_notification = true; + entry->ov.format = ov->format; + if (!entry->ov.data) + entry->ov.data = talloc_strdup(entry, ""); + entry->ov.data[0] = '\0'; // reuse memory allocation + entry->ov.data = talloc_strdup_append(entry->ov.data, ov->data); + entry->ov.res_x = ov->res_x; + entry->ov.res_y = ov->res_y; + zorder_changed |= entry->ov.z != ov->z; + entry->ov.z = ov->z; + + update_external(osd, obj, entry); + + obj->changed = true; + osd->want_redraw_notification = true; + + if (zorder_changed) { + qsort(obj->externals, obj->num_externals, sizeof(obj->externals[0]), + cmp_zorder); } done: pthread_mutex_unlock(&osd->lock); } +void osd_set_external_remove_owner(struct osd_state *osd, void *owner) +{ + pthread_mutex_lock(&osd->lock); + struct osd_object *obj = osd->objs[OSDTYPE_EXTERNAL]; + for (int n = obj->num_externals - 1; n >= 0; n--) { + struct osd_external *e = obj->externals[n]; + if (e->ov.owner == owner) { + destroy_external(e); + MP_TARRAY_REMOVE_AT(obj->externals, obj->num_externals, n); + obj->changed = true; + osd->want_redraw_notification = true; + } + } + pthread_mutex_unlock(&osd->lock); +} + static void append_ass(struct ass_state *ass, struct mp_osd_res *res, ASS_Image **img_list, bool *changed) { @@ -574,8 +609,8 @@ void osd_object_get_bitmaps(struct osd_state *osd, struct osd_object *obj, append_ass(&obj->ass, &obj->vo_res, &obj->ass_imgs[0], &obj->changed); for (int n = 0; n < obj->num_externals; n++) { - append_ass(&obj->externals[n].ass, &obj->vo_res, &obj->ass_imgs[n + 1], - &obj->changed); + append_ass(&obj->externals[n]->ass, &obj->vo_res, + &obj->ass_imgs[n + 1], &obj->changed); } mp_ass_packer_pack(obj->ass_packer, obj->ass_imgs, obj->num_externals + 1, |