| 1 |
#include "wayland.h" |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
#include <sys/mman.h> |
| 5 |
#include <sys/timerfd.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
const char *BM_XKB_MASK_NAMES[MASK_LAST] = { |
| 9 |
XKB_MOD_NAME_SHIFT, |
| 10 |
XKB_MOD_NAME_CAPS, |
| 11 |
XKB_MOD_NAME_CTRL, |
| 12 |
XKB_MOD_NAME_ALT, |
| 13 |
"Mod2", |
| 14 |
"Mod3", |
| 15 |
XKB_MOD_NAME_LOGO, |
| 16 |
"Mod5", |
| 17 |
}; |
| 18 |
|
| 19 |
const enum mod_bit BM_XKB_MODS[MASK_LAST] = { |
| 20 |
MOD_SHIFT, |
| 21 |
MOD_CAPS, |
| 22 |
MOD_CTRL, |
| 23 |
MOD_ALT, |
| 24 |
MOD_MOD2, |
| 25 |
MOD_MOD3, |
| 26 |
MOD_LOGO, |
| 27 |
MOD_MOD5 |
| 28 |
}; |
| 29 |
|
| 30 |
static void |
| 31 |
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 32 |
{ |
| 33 |
(void)wl_shm; |
| 34 |
struct wayland *wayland = data; |
| 35 |
wayland->formats |= (1 << format); |
| 36 |
} |
| 37 |
|
| 38 |
struct wl_shm_listener shm_listener = { |
| 39 |
.format = shm_format |
| 40 |
}; |
| 41 |
|
| 42 |
static void |
| 43 |
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) |
| 44 |
{ |
| 45 |
(void)keyboard; |
| 46 |
struct input *input = data; |
| 47 |
|
| 48 |
if (!data) { |
| 49 |
close(fd); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 54 |
close(fd); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
char *map_str; |
| 59 |
if ((map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) { |
| 60 |
close(fd); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
struct xkb_keymap *keymap = xkb_keymap_new_from_string(input->xkb.context, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, 0); |
| 65 |
munmap(map_str, size); |
| 66 |
close(fd); |
| 67 |
|
| 68 |
if (!keymap) { |
| 69 |
fprintf(stderr, "failed to compile keymap\n"); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
struct xkb_state *state; |
| 74 |
if (!(state = xkb_state_new(keymap))) { |
| 75 |
fprintf(stderr, "failed to create XKB state\n"); |
| 76 |
xkb_keymap_unref(keymap); |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
xkb_keymap_unref(input->xkb.keymap); |
| 81 |
xkb_state_unref(input->xkb.state); |
| 82 |
input->xkb.keymap = keymap; |
| 83 |
input->xkb.state = state; |
| 84 |
|
| 85 |
for (uint32_t i = 0; i < MASK_LAST; ++i) |
| 86 |
input->xkb.masks[i] = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, BM_XKB_MASK_NAMES[i]); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) |
| 91 |
{ |
| 92 |
(void)data, (void)keyboard, (void)serial, (void)surface, (void)keys; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) |
| 97 |
{ |
| 98 |
(void)keyboard, (void)serial, (void)surface; |
| 99 |
struct input *input = data; |
| 100 |
struct itimerspec its; |
| 101 |
its.it_interval.tv_sec = 0; |
| 102 |
its.it_interval.tv_nsec = 0; |
| 103 |
its.it_value.tv_sec = 0; |
| 104 |
its.it_value.tv_nsec = 0; |
| 105 |
timerfd_settime(*input->repeat_fd, 0, &its, NULL); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
press(struct input *input, xkb_keysym_t sym, uint32_t key, enum wl_keyboard_key_state state) |
| 110 |
{ |
| 111 |
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 112 |
input->sym = sym; |
| 113 |
input->code = key + 8; |
| 114 |
input->key_pending = true; |
| 115 |
} else if (!input->key_pending) { |
| 116 |
input->sym = XKB_KEY_NoSymbol; |
| 117 |
input->code = 0; |
| 118 |
} |
| 119 |
|
| 120 |
if (input->notify.key) |
| 121 |
input->notify.key(state, sym, key); |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state_w) |
| 126 |
{ |
| 127 |
(void)keyboard, (void)serial, (void)time; |
| 128 |
struct input *input = data; |
| 129 |
enum wl_keyboard_key_state state = state_w; |
| 130 |
|
| 131 |
if (!input->xkb.state) |
| 132 |
return; |
| 133 |
|
| 134 |
xkb_keysym_t sym = xkb_state_key_get_one_sym(input->xkb.state, key + 8); |
| 135 |
press(input, sym, key, state); |
| 136 |
|
| 137 |
if (state == WL_KEYBOARD_KEY_STATE_PRESSED && xkb_keymap_key_repeats(input->xkb.keymap, input->code)) { |
| 138 |
struct itimerspec its; |
| 139 |
input->repeat_sym = sym; |
| 140 |
input->repeat_key = key; |
| 141 |
its.it_interval.tv_sec = input->repeat_rate_sec; |
| 142 |
its.it_interval.tv_nsec = input->repeat_rate_nsec; |
| 143 |
its.it_value.tv_sec = input->repeat_delay_sec; |
| 144 |
its.it_value.tv_nsec = input->repeat_delay_nsec; |
| 145 |
timerfd_settime(*input->repeat_fd, 0, &its, NULL); |
| 146 |
} else if (state == WL_KEYBOARD_KEY_STATE_RELEASED && key == input->repeat_key) { |
| 147 |
struct itimerspec its; |
| 148 |
its.it_interval.tv_sec = 0; |
| 149 |
its.it_interval.tv_nsec = 0; |
| 150 |
its.it_value.tv_sec = 0; |
| 151 |
its.it_value.tv_nsec = 0; |
| 152 |
timerfd_settime(*input->repeat_fd, 0, &its, NULL); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) |
| 158 |
{ |
| 159 |
(void)keyboard, (void)serial; |
| 160 |
struct input *input = data; |
| 161 |
|
| 162 |
if (!input->xkb.keymap) |
| 163 |
return; |
| 164 |
|
| 165 |
xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, mods_locked, 0, 0, group); |
| 166 |
xkb_mod_mask_t mask = xkb_state_serialize_mods(input->xkb.state, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); |
| 167 |
|
| 168 |
input->modifiers = 0; |
| 169 |
for (uint32_t i = 0; i < MASK_LAST; ++i) { |
| 170 |
if (mask & input->xkb.masks[i]) |
| 171 |
input->modifiers |= BM_XKB_MODS[i]; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
set_repeat_info(struct input *input, int32_t rate, int32_t delay) |
| 177 |
{ |
| 178 |
assert(input); |
| 179 |
|
| 180 |
input->repeat_rate_sec = input->repeat_rate_nsec = 0; |
| 181 |
input->repeat_delay_sec = input->repeat_delay_nsec = 0; |
| 182 |
|
| 183 |
/* a rate of zero disables any repeating, regardless of the delay's value */ |
| 184 |
if (rate == 0) |
| 185 |
return; |
| 186 |
|
| 187 |
if (rate == 1) |
| 188 |
input->repeat_rate_sec = 1; |
| 189 |
else |
| 190 |
input->repeat_rate_nsec = 1000000000 / rate; |
| 191 |
|
| 192 |
input->repeat_delay_sec = delay / 1000; |
| 193 |
delay -= (input->repeat_delay_sec * 1000); |
| 194 |
input->repeat_delay_nsec = delay * 1000 * 1000; |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay) |
| 199 |
{ |
| 200 |
(void)keyboard; |
| 201 |
set_repeat_info(data, rate, delay); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, |
| 206 |
uint32_t serial, struct wl_surface *surface, |
| 207 |
wl_fixed_t surface_x, wl_fixed_t surface_y) |
| 208 |
{ |
| 209 |
(void)wl_pointer, (void)surface; |
| 210 |
struct input *input = data; |
| 211 |
input->pointer_event.event_mask |= POINTER_EVENT_ENTER; |
| 212 |
input->pointer_event.serial = serial; |
| 213 |
input->pointer_event.surface_x = surface_x, |
| 214 |
input->pointer_event.surface_y = surface_y; |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, |
| 219 |
uint32_t serial, struct wl_surface *surface) |
| 220 |
{ |
| 221 |
(void)wl_pointer, (void)surface; |
| 222 |
struct input *input = data; |
| 223 |
input->pointer_event.serial = serial; |
| 224 |
input->pointer_event.event_mask |= POINTER_EVENT_LEAVE; |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time, |
| 229 |
wl_fixed_t surface_x, wl_fixed_t surface_y) |
| 230 |
{ |
| 231 |
(void)wl_pointer; |
| 232 |
struct input *input = data; |
| 233 |
input->pointer_event.event_mask |= POINTER_EVENT_MOTION; |
| 234 |
input->pointer_event.time = time; |
| 235 |
input->pointer_event.surface_x = surface_x, |
| 236 |
input->pointer_event.surface_y = surface_y; |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, |
| 241 |
uint32_t time, uint32_t button, uint32_t state) |
| 242 |
{ |
| 243 |
(void)wl_pointer; |
| 244 |
struct input *input = data; |
| 245 |
input->pointer_event.event_mask |= POINTER_EVENT_BUTTON; |
| 246 |
input->pointer_event.time = time; |
| 247 |
input->pointer_event.serial = serial; |
| 248 |
input->pointer_event.button = button, |
| 249 |
input->pointer_event.state |= state; |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, |
| 254 |
uint32_t axis, wl_fixed_t value) |
| 255 |
{ |
| 256 |
(void)wl_pointer; |
| 257 |
struct input *input = data; |
| 258 |
input->pointer_event.event_mask |= POINTER_EVENT_AXIS; |
| 259 |
input->pointer_event.time = time; |
| 260 |
input->pointer_event.axes[axis].valid = true; |
| 261 |
input->pointer_event.axes[axis].value = value; |
| 262 |
} |
| 263 |
|
| 264 |
static void |
| 265 |
pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, |
| 266 |
uint32_t axis_source) |
| 267 |
{ |
| 268 |
(void)wl_pointer; |
| 269 |
struct input *input = data; |
| 270 |
input->pointer_event.event_mask |= POINTER_EVENT_AXIS_SOURCE; |
| 271 |
input->pointer_event.axis_source = axis_source; |
| 272 |
} |
| 273 |
|
| 274 |
static void |
| 275 |
pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer, |
| 276 |
uint32_t time, uint32_t axis) |
| 277 |
{ |
| 278 |
(void)wl_pointer; |
| 279 |
struct input *input = data; |
| 280 |
input->pointer_event.time = time; |
| 281 |
input->pointer_event.event_mask |= POINTER_EVENT_AXIS_STOP; |
| 282 |
input->pointer_event.axes[axis].valid = true; |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer, |
| 287 |
uint32_t axis, int32_t discrete) |
| 288 |
{ |
| 289 |
(void)wl_pointer; |
| 290 |
struct input *input = data; |
| 291 |
input->pointer_event.event_mask |= POINTER_EVENT_AXIS_DISCRETE; |
| 292 |
input->pointer_event.axes[axis].valid = true; |
| 293 |
input->pointer_event.axes[axis].discrete = discrete; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
static void |
| 298 |
pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) |
| 299 |
{ |
| 300 |
(void) data, (void) wl_pointer; |
| 301 |
} |
| 302 |
|
| 303 |
static struct touch_point * |
| 304 |
get_touch_point(struct input *input, int32_t id) |
| 305 |
{ |
| 306 |
struct touch_event *touch = &input->touch_event; |
| 307 |
int invalid = -1; |
| 308 |
for (size_t i = 0; i < 2; ++i) { |
| 309 |
if (touch->points[i].id == id) { |
| 310 |
invalid = i; |
| 311 |
} |
| 312 |
if (invalid == -1 && !touch->points[i].valid) { |
| 313 |
invalid = i; |
| 314 |
} |
| 315 |
} |
| 316 |
if (invalid == -1) { |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
touch->points[invalid].id = id; |
| 320 |
return &touch->points[invalid]; |
| 321 |
} |
| 322 |
|
| 323 |
static void |
| 324 |
reset_all_start_position(struct input *input) |
| 325 |
{ |
| 326 |
struct touch_event *event = &input->touch_event; |
| 327 |
for (size_t i = 0; i < 2; ++i) { |
| 328 |
struct touch_point *point = &event->points[i]; |
| 329 |
|
| 330 |
if (!point->valid) |
| 331 |
continue; |
| 332 |
|
| 333 |
point->surface_start_x = point->surface_x; |
| 334 |
point->surface_start_y = point->surface_y; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
static void |
| 339 |
revalidate_all_released(struct input *input) |
| 340 |
{ |
| 341 |
struct touch_event *event = &input->touch_event; |
| 342 |
for (size_t i = 0; i < 2; ++i) { |
| 343 |
struct touch_point *point = &event->points[i]; |
| 344 |
|
| 345 |
if (!point->valid && point->event_mask & TOUCH_EVENT_DOWN) |
| 346 |
point->valid = true; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
touch_handle_down(void *data, struct wl_touch *wl_touch, uint32_t serial, |
| 352 |
uint32_t time, struct wl_surface *surface, int32_t id, |
| 353 |
wl_fixed_t x, wl_fixed_t y) |
| 354 |
{ |
| 355 |
(void) wl_touch, (void) surface; |
| 356 |
struct input *input = data; |
| 357 |
struct touch_point *point = get_touch_point(input, id); |
| 358 |
if (point == NULL) { |
| 359 |
return; |
| 360 |
} |
| 361 |
point->valid = true; |
| 362 |
point->event_mask = TOUCH_EVENT_DOWN; |
| 363 |
point->surface_x = x, |
| 364 |
point->surface_y = y; |
| 365 |
input->touch_event.time = time; |
| 366 |
input->touch_event.serial = serial; |
| 367 |
input->touch_event.active += 1; |
| 368 |
|
| 369 |
revalidate_all_released(input); |
| 370 |
reset_all_start_position(input); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
touch_handle_up(void *data, struct wl_touch *wl_touch, uint32_t serial, |
| 375 |
uint32_t time, int32_t id) |
| 376 |
{ |
| 377 |
(void) time, (void) wl_touch, (void) serial; |
| 378 |
struct input *input = data; |
| 379 |
struct touch_point *point = get_touch_point(input, id); |
| 380 |
if (point == NULL) { |
| 381 |
return; |
| 382 |
} |
| 383 |
point->event_mask |= TOUCH_EVENT_UP; |
| 384 |
input->touch_event.active -= 1; |
| 385 |
|
| 386 |
reset_all_start_position(input); |
| 387 |
} |
| 388 |
|
| 389 |
static void |
| 390 |
touch_handle_motion(void *data, struct wl_touch *wl_touch, uint32_t time, |
| 391 |
int32_t id, wl_fixed_t x, wl_fixed_t y) |
| 392 |
{ |
| 393 |
(void) wl_touch; |
| 394 |
struct input *input = data; |
| 395 |
struct touch_point *point = get_touch_point(input, id); |
| 396 |
if (point == NULL) { |
| 397 |
return; |
| 398 |
} |
| 399 |
point->event_mask |= TOUCH_EVENT_MOTION; |
| 400 |
point->surface_x = x, point->surface_y = y; |
| 401 |
input->touch_event.time = time; |
| 402 |
} |
| 403 |
|
| 404 |
static void |
| 405 |
touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 406 |
{ |
| 407 |
(void) wl_touch, (void) data; |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
touch_handle_shape(void *data, struct wl_touch *wl_touch, |
| 412 |
int32_t id, wl_fixed_t major, wl_fixed_t minor) |
| 413 |
{ |
| 414 |
(void) wl_touch; |
| 415 |
struct input *input = data; |
| 416 |
struct touch_point *point = get_touch_point(input, id); |
| 417 |
if (point == NULL) { |
| 418 |
return; |
| 419 |
} |
| 420 |
point->event_mask |= TOUCH_EVENT_SHAPE; |
| 421 |
point->major = major, point->minor = minor; |
| 422 |
} |
| 423 |
|
| 424 |
static void |
| 425 |
touch_handle_orientation(void *data, struct wl_touch *wl_touch, |
| 426 |
int32_t id, wl_fixed_t orientation) |
| 427 |
{ |
| 428 |
(void) wl_touch; |
| 429 |
struct input *input = data; |
| 430 |
struct touch_point *point = get_touch_point(input, id); |
| 431 |
if (point == NULL) { |
| 432 |
return; |
| 433 |
} |
| 434 |
point->event_mask |= TOUCH_EVENT_ORIENTATION; |
| 435 |
point->orientation = orientation; |
| 436 |
} |
| 437 |
|
| 438 |
static void |
| 439 |
touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 440 |
{ |
| 441 |
(void) data, (void) wl_touch; |
| 442 |
} |
| 443 |
|
| 444 |
static const struct wl_pointer_listener pointer_listener = { |
| 445 |
.enter = pointer_handle_enter, |
| 446 |
.leave = pointer_handle_leave, |
| 447 |
.motion = pointer_handle_motion, |
| 448 |
.button = pointer_handle_button, |
| 449 |
.axis = pointer_handle_axis, |
| 450 |
.frame = pointer_handle_frame, |
| 451 |
.axis_source = pointer_handle_axis_source, |
| 452 |
.axis_stop = pointer_handle_axis_stop, |
| 453 |
.axis_discrete = pointer_handle_axis_discrete, |
| 454 |
}; |
| 455 |
|
| 456 |
static const struct wl_keyboard_listener keyboard_listener = { |
| 457 |
.keymap = keyboard_handle_keymap, |
| 458 |
.enter = keyboard_handle_enter, |
| 459 |
.leave = keyboard_handle_leave, |
| 460 |
.key = keyboard_handle_key, |
| 461 |
.modifiers = keyboard_handle_modifiers, |
| 462 |
.repeat_info = keyboard_handle_repeat_info |
| 463 |
}; |
| 464 |
|
| 465 |
static const struct wl_touch_listener wl_touch_listener = { |
| 466 |
.down = touch_handle_down, |
| 467 |
.up = touch_handle_up, |
| 468 |
.motion = touch_handle_motion, |
| 469 |
.frame = touch_handle_frame, |
| 470 |
.cancel = touch_handle_cancel, |
| 471 |
.shape = touch_handle_shape, |
| 472 |
.orientation = touch_handle_orientation, |
| 473 |
}; |
| 474 |
|
| 475 |
static void |
| 476 |
seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) |
| 477 |
{ |
| 478 |
struct input *input = data; |
| 479 |
|
| 480 |
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 481 |
input->keyboard = wl_seat_get_keyboard(seat); |
| 482 |
wl_keyboard_add_listener(input->keyboard, &keyboard_listener, data); |
| 483 |
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 484 |
wl_keyboard_destroy(input->keyboard); |
| 485 |
input->keyboard = NULL; |
| 486 |
} |
| 487 |
|
| 488 |
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 489 |
input->pointer = wl_seat_get_pointer(seat); |
| 490 |
wl_pointer_add_listener(input->pointer, &pointer_listener, data); |
| 491 |
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 492 |
wl_pointer_destroy(input->pointer); |
| 493 |
input->pointer = NULL; |
| 494 |
} |
| 495 |
|
| 496 |
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) { |
| 497 |
input->touch = wl_seat_get_touch(seat); |
| 498 |
wl_touch_add_listener(input->touch, &wl_touch_listener, data); |
| 499 |
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) { |
| 500 |
wl_touch_destroy(input->touch); |
| 501 |
input->touch = NULL; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
static void |
| 506 |
seat_handle_name(void *data, struct wl_seat *seat, const char *name) |
| 507 |
{ |
| 508 |
(void)data, (void)seat, (void)name; |
| 509 |
} |
| 510 |
|
| 511 |
static const struct wl_seat_listener seat_listener = { |
| 512 |
.capabilities = seat_handle_capabilities, |
| 513 |
.name = seat_handle_name |
| 514 |
}; |
| 515 |
|
| 516 |
static void |
| 517 |
display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, const char *model, int transform) |
| 518 |
{ |
| 519 |
(void)data, (void)wl_output, (void)x, (void)y, (void)physical_width, (void)physical_height, (void)subpixel, (void)make, (void)model, (void)transform; |
| 520 |
} |
| 521 |
|
| 522 |
static void |
| 523 |
display_handle_done(void *data, struct wl_output *wl_output) |
| 524 |
{ |
| 525 |
(void)data, (void)wl_output; |
| 526 |
} |
| 527 |
|
| 528 |
static void |
| 529 |
display_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) |
| 530 |
{ |
| 531 |
(void)wl_output; |
| 532 |
struct output *output = data; |
| 533 |
|
| 534 |
assert(scale > 0); |
| 535 |
output->scale = scale; |
| 536 |
} |
| 537 |
|
| 538 |
static void |
| 539 |
display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh) |
| 540 |
{ |
| 541 |
(void)wl_output, (void)refresh, (void)height, (void)width; |
| 542 |
struct output *output = data; |
| 543 |
|
| 544 |
if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 545 |
output->height = height; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
static void |
| 550 |
display_handle_name(void *data, struct wl_output *wl_output, const char *name) |
| 551 |
{ |
| 552 |
(void)wl_output; |
| 553 |
struct output *output = data; |
| 554 |
|
| 555 |
output->name = bm_strdup(name); |
| 556 |
} |
| 557 |
|
| 558 |
static void |
| 559 |
display_handle_description(void *data, struct wl_output *wl_output, const char *description) |
| 560 |
{ |
| 561 |
(void)data, (void)wl_output, (void)description; |
| 562 |
} |
| 563 |
|
| 564 |
static const struct wl_output_listener output_listener = { |
| 565 |
.geometry = display_handle_geometry, |
| 566 |
.mode = display_handle_mode, |
| 567 |
.done = display_handle_done, |
| 568 |
.scale = display_handle_scale, |
| 569 |
.name = display_handle_name, |
| 570 |
.description = display_handle_description, |
| 571 |
}; |
| 572 |
|
| 573 |
static void |
| 574 |
registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version) |
| 575 |
{ |
| 576 |
(void)version; |
| 577 |
struct wayland *wayland = data; |
| 578 |
|
| 579 |
if (strcmp(interface, "wl_compositor") == 0) { |
| 580 |
wayland->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 4); |
| 581 |
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { |
| 582 |
wayland->layer_shell = wl_registry_bind(registry, id, &zwlr_layer_shell_v1_interface, 3); |
| 583 |
} else if (strcmp(interface, "wl_seat") == 0) { |
| 584 |
wayland->seat = wl_registry_bind(registry, id, &wl_seat_interface, 7); |
| 585 |
wl_seat_add_listener(wayland->seat, &seat_listener, &wayland->input); |
| 586 |
} else if (strcmp(interface, "wl_shm") == 0) { |
| 587 |
wayland->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1); |
| 588 |
wl_shm_add_listener(wayland->shm, &shm_listener, data); |
| 589 |
} else if (strcmp(interface, "wl_output") == 0) { |
| 590 |
struct wl_output *wl_output = wl_registry_bind(registry, id, &wl_output_interface, 4); |
| 591 |
struct output *output = calloc(1, sizeof(struct output)); |
| 592 |
output->output = wl_output; |
| 593 |
wl_list_insert(&wayland->outputs, &output->link); |
| 594 |
wl_output_add_listener(wl_output, &output_listener, output); |
| 595 |
} else if (strcmp(interface, "wp_fractional_scale_manager_v1") == 0) { |
| 596 |
wayland->wfs_mgr = wl_registry_bind(registry, id, &wp_fractional_scale_manager_v1_interface, 1); |
| 597 |
} else if (strcmp(interface, "wp_viewporter") == 0) { |
| 598 |
wayland->viewporter = wl_registry_bind(registry, id, &wp_viewporter_interface, 1); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
static void |
| 603 |
registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) |
| 604 |
{ |
| 605 |
(void)data, (void)registry, (void)name; |
| 606 |
} |
| 607 |
|
| 608 |
static const struct wl_registry_listener registry_listener = { |
| 609 |
registry_handle_global, |
| 610 |
registry_handle_global_remove |
| 611 |
}; |
| 612 |
|
| 613 |
void |
| 614 |
bm_wl_repeat(struct wayland *wayland) |
| 615 |
{ |
| 616 |
uint64_t exp; |
| 617 |
if (read(wayland->fds.repeat, &exp, sizeof(exp)) != sizeof(exp)) |
| 618 |
return; |
| 619 |
|
| 620 |
if (wayland->input.notify.key) |
| 621 |
wayland->input.notify.key(WL_KEYBOARD_KEY_STATE_PRESSED, wayland->input.repeat_sym, wayland->input.repeat_key + 8); |
| 622 |
|
| 623 |
press(&wayland->input, wayland->input.repeat_sym, wayland->input.repeat_key, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 624 |
} |
| 625 |
|
| 626 |
void |
| 627 |
bm_wl_registry_destroy(struct wayland *wayland) |
| 628 |
{ |
| 629 |
assert(wayland); |
| 630 |
|
| 631 |
if (wayland->input.keyboard) |
| 632 |
wl_keyboard_destroy(wayland->input.keyboard); |
| 633 |
|
| 634 |
if (wayland->input.pointer) |
| 635 |
wl_pointer_destroy(wayland->input.pointer); |
| 636 |
|
| 637 |
if (wayland->input.touch) |
| 638 |
wl_touch_destroy(wayland->input.touch); |
| 639 |
|
| 640 |
if (wayland->viewporter) |
| 641 |
wp_viewporter_destroy(wayland->viewporter); |
| 642 |
|
| 643 |
if (wayland->wfs_mgr) |
| 644 |
wp_fractional_scale_manager_v1_destroy(wayland->wfs_mgr); |
| 645 |
|
| 646 |
struct output *output, *output_tmp; |
| 647 |
wl_list_for_each_safe(output, output_tmp, &wayland->outputs, link) { |
| 648 |
wl_list_remove(&output->link); |
| 649 |
wl_output_destroy(output->output); |
| 650 |
free(output->name); |
| 651 |
free(output); |
| 652 |
} |
| 653 |
|
| 654 |
if (wayland->shm) |
| 655 |
wl_shm_destroy(wayland->shm); |
| 656 |
|
| 657 |
if (wayland->seat) |
| 658 |
wl_seat_destroy(wayland->seat); |
| 659 |
|
| 660 |
if (wayland->layer_shell) |
| 661 |
zwlr_layer_shell_v1_destroy(wayland->layer_shell); |
| 662 |
|
| 663 |
if (wayland->compositor) |
| 664 |
wl_compositor_destroy(wayland->compositor); |
| 665 |
|
| 666 |
if (wayland->registry) |
| 667 |
wl_registry_destroy(wayland->registry); |
| 668 |
|
| 669 |
xkb_keymap_unref(wayland->input.xkb.keymap); |
| 670 |
xkb_state_unref(wayland->input.xkb.state); |
| 671 |
} |
| 672 |
|
| 673 |
bool |
| 674 |
bm_wl_registry_register(struct wayland *wayland) |
| 675 |
{ |
| 676 |
assert(wayland); |
| 677 |
|
| 678 |
if (!(wayland->registry = wl_display_get_registry(wayland->display))) |
| 679 |
return false; |
| 680 |
|
| 681 |
wl_registry_add_listener(wayland->registry, ®istry_listener, wayland); |
| 682 |
wl_display_roundtrip(wayland->display); // trip 1, registry globals |
| 683 |
if (!wayland->compositor || !wayland->seat || !wayland->shm || !wayland->layer_shell) |
| 684 |
return false; |
| 685 |
|
| 686 |
wl_display_roundtrip(wayland->display); // trip 2, global listeners |
| 687 |
if (!wayland->input.keyboard || !(wayland->formats & (1 << WL_SHM_FORMAT_ARGB8888))) |
| 688 |
return false; |
| 689 |
|
| 690 |
if (wayland->wfs_mgr && wayland->viewporter) { |
| 691 |
// This feature does not work with some compositors rn so mut be enabled with env var |
| 692 |
const char *env = getenv("BEMENU_WL_FRACTIONAL_SCALING"); |
| 693 |
if (env && (!strcmp(env, "1") || !strcmp(env, "true"))) { |
| 694 |
wayland->fractional_scaling = true; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
set_repeat_info(&wayland->input, 40, 400); |
| 699 |
return true; |
| 700 |
} |
| 701 |
|
| 702 |
/* vim: set ts=8 sw=4 tw=0 :*/ |