ipc, srv, gtk: the client's text goes over and a take-back comes back

The engine can reach a Hanja reading back into the text the client
already holds, but only if the frontend hands that text over and can
take some of it away again.  GTK 3 has both: retrieve-surrounding
brings the text around the cursor and delete-surrounding removes runes
before it, and a widget that answers neither leaves the text empty, so
nothing is ever reached into or taken from it.

The wire grows a control frame for the text, sent like the caret only
when it changes, and one byte in every response for the runes to take
back.  That byte moves the length fields along, so the version goes to
2: an old daemon and a new module, either way round, fail the handshake
and the module falls through to GtkIMContextSimple rather than misread
a frame.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-18 14:31:21 +09:00
parent f7277f54a3
commit 910bf51347
11 changed files with 286 additions and 41 deletions

38
ipc.c
View File

@@ -252,6 +252,22 @@ ipcpackcaret(unsigned char req[Ipccaretsz], int valid, int32_t x,
}
}
void
ipcpacksurround(unsigned char req[Ipcreqsz], size_t n)
{
memset(req, 0, Ipcreqsz);
req[0] = Ipcext;
req[1] = Ipcversion;
req[2] = Ipcopsurround;
putlen(req + 4, n);
}
size_t
ipcsurroundlen(const unsigned char req[Ipcreqsz])
{
return getlen(req + 4);
}
void
ipcunpackreq(const unsigned char req[Ipcreqsz], int *want, uint32_t *mod,
uint32_t *key)
@@ -281,6 +297,11 @@ ipcreqtype(const unsigned char req[Ipcreqsz])
if(req[0] != Ipcext || req[3] > 1)
return Ipcunknown;
return Ipccaret;
case Ipcopsurround:
if(req[0] != Ipcext || req[3] != 0 ||
getlen(req + 4) > Ipcfieldmax)
return Ipcunknown;
return Ipcsurround;
}
return Ipcunknown;
}
@@ -305,19 +326,21 @@ ipcreqreset(const unsigned char req[Ipcreqsz])
}
int
ipcpackresp(unsigned char *dst, size_t cap, int eaten,
ipcpackresp(unsigned char *dst, size_t cap, int eaten, int del,
const char *commit, size_t ncommit, const char *preedit, size_t npreedit,
int want)
{
size_t n;
if(ncommit > Ipcfieldmax || npreedit > Ipcfieldmax)
if(ncommit > Ipcfieldmax || npreedit > Ipcfieldmax ||
del < 0 || del > 0xff)
return -1;
n = Ipcresphdrsz + ncommit + (want ? Ipclensz + npreedit : 0);
if(cap < n)
return -1;
dst[0] = eaten != 0;
putlen(dst + 1, ncommit);
dst[1] = del;
putlen(dst + 2, ncommit);
memcpy(dst + Ipcresphdrsz, commit, ncommit);
if(want){
putlen(dst + Ipcresphdrsz + ncommit, npreedit);
@@ -398,7 +421,7 @@ readfield(int fd, char *dst, int64_t until)
int
ipcreadresp(int fd, int want, char *commit, char *preedit, Ipcresp *resp)
{
unsigned char eaten;
unsigned char head[2];
int64_t until;
int n;
@@ -406,13 +429,14 @@ ipcreadresp(int fd, int want, char *commit, char *preedit, Ipcresp *resp)
preedit[0] = '\0';
memset(resp, 0, sizeof *resp);
until = deadline();
if(readwait(fd, &eaten, 1, until) < 0)
if(readwait(fd, head, sizeof head, until) < 0)
return -1;
if(eaten > 1){
if(head[0] > 1){
errno = EPROTO;
return -1;
}
resp->eaten = eaten;
resp->eaten = head[0];
resp->del = head[1];
n = readfield(fd, commit, until);
if(n < 0)
return -1;