~eliasnaur/gio-patches

app/internal/window: (X11) Add support for UTF-8 window title. v1 PROPOSED

Denis Bernard: 1
 app/internal/window: (X11) Add support for UTF-8 window title.

 1 files changed, 32 insertions(+), 3 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/9035/mbox | git am -3
Learn more about email & git

[PATCH] app/internal/window: (X11) Add support for UTF-8 window title. Export this patch

Signed-off-by: Denis Bernard <db047h@gmail.com>
---
 app/internal/window/os_x11.go | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/app/internal/window/os_x11.go b/app/internal/window/os_x11.go
index d820360..7223199 100644
--- a/app/internal/window/os_x11.go
+++ b/app/internal/window/os_x11.go
@@ -62,6 +62,7 @@ type x11Window struct {
 	x  *C.Display
 	xw C.Window
 
+	atoms       map[string]C.Atom
 	evDelWindow C.Atom
 	stage       system.Stage
 	cfg         config
@@ -198,6 +199,26 @@ func (w *x11Window) destroy() {
 	C.XCloseDisplay(w.x)
 }
 
+// atom is a wrapper around XInternAtom that caches the results in order to
+// limit conversions/allocations from Go strings to C.
+//
+func (w *x11Window) atom(name string, onlyIfExists bool) C.Atom {
+	if a, ok := w.atoms[name]; ok {
+		return a
+	}
+	cname := C.CString(name)
+	flag := C.Bool(C.False)
+	if onlyIfExists {
+		flag = C.True
+	}
+	a := C.XInternAtom(w.x, cname, flag)
+	C.free(unsafe.Pointer(cname))
+	if a != C.None {
+		w.atoms[name] = a
+	}
+	return a
+}
+
 // x11EventHandler wraps static variables for the main event loop.
 // Its sole purpose is to prevent heap allocation and reduce clutter
 // in x11window.loop.
@@ -503,6 +524,7 @@ func newX11Window(gioWin Callbacks, opts *Options) error {
 		w: gioWin, x: dpy, xw: win,
 		width:  cfg.Px(opts.Width),
 		height: cfg.Px(opts.Height),
+		atoms:  make(map[string]C.Atom),
 		cfg:    cfg,
 		xim:    xim,
 		xic:    xic,
@@ -521,12 +543,19 @@ func newX11Window(gioWin Callbacks, opts *Options) error {
 	// set the name
 	ctitle := C.CString(opts.Title)
 	C.XStoreName(dpy, win, ctitle)
+	// set _NET_WM_NAME as well for UTF-8 support in window title.
+	C.XSetTextProperty(dpy, win,
+		&C.XTextProperty{
+			value:    (*C.uchar)(unsafe.Pointer(ctitle)),
+			encoding: w.atom("UTF8_STRING", false),
+			format:   8,
+			nitems:   C.ulong(len(opts.Title)),
+		},
+		w.atom("_NET_WM_NAME", false))
 	C.free(unsafe.Pointer(ctitle))
 
 	// extensions
-	ckey := C.CString("WM_DELETE_WINDOW")
-	w.evDelWindow = C.XInternAtom(dpy, ckey, C.False)
-	C.free(unsafe.Pointer(ckey))
+	w.evDelWindow = w.atom("WM_DELETE_WINDOW", false)
 	C.XSetWMProtocols(dpy, win, &w.evDelWindow, 1)
 
 	go func() {
-- 
2.17.1
View this thread in the archives