int snarfswap(char *fromsam, int nc, char **tosam) { char *s; s = getsnarf(); putsnarf(fromsam); *tosam = s; return s ? strlen(s) : 0; }
void acmegetsnarf(void) { char *s; int nb, nr, nulls, len; Rune *r; s = getsnarf(); if(s == nil || s[0]==0){ free(s); return; } len = strlen(s); r = runemalloc(len+1); cvttorunes(s, len, r, &nb, &nr, &nulls); bufreset(&snarfbuf); bufinsert(&snarfbuf, 0, r, nr); free(r); free(s); }
void checksnarf(Vnc *v) { Dir *dir; char *snarf; int len; if(snarffd < 0) { snarffd = open("/dev/snarf", OREAD); if(snarffd < 0) sysfatal("can't open /dev/snarf: %r"); } for(;;) { sleep(1000); dir = dirstat("/dev/snarf"); if(dir == nil) /* this happens under old drawterm */ continue; if(dir->qid.vers > snarfvers) { snarf = getsnarf(&len); vnclock(v); vncwrchar(v, MCCut); vncwrbytes(v, "pad", 3); vncwrlong(v, len); vncwrbytes(v, snarf, len); vncflush(v); vncunlock(v); free(snarf); snarfvers = dir->qid.vers; } free(dir); } }
/* * Handle a single wsysmsg. * Might queue for later (kbd, mouse read) */ void runmsg(Wsysmsg *m) { static uchar buf[65536]; int n; Memimage *i; switch(m->type){ case Tinit: memimageinit(); i = attachscreen(m->label, m->winsize); _initdisplaymemimage(i); replymsg(m); break; case Trdmouse: zlock(); mousetags.t[mousetags.wi++] = m->tag; if(mousetags.wi == nelem(mousetags.t)) mousetags.wi = 0; if(mousetags.wi == mousetags.ri) sysfatal("too many queued mouse reads"); mouse.stall = 0; matchmouse(); zunlock(); break; case Trdkbd: zlock(); kbdtags.t[kbdtags.wi++] = m->tag; if(kbdtags.wi == nelem(kbdtags.t)) kbdtags.wi = 0; if(kbdtags.wi == kbdtags.ri) sysfatal("too many queued keyboard reads"); kbd.stall = 0; matchkbd(); zunlock(); break; case Tmoveto: setmouse(m->mouse.xy); replymsg(m); break; case Tcursor: if(m->arrowcursor) setcursor(nil); else setcursor(&m->cursor); replymsg(m); break; case Tbouncemouse: // _xbouncemouse(&m->mouse); replymsg(m); break; case Tlabel: kicklabel(m->label); replymsg(m); break; case Trdsnarf: m->snarf = getsnarf(); replymsg(m); free(m->snarf); break; case Twrsnarf: putsnarf(m->snarf); replymsg(m); break; case Trddraw: n = m->count; if(n > sizeof buf) n = sizeof buf; n = _drawmsgread(buf, n); if(n < 0) replyerror(m); else{ m->count = n; m->data = buf; replymsg(m); } break; case Twrdraw: if(_drawmsgwrite(m->data, m->count) < 0) replyerror(m); else replymsg(m); break; case Ttop: // _xtopwindow(); replymsg(m); break; case Tresize: // _xresizewindow(m->rect); replymsg(m); break; } }
static long mouseread(Chan *c, void *va, long n, vlong off) { char buf[1+4*12+1], *s; uchar *p; ulong offset = off; Mousestate m; int b; p = va; switch((ulong)c->qid.path){ case Qdir: return devdirread(c, va, n, mousedir, nelem(mousedir), mousedevgen); case Qcursor: if(offset != 0) return 0; if(n < 2*4+2*2*16) error(Eshort); n = 2*4+2*2*16; lock(&cursor.lk); BPLONG(p+0, curs.offset.x); BPLONG(p+4, curs.offset.y); memmove(p+8, curs.clr, 2*16); memmove(p+40, curs.set, 2*16); unlock(&cursor.lk); return n; case Qmouse: while(mousechanged(0) == 0) sleep(&mouse.r, mousechanged, 0); mouse.qfull = 0; mousetime = seconds(); /* * No lock of the indices is necessary here, because ri is only * updated by us, and there is only one mouse reader * at a time. I suppose that more than one process * could try to read the fd at one time, but such behavior * is degenerate and already violates the calling * conventions for sleep above. */ if(mouse.ri != mouse.wi) { m = mouse.queue[mouse.ri]; if(++mouse.ri == nelem(mouse.queue)) mouse.ri = 0; } else { while(!canlock(&cursor.lk)) tsleep(&up->sleep, return0, 0, TK2MS(1)); m = mouse.mstate; unlock(&cursor.lk); } b = buttonmap[m.buttons&7]; /* put buttons 4 and 5 back in */ b |= m.buttons & (3<<3); if (scrollswap){ if (b == 8) b = 16; else if (b == 16) b = 8; } sprint(buf, "m%11d %11d %11d %11lud ", m.xy.x, m.xy.y, b, m.msec); mouse.lastcounter = m.counter; if(n > 1+4*12) n = 1+4*12; if(mouse.lastresize != mouse.resize){ mouse.lastresize = mouse.resize; buf[0] = 'r'; } memmove(va, buf, n); return n; case Qsnarf: if(offset == 0){ s = getsnarf(); if(c->aux) free(c->aux); c->aux = s; } if(c->aux == nil) return 0; return readstr(offset, va, n, c->aux); } return 0; }