Skip to content
Myr

Initial commit

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 · the first commit · Browse files · 18 files+1925 added−0 removed
This commit

State

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

Diff stats

18 files+1925 added−0 removed

  • other:+1925 added−0 removed

18 files+1925 added−0 removed

added:external/wlr-protocolsnot shown

Its text is not shown, added.

added:.gitignore+1 added
1bld/
added:.gitmodules+4 added
1[submodule "external/wlr-protocols"]2 path = external/wlr-protocols3 url = https://github.com/swaywm/wlr-protocols.git4 branch = master
added:application.h+6 added
1#pragma once23struct application {4 char *title;5 char *path;6};
added:CMakeLists.txt+84 added
1cmake_minimum_required(VERSION 3.13)2project(f00sel C)34set(CMAKE_C_STANDARD_REQUIRED ON)5set(CMAKE_C_EXTENSIONS OFF)6set(CMAKE_C_STANDARD 11)78set_property(DIRECTORY . APPEND PROPERTY COMPILE_DEFINITIONS9 $<$<CONFIG:Debug>:_DEBUG>10 _GNU_SOURCE11 )1213set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")1415find_package(PkgConfig REQUIRED)16pkg_check_modules(fontconfig REQUIRED IMPORTED_TARGET fontconfig)17pkg_check_modules(cairo REQUIRED IMPORTED_TARGET cairo cairo-ft)18pkg_check_modules(wayland REQUIRED IMPORTED_TARGET19 wayland-client wayland-cursor wlroots)20pkg_check_modules(xkb REQUIRED IMPORTED_TARGET xkbcommon)2122find_program(WAYLAND_SCANNER_EXECUTABLE wayland-scanner)23if (WAYLAND_SCANNER_EXECUTABLE-NOTFOUND)24 message(FATAL_ERROR "could not find wayland-scanner")25endif ()2627function (wayland_protocol _deps)28 set(deps "")29 foreach (xml_file ${ARGN})30 get_filename_component(base ${xml_file} NAME_WE)31 set(out_c ${base}.c)32 set(out_h ${base}.h)3334 add_custom_command(35 OUTPUT ${out_h}36 COMMAND ${WAYLAND_SCANNER_EXECUTABLE} client-header < ${xml_file} > ${out_h}37 VERBATIM38 MAIN_DEPENDENCY ${xml_file}39 )40 add_custom_command(41 OUTPUT ${out_c}42 COMMAND ${WAYLAND_SCANNER_EXECUTABLE} private-code < ${xml_file} > ${out_c}43 VERBATIM44 MAIN_DEPENDENCY ${xml_file}45 )4647 list(APPEND deps ${out_h})48 list(APPEND deps ${out_c})49 endforeach ()5051 set(${_deps} ${deps} PARENT_SCOPE)52endfunction ()5354pkg_check_modules(WAYLAND_PROTOCOLS REQUIRED wayland-protocols)55execute_process(56 COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-protocols57 OUTPUT_VARIABLE WAYLAND_PROTOCOLS58 OUTPUT_STRIP_TRAILING_WHITESPACE)5960wayland_protocol(61 wayland_protos62 ${PROJECT_SOURCE_DIR}/external/wlr-protocols/unstable/wlr-layer-shell-unstable-v1.xml63 ${WAYLAND_PROTOCOLS}/stable/xdg-shell/xdg-shell.xml64 ${WAYLAND_PROTOCOLS}/unstable/xdg-output/xdg-output-unstable-v1.xml65 )6667add_library(wayland-protocols STATIC EXCLUDE_FROM_ALL ${wayland_protos})68target_include_directories(wayland-protocols PUBLIC ${CMAKE_CURRENT_BINARY_DIR})6970add_executable(f00sel71 main.c72 font.c font.h73 render.c render.h74 shm.c shm.h75 log.c log.h76 xdg.c xdg.h77 tllist.h78 )79target_link_libraries(f00sel80 wayland-protocols81 PkgConfig::wayland82 PkgConfig::cairo83 PkgConfig::fontconfig84 PkgConfig::xkb)
added:font.c+84 added
1#include "font.h"23#include <stdlib.h>4#include <stdbool.h>56#include <fontconfig/fontconfig.h>7#include <cairo-ft.h>89#define LOG_MODULE "font"10#include "log.h"1112static void __attribute__((constructor))13init(void)14{15 FcInit();16}1718static void __attribute__((destructor))19fini(void)20{21 FcFini();22}23cairo_scaled_font_t *24font_from_name(const char *name, double *size)25{26 FcPattern *pattern = FcNameParse((const unsigned char *)name);27 if (pattern == NULL) {28 LOG_ERR("%s: failed to lookup font", name);29 return NULL;30 }3132 if (!FcConfigSubstitute(NULL, pattern, FcMatchPattern)) {33 LOG_ERR("%s: failed to do config substitution", name);34 FcPatternDestroy(pattern);35 return NULL;36 }3738 FcDefaultSubstitute(pattern);3940 FcResult result;41 FcPattern *final_pattern = FcFontMatch(NULL, pattern, &result);42 FcPatternDestroy(pattern);4344 if (final_pattern == NULL) {45 LOG_ERR("%s: failed to match font", name);46 return NULL;47 }4849 if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, size)) {50 LOG_ERR("%s: failed to get size", name);51 FcPatternDestroy(final_pattern);52 return NULL;53 }5455 cairo_font_face_t *face = cairo_ft_font_face_create_for_pattern(56 final_pattern);5758 FcPatternDestroy(final_pattern);5960 if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS) {61 LOG_ERR("%s: failed to create cairo font face", name);62 cairo_font_face_destroy(face);63 return NULL;64 }6566 cairo_matrix_t matrix, ctm;67 cairo_matrix_init_identity(&ctm);68 cairo_matrix_init_scale(&matrix, *size, *size);6970 cairo_font_options_t *options = cairo_font_options_create();71 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create(72 face, &matrix, &ctm, options);7374 cairo_font_options_destroy(options);75 cairo_font_face_destroy(face);7677 if (cairo_scaled_font_status(scaled_font) != CAIRO_STATUS_SUCCESS) {78 LOG_ERR("%s: failed to create scaled font", name);79 cairo_scaled_font_destroy(scaled_font);80 return NULL;81 }8283 return scaled_font;84}
added:font.h+5 added
1#pragma once23#include <cairo/cairo.h>45cairo_scaled_font_t *font_from_name(const char *name, double *size);
added:log.c+146 added
1#include "log.h"23#include <stdlib.h>4#include <stdio.h>5#include <stdarg.h>6#include <string.h>7#include <stdbool.h>8#include <errno.h>9#include <assert.h>10#include <unistd.h>1112#include <syslog.h>1314static bool colorize = false;1516static void __attribute__((constructor))17init(void)18{19 colorize = isatty(STDOUT_FILENO);20 openlog(NULL, /*LOG_PID*/0, LOG_USER);21 setlogmask(LOG_UPTO(LOG_WARNING));22}2324static void __attribute__((destructor))25fini(void)26{27 closelog();28}2930static void31_log(enum log_class log_class, const char *module, const char *file, int lineno,32 const char *fmt, int sys_errno, va_list va)33{34 const char *class = "abcd";35 int class_clr = 0;36 switch (log_class) {37 case LOG_CLASS_ERROR: class = " err"; class_clr = 31; break;38 case LOG_CLASS_WARNING: class = "warn"; class_clr = 33; break;39 case LOG_CLASS_INFO: class = "info"; class_clr = 97; break;40 case LOG_CLASS_DEBUG: class = " dbg"; class_clr = 36; break;41 }4243 char clr[16];44 snprintf(clr, sizeof(clr), "\e[%dm", class_clr);45 printf("%s%s%s: ", colorize ? clr : "", class, colorize ? "\e[0m" : "");4647 if (colorize)48 printf("\e[2m");49#if defined(_DEBUG)50 printf("%s:%d: ", file, lineno);51#else52 printf("%s: ", module);53#endif54 if (colorize)55 printf("\e[0m");5657 vprintf(fmt, va);5859 if (sys_errno != 0)60 printf(": %s", strerror(sys_errno));6162 printf("\n");63}6465static void66_sys_log(enum log_class log_class, const char *module, const char *file,67 int lineno, const char *fmt, int sys_errno, va_list va)68{69 /* Map our log level to syslog's level */70 int level = -1;71 switch (log_class) {72 case LOG_CLASS_ERROR: level = LOG_ERR; break;73 case LOG_CLASS_WARNING: level = LOG_WARNING; break;74 case LOG_CLASS_INFO: level = LOG_INFO; break;75 case LOG_CLASS_DEBUG: level = LOG_DEBUG; break;76 }7778 assert(level != -1);7980 const char *sys_err = sys_errno != 0 ? strerror(sys_errno) : NULL;8182 va_list va2;83 va_copy(va2, va);8485 /* Calculate required size of buffer holding the entire log message */86 int required_len = 0;87 required_len += strlen(module) + 2; /* "%s: " */88 required_len += vsnprintf(NULL, 0, fmt, va2); va_end(va2);8990 if (sys_errno != 0)91 required_len += strlen(sys_err) + 2; /* ": %s" */9293 /* Format the msg */94 char *msg = malloc(required_len + 1);95 int idx = 0;9697 idx += snprintf(&msg[idx], required_len + 1 - idx, "%s: ", module);98 idx += vsnprintf(&msg[idx], required_len + 1 - idx, fmt, va);99100 if (sys_errno != 0) {101 idx += snprintf(102 &msg[idx], required_len + 1 - idx, ": %s", strerror(sys_errno));103 }104105 syslog(level, "%s", msg);106 free(msg);107}108109void110log_msg(enum log_class log_class, const char *module,111 const char *file, int lineno, const char *fmt, ...)112{113 va_list ap1, ap2;114 va_start(ap1, fmt);115 va_copy(ap2, ap1);116 _log(log_class, module, file, lineno, fmt, 0, ap1);117 _sys_log(log_class, module, file, lineno, fmt, 0, ap2);118 va_end(ap1);119 va_end(ap2);120}121122void log_errno(enum log_class log_class, const char *module,123 const char *file, int lineno,124 const char *fmt, ...)125{126 va_list ap1, ap2;127 va_start(ap1, fmt);128 va_copy(ap2, ap1);129 _log(log_class, module, file, lineno, fmt, errno, ap1);130 _sys_log(log_class, module, file, lineno, fmt, errno, ap2);131 va_end(ap1);132 va_end(ap2);133}134135void log_errno_provided(enum log_class log_class, const char *module,136 const char *file, int lineno, int _errno,137 const char *fmt, ...)138{139 va_list ap1, ap2;140 va_start(ap1, fmt);141 va_copy(ap2, ap1);142 _log(log_class, module, file, lineno, fmt, _errno, ap1);143 _sys_log(log_class, module, file, lineno, fmt, _errno, ap2);144 va_end(ap1);145 va_end(ap2);146}
added:log.h+35 added
1#pragma once23enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG };45void log_msg(enum log_class log_class, const char *module,6 const char *file, int lineno,7 const char *fmt, ...) __attribute__((format (printf, 5, 6)));89void log_errno(enum log_class log_class, const char *module,10 const char *file, int lineno,11 const char *fmt, ...) __attribute__((format (printf, 5, 6)));1213void log_errno_provided(14 enum log_class log_class, const char *module,15 const char *file, int lineno, int _errno,16 const char *fmt, ...) __attribute__((format (printf, 6, 7)));1718#define LOG_ERR(fmt, ...) \19 log_msg(LOG_CLASS_ERROR, LOG_MODULE, __FILE__, __LINE__, fmt, ## __VA_ARGS__)20#define LOG_ERRNO(fmt, ...) \21 log_errno(LOG_CLASS_ERROR, LOG_MODULE, __FILE__, __LINE__, fmt, ## __VA_ARGS__)22#define LOG_ERRNO_P(fmt, _errno, ...) \23 log_errno_provided(LOG_CLASS_ERROR, LOG_MODULE, __FILE__, __LINE__, \24 _errno, fmt, ## __VA_ARGS__)25#define LOG_WARN(fmt, ...) \26 log_msg(LOG_CLASS_WARNING, LOG_MODULE, __FILE__, __LINE__, fmt, ## __VA_ARGS__)27#define LOG_INFO(fmt, ...) \28 log_msg(LOG_CLASS_INFO, LOG_MODULE, __FILE__, __LINE__, fmt, ## __VA_ARGS__)2930#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG31 #define LOG_DBG(fmt, ...) \32 log_msg(LOG_CLASS_DEBUG, LOG_MODULE, __FILE__, __LINE__, fmt, ## __VA_ARGS__)33#else34 #define LOG_DBG(fmt, ...)35#endif
added:main.c+858 added
1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4#include <stdbool.h>5#include <unistd.h>67#include <poll.h>89#include <sys/types.h>10#include <sys/mman.h>11#include <sys/wait.h>12#include <dirent.h>1314#include <cairo.h>1516#include <wayland-client.h>17#include <wayland-cursor.h>18#include <wayland-util.h>1920#include <xkbcommon/xkbcommon.h>21#include <xkbcommon/xkbcommon-keysyms.h>222324#include <xdg-output-unstable-v1.h>25#include <wlr-layer-shell-unstable-v1.h>2627#define LOG_MODULE "f00sel"28#define LOG_ENABLE_DBG 129#include "log.h"30#include "tllist.h"31#include "shm.h"32#include "font.h"33#include "render.h"34#include "application.h"35#include "match.h"36#include "xdg.h"3738struct monitor {39 struct wl_output *output;40 struct zxdg_output_v1 *xdg;41 char *name;4243 int x;44 int y;4546 int width_mm;47 int height_mm;4849 int width_px;50 int height_px;5152 int scale;53};5455struct wayland {56 struct wl_display *display;57 struct wl_registry *registry;58 struct wl_compositor *compositor;59 struct wl_surface *surface;60 struct zwlr_layer_shell_v1 *layer_shell;61 struct zwlr_layer_surface_v1 *layer_surface;62 struct wl_shm *shm;63 struct wl_seat *seat;64 struct wl_keyboard *keyboard;65 struct zxdg_output_manager_v1 *xdg_output_manager;6667 tll(struct monitor) monitors;68 const struct monitor *monitor;6970 struct xkb_context *xkb;71 struct xkb_keymap *xkb_keymap;72 struct xkb_state *xkb_state;73};7475static const int width = 500;76static const int height = 300;7778static double font_size;79static cairo_scaled_font_t *scaled_font;8081static struct prompt prompt;82static application_list_t applications;83static struct match *matches;84static size_t match_count;85static size_t selected;8687static bool keep_running = true;8889static void refresh(const struct wayland *wl);90static void update_matches(void);9192static void93shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)94{95}9697static const struct wl_shm_listener shm_listener = {98 .format = &shm_format,99};100101static void102keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,103 uint32_t format, int32_t fd, uint32_t size)104{105 struct wayland *wl = data;106107 char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);108109 wl->xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);110 wl->xkb_keymap = xkb_keymap_new_from_string(111 wl->xkb, map_str, XKB_KEYMAP_FORMAT_TEXT_V1,112 XKB_KEYMAP_COMPILE_NO_FLAGS);113114 /* TODO: initialize in enter? */115 wl->xkb_state = xkb_state_new(wl->xkb_keymap);116117 munmap(map_str, size);118 /* TODO: should we close(fd)? */119}120121static void122keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,123 struct wl_surface *surface, struct wl_array *keys)124{125 LOG_DBG("enter");126#if 0127 uint32_t *key;128 wl_array_for_each(key, keys)129 xkb_state_update_key(xkb_state, *key, 1);130#endif131}132133static void134keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,135 struct wl_surface *surface)136{137 keep_running = false;138}139140static size_t141prompt_prev_word(void)142{143 const char *space = &prompt.text[prompt.cursor - 1];144145 /* Ignore initial spaces */146 while (space >= prompt.text && *space == ' ')147 space--;148149 while (space >= prompt.text && *space != ' ')150 space--;151152 return space - prompt.text + 1;153}154155static size_t156prompt_next_word(void)157{158 const char *space = strchr(&prompt.text[prompt.cursor], ' ');159 if (space == NULL)160 return strlen(prompt.text);161 else162 return space - prompt.text + 1;163}164165static void166keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,167 uint32_t time, uint32_t key, uint32_t state)168{169 static bool mod_masks_initialized = false;170 static xkb_mod_mask_t ctrl = -1;171 static xkb_mod_mask_t alt = -1;172 //static xkb_mod_mask_t shift = -1;173174 struct wayland *wl = data;175176 if (!mod_masks_initialized) {177 mod_masks_initialized = true;178 ctrl = 1 << xkb_keymap_mod_get_index(wl->xkb_keymap, "Control");179 alt = 1 << xkb_keymap_mod_get_index(wl->xkb_keymap, "Mod1");180 //shift = 1 << xkb_keymap_mod_get_index(wl->xkb_keymap, "Shift");181 }182183 if (state == XKB_KEY_UP)184 return;185186 key += 8;187 xkb_keysym_t sym = xkb_state_key_get_one_sym(wl->xkb_state, key);188189 xkb_mod_mask_t mods = xkb_state_serialize_mods(190 wl->xkb_state, XKB_STATE_MODS_EFFECTIVE);191 xkb_mod_mask_t consumed = xkb_state_key_get_consumed_mods(wl->xkb_state, key);192 xkb_mod_mask_t significant = ctrl | alt /*| shift*/;193 xkb_mod_mask_t effective_mods = mods & ~consumed & significant;194195#if 0196 for (size_t i = 0; i < 32; i++) {197 if (mods & (1 << i)) {198 LOG_DBG("%s", xkb_keymap_mod_get_name(wl->xkb_keymap, i));199 }200 }201#endif202203 LOG_DBG("mod=0x%08x, consumed=0x%08x, significant=0x%08x, effective=0x%08x",204 mods, consumed, significant, effective_mods);205206 if (sym == XKB_KEY_Home || (sym == XKB_KEY_a && effective_mods == ctrl)) {207 prompt.cursor = 0;208 refresh(wl);209 }210211 else if (sym == XKB_KEY_End || (sym == XKB_KEY_e && effective_mods == ctrl)) {212 prompt.cursor = strlen(prompt.text);213 refresh(wl);214 }215216 else if ((sym == XKB_KEY_b && effective_mods == alt) ||217 (sym == XKB_KEY_Left && effective_mods == ctrl)) {218 prompt.cursor = prompt_prev_word();219 refresh(wl);220 }221222 else if ((sym == XKB_KEY_f && effective_mods == alt) ||223 (sym == XKB_KEY_Right && effective_mods == ctrl)) {224225 prompt.cursor = prompt_next_word();226 refresh(wl);227 }228229 else if (sym == XKB_KEY_Escape && effective_mods == 0)230 keep_running = false;231232 else if ((sym == XKB_KEY_p && effective_mods == ctrl) ||233 (sym == XKB_KEY_Up && effective_mods == 0)) {234 if (selected > 0) {235 selected--;236 refresh(wl);237 }238 }239240 else if ((sym == XKB_KEY_n && effective_mods == ctrl) ||241 (sym == XKB_KEY_Down && effective_mods == 0)) {242 if (selected + 1 < match_count) {243 selected++;244 refresh(wl);245 }246 }247248 else if (sym == XKB_KEY_Tab && effective_mods == 0) {249 if (selected + 1 < match_count) {250 selected++;251 refresh(wl);252 } else if (sym == XKB_KEY_Tab) {253 /* Tab cycles */254 selected = 0;255 refresh(wl);256 }257 }258259 else if ((sym == XKB_KEY_b && effective_mods == ctrl) ||260 (sym == XKB_KEY_Left && effective_mods == 0)) {261 if (prompt.cursor > 0) {262 prompt.cursor--;263 refresh(wl);264 }265 }266267 else if ((sym == XKB_KEY_f && effective_mods == ctrl) ||268 (sym == XKB_KEY_Right && effective_mods == 0)) {269 if (prompt.cursor + 1 <= strlen(prompt.text)) {270 prompt.cursor++;271 refresh(wl);272 }273 }274275 else if ((sym == XKB_KEY_d && effective_mods == ctrl) ||276 (sym == XKB_KEY_Delete && effective_mods == 0)) {277 if (prompt.cursor < strlen(prompt.text)) {278 memmove(&prompt.text[prompt.cursor], &prompt.text[prompt.cursor + 1],279 strlen(prompt.text) - prompt.cursor);280 update_matches();281 refresh(wl);282 }283 }284285 else if (sym == XKB_KEY_BackSpace && effective_mods == 0) {286 if (prompt.cursor > 0) {287 prompt.text[strlen(prompt.text) - 1] = '\0';288 prompt.cursor--;289290 update_matches();291 refresh(wl);292 }293 }294295 else if (sym == XKB_KEY_BackSpace && (effective_mods == ctrl ||296 effective_mods == alt)) {297 size_t new_cursor = prompt_prev_word();298 memmove(&prompt.text[new_cursor],299 &prompt.text[prompt.cursor],300 strlen(prompt.text) - prompt.cursor + 1);301 prompt.cursor = new_cursor;302 update_matches();303 refresh(wl);304 }305306 else if ((sym == XKB_KEY_d && effective_mods == alt) ||307 (sym == XKB_KEY_Delete && effective_mods == ctrl)) {308 size_t next_word = prompt_next_word();309 memmove(&prompt.text[prompt.cursor],310 &prompt.text[next_word],311 strlen(prompt.text) - next_word + 1);312 update_matches();313 refresh(wl);314 }315316 else if (sym == XKB_KEY_Return && effective_mods == 0) {317 if (match_count == 0)318 return;319320 assert(selected < match_count);321 LOG_DBG("exec(%s)", matches[selected].application->path);322323 pid_t pid = fork();324 if (pid == -1)325 LOG_ERRNO("failed to fork");326327 if (pid == 0) {328 /* Child */329 pid = fork();330 if (pid == 0)331 execlp(matches[selected].application->path,332 matches[selected].application->path, NULL);333 keep_running = false;334 } else {335 /* Parent */336 int status;337 if (waitpid(pid, &status, 0) == -1)338 LOG_ERRNO("failed to wait for pid %u", pid);339340 keep_running = false;341 }342 }343344 else if (effective_mods == 0) {345 char buf[128] = {0};346 int count = xkb_state_key_get_utf8(wl->xkb_state, key, buf, sizeof(buf));347348 if (count == 0)349 return;350351 const size_t new_len = strlen(prompt.text) + count + 1;352 char *new_text = malloc(new_len);353354 /* Everything from old prompt, up to the cursor */355 memcpy(new_text, prompt.text, prompt.cursor);356 new_text[prompt.cursor] = '\0';357358 /* New text just entered */359 strcat(new_text, buf);360361 /* Everything from old prompt, after cursor */362 strcat(new_text, &prompt.text[prompt.cursor]);363 new_text[new_len - 1] = '\0';364365 free(prompt.text);366 prompt.text = new_text;367 prompt.cursor += count;368369 LOG_DBG("prompt: \"%s\" (cursor=%zu, length=%zu)",370 prompt.text, prompt.cursor, new_len);371372 update_matches();373 refresh(wl);374 }375}376377static void378keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,379 uint32_t mods_depressed, uint32_t mods_latched,380 uint32_t mods_locked, uint32_t group)381{382 struct wayland *wl = data;383384 LOG_DBG("modifiers: depressed=0x%x, latched=0x%x, locked=0x%x, group=%u",385 mods_depressed, mods_latched, mods_locked, group);386387 xkb_state_update_mask(388 wl->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);389}390391static void392keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,393 int32_t rate, int32_t delay)394{395}396397static const struct wl_keyboard_listener keyboard_listener = {398 .keymap = &keyboard_keymap,399 .enter = &keyboard_enter,400 .leave = &keyboard_leave,401 .key = &keyboard_key,402 .modifiers = &keyboard_modifiers,403 .repeat_info = &keyboard_repeat_info,404};405406static void407seat_handle_capabilities(void *data, struct wl_seat *wl_seat,408 enum wl_seat_capability caps)409{410 struct wayland *wl = data;411412 if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD))413 return;414415 if (wl->keyboard != NULL)416 wl_keyboard_release(wl->keyboard);417 wl->keyboard = wl_seat_get_keyboard(wl_seat);418 wl_keyboard_add_listener(wl->keyboard, &keyboard_listener, wl);419}420421static void422seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)423{424}425426static const struct wl_seat_listener seat_listener = {427 .capabilities = seat_handle_capabilities,428 .name = seat_handle_name,429};430431static void432output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,433 int32_t physical_width, int32_t physical_height,434 int32_t subpixel, const char *make, const char *model,435 int32_t transform)436{437 struct monitor *mon = data;438 mon->x = x;439 mon->y = y;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 struct monitor *mon = data;449 mon->width_px = width;450 mon->height_px = height;451}452453static void454output_done(void *data, struct wl_output *wl_output)455{456}457458static void459output_scale(void *data, struct wl_output *wl_output, int32_t factor)460{461 struct monitor *mon = data;462 mon->scale = factor;463}464465466static const struct wl_output_listener output_listener = {467 .geometry = &output_geometry,468 .mode = &output_mode,469 .done = &output_done,470 .scale = &output_scale,471};472473static void474xdg_output_handle_logical_position(void *data,475 struct zxdg_output_v1 *xdg_output,476 int32_t x, int32_t y)477{478}479480static void481xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,482 int32_t width, int32_t height)483{484}485486static void487xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)488{489}490491static void492xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,493 const char *name)494{495 struct monitor *mon = data;496 mon->name = strdup(name);497}498499static void500xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,501 const char *description)502{503}504505static struct zxdg_output_v1_listener xdg_output_listener = {506 .logical_position = xdg_output_handle_logical_position,507 .logical_size = xdg_output_handle_logical_size,508 .done = xdg_output_handle_done,509 .name = xdg_output_handle_name,510 .description = xdg_output_handle_description,511};512513static void514handle_global(void *data, struct wl_registry *registry,515 uint32_t name, const char *interface, uint32_t version)516{517 struct wayland *wl = data;518519 if (strcmp(interface, wl_compositor_interface.name) == 0) {520 wl->compositor = wl_registry_bind(521 wl->registry, name, &wl_compositor_interface, 4);522 }523524 else if (strcmp(interface, wl_shm_interface.name) == 0) {525 wl->shm = wl_registry_bind(wl->registry, name, &wl_shm_interface, 1);526 wl_shm_add_listener(wl->shm, &shm_listener, wl);527 wl_display_roundtrip(wl->display);528 }529530 else if (strcmp(interface, wl_output_interface.name) == 0) {531 struct wl_output *output = wl_registry_bind(532 wl->registry, name, &wl_output_interface, 3);533534 tll_push_back(wl->monitors, ((struct monitor){.output = output}));535536 struct monitor *mon = &tll_back(wl->monitors);537 wl_output_add_listener(output, &output_listener, mon);538539 /*540 * The "output" interface doesn't give us the monitors'541 * identifiers (e.g. "LVDS-1"). Use the XDG output interface542 * for that.543 */544545 assert(wl->xdg_output_manager != NULL);546 mon->xdg = zxdg_output_manager_v1_get_xdg_output(547 wl->xdg_output_manager, mon->output);548549 zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);550 wl_display_roundtrip(wl->display);551 }552553 else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {554 wl->layer_shell = wl_registry_bind(555 wl->registry, name, &zwlr_layer_shell_v1_interface, 1);556 }557558 else if (strcmp(interface, wl_seat_interface.name) == 0) {559 wl->seat = wl_registry_bind(wl->registry, name, &wl_seat_interface, 3);560 wl_seat_add_listener(wl->seat, &seat_listener, wl);561 wl_display_roundtrip(wl->display);562 }563564 else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {565 wl->xdg_output_manager = wl_registry_bind(566 wl->registry, name, &zxdg_output_manager_v1_interface, 2);567 }568}569570static void571handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)572{573 LOG_WARN("global removed: %u", name);574}575576static const struct wl_registry_listener registry_listener = {577 .global = &handle_global,578 .global_remove = &handle_global_remove,579};580581static void582layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,583 uint32_t serial, uint32_t w, uint32_t h)584{585 zwlr_layer_surface_v1_ack_configure(surface, serial);586}587588static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {589 .configure = layer_surface_configure,590};591592#if 0593static void594find_programs(void)595{596 DIR *d = opendir("/usr/bin");597 if (d == NULL)598 return;599600 for (struct dirent *e = readdir(d); e != NULL; e = readdir(d)) {601 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)602 continue;603604 int len = snprintf(NULL, 0, "/usr/bin/%s", e->d_name);605 char *path = malloc(len + 1);606 snprintf(path, len + 1, "/usr/bin/%s", e->d_name);607608 struct application app = {609 .title = strdup(e->d_name),610 .path = path,611 };612613 tll_push_back(applications, app);614 }615616 closedir(d);617}618#endif619620static void621update_matches(void)622{623 /* Nothing entered; all programs found matches */624 if (strlen(prompt.text) == 0) {625 match_count = tll_length(applications);626627 size_t i = 0;628 tll_foreach(applications, it)629 matches[i++] = (struct match){.application = &it->item, .start = 0};630631 assert(selected < match_count);632 return;633 }634635 tll(struct match) _matches = tll_init();636 tll_foreach(applications, it) {637 const char *m = strcasestr(it->item.title, prompt.text);638 if (m == NULL)639 continue;640641 const size_t start = m - it->item.title;642 tll_push_back(643 _matches,644 ((struct match){.application = &it->item, .start = start}));645 }646647 size_t i = 0;648 match_count = tll_length(_matches);649 tll_foreach(_matches, it)650 matches[i++] = it->item;651652 tll_free(_matches);653654 if (selected >= match_count && selected > 0)655 selected = match_count - 1;656}657658static void659refresh(const struct wayland *wl)660{661 struct buffer *buf = shm_get_buffer(wl->shm, width, height);662663 /* Background */664 cairo_set_source_rgba(buf->cairo, 0.067, 0.067, 0.067, 0.9);665 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);666 cairo_paint(buf->cairo);667668 /* Border */669 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);670 cairo_set_line_width(buf->cairo, 2);671 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);672 cairo_rectangle(buf->cairo, 0, 0, width, height);673 cairo_stroke(buf->cairo);674675 render_prompt(buf, scaled_font, font_size, &prompt);676 render_match_list(buf, scaled_font, font_size, matches, match_count, selected);677678 cairo_surface_flush(buf->cairo_surface);679 wl_surface_attach(wl->surface, buf->wl_buf, 0, 0);680 wl_surface_damage(wl->surface, 0, 0, buf->width, buf->height);681 wl_surface_commit(wl->surface);682}683684int685main(int argc, const char *const *argv)686{687 prompt.text = calloc(1, 1);688 prompt.cursor = 0;689690 //find_programs();691 xdg_find_programs(&applications);692 matches = malloc(tll_length(applications) * sizeof(matches[0]));693 update_matches();694695 struct wayland wl = {0};696697 wl.display = wl_display_connect(NULL);698 if (wl.display == NULL) {699 LOG_ERR("failed to connect to wayland; no compositor running?");700 goto out;701 }702703 wl.registry = wl_display_get_registry(wl.display);704 if (wl.registry == NULL) {705 LOG_ERR("failed to get wayland registry");706 goto out;707 }708709 wl_registry_add_listener(wl.registry, &registry_listener, &wl);710 wl_display_roundtrip(wl.display);711712 if (wl.compositor == NULL) {713 LOG_ERR("no compositor");714 goto out;715 }716 if (wl.layer_shell == NULL) {717 LOG_ERR("no layer shell interface");718 goto out;719 }720 if (wl.shm == NULL) {721 LOG_ERR("no shared memory buffers interface");722 goto out;723 }724725 if (tll_length(wl.monitors) == 0) {726 LOG_ERR("no monitors");727 goto out;728 }729730 tll_foreach(wl.monitors, it) {731 const struct monitor *mon = &it->item;732 LOG_INFO("monitor: %s: %dx%d+%d+%d (%dx%dmm)",733 mon->name, mon->width_px, mon->height_px,734 mon->x, mon->y, mon->width_mm, mon->height_mm);735736#if 0737 /* TODO: detect primary output when user hasn't specified a monitor */738 if (bar->monitor == NULL)739 monitor = mon;740 else if (strcmp(bar->monitor, mon->name) == 0)741 monitor = mon;742#endif743 wl.monitor = mon;744 break;745 }746747 assert(wl.monitor != NULL);748749 wl.surface = wl_compositor_create_surface(wl.compositor);750 if (wl.surface == NULL) {751 LOG_ERR("failed to create panel surface");752 goto out;753 }754755#if 0756 wl.pointer.surface = wl_compositor_create_surface(wl.compositor);757 if (wl.pointer.surface == NULL) {758 LOG_ERR("failed to create cursor surface");759 goto out;760 }761762 wl.pointer.theme = wl_cursor_theme_load(NULL, 24, wl.shm);763 if (wl.pointer.theme == NULL) {764 LOG_ERR("failed to load cursor theme");765 goto out;766 }767#endif768769 wl.layer_surface = zwlr_layer_shell_v1_get_layer_surface(770 wl.layer_shell, wl.surface, wl.monitor->output,771 ZWLR_LAYER_SHELL_V1_LAYER_TOP, "f00sel");772773 if (wl.layer_surface == NULL) {774 LOG_ERR("failed to create layer shell surface");775 goto out;776 }777778 zwlr_layer_surface_v1_set_size(wl.layer_surface, width, height);779 zwlr_layer_surface_v1_set_keyboard_interactivity(wl.layer_surface, 1);780781 zwlr_layer_surface_v1_add_listener(782 wl.layer_surface, &layer_surface_listener, &wl);783784 /* Trigger a 'configure' event, after which we'll have the width */785 wl_surface_commit(wl.surface);786 wl_display_roundtrip(wl.display);787788 const char *font_name = "Dina:pixelsize=9";789 scaled_font = font_from_name(font_name, &font_size);790 refresh(&wl);791792 wl_display_dispatch_pending(wl.display);793 wl_display_flush(wl.display);794795 while (keep_running) {796 struct pollfd fds[] = {797 {.fd = wl_display_get_fd(wl.display), .events = POLLIN},798 };799800 wl_display_flush(wl.display);801802 if (fds[0].revents & POLLHUP) {803 LOG_WARN("disconnected from wayland");804 break;805 }806807 wl_display_dispatch(wl.display);808 }809810out:811 shm_fini();812813 free(matches);814 tll_foreach(applications, it) {815 free(it->item.title);816 free(it->item.path);817 tll_remove(applications, it);818 }819820 tll_foreach(wl.monitors, it) {821 free(it->item.name);822 if (it->item.xdg)823 zxdg_output_v1_destroy(it->item.xdg);824 if (it->item.output != NULL)825 wl_output_destroy(it->item.output);826 tll_remove(wl.monitors, it);827 }828829 if (wl.xdg_output_manager != NULL)830 zxdg_output_manager_v1_destroy(wl.xdg_output_manager);831832 if (wl.layer_surface != NULL)833 zwlr_layer_surface_v1_destroy(wl.layer_surface);834 if (wl.layer_shell != NULL)835 zwlr_layer_shell_v1_destroy(wl.layer_shell);836 if (wl.keyboard != NULL)837 wl_keyboard_destroy(wl.keyboard);838 if (wl.surface != NULL)839 wl_surface_destroy(wl.surface);840 if (wl.seat != NULL)841 wl_seat_destroy(wl.seat);842 if (wl.compositor != NULL)843 wl_compositor_destroy(wl.compositor);844 if (wl.shm != NULL)845 wl_shm_destroy(wl.shm);846 if (wl.registry != NULL)847 wl_registry_destroy(wl.registry);848 if (wl.display != NULL)849 wl_display_disconnect(wl.display);850 if (wl.xkb_state != NULL)851 xkb_state_unref(wl.xkb_state);852 if (wl.xkb_keymap != NULL)853 xkb_keymap_unref(wl.xkb_keymap);854 if (wl.xkb != NULL)855 xkb_context_unref(wl.xkb);856857 return 0;858}
added:match.h+9 added
1#pragma once23#include <stddef.h>4#include "application.h"56struct match {7 const struct application *application;8 size_t start;9};
added:render.c+132 added
1#include "render.h"23#include <stdlib.h>4#include <string.h>56void7render_prompt(struct buffer *buf,8 cairo_scaled_font_t *scaled_font, double font_size,9 const struct prompt *prompt)10{11 const size_t x = 20;12 const size_t y_base = 0;13 const size_t y_advance = font_size + 10; /* TODO: how much "extra" is "right"? */14 size_t y = y_base;1516 const char *const text = prompt->text;1718 cairo_glyph_t *glyphs = NULL;19 cairo_text_cluster_t *clusters = NULL;20 cairo_text_cluster_flags_t cluster_flags;21 int num_glyphs = 0;22 int num_clusters = 0;2324 cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(25 scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,26 &clusters, &num_clusters, &cluster_flags);27 assert(cr_status == CAIRO_STATUS_SUCCESS);2829 cairo_text_extents_t extents;30 cairo_scaled_font_glyph_extents(31 scaled_font, glyphs, num_glyphs, &extents);3233 for (int i = 0; i < num_glyphs; i++) {34 glyphs[i].x += x;35 glyphs[i].y += y +36 ((double)y_advance - extents.height) / 2 - extents.y_bearing;37 }3839 cairo_set_scaled_font(buf->cairo, scaled_font);40 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);41 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);42 cairo_show_text_glyphs(43 buf->cairo, text, -1, glyphs, num_glyphs,44 clusters, num_clusters, cluster_flags);4546 int cursor_x;47 if (prompt->cursor == 0)48 cursor_x = x;49 else if (prompt->cursor >= (size_t)num_glyphs)50 cursor_x = x + extents.x_advance;51 else52 cursor_x = glyphs[prompt->cursor].x;5354 cairo_set_line_width(buf->cairo, 1);55 cairo_move_to(buf->cairo, cursor_x - 0.5, y_base + 2);56 cairo_line_to(buf->cairo, cursor_x - 0.5, y_base + y_advance - 2);57 cairo_stroke(buf->cairo);5859 cairo_glyph_free(glyphs);60 cairo_text_cluster_free(clusters);6162 cairo_set_line_width(buf->cairo, 1);63 cairo_move_to(buf->cairo, 0, y_base + y_advance + 0.5);64 cairo_line_to(buf->cairo, buf->width, y_base + y_advance + 0.5);65 cairo_stroke(buf->cairo);66}6768void69render_match_list(struct buffer *buf,70 cairo_scaled_font_t *scaled_font, double font_size,71 const struct match matches[], size_t match_count,72 size_t selected)73{74 assert(match_count == 0 || selected < match_count);7576 const size_t x = 20;77 const size_t y_advance = font_size + 10; /* TODO: how much "extra" is "right"? */78 const size_t y_base = 2 + y_advance;79 size_t y = y_base;8081 for (size_t i = 0; i < match_count; i++) {82 const struct match *match = &matches[i];83 const char *const text = match->application->title;8485 cairo_glyph_t *glyphs = NULL;86 cairo_text_cluster_t *clusters = NULL;87 cairo_text_cluster_flags_t cluster_flags;88 int num_glyphs = 0;89 int num_clusters = 0;9091 cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(92 scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,93 &clusters, &num_clusters, &cluster_flags);94 assert(cr_status == CAIRO_STATUS_SUCCESS);9596 cairo_text_extents_t extents;97 cairo_scaled_font_glyph_extents(98 scaled_font, glyphs, num_glyphs, &extents);99100 for (int j = 0; j < num_glyphs; j++) {101 glyphs[j].x += x;102 glyphs[j].y += y +103 ((double)y_advance - extents.height) / 2 - extents.y_bearing;104 }105106 if (i == selected) {107 double f = 0.5;108 cairo_set_source_rgba(buf->cairo, 0.067+f, 0.067+f, 0.067+f, 0.9);109 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);110 cairo_rectangle(buf->cairo, x - 2, y + 2, buf->width - 2 * (x - 2), y_advance - 4);111 cairo_fill(buf->cairo);112 } else if ((y - y_base) % (2 * y_advance) == 0) {113 double f = 0.1;114 cairo_set_source_rgba(buf->cairo, 0.067+f, 0.067+f, 0.067+f, 0.9);115 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);116 cairo_rectangle(buf->cairo, x - 2, y + 2, buf->width - 2 * (x - 2), y_advance - 4);117 cairo_fill(buf->cairo);118 }119120 cairo_set_scaled_font(buf->cairo, scaled_font);121 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);122 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);123 cairo_show_text_glyphs(124 buf->cairo, text, -1, glyphs, num_glyphs,125 clusters, num_clusters, cluster_flags);126127 cairo_glyph_free(glyphs);128 cairo_text_cluster_free(clusters);129130 y += y_advance;131 }132}
added:render.h+21 added
1#pragma once23#include <cairo/cairo.h>45#include "shm.h"6#include "match.h"7#include "tllist.h"89struct prompt {10 char *text;11 size_t cursor;12};1314void render_prompt(struct buffer *buf,15 cairo_scaled_font_t *scaled_font, double font_size,16 const struct prompt *prompt);1718void render_match_list(struct buffer *buf,19 cairo_scaled_font_t *scaled_font, double font_size,20 const struct match matches[], size_t match_count,21 size_t selected);
added:shm.c+161 added
1#include "shm.h"23#include <unistd.h>4#include <assert.h>56#include <sys/types.h>7#include <sys/mman.h>8#include <linux/memfd.h>910#define LOG_MODULE "shm"11#include "log.h"12#include "tllist.h"1314static tll(struct buffer) buffers;1516static void17buffer_release(void *data, struct wl_buffer *wl_buffer)18{19 //printf("buffer release\n");20 struct buffer *buffer = data;21 assert(buffer->wl_buf == wl_buffer);22 assert(buffer->busy);23 buffer->busy = false;24}2526static const struct wl_buffer_listener buffer_listener = {27 .release = &buffer_release,28};2930struct buffer *31shm_get_buffer(struct wl_shm *shm, int width, int height)32{33 tll_foreach(buffers, it) {34 if (!it->item.busy) {35 it->item.busy = true;36 return &it->item;37 }38 }3940 /*41 * No existing buffer available. Create a new one by:42 *43 * 1. open a memory backed "file" with memfd_create()44 * 2. mmap() the memory file, to be used by the cairo surface45 * 3. create a wayland shm buffer for the same memory file46 *47 * The cairo surface and the wayland buffer are now sharing48 * memory.49 */5051 int pool_fd = -1;52 void *mmapped = NULL;53 size_t size = 0;5455 struct wl_shm_pool *pool = NULL;56 struct wl_buffer *buf = NULL;5758 cairo_surface_t *cairo_surface = NULL;59 cairo_t *cairo = NULL;6061 /* Backing memory for SHM */62 pool_fd = memfd_create("wayland-test-buffer-pool", MFD_CLOEXEC);63 if (pool_fd == -1) {64 LOG_ERRNO("failed to create SHM backing memory file");65 goto err;66 }6768 /* Total size */69 uint32_t stride = width * 4;70 size = stride * height;71 ftruncate(pool_fd, size);7273 mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, pool_fd, 0);74 if (mmapped == MAP_FAILED) {75 LOG_ERR("failed to mmap SHM backing memory file");76 goto err;77 }7879 pool = wl_shm_create_pool(shm, pool_fd, size);80 if (pool == NULL) {81 LOG_ERR("failed to create SHM pool");82 goto err;83 }8485 buf = wl_shm_pool_create_buffer(86 pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);87 if (buf == NULL) {88 LOG_ERR("failed to create SHM buffer");89 goto err;90 }9192 /* We use the entire pool for our single buffer */93 wl_shm_pool_destroy(pool); pool = NULL;94 close(pool_fd); pool_fd = -1;9596 /* Create a cairo surface around the mmapped memory */97 cairo_surface = cairo_image_surface_create_for_data(98 mmapped, CAIRO_FORMAT_ARGB32, width, height, stride);99 if (cairo_surface_status(cairo_surface) != CAIRO_STATUS_SUCCESS) {100 LOG_ERR("failed to create cairo surface: %s",101 cairo_status_to_string(cairo_surface_status(cairo_surface)));102 goto err;103 }104105 cairo = cairo_create(cairo_surface);106 if (cairo_status(cairo) != CAIRO_STATUS_SUCCESS) {107 LOG_ERR("failed to create cairo context: %s",108 cairo_status_to_string(cairo_status(cairo)));109 goto err;110 }111112 /* Push to list of available buffers, but marked as 'busy' */113 tll_push_back(114 buffers,115 ((struct buffer){116 .width = width,117 .height = height,118 .busy = true,119 .size = size,120 .mmapped = mmapped,121 .wl_buf = buf,122 .cairo_surface = cairo_surface,123 .cairo = cairo}124 )125 );126127 struct buffer *ret = &tll_back(buffers);128 wl_buffer_add_listener(ret->wl_buf, &buffer_listener, ret);129 return ret;130131err:132 if (cairo != NULL)133 cairo_destroy(cairo);134 if (cairo_surface != NULL)135 cairo_surface_destroy(cairo_surface);136 if (buf != NULL)137 wl_buffer_destroy(buf);138 if (pool != NULL)139 wl_shm_pool_destroy(pool);140 if (pool_fd != -1)141 close(pool_fd);142 if (mmapped != NULL)143 munmap(mmapped, size);144145 return NULL;146}147148void149shm_fini(void)150{151 tll_foreach(buffers, it) {152 struct buffer *buf = &it->item;153154 cairo_destroy(buf->cairo);155 cairo_surface_destroy(buf->cairo_surface);156 wl_buffer_destroy(buf->wl_buf);157 munmap(buf->mmapped, buf->size);158159 tll_remove(buffers, it);160 }161}
added:shm.h+24 added
1#pragma once23#include <stdbool.h>4#include <stddef.h>56#include <cairo/cairo.h>7#include <wayland-client.h>89struct buffer {10 int width;11 int height;1213 bool busy;14 size_t size;15 void *mmapped;1617 struct wl_buffer *wl_buf;1819 cairo_surface_t *cairo_surface;20 cairo_t *cairo;21};2223struct buffer *shm_get_buffer(struct wl_shm *shm, int width, int height);24void shm_fini(void);
added:tllist.h+157 added
1#pragma once23#include <stdlib.h>4#include <stddef.h>5#include <assert.h>67#define TLL_PASTE2( a, b) a##b8#define TLL_PASTE( a, b) TLL_PASTE2( a, b)910/* Utility macro to generate a list element struct with a unique struct tag */11#define TLL_UNIQUE_INNER_STRUCT(TYPE, ID) \12 struct TLL_PASTE(__tllist_ , ID) { \13 TYPE item; \14 struct TLL_PASTE(__tllist_, ID) *prev; \15 struct TLL_PASTE(__tllist_, ID) *next; \16 } *head, *tail;1718/*19 * Defines a new typed-list type, or directly instantiate a typed-list variable20 *21 * Example a, declare a variable (list of integers):22 * tll(int) my_list;23 *24 * Example b, declare a type, and then use the type:25 * tll(int, my_list_type);26 * struct my_list_type my_list;27 */28#define tll(TYPE, ...) \29 struct __VA_ARGS__ { \30 TLL_UNIQUE_INNER_STRUCT(TYPE, __COUNTER__) \31 size_t length; \32 }3334/* Initializer: tll(int) my_list = tll_init(); */35#define tll_init() {.head = NULL, .tail = NULL, .length = 0}3637/* Length/size of list: printf("size: %zu\n", tll_length(my_list)); */38#define tll_length(list) (list).length3940/* Adds a new item to the back of the list */41#define tll_push_back(list, new_item) \42 do { \43 __typeof__((list).head) __e = malloc(sizeof(*__e)); \44 __e->item = (new_item); \45 __e->prev = (list).tail; \46 __e->next = NULL; \47 if ((list).head == NULL) \48 (list).head = (list).tail = __e; \49 else { \50 (list).tail->next = __e; \51 (list).tail = __e; \52 } \53 (list).length++; \54 } while (0)5556/* Adds a new item to the front of the list */57#define tll_push_front(list, new_item) \58 do { \59 __typeof__((list).head) __e = malloc(sizeof(*__e)); \60 __e->item = (new_item); \61 __e->prev = NULL; \62 __e->next = (list).head; \63 if ((list).head == NULL) \64 (list).head = (list).tail = __e; \65 else { \66 (list).head->prev = __e; \67 (list).head = __e; \68 } \69 (list).length++; \70 } while (0)717273/*74 * Iterates the list. <it> is an iterator pointer. You can access the75 * list item with ->item:76 *77 * tll(int) my_list = vinit();78 * tll_push_back(my_list, 5);79 *80 * tll_foreach(my_list i) {81 * printf("%d\n", i->item);82 * }83*/84#define tll_foreach(list, it) \85 for (__typeof__(*(list).head) *it = (list).head, \86 *it_next = it != NULL ? it->next : NULL; \87 it != NULL; \88 it = it_next, \89 it_next = it_next != NULL ? it_next->next : NULL)9091/* Same as tll_foreach(), but iterates backwards */92#define tll_rforeach(list, it) \93 for (__typeof__(*(list).tail) *it = (list).tail, \94 *it_prev = it != NULL ? it->prev : NULL; \95 it != NULL; \96 it = it_prev, \97 it_prev = it_prev != NULL ? it_prev->prev : NULL)9899/*100 * Removes an entry from the list. <it> is an iterator. I.e. you can101 * only call this from inside a tll_foreach() or tll_rforeach() loop.102 */103#define tll_remove(list, it) \104 do { \105 assert((list).length > 0); \106 __typeof__((list).head) __prev = it->prev; \107 __typeof__((list).head) __next = it->next; \108 if (__prev != NULL) \109 __prev->next = __next; \110 else \111 (list).head = __next; \112 if (__next != NULL) \113 __next->prev = __prev; \114 else \115 (list).tail = __prev; \116 free(it); \117 (list).length--; \118 } while (0)119120/* Same as tll_remove(), but calls free_callback(it->item) */121#define tll_remove_and_free(list, it, free_callback) \122 do { \123 free_callback((it)->item); \124 tll_remove((list), (it)); \125 } while (0)126127#define tll_front(list) (list).head->item128#define tll_back(list) (list).tail->item129130/*131 * Removes the first element from the list, and returns it (note:132 * returns the *actual* item, not an iterator.133 */134#define tll_pop_front(list) \135 ({__typeof__((list).head) it = (list).head; \136 __typeof__((list).head->item) __ret = it->item; \137 tll_remove((list), it); \138 __ret; \139 })140141/* Same as tll_pop_front(), but returns/removes the *last* element */142#define tll_pop_back(list) \143 ({__typeof__((list).tail) it = (list).tail; \144 __typeof__((list).tail->item) __ret = it->item; \145 tll_remove((list), it); \146 __ret; \147 })148149/* Frees the list. This call is *not* needed if the list is already empty. */150#define tll_free(list) \151 tll_foreach(list, __it) \152 tll_remove(list, __it)153154/* Same as tll_free(), but also calls free_callback(item) for every item */155#define tll_free_and_free(list, free_callback) \156 tll_foreach(list, __it) \157 tll_remove_and_free(list, __it, free_callback)
added:xdg.c+190 added
1#include "xdg.h"23#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <stdbool.h>7#include <unistd.h>89#include <sys/types.h>10#include <sys/stat.h>11#include <dirent.h>12#include <fcntl.h>1314#define LOG_MODULE "xdg"15#define LOG_ENABLE_DBG 116#include "log.h"1718static void19parse_desktop_file(int fd, application_list_t *applications)20{21 FILE *f = fdopen(fd, "r");22 if (f == NULL)23 return;2425 bool is_desktop_entry = false;26 char *name = NULL;27 char *exec = NULL;2829 while (true) {30 char *line = NULL;31 size_t sz = 0;32 ssize_t len = getline(&line, &sz, f);3334 if (len == -1) {35 free(line);36 break;37 }3839 if (len == 0) {40 free(line);41 continue;42 }4344 if (line[len - 1] == '\n') {45 line[len - 1] = '\0';46 len--;47 }4849 if (len == 0) {50 free(line);51 continue;52 }5354 if (line[0] == '[' && line[len - 1] == ']') {55 if (strncasecmp(&line[1], "desktop entry", len - 2) == 0) {56 is_desktop_entry = true;57 free(line);58 continue;59 } else {60 free(line);61 break;62 }63 }6465 const char *key = strtok(line, "=");66 char *value = strtok(NULL, "=");6768 if (strcasecmp(key, "name") == 0)69 name = strdup(value);7071 else if (strcasecmp(key, "exec") == 0) {72 const char *first = strtok(value, " ");73 exec = strdup(first);74 }7576 free(line);77 }7879 if (is_desktop_entry && name != NULL && exec != NULL) {80 tll_push_back(81 *applications,82 ((struct application){.title = name, .path = exec}));83 } else {84 free(name);85 free(exec);86 }8788 fclose(f);89}9091static void92scan_dir(int base_fd, application_list_t *applications)93{94 DIR *d = fdopendir(base_fd);95 if (d == NULL) {96 LOG_ERRNO("failed to open directory");97 return;98 }99100 for (const struct dirent *e = readdir(d); e != NULL; e = readdir(d)) {101 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)102 continue;103104 struct stat st;105 if (fstatat(base_fd, e->d_name, &st, 0) == -1) {106 LOG_WARN("%s: failed to stat", e->d_name);107 continue;108 }109110 if (S_ISDIR(st.st_mode)) {111 int dir_fd = openat(base_fd, e->d_name, O_RDONLY);112 if (dir_fd == -1) {113 LOG_ERRNO("%s: failed to open", e->d_name);114 continue;115 }116117 scan_dir(dir_fd, applications);118 close(dir_fd);119 } else if (S_ISREG(st.st_mode)) {120 /* Skip files not ending with ".desktop" */121 const size_t desktop_len = strlen(".desktop");122 const size_t name_len = strlen(e->d_name);123 if (name_len < desktop_len)124 continue;125126 if (strcmp(&e->d_name[name_len - desktop_len], ".desktop") != 0)127 continue;128129 LOG_DBG("%s", e->d_name);130 int fd = openat(base_fd, e->d_name, O_RDONLY);131 if (fd == -1)132 LOG_WARN("%s: failed to open", e->d_name);133 else {134 parse_desktop_file(fd, applications);135 close(fd);136 }137 }138 }139140 closedir(d);141}142143void144xdg_find_programs(application_list_t *applications)145{146 const char *xdg_data_home = getenv("XDG_DATA_HOME");147 if (xdg_data_home != NULL) {148 int fd = open(xdg_data_home, O_RDONLY);149 if (fd == -1)150 LOG_WARN("%s: failed to open", xdg_data_home);151 else {152 scan_dir(fd, applications);153 close(fd);154 }155 }156157 const char *_xdg_data_dirs = getenv("XDG_DATA_DIRS");158 if (_xdg_data_dirs != NULL) {159 char *xdg_data_dirs = strdup(_xdg_data_dirs);160 for (const char *data_dir = strtok(xdg_data_dirs, ":");161 data_dir != NULL;162 data_dir = strtok(NULL, ":"))163 {164 int fd = open(data_dir, O_RDONLY);165 if (fd == -1)166 LOG_WARN("%s: failed to open", data_dir);167 else {168 scan_dir(fd, applications);169 close(fd);170 }171 }172173 free(xdg_data_dirs);174 } else {175 static const char *const default_paths[] = {176 "/usr/local/share",177 "/usr/share",178 };179180 for (size_t i = 0; i < sizeof(default_paths) / sizeof(default_paths[0]); i++) {181 int fd = open(default_paths[i], O_RDONLY);182 if (fd == -1)183 LOG_WARN("%s: failed to open", default_paths[i]);184 else {185 scan_dir(fd, applications);186 close(fd);187 }188 }189 }190}
added:xdg.h+8 added
1#pragma once23#include "application.h"4#include "tllist.h"56typedef tll(struct application) application_list_t;78void xdg_find_programs(application_list_t *applications);