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
24392439 since setting the option, and the window size was not restricted in other
24402440 ways. The property is unavailable if no video is active.
24412441
2442+``focused``
2443+ Whether the window has focus. Currently works only on X11 and Wayland.
2444+
24422445 ``display-names``
24432446 Names of the displays that the mpv window covers. On X11, these
24442447 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
24102410 return m_property_double_ro(action, arg, cmd->cached_window_scale);
24112411 }
24122412
2413+static 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+
24132428 static int mp_property_display_names(void *ctx, struct m_property *prop,
24142429 int action, void *arg)
24152430 {
@@ -3593,6 +3608,7 @@ static const struct m_property mp_properties_base[] = { · 1177 unmodified lines
35933608 PROPERTY_BITRATE("audio-bitrate", false, STREAM_AUDIO),
35943609 PROPERTY_BITRATE("sub-bitrate", false, STREAM_SUB),
35953610
3611+ {"focused", mp_property_focused},
35963612 {"display-names", mp_property_display_names},
35973613 {"display-fps", mp_property_display_fps},
35983614 {"estimated-display-fps", mp_property_estimated_display_fps},
@@ -3676,6 +3692,7 @@ static const char *const *const mp_event_property_change[] = { · 77 unmodified lines
36763692 "osd-par", "osd-dimensions"),
36773693 E(MP_EVENT_WIN_STATE, "display-names", "display-fps"),
36783694 E(MP_EVENT_WIN_STATE2, "display-hidpi-scale"),
3695+ E(MP_EVENT_FOCUS, "focused"),
36793696 E(MP_EVENT_CHANGE_PLAYLIST, "playlist", "playlist-pos", "playlist-pos-1",
36803697 "playlist-count", "playlist/count", "playlist-current-pos",
36813698 "playlist-playing-pos"),
player/command.h+1−0
@@ -98,6 +98,7 @@ enum { · 97 unmodified lines
9898 MP_EVENT_WIN_RESIZE,
9999 MP_EVENT_WIN_STATE,
100100 MP_EVENT_WIN_STATE2,
101+ MP_EVENT_FOCUS,
101102 MP_EVENT_CHANGE_PLAYLIST,
102103 MP_EVENT_CORE_IDLE,
103104 MP_EVENT_DURATION_UPDATE,
player/playloop.c+2−0
@@ -844,6 +844,8 @@ static void handle_vo_events(struct MPContext *mpctx) · 843 unmodified lines
844844 mp_notify(mpctx, MP_EVENT_WIN_STATE, NULL);
845845 if (events & VO_EVENT_DPI)
846846 mp_notify(mpctx, MP_EVENT_WIN_STATE2, NULL);
847+ if (events & VO_EVENT_FOCUS)
848+ mp_notify(mpctx, MP_EVENT_FOCUS, NULL);
847849 }
848850
849851 static void handle_sstep(struct MPContext *mpctx)
video/out/vo.h+4−1
@@ -48,10 +48,11 @@ enum { · 47 unmodified lines
4848 // Special thing for encode mode (vo_driver.initially_blocked).
4949 // Part of VO_EVENTS_USER to make vo_is_ready_for_frame() work properly.
5050 VO_EVENT_INITIAL_UNBLOCK = 1 << 7,
51+ VO_EVENT_FOCUS = 1 << 8,
5152
5253 // Set of events the player core may be interested in.
5354 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,
5556 };
5657
5758 enum mp_voctrl {
@@ -99,6 +100,8 @@ enum mp_voctrl { · 41 unmodified lines
99100 VOCTRL_GET_UNFS_WINDOW_SIZE, // int[2] (w/h)
100101 VOCTRL_SET_UNFS_WINDOW_SIZE, // int[2] (w/h)
101102
103+ VOCTRL_GET_FOCUSED, // bool*
104+
102105 // char *** (NULL terminated array compatible with CONF_TYPE_STRING_LIST)
103106 // names for displays the window is on
104107 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
447447 uint32_t serial, struct wl_surface *surface,
448448 struct wl_array *keys)
449449 {
450+ struct vo_wayland_state *wl = data;
451+ wl->has_keyboard_input = true;
450452 }
451453
452454 static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
453455 uint32_t serial, struct wl_surface *surface)
454456 {
457+ struct vo_wayland_state *wl = data;
458+ wl->has_keyboard_input = false;
455459 }
456460
457461 static 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
951955
952956 bool is_maximized = false;
953957 bool is_fullscreen = false;
958+ bool is_activated = false;
954959 enum xdg_toplevel_state *state;
955960 wl_array_for_each(state, states) {
956961 switch (*state) {
@@ -961,6 +966,7 @@ static void handle_toplevel_config(void *data, struct xdg_toplevel *toplevel, · 4 unmodified lines
961966 wl->pending_vo_events |= VO_EVENT_LIVE_RESIZING;
962967 break;
963968 case XDG_TOPLEVEL_STATE_ACTIVATED:
969+ is_activated = true;
964970 /*
965971 * If we get an ACTIVATED state, we know it cannot be
966972 * 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
992998 m_config_cache_write_opt(wl->vo_opts_cache, &vo_opts->window_maximized);
993999 }
9941000
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+
9951011 if (!(wl->pending_vo_events & VO_EVENT_LIVE_RESIZING))
9961012 vo_query_and_reset_events(wl->vo, VO_EVENT_LIVE_RESIZING);
9971013
@@ -1544,6 +1560,10 @@ int vo_wayland_control(struct vo *vo, int *events, int request, void *arg) · 546 unmodified lines
15441560 }
15451561 return VO_TRUE;
15461562 }
1563+ case VOCTRL_GET_FOCUSED: {
1564+ *(bool *)arg = wl->focused;
1565+ return VO_TRUE;
1566+ }
15471567 case VOCTRL_GET_DISPLAY_NAMES: {
15481568 *(char ***)arg = get_displays_spanned(wl);
15491569 return VO_TRUE;
video/out/wayland_common.h+3−0
@@ -76,6 +76,9 @@ struct vo_wayland_state { · 75 unmodified lines
7676 bool frame_wait;
7777 bool state_change;
7878 bool toplevel_configured;
79+ bool activated;
80+ bool has_keyboard_input;
81+ bool focused;
7982 int wakeup_pipe[2];
8083 int pending_vo_events;
8184 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
11281128 case FocusIn:
11291129 x11->has_focus = true;
11301130 vo_update_cursor(vo);
1131+ x11->pending_vo_events |= VO_EVENT_FOCUS;
11311132 break;
11321133 case FocusOut:
11331134 release_all_keys(vo);
11341135 x11->has_focus = false;
11351136 vo_update_cursor(vo);
1137+ x11->pending_vo_events |= VO_EVENT_FOCUS;
11361138 break;
11371139 case KeyRelease:
11381140 release_all_keys(vo);
@@ -1878,6 +1880,10 @@ int vo_x11_control(struct vo *vo, int *events, int request, void *arg) · 739 unmodified lines
18781880 }
18791881 return VO_TRUE;
18801882 }
1883+ case VOCTRL_GET_FOCUSED: {
1884+ *(bool *)arg = x11->has_focus;
1885+ return VO_TRUE;
1886+ }
18811887 case VOCTRL_GET_DISPLAY_NAMES: {
18821888 if (!x11->pseudo_mapped)
18831889 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