Skip to content
Myr

Merge branch 'fdm'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Daniel Eklöf · committed by Daniel Eklöf · parents 3700e1c, 3c5af59 · Browse files · 14 files+1688 added−1128 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

14 files+1688 added−1128 removed

  • other:+1688 added−1128 removed

14 files+1688 added−1128 removed

modified:application.c+4 added-3 removed
90 unmodified lines
91 return false;92}9394bool95application_execute(struct application *app, const struct prompt *prompt)95application_execute(const struct application *app, const struct prompt *prompt)96{97 const wchar_t *ptext = prompt_text(prompt);97 const size_t clen = wcstombs(NULL, prompt->text, 0);98 const size_t clen = wcstombs(NULL, ptext, 0);99 char cprompt[clen + 1];99 wcstombs(cprompt, prompt->text, clen + 1);100 wcstombs(cprompt, ptext, clen + 1);101102 const char *execute = app != NULL ? app->exec : cprompt;103 const char *path = app != NULL ? app->path : NULL;104
90 unmodified lines
modified:application.h+1 added-1 removed
29 unmodified lines
30 struct application *v;31 size_t count;32};3334bool application_execute(struct application *app, const struct prompt *prompt);34bool application_execute(const struct application *app, const struct prompt *prompt);
added:fdm.c+124 added
1#include "fdm.h"23#include <stdlib.h>4#include <unistd.h>5#include <errno.h>6#include <assert.h>7#include <sys/epoll.h>89#define LOG_MODULE "fdm"10#define LOG_ENABLE_DBG 011#include "log.h"12#include "tllist.h"1314struct fd {15 int fd;16 void *data;17 fdm_handler_t handler;18};1920struct fdm {21 int epoll_fd;22 tll(struct fd) fds;23};2425struct fdm *26fdm_init(void)27{28 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);29 if (epoll_fd == -1) {30 LOG_ERRNO("failed to create epoll FD");31 return NULL;32 }3334 struct fdm *fdm = malloc(sizeof(*fdm));35 *fdm = (struct fdm){36 .epoll_fd = epoll_fd,37 .fds = tll_init(),38 };39 return fdm;40}4142void43fdm_destroy(struct fdm *fdm)44{45 if (fdm == NULL)46 return;4748 if (tll_length(fdm->fds) > 0)49 LOG_WARN("FD list not empty");5051 assert(tll_length(fdm->fds) == 0);5253 tll_free(fdm->fds);54 close(fdm->epoll_fd);55 free(fdm);56}5758bool59fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data)60{61#if defined(_DEBUG)62 tll_foreach(fdm->fds, it) {63 if (it->item.fd == fd) {64 LOG_ERR("FD=%d already registered", fd);65 return false;66 }67 }68#endif6970 tll_push_back(71 fdm->fds, ((struct fd){.fd = fd, .data = data, .handler = handler}));7273 struct epoll_event ev = {74 .events = events,75 .data = {.ptr = &tll_back(fdm->fds)},76 };7778 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {79 LOG_ERRNO("failed to register FD with epoll");80 tll_pop_back(fdm->fds);81 return false;82 }8384 return true;85}8687bool88fdm_del(struct fdm *fdm, int fd)89{90 tll_foreach(fdm->fds, it) {91 if (it->item.fd == fd) {92 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_DEL, fd, NULL) < 0)93 LOG_ERRNO("failed to unregister FD=%d from epoll", fd);9495 tll_remove(fdm->fds, it);96 return true;97 }98 }99100 LOG_ERR("no such FD: %d", fd);101 return false;102}103104bool105fdm_poll(struct fdm *fdm)106{107 struct epoll_event events[tll_length(fdm->fds)];108 int ret = epoll_wait(fdm->epoll_fd, events, tll_length(fdm->fds), -1);109 if (ret == -1) {110 if (errno == EINTR)111 return true;112113 LOG_ERRNO("failed to epoll");114 return false;115 }116117 for (int i = 0; i < ret; i++) {118 struct fd *fd = events[i].data.ptr;119 if (!fd->handler(fdm, fd->fd, events[i].events, fd->data))120 return false;121 }122123 return true;124}
added:fdm.h+15 added
1#pragma once23#include <stdbool.h>45struct fdm;67typedef bool (*fdm_handler_t)(struct fdm *fdm, int fd, int events, void *data);89struct fdm *fdm_init(void);10void fdm_destroy(struct fdm *fdm);1112bool fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data);13bool fdm_del(struct fdm *fdm, int fd);1415bool fdm_poll(struct fdm *fdm);
modified:main.c+94 added-1101 removed
1#include <stdlib.h>2#include <stdio.h>3#include <string.h>3#include <stdbool.h>5#include <wctype.h>6#include <unistd.h>7#include <errno.h>4#include <getopt.h>56#include <locale.h>11#include <poll.h>78#include <sys/types.h>9#include <sys/mman.h>15#include <sys/wait.h>16#include <sys/eventfd.h>17#include <sys/timerfd.h>10#include <sys/stat.h>19#include <dirent.h>11#include <fcntl.h>1222#include <cairo.h>2324#include <wayland-client.h>25#include <wayland-cursor.h>26#include <wayland-util.h>2728#include <xkbcommon/xkbcommon.h>29#include <xkbcommon/xkbcommon-keysyms.h>3031#include <xdg-output-unstable-v1.h>32#include <wlr-layer-shell-unstable-v1.h>3313#define LOG_MODULE "fuzzel"14#define LOG_ENABLE_DBG 015#include "log.h"37#include "tllist.h"38#include "shm.h"39#include "render.h"16#include "application.h"41#include "match.h"42#include "xdg.h"43#include "font.h"17#include "dmenu.h"18#include "fdm.h"19#include "font.h"20#include "match.h"21#include "render.h"22#include "shm.h"23#include "tllist.h"24#include "version.h"25#include "wayland.h"26#include "xdg.h"2747struct monitor {48 struct wl_output *output;49 struct zxdg_output_v1 *xdg;50 char *name;5152 int x;53 int y;5455 int width_mm;56 int height_mm;5758 int width_px;59 int height_px;6061 int scale;62};6364struct wayland {65 struct wl_display *display;66 struct wl_registry *registry;67 struct wl_compositor *compositor;68 struct wl_surface *surface;69 struct zwlr_layer_shell_v1 *layer_shell;70 struct zwlr_layer_surface_v1 *layer_surface;71 struct wl_shm *shm;72 struct wl_seat *seat;73 struct wl_keyboard *keyboard;74 struct zxdg_output_manager_v1 *xdg_output_manager;7576 tll(struct monitor) monitors;77 const struct monitor *monitor;7879 struct xkb_context *xkb;80 struct xkb_keymap *xkb_keymap;81 struct xkb_state *xkb_state;82};8384struct context {85 volatile enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;86 int exit_code;8788 struct wayland wl;89 struct render *render;9091 bool dmenu_mode;92 struct prompt prompt;93 struct application_list applications;9495 struct match *matches;96 size_t match_count;97 size_t selected;9899 struct {100 int fd;101 bool dont_re_repeat;102 int32_t delay;103 int32_t rate;104 uint32_t key;105 } repeat;106107 bool frame_is_scheduled;108 struct buffer *pending;109110 /* Window configuration */111 int width;112 int height;113};114115static int max_matches;116117static void refresh(struct context *c);118static void update_matches(struct context *c);119120static void121shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)122{123}124125static const struct wl_shm_listener shm_listener = {126 .format = &shm_format,127};128129static bool130start_repeater(struct context *c, uint32_t key)131{132 if (c->repeat.dont_re_repeat)133 return true;134135 struct itimerspec t = {136 .it_value = {.tv_sec = 0, .tv_nsec = c->repeat.delay * 1000000},137 .it_interval = {.tv_sec = 0, .tv_nsec = 1000000000 / c->repeat.rate},138 };139140 if (t.it_value.tv_nsec >= 1000000000) {141 t.it_value.tv_sec += t.it_value.tv_nsec / 1000000000;142 t.it_value.tv_nsec %= 1000000000;143 }144 if (t.it_interval.tv_nsec >= 1000000000) {145 t.it_interval.tv_sec += t.it_interval.tv_nsec / 1000000000;146 t.it_interval.tv_nsec %= 1000000000;147 }148 if (timerfd_settime(c->repeat.fd, 0, &t, NULL) < 0) {149 LOG_ERRNO("failed to arm keyboard repeat timer");150 return false;151 }152153 c->repeat.key = key;154 return true;155}156157static bool158stop_repeater(struct context *c, uint32_t key)159{160 if (key != -1 && key != c->repeat.key)161 return true;162163 if (timerfd_settime(c->repeat.fd, 0, &(struct itimerspec){{0}}, NULL) < 0) {164 LOG_ERRNO("failed to disarm keyboard repeat timer");165 return false;166 }167168 return true;169}170171static void172keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,173 uint32_t format, int32_t fd, uint32_t size)174{175 struct context *c = data;176177 char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);178179 c->wl.xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);180 c->wl.xkb_keymap = xkb_keymap_new_from_string(181 c->wl.xkb, map_str, XKB_KEYMAP_FORMAT_TEXT_V1,182 XKB_KEYMAP_COMPILE_NO_FLAGS);183184 /* TODO: initialize in enter? */185 c->wl.xkb_state = xkb_state_new(c->wl.xkb_keymap);186187 munmap(map_str, size);188 close(fd);189}190191static void192keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,193 struct wl_surface *surface, struct wl_array *keys)194{195 LOG_DBG("enter");196#if 0197 uint32_t *key;198 wl_array_for_each(key, keys)199 xkb_state_update_key(xkb_state, *key, 1);200#endif201}202203static void204keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,205 struct wl_surface *surface)206{207 struct context *c = data;208 stop_repeater(c, -1);209 c->status = EXIT;210}211212static size_t213prompt_next_char(const struct prompt *prompt)214{215 if (prompt->text[prompt->cursor] == L'\0')216 return prompt->cursor;217 return prompt->cursor + 1;218}219220static size_t221prompt_prev_char(const struct prompt *prompt)222{223 if (prompt->cursor == 0)224 return 0;225226 return prompt->cursor - 1;227}228229static size_t230prompt_prev_word(const struct prompt *prompt)231{232 size_t prev_char = prompt_prev_char(prompt);233 const wchar_t *space = &prompt->text[prev_char];234235 /* Ignore initial spaces */236 while (space >= prompt->text && iswspace(*space))237 space--;238239 /* Skip non-spaces */240 while (space >= prompt->text && !iswspace(*space))241 space--;242243 return space - prompt->text + 1;244}245246static size_t247prompt_next_word(const struct prompt *prompt)248{249 const wchar_t *end = prompt->text + wcslen(prompt->text);250 const wchar_t *space = &prompt->text[prompt->cursor];251252 /* Ignore initial non-spaces */253 while (space < end && !iswspace(*space))254 space++;255256 /* Skip spaces */257 while (space < end && iswspace(*space))258 space++;259260 return space - prompt->text;261}262263static void264keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,265 uint32_t time, uint32_t key, uint32_t state)266{267 static bool mod_masks_initialized = false;268 static xkb_mod_mask_t ctrl = -1;269 static xkb_mod_mask_t alt = -1;270 //static xkb_mod_mask_t shift = -1;271272 struct context *c = data;273274 if (!mod_masks_initialized) {275 mod_masks_initialized = true;276 ctrl = 1 << xkb_keymap_mod_get_index(c->wl.xkb_keymap, "Control");277 alt = 1 << xkb_keymap_mod_get_index(c->wl.xkb_keymap, "Mod1");278 //shift = 1 << xkb_keymap_mod_get_index(c->wl.xkb_keymap, "Shift");279 }280281 if (state == XKB_KEY_UP) {282 stop_repeater(c, key);283 return;284 }285286 key += 8;287 xkb_keysym_t sym = xkb_state_key_get_one_sym(c->wl.xkb_state, key);288289 xkb_mod_mask_t mods = xkb_state_serialize_mods(290 c->wl.xkb_state, XKB_STATE_MODS_EFFECTIVE);291 xkb_mod_mask_t consumed = xkb_state_key_get_consumed_mods(c->wl.xkb_state, key);292 xkb_mod_mask_t significant = ctrl | alt /*| shift*/;293 xkb_mod_mask_t effective_mods = mods & ~consumed & significant;294295#if 0296 for (size_t i = 0; i < 32; i++) {297 if (mods & (1 << i)) {298 LOG_DBG("%s", xkb_keymap_mod_get_name(c->wl.xkb_keymap, i));299 }300 }301#endif302303 LOG_DBG("sym=%u, mod=0x%08x, consumed=0x%08x, significant=0x%08x, "304 "effective=0x%08x",305 sym, mods, consumed, significant, effective_mods);306307 if (sym == XKB_KEY_Home || (sym == XKB_KEY_a && effective_mods == ctrl)) {308 c->prompt.cursor = 0;309 refresh(c);310 }311312 else if (sym == XKB_KEY_End || (sym == XKB_KEY_e && effective_mods == ctrl)) {313 c->prompt.cursor = wcslen(c->prompt.text);314 refresh(c);315 }316317 else if ((sym == XKB_KEY_b && effective_mods == alt) ||318 (sym == XKB_KEY_Left && effective_mods == ctrl)) {319 c->prompt.cursor = prompt_prev_word(&c->prompt);320 refresh(c);321 }322323 else if ((sym == XKB_KEY_f && effective_mods == alt) ||324 (sym == XKB_KEY_Right && effective_mods == ctrl)) {325326 c->prompt.cursor = prompt_next_word(&c->prompt);327 refresh(c);328 }329330 else if ((sym == XKB_KEY_Escape && effective_mods == 0) ||331 (sym == XKB_KEY_g && effective_mods == ctrl)) {332 c->status = EXIT;333 }334335 else if ((sym == XKB_KEY_p && effective_mods == ctrl) ||336 (sym == XKB_KEY_Up && effective_mods == 0)) {337 if (c->selected > 0) {338 c->selected--;339 refresh(c);340 }341 }342343 else if ((sym == XKB_KEY_n && effective_mods == ctrl) ||344 (sym == XKB_KEY_Down && effective_mods == 0)) {345 if (c->selected + 1 < c->match_count) {346 c->selected++;347 refresh(c);348 }349 }350351 else if (sym == XKB_KEY_Tab && effective_mods == 0) {352 if (c->selected + 1 < c->match_count) {353 c->selected++;354 refresh(c);355 } else {356 c->selected = 0;357 refresh(c);358 }359 }360361 else if (sym == XKB_KEY_ISO_Left_Tab && effective_mods == 0) {362 if (c->selected > 0) {363 c->selected--;364 refresh(c);365 } else {366 c->selected = c->match_count - 1;367 refresh(c);368 }369 }370371 else if ((sym == XKB_KEY_b && effective_mods == ctrl) ||372 (sym == XKB_KEY_Left && effective_mods == 0)) {373 size_t new_cursor = prompt_prev_char(&c->prompt);374 if (new_cursor != c->prompt.cursor) {375 c->prompt.cursor = new_cursor;376 refresh(c);377 }378 }379380 else if ((sym == XKB_KEY_f && effective_mods == ctrl) ||381 (sym == XKB_KEY_Right && effective_mods == 0)) {382 size_t new_cursor = prompt_next_char(&c->prompt);383 if (new_cursor != c->prompt.cursor) {384 c->prompt.cursor = new_cursor;385 refresh(c);386 }387 }388389 else if ((sym == XKB_KEY_d && effective_mods == ctrl) ||390 (sym == XKB_KEY_Delete && effective_mods == 0)) {391 if (c->prompt.cursor < wcslen(c->prompt.text)) {392 size_t next_char = prompt_next_char(&c->prompt);393 memmove(&c->prompt.text[c->prompt.cursor],394 &c->prompt.text[next_char],395 (wcslen(c->prompt.text) - next_char + 1) * sizeof(wchar_t));396 update_matches(c);397 refresh(c);398 }399 }400401 else if (sym == XKB_KEY_BackSpace && effective_mods == 0) {402 if (c->prompt.cursor > 0) {403 size_t prev_char = prompt_prev_char(&c->prompt);404 c->prompt.text[prev_char] = L'\0';405 c->prompt.cursor = prev_char;406407 update_matches(c);408 refresh(c);409 }410 }411412 else if (sym == XKB_KEY_BackSpace && (effective_mods == ctrl ||413 effective_mods == alt)) {414 size_t new_cursor = prompt_prev_word(&c->prompt);415 memmove(&c->prompt.text[new_cursor],416 &c->prompt.text[c->prompt.cursor],417 (wcslen(c->prompt.text) - c->prompt.cursor + 1) * sizeof(wchar_t));418 c->prompt.cursor = new_cursor;419 update_matches(c);420 refresh(c);421 }422423 else if ((sym == XKB_KEY_d && effective_mods == alt) ||424 (sym == XKB_KEY_Delete && effective_mods == ctrl)) {425 size_t next_word = prompt_next_word(&c->prompt);426 memmove(&c->prompt.text[c->prompt.cursor],427 &c->prompt.text[next_word],428 (wcslen(c->prompt.text) - next_word + 1) * sizeof(wchar_t));429 update_matches(c);430 refresh(c);431 }432433 else if (sym == XKB_KEY_k && effective_mods == ctrl) {434 c->prompt.text[c->prompt.cursor] = L'\0';435 update_matches(c);436 refresh(c);437 }438439 else if (sym == XKB_KEY_Return && effective_mods == 0) {440 c->status = EXIT;441442 struct application *match = c->match_count > 0443 ? c->matches[c->selected].application444 : NULL;445446 if (c->dmenu_mode) {447 if (match == NULL) {448 c->status = KEEP_RUNNING;449 } else {450 dmenu_execute(match);451 c->exit_code = EXIT_SUCCESS;452 }453 } else {454 bool success = application_execute(match, &c->prompt);455 c->exit_code = success ? EXIT_SUCCESS : EXIT_FAILURE;456457 if (success && match != NULL) {458 c->status = EXIT_UPDATE_CACHE;459 match->count++;460 }461 }462 }463464 else if (effective_mods == 0) {465 char buf[128] = {0};466 int count = xkb_state_key_get_utf8(c->wl.xkb_state, key, buf, sizeof(buf));467468 if (count == 0)469 return;470471 const char *b = buf;472 mbstate_t ps = {0};473 size_t wlen = mbsnrtowcs(NULL, &b, count, 0, &ps);474475 const size_t new_len = wcslen(c->prompt.text) + wlen + 1;476 wchar_t *new_text = realloc(c->prompt.text, new_len * sizeof(wchar_t));477 if (new_text == NULL)478 return;479480 memmove(&new_text[c->prompt.cursor + wlen],481 &new_text[c->prompt.cursor],482 (wcslen(new_text) - c->prompt.cursor + 1) * sizeof(wchar_t));483484 b = buf;485 ps = (mbstate_t){0};486 mbsnrtowcs(&new_text[c->prompt.cursor], &b, count, wlen + 1, &ps);487488 c->prompt.text = new_text;489 c->prompt.cursor += wlen;490491 LOG_DBG("prompt: \"%S\" (cursor=%zu, length=%zu)",492 c->prompt.text, c->prompt.cursor, new_len);493494 update_matches(c);495 refresh(c);496 }497498 start_repeater(c, key - 8);499500}501502static void503keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,504 uint32_t mods_depressed, uint32_t mods_latched,505 uint32_t mods_locked, uint32_t group)506{507 struct context *c = data;508509 LOG_DBG("modifiers: depressed=0x%x, latched=0x%x, locked=0x%x, group=%u",510 mods_depressed, mods_latched, mods_locked, group);511512 xkb_state_update_mask(513 c->wl.xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);514}515516static void517keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,518 int32_t rate, int32_t delay)519{520 struct context *c = data;521 LOG_DBG("keyboard repeat: rate=%d, delay=%d", rate, delay);522 c->repeat.rate = rate;523 c->repeat.delay = delay;524}525526static const struct wl_keyboard_listener keyboard_listener = {527 .keymap = &keyboard_keymap,528 .enter = &keyboard_enter,529 .leave = &keyboard_leave,530 .key = &keyboard_key,531 .modifiers = &keyboard_modifiers,532 .repeat_info = &keyboard_repeat_info,533};534535static void536seat_handle_capabilities(void *data, struct wl_seat *wl_seat,537 enum wl_seat_capability caps)538{539 struct context *c = data;540541 if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD))542 return;543544 if (c->wl.keyboard != NULL)545 wl_keyboard_release(c->wl.keyboard);546547 c->wl.keyboard = wl_seat_get_keyboard(wl_seat);548 wl_keyboard_add_listener(c->wl.keyboard, &keyboard_listener, c);549}550551static void552seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)553{554}555556static const struct wl_seat_listener seat_listener = {557 .capabilities = seat_handle_capabilities,558 .name = seat_handle_name,559};560561static void562output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,563 int32_t physical_width, int32_t physical_height,564 int32_t subpixel, const char *make, const char *model,565 int32_t transform)566{567 struct monitor *mon = data;568 mon->width_mm = physical_width;569 mon->height_mm = physical_height;570}571572static void573output_mode(void *data, struct wl_output *wl_output, uint32_t flags,574 int32_t width, int32_t height, int32_t refresh)575{576}577578static void579output_done(void *data, struct wl_output *wl_output)580{581}582583static void584output_scale(void *data, struct wl_output *wl_output, int32_t factor)585{586 struct monitor *mon = data;587 mon->scale = factor;588}589590static const struct wl_output_listener output_listener = {591 .geometry = &output_geometry,592 .mode = &output_mode,593 .done = &output_done,594 .scale = &output_scale,595};596597static void598xdg_output_handle_logical_position(599 void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y)600{601 struct monitor *mon = data;602 mon->x = x;603 mon->y = y;604}605606static void607xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,608 int32_t width, int32_t height)609{610 struct monitor *mon = data;611 mon->width_px = width;612 mon->height_px = height;613}614615static void616xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)617{618}619620static void621xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,622 const char *name)623{624 struct monitor *mon = data;625 mon->name = strdup(name);626}627628static void629xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,630 const char *description)631{632}633634static struct zxdg_output_v1_listener xdg_output_listener = {635 .logical_position = xdg_output_handle_logical_position,636 .logical_size = xdg_output_handle_logical_size,637 .done = xdg_output_handle_done,638 .name = xdg_output_handle_name,639 .description = xdg_output_handle_description,640};641642static void643handle_global(void *data, struct wl_registry *registry,644 uint32_t name, const char *interface, uint32_t version)645{646 struct context *c = data;647648 if (strcmp(interface, wl_compositor_interface.name) == 0) {649 c->wl.compositor = wl_registry_bind(650 c->wl.registry, name, &wl_compositor_interface, 4);651 }652653 else if (strcmp(interface, wl_shm_interface.name) == 0) {654 c->wl.shm = wl_registry_bind(c->wl.registry, name, &wl_shm_interface, 1);655 wl_shm_add_listener(c->wl.shm, &shm_listener, &c->wl);656 wl_display_roundtrip(c->wl.display);657 }658659 else if (strcmp(interface, wl_output_interface.name) == 0) {660 struct wl_output *output = wl_registry_bind(661 c->wl.registry, name, &wl_output_interface, 3);662663 tll_push_back(c->wl.monitors, ((struct monitor){.output = output}));664665 struct monitor *mon = &tll_back(c->wl.monitors);666 wl_output_add_listener(output, &output_listener, mon);667668 /*669 * The "output" interface doesn't give us the monitors'670 * identifiers (e.g. "LVDS-1"). Use the XDG output interface671 * for that.672 */673674 assert(c->wl.xdg_output_manager != NULL);675 mon->xdg = zxdg_output_manager_v1_get_xdg_output(676 c->wl.xdg_output_manager, mon->output);677678 zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);679 wl_display_roundtrip(c->wl.display);680 }681682 else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {683 c->wl.layer_shell = wl_registry_bind(684 c->wl.registry, name, &zwlr_layer_shell_v1_interface, 1);685 }686687 else if (strcmp(interface, wl_seat_interface.name) == 0) {688 c->wl.seat = wl_registry_bind(c->wl.registry, name, &wl_seat_interface, 4);689 wl_seat_add_listener(c->wl.seat, &seat_listener, c);690 wl_display_roundtrip(c->wl.display);691 }692693 else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {694 c->wl.xdg_output_manager = wl_registry_bind(695 c->wl.registry, name, &zxdg_output_manager_v1_interface, 2);696 }697}698699static void700handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)701{702 LOG_WARN("global removed: %u", name);703}704705static const struct wl_registry_listener registry_listener = {706 .global = &handle_global,707 .global_remove = &handle_global_remove,708};709710static void711layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,712 uint32_t serial, uint32_t w, uint32_t h)713{714 zwlr_layer_surface_v1_ack_configure(surface, serial);715}716717static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {718 .configure = layer_surface_configure,719};720721static int722sort_match_by_count(const void *_a, const void *_b)723{724 const struct match *a = _a;725 const struct match *b = _b;726 return b->application->count - a->application->count;727}728729static wchar_t *730wcscasestr(const wchar_t *haystack, const wchar_t *needle)731{732 const size_t hay_len = wcslen(haystack);733 const size_t needle_len = wcslen(needle);734735 if (needle_len > hay_len)736 return NULL;737738 for (size_t i = 0; i < wcslen(haystack) - wcslen(needle) + 1; i++) {739 bool matched = true;740 for (size_t j = 0; j < wcslen(needle); j++) {741 if (towlower(haystack[i + j]) != towlower(needle[j])) {742 matched = false;743 break;744 }745 }746747 if (matched)748 return (wchar_t *)&haystack[i];749 }750751 return NULL;752}753754static void755update_matches(struct context *c)756{757 /* Nothing entered; all programs found matches */758 if (wcslen(c->prompt.text) == 0) {759760 for (size_t i = 0; i < c->applications.count; i++) {761 c->matches[i] = (struct match){762 .application = &c->applications.v[i],763 .start_title = -1,764 .start_comment = -1};765 }766767 /* Sort */768 c->match_count = c->applications.count;769 qsort(c->matches, c->match_count, sizeof(c->matches[0]), &sort_match_by_count);770771 /* Limit count (don't render outside window) */772 if (c->match_count > max_matches)773 c->match_count = max_matches;774775 if (c->selected >= c->match_count && c->selected > 0)776 c->selected = c->match_count - 1;777 return;778 }779780 c->match_count = 0;781 for (size_t i = 0; i < c->applications.count; i++) {782 struct application *app = &c->applications.v[i];783 size_t start_title = -1;784 size_t start_comment = -1;785786 const wchar_t *m = wcscasestr(app->title, c->prompt.text);787 if (m != NULL)788 start_title = m - app->title;789790 if (app->comment != NULL) {791 m = wcscasestr(app->comment, c->prompt.text);792 if (m != NULL)793 start_comment = m - app->comment;794 }795796 if (start_title == -1 && start_comment == -1)797 continue;798799 c->matches[c->match_count++] = (struct match){800 .application = app,801 .start_title = start_title,802 .start_comment = start_comment};803 }804805 /* Sort */806 qsort(c->matches, c->match_count, sizeof(c->matches[0]), &sort_match_by_count);807808 /* Limit count (don't render outside window) */809 if (c->match_count > max_matches)810 c->match_count = max_matches;811812 if (c->selected >= c->match_count && c->selected > 0)813 c->selected = c->match_count - 1;814}815816static void frame_callback(817 void *data, struct wl_callback *wl_callback, uint32_t callback_data);818819static const struct wl_callback_listener frame_listener = {820 .done = &frame_callback,821};822823static void824commit_buffer(struct context *c, struct buffer *buf)825{826 assert(buf->busy);827828 wl_surface_set_buffer_scale(c->wl.surface, c->wl.monitor->scale);829 wl_surface_attach(c->wl.surface, buf->wl_buf, 0, 0);830 wl_surface_damage(c->wl.surface, 0, 0, buf->width, buf->height);831832 struct wl_callback *cb = wl_surface_frame(c->wl.surface);833 wl_callback_add_listener(cb, &frame_listener, c);834835 c->frame_is_scheduled = true;836 wl_surface_commit(c->wl.surface);837}838839static void840frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_data)841{842 struct context *c = data;843844 c->frame_is_scheduled = false;845 wl_callback_destroy(wl_callback);846847 if (c->pending != NULL) {848 commit_buffer(c, c->pending);849 c->pending = NULL;850 }851}852853static void854refresh(struct context *c)855{856 LOG_DBG("refresh");857858 struct buffer *buf = shm_get_buffer(c->wl.shm, c->width, c->height);859860 /* Background + border */861 render_background(c->render, buf);862863 /* Window content */864 render_prompt(c->render, buf, &c->prompt);865 render_match_list(c->render, buf, c->matches, c->match_count,866 wcslen(c->prompt.text), c->selected);867868 cairo_surface_flush(buf->cairo_surface);869870 if (c->frame_is_scheduled) {871 /* There's already a frame being drawn - delay current frame872 * (overwriting any previous pending frame) */873874 if (c->pending != NULL)875 c->pending->busy = false;876877 c->pending = buf;878 } else {879 /* No pending frames - render immediately */880 assert(c->pending == NULL);881 commit_buffer(c, buf);882 }883}88428static struct rgba29hex_to_rgba(uint32_t color)30{31 return (struct rgba){
170 unmodified lines
202203 const char *output_name = NULL;204 const char *font_name = "monospace";205 const char *icon_theme = "hicolor";10631064 int width = 500;1065 int height = 300;1066 const int x_margin = 20;1067 const int y_margin = 4;1068 int border_width = 1;1069 int border_radius = 10;1070 uint32_t text_color = 0xffffffff;1071 uint32_t match_color = 0xcc9393ff;1072 uint32_t background = 0x000000ff;1073 uint32_t selection_color = 0x333333ff;1074 uint32_t border_color = 0xffffffff;206 bool dmenu_mode = false;207208 struct render_options render_options = {209 .width = 500,210 .height = 300,211 .x_margin = 20,212 .y_margin = 4,213 .border_size = 1,214 .border_radius = 10,215 .background_color = hex_to_rgba(0x000000ff),216 .border_color = hex_to_rgba(0xffffffff),217 .text_color = hex_to_rgba(0xffffffff),218 .match_color = hex_to_rgba(0xcc9393ff),219 .selection_color = hex_to_rgba(0x333333ff),220 };221222 while (true) {223 int c = getopt_long(argc, argv, ":o:f:i:g:b:t:m:s:B:r:C:dvh", longopts, NULL);224 if (c == -1)225 break;
11 unmodified lines
237 icon_theme = optarg;238 break;239240 case 'g':1096 if (sscanf(optarg, "%dx%d", &width, &height) != 2) {241 if (sscanf(optarg, "%dx%d", &render_options.width, &render_options.height) != 2) {242 LOG_ERR("%s: invalid geometry (must be <width>x<height>)", optarg);243 return EXIT_FAILURE;244 }245 break;2461102 case 'b':247 case 'b': {248 uint32_t background;249 if (sscanf(optarg, "%08x", &background) != 1) {250 LOG_ERR("%s: invalid color", optarg);251 return EXIT_FAILURE;252 }253 render_options.background_color = hex_to_rgba(background);254 break;255 }2561109 case 't':257 case 't': {258 uint32_t text_color;259 if (sscanf(optarg, "%08x", &text_color) != 1) {260 LOG_ERR("%s: invalid color", optarg);261 return EXIT_FAILURE;262 }263 render_options.text_color = hex_to_rgba(text_color);264 break;265 }2661116 case 'm':267 case 'm': {268 uint32_t match_color;269 if (sscanf(optarg, "%x", &match_color) != 1) {270 LOG_ERR("%s: invalid color", optarg);271 return EXIT_FAILURE;272 }273 render_options.match_color = hex_to_rgba(match_color);274 break;275 }2761123 case 's':277 case 's': {278 uint32_t selection_color;279 if (sscanf(optarg, "%x", &selection_color) != 1) {280 LOG_ERR("%s: invalid color", optarg);281 return EXIT_FAILURE;282 }283 render_options.selection_color = hex_to_rgba(selection_color);284 break;285 }286287 case 'B':1131 if (sscanf(optarg, "%d", &border_width) != 1) {288 if (sscanf(optarg, "%d", &render_options.border_size) != 1) {289 LOG_ERR(290 "%s: invalid border width (must be an integer)", optarg);291 return EXIT_FAILURE;292 }293 break;294295 case 'r':1139 if (sscanf(optarg, "%d", &border_radius) != 1) {296 if (sscanf(optarg, "%d", &render_options.border_radius) != 1) {297 LOG_ERR("%s: invalid border radius (must be an integer)", optarg);298 return EXIT_FAILURE;299 }300 break;3011145 case 'C':302 case 'C': {303 uint32_t border_color;304 if (sscanf(optarg, "%x", &border_color) != 1) {305 LOG_ERR("%s: invalid color", optarg);306 return EXIT_FAILURE;307 }308 render_options.border_color = hex_to_rgba(border_color);309 break;310 }311312 case 'd':313 dmenu_mode = true;314 break;
19 unmodified lines
334 int ret = EXIT_FAILURE;335336 setlocale(LC_ALL, "");3371178 struct context c = {1179 .status = KEEP_RUNNING,1180 .exit_code = EXIT_FAILURE,1181 .wl = {0},1182 .dmenu_mode = dmenu_mode,1183 .prompt = {1184 .prompt = wcsdup(L"> "),1185 .text = calloc(1, sizeof(wchar_t)),1186 .cursor = 01187 },1188 .repeat = {1189 .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK),1190 },1191 };338 /* Load applications */339 struct application_list applications = {0};340 struct fdm *fdm = NULL;341 struct prompt *prompt = NULL;342 struct matches *matches = NULL;343 struct render *render = NULL;344 struct wayland *wayl = NULL;345 struct font *font = NULL;3461193 struct font *font = font_from_name(font_name);1194 LOG_DBG("height: %d, ascent: %d, descent: %d",1195 font->fextents.height, font->fextents.ascent, font->fextents.descent);11961197 if (dmenu_mode)1198 dmenu_load_entries(&c.applications);1199 else1200 xdg_find_programs(icon_theme, font->fextents.height, &c.applications);1201 c.matches = malloc(c.applications.count * sizeof(c.matches[0]));1202 read_cache(&c.applications);12031204 c.wl.display = wl_display_connect(NULL);1205 if (c.wl.display == NULL) {1206 LOG_ERR("failed to connect to wayland; no compositor running?");347 if ((font = font_from_name(font_name)) == NULL)348 goto out;1208 }3491210 c.wl.registry = wl_display_get_registry(c.wl.display);1211 if (c.wl.registry == NULL) {1212 LOG_ERR("failed to get wayland registry");1213 goto out;1214 }350 LOG_DBG(351 "font: height: %d, ascent: %d, descent: %d",352 font->fextents.height, font->fextents.ascent, font->fextents.descent);3531216 wl_registry_add_listener(c.wl.registry, &registry_listener, &c);1217 wl_display_roundtrip(c.wl.display);354 /* Calculate number of entries we can show, based on the total355 * height and the fonts line height */356 const double line_height357 = 2 * render_options.y_margin + font->fextents.height;358 const size_t max_matches =359 (render_options.height - 2 * render_options.border_size - line_height)360 / line_height;3611219 if (c.wl.compositor == NULL) {1220 LOG_ERR("no compositor");1221 goto out;1222 }1223 if (c.wl.layer_shell == NULL) {1224 LOG_ERR("no layer shell interface");1225 goto out;1226 }1227 if (c.wl.shm == NULL) {1228 LOG_ERR("no shared memory buffers interface");1229 goto out;1230 }362 LOG_DBG("max matches: %d", max_matches);3631232 if (tll_length(c.wl.monitors) == 0) {1233 LOG_ERR("no monitors");1234 goto out;1235 }364 /* Load applications */365 if (dmenu_mode)366 dmenu_load_entries(&applications);367 else368 xdg_find_programs(icon_theme, font->fextents.height, &applications);369 read_cache(&applications);3701237 tll_foreach(c.wl.monitors, it) {1238 const struct monitor *mon = &it->item;1239 LOG_INFO("monitor: %s: %dx%d+%d+%d (%dx%dmm)",1240 mon->name, mon->width_px, mon->height_px,1241 mon->x, mon->y, mon->width_mm, mon->height_mm);12421243 if (output_name != NULL && strcmp(output_name, mon->name) == 0) {1244 c.wl.monitor = mon;1245 break;1246 } else if (output_name == NULL) {1247 /* Use last monitor found */1248 c.wl.monitor = mon;1249 }1250 }12511252 if (c.wl.monitor == NULL && output_name != NULL) {1253 LOG_ERR("%s: no output with that name found", output_name);371 if ((render = render_init(font, &render_options)) == NULL)372 goto out;1255 }3731257 if (c.wl.monitor == NULL) {1258 LOG_ERR("no outputs found");374 if ((fdm = fdm_init()) == NULL)375 goto out;1260 }3761262 c.wl.surface = wl_compositor_create_surface(c.wl.compositor);1263 if (c.wl.surface == NULL) {1264 LOG_ERR("failed to create panel surface");377 if ((prompt = prompt_init(L"> ")) == NULL)378 goto out;1266 }3791268#if 01269 c.wl.pointer.surface = wl_compositor_create_surface(c.wl.compositor);1270 if (c.wl.pointer.surface == NULL) {1271 LOG_ERR("failed to create cursor surface");380 if ((matches = matches_init(&applications, max_matches)) == NULL)381 goto out;1273 }3821275 c.wl.pointer.theme = wl_cursor_theme_load(NULL, 24, c.wl.shm);1276 if (c.wl.pointer.theme == NULL) {1277 LOG_ERR("failed to load cursor theme");1278 goto out;1279 }1280#endif383 matches_update(matches, prompt);3841282 c.wl.layer_surface = zwlr_layer_shell_v1_get_layer_surface(1283 c.wl.layer_shell, c.wl.surface, c.wl.monitor->output,1284 ZWLR_LAYER_SHELL_V1_LAYER_TOP, "fuzzel");12851286 if (c.wl.layer_surface == NULL) {1287 LOG_ERR("failed to create layer shell surface");385 if ((wayl = wayl_init(386 fdm, render, prompt, matches, render_options.width,387 render_options.height, output_name, dmenu_mode)) == NULL)388 goto out;1289 }3891291 const int scale = c.wl.monitor->scale;12921293 width /= scale; width *= scale;1294 height /= scale; height *= scale;12951296 c.width = width;1297 c.height = height;12981299 zwlr_layer_surface_v1_set_size(c.wl.layer_surface, width / scale, height / scale);1300 zwlr_layer_surface_v1_set_keyboard_interactivity(c.wl.layer_surface, 1);13011302 zwlr_layer_surface_v1_add_listener(1303 c.wl.layer_surface, &layer_surface_listener, &c.wl);13041305 /* Trigger a 'configure' event, after which we'll have the width */1306 wl_surface_commit(c.wl.surface);1307 wl_display_roundtrip(c.wl.display);13081309 const double line_height = 2 * y_margin + font->fextents.height;1310 max_matches = (height - 2 * border_width - line_height) / line_height;1311 LOG_DBG("max matches: %d", max_matches);13121313 struct options options = {1314 .width = c.width,1315 .height = c.height,1316 .x_margin = x_margin,1317 .y_margin = y_margin,1318 .border_size = border_width,1319 .border_radius = border_radius,1320 .background_color = hex_to_rgba(background),1321 .border_color = hex_to_rgba(border_color),1322 .text_color = hex_to_rgba(text_color),1323 .match_color = hex_to_rgba(match_color),1324 .selection_color = hex_to_rgba(selection_color),1325 };1326 c.render = render_init(font, options);13271328 update_matches(&c);1329 refresh(&c);13301331 wl_display_dispatch_pending(c.wl.display);1332 wl_display_flush(c.wl.display);13331334 while (c.status == KEEP_RUNNING) {1335 struct pollfd fds[] = {1336 {.fd = wl_display_get_fd(c.wl.display), .events = POLLIN},1337 {.fd = c.repeat.fd, .events = POLLIN},1338 };13391340 wl_display_flush(c.wl.display);1341 poll(fds, 2, -1);13421343 if (fds[0].revents & POLLHUP) {1344 LOG_WARN("disconnected from wayland");390 while (true) {391 wayl_flush(wayl);392 if (!fdm_poll(fdm))393 break;1346 }13471348 if (fds[1].revents & POLLIN) {1349 uint64_t expiration_count;1350 ssize_t ret = read(1351 c.repeat.fd, &expiration_count, sizeof(expiration_count));13521353 if (ret < 0 && errno != EAGAIN)1354 LOG_ERRNO("failed to read key repeat count from timer fd");1355 else if (ret > 0) {1356 c.repeat.dont_re_repeat = true;1357 for (size_t i = 0; i < expiration_count; i++)1358 keyboard_key(&c, NULL, 0, 0, c.repeat.key, XKB_KEY_DOWN);1359 c.repeat.dont_re_repeat = false;1360 }1361 }13621363 if (fds[0].revents & POLLIN)1364 wl_display_dispatch(c.wl.display);394 }3951367 if (c.status == EXIT_UPDATE_CACHE)1368 write_cache(&c.applications);396 if (wayl_update_cache(wayl))397 write_cache(&applications);3981370 ret = c.exit_code;399 ret = wayl_exit_code(wayl);400401out:13731374 render_destroy(c.render);402 shm_fini();4031377 free(c.prompt.prompt);1378 free(c.prompt.text);1379 for (size_t i = 0; i < c.applications.count; i++) {1380 struct application *app = &c.applications.v[i];404 wayl_destroy(wayl);405 render_destroy(render);406 matches_destroy(matches);407 prompt_destroy(prompt);408 fdm_destroy(fdm);409410 for (size_t i = 0; i < applications.count; i++) {411 struct application *app = &applications.v[i];412413 free(app->path);414 free(app->exec);415 free(app->title);416 free(app->comment);
1 unmodified line
418 cairo_surface_destroy(app->icon.surface);419 else if (app->icon.type == ICON_SVG)420 g_object_unref(app->icon.svg);421 }1391 free(c.applications.v);1392 free(c.matches);422 free(applications.v);4231394 tll_foreach(c.wl.monitors, it) {1395 free(it->item.name);1396 if (it->item.xdg)1397 zxdg_output_v1_destroy(it->item.xdg);1398 if (it->item.output != NULL)1399 wl_output_destroy(it->item.output);1400 tll_remove(c.wl.monitors, it);1401 }14021403 if (c.wl.xdg_output_manager != NULL)1404 zxdg_output_manager_v1_destroy(c.wl.xdg_output_manager);14051406 if (c.wl.layer_surface != NULL)1407 zwlr_layer_surface_v1_destroy(c.wl.layer_surface);1408 if (c.wl.layer_shell != NULL)1409 zwlr_layer_shell_v1_destroy(c.wl.layer_shell);1410 if (c.wl.keyboard != NULL)1411 wl_keyboard_destroy(c.wl.keyboard);1412 if (c.wl.surface != NULL)1413 wl_surface_destroy(c.wl.surface);1414 if (c.wl.seat != NULL)1415 wl_seat_destroy(c.wl.seat);1416 if (c.wl.compositor != NULL)1417 wl_compositor_destroy(c.wl.compositor);1418 if (c.wl.shm != NULL)1419 wl_shm_destroy(c.wl.shm);1420 if (c.wl.registry != NULL)1421 wl_registry_destroy(c.wl.registry);1422 if (c.wl.display != NULL)1423 wl_display_disconnect(c.wl.display);1424 if (c.wl.xkb_state != NULL)1425 xkb_state_unref(c.wl.xkb_state);1426 if (c.wl.xkb_keymap != NULL)1427 xkb_keymap_unref(c.wl.xkb_keymap);1428 if (c.wl.xkb != NULL)1429 xkb_context_unref(c.wl.xkb);1430424 cairo_debug_reset_static_data();425426 return ret;427}
added:match.c+188 added
1#include "match.h"23#include <wctype.h>4#include <assert.h>56struct matches {7 const struct application_list *applications;8 struct match *matches;9 size_t match_count;10 size_t selected;11 size_t max_matches;12};1314struct matches *15matches_init(const struct application_list *applications, size_t max_matches)16{17 struct matches *matches = malloc(sizeof(*matches));18 *matches = (struct matches) {19 .applications = applications,20 .matches = malloc(applications->count * sizeof(matches->matches[0])),21 .match_count = 0,22 .selected = 0,23 .max_matches = max_matches,24 };25 return matches;26}2728void29matches_destroy(struct matches *matches)30{31 if (matches == NULL)32 return;3334 free(matches->matches);35 free(matches);36}3738const struct match *39matches_get(const struct matches *matches, size_t idx)40{41 assert(idx < matches->match_count);42 return &matches->matches[idx];43}4445const struct match *46matches_get_match(const struct matches *matches)47{48 return matches->match_count > 049 ? matches_get(matches, matches->selected)50 : NULL;51}5253size_t54matches_get_count(const struct matches *matches)55{56 return matches->match_count;57}5859size_t60matches_get_match_index(const struct matches *matches)61{62 return matches->selected;63}6465bool66matches_selected_prev(struct matches *matches, bool wrap)67{68 if (matches->selected > 0) {69 matches->selected--;70 return true;71 } else if (wrap && matches->match_count > 1) {72 matches->selected = 0;73 return true;74 }7576 return false;77}7879bool80matches_selected_next(struct matches *matches, bool wrap)81{82 if (matches->selected + 1 < matches->match_count) {83 matches->selected++;84 return true;85 } else if (wrap && matches->match_count > 1) {86 matches->selected = matches->match_count - 1;87 return true;88 }8990 return false;91}9293static int94sort_match_by_count(const void *_a, const void *_b)95{96 const struct match *a = _a;97 const struct match *b = _b;98 return b->application->count - a->application->count;99}100101static wchar_t *102wcscasestr(const wchar_t *haystack, const wchar_t *needle)103{104 const size_t hay_len = wcslen(haystack);105 const size_t needle_len = wcslen(needle);106107 if (needle_len > hay_len)108 return NULL;109110 for (size_t i = 0; i < wcslen(haystack) - wcslen(needle) + 1; i++) {111 bool matched = true;112 for (size_t j = 0; j < wcslen(needle); j++) {113 if (towlower(haystack[i + j]) != towlower(needle[j])) {114 matched = false;115 break;116 }117 }118119 if (matched)120 return (wchar_t *)&haystack[i];121 }122123 return NULL;124}125126void127matches_update(struct matches *matches, const struct prompt *prompt)128{129 const wchar_t *ptext = prompt_text(prompt);130131 /* Nothing entered; all programs found matches */132 if (wcslen(ptext) == 0) {133134 for (size_t i = 0; i < matches->applications->count; i++) {135 matches->matches[i] = (struct match){136 .application = &matches->applications->v[i],137 .start_title = -1,138 .start_comment = -1};139 }140141 /* Sort */142 matches->match_count = matches->applications->count;143 qsort(matches->matches, matches->match_count, sizeof(matches->matches[0]), &sort_match_by_count);144145 /* Limit count (don't render outside window) */146 if (matches->match_count > matches->max_matches)147 matches->match_count = matches->max_matches;148149 if (matches->selected >= matches->match_count && matches->selected > 0)150 matches->selected = matches->match_count - 1;151 return;152 }153154 matches->match_count = 0;155 for (size_t i = 0; i < matches->applications->count; i++) {156 struct application *app = &matches->applications->v[i];157 size_t start_title = -1;158 size_t start_comment = -1;159160 const wchar_t *m = wcscasestr(app->title, ptext);161 if (m != NULL)162 start_title = m - app->title;163164 if (app->comment != NULL) {165 m = wcscasestr(app->comment, ptext);166 if (m != NULL)167 start_comment = m - app->comment;168 }169170 if (start_title == -1 && start_comment == -1)171 continue;172173 matches->matches[matches->match_count++] = (struct match){174 .application = app,175 .start_title = start_title,176 .start_comment = start_comment};177 }178179 /* Sort */180 qsort(matches->matches, matches->match_count, sizeof(matches->matches[0]), &sort_match_by_count);181182 /* Limit count (don't render outside window) */183 if (matches->match_count > matches->max_matches)184 matches->match_count = matches->max_matches;185186 if (matches->selected >= matches->match_count && matches->selected > 0)187 matches->selected = matches->match_count - 1;188}
modified:match.h+18 added
1#pragma once23#include <stdbool.h>4#include <unistd.h>56#include "application.h"7#include "prompt.h"89struct match {10 struct application *application;11 ssize_t start_title;12 ssize_t start_comment;13};1415struct matches;16struct matches *matches_init(17 const struct application_list *applications, size_t max_matches);18void matches_destroy(struct matches *matches);1920void matches_update(struct matches *matches, const struct prompt *prompt);2122const struct match *matches_get(const struct matches *matches, size_t idx);23const struct match *matches_get_match(const struct matches *matches);24size_t matches_get_count(const struct matches *matches);25size_t matches_get_match_index(const struct matches *matches);2627bool matches_selected_prev(struct matches *matches, bool wrap);28bool matches_selected_next(struct matches *matches, bool wrap);
modified:meson.build+4 added-1 removed
62 unmodified lines
63executable(64 'fuzzel',65 'application.c', 'application.h',66 'dmenu.c', 'dmenu.h',67 'fdm.c', 'fdm.h',68 'font.c', 'font.h',69 'icon.c', 'icon.h',70 'log.c', 'log.h',71 'main.c',71 'prompt.h',72 'match.c', 'match.h',73 'prompt.c', 'prompt.h',74 'render.c', 'render.h',75 'shm.c', 'shm.h',76 'stride.h',77 'tllist.h',78 'wayland.c', 'wayland.h',79 'xdg.c', 'xdg.h',80 wl_proto_src + wl_proto_headers, version,81 dependencies: [pixman,82 freetype,
12 unmodified lines
added:prompt.c+262 added
1#include "prompt.h"23#include <stdlib.h>4#include <string.h>5#include <wctype.h>67#define LOG_MODULE "prompt"8#define LOG_ENABLE_DBG 09#include "log.h"1011struct prompt {12 wchar_t *prompt;13 wchar_t *text;14 size_t cursor;15};1617struct prompt *18prompt_init(const wchar_t *prompt_text)19{20 struct prompt *prompt = malloc(sizeof(*prompt));21 *prompt = (struct prompt) {22 .prompt = wcsdup(prompt_text),23 .text = calloc(1, sizeof(wchar_t)),24 .cursor = 0,25 };2627 return prompt;28}2930void31prompt_destroy(struct prompt *prompt)32{33 if (prompt == NULL)34 return;3536 free(prompt->prompt);37 free(prompt->text);38 free(prompt);39}4041bool42prompt_insert_chars(struct prompt *prompt, const char *text, size_t len)43{44 const char *b = text;45 mbstate_t ps = {0};46 size_t wlen = mbsnrtowcs(NULL, &b, len, 0, &ps);4748 const size_t new_len = wcslen(prompt->text) + wlen + 1;49 wchar_t *new_text = realloc(prompt->text, new_len * sizeof(wchar_t));50 if (new_text == NULL)51 return false;5253 memmove(&new_text[prompt->cursor + wlen],54 &new_text[prompt->cursor],55 (wcslen(new_text) - prompt->cursor + 1) * sizeof(wchar_t));5657 b = text;58 ps = (mbstate_t){0};59 mbsnrtowcs(&new_text[prompt->cursor], &b, len, wlen + 1, &ps);6061 prompt->text = new_text;62 prompt->cursor += wlen;6364 LOG_DBG("prompt: \"%S\" (cursor=%zu, length=%zu)",65 prompt->text, prompt->cursor, new_len);6667 return true;68}6970const wchar_t *71prompt_prompt(const struct prompt *prompt)72{73 return prompt->prompt;74}7576const wchar_t *77prompt_text(const struct prompt *prompt)78{79 return prompt->text;80}8182size_t83prompt_cursor(const struct prompt *prompt)84{85 return prompt->cursor;86}8788static size_t89idx_next_char(struct prompt *prompt)90{91 if (prompt->text[prompt->cursor] == L'\0')92 return prompt->cursor;93 return prompt->cursor + 1;94}9596static size_t97idx_prev_char(const struct prompt *prompt)98{99 if (prompt->cursor == 0)100 return 0;101102 return prompt->cursor - 1;103}104105static size_t106idx_prev_word(const struct prompt *prompt)107{108 size_t prev_char = idx_prev_char(prompt);109 const wchar_t *space = &prompt->text[prev_char];110111 /* Ignore initial spaces */112 while (space >= prompt->text && iswspace(*space))113 space--;114115 /* Skip non-spaces */116 while (space >= prompt->text && !iswspace(*space))117 space--;118119 return space - prompt->text + 1;120}121122static size_t123idx_next_word(const struct prompt *prompt)124{125 const wchar_t *end = prompt->text + wcslen(prompt->text);126 const wchar_t *space = &prompt->text[prompt->cursor];127128 /* Ignore initial non-spaces */129 while (space < end && !iswspace(*space))130 space++;131132 /* Skip spaces */133 while (space < end && iswspace(*space))134 space++;135136 return space - prompt->text;137}138139bool140prompt_cursor_home(struct prompt *prompt)141{142 if (prompt->cursor == 0)143 return false;144145 prompt->cursor = 0;146 return true;147}148149bool150prompt_cursor_end(struct prompt *prompt)151{152 size_t text_len = wcslen(prompt->text);153 if (prompt->cursor >= text_len)154 return false;155156 prompt->cursor = text_len;157 return true;158}159160bool161prompt_cursor_next_char(struct prompt *prompt)162{163 size_t idx = idx_next_char(prompt);164 if (idx == prompt->cursor)165 return false;166167 prompt->cursor = idx;168 return true;169}170171bool172prompt_cursor_prev_char(struct prompt *prompt)173{174 size_t idx = idx_prev_char(prompt);175 if (idx == prompt->cursor)176 return false;177178 prompt->cursor = idx;179 return true;180}181182bool183prompt_cursor_prev_word(struct prompt *prompt)184{185 size_t idx = idx_prev_word(prompt);186 if (idx == prompt->cursor)187 return false;188189 prompt->cursor = idx;190 return true;191}192193bool194prompt_cursor_next_word(struct prompt *prompt)195{196 size_t idx = idx_next_word(prompt);197 if (idx == prompt->cursor)198 return false;199200 prompt->cursor = idx;201 return true;202}203204bool205prompt_erase_next_char(struct prompt *prompt)206{207 if (prompt->cursor >= wcslen(prompt->text))208 return false;209210 size_t next_char = idx_next_char(prompt);211 memmove(&prompt->text[prompt->cursor],212 &prompt->text[next_char],213 (wcslen(prompt->text) - next_char + 1) * sizeof(wchar_t));214 return true;215}216217bool218prompt_erase_prev_char(struct prompt *prompt)219{220221 if (prompt->cursor == 0)222 return false;223224 size_t prev_char = idx_prev_char(prompt);225 prompt->text[prev_char] = L'\0';226 prompt->cursor = prev_char;227 return true;228}229230bool231prompt_erase_next_word(struct prompt *prompt)232{233 size_t next_word = idx_next_word(prompt);234 if (next_word == prompt->cursor)235 return false;236237 memmove(&prompt->text[prompt->cursor],238 &prompt->text[next_word],239 (wcslen(prompt->text) - next_word + 1) * sizeof(wchar_t));240 return true;241}242243bool244prompt_erase_prev_word(struct prompt *prompt)245{246 size_t new_cursor = idx_prev_word(prompt);247 if (new_cursor == prompt->cursor)248 return false;249250 memmove(&prompt->text[new_cursor],251 &prompt->text[prompt->cursor],252 (wcslen(prompt->text) - prompt->cursor + 1) * sizeof(wchar_t));253 prompt->cursor = new_cursor;254 return true;255}256257bool258prompt_erase_after_cursor(struct prompt *prompt)259{260 prompt->text[prompt->cursor] = L'\0';261 return true;262}
modified:prompt.h+23 added-5 removed
1#pragma once23#include <stddef.h>4#include <stdbool.h>5#include <wchar.h>66struct prompt {7 wchar_t *prompt;8 wchar_t *text;9 size_t cursor;10};7struct prompt;8struct prompt *prompt_init(const wchar_t *prompt_text);9void prompt_destroy(struct prompt *prompt);1011bool prompt_insert_chars(struct prompt *prompt, const char *text, size_t len);1213const wchar_t *prompt_prompt(const struct prompt *prompt);14const wchar_t *prompt_text(const struct prompt *prompt);15size_t prompt_cursor(const struct prompt *prompt);1617bool prompt_cursor_home(struct prompt *prompt);18bool prompt_cursor_end(struct prompt *prompt);19bool prompt_cursor_next_char(struct prompt *prompt);20bool prompt_cursor_prev_char(struct prompt *prompt);21bool prompt_cursor_prev_word(struct prompt *prompt);22bool prompt_cursor_next_word(struct prompt *prompt);2324bool prompt_erase_next_char(struct prompt *prompt);25bool prompt_erase_prev_char(struct prompt *prompt);26bool prompt_erase_next_word(struct prompt *prompt);27bool prompt_erase_prev_word(struct prompt *prompt);28bool prompt_erase_after_cursor(struct prompt *prompt);
modified:render.c+16 added-12 removed
7 unmodified lines
8#include "log.h"9#include "font.h"1011struct render {12 struct options options;12 struct render_options options;13 struct font *regular_font;14};1516void
86 unmodified lines
103render_prompt(const struct render *render, struct buffer *buf,104 const struct prompt *prompt)105{106 struct font *font = render->regular_font;107 const size_t prompt_len = wcslen(prompt->prompt);108 const size_t text_len = wcslen(prompt->text);107108 const wchar_t *pprompt = prompt_prompt(prompt);109 const size_t prompt_len = wcslen(pprompt);110111 const wchar_t *ptext = prompt_text(prompt);112 const size_t text_len = wcslen(ptext);113114 int x = render->options.border_size + render->options.x_margin;115 int y = render->options.border_size + render->options.y_margin + font->fextents.ascent;116117 for (size_t i = 0; i < prompt_len + text_len; i++) {114 wchar_t wc = i < prompt_len ? prompt->prompt[i] : prompt->text[i - prompt_len];118 wchar_t wc = i < prompt_len ? pprompt[i] : ptext[i - prompt_len];119 const struct glyph *glyph = font_glyph_for_wc(font, wc);120 if (glyph == NULL)121 continue;122123 render_glyph(buf->pix, glyph, x, y, &render->options.pix_text_color);124 x += glyph->x_advance;125126 /* Cursor */123 if (prompt->cursor + prompt_len - 1 == i) {127 if (prompt_cursor(prompt) + prompt_len - 1 == i) {128 pixman_image_fill_rectangles(129 PIXMAN_OP_SRC, buf->pix, &render->options.pix_text_color,130 1, &(pixman_rectangle16_t){131 x, y - font->fextents.ascent,
25 unmodified lines
157}158159void160render_match_list(const struct render *render, struct buffer *buf,157 const struct match matches[], size_t match_count,158 size_t match_length, size_t selected)161 const struct prompt *prompt, const struct matches *matches)162{163 struct font *font = render->regular_font;164 const double x_margin = render->options.x_margin;165 const double y_margin = render->options.y_margin;166 const double border_size = render->options.border_size;167 const size_t match_count = matches_get_count(matches);168 const size_t selected = matches_get_match_index(matches);169170 assert(match_count == 0 || selected < match_count);171172 const double row_height = 2 * y_margin + font->fextents.height;
6 unmodified lines
179 */180 double y = first_row + (row_height + font->fextents.height) / 2 - font->fextents.descent;181182 for (size_t i = 0; i < match_count; i++) {178 const struct match *match = &matches[i];183 const struct match *match = matches_get(matches, i);//&matches[i];184185 /* Hightlight selected entry */186 if (i == selected) {187 const struct rgba *sc = &render->options.selection_color;
71 unmodified lines
259260 /* Application title */261 render_match_text(262 buf, &cur_x, y,258 match->application->title, match->start_title, match_length,263 match->application->title, match->start_title, wcslen(prompt_text(prompt)),264 render->regular_font,265 render->options.pix_text_color, render->options.pix_match_color);266267 y += row_height;268 }269}270271struct render *267render_init(struct font *font, struct options options)272render_init(struct font *font, const struct render_options *options)273{274 struct render *render = calloc(1, sizeof(*render));270 render->options = options;275 render->options = *options;276 render->regular_font = font;277278 /* TODO: the one providing the options should calculate these */279 render->options.pix_background_color = rgba2pixman(render->options.background_color);
9 unmodified lines
289{290 if (render == NULL)291 return;292288 //cairo_scaled_font_destroy(render->regular_font);293 font_destroy(render->regular_font);294 free(render);295}
modified:render.h+5 added-5 removed
8 unmodified lines
9#include "tllist.h"1011struct rgba {double r; double g; double b; double a;};1213struct options {13struct render_options {14 int width;15 int height;16 int x_margin;17 int y_margin;
14 unmodified lines
32 pixman_color_t pix_selection_color;33};3435struct render;36struct render *render_init(struct font *font, struct options options);36struct render *render_init(struct font *font, const struct render_options *options);37void render_destroy(struct render *render);3839void render_background(const struct render *render, struct buffer *buf);4041void render_prompt(42 const struct render *render, struct buffer *buf,43 const struct prompt *prompt);4445void render_match_list(const struct render *render, struct buffer *buf,46 const struct match matches[], size_t match_count,47 size_t match_length, size_t selected);45void render_match_list(46 const struct render *render, struct buffer *buf,47 const struct prompt *prompt, const struct matches *matches);
added:wayland.c+915 added
1#include "wayland.h"23#include <stdlib.h>4#include <wctype.h>5#include <unistd.h>6#include <errno.h>78#include <sys/mman.h>9#include <sys/epoll.h>10#include <sys/timerfd.h>1112#include <wayland-client.h>13#include <wayland-cursor.h>14#include <wayland-util.h>1516#include <xkbcommon/xkbcommon.h>17#include <xkbcommon/xkbcommon-keysyms.h>1819#include <xdg-output-unstable-v1.h>20#include <wlr-layer-shell-unstable-v1.h>2122#define LOG_MODULE "wayland"23#define LOG_ENABLE_DBG 024#include "log.h"25#include "tllist.h"26#include "prompt.h"27#include "dmenu.h"28#include "shm.h"29#include "render.h"3031struct monitor {32 struct wl_output *output;33 struct zxdg_output_v1 *xdg;34 char *name;3536 int x;37 int y;3839 int width_mm;40 int height_mm;4142 int width_px;43 int height_px;4445 int scale;46};4748struct repeat {49 int fd;50 int32_t delay;51 int32_t rate;5253 bool dont_re_repeat;54 uint32_t key;55};5657struct wayland {58 struct fdm *fdm;59 struct render *render;60 struct prompt *prompt;61 struct matches *matches;6263 int width;64 int height;6566 struct repeat repeat;6768 enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;69 int exit_code;70 bool dmenu_mode;7172 bool frame_is_scheduled;73 struct buffer *pending;7475 struct wl_display *display;76 struct wl_registry *registry;77 struct wl_compositor *compositor;78 struct wl_surface *surface;79 struct zwlr_layer_shell_v1 *layer_shell;80 struct zwlr_layer_surface_v1 *layer_surface;81 struct wl_shm *shm;82 struct wl_seat *seat;83 struct wl_keyboard *keyboard;84 struct zxdg_output_manager_v1 *xdg_output_manager;8586 tll(struct monitor) monitors;87 const struct monitor *monitor;8889 struct xkb_context *xkb;90 struct xkb_keymap *xkb_keymap;91 struct xkb_state *xkb_state;92};9394static void refresh(struct wayland *wayl);9596bool97repeat_start(struct repeat *repeat, uint32_t key)98{99 if (repeat->dont_re_repeat)100 return true;101102 struct itimerspec t = {103 .it_value = {.tv_sec = 0, .tv_nsec = repeat->delay * 1000000},104 .it_interval = {.tv_sec = 0, .tv_nsec = 1000000000 / repeat->rate},105 };106107 if (t.it_value.tv_nsec >= 1000000000) {108 t.it_value.tv_sec += t.it_value.tv_nsec / 1000000000;109 t.it_value.tv_nsec %= 1000000000;110 }111 if (t.it_interval.tv_nsec >= 1000000000) {112 t.it_interval.tv_sec += t.it_interval.tv_nsec / 1000000000;113 t.it_interval.tv_nsec %= 1000000000;114 }115 if (timerfd_settime(repeat->fd, 0, &t, NULL) < 0) {116 LOG_ERRNO("failed to arm keyboard repeat timer");117 return false;118 }119120 repeat->key = key;121 return true;122}123124bool125repeat_stop(struct repeat *repeat, uint32_t key)126{127 if (key != (uint32_t)-1 && key != repeat->key)128 return true;129130 if (timerfd_settime(repeat->fd, 0, &(struct itimerspec){{0}}, NULL) < 0) {131 LOG_ERRNO("failed to disarm keyboard repeat timer");132 return false;133 }134135 return true;136}137138static void139shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)140{141}142143static const struct wl_shm_listener shm_listener = {144 .format = &shm_format,145};146147static void148keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,149 uint32_t format, int32_t fd, uint32_t size)150{151 struct wayland *wayl = data;152153 char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);154155 wayl->xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);156 wayl->xkb_keymap = xkb_keymap_new_from_string(157 wayl->xkb, map_str, XKB_KEYMAP_FORMAT_TEXT_V1,158 XKB_KEYMAP_COMPILE_NO_FLAGS);159160 /* TODO: initialize in enter? */161 wayl->xkb_state = xkb_state_new(wayl->xkb_keymap);162163 munmap(map_str, size);164 close(fd);165}166167static void168keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,169 struct wl_surface *surface, struct wl_array *keys)170{171 LOG_DBG("enter");172#if 0173 uint32_t *key;174 wl_array_for_each(key, keys)175 xkb_state_update_key(xkb_state, *key, 1);176#endif177}178179static void180keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,181 struct wl_surface *surface)182{183 struct wayland *wayl = data;184 repeat_stop(&wayl->repeat, -1);185 wayl->status = EXIT;186}187188static void189keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,190 uint32_t time, uint32_t key, uint32_t state)191{192 static bool mod_masks_initialized = false;193 static xkb_mod_mask_t ctrl = -1;194 static xkb_mod_mask_t alt = -1;195 //static xkb_mod_mask_t shift = -1;196197 struct wayland *wayl = data;198199 if (!mod_masks_initialized) {200 mod_masks_initialized = true;201 ctrl = 1 << xkb_keymap_mod_get_index(wayl->xkb_keymap, "Control");202 alt = 1 << xkb_keymap_mod_get_index(wayl->xkb_keymap, "Mod1");203 //shift = 1 << xkb_keymap_mod_get_index(wayl->xkb_keymap, "Shift");204 }205206 if (state == XKB_KEY_UP) {207 repeat_stop(&wayl->repeat, key);208 return;209 }210211 key += 8;212 xkb_keysym_t sym = xkb_state_key_get_one_sym(wayl->xkb_state, key);213214 xkb_mod_mask_t mods = xkb_state_serialize_mods(215 wayl->xkb_state, XKB_STATE_MODS_EFFECTIVE);216 xkb_mod_mask_t consumed = xkb_state_key_get_consumed_mods(wayl->xkb_state, key);217 xkb_mod_mask_t significant = ctrl | alt /*| shift*/;218 xkb_mod_mask_t effective_mods = mods & ~consumed & significant;219220#if 0221 for (size_t i = 0; i < 32; i++) {222 if (mods & (1 << i)) {223 LOG_DBG("%s", xkb_keymap_mod_get_name(wayl->xkb_keymap, i));224 }225 }226#endif227228 LOG_DBG("sym=%u, mod=0x%08x, consumed=0x%08x, significant=0x%08x, "229 "effective=0x%08x",230 sym, mods, consumed, significant, effective_mods);231232 if (sym == XKB_KEY_Home || (sym == XKB_KEY_a && effective_mods == ctrl)) {233 if (prompt_cursor_home(wayl->prompt))234 refresh(wayl);235 }236237 else if (sym == XKB_KEY_End || (sym == XKB_KEY_e && effective_mods == ctrl)) {238 if (prompt_cursor_end(wayl->prompt))239 refresh(wayl);240 }241242 else if ((sym == XKB_KEY_b && effective_mods == alt) ||243 (sym == XKB_KEY_Left && effective_mods == ctrl)) {244 if (prompt_cursor_prev_word(wayl->prompt))245 refresh(wayl);246 }247248 else if ((sym == XKB_KEY_f && effective_mods == alt) ||249 (sym == XKB_KEY_Right && effective_mods == ctrl)) {250251 if (prompt_cursor_next_word(wayl->prompt))252 refresh(wayl);253 }254255 else if ((sym == XKB_KEY_Escape && effective_mods == 0) ||256 (sym == XKB_KEY_g && effective_mods == ctrl)) {257 wayl->status = EXIT;258 }259260 else if ((sym == XKB_KEY_p && effective_mods == ctrl) ||261 (sym == XKB_KEY_Up && effective_mods == 0)) {262 if (matches_selected_prev(wayl->matches, false))263 refresh(wayl);264 }265266 else if ((sym == XKB_KEY_n && effective_mods == ctrl) ||267 (sym == XKB_KEY_Down && effective_mods == 0)) {268 if (matches_selected_next(wayl->matches, false))269 refresh(wayl);270 }271272 else if (sym == XKB_KEY_Tab && effective_mods == 0) {273 if (matches_selected_next(wayl->matches, true))274 refresh(wayl);275 }276277 else if (sym == XKB_KEY_ISO_Left_Tab && effective_mods == 0) {278 if (matches_selected_prev(wayl->matches, true))279 refresh(wayl);280 }281282 else if ((sym == XKB_KEY_b && effective_mods == ctrl) ||283 (sym == XKB_KEY_Left && effective_mods == 0)) {284 if (prompt_cursor_prev_char(wayl->prompt))285 refresh(wayl);286 }287288 else if ((sym == XKB_KEY_f && effective_mods == ctrl) ||289 (sym == XKB_KEY_Right && effective_mods == 0)) {290 if (prompt_cursor_next_char(wayl->prompt))291 refresh(wayl);292 }293294 else if ((sym == XKB_KEY_d && effective_mods == ctrl) ||295 (sym == XKB_KEY_Delete && effective_mods == 0)) {296 if (prompt_erase_next_char(wayl->prompt)) {297 matches_update(wayl->matches, wayl->prompt);298 refresh(wayl);299 }300 }301302 else if (sym == XKB_KEY_BackSpace && effective_mods == 0) {303 if (prompt_erase_prev_char(wayl->prompt)) {304 matches_update(wayl->matches, wayl->prompt);305 refresh(wayl);306 }307 }308309 else if (sym == XKB_KEY_BackSpace && (effective_mods == ctrl ||310 effective_mods == alt)) {311 if (prompt_erase_prev_word(wayl->prompt)) {312 matches_update(wayl->matches, wayl->prompt);313 refresh(wayl);314 }315 }316317 else if ((sym == XKB_KEY_d && effective_mods == alt) ||318 (sym == XKB_KEY_Delete && effective_mods == ctrl)) {319 if (prompt_erase_next_word(wayl->prompt)) {320 matches_update(wayl->matches, wayl->prompt);321 refresh(wayl);322 }323 }324325 else if (sym == XKB_KEY_k && effective_mods == ctrl) {326 if (prompt_erase_after_cursor(wayl->prompt)) {327 matches_update(wayl->matches, wayl->prompt);328 refresh(wayl);329 }330 }331332 else if (sym == XKB_KEY_Return && effective_mods == 0) {333 wayl->status = EXIT;334335 const struct match *match = matches_get_match(wayl->matches);336 struct application *app = match != NULL ? match->application : NULL;337338 if (wayl->dmenu_mode) {339 if (match == NULL) {340 wayl->status = KEEP_RUNNING;341 } else {342 dmenu_execute(app);343 wayl->exit_code = EXIT_SUCCESS;344 }345 } else {346 bool success = application_execute(app, wayl->prompt);347 wayl->exit_code = success ? EXIT_SUCCESS : EXIT_FAILURE;348349 if (success && match != NULL) {350 wayl->status = EXIT_UPDATE_CACHE;351 app->count++;352 }353 }354 }355356 else if (effective_mods == 0) {357 char buf[128] = {0};358 int count = xkb_state_key_get_utf8(wayl->xkb_state, key, buf, sizeof(buf));359360 if (count == 0)361 return;362363 if (!prompt_insert_chars(wayl->prompt, buf, count))364 return;365366 matches_update(wayl->matches, wayl->prompt);367 refresh(wayl);368 }369370 repeat_start(&wayl->repeat, key - 8);371372}373374static void375keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,376 uint32_t mods_depressed, uint32_t mods_latched,377 uint32_t mods_locked, uint32_t group)378{379 struct wayland *wayl = data;380381 LOG_DBG("modifiers: depressed=0x%x, latched=0x%x, locked=0x%x, group=%u",382 mods_depressed, mods_latched, mods_locked, group);383384 xkb_state_update_mask(385 wayl->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);386}387388static void389keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,390 int32_t rate, int32_t delay)391{392 struct wayland *wayl = data;393 LOG_DBG("keyboard repeat: rate=%d, delay=%d", rate, delay);394 wayl->repeat.delay = delay;395 wayl->repeat.rate = rate;396}397398static const struct wl_keyboard_listener keyboard_listener = {399 .keymap = &keyboard_keymap,400 .enter = &keyboard_enter,401 .leave = &keyboard_leave,402 .key = &keyboard_key,403 .modifiers = &keyboard_modifiers,404 .repeat_info = &keyboard_repeat_info,405};406407static void408seat_handle_capabilities(void *data, struct wl_seat *wl_seat,409 enum wl_seat_capability caps)410{411 struct wayland *wayl = data;412413 if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD))414 return;415416 if (wayl->keyboard != NULL)417 wl_keyboard_release(wayl->keyboard);418419 wayl->keyboard = wl_seat_get_keyboard(wl_seat);420 wl_keyboard_add_listener(wayl->keyboard, &keyboard_listener, wayl);421}422423static void424seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)425{426}427428static const struct wl_seat_listener seat_listener = {429 .capabilities = seat_handle_capabilities,430 .name = seat_handle_name,431};432433static void434output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,435 int32_t physical_width, int32_t physical_height,436 int32_t subpixel, const char *make, const char *model,437 int32_t transform)438{439 struct monitor *mon = data;440 mon->width_mm = physical_width;441 mon->height_mm = physical_height;442}443444static void445output_mode(void *data, struct wl_output *wl_output, uint32_t flags,446 int32_t width, int32_t height, int32_t refresh)447{448}449450static void451output_done(void *data, struct wl_output *wl_output)452{453}454455static void456output_scale(void *data, struct wl_output *wl_output, int32_t factor)457{458 struct monitor *mon = data;459 mon->scale = factor;460}461462static const struct wl_output_listener output_listener = {463 .geometry = &output_geometry,464 .mode = &output_mode,465 .done = &output_done,466 .scale = &output_scale,467};468469static void470xdg_output_handle_logical_position(471 void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y)472{473 struct monitor *mon = data;474 mon->x = x;475 mon->y = y;476}477478static void479xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,480 int32_t width, int32_t height)481{482 struct monitor *mon = data;483 mon->width_px = width;484 mon->height_px = height;485}486487static void488xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)489{490}491492static void493xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,494 const char *name)495{496 struct monitor *mon = data;497 mon->name = strdup(name);498}499500static void501xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,502 const char *description)503{504}505506static struct zxdg_output_v1_listener xdg_output_listener = {507 .logical_position = xdg_output_handle_logical_position,508 .logical_size = xdg_output_handle_logical_size,509 .done = xdg_output_handle_done,510 .name = xdg_output_handle_name,511 .description = xdg_output_handle_description,512};513514static void515handle_global(void *data, struct wl_registry *registry,516 uint32_t name, const char *interface, uint32_t version)517{518 struct wayland *wayl = data;519520 if (strcmp(interface, wl_compositor_interface.name) == 0) {521 wayl->compositor = wl_registry_bind(522 wayl->registry, name, &wl_compositor_interface, 4);523 }524525 else if (strcmp(interface, wl_shm_interface.name) == 0) {526 wayl->shm = wl_registry_bind(wayl->registry, name, &wl_shm_interface, 1);527 wl_shm_add_listener(wayl->shm, &shm_listener, wayl);528 wl_display_roundtrip(wayl->display);529 }530531 else if (strcmp(interface, wl_output_interface.name) == 0) {532 struct wl_output *output = wl_registry_bind(533 wayl->registry, name, &wl_output_interface, 3);534535 tll_push_back(wayl->monitors, ((struct monitor){.output = output}));536537 struct monitor *mon = &tll_back(wayl->monitors);538 wl_output_add_listener(output, &output_listener, mon);539540 /*541 * The "output" interface doesn't give us the monitors'542 * identifiers (e.g. "LVDS-1"). Use the XDG output interface543 * for that.544 */545546 assert(wayl->xdg_output_manager != NULL);547 mon->xdg = zxdg_output_manager_v1_get_xdg_output(548 wayl->xdg_output_manager, mon->output);549550 zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);551 wl_display_roundtrip(wayl->display);552 }553554 else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {555 wayl->layer_shell = wl_registry_bind(556 wayl->registry, name, &zwlr_layer_shell_v1_interface, 1);557 }558559 else if (strcmp(interface, wl_seat_interface.name) == 0) {560 wayl->seat = wl_registry_bind(wayl->registry, name, &wl_seat_interface, 4);561 wl_seat_add_listener(wayl->seat, &seat_listener, wayl);562 wl_display_roundtrip(wayl->display);563 }564565 else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {566 wayl->xdg_output_manager = wl_registry_bind(567 wayl->registry, name, &zxdg_output_manager_v1_interface, 2);568 }569}570571static void572handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)573{574 LOG_WARN("global removed: %u", name);575}576577static const struct wl_registry_listener registry_listener = {578 .global = &handle_global,579 .global_remove = &handle_global_remove,580};581582static void583layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,584 uint32_t serial, uint32_t w, uint32_t h)585{586 zwlr_layer_surface_v1_ack_configure(surface, serial);587}588589static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {590 .configure = layer_surface_configure,591};592593static void frame_callback(594 void *data, struct wl_callback *wl_callback, uint32_t callback_data);595596static const struct wl_callback_listener frame_listener = {597 .done = &frame_callback,598};599600static void601commit_buffer(struct wayland *wayl, struct buffer *buf)602{603 assert(buf->busy);604605 wl_surface_set_buffer_scale(wayl->surface, wayl->monitor->scale);606 wl_surface_attach(wayl->surface, buf->wl_buf, 0, 0);607 wl_surface_damage_buffer(wayl->surface, 0, 0, buf->width, buf->height);608609 struct wl_callback *cb = wl_surface_frame(wayl->surface);610 wl_callback_add_listener(cb, &frame_listener, wayl);611612 wayl->frame_is_scheduled = true;613 wl_surface_commit(wayl->surface);614}615616static void617frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_data)618{619 struct wayland *wayl = data;620621 wayl->frame_is_scheduled = false;622 wl_callback_destroy(wl_callback);623624 if (wayl->pending != NULL) {625 commit_buffer(wayl, wayl->pending);626 wayl->pending = NULL;627 }628}629630static void631refresh(struct wayland *wayl)632{633 struct buffer *buf = shm_get_buffer(wayl->shm, wayl->width, wayl->height);634635 /* Background + border */636 render_background(wayl->render, buf);637638 /* Window content */639 render_prompt(wayl->render, buf, wayl->prompt);640 render_match_list(wayl->render, buf, wayl->prompt, wayl->matches);641642 cairo_surface_flush(buf->cairo_surface);643644 if (wayl->frame_is_scheduled) {645 /* There's already a frame being drawn - delay current frame646 * (overwriting any previous pending frame) */647648 if (wayl->pending != NULL)649 wayl->pending->busy = false;650651 wayl->pending = buf;652 } else {653 /* No pending frames - render immediately */654 assert(wayl->pending == NULL);655 commit_buffer(wayl, buf);656 }657}658659static bool660fdm_handler(struct fdm *fdm, int fd, int events, void *data)661{662 struct wayland *wayl = data;663 int event_count = wl_display_dispatch(wayl->display);664665 if (events & EPOLLHUP) {666 LOG_WARN("disconnected from Wayland");667 return false;668 }669670 return event_count != -1 && wayl->status == KEEP_RUNNING;671}672673static bool674fdm_repeat(struct fdm *fdm, int fd, int events, void *data)675{676 struct wayland *wayl = data;677 struct repeat *repeat = &wayl->repeat;678679 uint64_t expiration_count;680 ssize_t ret = read(681 repeat->fd, &expiration_count, sizeof(expiration_count));682683 if (ret < 0 && errno != EAGAIN) {684 LOG_ERRNO("failed to read key repeat count from timer fd");685 return false;686 }687688 repeat->dont_re_repeat = true;689 for (size_t i = 0; i < expiration_count; i++)690 keyboard_key(wayl, NULL, 0, 0, repeat->key, XKB_KEY_DOWN);691 repeat->dont_re_repeat = false;692693 if (events & EPOLLHUP) {694 LOG_ERR("keyboard repeater timer FD closed unexpectedly");695 return false;696 }697698 return true;699}700701struct wayland *702wayl_init(struct fdm *fdm, struct render *render, struct prompt *prompt,703 struct matches *matches, int width, int height,704 const char *output_name, bool dmenu_mode)705{706 struct wayland *wayl = calloc(1, sizeof(*wayl));707708 wayl->fdm = fdm;709 wayl->render = render;710 wayl->prompt = prompt;711 wayl->matches = matches;712 wayl->status = KEEP_RUNNING;713 wayl->exit_code = EXIT_FAILURE;714 wayl->dmenu_mode = dmenu_mode;715716 wayl->repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK);717 if (wayl->repeat.fd == -1) {718 LOG_ERRNO("failed to create keyboard repeat timer FD");719 goto out;720 }721722 wayl->display = wl_display_connect(NULL);723 if (wayl->display == NULL) {724 LOG_ERR("failed to connect to wayland; no compositor running?");725 goto out;726 }727728 wayl->registry = wl_display_get_registry(wayl->display);729 if (wayl->registry == NULL) {730 LOG_ERR("failed to get wayland registry");731 goto out;732 }733734 wl_registry_add_listener(wayl->registry, &registry_listener, wayl);735 wl_display_roundtrip(wayl->display);736737 if (wayl->compositor == NULL) {738 LOG_ERR("no compositor");739 goto out;740 }741 if (wayl->layer_shell == NULL) {742 LOG_ERR("no layer shell interface");743 goto out;744 }745 if (wayl->shm == NULL) {746 LOG_ERR("no shared memory buffers interface");747 goto out;748 }749750 if (tll_length(wayl->monitors) == 0) {751 LOG_ERR("no monitors");752 goto out;753 }754755 tll_foreach(wayl->monitors, it) {756 const struct monitor *mon = &it->item;757 LOG_INFO("monitor: %s: %dx%d+%d+%d (%dx%dmm)",758 mon->name, mon->width_px, mon->height_px,759 mon->x, mon->y, mon->width_mm, mon->height_mm);760761 if (output_name != NULL && strcmp(output_name, mon->name) == 0) {762 wayl->monitor = mon;763 break;764 } else if (output_name == NULL) {765 /* Use last monitor found */766 wayl->monitor = mon;767 }768 }769770 if (wayl->monitor == NULL && output_name != NULL) {771 LOG_ERR("%s: no output with that name found", output_name);772 goto out;773 }774775 if (wayl->monitor == NULL) {776 LOG_ERR("no outputs found");777 goto out;778 }779780 wayl->surface = wl_compositor_create_surface(wayl->compositor);781 if (wayl->surface == NULL) {782 LOG_ERR("failed to create panel surface");783 goto out;784 }785786#if 0787 wayl->pointer.surface = wl_compositor_create_surface(wayl->compositor);788 if (wayl->pointer.surface == NULL) {789 LOG_ERR("failed to create cursor surface");790 goto out;791 }792793 wayl->pointer.theme = wl_cursor_theme_load(NULL, 24, wayl->shm);794 if (wayl->pointer.theme == NULL) {795 LOG_ERR("failed to load cursor theme");796 goto out;797 }798#endif799800 wayl->layer_surface = zwlr_layer_shell_v1_get_layer_surface(801 wayl->layer_shell, wayl->surface, wayl->monitor->output,802 ZWLR_LAYER_SHELL_V1_LAYER_TOP, "fuzzel");803804 if (wayl->layer_surface == NULL) {805 LOG_ERR("failed to create layer shell surface");806 goto out;807 }808809 const int scale = wayl->monitor->scale;810811 width /= scale; width *= scale;812 height /= scale; height *= scale;813814 wayl->width = width;815 wayl->height = height;816817 zwlr_layer_surface_v1_set_size(wayl->layer_surface, width / scale, height / scale);818 zwlr_layer_surface_v1_set_keyboard_interactivity(wayl->layer_surface, 1);819820 zwlr_layer_surface_v1_add_listener(821 wayl->layer_surface, &layer_surface_listener, wayl);822823 /* Trigger a 'configure' event, after which we'll have the width */824 wl_surface_commit(wayl->surface);825 wl_display_roundtrip(wayl->display);826827 if (!fdm_add(wayl->fdm, wl_display_get_fd(wayl->display), EPOLLIN, &fdm_handler, wayl)) {828 LOG_ERR("failed to register Wayland socket with FDM");829 goto out;830 }831832 if (!fdm_add(wayl->fdm, wayl->repeat.fd, EPOLLIN, &fdm_repeat, wayl)) {833 LOG_ERR("failed to register keyboard repeat timer FD with FDM");834 goto out;835 }836837 refresh(wayl);838 return wayl;839840out:841 wayl_destroy(wayl);842 return NULL;843}844845void846wayl_destroy(struct wayland *wayl)847{848 if (wayl == NULL)849 return;850851 if (wayl->display != NULL)852 fdm_del(wayl->fdm, wl_display_get_fd(wayl->display));853854 if (wayl->repeat.fd > 0) {855 fdm_del(wayl->fdm, wayl->repeat.fd);856 close(wayl->repeat.fd);857 }858859 tll_foreach(wayl->monitors, it) {860 free(it->item.name);861 if (it->item.xdg)862 zxdg_output_v1_destroy(it->item.xdg);863 if (it->item.output != NULL)864 wl_output_destroy(it->item.output);865 tll_remove(wayl->monitors, it);866 }867868 if (wayl->xdg_output_manager != NULL)869 zxdg_output_manager_v1_destroy(wayl->xdg_output_manager);870871 if (wayl->layer_surface != NULL)872 zwlr_layer_surface_v1_destroy(wayl->layer_surface);873 if (wayl->layer_shell != NULL)874 zwlr_layer_shell_v1_destroy(wayl->layer_shell);875 if (wayl->keyboard != NULL)876 wl_keyboard_destroy(wayl->keyboard);877 if (wayl->surface != NULL)878 wl_surface_destroy(wayl->surface);879 if (wayl->seat != NULL)880 wl_seat_destroy(wayl->seat);881 if (wayl->compositor != NULL)882 wl_compositor_destroy(wayl->compositor);883 if (wayl->shm != NULL)884 wl_shm_destroy(wayl->shm);885 if (wayl->registry != NULL)886 wl_registry_destroy(wayl->registry);887 if (wayl->display != NULL)888 wl_display_disconnect(wayl->display);889 if (wayl->xkb_state != NULL)890 xkb_state_unref(wayl->xkb_state);891 if (wayl->xkb_keymap != NULL)892 xkb_keymap_unref(wayl->xkb_keymap);893 if (wayl->xkb != NULL)894 xkb_context_unref(wayl->xkb);895896 free(wayl);897}898899void900wayl_flush(struct wayland *wayl)901{902 wl_display_flush(wayl->display);903}904905bool906wayl_exit_code(const struct wayland *wayl)907{908 return wayl->exit_code;909}910911bool912wayl_update_cache(const struct wayland *wayl)913{914 return wayl->status == EXIT_UPDATE_CACHE;915}
added:wayland.h+19 added
1#pragma once23#include "fdm.h"45#include "prompt.h"6#include "match.h"7#include "render.h"89struct wayland;10struct wayland *wayl_init(11 struct fdm *fdm, struct render *render, struct prompt *prompt,12 struct matches *matches, int width, int height, const char *output_name,13 bool dmenu_mode);14void wayl_destroy(struct wayland *wayl);1516void wayl_flush(struct wayland *wayl);1718bool wayl_exit_code(const struct wayland *wayl);19bool wayl_update_cache(const struct wayland *wayl);