From: Johan Malm Date: Fri, 2 Oct 2020 20:19:56 +0000 (+0100) Subject: Refactor seat.c, keyboard.c, cursor.c X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=25829d122c2fd530df12d7383ad48259717a000e;p=proto%2Flabwc.git Refactor seat.c, keyboard.c, cursor.c Use wlr_keyboard_group --- diff --git a/include/labwc.h b/include/labwc.h index 365ca1ae..622247a1 100644 --- a/include/labwc.h +++ b/include/labwc.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,36 @@ enum cursor_mode { LAB_CURSOR_RESIZE, }; +struct input { + struct wlr_input_device *wlr_input_device; + struct seat *seat; + struct wl_listener destroy; + struct wl_list link; /* seat::inputs */ +}; + +struct seat { + struct wlr_seat *seat; + struct server *server; + struct wlr_keyboard_group *keyboard_group; + struct wlr_cursor *cursor; + struct wlr_xcursor_manager *xcursor_manager; + + struct wl_list inputs; + struct wl_listener new_input; + + struct wl_listener cursor_motion; + struct wl_listener cursor_motion_absolute; + struct wl_listener cursor_button; + struct wl_listener cursor_axis; + struct wl_listener cursor_frame; + + struct wl_listener request_cursor; + struct wl_listener request_set_selection; + + struct wl_listener keyboard_key; + struct wl_listener keyboard_modifiers; +}; + struct server { struct wl_display *wl_display; struct wlr_renderer *renderer; @@ -56,22 +87,11 @@ struct server { struct wl_listener xdg_toplevel_decoration; struct wlr_xwayland *xwayland; struct wl_listener new_xwayland_surface; + struct wl_list views; struct wl_list unmanaged_surfaces; - struct wlr_cursor *cursor; - struct wlr_xcursor_manager *cursor_mgr; - struct wl_listener cursor_motion; - struct wl_listener cursor_motion_absolute; - struct wl_listener cursor_button; - struct wl_listener cursor_axis; - struct wl_listener cursor_frame; - - struct wlr_seat *seat; - struct wl_listener new_input; - struct wl_listener request_cursor; - struct wl_listener request_set_selection; - struct wl_list keyboards; + struct seat seat; /* cursor interactive */ enum cursor_mode cursor_mode; @@ -80,9 +100,9 @@ struct server { struct wlr_box grab_box; uint32_t resize_edges; - struct wlr_output_layout *output_layout; struct wl_list outputs; struct wl_listener new_output; + struct wlr_output_layout *output_layout; /* Set when in cycle (alt-tab) mode */ struct view *cycle_view; @@ -180,21 +200,12 @@ struct xwayland_unmanaged { struct wl_listener destroy; }; -struct keyboard { - struct wl_list link; - struct server *server; - struct wlr_input_device *device; - - struct wl_listener modifiers; - struct wl_listener key; -}; - void xdg_toplevel_decoration(struct wl_listener *listener, void *data); void xdg_surface_new(struct wl_listener *listener, void *data); void xwayland_surface_new(struct wl_listener *listener, void *data); void xwayland_unmanaged_create(struct server *server, - struct wlr_xwayland_surface *xsurface); + struct wlr_xwayland_surface *xsurface); /** * view_get_surface_geometry - geometry relative to view @@ -223,28 +234,22 @@ struct view *desktop_view_at(struct server *server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy, int *view_area); -void seat_init(struct wlr_seat *seat); +void cursor_init(struct seat *seat); +void keyboard_init(struct seat *seat); +void seat_init(struct server *server); +void seat_finish(struct server *server); void seat_focus_surface(struct wlr_surface *surface); struct wlr_surface *seat_focused_surface(void); void interactive_begin(struct view *view, enum cursor_mode mode, uint32_t edges); +void output_init(struct server *server); + void server_init(struct server *server); void server_start(struct server *server); void server_finish(struct server *server); -void cursor_motion(struct wl_listener *listener, void *data); -void cursor_motion_absolute(struct wl_listener *listener, void *data); -void cursor_button(struct wl_listener *listener, void *data); -void cursor_axis(struct wl_listener *listener, void *data); -void cursor_frame(struct wl_listener *listener, void *data); -void cursor_new(struct server *server, struct wlr_input_device *device); - -void keyboard_new(struct server *server, struct wlr_input_device *device); - -void output_init(struct server *server); - struct border deco_thickness(struct view *view); struct wlr_box deco_max_extents(struct view *view); struct wlr_box deco_box(struct view *view, enum deco_part deco_part); diff --git a/src/cursor.c b/src/cursor.c index 61facafc..c9931915 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -1,11 +1,45 @@ #include "labwc.h" +static void +request_cursor_notify(struct wl_listener *listener, void *data) +{ + struct seat *seat = wl_container_of(listener, seat, request_cursor); + /* + * This event is rasied by the seat when a client provides a cursor + * image + */ + struct wlr_seat_pointer_request_set_cursor_event *event = data; + struct wlr_seat_client *focused_client = + seat->seat->pointer_state.focused_client; + + /* This can be sent by any client, so we check to make sure this one is + * actually has pointer focus first. */ + if (focused_client == event->seat_client) { + /* Once we've vetted the client, we can tell the cursor to use + * the provided surface as the cursor image. It will set the + * hardware cursor on the output that it's currently on and + * continue to do so as the cursor moves between outputs. */ + wlr_cursor_set_surface(seat->cursor, event->surface, + event->hotspot_x, event->hotspot_y); + } +} + +static void +request_set_selection_notify(struct wl_listener *listener, void *data) +{ + struct seat *seat = wl_container_of( + listener, seat, request_set_selection); + struct wlr_seat_request_set_selection_event *event = data; + wlr_seat_set_selection(seat->seat, event->source, + event->serial); +} + static void process_cursor_move(struct server *server, uint32_t time) { /* Move the grabbed view to the new position. */ - double dx = server->cursor->x - server->grab_x; - double dy = server->cursor->y - server->grab_y; + double dx = server->seat.cursor->x - server->grab_x; + double dy = server->seat.cursor->y - server->grab_y; server->grabbed_view->x = server->grab_box.x + dx; server->grabbed_view->y = server->grab_box.y + dy; @@ -29,8 +63,8 @@ process_cursor_resize(struct server *server, uint32_t time) * TODO: Wait for the client to prepare a buffer at the new size, then * commit any movement that was prepared. */ - double dx = server->cursor->x - server->grab_x; - double dy = server->cursor->y - server->grab_y; + double dx = server->seat.cursor->x - server->grab_x; + double dy = server->seat.cursor->y - server->grab_y; struct view *view = server->grabbed_view; struct wlr_box new_view_geo = { @@ -79,11 +113,11 @@ process_cursor_motion(struct server *server, uint32_t time) /* Otherwise, find the view under the pointer and send the event along. */ double sx, sy; - struct wlr_seat *seat = server->seat; + struct wlr_seat *wlr_seat = server->seat.seat; struct wlr_surface *surface = NULL; int view_area; struct view *view = - desktop_view_at(server, server->cursor->x, server->cursor->y, + desktop_view_at(server, server->seat.cursor->x, server->seat.cursor->y, &surface, &sx, &sy, &view_area); if (!view) { /* @@ -92,7 +126,7 @@ process_cursor_motion(struct server *server, uint32_t time) * you move it around the screen, not over any views. */ wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, XCURSOR_DEFAULT, server->cursor); + server->seat.xcursor_manager, XCURSOR_DEFAULT, server->seat.cursor); } /* TODO: Could we use wlr_xcursor_get_resize_name() here?? */ @@ -101,28 +135,28 @@ process_cursor_motion(struct server *server, uint32_t time) break; case LAB_DECO_PART_TOP: wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, "top_side", server->cursor); + server->seat.xcursor_manager, "top_side", server->seat.cursor); break; case LAB_DECO_PART_RIGHT: wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, "right_side", server->cursor); + server->seat.xcursor_manager, "right_side", server->seat.cursor); break; case LAB_DECO_PART_BOTTOM: wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, "bottom_side", server->cursor); + server->seat.xcursor_manager, "bottom_side", server->seat.cursor); break; case LAB_DECO_PART_LEFT: wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, "left_side", server->cursor); + server->seat.xcursor_manager, "left_side", server->seat.cursor); break; default: wlr_xcursor_manager_set_cursor_image( - server->cursor_mgr, XCURSOR_DEFAULT, server->cursor); + server->seat.xcursor_manager, XCURSOR_DEFAULT, server->seat.cursor); break; } if (surface) { bool focus_changed = - seat->pointer_state.focused_surface != surface; + wlr_seat->pointer_state.focused_surface != surface; /* * "Enter" the surface if necessary. This lets the client know * that the cursor has entered one of its surfaces. @@ -131,18 +165,18 @@ process_cursor_motion(struct server *server, uint32_t time) * distinct from keyboard focus. You get pointer focus by moving * the pointer over a window. */ - wlr_seat_pointer_notify_enter(seat, surface, sx, sy); + wlr_seat_pointer_notify_enter(wlr_seat, surface, sx, sy); if (!focus_changed) { /* * The enter event contains coordinates, so we only need * to notify on motion if the focus did not change. */ - wlr_seat_pointer_notify_motion(seat, time, sx, sy); + wlr_seat_pointer_notify_motion(wlr_seat, time, sx, sy); } } else { /* Clear pointer focus so future button events and such are not * sent to the last client to have the cursor over it. */ - wlr_seat_pointer_clear_focus(seat); + wlr_seat_pointer_clear_focus(wlr_seat); } } @@ -153,8 +187,7 @@ cursor_motion(struct wl_listener *listener, void *data) * This event is forwarded by the cursor when a pointer emits a * _relative_ pointer motion event (i.e. a delta) */ - struct server *server = - wl_container_of(listener, server, cursor_motion); + struct seat *seat = wl_container_of(listener, seat, cursor_motion); struct wlr_event_pointer_motion *event = data; /* @@ -164,9 +197,9 @@ cursor_motion(struct wl_listener *listener, void *data) * device which generated the event. You can pass NULL for the device * if you want to move the cursor around without any input. */ - wlr_cursor_move(server->cursor, event->device, event->delta_x, - event->delta_y); - process_cursor_motion(server, event->time_msec); + wlr_cursor_move(seat->cursor, event->device, event->delta_x, + event->delta_y); + process_cursor_motion(seat->server, event->time_msec); } void @@ -180,12 +213,11 @@ cursor_motion_absolute(struct wl_listener *listener, void *data) * window from any edge, so we have to warp the mouse there. There is * also some hardware which emits these events. */ - struct server *server = - wl_container_of(listener, server, cursor_motion_absolute); + struct seat *seat = wl_container_of( + listener, seat, cursor_motion_absolute); struct wlr_event_pointer_motion_absolute *event = data; - wlr_cursor_warp_absolute(server->cursor, event->device, event->x, - event->y); - process_cursor_motion(server, event->time_msec); + wlr_cursor_warp_absolute(seat->cursor, event->device, event->x, event->y); + process_cursor_motion(seat->server, event->time_msec); } void @@ -195,21 +227,21 @@ cursor_button(struct wl_listener *listener, void *data) * This event is forwarded by the cursor when a pointer emits a button * event. */ - struct server *server = - wl_container_of(listener, server, cursor_button); + struct seat *seat = wl_container_of(listener, seat, cursor_button); + struct server *server = seat->server; struct wlr_event_pointer_button *event = data; /* * Notify the client with pointer focus that a button press has * occurred. */ - wlr_seat_pointer_notify_button(server->seat, event->time_msec, - event->button, event->state); + wlr_seat_pointer_notify_button(seat->seat, event->time_msec, + event->button, event->state); double sx, sy; struct wlr_surface *surface; int view_area; struct view *view = - desktop_view_at(server, server->cursor->x, server->cursor->y, + desktop_view_at(server, server->seat.cursor->x, server->seat.cursor->y, &surface, &sx, &sy, &view_area); if (event->state == WLR_BUTTON_RELEASED) { /* Exit interactive move/resize mode. */ @@ -254,13 +286,13 @@ cursor_axis(struct wl_listener *listener, void *data) * This event is forwarded by the cursor when a pointer emits an axis * event, for example when you move the scroll wheel. */ - struct server *server = wl_container_of(listener, server, cursor_axis); + struct seat *seat = wl_container_of(listener, seat, cursor_axis); struct wlr_event_pointer_axis *event = data; /* Notify the client with pointer focus of the axis event. */ - wlr_seat_pointer_notify_axis(server->seat, event->time_msec, - event->orientation, event->delta, - event->delta_discrete, event->source); + wlr_seat_pointer_notify_axis(seat->seat, event->time_msec, + event->orientation, event->delta, event->delta_discrete, + event->source); } void @@ -273,14 +305,38 @@ cursor_frame(struct wl_listener *listener, void *data) * at the same time, in which case a frame event won't be sent in * between. */ - struct server *server = wl_container_of(listener, server, cursor_frame); + struct seat *seat = wl_container_of(listener, seat, cursor_frame); /* Notify the client with pointer focus of the frame event. */ - wlr_seat_pointer_notify_frame(server->seat); + wlr_seat_pointer_notify_frame(seat->seat); } void -cursor_new(struct server *server, struct wlr_input_device *device) +cursor_init(struct seat *seat) { - /* TODO: Configure libinput on device to set tap, acceleration, etc */ - wlr_cursor_attach_input_device(server->cursor, device); + seat->xcursor_manager = wlr_xcursor_manager_create(NULL, 24); + wlr_xcursor_manager_load(seat->xcursor_manager, 1); + + seat->cursor_motion.notify = cursor_motion; + wl_signal_add(&seat->cursor->events.motion, &seat->cursor_motion); + seat->cursor_motion_absolute.notify = cursor_motion_absolute; + wl_signal_add(&seat->cursor->events.motion_absolute, + &seat->cursor_motion_absolute); + seat->cursor_button.notify = cursor_button; + wl_signal_add(&seat->cursor->events.button, &seat->cursor_button); + seat->cursor_axis.notify = cursor_axis; + wl_signal_add(&seat->cursor->events.axis, &seat->cursor_axis); + seat->cursor_frame.notify = cursor_frame; + wl_signal_add(&seat->cursor->events.frame, &seat->cursor_frame); + + seat->request_cursor.notify = request_cursor_notify; + wl_signal_add(&seat->seat->events.request_set_cursor, &seat->request_cursor); + seat->request_set_selection.notify = request_set_selection_notify; + wl_signal_add(&seat->seat->events.request_set_selection, &seat->request_set_selection); + + /* TODO: + * seat->request_set_primary_selection.notify = + * request_set_primary_selectioni_notify; + * wl_signal_add(&seat->seat->events.request_set_primary_selection, + * &seat->request_set_primary_selection); + */ } diff --git a/src/desktop.c b/src/desktop.c index ad2d81ee..18e3bad2 100644 --- a/src/desktop.c +++ b/src/desktop.c @@ -68,23 +68,23 @@ static void focus_view(struct view *view) { struct server *server = view->server; - struct wlr_seat *seat = server->seat; + struct wlr_seat *wlr_seat = server->seat.seat; struct wlr_surface *prev_surface; - prev_surface = seat->keyboard_state.focused_surface; + prev_surface = wlr_seat->keyboard_state.focused_surface; if (prev_surface == view->surface) { /* Don't re-focus an already focused surface. */ return; } if (prev_surface) { - set_activated(seat->keyboard_state.focused_surface, false); + set_activated(wlr_seat->keyboard_state.focused_surface, false); } move_to_front(view); set_activated(view->surface, true); - struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); - wlr_seat_keyboard_notify_enter(seat, view->surface, keyboard->keycodes, - keyboard->num_keycodes, - &keyboard->modifiers); + struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(wlr_seat); + wlr_seat_keyboard_notify_enter(wlr_seat, view->surface, + keyboard->keycodes, keyboard->num_keycodes, + &keyboard->modifiers); move_xwayland_sub_views_to_front(view); } diff --git a/src/interactive.c b/src/interactive.c index 9de6eb21..77198e98 100644 --- a/src/interactive.c +++ b/src/interactive.c @@ -8,13 +8,14 @@ interactive_begin(struct view *view, enum cursor_mode mode, uint32_t edges) * the compositor stops propegating pointer events to clients and * instead consumes them itself, to move or resize windows. */ + struct seat *seat = &view->server->seat; struct server *server = view->server; server->grabbed_view = view; server->cursor_mode = mode; /* Remember view and cursor positions at start of move/resize */ - server->grab_x = server->cursor->x; - server->grab_y = server->cursor->y; + server->grab_x = seat->cursor->x; + server->grab_y = seat->cursor->y; struct wlr_box box = { .x = view->x, .y = view->y, .width = view->w, .height = view->h }; diff --git a/src/keyboard.c b/src/keyboard.c index 9d400f86..496d328b 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -2,24 +2,11 @@ #include "labwc.h" static void -keyboard_handle_modifiers(struct wl_listener *listener, void *data) +keyboard_modifiers_notify(struct wl_listener *listener, void *data) { - /* - * This event is raised when a modifier key, such as shift or alt, is - * pressed. We simply communicate this to the client. - */ - struct keyboard *keyboard = - wl_container_of(listener, keyboard, modifiers); - /* - * A seat can only have one keyboard, but this is a limitation of the - * Wayland protocol - not wlroots. We assign all connected keyboards to - * the same seat. You can swap out the underlying wlr_keyboard like - * this and wlr_seat handles this transparently. - */ - wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device); - /* Send modifiers to the client. */ - wlr_seat_keyboard_notify_modifiers( - keyboard->server->seat, &keyboard->device->keyboard->modifiers); + struct seat *seat = wl_container_of(listener, seat, keyboard_modifiers); + wlr_seat_keyboard_notify_modifiers(seat->seat, + &seat->keyboard_group->keyboard.modifiers); } static bool @@ -42,24 +29,24 @@ handle_keybinding(struct server *server, uint32_t modifiers, xkb_keysym_t sym) } static void -keyboard_handle_key(struct wl_listener *listener, void *data) +keyboard_key_notify(struct wl_listener *listener, void *data) { /* This event is raised when a key is pressed or released. */ - struct keyboard *keyboard = wl_container_of(listener, keyboard, key); - struct server *server = keyboard->server; + struct seat *seat = wl_container_of(listener, seat, keyboard_key); + struct server *server = seat->server; struct wlr_event_keyboard_key *event = data; - struct wlr_seat *seat = server->seat; + struct wlr_seat *wlr_seat = server->seat.seat; + struct wlr_input_device *device = seat->keyboard_group->input_device; /* Translate libinput keycode -> xkbcommon */ uint32_t keycode = event->keycode + 8; /* Get a list of keysyms based on the keymap for this keyboard */ const xkb_keysym_t *syms; - int nsyms = xkb_state_key_get_syms( - keyboard->device->keyboard->xkb_state, keycode, &syms); + int nsyms = xkb_state_key_get_syms(device->keyboard->xkb_state, keycode, &syms); bool handled = false; uint32_t modifiers = - wlr_keyboard_get_modifiers(keyboard->device->keyboard); + wlr_keyboard_get_modifiers(device->keyboard); if (server->cycle_view) { if ((syms[0] == XKB_KEY_Alt_L) && @@ -84,42 +71,28 @@ keyboard_handle_key(struct wl_listener *listener, void *data) if (!handled) { /* Otherwise, we pass it along to the client. */ - wlr_seat_set_keyboard(seat, keyboard->device); - wlr_seat_keyboard_notify_key(seat, event->time_msec, + wlr_seat_set_keyboard(wlr_seat, device); + wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec, event->keycode, event->state); } } void -keyboard_new(struct server *server, struct wlr_input_device *device) +keyboard_init(struct seat *seat) { - struct keyboard *keyboard = calloc(1, sizeof(struct keyboard)); - keyboard->server = server; - keyboard->device = device; - - /* - * We need to prepare an XKB keymap and assign it to the keyboard. This - * assumes the defaults (e.g. layout = "us"). - */ + seat->keyboard_group = wlr_keyboard_group_create(); + struct wlr_keyboard *kb = &seat->keyboard_group->keyboard; struct xkb_rule_names rules = { 0 }; struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - struct xkb_keymap *keymap = xkb_map_new_from_names( - context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); - - wlr_keyboard_set_keymap(device->keyboard, keymap); + struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules, + XKB_KEYMAP_COMPILE_NO_FLAGS); + wlr_keyboard_set_keymap(kb, keymap); xkb_keymap_unref(keymap); xkb_context_unref(context); - wlr_keyboard_set_repeat_info(device->keyboard, 25, 600); - - /* Here we set up listeners for keyboard events. */ - keyboard->modifiers.notify = keyboard_handle_modifiers; - wl_signal_add(&device->keyboard->events.modifiers, - &keyboard->modifiers); - keyboard->key.notify = keyboard_handle_key; - wl_signal_add(&device->keyboard->events.key, &keyboard->key); - - wlr_seat_set_keyboard(server->seat, device); + wlr_keyboard_set_repeat_info(kb, 25, 600); - /* And add the keyboard to our list of keyboards */ - wl_list_insert(&server->keyboards, &keyboard->link); + seat->keyboard_key.notify = keyboard_key_notify; + wl_signal_add(&kb->events.key, &seat->keyboard_key); + seat->keyboard_modifiers.notify = keyboard_modifiers_notify; + wl_signal_add(&kb->events.modifiers, &seat->keyboard_modifiers); } diff --git a/src/layers.c b/src/layers.c index 269186a4..8f07f484 100644 --- a/src/layers.c +++ b/src/layers.c @@ -275,8 +275,8 @@ server_new_layer_surface(struct wl_listener *listener, void *data) struct wlr_layer_surface_v1 *layer_surface = data; if (!layer_surface->output) { struct wlr_output *output = wlr_output_layout_output_at( - server->output_layout, server->cursor->x, - server->cursor->y); + server->output_layout, server->seat.cursor->x, + server->seat.cursor->y); layer_surface->output = output; } diff --git a/src/output.c b/src/output.c index 901b0faa..7cf62b73 100644 --- a/src/output.c +++ b/src/output.c @@ -141,7 +141,7 @@ render_decorations(struct wlr_output *output, struct view *view) draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TITLE)); /* button background */ - struct wlr_cursor *cur = view->server->cursor; + struct wlr_cursor *cur = view->server->seat.cursor; enum deco_part deco_part = deco_at(view, cur->x, cur->y); struct wlr_box box = deco_box(view, deco_part); diff --git a/src/seat.c b/src/seat.c index d311886d..1ae7f311 100644 --- a/src/seat.c +++ b/src/seat.c @@ -1,11 +1,116 @@ +#include #include "labwc.h" static struct wlr_seat *current_seat; +static void +input_device_destroy(struct wl_listener *listener, void *data) +{ + struct input *input = wl_container_of(listener, input, destroy); + wl_list_remove(&input->link); + free(input); +} + +void +new_pointer(struct seat *seat, struct input *input) +{ + /* TODO: Configure libinput on device to set tap, acceleration, etc */ + wlr_cursor_attach_input_device(seat->cursor, input->wlr_input_device); +} + +void +new_keyboard(struct seat *seat, struct input *input) +{ + struct wlr_keyboard *kb = input->wlr_input_device->keyboard; + wlr_keyboard_set_keymap(kb, seat->keyboard_group->keyboard.keymap); + wlr_keyboard_group_add_keyboard(seat->keyboard_group, kb); + wlr_seat_set_keyboard(seat->seat, input->wlr_input_device); +} + +static void +new_input_notify(struct wl_listener *listener, void *data) +{ + struct seat *seat = wl_container_of(listener, seat, new_input); + struct wlr_input_device *device = data; + struct input *input = calloc(1, sizeof(struct input)); + input->wlr_input_device = device; + input->seat = seat; + + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + new_keyboard(seat, input); + break; + case WLR_INPUT_DEVICE_POINTER: + new_pointer(seat, input); + break; + default: + wlr_log(WLR_INFO, "unsupported input device"); + break; + } + + input->destroy.notify = input_device_destroy; + wl_signal_add(&device->events.destroy, &input->destroy); + wl_list_insert(&seat->inputs, &input->link); + + uint32_t caps = 0; + wl_list_for_each(input, &seat->inputs, link) { + switch (input->wlr_input_device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + caps |= WL_SEAT_CAPABILITY_KEYBOARD; + break; + case WLR_INPUT_DEVICE_POINTER: + caps |= WL_SEAT_CAPABILITY_POINTER; + break; + default: + break; + } + } + wlr_seat_set_capabilities(seat->seat, caps); +} + +void +seat_init(struct server *server) +{ + struct seat *seat = &server->seat; + seat->server = server; + + seat->seat = wlr_seat_create(server->wl_display, "seat0"); + if (!seat->seat) { + wlr_log(WLR_ERROR, "cannot allocate seat"); + exit(EXIT_FAILURE); + } + current_seat = seat->seat; + + wl_list_init(&seat->inputs); + seat->new_input.notify = new_input_notify; + wl_signal_add(&server->backend->events.new_input, &seat->new_input); + + seat->cursor = wlr_cursor_create(); + if (!seat->cursor) { + wlr_log(WLR_ERROR, "unable to create cursor"); + exit(EXIT_FAILURE); + } + wlr_cursor_attach_output_layout(seat->cursor, server->output_layout); + + keyboard_init(seat); + cursor_init(seat); +} + void -seat_init(struct wlr_seat *seat) +seat_finish(struct server *server) { - current_seat = seat; + struct seat *seat = &server->seat; + wl_list_remove(&seat->cursor_motion.link); + wl_list_remove(&seat->cursor_motion_absolute.link); + wl_list_remove(&seat->cursor_button.link); + wl_list_remove(&seat->cursor_axis.link); + wl_list_remove(&seat->cursor_frame.link); + wl_list_remove(&seat->request_cursor.link); + wl_list_remove(&seat->request_set_selection.link); + wl_list_remove(&seat->new_input.link); + wlr_xcursor_manager_destroy(seat->xcursor_manager); + wlr_cursor_destroy(seat->cursor); + wlr_seat_destroy(server->seat.seat); } void diff --git a/src/server.c b/src/server.c index d1852e2d..4d2a5cd8 100644 --- a/src/server.c +++ b/src/server.c @@ -14,71 +14,6 @@ static struct wlr_compositor *compositor; static struct wl_event_source *sighup_source; -static void -server_new_input(struct wl_listener *listener, void *data) -{ - /* - * This event is raised by the backend when a new input device becomes - * available. - */ - struct server *server = wl_container_of(listener, server, new_input); - struct wlr_input_device *device = data; - switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard_new(server, device); - break; - case WLR_INPUT_DEVICE_POINTER: - cursor_new(server, device); - break; - default: - break; - } - - /* - * We need to let the wlr_seat know what our capabilities are, which is - * communiciated to the client. - */ - uint32_t caps = WL_SEAT_CAPABILITY_POINTER; - if (!wl_list_empty(&server->keyboards)) { - caps |= WL_SEAT_CAPABILITY_KEYBOARD; - } - wlr_seat_set_capabilities(server->seat, caps); -} - -static void -seat_request_cursor(struct wl_listener *listener, void *data) -{ - struct server *server = - wl_container_of(listener, server, request_cursor); - /* - * This event is rasied by the seat when a client provides a cursor - * image - */ - struct wlr_seat_pointer_request_set_cursor_event *event = data; - struct wlr_seat_client *focused_client = - server->seat->pointer_state.focused_client; - - /* This can be sent by any client, so we check to make sure this one is - * actually has pointer focus first. */ - if (focused_client == event->seat_client) { - /* Once we've vetted the client, we can tell the cursor to use - * the provided surface as the cursor image. It will set the - * hardware cursor on the output that it's currently on and - * continue to do so as the cursor moves between outputs. */ - wlr_cursor_set_surface(server->cursor, event->surface, - event->hotspot_x, event->hotspot_y); - } -} - -static void -seat_request_set_selection(struct wl_listener *listener, void *data) -{ - struct server *server = - wl_container_of(listener, server, request_set_selection); - struct wlr_seat_request_set_selection_event *event = data; - wlr_seat_set_selection(server->seat, event->source, event->serial); -} - static void reload_config_and_theme(void) { @@ -161,49 +96,7 @@ server_init(struct server *server) } output_init(server); - - /* - * Configures a seat, which is a single "seat" at which a user sits - * and operates the computer. This conceptually includes up to one - * keyboard, pointer, touch, and drawing tablet device. We also rig up - * a listener to let us know when new input devices are available on - * the backend. - */ - server->seat = wlr_seat_create(server->wl_display, "seat0"); - if (!server->seat) { - wlr_log(WLR_ERROR, "cannot allocate seat0"); - exit(EXIT_FAILURE); - } - seat_init(server->seat); - - server->cursor = wlr_cursor_create(); - if (!server->cursor) { - wlr_log(WLR_ERROR, "unable to create cursor"); - exit(EXIT_FAILURE); - } - wlr_cursor_attach_output_layout(server->cursor, server->output_layout); - - server->cursor_motion.notify = cursor_motion; - wl_signal_add(&server->cursor->events.motion, &server->cursor_motion); - server->cursor_motion_absolute.notify = cursor_motion_absolute; - wl_signal_add(&server->cursor->events.motion_absolute, - &server->cursor_motion_absolute); - server->cursor_button.notify = cursor_button; - wl_signal_add(&server->cursor->events.button, &server->cursor_button); - server->cursor_axis.notify = cursor_axis; - wl_signal_add(&server->cursor->events.axis, &server->cursor_axis); - server->cursor_frame.notify = cursor_frame; - wl_signal_add(&server->cursor->events.frame, &server->cursor_frame); - - wl_list_init(&server->keyboards); - server->new_input.notify = server_new_input; - wl_signal_add(&server->backend->events.new_input, &server->new_input); - server->request_cursor.notify = seat_request_cursor; - wl_signal_add(&server->seat->events.request_set_cursor, - &server->request_cursor); - server->request_set_selection.notify = seat_request_set_selection; - wl_signal_add(&server->seat->events.request_set_selection, - &server->request_set_selection); + seat_init(server); /* Init xdg-shell */ server->xdg_shell = wlr_xdg_shell_create(server->wl_display); @@ -247,7 +140,7 @@ server_init(struct server *server) /* Init xwayland */ server->xwayland = - wlr_xwayland_create(server->wl_display, compositor, false); + wlr_xwayland_create(server->wl_display, compositor, true); if (!server->xwayland) { wlr_log(WLR_ERROR, "cannot create xwayland server"); exit(EXIT_FAILURE); @@ -256,12 +149,6 @@ server_init(struct server *server) wl_signal_add(&server->xwayland->events.new_surface, &server->new_xwayland_surface); - server->cursor_mgr = - wlr_xcursor_manager_create(XCURSOR_DEFAULT, XCURSOR_SIZE); - if (!server->cursor_mgr) { - wlr_log(WLR_ERROR, "cannot create xwayland xcursor manager"); - } - if (setenv("DISPLAY", server->xwayland->display_name, true) < 0) { wlr_log_errno(WLR_ERROR, "unable to set DISPLAY for xwayland"); } else { @@ -269,12 +156,12 @@ server_init(struct server *server) server->xwayland->display_name); } - if (!wlr_xcursor_manager_load(server->cursor_mgr, 1)) { + if (!wlr_xcursor_manager_load(server->seat.xcursor_manager, 1)) { wlr_log(WLR_ERROR, "cannot load xwayland xcursor theme"); } struct wlr_xcursor *xcursor; - xcursor = wlr_xcursor_manager_get_xcursor(server->cursor_mgr, + xcursor = wlr_xcursor_manager_get_xcursor(server->seat.xcursor_manager, XCURSOR_DEFAULT, 1); if (xcursor) { struct wlr_xcursor_image *image = xcursor->images[0]; @@ -313,7 +200,7 @@ server_start(struct server *server) wl_display_init_shm(server->wl_display); - wlr_xwayland_set_seat(server->xwayland, server->seat); + wlr_xwayland_set_seat(server->xwayland, server->seat.seat); } void @@ -324,15 +211,9 @@ server_finish(struct server *server) wl_list_remove(&o->link); free(o); } - struct keyboard *k, *k_tmp; - wl_list_for_each_safe (k, k_tmp, &server->keyboards, link) { - wl_list_remove(&k->link); - free(k); - } - wlr_cursor_destroy(server->cursor); wlr_output_layout_destroy(server->output_layout); + seat_finish(server); wlr_xwayland_destroy(server->xwayland); - wlr_xcursor_manager_destroy(server->cursor_mgr); wl_event_source_remove(sighup_source); wl_display_destroy_clients(server->wl_display); wl_display_destroy(server->wl_display);