/*
 * The glasses' own 3D mode: one 3840x1080 picture, an eye in each half,
 * which Monado needs for two eyes (xrruntime.c). Asked for over their
 * control port, TCP 169.254.2.1:52999 on the same link as the IMU's
 * stream: a frame of message id (2 bytes), length (4) and transaction
 * id (4, top bit set), all big-endian, then the body. Set-input-mode is
 * id 10274 with 1a 02 08 01 for 3D and 08 00 for 2D. That is what
 * kevindurb/xreal-linux sends an XREAL 1S; the 1 Pro+ is the same
 * family (the same link and IMU port), but this was not tried on one:
 * off unless [glasses] sbs_switch = true.
 *
 * Only the mode we switched from is switched back to: going to 3D
 * notes "was-2d" in ~/.local/state/cont8nuum/glasses-mode, and going
 * back to 2D happens only with that note. Sent from a child, so a link
 * that doesn't answer never holds up the compositor.
 */
#define _GNU_SOURCE // SO_BINDTODEVICE
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wlr/util/log.h>
#include "imu.h"
#include "server.h"

#define CONTROL_HOST "169.254.2.1"
#define CONTROL_PORT 52999
#define SET_INPUT_MODE 10274

static bool send_mode(bool sbs) {
	char dev[IF_NAMESIZE] = "";
	if (!c8_imu_glasses_link(dev)) {
		return false;
	}
	int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
	if (fd < 0) {
		return false;
	}
	struct timeval tv = { 2, 0 };
	setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
	setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
	/* Tailscale may route 169.254/16 elsewhere: imu.c says why */
	setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev));
	struct sockaddr_in sa = { .sin_family = AF_INET,
		.sin_port = htons(CONTROL_PORT) };
	inet_pton(AF_INET, CONTROL_HOST, &sa.sin_addr);
	uint8_t msg[] = {
		SET_INPUT_MODE >> 8, SET_INPUT_MODE & 0xff,
		0, 0, 0, 8, // the transaction id and the body
		0x80, 0, 0, 1,
		0x1a, 0x02, 0x08, sbs ? 1 : 0,
	};
	bool ok = connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0 &&
		write(fd, msg, sizeof(msg)) == (ssize_t)sizeof(msg);
	uint8_t reply[64];
	if (ok) {
		ok = read(fd, reply, sizeof(reply)) >= 6; // its id and length
	}
	close(fd);
	return ok;
}

void c8_glasses_set_sbs(struct c8_server *server, bool sbs, bool was_2d) {
	if (!server->config.glasses.sbs_switch) {
		return;
	}
	char note[4096];
	if (!c8_state_path("glasses-mode", note, sizeof(note), true)) {
		return;
	}
	if (!sbs && access(note, F_OK) != 0) {
		return; // they were in 3D before us: left so
	}
	if (sbs && !was_2d) {
		return;
	}
	wlr_log(WLR_INFO, "Glasses: asking for their %s mode", sbs ? "3D" : "2D");
	pid_t pid = fork();
	if (pid == 0) {
		if (fork() == 0) {
			bool ok = send_mode(sbs);
			if (ok && sbs) {
				FILE *f = fopen(note, "w");
				if (f) {
					fputs("was-2d\n", f);
					fclose(f);
				}
			} else if (ok) {
				unlink(note);
			}
			_exit(ok ? 0 : 1);
		}
		_exit(0);
	} else if (pid > 0) {
		waitpid(pid, NULL, 0);
	}
}
