Add auto focus for views and basic keyboard handling

This commit is contained in:
buffet 2019-12-24 15:43:36 +00:00
parent 1931dcc605
commit abda3d3a0e
6 changed files with 67 additions and 3 deletions

View file

@ -52,4 +52,6 @@ void kiwmi_view_for_each_surface(
wlr_surface_iterator_func_t iterator,
void *user_data);
void focus_view(struct kiwmi_view *view, struct wlr_surface *surface);
#endif /* KIWMI_DESKTOP_VIEW_H */

View file

@ -14,6 +14,7 @@ struct kiwmi_input {
struct wl_list keyboards; // struct kiwmi_keyboard::link
struct wl_listener new_input;
struct kiwmi_cursor *cursor;
struct wlr_seat *seat;
};
bool input_init(struct kiwmi_input *input);

View file

@ -6,6 +6,7 @@
*/
#include "desktop/view.h"
#include "server.h"
void
kiwmi_view_for_each_surface(
@ -17,3 +18,40 @@ kiwmi_view_for_each_surface(
view->impl->for_each_surface(view, iterator, user_data);
}
}
void
focus_view(struct kiwmi_view *view, struct wlr_surface *surface)
{
if (!view) {
return;
}
struct kiwmi_desktop *desktop = view->desktop;
struct kiwmi_server *server = wl_container_of(desktop, server, desktop);
struct wlr_seat *seat = server->input.seat;
struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface;
if (prev_surface == surface) {
return;
}
if (prev_surface) {
struct wlr_xdg_surface *previous = wlr_xdg_surface_from_wlr_surface(
seat->keyboard_state.focused_surface);
wlr_xdg_toplevel_set_activated(previous, false);
}
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
// move view to front
wl_list_remove(&view->link);
wl_list_insert(&desktop->views, &view->link);
wlr_xdg_toplevel_set_activated(view->xdg_surface, true);
wlr_seat_keyboard_notify_enter(
seat,
view->xdg_surface->surface,
keyboard->keycodes,
keyboard->num_keycodes,
&keyboard->modifiers);
}

View file

@ -19,6 +19,7 @@ xdg_surface_map_notify(struct wl_listener *listener, void *UNUSED(data))
{
struct kiwmi_view *view = wl_container_of(listener, view, map);
view->mapped = true;
focus_view(view, view->xdg_surface->surface);
}
static void

View file

@ -11,6 +11,7 @@
#include <wlr/backend.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include "desktop/desktop.h"
@ -56,6 +57,13 @@ new_input_notify(struct wl_listener *listener, void *data)
// NOT HANDLED
break;
}
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
if (!wl_list_empty(&input->keyboards)) {
caps |= WL_SEAT_CAPABILITY_KEYBOARD;
}
wlr_seat_set_capabilities(input->seat, caps);
}
bool
@ -69,6 +77,8 @@ input_init(struct kiwmi_input *input)
return false;
}
input->seat = wlr_seat_create(server->wl_display, "seat-0");
wl_list_init(&input->keyboards);
input->new_input.notify = new_input_notify;

View file

@ -14,6 +14,7 @@
#include <wlr/backend.h>
#include <wlr/backend/multi.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/util/log.h>
#include <xkbcommon/xkbcommon.h>
@ -41,10 +42,13 @@ switch_vt(const xkb_keysym_t *syms, int nsyms, struct wlr_backend *backend)
}
static void
keyboard_modifiers_notify(
struct wl_listener *UNUSED(listener),
void *UNUSED(data))
keyboard_modifiers_notify(struct wl_listener *listener, void *UNUSED(data))
{
struct kiwmi_keyboard *keyboard =
wl_container_of(listener, keyboard, modifiers);
wlr_seat_set_keyboard(keyboard->server->input.seat, keyboard->device);
wlr_seat_keyboard_notify_modifiers(
keyboard->server->input.seat, &keyboard->device->keyboard->modifiers);
}
static void
@ -78,6 +82,12 @@ keyboard_key_notify(struct wl_listener *listener, void *data)
}
}
}
if (!handled) {
wlr_seat_set_keyboard(server->input.seat, keyboard->device);
wlr_seat_keyboard_notify_key(
server->input.seat, event->time_msec, event->keycode, event->state);
}
}
struct kiwmi_keyboard *
@ -109,5 +119,7 @@ keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device)
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(device->keyboard, 25, 600);
wlr_seat_set_keyboard(server->input.seat, device);
return keyboard;
}