diff --git a/src/ibus-bamboo/engine.go b/src/ibus-bamboo/engine.go index a9156af4..c6240c39 100644 --- a/src/ibus-bamboo/engine.go +++ b/src/ibus-bamboo/engine.go @@ -72,7 +72,6 @@ Return: This function gets called whenever a key is pressed. */ func (e *IBusBambooEngine) ProcessKeyEvent(keyVal uint32, keyCode uint32, state uint32) (bool, *dbus.Error) { - e.checkWmClass() if e.checkInputMode(usIM) { if e.isInputModeLTOpened || keyVal == IBusOpenLookupTable { // return false, nil @@ -119,17 +118,21 @@ func (e *IBusBambooEngine) ProcessKeyEvent(keyVal uint32, keyCode uint32, state func (e *IBusBambooEngine) FocusIn() *dbus.Error { log.Print("FocusIn.") - fmt.Printf("WM_CLASS=(%s)\n", e.getWmClass()) - - e.checkWmClass() + var latestWm string + if isGnome && isGnomeOverviewVisible() { + latestWm = "" + } else { + latestWm = e.getLatestWmClass() + } + e.checkWmClass(latestWm) e.RegisterProperties(e.propList) e.RequireSurroundingText() + fmt.Printf("WM_CLASS=(%s)\n", e.getWmClass()) return nil } func (e *IBusBambooEngine) FocusOut() *dbus.Error { log.Print("FocusOut.") - // e.checkWmClass() return nil } @@ -144,13 +147,11 @@ func (e *IBusBambooEngine) Reset() *dbus.Error { func (e *IBusBambooEngine) Enable() *dbus.Error { fmt.Print("Enable.") e.RequireSurroundingText() - startInputWatching() return nil } func (e *IBusBambooEngine) Disable() *dbus.Error { fmt.Print("Disable.") - stopInputWatching() return nil } diff --git a/src/ibus-bamboo/engine_utils.go b/src/ibus-bamboo/engine_utils.go index aa1f828c..d69cd48d 100644 --- a/src/ibus-bamboo/engine_utils.go +++ b/src/ibus-bamboo/engine_utils.go @@ -131,9 +131,9 @@ func (e *IBusBambooEngine) resetBuffer() { } } -func (e *IBusBambooEngine) checkWmClass() { - if e.wmClasses != e.getWmClass() { - e.wmClasses = e.getWmClass() +func (e *IBusBambooEngine) checkWmClass(newId string) { + if e.wmClasses != newId { + e.wmClasses = newId e.resetBuffer() e.resetFakeBackspace() } @@ -238,7 +238,7 @@ func (e *IBusBambooEngine) openLookupTable() { } func (e *IBusBambooEngine) ltProcessKeyEvent(keyVal uint32, keyCode uint32, state uint32) (bool, *dbus.Error) { - var wmClasses = x11GetFocusWindowClass() + var wmClasses = e.getWmClass() //e.HideLookupTable() fmt.Printf("keyCode 0x%04x keyval 0x%04x | %c\n", keyCode, keyVal, rune(keyVal)) //e.HideAuxiliaryText() @@ -378,10 +378,14 @@ func (e *IBusBambooEngine) inBrowserList() bool { } func (e *IBusBambooEngine) getWmClass() string { + return e.wmClasses +} + +func (e *IBusBambooEngine) getLatestWmClass() string { var wmClass string if isWayland { if isGnome { - wmClass = gnomeGetFocusWindowClass() + wmClass, _ = gnomeGetFocusWindowClass() } else { wmClass = wlAppId } diff --git a/src/ibus-bamboo/gnome_introspect.go b/src/ibus-bamboo/gnome_introspect.go deleted file mode 100644 index f7068214..00000000 --- a/src/ibus-bamboo/gnome_introspect.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "github.com/godbus/dbus" -) - -func gnomeGetFocusWindowClass() string { - conn, err := dbus.SessionBus() - if err != nil { - panic(err) - } - defer conn.Close() - - js_code := ` - global._ib_current_window = () => { - var window_list = global.get_window_actors(); - var active_window_actor = window_list.find(window => window.meta_window.has_focus()); - var active_window = active_window_actor.get_meta_window(); - var vm_class = active_window.get_wm_class(); - var title = active_window.get_title(); - var result = vm_class; - return result; - } - ` - obj := conn.Object("org.gnome.Shell", "/org/gnome/Shell") - call := obj.Call("org.gnome.Shell.Eval", 0, js_code) - if call.Err != nil { - panic(call.Err) - } - var s string - var ok bool - err = obj.Call("org.gnome.Shell.Eval", 0, "global._ib_current_window()").Store(&ok, &s) - if (err != nil) { - panic(err) - } - return s -} diff --git a/src/ibus-bamboo/gnome_introspector.go b/src/ibus-bamboo/gnome_introspector.go new file mode 100644 index 00000000..8adf5b4c --- /dev/null +++ b/src/ibus-bamboo/gnome_introspector.go @@ -0,0 +1,54 @@ +package main + +import ( + "errors" + + "github.com/godbus/dbus" +) + +func gnomeGetFocusWindowClass() ( string, error ) { + conn, err := dbus.SessionBus() + var s string + if err != nil { + return s, err + } + defer func() { + if err = conn.Hello(); err == nil { + conn.Close() + } + }() + + js_code := "global.get_window_actors().find(window => window.meta_window.has_focus()).get_meta_window().get_wm_class()" + obj := conn.Object("org.gnome.Shell", "/org/gnome/Shell") + var ok bool + err = obj.Call("org.gnome.Shell.Eval", 0, js_code).Store(&ok, &s) + if !ok { + err = errors.New(s) + } + if (err != nil) { + return "", err + } + return s, nil +} + +func isGnomeOverviewVisible() ( bool ) { + conn, err := dbus.SessionBus() + if err != nil { + return false + } + defer func() { + if err = conn.Hello(); err == nil { + conn.Close() + } + }() + + js_code := "Main.overview.visible" + obj := conn.Object("org.gnome.Shell", "/org/gnome/Shell") + var visible string + var ok bool + err = obj.Call("org.gnome.Shell.Eval", 0, js_code).Store(&ok, &visible) + if !ok || err != nil { + return false + } + return visible == "true" +} diff --git a/src/ibus-bamboo/wl_introspect.go b/src/ibus-bamboo/wl_introspector.go similarity index 100% rename from src/ibus-bamboo/wl_introspect.go rename to src/ibus-bamboo/wl_introspector.go diff --git a/src/ibus-bamboo/x11.go b/src/ibus-bamboo/x11.go index 99038825..99c7b201 100644 --- a/src/ibus-bamboo/x11.go +++ b/src/ibus-bamboo/x11.go @@ -40,8 +40,8 @@ extern void x11SendShiftR(); extern void x11SendShiftLeft(int n, int r, int timeout); extern void setXIgnoreErrorHandler(); extern char* x11GetFocusWindowClass(); -extern void start_input_watching(); -extern void stop_input_watching(); +extern void x11StartWindowInspector(); +extern void x11StopWindowInspector(); */ import "C" import ( @@ -65,12 +65,12 @@ func mouse_click_handler() { var onMouseMove func() var onMouseClick func() -func startInputWatching() { - C.start_input_watching() +func x11StartWindowInspector() { + C.x11StartWindowInspector() } -func stopInputWatching() { - C.stop_input_watching() +func x11StopWindowInspector() { + C.x11StopWindowInspector() } func startMouseRecording() { diff --git a/src/ibus-bamboo/x11_introspect.c b/src/ibus-bamboo/x11_introspector.c similarity index 95% rename from src/ibus-bamboo/x11_introspect.c rename to src/ibus-bamboo/x11_introspector.c index a9344d91..c6bff7e8 100644 --- a/src/ibus-bamboo/x11_introspect.c +++ b/src/ibus-bamboo/x11_introspector.c @@ -97,7 +97,11 @@ char * x11GetFocusWindowClassByDpy(Display *display) { } char * x11GetFocusWindowClass() { - return text; + Display * dpy; + dpy = XOpenDisplay(NULL); + char * wm = x11GetFocusWindowClassByDpy(dpy); + XCloseDisplay(dpy); + return wm; } static int input_watching = 0; @@ -143,11 +147,12 @@ static void* thread_input_watching(void* data) } input_watching = 0; th_count--; + free(text); XCloseDisplay(dpy); return NULL; } -void start_input_watching() +void x11StartWindowInspector() { setbuf(stdout, NULL); setbuf(stderr, NULL); @@ -161,6 +166,6 @@ void start_input_watching() pthread_detach(th_input_watch); } -void stop_input_watching() { +void x11StopWindowInspector() { input_watching = 0; }