Skip to content

Commit 957e9a3

Browse files
committed
input: use mnemonic names for mouse buttons
mpv's mouse button numbering is based on X11 button numbering, which allows for an arbitrary number of buttons and includes mouse wheel input as buttons 3-6. This button numbering was used throughout the codebase and exposed in input.conf, and it was difficult to remember which physical button each number actually referred to and which referred to the scroll wheel. In practice, PC mice only have between two and five buttons and one or two scroll wheel axes, which are more or less in the same location and have more or less the same function. This allows us to use names to refer to the buttons instead of numbers, which makes input.conf syntax a lot easier to remember. It also makes the syntax robust to changes in mpv's underlying numbering. The old MOUSE_BTNx names are still understood as deprecated aliases of the named buttons. This changes both the input.conf syntax and the MP_MOUSE_BTNx symbols in the codebase, since I think both would benefit from using names over numbers, especially since some platforms don't use X11 button numbering and handle different mouse buttons in different windowing system events. This also makes the names shorter, since otherwise they would be pretty long, and it removes the high-numbered MOUSE_BTNx_DBL names, since they weren't used. Names are the same as used in Qt: https://doc.qt.io/qt-5/qt.html#MouseButton-enum
1 parent 449d972 commit 957e9a3

File tree

13 files changed

+172
-171
lines changed

13 files changed

+172
-171
lines changed

DOCS/interface-changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Interface changes
5959
obscure use that stopped working with the change above. It was also
6060
prone to be confused with a feature not implemented by it: auto did _not_
6161
mean that deinterlacing was enabled on demand.)
62+
- add shortened mnemonic names for mouse button bindings, eg. mbtn_left
63+
the old numeric names (mouse_btn0) are deprecated
64+
- remove mouse_btn3_dbl and up, since they are only generated for buttons
65+
0-2 (these now print an error when sent from the 'mouse' command)
6266
--- mpv 0.26.0 ---
6367
- remove remaining deprecated audio device options, like --alsa-device
6468
Some of them were removed in earlier releases.

etc/input.conf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
# If this is enabled, treat all the following bindings as default.
2929
#default-bindings start
3030

31-
#MOUSE_BTN0 ignore # don't do anything
32-
#MOUSE_BTN0_DBL cycle fullscreen # toggle fullscreen on/off
33-
#MOUSE_BTN2 cycle pause # toggle pause on/off
34-
#MOUSE_BTN3 seek 10
35-
#MOUSE_BTN4 seek -10
36-
#MOUSE_BTN5 add volume -2
37-
#MOUSE_BTN6 add volume 2
31+
#MBTN_LEFT ignore # don't do anything
32+
#MBTN_LEFT_DBL cycle fullscreen # toggle fullscreen on/off
33+
#MBTN_RIGHT cycle pause # toggle pause on/off
34+
#WHEEL_UP seek 10
35+
#WHEEL_DOWN seek -10
36+
#WHEEL_LEFT add volume -2
37+
#WHEEL_RIGHT add volume 2
3838

3939
# Mouse wheels, touchpad or other input devices that have axes
4040
# if the input devices supports precise scrolling it will also scale the

input/input.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ static void mp_input_feed_key(struct input_ctx *ictx, int code, double scale,
732732
if (ictx->last_doubleclick_key_down == code &&
733733
now - ictx->last_doubleclick_time < opts->doubleclick_time / 1000.0)
734734
{
735-
if (code >= MP_MOUSE_BTN0 && code <= MP_MOUSE_BTN2) {
736-
interpret_key(ictx, code - MP_MOUSE_BTN0 + MP_MOUSE_BTN0_DBL,
735+
if (code >= MP_MBTN_LEFT && code <= MP_MBTN_RIGHT) {
736+
interpret_key(ictx, code - MP_MOUSE_BASE + MP_MOUSE_DBL_BASE,
737737
1, 1);
738738
}
739739
}

input/keycodes.c

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,46 +78,29 @@ static const struct key_name key_names[] = {
7878
{ MP_KEY_KPDEC, "KP_DEC" },
7979
{ MP_KEY_KPINS, "KP_INS" },
8080
{ MP_KEY_KPENTER, "KP_ENTER" },
81-
{ MP_MOUSE_BTN0, "MOUSE_BTN0" },
82-
{ MP_MOUSE_BTN1, "MOUSE_BTN1" },
83-
{ MP_MOUSE_BTN2, "MOUSE_BTN2" },
84-
{ MP_MOUSE_BTN3, "MOUSE_BTN3" },
85-
{ MP_MOUSE_BTN4, "MOUSE_BTN4" },
86-
{ MP_MOUSE_BTN5, "MOUSE_BTN5" },
87-
{ MP_MOUSE_BTN6, "MOUSE_BTN6" },
88-
{ MP_MOUSE_BTN7, "MOUSE_BTN7" },
89-
{ MP_MOUSE_BTN8, "MOUSE_BTN8" },
90-
{ MP_MOUSE_BTN9, "MOUSE_BTN9" },
91-
{ MP_MOUSE_BTN10, "MOUSE_BTN10" },
92-
{ MP_MOUSE_BTN11, "MOUSE_BTN11" },
93-
{ MP_MOUSE_BTN12, "MOUSE_BTN12" },
94-
{ MP_MOUSE_BTN13, "MOUSE_BTN13" },
95-
{ MP_MOUSE_BTN14, "MOUSE_BTN14" },
96-
{ MP_MOUSE_BTN15, "MOUSE_BTN15" },
97-
{ MP_MOUSE_BTN16, "MOUSE_BTN16" },
98-
{ MP_MOUSE_BTN17, "MOUSE_BTN17" },
99-
{ MP_MOUSE_BTN18, "MOUSE_BTN18" },
100-
{ MP_MOUSE_BTN19, "MOUSE_BTN19" },
101-
{ MP_MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
102-
{ MP_MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
103-
{ MP_MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
104-
{ MP_MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
105-
{ MP_MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
106-
{ MP_MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
107-
{ MP_MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
108-
{ MP_MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
109-
{ MP_MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
110-
{ MP_MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
111-
{ MP_MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
112-
{ MP_MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
113-
{ MP_MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
114-
{ MP_MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
115-
{ MP_MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
116-
{ MP_MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
117-
{ MP_MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
118-
{ MP_MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
119-
{ MP_MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
120-
{ MP_MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
81+
{ MP_MBTN_LEFT, "MBTN_LEFT" },
82+
{ MP_MBTN_MID, "MBTN_MID" },
83+
{ MP_MBTN_RIGHT, "MBTN_RIGHT" },
84+
{ MP_WHEEL_UP, "WHEEL_UP" },
85+
{ MP_WHEEL_DOWN, "WHEEL_DOWN" },
86+
{ MP_WHEEL_LEFT, "WHEEL_LEFT" },
87+
{ MP_WHEEL_RIGHT, "WHEEL_RIGHT" },
88+
{ MP_MBTN_BACK, "MBTN_BACK" },
89+
{ MP_MBTN_FORWARD, "MBTN_FORWARD" },
90+
{ MP_MBTN9, "MBTN9" },
91+
{ MP_MBTN10, "MBTN10" },
92+
{ MP_MBTN11, "MBTN11" },
93+
{ MP_MBTN12, "MBTN12" },
94+
{ MP_MBTN13, "MBTN13" },
95+
{ MP_MBTN14, "MBTN14" },
96+
{ MP_MBTN15, "MBTN15" },
97+
{ MP_MBTN16, "MBTN16" },
98+
{ MP_MBTN17, "MBTN17" },
99+
{ MP_MBTN18, "MBTN18" },
100+
{ MP_MBTN19, "MBTN19" },
101+
{ MP_MBTN_LEFT_DBL, "MBTN_LEFT_DBL" },
102+
{ MP_MBTN_MID_DBL, "MBTN_MID_DBL" },
103+
{ MP_MBTN_RIGHT_DBL, "MBTN_RIGHT_DBL" },
121104

122105
{ MP_AR_PLAY, "AR_PLAY" },
123106
{ MP_AR_PLAY_HOLD, "AR_PLAY_HOLD" },
@@ -169,6 +152,31 @@ static const struct key_name key_names[] = {
169152
{ MP_KEY_PREV, "XF86_PREV" },
170153
{ MP_KEY_NEXT, "XF86_NEXT" },
171154

155+
// Deprecated numeric aliases for the mouse buttons
156+
{ MP_MBTN_LEFT, "MOUSE_BTN0" },
157+
{ MP_MBTN_MID, "MOUSE_BTN1" },
158+
{ MP_MBTN_RIGHT, "MOUSE_BTN2" },
159+
{ MP_WHEEL_UP, "MOUSE_BTN3" },
160+
{ MP_WHEEL_DOWN, "MOUSE_BTN4" },
161+
{ MP_WHEEL_LEFT, "MOUSE_BTN5" },
162+
{ MP_WHEEL_RIGHT, "MOUSE_BTN6" },
163+
{ MP_MBTN_BACK, "MOUSE_BTN7" },
164+
{ MP_MBTN_FORWARD, "MOUSE_BTN8" },
165+
{ MP_MBTN9, "MOUSE_BTN9" },
166+
{ MP_MBTN10, "MOUSE_BTN10" },
167+
{ MP_MBTN11, "MOUSE_BTN11" },
168+
{ MP_MBTN12, "MOUSE_BTN12" },
169+
{ MP_MBTN13, "MOUSE_BTN13" },
170+
{ MP_MBTN14, "MOUSE_BTN14" },
171+
{ MP_MBTN15, "MOUSE_BTN15" },
172+
{ MP_MBTN16, "MOUSE_BTN16" },
173+
{ MP_MBTN17, "MOUSE_BTN17" },
174+
{ MP_MBTN18, "MOUSE_BTN18" },
175+
{ MP_MBTN19, "MOUSE_BTN19" },
176+
{ MP_MBTN_LEFT_DBL, "MOUSE_BTN0_DBL" },
177+
{ MP_MBTN_MID_DBL, "MOUSE_BTN1_DBL" },
178+
{ MP_MBTN_RIGHT_DBL, "MOUSE_BTN2_DBL" },
179+
172180
{ MP_KEY_CLOSE_WIN, "CLOSE_WIN" },
173181
{ MP_KEY_MOUSE_MOVE, "MOUSE_MOVE" },
174182
{ MP_KEY_MOUSE_LEAVE, "MOUSE_LEAVE" },

input/keycodes.h

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -98,57 +98,40 @@
9898
#define MP_KEY_KPENTER (MP_KEY_KEYPAD+13)
9999

100100
// Mouse events from VOs
101-
#define MP_MOUSE_BASE ((MP_KEY_BASE+0xA0)|MP_NO_REPEAT_KEY|MP_KEY_EMIT_ON_UP)
102-
#define MP_MOUSE_BTN0 (MP_MOUSE_BASE+0)
103-
#define MP_MOUSE_BTN1 (MP_MOUSE_BASE+1)
104-
#define MP_MOUSE_BTN2 (MP_MOUSE_BASE+2)
105-
#define MP_MOUSE_BTN3 (MP_MOUSE_BASE+3)
106-
#define MP_MOUSE_BTN4 (MP_MOUSE_BASE+4)
107-
#define MP_MOUSE_BTN5 (MP_MOUSE_BASE+5)
108-
#define MP_MOUSE_BTN6 (MP_MOUSE_BASE+6)
109-
#define MP_MOUSE_BTN7 (MP_MOUSE_BASE+7)
110-
#define MP_MOUSE_BTN8 (MP_MOUSE_BASE+8)
111-
#define MP_MOUSE_BTN9 (MP_MOUSE_BASE+9)
112-
#define MP_MOUSE_BTN10 (MP_MOUSE_BASE+10)
113-
#define MP_MOUSE_BTN11 (MP_MOUSE_BASE+11)
114-
#define MP_MOUSE_BTN12 (MP_MOUSE_BASE+12)
115-
#define MP_MOUSE_BTN13 (MP_MOUSE_BASE+13)
116-
#define MP_MOUSE_BTN14 (MP_MOUSE_BASE+14)
117-
#define MP_MOUSE_BTN15 (MP_MOUSE_BASE+15)
118-
#define MP_MOUSE_BTN16 (MP_MOUSE_BASE+16)
119-
#define MP_MOUSE_BTN17 (MP_MOUSE_BASE+17)
120-
#define MP_MOUSE_BTN18 (MP_MOUSE_BASE+18)
121-
#define MP_MOUSE_BTN19 (MP_MOUSE_BASE+19)
122-
#define MP_MOUSE_BTN_END (MP_MOUSE_BASE+20)
101+
#define MP_MOUSE_BASE ((MP_KEY_BASE+0xA0)|MP_NO_REPEAT_KEY|MP_KEY_EMIT_ON_UP)
102+
#define MP_MBTN_LEFT (MP_MOUSE_BASE+0)
103+
#define MP_MBTN_MID (MP_MOUSE_BASE+1)
104+
#define MP_MBTN_RIGHT (MP_MOUSE_BASE+2)
105+
#define MP_WHEEL_UP (MP_MOUSE_BASE+3)
106+
#define MP_WHEEL_DOWN (MP_MOUSE_BASE+4)
107+
#define MP_WHEEL_LEFT (MP_MOUSE_BASE+5)
108+
#define MP_WHEEL_RIGHT (MP_MOUSE_BASE+6)
109+
#define MP_MBTN_BACK (MP_MOUSE_BASE+7)
110+
#define MP_MBTN_FORWARD (MP_MOUSE_BASE+8)
111+
#define MP_MBTN9 (MP_MOUSE_BASE+9)
112+
#define MP_MBTN10 (MP_MOUSE_BASE+10)
113+
#define MP_MBTN11 (MP_MOUSE_BASE+11)
114+
#define MP_MBTN12 (MP_MOUSE_BASE+12)
115+
#define MP_MBTN13 (MP_MOUSE_BASE+13)
116+
#define MP_MBTN14 (MP_MOUSE_BASE+14)
117+
#define MP_MBTN15 (MP_MOUSE_BASE+15)
118+
#define MP_MBTN16 (MP_MOUSE_BASE+16)
119+
#define MP_MBTN17 (MP_MOUSE_BASE+17)
120+
#define MP_MBTN18 (MP_MOUSE_BASE+18)
121+
#define MP_MBTN19 (MP_MOUSE_BASE+19)
122+
#define MP_MOUSE_END (MP_MOUSE_BASE+20)
123123

124124
#define MP_KEY_IS_MOUSE_BTN_SINGLE(code) \
125-
((code) >= MP_MOUSE_BASE && (code) < MP_MOUSE_BTN_END)
126-
127-
#define MP_MOUSE_BASE_DBL ((MP_KEY_BASE+0xC0)|MP_NO_REPEAT_KEY)
128-
#define MP_MOUSE_BTN0_DBL (MP_MOUSE_BASE_DBL+0)
129-
#define MP_MOUSE_BTN1_DBL (MP_MOUSE_BASE_DBL+1)
130-
#define MP_MOUSE_BTN2_DBL (MP_MOUSE_BASE_DBL+2)
131-
#define MP_MOUSE_BTN3_DBL (MP_MOUSE_BASE_DBL+3)
132-
#define MP_MOUSE_BTN4_DBL (MP_MOUSE_BASE_DBL+4)
133-
#define MP_MOUSE_BTN5_DBL (MP_MOUSE_BASE_DBL+5)
134-
#define MP_MOUSE_BTN6_DBL (MP_MOUSE_BASE_DBL+6)
135-
#define MP_MOUSE_BTN7_DBL (MP_MOUSE_BASE_DBL+7)
136-
#define MP_MOUSE_BTN8_DBL (MP_MOUSE_BASE_DBL+8)
137-
#define MP_MOUSE_BTN9_DBL (MP_MOUSE_BASE_DBL+9)
138-
#define MP_MOUSE_BTN10_DBL (MP_MOUSE_BASE_DBL+10)
139-
#define MP_MOUSE_BTN11_DBL (MP_MOUSE_BASE_DBL+11)
140-
#define MP_MOUSE_BTN12_DBL (MP_MOUSE_BASE_DBL+12)
141-
#define MP_MOUSE_BTN13_DBL (MP_MOUSE_BASE_DBL+13)
142-
#define MP_MOUSE_BTN14_DBL (MP_MOUSE_BASE_DBL+14)
143-
#define MP_MOUSE_BTN15_DBL (MP_MOUSE_BASE_DBL+15)
144-
#define MP_MOUSE_BTN16_DBL (MP_MOUSE_BASE_DBL+16)
145-
#define MP_MOUSE_BTN17_DBL (MP_MOUSE_BASE_DBL+17)
146-
#define MP_MOUSE_BTN18_DBL (MP_MOUSE_BASE_DBL+18)
147-
#define MP_MOUSE_BTN19_DBL (MP_MOUSE_BASE_DBL+19)
148-
#define MP_MOUSE_BTN_DBL_END (MP_MOUSE_BASE_DBL+20)
125+
((code) >= MP_MOUSE_BASE && (code) < MP_MOUSE_END)
126+
127+
#define MP_MOUSE_DBL_BASE ((MP_KEY_BASE+0xC0)|MP_NO_REPEAT_KEY)
128+
#define MP_MBTN_LEFT_DBL (MP_MOUSE_DBL_BASE+0)
129+
#define MP_MBTN_MID_DBL (MP_MOUSE_DBL_BASE+1)
130+
#define MP_MBTN_RIGHT_DBL (MP_MOUSE_DBL_BASE+2)
131+
#define MP_MOUSE_DBL_END (MP_MOUSE_DBL_BASE+20)
149132

150133
#define MP_KEY_IS_MOUSE_BTN_DBL(code) \
151-
((code) >= MP_MOUSE_BTN0_DBL && (code) < MP_MOUSE_BTN_DBL_END)
134+
((code) >= MP_MOUSE_DBL_BASE && (code) < MP_MOUSE_DBL_END)
152135

153136
// Apple Remote input module
154137
#define MP_AR_BASE (MP_KEY_BASE+0xE0)

player/command.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5601,7 +5601,12 @@ int run_command(struct MPContext *mpctx, struct mp_cmd *cmd, struct mpv_node *re
56015601
return -1;
56025602
}
56035603
const bool dbc = cmd->args[3].v.i;
5604-
button += dbc ? MP_MOUSE_BASE_DBL : MP_MOUSE_BASE;
5604+
if (dbc && button > (MP_MBTN_RIGHT - MP_MOUSE_BASE)) {
5605+
MP_ERR(mpctx, "%d is not a valid mouse button for double-clicks.\n",
5606+
button);
5607+
return -1;
5608+
}
5609+
button += dbc ? MP_MOUSE_DBL_BASE : MP_MOUSE_BASE;
56055610
mp_input_set_mouse_pos_artificial(mpctx->input, x, y);
56065611
mp_input_put_key_artificial(mpctx->input, button);
56075612
break;

0 commit comments

Comments
 (0)