diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 43 | ||||
| -rw-r--r-- | config.h | 16 | ||||
| -rw-r--r-- | dmenu-wl.c | 475 | ||||
| -rw-r--r-- | drw.c | 271 | ||||
| -rw-r--r-- | drw.h | 44 | ||||
| -rw-r--r-- | protocol/dwl-bar-ipc-unstable-v1.xml | 157 | ||||
| -rw-r--r-- | protocol/wlr-layer-shell-unstable.xml | 390 | ||||
| -rw-r--r-- | util.c | 11 | ||||
| -rw-r--r-- | util.h | 6 | ||||
| -rw-r--r-- | wayland.c | 66 | ||||
| -rw-r--r-- | wayland.h | 11 |
12 files changed, 1493 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d305e16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dmenu-wl +*.o +*-protocol.* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d5a311 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +WLR_LAYER_SHELL=protocol/wlr-layer-shell-unstable.xml +XDG_SHELL=/usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml +DWL_IPC=protocol/dwl-bar-ipc-unstable-v1.xml +PROTOCOLS=wlr-layer-shell-protocol.c xdg-shell-protocol.c dwl-bar-ipc-protocol.c +SRC=dmenu-wl.c drw.c util.c wayland.c +OBJ=${SRC:.c=.o} ${PROTOCOLS:.c=.o} +LDFLAGS=-lfreetype -lwayland-client -lrt -lxkbcommon -lm -lgrapheme +CFLAGS=-I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -pthread +CC=gcc + +default_target: options dmenu-wl + +options: + @echo ${OBJ} + +wlr-layer-shell-protocol.h: + wayland-scanner client-header < ${WLR_LAYER_SHELL} > $@ + +wlr-layer-shell-protocol.c: wlr-layer-shell-protocol.h + wayland-scanner private-code < ${WLR_LAYER_SHELL} > $@ + +xdg-shell-protocol.h: + wayland-scanner client-header < ${XDG_SHELL} > $@ + +xdg-shell-protocol.c: xdg-shell-protocol.h + wayland-scanner private-code < ${XDG_SHELL} > $@ + +dwl-bar-ipc-protocol.h: + wayland-scanner client-header < ${DWL_IPC} > $@ + +dwl-bar-ipc-protocol.c: dwl-bar-ipc-protocol.h + wayland-scanner private-code < ${DWL_IPC} > $@ + +.c.o: + ${CC} -c $< ${CFLAGS} + +${OBJ}: ${PROTOCOLS} config.h + +dmenu-wl: ${OBJ} + ${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + rm ${OBJ} dmenu-wl ${PROTOCOLS} ${PROTOCOLS:.c=.h} diff --git a/config.h b/config.h new file mode 100644 index 0000000..dc0d258 --- /dev/null +++ b/config.h @@ -0,0 +1,16 @@ +#include <stdint.h> + +/* colors in argb */ +static uint32_t background = 0xff282828; +static uint32_t foreground = 0xffebdbb2; +static uint32_t highlight = 0xffb16286; + +static unsigned lines = 20; +static unsigned padding = 10; +static unsigned borderwidth = 2; +static unsigned fontsize = 11; +static char fontpath[] = "/usr/share/fonts/TTF/Sauce Code Pro Nerd Font Complete Mono.ttf"; + +static unsigned cursor_width = 1; +static unsigned cursor_vertical_offset = 2; +static unsigned cursor_horizontal_offset = 1; diff --git a/dmenu-wl.c b/dmenu-wl.c new file mode 100644 index 0000000..3cc5a05 --- /dev/null +++ b/dmenu-wl.c @@ -0,0 +1,475 @@ +#define _POSIX_C_SOURCE 200112L +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <wayland-client.h> +#include <xkbcommon/xkbcommon.h> +#include <malloc.h> +#include <unistd.h> + +#include "xdg-shell-protocol.h" +#include "dwl-bar-ipc-protocol.h" +#include "wlr-layer-shell-protocol.h" + +#include "drw.h" + +/* macro definitions */ +#define MAX_LINE_LENGTH 1024 +#define match_then_bind(obj, inter, ver) \ + if (strcmp(interface, inter.name) == 0) { \ + obj = wl_registry_bind(wl_registry, name, &inter, ver); + +#define end_match } +#define or_match } else + +/* struct definitions */ +typedef struct { + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_shm *shm; + struct zdwl_manager_v1 *dwl_manager; + struct zwlr_layer_shell_v1 *layer; + struct wl_seat *seat; + struct wl_keyboard *keyboard; + struct xkb_context *xkb_context; + struct xkb_state *xkb_state; + struct xkb_keymap *xkb_keymap; + struct { + uint32_t control; + uint32_t alt; + uint32_t shift; + } mod; +} Client; + +typedef struct Monitor Monitor; +struct Monitor { + uint32_t width; + Canvas *canvas; + Font *font; + struct zdwl_output_v1 *dwl_output; + struct wl_output *output; + struct wl_surface *surface; + struct zwlr_layer_surface_v1 *wlr_surface; + Monitor *next; +}; + +typedef struct Option Option; +struct Option { + char *name; + Option *next; + Option *previous; + Option *next_match; + Option *previous_match; +}; + +typedef struct Options Options; +struct Options { + Option *first; + Option *last; + Option *first_match; + Option *last_match; + Option *selection; +} options = { 0 }; + +/* function definition */ +static void generate_matches(); +static int match_option(char *name); +static void draw_dmenu(Monitor *monitor); +static void setup(); +static void readstdin(); +static Monitor *add_monitor(); +static void registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version); +static void seat_capabilities(void *data, struct wl_seat *seat, uint32_t capabilities); +static void dwl_output_active(void *data, struct zdwl_output_v1 *output, uint32_t active); +static void wlr_layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t w, uint32_t h); +static void keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size); +static void keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state); +static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group); +static void keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay); +static void dummy() {} + +/* global variables */ +static const struct wl_registry_listener registry_listnener = { + .global = registry_global, + .global_remove = dummy +}; + +static const struct zdwl_output_v1_listener dwl_output_listener = { + .active = dwl_output_active, + .appid = dummy, + .frame = dummy, + .layout = dummy, + .tag = dummy, + .title = dummy, + .toggle_visibility = dummy, +}; + +static const struct zwlr_layer_surface_v1_listener wlr_layer_surface_listener = { + .closed = dummy, + .configure = wlr_layer_surface_configure, +}; + +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, + .name = dummy, +}; + +static const struct wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_keymap, + .enter = dummy, + .key = keyboard_key, + .leave = dummy, + .modifiers = keyboard_modifiers, + .repeat_info = keyboard_repeat_info, +}; + +Client client = { 0 }; +Monitor *monitors = 0; +Monitor *active_monitor = 0; +uint32_t height = 50; +uint32_t numitems = 0; +char input_field[MAX_LINE_LENGTH] = { 0 }; +int running = 1; + +#include "config.h" + +/* function implementations */ +int +option_match(char *name) +{ + int i = 0; + + for (; *name; ++name) { + if (input_field[i] == 0) + return 1; + + if (*name != input_field[i]) { + i = 0; + } else { + ++i; + } + } + + if (input_field[i] == 0) + return 1; + + return 0; +} + + +void +generate_matches() +{ + Option *match = options.first; + options.first_match = 0; + options.last_match = 0; + + for (; match; match = match->next) { + if (option_match(match->name)) { + if (options.first_match == 0) { + options.first_match = match; + options.last_match = match; + } else { + options.last_match->next_match = match; + match->previous_match = options.last; + options.last_match = match; + } + } + } + + options.selection = options.first_match; + if (options.last_match) + options.last_match->next_match = 0; +} + + +void +draw_dmenu(Monitor *monitor) +{ + Option *match = options.first_match; + unsigned x = 0; + unsigned y = 0; + + draw_rect(monitor->canvas, 0, 0, monitor->width, height, highlight); + draw_rect(monitor->canvas, borderwidth, borderwidth, + monitor->width - 2 * borderwidth, height - 2 * borderwidth, background); + draw_rect(monitor->canvas, 0, fontsize + borderwidth + 2 * padding, + monitor->width, borderwidth, highlight); + x = draw_font(monitor->canvas, monitor->font, input_field, + padding + borderwidth, borderwidth + fontsize + padding, foreground); + draw_rect(monitor->canvas, x + cursor_horizontal_offset, + borderwidth + padding - cursor_vertical_offset, cursor_width, + fontsize + 2 * cursor_vertical_offset, foreground); + + y = 2 * borderwidth + fontsize + 2 * padding; + + for (; match; match = match->next_match) { + if (match == options.selection) + draw_rect(monitor->canvas, 0, y, monitor->width, fontsize + 2 * padding, highlight); + y += fontsize + padding; + draw_font(monitor->canvas, monitor->font, match->name, padding, y, foreground); + y += padding; + } + + wl_surface_attach(monitor->surface, (struct wl_buffer*)monitor->canvas->buffer, 0, 0); + wl_surface_damage_buffer(monitor->surface, 0, 0, monitor->width, height); + wl_surface_commit(monitor->surface); +} + + +Monitor * +add_monitor() +{ + Monitor *last = monitors; + Monitor *mon = malloc(sizeof(Monitor)); + memset(mon, 0, sizeof(Monitor)); + + if (monitors == 0) { + monitors = mon; + } else { + for (; last->next; last = last->next); + last->next = mon; + } + + return mon; +} + +void +registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) +{ + struct wl_output *output; + Monitor *monitor; + + match_then_bind(client.shm, wl_shm_interface, 1) + or_match match_then_bind(client.compositor, wl_compositor_interface, 4) + or_match match_then_bind(client.layer, zwlr_layer_shell_v1_interface, 1) + or_match match_then_bind(client.dwl_manager, zdwl_manager_v1_interface, 1) + or_match match_then_bind(client.seat, wl_seat_interface, 7) + wl_seat_add_listener(client.seat, &seat_listener, &client); + or_match match_then_bind(output, wl_output_interface, 1) + monitor = add_monitor(); + monitor->output = output; + end_match +} + + +void +seat_capabilities(void *data, struct wl_seat *seat, uint32_t capabilities) +{ + int has_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; + + if (has_keyboard && client.keyboard == NULL) { + client.keyboard = wl_seat_get_keyboard(client.seat); + wl_keyboard_add_listener(client.keyboard, &keyboard_listener, &client); + } else { + wl_keyboard_release(client.keyboard); + client.keyboard = NULL; + } +} + + +void +dwl_output_active(void *data, struct zdwl_output_v1 *output, uint32_t active) +{ + Monitor *monitor = data; + + if (active && active_monitor == 0) { + active_monitor = data; + } +} + + +void +wlr_layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t w, uint32_t h) +{ + Monitor *mon = data; + mon->width = w; + + zwlr_layer_surface_v1_ack_configure(surface, serial); + + if (mon->canvas == 0) + mon->canvas = create_drw(client.shm, mon->width, height); + draw_dmenu(mon); +} + + +void +keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size) +{ + char *key_shm; + struct xkb_keymap *keymap; + struct xkb_state *state; + + if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) + return; + + key_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + + if (key_shm == MAP_FAILED) + return; + + keymap = xkb_keymap_new_from_string( + client.xkb_context, key_shm, + XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS + ); + + munmap(key_shm, size); + close(fd); + + state = xkb_state_new(keymap); + xkb_keymap_unref(client.xkb_keymap); + xkb_state_unref(client.xkb_state); + + client.xkb_state = state; + client.xkb_keymap = keymap; +} + + +void +keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) +{ + int genmatch = 0; + char buf[128]; + uint32_t keycode = key + 8; + xkb_keysym_t sym = xkb_state_key_get_one_sym(client.xkb_state, keycode); + xkb_keysym_get_name(sym, buf, sizeof(buf)); + xkb_state_key_get_utf8(client.xkb_state, keycode, buf, sizeof(buf)); + + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { + switch (sym) { + case XKB_KEY_KP_Enter: /* fallthrough */ + case XKB_KEY_Return: + fputs(options.selection ? options.selection->name: input_field, stdout); + running = 0; + break; + case XKB_KEY_Escape: + running = 0; + break; + case XKB_KEY_BackSpace: + if (strlen(input_field)) + input_field[strlen(input_field) - 1] = 0; + genmatch = 1; + break; + default: + strncpy(input_field + strlen(input_field), buf, MAX_LINE_LENGTH - strlen(input_field)); + genmatch = 1; + break; + } + + if (running) { + generate_matches(); + draw_dmenu(active_monitor); + } + } +} + + +void +keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) +{ + xkb_state_update_mask(client.xkb_state, depressed, latched, locked, 0, 0, group); + client.mod.alt = xkb_state_mod_name_is_active(client.xkb_state, XKB_MOD_NAME_ALT, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); + client.mod.control = xkb_state_mod_name_is_active(client.xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); + client.mod.shift = xkb_state_mod_name_is_active(client.xkb_state, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); +} + + +void +keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay) +{ + /* TODO */ +} + + +void +readstdin() +{ + unsigned s; + char *buf = malloc(MAX_LINE_LENGTH); + Option *option; + + while (fgets(buf, MAX_LINE_LENGTH, stdin) != 0) { + ++numitems; + option = malloc(sizeof(Option)); + option->name = buf; + s = strlen(option->name); + if (option->name[s - 1] == '\n') + option->name[s - 1] = 0; + option->next = 0; + option->next_match = 0; + option->previous_match = 0; + + if (options.first == 0) { + options.first = option; + options.last = option; + option->previous = 0; + } else { + options.last->next = option; + option->previous = options.last; + options.last = option; + } + + buf = malloc(MAX_LINE_LENGTH); + } + + free(buf); +} + + +void +setup() +{ + Monitor *mon; + char namespace[] = "dmenu-wl"; + uint32_t layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; + uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + + if (numitems < lines) + lines = numitems; + + generate_matches(); + height = (lines + 1) * (fontsize + padding * 2) + 3 * borderwidth; + + client.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + client.display = wl_display_connect(0); + client.registry = wl_display_get_registry(client.display); + wl_registry_add_listener(client.registry, ®istry_listnener, &client); + wl_display_roundtrip(client.display); + + + for (mon = monitors; mon; mon = mon->next) { + mon->dwl_output = zdwl_manager_v1_get_output(client.dwl_manager, mon->output); + zdwl_output_v1_add_listener(mon->dwl_output, &dwl_output_listener, mon); + } + wl_display_roundtrip(client.display); + + active_monitor->font = create_font(fontpath, fontsize * 1.5); + + active_monitor->surface = wl_compositor_create_surface(client.compositor); + active_monitor->wlr_surface = zwlr_layer_shell_v1_get_layer_surface(client.layer, active_monitor->surface, active_monitor->output, layer, namespace); + + zwlr_layer_surface_v1_set_exclusive_zone(active_monitor->wlr_surface, -1); + zwlr_layer_surface_v1_set_keyboard_interactivity(active_monitor->wlr_surface, 1); + + zwlr_layer_surface_v1_add_listener(active_monitor->wlr_surface, &wlr_layer_surface_listener, active_monitor); + + zwlr_layer_surface_v1_set_size(active_monitor->wlr_surface, 0, height); + zwlr_layer_surface_v1_set_anchor(active_monitor->wlr_surface, anchor); + + wl_surface_commit(active_monitor->surface); + wl_display_roundtrip(client.display); +} + + +int +main(int argc, char *argv[]) +{ + readstdin(); + setup(); + + while (running && wl_display_dispatch(client.display)) { + } + + wl_display_dispatch(client.display); +} @@ -0,0 +1,271 @@ +#include <sys/mman.h> +#include <unistd.h> +#include <math.h> +#include <grapheme.h> + +#include "drw.h" +#include "util.h" + +/* static function declarations */ +static Color to_color(uint32_t color); +static uint32_t to_uint32_t(Color color); +static void draw_bitmap(Canvas *canvas, FT_Bitmap *bitmap, unsigned x, unsigned y, uint32_t color); + + +Color +to_color(uint32_t color) +{ + return *(Color*)(&color); +} + + +uint32_t +to_uint32_t(Color color) +{ + return *(uint32_t*)(&color); +} + + +Canvas* +create_drw(struct wl_shm *shm, unsigned width, unsigned height) +{ + struct wl_shm_pool *pool; + Canvas *canvas; + int stride; + int fd; + + canvas = malloc(sizeof(Canvas)); + canvas->width = width; + canvas->height = height; + + stride = width * sizeof(uint32_t); + canvas->size = stride * height; + + fd = allocate_shm_file(canvas->size); + if (fd == -1) { + return NULL; + } + + canvas->data = mmap( + NULL, + canvas->size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0 + ); + + if (canvas->data == MAP_FAILED) { + close(fd); + return NULL; + } + + pool = wl_shm_create_pool( + shm, + fd, + canvas->size + ); + + canvas->buffer = wl_shm_pool_create_buffer( + pool, + 0, + width, + height, + stride, + WL_SHM_FORMAT_XRGB8888 + ); + + wl_shm_pool_destroy(pool); + close(fd); + + return canvas; +} + + +void +free_drw(Canvas *canvas) +{ + munmap(canvas->data, canvas->size); + free(canvas); +} + + +void +draw_point(Canvas *canvas, unsigned x, unsigned y, uint32_t color) +{ + if (!(x < canvas->width && y < canvas->height)) + return; + + canvas->data[x + canvas->width * y] = color; +} + + +void +draw_rect(Canvas *canvas, unsigned x, unsigned y, unsigned width, unsigned height, uint32_t color) { + int py; + int px; + + for (py = y; py < y + height; ++py) { + for (px = x; px < x + width; ++px) { + draw_point(canvas, px, py, color); + } + } +} + +Font * +create_font(const char *fontpath, unsigned size) +{ + FT_Error error; + Font * font; + + font = malloc(sizeof(Font)); + + error = FT_Init_FreeType(&font->library); + + if (error) + die("cannot init library"); + + error = FT_New_Face(font->library, fontpath, 0, &font->face); + if (error) + die("cannot open face"); + + error = FT_Set_Pixel_Sizes(font->face, 0, size); + if (error) + die("cannot set char size"); + + error = FT_Select_Charmap(font->face, ft_encoding_unicode); + if (error) + die("cannot set unicode"); + + return font; +} + +void +free_font(Font *font) +{ + FT_Done_Face(font->face); + FT_Done_FreeType(font->library); + free(font); +} + + +void +draw_bitmap(Canvas *canvas, FT_Bitmap *bitmap, unsigned x, unsigned y, uint32_t color) +{ + FT_Int i, j, p, q; + double strength; + uint32_t pixel; + + Color fg = to_color(color); + Color bg; + Color c; + + c.a = fg.a; + + i = x; + p = 0; + + while (p < bitmap->width) { + j = y; + q = 0; + + while (q < bitmap->rows) { + if (i < 0 || j < 0 || i >= canvas->width || j >= canvas->height) { + j++; + q++; + continue; + } + + bg = to_color(canvas->data[i + j * canvas->width]); + strength = bitmap->buffer[p + q * bitmap->width]; + strength /= 255; + + c.r = (strength * fg.r) + ((1 - strength) * bg.r); + c.g = (strength * fg.g) + ((1 - strength) * bg.g); + c.b = (strength * fg.b) + ((1 - strength) * bg.b); + + pixel = to_uint32_t(c); + draw_point(canvas, i, j, pixel); + + j++; + q++; + } + + i++; + p++; + } +} + + +unsigned +draw_font(Canvas *canvas, Font *font, const char *text, unsigned x, unsigned y, uint32_t color) +{ + FT_GlyphSlot slot; + FT_Error error; + FT_UInt glyph_index; + FT_UInt previous = 0; + uint_least32_t charcode; + FT_Bool has_kerning = FT_HAS_KERNING(font->face); + FT_Vector delta; + + slot = font->face->glyph; + + for (; *text;) { + text += grapheme_decode_utf8(text, sizeof(uint_least32_t), &charcode); + + glyph_index = FT_Get_Char_Index(font->face, charcode); + + if (has_kerning && previous && glyph_index) { + FT_Get_Kerning(font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); + x += delta.x >> 6; + } + + error = FT_Load_Glyph(font->face, glyph_index, FT_LOAD_RENDER); + if (error) { + fprintf(stderr, "warning: char not loaded, skipping...\n"); + continue; + } + + draw_bitmap(canvas, &slot->bitmap, x + slot->bitmap_left, y - slot->bitmap_top, color); + + x += slot->advance.x >> 6; + } + + return x; +} + +unsigned +font_width(Font *font, const char *text) +{ + FT_GlyphSlot slot; + FT_Error error; + FT_UInt glyph_index; + FT_UInt previous = 0; + uint_least32_t charcode; + FT_Bool has_kerning = FT_HAS_KERNING(font->face); + FT_Vector delta; + unsigned x = 0; + + slot = font->face->glyph; + + for (; *text;) { + text += grapheme_decode_utf8(text, sizeof(uint_least32_t), &charcode); + + glyph_index = FT_Get_Char_Index(font->face, charcode); + + if (has_kerning && previous && glyph_index) { + FT_Get_Kerning(font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); + x += delta.x >> 6; + } + + error = FT_Load_Glyph(font->face, glyph_index, FT_LOAD_RENDER); + if (error) { + fprintf(stderr, "warning: char not loaded, skipping...\n"); + continue; + } + + x += slot->advance.x >> 6; + } + + return x; +} @@ -0,0 +1,44 @@ +#ifndef DRW_H +#define DRW_H + +#include <stdint.h> +#include <ft2build.h> +#include FT_FREETYPE_H +#include FT_BITMAP_H + +#include "wayland.h" + +/* type definitions */ +typedef struct { + uint32_t *data; + unsigned width; + unsigned height; + unsigned size; + struct wl_buffer *buffer; +} Canvas; + +typedef struct { + FT_Library library; + FT_Face face; +} Font; + +typedef struct { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; +} Color; + +/* exported functions */ +Canvas* create_drw(struct wl_shm *shm, unsigned width, unsigned height); +void free_drw(Canvas *canvas); + +void draw_rect(Canvas *canvas, unsigned x, unsigned y, unsigned width, unsigned height, uint32_t color); +void draw_point(Canvas *canvas, unsigned x, unsigned y, uint32_t color); + +Font* create_font(const char *fontpath, unsigned size); +void free_font(Font *font); +unsigned draw_font(Canvas *canvas, Font *font, const char *text, unsigned x, unsigned y, uint32_t color); +unsigned font_width(Font *font, const char *text); + +#endif diff --git a/protocol/dwl-bar-ipc-unstable-v1.xml b/protocol/dwl-bar-ipc-unstable-v1.xml new file mode 100644 index 0000000..9c15246 --- /dev/null +++ b/protocol/dwl-bar-ipc-unstable-v1.xml @@ -0,0 +1,157 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +This is largely ripped from somebar's ipc patchset; just with some personal modifications. +I would probably just submit raphi's patchset but I don't think that would be polite. +--> +<protocol name="dwl_bar_ipc_unstable_v1"> + <description summary="inter-proccess-communication about dwl's state"> + This protocol allows clients to get updates from dwl and vice versa. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible + changes may be added together with the corresponding interface + version bump. + Backward incompatible changes are done by bumping the version + number in the protocol and interface names and resetting the + interface version. Once the protocol is to be declared stable, + the 'z' prefix and the version number in the protocol and + interface names are removed and the interface version number is + reset. + </description> + + <interface name="zdwl_manager_v1" version="2"> + <description summary="manage dwl state"> + This interface is exposed as a global in wl_registry. + + Clients can use this interface to get a dwl_output. + After binding the client will revieve dwl_manager.tag and dwl_manager.layout events. + The dwl_manager.tag and dwl_manager.layout events expose tags and layouts to the client. + </description> + + <request name="release" type="destructor"> + <description summary="release dwl_manager"> + Indicates that the client will not the dwl_manager object anymore. + Objects created through this instance are not affected. + </description> + </request> + + <request name="get_output"> + <description summary="get a dwl_output for a wl_output"> + Get a dwl_output for the specified wl_output. + </description> + <arg name="id" type="new_id" interface="zdwl_output_v1"/> + <arg name="output" type="object" interface="wl_output"/> + </request> + + <event name="tag"> + <description summary="Announces a tag"> + This event is sent after binding. + A roundtrip after binding guarantees the client recieved all tags. + </description> + <arg name="name" type="string"/> + </event> + + <event name="layout"> + <description summary="Announces a layout"> + This event is sent after binding. + A roundtrip after binding guarantees the client recieved all layouts. + </description> + <arg name="name" type="string"/> + </event> + </interface> + + <interface name="zdwl_output_v1" version="2"> + <description summary="control dwl output"> + Observe and control a dwl output. + + Events are double-buffered: + Clients should cache events and redraw when a dwl_output.done event is sent. + + Request are not double-buffered: + The compositor will update immediately upon request. + </description> + + <enum name="tag_state"> + <entry name="none" value="0" summary="no state"/> + <entry name="active" value="1" summary="tag is active"/> + <entry name="urgent" value="2" summary="tag has at least one urgent client"/> + </enum> + + <request name="release" type="destructor"> + <description summary="release dwl_output"> + Indicates to that the client no longer needs this dwl_output. + </description> + </request> + + <event name="toggle_visibility"> + <description summary="Toggle client visibilty"> + Indicates the client should hide or show themselves. + If the client is visible then hide, if hidden then show. + </description> + </event> + + <event name="active"> + <description summary="Update the selected output."> + Indicates if the output is active. Zero is invalid, nonzero is valid. + </description> + <arg name="active" type="uint"/> + </event> + + <event name="tag"> + <description summary="Update the state of a tag."> + Indicates that a tag has been updated. + </description> + <arg name="tag" type="uint" summary="Index of the tag"/> + <arg name="state" type="uint" enum="tag_state" summary="The state of the tag."/> + <arg name="clients" type="uint" summary="The number of clients in the tag."/> + <arg name="focused" type="uint" summary="If there is a focused client. Nonzero being valid, zero being invalid."/> + </event> + + <event name="layout"> + <description summary="Update the layout."> + Indicates a new layout is selected. + </description> + <arg name="layout" type="uint" summary="Index of the layout."/> + </event> + + <event name="title"> + <description summary="Update the title."> + Indicates the title has changed. + </description> + <arg name="title" type="string" summary="The new title name."/> + </event> + + <event name="appid" since="2"> + <description summary="Update the appid."> + Indicates the appid has changed. + </description> + <arg name="appid" type="string" summary="The new appid."/> + </event> + + <event name="frame"> + <description summary="The update sequence is done."> + Indicates that a sequence of status updates have finished and the client should redraw. + </description> + </event> + + <request name="set_layout"> + <description summary="Set the layout of this output"/> + <arg name="index" type="uint" summary="index of a layout recieved by dwl_manager.layout"/> + </request> + + <request name="set_tags"> + <description summary="Set the active tags of this output"/> + <arg name="tagmask" type="uint" summary="bitmask of the tags that should be set."/> + <arg name="toggle_tagset" type="uint" summary="toggle the selected tagset, zero for invalid, nonzero for valid."/> + </request> + + <request name="set_client_tags"> + <description summary="Set the tags of the focused client."> + The tags are updated as follows: + new_tags = (current_tags AND and_tags) XOR xor_tags + </description> + <arg name="and_tags" type="uint"/> + <arg name="xor_tags" type="uint"/> + </request> + </interface> +</protocol> diff --git a/protocol/wlr-layer-shell-unstable.xml b/protocol/wlr-layer-shell-unstable.xml new file mode 100644 index 0000000..d62fd51 --- /dev/null +++ b/protocol/wlr-layer-shell-unstable.xml @@ -0,0 +1,390 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="wlr_layer_shell_unstable_v1"> + <copyright> + Copyright © 2017 Drew DeVault + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + </copyright> + + <interface name="zwlr_layer_shell_v1" version="4"> + <description summary="create surfaces that are layers of the desktop"> + Clients can use this interface to assign the surface_layer role to + wl_surfaces. Such surfaces are assigned to a "layer" of the output and + rendered with a defined z-depth respective to each other. They may also be + anchored to the edges and corners of a screen and specify input handling + semantics. This interface should be suitable for the implementation of + many desktop shell components, and a broad number of other applications + that interact with the desktop. + </description> + + <request name="get_layer_surface"> + <description summary="create a layer_surface from a surface"> + Create a layer surface for an existing surface. This assigns the role of + layer_surface, or raises a protocol error if another role is already + assigned. + + Creating a layer surface from a wl_surface which has a buffer attached + or committed is a client error, and any attempts by a client to attach + or manipulate a buffer prior to the first layer_surface.configure call + must also be treated as errors. + + After creating a layer_surface object and setting it up, the client + must perform an initial commit without any buffer attached. + The compositor will reply with a layer_surface.configure event. + The client must acknowledge it and is then allowed to attach a buffer + to map the surface. + + You may pass NULL for output to allow the compositor to decide which + output to use. Generally this will be the one that the user most + recently interacted with. + + Clients can specify a namespace that defines the purpose of the layer + surface. + </description> + <arg name="id" type="new_id" interface="zwlr_layer_surface_v1"/> + <arg name="surface" type="object" interface="wl_surface"/> + <arg name="output" type="object" interface="wl_output" allow-null="true"/> + <arg name="layer" type="uint" enum="layer" summary="layer to add this surface to"/> + <arg name="namespace" type="string" summary="namespace for the layer surface"/> + </request> + + <enum name="error"> + <entry name="role" value="0" summary="wl_surface has another role"/> + <entry name="invalid_layer" value="1" summary="layer value is invalid"/> + <entry name="already_constructed" value="2" summary="wl_surface has a buffer attached or committed"/> + </enum> + + <enum name="layer"> + <description summary="available layers for surfaces"> + These values indicate which layers a surface can be rendered in. They + are ordered by z depth, bottom-most first. Traditional shell surfaces + will typically be rendered between the bottom and top layers. + Fullscreen shell surfaces are typically rendered at the top layer. + Multiple surfaces can share a single layer, and ordering within a + single layer is undefined. + </description> + + <entry name="background" value="0"/> + <entry name="bottom" value="1"/> + <entry name="top" value="2"/> + <entry name="overlay" value="3"/> + </enum> + + <!-- Version 3 additions --> + + <request name="destroy" type="destructor" since="3"> + <description summary="destroy the layer_shell object"> + This request indicates that the client will not use the layer_shell + object any more. Objects that have been created through this instance + are not affected. + </description> + </request> + </interface> + + <interface name="zwlr_layer_surface_v1" version="4"> + <description summary="layer metadata interface"> + An interface that may be implemented by a wl_surface, for surfaces that + are designed to be rendered as a layer of a stacked desktop-like + environment. + + Layer surface state (layer, size, anchor, exclusive zone, + margin, interactivity) is double-buffered, and will be applied at the + time wl_surface.commit of the corresponding wl_surface is called. + + Attaching a null buffer to a layer surface unmaps it. + + Unmapping a layer_surface means that the surface cannot be shown by the + compositor until it is explicitly mapped again. The layer_surface + returns to the state it had right after layer_shell.get_layer_surface. + The client can re-map the surface by performing a commit without any + buffer attached, waiting for a configure event and handling it as usual. + </description> + + <request name="set_size"> + <description summary="sets the size of the surface"> + Sets the size of the surface in surface-local coordinates. The + compositor will display the surface centered with respect to its + anchors. + + If you pass 0 for either value, the compositor will assign it and + inform you of the assignment in the configure event. You must set your + anchor to opposite edges in the dimensions you omit; not doing so is a + protocol error. Both values are 0 by default. + + Size is double-buffered, see wl_surface.commit. + </description> + <arg name="width" type="uint"/> + <arg name="height" type="uint"/> + </request> + + <request name="set_anchor"> + <description summary="configures the anchor point of the surface"> + Requests that the compositor anchor the surface to the specified edges + and corners. If two orthogonal edges are specified (e.g. 'top' and + 'left'), then the anchor point will be the intersection of the edges + (e.g. the top left corner of the output); otherwise the anchor point + will be centered on that edge, or in the center if none is specified. + + Anchor is double-buffered, see wl_surface.commit. + </description> + <arg name="anchor" type="uint" enum="anchor"/> + </request> + + <request name="set_exclusive_zone"> + <description summary="configures the exclusive geometry of this surface"> + Requests that the compositor avoids occluding an area with other + surfaces. The compositor's use of this information is + implementation-dependent - do not assume that this region will not + actually be occluded. + + A positive value is only meaningful if the surface is anchored to one + edge or an edge and both perpendicular edges. If the surface is not + anchored, anchored to only two perpendicular edges (a corner), anchored + to only two parallel edges or anchored to all edges, a positive value + will be treated the same as zero. + + A positive zone is the distance from the edge in surface-local + coordinates to consider exclusive. + + Surfaces that do not wish to have an exclusive zone may instead specify + how they should interact with surfaces that do. If set to zero, the + surface indicates that it would like to be moved to avoid occluding + surfaces with a positive exclusive zone. If set to -1, the surface + indicates that it would not like to be moved to accommodate for other + surfaces, and the compositor should extend it all the way to the edges + it is anchored to. + + For example, a panel might set its exclusive zone to 10, so that + maximized shell surfaces are not shown on top of it. A notification + might set its exclusive zone to 0, so that it is moved to avoid + occluding the panel, but shell surfaces are shown underneath it. A + wallpaper or lock screen might set their exclusive zone to -1, so that + they stretch below or over the panel. + + The default value is 0. + + Exclusive zone is double-buffered, see wl_surface.commit. + </description> + <arg name="zone" type="int"/> + </request> + + <request name="set_margin"> + <description summary="sets a margin from the anchor point"> + Requests that the surface be placed some distance away from the anchor + point on the output, in surface-local coordinates. Setting this value + for edges you are not anchored to has no effect. + + The exclusive zone includes the margin. + + Margin is double-buffered, see wl_surface.commit. + </description> + <arg name="top" type="int"/> + <arg name="right" type="int"/> + <arg name="bottom" type="int"/> + <arg name="left" type="int"/> + </request> + + <enum name="keyboard_interactivity"> + <description summary="types of keyboard interaction possible for a layer shell surface"> + Types of keyboard interaction possible for layer shell surfaces. The + rationale for this is twofold: (1) some applications are not interested + in keyboard events and not allowing them to be focused can improve the + desktop experience; (2) some applications will want to take exclusive + keyboard focus. + </description> + + <entry name="none" value="0"> + <description summary="no keyboard focus is possible"> + This value indicates that this surface is not interested in keyboard + events and the compositor should never assign it the keyboard focus. + + This is the default value, set for newly created layer shell surfaces. + + This is useful for e.g. desktop widgets that display information or + only have interaction with non-keyboard input devices. + </description> + </entry> + <entry name="exclusive" value="1"> + <description summary="request exclusive keyboard focus"> + Request exclusive keyboard focus if this surface is above the shell surface layer. + + For the top and overlay layers, the seat will always give + exclusive keyboard focus to the top-most layer which has keyboard + interactivity set to exclusive. If this layer contains multiple + surfaces with keyboard interactivity set to exclusive, the compositor + determines the one receiving keyboard events in an implementation- + defined manner. In this case, no guarantee is made when this surface + will receive keyboard focus (if ever). + + For the bottom and background layers, the compositor is allowed to use + normal focus semantics. + + This setting is mainly intended for applications that need to ensure + they receive all keyboard events, such as a lock screen or a password + prompt. + </description> + </entry> + <entry name="on_demand" value="2" since="4"> + <description summary="request regular keyboard focus semantics"> + This requests the compositor to allow this surface to be focused and + unfocused by the user in an implementation-defined manner. The user + should be able to unfocus this surface even regardless of the layer + it is on. + + Typically, the compositor will want to use its normal mechanism to + manage keyboard focus between layer shell surfaces with this setting + and regular toplevels on the desktop layer (e.g. click to focus). + Nevertheless, it is possible for a compositor to require a special + interaction to focus or unfocus layer shell surfaces (e.g. requiring + a click even if focus follows the mouse normally, or providing a + keybinding to switch focus between layers). + + This setting is mainly intended for desktop shell components (e.g. + panels) that allow keyboard interaction. Using this option can allow + implementing a desktop shell that can be fully usable without the + mouse. + </description> + </entry> + </enum> + + <request name="set_keyboard_interactivity"> + <description summary="requests keyboard events"> + Set how keyboard events are delivered to this surface. By default, + layer shell surfaces do not receive keyboard events; this request can + be used to change this. + + This setting is inherited by child surfaces set by the get_popup + request. + + Layer surfaces receive pointer, touch, and tablet events normally. If + you do not want to receive them, set the input region on your surface + to an empty region. + + Keyboard interactivity is double-buffered, see wl_surface.commit. + </description> + <arg name="keyboard_interactivity" type="uint" enum="keyboard_interactivity"/> + </request> + + <request name="get_popup"> + <description summary="assign this layer_surface as an xdg_popup parent"> + This assigns an xdg_popup's parent to this layer_surface. This popup + should have been created via xdg_surface::get_popup with the parent set + to NULL, and this request must be invoked before committing the popup's + initial state. + + See the documentation of xdg_popup for more details about what an + xdg_popup is and how it is used. + </description> + <arg name="popup" type="object" interface="xdg_popup"/> + </request> + + <request name="ack_configure"> + <description summary="ack a configure event"> + When a configure event is received, if a client commits the + surface in response to the configure event, then the client + must make an ack_configure request sometime before the commit + request, passing along the serial of the configure event. + + If the client receives multiple configure events before it + can respond to one, it only has to ack the last configure event. + + A client is not required to commit immediately after sending + an ack_configure request - it may even ack_configure several times + before its next surface commit. + + A client may send multiple ack_configure requests before committing, but + only the last request sent before a commit indicates which configure + event the client really is responding to. + </description> + <arg name="serial" type="uint" summary="the serial from the configure event"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy the layer_surface"> + This request destroys the layer surface. + </description> + </request> + + <event name="configure"> + <description summary="suggest a surface change"> + The configure event asks the client to resize its surface. + + Clients should arrange their surface for the new states, and then send + an ack_configure request with the serial sent in this configure event at + some point before committing the new surface. + + The client is free to dismiss all but the last configure event it + received. + + The width and height arguments specify the size of the window in + surface-local coordinates. + + The size is a hint, in the sense that the client is free to ignore it if + it doesn't resize, pick a smaller size (to satisfy aspect ratio or + resize in steps of NxM pixels). If the client picks a smaller size and + is anchored to two opposite anchors (e.g. 'top' and 'bottom'), the + surface will be centered on this axis. + + If the width or height arguments are zero, it means the client should + decide its own window dimension. + </description> + <arg name="serial" type="uint"/> + <arg name="width" type="uint"/> + <arg name="height" type="uint"/> + </event> + + <event name="closed"> + <description summary="surface should be closed"> + The closed event is sent by the compositor when the surface will no + longer be shown. The output may have been destroyed or the user may + have asked for it to be removed. Further changes to the surface will be + ignored. The client should destroy the resource after receiving this + event, and create a new surface if they so choose. + </description> + </event> + + <enum name="error"> + <entry name="invalid_surface_state" value="0" summary="provided surface state is invalid"/> + <entry name="invalid_size" value="1" summary="size is invalid"/> + <entry name="invalid_anchor" value="2" summary="anchor bitfield is invalid"/> + <entry name="invalid_keyboard_interactivity" value="3" summary="keyboard interactivity is invalid"/> + </enum> + + <enum name="anchor" bitfield="true"> + <entry name="top" value="1" summary="the top edge of the anchor rectangle"/> + <entry name="bottom" value="2" summary="the bottom edge of the anchor rectangle"/> + <entry name="left" value="4" summary="the left edge of the anchor rectangle"/> + <entry name="right" value="8" summary="the right edge of the anchor rectangle"/> + </enum> + + <!-- Version 2 additions --> + + <request name="set_layer" since="2"> + <description summary="change the layer of the surface"> + Change the layer that the surface is rendered on. + + Layer is double-buffered, see wl_surface.commit. + </description> + <arg name="layer" type="uint" enum="zwlr_layer_shell_v1.layer" summary="layer to move this surface to"/> + </request> + </interface> +</protocol> @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "util.h" + +void +die(const char *error) +{ + fprintf(stderr, "error: %s\n", error); + exit(-1); +} @@ -0,0 +1,6 @@ +#ifndef UTIL_H +#define UTIL_H + +void die(const char *error); + +#endif diff --git a/wayland.c b/wayland.c new file mode 100644 index 0000000..0787ec3 --- /dev/null +++ b/wayland.c @@ -0,0 +1,66 @@ +#include <errno.h> +#include <time.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <unistd.h> + +#include "wayland.h" + +static void randname(char *buf); +static int create_shm_file(void); + + +void +randname(char *buf) +{ + struct timespec ts; + long r; + int i; + + clock_gettime(CLOCK_REALTIME, &ts); + r = ts.tv_nsec; + for (i = 0; i < 6; ++i) { + buf[i] = 'A'+(r&15)+(r&16)*2; + r >>= 5; + } +} + +int +create_shm_file(void) +{ + int fd; + int retries = 100; + char name[] = "/wl_shm-XXXXXX"; + + do { + randname(name + sizeof(name) - 7); + --retries; + fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + return -1; +} + +int +allocate_shm_file(size_t size) +{ + int ret; + int fd = create_shm_file(); + + if (fd < 0) + return -1; + + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} diff --git a/wayland.h b/wayland.h new file mode 100644 index 0000000..5646f48 --- /dev/null +++ b/wayland.h @@ -0,0 +1,11 @@ +#ifndef WAYLAND_H +#define WAYLAND_H + +#include <wayland-client.h> +#include "xdg-shell-protocol.h" +#include "wlr-layer-shell-protocol.h" +#include "dwl-bar-ipc-protocol.h" + +int allocate_shm_file(size_t size); + +#endif |