diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-20 15:21:21 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-20 15:21:21 +0100 |
| commit | 05e97f8346521b50fbedcc2e7d0679e297d4d98b (patch) | |
| tree | 6f0a909ffd1c5bcf5e1b95fa7ae61d721826e7db | |
make first sketch of bar using freetype2 instead of cairo
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 48 | ||||
| -rw-r--r-- | config.def.h | 11 | ||||
| -rw-r--r-- | dbar.c | 494 | ||||
| -rw-r--r-- | drw.c | 271 | ||||
| -rw-r--r-- | drw.h | 44 | ||||
| -rw-r--r-- | protocols/dwl-bar-ipc-unstable-v1.xml | 157 | ||||
| -rw-r--r-- | protocols/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 | 34 |
12 files changed, 1537 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d817976 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +compile_commands.json +.cache +*.o +dbar +*-protocol.* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1616c99 --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +PROTOCOLS = xdg-shell-client-protocol.c wlr-layer-shell-unstable.c xdg-output-unstable-v1-client-protocol.c dwl-bar-ipc-unstable-v1-protocol.c +SRC = ${PROTOCOLS} dbar.c drw.c util.c wayland.c +OBJ = ${SRC:.c=.o} +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 +LIBS = -lfreetype -lwayland-client -lrt -lxkbcommon -lm -lgrapheme + +all: dbar + +config.h: + cp config.def.h config.h + +dwl-bar-ipc-unstable-v1-protocol.h: + wayland-scanner client-header < protocols/dwl-bar-ipc-unstable-v1.xml > dwl-bar-ipc-unstable-v1-protocol.h + +dwl-bar-ipc-unstable-v1-protocol.c: dwl-bar-ipc-unstable-v1-protocol.h + wayland-scanner private-code < protocols/dwl-bar-ipc-unstable-v1.xml > dwl-bar-ipc-unstable-v1-protocol.c + +xdg-shell-client-protocol.h: + wayland-scanner client-header < /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml > xdg-shell-client-protocol.h + +xdg-shell-client-protocol.c: xdg-shell-client-protocol.h + wayland-scanner private-code < /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml > xdg-shell-client-protocol.c + +xdg-output-unstable-v1-client-protocol.h: + wayland-scanner client-header < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1-client-protocol.h + +xdg-output-unstable-v1-client-protocol.c: xdg-output-unstable-v1-client-protocol.h + wayland-scanner private-code < /usr/share/wayland-protocols/unstable/xdg-output/xdg-output-unstable-v1.xml > xdg-output-unstable-v1-client-protocol.c + +wlr-layer-shell-unstable.c: wlr-layer-shell-unstable.h + wayland-scanner private-code < protocols/wlr-layer-shell-unstable.xml > wlr-layer-shell-unstable.c + +wlr-layer-shell-unstable.h: + wayland-scanner client-header < protocols/wlr-layer-shell-unstable.xml > wlr-layer-shell-unstable.h + +.c.o: + gcc -c $< ${CFLAGS} + +${OBJ}: ${PROTOCOLS} config.h + +dbar: ${OBJ} ${SRC} + gcc ${OBJ} -o dbar ${LIBS} + +run: dbar + ./dbar + +clean: + rm *.o dbar ${PROTOCOLS} ${PROTOCOLS:.c=.h} diff --git a/config.def.h b/config.def.h new file mode 100644 index 0000000..edfc824 --- /dev/null +++ b/config.def.h @@ -0,0 +1,11 @@ +#include <stdint.h> + +/* The colors are in ARGB format */ +static uint32_t background = 0xff282828; +static uint32_t foreground = 0xffffffff; +static uint32_t highlight = 0xffb16286; + +static uint32_t height = 30; +static uint32_t padding = 10; +static const char *fontpath = "/usr/share/fonts/TTF/Sauce Code Pro Nerd Font Complete Mono.ttf"; +static const char *statusfile = "/tmp/statusline"; @@ -0,0 +1,494 @@ +#define _POSIX_C_SOURCE 200112L +#include <assert.h> +#include <string.h> +#include <wayland-client.h> +#include <xkbcommon/xkbcommon.h> +#include <sys/mman.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <poll.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/inotify.h> + +#include "drw.h" +#include "util.h" + +/* type definitions */ +typedef struct Bar Bar; +struct Bar { + unsigned width; + uint32_t seltag; + uint32_t urgtag; + uint32_t clients; + uint32_t active; + uint32_t layout; + char *progname; + uint32_t update_count; + Canvas *canvas; + struct zdwl_output_v1 *dwl_output; + struct wl_output *wl_output; + struct wl_surface *wl_surface; + struct zwlr_layer_surface_v1 *zwlr_surface; + Bar *next; +}; + +/* function declarations */ +static void update_bar(Bar *bar); +static void wl_buffer_release(void *data, struct wl_buffer *wl_buffer); +static void registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version); +static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t w, uint32_t h); +static void layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface); +static void dwl_manager_tag(void *data, struct zdwl_manager_v1 *zdwl_manager_v1, const char *name); +static void dwl_manager_layout(void *data, struct zdwl_manager_v1 *zdwl_manager_v1, const char *name); +static void dwl_output_active(void *data, struct zdwl_output_v1 *zdwl_output_v1, uint32_t active); +static void dwl_output_tag(void *data, struct zdwl_output_v1 *zdwl_output_v1, uint32_t tag, uint32_t state, uint32_t clients, uint32_t focused); +static void dwl_output_title(void *data, struct zdwl_output_v1 *zdwl_output_v1, const char *title); +static void dwl_output_layout(void *data, struct zdwl_output_v1 *zdwl_output_v1, uint32_t layout); +static void dummy(); +static void wayland_flush(); +static void setup(); +struct wl_buffer *draw_frame(Bar *bar); +static Bar *add_bar(); + + +/* global variables */ +static const struct wl_buffer_listener wl_buffer_listener = { + .release = wl_buffer_release, +}; + +static const struct wl_registry_listener wl_registry_listener = { + .global = registry_global, + .global_remove = dummy, +}; + +static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { + .configure = layer_surface_configure, + .closed = layer_surface_closed +}; + +static const struct zdwl_manager_v1_listener dwl_manager_listener = { + .tag = dwl_manager_tag, + .layout = dwl_manager_layout, +}; + +static const struct zdwl_output_v1_listener dwl_output_listener = { + .toggle_visibility = dummy, + .active = dwl_output_active, + .tag = dwl_output_tag, + .layout = dwl_output_layout, + .title = dwl_output_title, + .appid = dummy, + .frame = dummy, +}; + +static Bar *bars = NULL; +static char statusline[1024] = ""; +struct client_state bar_state = { 0 }; +struct pollfd pollfds[2]; +int displayfd; + +/* configuration */ +#include "config.h" + +/* function implementations */ +void +wl_buffer_release(void *data, struct wl_buffer *wl_buffer) +{ + fprintf(stderr, "Buffer Release\n"); + wl_buffer_destroy(wl_buffer); +} + + +struct wl_buffer * +draw_frame(Bar *bar) +{ + + Canvas *canvas = bar->canvas; + + if (canvas == 0) + return 0; + + struct wl_buffer *buffer = canvas->buffer; + Font *font = create_font(fontpath, (height - padding * 2) * 1.5); + unsigned w; + unsigned x = 0; + unsigned i = 0; + ListElement *tag; + ListElement *layout; + + draw_rect(canvas, 0, 0, bar->width, height, background); + + for (tag = bar_state.tags.first; tag; tag = tag->next) { + x += padding; + + w = font_width(font, tag->name); + + if (bar->urgtag & (1 << i)) { + draw_rect(canvas, x - padding, 0, w + 2 * padding, height, foreground); + draw_font(canvas, font, tag->name, x, height - padding, background); + } else { + if (bar->seltag & (1 << i)) { + draw_rect(canvas, x - padding, 0, w + 2 * padding, height, highlight); + } + draw_font(canvas, font, tag->name, x, height - padding, foreground); + } + + + x += padding + w; + ++i; + } + + i = 0; + for (layout = bar_state.layouts.first; layout; layout = layout->next) { + if (i++ == bar->layout) + break; + } + + x = draw_font(canvas, font, layout->name, x + padding, height - padding, foreground); + + w = font_width(font, statusline); + + x += 3 * padding; + x = draw_font(canvas, font, bar->progname, x, height - padding, foreground); + + if (bar->active) { + draw_rect(canvas, bar->width - 2 * padding - w, 0, bar->width, height, highlight); + draw_font(canvas, font, statusline, bar->width - padding - w, height - padding, foreground); + } + + + free_font(font); + return buffer; +} + +void +update_bar(Bar *bar) +{ + struct wl_buffer *buffer = draw_frame(bar); + + if (!buffer) + return; + + wl_surface_attach(bar->wl_surface, buffer, 0, 0); + wl_surface_damage_buffer(bar->wl_surface, 0, 0, bar->width, height); + wl_surface_commit(bar->wl_surface); +} + + +void +registry_global(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface, uint32_t version) +{ + struct client_state *state = data; + Bar *bar; + + if (strcmp(interface, wl_shm_interface.name) == 0) { + state->wl_shm = wl_registry_bind( + wl_registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_compositor_interface.name) == 0) { + state->wl_compositor = wl_registry_bind( + wl_registry, name, &wl_compositor_interface, 4); + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + state->zwlr_layer = wl_registry_bind(wl_registry, name, &zwlr_layer_shell_v1_interface, 1); + } else if (strcmp(interface, zdwl_manager_v1_interface.name) == 0) { + state->dwl_manager = wl_registry_bind(wl_registry, name, &zdwl_manager_v1_interface, 1); + } else if (strcmp(interface, wl_output_interface.name) == 0) { + bar = add_bar(); + bar->wl_output = wl_registry_bind(wl_registry, name, &wl_output_interface, 1); + } +} + + +void +layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t w, uint32_t h) +{ + Bar *bar = data; + + bar->width = w; + height = h; + + zwlr_layer_surface_v1_set_exclusive_zone(surface, height); + zwlr_layer_surface_v1_ack_configure(surface, serial); + + bar->canvas = create_drw(bar_state.wl_shm, bar->width, height); + + update_bar(bar); +} + + +void +layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) +{ + Bar *bar = data; + zwlr_layer_surface_v1_destroy(surface); + wl_surface_destroy(bar->wl_surface); +} + +void +dwl_manager_tag(void *data, struct zdwl_manager_v1 *zdwl_manager_v1, const char *name) +{ + struct client_state *state = data; + ListElement *tag = state->tags.first; + + ++state->tags.size; + + if (state->tags.first == 0) { + state->tags.first = malloc(sizeof(ListElement)); + state->tags.last = state->tags.first; + state->tags.first->name = malloc(strlen(name)); + strcpy((char *)state->tags.first->name, name); + state->tags.first->next = 0; + state->tags.first->previous = 0; + return; + } + + for (; tag->next; tag = tag->next); + + tag->next = malloc(sizeof(ListElement)); + tag->next->previous = tag; + tag = tag->next; + tag->name = malloc(strlen(name)); + strcpy((char *)tag->name, name); + tag->next = 0; + + state->tags.last = tag; +} + + +void +dwl_manager_layout(void *data, struct zdwl_manager_v1 *zdwl_manager_v1, const char *name) +{ + struct client_state *state = data; + ListElement *layout = state->layouts.first; + + ++state->layouts.size; + + if (state->layouts.first == 0) { + state->layouts.first = malloc(sizeof(ListElement)); + state->layouts.last = state->layouts.first; + state->layouts.first->name = malloc(strlen(name)); + strcpy((char *)state->layouts.first->name, name); + state->layouts.first->next = 0; + state->layouts.first->previous = 0; + return; + } + + for (; layout->next; layout = layout->next); + + layout->next = malloc(sizeof(ListElement)); + layout->next->previous = layout; + layout = layout->next; + layout->name = malloc(strlen(name)); + strcpy((char *)layout->name, name); + layout->next = 0; + + state->layouts.last = layout; +} + + +void +dwl_output_active(void *data, struct zdwl_output_v1* zdwl_output_v1, uint32_t active) +{ + Bar *bar = data; + bar->active = active; + update_bar(bar); +} + + +void +dwl_output_tag(void *data, struct zdwl_output_v1 *zdwl_output_v1, uint32_t tag, uint32_t state, uint32_t clients, uint32_t focused) +{ + Bar *bar = data; + + if (state == ZDWL_OUTPUT_V1_TAG_STATE_ACTIVE) { + bar->seltag |= 1 << tag; + bar->urgtag &= ~(1 << tag); + } else if (state == ZDWL_OUTPUT_V1_TAG_STATE_URGENT) { + bar->urgtag |= 1 << tag; + bar->seltag &= ~(1 << tag); + } else { + bar->seltag &= ~(1 << tag); + bar->urgtag &= ~(1 << tag); + } + + bar->clients = clients; + + if (++bar->update_count == bar_state.tags.size) { + update_bar(bar); + } +} + + +void +dwl_output_title(void *data, struct zdwl_output_v1 *zdwl_output_v1, const char *title) +{ + Bar *bar = data; + + if (bar->progname) + free(bar->progname); + + bar->progname = malloc(strlen(title)); + strcpy(bar->progname, title); +} + + +void +dwl_output_layout(void *data, struct zdwl_output_v1 *zdwl_output_v1, uint32_t layout) +{ + Bar *bar = data; + bar->layout = layout; + update_bar(bar); +} + + +void +dummy() +{ + /* Who cares */ +} + + +Bar * +add_bar() +{ + Bar *bar = bars; + + if (!bar) { + bars = malloc(sizeof(Bar)); + memset(bars, 0, sizeof(Bar)); + return bars; + } + + for (; bar->next; bar = bar->next); + + bar->next = malloc(sizeof(Bar)); + memset(bar->next, 0, sizeof(Bar)); + + return bar->next; +} + + +void +setup() +{ + Bar *bar; + char *namespace = "dbar"; + 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; + + bar_state.wl_display = wl_display_connect(NULL); + bar_state.wl_registry = wl_display_get_registry(bar_state.wl_display); + wl_registry_add_listener(bar_state.wl_registry, &wl_registry_listener, &bar_state); + wl_display_roundtrip(bar_state.wl_display); + + zdwl_manager_v1_add_listener(bar_state.dwl_manager, &dwl_manager_listener, &bar_state); + wl_display_roundtrip(bar_state.wl_display); + + for (bar = bars; bar; bar = bar->next) { + bar->dwl_output = zdwl_manager_v1_get_output(bar_state.dwl_manager, bar->wl_output); + zdwl_output_v1_add_listener(bar->dwl_output, &dwl_output_listener, bar); + + bar->wl_surface = wl_compositor_create_surface(bar_state.wl_compositor); + bar->zwlr_surface = zwlr_layer_shell_v1_get_layer_surface(bar_state.zwlr_layer, bar->wl_surface, bar->wl_output, layer, namespace); + + zwlr_layer_surface_v1_add_listener(bar->zwlr_surface, &layer_surface_listener, bar); + + zwlr_layer_surface_v1_set_size(bar->zwlr_surface, bar->width, height); + zwlr_layer_surface_v1_set_anchor(bar->zwlr_surface, anchor); + + wl_surface_commit(bar->wl_surface); + wl_display_roundtrip(bar_state.wl_display); + } + +} + + +void +wayland_flush() +{ + int i; + + wl_display_dispatch_pending(bar_state.wl_display); + + if (wl_display_flush(bar_state.wl_display) < 0 && errno == EAGAIN) { + for (i = 0; i < 2; ++i) { + if (pollfds[i].fd == displayfd) { + pollfds[i].events |= POLLOUT; + } + } + } +} + + +int +main(int argc, char *argv[]) +{ + Bar *bar; + int status; + struct inotify_event event; + char laststat[1024]; + int s; + int i; + int inotify_fd = inotify_init1(IN_NONBLOCK); + int running = 1; + + setup(); + + status = open(statusfile, O_CREAT, 0600); + close(status); + + inotify_add_watch(inotify_fd, statusfile, IN_MODIFY); + + displayfd = wl_display_get_fd(bar_state.wl_display); + + pollfds[0].fd = inotify_fd; + pollfds[0].events = POLLIN; + pollfds[1].fd = displayfd; + pollfds[1].events = POLLIN; + + status = open(statusfile, O_RDONLY); + + + while (running) { + + wayland_flush(); + + if (poll(pollfds, 2, -1) > 0) { + for (i = 0; i < 2; ++i) { + if (pollfds[i].fd == inotify_fd && (pollfds[i].revents & POLLIN)) { + read(pollfds[i].fd, &event, sizeof(struct inotify_event)); + + if (event.mask & IN_MODIFY) { + lseek(status, 0, SEEK_SET); + s = read(status, statusline, 1024); + statusline[s - 1] = 0; + + if (strcmp(laststat, statusline) != 0) { + for (bar = bars; bar; bar = bar->next) + update_bar(bar); + } + + strcpy(laststat, statusline); + } + + } else if (pollfds[i].fd == displayfd) { + if (pollfds[i].revents & POLLIN) { + if (wl_display_dispatch(bar_state.wl_display) < 0) + running = 0; + } + + if (pollfds[i].revents & POLLOUT) { + pollfds[i].events = POLLIN; + wayland_flush(); + } + } + } + } + } + + close(status); + + return 0; +} @@ -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/protocols/dwl-bar-ipc-unstable-v1.xml b/protocols/dwl-bar-ipc-unstable-v1.xml new file mode 100644 index 0000000..9c15246 --- /dev/null +++ b/protocols/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/protocols/wlr-layer-shell-unstable.xml b/protocols/wlr-layer-shell-unstable.xml new file mode 100644 index 0000000..d62fd51 --- /dev/null +++ b/protocols/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..4844d28 --- /dev/null +++ b/wayland.h @@ -0,0 +1,34 @@ +#ifndef WAYLAND_H +#define WAYLAND_H + +#include "xdg-shell-client-protocol.h" +#include "wlr-layer-shell-unstable.h" +#include "dwl-bar-ipc-unstable-v1-protocol.h" + +typedef struct ListElement ListElement; +struct ListElement { + const char *name; + ListElement *next; + ListElement *previous; +}; + +typedef struct { + ListElement *first; + ListElement *last; + uint32_t size; +} List; + +struct client_state { + struct wl_display *wl_display; + struct wl_registry *wl_registry; + struct wl_shm *wl_shm; + struct wl_compositor *wl_compositor; + struct zwlr_layer_shell_v1 *zwlr_layer; + struct zdwl_manager_v1 *dwl_manager; + List tags; + List layouts; +}; + +int allocate_shm_file(size_t size); + +#endif |