dmw

wayland fork of dwm using neuswc/neuwld
git clone https://git.daat.foo/dmw.git
Log | Files | Refs | README | LICENSE

bar.c (9176B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "bar.h"
      3 #include "swc-client-protocol.h"
      4 
      5 #include <errno.h>
      6 #include <fcntl.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <sys/mman.h>
     11 #include <sys/socket.h>
     12 #include <unistd.h>
     13 #include <wayland-client.h>
     14 #include <wayland-server-core.h>
     15 #include <wld/wld.h>
     16 #include <wld/pixman.h>
     17 
     18 #include "util.h"
     19 
     20 struct Canvas {
     21 	struct wl_buffer *buffer;
     22 	struct wld_buffer *pixels;
     23 	void *addr;
     24 	size_t size;
     25 	int width, busy;
     26 	struct Canvas *next;
     27 };
     28 
     29 static struct wl_client *peer;
     30 static struct wl_display *client;
     31 static struct wl_event_loop *loop;
     32 static struct wl_event_source *source;
     33 static struct wl_registry *registry;
     34 static struct wl_compositor *compositor;
     35 static struct wl_seat *seat;
     36 static struct wl_pointer *pointer;
     37 static struct wl_shm *shm;
     38 static struct swc_panel_manager *panels;
     39 static struct wld_renderer *renderer;
     40 static struct wld_font_context *fonts;
     41 struct wld_font *barfont;
     42 static struct Bar *hover;
     43 static int pointerx, top;
     44 
     45 static void
     46 flush(void)
     47 {
     48 	uint32_t events = WL_EVENT_READABLE;
     49 
     50 	if (wl_display_flush(client) < 0) {
     51 		if (errno != EAGAIN)
     52 			die("dmw: bar connection:");
     53 		events |= WL_EVENT_WRITABLE;
     54 	}
     55 	wl_event_source_fd_update(source, events);
     56 }
     57 
     58 static int
     59 dispatch(int fd, uint32_t events, void *data)
     60 {
     61 	if (events & (WL_EVENT_HANGUP | WL_EVENT_ERROR) ||
     62 	    ((events & WL_EVENT_READABLE) && wl_display_dispatch(client) < 0))
     63 		die("dmw: bar disconnected");
     64 	flush();
     65 	return 0;
     66 }
     67 
     68 static void
     69 released(void *data, struct wl_buffer *buffer)
     70 {
     71 	struct Canvas *c = data;
     72 	c->busy = 0;
     73 }
     74 
     75 static struct Canvas *
     76 canvas(struct Bar *b)
     77 {
     78 	static const struct wl_buffer_listener listener = { .release = released };
     79 	char path[] = "/tmp/dmw-bar-XXXXXX";
     80 	struct Canvas *c;
     81 	struct wl_shm_pool *pool;
     82 	int fd;
     83 
     84 	for (c = b->buffers; c; c = c->next)
     85 		if (!c->busy && c->width == b->width)
     86 			return c;
     87 	c = ecalloc(1, sizeof *c);
     88 	c->size = (size_t)b->width * 4 * b->height;
     89 	if ((fd = mkstemp(path)) < 0)
     90 		die("dmw: creating bar buffer:");
     91 	unlink(path);
     92 	if (ftruncate(fd, c->size) < 0 ||
     93 	    (c->addr = mmap(NULL, c->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
     94 		die("dmw: mapping bar:");
     95 	pool = wl_shm_create_pool(shm, fd, c->size);
     96 	close(fd);
     97 	c->width = b->width;
     98 	c->buffer = wl_shm_pool_create_buffer(pool, 0, b->width, b->height, b->width * 4, WL_SHM_FORMAT_XRGB8888);
     99 	wl_shm_pool_destroy(pool);
    100 	c->pixels = wld_import_buffer(wld_pixman_context, WLD_OBJECT_DATA, (union wld_object){ .ptr = c->addr },
    101 	                              b->width, b->height, WLD_FORMAT_XRGB8888, b->width * 4);
    102 	if (!c->pixels)
    103 		die("dmw: cannot import bar pixels");
    104 	wl_buffer_add_listener(c->buffer, &listener, c);
    105 	c->next = b->buffers;
    106 	return b->buffers = c;
    107 }
    108 
    109 static void
    110 freecanvas(struct Canvas *c)
    111 {
    112 	wl_buffer_destroy(c->buffer);
    113 	wld_buffer_unreference(c->pixels);
    114 	munmap(c->addr, c->size);
    115 	free(c);
    116 }
    117 
    118 static void
    119 paint(void *data)
    120 {
    121 	struct Bar *b = data;
    122 	struct Canvas *c, **p;
    123 
    124 	b->idle = NULL;
    125 	for (p = &b->buffers; (c = *p); ) {
    126 		if (c->width != b->width) {
    127 			*p = c->next;
    128 			freecanvas(c);
    129 		} else
    130 			p = &c->next;
    131 	}
    132 	if (b->shown != b->wanted) {
    133 		b->shown = b->wanted;
    134 		swc_panel_set_strut(b->panel, b->shown ? b->height : 0, 0, b->width);
    135 	}
    136 	if (b->shown) {
    137 		c = canvas(b);
    138 		if (!wld_set_target_buffer(renderer, c->pixels))
    139 			die("dmw: cannot draw bar");
    140 		bar_paint(b, renderer);
    141 		wld_flush(renderer);
    142 		wl_surface_attach(b->surface, c->buffer, 0, 0);
    143 		wl_surface_damage(b->surface, 0, 0, b->width, b->height);
    144 		c->busy = 1;
    145 	} else
    146 		wl_surface_attach(b->surface, NULL, 0, 0);
    147 	wl_surface_commit(b->surface);
    148 	flush();
    149 }
    150 
    151 void
    152 bar_update(struct Bar *b, int show)
    153 {
    154 	b->wanted = show;
    155 	if (client && b->width && !b->idle)
    156 		b->idle = wl_event_loop_add_idle(loop, paint, b);
    157 }
    158 
    159 static void
    160 docked(void *data, struct swc_panel *panel, uint32_t length)
    161 {
    162 	struct Bar *b = data;
    163 	b->width = length;
    164 	bar_update(b, b->wanted);
    165 }
    166 
    167 static void
    168 screenbound(void *data, struct wl_callback *sync, uint32_t serial)
    169 {
    170 	static const struct swc_panel_listener listener = { .docked = docked };
    171 	struct swc_screen *output = data;
    172 	struct wl_resource *res;
    173 	struct Bar *b;
    174 
    175 	wl_callback_destroy(sync);
    176 	/* the panel protocol omits output coordinates, so match screens by resource: */
    177 	res = wl_client_get_object(peer, wl_proxy_get_id((struct wl_proxy *)output));
    178 	b = res ? bar_for_screen(wl_resource_get_user_data(res)) : NULL;
    179 	if (!b) {
    180 		swc_screen_destroy(output);
    181 		return;
    182 	}
    183 	b->output = output;
    184 	b->height = barfont->height + 2;
    185 	b->surface = wl_compositor_create_surface(compositor);
    186 	wl_surface_set_user_data(b->surface, b);
    187 	b->panel = swc_panel_manager_create_panel(panels, b->surface);
    188 	swc_panel_add_listener(b->panel, &listener, b);
    189 	swc_panel_dock(b->panel, top ? SWC_PANEL_EDGE_TOP : SWC_PANEL_EDGE_BOTTOM, output, 0);
    190 }
    191 
    192 static void
    193 enter(void *data, struct wl_pointer *ptr, uint32_t serial, struct wl_surface *s, wl_fixed_t x, wl_fixed_t y)
    194 {
    195 	hover = s ? wl_surface_get_user_data(s) : NULL;
    196 	pointerx = wl_fixed_to_int(x);
    197 }
    198 
    199 static void
    200 leave(void *data, struct wl_pointer *ptr, uint32_t serial, struct wl_surface *s)
    201 {
    202 	hover = NULL;
    203 }
    204 
    205 static void
    206 motion(void *data, struct wl_pointer *ptr, uint32_t time, wl_fixed_t x, wl_fixed_t y)
    207 {
    208 	pointerx = wl_fixed_to_int(x);
    209 }
    210 
    211 static void
    212 button(void *data, struct wl_pointer *ptr, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
    213 {
    214 	if (hover && state == WL_POINTER_BUTTON_STATE_PRESSED)
    215 		bar_click(hover, pointerx, key);
    216 }
    217 
    218 static void
    219 axis(void *data, struct wl_pointer *ptr, uint32_t time, uint32_t axis, wl_fixed_t value)
    220 {
    221 }
    222 
    223 static void
    224 capabilities(void *data, struct wl_seat *s, uint32_t caps)
    225 {
    226 	static const struct wl_pointer_listener listener = {
    227 		.enter = enter, .leave = leave, .motion = motion, .button = button, .axis = axis,
    228 	};
    229 	if ((caps & WL_SEAT_CAPABILITY_POINTER) && !pointer) {
    230 		pointer = wl_seat_get_pointer(s);
    231 		wl_pointer_add_listener(pointer, &listener, NULL);
    232 	} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && pointer) {
    233 		wl_pointer_destroy(pointer);
    234 		pointer = NULL;
    235 		hover = NULL;
    236 	}
    237 }
    238 
    239 static void
    240 global(void *data, struct wl_registry *reg, uint32_t name, const char *iface, uint32_t version)
    241 {
    242 	static const struct wl_seat_listener seat_listener = { .capabilities = capabilities };
    243 	static const struct wl_callback_listener bound = { .done = screenbound };
    244 	struct swc_screen *output;
    245 
    246 	if (!strcmp(iface, "wl_shm"))
    247 		shm = wl_registry_bind(reg, name, &wl_shm_interface, 1);
    248 	else if (!strcmp(iface, "wl_compositor"))
    249 		compositor = wl_registry_bind(reg, name, &wl_compositor_interface, 1);
    250 	else if (!strcmp(iface, "swc_panel_manager"))
    251 		panels = wl_registry_bind(reg, name, &swc_panel_manager_interface, 1);
    252 	else if (!strcmp(iface, "swc_screen")) {
    253 		output = wl_registry_bind(reg, name, &swc_screen_interface, 1);
    254 		wl_callback_add_listener(wl_display_sync(client), &bound, output);
    255 	} else if (!strcmp(iface, "wl_seat")) {
    256 		seat = wl_registry_bind(reg, name, &wl_seat_interface, 1);
    257 		wl_seat_add_listener(seat, &seat_listener, NULL);
    258 	}
    259 }
    260 
    261 static void
    262 removed(void *data, struct wl_registry *reg, uint32_t name)
    263 {
    264 }
    265 
    266 void
    267 bar_init(struct wl_display *display, const char *font, int topbar)
    268 {
    269 	static const struct wl_registry_listener listener = { global, removed };
    270 	int fds[2];
    271 
    272 	top = topbar;
    273 	loop = wl_display_get_event_loop(display);
    274 	fonts = wld_font_create_context();
    275 	if (!fonts || !(barfont = wld_font_open_name(fonts, font)) ||
    276 	    !(renderer = wld_create_renderer(wld_pixman_context)))
    277 		die("dmw: cannot initialize bar font %s", font);
    278 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
    279 		die("dmw: creating bar connection:");
    280 	fcntl(fds[0], F_SETFD, FD_CLOEXEC);
    281 	fcntl(fds[1], F_SETFD, FD_CLOEXEC);
    282 	if (!(peer = wl_client_create(display, fds[0])) ||
    283 	    !(client = wl_display_connect_to_fd(fds[1])))
    284 		die("dmw: cannot connect bar");
    285 	source = wl_event_loop_add_fd(loop, fds[1], WL_EVENT_READABLE, dispatch, NULL);
    286 	if (!source)
    287 		die("dmw: cannot watch bar connection:");
    288 	registry = wl_display_get_registry(client);
    289 	wl_registry_add_listener(registry, &listener, NULL);
    290 	flush();
    291 }
    292 
    293 int
    294 textwidth(const char *s)
    295 {
    296 	struct wld_extents ext;
    297 	wld_font_text_extents(barfont, s, &ext);
    298 	return ext.advance;
    299 }
    300 
    301 void
    302 bar_close(struct Bar *b)
    303 {
    304 	struct Canvas *c;
    305 	if (hover == b)
    306 		hover = NULL;
    307 	if (b->idle)
    308 		wl_event_source_remove(b->idle);
    309 	while ((c = b->buffers)) {
    310 		b->buffers = c->next;
    311 		freecanvas(c);
    312 	}
    313 	if (b->panel) {
    314 		swc_panel_destroy(b->panel);
    315 		wl_surface_destroy(b->surface);
    316 		swc_screen_destroy(b->output);
    317 	}
    318 	b->panel = NULL;
    319 	b->width = 0;
    320 	b->idle = NULL;
    321 	if (client)
    322 		flush();
    323 }
    324 
    325 void
    326 bar_finish(void)
    327 {
    328 	if (pointer)
    329 		wl_pointer_destroy(pointer);
    330 	if (seat)
    331 		wl_seat_destroy(seat);
    332 	if (shm)
    333 		wl_shm_destroy(shm);
    334 	if (panels)
    335 		swc_panel_manager_destroy(panels);
    336 	if (compositor)
    337 		wl_compositor_destroy(compositor);
    338 	wl_registry_destroy(registry);
    339 	wl_event_source_remove(source);
    340 	wl_display_disconnect(client);
    341 	client = NULL;
    342 	wld_destroy_renderer(renderer);
    343 	wld_font_close(barfont);
    344 	wld_font_destroy_context(fonts);
    345 }