simplify focusclient

This arranges the function into some logical tasks: deactivate the old
client, update wlroots' keyboard focus, update our data structures, and
activate the new client.  The last two only need to be done when
focusing something new, so an early return saves some horizontal space.
This commit is contained in:
Devin J. Pohly 2020-07-31 13:53:33 -05:00
parent 0a59f47c18
commit 90d80c0de9

67
dwl.c
View file

@ -681,63 +681,48 @@ dirtomon(int dir)
void
focusclient(Client *c, struct wlr_surface *surface, int lift)
{
Client *sel = selclient();
struct wlr_keyboard *kb;
/* Previously focused surface */
struct wlr_surface *psurface = seat->keyboard_state.focused_surface;
Client *old = selclient();
struct wlr_keyboard *kb = wlr_seat_get_keyboard(seat);
if (c) {
/* assert(VISIBLEON(c, c->mon)); ? */
/* Use top-level wlr_surface if nothing more specific given */
if (!surface)
/* Use top-level wlr_surface if nothing more specific is given */
if (c && !surface)
surface = WLR_SURFACE(c);
/* Focus the correct monitor (must come after selclient!) */
selmon = c->mon;
/* Deactivate old client if focus is changing */
if (c != old && old) {
if (old->type != XDGShell)
wlr_xwayland_surface_activate(old->xwayland_surface, 0);
else
wlr_xdg_toplevel_set_activated(old->xdg_surface, 0);
}
/* Move the client to the front of the focus stack */
/* Update wlroots' keyboard focus */
if (!c) {
/* With no client, all we have left is to clear focus */
wlr_seat_keyboard_notify_clear_focus(seat);
return;
}
/* Otherwise, update the focus if it has changed */
if (surface != seat->keyboard_state.focused_surface)
wlr_seat_keyboard_notify_enter(seat, surface,
kb->keycodes, kb->num_keycodes, &kb->modifiers);
/* Select client's monitor, move it to the top of the focus stack, and
* raise it in the stacking order if requested. */
selmon = c->mon;
wl_list_remove(&c->flink);
wl_list_insert(&fstack, &c->flink);
/* Also raise client in stacking order if requested */
if (lift) {
wl_list_remove(&c->slink);
wl_list_insert(&stack, &c->slink);
}
}
/*
* If the focused surface has changed, tell the seat to have the
* keyboard enter the new surface. wlroots will keep track of this and
* automatically send key events to the appropriate clients. If surface
* is NULL, we clear the focus instead.
*/
if (!surface) {
wlr_seat_keyboard_notify_clear_focus(seat);
} else if (surface != psurface) {
kb = wlr_seat_get_keyboard(seat);
wlr_seat_keyboard_notify_enter(seat, surface,
kb->keycodes, kb->num_keycodes, &kb->modifiers);
}
/*
* If the focused toplevel has changed, deactivate the old one. Always
* activate the current one. This lets the clients know to repaint
* accordingly, e.g. show/hide a caret.
*/
if (c != sel && sel) {
if (sel->type != XDGShell)
wlr_xwayland_surface_activate(sel->xwayland_surface, 0);
else
wlr_xdg_toplevel_set_activated(sel->xdg_surface, 0);
}
if (c) {
/* Activate the new client */
if (c->type != XDGShell)
wlr_xwayland_surface_activate(c->xwayland_surface, 1);
else
wlr_xdg_toplevel_set_activated(c->xdg_surface, 1);
}
}
void
focusmon(const Arg *arg)