summaryrefslogtreecommitdiff
path: root/sys/src/cmd/vnc/devcons.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@rei2.9hal>2012-02-25 23:47:09 +0100
committercinap_lenrek <cinap_lenrek@rei2.9hal>2012-02-25 23:47:09 +0100
commitce6b942ff12cbb7fe7da634004b8f7eed76eee3d (patch)
treeba230e02aae16a07ce7fa8d8b066876ec742d902 /sys/src/cmd/vnc/devcons.c
parentf2b6ec3140935d88d065359619c04992939a1ddc (diff)
vncs: /dev/kbd support by running kbdfs and forwarding keycodes thru kbdin
Diffstat (limited to 'sys/src/cmd/vnc/devcons.c')
-rw-r--r--sys/src/cmd/vnc/devcons.c254
1 files changed, 5 insertions, 249 deletions
diff --git a/sys/src/cmd/vnc/devcons.c b/sys/src/cmd/vnc/devcons.c
index 16804fce4..b328bdaaf 100644
--- a/sys/src/cmd/vnc/devcons.c
+++ b/sys/src/cmd/vnc/devcons.c
@@ -4,180 +4,10 @@
#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,
@@ -194,12 +24,6 @@ static Dirtab consdir[]={
"winname", {Qwinname}, 0, 0000,
};
-static void
-consinit(void)
-{
- kbdputcinit();
-}
-
static Chan*
consattach(char *spec)
{
@@ -225,9 +49,6 @@ consopen(Chan *c, int omode)
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)
@@ -266,12 +87,6 @@ consclose(Chan *c)
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:
@@ -289,9 +104,6 @@ consclose(Chan *c)
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){
@@ -310,48 +122,8 @@ consread(Chan *c, void *buf, long n, vlong off)
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;
+ error(Egreg);
+ return -1;
default:
print("consread 0x%llux\n", c->qid.path);
@@ -364,8 +136,7 @@ static long
conswrite(Chan *c, void *va, long n, vlong)
{
Snarf *t;
- char buf[256], *a;
- char ch;
+ char *a;
switch((ulong)c->qid.path){
case Qcons:
@@ -373,22 +144,7 @@ conswrite(Chan *c, void *va, long n, vlong)
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++;
- }
+ error(Egreg);
break;
case Qsnarf:
@@ -416,7 +172,7 @@ Dev consdevtab = {
"cons",
devreset,
- consinit,
+ devinit,
consattach,
conswalk,
consstat,