summaryrefslogtreecommitdiff
path: root/sys/src/cmd/cec/mux.c
blob: 66f4fa6df43d11c6b73bc043bf3475162dc45259 (plain)
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
#include <u.h>
#include <libc.h>
#include "cec.h"

typedef struct {
	char	type;
	char	pad[3];
	Pkt	p;
} Muxmsg;

typedef struct {
	int	fd;
	int	type;
	int	pid;
} Muxproc;

struct Mux {
	Muxmsg	m;
	Muxproc	p[2];
	int	pfd[2];
	int	inuse;
};

static Mux smux = {
.inuse	= -1,
};

void
muxcec(int, int cfd)
{
	Muxmsg m;
	int l;

	m.type = Fcec;
	while((l = netget(&m.p, sizeof m.p)) > 0)
		if(write(cfd, &m, l+4) != l+4)
			break;
	exits("");
}

void
muxkbd(int kfd, int cfd)
{
	Muxmsg m;

	m.type = Fkbd;
	while((m.p.len = read(kfd, m.p.data, sizeof m.p.data)) > 0)
		if(write(cfd, &m, m.p.len+22) != m.p.len+22)
			break;
	m.type = Ffatal;
	write(cfd, &m, 4);
	exits("");
}

int
muxproc(Mux *m, Muxproc *p, int fd, void (*f)(int, int), int type)
{
	memset(p, 0, sizeof p);
	p->type = -1;
	switch(p->pid = rfork(RFPROC|RFFDG)){
	case -1:
		return -1;
	case 0:
		close(m->pfd[0]);
		f(fd, m->pfd[1]);
	default:
		p->fd = fd;
		p->type = type;
		return p->pid;
	}
}

void
muxfree(Mux *m)
{
	close(m->pfd[0]);
	close(m->pfd[1]);
	postnote(PNPROC, m->p[0].pid, "this note goes to 11");
	postnote(PNPROC, m->p[1].pid, "this note goes to 11");
	waitpid();
	waitpid();
	memset(m, 0, sizeof *m);
	m->inuse = -1;
}

Mux*
mux(int fd[2])
{
	Mux *m;

	if(smux.inuse != -1)
		sysfatal("mux in use");
	m = &smux;
	m->inuse = 1;
	if(pipe(m->pfd) == -1)
		sysfatal("pipe: %r");
	muxproc(m, m->p+0, fd[0], muxkbd, Fkbd);
	muxproc(m, m->p+1, fd[1], muxcec, Fcec);
	close(m->pfd[1]);
	return m;
}

int
muxread(Mux *m, Pkt *p)
{
	if(read(m->pfd[0], &m->m, sizeof m->m) == -1)
		return -1;
	memcpy(p, &m->m.p, sizeof *p);
	return m->m.type;
}