Skip to content

Commit

Permalink
Remove access to wlr_input_device union
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion authored and kraftwerk28 committed Aug 7, 2022
1 parent 295af24 commit d26cf69
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 92 deletions.
1 change: 1 addition & 0 deletions include/sway/input/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct sway_shortcut_state {

struct sway_keyboard {
struct sway_seat_device *seat_device;
struct wlr_keyboard *wlr;

struct xkb_keymap *keymap;
xkb_layout_index_t effective_layout;
Expand Down
1 change: 1 addition & 0 deletions include/sway/input/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

struct sway_switch {
struct sway_seat_device *seat_device;
struct wlr_switch *wlr;
enum wlr_switch_state state;
enum wlr_switch_type type;

Expand Down
1 change: 1 addition & 0 deletions include/sway/input/tablet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct sway_tablet_pad {
struct wl_list link;
struct sway_seat_device *seat_device;
struct sway_tablet *tablet;
struct wlr_tablet_pad *wlr;
struct wlr_tablet_v2_tablet_pad *tablet_v2_pad;

struct wl_listener attach;
Expand Down
5 changes: 2 additions & 3 deletions sway/commands/input/xkb_switch_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ struct cmd_results *input_cmd_xkb_switch_layout(int argc, char **argv) {
struct xkb_switch_layout_action *action =
&actions[actions_len++];

action->keyboard = dev->wlr_device->keyboard;
action->keyboard = wlr_keyboard_from_input_device(dev->wlr_device);
if (relative) {
action->layout = get_layout_relative(
dev->wlr_device->keyboard, relative);
action->layout = get_layout_relative(action->keyboard, relative);
} else {
action->layout = layout;
}
Expand Down
2 changes: 1 addition & 1 deletion sway/input/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ static void apply_mapping_from_region(struct wlr_input_device *device,
double y1 = region->y1, y2 = region->y2;

if (region->mm && device->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
struct wlr_tablet *tablet = device->tablet;
struct wlr_tablet *tablet = wlr_tablet_from_input_device(device);
if (tablet->width_mm == 0 || tablet->height_mm == 0) {
return;
}
Expand Down
117 changes: 49 additions & 68 deletions sway/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,12 @@ static bool keyboard_execute_compositor_binding(struct sway_keyboard *keyboard,
static size_t keyboard_keysyms_translated(struct sway_keyboard *keyboard,
xkb_keycode_t keycode, const xkb_keysym_t **keysyms,
uint32_t *modifiers) {
struct wlr_input_device *device =
keyboard->seat_device->input_device->wlr_device;
*modifiers = wlr_keyboard_get_modifiers(device->keyboard);
*modifiers = wlr_keyboard_get_modifiers(keyboard->wlr);
xkb_mod_mask_t consumed = xkb_state_key_get_consumed_mods2(
device->keyboard->xkb_state, keycode, XKB_CONSUMED_MODE_XKB);
keyboard->wlr->xkb_state, keycode, XKB_CONSUMED_MODE_XKB);
*modifiers = *modifiers & ~consumed;

return xkb_state_key_get_syms(device->keyboard->xkb_state,
return xkb_state_key_get_syms(keyboard->wlr->xkb_state,
keycode, keysyms);
}

Expand All @@ -314,13 +312,11 @@ static size_t keyboard_keysyms_translated(struct sway_keyboard *keyboard,
static size_t keyboard_keysyms_raw(struct sway_keyboard *keyboard,
xkb_keycode_t keycode, const xkb_keysym_t **keysyms,
uint32_t *modifiers) {
struct wlr_input_device *device =
keyboard->seat_device->input_device->wlr_device;
*modifiers = wlr_keyboard_get_modifiers(device->keyboard);
*modifiers = wlr_keyboard_get_modifiers(keyboard->wlr);

xkb_layout_index_t layout_index = xkb_state_key_get_layout(
device->keyboard->xkb_state, keycode);
return xkb_keymap_key_get_syms_by_level(device->keyboard->keymap,
keyboard->wlr->xkb_state, keycode);
return xkb_keymap_key_get_syms_by_level(keyboard->wlr->keymap,
keycode, layout_index, 0, keysyms);
}

Expand Down Expand Up @@ -360,8 +356,7 @@ static void update_keyboard_state(struct sway_keyboard *keyboard,
keyinfo->keycode, &keyinfo->translated_keysyms,
&keyinfo->translated_modifiers);

keyinfo->code_modifiers = wlr_keyboard_get_modifiers(
keyboard->seat_device->input_device->wlr_device->keyboard);
keyinfo->code_modifiers = wlr_keyboard_get_modifiers(keyboard->wlr);

// Update shortcut model keyinfo
update_shortcut_state(&keyboard->state_keycodes, raw_keycode, keystate,
Expand Down Expand Up @@ -407,7 +402,7 @@ static void handle_key_event(struct sway_keyboard *keyboard,
struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;
char *device_identifier = input_device_get_identifier(wlr_device);
bool exact_identifier = wlr_device->keyboard->group != NULL;
bool exact_identifier = keyboard->wlr->group != NULL;
seat_idle_notify_activity(seat, IDLE_SOURCE_KEYBOARD);
bool input_inhibited = seat->exclusive_client != NULL ||
server.session_lock.locked;
Expand Down Expand Up @@ -478,10 +473,10 @@ static void handle_key_event(struct sway_keyboard *keyboard,
// Set up (or clear) keyboard repeat for a pressed binding. Since the
// binding may remove the keyboard, the timer needs to be updated first
if (binding && !(binding->flags & BINDING_NOREPEAT) &&
wlr_device->keyboard->repeat_info.delay > 0) {
keyboard->wlr->repeat_info.delay > 0) {
keyboard->repeat_binding = binding;
if (wl_event_source_timer_update(keyboard->key_repeat_source,
wlr_device->keyboard->repeat_info.delay) < 0) {
keyboard->wlr->repeat_info.delay) < 0) {
sway_log(SWAY_DEBUG, "failed to set key repeat timer");
}
} else if (keyboard->repeat_binding) {
Expand All @@ -493,7 +488,7 @@ static void handle_key_event(struct sway_keyboard *keyboard,
handled = true;
}

if (!handled && wlr_device->keyboard->group) {
if (!handled && keyboard->wlr->group) {
// Only handle device specific bindings for keyboards in a group
free(device_identifier);
return;
Expand All @@ -518,7 +513,7 @@ static void handle_key_event(struct sway_keyboard *keyboard,
&keyboard->state_pressed_sent, event->keycode,
event->state, keyinfo.keycode, 0);
if (pressed_sent) {
wlr_seat_set_keyboard(wlr_seat, wlr_device->keyboard);
wlr_seat_set_keyboard(wlr_seat, keyboard->wlr);
wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,
event->keycode, event->state);
handled = true;
Expand All @@ -529,8 +524,7 @@ static void handle_key_event(struct sway_keyboard *keyboard,
struct wlr_input_method_keyboard_grab_v2 *kb_grab = keyboard_get_im_grab(keyboard);

if (kb_grab) {
wlr_input_method_keyboard_grab_v2_set_keyboard(kb_grab,
wlr_device->keyboard);
wlr_input_method_keyboard_grab_v2_set_keyboard(kb_grab, keyboard->wlr);
wlr_input_method_keyboard_grab_v2_send_key(kb_grab,
event->time_msec, event->keycode, event->state);
handled = true;
Expand All @@ -543,7 +537,7 @@ static void handle_key_event(struct sway_keyboard *keyboard,
update_shortcut_state(
&keyboard->state_pressed_sent, event->keycode, event->state,
keyinfo.keycode, 0);
wlr_seat_set_keyboard(wlr_seat, wlr_device->keyboard);
wlr_seat_set_keyboard(wlr_seat, keyboard->wlr);
wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,
event->keycode, event->state);
}
Expand Down Expand Up @@ -619,14 +613,12 @@ static void handle_keyboard_group_leave(struct wl_listener *listener,
}

static int handle_keyboard_repeat(void *data) {
struct sway_keyboard *keyboard = (struct sway_keyboard *)data;
struct wlr_keyboard *wlr_device =
keyboard->seat_device->input_device->wlr_device->keyboard;
struct sway_keyboard *keyboard = data;
if (keyboard->repeat_binding) {
if (wlr_device->repeat_info.rate > 0) {
if (keyboard->wlr->repeat_info.rate > 0) {
// We queue the next event first, as the command might cancel it
if (wl_event_source_timer_update(keyboard->key_repeat_source,
1000 / wlr_device->repeat_info.rate) < 0) {
1000 / keyboard->wlr->repeat_info.rate) < 0) {
sway_log(SWAY_DEBUG, "failed to update key repeat timer");
}
}
Expand Down Expand Up @@ -659,31 +651,28 @@ static void determine_bar_visibility(uint32_t modifiers) {
}

static void handle_modifier_event(struct sway_keyboard *keyboard) {
struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;
if (!wlr_device->keyboard->group) {
if (!keyboard->wlr->group) {
struct wlr_input_method_keyboard_grab_v2 *kb_grab = keyboard_get_im_grab(keyboard);

if (kb_grab) {
wlr_input_method_keyboard_grab_v2_set_keyboard(kb_grab,
wlr_device->keyboard);
wlr_input_method_keyboard_grab_v2_set_keyboard(kb_grab, keyboard->wlr);
wlr_input_method_keyboard_grab_v2_send_modifiers(kb_grab,
&wlr_device->keyboard->modifiers);
&keyboard->wlr->modifiers);
} else {
struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat;
wlr_seat_set_keyboard(wlr_seat, wlr_device->keyboard);
wlr_seat_set_keyboard(wlr_seat, keyboard->wlr);
wlr_seat_keyboard_notify_modifiers(wlr_seat,
&wlr_device->keyboard->modifiers);
&keyboard->wlr->modifiers);
}

uint32_t modifiers = wlr_keyboard_get_modifiers(wlr_device->keyboard);
uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->wlr);
determine_bar_visibility(modifiers);
}

if (wlr_device->keyboard->modifiers.group != keyboard->effective_layout) {
keyboard->effective_layout = wlr_device->keyboard->modifiers.group;
if (keyboard->wlr->modifiers.group != keyboard->effective_layout) {
keyboard->effective_layout = keyboard->wlr->modifiers.group;

if (!wlr_keyboard_group_from_wlr_keyboard(wlr_device->keyboard)) {
if (!wlr_keyboard_group_from_wlr_keyboard(keyboard->wlr)) {
ipc_event_input("xkb_layout", keyboard->seat_device->input_device);
}
}
Expand Down Expand Up @@ -712,6 +701,7 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
}

keyboard->seat_device = device;
keyboard->wlr = wlr_keyboard_from_input_device(device->input_device->wlr_device);
device->keyboard = keyboard;

wl_list_init(&keyboard->keyboard_key.link);
Expand Down Expand Up @@ -820,13 +810,12 @@ static void destroy_empty_wlr_keyboard_group(void *data) {

static void sway_keyboard_group_remove(struct sway_keyboard *keyboard) {
struct sway_input_device *device = keyboard->seat_device->input_device;
struct wlr_keyboard *wlr_keyboard = device->wlr_device->keyboard;
struct wlr_keyboard_group *wlr_group = wlr_keyboard->group;
struct wlr_keyboard_group *wlr_group = keyboard->wlr->group;

sway_log(SWAY_DEBUG, "Removing keyboard %s from group %p",
device->identifier, wlr_group);

wlr_keyboard_group_remove_keyboard(wlr_keyboard->group, wlr_keyboard);
wlr_keyboard_group_remove_keyboard(keyboard->wlr->group, keyboard->wlr);

if (wl_list_empty(&wlr_group->devices)) {
sway_log(SWAY_DEBUG, "Destroying empty keyboard group %p",
Expand All @@ -851,9 +840,7 @@ static void sway_keyboard_group_remove(struct sway_keyboard *keyboard) {
}

static void sway_keyboard_group_remove_invalid(struct sway_keyboard *keyboard) {
struct sway_input_device *device = keyboard->seat_device->input_device;
struct wlr_keyboard *wlr_keyboard = device->wlr_device->keyboard;
if (!wlr_keyboard->group) {
if (!keyboard->wlr->group) {
return;
}

Expand All @@ -869,7 +856,7 @@ static void sway_keyboard_group_remove_invalid(struct sway_keyboard *keyboard) {
break;
case KEYBOARD_GROUP_DEFAULT: /* fallthrough */
case KEYBOARD_GROUP_SMART:;
struct wlr_keyboard_group *group = wlr_keyboard->group;
struct wlr_keyboard_group *group = keyboard->wlr->group;
if (!wlr_keyboard_keymaps_match(keyboard->keymap, group->keyboard.keymap) ||
!repeat_info_match(keyboard, &group->keyboard)) {
sway_keyboard_group_remove(keyboard);
Expand All @@ -880,7 +867,6 @@ static void sway_keyboard_group_remove_invalid(struct sway_keyboard *keyboard) {

static void sway_keyboard_group_add(struct sway_keyboard *keyboard) {
struct sway_input_device *device = keyboard->seat_device->input_device;
struct wlr_keyboard *wlr_keyboard = device->wlr_device->keyboard;
struct sway_seat *seat = keyboard->seat_device->sway_seat;
struct seat_config *sc = seat_get_config(seat);

Expand Down Expand Up @@ -912,7 +898,7 @@ static void sway_keyboard_group_add(struct sway_keyboard *keyboard) {
repeat_info_match(keyboard, &wlr_group->keyboard)) {
sway_log(SWAY_DEBUG, "Adding keyboard %s to group %p",
device->identifier, wlr_group);
wlr_keyboard_group_add_keyboard(wlr_group, wlr_keyboard);
wlr_keyboard_group_add_keyboard(wlr_group, keyboard->wlr);
return;
}
break;
Expand Down Expand Up @@ -960,7 +946,7 @@ static void sway_keyboard_group_add(struct sway_keyboard *keyboard) {

sway_log(SWAY_DEBUG, "Adding keyboard %s to group %p",
device->identifier, sway_group->wlr_group);
wlr_keyboard_group_add_keyboard(sway_group->wlr_group, wlr_keyboard);
wlr_keyboard_group_add_keyboard(sway_group->wlr_group, keyboard->wlr);

wl_list_insert(&seat->keyboard_groups, &sway_group->link);

Expand Down Expand Up @@ -992,10 +978,8 @@ static void sway_keyboard_group_add(struct sway_keyboard *keyboard) {
void sway_keyboard_configure(struct sway_keyboard *keyboard) {
struct input_config *input_config =
input_device_get_config(keyboard->seat_device->input_device);
struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;

if (!sway_assert(!wlr_keyboard_group_from_wlr_keyboard(wlr_device->keyboard),
if (!sway_assert(!wlr_keyboard_group_from_wlr_keyboard(keyboard->wlr),
"sway_keyboard_configure should not be called with a "
"keyboard group's keyboard")) {
return;
Expand Down Expand Up @@ -1037,11 +1021,11 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) {

sway_keyboard_group_remove_invalid(keyboard);

wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap);
wlr_keyboard_set_repeat_info(wlr_device->keyboard,
wlr_keyboard_set_keymap(keyboard->wlr, keyboard->keymap);
wlr_keyboard_set_repeat_info(keyboard->wlr,
keyboard->repeat_rate, keyboard->repeat_delay);

if (!wlr_device->keyboard->group) {
if (!keyboard->wlr->group) {
sway_keyboard_group_add(keyboard);
}

Expand All @@ -1061,40 +1045,38 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) {
}
}
if (locked_mods) {
wlr_keyboard_notify_modifiers(wlr_device->keyboard, 0, 0,
wlr_keyboard_notify_modifiers(keyboard->wlr, 0, 0,
locked_mods, 0);
uint32_t leds = 0;
for (uint32_t i = 0; i < WLR_LED_COUNT; ++i) {
if (xkb_state_led_index_is_active(
wlr_device->keyboard->xkb_state,
wlr_device->keyboard->led_indexes[i])) {
if (xkb_state_led_index_is_active(keyboard->wlr->xkb_state,
keyboard->wlr->led_indexes[i])) {
leds |= (1 << i);
}
}
if (wlr_device->keyboard->group) {
wlr_keyboard_led_update(
&wlr_device->keyboard->group->keyboard, leds);
if (keyboard->wlr->group) {
wlr_keyboard_led_update(&keyboard->wlr->group->keyboard, leds);
} else {
wlr_keyboard_led_update(wlr_device->keyboard, leds);
wlr_keyboard_led_update(keyboard->wlr, leds);
}
}
} else {
xkb_keymap_unref(keymap);
sway_keyboard_group_remove_invalid(keyboard);
if (!wlr_device->keyboard->group) {
if (!keyboard->wlr->group) {
sway_keyboard_group_add(keyboard);
}
}

struct wlr_seat *seat = keyboard->seat_device->sway_seat->wlr_seat;
wlr_seat_set_keyboard(seat, wlr_device->keyboard);
wlr_seat_set_keyboard(seat, keyboard->wlr);

wl_list_remove(&keyboard->keyboard_key.link);
wl_signal_add(&wlr_device->keyboard->events.key, &keyboard->keyboard_key);
wl_signal_add(&keyboard->wlr->events.key, &keyboard->keyboard_key);
keyboard->keyboard_key.notify = handle_keyboard_key;

wl_list_remove(&keyboard->keyboard_modifiers.link);
wl_signal_add(&wlr_device->keyboard->events.modifiers,
wl_signal_add(&keyboard->wlr->events.modifiers,
&keyboard->keyboard_modifiers);
keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers;

Expand All @@ -1111,12 +1093,11 @@ void sway_keyboard_destroy(struct sway_keyboard *keyboard) {
if (!keyboard) {
return;
}
if (keyboard->seat_device->input_device->wlr_device->keyboard->group) {
if (keyboard->wlr->group) {
sway_keyboard_group_remove(keyboard);
}
struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat;
struct sway_input_device *device = keyboard->seat_device->input_device;
if (wlr_seat_get_keyboard(wlr_seat) == device->wlr_device->keyboard) {
if (wlr_seat_get_keyboard(wlr_seat) == keyboard->wlr) {
wlr_seat_set_keyboard(wlr_seat, NULL);
}
if (keyboard->keymap) {
Expand Down
Loading

0 comments on commit d26cf69

Please sign in to comment.