Coverage Report

Created: 2025-12-11 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/input/input.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include "config.h"
19
20
#include <stdlib.h>
21
#include <string.h>
22
#include <stdio.h>
23
#include <stdbool.h>
24
#include <math.h>
25
#include <errno.h>
26
#include <sys/types.h>
27
#include <sys/stat.h>
28
#include <fcntl.h>
29
#include <assert.h>
30
31
#include <mpv/client.h>
32
33
#include "osdep/io.h"
34
#include "misc/rendezvous.h"
35
36
#include "input.h"
37
#include "keycodes.h"
38
#include "osdep/threads.h"
39
#include "osdep/timer.h"
40
#include "common/msg.h"
41
#include "common/global.h"
42
#include "options/m_config.h"
43
#include "options/m_option.h"
44
#include "options/path.h"
45
#include "mpv_talloc.h"
46
#include "options/options.h"
47
#include "misc/bstr.h"
48
#include "misc/node.h"
49
#include "stream/stream.h"
50
#include "common/common.h"
51
52
#if HAVE_COCOA
53
#include "osdep/mac/app_bridge.h"
54
#endif
55
56
5.63M
#define input_lock(ictx)    mp_mutex_lock(&ictx->mutex)
57
5.63M
#define input_unlock(ictx)  mp_mutex_unlock(&ictx->mutex)
58
59
27.6M
#define MP_MAX_KEY_DOWN 16
60
61
struct cmd_bind {
62
    int keys[MP_MAX_KEY_DOWN];
63
    int num_keys;
64
    char *cmd;
65
    char *location;     // filename/line number of definition
66
    char *desc;         // human readable description
67
    bool is_builtin;
68
    struct cmd_bind_section *owner;
69
};
70
71
struct cmd_bind_section {
72
    char *owner;
73
    struct cmd_bind *binds;
74
    int num_binds;
75
    bstr section;
76
    struct mp_rect mouse_area;  // set at runtime, if at all
77
    bool mouse_area_set;        // mouse_area is valid and should be tested
78
};
79
80
0
#define MP_MAX_SOURCES 10
81
0
#define MP_MAX_TABLET_PAD_BUTTONS 10
82
83
struct active_section {
84
    bstr name;
85
    int flags;
86
};
87
88
struct cmd_queue {
89
    struct mp_cmd *first;
90
};
91
92
struct wheel_state {
93
    double dead_zone_accum;
94
    double unit_accum;
95
};
96
97
struct touch_point {
98
    int id;
99
    int x, y;
100
};
101
102
struct input_ctx {
103
    mp_mutex mutex;
104
    struct mp_log *log;
105
    struct mpv_global *global;
106
    struct m_config_cache *opts_cache;
107
    struct input_opts *opts;
108
109
    bool using_ar;
110
    bool using_cocoa_media_keys;
111
112
    // Autorepeat stuff
113
    short ar_state;
114
    int64_t last_ar;
115
116
    // history of key downs - the newest is in position 0
117
    int key_history[MP_MAX_KEY_DOWN];
118
    // key code of the last key that triggered MP_KEY_STATE_DOWN
119
    int last_key_down;
120
    int64_t last_key_down_time;
121
    struct mp_cmd *current_down_cmd;
122
123
    int last_doubleclick_key_down;
124
    double last_doubleclick_time;
125
126
    // VO dragging state
127
    bool dragging_button_down;
128
    int mouse_drag_x, mouse_drag_y;
129
    // Raw mouse position before transform
130
    int mouse_raw_x, mouse_raw_y;
131
132
    // Mouse position on the consumer side (as command.c sees it)
133
    int mouse_x, mouse_y;
134
    int mouse_hover;  // updated on mouse-enter/leave
135
    bstr mouse_section; // last section to receive mouse event
136
137
    // Mouse position on the producer side (as the VO sees it)
138
    // Unlike mouse_x/y, this can be used to resolve mouse click bindings.
139
    int mouse_vo_x, mouse_vo_y;
140
141
    bool mouse_mangle, mouse_src_mangle;
142
    struct mp_rect mouse_src, mouse_dst;
143
144
    // Wheel state (MP_WHEEL_*)
145
    struct wheel_state wheel_state_y; // MP_WHEEL_UP/MP_WHEEL_DOWN
146
    struct wheel_state wheel_state_x; // MP_WHEEL_LEFT/MP_WHEEL_RIGHT
147
    struct wheel_state *wheel_current; // The direction currently being scrolled
148
    double last_wheel_time; // mp_time_sec() of the last wheel event
149
150
    // List of command binding sections
151
    struct cmd_bind_section **sections;
152
    int num_sections;
153
154
    // List currently active command sections
155
    struct active_section *active_sections;
156
    int num_active_sections;
157
158
    // List currently active touch points
159
    struct touch_point *touch_points;
160
    int num_touch_points;
161
162
    int tablet_x, tablet_y;
163
    // Indicates tablet tools in proximity / close to tablet surface
164
    bool tablet_tool_in_proximity;
165
    bool tablet_tool_down;
166
    bool tablet_tool_stylus_btn1_pressed;
167
    bool tablet_tool_stylus_btn2_pressed;
168
    bool tablet_tool_stylus_btn3_pressed;
169
    bool tablet_pad_focus;
170
    bool tablet_pad_buttons_pressed[MP_MAX_TABLET_PAD_BUTTONS];
171
    int tablet_pad_buttons;
172
173
    unsigned int mouse_event_counter;
174
175
    struct mp_input_src *sources[MP_MAX_SOURCES];
176
    int num_sources;
177
178
    struct cmd_queue cmd_queue;
179
180
    void (*wakeup_cb)(void *ctx);
181
    void *wakeup_ctx;
182
};
183
184
static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
185
                        const char *location, const bstr restrict_section);
186
static void close_input_sources(struct input_ctx *ictx);
187
static bool test_mouse(struct input_ctx *ictx, int x, int y, int rej_flags);
188
189
#define OPT_BASE_STRUCT struct input_opts
190
struct input_opts {
191
    char *config_file;
192
    int doubleclick_time;
193
    // Maximum number of queued commands from keypresses (limit to avoid
194
    // repeated slow commands piling up)
195
    int key_fifo_size;
196
    // Autorepeat config (be aware of mp_input_set_repeat_info())
197
    int ar_delay;
198
    int ar_rate;
199
    int dragging_deadzone;
200
    bool use_alt_gr;
201
    bool use_gamepad;
202
    bool use_media_keys;
203
    bool default_bindings;
204
    bool builtin_bindings;
205
    bool builtin_dragging;
206
    bool enable_mouse_movements;
207
    bool vo_key_input;
208
    bool test;
209
    bool allow_win_drag;
210
    bool preprocess_wheel;
211
    bool touch_emulate_mouse;
212
    bool tablet_emulate_mouse;
213
};
214
215
const struct m_sub_options input_config = {
216
    .opts = (const m_option_t[]) {
217
        {"input-conf", OPT_STRING(config_file), .flags = M_OPT_FILE},
218
        {"input-ar-delay", OPT_INT(ar_delay)},
219
        {"input-ar-rate", OPT_INT(ar_rate)},
220
        {"input-keylist", OPT_PRINT(mp_print_key_list)},
221
        {"input-cmdlist", OPT_PRINT(mp_print_cmd_list)},
222
        {"input-default-bindings", OPT_BOOL(default_bindings)},
223
        {"input-builtin-bindings", OPT_BOOL(builtin_bindings)},
224
        {"input-builtin-dragging", OPT_BOOL(builtin_dragging)},
225
        {"input-test", OPT_BOOL(test)},
226
        {"input-doubleclick-time", OPT_INT(doubleclick_time),
227
         M_RANGE(0, 1000)},
228
        {"input-right-alt-gr", OPT_BOOL(use_alt_gr)},
229
        {"input-key-fifo-size", OPT_INT(key_fifo_size), M_RANGE(2, 65000)},
230
        {"input-cursor", OPT_BOOL(enable_mouse_movements)},
231
        {"input-vo-keyboard", OPT_BOOL(vo_key_input)},
232
        {"input-media-keys", OPT_BOOL(use_media_keys)},
233
        {"input-preprocess-wheel", OPT_BOOL(preprocess_wheel)},
234
        {"input-touch-emulate-mouse", OPT_BOOL(touch_emulate_mouse)},
235
        {"input-tablet-emulate-mouse", OPT_BOOL(tablet_emulate_mouse)},
236
        {"input-dragging-deadzone", OPT_INT(dragging_deadzone)},
237
#if HAVE_SDL2_GAMEPAD
238
        {"input-gamepad", OPT_BOOL(use_gamepad)},
239
#endif
240
        {"window-dragging", OPT_BOOL(allow_win_drag)},
241
        {0}
242
    },
243
    .size = sizeof(struct input_opts),
244
    .defaults = &(const struct input_opts){
245
        .key_fifo_size = 7,
246
        .doubleclick_time = 300,
247
        .ar_delay = 200,
248
        .ar_rate = 40,
249
        .dragging_deadzone = 3,
250
        .use_alt_gr = true,
251
        .enable_mouse_movements = true,
252
        .use_media_keys = true,
253
        .default_bindings = true,
254
        .builtin_bindings = true,
255
        .builtin_dragging = true,
256
        .vo_key_input = true,
257
        .allow_win_drag = true,
258
        .preprocess_wheel = true,
259
        .touch_emulate_mouse = true,
260
        .tablet_emulate_mouse = true,
261
    },
262
    .change_flags = UPDATE_INPUT,
263
};
264
265
static const char builtin_input_conf[] =
266
#include "etc/input.conf.inc"
267
;
268
269
static bool test_rect(struct mp_rect *rc, int x, int y)
270
0
{
271
0
    return x >= rc->x0 && y >= rc->y0 && x < rc->x1 && y < rc->y1;
272
0
}
273
274
static int queue_count_cmds(struct cmd_queue *queue)
275
0
{
276
0
    int res = 0;
277
0
    for (struct mp_cmd *cmd = queue->first; cmd; cmd = cmd->queue_next)
278
0
        res++;
279
0
    return res;
280
0
}
281
282
static void queue_remove(struct cmd_queue *queue, struct mp_cmd *cmd)
283
0
{
284
0
    struct mp_cmd **p_prev = &queue->first;
285
0
    while (*p_prev != cmd) {
286
0
        p_prev = &(*p_prev)->queue_next;
287
0
    }
288
    // if this fails, cmd was not in the queue
289
0
    mp_assert(*p_prev == cmd);
290
0
    *p_prev = cmd->queue_next;
291
0
}
292
293
static struct mp_cmd *queue_remove_head(struct cmd_queue *queue)
294
1.86M
{
295
1.86M
    struct mp_cmd *ret = queue->first;
296
1.86M
    if (ret)
297
0
        queue_remove(queue, ret);
298
1.86M
    return ret;
299
1.86M
}
300
301
static void queue_add_tail(struct cmd_queue *queue, struct mp_cmd *cmd)
302
0
{
303
0
    struct mp_cmd **p_prev = &queue->first;
304
0
    while (*p_prev)
305
0
        p_prev = &(*p_prev)->queue_next;
306
0
    *p_prev = cmd;
307
0
    cmd->queue_next = NULL;
308
0
}
309
310
static struct mp_cmd *queue_peek_tail(struct cmd_queue *queue)
311
0
{
312
0
    struct mp_cmd *cur = queue->first;
313
0
    while (cur && cur->queue_next)
314
0
        cur = cur->queue_next;
315
0
    return cur;
316
0
}
317
318
static void queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
319
0
{
320
0
    if (!cmd)
321
0
        return;
322
0
    queue_add_tail(&ictx->cmd_queue, cmd);
323
0
    mp_input_wakeup(ictx);
324
0
}
325
326
static void append_bind_info(struct input_ctx *ictx, char **pmsg,
327
                             struct cmd_bind *bind)
328
0
{
329
0
    char *msg = *pmsg;
330
0
    struct mp_cmd *cmd = mp_input_parse_cmd(ictx, bstr0(bind->cmd),
331
0
                                            bind->location);
332
0
    char *stripped = cmd ? cmd->original : bind->cmd;
333
0
    msg = talloc_asprintf_append(msg, " '%s'", stripped);
334
0
    if (!cmd)
335
0
        msg = talloc_asprintf_append(msg, " (invalid)");
336
0
    if (!bstr_equals0(bind->owner->section, "default"))
337
0
        msg = talloc_asprintf_append(msg, " in section {%.*s}",
338
0
                                     BSTR_P(bind->owner->section));
339
0
    msg = talloc_asprintf_append(msg, " in %s", bind->location);
340
0
    if (bind->is_builtin)
341
0
        msg = talloc_asprintf_append(msg, " (default)");
342
0
    talloc_free(cmd);
343
0
    *pmsg = msg;
344
0
}
345
346
static mp_cmd_t *handle_test(struct input_ctx *ictx, int code)
347
0
{
348
0
    if (code == MP_KEY_CLOSE_WIN) {
349
0
        MP_WARN(ictx,
350
0
            "CLOSE_WIN was received. This pseudo key can be remapped too,\n"
351
0
            "but --input-test will always quit when receiving it.\n");
352
0
        const char *args[] = {"quit", NULL};
353
0
        mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
354
0
        return res;
355
0
    }
356
357
0
    char *key_buf = mp_input_get_key_combo_name(&code, 1);
358
0
    char *msg = talloc_asprintf(NULL, "Key %s is bound to:\n", key_buf);
359
0
    talloc_free(key_buf);
360
361
0
    int count = 0;
362
0
    for (int n = 0; n < ictx->num_sections; n++) {
363
0
        struct cmd_bind_section *bs = ictx->sections[n];
364
365
0
        for (int i = 0; i < bs->num_binds; i++) {
366
0
            if (bs->binds[i].num_keys && bs->binds[i].keys[0] == code) {
367
0
                count++;
368
0
                if (count > 1)
369
0
                    msg = talloc_asprintf_append(msg, "\n");
370
0
                msg = talloc_asprintf_append(msg, "%d. ", count);
371
0
                append_bind_info(ictx, &msg, &bs->binds[i]);
372
0
            }
373
0
        }
374
0
    }
375
376
0
    if (!count)
377
0
        msg = talloc_asprintf_append(msg, "(nothing)");
378
379
0
    MP_INFO(ictx, "%s\n", msg);
380
0
    const char *args[] = {"show-text", msg, NULL};
381
0
    mp_cmd_t *res = mp_input_parse_cmd_strv(ictx->log, args);
382
0
    talloc_free(msg);
383
0
    return res;
384
0
}
385
386
static struct cmd_bind_section *find_section(struct input_ctx *ictx,
387
                                             bstr section)
388
27.9M
{
389
28.4M
    for (int n = 0; n < ictx->num_sections; n++) {
390
28.1M
        struct cmd_bind_section *bs = ictx->sections[n];
391
28.1M
        if (bstr_equals(section, bs->section))
392
27.6M
            return bs;
393
28.1M
    }
394
304k
    return NULL;
395
27.9M
}
396
397
static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
398
                                                 bstr section)
399
27.9M
{
400
27.9M
    if (section.len == 0)
401
27.3M
        section = bstr0("default");
402
27.9M
    struct cmd_bind_section *bind_section = find_section(ictx, section);
403
27.9M
    if (bind_section)
404
27.6M
        return bind_section;
405
304k
    bind_section = talloc_ptrtype(ictx, bind_section);
406
304k
    *bind_section = (struct cmd_bind_section) {
407
304k
        .section = bstrdup(bind_section, section),
408
304k
        .mouse_area = {INT_MIN, INT_MIN, INT_MAX, INT_MAX},
409
304k
        .mouse_area_set = true,
410
304k
    };
411
304k
    MP_TARRAY_APPEND(ictx, ictx->sections, ictx->num_sections, bind_section);
412
304k
    return bind_section;
413
27.9M
}
414
415
static void key_buf_add(int *buf, int code)
416
0
{
417
0
    for (int n = MP_MAX_KEY_DOWN - 1; n > 0; n--)
418
0
        buf[n] = buf[n - 1];
419
0
    buf[0] = code;
420
0
}
421
422
static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx,
423
                                                  bstr section, int code)
424
0
{
425
0
    struct cmd_bind_section *bs = get_bind_section(ictx, section);
426
427
0
    if (!bs->num_binds)
428
0
        return NULL;
429
430
0
    int keys[MP_MAX_KEY_DOWN];
431
0
    memcpy(keys, ictx->key_history, sizeof(keys));
432
0
    key_buf_add(keys, code);
433
434
0
    struct cmd_bind *best = NULL;
435
436
    // Prefer user-defined keys over builtin bindings
437
0
    for (int builtin = 0; builtin < 2; builtin++) {
438
0
        if (builtin && !ictx->opts->default_bindings)
439
0
            break;
440
0
        for (int n = 0; n < bs->num_binds; n++) {
441
0
            if (bs->binds[n].is_builtin == (bool)builtin) {
442
0
                struct cmd_bind *b = &bs->binds[n];
443
                // we have: keys=[key2 key1 keyX ...]
444
                // and: b->keys=[key1 key2] (and may be just a prefix)
445
0
                for (int i = 0; i < b->num_keys; i++) {
446
0
                    if (b->keys[i] != keys[b->num_keys - 1 - i])
447
0
                        goto skip;
448
0
                }
449
0
                if (!best || b->num_keys > best->num_keys)
450
0
                    best = b;
451
0
            skip: ;
452
0
            }
453
0
        }
454
0
    }
455
0
    return best;
456
0
}
457
458
static struct cmd_bind *find_any_bind_for_key(struct input_ctx *ictx,
459
                                              bstr force_section, int code)
460
0
{
461
0
    if (force_section.len)
462
0
        return find_bind_for_key_section(ictx, force_section, code);
463
464
0
    bool use_mouse = MP_KEY_DEPENDS_ON_MOUSE_POS(code);
465
466
    // First look whether a mouse section is capturing all mouse input
467
    // exclusively (regardless of the active section stack order).
468
0
    if (use_mouse && MP_KEY_IS_MOUSE_BTN_SINGLE(ictx->last_key_down) &&
469
0
        !MP_KEY_IS_MOUSE_BTN_DBL(code))
470
0
    {
471
0
        struct cmd_bind *bind =
472
0
            find_bind_for_key_section(ictx, ictx->mouse_section, code);
473
0
        if (bind)
474
0
            return bind;
475
0
    }
476
477
0
    struct cmd_bind *best_bind = NULL;
478
0
    for (int i = ictx->num_active_sections - 1; i >= 0; i--) {
479
0
        struct active_section *s = &ictx->active_sections[i];
480
0
        struct cmd_bind *bind = find_bind_for_key_section(ictx, s->name, code);
481
0
        if (bind) {
482
0
            struct cmd_bind_section *bs = bind->owner;
483
0
            if (!use_mouse || (bs->mouse_area_set && test_rect(&bs->mouse_area,
484
0
                                                               ictx->mouse_vo_x,
485
0
                                                               ictx->mouse_vo_y)))
486
0
            {
487
0
                if (!best_bind || bind->num_keys > best_bind->num_keys ||
488
0
                    (best_bind->is_builtin && !bind->is_builtin &&
489
0
                     bind->num_keys == best_bind->num_keys))
490
0
                {
491
0
                    best_bind = bind;
492
0
                }
493
0
            }
494
0
        }
495
0
        if (s->flags & MP_INPUT_EXCLUSIVE)
496
0
            break;
497
0
        if (best_bind && (s->flags & MP_INPUT_ON_TOP))
498
0
            break;
499
0
    }
500
501
0
    return best_bind;
502
0
}
503
504
static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, bstr force_section,
505
                                   int code)
506
0
{
507
0
    if (ictx->opts->test)
508
0
        return handle_test(ictx, code);
509
510
0
    struct cmd_bind *cmd = NULL;
511
512
0
    if (MP_KEY_IS_UNICODE(code))
513
0
        cmd = find_any_bind_for_key(ictx, force_section, MP_KEY_ANY_UNICODE);
514
0
    if (!cmd)
515
0
        cmd = find_any_bind_for_key(ictx, force_section, code);
516
0
    if (!cmd)
517
0
        cmd = find_any_bind_for_key(ictx, force_section, MP_KEY_UNMAPPED);
518
0
    if (!cmd) {
519
0
        if (code == MP_KEY_CLOSE_WIN)
520
0
            return mp_input_parse_cmd_strv(ictx->log, (const char*[]){"quit", 0});
521
0
        int msgl = MSGL_WARN;
522
0
        if (MP_KEY_IS_MOUSE_MOVE(code))
523
0
            msgl = MSGL_TRACE;
524
0
        char *key_buf = mp_input_get_key_combo_name(&code, 1);
525
0
        MP_MSG(ictx, msgl, "No key binding found for key '%s'.\n", key_buf);
526
0
        talloc_free(key_buf);
527
0
        return NULL;
528
0
    }
529
0
    mp_cmd_t *ret = mp_input_parse_cmd(ictx, bstr0(cmd->cmd), cmd->location);
530
0
    if (ret) {
531
0
        ret->input_section = cmd->owner->section;
532
0
        ret->key_name = talloc_steal(ret, mp_input_get_key_combo_name(&code, 1));
533
0
        MP_TRACE(ictx, "key '%s' -> '%s' in '%.*s'\n",
534
0
                 ret->key_name, cmd->cmd, BSTR_P(ret->input_section));
535
0
        if (MP_KEY_IS_UNICODE(code)) {
536
0
            bstr text = {0};
537
0
            mp_append_utf8_bstr(ret, &text, code);
538
0
            ret->key_text = text.start;
539
0
        }
540
0
        ret->is_mouse_button = code & MP_KEY_EMIT_ON_UP;
541
0
    } else {
542
0
        char *key_buf = mp_input_get_key_combo_name(&code, 1);
543
0
        MP_ERR(ictx, "Invalid command for key binding '%s': '%s'\n",
544
0
               key_buf, cmd->cmd);
545
0
        talloc_free(key_buf);
546
0
    }
547
0
    return ret;
548
0
}
549
550
static void update_mouse_section(struct input_ctx *ictx)
551
0
{
552
0
    struct cmd_bind *bind =
553
0
        find_any_bind_for_key(ictx, (bstr){0}, MP_KEY_MOUSE_MOVE);
554
555
0
    bstr new_section = bind ? bind->owner->section : bstr0("default");
556
557
0
    bstr old = ictx->mouse_section;
558
0
    ictx->mouse_section = new_section;
559
560
0
    if (!bstr_equals(old, ictx->mouse_section)) {
561
0
        MP_TRACE(ictx, "input: switch section %.*s -> %.*s\n",
562
0
                 BSTR_P(old), BSTR_P(ictx->mouse_section));
563
0
        queue_cmd(ictx, get_cmd_from_keys(ictx, old, MP_KEY_MOUSE_LEAVE));
564
0
    }
565
0
}
566
567
// Called when the currently held-down key is released. This (usually) sends
568
// the key-up version of the command associated with the keys that were held
569
// down.
570
// If the drop_current parameter is set to true, then don't send the key-up
571
// command. Unless we've already sent a key-down event, in which case the
572
// input receiver (the player) must get a key-up event, or it would get stuck
573
// thinking a key is still held down. In this case, mark the command as
574
// canceled so that it can be distinguished from a normally triggered command.
575
static void release_down_cmd(struct input_ctx *ictx, bool drop_current)
576
0
{
577
0
    if (ictx->current_down_cmd && ictx->current_down_cmd->emit_on_up &&
578
0
        (!drop_current || ictx->current_down_cmd->def->on_updown))
579
0
    {
580
0
        memset(ictx->key_history, 0, sizeof(ictx->key_history));
581
0
        ictx->current_down_cmd->is_up = true;
582
0
        if (drop_current)
583
0
            ictx->current_down_cmd->canceled = true;
584
0
        queue_cmd(ictx, ictx->current_down_cmd);
585
0
    } else {
586
0
        talloc_free(ictx->current_down_cmd);
587
0
    }
588
0
    ictx->current_down_cmd = NULL;
589
0
    ictx->last_key_down = 0;
590
0
    ictx->last_key_down_time = 0;
591
0
    ictx->ar_state = -1;
592
0
    update_mouse_section(ictx);
593
0
}
594
595
// We don't want it to append to the command queue indefinitely, because that
596
// could lead to situations where recovery would take too long.
597
static bool should_drop_cmd(struct input_ctx *ictx, struct mp_cmd *cmd)
598
0
{
599
0
    struct cmd_queue *queue = &ictx->cmd_queue;
600
0
    return queue_count_cmds(queue) >= ictx->opts->key_fifo_size;
601
0
}
602
603
static struct mp_cmd *resolve_key(struct input_ctx *ictx, int code)
604
0
{
605
0
    update_mouse_section(ictx);
606
0
    struct mp_cmd *cmd = get_cmd_from_keys(ictx, (bstr){0}, code);
607
0
    key_buf_add(ictx->key_history, code);
608
0
    if (cmd && !cmd->def->is_ignore && !should_drop_cmd(ictx, cmd))
609
0
        return cmd;
610
0
    talloc_free(cmd);
611
0
    return NULL;
612
0
}
613
614
static void interpret_key(struct input_ctx *ictx, int code, double scale,
615
                          int scale_units)
616
0
{
617
0
    int state = code & (MP_KEY_STATE_DOWN | MP_KEY_STATE_UP);
618
0
    bool no_emit = code & MP_KEY_STATE_SET_ONLY;
619
0
    code = code & ~(unsigned)(state | MP_KEY_STATE_SET_ONLY);
620
621
0
    if (mp_msg_test(ictx->log, MSGL_TRACE)) {
622
0
        char *key = mp_input_get_key_name(code);
623
0
        MP_TRACE(ictx, "key code=%#x '%s'%s%s\n",
624
0
                 code, key, (state & MP_KEY_STATE_DOWN) ? " down" : "",
625
0
                 (state & MP_KEY_STATE_UP) ? " up" : "");
626
0
        talloc_free(key);
627
0
    }
628
629
0
    if (MP_KEY_DEPENDS_ON_MOUSE_POS(code & ~MP_KEY_MODIFIER_MASK)) {
630
0
        ictx->mouse_event_counter++;
631
0
        mp_input_wakeup(ictx);
632
0
    }
633
634
0
    struct mp_cmd *cmd = NULL;
635
636
0
    if (state == MP_KEY_STATE_DOWN) {
637
        // Protect against VOs which send STATE_DOWN with autorepeat
638
0
        if (ictx->last_key_down == code)
639
0
            return;
640
        // Cancel current down-event (there can be only one)
641
0
        release_down_cmd(ictx, true);
642
0
        cmd = resolve_key(ictx, code);
643
0
        if (cmd) {
644
0
            cmd->is_up_down = true;
645
0
            cmd->emit_on_up = (code & MP_KEY_EMIT_ON_UP) || cmd->def->on_updown;
646
0
            ictx->current_down_cmd = mp_cmd_clone(cmd);
647
0
        }
648
0
        ictx->last_key_down = code;
649
0
        ictx->last_key_down_time = mp_time_ns();
650
0
        ictx->ar_state = 0;
651
0
        mp_input_wakeup(ictx); // possibly start timer for autorepeat
652
0
    } else if (state == MP_KEY_STATE_UP) {
653
        // Most VOs send RELEASE_ALL anyway
654
0
        release_down_cmd(ictx, false);
655
0
    } else {
656
        // Press of key with no separate down/up events
657
        // Mixing press events and up/down with the same key is not supported,
658
        // and input sources shouldn't do this, but can happen anyway if
659
        // multiple input sources interfere with each other.
660
0
        if (ictx->last_key_down == code)
661
0
            release_down_cmd(ictx, false);
662
0
        cmd = resolve_key(ictx, code);
663
0
    }
664
665
0
    if (!cmd)
666
0
        return;
667
668
    // Don't emit a command on key-down if the key is designed to emit commands
669
    // on key-up (like mouse buttons), or setting key state only without emitting commands.
670
    // Also, if the command specifically should be sent both on key down and key up,
671
    // still emit the command.
672
0
    if ((cmd->emit_on_up && !cmd->def->on_updown) || no_emit) {
673
0
        talloc_free(cmd);
674
0
        return;
675
0
    }
676
677
0
    memset(ictx->key_history, 0, sizeof(ictx->key_history));
678
679
0
    if (mp_input_is_scalable_cmd(cmd)) {
680
0
        cmd->scale = scale;
681
0
        cmd->scale_units = scale_units;
682
0
        queue_cmd(ictx, cmd);
683
0
    } else {
684
        // Non-scalable commands won't understand cmd->scale, so synthesize
685
        // multiple commands with cmd->scale = 1
686
0
        cmd->scale = 1;
687
0
        cmd->scale_units = 1;
688
        // Avoid spamming the player with too many commands
689
0
        scale_units = MPMIN(scale_units, 20);
690
0
        for (int i = 0; i < scale_units - 1; i++)
691
0
            queue_cmd(ictx, mp_cmd_clone(cmd));
692
0
        if (scale_units) {
693
0
            queue_cmd(ictx, cmd);
694
0
        } else {
695
0
            talloc_free(cmd);
696
0
        }
697
0
    }
698
0
}
699
700
// Pre-processing for MP_WHEEL_* events. If this returns false, the caller
701
// should discard the event.
702
static bool process_wheel(struct input_ctx *ictx, int code, double *scale,
703
                          int *scale_units)
704
0
{
705
    // Size of the deadzone in scroll units. The user must scroll at least this
706
    // much in any direction before their scroll is registered.
707
0
    static const double DEADZONE_DIST = 0.125;
708
    // The deadzone accumulator is reset if no scrolls happened in this many
709
    // seconds, e.g. the user is assumed to have finished scrolling.
710
0
    static const double DEADZONE_SCROLL_TIME = 0.2;
711
    // The scale_units accumulator is reset if no scrolls happened in this many
712
    // seconds. This value should be fairly large, so commands will still be
713
    // sent when the user scrolls slowly.
714
0
    static const double UNIT_SCROLL_TIME = 0.5;
715
716
    // Determine which direction is being scrolled
717
0
    double dir;
718
0
    struct wheel_state *state;
719
0
    switch (code) {
720
0
    case MP_WHEEL_UP:    dir = -1; state = &ictx->wheel_state_y; break;
721
0
    case MP_WHEEL_DOWN:  dir = +1; state = &ictx->wheel_state_y; break;
722
0
    case MP_WHEEL_LEFT:  dir = -1; state = &ictx->wheel_state_x; break;
723
0
    case MP_WHEEL_RIGHT: dir = +1; state = &ictx->wheel_state_x; break;
724
0
    default:
725
0
        return true;
726
0
    }
727
728
    // Reset accumulators if it's determined that the user finished scrolling
729
0
    double now = mp_time_sec();
730
0
    if (now > ictx->last_wheel_time + DEADZONE_SCROLL_TIME) {
731
0
        ictx->wheel_current = NULL;
732
0
        ictx->wheel_state_y.dead_zone_accum = 0;
733
0
        ictx->wheel_state_x.dead_zone_accum = 0;
734
0
    }
735
0
    if (now > ictx->last_wheel_time + UNIT_SCROLL_TIME) {
736
0
        ictx->wheel_state_y.unit_accum = 0;
737
0
        ictx->wheel_state_x.unit_accum = 0;
738
0
    }
739
0
    ictx->last_wheel_time = now;
740
741
    // Process wheel deadzone. A lot of touchpad drivers don't filter scroll
742
    // input, which makes it difficult for the user to send WHEEL_UP/DOWN
743
    // without accidentally triggering WHEEL_LEFT/RIGHT. We try to fix this by
744
    // implementing a deadzone. When the value of either direction breaks out
745
    // of the deadzone, events from the other direction will be ignored until
746
    // the user finishes scrolling.
747
0
    if (ictx->wheel_current == NULL) {
748
0
        state->dead_zone_accum += *scale * dir;
749
0
        if (state->dead_zone_accum * dir > DEADZONE_DIST) {
750
0
            ictx->wheel_current = state;
751
0
            *scale = state->dead_zone_accum * dir;
752
0
        }
753
0
    }
754
0
    if (ictx->wheel_current != state)
755
0
        return false;
756
757
    // Determine scale_units. This is incremented every time the accumulated
758
    // scale value crosses 1.0. Non-scalable input commands will be ran that
759
    // many times.
760
0
    state->unit_accum += *scale * dir;
761
0
    *scale_units = trunc(state->unit_accum * dir);
762
0
    state->unit_accum -= *scale_units * dir;
763
0
    return true;
764
0
}
765
766
static void feed_key(struct input_ctx *ictx, int code, double scale,
767
                              bool force_mouse)
768
0
{
769
0
    struct input_opts *opts = ictx->opts;
770
771
0
    code = mp_normalize_keycode(code);
772
0
    int unmod = code & ~MP_KEY_MODIFIER_MASK;
773
0
    if (code == MP_INPUT_RELEASE_ALL) {
774
0
        MP_TRACE(ictx, "release all\n");
775
0
        release_down_cmd(ictx, false);
776
0
        ictx->dragging_button_down = false;
777
0
        return;
778
0
    }
779
0
    if (code == MP_TOUCH_RELEASE_ALL) {
780
0
        MP_TRACE(ictx, "release all touch\n");
781
0
        ictx->num_touch_points = 0;
782
0
        return;
783
0
    }
784
0
    if (!opts->enable_mouse_movements && MP_KEY_IS_MOUSE(unmod) && !force_mouse)
785
0
        return;
786
0
    if (unmod == MP_KEY_MOUSE_LEAVE || unmod == MP_KEY_MOUSE_ENTER) {
787
0
        ictx->mouse_hover = unmod == MP_KEY_MOUSE_ENTER;
788
0
        update_mouse_section(ictx);
789
790
0
        mp_cmd_t *cmd = get_cmd_from_keys(ictx, (bstr){0}, code);
791
0
        if (!cmd)  // queue dummy cmd so that mouse-pos can notify observers
792
0
            cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
793
0
        if (cmd)
794
0
            cmd->notify_event = true;
795
0
        queue_cmd(ictx, cmd);
796
0
        return;
797
0
    }
798
0
    double now = mp_time_sec();
799
    // ignore system double-click if we generate these events ourselves
800
0
    if (!force_mouse && opts->doubleclick_time && MP_KEY_IS_MOUSE_BTN_DBL(unmod))
801
0
        return;
802
0
    int units = 1;
803
0
    if (MP_KEY_IS_WHEEL(unmod) && opts->preprocess_wheel && !process_wheel(ictx, unmod, &scale, &units))
804
0
        return;
805
0
    interpret_key(ictx, code, scale, units);
806
0
    if (code & MP_KEY_STATE_DOWN) {
807
0
        code &= ~MP_KEY_STATE_DOWN;
808
0
        if (ictx->last_doubleclick_key_down == code &&
809
0
            now - ictx->last_doubleclick_time < opts->doubleclick_time / 1000.0 &&
810
0
            code >= MP_MBTN_LEFT && code <= MP_MBTN_RIGHT)
811
0
        {
812
0
            now = 0;
813
0
            interpret_key(ictx, code - MP_MBTN_BASE + MP_MBTN_DBL_BASE,
814
0
                          1, 1);
815
0
        } else if (code == MP_MBTN_LEFT && ictx->opts->allow_win_drag &&
816
0
                   !test_mouse(ictx, ictx->mouse_vo_x, ictx->mouse_vo_y, MP_INPUT_ALLOW_VO_DRAGGING))
817
0
        {
818
            // This is a mouse left button down event which isn't part of a double-click,
819
            // and the mouse is on an input section which allows VO dragging.
820
            // Mark the dragging mouse button down in this case.
821
0
            ictx->dragging_button_down = true;
822
            // Store the current mouse position for deadzone handling.
823
0
            ictx->mouse_drag_x = ictx->mouse_raw_x;
824
0
            ictx->mouse_drag_y = ictx->mouse_raw_y;
825
0
        }
826
0
        ictx->last_doubleclick_key_down = code;
827
0
        ictx->last_doubleclick_time = now;
828
0
    }
829
0
    if (code & MP_KEY_STATE_UP) {
830
0
        code &= ~MP_KEY_STATE_UP;
831
0
        if (code == MP_MBTN_LEFT) {
832
            // This is a mouse left button up event. Mark the dragging mouse button up.
833
0
            ictx->dragging_button_down = false;
834
0
        }
835
0
    }
836
0
}
837
838
void mp_input_put_key(struct input_ctx *ictx, int code)
839
0
{
840
0
    input_lock(ictx);
841
0
    feed_key(ictx, code, 1, false);
842
0
    input_unlock(ictx);
843
0
}
844
845
void mp_input_put_key_artificial(struct input_ctx *ictx, int code, double value)
846
0
{
847
0
    if (value == 0.0)
848
0
        return;
849
0
    input_lock(ictx);
850
0
    feed_key(ictx, code, value, true);
851
0
    input_unlock(ictx);
852
0
}
853
854
void mp_input_put_key_utf8(struct input_ctx *ictx, int mods, struct bstr t)
855
0
{
856
0
    if (!t.len)
857
0
        return;
858
0
    input_lock(ictx);
859
0
    while (t.len) {
860
0
        int code = bstr_decode_utf8(t, &t);
861
0
        if (code < 0)
862
0
            break;
863
0
        feed_key(ictx, code | mods, 1, false);
864
0
    }
865
0
    input_unlock(ictx);
866
0
}
867
868
void mp_input_put_wheel(struct input_ctx *ictx, int direction, double value)
869
0
{
870
0
    if (value == 0.0)
871
0
        return;
872
0
    input_lock(ictx);
873
0
    feed_key(ictx, direction, value, false);
874
0
    input_unlock(ictx);
875
0
}
876
877
void mp_input_set_mouse_transform(struct input_ctx *ictx, struct mp_rect *dst,
878
                                  struct mp_rect *src)
879
61.8k
{
880
61.8k
    input_lock(ictx);
881
61.8k
    ictx->mouse_mangle = dst || src;
882
61.8k
    if (ictx->mouse_mangle) {
883
0
        ictx->mouse_dst = *dst;
884
0
        ictx->mouse_src_mangle = !!src;
885
0
        if (ictx->mouse_src_mangle)
886
0
            ictx->mouse_src = *src;
887
0
    }
888
61.8k
    input_unlock(ictx);
889
61.8k
}
890
891
bool mp_input_mouse_enabled(struct input_ctx *ictx)
892
0
{
893
0
    input_lock(ictx);
894
0
    bool r = ictx->opts->enable_mouse_movements;
895
0
    input_unlock(ictx);
896
0
    return r;
897
0
}
898
899
bool mp_input_vo_keyboard_enabled(struct input_ctx *ictx)
900
0
{
901
0
    input_lock(ictx);
902
0
    bool r = ictx->opts->vo_key_input;
903
0
    input_unlock(ictx);
904
0
    return r;
905
0
}
906
907
static void set_mouse_pos(struct input_ctx *ictx, int x, int y, bool quiet)
908
0
{
909
0
    MP_TRACE(ictx, "mouse move %d/%d\n", x, y);
910
911
0
    if (ictx->mouse_raw_x == x && ictx->mouse_raw_y == y) {
912
0
        return;
913
0
    }
914
0
    ictx->mouse_raw_x = x;
915
0
    ictx->mouse_raw_y = y;
916
917
0
    if (ictx->mouse_mangle) {
918
0
        struct mp_rect *src = &ictx->mouse_src;
919
0
        struct mp_rect *dst = &ictx->mouse_dst;
920
0
        x = MPCLAMP(x, dst->x0, dst->x1) - dst->x0;
921
0
        y = MPCLAMP(y, dst->y0, dst->y1) - dst->y0;
922
0
        if (ictx->mouse_src_mangle) {
923
0
            x = x * 1.0 / (dst->x1 - dst->x0) * (src->x1 - src->x0) + src->x0;
924
0
            y = y * 1.0 / (dst->y1 - dst->y0) * (src->y1 - src->y0) + src->y0;
925
0
        }
926
0
        MP_TRACE(ictx, "-> %d/%d\n", x, y);
927
0
    }
928
929
0
    if (!quiet)
930
0
        ictx->mouse_event_counter++;
931
0
    ictx->mouse_vo_x = x;
932
0
    ictx->mouse_vo_y = y;
933
934
0
    update_mouse_section(ictx);
935
0
    struct mp_cmd *cmd = get_cmd_from_keys(ictx, (bstr){0}, MP_KEY_MOUSE_MOVE);
936
0
    if (!cmd)
937
0
        cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
938
939
0
    if (cmd) {
940
0
        cmd->mouse_move = true;
941
0
        cmd->notify_event = true;
942
0
        cmd->mouse_x = x;
943
0
        cmd->mouse_y = y;
944
0
        if (should_drop_cmd(ictx, cmd)) {
945
0
            talloc_free(cmd);
946
0
        } else {
947
            // Coalesce with previous mouse move events (i.e. replace it)
948
0
            struct mp_cmd *tail = queue_peek_tail(&ictx->cmd_queue);
949
0
            if (tail && tail->mouse_move) {
950
0
                queue_remove(&ictx->cmd_queue, tail);
951
0
                talloc_free(tail);
952
0
            }
953
0
            queue_cmd(ictx, cmd);
954
0
        }
955
0
    }
956
957
0
    bool mouse_outside_dragging_deadzone =
958
0
        abs(ictx->mouse_raw_x - ictx->mouse_drag_x) >= ictx->opts->dragging_deadzone ||
959
0
        abs(ictx->mouse_raw_y - ictx->mouse_drag_y) >= ictx->opts->dragging_deadzone;
960
0
    if (ictx->dragging_button_down && mouse_outside_dragging_deadzone &&
961
0
        ictx->opts->builtin_dragging)
962
0
    {
963
        // Begin built-in VO dragging if the mouse moves while the dragging button is down.
964
0
        ictx->dragging_button_down = false;
965
        // Prevent activation of MBTN_LEFT key binding if VO dragging begins.
966
0
        release_down_cmd(ictx, true);
967
        // Prevent activation of MBTN_LEFT_DBL if VO dragging begins.
968
0
        ictx->last_doubleclick_time = 0;
969
0
        mp_cmd_t *drag_cmd = mp_input_parse_cmd(ictx, bstr0("begin-vo-dragging"), "<internal>");
970
0
        queue_cmd(ictx, drag_cmd);
971
0
    }
972
0
}
973
974
void mp_input_set_mouse_pos_artificial(struct input_ctx *ictx, int x, int y)
975
0
{
976
0
    input_lock(ictx);
977
0
    set_mouse_pos(ictx, x, y, false);
978
0
    input_unlock(ictx);
979
0
}
980
981
void mp_input_set_mouse_pos(struct input_ctx *ictx, int x, int y, bool quiet)
982
0
{
983
0
    input_lock(ictx);
984
0
    if (ictx->opts->enable_mouse_movements)
985
0
        set_mouse_pos(ictx, x, y, quiet);
986
0
    input_unlock(ictx);
987
0
}
988
989
static int find_touch_point_index(struct input_ctx *ictx, int id)
990
0
{
991
0
    for (int i = 0; i < ictx->num_touch_points; i++) {
992
0
        if (ictx->touch_points[i].id == id)
993
0
            return i;
994
0
    }
995
0
    return -1;
996
0
}
997
998
static void notify_touch_update(struct input_ctx *ictx)
999
0
{
1000
    // queue dummy cmd so that touch-pos can notify observers
1001
0
    mp_cmd_t *cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
1002
0
    if (cmd)
1003
0
        cmd->notify_event = true;
1004
0
    queue_cmd(ictx, cmd);
1005
0
}
1006
1007
static void update_touch_point(struct input_ctx *ictx, int idx, int id, int x, int y)
1008
0
{
1009
0
    MP_TRACE(ictx, "Touch point %d update (id %d) %d/%d\n",
1010
0
             idx, id, x, y);
1011
0
    if (ictx->touch_points[idx].x == x && ictx->touch_points[idx].y == y)
1012
0
        return;
1013
0
    ictx->touch_points[idx].x = x;
1014
0
    ictx->touch_points[idx].y = y;
1015
    // Emulate mouse input from the primary touch point (the first one added)
1016
0
    if (ictx->opts->touch_emulate_mouse && idx == 0)
1017
0
        set_mouse_pos(ictx, x, y, false);
1018
0
    notify_touch_update(ictx);
1019
0
}
1020
1021
void mp_input_add_touch_point(struct input_ctx *ictx, int id, int x, int y)
1022
0
{
1023
0
    input_lock(ictx);
1024
0
    int idx = find_touch_point_index(ictx, id);
1025
0
    if (idx != -1) {
1026
0
        MP_WARN(ictx, "Touch point %d (id %d) already exists! Treat as update.\n",
1027
0
                idx, id);
1028
0
        update_touch_point(ictx, idx, id, x, y);
1029
0
    } else {
1030
0
        MP_TRACE(ictx, "Touch point %d add (id %d) %d/%d\n",
1031
0
                 ictx->num_touch_points, id, x, y);
1032
0
        MP_TARRAY_APPEND(ictx, ictx->touch_points, ictx->num_touch_points,
1033
0
                         (struct touch_point){id, x, y});
1034
        // Emulate MBTN_LEFT down if this is the only touch point
1035
0
        if (ictx->opts->touch_emulate_mouse && ictx->num_touch_points == 1) {
1036
0
            set_mouse_pos(ictx, x, y, false);
1037
0
            feed_key(ictx, MP_MBTN_LEFT | MP_KEY_STATE_DOWN, 1, false);
1038
0
        }
1039
0
        notify_touch_update(ictx);
1040
0
    }
1041
0
    input_unlock(ictx);
1042
0
}
1043
1044
void mp_input_update_touch_point(struct input_ctx *ictx, int id, int x, int y)
1045
0
{
1046
0
    input_lock(ictx);
1047
0
    int idx = find_touch_point_index(ictx, id);
1048
0
    if (idx != -1) {
1049
0
        update_touch_point(ictx, idx, id, x, y);
1050
0
    } else {
1051
0
        MP_WARN(ictx, "Touch point id %d does not exist!\n", id);
1052
0
    }
1053
0
    input_unlock(ictx);
1054
0
}
1055
1056
void mp_input_remove_touch_point(struct input_ctx *ictx, int id)
1057
0
{
1058
0
    input_lock(ictx);
1059
0
    int idx = find_touch_point_index(ictx, id);
1060
0
    if (idx != -1) {
1061
0
        MP_TRACE(ictx, "Touch point %d remove (id %d)\n", idx, id);
1062
0
        MP_TARRAY_REMOVE_AT(ictx->touch_points, ictx->num_touch_points, idx);
1063
        // Emulate MBTN_LEFT up if there are no touch points left
1064
0
        if (ictx->opts->touch_emulate_mouse && ictx->num_touch_points == 0)
1065
0
            feed_key(ictx, MP_MBTN_LEFT | MP_KEY_STATE_UP, 1, false);
1066
0
        notify_touch_update(ictx);
1067
0
    }
1068
0
    input_unlock(ictx);
1069
0
}
1070
1071
int mp_input_get_touch_pos(struct input_ctx *ictx, int count, int *x, int *y, int *id)
1072
100
{
1073
100
    input_lock(ictx);
1074
100
    int num_touch_points = ictx->num_touch_points;
1075
100
    for (int i = 0; i < MPMIN(num_touch_points, count); i++) {
1076
0
        x[i] = ictx->touch_points[i].x;
1077
0
        y[i] = ictx->touch_points[i].y;
1078
0
        id[i] = ictx->touch_points[i].id;
1079
0
    }
1080
100
    input_unlock(ictx);
1081
100
    return num_touch_points;
1082
100
}
1083
1084
static void notify_tablet_update(struct input_ctx *ictx)
1085
0
{
1086
    // queue dummy cmd so that tablet-pos can notify observers
1087
0
    mp_cmd_t *cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
1088
0
    if (cmd)
1089
0
        cmd->notify_event = true;
1090
0
    queue_cmd(ictx, cmd);
1091
0
}
1092
1093
void mp_input_set_tablet_tool_in_proximity(struct input_ctx *ictx, bool in_proximity)
1094
0
{
1095
0
    MP_TRACE(ictx, "tablet tool proximity %s\n", in_proximity ? "in" : "out");
1096
1097
0
    input_lock(ictx);
1098
0
    ictx->tablet_tool_in_proximity = in_proximity;
1099
0
    if (!in_proximity) {
1100
0
        ictx->tablet_tool_down = false;
1101
0
        ictx->tablet_tool_stylus_btn1_pressed = false;
1102
0
        ictx->tablet_tool_stylus_btn2_pressed = false;
1103
0
        ictx->tablet_tool_stylus_btn3_pressed = false;
1104
0
    }
1105
0
    notify_tablet_update(ictx);
1106
0
    input_unlock(ictx);
1107
0
}
1108
1109
void mp_input_tablet_tool_down(struct input_ctx *ictx)
1110
0
{
1111
0
    MP_TRACE(ictx, "tablet tool down\n");
1112
1113
0
    input_lock(ictx);
1114
0
    ictx->tablet_tool_down = true;
1115
0
    if (ictx->opts->tablet_emulate_mouse && ictx->tablet_tool_in_proximity)
1116
0
        feed_key(ictx, MP_MBTN_LEFT | MP_KEY_STATE_DOWN, 1, false);
1117
1118
0
    notify_tablet_update(ictx);
1119
0
    input_unlock(ictx);
1120
0
}
1121
1122
void mp_input_tablet_tool_up(struct input_ctx *ictx)
1123
0
{
1124
0
    MP_TRACE(ictx, "tablet tool up\n");
1125
1126
0
    input_lock(ictx);
1127
0
    ictx->tablet_tool_down = false;
1128
0
    if (ictx->opts->tablet_emulate_mouse && ictx->tablet_tool_in_proximity)
1129
0
        feed_key(ictx, MP_MBTN_LEFT | MP_KEY_STATE_UP, 1, false);
1130
1131
0
    notify_tablet_update(ictx);
1132
0
    input_unlock(ictx);
1133
0
}
1134
1135
void mp_input_tablet_tool_button(struct input_ctx *ictx, int button, int state)
1136
0
{
1137
0
    char *key = mp_input_get_key_name(button);
1138
0
    MP_TRACE(ictx, "tablet tool button %s %s%s \n",
1139
0
             key,
1140
0
             (state & MP_KEY_STATE_DOWN) ? "pressed" : "",
1141
0
             (state & MP_KEY_STATE_UP) ? "released" : "");
1142
1143
0
    input_lock(ictx);
1144
1145
0
    switch (button) {
1146
0
    case MP_KEY_TABLET_TOOL_STYLUS_BTN1:
1147
0
        ictx->tablet_tool_stylus_btn1_pressed = state == MP_KEY_STATE_DOWN;
1148
0
        button = MP_MBTN_MID;
1149
0
        break;
1150
0
    case MP_KEY_TABLET_TOOL_STYLUS_BTN2:
1151
0
        ictx->tablet_tool_stylus_btn2_pressed = state == MP_KEY_STATE_DOWN;
1152
0
        button = MP_MBTN_RIGHT;
1153
0
        break;
1154
0
    case MP_KEY_TABLET_TOOL_STYLUS_BTN3:
1155
0
        ictx->tablet_tool_stylus_btn3_pressed = state == MP_KEY_STATE_DOWN;
1156
0
        button = MP_MBTN_BACK;
1157
0
        break;
1158
0
    default:
1159
0
        break;
1160
0
    }
1161
1162
0
    if (ictx->opts->tablet_emulate_mouse && ictx->tablet_tool_in_proximity && button)
1163
0
        feed_key(ictx, button | state, 1, false);
1164
1165
0
    notify_tablet_update(ictx);
1166
0
    input_unlock(ictx);
1167
0
}
1168
1169
void mp_input_set_tablet_pad_focus(struct input_ctx *ictx, bool focus, int buttons)
1170
0
{
1171
0
    MP_TRACE(ictx, "tablet pad focus %s\n", focus ? "enter" : "leave");
1172
1173
0
    input_lock(ictx);
1174
0
    ictx->tablet_pad_focus = focus;
1175
0
    if (!focus) {
1176
0
        for (int i = 0; i < ictx->tablet_pad_buttons; i++)
1177
0
            ictx->tablet_pad_buttons_pressed[i] = false;
1178
0
    }
1179
0
    ictx->tablet_pad_buttons = buttons;
1180
0
    notify_tablet_update(ictx);
1181
0
    input_unlock(ictx);
1182
0
}
1183
1184
void mp_input_tablet_pad_button(struct input_ctx *ictx, int button, int state)
1185
0
{
1186
0
    MP_TRACE(ictx, "tablet pad button %d %s%s \n",
1187
0
             button,
1188
0
             (state & MP_KEY_STATE_DOWN) ? "pressed" : "",
1189
0
             (state & MP_KEY_STATE_UP) ? "released" : "");
1190
1191
0
    input_lock(ictx);
1192
0
    if (button < MP_MAX_TABLET_PAD_BUTTONS)
1193
0
        ictx->tablet_pad_buttons_pressed[button] = state == MP_KEY_STATE_DOWN;
1194
0
    else
1195
0
        MP_WARN(ictx, "Tablet pad button %d outside of range!\n", button);
1196
1197
0
    notify_tablet_update(ictx);
1198
0
    input_unlock(ictx);
1199
0
}
1200
1201
void mp_input_set_tablet_pos(struct input_ctx *ictx, int x, int y, bool quiet)
1202
0
{
1203
0
    MP_TRACE(ictx, "tablet tool position %d/%d \n", x, y);
1204
1205
0
    input_lock(ictx);
1206
0
    ictx->tablet_x = x;
1207
0
    ictx->tablet_y = y;
1208
0
    if (ictx->opts->tablet_emulate_mouse && ictx->tablet_tool_in_proximity)
1209
0
        set_mouse_pos(ictx, x, y, quiet);
1210
1211
0
    notify_tablet_update(ictx);
1212
0
    input_unlock(ictx);
1213
0
}
1214
1215
void mp_input_get_tablet_pos(struct input_ctx *ictx, int *x, int *y,
1216
                             bool *tool_in_proximity, bool *tool_down,
1217
                             bool *tool_stylus_btn1_pressed,
1218
                             bool *tool_stylus_btn2_pressed,
1219
                             bool *tool_stylus_btn3_pressed,
1220
                             bool *pad_focus,
1221
                             bool **pad_buttons_pressed,
1222
                             int *pad_buttons)
1223
69
{
1224
69
    input_lock(ictx);
1225
69
    *x = ictx->tablet_x;
1226
69
    *y = ictx->tablet_y;
1227
69
    *tool_in_proximity = ictx->tablet_tool_in_proximity;
1228
69
    *tool_down = ictx->tablet_tool_down;
1229
69
    *tool_stylus_btn1_pressed = ictx->tablet_tool_stylus_btn1_pressed;
1230
69
    *tool_stylus_btn2_pressed = ictx->tablet_tool_stylus_btn2_pressed;
1231
69
    *tool_stylus_btn3_pressed = ictx->tablet_tool_stylus_btn3_pressed;
1232
69
    *pad_focus = ictx->tablet_pad_focus;
1233
69
    *pad_buttons_pressed = ictx->tablet_pad_buttons_pressed;
1234
69
    *pad_buttons = ictx->tablet_pad_buttons;
1235
69
    input_unlock(ictx);
1236
69
}
1237
1238
static bool test_mouse(struct input_ctx *ictx, int x, int y, int rej_flags)
1239
907k
{
1240
907k
    bool res = false;
1241
1.81M
    for (int i = 0; i < ictx->num_active_sections; i++) {
1242
907k
        struct active_section *as = &ictx->active_sections[i];
1243
907k
        if (as->flags & rej_flags)
1244
907k
            continue;
1245
0
        struct cmd_bind_section *s = get_bind_section(ictx, as->name);
1246
0
        if (s->mouse_area_set && test_rect(&s->mouse_area, x, y)) {
1247
0
            res = true;
1248
0
            break;
1249
0
        }
1250
0
    }
1251
907k
    return res;
1252
907k
}
1253
1254
static bool test_mouse_active(struct input_ctx *ictx, int x, int y)
1255
907k
{
1256
907k
    return test_mouse(ictx, x, y, MP_INPUT_ALLOW_HIDE_CURSOR);
1257
907k
}
1258
1259
bool mp_input_test_mouse_active(struct input_ctx *ictx, int x, int y)
1260
0
{
1261
0
    input_lock(ictx);
1262
0
    bool res = test_mouse_active(ictx, x, y);
1263
0
    input_unlock(ictx);
1264
0
    return res;
1265
0
}
1266
1267
bool mp_input_test_dragging(struct input_ctx *ictx, int x, int y)
1268
0
{
1269
0
    input_lock(ictx);
1270
0
    bool r = !ictx->opts->allow_win_drag ||
1271
0
                        test_mouse(ictx, x, y, MP_INPUT_ALLOW_VO_DRAGGING);
1272
0
    input_unlock(ictx);
1273
0
    return r;
1274
0
}
1275
1276
unsigned int mp_input_get_mouse_event_counter(struct input_ctx *ictx)
1277
907k
{
1278
    // Make the frontend always display the mouse cursor (as long as it's not
1279
    // forced invisible) if mouse input is desired.
1280
907k
    input_lock(ictx);
1281
907k
    if (test_mouse_active(ictx, ictx->mouse_x, ictx->mouse_y))
1282
0
        ictx->mouse_event_counter++;
1283
907k
    int ret = ictx->mouse_event_counter;
1284
907k
    input_unlock(ictx);
1285
907k
    return ret;
1286
907k
}
1287
1288
// adjust min time to wait until next repeat event
1289
static void adjust_max_wait_time(struct input_ctx *ictx, double *time)
1290
1.86M
{
1291
1.86M
    struct input_opts *opts = ictx->opts;
1292
1.86M
    if (ictx->last_key_down && opts->ar_rate > 0 && ictx->ar_state >= 0) {
1293
0
        *time = MPMIN(*time, 1.0 / opts->ar_rate);
1294
0
        *time = MPMIN(*time, opts->ar_delay / 1000.0);
1295
0
    }
1296
1.86M
}
1297
1298
int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1299
0
{
1300
0
    if (!cmd)
1301
0
        return 0;
1302
0
    input_lock(ictx);
1303
0
    queue_cmd(ictx, cmd);
1304
0
    input_unlock(ictx);
1305
0
    return 1;
1306
0
}
1307
1308
static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1309
1.86M
{
1310
1.86M
    struct input_opts *opts = ictx->opts;
1311
1312
    // No input : autorepeat ?
1313
1.86M
    if (opts->ar_rate <= 0 || !ictx->current_down_cmd || !ictx->last_key_down ||
1314
0
        (ictx->last_key_down & MP_NO_REPEAT_KEY) ||
1315
0
        !mp_input_is_repeatable_cmd(ictx->current_down_cmd))
1316
1.86M
        ictx->ar_state = -1; // disable
1317
1318
1.86M
    if (ictx->ar_state >= 0) {
1319
0
        int64_t t = mp_time_ns();
1320
0
        if (ictx->last_ar + MP_TIME_S_TO_NS(2) < t)
1321
0
            ictx->last_ar = t;
1322
        // First time : wait delay
1323
0
        if (ictx->ar_state == 0
1324
0
            && (t - ictx->last_key_down_time) >= MP_TIME_MS_TO_NS(opts->ar_delay))
1325
0
        {
1326
0
            ictx->ar_state = 1;
1327
0
            ictx->last_ar = ictx->last_key_down_time + MP_TIME_MS_TO_NS(opts->ar_delay);
1328
            // Then send rate / sec event
1329
0
        } else if (ictx->ar_state == 1
1330
0
                   && (t - ictx->last_ar) >= 1e9 / opts->ar_rate) {
1331
0
            ictx->last_ar += 1e9 / opts->ar_rate;
1332
0
        } else {
1333
0
            return NULL;
1334
0
        }
1335
0
        struct mp_cmd *ret = mp_cmd_clone(ictx->current_down_cmd);
1336
0
        ret->repeated = true;
1337
0
        return ret;
1338
0
    }
1339
1.86M
    return NULL;
1340
1.86M
}
1341
1342
double mp_input_get_delay(struct input_ctx *ictx)
1343
1.86M
{
1344
1.86M
    input_lock(ictx);
1345
1.86M
    double seconds = INFINITY;
1346
1.86M
    adjust_max_wait_time(ictx, &seconds);
1347
1.86M
    input_unlock(ictx);
1348
1.86M
    return seconds;
1349
1.86M
}
1350
1351
void mp_input_wakeup(struct input_ctx *ictx)
1352
0
{
1353
0
    ictx->wakeup_cb(ictx->wakeup_ctx);
1354
0
}
1355
1356
mp_cmd_t *mp_input_read_cmd(struct input_ctx *ictx)
1357
1.86M
{
1358
1.86M
    input_lock(ictx);
1359
1.86M
    struct mp_cmd *ret = queue_remove_head(&ictx->cmd_queue);
1360
1.86M
    if (!ret)
1361
1.86M
        ret = check_autorepeat(ictx);
1362
1.86M
    if (ret && ret->mouse_move) {
1363
0
        ictx->mouse_x = ret->mouse_x;
1364
0
        ictx->mouse_y = ret->mouse_y;
1365
0
    }
1366
1.86M
    input_unlock(ictx);
1367
1.86M
    return ret;
1368
1.86M
}
1369
1370
void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y, int *hover)
1371
83
{
1372
83
    input_lock(ictx);
1373
83
    *x = ictx->mouse_x;
1374
83
    *y = ictx->mouse_y;
1375
83
    *hover = ictx->mouse_hover;
1376
83
    input_unlock(ictx);
1377
83
}
1378
1379
// If name is NULL, return "default".
1380
// Return a statically allocated name of the section (i.e. return value never
1381
// gets deallocated).
1382
static bstr normalize_section(struct input_ctx *ictx, bstr name)
1383
319k
{
1384
319k
    return get_bind_section(ictx, name)->section;
1385
319k
}
1386
1387
static void disable_section(struct input_ctx *ictx, bstr name)
1388
159k
{
1389
159k
    name = normalize_section(ictx, name);
1390
1391
    // Remove old section, or make sure it's on top if re-enabled
1392
160k
    for (int i = ictx->num_active_sections - 1; i >= 0; i--) {
1393
403
        struct active_section *as = &ictx->active_sections[i];
1394
403
        if (bstr_equals(as->name, name)) {
1395
0
            MP_TARRAY_REMOVE_AT(ictx->active_sections,
1396
0
                                ictx->num_active_sections, i);
1397
0
        }
1398
403
    }
1399
159k
}
1400
1401
void mp_input_disable_section(struct input_ctx *ictx, char *name)
1402
0
{
1403
0
    input_lock(ictx);
1404
0
    disable_section(ictx, bstr0(name));
1405
0
    input_unlock(ictx);
1406
0
}
1407
1408
void mp_input_enable_section(struct input_ctx *ictx, char *name, int flags)
1409
159k
{
1410
159k
    bstr bname = bstr0(name);
1411
159k
    input_lock(ictx);
1412
159k
    bname = normalize_section(ictx, bname);
1413
1414
159k
    disable_section(ictx, bname);
1415
1416
159k
    MP_TRACE(ictx, "enable section '%.*s'\n", BSTR_P(bname));
1417
1418
159k
    int top = ictx->num_active_sections;
1419
159k
    if (!(flags & MP_INPUT_ON_TOP)) {
1420
        // insert before the first top entry
1421
160k
        for (top = 0; top < ictx->num_active_sections; top++) {
1422
403
            if (ictx->active_sections[top].flags & MP_INPUT_ON_TOP)
1423
0
                break;
1424
403
        }
1425
159k
    }
1426
159k
    MP_TARRAY_INSERT_AT(ictx, ictx->active_sections, ictx->num_active_sections,
1427
159k
                        top, (struct active_section){bname, flags});
1428
1429
159k
    MP_TRACE(ictx, "active section stack:\n");
1430
320k
    for (int n = 0; n < ictx->num_active_sections; n++) {
1431
160k
        MP_TRACE(ictx, " %.*s %d\n", BSTR_P(ictx->active_sections[n].name),
1432
160k
                 ictx->active_sections[n].flags);
1433
160k
    }
1434
1435
159k
    input_unlock(ictx);
1436
159k
}
1437
1438
void mp_input_disable_all_sections(struct input_ctx *ictx)
1439
0
{
1440
0
    input_lock(ictx);
1441
0
    ictx->num_active_sections = 0;
1442
0
    input_unlock(ictx);
1443
0
}
1444
1445
void mp_input_set_section_mouse_area(struct input_ctx *ictx, char *name,
1446
                                     int x0, int y0, int x1, int y1)
1447
0
{
1448
0
    input_lock(ictx);
1449
0
    struct cmd_bind_section *s = get_bind_section(ictx, bstr0(name));
1450
0
    s->mouse_area = (struct mp_rect){x0, y0, x1, y1};
1451
0
    s->mouse_area_set = x0 != x1 && y0 != y1;
1452
0
    input_unlock(ictx);
1453
0
}
1454
1455
static void bind_dealloc(struct cmd_bind *bind)
1456
27.6M
{
1457
27.6M
    talloc_free(bind->cmd);
1458
27.6M
    talloc_free(bind->location);
1459
27.6M
    talloc_free(bind->desc);
1460
27.6M
}
1461
1462
// builtin: if true, remove all builtin binds, else remove all user binds
1463
static void remove_binds(struct cmd_bind_section *bs, bool builtin)
1464
0
{
1465
0
    for (int n = bs->num_binds - 1; n >= 0; n--) {
1466
0
        if (bs->binds[n].is_builtin == builtin) {
1467
0
            bind_dealloc(&bs->binds[n]);
1468
0
            mp_assert(bs->num_binds >= 1);
1469
0
            bs->binds[n] = bs->binds[bs->num_binds - 1];
1470
0
            bs->num_binds--;
1471
0
        }
1472
0
    }
1473
0
}
1474
1475
void mp_input_define_section(struct input_ctx *ictx, char *name, char *location,
1476
                             char *contents, bool builtin, char *owner)
1477
0
{
1478
0
    bstr bname = bstr0(name);
1479
0
    if (!bname.len)
1480
0
        return; // parse_config() changes semantics with restrict_section==empty
1481
0
    input_lock(ictx);
1482
    // Delete:
1483
0
    struct cmd_bind_section *bs = get_bind_section(ictx, bname);
1484
0
    if ((!bs->owner || (owner && strcmp(bs->owner, owner) != 0)) &&
1485
0
        !bstr_equals0(bs->section, "default"))
1486
0
    {
1487
0
        talloc_replace(bs, bs->owner, owner);
1488
0
    }
1489
0
    remove_binds(bs, builtin);
1490
0
    if (contents && contents[0]) {
1491
        // Redefine:
1492
0
        parse_config(ictx, builtin, bstr0(contents), location, bname);
1493
0
    } else {
1494
        // Disable:
1495
0
        disable_section(ictx, bname);
1496
0
    }
1497
0
    input_unlock(ictx);
1498
0
}
1499
1500
void mp_input_remove_sections_by_owner(struct input_ctx *ictx, char *owner)
1501
163k
{
1502
163k
    input_lock(ictx);
1503
476k
    for (int n = 0; n < ictx->num_sections; n++) {
1504
312k
        struct cmd_bind_section *bs = ictx->sections[n];
1505
312k
        if (bs->owner && owner && strcmp(bs->owner, owner) == 0) {
1506
0
            disable_section(ictx, bs->section);
1507
0
            remove_binds(bs, false);
1508
0
            remove_binds(bs, true);
1509
0
        }
1510
312k
    }
1511
163k
    input_unlock(ictx);
1512
163k
}
1513
1514
static bool bind_matches_key(struct cmd_bind *bind, int num_keys, const int *keys)
1515
2.57G
{
1516
2.57G
    if (bind->num_keys != num_keys)
1517
376M
        return false;
1518
2.20G
    for (int i = 0; i < num_keys; i++) {
1519
2.20G
        if (bind->keys[i] != keys[i])
1520
2.19G
            return false;
1521
2.20G
    }
1522
214k
    return true;
1523
2.19G
}
1524
1525
static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
1526
                      const int *keys, int num_keys, bstr command,
1527
                      const char *loc, const char *desc)
1528
27.6M
{
1529
27.6M
    struct cmd_bind_section *bs = get_bind_section(ictx, section);
1530
27.6M
    struct cmd_bind *bind = NULL;
1531
1532
27.6M
    mp_assert(num_keys <= MP_MAX_KEY_DOWN);
1533
1534
2.59G
    for (int n = 0; n < bs->num_binds; n++) {
1535
2.57G
        struct cmd_bind *b = &bs->binds[n];
1536
2.57G
        if (bind_matches_key(b, num_keys, keys) && b->is_builtin == builtin) {
1537
144k
            bind = b;
1538
144k
            break;
1539
144k
        }
1540
2.57G
    }
1541
1542
27.6M
    if (!bind) {
1543
27.4M
        struct cmd_bind empty = {{0}};
1544
27.4M
        MP_TARRAY_APPEND(bs, bs->binds, bs->num_binds, empty);
1545
27.4M
        bind = &bs->binds[bs->num_binds - 1];
1546
27.4M
    }
1547
1548
27.6M
    bind_dealloc(bind);
1549
1550
27.6M
    *bind = (struct cmd_bind) {
1551
27.6M
        .cmd = bstrdup0(bs->binds, command),
1552
27.6M
        .location = talloc_strdup(bs->binds, loc),
1553
27.6M
        .desc = talloc_strdup(bs->binds, desc),
1554
27.6M
        .owner = bs,
1555
27.6M
        .is_builtin = builtin,
1556
27.6M
        .num_keys = num_keys,
1557
27.6M
    };
1558
27.6M
    memcpy(bind->keys, keys, num_keys * sizeof(bind->keys[0]));
1559
27.6M
    if (mp_msg_test(ictx->log, MSGL_DEBUG)) {
1560
27.6M
        char *s = mp_input_get_key_combo_name(keys, num_keys);
1561
27.6M
        MP_TRACE(ictx, "add: section='%.*s' key='%s'%s cmd='%s' location='%s'\n",
1562
27.6M
                 BSTR_P(bind->owner->section), s, bind->is_builtin ? " builtin" : "",
1563
27.6M
                 bind->cmd, bind->location);
1564
27.6M
        talloc_free(s);
1565
27.6M
    }
1566
27.6M
}
1567
1568
// restrict_section: every entry is forced to this section name
1569
//          if NULL, load normally and allow any sections
1570
static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
1571
                        const char *location, const bstr restrict_section)
1572
31.0M
{
1573
31.0M
    int n_binds = 0;
1574
31.0M
    int line_no = 0;
1575
31.0M
    char *cur_loc = NULL;
1576
1577
62.2M
    while (data.len) {
1578
31.2M
        line_no++;
1579
31.2M
        if (cur_loc)
1580
183k
            talloc_free(cur_loc);
1581
31.2M
        cur_loc = talloc_asprintf(NULL, "%s:%d", location, line_no);
1582
1583
31.2M
        bstr line = bstr_strip_linebreaks(bstr_getline(data, &data));
1584
31.2M
        line = bstr_lstrip(line);
1585
31.2M
        if (line.len == 0 || bstr_startswith0(line, "#"))
1586
3.44M
            continue;
1587
27.7M
        if (bstr_eatstart0(&line, "default-bindings ")) {
1588
143k
            bstr orig = line;
1589
143k
            bstr_split_tok(line, "#", &line, &(bstr){0});
1590
143k
            line = bstr_strip(line);
1591
143k
            if (bstr_equals0(line, "start")) {
1592
143k
                builtin = true;
1593
143k
            } else {
1594
234
                MP_ERR(ictx, "Broken line: %.*s at %s\n", BSTR_P(orig), cur_loc);
1595
234
            }
1596
143k
            continue;
1597
143k
        }
1598
27.6M
        struct bstr command;
1599
        // Find the key name starting a line
1600
27.6M
        struct bstr keyname = bstr_split(line, WHITESPACE, &command);
1601
27.6M
        command = bstr_strip(command);
1602
27.6M
        if (command.len == 0) {
1603
10.7k
            MP_ERR(ictx, "Unfinished key binding: %.*s at %s\n", BSTR_P(line),
1604
10.7k
                   cur_loc);
1605
10.7k
            continue;
1606
10.7k
        }
1607
27.6M
        char *name = bstrdup0(NULL, keyname);
1608
27.6M
        int keys[MP_MAX_KEY_DOWN];
1609
27.6M
        int num_keys = 0;
1610
27.6M
        if (!mp_input_get_keys_from_string(name, MP_MAX_KEY_DOWN, &num_keys, keys)) {
1611
10.8k
            talloc_free(name);
1612
10.8k
            MP_ERR(ictx, "Unknown key '%.*s' at %s\n", BSTR_P(keyname), cur_loc);
1613
10.8k
            continue;
1614
10.8k
        }
1615
27.6M
        talloc_free(name);
1616
1617
27.6M
        bstr section = restrict_section;
1618
27.6M
        if (!section.len) {
1619
27.6M
            if (bstr_startswith0(command, "{")) {
1620
435k
                int p = bstrchr(command, '}');
1621
435k
                if (p != -1) {
1622
432k
                    section = bstr_strip(bstr_splice(command, 1, p));
1623
432k
                    command = bstr_lstrip(bstr_cut(command, p + 1));
1624
432k
                }
1625
435k
            }
1626
27.6M
        }
1627
1628
        // Print warnings if invalid commands are encountered.
1629
27.6M
        struct mp_cmd *cmd = mp_input_parse_cmd(ictx, command, cur_loc);
1630
27.6M
        const char *desc = NULL;
1631
27.6M
        if (cmd) {
1632
27.4M
            desc = cmd->desc;
1633
27.4M
            command = bstr0(cmd->original);
1634
27.4M
        }
1635
1636
27.6M
        bind_keys(ictx, builtin, section, keys, num_keys, command, cur_loc, desc);
1637
27.6M
        n_binds++;
1638
1639
27.6M
        talloc_free(cmd);
1640
27.6M
    }
1641
1642
31.0M
    talloc_free(cur_loc);
1643
1644
31.0M
    return n_binds;
1645
31.0M
}
1646
1647
static bool parse_config_file(struct input_ctx *ictx, char *file)
1648
8.90k
{
1649
8.90k
    bool r = false;
1650
8.90k
    void *tmp = talloc_new(NULL);
1651
1652
8.90k
    file = mp_get_user_path(tmp, ictx->global, file);
1653
1654
8.90k
    bstr data = stream_read_file2(file, tmp, STREAM_ORIGIN_DIRECT | STREAM_READ,
1655
8.90k
                                  ictx->global, 1000000);
1656
8.90k
    if (data.start) {
1657
7.36k
        MP_VERBOSE(ictx, "Parsing input config file %s\n", file);
1658
7.36k
        bstr_eatstart0(&data, "\xEF\xBB\xBF"); // skip BOM
1659
7.36k
        int num = parse_config(ictx, false, data, file, (bstr){0});
1660
7.36k
        MP_VERBOSE(ictx, "Input config file %s parsed: %d binds\n", file, num);
1661
7.36k
        r = true;
1662
7.36k
    } else {
1663
1.54k
        MP_ERR(ictx, "Error reading input config file %s\n", file);
1664
1.54k
    }
1665
1666
8.90k
    talloc_free(tmp);
1667
8.90k
    return r;
1668
8.90k
}
1669
1670
struct input_ctx *mp_input_init(struct mpv_global *global,
1671
                                void (*wakeup_cb)(void *ctx),
1672
                                void *wakeup_ctx)
1673
159k
{
1674
1675
159k
    struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1676
159k
    *ictx = (struct input_ctx){
1677
159k
        .global = global,
1678
159k
        .ar_state = -1,
1679
159k
        .log = mp_log_new(ictx, global->log, "input"),
1680
159k
        .mouse_section = bstr0("default"),
1681
159k
        .opts_cache = m_config_cache_alloc(ictx, global, &input_config),
1682
159k
        .wakeup_cb = wakeup_cb,
1683
159k
        .wakeup_ctx = wakeup_ctx,
1684
159k
        .active_sections = talloc_array(ictx, struct active_section, 0),
1685
159k
        .touch_points = talloc_array(ictx, struct touch_point, 0),
1686
159k
    };
1687
1688
159k
    ictx->opts = ictx->opts_cache->opts;
1689
1690
159k
    mp_mutex_init(&ictx->mutex);
1691
1692
    // Setup default section, so that it does nothing.
1693
159k
    mp_input_enable_section(ictx, NULL, MP_INPUT_ALLOW_VO_DRAGGING |
1694
159k
                                        MP_INPUT_ALLOW_HIDE_CURSOR);
1695
1696
159k
    return ictx;
1697
159k
}
1698
1699
static void reload_opts(struct input_ctx *ictx, bool shutdown)
1700
445k
{
1701
445k
    m_config_cache_update(ictx->opts_cache);
1702
1703
#if HAVE_COCOA
1704
    struct input_opts *opts = ictx->opts;
1705
1706
    if (ictx->using_cocoa_media_keys != (opts->use_media_keys && !shutdown)) {
1707
        ictx->using_cocoa_media_keys = !ictx->using_cocoa_media_keys;
1708
        if (ictx->using_cocoa_media_keys) {
1709
            cocoa_init_media_keys();
1710
        } else {
1711
            cocoa_uninit_media_keys();
1712
        }
1713
    }
1714
#endif
1715
445k
}
1716
1717
void mp_input_update_opts(struct input_ctx *ictx)
1718
143k
{
1719
143k
    input_lock(ictx);
1720
143k
    reload_opts(ictx, false);
1721
143k
    input_unlock(ictx);
1722
143k
}
1723
1724
void mp_input_load_config(struct input_ctx *ictx)
1725
143k
{
1726
143k
    input_lock(ictx);
1727
1728
143k
    reload_opts(ictx, false);
1729
1730
    // "Uncomment" the default key bindings in etc/input.conf and add them.
1731
    // All lines that do not start with '# ' are parsed.
1732
143k
    bstr builtin = bstr0(builtin_input_conf);
1733
35.3M
    while (ictx->opts->builtin_bindings && builtin.len) {
1734
35.1M
        bstr line = bstr_getline(builtin, &builtin);
1735
35.1M
        bstr_eatstart0(&line, "#");
1736
35.1M
        if (!bstr_startswith0(line, " "))
1737
31.0M
            parse_config(ictx, true, line, "<builtin>", (bstr){0});
1738
35.1M
    }
1739
1740
143k
    bool config_ok = false;
1741
143k
    if (ictx->opts->config_file && ictx->opts->config_file[0])
1742
2.50k
        config_ok = parse_config_file(ictx, ictx->opts->config_file);
1743
143k
    if (!config_ok) {
1744
        // Try global conf dir
1745
142k
        void *tmp = talloc_new(NULL);
1746
142k
        char **files = mp_find_all_config_files(tmp, ictx->global, "input.conf");
1747
142k
        for (int n = 0; files && files[n]; n++)
1748
0
            parse_config_file(ictx, files[n]);
1749
142k
        talloc_free(tmp);
1750
142k
    }
1751
1752
143k
    bool use_gamepad = ictx->opts->use_gamepad;
1753
143k
    input_unlock(ictx);
1754
1755
#if HAVE_SDL2_GAMEPAD
1756
    if (use_gamepad)
1757
        mp_input_sdl_gamepad_add(ictx);
1758
#else
1759
143k
    (void)use_gamepad;
1760
143k
#endif
1761
143k
}
1762
1763
bool mp_input_load_config_file(struct input_ctx *ictx, char *file)
1764
6.39k
{
1765
6.39k
    input_lock(ictx);
1766
6.39k
    bool result = parse_config_file(ictx, file);
1767
6.39k
    input_unlock(ictx);
1768
6.39k
    return result;
1769
6.39k
}
1770
1771
static void clear_queue(struct cmd_queue *queue)
1772
159k
{
1773
159k
    while (queue->first) {
1774
0
        struct mp_cmd *item = queue->first;
1775
0
        queue_remove(queue, item);
1776
0
        talloc_free(item);
1777
0
    }
1778
159k
}
1779
1780
void mp_input_uninit(struct input_ctx *ictx)
1781
159k
{
1782
159k
    if (!ictx)
1783
0
        return;
1784
1785
159k
    input_lock(ictx);
1786
159k
    reload_opts(ictx, true);
1787
159k
    input_unlock(ictx);
1788
1789
159k
    close_input_sources(ictx);
1790
159k
    clear_queue(&ictx->cmd_queue);
1791
159k
    talloc_free(ictx->current_down_cmd);
1792
159k
    mp_mutex_destroy(&ictx->mutex);
1793
159k
    talloc_free(ictx);
1794
159k
}
1795
1796
bool mp_input_use_alt_gr(struct input_ctx *ictx)
1797
0
{
1798
0
    input_lock(ictx);
1799
0
    bool r = ictx->opts->use_alt_gr;
1800
0
    input_unlock(ictx);
1801
0
    return r;
1802
0
}
1803
1804
bool mp_input_use_media_keys(struct input_ctx *ictx)
1805
0
{
1806
0
    input_lock(ictx);
1807
0
    bool r = ictx->opts->use_media_keys;
1808
0
    input_unlock(ictx);
1809
0
    return r;
1810
0
}
1811
1812
struct mp_cmd *mp_input_parse_cmd(struct input_ctx *ictx, bstr str,
1813
                                  const char *location)
1814
27.6M
{
1815
27.6M
    return mp_input_parse_cmd_str(ictx->log, str, location);
1816
27.6M
}
1817
1818
void mp_input_run_cmd(struct input_ctx *ictx, const char **cmd)
1819
0
{
1820
0
    input_lock(ictx);
1821
0
    queue_cmd(ictx, mp_input_parse_cmd_strv(ictx->log, cmd));
1822
0
    input_unlock(ictx);
1823
0
}
1824
1825
bool mp_input_bind_key(struct input_ctx *ictx, const char *key, bstr command,
1826
                       const char *desc)
1827
0
{
1828
0
    char *name = talloc_strdup(NULL, key);
1829
0
    int keys[MP_MAX_KEY_DOWN];
1830
0
    int num_keys = 0;
1831
0
    if (!mp_input_get_keys_from_string(name, MP_MAX_KEY_DOWN, &num_keys, keys)) {
1832
0
        talloc_free(name);
1833
0
        return false;
1834
0
    }
1835
0
    talloc_free(name);
1836
1837
0
    input_lock(ictx);
1838
0
    bind_keys(ictx, false, (bstr){0}, keys, num_keys, command,
1839
0
              "keybind-command", desc);
1840
0
    input_unlock(ictx);
1841
0
    return true;
1842
0
}
1843
1844
struct mpv_node mp_input_get_bindings(struct input_ctx *ictx)
1845
36
{
1846
36
    input_lock(ictx);
1847
36
    struct mpv_node root;
1848
36
    node_init(&root, MPV_FORMAT_NODE_ARRAY, NULL);
1849
1850
72
    for (int x = 0; x < ictx->num_sections; x++) {
1851
36
        struct cmd_bind_section *s = ictx->sections[x];
1852
36
        int priority = -1;
1853
1854
36
        for (int i = 0; i < ictx->num_active_sections; i++) {
1855
36
            struct active_section *as = &ictx->active_sections[i];
1856
36
            if (bstr_equals(as->name, s->section)) {
1857
36
                priority = i;
1858
36
                break;
1859
36
            }
1860
36
        }
1861
1862
36
        for (int n = 0; n < s->num_binds; n++) {
1863
0
            struct cmd_bind *b = &s->binds[n];
1864
0
            struct mpv_node *entry = node_array_add(&root, MPV_FORMAT_NODE_MAP);
1865
1866
0
            int b_priority = priority;
1867
0
            if (b->is_builtin && !ictx->opts->default_bindings)
1868
0
                b_priority = -1;
1869
1870
            // Try to fixup the weird logic so consumer of this bindings list
1871
            // does not get too confused.
1872
0
            if (b_priority >= 0 && !b->is_builtin)
1873
0
                b_priority += ictx->num_active_sections;
1874
1875
0
            node_map_add_bstr(entry, "section", s->section);
1876
0
            if (s->owner)
1877
0
                node_map_add_string(entry, "owner", s->owner);
1878
0
            node_map_add_string(entry, "cmd", b->cmd);
1879
0
            node_map_add_flag(entry, "is_weak", b->is_builtin);
1880
0
            node_map_add_int64(entry, "priority", b_priority);
1881
0
            if (b->desc)
1882
0
                node_map_add_string(entry, "comment", b->desc);
1883
1884
0
            char *key = mp_input_get_key_combo_name(b->keys, b->num_keys);
1885
0
            node_map_add_string(entry, "key", key);
1886
0
            talloc_free(key);
1887
0
        }
1888
36
    }
1889
1890
36
    input_unlock(ictx);
1891
36
    return root;
1892
36
}
1893
1894
struct mp_input_src_internal {
1895
    mp_thread thread;
1896
    bool thread_running;
1897
    bool init_done;
1898
1899
    char *cmd_buffer;
1900
    size_t cmd_buffer_size;
1901
    bool drop;
1902
};
1903
1904
static struct mp_input_src *input_add_src(struct input_ctx *ictx)
1905
0
{
1906
0
    input_lock(ictx);
1907
0
    if (ictx->num_sources == MP_MAX_SOURCES) {
1908
0
        input_unlock(ictx);
1909
0
        return NULL;
1910
0
    }
1911
1912
0
    char name[80];
1913
0
    snprintf(name, sizeof(name), "#%d", ictx->num_sources + 1);
1914
0
    struct mp_input_src *src = talloc_ptrtype(NULL, src);
1915
0
    *src = (struct mp_input_src){
1916
0
        .global = ictx->global,
1917
0
        .log = mp_log_new(src, ictx->log, name),
1918
0
        .input_ctx = ictx,
1919
0
        .in = talloc_zero(src, struct mp_input_src_internal),
1920
0
    };
1921
1922
0
    ictx->sources[ictx->num_sources++] = src;
1923
1924
0
    input_unlock(ictx);
1925
0
    return src;
1926
0
}
1927
1928
static void input_src_kill(struct mp_input_src *src);
1929
1930
static void close_input_sources(struct input_ctx *ictx)
1931
159k
{
1932
    // To avoid lock-order issues, we first remove each source from the context,
1933
    // and then destroy it.
1934
159k
    while (1) {
1935
159k
        input_lock(ictx);
1936
159k
        struct mp_input_src *src = ictx->num_sources ? ictx->sources[0] : NULL;
1937
159k
        input_unlock(ictx);
1938
159k
        if (!src)
1939
159k
            break;
1940
0
        input_src_kill(src);
1941
0
    }
1942
159k
}
1943
1944
static void input_src_kill(struct mp_input_src *src)
1945
0
{
1946
0
    if (!src)
1947
0
        return;
1948
0
    struct input_ctx *ictx = src->input_ctx;
1949
0
    input_lock(ictx);
1950
0
    for (int n = 0; n < ictx->num_sources; n++) {
1951
0
        if (ictx->sources[n] == src) {
1952
0
            MP_TARRAY_REMOVE_AT(ictx->sources, ictx->num_sources, n);
1953
0
            input_unlock(ictx);
1954
0
            if (src->cancel)
1955
0
                src->cancel(src);
1956
0
            if (src->in->thread_running)
1957
0
                mp_thread_join(src->in->thread);
1958
0
            if (src->uninit)
1959
0
                src->uninit(src);
1960
0
            talloc_free(src);
1961
0
            return;
1962
0
        }
1963
0
    }
1964
0
    MP_ASSERT_UNREACHABLE();
1965
0
}
1966
1967
void mp_input_src_init_done(struct mp_input_src *src)
1968
0
{
1969
0
    mp_assert(!src->in->init_done);
1970
0
    mp_assert(src->in->thread_running);
1971
0
    mp_assert(mp_thread_id_equal(mp_thread_get_id(src->in->thread), mp_thread_current_id()));
1972
0
    src->in->init_done = true;
1973
0
    mp_rendezvous(&src->in->init_done, 0);
1974
0
}
1975
1976
static MP_THREAD_VOID input_src_thread(void *ptr)
1977
0
{
1978
0
    void **args = ptr;
1979
0
    struct mp_input_src *src = args[0];
1980
0
    void (*loop_fn)(struct mp_input_src *src, void *ctx) = args[1];
1981
0
    void *ctx = args[2];
1982
1983
0
    mp_thread_set_name("input");
1984
1985
0
    src->in->thread_running = true;
1986
1987
0
    loop_fn(src, ctx);
1988
1989
0
    if (!src->in->init_done)
1990
0
        mp_rendezvous(&src->in->init_done, -1);
1991
1992
0
    MP_THREAD_RETURN();
1993
0
}
1994
1995
int mp_input_add_thread_src(struct input_ctx *ictx, void *ctx,
1996
    void (*loop_fn)(struct mp_input_src *src, void *ctx))
1997
0
{
1998
0
    struct mp_input_src *src = input_add_src(ictx);
1999
0
    if (!src)
2000
0
        return -1;
2001
2002
0
    void *args[] = {src, loop_fn, ctx};
2003
0
    if (mp_thread_create(&src->in->thread, input_src_thread, args)) {
2004
0
        input_src_kill(src);
2005
0
        return -1;
2006
0
    }
2007
0
    if (mp_rendezvous(&src->in->init_done, 0) < 0) {
2008
0
        input_src_kill(src);
2009
0
        return -1;
2010
0
    }
2011
0
    return 0;
2012
0
}
2013
2014
0
#define CMD_BUFFER (4 * 4096)
2015
2016
void mp_input_src_feed_cmd_text(struct mp_input_src *src, char *buf, size_t len)
2017
0
{
2018
0
    struct mp_input_src_internal *in = src->in;
2019
0
    if (!in->cmd_buffer)
2020
0
        in->cmd_buffer = talloc_size(in, CMD_BUFFER);
2021
0
    while (len) {
2022
0
        char *next = memchr(buf, '\n', len);
2023
0
        bool term = !!next;
2024
0
        next = next ? next + 1 : buf + len;
2025
0
        size_t copy = next - buf;
2026
0
        bool overflow = copy > CMD_BUFFER - in->cmd_buffer_size;
2027
0
        if (overflow || in->drop) {
2028
0
            in->cmd_buffer_size = 0;
2029
0
            in->drop = overflow || !term;
2030
0
            MP_WARN(src, "Dropping overlong line.\n");
2031
0
        } else {
2032
0
            memcpy(in->cmd_buffer + in->cmd_buffer_size, buf, copy);
2033
0
            in->cmd_buffer_size += copy;
2034
0
            buf += copy;
2035
0
            len -= copy;
2036
0
            if (term) {
2037
0
                bstr s = {in->cmd_buffer, in->cmd_buffer_size};
2038
0
                s = bstr_strip(s);
2039
0
                struct mp_cmd *cmd = mp_input_parse_cmd_str(src->log, s, "<>");
2040
0
                if (cmd) {
2041
0
                    input_lock(src->input_ctx);
2042
0
                    queue_cmd(src->input_ctx, cmd);
2043
0
                    input_unlock(src->input_ctx);
2044
0
                }
2045
0
                in->cmd_buffer_size = 0;
2046
0
            }
2047
0
        }
2048
0
    }
2049
0
}
2050
2051
void mp_input_set_repeat_info(struct input_ctx *ictx, int rate, int delay)
2052
0
{
2053
0
    input_lock(ictx);
2054
0
    ictx->opts->ar_rate = rate;
2055
0
    ictx->opts->ar_delay = delay;
2056
0
    input_unlock(ictx);
2057
0
}