Skip to content

Commit

Permalink
auto_profiles.lua: add a way to limit profile applications
Browse files Browse the repository at this point in the history
This allows users to specify the maximum amount of times they want a
profile to be applied. Fixes mpv-player#14426.
  • Loading branch information
Dudemanguy committed Jun 25, 2024
1 parent 0db6aba commit de84858
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,10 @@ truthy to falsy or error. If you want to use this, you need to set
``profile-restore`` for the profile. Another possibility it to create another
profile with an inverse condition to undo the other profile.

The maxmimum amount of times a profile is applied when the profile condition is
true can be limited by setting ``profile-applications``. If this is not set,
then profiles can be applied an infinite amount of times.

Recursive profiles can be used. But it is discouraged to reference other
conditional profiles in a conditional profile, since this can lead to tricky
and unintuitive behavior.
Expand All @@ -910,6 +914,14 @@ and unintuitive behavior.
profile-cond=width >= 1280
hue=-50

Make only HD video fullscreen but only apply the condition up to 3 times:

::
[something]
profile-cond=width >= 1280
profile-applications=3
fullscreen

Make only videos containing "youtube" or "youtu.be" in their path brighter:

::
Expand Down
17 changes: 17 additions & 0 deletions options/m_config_frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <assert.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -50,6 +51,7 @@ struct m_profile {
char *name;
char *desc;
char *cond;
int applications;
int restore_mode;
int num_opts;
// Option/value pair array.
Expand All @@ -68,6 +70,12 @@ struct m_opt_backup {
void *backup, *nval;
};

static const struct m_option profile_applications_opt = {
.name = "profile-applications",
.type = &m_option_type_int,
M_RANGE(0, INT_MAX),
};

static const struct m_option profile_restore_mode_opt = {
.name = "profile-restore",
.type = &m_option_type_choice,
Expand Down Expand Up @@ -962,6 +970,10 @@ int m_config_set_profile_option(struct m_config *config, struct m_profile *p,
p->cond = bstrto0(p, val);
return 0;
}
if (bstr_equals0(name, profile_applications_opt.name)) {
return m_option_parse(config->log, &profile_applications_opt, name, val,
&p->applications);
}
if (bstr_equals0(name, profile_restore_mode_opt.name)) {
return m_option_parse(config->log, &profile_restore_mode_opt, name, val,
&p->restore_mode);
Expand Down Expand Up @@ -1069,6 +1081,11 @@ struct mpv_node m_config_get_profiles(struct m_config *config)
node_map_add_string(entry, "profile-desc", profile->desc);
if (profile->cond)
node_map_add_string(entry, "profile-cond", profile->cond);
if (profile->applications) {
char *s = m_option_print(&profile_applications_opt, &profile->applications);
node_map_add_string(entry, profile_applications_opt.name, s);
talloc_free(s);
}
if (profile->restore_mode) {
char *s =
m_option_print(&profile_restore_mode_opt, &profile->restore_mode);
Expand Down
10 changes: 8 additions & 2 deletions player/lua/auto_profiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ local function evaluate(profile)
end
res = not not res
if res ~= profile.status then
if res == true then
if profile.applications > profile.max_applications then
msg.verbose("Maximum applications exceeded for profile: " .. profile.name)
return
elseif res == true then
msg.info("Applying auto profile: " .. profile.name)
mp.commandv("apply-profile", profile.name)
elseif profile.status == true and profile.has_restore_opt then
msg.info("Restoring profile: " .. profile.name)
mp.commandv("apply-profile", profile.name, "restore")
end
profile.applications = profile.applications + 1
end
profile.status = res
profile.dirty = false
Expand Down Expand Up @@ -176,7 +180,9 @@ local function load_profiles(profiles_property)
properties = {},
status = nil,
dirty = true, -- need re-evaluate
has_restore_opt = v["profile-restore"] and v["profile-restore"] ~= "default"
has_restore_opt = v["profile-restore"] and v["profile-restore"] ~= "default",
max_applications = v["profile-applications"] and tonumber(v["profile-applications"]) or math.huge,
applications = 0
}
profiles[#profiles + 1] = profile
have_dirty_profiles = true
Expand Down

0 comments on commit de84858

Please sign in to comment.