#include "wm.h" // ----------------------------------------------------------------------- // keyboard static void keymodifier(struct wl_listener *l, void *data) { Keyboard *keyboard = wl_container_of(l, keyboard, event.modify); wlr_seat_set_keyboard(server.input.seat, keyboard->device); wlr_seat_keyboard_notify_modifiers(server.input.seat, &keyboard->device->keyboard->modifiers); } static int keybinding(uint32 modifier, xkb_keysym_t sym) { Key *key; for(key=cfg·binding; key!=cfg·endbinding; ++key) { if(modifier == key->modifier && sym == key->sym && key->action){ key->action(&key->arg); return 1; } } return 0; } static void keypress(struct wl_listener *l, void *data) { int i,h,n; uint32 keycode, modifier; const xkb_keysym_t *syms; struct Keyboard *keyboard = wl_container_of(l, keyboard, event.press); struct wlr_event_keyboard_key *event = data; keycode = event->keycode + 8; h = 0; n = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms); modifier = wlr_keyboard_get_modifiers(keyboard->device->keyboard); if(event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { for(i=0; idevice); wlr_seat_keyboard_notify_key(server.input.seat, event->time_msec, event->keycode, event->state); } } static void free_keyboard(struct wl_listener *l, void *data) { struct wlr_input_device *device = data; Keyboard *keyboard = device->data; /* wl_list_remove(&keyboard->link); wl_list_remove(&keyboard->event.modify.link); wl_list_remove(&keyboard->event.press.link); wl_list_remove(&keyboard->event.destroy.link); free(keyboard); */ } static void make_keyboard(struct wlr_input_device *device) { Keyboard *keyboard; struct xkb_context *context; struct xkb_keymap *keymap; context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); keymap = xkb_keymap_new_from_names(context, nil, XKB_KEYMAP_COMPILE_NO_FLAGS); wlr_keyboard_set_keymap(device->keyboard, keymap); xkb_keymap_unref(keymap); xkb_context_unref(context); wlr_keyboard_set_repeat_info(device->keyboard, cfg·repeat_rate, cfg·repeat_delay); keyboard = calloc(1, sizeof(*keyboard)); keyboard->device = device; keyboard->event.modify.notify = keymodifier; wl_signal_add(&device->keyboard->events.modifiers, &keyboard->event.modify); keyboard->event.press.notify = keypress; wl_signal_add(&device->keyboard->events.key, &keyboard->event.press); keyboard->event.destroy.notify = free_keyboard; wl_signal_add(&device->keyboard->events.destroy, &keyboard->event.destroy); wlr_seat_set_keyboard(server.input.seat, device); wl_list_insert(&server.input.keyboards, &keyboard->link); } // ----------------------------------------------------------------------- // cursor static void cursormove(uint32 time) { server.grab.x = server.cursor.dot->x - server.grab.x; server.grab.y = server.cursor.dot->y - server.grab.y; } static void cursorresize(uint32 time) { struct wlr_box box; Client *client = server.grab.client; double bx = server.cursor.dot->x - server.grab.x; double by = server.cursor.dot->y - server.grab.y; int new_left = server.grab.box.x; int new_right = server.grab.box.x + server.grab.box.width; int new_top = server.grab.box.y; int new_bottom = server.grab.box.y + server.grab.box.height; int new_width, new_height; if(server.resize & WLR_EDGE_TOP) { new_top = by; if (new_top >= new_bottom) new_top = new_bottom - 1; }else if (server.resize & WLR_EDGE_BOTTOM) { new_bottom = by; if (new_bottom <= new_top) new_bottom = new_top + 1; } if(server.resize & WLR_EDGE_LEFT) { new_left = bx; if (new_left >= new_right) new_left = new_right - 1; } else if(server.resize & WLR_EDGE_RIGHT) { new_right = bx; if (new_right <= new_left) new_right = new_left + 1; } wlr_xdg_surface_get_geometry(client->xdg, &box); client->geometry.x = new_left - box.x; client->geometry.y = new_top - box.y; new_width = new_right - new_left; new_height = new_bottom - new_top; wlr_xdg_toplevel_set_size(client->xdg, new_width, new_height); } static void move(uint32 time) { double sx, sy; Client *client; struct wlr_seat *seat; struct wlr_surface *surface; if(server.cursor.mode == CursorMove) { resize(server.grab.client, server.cursor.dot->x - server.grab.x, server.cursor.dot->y - server.grab.y, server.grab.client->geometry.width, server.grab.client->geometry.height, 1 ); return; } if(server.cursor.mode == CursorResize) { resize(server.grab.client, server.cursor.dot->x, server.cursor.dot->y, server.grab.client->geometry.width, server.grab.client->geometry.height, 1 ); return; } /* Otherwise, find the client under the pointer and send the event along. */ seat = server.input.seat; surface = nil; client = client_at(server.cursor.dot->x, server.cursor.dot->y, &surface, &sx, &sy); if(!client) wlr_xcursor_manager_set_cursor_image(server.cursor.manager, "left_ptr", server.cursor.dot); if(surface) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); } else { wlr_seat_pointer_clear_focus(seat); } } void cursor_move(struct wl_listener *l, void *data) { struct wlr_event_pointer_motion *event = data; wlr_cursor_move(server.cursor.dot, event->device, event->delta_x, event->delta_y); move(event->time_msec); } void cursor_move_abs(struct wl_listener *l, void *data) { struct wlr_event_pointer_motion_absolute *event = data; wlr_cursor_warp_absolute(server.cursor.dot, event->device, event->x, event->y); move(event->time_msec); } void cursor_button(struct wl_listener *l, void *data) { Client *client; double sx, sy; struct wlr_surface *surface; struct wlr_event_pointer_button *event = data; wlr_seat_pointer_notify_button(server.input.seat, event->time_msec, event->button, event->state); client = client_at(server.cursor.dot->x, server.cursor.dot->y, &surface, &sx, &sy); if (event->state == WLR_BUTTON_RELEASED) server.cursor.mode = CursorPassthrough; else focus(client, 1); } void cursor_axis(struct wl_listener *l, void *data) { struct wlr_event_pointer_axis *event = data; /* Notify the client with pointer focus of the axis event. */ wlr_seat_pointer_notify_axis(server.input.seat, event->time_msec, event->orientation, event->delta, event->delta_discrete, event->source); } void cursor_frame(struct wl_listener *l, void *data) { wlr_seat_pointer_notify_frame(server.input.seat); } void request_cursor(struct wl_listener *l, void *data) { struct wlr_seat_pointer_request_set_cursor_event *event = data; struct wlr_seat_client *focused = server.input.seat->pointer_state.focused_client; if(focused == event->seat_client) wlr_cursor_set_surface(server.cursor.dot, event->surface, event->hotspot_x, event->hotspot_y); } void request_set_selection(struct wl_listener *l, void *data) { struct wlr_seat_request_set_selection_event *event = data; wlr_seat_set_selection(server.input.seat, event->source, event->serial); } static void make_pointer(struct wlr_input_device *device) { wlr_cursor_attach_input_device(server.cursor.dot, device); } // ----------------------------------------------------------------------- // generic input void make_input(struct wl_listener *l, void *data) { uint32 capability; struct wlr_input_device *device = data; switch(device->type) { case WLR_INPUT_DEVICE_KEYBOARD: make_keyboard(device); break; case WLR_INPUT_DEVICE_POINTER: make_pointer(device); /* fallthrough */ default: break; } capability = WL_SEAT_CAPABILITY_POINTER; if(!wl_list_empty(&server.input.keyboards)) capability |= WL_SEAT_CAPABILITY_KEYBOARD; wlr_seat_set_capabilities(server.input.seat, capability); }