diff --git a/ibus/_common.cgo b/_common.cgo similarity index 99% rename from ibus/_common.cgo rename to _common.cgo index 6863f66..d5af05c 100644 --- a/ibus/_common.cgo +++ b/_common.cgo @@ -1,4 +1,4 @@ -package ibus +package goibus /* #cgo pkg-config: ibus-1.0 diff --git a/ibus/bus.go b/bus.go similarity index 95% rename from ibus/bus.go rename to bus.go index eb29c49..c5cdbb4 100644 --- a/ibus/bus.go +++ b/bus.go @@ -1,6 +1,6 @@ -package ibus +package goibus -import "github.com/godbus/dbus" +import "github.com/godbus/dbus/v5" type Bus struct { dbusConn *dbus.Conn diff --git a/ibus/common.go b/common.go similarity index 84% rename from ibus/common.go rename to common.go index 88bf4b1..216e8e4 100644 --- a/ibus/common.go +++ b/common.go @@ -1,4 +1,4 @@ -package ibus +package goibus import ( "fmt" @@ -7,7 +7,7 @@ import ( "strconv" "strings" - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) const ( @@ -79,20 +79,31 @@ func GetSocketPath() string { if path != "" { return path } - display := os.Getenv("DISPLAY") + display := os.Getenv("WAYLAND_DISPLAY") + isWayland := true + if display == "" { + isWayland = false + display = os.Getenv("DISPLAY") + } if display == "" { fmt.Fprintf(os.Stderr, "DISPLAY is empty! We use default DISPLAY (:0.0)") display = ":0.0" } - // format is {hostname}:{displaynumber}.{screennumber} hostname := "unix" - HDS := strings.SplitN(display, ":", 2) - DS := strings.SplitN(HDS[1], ".", 2) - - if HDS[0] != "" { - hostname = HDS[0] + displayNumber := "" + if isWayland { + displayNumber = display + } else { + // format is {hostname}:{displaynumber}.{screennumber} + HDS := strings.SplitN(display, ":", 2) + DS := strings.SplitN(HDS[1], ".", 2) + + if HDS[0] != "" { + hostname = HDS[0] + } + displayNumber = DS[0] } - p := fmt.Sprintf("%s-%s-%s", GetLocalMachineId(), hostname, DS[0]) + p := fmt.Sprintf("%s-%s-%s", GetLocalMachineId(), hostname, displayNumber) path = GetUserConfigDir() + "/ibus/bus/" + p return path diff --git a/ibus/component.go b/component.go similarity index 97% rename from ibus/component.go rename to component.go index a78ae49..2552658 100644 --- a/ibus/component.go +++ b/component.go @@ -1,10 +1,11 @@ -package ibus +package goibus import ( "encoding/xml" "fmt" - "github.com/godbus/dbus" "io" + + "github.com/godbus/dbus/v5" ) type Component struct { diff --git a/ibus/engine.go b/engine.go similarity index 99% rename from ibus/engine.go rename to engine.go index e507f76..aa3462f 100644 --- a/ibus/engine.go +++ b/engine.go @@ -1,7 +1,7 @@ -package ibus +package goibus import ( - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) type Engine struct { diff --git a/ibus/engineDesc.go b/engineDesc.go similarity index 98% rename from ibus/engineDesc.go rename to engineDesc.go index a2f61c9..1cac4e5 100644 --- a/ibus/engineDesc.go +++ b/engineDesc.go @@ -1,7 +1,7 @@ -package ibus +package goibus import ( - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) type EngineDesc struct { diff --git a/ibus/factory.go b/factory.go similarity index 96% rename from ibus/factory.go rename to factory.go index 2be4a3d..584d671 100644 --- a/ibus/factory.go +++ b/factory.go @@ -1,7 +1,7 @@ -package ibus +package goibus import ( - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) type Factory struct { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7c5f27a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/BambooEngine/goibus + +go 1.13 + +require github.com/godbus/dbus/v5 v5.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..024b269 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/ibus/lookupTable.go b/ibus/lookupTable.go deleted file mode 100644 index 2f7c959..0000000 --- a/ibus/lookupTable.go +++ /dev/null @@ -1,39 +0,0 @@ -package ibus - -import ( - "github.com/godbus/dbus" -) - -type LookupTable struct { - Name string - Attachments map[string]dbus.Variant - PageSize uint32 - CursorPos uint32 - CursorVisible bool - Round bool - Orientation int32 - Candidates []dbus.Variant - Labels []dbus.Variant -} - -func NewLookupTable() *LookupTable { - lt := &LookupTable{} - lt.Name = "IBusLookupTable" - lt.PageSize = 5 - lt.CursorPos = 0 - lt.CursorVisible = true - lt.Round = false - lt.Orientation = ORIENTATION_SYSTEM - - return lt -} - -func (lt *LookupTable) AppendCandidate(text string) { - t := NewText(text) - lt.Candidates = append(lt.Candidates, dbus.MakeVariant(*t)) -} - -func (lt *LookupTable) AppendLabel(label string) { - l := NewText(label) - lt.Labels = append(lt.Labels, dbus.MakeVariant(*l)) -} diff --git a/lookupTable.go b/lookupTable.go new file mode 100644 index 0000000..a9f5e0b --- /dev/null +++ b/lookupTable.go @@ -0,0 +1,137 @@ +package goibus + +import ( + "github.com/godbus/dbus/v5" +) + +type LookupTable struct { + Name string + Attachments map[string]dbus.Variant + PageSize uint32 + CursorPos uint32 + CursorVisible bool + Round bool + Orientation int32 + Candidates []dbus.Variant + Labels []dbus.Variant +} + +func NewLookupTable() *LookupTable { + lt := &LookupTable{} + lt.Name = "IBusLookupTable" + lt.PageSize = 5 + lt.CursorPos = 0 + lt.CursorVisible = true + lt.Round = false + lt.Orientation = ORIENTATION_SYSTEM + + return lt +} + +func (lt *LookupTable) AppendCandidate(text string) { + t := NewText(text) + lt.Candidates = append(lt.Candidates, dbus.MakeVariant(*t)) +} + +func (lt *LookupTable) AppendLabel(label string) { + l := NewText(label) + lt.Labels = append(lt.Labels, dbus.MakeVariant(*l)) +} + +func (lt *LookupTable) SetCursorPos(pos uint32) bool { + if pos >= uint32(len(lt.Candidates)) || pos < 0 { + return false + } + lt.CursorPos = pos + return true +} + +func (lt *LookupTable) GetCursorPos() uint32 { + return lt.CursorPos +} + +func (lt *LookupTable) GetCursorPosInCurrentPage() uint32 { + return lt.CursorPos % lt.PageSize +} + +func (lt *LookupTable) SetCursorPosInCurrentPage(pos uint32) bool { + if pos < 0 || pos >= lt.PageSize { + return false + } + pos += lt.GetCursorPosInCurrentPage() + if pos >= uint32(len(lt.Candidates)) { + return false + } + lt.CursorPos = pos + return true +} + +func (lt *LookupTable) CursorUp() bool { + if lt.CursorPos == 0 { + if lt.Round { + lt.CursorPos = uint32(len(lt.Candidates)) - 1 + return true + } else { + return false + } + } + lt.CursorPos -= 1 + return true +} + +func (lt *LookupTable) CursorDown() bool { + if lt.CursorPos == uint32(len(lt.Candidates)) { + if lt.Round { + lt.CursorPos = 0 + return true + } else { + return false + } + } + lt.CursorPos += 1 + return true +} + +func (lt *LookupTable) PageUp() bool { + if lt.CursorPos < lt.PageSize { + if lt.Round { + var nrCandidates = uint32(len(lt.Candidates)) + var maxPage = uint32(nrCandidates / lt.PageSize) + lt.CursorPos += maxPage * lt.PageSize + if lt.CursorPos > nrCandidates-1 { + lt.CursorPos = nrCandidates - 1 + } + return true + } else { + return false + } + } + lt.CursorPos -= lt.PageSize + return true +} + +func (lt *LookupTable) PageDown() bool { + var currentPage = lt.CursorPos / lt.PageSize + var nrCandidates = uint32(len(lt.Candidates)) + var maxPage = nrCandidates / lt.PageSize + if currentPage >= maxPage { + if lt.Round { + lt.CursorPos %= lt.PageSize + return true + } else { + return false + } + } + var pos = lt.CursorPos + lt.PageSize + if pos >= nrCandidates { + pos = nrCandidates - 1 + } + lt.CursorPos = pos + return true +} + +func (lt *LookupTable) Clean() { + lt.Candidates = nil + lt.Labels = nil + lt.CursorPos = 0 +} diff --git a/ibus/property.go b/property.go similarity index 97% rename from ibus/property.go rename to property.go index b5751f7..1a9e9b5 100644 --- a/ibus/property.go +++ b/property.go @@ -1,7 +1,7 @@ -package ibus +package goibus import ( - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) type PropList struct { diff --git a/ibus/text.go b/text.go similarity index 96% rename from ibus/text.go rename to text.go index 6c7c691..af6ed33 100644 --- a/ibus/text.go +++ b/text.go @@ -1,7 +1,7 @@ -package ibus +package goibus import ( - "github.com/godbus/dbus" + "github.com/godbus/dbus/v5" ) type Attribute struct {