[hackers] [st] better Input Method Editor (IME) support || Ivan Tham

From: <git_AT_suckless.org>
Date: Tue, 12 Feb 2019 18:46:20 +0100 (CET)

commit e85b6b64660214121164ea97fb098eaa4935f7db
Author: Ivan Tham <pickfire_AT_riseup.net>
AuthorDate: Tue Feb 12 18:41:41 2019 +0100
Commit: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
CommitDate: Tue Feb 12 18:45:15 2019 +0100

    better Input Method Editor (IME) support
    
    Features:
    
    - Allow input methods swap with hotkey (E.g. left ctrl + left shift).
    - Over-the-spot pre-editing style, pre-edit data placed over insertion point.
    - Restart IME without segmentation fault.
    
    TODO:
    
    - Automatically pickup IME if st started before IME

diff --git a/st.c b/st.c
index b8e6077..cf8687e 100644
--- a/st.c
+++ b/st.c
_AT_@ -2594,6 +2594,7 @@ draw(void)
                         term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
         term.ocx = cx, term.ocy = term.c.y;
         xfinishdraw();
+ xximspot(term.ocx, term.ocy);
 }
 
 void
diff --git a/win.h b/win.h
index 31f327d..a6ef1b9 100644
--- a/win.h
+++ b/win.h
_AT_@ -36,3 +36,4 @@ void xsetmode(int, unsigned int);
 void xsetpointermotion(int);
 void xsetsel(char *);
 int xstartdraw(void);
+void xximspot(int, int);
diff --git a/x.c b/x.c
index 0422421..865dacc 100644
--- a/x.c
+++ b/x.c
_AT_@ -139,6 +139,9 @@ static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
 static void xdrawglyph(Glyph, int, int);
 static void xclear(int, int, int, int);
 static int xgeommasktogravity(int);
+static void ximopen(Display *);
+static void ximinstantiate(Display *, XPointer, XPointer);
+static void ximdestroy(XIM, XPointer, XPointer);
 static void xinit(int, int);
 static void cresize(int, int);
 static void xresize(int, int);
_AT_@ -996,6 +999,43 @@ xunloadfonts(void)
         xunloadfont(&dc.ibfont);
 }
 
+void
+ximopen(Display *dpy)
+{
+ XIMCallback destroy = { .client_data = NULL, .callback = ximdestroy };
+
+ if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
+ XSetLocaleModifiers("_AT_im=local");
+ if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
+ XSetLocaleModifiers("_AT_im=");
+ if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL)
+ die("XOpenIM failed. Could not open input device.\n");
+ }
+ }
+ if (XSetIMValues(xw.xim, XNDestroyCallback, &destroy, NULL) != NULL)
+ die("XSetIMValues failed. Could not set input method value.\n");
+ xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
+ XNClientWindow, xw.win, XNFocusWindow, xw.win, NULL);
+ if (xw.xic == NULL)
+ die("XCreateIC failed. Could not obtain input method.\n");
+}
+
+void
+ximinstantiate(Display *dpy, XPointer client, XPointer call)
+{
+ ximopen(dpy);
+ XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
+ ximinstantiate, NULL);
+}
+
+void
+ximdestroy(XIM xim, XPointer client, XPointer call)
+{
+ xw.xim = NULL;
+ XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL,
+ ximinstantiate, NULL);
+}
+
 void
 xinit(int cols, int rows)
 {
_AT_@ -1033,7 +1073,7 @@ xinit(int cols, int rows)
         xw.attrs.background_pixel = dc.col[defaultbg].pixel;
         xw.attrs.border_pixel = dc.col[defaultbg].pixel;
         xw.attrs.bit_gravity = NorthWestGravity;
- xw.attrs.event_mask = FocusChangeMask | KeyPressMask
+ xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
                 | ExposureMask | VisibilityChangeMask | StructureNotifyMask
                 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
         xw.attrs.colormap = xw.cmap;
_AT_@ -1061,22 +1101,7 @@ xinit(int cols, int rows)
         xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
 
         /* input methods */
- if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
- XSetLocaleModifiers("_AT_im=local");
- if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
- XSetLocaleModifiers("_AT_im=");
- if ((xw.xim = XOpenIM(xw.dpy,
- NULL, NULL, NULL)) == NULL) {
- die("XOpenIM failed. Could not open input"
- " device.\n");
- }
- }
- }
- xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
- | XIMStatusNothing, XNClientWindow, xw.win,
- XNFocusWindow, xw.win, NULL);
- if (xw.xic == NULL)
- die("XCreateIC failed. Could not obtain input method.\n");
+ ximopen(xw.dpy);
 
         /* white cursor, black outline */
         cursor = XCreateFontCursor(xw.dpy, mouseshape);
_AT_@ -1554,6 +1579,16 @@ xfinishdraw(void)
                                 defaultfg : defaultbg].pixel);
 }
 
+void
+xximspot(int x, int y)
+{
+ XPoint spot = { borderpx + x * win.cw, borderpx + (y + 1) * win.ch };
+ XVaNestedList attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
+
+ XSetICValues(xw.xic, XNPreeditAttributes, attr, NULL);
+ XFree(attr);
+}
+
 void
 expose(XEvent *ev)
 {
Received on Tue Feb 12 2019 - 18:46:20 CET

This archive was generated by hypermail 2.3.0 : Tue Feb 12 2019 - 18:48:21 CET