dwlb

feature-complete bar for dwl
Log | Files | Refs | README | LICENSE

snhost.c (8852B) - View raw


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "snhost.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <gtk/gtk.h>

#include "snwatcher.h"
#include "snitem.h"


struct _SnHost
{
	GtkWidget parent_instance;

	GtkWidget* box;
	SnWatcher* watcher;
	GHashTable* snitems;
	char* mon;

	unsigned long reg_sub_id;
	unsigned long unreg_sub_id;

	int defaultwidth;
	int defaultheight;
	int iconsize;
	int margins;
	int spacing;

	int nitems;
	int curwidth;
	gboolean exiting;
};

G_DEFINE_FINAL_TYPE(SnHost, sn_host, GTK_TYPE_WINDOW)

enum
{
	PROP_MON = 1,
	PROP_DEFAULTWIDTH,
	PROP_DEFAULTHEIGHT,
	PROP_ICONSIZE,
	PROP_MARGINS,
	PROP_SPACING,
	N_PROPERTIES
};

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };


static void	sn_host_constructed	(GObject *obj);
static void	sn_host_dispose		(GObject *obj);
static void	sn_host_finalize	(GObject *obj);


static void
dwlb_request_resize(SnHost *self)
{
	if (self->exiting) {
		// Restore original size on exit.
		self->curwidth = 0;
	} else if (self->nitems <= 1) {
		// Width of 1 icon even when there are none.
		self->curwidth = self->iconsize + 2 * self->margins;
	} else {
		self->curwidth =
			// Icons themselves.
			self->nitems * self->iconsize +

			// Spacing between icons.
			(self->nitems - 1) * self->spacing +

			// Margins before first icon and after last icon.
			2 * self->margins;
	}

	struct sockaddr_un sockaddr;

	sockaddr.sun_family = AF_UNIX;

	snprintf(sockaddr.sun_path,
	         sizeof(sockaddr.sun_path),
	         "%s/dwlb/dwlb-0",
	         g_get_user_runtime_dir());

	char sockbuf[64] = {0};
	snprintf(sockbuf, sizeof(sockbuf), "%s %s %i", self->mon, "resize", self->curwidth);
	size_t len = strlen(sockbuf);
	int sock_fd = socket(AF_UNIX, SOCK_STREAM, 1);

	int connstatus =
		connect(sock_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));

	if (connstatus != 0)
		g_error("Error connecting to dwlb socket");

	size_t sendstatus =
		send(sock_fd, sockbuf, len, 0);

	if (sendstatus == (size_t)-1)
		g_error("Could not send size update to %s", sockaddr.sun_path);

	close(sock_fd);
}

static void
sn_host_register_item(SnWatcher *watcher,
                      const char *busname,
                      const char *busobj,
                      SnHost *self)
{
	g_debug("Adding %s to snhost %s", busname, self->mon);

	SnItem *snitem = sn_item_new(busname, busobj, self->iconsize);
	gtk_box_append(GTK_BOX(self->box), GTK_WIDGET(snitem));
	g_hash_table_insert(self->snitems, g_strdup(busname), snitem);

	self->nitems = self->nitems + 1;
	dwlb_request_resize(self);
}

static void
sn_host_unregister_item(SnWatcher *watcher, const char *busname, SnHost *self)
{
	g_debug("Removing %s from snhost %s", busname, self->mon);

	GtkBox *box = GTK_BOX(self->box);
	void *match = g_hash_table_lookup(self->snitems, busname);
	GtkWidget *snitem = GTK_WIDGET(match);

	gtk_box_remove(box, snitem);
	g_hash_table_remove(self->snitems, busname);

	self->nitems = self->nitems - 1;
	dwlb_request_resize(self);
}

static void
sn_host_set_property(GObject *object,
                     unsigned int property_id,
                     const GValue *value,
                     GParamSpec *pspec)
{
	SnHost *self = SN_HOST(object);

	switch (property_id) {
		case PROP_MON:
			self->mon = g_value_dup_string(value);
			break;
		case PROP_DEFAULTWIDTH:
			self->defaultwidth = g_value_get_int(value);
			break;
		case PROP_DEFAULTHEIGHT:
			self->defaultheight = g_value_get_int(value);
			break;
		case PROP_ICONSIZE:
			self->iconsize = g_value_get_int(value);
			break;
		case PROP_MARGINS:
			self->margins = g_value_get_int(value);
			break;
		case PROP_SPACING:
			self->spacing = g_value_get_int(value);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
			break;
	}
}

static void
sn_host_class_init(SnHostClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->set_property = sn_host_set_property;
	object_class->constructed = sn_host_constructed;
	object_class->dispose = sn_host_dispose;
	object_class->finalize = sn_host_finalize;

	obj_properties[PROP_MON] =
		g_param_spec_string("mon", NULL, NULL,
		                 NULL,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_DEFAULTWIDTH] =
		g_param_spec_int("defaultwidth", NULL, NULL,
		                 INT_MIN,
		                 INT_MAX,
		                 22,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_DEFAULTHEIGHT] =
		g_param_spec_int("defaultheight", NULL, NULL,
		                 INT_MIN,
		                 INT_MAX,
		                 22,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_ICONSIZE] =
		g_param_spec_int("iconsize", NULL, NULL,
		                 INT_MIN,
		                 INT_MAX,
		                 22,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_MARGINS] =
		g_param_spec_int("margins", NULL, NULL,
		                 INT_MIN,
		                 INT_MAX,
		                 4,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_SPACING] =
		g_param_spec_int("spacing", NULL, NULL,
		                 INT_MIN,
		                 INT_MAX,
		                 4,
		                 G_PARAM_CONSTRUCT_ONLY |
		                 G_PARAM_WRITABLE |
		                 G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
}

static void
sn_host_init(SnHost *self)
{
	self->snitems = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	self->exiting = FALSE;
	self->nitems = 0;

	self->watcher = sn_watcher_new();

	self->reg_sub_id = g_signal_connect(self->watcher,
	                                    "trayitem-registered",
	                                    G_CALLBACK(sn_host_register_item),
	                                    self);

	self->unreg_sub_id = g_signal_connect(self->watcher,
	                                      "trayitem-unregistered",
	                                      G_CALLBACK(sn_host_unregister_item),
	                                      self);
}

static void
sn_host_constructed(GObject *obj)
{
	SnHost *self = SN_HOST(obj);

	GtkWindow *window = GTK_WINDOW(self);
	gtk_window_set_decorated(window, FALSE);
	gtk_window_set_default_size(window, self->defaultwidth, self->defaultheight);

	self->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, self->spacing);
	gtk_box_set_homogeneous(GTK_BOX(self->box), TRUE);
	gtk_box_set_spacing(GTK_BOX(self->box), self->margins);

	GtkWidget *widget = GTK_WIDGET(self->box);
	gtk_widget_set_vexpand(widget, TRUE);
	gtk_widget_set_hexpand(widget, TRUE);
	gtk_widget_set_margin_start(widget, self->margins);
	gtk_widget_set_margin_end(widget, self->margins);
	gtk_widget_set_margin_top(widget, self->margins);
	gtk_widget_set_margin_bottom(widget, self->margins);

	gtk_window_set_child(GTK_WINDOW(self), self->box);

	dwlb_request_resize(self);

	g_debug("Created snhost for monitor %s", self->mon);

	G_OBJECT_CLASS(sn_host_parent_class)->constructed(obj);
}

static void
sn_host_dispose(GObject *obj)
{
	SnHost *self = SN_HOST(obj);

	g_debug("Disposing snhost of monitor %s", self->mon);
	self->exiting = TRUE;

	if (self->reg_sub_id > 0) {
		g_signal_handler_disconnect(self->watcher, self->reg_sub_id);
		self->reg_sub_id = 0;
	}

	if (self->unreg_sub_id > 0) {
		g_signal_handler_disconnect(self->watcher, self->unreg_sub_id);
		self->reg_sub_id = 0;
	}

	if (self->watcher) {
		g_object_unref(self->watcher);
		self->watcher = NULL;
	}

	dwlb_request_resize(self);

	G_OBJECT_CLASS(sn_host_parent_class)->dispose(obj);
}

static void
sn_host_finalize(GObject *obj)
{
	SnHost *self = SN_HOST(obj);

	g_hash_table_destroy(self->snitems);
	g_free(self->mon);

	G_OBJECT_CLASS(sn_host_parent_class)->finalize(obj);
}

SnHost*
sn_host_new(int defaultwidth,
            int defaultheight,
            int iconsize,
            int margins,
            int spacing,
            const char *conn)
{
	return g_object_new(SN_TYPE_HOST,
	                    "defaultwidth", defaultwidth,
	                    "defaultheight", defaultheight,
	                    "iconsize", iconsize,
	                    "margins", margins,
	                    "spacing", spacing,
	                    "mon", conn,
	                    NULL);
}