summaryrefslogtreecommitdiff
path: root/sys/src/cmd/vnc/devcons.c
blob: 16804fce40f05582fb10c20323f2e17e40812e1d (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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include	<u.h>
#include	<libc.h>
#include	"compat.h"
#include	"kbd.h"
#include	"error.h"

typedef struct Queue	Queue;
struct Queue
{
	QLock	qwait;
	Rendez	rwait;

	Lock	lock;
	int	notempty;
	char	buf[1024];
	char	*w;
	char	*r;
	char	*e;
};

Queue*	kbdq;			/* unprocessed console input */
Queue*	lineq;			/* processed console input */
Snarf	snarf = {
	.vers =	1
};

static struct
{
	QLock;
	int	raw;		/* true if we shouldn't process input */
	int	ctl;		/* number of opens to the control file */
	int	x;		/* index into line */
	char	line[1024];	/* current input line */
} kbd;

/*
 * cheapo fixed-length queues
 */
static void
qwrite(Queue *q, void *v, int n)
{
	char *buf, *next;
	int i;

	buf = v;
	lock(&q->lock);
	for(i = 0; i < n; i++){
		next = q->w+1;
		if(next >= q->e)
			next = q->buf;
		if(next == q->r)
			break;
		*q->w = buf[i];
		q->w = next;
	}
	q->notempty = 1;
	unlock(&q->lock);
	rendwakeup(&q->rwait);
}

static int
qcanread(void *vq)
{
	Queue *q;
	int ne;

	q = vq;
	lock(&q->lock);
	ne = q->notempty;
	unlock(&q->lock);
	return ne;
}

static int
qread(Queue *q, void *v, int n)
{
	char *a;
	int nn, notempty;

	if(n == 0)
		return 0;
	a = v;
	nn = 0;
	for(;;){
		lock(&q->lock);

		while(nn < n && q->r != q->w){
			a[nn++] = *q->r++;
			if(q->r >= q->e)
				q->r = q->buf;
		}

		notempty = q->notempty;
		q->notempty = q->r != q->w;
		unlock(&q->lock);
		if(notempty)
			break;

		/*
		 * wait for something to show up in the kbd buffer.
		 */
		qlock(&q->qwait);
		if(waserror()){
			qunlock(&q->qwait);
			nexterror();
		}
		rendsleep(&q->rwait, qcanread, q);
		qunlock(&q->qwait);
		poperror();
	}
	return nn;
}

static Queue *
mkqueue(void)
{
	Queue *q;

	q = smalloc(sizeof(Queue));
	q->r = q->buf;
	q->w = q->r;
	q->e = &q->buf[sizeof q->buf];
	q->notempty = 0;
	return q;
}

static void
echoscreen(char *buf, int n)
{
	char *e, *p;
	char ebuf[128];
	int x;

	p = ebuf;
	e = ebuf + sizeof(ebuf) - 4;
	while(n-- > 0){
		if(p >= e){
			screenputs(ebuf, p - ebuf);
			p = ebuf;
		}
		x = *buf++;
		if(x == 0x15){
			*p++ = '^';
			*p++ = 'U';
			*p++ = '\n';
		} else
			*p++ = x;
	}
	if(p != ebuf)
		screenputs(ebuf, p - ebuf);
}

/*
 *  Put character, possibly a rune, into read queue at interrupt time.
 *  Called at interrupt time to process a character.
 */
void
kbdputc(int ch)
{
	int n;
	char buf[3];
	Rune r;

	r = ch;
	n = runetochar(buf, &r);
	qwrite(kbdq, buf, n);
	if(!kbd.raw)
		echoscreen(buf, n);
}

static void
kbdputcinit(void)
{
	kbdq = mkqueue();
	lineq = mkqueue();
	kbd.raw = 0;
	kbd.ctl = 0;
	kbd.x = 0;
}

enum{
	Qdir,
	Qcons,
	Qconsctl,
	Qsnarf,
	Qwinname,
};

static Dirtab consdir[]={
	".",		{Qdir, 0, QTDIR},	0,		DMDIR|0555,
	"cons",		{Qcons},	0,		0660,
	"consctl",	{Qconsctl},	0,		0220,
	"snarf",	{Qsnarf},	0,		0600,
	"winname",	{Qwinname},	0,		0000,
};

static void
consinit(void)
{
	kbdputcinit();
}

static Chan*
consattach(char *spec)
{
	return devattach('c', spec);
}

static Walkqid*
conswalk(Chan *c, Chan *nc, char **name, int nname)
{
	return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
}

static int
consstat(Chan *c, uchar *dp, int n)
{
	return devstat(c, dp, n, consdir, nelem(consdir), devgen);
}

static Chan*
consopen(Chan *c, int omode)
{
	c->aux = nil;
	c = devopen(c, omode, consdir, nelem(consdir), devgen);
	switch((ulong)c->qid.path){
	case Qconsctl:
		qlock(&kbd);
		kbd.ctl++;
		qunlock(&kbd);
		break;
	case Qsnarf:
		if((c->mode&3) == OWRITE || (c->mode&3) == ORDWR)
			c->aux = smalloc(sizeof(Snarf));
		break;
	}
	return c;
}

void
setsnarf(char *buf, int n, int *vers)
{
	int i;

	qlock(&snarf);
	snarf.vers++;
	if(vers)
		*vers = snarf.vers;	
	for(i = 0; i < nelem(consdir); i++){
		if(consdir[i].qid.type == Qsnarf){
			consdir[i].qid.vers = snarf.vers;
			break;
		}
	}
	free(snarf.buf);
	snarf.n = n;
	snarf.buf = buf;
	qunlock(&snarf);
}

static void
consclose(Chan *c)
{
	Snarf *t;

	switch((ulong)c->qid.path){
	/* last close of control file turns off raw */
	case Qconsctl:
		if(c->flag&COPEN){
			qlock(&kbd);
			if(--kbd.ctl == 0)
				kbd.raw = 0;
			qunlock(&kbd);
		}
		break;
	/* odd behavior but really ok: replace snarf buffer when /dev/snarf is closed */
	case Qsnarf:
		t = c->aux;
		if(t == nil)
			break;
		setsnarf(t->buf, t->n, 0);
		t->buf = nil;	/* setsnarf took it */
		free(t);
		c->aux = nil;
		break;
	}
}

static long
consread(Chan *c, void *buf, long n, vlong off)
{
	char ch;
	int	send;

	if(n <= 0)
		return n;
	switch((ulong)c->qid.path){
	case Qsnarf:
		qlock(&snarf);
		if(off < snarf.n){
			if(off + n > snarf.n)
				n = snarf.n - off;
			memmove(buf, snarf.buf+off, n);
		}else
			n = 0;
		qunlock(&snarf);
		return n;

	case Qdir:
		return devdirread(c, buf, n, consdir, nelem(consdir), devgen);

	case Qcons:
		qlock(&kbd);
		if(waserror()){
			qunlock(&kbd);
			nexterror();
		}
		while(!qcanread(lineq)){
			qread(kbdq, &ch, 1);
			send = 0;
			if(ch == 0){
				/* flush output on rawoff -> rawon */
				if(kbd.x > 0)
					send = !qcanread(kbdq);
			}else if(kbd.raw){
				kbd.line[kbd.x++] = ch;
				send = !qcanread(kbdq);
			}else{
				switch(ch){
				case '\b':
					if(kbd.x > 0)
						kbd.x--;
					break;
				case 0x15:	/* ^U */
					kbd.x = 0;
					break;
				case '\n':
				case 0x04:	/* ^D */
					send = 1;
				default:
					if(ch != 0x04)
						kbd.line[kbd.x++] = ch;
					break;
				}
			}
			if(send || kbd.x == sizeof kbd.line){
				qwrite(lineq, kbd.line, kbd.x);
				kbd.x = 0;
			}
		}
		n = qread(lineq, buf, n);
		qunlock(&kbd);
		poperror();
		return n;

	default:
		print("consread 0x%llux\n", c->qid.path);
		error(Egreg);
	}
	return -1;		/* never reached */
}

static long
conswrite(Chan *c, void *va, long n, vlong)
{
	Snarf *t;
	char buf[256], *a;
	char ch;

	switch((ulong)c->qid.path){
	case Qcons:
		screenputs(va, n);
		break;

	case Qconsctl:
		if(n >= sizeof(buf))
			n = sizeof(buf)-1;
		strncpy(buf, va, n);
		buf[n] = 0;
		for(a = buf; a;){
			if(strncmp(a, "rawon", 5) == 0){
				kbd.raw = 1;
				/* clumsy hack - wake up reader */
				ch = 0;
				qwrite(kbdq, &ch, 1);			
			} else if(strncmp(a, "rawoff", 6) == 0){
				kbd.raw = 0;
			}
			if(a = strchr(a, ' '))
				a++;
		}
		break;

	case Qsnarf:
		t = c->aux;
		/* always append only */
		if(t->n > MAXSNARF)	/* avoid thrashing when people cut huge text */
			error("snarf buffer too big");
		a = realloc(t->buf, t->n + n + 1);
		if(a == nil)
			error("snarf buffer too big");
		t->buf = a;
		memmove(t->buf+t->n, va, n);
		t->n += n;
		t->buf[t->n] = '\0';
		break;
	default:
		print("conswrite: 0x%llux\n", c->qid.path);
		error(Egreg);
	}
	return n;
}

Dev consdevtab = {
	'c',
	"cons",

	devreset,
	consinit,
	consattach,
	conswalk,
	consstat,
	consopen,
	devcreate,
	consclose,
	consread,
	devbread,
	conswrite,
	devbwrite,
	devremove,
	devwstat,
};