| 
									
										
										
										
											2017-03-10 23:41:24 -05:00
										 |  |  | #define _XOPEN_SOURCE 500
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | #include <assert.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <poll.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2016-02-22 17:27:17 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | #include <sys/wait.h>
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <wayland-client.h>
 | 
					
						
							|  |  |  | #include <wlr/util/log.h>
 | 
					
						
							| 
									
										
										
										
											2016-09-01 08:18:37 -04:00
										 |  |  | #include "swaybar/render.h"
 | 
					
						
							|  |  |  | #include "swaybar/config.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-07 16:45:28 -07:00
										 |  |  | #include "swaybar/event_loop.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-01 08:18:37 -04:00
										 |  |  | #include "swaybar/bar.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:56:02 -04:00
										 |  |  | #include "swaybar/ipc.h"
 | 
					
						
							|  |  |  | #include "ipc-client.h"
 | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | #include "list.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | #include "pango.h"
 | 
					
						
							|  |  |  | #include "pool-buffer.h"
 | 
					
						
							|  |  |  | #include "wlr-layer-shell-unstable-v1-client-protocol.h"
 | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void bar_init(struct swaybar *bar) { | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 	bar->config = init_config(); | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | 	wl_list_init(&bar->outputs); | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | struct swaybar_output *new_output(const char *name) { | 
					
						
							|  |  |  | 	struct swaybar_output *output = malloc(sizeof(struct swaybar_output)); | 
					
						
							| 
									
										
										
										
											2016-02-22 17:27:17 +01:00
										 |  |  | 	output->name = strdup(name); | 
					
						
							|  |  |  | 	return output; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void layer_surface_configure(void *data, | 
					
						
							|  |  |  | 		struct zwlr_layer_surface_v1 *surface, | 
					
						
							|  |  |  | 		uint32_t serial, uint32_t width, uint32_t height) { | 
					
						
							|  |  |  | 	struct swaybar_output *output = data; | 
					
						
							|  |  |  | 	output->width = width; | 
					
						
							|  |  |  | 	output->height = height; | 
					
						
							|  |  |  | 	zwlr_layer_surface_v1_ack_configure(surface, serial); | 
					
						
							|  |  |  | 	render_frame(output->bar, output); | 
					
						
							| 
									
										
										
										
											2016-07-10 22:23:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void layer_surface_closed(void *_output, | 
					
						
							|  |  |  | 		struct zwlr_layer_surface_v1 *surface) { | 
					
						
							|  |  |  | 	// TODO: Deal with hotplugging
 | 
					
						
							|  |  |  | 	struct swaybar_output *output = output; | 
					
						
							|  |  |  | 	zwlr_layer_surface_v1_destroy(output->layer_surface); | 
					
						
							|  |  |  | 	wl_surface_destroy(output->surface); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-23 15:22:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | struct zwlr_layer_surface_v1_listener layer_surface_listener = { | 
					
						
							|  |  |  | 	.configure = layer_surface_configure, | 
					
						
							|  |  |  | 	.closed = layer_surface_closed, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void handle_global(void *data, struct wl_registry *registry, | 
					
						
							|  |  |  | 		uint32_t name, const char *interface, uint32_t version) { | 
					
						
							|  |  |  | 	struct swaybar *bar = data; | 
					
						
							|  |  |  | 	if (strcmp(interface, wl_compositor_interface.name) == 0) { | 
					
						
							|  |  |  | 		bar->compositor = wl_registry_bind(registry, name, | 
					
						
							|  |  |  | 				&wl_compositor_interface, 1); | 
					
						
							|  |  |  | 	} else if (strcmp(interface, wl_shm_interface.name) == 0) { | 
					
						
							|  |  |  | 		bar->shm = wl_registry_bind(registry, name, | 
					
						
							|  |  |  | 				&wl_shm_interface, 1); | 
					
						
							|  |  |  | 	} else if (strcmp(interface, wl_output_interface.name) == 0) { | 
					
						
							| 
									
										
										
										
											2018-03-29 00:07:35 -04:00
										 |  |  | 		static size_t index = 0; | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | 		struct swaybar_output *output = | 
					
						
							|  |  |  | 			calloc(1, sizeof(struct swaybar_output)); | 
					
						
							|  |  |  | 		output->bar = bar; | 
					
						
							|  |  |  | 		output->output = wl_registry_bind(registry, name, | 
					
						
							|  |  |  | 				&wl_output_interface, 1); | 
					
						
							| 
									
										
										
										
											2018-03-29 00:07:35 -04:00
										 |  |  | 		output->index = index++; | 
					
						
							| 
									
										
										
										
											2018-03-29 00:21:05 -04:00
										 |  |  | 		wl_list_init(&output->workspaces); | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | 		wl_list_insert(&bar->outputs, &output->link); | 
					
						
							|  |  |  | 	} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { | 
					
						
							|  |  |  | 		bar->layer_shell = wl_registry_bind( | 
					
						
							|  |  |  | 				registry, name, &zwlr_layer_shell_v1_interface, 1); | 
					
						
							| 
									
										
										
										
											2016-07-17 11:26:38 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-17 11:26:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void handle_global_remove(void *data, struct wl_registry *registry, | 
					
						
							|  |  |  | 		uint32_t name) { | 
					
						
							|  |  |  | 	// who cares
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:14:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static const struct wl_registry_listener registry_listener = { | 
					
						
							|  |  |  | 	.global = handle_global, | 
					
						
							|  |  |  | 	.global_remove = handle_global_remove, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | void bar_setup(struct swaybar *bar, | 
					
						
							|  |  |  | 		const char *socket_path, const char *bar_id) { | 
					
						
							|  |  |  | 	bar_init(bar); | 
					
						
							| 
									
										
										
										
											2017-06-07 16:45:28 -07:00
										 |  |  | 	init_event_loop(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:56:02 -04:00
										 |  |  | 	bar->ipc_socketfd = ipc_open_socket(socket_path); | 
					
						
							|  |  |  | 	bar->ipc_event_socketfd = ipc_open_socket(socket_path); | 
					
						
							| 
									
										
										
										
											2018-03-29 00:21:05 -04:00
										 |  |  | 	ipc_initialize(bar, bar_id); | 
					
						
							| 
									
										
										
										
											2018-03-28 23:56:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | 	assert(bar->display = wl_display_connect(NULL)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct wl_registry *registry = wl_display_get_registry(bar->display); | 
					
						
							|  |  |  | 	wl_registry_add_listener(registry, ®istry_listener, bar); | 
					
						
							|  |  |  | 	wl_display_roundtrip(bar->display); | 
					
						
							|  |  |  | 	assert(bar->compositor && bar->layer_shell && bar->shm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TODO: we might not necessarily be meant to do all of the outputs
 | 
					
						
							|  |  |  | 	struct swaybar_output *output; | 
					
						
							|  |  |  | 	wl_list_for_each(output, &bar->outputs, link) { | 
					
						
							| 
									
										
										
										
											2018-03-29 00:07:35 -04:00
										 |  |  | 		struct config_output *coutput; | 
					
						
							|  |  |  | 		wl_list_for_each(coutput, &bar->config->outputs, link) { | 
					
						
							|  |  |  | 			if (coutput->index != output->index) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-03-29 00:21:05 -04:00
										 |  |  | 			output->name = strdup(coutput->name); | 
					
						
							| 
									
										
										
										
											2018-03-29 00:07:35 -04:00
										 |  |  | 			assert(output->surface = wl_compositor_create_surface( | 
					
						
							|  |  |  | 					bar->compositor)); | 
					
						
							|  |  |  | 			output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( | 
					
						
							|  |  |  | 					bar->layer_shell, output->surface, output->output, | 
					
						
							|  |  |  | 					ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); | 
					
						
							|  |  |  | 			assert(output->layer_surface); | 
					
						
							|  |  |  | 			zwlr_layer_surface_v1_add_listener(output->layer_surface, | 
					
						
							|  |  |  | 					&layer_surface_listener, output); | 
					
						
							|  |  |  | 			zwlr_layer_surface_v1_set_anchor(output->layer_surface, | 
					
						
							|  |  |  | 					bar->config->position); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-22 17:27:17 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-29 00:21:05 -04:00
										 |  |  | 	ipc_get_workspaces(bar); | 
					
						
							|  |  |  | 	wl_list_for_each(output, &bar->outputs, link) { | 
					
						
							|  |  |  | 		render_frame(bar, output); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void display_in(int fd, short mask, void *_bar) { | 
					
						
							|  |  |  | 	struct swaybar *bar = (struct swaybar *)_bar; | 
					
						
							|  |  |  | 	if (wl_display_dispatch(bar->display) == -1) { | 
					
						
							|  |  |  | 		wlr_log(L_ERROR, "failed to dispatch wl: %d", errno); | 
					
						
							| 
									
										
										
										
											2017-06-07 16:45:28 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:56:02 -04:00
										 |  |  | static void ipc_in(int fd, short mask, void *_bar) { | 
					
						
							|  |  |  | 	struct swaybar *bar = (struct swaybar *)_bar; | 
					
						
							|  |  |  | 	handle_ipc_event(bar); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | void bar_run(struct swaybar *bar) { | 
					
						
							|  |  |  | 	add_event(wl_display_get_fd(bar->display), POLLIN, display_in, bar); | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 	while (1) { | 
					
						
							| 
									
										
										
										
											2017-06-07 16:45:28 -07:00
										 |  |  | 		event_loop_poll(); | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | static void free_outputs(struct wl_list *list) { | 
					
						
							|  |  |  | 	struct swaybar_output *output, *tmp; | 
					
						
							|  |  |  | 	wl_list_for_each_safe(output, tmp, list, link) { | 
					
						
							|  |  |  | 		wl_list_remove(&output->link); | 
					
						
							|  |  |  | 		free(output->name); | 
					
						
							|  |  |  | 		free(output); | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-28 23:04:20 -04:00
										 |  |  | void bar_teardown(struct swaybar *bar) { | 
					
						
							|  |  |  | 	free_outputs(&bar->outputs); | 
					
						
							| 
									
										
										
										
											2016-03-31 20:50:07 +11:00
										 |  |  | 	if (bar->config) { | 
					
						
							|  |  |  | 		free_config(bar->config); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-24 02:34:20 +01:00
										 |  |  | } |