forked from mpv-player/mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
1711 lines (1502 loc) · 52.5 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <pthread.h>
#include <assert.h>
#include "osdep/io.h"
#include "misc/rendezvous.h"
#include "input.h"
#include "keycodes.h"
#include "osdep/threads.h"
#include "osdep/timer.h"
#include "common/msg.h"
#include "common/global.h"
#include "options/m_config.h"
#include "options/m_option.h"
#include "options/path.h"
#include "mpv_talloc.h"
#include "options/options.h"
#include "misc/bstr.h"
#include "misc/node.h"
#include "stream/stream.h"
#include "common/common.h"
#if HAVE_COCOA
#include "osdep/macosx_events.h"
#endif
#define input_lock(ictx) pthread_mutex_lock(&ictx->mutex)
#define input_unlock(ictx) pthread_mutex_unlock(&ictx->mutex)
#define MP_MAX_KEY_DOWN 4
struct cmd_bind {
int keys[MP_MAX_KEY_DOWN];
int num_keys;
char *cmd;
char *location; // filename/line number of definition
char *desc; // human readable description
bool is_builtin;
struct cmd_bind_section *owner;
};
struct cmd_bind_section {
char *owner;
struct cmd_bind *binds;
int num_binds;
char *section;
struct mp_rect mouse_area; // set at runtime, if at all
bool mouse_area_set; // mouse_area is valid and should be tested
};
#define MP_MAX_SOURCES 10
#define MAX_ACTIVE_SECTIONS 50
struct active_section {
char *name;
int flags;
};
struct cmd_queue {
struct mp_cmd *first;
};
struct wheel_state {
double dead_zone_accum;
double unit_accum;
};
struct input_ctx {
pthread_mutex_t mutex;
struct mp_log *log;
struct mpv_global *global;
struct m_config_cache *opts_cache;
struct input_opts *opts;
bool using_ar;
bool using_cocoa_media_keys;
// Autorepeat stuff
short ar_state;
int64_t last_ar;
// history of key downs - the newest is in position 0
int key_history[MP_MAX_KEY_DOWN];
// key code of the last key that triggered MP_KEY_STATE_DOWN
int last_key_down;
int64_t last_key_down_time;
struct mp_cmd *current_down_cmd;
int last_doubleclick_key_down;
double last_doubleclick_time;
// Mouse position on the consumer side (as command.c sees it)
int mouse_x, mouse_y;
char *mouse_section; // last section to receive mouse event
// Mouse position on the producer side (as the VO sees it)
// Unlike mouse_x/y, this can be used to resolve mouse click bindings.
int mouse_vo_x, mouse_vo_y;
bool mouse_mangle, mouse_src_mangle;
struct mp_rect mouse_src, mouse_dst;
// Wheel state (MP_WHEEL_*)
struct wheel_state wheel_state_y; // MP_WHEEL_UP/MP_WHEEL_DOWN
struct wheel_state wheel_state_x; // MP_WHEEL_LEFT/MP_WHEEL_RIGHT
struct wheel_state *wheel_current; // The direction currently being scrolled
double last_wheel_time; // mp_time_sec() of the last wheel event
// List of command binding sections
struct cmd_bind_section **sections;
int num_sections;
// List currently active command sections
struct active_section active_sections[MAX_ACTIVE_SECTIONS];
int num_active_sections;
unsigned int mouse_event_counter;
struct mp_input_src *sources[MP_MAX_SOURCES];
int num_sources;
struct cmd_queue cmd_queue;
void (*wakeup_cb)(void *ctx);
void *wakeup_ctx;
};
static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
const char *location, const char *restrict_section);
static void close_input_sources(struct input_ctx *ictx);
#define OPT_BASE_STRUCT struct input_opts
struct input_opts {
char *config_file;
int doubleclick_time;
// Maximum number of queued commands from keypresses (limit to avoid
// repeated slow commands piling up)
int key_fifo_size;
// Autorepeat config (be aware of mp_input_set_repeat_info())
int ar_delay;
int ar_rate;
int use_alt_gr;
int use_appleremote;
int use_gamepad;
int use_media_keys;
int default_bindings;
int enable_mouse_movements;
int vo_key_input;
int test;
int allow_win_drag;
};
const struct m_sub_options input_config = {
.opts = (const m_option_t[]) {
OPT_STRING("input-conf", config_file, M_OPT_FILE),
OPT_INT("input-ar-delay", ar_delay, 0),
OPT_INT("input-ar-rate", ar_rate, 0),
OPT_PRINT("input-keylist", mp_print_key_list),
OPT_PRINT("input-cmdlist", mp_print_cmd_list),
OPT_FLAG("input-default-bindings", default_bindings, 0),
OPT_FLAG("input-test", test, 0),
OPT_INTRANGE("input-doubleclick-time", doubleclick_time, 0, 0, 1000),
OPT_FLAG("input-right-alt-gr", use_alt_gr, 0),
OPT_INTRANGE("input-key-fifo-size", key_fifo_size, 0, 2, 65000),
OPT_FLAG("input-cursor", enable_mouse_movements, 0),
OPT_FLAG("input-vo-keyboard", vo_key_input, 0),
OPT_FLAG("input-media-keys", use_media_keys, 0),
#if HAVE_COCOA
OPT_FLAG("input-appleremote", use_appleremote, 0),
#endif
#if HAVE_SDL2_GAMEPAD
OPT_FLAG("input-gamepad", use_gamepad, 0),
#endif
OPT_FLAG("window-dragging", allow_win_drag, 0),
OPT_REPLACED("input-x11-keyboard", "input-vo-keyboard"),
{0}
},
.size = sizeof(struct input_opts),
.defaults = &(const struct input_opts){
.key_fifo_size = 7,
.doubleclick_time = 300,
.ar_delay = 200,
.ar_rate = 40,
.use_alt_gr = 1,
.enable_mouse_movements = 1,
.use_media_keys = 1,
#if HAVE_COCOA
.use_appleremote = 1,
#endif
.default_bindings = 1,
.vo_key_input = 1,
.allow_win_drag = 1,
},
.change_flags = UPDATE_INPUT,
};
static const char builtin_input_conf[] =
#include "input/input_conf.h"
;
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;
}
static int queue_count_cmds(struct cmd_queue *queue)
{
int res = 0;
for (struct mp_cmd *cmd = queue->first; cmd; cmd = cmd->queue_next)
res++;
return res;
}
static void queue_remove(struct cmd_queue *queue, struct mp_cmd *cmd)
{
struct mp_cmd **p_prev = &queue->first;
while (*p_prev != cmd) {
p_prev = &(*p_prev)->queue_next;
}
// if this fails, cmd was not in the queue
assert(*p_prev == cmd);
*p_prev = cmd->queue_next;
}
static struct mp_cmd *queue_remove_head(struct cmd_queue *queue)
{
struct mp_cmd *ret = queue->first;
if (ret)
queue_remove(queue, ret);
return ret;
}
static void queue_add_tail(struct cmd_queue *queue, struct mp_cmd *cmd)
{
struct mp_cmd **p_prev = &queue->first;
while (*p_prev)
p_prev = &(*p_prev)->queue_next;
*p_prev = cmd;
cmd->queue_next = NULL;
}
static struct mp_cmd *queue_peek_tail(struct cmd_queue *queue)
{
struct mp_cmd *cur = queue->first;
while (cur && cur->queue_next)
cur = cur->queue_next;
return cur;
}
static void append_bind_info(struct input_ctx *ictx, char **pmsg,
struct cmd_bind *bind)
{
char *msg = *pmsg;
struct mp_cmd *cmd = mp_input_parse_cmd(ictx, bstr0(bind->cmd),
bind->location);
char *stripped = cmd ? cmd->original : bind->cmd;
msg = talloc_asprintf_append(msg, " '%s'", stripped);
if (!cmd)
msg = talloc_asprintf_append(msg, " (invalid)");
if (strcmp(bind->owner->section, "default") != 0)
msg = talloc_asprintf_append(msg, " in section {%s}",
bind->owner->section);
msg = talloc_asprintf_append(msg, " in %s", bind->location);
if (bind->is_builtin)
msg = talloc_asprintf_append(msg, " (default)");
talloc_free(cmd);
*pmsg = msg;
}
static mp_cmd_t *handle_test(struct input_ctx *ictx, int code)
{
if (code == MP_KEY_CLOSE_WIN) {
MP_WARN(ictx,
"CLOSE_WIN was received. This pseudo key can be remapped too,\n"
"but --input-test will always quit when receiving it.\n");
const char *args[] = {"quit", NULL};
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
return res;
}
char *key_buf = mp_input_get_key_combo_name(&code, 1);
char *msg = talloc_asprintf(NULL, "Key %s is bound to:\n", key_buf);
talloc_free(key_buf);
int count = 0;
for (int n = 0; n < ictx->num_sections; n++) {
struct cmd_bind_section *bs = ictx->sections[n];
for (int i = 0; i < bs->num_binds; i++) {
if (bs->binds[i].num_keys && bs->binds[i].keys[0] == code) {
count++;
if (count > 1)
msg = talloc_asprintf_append(msg, "\n");
msg = talloc_asprintf_append(msg, "%d. ", count);
append_bind_info(ictx, &msg, &bs->binds[i]);
}
}
}
if (!count)
msg = talloc_asprintf_append(msg, "(nothing)");
MP_INFO(ictx, "%s\n", msg);
const char *args[] = {"show-text", msg, NULL};
mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
talloc_free(msg);
return res;
}
static struct cmd_bind_section *find_section(struct input_ctx *ictx,
bstr section)
{
for (int n = 0; n < ictx->num_sections; n++) {
struct cmd_bind_section *bs = ictx->sections[n];
if (bstr_equals0(section, bs->section))
return bs;
}
return NULL;
}
static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
bstr section)
{
if (section.len == 0)
section = bstr0("default");
struct cmd_bind_section *bind_section = find_section(ictx, section);
if (bind_section)
return bind_section;
bind_section = talloc_ptrtype(ictx, bind_section);
*bind_section = (struct cmd_bind_section) {
.section = bstrdup0(bind_section, section),
.mouse_area = {INT_MIN, INT_MIN, INT_MAX, INT_MAX},
.mouse_area_set = true,
};
MP_TARRAY_APPEND(ictx, ictx->sections, ictx->num_sections, bind_section);
return bind_section;
}
static void key_buf_add(int *buf, int code)
{
for (int n = MP_MAX_KEY_DOWN - 1; n > 0; n--)
buf[n] = buf[n - 1];
buf[0] = code;
}
static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx,
char *section, int code)
{
struct cmd_bind_section *bs = get_bind_section(ictx, bstr0(section));
if (!bs->num_binds)
return NULL;
int keys[MP_MAX_KEY_DOWN];
memcpy(keys, ictx->key_history, sizeof(keys));
key_buf_add(keys, code);
struct cmd_bind *best = NULL;
// Prefer user-defined keys over builtin bindings
for (int builtin = 0; builtin < 2; builtin++) {
if (builtin && !ictx->opts->default_bindings)
break;
if (best)
break;
for (int n = 0; n < bs->num_binds; n++) {
if (bs->binds[n].is_builtin == (bool)builtin) {
struct cmd_bind *b = &bs->binds[n];
// we have: keys=[key2 key1 keyX ...]
// and: b->keys=[key1 key2] (and may be just a prefix)
for (int i = 0; i < b->num_keys; i++) {
if (b->keys[i] != keys[b->num_keys - 1 - i])
goto skip;
}
if (!best || b->num_keys >= best->num_keys)
best = b;
skip: ;
}
}
}
return best;
}
static struct cmd_bind *find_any_bind_for_key(struct input_ctx *ictx,
char *force_section, int code)
{
if (force_section)
return find_bind_for_key_section(ictx, force_section, code);
bool use_mouse = MP_KEY_DEPENDS_ON_MOUSE_POS(code);
// First look whether a mouse section is capturing all mouse input
// exclusively (regardless of the active section stack order).
if (use_mouse && MP_KEY_IS_MOUSE_BTN_SINGLE(ictx->last_key_down)) {
struct cmd_bind *bind =
find_bind_for_key_section(ictx, ictx->mouse_section, code);
if (bind)
return bind;
}
struct cmd_bind *best_bind = NULL;
for (int i = ictx->num_active_sections - 1; i >= 0; i--) {
struct active_section *s = &ictx->active_sections[i];
struct cmd_bind *bind = find_bind_for_key_section(ictx, s->name, code);
if (bind) {
struct cmd_bind_section *bs = bind->owner;
if (!use_mouse || (bs->mouse_area_set && test_rect(&bs->mouse_area,
ictx->mouse_vo_x,
ictx->mouse_vo_y)))
{
if (!best_bind || (best_bind->is_builtin && !bind->is_builtin))
best_bind = bind;
}
}
if (s->flags & MP_INPUT_EXCLUSIVE)
break;
if (best_bind && (s->flags & MP_INPUT_ON_TOP))
break;
}
return best_bind;
}
static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, char *force_section,
int code)
{
if (ictx->opts->test)
return handle_test(ictx, code);
struct cmd_bind *cmd = NULL;
if (MP_KEY_IS_UNICODE(code))
cmd = find_any_bind_for_key(ictx, force_section, MP_KEY_ANY_UNICODE);
if (!cmd)
cmd = find_any_bind_for_key(ictx, force_section, code);
if (!cmd)
cmd = find_any_bind_for_key(ictx, force_section, MP_KEY_UNMAPPED);
if (!cmd) {
if (code == MP_KEY_CLOSE_WIN)
return mp_input_parse_cmd_strv(ictx->log, (const char*[]){"quit", 0});
int msgl = MSGL_WARN;
if (MP_KEY_IS_MOUSE_MOVE(code))
msgl = MSGL_TRACE;
char *key_buf = mp_input_get_key_combo_name(&code, 1);
MP_MSG(ictx, msgl, "No key binding found for key '%s'.\n", key_buf);
talloc_free(key_buf);
return NULL;
}
mp_cmd_t *ret = mp_input_parse_cmd(ictx, bstr0(cmd->cmd), cmd->location);
if (ret) {
ret->input_section = cmd->owner->section;
ret->key_name = talloc_steal(ret, mp_input_get_key_combo_name(&code, 1));
MP_TRACE(ictx, "key '%s' -> '%s' in '%s'\n",
ret->key_name, cmd->cmd, ret->input_section);
if (MP_KEY_IS_UNICODE(code)) {
bstr text = {0};
mp_append_utf8_bstr(ret, &text, code);
ret->key_text = text.start;
}
ret->is_mouse_button = code & MP_KEY_EMIT_ON_UP;
} else {
char *key_buf = mp_input_get_key_combo_name(&code, 1);
MP_ERR(ictx, "Invalid command for key binding '%s': '%s'\n",
key_buf, cmd->cmd);
talloc_free(key_buf);
}
return ret;
}
static void update_mouse_section(struct input_ctx *ictx)
{
struct cmd_bind *bind =
find_any_bind_for_key(ictx, NULL, MP_KEY_MOUSE_MOVE);
char *new_section = bind ? bind->owner->section : "default";
char *old = ictx->mouse_section;
ictx->mouse_section = new_section;
if (strcmp(old, ictx->mouse_section) != 0) {
MP_TRACE(ictx, "input: switch section %s -> %s\n",
old, ictx->mouse_section);
mp_input_queue_cmd(ictx, get_cmd_from_keys(ictx, old, MP_KEY_MOUSE_LEAVE));
}
}
// Called when the currently held-down key is released. This (usually) sends
// the a key-up version of the command associated with the keys that were held
// down.
// If the drop_current parameter is set to true, then don't send the key-up
// command. Unless we've already sent a key-down event, in which case the
// input receiver (the player) must get a key-up event, or it would get stuck
// thinking a key is still held down.
static void release_down_cmd(struct input_ctx *ictx, bool drop_current)
{
if (ictx->current_down_cmd && ictx->current_down_cmd->emit_on_up &&
(!drop_current || ictx->current_down_cmd->def->on_updown))
{
memset(ictx->key_history, 0, sizeof(ictx->key_history));
ictx->current_down_cmd->is_up = true;
mp_input_queue_cmd(ictx, ictx->current_down_cmd);
} else {
talloc_free(ictx->current_down_cmd);
}
ictx->current_down_cmd = NULL;
ictx->last_key_down = 0;
ictx->last_key_down_time = 0;
ictx->ar_state = -1;
update_mouse_section(ictx);
}
// We don't want the append to the command queue indefinitely, because that
// could lead to situations where recovery would take too long.
static bool should_drop_cmd(struct input_ctx *ictx, struct mp_cmd *cmd)
{
struct cmd_queue *queue = &ictx->cmd_queue;
return queue_count_cmds(queue) >= ictx->opts->key_fifo_size;
}
static struct mp_cmd *resolve_key(struct input_ctx *ictx, int code)
{
update_mouse_section(ictx);
struct mp_cmd *cmd = get_cmd_from_keys(ictx, NULL, code);
key_buf_add(ictx->key_history, code);
if (cmd && !cmd->def->is_ignore && !should_drop_cmd(ictx, cmd))
return cmd;
talloc_free(cmd);
return NULL;
}
static void interpret_key(struct input_ctx *ictx, int code, double scale,
int scale_units)
{
int state = code & (MP_KEY_STATE_DOWN | MP_KEY_STATE_UP);
code = code & ~(unsigned)state;
if (mp_msg_test(ictx->log, MSGL_TRACE)) {
char *key = mp_input_get_key_name(code);
MP_TRACE(ictx, "key code=%#x '%s'%s%s\n",
code, key, (state & MP_KEY_STATE_DOWN) ? " down" : "",
(state & MP_KEY_STATE_UP) ? " up" : "");
talloc_free(key);
}
if (MP_KEY_DEPENDS_ON_MOUSE_POS(code & ~MP_KEY_MODIFIER_MASK)) {
ictx->mouse_event_counter++;
mp_input_wakeup(ictx);
}
struct mp_cmd *cmd = NULL;
if (state == MP_KEY_STATE_DOWN) {
// Protect against VOs which send STATE_DOWN with autorepeat
if (ictx->last_key_down == code)
return;
// Cancel current down-event (there can be only one)
release_down_cmd(ictx, true);
cmd = resolve_key(ictx, code);
if (cmd) {
cmd->is_up_down = true;
cmd->emit_on_up = (code & MP_KEY_EMIT_ON_UP) || cmd->def->on_updown;
ictx->current_down_cmd = mp_cmd_clone(cmd);
}
ictx->last_key_down = code;
ictx->last_key_down_time = mp_time_us();
ictx->ar_state = 0;
mp_input_wakeup(ictx); // possibly start timer for autorepeat
} else if (state == MP_KEY_STATE_UP) {
// Most VOs send RELEASE_ALL anyway
release_down_cmd(ictx, false);
} else {
// Press of key with no separate down/up events
// Mixing press events and up/down with the same key is not supported,
// and input sources shouldn't do this, but can happen anyway if
// multiple input sources interfere with each others.
if (ictx->last_key_down == code)
release_down_cmd(ictx, false);
cmd = resolve_key(ictx, code);
}
if (!cmd)
return;
// Don't emit a command on key-down if the key is designed to emit commands
// on key-up (like mouse buttons). Also, if the command specifically should
// be sent both on key down and key up, still emit the command.
if (cmd->emit_on_up && !cmd->def->on_updown) {
talloc_free(cmd);
return;
}
memset(ictx->key_history, 0, sizeof(ictx->key_history));
if (mp_input_is_scalable_cmd(cmd)) {
cmd->scale = scale;
cmd->scale_units = scale_units;
mp_input_queue_cmd(ictx, cmd);
} else {
// Non-scalable commands won't understand cmd->scale, so synthesize
// multiple commands with cmd->scale = 1
cmd->scale = 1;
cmd->scale_units = 1;
// Avoid spamming the player with too many commands
scale_units = MPMIN(scale_units, 20);
for (int i = 0; i < scale_units - 1; i++)
mp_input_queue_cmd(ictx, mp_cmd_clone(cmd));
if (scale_units)
mp_input_queue_cmd(ictx, cmd);
}
}
// Pre-processing for MP_WHEEL_* events. If this returns false, the caller
// should discard the event.
static bool process_wheel(struct input_ctx *ictx, int code, double *scale,
int *scale_units)
{
// Size of the deadzone in scroll units. The user must scroll at least this
// much in any direction before their scroll is registered.
static const double DEADZONE_DIST = 0.125;
// The deadzone accumulator is reset if no scrolls happened in this many
// seconds, eg. the user is assumed to have finished scrolling.
static const double DEADZONE_SCROLL_TIME = 0.2;
// The scale_units accumulator is reset if no scrolls happened in this many
// seconds. This value should be fairly large, so commands will still be
// sent when the user scrolls slowly.
static const double UNIT_SCROLL_TIME = 0.5;
// Determine which direction is being scrolled
double dir;
struct wheel_state *state;
switch (code) {
case MP_WHEEL_UP: dir = -1; state = &ictx->wheel_state_y; break;
case MP_WHEEL_DOWN: dir = +1; state = &ictx->wheel_state_y; break;
case MP_WHEEL_LEFT: dir = -1; state = &ictx->wheel_state_x; break;
case MP_WHEEL_RIGHT: dir = +1; state = &ictx->wheel_state_x; break;
default:
return true;
}
// Reset accumulators if it's determined that the user finished scrolling
double now = mp_time_sec();
if (now > ictx->last_wheel_time + DEADZONE_SCROLL_TIME) {
ictx->wheel_current = NULL;
ictx->wheel_state_y.dead_zone_accum = 0;
ictx->wheel_state_x.dead_zone_accum = 0;
}
if (now > ictx->last_wheel_time + UNIT_SCROLL_TIME) {
ictx->wheel_state_y.unit_accum = 0;
ictx->wheel_state_x.unit_accum = 0;
}
ictx->last_wheel_time = now;
// Process wheel deadzone. A lot of touchpad drivers don't filter scroll
// input, which makes it difficult for the user to send WHEEL_UP/DOWN
// without accidentally triggering WHEEL_LEFT/RIGHT. We try to fix this by
// implementing a deadzone. When the value of either direction breaks out
// of the deadzone, events from the other direction will be ignored until
// the user finishes scrolling.
if (ictx->wheel_current == NULL) {
state->dead_zone_accum += *scale * dir;
if (state->dead_zone_accum * dir > DEADZONE_DIST) {
ictx->wheel_current = state;
*scale = state->dead_zone_accum * dir;
}
}
if (ictx->wheel_current != state)
return false;
// Determine scale_units. This is incremented every time the accumulated
// scale value crosses 1.0. Non-scalable input commands will be ran that
// many times.
state->unit_accum += *scale * dir;
*scale_units = trunc(state->unit_accum * dir);
state->unit_accum -= *scale_units * dir;
return true;
}
static void mp_input_feed_key(struct input_ctx *ictx, int code, double scale,
bool force_mouse)
{
struct input_opts *opts = ictx->opts;
code = mp_normalize_keycode(code);
int unmod = code & ~MP_KEY_MODIFIER_MASK;
if (code == MP_INPUT_RELEASE_ALL) {
MP_TRACE(ictx, "release all\n");
release_down_cmd(ictx, false);
return;
}
if (!opts->enable_mouse_movements && MP_KEY_IS_MOUSE(unmod) && !force_mouse)
return;
if (unmod == MP_KEY_MOUSE_LEAVE || unmod == MP_KEY_MOUSE_ENTER) {
update_mouse_section(ictx);
mp_input_queue_cmd(ictx, get_cmd_from_keys(ictx, NULL, code));
return;
}
double now = mp_time_sec();
// ignore system-doubleclick if we generate these events ourselves
if (!force_mouse && opts->doubleclick_time && MP_KEY_IS_MOUSE_BTN_DBL(unmod))
return;
int units = 1;
if (MP_KEY_IS_WHEEL(unmod) && !process_wheel(ictx, unmod, &scale, &units))
return;
interpret_key(ictx, code, scale, units);
if (code & MP_KEY_STATE_DOWN) {
code &= ~MP_KEY_STATE_DOWN;
if (ictx->last_doubleclick_key_down == code &&
now - ictx->last_doubleclick_time < opts->doubleclick_time / 1000.0)
{
if (code >= MP_MBTN_LEFT && code <= MP_MBTN_RIGHT) {
interpret_key(ictx, code - MP_MBTN_BASE + MP_MBTN_DBL_BASE,
1, 1);
}
}
ictx->last_doubleclick_key_down = code;
ictx->last_doubleclick_time = now;
}
}
void mp_input_put_key(struct input_ctx *ictx, int code)
{
input_lock(ictx);
mp_input_feed_key(ictx, code, 1, false);
input_unlock(ictx);
}
void mp_input_put_key_artificial(struct input_ctx *ictx, int code)
{
input_lock(ictx);
mp_input_feed_key(ictx, code, 1, true);
input_unlock(ictx);
}
void mp_input_put_key_utf8(struct input_ctx *ictx, int mods, struct bstr t)
{
while (t.len) {
int code = bstr_decode_utf8(t, &t);
if (code < 0)
break;
mp_input_put_key(ictx, code | mods);
}
}
void mp_input_put_wheel(struct input_ctx *ictx, int direction, double value)
{
if (value == 0.0)
return;
input_lock(ictx);
mp_input_feed_key(ictx, direction, value, false);
input_unlock(ictx);
}
void mp_input_set_mouse_transform(struct input_ctx *ictx, struct mp_rect *dst,
struct mp_rect *src)
{
input_lock(ictx);
ictx->mouse_mangle = dst || src;
if (ictx->mouse_mangle) {
ictx->mouse_dst = *dst;
ictx->mouse_src_mangle = !!src;
if (ictx->mouse_src_mangle)
ictx->mouse_src = *src;
}
input_unlock(ictx);
}
bool mp_input_mouse_enabled(struct input_ctx *ictx)
{
input_lock(ictx);
bool r = ictx->opts->enable_mouse_movements;
input_unlock(ictx);
return r;
}
bool mp_input_vo_keyboard_enabled(struct input_ctx *ictx)
{
input_lock(ictx);
bool r = ictx->opts->vo_key_input;
input_unlock(ictx);
return r;
}
void mp_input_set_mouse_pos(struct input_ctx *ictx, int x, int y)
{
input_lock(ictx);
if (ictx->opts->enable_mouse_movements)
mp_input_set_mouse_pos_artificial(ictx, x, y);
input_unlock(ictx);
}
void mp_input_set_mouse_pos_artificial(struct input_ctx *ictx, int x, int y)
{
input_lock(ictx);
MP_TRACE(ictx, "mouse move %d/%d\n", x, y);
if (ictx->mouse_vo_x == x && ictx->mouse_vo_y == y) {
input_unlock(ictx);
return;
}
if (ictx->mouse_mangle) {
struct mp_rect *src = &ictx->mouse_src;
struct mp_rect *dst = &ictx->mouse_dst;
x = MPCLAMP(x, dst->x0, dst->x1) - dst->x0;
y = MPCLAMP(y, dst->y0, dst->y1) - dst->y0;
if (ictx->mouse_src_mangle) {
x = x * 1.0 / (dst->x1 - dst->x0) * (src->x1 - src->x0) + src->x0;
y = y * 1.0 / (dst->y1 - dst->y0) * (src->y1 - src->y0) + src->y0;
}
MP_TRACE(ictx, "-> %d/%d\n", x, y);
}
ictx->mouse_event_counter++;
ictx->mouse_vo_x = x;
ictx->mouse_vo_y = y;
update_mouse_section(ictx);
struct mp_cmd *cmd = get_cmd_from_keys(ictx, NULL, MP_KEY_MOUSE_MOVE);
if (!cmd)
cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
if (cmd) {
cmd->mouse_move = true;
cmd->mouse_x = x;
cmd->mouse_y = y;
if (should_drop_cmd(ictx, cmd)) {
talloc_free(cmd);
} else {
// Coalesce with previous mouse move events (i.e. replace it)
struct mp_cmd *tail = queue_peek_tail(&ictx->cmd_queue);
if (tail && tail->mouse_move) {
queue_remove(&ictx->cmd_queue, tail);
talloc_free(tail);
}
mp_input_queue_cmd(ictx, cmd);
}
}
input_unlock(ictx);
}
unsigned int mp_input_get_mouse_event_counter(struct input_ctx *ictx)
{
// Make the frontend always display the mouse cursor (as long as it's not
// forced invisible) if mouse input is desired.
input_lock(ictx);
if (mp_input_test_mouse_active(ictx, ictx->mouse_x, ictx->mouse_y))
ictx->mouse_event_counter++;
int ret = ictx->mouse_event_counter;
input_unlock(ictx);
return ret;
}
// adjust min time to wait until next repeat event
static void adjust_max_wait_time(struct input_ctx *ictx, double *time)
{
struct input_opts *opts = ictx->opts;
if (ictx->last_key_down && opts->ar_rate > 0 && ictx->ar_state >= 0) {
*time = MPMIN(*time, 1.0 / opts->ar_rate);
*time = MPMIN(*time, opts->ar_delay / 1000.0);
}
}
int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
{
input_lock(ictx);
if (cmd) {
queue_add_tail(&ictx->cmd_queue, cmd);
mp_input_wakeup(ictx);
}
input_unlock(ictx);
return 1;
}
static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
{
struct input_opts *opts = ictx->opts;
// No input : autorepeat ?
if (opts->ar_rate <= 0 || !ictx->current_down_cmd || !ictx->last_key_down ||
(ictx->last_key_down & MP_NO_REPEAT_KEY) ||
!mp_input_is_repeatable_cmd(ictx->current_down_cmd))
ictx->ar_state = -1; // disable
if (ictx->ar_state >= 0) {
int64_t t = mp_time_us();
if (ictx->last_ar + 2000000 < t)
ictx->last_ar = t;
// First time : wait delay
if (ictx->ar_state == 0
&& (t - ictx->last_key_down_time) >= opts->ar_delay * 1000)
{
ictx->ar_state = 1;
ictx->last_ar = ictx->last_key_down_time + opts->ar_delay * 1000;
// Then send rate / sec event
} else if (ictx->ar_state == 1
&& (t - ictx->last_ar) >= 1000000 / opts->ar_rate) {
ictx->last_ar += 1000000 / opts->ar_rate;
} else {
return NULL;
}
struct mp_cmd *ret = mp_cmd_clone(ictx->current_down_cmd);
ret->repeated = true;
return ret;
}
return NULL;
}
double mp_input_get_delay(struct input_ctx *ictx)
{
input_lock(ictx);
double seconds = INFINITY;
adjust_max_wait_time(ictx, &seconds);
input_unlock(ictx);
return seconds;
}
void mp_input_wakeup(struct input_ctx *ictx)
{
ictx->wakeup_cb(ictx->wakeup_ctx);
}
mp_cmd_t *mp_input_read_cmd(struct input_ctx *ictx)
{
input_lock(ictx);
struct mp_cmd *ret = queue_remove_head(&ictx->cmd_queue);
if (!ret)
ret = check_autorepeat(ictx);
if (ret && ret->mouse_move) {
ictx->mouse_x = ret->mouse_x;
ictx->mouse_y = ret->mouse_y;
}
input_unlock(ictx);
return ret;
}
void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y)
{
input_lock(ictx);
*x = ictx->mouse_x;
*y = ictx->mouse_y;
input_unlock(ictx);
}
// If name is NULL, return "default".
// Return a statically allocated name of the section (i.e. return value never
// gets deallocated).
static char *normalize_section(struct input_ctx *ictx, char *name)
{
return get_bind_section(ictx, bstr0(name))->section;
}
void mp_input_disable_section(struct input_ctx *ictx, char *name)
{
input_lock(ictx);
name = normalize_section(ictx, name);
// Remove old section, or make sure it's on top if re-enabled
for (int i = ictx->num_active_sections - 1; i >= 0; i--) {
struct active_section *as = &ictx->active_sections[i];
if (strcmp(as->name, name) == 0) {
MP_TARRAY_REMOVE_AT(ictx->active_sections,
ictx->num_active_sections, i);
}
}
input_unlock(ictx);
}
void mp_input_enable_section(struct input_ctx *ictx, char *name, int flags)
{