Compare commits
6 Commits
b4fdc6e813
...
3a203766d5
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a203766d5 | |||
| 884ba56619 | |||
| 9afad8e4ff | |||
| 64d50afc6b | |||
| b887972fb2 | |||
| d06cef6575 |
18
Makefile
18
Makefile
@@ -1,19 +1,29 @@
|
|||||||
CC = 9c
|
CC = 9c
|
||||||
LD = 9l
|
LD = 9l
|
||||||
CFLAGS = -Wall -Wextra -O2
|
DBUS_CFLAGS := $(shell pkg-config --cflags dbus-1)
|
||||||
|
DBUS_LIBS := $(shell pkg-config --libs dbus-1)
|
||||||
|
CFLAGS = -Wall -Wextra -O2 -g $(DBUS_CFLAGS)
|
||||||
PROG = strans
|
PROG = strans
|
||||||
|
|
||||||
SRCS = $(wildcard *.c)
|
SRCS = $(wildcard *.c)
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
all: $(PROG)
|
all: $(PROG) xim bench
|
||||||
|
|
||||||
$(PROG): $(OBJS)
|
$(PROG): $(OBJS)
|
||||||
$(LD) -o $@ $(OBJS) -lthread -lString -lbio -lxcb -lm
|
$(LD) -o $@ $(OBJS) -lthread -lString -lbio -lxcb -lm $(DBUS_LIBS)
|
||||||
|
|
||||||
$(OBJS): dat.h fn.h ipc.h
|
$(OBJS): dat.h fn.h ipc.h
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJS) $(PROG)
|
rm -f $(OBJS) $(PROG)
|
||||||
|
make -C xim/ clean
|
||||||
|
make -C bench/ clean
|
||||||
|
|
||||||
.PHONY: all clean
|
xim:
|
||||||
|
make -C xim/
|
||||||
|
|
||||||
|
bench:
|
||||||
|
make -C bench/
|
||||||
|
|
||||||
|
.PHONY: all clean xim bench
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ Inspired by 9front's ktrans. Threads communicate via CSP channels.
|
|||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- plan9port
|
- plan9port
|
||||||
|
- dbus-1
|
||||||
- gtk+-3.0 (optional, for GTK IM module)
|
- gtk+-3.0 (optional, for GTK IM module)
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
@@ -28,6 +29,12 @@ For GTK apps:
|
|||||||
|
|
||||||
GTK_IM_MODULE=strans gedit
|
GTK_IM_MODULE=strans gedit
|
||||||
|
|
||||||
|
For IBus apps (kitty, foot, etc.):
|
||||||
|
|
||||||
|
GLFW_IM_MODULE=ibus kitty
|
||||||
|
|
||||||
|
Strans itself is the IBus endpoint; no ibus-daemon or fcitx5 needed.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Switch input modes with Ctrl + key:
|
Switch input modes with Ctrl + key:
|
||||||
@@ -50,6 +57,7 @@ Four threads communicate via CSP channels:
|
|||||||
- [dictthread](dict.c#L38): dictionary lookup
|
- [dictthread](dict.c#L38): dictionary lookup
|
||||||
- [drawthread](win.c#L133): preedit window rendering
|
- [drawthread](win.c#L133): preedit window rendering
|
||||||
- [srvthread](srv.c#L42): IPC via unix socket
|
- [srvthread](srv.c#L42): IPC via unix socket
|
||||||
|
- [ibusthread](ibus.c): IBus D-Bus endpoint for GLFW/kitty
|
||||||
|
|
||||||
Adapters (strans-xim, im-strans.so) bridge X11/GTK events.
|
Adapters (strans-xim, im-strans.so) bridge X11/GTK events.
|
||||||
|
|
||||||
|
|||||||
23
bench.sh
Executable file
23
bench.sh
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pkill strans
|
||||||
|
pkill strans-xim
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
./strans map font &
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
# warm up glyph cache
|
||||||
|
./bench/bench bench/bench.keys 100
|
||||||
|
echo "cache warmed up"
|
||||||
|
|
||||||
|
STRANS_PID=$(pgrep -x strans)
|
||||||
|
perf record -g -o bench/perf.data -p "$STRANS_PID" &
|
||||||
|
PERF_PID=$!
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
./bench/bench bench/bench.keys 100
|
||||||
|
|
||||||
|
kill -INT $PERF_PID
|
||||||
|
wait $PERF_PID 2>/dev/null
|
||||||
|
pkill strans
|
||||||
8
bench/Makefile
Normal file
8
bench/Makefile
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
CC = cc
|
||||||
|
CFLAGS = -Wall -O2
|
||||||
|
|
||||||
|
bench: main.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ main.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f bench
|
||||||
BIN
bench/bench
Executable file
BIN
bench/bench
Executable file
Binary file not shown.
30
bench/bench.keys
Normal file
30
bench/bench.keys
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
^ntoukyounotenki^Ryoidesu^R
|
||||||
|
konpyuutanopuroguramu^B^B^Bgramu^R
|
||||||
|
nihongowobenkyoushiteimasu^R
|
||||||
|
sakuranoshanohana^B^B^B^Bhanagasaiteiru^R
|
||||||
|
kaigi^Ekyounokaigi^R
|
||||||
|
ashitahaiitenkininarudeshou^R
|
||||||
|
^sdkssudgktpdy^Rgksrmfekfqnxm^R
|
||||||
|
vmfhrrh^Btprtmxm^Rdufrlavldml^R
|
||||||
|
dmlrlwkekfwprtm^R
|
||||||
|
gksrnl^B^B^Bdlfp^R
|
||||||
|
answkd^Eanswkd^R
|
||||||
|
qkrtlwkf^B^B^Bwkfgkf^R
|
||||||
|
^thello world^R
|
||||||
|
the quick brown fox jumps over the lazy dog^R
|
||||||
|
programming is fun^R
|
||||||
|
^vVietNam^Rxinchaobancokhoekhong^R
|
||||||
|
toidilambaitap^B^B^Bbaitap^R
|
||||||
|
hoctiengviet^R
|
||||||
|
^nkaishanitsuutomeshiteimasu^R
|
||||||
|
tanaboraguukoudesu^B^B^B^B^Bkoudesu^R
|
||||||
|
^srhksrnrdlTkfgkwl^R
|
||||||
|
answkddkssud^B^Bdkssud^R
|
||||||
|
wnsdydgktpdy^R
|
||||||
|
^vtoidenlopcuahang^R
|
||||||
|
motngaymoiconduong^R
|
||||||
|
^thigh performance input method^R
|
||||||
|
^nnyuuryokuhouhou^R
|
||||||
|
tesutonosuuretsu^R
|
||||||
|
^sxkfldzmsdml^R
|
||||||
|
qhfmfjxm^R
|
||||||
168
bench/main.c
Normal file
168
bench/main.c
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
enum {
|
||||||
|
Kback = 0xf008,
|
||||||
|
Kret = 0xf00d,
|
||||||
|
Kesc = 0xf01b,
|
||||||
|
Mctrl = 1<<2,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Key Key;
|
||||||
|
struct Key {
|
||||||
|
int k;
|
||||||
|
int mod;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Key *keys;
|
||||||
|
static int nkeys;
|
||||||
|
static int fd;
|
||||||
|
|
||||||
|
static void
|
||||||
|
die(char *msg)
|
||||||
|
{
|
||||||
|
perror(msg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
addkey(int k, int mod)
|
||||||
|
{
|
||||||
|
static int cap;
|
||||||
|
|
||||||
|
if(nkeys >= cap){
|
||||||
|
cap = cap ? cap * 2 : 256;
|
||||||
|
keys = realloc(keys, cap * sizeof(Key));
|
||||||
|
if(!keys)
|
||||||
|
die("realloc");
|
||||||
|
}
|
||||||
|
keys[nkeys].k = k;
|
||||||
|
keys[nkeys].mod = mod;
|
||||||
|
nkeys++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
loadkeys(char *file)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
f = fopen(file, "r");
|
||||||
|
if(!f)
|
||||||
|
die(file);
|
||||||
|
while((c = fgetc(f)) != EOF){
|
||||||
|
if(c == '\n' || c == '\r')
|
||||||
|
continue;
|
||||||
|
if(c != '^'){
|
||||||
|
addkey(c, 0);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
c = fgetc(f);
|
||||||
|
if(c == EOF)
|
||||||
|
break;
|
||||||
|
switch(c){
|
||||||
|
case 'B':
|
||||||
|
addkey(Kback, 0);
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
addkey(Kret, 0);
|
||||||
|
break;
|
||||||
|
case 'E':
|
||||||
|
addkey(Kesc, 0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
addkey(c, Mctrl);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dial(void)
|
||||||
|
{
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
|
||||||
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if(fd < 0)
|
||||||
|
die("socket");
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
snprintf(addr.sun_path, sizeof(addr.sun_path),
|
||||||
|
"/tmp/strans.%d", getuid());
|
||||||
|
if(connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
||||||
|
die("connect");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sendkey(int key, int mod)
|
||||||
|
{
|
||||||
|
unsigned char req[4];
|
||||||
|
|
||||||
|
req[0] = 0;
|
||||||
|
req[1] = mod;
|
||||||
|
req[2] = key & 0xff;
|
||||||
|
req[3] = (key >> 8) & 0xff;
|
||||||
|
if(write(fd, req, 4) != 4)
|
||||||
|
die("write");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
readresp(void)
|
||||||
|
{
|
||||||
|
unsigned char buf[256];
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if(read(fd, buf, 2) != 2)
|
||||||
|
return -1;
|
||||||
|
n = buf[1];
|
||||||
|
if(n > 0 && read(fd, buf + 2, n) != n)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double
|
||||||
|
now(void)
|
||||||
|
{
|
||||||
|
struct timespec ts;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i, j, niter;
|
||||||
|
double t0, t1, dt;
|
||||||
|
|
||||||
|
if(argc < 2){
|
||||||
|
fprintf(stderr, "usage: bench file [niter]\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadkeys(argv[1]);
|
||||||
|
niter = argc > 2 ? atoi(argv[2]) : 1000;
|
||||||
|
dial();
|
||||||
|
t0 = now();
|
||||||
|
for(i = 0; i < niter; i++)
|
||||||
|
for(j = 0; j < nkeys; j++){
|
||||||
|
sendkey(keys[j].k, keys[j].mod);
|
||||||
|
if(readresp() < 0){
|
||||||
|
fprintf(stderr, "failed at iter %d key %d\n", i, j);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t1 = now();
|
||||||
|
dt = t1 - t0;
|
||||||
|
printf("%d iters x %d keys = %d keys\n", niter, nkeys, niter * nkeys);
|
||||||
|
printf("%.3f ms total, %.3f us/key, %.3f us/iter\n",
|
||||||
|
dt * 1000, dt * 1e6 / (niter * nkeys), dt * 1e6 / niter);
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
bench/perf.data
Normal file
BIN
bench/perf.data
Normal file
Binary file not shown.
BIN
bench/perf.data.old
Normal file
BIN
bench/perf.data.old
Normal file
Binary file not shown.
7
dat.h
7
dat.h
@@ -97,6 +97,9 @@ struct Lang
|
|||||||
int lang;
|
int lang;
|
||||||
char *mapname;
|
char *mapname;
|
||||||
char *dictname;
|
char *dictname;
|
||||||
|
Emit (*trans)(Im*, Rune);
|
||||||
|
void (*back)(Im*);
|
||||||
|
void (*dictq)(Im*);
|
||||||
Trie *map;
|
Trie *map;
|
||||||
Hmap *dict;
|
Hmap *dict;
|
||||||
};
|
};
|
||||||
@@ -113,8 +116,8 @@ struct Im
|
|||||||
typedef struct Drawcmd Drawcmd;
|
typedef struct Drawcmd Drawcmd;
|
||||||
struct Drawcmd
|
struct Drawcmd
|
||||||
{
|
{
|
||||||
Str preedit;
|
Str pre;
|
||||||
Str kouho[Maxkouho];
|
Str kouho[Maxdisp];
|
||||||
int nkouho;
|
int nkouho;
|
||||||
int sel;
|
int sel;
|
||||||
};
|
};
|
||||||
|
|||||||
5
fn.h
5
fn.h
@@ -27,8 +27,13 @@ void dictthread(void*);
|
|||||||
void drawthread(void*);
|
void drawthread(void*);
|
||||||
void imthread(void*);
|
void imthread(void*);
|
||||||
Emit transmap(Im*, Rune);
|
Emit transmap(Im*, Rune);
|
||||||
|
Emit transko(Im*, Rune);
|
||||||
|
Emit transvi(Im*, Rune);
|
||||||
|
void backko(Im*);
|
||||||
|
void dictsend(Im*, Str*);
|
||||||
|
|
||||||
void srvthread(void*);
|
void srvthread(void*);
|
||||||
|
void ibusthread(void*);
|
||||||
|
|
||||||
void* emalloc(ulong);
|
void* emalloc(ulong);
|
||||||
void* erealloc(void*, ulong);
|
void* erealloc(void*, ulong);
|
||||||
|
|||||||
22
font.c
22
font.c
@@ -99,7 +99,8 @@ void
|
|||||||
putfont(u32int *buf, int w, int h, int px, int py, Rune r)
|
putfont(u32int *buf, int w, int h, int px, int py, Rune r)
|
||||||
{
|
{
|
||||||
Glyph *g;
|
Glyph *g;
|
||||||
int i, j, x, y, a, sel, f;
|
int i, j, a, sel, f;
|
||||||
|
int y0, j0, j1, x0, i0, i1;
|
||||||
u32int *p;
|
u32int *p;
|
||||||
|
|
||||||
if(r >= Nglyphs)
|
if(r >= Nglyphs)
|
||||||
@@ -116,17 +117,18 @@ putfont(u32int *buf, int w, int h, int px, int py, Rune r)
|
|||||||
if(g->bmp == nil)
|
if(g->bmp == nil)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(j = 0; j < g->h; j++){
|
|
||||||
y = py + j + g->oy + Fontsz - Fontbase;
|
y0 = py + g->oy + Fontsz - Fontbase;
|
||||||
if(y < 0 || y >= h)
|
j0 = y0 < 0 ? -y0 : 0;
|
||||||
continue;
|
j1 = y0 + g->h > h ? h - y0 : g->h;
|
||||||
for(i = 0; i < g->w; i++){
|
x0 = px + g->ox;
|
||||||
x = px + i + g->ox;
|
i0 = x0 < 0 ? -x0 : 0;
|
||||||
if(x < 0 || x >= w)
|
i1 = x0 + g->w > w ? w - x0 : g->w;
|
||||||
continue;
|
for(j = j0; j < j1; j++){
|
||||||
|
for(i = i0; i < i1; i++){
|
||||||
a = g->bmp[j * g->w + i];
|
a = g->bmp[j * g->w + i];
|
||||||
if(a > 0){
|
if(a > 0){
|
||||||
p = &buf[y * w + x];
|
p = &buf[(y0 + j) * w + x0 + i];
|
||||||
sel = (*p == Colsel) ? 1 : 0;
|
sel = (*p == Colsel) ? 1 : 0;
|
||||||
*p = blendtab[sel][a];
|
*p = blendtab[sel][a];
|
||||||
}
|
}
|
||||||
|
|||||||
585
ibus.c
Normal file
585
ibus.c
Normal file
@@ -0,0 +1,585 @@
|
|||||||
|
#include "dat.h"
|
||||||
|
#include "fn.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <dbus/dbus.h>
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
Maxwatches = 32,
|
||||||
|
Maxconns = 16,
|
||||||
|
Relmask = 1<<30,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Watch Watch;
|
||||||
|
struct Watch
|
||||||
|
{
|
||||||
|
DBusWatch *w;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Watch watches[Maxwatches];
|
||||||
|
static int nwatches;
|
||||||
|
static DBusConnection *conns[Maxconns];
|
||||||
|
static int nconns;
|
||||||
|
static DBusServer *srv;
|
||||||
|
static char addrfile[256];
|
||||||
|
static int icctr;
|
||||||
|
static int busctr;
|
||||||
|
|
||||||
|
static DBusHandlerResult onmsg(DBusConnection*, DBusMessage*, void*);
|
||||||
|
|
||||||
|
static void
|
||||||
|
unlinkaddr(void)
|
||||||
|
{
|
||||||
|
if(addrfile[0] != '\0')
|
||||||
|
unlink(addrfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
machineid(char *buf, int sz)
|
||||||
|
{
|
||||||
|
int fd, n;
|
||||||
|
|
||||||
|
fd = open("/etc/machine-id", 0);
|
||||||
|
if(fd < 0)
|
||||||
|
fd = open("/var/lib/dbus/machine-id", 0);
|
||||||
|
if(fd < 0){
|
||||||
|
buf[0] = '\0';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
n = read(fd, buf, sz - 1);
|
||||||
|
close(fd);
|
||||||
|
if(n < 0) n = 0;
|
||||||
|
buf[n] = '\0';
|
||||||
|
while(n > 0 && (buf[n-1] == '\n' || buf[n-1] == '\r'))
|
||||||
|
buf[--n] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
xdisplay(char *host, int hsz, char *num, int nsz)
|
||||||
|
{
|
||||||
|
char *d, *colon, *dot, *p;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
strncpy(host, "unix", hsz);
|
||||||
|
host[hsz-1] = '\0';
|
||||||
|
strncpy(num, "0", nsz);
|
||||||
|
num[nsz-1] = '\0';
|
||||||
|
d = getenv("DISPLAY");
|
||||||
|
if(d == nil || d[0] == '\0')
|
||||||
|
return;
|
||||||
|
colon = strchr(d, ':');
|
||||||
|
if(colon == nil)
|
||||||
|
return;
|
||||||
|
if(colon > d){
|
||||||
|
n = colon - d;
|
||||||
|
if(n >= hsz) n = hsz - 1;
|
||||||
|
memcpy(host, d, n);
|
||||||
|
host[n] = '\0';
|
||||||
|
}
|
||||||
|
p = colon + 1;
|
||||||
|
dot = strchr(p, '.');
|
||||||
|
n = dot ? dot - p : (int)strlen(p);
|
||||||
|
if(n >= nsz) n = nsz - 1;
|
||||||
|
memcpy(num, p, n);
|
||||||
|
num[n] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
buildaddrpath(char *buf, int sz)
|
||||||
|
{
|
||||||
|
char mid[64], host[64], num[8], *cfg, *home;
|
||||||
|
struct stat st;
|
||||||
|
char dir[256];
|
||||||
|
|
||||||
|
machineid(mid, sizeof(mid));
|
||||||
|
if(mid[0] == '\0')
|
||||||
|
return -1;
|
||||||
|
xdisplay(host, sizeof(host), num, sizeof(num));
|
||||||
|
cfg = getenv("XDG_CONFIG_HOME");
|
||||||
|
if(cfg != nil && cfg[0] != '\0')
|
||||||
|
snprintf(dir, sizeof(dir), "%s/ibus/bus", cfg);
|
||||||
|
else{
|
||||||
|
home = getenv("HOME");
|
||||||
|
if(home == nil)
|
||||||
|
return -1;
|
||||||
|
snprintf(dir, sizeof(dir), "%s/.config/ibus/bus", home);
|
||||||
|
}
|
||||||
|
if(stat(dir, &st) < 0){
|
||||||
|
char tmp[256];
|
||||||
|
char *p;
|
||||||
|
strncpy(tmp, dir, sizeof(tmp));
|
||||||
|
tmp[sizeof(tmp)-1] = '\0';
|
||||||
|
for(p = tmp+1; *p; p++)
|
||||||
|
if(*p == '/'){
|
||||||
|
*p = '\0';
|
||||||
|
mkdir(tmp, 0700);
|
||||||
|
*p = '/';
|
||||||
|
}
|
||||||
|
mkdir(tmp, 0700);
|
||||||
|
}
|
||||||
|
snprintf(buf, sz, "%s/%s-%s-%s", dir, mid, host, num);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
writeaddr(char *path, char *addr)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
fp = fopen(path, "w");
|
||||||
|
if(fp == nil)
|
||||||
|
return -1;
|
||||||
|
fprintf(fp, "IBUS_ADDRESS=%s\n", addr);
|
||||||
|
fprintf(fp, "IBUS_DAEMON_PID=%d\n", (int)getpid());
|
||||||
|
fclose(fp);
|
||||||
|
chmod(path, 0600);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static dbus_bool_t
|
||||||
|
addwatch(DBusWatch *w, void *_)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
USED(_);
|
||||||
|
for(i = 0; i < nwatches; i++)
|
||||||
|
if(watches[i].w == nil){
|
||||||
|
watches[i].w = w;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
if(nwatches >= Maxwatches)
|
||||||
|
return FALSE;
|
||||||
|
watches[nwatches++].w = w;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
removewatch(DBusWatch *w, void *_)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
USED(_);
|
||||||
|
for(i = 0; i < nwatches; i++)
|
||||||
|
if(watches[i].w == w){
|
||||||
|
watches[i].w = nil;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
togglewatch(DBusWatch *w, void *_)
|
||||||
|
{
|
||||||
|
USED(w);
|
||||||
|
USED(_);
|
||||||
|
}
|
||||||
|
|
||||||
|
static u32int
|
||||||
|
kget(u32int sym)
|
||||||
|
{
|
||||||
|
if(sym >= 0xff00 && sym <= 0xffff)
|
||||||
|
return Kspec + (sym - 0xff00);
|
||||||
|
return sym;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u32int
|
||||||
|
mget(u32int state)
|
||||||
|
{
|
||||||
|
u32int m;
|
||||||
|
|
||||||
|
m = 0;
|
||||||
|
if(state & (1<<0)) m |= Mshift;
|
||||||
|
if(state & (1<<2)) m |= Mctrl;
|
||||||
|
if(state & (1<<3)) m |= Malt;
|
||||||
|
if(state & (1<<6)) m |= Msuper;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
sendkey(u32int ks, u32int mod, char *out, int sz, int *eaten)
|
||||||
|
{
|
||||||
|
Keyreq kr;
|
||||||
|
int p[2];
|
||||||
|
uchar hdr[2];
|
||||||
|
int n;
|
||||||
|
|
||||||
|
*eaten = 0;
|
||||||
|
out[0] = '\0';
|
||||||
|
if(pipe(p) < 0)
|
||||||
|
return 0;
|
||||||
|
kr.fd = p[1];
|
||||||
|
kr.ks = ks;
|
||||||
|
kr.mod = mod;
|
||||||
|
chansend(keyc, &kr);
|
||||||
|
if(read(p[0], hdr, 2) != 2){
|
||||||
|
close(p[0]);
|
||||||
|
close(p[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*eaten = hdr[0];
|
||||||
|
n = hdr[1];
|
||||||
|
if(n > 0 && n < sz){
|
||||||
|
if(read(p[0], out, n) != n)
|
||||||
|
n = 0;
|
||||||
|
}else
|
||||||
|
n = 0;
|
||||||
|
out[n] = '\0';
|
||||||
|
close(p[0]);
|
||||||
|
close(p[1]);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sendreset(void)
|
||||||
|
{
|
||||||
|
char buf[64];
|
||||||
|
int eaten;
|
||||||
|
|
||||||
|
sendkey(Kesc, 0, buf, sizeof(buf), &eaten);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
appendibustext(DBusMessageIter *it, const char *s)
|
||||||
|
{
|
||||||
|
DBusMessageIter v, st, attach, alv, ali, attr, alist;
|
||||||
|
const char *name = "IBusText";
|
||||||
|
const char *aname = "IBusAttrList";
|
||||||
|
|
||||||
|
dbus_message_iter_open_container(it, DBUS_TYPE_VARIANT, "(sa{sv}sv)", &v);
|
||||||
|
dbus_message_iter_open_container(&v, DBUS_TYPE_STRUCT, nil, &st);
|
||||||
|
dbus_message_iter_append_basic(&st, DBUS_TYPE_STRING, &name);
|
||||||
|
dbus_message_iter_open_container(&st, DBUS_TYPE_ARRAY, "{sv}", &attach);
|
||||||
|
dbus_message_iter_close_container(&st, &attach);
|
||||||
|
dbus_message_iter_append_basic(&st, DBUS_TYPE_STRING, &s);
|
||||||
|
dbus_message_iter_open_container(&st, DBUS_TYPE_VARIANT, "(sa{sv}av)", &alv);
|
||||||
|
dbus_message_iter_open_container(&alv, DBUS_TYPE_STRUCT, nil, &ali);
|
||||||
|
dbus_message_iter_append_basic(&ali, DBUS_TYPE_STRING, &aname);
|
||||||
|
dbus_message_iter_open_container(&ali, DBUS_TYPE_ARRAY, "{sv}", &attr);
|
||||||
|
dbus_message_iter_close_container(&ali, &attr);
|
||||||
|
dbus_message_iter_open_container(&ali, DBUS_TYPE_ARRAY, "v", &alist);
|
||||||
|
dbus_message_iter_close_container(&ali, &alist);
|
||||||
|
dbus_message_iter_close_container(&alv, &ali);
|
||||||
|
dbus_message_iter_close_container(&st, &alv);
|
||||||
|
dbus_message_iter_close_container(&v, &st);
|
||||||
|
dbus_message_iter_close_container(it, &v);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
emitcommit(DBusConnection *c, const char *path, const char *text)
|
||||||
|
{
|
||||||
|
DBusMessage *sig;
|
||||||
|
DBusMessageIter it;
|
||||||
|
|
||||||
|
sig = dbus_message_new_signal(path, "org.freedesktop.IBus.InputContext", "CommitText");
|
||||||
|
if(sig == nil)
|
||||||
|
return;
|
||||||
|
dbus_message_iter_init_append(sig, &it);
|
||||||
|
appendibustext(&it, text);
|
||||||
|
dbus_connection_send(c, sig, nil);
|
||||||
|
dbus_message_unref(sig);
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handlehello(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
DBusMessage *r;
|
||||||
|
char name[32];
|
||||||
|
const char *np;
|
||||||
|
|
||||||
|
busctr++;
|
||||||
|
snprintf(name, sizeof(name), ":1.%d", busctr);
|
||||||
|
np = name;
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
dbus_message_append_args(r, DBUS_TYPE_STRING, &np, DBUS_TYPE_INVALID);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handlecreate(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
DBusMessage *r;
|
||||||
|
char path[64];
|
||||||
|
const char *pp;
|
||||||
|
|
||||||
|
icctr++;
|
||||||
|
snprintf(path, sizeof(path), "/org/freedesktop/IBus/InputContext_%d", icctr);
|
||||||
|
pp = path;
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
dbus_message_append_args(r, DBUS_TYPE_OBJECT_PATH, &pp, DBUS_TYPE_INVALID);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handlekey(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
DBusMessage *r;
|
||||||
|
DBusError err;
|
||||||
|
dbus_uint32_t sym, code, state;
|
||||||
|
char commit[256];
|
||||||
|
int eaten, n;
|
||||||
|
dbus_bool_t b;
|
||||||
|
u32int ks, mod;
|
||||||
|
|
||||||
|
dbus_error_init(&err);
|
||||||
|
if(!dbus_message_get_args(m, &err,
|
||||||
|
DBUS_TYPE_UINT32, &sym,
|
||||||
|
DBUS_TYPE_UINT32, &code,
|
||||||
|
DBUS_TYPE_UINT32, &state,
|
||||||
|
DBUS_TYPE_INVALID)){
|
||||||
|
dbus_error_free(&err);
|
||||||
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||||
|
}
|
||||||
|
USED(code);
|
||||||
|
if(state & Relmask){
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
b = FALSE;
|
||||||
|
dbus_message_append_args(r, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
ks = kget(sym);
|
||||||
|
mod = mget(state);
|
||||||
|
n = sendkey(ks, mod, commit, sizeof(commit), &eaten);
|
||||||
|
if(n > 0)
|
||||||
|
emitcommit(c, dbus_message_get_path(m), commit);
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
b = eaten ? TRUE : FALSE;
|
||||||
|
dbus_message_append_args(r, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handlenoop(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
DBusMessage *r;
|
||||||
|
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handlereset(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
sendreset();
|
||||||
|
return handlenoop(c, m);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char introspectxml[] =
|
||||||
|
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
|
||||||
|
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
|
||||||
|
"<node>\n"
|
||||||
|
" <interface name=\"org.freedesktop.IBus\">\n"
|
||||||
|
" <method name=\"CreateInputContext\">\n"
|
||||||
|
" <arg direction=\"in\" type=\"s\"/>\n"
|
||||||
|
" <arg direction=\"out\" type=\"o\"/>\n"
|
||||||
|
" </method>\n"
|
||||||
|
" </interface>\n"
|
||||||
|
" <interface name=\"org.freedesktop.IBus.InputContext\">\n"
|
||||||
|
" <method name=\"ProcessKeyEvent\">\n"
|
||||||
|
" <arg direction=\"in\" type=\"u\"/>\n"
|
||||||
|
" <arg direction=\"in\" type=\"u\"/>\n"
|
||||||
|
" <arg direction=\"in\" type=\"u\"/>\n"
|
||||||
|
" <arg direction=\"out\" type=\"b\"/>\n"
|
||||||
|
" </method>\n"
|
||||||
|
" <method name=\"FocusIn\"/>\n"
|
||||||
|
" <method name=\"FocusOut\"/>\n"
|
||||||
|
" <method name=\"Reset\"/>\n"
|
||||||
|
" <method name=\"Destroy\"/>\n"
|
||||||
|
" <method name=\"SetCapabilities\"><arg direction=\"in\" type=\"u\"/></method>\n"
|
||||||
|
" <method name=\"SetCursorLocation\">"
|
||||||
|
"<arg direction=\"in\" type=\"i\"/><arg direction=\"in\" type=\"i\"/>"
|
||||||
|
"<arg direction=\"in\" type=\"i\"/><arg direction=\"in\" type=\"i\"/></method>\n"
|
||||||
|
" <method name=\"SetEngine\"><arg direction=\"in\" type=\"s\"/></method>\n"
|
||||||
|
" <signal name=\"CommitText\"><arg type=\"v\"/></signal>\n"
|
||||||
|
" </interface>\n"
|
||||||
|
"</node>\n";
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
handleintrospect(DBusConnection *c, DBusMessage *m)
|
||||||
|
{
|
||||||
|
DBusMessage *r;
|
||||||
|
const char *xml = introspectxml;
|
||||||
|
|
||||||
|
r = dbus_message_new_method_return(m);
|
||||||
|
dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID);
|
||||||
|
dbus_connection_send(c, r, nil);
|
||||||
|
dbus_message_unref(r);
|
||||||
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DBusHandlerResult
|
||||||
|
onmsg(DBusConnection *c, DBusMessage *m, void *_)
|
||||||
|
{
|
||||||
|
const char *iface, *member, *path;
|
||||||
|
|
||||||
|
USED(_);
|
||||||
|
if(dbus_message_get_type(m) != DBUS_MESSAGE_TYPE_METHOD_CALL)
|
||||||
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||||
|
iface = dbus_message_get_interface(m);
|
||||||
|
member = dbus_message_get_member(m);
|
||||||
|
path = dbus_message_get_path(m);
|
||||||
|
if(iface == nil || member == nil || path == nil)
|
||||||
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||||
|
if(strcmp(iface, "org.freedesktop.DBus.Introspectable") == 0
|
||||||
|
&& strcmp(member, "Introspect") == 0)
|
||||||
|
return handleintrospect(c, m);
|
||||||
|
if(strcmp(iface, "org.freedesktop.DBus") == 0){
|
||||||
|
if(strcmp(member, "Hello") == 0)
|
||||||
|
return handlehello(c, m);
|
||||||
|
if(strcmp(member, "AddMatch") == 0
|
||||||
|
|| strcmp(member, "RemoveMatch") == 0)
|
||||||
|
return handlenoop(c, m);
|
||||||
|
}
|
||||||
|
if(strcmp(iface, "org.freedesktop.IBus") == 0){
|
||||||
|
if(strcmp(member, "CreateInputContext") == 0)
|
||||||
|
return handlecreate(c, m);
|
||||||
|
}
|
||||||
|
if(strcmp(iface, "org.freedesktop.IBus.InputContext") == 0){
|
||||||
|
if(strcmp(member, "ProcessKeyEvent") == 0)
|
||||||
|
return handlekey(c, m);
|
||||||
|
if(strcmp(member, "FocusOut") == 0
|
||||||
|
|| strcmp(member, "Reset") == 0)
|
||||||
|
return handlereset(c, m);
|
||||||
|
if(strcmp(member, "FocusIn") == 0
|
||||||
|
|| strcmp(member, "Destroy") == 0
|
||||||
|
|| strcmp(member, "SetCapabilities") == 0
|
||||||
|
|| strcmp(member, "SetCursorLocation") == 0
|
||||||
|
|| strcmp(member, "SetEngine") == 0)
|
||||||
|
return handlenoop(c, m);
|
||||||
|
}
|
||||||
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
newconn(DBusServer *s, DBusConnection *c, void *_)
|
||||||
|
{
|
||||||
|
DBusObjectPathVTable vt;
|
||||||
|
|
||||||
|
USED(s);
|
||||||
|
USED(_);
|
||||||
|
if(nconns >= Maxconns)
|
||||||
|
return;
|
||||||
|
dbus_connection_ref(c);
|
||||||
|
dbus_connection_set_watch_functions(c, addwatch, removewatch, togglewatch, nil, nil);
|
||||||
|
memset(&vt, 0, sizeof(vt));
|
||||||
|
vt.message_function = onmsg;
|
||||||
|
dbus_connection_register_fallback(c, "/", &vt, nil);
|
||||||
|
conns[nconns++] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pruneconns(void)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
for(i = 0; i < nconns; i++){
|
||||||
|
if(dbus_connection_get_is_connected(conns[i])){
|
||||||
|
conns[j++] = conns[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dbus_connection_unref(conns[i]);
|
||||||
|
}
|
||||||
|
nconns = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ibusinit(void)
|
||||||
|
{
|
||||||
|
DBusError err;
|
||||||
|
char addr[128];
|
||||||
|
char *full;
|
||||||
|
|
||||||
|
if(buildaddrpath(addrfile, sizeof(addrfile)) < 0){
|
||||||
|
fprintf(stderr, "strans: ibus: cannot build address path\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
unlink(addrfile);
|
||||||
|
snprintf(addr, sizeof(addr), "unix:abstract=strans-%d", (int)getpid());
|
||||||
|
dbus_error_init(&err);
|
||||||
|
srv = dbus_server_listen(addr, &err);
|
||||||
|
if(srv == nil){
|
||||||
|
fprintf(stderr, "strans: ibus: listen: %s\n", err.message);
|
||||||
|
dbus_error_free(&err);
|
||||||
|
addrfile[0] = '\0';
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
dbus_server_set_new_connection_function(srv, newconn, nil, nil);
|
||||||
|
dbus_server_set_watch_functions(srv, addwatch, removewatch, togglewatch, nil, nil);
|
||||||
|
full = dbus_server_get_address(srv);
|
||||||
|
if(writeaddr(addrfile, full) < 0){
|
||||||
|
fprintf(stderr, "strans: ibus: cannot write %s\n", addrfile);
|
||||||
|
dbus_free(full);
|
||||||
|
dbus_server_disconnect(srv);
|
||||||
|
dbus_server_unref(srv);
|
||||||
|
srv = nil;
|
||||||
|
addrfile[0] = '\0';
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
dbus_free(full);
|
||||||
|
atexit(unlinkaddr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ibusthread(void *_)
|
||||||
|
{
|
||||||
|
struct pollfd pfds[Maxwatches];
|
||||||
|
int wi[Maxwatches];
|
||||||
|
int i, n, rv;
|
||||||
|
unsigned int f;
|
||||||
|
|
||||||
|
USED(_);
|
||||||
|
threadsetname("ibus");
|
||||||
|
if(ibusinit() < 0)
|
||||||
|
return;
|
||||||
|
for(;;){
|
||||||
|
n = 0;
|
||||||
|
for(i = 0; i < nwatches && n < Maxwatches; i++){
|
||||||
|
if(watches[i].w == nil)
|
||||||
|
continue;
|
||||||
|
if(!dbus_watch_get_enabled(watches[i].w))
|
||||||
|
continue;
|
||||||
|
pfds[n].fd = dbus_watch_get_unix_fd(watches[i].w);
|
||||||
|
pfds[n].events = 0;
|
||||||
|
f = dbus_watch_get_flags(watches[i].w);
|
||||||
|
if(f & DBUS_WATCH_READABLE) pfds[n].events |= POLLIN;
|
||||||
|
if(f & DBUS_WATCH_WRITABLE) pfds[n].events |= POLLOUT;
|
||||||
|
wi[n] = i;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
rv = poll(pfds, n, 200);
|
||||||
|
if(rv < 0)
|
||||||
|
continue;
|
||||||
|
for(i = 0; i < n; i++){
|
||||||
|
if(pfds[i].revents == 0)
|
||||||
|
continue;
|
||||||
|
if(watches[wi[i]].w == nil)
|
||||||
|
continue;
|
||||||
|
f = 0;
|
||||||
|
if(pfds[i].revents & POLLIN) f |= DBUS_WATCH_READABLE;
|
||||||
|
if(pfds[i].revents & POLLOUT) f |= DBUS_WATCH_WRITABLE;
|
||||||
|
if(pfds[i].revents & POLLHUP) f |= DBUS_WATCH_HANGUP;
|
||||||
|
if(pfds[i].revents & POLLERR) f |= DBUS_WATCH_ERROR;
|
||||||
|
dbus_watch_handle(watches[wi[i]].w, f);
|
||||||
|
}
|
||||||
|
for(i = 0; i < nconns; i++)
|
||||||
|
while(dbus_connection_dispatch(conns[i]) == DBUS_DISPATCH_DATA_REMAINS)
|
||||||
|
;
|
||||||
|
pruneconns();
|
||||||
|
}
|
||||||
|
}
|
||||||
327
ko.c
Normal file
327
ko.c
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
#include "dat.h"
|
||||||
|
#include "fn.h"
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
Sbase = 0xAC00,
|
||||||
|
Jbase = 0x3131,
|
||||||
|
Jrange = 51,
|
||||||
|
Ncho = 19,
|
||||||
|
Njung = 21,
|
||||||
|
Njong = 28,
|
||||||
|
};
|
||||||
|
|
||||||
|
static Rune cho[] = {
|
||||||
|
L'ㄱ', L'ㄲ', L'ㄴ', L'ㄷ', L'ㄸ',
|
||||||
|
L'ㄹ', L'ㅁ', L'ㅂ', L'ㅃ', L'ㅅ',
|
||||||
|
L'ㅆ', L'ㅇ', L'ㅈ', L'ㅉ', L'ㅊ',
|
||||||
|
L'ㅋ', L'ㅌ', L'ㅍ', L'ㅎ',
|
||||||
|
};
|
||||||
|
|
||||||
|
static Rune jung[] = {
|
||||||
|
L'ㅏ', L'ㅐ', L'ㅑ', L'ㅒ', L'ㅓ',
|
||||||
|
L'ㅔ', L'ㅕ', L'ㅖ', L'ㅗ', L'ㅘ',
|
||||||
|
L'ㅙ', L'ㅚ', L'ㅛ', L'ㅜ', L'ㅝ',
|
||||||
|
L'ㅞ', L'ㅟ', L'ㅠ', L'ㅡ', L'ㅢ',
|
||||||
|
L'ㅣ',
|
||||||
|
};
|
||||||
|
|
||||||
|
static Rune jong[] = {
|
||||||
|
0, L'ㄱ', L'ㄲ', L'ㄳ', L'ㄴ',
|
||||||
|
L'ㄵ', L'ㄶ', L'ㄷ', L'ㄹ', L'ㄺ',
|
||||||
|
L'ㄻ', L'ㄼ', L'ㄽ', L'ㄾ', L'ㄿ',
|
||||||
|
L'ㅀ', L'ㅁ', L'ㅂ', L'ㅄ', L'ㅅ',
|
||||||
|
L'ㅆ', L'ㅇ', L'ㅈ', L'ㅊ', L'ㅋ',
|
||||||
|
L'ㅌ', L'ㅍ', L'ㅎ',
|
||||||
|
};
|
||||||
|
|
||||||
|
static int choidx[Jrange] = {
|
||||||
|
[L'ㄱ'-Jbase] = 1, [L'ㄲ'-Jbase] = 2, [L'ㄴ'-Jbase] = 3,
|
||||||
|
[L'ㄷ'-Jbase] = 4, [L'ㄸ'-Jbase] = 5, [L'ㄹ'-Jbase] = 6,
|
||||||
|
[L'ㅁ'-Jbase] = 7, [L'ㅂ'-Jbase] = 8, [L'ㅃ'-Jbase] = 9,
|
||||||
|
[L'ㅅ'-Jbase] = 10, [L'ㅆ'-Jbase] = 11, [L'ㅇ'-Jbase] = 12,
|
||||||
|
[L'ㅈ'-Jbase] = 13, [L'ㅉ'-Jbase] = 14, [L'ㅊ'-Jbase] = 15,
|
||||||
|
[L'ㅋ'-Jbase] = 16, [L'ㅌ'-Jbase] = 17, [L'ㅍ'-Jbase] = 18,
|
||||||
|
[L'ㅎ'-Jbase] = 19,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int jungidx[Jrange] = {
|
||||||
|
[L'ㅏ'-Jbase] = 1, [L'ㅐ'-Jbase] = 2, [L'ㅑ'-Jbase] = 3,
|
||||||
|
[L'ㅒ'-Jbase] = 4, [L'ㅓ'-Jbase] = 5, [L'ㅔ'-Jbase] = 6,
|
||||||
|
[L'ㅕ'-Jbase] = 7, [L'ㅖ'-Jbase] = 8, [L'ㅗ'-Jbase] = 9,
|
||||||
|
[L'ㅘ'-Jbase] = 10, [L'ㅙ'-Jbase] = 11, [L'ㅚ'-Jbase] = 12,
|
||||||
|
[L'ㅛ'-Jbase] = 13, [L'ㅜ'-Jbase] = 14, [L'ㅝ'-Jbase] = 15,
|
||||||
|
[L'ㅞ'-Jbase] = 16, [L'ㅟ'-Jbase] = 17, [L'ㅠ'-Jbase] = 18,
|
||||||
|
[L'ㅡ'-Jbase] = 19, [L'ㅢ'-Jbase] = 20, [L'ㅣ'-Jbase] = 21,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int jongidx[Jrange] = {
|
||||||
|
[L'ㄱ'-Jbase] = 2, [L'ㄲ'-Jbase] = 3, [L'ㄳ'-Jbase] = 4,
|
||||||
|
[L'ㄴ'-Jbase] = 5, [L'ㄵ'-Jbase] = 6, [L'ㄶ'-Jbase] = 7,
|
||||||
|
[L'ㄷ'-Jbase] = 8, [L'ㄹ'-Jbase] = 9, [L'ㄺ'-Jbase] = 10,
|
||||||
|
[L'ㄻ'-Jbase] = 11, [L'ㄼ'-Jbase] = 12, [L'ㄽ'-Jbase] = 13,
|
||||||
|
[L'ㄾ'-Jbase] = 14, [L'ㄿ'-Jbase] = 15, [L'ㅀ'-Jbase] = 16,
|
||||||
|
[L'ㅁ'-Jbase] = 17, [L'ㅂ'-Jbase] = 18, [L'ㅄ'-Jbase] = 19,
|
||||||
|
[L'ㅅ'-Jbase] = 20, [L'ㅆ'-Jbase] = 21, [L'ㅇ'-Jbase] = 22,
|
||||||
|
[L'ㅈ'-Jbase] = 23, [L'ㅊ'-Jbase] = 24, [L'ㅋ'-Jbase] = 25,
|
||||||
|
[L'ㅌ'-Jbase] = 26, [L'ㅍ'-Jbase] = 27, [L'ㅎ'-Jbase] = 28,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define Choidx(r) (choidx[(r) - Jbase] - 1)
|
||||||
|
#define Jungidx(r) (jungidx[(r) - Jbase] - 1)
|
||||||
|
#define Jongidx(r) (jongidx[(r) - Jbase] - 1)
|
||||||
|
|
||||||
|
static Rune jamomap[128] = {
|
||||||
|
['r'] = L'ㄱ', ['R'] = L'ㄲ',
|
||||||
|
['s'] = L'ㄴ', ['S'] = L'ㄴ',
|
||||||
|
['e'] = L'ㄷ', ['E'] = L'ㄸ',
|
||||||
|
['f'] = L'ㄹ', ['F'] = L'ㄹ',
|
||||||
|
['a'] = L'ㅁ', ['A'] = L'ㅁ',
|
||||||
|
['q'] = L'ㅂ', ['Q'] = L'ㅃ',
|
||||||
|
['t'] = L'ㅅ', ['T'] = L'ㅆ',
|
||||||
|
['d'] = L'ㅇ', ['D'] = L'ㅇ',
|
||||||
|
['w'] = L'ㅈ', ['W'] = L'ㅉ',
|
||||||
|
['c'] = L'ㅊ', ['C'] = L'ㅊ',
|
||||||
|
['z'] = L'ㅋ', ['Z'] = L'ㅋ',
|
||||||
|
['x'] = L'ㅌ', ['X'] = L'ㅌ',
|
||||||
|
['v'] = L'ㅍ', ['V'] = L'ㅍ',
|
||||||
|
['g'] = L'ㅎ', ['G'] = L'ㅎ',
|
||||||
|
['k'] = L'ㅏ', ['K'] = L'ㅏ',
|
||||||
|
['o'] = L'ㅐ', ['O'] = L'ㅒ',
|
||||||
|
['i'] = L'ㅑ', ['I'] = L'ㅑ',
|
||||||
|
['j'] = L'ㅓ', ['J'] = L'ㅓ',
|
||||||
|
['p'] = L'ㅔ', ['P'] = L'ㅖ',
|
||||||
|
['u'] = L'ㅕ', ['U'] = L'ㅕ',
|
||||||
|
['h'] = L'ㅗ', ['H'] = L'ㅗ',
|
||||||
|
['y'] = L'ㅛ', ['Y'] = L'ㅛ',
|
||||||
|
['n'] = L'ㅜ', ['N'] = L'ㅜ',
|
||||||
|
['b'] = L'ㅠ', ['B'] = L'ㅠ',
|
||||||
|
['m'] = L'ㅡ', ['M'] = L'ㅡ',
|
||||||
|
['l'] = L'ㅣ', ['L'] = L'ㅣ',
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Cpair Cpair;
|
||||||
|
struct Cpair
|
||||||
|
{
|
||||||
|
Rune b;
|
||||||
|
Rune a;
|
||||||
|
Rune r;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Cpair cvow[] = {
|
||||||
|
{L'ㅗ', L'ㅏ', L'ㅘ'},
|
||||||
|
{L'ㅗ', L'ㅐ', L'ㅙ'},
|
||||||
|
{L'ㅗ', L'ㅣ', L'ㅚ'},
|
||||||
|
{L'ㅜ', L'ㅓ', L'ㅝ'},
|
||||||
|
{L'ㅜ', L'ㅔ', L'ㅞ'},
|
||||||
|
{L'ㅜ', L'ㅣ', L'ㅟ'},
|
||||||
|
{L'ㅡ', L'ㅣ', L'ㅢ'},
|
||||||
|
};
|
||||||
|
|
||||||
|
static Cpair cjong[] = {
|
||||||
|
{L'ㄱ', L'ㅅ', L'ㄳ'},
|
||||||
|
{L'ㄴ', L'ㅈ', L'ㄵ'},
|
||||||
|
{L'ㄴ', L'ㅎ', L'ㄶ'},
|
||||||
|
{L'ㄹ', L'ㄱ', L'ㄺ'},
|
||||||
|
{L'ㄹ', L'ㅁ', L'ㄻ'},
|
||||||
|
{L'ㄹ', L'ㅂ', L'ㄼ'},
|
||||||
|
{L'ㄹ', L'ㅅ', L'ㄽ'},
|
||||||
|
{L'ㄹ', L'ㅌ', L'ㄾ'},
|
||||||
|
{L'ㄹ', L'ㅍ', L'ㄿ'},
|
||||||
|
{L'ㄹ', L'ㅎ', L'ㅀ'},
|
||||||
|
{L'ㅂ', L'ㅅ', L'ㅄ'},
|
||||||
|
};
|
||||||
|
|
||||||
|
static Rune
|
||||||
|
keytojamo(int c)
|
||||||
|
{
|
||||||
|
if(c >= 0 && c < 128)
|
||||||
|
return jamomap[c];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Rune
|
||||||
|
combine(Cpair *t, int n, Rune b, Rune a)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < n; i++)
|
||||||
|
if(t[i].b == b && t[i].a == a)
|
||||||
|
return t[i].r;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
splitpair(Cpair *t, int n, Rune c, Rune *stay, Rune *next)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < n; i++){
|
||||||
|
if(t[i].r == c){
|
||||||
|
*stay = t[i].b;
|
||||||
|
*next = t[i].a;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Rune
|
||||||
|
compose(int c, int j, int jo)
|
||||||
|
{
|
||||||
|
return Sbase + c*Njung*Njong + j*Njong + jo;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
decompose(Rune s, int *c, int *j, int *jo)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = s - Sbase;
|
||||||
|
*c = i / (Njung * Njong);
|
||||||
|
*j = (i / Njong) % Njung;
|
||||||
|
*jo = i % Njong;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
issyl(Rune r)
|
||||||
|
{
|
||||||
|
return r >= Sbase && r < Sbase + Ncho*Njung*Njong;
|
||||||
|
}
|
||||||
|
|
||||||
|
Emit
|
||||||
|
transko(Im *im, Rune c)
|
||||||
|
{
|
||||||
|
Emit e;
|
||||||
|
Rune jm, jc, last, comb, stay, next;
|
||||||
|
int ci, ji, joi, ni, si, vi;
|
||||||
|
|
||||||
|
memset(&e, 0, sizeof e);
|
||||||
|
jm = keytojamo(c);
|
||||||
|
if(jm == 0){
|
||||||
|
if(im->pre.n > 0){
|
||||||
|
e.s = im->pre;
|
||||||
|
sclear(&im->pre);
|
||||||
|
}
|
||||||
|
sputr(&e.s, c);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
e.eat = 1;
|
||||||
|
last = slastr(&im->pre);
|
||||||
|
if(last == 0){
|
||||||
|
sputr(&e.next, jm);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!issyl(last)){
|
||||||
|
ci = Choidx(last);
|
||||||
|
ji = Jungidx(jm);
|
||||||
|
if(ci >= 0 && ji >= 0){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.next, compose(ci, ji, 0));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
if(Jungidx(last) >= 0 && ji >= 0){
|
||||||
|
comb = combine(cvow, nelem(cvow), last, jm);
|
||||||
|
if(comb){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.next, comb);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.s = im->pre;
|
||||||
|
sputr(&e.next, jm);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
decompose(last, &ci, &ji, &joi);
|
||||||
|
|
||||||
|
if(joi == 0){
|
||||||
|
ni = Jongidx(jm);
|
||||||
|
if(ni > 0){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.next, compose(ci, ji, ni));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
vi = Jungidx(jm);
|
||||||
|
if(vi >= 0){
|
||||||
|
comb = combine(cvow, nelem(cvow), jung[ji], jm);
|
||||||
|
if(comb){
|
||||||
|
ni = Jungidx(comb);
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.next, compose(ci, ni, 0));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(joi > 0){
|
||||||
|
comb = combine(cjong, nelem(cjong), jong[joi], jm);
|
||||||
|
if(comb){
|
||||||
|
ni = Jongidx(comb);
|
||||||
|
if(ni > 0){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.next, compose(ci, ji, ni));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vi = Jungidx(jm);
|
||||||
|
if(vi >= 0){
|
||||||
|
jc = jong[joi];
|
||||||
|
if(splitpair(cjong, nelem(cjong), jc, &stay, &next)){
|
||||||
|
si = Jongidx(stay);
|
||||||
|
ni = Choidx(next);
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.s, compose(ci, ji, si));
|
||||||
|
sputr(&e.next, compose(ni, vi, 0));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
ni = Choidx(jc);
|
||||||
|
if(ni >= 0){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&e.s, compose(ci, ji, 0));
|
||||||
|
sputr(&e.next, compose(ni, vi, 0));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e.s = im->pre;
|
||||||
|
sputr(&e.next, jm);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
backko(Im *im)
|
||||||
|
{
|
||||||
|
Rune last;
|
||||||
|
int c, j, jo;
|
||||||
|
Rune stay, next;
|
||||||
|
|
||||||
|
last = slastr(&im->pre);
|
||||||
|
if(!issyl(last)){
|
||||||
|
if(splitpair(cvow, nelem(cvow), last, &stay, &next)){
|
||||||
|
spopr(&im->pre);
|
||||||
|
sputr(&im->pre, stay);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
spopr(&im->pre);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
decompose(last, &c, &j, &jo);
|
||||||
|
spopr(&im->pre);
|
||||||
|
if(jo > 0 && splitpair(cjong, nelem(cjong), jong[jo], &stay, &next)){
|
||||||
|
sputr(&im->pre, compose(c, j, Jongidx(stay)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(jo > 0){
|
||||||
|
sputr(&im->pre, compose(c, j, 0));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(splitpair(cvow, nelem(cvow), jung[j], &stay, &next)){
|
||||||
|
sputr(&im->pre, compose(c, Jungidx(stay), 0));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sputr(&im->pre, cho[c]);
|
||||||
|
}
|
||||||
3
main.c
3
main.c
@@ -61,7 +61,7 @@ threadmain(int argc, char **argv)
|
|||||||
usage();
|
usage();
|
||||||
|
|
||||||
fontdir = argv[2];
|
fontdir = argv[2];
|
||||||
drawc = chancreate(sizeof(Drawcmd), 0);
|
drawc = chancreate(sizeof(Drawcmd), 4);
|
||||||
keyc = chancreate(sizeof(Keyreq), 0);
|
keyc = chancreate(sizeof(Keyreq), 0);
|
||||||
dictreqc = chancreate(sizeof(Dictreq), 4);
|
dictreqc = chancreate(sizeof(Dictreq), 4);
|
||||||
dictresc = chancreate(sizeof(Dictres), 0);
|
dictresc = chancreate(sizeof(Dictres), 0);
|
||||||
@@ -69,6 +69,7 @@ threadmain(int argc, char **argv)
|
|||||||
dictinit(argv[1]);
|
dictinit(argv[1]);
|
||||||
proccreate(drawthread, nil, 16384);
|
proccreate(drawthread, nil, 16384);
|
||||||
proccreate(srvthread, nil, 16384);
|
proccreate(srvthread, nil, 16384);
|
||||||
|
proccreate(ibusthread, nil, 32768);
|
||||||
threadcreate(dictthread, nil, 16384);
|
threadcreate(dictthread, nil, 16384);
|
||||||
threadcreate(imthread, nil, 16384);
|
threadcreate(imthread, nil, 16384);
|
||||||
|
|
||||||
|
|||||||
11212
map/hangul.map
11212
map/hangul.map
File diff suppressed because it is too large
Load Diff
2521
map/telex.map
2521
map/telex.map
File diff suppressed because it is too large
Load Diff
86
strans.c
86
strans.c
@@ -2,14 +2,21 @@
|
|||||||
#include "fn.h"
|
#include "fn.h"
|
||||||
|
|
||||||
static Im im;
|
static Im im;
|
||||||
|
static void dictqmap(Im*);
|
||||||
|
|
||||||
|
static void
|
||||||
|
backmap(Im *im)
|
||||||
|
{
|
||||||
|
spopr(&im->pre);
|
||||||
|
}
|
||||||
|
|
||||||
Lang langs[] = {
|
Lang langs[] = {
|
||||||
{LangEN, "english", nil, nil, nil},
|
{LangEN, "english", nil, transmap, backmap, dictqmap, nil, nil},
|
||||||
{LangJP, "hira", "kanji", nil, nil},
|
{LangJP, "hira", "kanji", transmap, backmap, dictqmap, nil, nil},
|
||||||
{LangJPK, "kata", "kanji", nil, nil},
|
{LangJPK, "kata", "kanji", transmap, backmap, dictqmap, nil, nil},
|
||||||
{LangKO, "hangul", nil, nil, nil},
|
{LangKO, "hangul", nil, transko, backko, dictqmap, nil, nil},
|
||||||
{LangEMOJI, "emoji", "emoji", nil, nil},
|
{LangEMOJI, "emoji", "emoji", transmap, backmap, dictqmap, nil, nil},
|
||||||
{LangVI, "telex", nil, nil, nil},
|
{LangVI, "telex", nil, transvi, backmap, dictqmap, nil, nil},
|
||||||
};
|
};
|
||||||
int nlang = nelem(langs);
|
int nlang = nelem(langs);
|
||||||
|
|
||||||
@@ -17,7 +24,7 @@ static void
|
|||||||
clearkouho(void)
|
clearkouho(void)
|
||||||
{
|
{
|
||||||
im.nkouho = 0;
|
im.nkouho = 0;
|
||||||
im.sel = 0;
|
im.sel = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -36,15 +43,18 @@ static void
|
|||||||
show(void)
|
show(void)
|
||||||
{
|
{
|
||||||
Drawcmd dc;
|
Drawcmd dc;
|
||||||
int i;
|
int i, first, n;
|
||||||
|
|
||||||
sclear(&dc.preedit);
|
sclear(&dc.pre);
|
||||||
if(!mapget(im.l->map, &im.pre, &dc.preedit))
|
if(!mapget(im.l->map, &im.pre, &dc.pre))
|
||||||
dc.preedit = im.pre;
|
dc.pre = im.pre;
|
||||||
dc.nkouho = im.nkouho;
|
first = im.sel >= Maxdisp ? im.sel - Maxdisp + 1 : 0;
|
||||||
dc.sel = im.sel;
|
n = im.nkouho - first;
|
||||||
for(i = 0; i < dc.nkouho; i++)
|
if(n > Maxdisp) n = Maxdisp;
|
||||||
dc.kouho[i] = im.kouho[i];
|
dc.nkouho = n;
|
||||||
|
dc.sel = im.sel >= 0 ? im.sel - first : -1;
|
||||||
|
for(i = 0; i < n; i++)
|
||||||
|
dc.kouho[i] = im.kouho[first + i];
|
||||||
chansend(drawc, &dc);
|
chansend(drawc, &dc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,25 +66,32 @@ reset(void)
|
|||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
dictq(void)
|
dictsend(Im *im, Str *key)
|
||||||
{
|
{
|
||||||
Str dict;
|
|
||||||
Dictreq req;
|
Dictreq req;
|
||||||
|
|
||||||
if(!mapget(im.l->map, &im.pre, &dict)){
|
req.key = *key;
|
||||||
|
req.lang = im->l->lang;
|
||||||
|
req.pre = im->pre;
|
||||||
|
channbsend(dictreqc, &req);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dictqmap(Im *im)
|
||||||
|
{
|
||||||
|
Str dict;
|
||||||
|
|
||||||
|
if(!mapget(im->l->map, &im->pre, &dict)){
|
||||||
clearkouho();
|
clearkouho();
|
||||||
show();
|
show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
req.key = dict;
|
dictsend(im, &dict);
|
||||||
req.lang = im.l->lang;
|
|
||||||
req.pre = im.pre;
|
|
||||||
channbsend(dictreqc, &req);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
checklang(int c)
|
setlang(int c)
|
||||||
{
|
{
|
||||||
Lang *l;
|
Lang *l;
|
||||||
|
|
||||||
@@ -88,10 +105,10 @@ checklang(int c)
|
|||||||
static void
|
static void
|
||||||
commit(Str *com)
|
commit(Str *com)
|
||||||
{
|
{
|
||||||
Str kana;
|
Str val;
|
||||||
|
|
||||||
if(mapget(im.l->map, &im.pre, &kana))
|
if(mapget(im.l->map, &im.pre, &val))
|
||||||
sappend(com, &kana);
|
sappend(com, &val);
|
||||||
else
|
else
|
||||||
sappend(com, &im.pre);
|
sappend(com, &im.pre);
|
||||||
sclear(&im.pre);
|
sclear(&im.pre);
|
||||||
@@ -103,7 +120,7 @@ dotrans(Rune c, Str *com)
|
|||||||
Emit e;
|
Emit e;
|
||||||
Dictreq req;
|
Dictreq req;
|
||||||
|
|
||||||
e = transmap(&im, c);
|
e = im.l->trans(&im, c);
|
||||||
if(e.s.n > 0)
|
if(e.s.n > 0)
|
||||||
sappend(com, &e.s);
|
sappend(com, &e.s);
|
||||||
sclear(&im.pre);
|
sclear(&im.pre);
|
||||||
@@ -186,7 +203,7 @@ keystroke(u32int ks, u32int mod, Str *com)
|
|||||||
return 0;
|
return 0;
|
||||||
if(ks == Kdown && im.sel < im.nkouho - 1)
|
if(ks == Kdown && im.sel < im.nkouho - 1)
|
||||||
im.sel++;
|
im.sel++;
|
||||||
if(ks == Kup && im.sel > 0)
|
if(ks == Kup && im.sel >= 0)
|
||||||
im.sel--;
|
im.sel--;
|
||||||
show();
|
show();
|
||||||
return 1;
|
return 1;
|
||||||
@@ -216,12 +233,12 @@ keystroke(u32int ks, u32int mod, Str *com)
|
|||||||
if(ks == Kback){
|
if(ks == Kback){
|
||||||
if(im.pre.n == 0)
|
if(im.pre.n == 0)
|
||||||
return 0;
|
return 0;
|
||||||
spopr(&im.pre);
|
im.l->back(&im);
|
||||||
if(im.pre.n == 0){
|
if(im.pre.n == 0){
|
||||||
reset();
|
reset();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
dictq();
|
im.l->dictq(&im);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(ks == Kesc){
|
if(ks == Kesc){
|
||||||
@@ -239,10 +256,15 @@ keystroke(u32int ks, u32int mod, Str *com)
|
|||||||
reset();
|
reset();
|
||||||
if(ks >= 'a' && ks <= 'z')
|
if(ks >= 'a' && ks <= 'z')
|
||||||
ks -= 'a' - 1;
|
ks -= 'a' - 1;
|
||||||
if(checklang(ks))
|
if(setlang(ks))
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if(ks == '0' && im.nkouho > 0){
|
||||||
|
commit(com);
|
||||||
|
reset();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if(ks > 0x7f || ks == ' '){
|
if(ks > 0x7f || ks == ' '){
|
||||||
commit(com);
|
commit(com);
|
||||||
sputr(com, ks);
|
sputr(com, ks);
|
||||||
|
|||||||
160
vi.c
Normal file
160
vi.c
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
#include "dat.h"
|
||||||
|
#include "fn.h"
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
Rune base;
|
||||||
|
Rune tone[5]; /* s f r x j */
|
||||||
|
} vitone[] = {
|
||||||
|
{L'a', {L'á', L'à', L'ả', L'ã', L'ạ'}},
|
||||||
|
{L'â', {L'ấ', L'ầ', L'ẩ', L'ẫ', L'ậ'}},
|
||||||
|
{L'ă', {L'ắ', L'ằ', L'ẳ', L'ẵ', L'ặ'}},
|
||||||
|
{L'e', {L'é', L'è', L'ẻ', L'ẽ', L'ẹ'}},
|
||||||
|
{L'ê', {L'ế', L'ề', L'ể', L'ễ', L'ệ'}},
|
||||||
|
{L'i', {L'í', L'ì', L'ỉ', L'ĩ', L'ị'}},
|
||||||
|
{L'o', {L'ó', L'ò', L'ỏ', L'õ', L'ọ'}},
|
||||||
|
{L'ô', {L'ố', L'ồ', L'ổ', L'ỗ', L'ộ'}},
|
||||||
|
{L'ơ', {L'ớ', L'ờ', L'ở', L'ỡ', L'ợ'}},
|
||||||
|
{L'u', {L'ú', L'ù', L'ủ', L'ũ', L'ụ'}},
|
||||||
|
{L'ư', {L'ứ', L'ừ', L'ử', L'ữ', L'ự'}},
|
||||||
|
{L'y', {L'ý', L'ỳ', L'ỷ', L'ỹ', L'ỵ'}},
|
||||||
|
{L'A', {L'Á', L'À', L'Ả', L'Ã', L'Ạ'}},
|
||||||
|
{L'Â', {L'Ấ', L'Ầ', L'Ẩ', L'Ẫ', L'Ậ'}},
|
||||||
|
{L'Ă', {L'Ắ', L'Ằ', L'Ẳ', L'Ẵ', L'Ặ'}},
|
||||||
|
{L'E', {L'É', L'È', L'Ẻ', L'Ẽ', L'Ẹ'}},
|
||||||
|
{L'Ê', {L'Ế', L'Ề', L'Ể', L'Ễ', L'Ệ'}},
|
||||||
|
{L'I', {L'Í', L'Ì', L'Ỉ', L'Ĩ', L'Ị'}},
|
||||||
|
{L'O', {L'Ó', L'Ò', L'Ỏ', L'Õ', L'Ọ'}},
|
||||||
|
{L'Ô', {L'Ố', L'Ồ', L'Ổ', L'Ỗ', L'Ộ'}},
|
||||||
|
{L'Ơ', {L'Ớ', L'Ờ', L'Ở', L'Ỡ', L'Ợ'}},
|
||||||
|
{L'U', {L'Ú', L'Ù', L'Ủ', L'Ũ', L'Ụ'}},
|
||||||
|
{L'Ư', {L'Ứ', L'Ừ', L'Ử', L'Ữ', L'Ự'}},
|
||||||
|
{L'Y', {L'Ý', L'Ỳ', L'Ỷ', L'Ỹ', L'Ỵ'}},
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
istone(Rune c)
|
||||||
|
{
|
||||||
|
return c == 's' || c == 'f' || c == 'r' || c == 'x' || c == 'j';
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
toneidx(Rune c)
|
||||||
|
{
|
||||||
|
switch(c){
|
||||||
|
case 's': return 0;
|
||||||
|
case 'f': return 1;
|
||||||
|
case 'r': return 2;
|
||||||
|
case 'x': return 3;
|
||||||
|
case 'j': return 4;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
isvowel(Rune c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < nelem(vitone); i++)
|
||||||
|
if(vitone[i].base == c)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Rune
|
||||||
|
removetone(Rune c)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for(i = 0; i < nelem(vitone); i++){
|
||||||
|
if(vitone[i].base == c)
|
||||||
|
return c;
|
||||||
|
for(j = 0; j < 5; j++)
|
||||||
|
if(vitone[i].tone[j] == c)
|
||||||
|
return vitone[i].base;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Rune
|
||||||
|
applytone(Rune c, int tidx)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
Rune base;
|
||||||
|
|
||||||
|
base = removetone(c);
|
||||||
|
for(i = 0; i < nelem(vitone); i++)
|
||||||
|
if(vitone[i].base == base)
|
||||||
|
return vitone[i].tone[tidx];
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
Emit
|
||||||
|
transvi(Im *im, Rune c)
|
||||||
|
{
|
||||||
|
Emit e;
|
||||||
|
Str mapped, pre;
|
||||||
|
int i, tidx, vi, last, penult;
|
||||||
|
Rune v, b1, b2;
|
||||||
|
|
||||||
|
if(!istone(c) && c != 'z')
|
||||||
|
return transmap(im, c);
|
||||||
|
|
||||||
|
memset(&e, 0, sizeof e);
|
||||||
|
if(im->pre.n == 0){
|
||||||
|
sputr(&e.s, c);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
if(im->pre.r[im->pre.n - 1] == '\\'){
|
||||||
|
pre = im->pre;
|
||||||
|
pre.n--;
|
||||||
|
if(!mapget(im->l->map, &pre, &mapped))
|
||||||
|
mapped = pre;
|
||||||
|
sputr(&mapped, c);
|
||||||
|
e.eat = 1;
|
||||||
|
e.s = mapped;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
if(!mapget(im->l->map, &im->pre, &mapped))
|
||||||
|
mapped = im->pre;
|
||||||
|
last = -1;
|
||||||
|
penult = -1;
|
||||||
|
for(i = 0; i < mapped.n; i++){
|
||||||
|
v = removetone(mapped.r[i]);
|
||||||
|
if(isvowel(v)){
|
||||||
|
penult = last;
|
||||||
|
last = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vi = -1;
|
||||||
|
if(last >= 0){
|
||||||
|
if(last < mapped.n - 1 || penult < 0)
|
||||||
|
vi = last;
|
||||||
|
else{
|
||||||
|
b1 = removetone(mapped.r[penult]);
|
||||||
|
b2 = removetone(mapped.r[last]);
|
||||||
|
if((b1 == 'o' || b1 == 'O') && (b2 == 'a' || b2 == 'A' || b2 == 'e' || b2 == 'E'))
|
||||||
|
vi = last;
|
||||||
|
else if((b1 == 'u' || b1 == 'U') && (b2 == 'y' || b2 == 'Y'))
|
||||||
|
vi = last;
|
||||||
|
else
|
||||||
|
vi = penult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(vi < 0){
|
||||||
|
e.eat = 1;
|
||||||
|
e.s = mapped;
|
||||||
|
sputr(&e.s, c);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c == 'z')
|
||||||
|
mapped.r[vi] = removetone(mapped.r[vi]);
|
||||||
|
else{
|
||||||
|
tidx = toneidx(c);
|
||||||
|
mapped.r[vi] = applytone(mapped.r[vi], tidx);
|
||||||
|
}
|
||||||
|
e.eat = 1;
|
||||||
|
e.next = mapped;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
23
win.c
23
win.c
@@ -71,9 +71,11 @@ drawkouho(Drawcmd *dc, int first, int n, int w, int h)
|
|||||||
Str *s;
|
Str *s;
|
||||||
|
|
||||||
memset(img, (uchar)Colbg, w * h * sizeof(u32int));
|
memset(img, (uchar)Colbg, w * h * sizeof(u32int));
|
||||||
drawstr(img, 0, 0, dc->preedit.r, dc->preedit.n, w, h);
|
drawstr(img, 0, 0, dc->pre.r, dc->pre.n, w, h);
|
||||||
sely = Fontsz + (dc->sel - first) * Fontsz;
|
if(dc->sel >= 0){
|
||||||
memset(img + sely * w, (uchar)Colsel, Fontsz * w * sizeof(u32int));
|
sely = Fontsz + dc->sel * Fontsz;
|
||||||
|
memset(img + sely * w, (uchar)Colsel, Fontsz * w * sizeof(u32int));
|
||||||
|
}
|
||||||
for(i = 0, y = Fontsz; i < n; i++, y += Fontsz){
|
for(i = 0, y = Fontsz; i < n; i++, y += Fontsz){
|
||||||
s = &dc->kouho[first+i];
|
s = &dc->kouho[first+i];
|
||||||
putfont(img, w, h, 0, y, '1' + i + Asciitofull);
|
putfont(img, w, h, 0, y, '1' + i + Asciitofull);
|
||||||
@@ -99,17 +101,16 @@ winhide(void)
|
|||||||
static void
|
static void
|
||||||
winshow(Drawcmd *dc)
|
winshow(Drawcmd *dc)
|
||||||
{
|
{
|
||||||
int px, py, w, h, i, n, first, maxw;
|
int px, py, w, h, i, n, maxw;
|
||||||
u32int vals[4];
|
u32int vals[4];
|
||||||
xcb_query_pointer_reply_t *ptr;
|
xcb_query_pointer_reply_t *ptr;
|
||||||
xcb_query_pointer_cookie_t cookie;
|
xcb_query_pointer_cookie_t cookie;
|
||||||
|
|
||||||
cookie = xcb_query_pointer(conn, scr->root);
|
cookie = xcb_query_pointer(conn, scr->root);
|
||||||
first = dc->sel >= Maxdisp ? dc->sel - Maxdisp + 1 : 0;
|
n = dc->nkouho;
|
||||||
n = min(dc->nkouho - first, Maxdisp);
|
maxw = dc->pre.n;
|
||||||
maxw = dc->preedit.n;
|
|
||||||
for(i = 0; i < n; i++)
|
for(i = 0; i < n; i++)
|
||||||
maxw = max(maxw, dc->kouho[first+i].n);
|
maxw = max(maxw, dc->kouho[i].n);
|
||||||
ptr = xcb_query_pointer_reply(conn, cookie, nil);
|
ptr = xcb_query_pointer_reply(conn, cookie, nil);
|
||||||
if(ptr == nil)
|
if(ptr == nil)
|
||||||
die("xcb_query_pointer");
|
die("xcb_query_pointer");
|
||||||
@@ -125,7 +126,7 @@ winshow(Drawcmd *dc)
|
|||||||
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
|
||||||
vals);
|
vals);
|
||||||
xcb_map_window(conn, win);
|
xcb_map_window(conn, win);
|
||||||
drawkouho(dc, first, n, w, h);
|
drawkouho(dc, 0, n, w, h);
|
||||||
putimage(w, h);
|
putimage(w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +138,9 @@ drawthread(void*)
|
|||||||
threadsetname("draw");
|
threadsetname("draw");
|
||||||
wininit();
|
wininit();
|
||||||
while(chanrecv(drawc, &dc) > 0){
|
while(chanrecv(drawc, &dc) > 0){
|
||||||
if(dc.nkouho == 0 && dc.preedit.n == 0)
|
while(channbrecv(drawc, &dc) > 0)
|
||||||
|
;
|
||||||
|
if(dc.nkouho == 0 && dc.pre.n == 0)
|
||||||
winhide();
|
winhide();
|
||||||
else
|
else
|
||||||
winshow(&dc);
|
winshow(&dc);
|
||||||
|
|||||||
Reference in New Issue
Block a user