From b9a07a67b85e9192faa0e285b4419bd5ef242a03 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Sep 2021 14:34:34 -0700 Subject: feat: reorganized code to be more modular --- sys/cmd/wm/input.c | 285 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 sys/cmd/wm/input.c (limited to 'sys/cmd/wm/input.c') diff --git a/sys/cmd/wm/input.c b/sys/cmd/wm/input.c new file mode 100644 index 0000000..e155024 --- /dev/null +++ b/sys/cmd/wm/input.c @@ -0,0 +1,285 @@ +#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(xkb_keysym_t sym) +{ + Client *current, *next; + + switch(sym) { + case XKB_KEY_Escape: + wl_display_terminate(server.display); + break; + case XKB_KEY_F1: + /* cycle to the next client */ + if(wl_list_length(&server.clients) < 2) break; + + current = wl_container_of(server.clients.next, current, link); + next = wl_container_of(current->link.next, next, link); + + focus(next, next->xdg->surface); + + /* move previous client to the end of the list */ + wl_list_remove(¤t->link); + wl_list_insert(server.clients.prev, ¤t->link); + break; + default: + return false; + } + return true; +} + +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; + struct wlr_seat *seat = server.input.seat; + + 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((modifier & WLR_MODIFIER_ALT) && event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { + for(i=0; idevice); + wlr_seat_keyboard_notify_key(seat, event->time_msec, + event->keycode, event->state); + } +} + +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); + wlr_keyboard_set_keymap(device->keyboard, keymap); + + xkb_keymap_unref(keymap); + xkb_context_unref(context); + + wlr_keyboard_set_repeat_info(device->keyboard, 25, 600); + + 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); + + 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->x = new_left - box.x; + client->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 +docursor(uint32 time) +{ + double sx, sy; + Client *client; + struct wlr_seat *seat; + struct wlr_surface *surface; + + if(server.cursor.mode == CursorMove) + return cursormove(time); + else if(server.cursor.mode == CursorResize) + return cursorresize(time); + + /* Otherwise, find the client under the pointer and send the event along. */ + seat = server.input.seat; + surface = nil; + client = clientat(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); + docursor(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); + docursor(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 = clientat(server.cursor.dot->x, server.cursor.dot->y, &surface, &sx, &sy); + if (event->state == WLR_BUTTON_RELEASED) + server.cursor.mode = CursorPassthrough; + else + focus(client, surface); +} + +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_client = server.input.seat->pointer_state.focused_client; + if(focused_client == 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); +} + -- cgit v1.2.1