Hiljto, sorry for the wait. I had sent a reply to the thread but somehow
it does not appear on the list. I am not sure how to reply to an email
that I have deleted so I just send a new patch with the description.

On Tue, Jan 29, 2019 at 01:41:13PM +0800, Ivan Tham wrote:
Register XNDestroyCallback on IME to reinstatiate XIM.

This does not solve:
- IME not usable if st started before IME
- st goes blank with two st in a dwm tag after restarting IME, might be
 dwm bug as it is happens to other terminal as well

Steps to reproduce (using dwm):
1. Open `fcitx`
2. Open `st`
3. Close `fcitx`
4. `st` crashed
---
x.c | 61 +++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 45 insertions(+), 16 deletions(-)

diff --git a/x.c b/x.c
index 0422421..e5ae6a2 100644
--- a/x.c
+++ b/x.c
@@ -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);
@@ -996,6 +999,47 @@ 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("@im=local");
+               if ((xw.xim =  XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
+                       XSetLocaleModifiers("@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)
{
@@ -1061,22 +1105,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("@im=local");
-               if ((xw.xim =  XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
-                       XSetLocaleModifiers("@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);
--
2.20.1



--
Do what you like, like what you do.  -- Pickfire

Reply via email to