/*
 * /dev/rfkill: the kernel tells us every radio when it is opened, then
 * each change; writing an event blocks or unblocks one kind or all.
 * (The same file is in c8-shell and c8-settings: change both.)
 */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/rfkill.h>
#include <glib.h>
#include <glib-unix.h>
#include "rfkill.h"

#define MAX_RADIOS 32
#define MAX_LISTENERS 4

static struct {
	int fd;
	bool tried;
	struct radio {
		guint32 idx;
		guint8 type, soft, hard;
	} radios[MAX_RADIOS];
	int count;
	struct {
		rfkill_fn fn;
		void *data;
	} listeners[MAX_LISTENERS];
} rk = { .fd = -1 };

static void update(const struct rfkill_event *ev) {
	int at = -1;
	for (int i = 0; i < rk.count; i++) {
		if (rk.radios[i].idx == ev->idx) {
			at = i;
		}
	}
	if (ev->op == RFKILL_OP_DEL) {
		if (at >= 0) {
			rk.radios[at] = rk.radios[--rk.count];
		}
		return;
	}
	if (at < 0) {
		if (rk.count == MAX_RADIOS) {
			return;
		}
		at = rk.count++;
	}
	rk.radios[at] = (struct radio){ ev->idx, ev->type, ev->soft, ev->hard };
}

static gboolean readable(gint fd, GIOCondition cond, gpointer data) {
	struct rfkill_event ev;
	bool changed = false;
	ssize_t n;
	while ((n = read(fd, &ev, RFKILL_EVENT_SIZE_V1)) == RFKILL_EVENT_SIZE_V1) {
		update(&ev);
		changed = true;
	}
	if (n == 0 || (n < 0 && errno != EAGAIN && errno != EINTR)) {
		close(rk.fd);
		rk.fd = -1;
		return G_SOURCE_REMOVE;
	}
	for (int i = 0; changed && i < MAX_LISTENERS; i++) {
		if (rk.listeners[i].fn) {
			rk.listeners[i].fn(rk.listeners[i].data);
		}
	}
	return G_SOURCE_CONTINUE;
}

static void open_rfkill(void) {
	if (rk.tried) {
		return;
	}
	rk.tried = true;
	rk.fd = open("/dev/rfkill", O_RDWR | O_NONBLOCK | O_CLOEXEC);
	if (rk.fd < 0) {
		return; // no radios, or not ours to use
	}
	readable(rk.fd, G_IO_IN, NULL); // what there is now
	g_unix_fd_add(rk.fd, G_IO_IN, readable, NULL);
}

void rfkill_listen(rfkill_fn fn, void *data) {
	open_rfkill();
	for (int i = 0; i < MAX_LISTENERS; i++) {
		if (rk.listeners[i].fn == NULL) {
			rk.listeners[i].fn = fn;
			rk.listeners[i].data = data;
			return;
		}
	}
}

void rfkill_unlisten(rfkill_fn fn, void *data) {
	for (int i = 0; i < MAX_LISTENERS; i++) {
		if (rk.listeners[i].fn == fn && rk.listeners[i].data == data) {
			rk.listeners[i].fn = NULL;
		}
	}
}

bool rfkill_any(void) {
	open_rfkill();
	return rk.count > 0;
}

bool rfkill_airplane(void) {
	open_rfkill();
	for (int i = 0; i < rk.count; i++) {
		if (!rk.radios[i].soft && !rk.radios[i].hard) {
			return false;
		}
	}
	return rk.count > 0;
}

bool rfkill_switched_off(void) {
	open_rfkill();
	for (int i = 0; i < rk.count; i++) {
		if (rk.radios[i].hard) {
			return true;
		}
	}
	return false;
}

bool rfkill_blocked(int type) {
	open_rfkill();
	bool any = false;
	for (int i = 0; i < rk.count; i++) {
		if (rk.radios[i].type == type) {
			if (!rk.radios[i].soft && !rk.radios[i].hard) {
				return false;
			}
			any = true;
		}
	}
	return any;
}

static void change_all(int type, bool blocked) {
	open_rfkill();
	if (rk.fd < 0) {
		return;
	}
	struct rfkill_event ev;
	memset(&ev, 0, sizeof(ev));
	ev.op = RFKILL_OP_CHANGE_ALL;
	ev.type = type;
	ev.soft = blocked;
	if (write(rk.fd, &ev, RFKILL_EVENT_SIZE_V1) != RFKILL_EVENT_SIZE_V1) {
		g_warning("rfkill: %s", g_strerror(errno));
	}
}

void rfkill_set_airplane(bool on) {
	change_all(RFKILL_TYPE_ALL, on);
}

void rfkill_block(int type, bool blocked) {
	change_all(type, blocked);
}
