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
Provenance
- Commit
063a5b3- Tree
30d4584- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
3700e1c- Parent 2
3c5af59
14 files changed+1688 added−1128 removed
modified:application.c+4 added-3 removed
90 unmodified lines91 return false;92 }93 94 bool95-application_execute(struct application *app, const struct prompt *prompt)95+application_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);101 102 const char *execute = app != NULL ? app->exec : cprompt;103 const char *path = app != NULL ? app->path : NULL;104 90 unmodified linesmodified:application.h+1 added-1 removed
29 unmodified lines30 struct application *v;31 size_t count;32 };33 34-bool application_execute(struct application *app, const struct prompt *prompt);34+bool application_execute(const struct application *app, const struct prompt *prompt);added:fdm.c+124 added
1+#include "fdm.h"2+3+#include <stdlib.h>4+#include <unistd.h>5+#include <errno.h>6+#include <assert.h>7+#include <sys/epoll.h>8+9+#define LOG_MODULE "fdm"10+#define LOG_ENABLE_DBG 011+#include "log.h"12+#include "tllist.h"13+14+struct fd {15+ int fd;16+ void *data;17+ fdm_handler_t handler;18+};19+20+struct fdm {21+ int epoll_fd;22+ tll(struct fd) fds;23+};24+25+struct fdm *26+fdm_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+ }33+34+ struct fdm *fdm = malloc(sizeof(*fdm));35+ *fdm = (struct fdm){36+ .epoll_fd = epoll_fd,37+ .fds = tll_init(),38+ };39+ return fdm;40+}41+42+void43+fdm_destroy(struct fdm *fdm)44+{45+ if (fdm == NULL)46+ return;47+48+ if (tll_length(fdm->fds) > 0)49+ LOG_WARN("FD list not empty");50+51+ assert(tll_length(fdm->fds) == 0);52+53+ tll_free(fdm->fds);54+ close(fdm->epoll_fd);55+ free(fdm);56+}57+58+bool59+fdm_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+#endif69+70+ tll_push_back(71+ fdm->fds, ((struct fd){.fd = fd, .data = data, .handler = handler}));72+73+ struct epoll_event ev = {74+ .events = events,75+ .data = {.ptr = &tll_back(fdm->fds)},76+ };77+78+ 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+ }83+84+ return true;85+}86+87+bool88+fdm_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);94+95+ tll_remove(fdm->fds, it);96+ return true;97+ }98+ }99+100+ LOG_ERR("no such FD: %d", fd);101+ return false;102+}103+104+bool105+fdm_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;112+113+ LOG_ERRNO("failed to epoll");114+ return false;115+ }116+117+ 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+ }122+123+ return true;124+}added:fdm.h+15 added
1+#pragma once2+3+#include <stdbool.h>4+5+struct fdm;6+7+typedef bool (*fdm_handler_t)(struct fdm *fdm, int fd, int events, void *data);8+9+struct fdm *fdm_init(void);10+void fdm_destroy(struct fdm *fdm);11+12+bool fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data);13+bool fdm_del(struct fdm *fdm, int fd);14+15+bool 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>5 6 #include <locale.h>11-#include <poll.h>7 8 #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>12 22-#include <cairo.h>23-24-#include <wayland-client.h>25-#include <wayland-cursor.h>26-#include <wayland-util.h>27-28-#include <xkbcommon/xkbcommon.h>29-#include <xkbcommon/xkbcommon-keysyms.h>30-31-#include <xdg-output-unstable-v1.h>32-#include <wlr-layer-shell-unstable-v1.h>33-13 #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"27 47-struct monitor {48- struct wl_output *output;49- struct zxdg_output_v1 *xdg;50- char *name;51-52- int x;53- int y;54-55- int width_mm;56- int height_mm;57-58- int width_px;59- int height_px;60-61- int scale;62-};63-64-struct 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;75-76- tll(struct monitor) monitors;77- const struct monitor *monitor;78-79- struct xkb_context *xkb;80- struct xkb_keymap *xkb_keymap;81- struct xkb_state *xkb_state;82-};83-84-struct context {85- volatile enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;86- int exit_code;87-88- struct wayland wl;89- struct render *render;90-91- bool dmenu_mode;92- struct prompt prompt;93- struct application_list applications;94-95- struct match *matches;96- size_t match_count;97- size_t selected;98-99- struct {100- int fd;101- bool dont_re_repeat;102- int32_t delay;103- int32_t rate;104- uint32_t key;105- } repeat;106-107- bool frame_is_scheduled;108- struct buffer *pending;109-110- /* Window configuration */111- int width;112- int height;113-};114-115-static int max_matches;116-117-static void refresh(struct context *c);118-static void update_matches(struct context *c);119-120-static void121-shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)122-{123-}124-125-static const struct wl_shm_listener shm_listener = {126- .format = &shm_format,127-};128-129-static bool130-start_repeater(struct context *c, uint32_t key)131-{132- if (c->repeat.dont_re_repeat)133- return true;134-135- 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- };139-140- 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- }152-153- c->repeat.key = key;154- return true;155-}156-157-static bool158-stop_repeater(struct context *c, uint32_t key)159-{160- if (key != -1 && key != c->repeat.key)161- return true;162-163- 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- }167-168- return true;169-}170-171-static void172-keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,173- uint32_t format, int32_t fd, uint32_t size)174-{175- struct context *c = data;176-177- char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);178-179- 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);183-184- /* TODO: initialize in enter? */185- c->wl.xkb_state = xkb_state_new(c->wl.xkb_keymap);186-187- munmap(map_str, size);188- close(fd);189-}190-191-static void192-keyboard_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-}202-203-static void204-keyboard_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-}211-212-static size_t213-prompt_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-}219-220-static size_t221-prompt_prev_char(const struct prompt *prompt)222-{223- if (prompt->cursor == 0)224- return 0;225-226- return prompt->cursor - 1;227-}228-229-static size_t230-prompt_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];234-235- /* Ignore initial spaces */236- while (space >= prompt->text && iswspace(*space))237- space--;238-239- /* Skip non-spaces */240- while (space >= prompt->text && !iswspace(*space))241- space--;242-243- return space - prompt->text + 1;244-}245-246-static size_t247-prompt_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];251-252- /* Ignore initial non-spaces */253- while (space < end && !iswspace(*space))254- space++;255-256- /* Skip spaces */257- while (space < end && iswspace(*space))258- space++;259-260- return space - prompt->text;261-}262-263-static void264-keyboard_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;271-272- struct context *c = data;273-274- 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- }280-281- if (state == XKB_KEY_UP) {282- stop_repeater(c, key);283- return;284- }285-286- key += 8;287- xkb_keysym_t sym = xkb_state_key_get_one_sym(c->wl.xkb_state, key);288-289- 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;294-295-#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-#endif302-303- LOG_DBG("sym=%u, mod=0x%08x, consumed=0x%08x, significant=0x%08x, "304- "effective=0x%08x",305- sym, mods, consumed, significant, effective_mods);306-307- if (sym == XKB_KEY_Home || (sym == XKB_KEY_a && effective_mods == ctrl)) {308- c->prompt.cursor = 0;309- refresh(c);310- }311-312- 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- }316-317- 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- }322-323- else if ((sym == XKB_KEY_f && effective_mods == alt) ||324- (sym == XKB_KEY_Right && effective_mods == ctrl)) {325-326- c->prompt.cursor = prompt_next_word(&c->prompt);327- refresh(c);328- }329-330- else if ((sym == XKB_KEY_Escape && effective_mods == 0) ||331- (sym == XKB_KEY_g && effective_mods == ctrl)) {332- c->status = EXIT;333- }334-335- 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- }342-343- 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- }350-351- 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- }360-361- 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- }370-371- 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- }379-380- 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- }388-389- 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- }400-401- 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;406-407- update_matches(c);408- refresh(c);409- }410- }411-412- 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- }422-423- 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- }432-433- 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- }438-439- else if (sym == XKB_KEY_Return && effective_mods == 0) {440- c->status = EXIT;441-442- struct application *match = c->match_count > 0443- ? c->matches[c->selected].application444- : NULL;445-446- 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;456-457- if (success && match != NULL) {458- c->status = EXIT_UPDATE_CACHE;459- match->count++;460- }461- }462- }463-464- 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));467-468- if (count == 0)469- return;470-471- const char *b = buf;472- mbstate_t ps = {0};473- size_t wlen = mbsnrtowcs(NULL, &b, count, 0, &ps);474-475- 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;479-480- memmove(&new_text[c->prompt.cursor + wlen],481- &new_text[c->prompt.cursor],482- (wcslen(new_text) - c->prompt.cursor + 1) * sizeof(wchar_t));483-484- b = buf;485- ps = (mbstate_t){0};486- mbsnrtowcs(&new_text[c->prompt.cursor], &b, count, wlen + 1, &ps);487-488- c->prompt.text = new_text;489- c->prompt.cursor += wlen;490-491- LOG_DBG("prompt: \"%S\" (cursor=%zu, length=%zu)",492- c->prompt.text, c->prompt.cursor, new_len);493-494- update_matches(c);495- refresh(c);496- }497-498- start_repeater(c, key - 8);499-500-}501-502-static void503-keyboard_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;508-509- LOG_DBG("modifiers: depressed=0x%x, latched=0x%x, locked=0x%x, group=%u",510- mods_depressed, mods_latched, mods_locked, group);511-512- xkb_state_update_mask(513- c->wl.xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);514-}515-516-static void517-keyboard_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-}525-526-static 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-};534-535-static void536-seat_handle_capabilities(void *data, struct wl_seat *wl_seat,537- enum wl_seat_capability caps)538-{539- struct context *c = data;540-541- if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD))542- return;543-544- if (c->wl.keyboard != NULL)545- wl_keyboard_release(c->wl.keyboard);546-547- c->wl.keyboard = wl_seat_get_keyboard(wl_seat);548- wl_keyboard_add_listener(c->wl.keyboard, &keyboard_listener, c);549-}550-551-static void552-seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)553-{554-}555-556-static const struct wl_seat_listener seat_listener = {557- .capabilities = seat_handle_capabilities,558- .name = seat_handle_name,559-};560-561-static void562-output_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-}571-572-static void573-output_mode(void *data, struct wl_output *wl_output, uint32_t flags,574- int32_t width, int32_t height, int32_t refresh)575-{576-}577-578-static void579-output_done(void *data, struct wl_output *wl_output)580-{581-}582-583-static void584-output_scale(void *data, struct wl_output *wl_output, int32_t factor)585-{586- struct monitor *mon = data;587- mon->scale = factor;588-}589-590-static const struct wl_output_listener output_listener = {591- .geometry = &output_geometry,592- .mode = &output_mode,593- .done = &output_done,594- .scale = &output_scale,595-};596-597-static void598-xdg_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-}605-606-static void607-xdg_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-}614-615-static void616-xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)617-{618-}619-620-static void621-xdg_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-}627-628-static void629-xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,630- const char *description)631-{632-}633-634-static 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-};641-642-static void643-handle_global(void *data, struct wl_registry *registry,644- uint32_t name, const char *interface, uint32_t version)645-{646- struct context *c = data;647-648- 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- }652-653- 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- }658-659- 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);662-663- tll_push_back(c->wl.monitors, ((struct monitor){.output = output}));664-665- struct monitor *mon = &tll_back(c->wl.monitors);666- wl_output_add_listener(output, &output_listener, mon);667-668- /*669- * The "output" interface doesn't give us the monitors'670- * identifiers (e.g. "LVDS-1"). Use the XDG output interface671- * for that.672- */673-674- 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);677-678- zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);679- wl_display_roundtrip(c->wl.display);680- }681-682- 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- }686-687- 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- }692-693- 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-}698-699-static void700-handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)701-{702- LOG_WARN("global removed: %u", name);703-}704-705-static const struct wl_registry_listener registry_listener = {706- .global = &handle_global,707- .global_remove = &handle_global_remove,708-};709-710-static void711-layer_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-}716-717-static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {718- .configure = layer_surface_configure,719-};720-721-static int722-sort_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-}728-729-static wchar_t *730-wcscasestr(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);734-735- if (needle_len > hay_len)736- return NULL;737-738- 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- }746-747- if (matched)748- return (wchar_t *)&haystack[i];749- }750-751- return NULL;752-}753-754-static void755-update_matches(struct context *c)756-{757- /* Nothing entered; all programs found matches */758- if (wcslen(c->prompt.text) == 0) {759-760- 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- }766-767- /* Sort */768- c->match_count = c->applications.count;769- qsort(c->matches, c->match_count, sizeof(c->matches[0]), &sort_match_by_count);770-771- /* Limit count (don't render outside window) */772- if (c->match_count > max_matches)773- c->match_count = max_matches;774-775- if (c->selected >= c->match_count && c->selected > 0)776- c->selected = c->match_count - 1;777- return;778- }779-780- 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;785-786- const wchar_t *m = wcscasestr(app->title, c->prompt.text);787- if (m != NULL)788- start_title = m - app->title;789-790- if (app->comment != NULL) {791- m = wcscasestr(app->comment, c->prompt.text);792- if (m != NULL)793- start_comment = m - app->comment;794- }795-796- if (start_title == -1 && start_comment == -1)797- continue;798-799- c->matches[c->match_count++] = (struct match){800- .application = app,801- .start_title = start_title,802- .start_comment = start_comment};803- }804-805- /* Sort */806- qsort(c->matches, c->match_count, sizeof(c->matches[0]), &sort_match_by_count);807-808- /* Limit count (don't render outside window) */809- if (c->match_count > max_matches)810- c->match_count = max_matches;811-812- if (c->selected >= c->match_count && c->selected > 0)813- c->selected = c->match_count - 1;814-}815-816-static void frame_callback(817- void *data, struct wl_callback *wl_callback, uint32_t callback_data);818-819-static const struct wl_callback_listener frame_listener = {820- .done = &frame_callback,821-};822-823-static void824-commit_buffer(struct context *c, struct buffer *buf)825-{826- assert(buf->busy);827-828- 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);831-832- struct wl_callback *cb = wl_surface_frame(c->wl.surface);833- wl_callback_add_listener(cb, &frame_listener, c);834-835- c->frame_is_scheduled = true;836- wl_surface_commit(c->wl.surface);837-}838-839-static void840-frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_data)841-{842- struct context *c = data;843-844- c->frame_is_scheduled = false;845- wl_callback_destroy(wl_callback);846-847- if (c->pending != NULL) {848- commit_buffer(c, c->pending);849- c->pending = NULL;850- }851-}852-853-static void854-refresh(struct context *c)855-{856- LOG_DBG("refresh");857-858- struct buffer *buf = shm_get_buffer(c->wl.shm, c->width, c->height);859-860- /* Background + border */861- render_background(c->render, buf);862-863- /* 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);867-868- cairo_surface_flush(buf->cairo_surface);869-870- if (c->frame_is_scheduled) {871- /* There's already a frame being drawn - delay current frame872- * (overwriting any previous pending frame) */873-874- if (c->pending != NULL)875- c->pending->busy = false;876-877- c->pending = buf;878- } else {879- /* No pending frames - render immediately */880- assert(c->pending == NULL);881- commit_buffer(c, buf);882- }883-}884-28 static struct rgba29 hex_to_rgba(uint32_t color)30 {31 return (struct rgba){170 unmodified lines202 203 const char *output_name = NULL;204 const char *font_name = "monospace";205 const char *icon_theme = "hicolor";1063-1064- 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;207 208+ 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+ };221+222 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 lines237 icon_theme = optarg;238 break;239 240 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;246 1102- 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+ }256 1109- 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+ }266 1116- 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+ }276 1123- 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+ }286 287 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;294 295 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;301 1145- 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+ }311 312 case 'd':313 dmenu_mode = true;314 break;19 unmodified lines334 int ret = EXIT_FAILURE;335 336 setlocale(LC_ALL, "");337 1178- 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;346 1193- 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);1196-1197- 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);1203-1204- 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- }349 1210- 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);353 1216- wl_registry_add_listener(c.wl.registry, ®istry_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;361 1219- 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);363 1232- 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);370 1237- 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);1242-1243- 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- }1251-1252- 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- }373 1257- if (c.wl.monitor == NULL) {1258- LOG_ERR("no outputs found");374+ if ((fdm = fdm_init()) == NULL)375 goto out;1260- }376 1262- 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- }379 1268-#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- }382 1275- 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);384 1282- 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");1285-1286- 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- }389 1291- const int scale = c.wl.monitor->scale;1292-1293- width /= scale; width *= scale;1294- height /= scale; height *= scale;1295-1296- c.width = width;1297- c.height = height;1298-1299- 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);1301-1302- zwlr_layer_surface_v1_add_listener(1303- c.wl.layer_surface, &layer_surface_listener, &c.wl);1304-1305- /* 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);1308-1309- 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);1312-1313- 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);1327-1328- update_matches(&c);1329- refresh(&c);1330-1331- wl_display_dispatch_pending(c.wl.display);1332- wl_display_flush(c.wl.display);1333-1334- 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- };1339-1340- wl_display_flush(c.wl.display);1341- poll(fds, 2, -1);1342-1343- 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- }1347-1348- if (fds[1].revents & POLLIN) {1349- uint64_t expiration_count;1350- ssize_t ret = read(1351- c.repeat.fd, &expiration_count, sizeof(expiration_count));1352-1353- 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- }1362-1363- if (fds[0].revents & POLLIN)1364- wl_display_dispatch(c.wl.display);394 }395 1367- if (c.status == EXIT_UPDATE_CACHE)1368- write_cache(&c.applications);396+ if (wayl_update_cache(wayl))397+ write_cache(&applications);398 1370- ret = c.exit_code;399+ ret = wayl_exit_code(wayl);400 401 out:1373-1374- render_destroy(c.render);402 shm_fini();403 1377- 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);409 410+ for (size_t i = 0; i < applications.count; i++) {411+ struct application *app = &applications.v[i];412+413 free(app->path);414 free(app->exec);415 free(app->title);416 free(app->comment);1 unmodified line418 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);423 1394- 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- }1402-1403- if (c.wl.xdg_output_manager != NULL)1404- zxdg_output_manager_v1_destroy(c.wl.xdg_output_manager);1405-1406- 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);1430-424 cairo_debug_reset_static_data();425 426 return ret;427 }added:match.c+188 added
1+#include "match.h"2+3+#include <wctype.h>4+#include <assert.h>5+6+struct 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+};13+14+struct matches *15+matches_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+}27+28+void29+matches_destroy(struct matches *matches)30+{31+ if (matches == NULL)32+ return;33+34+ free(matches->matches);35+ free(matches);36+}37+38+const struct match *39+matches_get(const struct matches *matches, size_t idx)40+{41+ assert(idx < matches->match_count);42+ return &matches->matches[idx];43+}44+45+const struct match *46+matches_get_match(const struct matches *matches)47+{48+ return matches->match_count > 049+ ? matches_get(matches, matches->selected)50+ : NULL;51+}52+53+size_t54+matches_get_count(const struct matches *matches)55+{56+ return matches->match_count;57+}58+59+size_t60+matches_get_match_index(const struct matches *matches)61+{62+ return matches->selected;63+}64+65+bool66+matches_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+ }75+76+ return false;77+}78+79+bool80+matches_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+ }89+90+ return false;91+}92+93+static int94+sort_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+}100+101+static wchar_t *102+wcscasestr(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);106+107+ if (needle_len > hay_len)108+ return NULL;109+110+ 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+ }118+119+ if (matched)120+ return (wchar_t *)&haystack[i];121+ }122+123+ return NULL;124+}125+126+void127+matches_update(struct matches *matches, const struct prompt *prompt)128+{129+ const wchar_t *ptext = prompt_text(prompt);130+131+ /* Nothing entered; all programs found matches */132+ if (wcslen(ptext) == 0) {133+134+ 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+ }140+141+ /* Sort */142+ matches->match_count = matches->applications->count;143+ qsort(matches->matches, matches->match_count, sizeof(matches->matches[0]), &sort_match_by_count);144+145+ /* Limit count (don't render outside window) */146+ if (matches->match_count > matches->max_matches)147+ matches->match_count = matches->max_matches;148+149+ if (matches->selected >= matches->match_count && matches->selected > 0)150+ matches->selected = matches->match_count - 1;151+ return;152+ }153+154+ 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;159+160+ const wchar_t *m = wcscasestr(app->title, ptext);161+ if (m != NULL)162+ start_title = m - app->title;163+164+ if (app->comment != NULL) {165+ m = wcscasestr(app->comment, ptext);166+ if (m != NULL)167+ start_comment = m - app->comment;168+ }169+170+ if (start_title == -1 && start_comment == -1)171+ continue;172+173+ matches->matches[matches->match_count++] = (struct match){174+ .application = app,175+ .start_title = start_title,176+ .start_comment = start_comment};177+ }178+179+ /* Sort */180+ qsort(matches->matches, matches->match_count, sizeof(matches->matches[0]), &sort_match_by_count);181+182+ /* Limit count (don't render outside window) */183+ if (matches->match_count > matches->max_matches)184+ matches->match_count = matches->max_matches;185+186+ if (matches->selected >= matches->match_count && matches->selected > 0)187+ matches->selected = matches->match_count - 1;188+}modified:match.h+18 added
1 #pragma once2 3+#include <stdbool.h>4 #include <unistd.h>5+6 #include "application.h"7+#include "prompt.h"8 9 struct match {10 struct application *application;11 ssize_t start_title;12 ssize_t start_comment;13 };14+15+struct matches;16+struct matches *matches_init(17+ const struct application_list *applications, size_t max_matches);18+void matches_destroy(struct matches *matches);19+20+void matches_update(struct matches *matches, const struct prompt *prompt);21+22+const struct match *matches_get(const struct matches *matches, size_t idx);23+const struct match *matches_get_match(const struct matches *matches);24+size_t matches_get_count(const struct matches *matches);25+size_t matches_get_match_index(const struct matches *matches);26+27+bool matches_selected_prev(struct matches *matches, bool wrap);28+bool matches_selected_next(struct matches *matches, bool wrap);modified:meson.build+4 added-1 removed
62 unmodified lines63 executable(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 linesadded:prompt.c+262 added
1+#include "prompt.h"2+3+#include <stdlib.h>4+#include <string.h>5+#include <wctype.h>6+7+#define LOG_MODULE "prompt"8+#define LOG_ENABLE_DBG 09+#include "log.h"10+11+struct prompt {12+ wchar_t *prompt;13+ wchar_t *text;14+ size_t cursor;15+};16+17+struct prompt *18+prompt_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+ };26+27+ return prompt;28+}29+30+void31+prompt_destroy(struct prompt *prompt)32+{33+ if (prompt == NULL)34+ return;35+36+ free(prompt->prompt);37+ free(prompt->text);38+ free(prompt);39+}40+41+bool42+prompt_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);47+48+ 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;52+53+ memmove(&new_text[prompt->cursor + wlen],54+ &new_text[prompt->cursor],55+ (wcslen(new_text) - prompt->cursor + 1) * sizeof(wchar_t));56+57+ b = text;58+ ps = (mbstate_t){0};59+ mbsnrtowcs(&new_text[prompt->cursor], &b, len, wlen + 1, &ps);60+61+ prompt->text = new_text;62+ prompt->cursor += wlen;63+64+ LOG_DBG("prompt: \"%S\" (cursor=%zu, length=%zu)",65+ prompt->text, prompt->cursor, new_len);66+67+ return true;68+}69+70+const wchar_t *71+prompt_prompt(const struct prompt *prompt)72+{73+ return prompt->prompt;74+}75+76+const wchar_t *77+prompt_text(const struct prompt *prompt)78+{79+ return prompt->text;80+}81+82+size_t83+prompt_cursor(const struct prompt *prompt)84+{85+ return prompt->cursor;86+}87+88+static size_t89+idx_next_char(struct prompt *prompt)90+{91+ if (prompt->text[prompt->cursor] == L'\0')92+ return prompt->cursor;93+ return prompt->cursor + 1;94+}95+96+static size_t97+idx_prev_char(const struct prompt *prompt)98+{99+ if (prompt->cursor == 0)100+ return 0;101+102+ return prompt->cursor - 1;103+}104+105+static size_t106+idx_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];110+111+ /* Ignore initial spaces */112+ while (space >= prompt->text && iswspace(*space))113+ space--;114+115+ /* Skip non-spaces */116+ while (space >= prompt->text && !iswspace(*space))117+ space--;118+119+ return space - prompt->text + 1;120+}121+122+static size_t123+idx_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];127+128+ /* Ignore initial non-spaces */129+ while (space < end && !iswspace(*space))130+ space++;131+132+ /* Skip spaces */133+ while (space < end && iswspace(*space))134+ space++;135+136+ return space - prompt->text;137+}138+139+bool140+prompt_cursor_home(struct prompt *prompt)141+{142+ if (prompt->cursor == 0)143+ return false;144+145+ prompt->cursor = 0;146+ return true;147+}148+149+bool150+prompt_cursor_end(struct prompt *prompt)151+{152+ size_t text_len = wcslen(prompt->text);153+ if (prompt->cursor >= text_len)154+ return false;155+156+ prompt->cursor = text_len;157+ return true;158+}159+160+bool161+prompt_cursor_next_char(struct prompt *prompt)162+{163+ size_t idx = idx_next_char(prompt);164+ if (idx == prompt->cursor)165+ return false;166+167+ prompt->cursor = idx;168+ return true;169+}170+171+bool172+prompt_cursor_prev_char(struct prompt *prompt)173+{174+ size_t idx = idx_prev_char(prompt);175+ if (idx == prompt->cursor)176+ return false;177+178+ prompt->cursor = idx;179+ return true;180+}181+182+bool183+prompt_cursor_prev_word(struct prompt *prompt)184+{185+ size_t idx = idx_prev_word(prompt);186+ if (idx == prompt->cursor)187+ return false;188+189+ prompt->cursor = idx;190+ return true;191+}192+193+bool194+prompt_cursor_next_word(struct prompt *prompt)195+{196+ size_t idx = idx_next_word(prompt);197+ if (idx == prompt->cursor)198+ return false;199+200+ prompt->cursor = idx;201+ return true;202+}203+204+bool205+prompt_erase_next_char(struct prompt *prompt)206+{207+ if (prompt->cursor >= wcslen(prompt->text))208+ return false;209+210+ 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+}216+217+bool218+prompt_erase_prev_char(struct prompt *prompt)219+{220+221+ if (prompt->cursor == 0)222+ return false;223+224+ size_t prev_char = idx_prev_char(prompt);225+ prompt->text[prev_char] = L'\0';226+ prompt->cursor = prev_char;227+ return true;228+}229+230+bool231+prompt_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;236+237+ memmove(&prompt->text[prompt->cursor],238+ &prompt->text[next_word],239+ (wcslen(prompt->text) - next_word + 1) * sizeof(wchar_t));240+ return true;241+}242+243+bool244+prompt_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;249+250+ 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+}256+257+bool258+prompt_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 once2 3 #include <stddef.h>4+#include <stdbool.h>5 #include <wchar.h>6 6-struct prompt {7- wchar_t *prompt;8- wchar_t *text;9- size_t cursor;10-};7+struct prompt;8+struct prompt *prompt_init(const wchar_t *prompt_text);9+void prompt_destroy(struct prompt *prompt);10+11+bool prompt_insert_chars(struct prompt *prompt, const char *text, size_t len);12+13+const wchar_t *prompt_prompt(const struct prompt *prompt);14+const wchar_t *prompt_text(const struct prompt *prompt);15+size_t prompt_cursor(const struct prompt *prompt);16+17+bool prompt_cursor_home(struct prompt *prompt);18+bool prompt_cursor_end(struct prompt *prompt);19+bool prompt_cursor_next_char(struct prompt *prompt);20+bool prompt_cursor_prev_char(struct prompt *prompt);21+bool prompt_cursor_prev_word(struct prompt *prompt);22+bool prompt_cursor_next_word(struct prompt *prompt);23+24+bool prompt_erase_next_char(struct prompt *prompt);25+bool prompt_erase_prev_char(struct prompt *prompt);26+bool prompt_erase_next_word(struct prompt *prompt);27+bool prompt_erase_prev_word(struct prompt *prompt);28+bool prompt_erase_after_cursor(struct prompt *prompt);modified:render.c+16 added-12 removed
7 unmodified lines8 #include "log.h"9 #include "font.h"10 11 struct render {12- struct options options;12+ struct render_options options;13 struct font *regular_font;14 };15 16 void86 unmodified lines103 render_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);107 108+ const wchar_t *pprompt = prompt_prompt(prompt);109+ const size_t prompt_len = wcslen(pprompt);110+111+ const wchar_t *ptext = prompt_text(prompt);112+ const size_t text_len = wcslen(ptext);113+114 int x = render->options.border_size + render->options.x_margin;115 int y = render->options.border_size + render->options.y_margin + font->fextents.ascent;116 117 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;122 123 render_glyph(buf->pix, glyph, x, y, &render->options.pix_text_color);124 x += glyph->x_advance;125 126 /* 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 lines157 }158 159 void160 render_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);169 170 assert(match_count == 0 || selected < match_count);171 172 const double row_height = 2 * y_margin + font->fextents.height;6 unmodified lines179 */180 double y = first_row + (row_height + font->fextents.height) / 2 - font->fextents.descent;181 182 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];184 185 /* Hightlight selected entry */186 if (i == selected) {187 const struct rgba *sc = &render->options.selection_color;71 unmodified lines259 260 /* 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);266 267 y += row_height;268 }269 }270 271 struct render *267-render_init(struct font *font, struct options options)272+render_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;277 278 /* TODO: the one providing the options should calculate these */279 render->options.pix_background_color = rgba2pixman(render->options.background_color);9 unmodified lines289 {290 if (render == NULL)291 return;292 288- //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 lines9 #include "tllist.h"10 11 struct rgba {double r; double g; double b; double a;};12 13-struct options {13+struct render_options {14 int width;15 int height;16 int x_margin;17 int y_margin;14 unmodified lines32 pixman_color_t pix_selection_color;33 };34 35 struct render;36-struct render *render_init(struct font *font, struct options options);36+struct render *render_init(struct font *font, const struct render_options *options);37 void render_destroy(struct render *render);38 39 void render_background(const struct render *render, struct buffer *buf);40 41 void render_prompt(42 const struct render *render, struct buffer *buf,43 const struct prompt *prompt);44 45-void 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);45+void 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"2+3+#include <stdlib.h>4+#include <wctype.h>5+#include <unistd.h>6+#include <errno.h>7+8+#include <sys/mman.h>9+#include <sys/epoll.h>10+#include <sys/timerfd.h>11+12+#include <wayland-client.h>13+#include <wayland-cursor.h>14+#include <wayland-util.h>15+16+#include <xkbcommon/xkbcommon.h>17+#include <xkbcommon/xkbcommon-keysyms.h>18+19+#include <xdg-output-unstable-v1.h>20+#include <wlr-layer-shell-unstable-v1.h>21+22+#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"30+31+struct monitor {32+ struct wl_output *output;33+ struct zxdg_output_v1 *xdg;34+ char *name;35+36+ int x;37+ int y;38+39+ int width_mm;40+ int height_mm;41+42+ int width_px;43+ int height_px;44+45+ int scale;46+};47+48+struct repeat {49+ int fd;50+ int32_t delay;51+ int32_t rate;52+53+ bool dont_re_repeat;54+ uint32_t key;55+};56+57+struct wayland {58+ struct fdm *fdm;59+ struct render *render;60+ struct prompt *prompt;61+ struct matches *matches;62+63+ int width;64+ int height;65+66+ struct repeat repeat;67+68+ enum { KEEP_RUNNING, EXIT_UPDATE_CACHE, EXIT} status;69+ int exit_code;70+ bool dmenu_mode;71+72+ bool frame_is_scheduled;73+ struct buffer *pending;74+75+ 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;85+86+ tll(struct monitor) monitors;87+ const struct monitor *monitor;88+89+ struct xkb_context *xkb;90+ struct xkb_keymap *xkb_keymap;91+ struct xkb_state *xkb_state;92+};93+94+static void refresh(struct wayland *wayl);95+96+bool97+repeat_start(struct repeat *repeat, uint32_t key)98+{99+ if (repeat->dont_re_repeat)100+ return true;101+102+ 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+ };106+107+ 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+ }119+120+ repeat->key = key;121+ return true;122+}123+124+bool125+repeat_stop(struct repeat *repeat, uint32_t key)126+{127+ if (key != (uint32_t)-1 && key != repeat->key)128+ return true;129+130+ if (timerfd_settime(repeat->fd, 0, &(struct itimerspec){{0}}, NULL) < 0) {131+ LOG_ERRNO("failed to disarm keyboard repeat timer");132+ return false;133+ }134+135+ return true;136+}137+138+static void139+shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)140+{141+}142+143+static const struct wl_shm_listener shm_listener = {144+ .format = &shm_format,145+};146+147+static void148+keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,149+ uint32_t format, int32_t fd, uint32_t size)150+{151+ struct wayland *wayl = data;152+153+ char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);154+155+ 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);159+160+ /* TODO: initialize in enter? */161+ wayl->xkb_state = xkb_state_new(wayl->xkb_keymap);162+163+ munmap(map_str, size);164+ close(fd);165+}166+167+static void168+keyboard_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+}178+179+static void180+keyboard_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+}187+188+static void189+keyboard_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;196+197+ struct wayland *wayl = data;198+199+ 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+ }205+206+ if (state == XKB_KEY_UP) {207+ repeat_stop(&wayl->repeat, key);208+ return;209+ }210+211+ key += 8;212+ xkb_keysym_t sym = xkb_state_key_get_one_sym(wayl->xkb_state, key);213+214+ 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;219+220+#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+#endif227+228+ LOG_DBG("sym=%u, mod=0x%08x, consumed=0x%08x, significant=0x%08x, "229+ "effective=0x%08x",230+ sym, mods, consumed, significant, effective_mods);231+232+ if (sym == XKB_KEY_Home || (sym == XKB_KEY_a && effective_mods == ctrl)) {233+ if (prompt_cursor_home(wayl->prompt))234+ refresh(wayl);235+ }236+237+ else if (sym == XKB_KEY_End || (sym == XKB_KEY_e && effective_mods == ctrl)) {238+ if (prompt_cursor_end(wayl->prompt))239+ refresh(wayl);240+ }241+242+ 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+ }247+248+ else if ((sym == XKB_KEY_f && effective_mods == alt) ||249+ (sym == XKB_KEY_Right && effective_mods == ctrl)) {250+251+ if (prompt_cursor_next_word(wayl->prompt))252+ refresh(wayl);253+ }254+255+ else if ((sym == XKB_KEY_Escape && effective_mods == 0) ||256+ (sym == XKB_KEY_g && effective_mods == ctrl)) {257+ wayl->status = EXIT;258+ }259+260+ 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+ }265+266+ 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+ }271+272+ else if (sym == XKB_KEY_Tab && effective_mods == 0) {273+ if (matches_selected_next(wayl->matches, true))274+ refresh(wayl);275+ }276+277+ else if (sym == XKB_KEY_ISO_Left_Tab && effective_mods == 0) {278+ if (matches_selected_prev(wayl->matches, true))279+ refresh(wayl);280+ }281+282+ 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+ }287+288+ 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+ }293+294+ 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+ }301+302+ 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+ }308+309+ 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+ }316+317+ 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+ }324+325+ 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+ }331+332+ else if (sym == XKB_KEY_Return && effective_mods == 0) {333+ wayl->status = EXIT;334+335+ const struct match *match = matches_get_match(wayl->matches);336+ struct application *app = match != NULL ? match->application : NULL;337+338+ 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;348+349+ if (success && match != NULL) {350+ wayl->status = EXIT_UPDATE_CACHE;351+ app->count++;352+ }353+ }354+ }355+356+ 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));359+360+ if (count == 0)361+ return;362+363+ if (!prompt_insert_chars(wayl->prompt, buf, count))364+ return;365+366+ matches_update(wayl->matches, wayl->prompt);367+ refresh(wayl);368+ }369+370+ repeat_start(&wayl->repeat, key - 8);371+372+}373+374+static void375+keyboard_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;380+381+ LOG_DBG("modifiers: depressed=0x%x, latched=0x%x, locked=0x%x, group=%u",382+ mods_depressed, mods_latched, mods_locked, group);383+384+ xkb_state_update_mask(385+ wayl->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);386+}387+388+static void389+keyboard_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+}397+398+static 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+};406+407+static void408+seat_handle_capabilities(void *data, struct wl_seat *wl_seat,409+ enum wl_seat_capability caps)410+{411+ struct wayland *wayl = data;412+413+ if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD))414+ return;415+416+ if (wayl->keyboard != NULL)417+ wl_keyboard_release(wayl->keyboard);418+419+ wayl->keyboard = wl_seat_get_keyboard(wl_seat);420+ wl_keyboard_add_listener(wayl->keyboard, &keyboard_listener, wayl);421+}422+423+static void424+seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)425+{426+}427+428+static const struct wl_seat_listener seat_listener = {429+ .capabilities = seat_handle_capabilities,430+ .name = seat_handle_name,431+};432+433+static void434+output_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+}443+444+static void445+output_mode(void *data, struct wl_output *wl_output, uint32_t flags,446+ int32_t width, int32_t height, int32_t refresh)447+{448+}449+450+static void451+output_done(void *data, struct wl_output *wl_output)452+{453+}454+455+static void456+output_scale(void *data, struct wl_output *wl_output, int32_t factor)457+{458+ struct monitor *mon = data;459+ mon->scale = factor;460+}461+462+static const struct wl_output_listener output_listener = {463+ .geometry = &output_geometry,464+ .mode = &output_mode,465+ .done = &output_done,466+ .scale = &output_scale,467+};468+469+static void470+xdg_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+}477+478+static void479+xdg_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+}486+487+static void488+xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)489+{490+}491+492+static void493+xdg_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+}499+500+static void501+xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,502+ const char *description)503+{504+}505+506+static 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+};513+514+static void515+handle_global(void *data, struct wl_registry *registry,516+ uint32_t name, const char *interface, uint32_t version)517+{518+ struct wayland *wayl = data;519+520+ if (strcmp(interface, wl_compositor_interface.name) == 0) {521+ wayl->compositor = wl_registry_bind(522+ wayl->registry, name, &wl_compositor_interface, 4);523+ }524+525+ 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+ }530+531+ 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);534+535+ tll_push_back(wayl->monitors, ((struct monitor){.output = output}));536+537+ struct monitor *mon = &tll_back(wayl->monitors);538+ wl_output_add_listener(output, &output_listener, mon);539+540+ /*541+ * The "output" interface doesn't give us the monitors'542+ * identifiers (e.g. "LVDS-1"). Use the XDG output interface543+ * for that.544+ */545+546+ assert(wayl->xdg_output_manager != NULL);547+ mon->xdg = zxdg_output_manager_v1_get_xdg_output(548+ wayl->xdg_output_manager, mon->output);549+550+ zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);551+ wl_display_roundtrip(wayl->display);552+ }553+554+ 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+ }558+559+ 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+ }564+565+ 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+}570+571+static void572+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)573+{574+ LOG_WARN("global removed: %u", name);575+}576+577+static const struct wl_registry_listener registry_listener = {578+ .global = &handle_global,579+ .global_remove = &handle_global_remove,580+};581+582+static void583+layer_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+}588+589+static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {590+ .configure = layer_surface_configure,591+};592+593+static void frame_callback(594+ void *data, struct wl_callback *wl_callback, uint32_t callback_data);595+596+static const struct wl_callback_listener frame_listener = {597+ .done = &frame_callback,598+};599+600+static void601+commit_buffer(struct wayland *wayl, struct buffer *buf)602+{603+ assert(buf->busy);604+605+ 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);608+609+ struct wl_callback *cb = wl_surface_frame(wayl->surface);610+ wl_callback_add_listener(cb, &frame_listener, wayl);611+612+ wayl->frame_is_scheduled = true;613+ wl_surface_commit(wayl->surface);614+}615+616+static void617+frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_data)618+{619+ struct wayland *wayl = data;620+621+ wayl->frame_is_scheduled = false;622+ wl_callback_destroy(wl_callback);623+624+ if (wayl->pending != NULL) {625+ commit_buffer(wayl, wayl->pending);626+ wayl->pending = NULL;627+ }628+}629+630+static void631+refresh(struct wayland *wayl)632+{633+ struct buffer *buf = shm_get_buffer(wayl->shm, wayl->width, wayl->height);634+635+ /* Background + border */636+ render_background(wayl->render, buf);637+638+ /* Window content */639+ render_prompt(wayl->render, buf, wayl->prompt);640+ render_match_list(wayl->render, buf, wayl->prompt, wayl->matches);641+642+ cairo_surface_flush(buf->cairo_surface);643+644+ if (wayl->frame_is_scheduled) {645+ /* There's already a frame being drawn - delay current frame646+ * (overwriting any previous pending frame) */647+648+ if (wayl->pending != NULL)649+ wayl->pending->busy = false;650+651+ wayl->pending = buf;652+ } else {653+ /* No pending frames - render immediately */654+ assert(wayl->pending == NULL);655+ commit_buffer(wayl, buf);656+ }657+}658+659+static bool660+fdm_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);664+665+ if (events & EPOLLHUP) {666+ LOG_WARN("disconnected from Wayland");667+ return false;668+ }669+670+ return event_count != -1 && wayl->status == KEEP_RUNNING;671+}672+673+static bool674+fdm_repeat(struct fdm *fdm, int fd, int events, void *data)675+{676+ struct wayland *wayl = data;677+ struct repeat *repeat = &wayl->repeat;678+679+ uint64_t expiration_count;680+ ssize_t ret = read(681+ repeat->fd, &expiration_count, sizeof(expiration_count));682+683+ if (ret < 0 && errno != EAGAIN) {684+ LOG_ERRNO("failed to read key repeat count from timer fd");685+ return false;686+ }687+688+ 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;692+693+ if (events & EPOLLHUP) {694+ LOG_ERR("keyboard repeater timer FD closed unexpectedly");695+ return false;696+ }697+698+ return true;699+}700+701+struct wayland *702+wayl_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));707+708+ 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;715+716+ 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+ }721+722+ 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+ }727+728+ 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+ }733+734+ wl_registry_add_listener(wayl->registry, ®istry_listener, wayl);735+ wl_display_roundtrip(wayl->display);736+737+ 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+ }749+750+ if (tll_length(wayl->monitors) == 0) {751+ LOG_ERR("no monitors");752+ goto out;753+ }754+755+ 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);760+761+ 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+ }769+770+ if (wayl->monitor == NULL && output_name != NULL) {771+ LOG_ERR("%s: no output with that name found", output_name);772+ goto out;773+ }774+775+ if (wayl->monitor == NULL) {776+ LOG_ERR("no outputs found");777+ goto out;778+ }779+780+ 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+ }785+786+#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+ }792+793+ 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+#endif799+800+ 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");803+804+ if (wayl->layer_surface == NULL) {805+ LOG_ERR("failed to create layer shell surface");806+ goto out;807+ }808+809+ const int scale = wayl->monitor->scale;810+811+ width /= scale; width *= scale;812+ height /= scale; height *= scale;813+814+ wayl->width = width;815+ wayl->height = height;816+817+ 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);819+820+ zwlr_layer_surface_v1_add_listener(821+ wayl->layer_surface, &layer_surface_listener, wayl);822+823+ /* Trigger a 'configure' event, after which we'll have the width */824+ wl_surface_commit(wayl->surface);825+ wl_display_roundtrip(wayl->display);826+827+ 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+ }831+832+ 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+ }836+837+ refresh(wayl);838+ return wayl;839+840+out:841+ wayl_destroy(wayl);842+ return NULL;843+}844+845+void846+wayl_destroy(struct wayland *wayl)847+{848+ if (wayl == NULL)849+ return;850+851+ if (wayl->display != NULL)852+ fdm_del(wayl->fdm, wl_display_get_fd(wayl->display));853+854+ if (wayl->repeat.fd > 0) {855+ fdm_del(wayl->fdm, wayl->repeat.fd);856+ close(wayl->repeat.fd);857+ }858+859+ 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+ }867+868+ if (wayl->xdg_output_manager != NULL)869+ zxdg_output_manager_v1_destroy(wayl->xdg_output_manager);870+871+ 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);895+896+ free(wayl);897+}898+899+void900+wayl_flush(struct wayland *wayl)901+{902+ wl_display_flush(wayl->display);903+}904+905+bool906+wayl_exit_code(const struct wayland *wayl)907+{908+ return wayl->exit_code;909+}910+911+bool912+wayl_update_cache(const struct wayland *wayl)913+{914+ return wayl->status == EXIT_UPDATE_CACHE;915+}added:wayland.h+19 added
1+#pragma once2+3+#include "fdm.h"4+5+#include "prompt.h"6+#include "match.h"7+#include "render.h"8+9+struct wayland;10+struct 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);14+void wayl_destroy(struct wayland *wayl);15+16+void wayl_flush(struct wayland *wayl);17+18+bool wayl_exit_code(const struct wayland *wayl);19+bool wayl_update_cache(const struct wayland *wayl);