103 lines
2.0 KiB
C
103 lines
2.0 KiB
C
#include "dat.h"
|
|
#include "fn.h"
|
|
|
|
#include <sys/stat.h>
|
|
#include <sys/un.h>
|
|
|
|
static char adir[256];
|
|
static Channel *clientc;
|
|
|
|
static int
|
|
srvreadkey(int fd, Keyreq *kr)
|
|
{
|
|
uchar req[Ipcreqsz];
|
|
u32int ks, mod;
|
|
int want;
|
|
|
|
if(ipcreadn(fd, req, sizeof req) < 0)
|
|
return -1;
|
|
ipcunpackreq(req, &want, &mod, &ks);
|
|
kr->op = ipcreqreset(req) ? Keyreset : Keypress;
|
|
kr->ks = ks;
|
|
kr->mod = mod;
|
|
memset(&kr->caret, 0, sizeof kr->caret);
|
|
return want;
|
|
}
|
|
|
|
static void
|
|
clientthread(void *arg)
|
|
{
|
|
Channel *reply;
|
|
int fd;
|
|
Keyreq kr;
|
|
Keyres res;
|
|
uchar out[Ipcmaxresp];
|
|
char commit[Maxutf], preedit[Maxutf];
|
|
int n, ncommit, npreedit, want;
|
|
uchar token;
|
|
|
|
fd = (int)(uintptr)arg;
|
|
threadsetname("client %d", fd);
|
|
reply = chancreate(sizeof(Keyres), 0);
|
|
kr.reply = reply;
|
|
kr.owner = &fd;
|
|
while((want = srvreadkey(fd, &kr)) >= 0){
|
|
chansend(keyc, &kr);
|
|
chanrecv(reply, &res);
|
|
ncommit = stoutf(&res.commit, commit, sizeof commit);
|
|
npreedit = stoutf(&res.preedit, preedit, sizeof preedit);
|
|
n = ipcpackresp(out, sizeof out, res.eaten,
|
|
commit, ncommit, preedit, npreedit, want);
|
|
if(n < 0 || ipcsend(fd, out, n) < 0)
|
|
break;
|
|
}
|
|
kr.op = Keyrelease;
|
|
chansend(keyc, &kr);
|
|
chanrecv(reply, &res);
|
|
chanfree(reply);
|
|
close(fd);
|
|
chanrecv(clientc, &token);
|
|
}
|
|
|
|
void
|
|
srvinit(void)
|
|
{
|
|
char *addr, path[sizeof(((struct sockaddr_un*)0)->sun_path)];
|
|
|
|
if(ipcpath(path, sizeof path) < 0)
|
|
die("IPC path is too long");
|
|
addr = smprint("unix!%s", path);
|
|
if(addr == nil)
|
|
die("out of memory");
|
|
if(announce(addr, adir) < 0)
|
|
die("IPC endpoint is already in use: %r");
|
|
free(addr);
|
|
if(chmod(path, 0600) < 0)
|
|
die("can't protect IPC endpoint: %s", path);
|
|
}
|
|
|
|
void
|
|
srvthread(void*)
|
|
{
|
|
char ldir[40];
|
|
int fd;
|
|
uchar token;
|
|
|
|
threadsetname("srv");
|
|
token = 0;
|
|
clientc = chancreate(sizeof token, Maxclients);
|
|
for(;;){
|
|
fd = listen(adir, ldir);
|
|
if(fd < 0)
|
|
continue;
|
|
if(channbsend(clientc, &token) <= 0){
|
|
close(fd);
|
|
continue;
|
|
}
|
|
if(proccreate(clientthread, (void*)(uintptr)fd, 8192) < 0){
|
|
chanrecv(clientc, &token);
|
|
close(fd);
|
|
}
|
|
}
|
|
}
|