Skip to content

Commit

Permalink
various: define builtin options and key bindings for images
Browse files Browse the repository at this point in the history
This makes mpv a good image viewer out of the box, as it is currently
hard to setup.

It adds a builtin image conditional profile that users can extend in
mpv.conf. It is written to not restore and reapply the options on each
image change, because that is slow for certain options (e.g.
--d3d11-flip=no restarts the VO), and causes visible flicker when
options like gamma are unapplied before changing image and reapplied on
the next image. But it still restores the previous options after
switching to a video or audio file.

etc/image-input.conf defines default key bindings for image, and
~/.config/mpv/image-input.conf can override them. sxiv is the main
inspiration for the default key bindings.

Files called image-input.conf are added to an input section called
image, and builtin.conf enables it for images and disables it for
non-images. Because enable-section and disable-section are only called
in builtin.conf, they can be replaced with a different mechanism when
deprecated input sections are removed without a breaking change.
  • Loading branch information
guidocella committed Nov 24, 2024
1 parent 68d9ed2 commit c3fc379
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 7 deletions.
6 changes: 6 additions & 0 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ defined here::

https://github.com/mpv-player/mpv/blob/master/etc/input.conf

Additionally, ``image-input.conf`` can be placed in mpv's configuration
directory to enable key bindings exclusively for images. The default image
bindings are defined here::

https://github.com/mpv-player/mpv/blob/master/etc/image-input.conf

A list of special keys can be obtained with

``mpv --input-keylist``
Expand Down
120 changes: 120 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,79 @@ To use this feature, you need to fill the ``menu-data`` property with menu
definition data, and add a keybinding to run the ``context-menu`` command,
which can be done with a user script.

Image Bindings
--------------

Several key bindings are changed when viewing images:

LEFT, RIGHT, UP, DOWN, h, j, k, l
Pan the image when it is larger than the window.

Ctrl+LEFT, Ctrl+RIGHT, Ctrl+UP, Ctrl+DOWN, Ctrl+h, Ctrl+j, Ctrl+k, Ctrl+l
Align the image to one of the boundaries of the window when it is larger
than the window.

= and -
Change the zoom.

+ and _
Change the zoom slowly.

0
Reset the zoom.

u
Toggle between showing the image unscaled in its original dimensions and
fitting it to the window.

o
Fill the window with the image.

s
Toggle between showing images for 5 seconds in a slideshow and keeping them
open forever.

n
Go to the next playlist entry.

p
Go to the previous playlist entry.

N
Go to the next sub-playlist (e.g. directory or achive).

P
Go to the previous sub-playlist.

g-g
Go to the first playlist entry.

G
Go to the last playlist entry.

]
Skip 10 playlist entries forwards.

[
Skip 10 playlist entries backwards.

r and R
Rotate counterclockwise.

t
Rotate clockwise.

v
Rotate by 180 degrees.

Right click
Pan while holding the button keeping the clicked part of the image under the
cursor.

Wheel up/down
Change the zoom while keeping the part of the image hovered by the cursor
under it.

USAGE
=====

Expand Down Expand Up @@ -1008,6 +1081,50 @@ example, ``math`` is defined and gives access to the Lua standard math library.
This feature is subject to change indefinitely. You might be forced to
adjust your profiles on mpv updates.

Image profile
-------------------

mpv has a builtin conditional profile that is automatically applied to images
and restored when switching to a video or audio file. Its options are:

.. admonition:: Builtin profile definition

::

[image]
script-opt=osc-deadzone=1
video-aspect-override=no
prefetch-playlist
input-preprocess-wheel=no

It can be extended in mpv.conf without overwriting it completely:

.. admonition:: Example to enable the screensaver with images

::

[image]
stop-screensaver=no

You can stop applying it automatically by specifying an empty ``profile-cond``:

.. admonition:: Example to disable the image conditional profile

::

[image]
profile-cond=

This profile uses ``--input-commands`` to enable image specific key bindings.
This can be disabled with ``input-commands-clr``. Extra commands should be
specified with ``input-commands-append`` to keep enabling the image key
bindings.

To reset zoom and rotation between images, it is recommended to place
``reset-on-next-file=video-rotate,video-zoom,panscan,video-unscaled`` in the top
level of mpv.conf, as enabling it a conditional profile makes it take effect
only from the second file.

Legacy auto profiles
--------------------

Expand Down Expand Up @@ -1653,6 +1770,9 @@ All configuration files should be encoded in UTF-8.
``~/.config/mpv/input.conf``
key bindings (see `INPUT.CONF`_ section)

``~/.config/mpv/image-input.conf``
key bindings for images (see `INPUT.CONF`_ section)

``~/.config/mpv/fonts.conf``
Fontconfig fonts.conf that is customized for mpv. You should include system
fonts.conf in this file or mpv would not know about fonts that you already
Expand Down
13 changes: 13 additions & 0 deletions etc/builtin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ sub-shadow-offset=4
[box]
profile=osd-box
profile=sub-box

[image]
profile-cond=(get('current-tracks/video', {}).image and not get('current-tracks/video', {}).albumart) or (not get('current-tracks/video') and get('user-data/mpv/image'))
profile-restore=copy
input-commands=no-osd set user-data/mpv/image 1; enable-section image allow-hide-cursor+allow-vo-dragging
script-opt=osc-deadzone=1
video-aspect-override=no
prefetch-playlist
input-preprocess-wheel=no

[non-image]
profile-cond=get('user-data/mpv/image') and (not get('current-tracks/video', {}).image or get('current-tracks/video', {}).albumart)
input-commands=no-osd del user-data/mpv/image; disable-section image
86 changes: 86 additions & 0 deletions etc/image-input.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Image key bindings
#
# Location of user-defined bindings: ~/.config/mpv/image-input.conf
#
# Lines starting with # are comments. Use SHARP to assign the # key.
# Copy this file and uncomment and edit the bindings you want to change.
#
# List of commands and further details: DOCS/man/input.rst
# List of special keys: --input-keylist
# Keybindings testing mode: mpv --input-test --force-window --idle
#
# Use 'ignore' to unbind a key fully (e.g. 'ctrl+a ignore').
#
# Strings need to be quoted and escaped:
# KEY show-text "This is a single backslash: \\ and a quote: \" !"
#
# You can use modifier-key combinations like Shift+Left or Ctrl+Alt+x with
# the modifiers Shift, Ctrl, Alt and Meta (may not work on the terminal).
#
# The default keybindings are hardcoded into the mpv binary.
# You can disable them completely with: --no-input-default-bindings

#LEFT script-binding/positioning pan-x -0.1 # pan left
#DOWN script-binding/positioning pan-y 0.1 # pan down
#UP script-binding/positioning pan-y -0.1 # pan up
#RIGHT script-binding/positioning pan-x 0.1 # pan right
#h script-binding/positioning pan-x -0.1 # pan left
#j script-binding/positioning pan-y 0.1 # pan down
#k script-binding/positioning pan-y -0.1 # pan up
#l script-binding/positioning pan-x 0.1 # pan right
#Shift+LEFT script-binding/positioning pan-x -0.01 # pan left slowly
#Shift+DOWN script-binding/positioning pan-y 0.01 # pan down slowly
#Shift+UP script-binding/positioning pan-y -0.01 # pan up slowly
#Shift+RIGHT script-binding/positioning pan-x 0.01 # pan right slowly
#H script-binding/positioning pan-x -0.01 # pan left slowly
#J script-binding/positioning pan-y 0.01 # pan down slowly
#K script-binding/positioning pan-y -0.01 # pan up slowly
#L script-binding/positioning pan-x 0.01 # pan right slowly

# Recommened on a touchpad:
# WHEEL_UP script-binding/positioning pan-y -0.1 # pan up
# WHEEL_DOWN script-binding/positioning pan-y 0.1 # pan down
# WHEEL_LEFT script-binding/positioning pan-x -0.1 # pan left
# WHEEL_RIGHT script-binding/positioning pan-x 0.1 # pan right

#Ctrl+LEFT no-osd set video-align-x -1 # align to the left
#Ctrl+DOWN no-osd set video-align-y 1 # align to the bottom
#Ctrl+UP no-osd set video-align-y -1 # align to the top
#Ctrl+RIGHT no-osd set video-align-x 1 # align to the right
#Ctrl+h no-osd set video-align-x -1 # align to the left
#Ctrl+j no-osd set video-align-y 1 # align to the bottom
#Ctrl+k no-osd set video-align-y -1 # align to the top
#Ctrl+l no-osd set video-align-x 1 # align to the right

= add video-zoom 0.25 # zoom in
- add video-zoom -0.25 # zoom out
+ add video-zoom 0.05 # zoom in slowly
_ add video-zoom -0.05 # zoom out slowly
0 no-osd set video-zoom 0; no-osd set panscan 0 # reset zoom

#u no-osd cycle-values video-unscaled yes no; no-osd set video-zoom 0; no-osd set panscan 0 # toggle scaling the image to the window.

#o no-osd set panscan 1; no-osd set video-unscaled no; no-osd set video-zoom 0 # fill black bars

#MBTN_RIGHT script-binding positioning/drag-to-pan # pan around the clicked point

#WHEEL_UP script-message cursor-centric-zoom 0.1 # zoom in towards the cursor
#WHEEL_DOWN script-message cursor-centric-zoom -0.1 # zoom out towards the cursor

#s cycle-values image-display-duration 5 inf; no-osd set video-zoom 0; no-osd set panscan 0; no-osd set video-unscaled no # toggle slideshow

#n repeatable playlist-next # go to the next file
#p repeatable playlist-prev # go to the previous file
#N playlist-next-playlist # go to the next sub-playlist
#P playlist-prev-playlist # go to the previous sub-playlist

#g-g no-osd set playlist-pos 0 # go to the first playlist entry
#G no-osd set playlist-pos-1 ${playlist-count} # go to the last playlist entry

] no-osd add playlist-pos 10 # go 10 playlist entries forwards
[ no-osd add playlist-pos -10 # go 10 playlist entries backwards

#r cycle-values video-rotate 270 180 90 0 # rotate counterclockwise
#R cycle-values video-rotate 270 180 90 0 # rotate counterclockwise
#t cycle-values video-rotate 90 180 270 0 # rotate clockwise
#v cycle-values video-rotate 0 180 # rotate by 180 degrees
2 changes: 1 addition & 1 deletion etc/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ foreach size: icons
sources += icon
endforeach

etc_files = ['input.conf', 'builtin.conf']
etc_files = ['input.conf', 'builtin.conf', 'image-input.conf']
foreach file: etc_files
etc_file = custom_target(file,
input: file,
Expand Down
26 changes: 21 additions & 5 deletions input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ static const char builtin_input_conf[] =
#include "etc/input.conf.inc"
;

static const char builtin_image_input_conf[] =
#include "etc/image-input.conf.inc"
;

static bool test_rect(struct mp_rect *rc, int x, int y)
{
return x >= rc->x0 && y >= rc->y0 && x < rc->x1 && y < rc->y1;
Expand Down Expand Up @@ -1467,7 +1471,7 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
return n_binds;
}

static bool parse_config_file(struct input_ctx *ictx, char *file)
static bool parse_config_file(struct input_ctx *ictx, char *file, char *section)
{
bool r = false;
void *tmp = talloc_new(NULL);
Expand All @@ -1479,7 +1483,7 @@ static bool parse_config_file(struct input_ctx *ictx, char *file)
if (data.start) {
MP_VERBOSE(ictx, "Parsing input config file %s\n", file);
bstr_eatstart0(&data, "\xEF\xBB\xBF"); // skip BOM
int num = parse_config(ictx, false, data, file, (bstr){0});
int num = parse_config(ictx, false, data, file, bstr0(section));
MP_VERBOSE(ictx, "Input config file %s parsed: %d binds\n", file, num);
r = true;
} else {
Expand Down Expand Up @@ -1560,15 +1564,26 @@ void mp_input_load_config(struct input_ctx *ictx)
parse_config(ictx, true, line, "<builtin>", (bstr){0});
}

builtin = bstr0(builtin_image_input_conf);
while (ictx->opts->builtin_bindings && builtin.len) {
bstr line = bstr_getline(builtin, &builtin);
bstr_eatstart0(&line, "#");
if (!bstr_startswith0(line, " "))
parse_config(ictx, true, line, "<builtin-image>", bstr0("image"));
}

bool config_ok = false;
if (ictx->opts->config_file && ictx->opts->config_file[0])
config_ok = parse_config_file(ictx, ictx->opts->config_file);
config_ok = parse_config_file(ictx, ictx->opts->config_file, NULL);
if (!config_ok) {
// Try global conf dir
void *tmp = talloc_new(NULL);
char **files = mp_find_all_config_files(tmp, ictx->global, "input.conf");
for (int n = 0; files && files[n]; n++)
parse_config_file(ictx, files[n]);
parse_config_file(ictx, files[n], NULL);
files = mp_find_all_config_files(tmp, ictx->global, "image-input.conf");
for (int n = 0; files && files[n]; n++)
parse_config_file(ictx, files[n], "image");
talloc_free(tmp);
}

Expand All @@ -1586,7 +1601,8 @@ void mp_input_load_config(struct input_ctx *ictx)
bool mp_input_load_config_file(struct input_ctx *ictx, char *file)
{
input_lock(ictx);
bool result = parse_config_file(ictx, file);
char *section = strcmp(mp_basename(file), "image-input.conf") ? NULL : "image";
bool result = parse_config_file(ictx, file, section);
input_unlock(ictx);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ if get_option('cplayer')
datadir = get_option('datadir')
confdir = get_option('sysconfdir')

conf_files = ['etc/mpv.conf', 'etc/input.conf',
conf_files = ['etc/mpv.conf', 'etc/input.conf', 'etc/image-input.conf',
'etc/mplayer-input.conf', 'etc/restore-old-bindings.conf',
'etc/restore-osc-bindings.conf']
install_data(conf_files, install_dir: join_paths(datadir, 'doc', 'mpv'))
Expand Down

0 comments on commit c3fc379

Please sign in to comment.