-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
216 lines (187 loc) · 5.14 KB
/
Copy pathmain.go
File metadata and controls
216 lines (187 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/godbus/dbus/v5"
)
const (
engineName = "AvroBangla"
engineLongName = "Avro Bangla Phonetic"
engineDescription = "Bangla phonetic input method based on Avro"
engineLanguage = "bn"
// Use FreeBSD standard icon path; fallback to generic if not found
engineIcon = "/usr/local/share/icons/hicolor/48x48/apps/ibus-engine.png"
engineLayout = "us"
engineHotkeys = "<Super>space"
)
// IBusEngine wraps the AvroEngine with IBus-specific state
type IBusEngine struct {
avroEngine *AvroEngine
buffer string
preeditText string
candidates []string
selectedIdx int
conn *dbus.Conn
objectPath dbus.ObjectPath
isActive bool
}
// NewIBusEngine creates a new IBus engine wrapper
func NewIBusEngine(conn *dbus.Conn, objectPath dbus.ObjectPath) *IBusEngine {
return &IBusEngine{
avroEngine: NewAvroEngine(),
buffer: "",
preeditText: "",
candidates: []string{},
selectedIdx: 0,
conn: conn,
objectPath: objectPath,
isActive: true,
}
}
// ProcessKey handles key press events
func (e *IBusEngine) ProcessKey(keyval uint32, keycode uint32, state uint32) bool {
// Check if it's a printable character
if state&0x4 == 0 { // Not shifted by control
char := e.keyvalToChar(keyval)
if char != 0 {
e.buffer += string(char)
e.updatePreedit()
return true
}
}
// Handle special keys
switch keyval {
case 65293, 65421: // KEY_Return, KEY_KP_Enter
// Commit current buffer
if e.buffer != "" {
converted := e.avroEngine.Parse(e.buffer)
e.commitText(converted)
e.buffer = ""
e.preeditText = ""
e.updatePreedit()
}
return true
case 32: // KEY_space
// Space commits the current conversion
if e.buffer != "" {
converted := e.avroEngine.Parse(e.buffer)
e.commitText(converted)
e.buffer = ""
e.preeditText = ""
e.updatePreedit()
}
return false // Let space through after commit
case 65288: // KEY_BackSpace
if len(e.buffer) > 0 {
e.buffer = e.buffer[:len(e.buffer)-1]
e.updatePreedit()
}
return len(e.buffer) > 0
case 65307: // KEY_Escape
// Clear buffer on escape
e.buffer = ""
e.preeditText = ""
e.updatePreedit()
return true
case 65289: // KEY_Tab
// Could be used for candidate selection in future
if e.buffer != "" {
converted := e.avroEngine.Parse(e.buffer)
e.commitText(converted)
e.buffer = ""
e.preeditText = ""
e.updatePreedit()
}
return true
}
return false
}
// updatePreedit updates the preedit text displayed to the user
func (e *IBusEngine) updatePreedit() {
if e.buffer == "" {
e.hidePreeditText()
return
}
// Show the buffer as preedit text
e.updatePreeditText(e.buffer, len(e.buffer), true)
}
// Reset resets the engine state
func (e *IBusEngine) Reset() {
e.buffer = ""
e.preeditText = ""
e.candidates = []string{}
e.selectedIdx = 0
}
// FocusIn handles focus in event
func (e *IBusEngine) FocusIn() {
e.isActive = true
}
// FocusOut handles focus out event
func (e *IBusEngine) FocusOut() {
// Commit any pending text on focus out
if e.buffer != "" {
converted := e.avroEngine.Parse(e.buffer)
e.commitText(converted)
e.buffer = ""
e.preeditText = ""
}
e.isActive = false
}
// keyvalToChar converts a keyval to a rune
func (e *IBusEngine) keyvalToChar(keyval uint32) rune {
// Handle basic ASCII range
if keyval >= 32 && keyval <= 126 {
return rune(keyval)
}
return 0
}
// D-Bus helper methods for IBus communication
func (e *IBusEngine) commitText(text string) {
variant := dbus.MakeVariant(text)
e.conn.Object("org.freedesktop.IBus", e.objectPath).Call("IBus.Engine.CommitText", 0, variant)
}
func (e *IBusEngine) updatePreeditText(text string, cursorPos int, visible bool) {
variant := dbus.MakeVariant(text)
e.conn.Object("org.freedesktop.IBus", e.objectPath).Call("IBus.Engine.UpdatePreeditText", 0, variant, uint32(cursorPos), visible)
}
func (e *IBusEngine) hidePreeditText() {
e.conn.Object("org.freedesktop.IBus", e.objectPath).Call("IBus.Engine.HidePreeditText", 0)
}
func main() {
// Parse command line flags
ibusAddress := flag.String("ibus", "", "IBus address")
flag.Parse()
if *ibusAddress == "" {
fmt.Fprintln(os.Stderr, "Error: IBus address not provided")
os.Exit(1)
}
// Connect to D-Bus session bus
conn, err := dbus.ConnectSessionBus()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to D-Bus: %v\n", err)
os.Exit(1)
}
// Create our engine wrapper
engineWrapper := NewIBusEngine(conn, "/org/freedesktop/IBus/Engine/"+engineName)
// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
engineWrapper.Reset()
os.Exit(0)
}()
// Register with IBus
// In a full implementation, we would register the engine and set up
// proper D-Bus message handlers here. For now, we'll just print info.
fmt.Printf("Avro Bangla engine initialized\n")
fmt.Printf("Engine Name: %s\n", engineName)
fmt.Printf("Engine Long Name: %s\n", engineLongName)
fmt.Printf("Description: %s\n", engineDescription)
fmt.Printf("Language: %s\n", engineLanguage)
// Keep running until interrupted
select {}
}