diff options
Diffstat (limited to 'input/input.c')
-rw-r--r-- | input/input.c | 182 |
1 files changed, 99 insertions, 83 deletions
diff --git a/input/input.c b/input/input.c index 1d0c60569c..9fd2a16f0c 100644 --- a/input/input.c +++ b/input/input.c @@ -28,7 +28,6 @@ #include <sys/stat.h> #include <sys/time.h> #include <fcntl.h> -#include <pthread.h> #include <assert.h> #include "osdep/io.h" @@ -51,13 +50,13 @@ #include "common/common.h" #if HAVE_COCOA -#include "osdep/macosx_events.h" +#include "osdep/mac/app_bridge.h" #endif -#define input_lock(ictx) pthread_mutex_lock(&ictx->mutex) -#define input_unlock(ictx) pthread_mutex_unlock(&ictx->mutex) +#define input_lock(ictx) mp_mutex_lock(&ictx->mutex) +#define input_unlock(ictx) mp_mutex_unlock(&ictx->mutex) -#define MP_MAX_KEY_DOWN 4 +#define MP_MAX_KEY_DOWN 16 struct cmd_bind { int keys[MP_MAX_KEY_DOWN]; @@ -80,8 +79,6 @@ struct cmd_bind_section { #define MP_MAX_SOURCES 10 -#define MAX_ACTIVE_SECTIONS 50 - struct active_section { char *name; int flags; @@ -97,7 +94,7 @@ struct wheel_state { }; struct input_ctx { - pthread_mutex_t mutex; + mp_mutex mutex; struct mp_log *log; struct mpv_global *global; struct m_config_cache *opts_cache; @@ -122,6 +119,7 @@ struct input_ctx { // Mouse position on the consumer side (as command.c sees it) int mouse_x, mouse_y; + int mouse_hover; // updated on mouse-enter/leave char *mouse_section; // last section to receive mouse event // Mouse position on the producer side (as the VO sees it) @@ -142,7 +140,7 @@ struct input_ctx { int num_sections; // List currently active command sections - struct active_section active_sections[MAX_ACTIVE_SECTIONS]; + struct active_section *active_sections; int num_active_sections; unsigned int mouse_event_counter; @@ -170,15 +168,16 @@ struct input_opts { // 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; + bool use_alt_gr; + bool use_gamepad; + bool use_media_keys; + bool default_bindings; + bool builtin_bindings; + bool enable_mouse_movements; + bool vo_key_input; + bool test; + bool allow_win_drag; + bool preprocess_wheel; }; const struct m_sub_options input_config = { @@ -188,23 +187,21 @@ const struct m_sub_options input_config = { {"input-ar-rate", OPT_INT(ar_rate)}, {"input-keylist", OPT_PRINT(mp_print_key_list)}, {"input-cmdlist", OPT_PRINT(mp_print_cmd_list)}, - {"input-default-bindings", OPT_FLAG(default_bindings)}, - {"input-test", OPT_FLAG(test)}, + {"input-default-bindings", OPT_BOOL(default_bindings)}, + {"input-builtin-bindings", OPT_BOOL(builtin_bindings)}, + {"input-test", OPT_BOOL(test)}, {"input-doubleclick-time", OPT_INT(doubleclick_time), M_RANGE(0, 1000)}, - {"input-right-alt-gr", OPT_FLAG(use_alt_gr)}, + {"input-right-alt-gr", OPT_BOOL(use_alt_gr)}, {"input-key-fifo-size", OPT_INT(key_fifo_size), M_RANGE(2, 65000)}, - {"input-cursor", OPT_FLAG(enable_mouse_movements)}, - {"input-vo-keyboard", OPT_FLAG(vo_key_input)}, - {"input-media-keys", OPT_FLAG(use_media_keys)}, + {"input-cursor", OPT_BOOL(enable_mouse_movements)}, + {"input-vo-keyboard", OPT_BOOL(vo_key_input)}, + {"input-media-keys", OPT_BOOL(use_media_keys)}, + {"input-preprocess-wheel", OPT_BOOL(preprocess_wheel)}, #if HAVE_SDL2_GAMEPAD - {"input-gamepad", OPT_FLAG(use_gamepad)}, -#endif - {"window-dragging", OPT_FLAG(allow_win_drag)}, - {"input-x11-keyboard", OPT_REPLACED("input-vo-keyboard")}, -#if HAVE_COCOA - {"input-appleremote", OPT_REMOVED("replaced by MediaPlayer support")}, + {"input-gamepad", OPT_BOOL(use_gamepad)}, #endif + {"window-dragging", OPT_BOOL(allow_win_drag)}, {0} }, .size = sizeof(struct input_opts), @@ -213,18 +210,20 @@ const struct m_sub_options input_config = { .doubleclick_time = 300, .ar_delay = 200, .ar_rate = 40, - .use_alt_gr = 1, - .enable_mouse_movements = 1, - .use_media_keys = 1, - .default_bindings = 1, - .vo_key_input = 1, - .allow_win_drag = 1, + .use_alt_gr = true, + .enable_mouse_movements = true, + .use_media_keys = true, + .default_bindings = true, + .builtin_bindings = true, + .vo_key_input = true, + .allow_win_drag = true, + .preprocess_wheel = true, }, .change_flags = UPDATE_INPUT, }; static const char builtin_input_conf[] = -#include "generated/etc/input.conf.inc" +#include "etc/input.conf.inc" ; static bool test_rect(struct mp_rect *rc, int x, int y) @@ -591,7 +590,7 @@ static void interpret_key(struct input_ctx *ictx, int code, double scale, ictx->current_down_cmd = mp_cmd_clone(cmd); } ictx->last_key_down = code; - ictx->last_key_down_time = mp_time_us(); + ictx->last_key_down_time = mp_time_ns(); ictx->ar_state = 0; mp_input_wakeup(ictx); // possibly start timer for autorepeat } else if (state == MP_KEY_STATE_UP) { @@ -719,8 +718,13 @@ static void mp_input_feed_key(struct input_ctx *ictx, int code, double scale, if (!opts->enable_mouse_movements && MP_KEY_IS_MOUSE(unmod) && !force_mouse) return; if (unmod == MP_KEY_MOUSE_LEAVE || unmod == MP_KEY_MOUSE_ENTER) { + ictx->mouse_hover = unmod == MP_KEY_MOUSE_ENTER; update_mouse_section(ictx); - mp_input_queue_cmd(ictx, get_cmd_from_keys(ictx, NULL, code)); + + mp_cmd_t *cmd = get_cmd_from_keys(ictx, NULL, code); + if (!cmd) // queue dummy cmd so that mouse-pos can notify observers + cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>"); + mp_input_queue_cmd(ictx, cmd); return; } double now = mp_time_sec(); @@ -728,18 +732,23 @@ static void mp_input_feed_key(struct input_ctx *ictx, int code, double scale, 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)) + if (MP_KEY_IS_WHEEL(unmod) && opts->preprocess_wheel && !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) + now - ictx->last_doubleclick_time < opts->doubleclick_time / 1000.0 && + code >= MP_MBTN_LEFT && code <= MP_MBTN_RIGHT) { - if (code >= MP_MBTN_LEFT && code <= MP_MBTN_RIGHT) { - interpret_key(ictx, code - MP_MBTN_BASE + MP_MBTN_DBL_BASE, - 1, 1); - } + now = 0; + interpret_key(ictx, code - MP_MBTN_BASE + MP_MBTN_DBL_BASE, + 1, 1); + } else if (code == MP_MBTN_LEFT) { + // This is a mouse left botton down event which isn't part of a doubleclick. + // Initialize vo dragging in this case. + mp_cmd_t *cmd = mp_input_parse_cmd(ictx, bstr0("begin-vo-dragging"), "<internal>"); + mp_input_queue_cmd(ictx, cmd); } ictx->last_doubleclick_key_down = code; ictx->last_doubleclick_time = now; @@ -753,10 +762,12 @@ void mp_input_put_key(struct input_ctx *ictx, int code) input_unlock(ictx); } -void mp_input_put_key_artificial(struct input_ctx *ictx, int code) +void mp_input_put_key_artificial(struct input_ctx *ictx, int code, double value) { + if (value == 0.0) + return; input_lock(ictx); - mp_input_feed_key(ictx, code, 1, true); + mp_input_feed_key(ictx, code, value, true); input_unlock(ictx); } @@ -911,19 +922,19 @@ static mp_cmd_t *check_autorepeat(struct input_ctx *ictx) ictx->ar_state = -1; // disable if (ictx->ar_state >= 0) { - int64_t t = mp_time_us(); - if (ictx->last_ar + 2000000 < t) + int64_t t = mp_time_ns(); + if (ictx->last_ar + MP_TIME_S_TO_NS(2) < t) ictx->last_ar = t; // First time : wait delay if (ictx->ar_state == 0 - && (t - ictx->last_key_down_time) >= opts->ar_delay * 1000) + && (t - ictx->last_key_down_time) >= MP_TIME_MS_TO_NS(opts->ar_delay)) { ictx->ar_state = 1; - ictx->last_ar = ictx->last_key_down_time + opts->ar_delay * 1000; + ictx->last_ar = ictx->last_key_down_time + MP_TIME_MS_TO_NS(opts->ar_delay); // 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; + && (t - ictx->last_ar) >= 1e9 / opts->ar_rate) { + ictx->last_ar += 1e9 / opts->ar_rate; } else { return NULL; } @@ -962,11 +973,12 @@ mp_cmd_t *mp_input_read_cmd(struct input_ctx *ictx) return ret; } -void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y) +void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y, int *hover) { input_lock(ictx); *x = ictx->mouse_x; *y = ictx->mouse_y; + *hover = ictx->mouse_hover; input_unlock(ictx); } @@ -1003,20 +1015,16 @@ void mp_input_enable_section(struct input_ctx *ictx, char *name, int flags) MP_TRACE(ictx, "enable section '%s'\n", name); - if (ictx->num_active_sections < MAX_ACTIVE_SECTIONS) { - int top = ictx->num_active_sections; - if (!(flags & MP_INPUT_ON_TOP)) { - // insert before the first top entry - for (top = 0; top < ictx->num_active_sections; top++) { - if (ictx->active_sections[top].flags & MP_INPUT_ON_TOP) - break; - } - for (int n = ictx->num_active_sections; n > top; n--) - ictx->active_sections[n] = ictx->active_sections[n - 1]; + int top = ictx->num_active_sections; + if (!(flags & MP_INPUT_ON_TOP)) { + // insert before the first top entry + for (top = 0; top < ictx->num_active_sections; top++) { + if (ictx->active_sections[top].flags & MP_INPUT_ON_TOP) + break; } - ictx->active_sections[top] = (struct active_section){name, flags}; - ictx->num_active_sections++; } + MP_TARRAY_INSERT_AT(ictx, ictx->active_sections, ictx->num_active_sections, + top, (struct active_section){name, flags}); MP_TRACE(ictx, "active section stack:\n"); for (int n = 0; n < ictx->num_active_sections; n++) { @@ -1269,9 +1277,9 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data, return n_binds; } -static int parse_config_file(struct input_ctx *ictx, char *file, bool warn) +static bool parse_config_file(struct input_ctx *ictx, char *file) { - int r = 0; + bool r = false; void *tmp = talloc_new(NULL); stream_t *s = NULL; @@ -1288,7 +1296,7 @@ static int parse_config_file(struct input_ctx *ictx, char *file, bool warn) MP_VERBOSE(ictx, "Parsing input config file %s\n", file); int num = parse_config(ictx, false, data, file, NULL); MP_VERBOSE(ictx, "Input config file %s parsed: %d binds\n", file, num); - r = 1; + r = true; } else { MP_ERR(ictx, "Error reading input config file %s\n", file); } @@ -1313,11 +1321,12 @@ struct input_ctx *mp_input_init(struct mpv_global *global, .opts_cache = m_config_cache_alloc(ictx, global, &input_config), .wakeup_cb = wakeup_cb, .wakeup_ctx = wakeup_ctx, + .active_sections = talloc_array(ictx, struct active_section, 0), }; ictx->opts = ictx->opts_cache->opts; - mpthread_mutex_init_recursive(&ictx->mutex); + mp_mutex_init_type(&ictx->mutex, MP_MUTEX_RECURSIVE); // Setup default section, so that it does nothing. mp_input_enable_section(ictx, NULL, MP_INPUT_ALLOW_VO_DRAGGING | @@ -1360,7 +1369,7 @@ void mp_input_load_config(struct input_ctx *ictx) // "Uncomment" the default key bindings in etc/input.conf and add them. // All lines that do not start with '# ' are parsed. bstr builtin = bstr0(builtin_input_conf); - while (builtin.len) { + while (ictx->opts->builtin_bindings && builtin.len) { bstr line = bstr_getline(builtin, &builtin); bstr_eatstart0(&line, "#"); if (!bstr_startswith0(line, " ")) @@ -1369,13 +1378,13 @@ void mp_input_load_config(struct input_ctx *ictx) bool config_ok = false; if (ictx->opts->config_file && ictx->opts->config_file[0]) - config_ok = parse_config_file(ictx, ictx->opts->config_file, true); + config_ok = parse_config_file(ictx, ictx->opts->config_file); if (!config_ok) { // Try global conf dir void *tmp = talloc_new(NULL); char **files = mp_find_all_config_files(tmp, ictx->global, "input.conf"); for (int n = 0; files && files[n]; n++) - parse_config_file(ictx, files[n], false); + parse_config_file(ictx, files[n]); talloc_free(tmp); } @@ -1388,6 +1397,14 @@ void mp_input_load_config(struct input_ctx *ictx) input_unlock(ictx); } +bool mp_input_load_config_file(struct input_ctx *ictx, char *file) +{ + input_lock(ictx); + bool result = parse_config_file(ictx, file); + input_unlock(ictx); + return result; +} + static void clear_queue(struct cmd_queue *queue) { while (queue->first) { @@ -1409,7 +1426,7 @@ void mp_input_uninit(struct input_ctx *ictx) close_input_sources(ictx); clear_queue(&ictx->cmd_queue); talloc_free(ictx->current_down_cmd); - pthread_mutex_destroy(&ictx->mutex); + mp_mutex_destroy(&ictx->mutex); talloc_free(ictx); } @@ -1529,7 +1546,7 @@ struct mpv_node mp_input_get_bindings(struct input_ctx *ictx) } struct mp_input_src_internal { - pthread_t thread; + mp_thread thread; bool thread_running; bool init_done; @@ -1591,33 +1608,33 @@ static void mp_input_src_kill(struct mp_input_src *src) if (src->cancel) src->cancel(src); if (src->in->thread_running) - pthread_join(src->in->thread, NULL); + mp_thread_join(src->in->thread); if (src->uninit) src->uninit(src); talloc_free(src); return; } } - abort(); + MP_ASSERT_UNREACHABLE(); } void mp_input_src_init_done(struct mp_input_src *src) { assert(!src->in->init_done); assert(src->in->thread_running); - assert(pthread_equal(src->in->thread, pthread_self())); + assert(mp_thread_id_equal(mp_thread_get_id(src->in->thread), mp_thread_current_id())); src->in->init_done = true; mp_rendezvous(&src->in->init_done, 0); } -static void *input_src_thread(void *ptr) +static MP_THREAD_VOID input_src_thread(void *ptr) { void **args = ptr; struct mp_input_src *src = args[0]; void (*loop_fn)(struct mp_input_src *src, void *ctx) = args[1]; void *ctx = args[2]; - mpthread_set_name("input source"); + mp_thread_set_name("input"); src->in->thread_running = true; @@ -1626,7 +1643,7 @@ static void *input_src_thread(void *ptr) if (!src->in->init_done) mp_rendezvous(&src->in->init_done, -1); - return NULL; + MP_THREAD_RETURN(); } int mp_input_add_thread_src(struct input_ctx *ictx, void *ctx, @@ -1637,7 +1654,7 @@ int mp_input_add_thread_src(struct input_ctx *ictx, void *ctx, return -1; void *args[] = {src, loop_fn, ctx}; - if (pthread_create(&src->in->thread, NULL, input_src_thread, args)) { + if (mp_thread_create(&src->in->thread, input_src_thread, args)) { mp_input_src_kill(src); return -1; } @@ -1689,4 +1706,3 @@ void mp_input_set_repeat_info(struct input_ctx *ictx, int rate, int delay) ictx->opts->ar_delay = delay; input_unlock(ictx); } - |