History

command: add read-only focused property

Add a property that returns whether the window is focused, currently only for X11 and Wayland. My use cause for this is having an equivalent of pause-when-minimize.lua for tiling window managers: make mpv play only while it's in the current workspace or is focused (I'm fine with either one but prefer focus). On X I do this by observing display-names, which is empty when the rectangles of the display and mpv don't intersect, but on Wayland its value doesn't change when mpv leaves the current workspace (and the same check doesn't work since the geometries still intersect). This could later be made writable as requested in #6252. Note that on Wayland se shouldn't consider an unactivated window with keyboard input focused. The wlroots compositors I tested set activated after changing the keyboard focus, so if you set wl->focused only in keyboard_handle_enter() and keyboard_handle_leave() to avoid adding the "has_keyboard_input" member, focused isn't set to true when first opening mpv until you focus another window and focus mpv again. Conversely, if that order can't be assumed for all compositors, we should toggle wl->focused when necessary in keyboard_handle_enter() and keyboard_handle_leave() as well as in handle_toplevel_config().

Guido Cella committed 6 years ago

Commit 9b9ce741 parent 5a4fc868 files · +56 −1Browse files

Unified · Split

8 files
  1. DOCS/man/input.rst +3 −0
  2. player/command.c +17 −0
  3. player/command.h +1 −0
  4. player/playloop.c +2 −0
  5. video/out/vo.h +4 −1
  6. video/out/wayland_common.c +20 −0
  7. video/out/wayland_common.h +3 −0
  8. video/out/x11_common.c +6 −0
DOCS/man/input.rst+3−0
@@ -2439,6 +2439,9 @@ Property list · 2438 unmodified lines
2439 since setting the option, and the window size was not restricted in other2439 since setting the option, and the window size was not restricted in other
2440 ways. The property is unavailable if no video is active.2440 ways. The property is unavailable if no video is active.
2441 2441
2442``focused``
2443 Whether the window has focus. Currently works only on X11 and Wayland.
2444
2442``display-names``2445``display-names``
2443 Names of the displays that the mpv window covers. On X11, these2446 Names of the displays that the mpv window covers. On X11, these
2444 are the xrandr names (LVDS1, HDMI1, DP1, VGA1, etc.). On Windows, these2447 are the xrandr names (LVDS1, HDMI1, DP1, VGA1, etc.). On Windows, these
player/command.c+17−0
@@ -2410,6 +2410,21 @@ static int mp_property_hidpi_scale(void *ctx, struct m_property *prop, · 2409 unmodified lines
2410 return m_property_double_ro(action, arg, cmd->cached_window_scale);2410 return m_property_double_ro(action, arg, cmd->cached_window_scale);
2411}2411}
2412 2412
2413static int mp_property_focused(void *ctx, struct m_property *prop,
2414 int action, void *arg)
2415{
2416 MPContext *mpctx = ctx;
2417 struct vo *vo = mpctx->video_out;
2418 if (!vo)
2419 return M_PROPERTY_UNAVAILABLE;
2420
2421 bool focused;
2422 if (vo_control(vo, VOCTRL_GET_FOCUSED, &focused) < 1)
2423 return M_PROPERTY_UNAVAILABLE;
2424
2425 return m_property_flag_ro(action, arg, focused);
2426}
2427
2413static int mp_property_display_names(void *ctx, struct m_property *prop,2428static int mp_property_display_names(void *ctx, struct m_property *prop,
2414 int action, void *arg)2429 int action, void *arg)
2415{2430{
@@ -3593,6 +3608,7 @@ static const struct m_property mp_properties_base[] = { · 1177 unmodified lines
3593 PROPERTY_BITRATE("audio-bitrate", false, STREAM_AUDIO),3608 PROPERTY_BITRATE("audio-bitrate", false, STREAM_AUDIO),
3594 PROPERTY_BITRATE("sub-bitrate", false, STREAM_SUB),3609 PROPERTY_BITRATE("sub-bitrate", false, STREAM_SUB),
3595 3610
3611 {"focused", mp_property_focused},
3596 {"display-names", mp_property_display_names},3612 {"display-names", mp_property_display_names},
3597 {"display-fps", mp_property_display_fps},3613 {"display-fps", mp_property_display_fps},
3598 {"estimated-display-fps", mp_property_estimated_display_fps},3614 {"estimated-display-fps", mp_property_estimated_display_fps},
@@ -3676,6 +3692,7 @@ static const char *const *const mp_event_property_change[] = { · 77 unmodified lines
3676 "osd-par", "osd-dimensions"),3692 "osd-par", "osd-dimensions"),
3677 E(MP_EVENT_WIN_STATE, "display-names", "display-fps"),3693 E(MP_EVENT_WIN_STATE, "display-names", "display-fps"),
3678 E(MP_EVENT_WIN_STATE2, "display-hidpi-scale"),3694 E(MP_EVENT_WIN_STATE2, "display-hidpi-scale"),
3695 E(MP_EVENT_FOCUS, "focused"),
3679 E(MP_EVENT_CHANGE_PLAYLIST, "playlist", "playlist-pos", "playlist-pos-1",3696 E(MP_EVENT_CHANGE_PLAYLIST, "playlist", "playlist-pos", "playlist-pos-1",
3680 "playlist-count", "playlist/count", "playlist-current-pos",3697 "playlist-count", "playlist/count", "playlist-current-pos",
3681 "playlist-playing-pos"),3698 "playlist-playing-pos"),
player/command.h+1−0
@@ -98,6 +98,7 @@ enum { · 97 unmodified lines
98 MP_EVENT_WIN_RESIZE,98 MP_EVENT_WIN_RESIZE,
99 MP_EVENT_WIN_STATE,99 MP_EVENT_WIN_STATE,
100 MP_EVENT_WIN_STATE2,100 MP_EVENT_WIN_STATE2,
101 MP_EVENT_FOCUS,
101 MP_EVENT_CHANGE_PLAYLIST,102 MP_EVENT_CHANGE_PLAYLIST,
102 MP_EVENT_CORE_IDLE,103 MP_EVENT_CORE_IDLE,
103 MP_EVENT_DURATION_UPDATE,104 MP_EVENT_DURATION_UPDATE,
player/playloop.c+2−0
@@ -844,6 +844,8 @@ static void handle_vo_events(struct MPContext *mpctx) · 843 unmodified lines
844 mp_notify(mpctx, MP_EVENT_WIN_STATE, NULL);844 mp_notify(mpctx, MP_EVENT_WIN_STATE, NULL);
845 if (events & VO_EVENT_DPI)845 if (events & VO_EVENT_DPI)
846 mp_notify(mpctx, MP_EVENT_WIN_STATE2, NULL);846 mp_notify(mpctx, MP_EVENT_WIN_STATE2, NULL);
847 if (events & VO_EVENT_FOCUS)
848 mp_notify(mpctx, MP_EVENT_FOCUS, NULL);
847}849}
848 850
849static void handle_sstep(struct MPContext *mpctx)851static void handle_sstep(struct MPContext *mpctx)
video/out/vo.h+4−1
@@ -48,10 +48,11 @@ enum { · 47 unmodified lines
48 // Special thing for encode mode (vo_driver.initially_blocked).48 // Special thing for encode mode (vo_driver.initially_blocked).
49 // Part of VO_EVENTS_USER to make vo_is_ready_for_frame() work properly.49 // Part of VO_EVENTS_USER to make vo_is_ready_for_frame() work properly.
50 VO_EVENT_INITIAL_UNBLOCK = 1 << 7,50 VO_EVENT_INITIAL_UNBLOCK = 1 << 7,
51 VO_EVENT_FOCUS = 1 << 8,
51 52
52 // Set of events the player core may be interested in.53 // Set of events the player core may be interested in.
53 VO_EVENTS_USER = VO_EVENT_RESIZE | VO_EVENT_WIN_STATE | VO_EVENT_DPI |54 VO_EVENTS_USER = VO_EVENT_RESIZE | VO_EVENT_WIN_STATE | VO_EVENT_DPI |
54 VO_EVENT_INITIAL_UNBLOCK,55 VO_EVENT_INITIAL_UNBLOCK | VO_EVENT_FOCUS,
55};56};
56 57
57enum mp_voctrl {58enum mp_voctrl {
@@ -99,6 +100,8 @@ enum mp_voctrl { · 41 unmodified lines
99 VOCTRL_GET_UNFS_WINDOW_SIZE, // int[2] (w/h)100 VOCTRL_GET_UNFS_WINDOW_SIZE, // int[2] (w/h)
100 VOCTRL_SET_UNFS_WINDOW_SIZE, // int[2] (w/h)101 VOCTRL_SET_UNFS_WINDOW_SIZE, // int[2] (w/h)
101 102
103 VOCTRL_GET_FOCUSED, // bool*
104
102 // char *** (NULL terminated array compatible with CONF_TYPE_STRING_LIST)105 // char *** (NULL terminated array compatible with CONF_TYPE_STRING_LIST)
103 // names for displays the window is on106 // names for displays the window is on
104 VOCTRL_GET_DISPLAY_NAMES,107 VOCTRL_GET_DISPLAY_NAMES,
video/out/wayland_common.c+20−0
@@ -447,11 +447,15 @@ static void keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, · 446 unmodified lines
447 uint32_t serial, struct wl_surface *surface,447 uint32_t serial, struct wl_surface *surface,
448 struct wl_array *keys)448 struct wl_array *keys)
449{449{
450 struct vo_wayland_state *wl = data;
451 wl->has_keyboard_input = true;
450}452}
451 453
452static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,454static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
453 uint32_t serial, struct wl_surface *surface)455 uint32_t serial, struct wl_surface *surface)
454{456{
457 struct vo_wayland_state *wl = data;
458 wl->has_keyboard_input = false;
455}459}
456 460
457static bool create_input(struct vo_wayland_state *wl)461static bool create_input(struct vo_wayland_state *wl)
@@ -951,6 +955,7 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel, · 493 unmodified lines
951 955
952 bool is_maximized = false;956 bool is_maximized = false;
953 bool is_fullscreen = false;957 bool is_fullscreen = false;
958 bool is_activated = false;
954 enum xdg_toplevel_state *state;959 enum xdg_toplevel_state *state;
955 wl_array_for_each(state, states) {960 wl_array_for_each(state, states) {
956 switch (*state) {961 switch (*state) {
@@ -961,6 +966,7 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel, · 4 unmodified lines
961 wl->pending_vo_events |= VO_EVENT_LIVE_RESIZING;966 wl->pending_vo_events |= VO_EVENT_LIVE_RESIZING;
962 break;967 break;
963 case XDG_TOPLEVEL_STATE_ACTIVATED:968 case XDG_TOPLEVEL_STATE_ACTIVATED:
969 is_activated = true;
964 /*970 /*
965 * If we get an ACTIVATED state, we know it cannot be971 * If we get an ACTIVATED state, we know it cannot be
966 * minimized, but it may not have been minimized972 * minimized, but it may not have been minimized
@@ -992,6 +998,16 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel, · 25 unmodified lines
992 m_config_cache_write_opt(wl->vo_opts_cache, &vo_opts->window_maximized);998 m_config_cache_write_opt(wl->vo_opts_cache, &vo_opts->window_maximized);
993 }999 }
994 1000
1001 if (wl->activated != is_activated) {
1002 wl->activated = is_activated;
1003 if ((!wl->focused && wl->activated && wl->has_keyboard_input) ||
1004 (wl->focused && !wl->activated))
1005 {
1006 wl->focused = !wl->focused;
1007 wl->pending_vo_events |= VO_EVENT_FOCUS;
1008 }
1009 }
1010
995 if (!(wl->pending_vo_events & VO_EVENT_LIVE_RESIZING))1011 if (!(wl->pending_vo_events & VO_EVENT_LIVE_RESIZING))
996 vo_query_and_reset_events(wl->vo, VO_EVENT_LIVE_RESIZING);1012 vo_query_and_reset_events(wl->vo, VO_EVENT_LIVE_RESIZING);
997 1013
@@ -1544,6 +1560,10 @@ int vo_wayland_control(struct vo *vo, int *events, int request, void *arg) · 546 unmodified lines
1544 }1560 }
1545 return VO_TRUE;1561 return VO_TRUE;
1546 }1562 }
1563 case VOCTRL_GET_FOCUSED: {
1564 *(bool *)arg = wl->focused;
1565 return VO_TRUE;
1566 }
1547 case VOCTRL_GET_DISPLAY_NAMES: {1567 case VOCTRL_GET_DISPLAY_NAMES: {
1548 *(char ***)arg = get_displays_spanned(wl);1568 *(char ***)arg = get_displays_spanned(wl);
1549 return VO_TRUE;1569 return VO_TRUE;
video/out/wayland_common.h+3−0
@@ -76,6 +76,9 @@ struct vo_wayland_state { · 75 unmodified lines
76 bool frame_wait;76 bool frame_wait;
77 bool state_change;77 bool state_change;
78 bool toplevel_configured;78 bool toplevel_configured;
79 bool activated;
80 bool has_keyboard_input;
81 bool focused;
79 int wakeup_pipe[2];82 int wakeup_pipe[2];
80 int pending_vo_events;83 int pending_vo_events;
81 int mouse_x;84 int mouse_x;
video/out/x11_common.c+6−0
@@ -1128,11 +1128,13 @@ void vo_x11_check_events(struct vo *vo) · 1127 unmodified lines
1128 case FocusIn:1128 case FocusIn:
1129 x11->has_focus = true;1129 x11->has_focus = true;
1130 vo_update_cursor(vo);1130 vo_update_cursor(vo);
1131 x11->pending_vo_events |= VO_EVENT_FOCUS;
1131 break;1132 break;
1132 case FocusOut:1133 case FocusOut:
1133 release_all_keys(vo);1134 release_all_keys(vo);
1134 x11->has_focus = false;1135 x11->has_focus = false;
1135 vo_update_cursor(vo);1136 vo_update_cursor(vo);
1137 x11->pending_vo_events |= VO_EVENT_FOCUS;
1136 break;1138 break;
1137 case KeyRelease:1139 case KeyRelease:
1138 release_all_keys(vo);1140 release_all_keys(vo);
@@ -1878,6 +1880,10 @@ int vo_x11_control(struct vo *vo, int *events, int request, void *arg) · 739 unmodified lines
1878 }1880 }
1879 return VO_TRUE;1881 return VO_TRUE;
1880 }1882 }
1883 case VOCTRL_GET_FOCUSED: {
1884 *(bool *)arg = x11->has_focus;
1885 return VO_TRUE;
1886 }
1881 case VOCTRL_GET_DISPLAY_NAMES: {1887 case VOCTRL_GET_DISPLAY_NAMES: {
1882 if (!x11->pseudo_mapped)1888 if (!x11->pseudo_mapped)
1883 return VO_FALSE;1889 return VO_FALSE;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t