Skip to content

Commit

Permalink
Fix focus window issue on Gnome Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
luongthanhlam committed Mar 13, 2021
1 parent e093616 commit cc6a758
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 58 deletions.
15 changes: 8 additions & 7 deletions src/ibus-bamboo/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
14 changes: 9 additions & 5 deletions src/ibus-bamboo/engine_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 0 additions & 37 deletions src/ibus-bamboo/gnome_introspect.go

This file was deleted.

54 changes: 54 additions & 0 deletions src/ibus-bamboo/gnome_introspector.go
Original file line number Diff line number Diff line change
@@ -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"
}
File renamed without changes.
12 changes: 6 additions & 6 deletions src/ibus-bamboo/x11.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -161,6 +166,6 @@ void start_input_watching()
pthread_detach(th_input_watch);
}

void stop_input_watching() {
void x11StopWindowInspector() {
input_watching = 0;
}

0 comments on commit cc6a758

Please sign in to comment.