| 1 |
#include "internal.h" |
| 2 |
#include "wayland.h" |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#include <sys/epoll.h> |
| 12 |
#include <sys/timerfd.h> |
| 13 |
|
| 14 |
static int efd; |
| 15 |
|
| 16 |
static void |
| 17 |
render_windows_if_pending(struct bm_menu *menu, struct wayland *wayland) { |
| 18 |
struct window *window; |
| 19 |
wl_list_for_each(window, &wayland->windows, link) { |
| 20 |
if (window->render_pending) |
| 21 |
bm_wl_window_render(window, wayland->display, menu); |
| 22 |
} |
| 23 |
wl_display_flush(wayland->display); |
| 24 |
} |
| 25 |
|
| 26 |
static bool |
| 27 |
wait_for_events(struct wayland *wayland) { |
| 28 |
wl_display_dispatch_pending(wayland->display); |
| 29 |
|
| 30 |
if (wl_display_flush(wayland->display) < 0 && errno != EAGAIN) |
| 31 |
return false; |
| 32 |
|
| 33 |
struct epoll_event ep[16]; |
| 34 |
uint32_t num = epoll_wait(efd, ep, 16, -1); |
| 35 |
for (uint32_t i = 0; i < num; ++i) { |
| 36 |
if (ep[i].data.ptr == &wayland->fds.display) { |
| 37 |
if (ep[i].events & EPOLLERR || ep[i].events & EPOLLHUP || |
| 38 |
((ep[i].events & EPOLLIN) && wl_display_dispatch(wayland->display) < 0)) |
| 39 |
return false; |
| 40 |
} else if (ep[i].data.ptr == &wayland->fds.repeat) { |
| 41 |
bm_wl_repeat(wayland); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
schedule_windows_render_if_dirty(struct bm_menu *menu, struct wayland *wayland) { |
| 50 |
struct window *window; |
| 51 |
wl_list_for_each(window, &wayland->windows, link) { |
| 52 |
if (window->render_pending) { |
| 53 |
// This does not happen during normal execution, but only when the windows need to |
| 54 |
// be(re)created. We need to do the render ASAP (not schedule it) because otherwise, |
| 55 |
// since we lack a window, we may not receive further events and will get deadlocked |
| 56 |
render_windows_if_pending(menu, wayland); |
| 57 |
} |
| 58 |
if (menu->dirty) { |
| 59 |
bm_wl_window_schedule_render(window); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
menu->dirty = false; |
| 64 |
} |
| 65 |
|
| 66 |
static bool |
| 67 |
render(struct bm_menu *menu) |
| 68 |
{ |
| 69 |
struct wayland *wayland = menu->renderer->internal; |
| 70 |
|
| 71 |
schedule_windows_render_if_dirty(menu, wayland); |
| 72 |
if (!wait_for_events(wayland)) |
| 73 |
return false; |
| 74 |
render_windows_if_pending(menu, wayland); |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
static enum bm_key |
| 80 |
poll_key(const struct bm_menu *menu, unsigned int *unicode) |
| 81 |
{ |
| 82 |
struct wayland *wayland = menu->renderer->internal; |
| 83 |
assert(wayland && unicode); |
| 84 |
*unicode = 0; |
| 85 |
|
| 86 |
if (wayland->input.sym == XKB_KEY_NoSymbol) |
| 87 |
return BM_KEY_NONE; |
| 88 |
|
| 89 |
xkb_keysym_t sym = wayland->input.sym; |
| 90 |
uint32_t mods = wayland->input.modifiers; |
| 91 |
*unicode = xkb_state_key_get_utf32(wayland->input.xkb.state, wayland->input.code); |
| 92 |
|
| 93 |
wayland->input.sym = XKB_KEY_NoSymbol; |
| 94 |
wayland->input.code = 0; |
| 95 |
|
| 96 |
if (!wayland->input.key_pending) |
| 97 |
return BM_KEY_UNICODE; |
| 98 |
wayland->input.key_pending = false; |
| 99 |
|
| 100 |
switch (sym) { |
| 101 |
case XKB_KEY_Up: |
| 102 |
return BM_KEY_UP; |
| 103 |
|
| 104 |
case XKB_KEY_Down: |
| 105 |
return BM_KEY_DOWN; |
| 106 |
|
| 107 |
case XKB_KEY_Left: |
| 108 |
return (mods & MOD_SHIFT ? BM_KEY_UP : BM_KEY_LEFT); |
| 109 |
|
| 110 |
case XKB_KEY_Right: |
| 111 |
return (mods & MOD_SHIFT ? BM_KEY_DOWN : BM_KEY_RIGHT); |
| 112 |
|
| 113 |
case XKB_KEY_Home: |
| 114 |
return BM_KEY_HOME; |
| 115 |
|
| 116 |
case XKB_KEY_End: |
| 117 |
return BM_KEY_END; |
| 118 |
|
| 119 |
case XKB_KEY_SunPageUp: |
| 120 |
return (mods & MOD_SHIFT ? BM_KEY_SHIFT_PAGE_UP : BM_KEY_PAGE_UP); |
| 121 |
|
| 122 |
case XKB_KEY_less: |
| 123 |
return (mods & MOD_ALT ? BM_KEY_SHIFT_PAGE_UP : BM_KEY_UNICODE); |
| 124 |
|
| 125 |
case XKB_KEY_SunPageDown: |
| 126 |
return (mods & MOD_SHIFT ? BM_KEY_SHIFT_PAGE_DOWN : BM_KEY_PAGE_DOWN); |
| 127 |
|
| 128 |
case XKB_KEY_greater: |
| 129 |
return (mods & MOD_ALT ? BM_KEY_SHIFT_PAGE_DOWN : BM_KEY_UNICODE); |
| 130 |
|
| 131 |
case XKB_KEY_v: |
| 132 |
return (mods & MOD_CTRL ? BM_KEY_PAGE_DOWN : (mods & MOD_ALT ? BM_KEY_PAGE_UP : BM_KEY_UNICODE)); |
| 133 |
|
| 134 |
case XKB_KEY_BackSpace: |
| 135 |
return BM_KEY_BACKSPACE; |
| 136 |
|
| 137 |
case XKB_KEY_Delete: |
| 138 |
return (mods & MOD_SHIFT ? BM_KEY_LINE_DELETE_LEFT : BM_KEY_DELETE); |
| 139 |
|
| 140 |
case XKB_KEY_ISO_Left_Tab: //shift tab gives this sym |
| 141 |
case XKB_KEY_Tab: |
| 142 |
return (mods & MOD_SHIFT ? BM_KEY_SHIFT_TAB : BM_KEY_TAB); |
| 143 |
|
| 144 |
case XKB_KEY_Insert: |
| 145 |
return BM_KEY_SHIFT_RETURN; |
| 146 |
|
| 147 |
case XKB_KEY_KP_Enter: |
| 148 |
case XKB_KEY_Return: |
| 149 |
return (mods & MOD_CTRL ? BM_KEY_CONTROL_RETURN : (mods & MOD_SHIFT ? BM_KEY_SHIFT_RETURN : BM_KEY_RETURN)); |
| 150 |
|
| 151 |
case XKB_KEY_g: |
| 152 |
if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE; |
| 153 |
// fall through |
| 154 |
case XKB_KEY_c: |
| 155 |
if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE; |
| 156 |
// fall through |
| 157 |
case XKB_KEY_bracketleft: |
| 158 |
if (!(mods & MOD_CTRL)) return BM_KEY_UNICODE; |
| 159 |
// fall through |
| 160 |
case XKB_KEY_Escape: |
| 161 |
return BM_KEY_ESCAPE; |
| 162 |
|
| 163 |
case XKB_KEY_p: |
| 164 |
return (mods & MOD_CTRL ? BM_KEY_UP : BM_KEY_UNICODE); |
| 165 |
|
| 166 |
case XKB_KEY_n: |
| 167 |
return (mods & MOD_CTRL ? BM_KEY_DOWN : BM_KEY_UNICODE); |
| 168 |
|
| 169 |
case XKB_KEY_l: |
| 170 |
return (mods & MOD_CTRL ? BM_KEY_LEFT : (mods & MOD_ALT ? BM_KEY_DOWN : BM_KEY_UNICODE)); |
| 171 |
|
| 172 |
case XKB_KEY_b: |
| 173 |
return (mods & MOD_CTRL ? BM_KEY_LEFT : BM_KEY_UNICODE); |
| 174 |
|
| 175 |
case XKB_KEY_f: |
| 176 |
return (mods & MOD_CTRL ? BM_KEY_RIGHT : BM_KEY_UNICODE); |
| 177 |
|
| 178 |
case XKB_KEY_a: |
| 179 |
return (mods & MOD_CTRL ? BM_KEY_HOME : BM_KEY_UNICODE); |
| 180 |
|
| 181 |
case XKB_KEY_e: |
| 182 |
return (mods & MOD_CTRL ? BM_KEY_END : BM_KEY_UNICODE); |
| 183 |
|
| 184 |
case XKB_KEY_h: |
| 185 |
return (mods & MOD_CTRL ? BM_KEY_BACKSPACE : (mods & MOD_ALT ? BM_KEY_UP : BM_KEY_UNICODE)); |
| 186 |
|
| 187 |
case XKB_KEY_u: |
| 188 |
return (mods & MOD_CTRL ? BM_KEY_LINE_DELETE_LEFT : (mods & MOD_ALT ? BM_KEY_PAGE_UP : BM_KEY_UNICODE)); |
| 189 |
|
| 190 |
case XKB_KEY_k: |
| 191 |
return (mods & MOD_CTRL ? BM_KEY_LINE_DELETE_RIGHT : (mods & MOD_ALT ? BM_KEY_UP : BM_KEY_UNICODE)); |
| 192 |
|
| 193 |
case XKB_KEY_w: |
| 194 |
return (mods & MOD_CTRL ? BM_KEY_WORD_DELETE : BM_KEY_UNICODE); |
| 195 |
|
| 196 |
case XKB_KEY_j: |
| 197 |
return (mods & MOD_ALT ? BM_KEY_DOWN : BM_KEY_UNICODE); |
| 198 |
|
| 199 |
case XKB_KEY_d: |
| 200 |
return (mods & MOD_ALT ? BM_KEY_PAGE_DOWN : BM_KEY_UNICODE); |
| 201 |
|
| 202 |
case XKB_KEY_m: |
| 203 |
return (mods & MOD_CTRL ? BM_KEY_RETURN : BM_KEY_UNICODE); |
| 204 |
|
| 205 |
case XKB_KEY_y: |
| 206 |
return (mods & MOD_CTRL ? BM_KEY_PASTE_PRIMARY : BM_KEY_UNICODE); |
| 207 |
|
| 208 |
case XKB_KEY_Y: |
| 209 |
return (mods & MOD_CTRL ? BM_KEY_PASTE_CLIPBOARD : BM_KEY_UNICODE); |
| 210 |
|
| 211 |
case XKB_KEY_1: |
| 212 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_1; |
| 213 |
break; |
| 214 |
case XKB_KEY_2: |
| 215 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_2; |
| 216 |
break; |
| 217 |
case XKB_KEY_3: |
| 218 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_3; |
| 219 |
break; |
| 220 |
case XKB_KEY_4: |
| 221 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_4; |
| 222 |
break; |
| 223 |
case XKB_KEY_5: |
| 224 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_5; |
| 225 |
break; |
| 226 |
case XKB_KEY_6: |
| 227 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_6; |
| 228 |
break; |
| 229 |
case XKB_KEY_7: |
| 230 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_7; |
| 231 |
break; |
| 232 |
case XKB_KEY_8: |
| 233 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_8; |
| 234 |
break; |
| 235 |
case XKB_KEY_9: |
| 236 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_9; |
| 237 |
break; |
| 238 |
case XKB_KEY_0: |
| 239 |
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_10; |
| 240 |
break; |
| 241 |
|
| 242 |
default: break; |
| 243 |
} |
| 244 |
|
| 245 |
return BM_KEY_UNICODE; |
| 246 |
} |
| 247 |
|
| 248 |
struct bm_pointer |
| 249 |
poll_pointer(const struct bm_menu *menu) |
| 250 |
{ |
| 251 |
struct wayland *wayland = menu->renderer->internal; |
| 252 |
struct input *input = &wayland->input; |
| 253 |
struct pointer_event *event = &input->pointer_event; |
| 254 |
assert(wayland && event); |
| 255 |
|
| 256 |
struct bm_pointer bm_pointer = {0}; |
| 257 |
|
| 258 |
bm_pointer.event_mask = event->event_mask; |
| 259 |
bm_pointer.pos_x = wl_fixed_to_int(event->surface_x); |
| 260 |
bm_pointer.pos_y = wl_fixed_to_int(event->surface_y); |
| 261 |
bm_pointer.time = event->time; |
| 262 |
bm_pointer.axes[0].valid = event->axes[0].valid; |
| 263 |
bm_pointer.axes[0].value = event->axes[0].value; |
| 264 |
bm_pointer.axes[0].discrete = event->axes[0].discrete; |
| 265 |
bm_pointer.axes[1].valid = event->axes[1].valid; |
| 266 |
bm_pointer.axes[1].value = event->axes[1].value; |
| 267 |
bm_pointer.axes[1].discrete = event->axes[1].discrete; |
| 268 |
bm_pointer.axis_source = event->axis_source; |
| 269 |
|
| 270 |
bm_pointer.button = BM_POINTER_KEY_NONE; |
| 271 |
switch (event->button) { |
| 272 |
case BTN_LEFT: |
| 273 |
bm_pointer.button = BM_POINTER_KEY_PRIMARY; |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
if (event->state & WL_POINTER_BUTTON_STATE_PRESSED) { |
| 278 |
bm_pointer.state = POINTER_STATE_PRESSED; |
| 279 |
} |
| 280 |
if (event->state & WL_POINTER_BUTTON_STATE_RELEASED) { |
| 281 |
bm_pointer.state = POINTER_STATE_RELEASED; |
| 282 |
} |
| 283 |
|
| 284 |
memset(event, 0, sizeof(*event)); |
| 285 |
return bm_pointer; |
| 286 |
} |
| 287 |
|
| 288 |
struct bm_touch |
| 289 |
poll_touch(const struct bm_menu *menu) |
| 290 |
{ |
| 291 |
struct wayland *wayland = menu->renderer->internal; |
| 292 |
struct input *input = &wayland->input; |
| 293 |
struct touch_event *event = &input->touch_event; |
| 294 |
assert(wayland && event); |
| 295 |
|
| 296 |
struct bm_touch bm_touch; |
| 297 |
|
| 298 |
for (size_t i = 0; i < 2; ++i) { |
| 299 |
struct touch_point *point = &event->points[i]; |
| 300 |
|
| 301 |
if (!point->valid) { |
| 302 |
bm_touch.points[i].event_mask = 0; |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
bm_touch.points[i].event_mask = point->event_mask; |
| 307 |
bm_touch.points[i].pos_x = wl_fixed_to_int(point->surface_x); |
| 308 |
bm_touch.points[i].pos_y = wl_fixed_to_int(point->surface_y); |
| 309 |
bm_touch.points[i].start_x = wl_fixed_to_int(point->surface_start_x); |
| 310 |
bm_touch.points[i].start_y = wl_fixed_to_int(point->surface_start_y); |
| 311 |
bm_touch.points[i].major = point->major; |
| 312 |
bm_touch.points[i].minor = point->minor; |
| 313 |
bm_touch.points[i].orientation = point->orientation; |
| 314 |
|
| 315 |
if (point->event_mask & TOUCH_EVENT_UP) { |
| 316 |
point->valid = false; |
| 317 |
point->event_mask = 0; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return bm_touch; |
| 322 |
} |
| 323 |
|
| 324 |
void |
| 325 |
release_touch(const struct bm_menu *menu) |
| 326 |
{ |
| 327 |
struct wayland *wayland = menu->renderer->internal; |
| 328 |
struct input *input = &wayland->input; |
| 329 |
struct touch_event *event = &input->touch_event; |
| 330 |
assert(wayland && event); |
| 331 |
|
| 332 |
for (size_t i = 0; i < 2; ++i) { |
| 333 |
struct touch_point *point = &event->points[i]; |
| 334 |
point->valid = false; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
static uint32_t |
| 339 |
get_displayed_count(const struct bm_menu *menu) |
| 340 |
{ |
| 341 |
struct wayland *wayland = menu->renderer->internal; |
| 342 |
assert(wayland); |
| 343 |
uint32_t max = 0; |
| 344 |
struct window *window; |
| 345 |
wl_list_for_each(window, &wayland->windows, link) { |
| 346 |
if (window->displayed > max) |
| 347 |
max = window->displayed; |
| 348 |
} |
| 349 |
return max; |
| 350 |
} |
| 351 |
|
| 352 |
static uint32_t |
| 353 |
get_height(const struct bm_menu *menu) |
| 354 |
{ |
| 355 |
struct wayland *wayland = menu->renderer->internal; |
| 356 |
assert(wayland); |
| 357 |
uint32_t max = 0; |
| 358 |
struct window *window; |
| 359 |
wl_list_for_each(window, &wayland->windows, link) { |
| 360 |
if (window->displayed > max) |
| 361 |
max = window->height; |
| 362 |
} |
| 363 |
return max; |
| 364 |
} |
| 365 |
|
| 366 |
static uint32_t |
| 367 |
get_width(const struct bm_menu *menu) |
| 368 |
{ |
| 369 |
struct wayland *wayland = menu->renderer->internal; |
| 370 |
assert(wayland); |
| 371 |
uint32_t max = 0; |
| 372 |
struct window *window; |
| 373 |
wl_list_for_each(window, &wayland->windows, link) { |
| 374 |
if (window->displayed > max) |
| 375 |
max = window->width; |
| 376 |
} |
| 377 |
return max; |
| 378 |
} |
| 379 |
|
| 380 |
static void |
| 381 |
set_width(const struct bm_menu *menu, uint32_t margin, float factor) |
| 382 |
{ |
| 383 |
struct wayland *wayland = menu->renderer->internal; |
| 384 |
assert(wayland); |
| 385 |
|
| 386 |
struct window *window; |
| 387 |
wl_list_for_each(window, &wayland->windows, link) { |
| 388 |
bm_wl_window_set_width(window, wayland->display, margin, factor); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
static void |
| 393 |
set_align(const struct bm_menu *menu, enum bm_align align) |
| 394 |
{ |
| 395 |
struct wayland *wayland = menu->renderer->internal; |
| 396 |
assert(wayland); |
| 397 |
|
| 398 |
struct window *window; |
| 399 |
wl_list_for_each(window, &wayland->windows, link) { |
| 400 |
bm_wl_window_set_align(window, wayland->display, align); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
static void |
| 405 |
set_y_offset(const struct bm_menu *menu, int32_t y_offset) |
| 406 |
{ |
| 407 |
struct wayland *wayland = menu->renderer->internal; |
| 408 |
assert(wayland); |
| 409 |
|
| 410 |
struct window *window; |
| 411 |
wl_list_for_each(window, &wayland->windows, link) { |
| 412 |
bm_wl_window_set_y_offset(window, wayland->display, y_offset); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
static void |
| 417 |
grab_keyboard(const struct bm_menu *menu, bool grab) |
| 418 |
{ |
| 419 |
struct wayland *wayland = menu->renderer->internal; |
| 420 |
assert(wayland); |
| 421 |
|
| 422 |
struct window *window; |
| 423 |
wl_list_for_each(window, &wayland->windows, link) { |
| 424 |
bm_wl_window_grab_keyboard(window, wayland->display, grab); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
static void |
| 429 |
set_overlap(const struct bm_menu *menu, bool overlap) |
| 430 |
{ |
| 431 |
struct wayland *wayland = menu->renderer->internal; |
| 432 |
assert(wayland); |
| 433 |
|
| 434 |
struct window *window; |
| 435 |
wl_list_for_each(window, &wayland->windows, link) { |
| 436 |
bm_wl_window_set_overlap(window, wayland->display, overlap); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void |
| 441 |
wl_surface_enter(void *data, struct wl_surface *wl_surface, |
| 442 |
struct wl_output *wl_output) |
| 443 |
{ |
| 444 |
(void)wl_surface; |
| 445 |
struct window *window = data; |
| 446 |
struct wayland *wayland = window->wayland; |
| 447 |
|
| 448 |
struct output *output; |
| 449 |
wl_list_for_each(output, &wayland->outputs, link) { |
| 450 |
if (output->output == wl_output) { |
| 451 |
struct surf_output *surf_output = calloc(1, sizeof(struct surf_output)); |
| 452 |
surf_output->output = output; |
| 453 |
wl_list_insert(&window->surf_outputs, &surf_output->link); |
| 454 |
break; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
window_update_output(window); |
| 459 |
} |
| 460 |
|
| 461 |
void |
| 462 |
wl_surface_leave(void *data, struct wl_surface *wl_surface, |
| 463 |
struct wl_output *wl_output) |
| 464 |
{ |
| 465 |
(void)wl_surface; |
| 466 |
struct window *window = data; |
| 467 |
|
| 468 |
struct surf_output *surf_output, *surf_output_tmp; |
| 469 |
wl_list_for_each_safe(surf_output, surf_output_tmp, &window->surf_outputs, link) { |
| 470 |
if (surf_output->output->output == wl_output) { |
| 471 |
wl_list_remove(&surf_output->link); |
| 472 |
free(surf_output); |
| 473 |
break; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
window_update_output(window); |
| 478 |
} |
| 479 |
|
| 480 |
static const struct wl_surface_listener surface_listener = { |
| 481 |
.enter = wl_surface_enter, |
| 482 |
.leave = wl_surface_leave, |
| 483 |
}; |
| 484 |
|
| 485 |
static void |
| 486 |
destroy_windows(struct wayland *wayland) |
| 487 |
{ |
| 488 |
struct window *window, *window_tmp; |
| 489 |
wl_list_for_each_safe(window, window_tmp, &wayland->windows, link) { |
| 490 |
wl_list_remove(&window->link); |
| 491 |
bm_wl_window_destroy(window); |
| 492 |
free(window); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void |
| 497 |
window_update_output(struct window *window) |
| 498 |
{ |
| 499 |
int32_t max_scale = 1; |
| 500 |
uint32_t min_max_height = 0; |
| 501 |
|
| 502 |
struct surf_output *surf_output; |
| 503 |
wl_list_for_each(surf_output, &window->surf_outputs, link) { |
| 504 |
if (surf_output->output->scale > max_scale) { |
| 505 |
max_scale = surf_output->output->scale; |
| 506 |
} |
| 507 |
if (min_max_height == 0 || surf_output->output->height < min_max_height) { |
| 508 |
min_max_height = surf_output->output->height; |
| 509 |
}; |
| 510 |
} |
| 511 |
|
| 512 |
if (min_max_height != window->max_height) { |
| 513 |
window->max_height = min_max_height; |
| 514 |
} |
| 515 |
|
| 516 |
if (max_scale != window->scale) { |
| 517 |
window->scale = max_scale; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
void |
| 522 |
recreate_windows(const struct bm_menu *menu, struct wayland *wayland) |
| 523 |
{ |
| 524 |
destroy_windows(wayland); |
| 525 |
|
| 526 |
struct window *window = calloc(1, sizeof(struct window)); |
| 527 |
wl_list_init(&window->surf_outputs); |
| 528 |
window->wayland = wayland; |
| 529 |
window->align = menu->align; |
| 530 |
window->hmargin_size = menu->hmargin_size; |
| 531 |
window->width_factor = menu->width_factor; |
| 532 |
|
| 533 |
// TODO: this should not be necessary, but Sway 1.8.1 does not trigger event |
| 534 |
// surface.enter before we actually need to render the first frame. |
| 535 |
window->scale = 1; |
| 536 |
window->max_height = 640; |
| 537 |
|
| 538 |
struct wl_surface *surface = NULL; |
| 539 |
if (!(surface = wl_compositor_create_surface(wayland->compositor))) |
| 540 |
goto fail; |
| 541 |
|
| 542 |
wl_surface_add_listener(surface, &surface_listener, window); |
| 543 |
|
| 544 |
struct output *output = NULL; |
| 545 |
if (wayland->selected_output) { |
| 546 |
fprintf(stderr, "selected output\n"); |
| 547 |
output = wayland->selected_output; |
| 548 |
}; |
| 549 |
|
| 550 |
struct wl_output *wl_output = NULL; |
| 551 |
if (output) |
| 552 |
wl_output = output->output; |
| 553 |
|
| 554 |
if (!bm_wl_window_create(window, wayland->display, wayland->shm, |
| 555 |
wl_output, wayland->layer_shell, surface)) { |
| 556 |
free(window); |
| 557 |
goto fail; |
| 558 |
} |
| 559 |
|
| 560 |
window->notify.render = bm_cairo_paint; |
| 561 |
window->render_pending = true; |
| 562 |
wl_list_insert(&wayland->windows, &window->link); |
| 563 |
|
| 564 |
set_overlap(menu, menu->overlap); |
| 565 |
grab_keyboard(menu, menu->grabbed); |
| 566 |
return; |
| 567 |
|
| 568 |
fail: |
| 569 |
// This is non handlable if fails on recreation |
| 570 |
fprintf(stderr, "wayland window creation failed :/\n"); |
| 571 |
abort(); |
| 572 |
} |
| 573 |
|
| 574 |
static void |
| 575 |
set_monitor(const struct bm_menu *menu, int32_t monitor) |
| 576 |
{ |
| 577 |
(void)monitor; |
| 578 |
struct wayland *wayland = menu->renderer->internal; |
| 579 |
assert(wayland); |
| 580 |
recreate_windows(menu, wayland); |
| 581 |
} |
| 582 |
|
| 583 |
static void |
| 584 |
set_monitor_name(const struct bm_menu *menu, char *monitor_name) |
| 585 |
{ |
| 586 |
(void)monitor_name; |
| 587 |
struct wayland *wayland = menu->renderer->internal; |
| 588 |
assert(wayland); |
| 589 |
|
| 590 |
if (!monitor_name) { |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
struct output *output; |
| 595 |
wl_list_for_each(output, &wayland->outputs, link) { |
| 596 |
if (0 == strcmp(monitor_name, output->name)) { |
| 597 |
wayland->selected_output = output; |
| 598 |
recreate_windows(menu, wayland); |
| 599 |
return; |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
static void |
| 605 |
destructor(struct bm_menu *menu) |
| 606 |
{ |
| 607 |
struct wayland *wayland = menu->renderer->internal; |
| 608 |
|
| 609 |
if (!wayland) |
| 610 |
return; |
| 611 |
|
| 612 |
destroy_windows(wayland); |
| 613 |
bm_wl_registry_destroy(wayland); |
| 614 |
|
| 615 |
xkb_context_unref(wayland->input.xkb.context); |
| 616 |
|
| 617 |
if (wayland->display) { |
| 618 |
epoll_ctl(efd, EPOLL_CTL_DEL, wayland->fds.repeat, NULL); |
| 619 |
epoll_ctl(efd, EPOLL_CTL_DEL, wayland->fds.display, NULL); |
| 620 |
close(wayland->fds.repeat); |
| 621 |
wl_display_flush(wayland->display); |
| 622 |
wl_display_disconnect(wayland->display); |
| 623 |
} |
| 624 |
|
| 625 |
free(wayland); |
| 626 |
menu->renderer->internal = NULL; |
| 627 |
} |
| 628 |
|
| 629 |
static bool |
| 630 |
constructor(struct bm_menu *menu) |
| 631 |
{ |
| 632 |
if (!getenv("WAYLAND_DISPLAY") && !getenv("WAYLAND_SOCKET")) |
| 633 |
return false; |
| 634 |
|
| 635 |
struct wayland *wayland; |
| 636 |
if (!(menu->renderer->internal = wayland = calloc(1, sizeof(struct wayland)))) |
| 637 |
goto fail; |
| 638 |
|
| 639 |
wl_list_init(&wayland->windows); |
| 640 |
wl_list_init(&wayland->outputs); |
| 641 |
|
| 642 |
if (!(wayland->display = wl_display_connect(NULL))) |
| 643 |
goto fail; |
| 644 |
|
| 645 |
if (!(wayland->input.xkb.context = xkb_context_new(XKB_CONTEXT_NO_FLAGS))) |
| 646 |
goto fail; |
| 647 |
|
| 648 |
if (!bm_wl_registry_register(wayland)) |
| 649 |
goto fail; |
| 650 |
|
| 651 |
wayland->fds.display = wl_display_get_fd(wayland->display); |
| 652 |
wayland->fds.repeat = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); |
| 653 |
wayland->input.repeat_fd = &wayland->fds.repeat; |
| 654 |
wayland->input.key_pending = false; |
| 655 |
recreate_windows(menu, wayland); |
| 656 |
|
| 657 |
if (!efd && (efd = epoll_create1(EPOLL_CLOEXEC)) < 0) |
| 658 |
goto fail; |
| 659 |
|
| 660 |
struct epoll_event ep; |
| 661 |
ep.events = EPOLLIN | EPOLLERR | EPOLLHUP; |
| 662 |
ep.data.ptr = &wayland->fds.display; |
| 663 |
epoll_ctl(efd, EPOLL_CTL_ADD, wayland->fds.display, &ep); |
| 664 |
|
| 665 |
struct epoll_event ep2; |
| 666 |
ep2.events = EPOLLIN; |
| 667 |
ep2.data.ptr = &wayland->fds.repeat; |
| 668 |
epoll_ctl(efd, EPOLL_CTL_ADD, wayland->fds.repeat, &ep2); |
| 669 |
return true; |
| 670 |
|
| 671 |
fail: |
| 672 |
destructor(menu); |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
BM_PUBLIC extern const char* |
| 677 |
register_renderer(struct render_api *api) |
| 678 |
{ |
| 679 |
api->constructor = constructor; |
| 680 |
api->destructor = destructor; |
| 681 |
api->get_displayed_count = get_displayed_count; |
| 682 |
api->get_height = get_height; |
| 683 |
api->get_width = get_width; |
| 684 |
api->poll_key = poll_key; |
| 685 |
api->poll_pointer = poll_pointer; |
| 686 |
api->poll_touch = poll_touch; |
| 687 |
api->release_touch = release_touch; |
| 688 |
api->render = render; |
| 689 |
api->set_align = set_align; |
| 690 |
api->set_y_offset = set_y_offset; |
| 691 |
api->set_width = set_width; |
| 692 |
api->grab_keyboard = grab_keyboard; |
| 693 |
api->set_overlap = set_overlap; |
| 694 |
api->set_monitor = set_monitor; |
| 695 |
api->set_monitor_name = set_monitor_name; |
| 696 |
api->priorty = BM_PRIO_GUI; |
| 697 |
api->version = BM_PLUGIN_VERSION; |
| 698 |
return "wayland"; |
| 699 |
} |
| 700 |
|
| 701 |
/* vim: set ts=8 sw=4 tw=0 :*/ |