]> Sergey Matveev's repositories - st.git/blobdiff - x.c
fix possible rare crash when Xutf8TextPropertyToTextList fails
[st.git] / x.c
diff --git a/x.c b/x.c
index cbbd11f0c407dadb615f8ba10ce46a92e9312c7f..89786b8bd7a5db38ab008c2d7287b49e86e01d60 100644 (file)
--- a/x.c
+++ b/x.c
@@ -48,7 +48,7 @@ typedef struct {
 /* X modifiers */
 #define XK_ANY_MOD    UINT_MAX
 #define XK_NO_MOD     0
-#define XK_SWITCH_MOD (1<<13)
+#define XK_SWITCH_MOD (1<<13|1<<14)
 
 /* function definitions used in config.h */
 static void clipcopy(const Arg *);
@@ -93,7 +93,7 @@ typedef struct {
        Window win;
        Drawable buf;
        GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
-       Atom xembed, wmdeletewin, netwmname, netwmpid;
+       Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
        struct {
                XIM xim;
                XIC xic;
@@ -156,7 +156,7 @@ static void xresize(int, int);
 static void xhints(void);
 static int xloadcolor(int, const char *, Color *);
 static int xloadfont(Font *, FcPattern *);
-static void xloadfonts(char *, double);
+static void xloadfonts(const char *, double);
 static void xunloadfont(Font *);
 static void xunloadfonts(void);
 static void xsetenv(void);
@@ -387,7 +387,9 @@ mousereport(XEvent *e)
                        button = 3;
                } else {
                        button -= Button1;
-                       if (button >= 3)
+                       if (button >= 7)
+                               button += 128 - 7;
+                       else if (button >= 3)
                                button += 64 - 3;
                }
                if (e->xbutton.type == ButtonPress) {
@@ -950,7 +952,7 @@ xloadfont(Font *f, FcPattern *pattern)
 }
 
 void
-xloadfonts(char *fontstr, double fontsize)
+xloadfonts(const char *fontstr, double fontsize)
 {
        FcPattern *pattern;
        double fontval;
@@ -958,7 +960,7 @@ xloadfonts(char *fontstr, double fontsize)
        if (fontstr[0] == '-')
                pattern = XftXlfdParse(fontstr, False, False);
        else
-               pattern = FcNameParse((FcChar8 *)fontstr);
+               pattern = FcNameParse((const FcChar8 *)fontstr);
 
        if (!pattern)
                die("can't open font %s\n", fontstr);
@@ -1186,6 +1188,7 @@ xinit(int cols, int rows)
        xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
        xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
        xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
+       xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False);
        XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
 
        xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
@@ -1526,8 +1529,9 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
        /* draw the new one */
        if (IS_SET(MODE_FOCUSED)) {
                switch (win.cursor) {
-               case 7: /* st extension: snowman (U+2603) */
-                       g.u = 0x2603;
+               case 7: /* st extension */
+                       g.u = 0x2603; /* snowman (U+2603) */
+                       /* FALLTHROUGH */
                case 0: /* Blinking Block */
                case 1: /* Blinking Block (Default) */
                case 2: /* Steady Block */
@@ -1578,14 +1582,29 @@ xsetenv(void)
        setenv("WINDOWID", buf, 1);
 }
 
+void
+xseticontitle(char *p)
+{
+       XTextProperty prop;
+       DEFAULT(p, opt_title);
+
+       if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
+                                       &prop) != Success)
+               return;
+       XSetWMIconName(xw.dpy, xw.win, &prop);
+       XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmiconname);
+       XFree(prop.value);
+}
+
 void
 xsettitle(char *p)
 {
        XTextProperty prop;
        DEFAULT(p, opt_title);
 
-       Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
-                       &prop);
+       if (Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
+                                       &prop) != Success)
+               return;
        XSetWMName(xw.dpy, xw.win, &prop);
        XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
        XFree(prop.value);
@@ -1689,8 +1708,7 @@ xsetmode(int set, unsigned int flags)
 int
 xsetcursor(int cursor)
 {
-       DEFAULT(cursor, 1);
-       if (!BETWEEN(cursor, 0, 6))
+       if (!BETWEEN(cursor, 0, 7)) /* 7: st extension */
                return 1;
        win.cursor = cursor;
        return 0;
@@ -1927,7 +1945,7 @@ run(void)
                 * triggers drawing, we first wait a bit to ensure we got
                 * everything, and if nothing new arrives - we draw.
                 * We start with trying to wait minlatency ms. If more content
-                * arrives sooner, we retry with shorter and shorter preiods,
+                * arrives sooner, we retry with shorter and shorter periods,
                 * and eventually draw even without idle after maxlatency ms.
                 * Typically this results in low latency while interacting,
                 * maximum latency intervals during `cat huge.txt`, and perfect
@@ -1982,7 +2000,7 @@ main(int argc, char *argv[])
 {
        xw.l = xw.t = 0;
        xw.isfixed = False;
-       win.cursor = cursorshape;
+       xsetcursor(cursorshape);
 
        ARGBEGIN {
        case 'a':