-
Notifications
You must be signed in to change notification settings - Fork 1
/
varnam.go
164 lines (133 loc) · 3.62 KB
/
varnam.go
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
package main
/**
* gittu-engine - An IBus Engine in Go
* goibus - golang implementation of libibus
* Copyright Sarim Khan, 2016
* Copyright Nguyen Tran Hau, 2021
* https://github.com/sarim/goibus
* Licensed under Mozilla Public License 1.1 ("MPL")
*
* Derivative Changes: Modified names, added preferences
* Copyright Subin Siby, 2021
*/
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"github.com/varnamproject/govarnam-ibus/ibus"
"github.com/godbus/dbus/v5"
)
var installPrefix = "/usr/local"
var engineName = "Varnam"
var engineCode = "varnam"
var upstreamURL = "https://varnamproject.github.io"
// Bus name related to the engine used which is govarnam
var busName = "org.freedesktop.IBus.GoVarnam"
var schemeIDFlag = flag.String("s", "", "Scheme ID")
var engineLang = flag.String("lang", "", "Language")
var schemeID = ""
var inscriptMode = false
var debug = flag.Bool("debug", false, "Enable debugging")
var embeded = flag.Bool("ibus", false, "Run the embeded ibus component")
var standalone = flag.Bool("standalone", false, "Run standalone by creating new component")
var generatexml = flag.String("xml", "", "Write xml representation of component to file or stdout if file == \"-\"")
var prefix = flag.String("prefix", "", "Prefix location")
var prefs = flag.Bool("prefs", false, "Show preferences window")
func makeComponent() *ibus.Component {
component := ibus.NewComponent(
busName,
engineName+" Input Engine",
"1.6.0",
"AGPL-3.0",
"Subin Siby",
upstreamURL,
installPrefix+"/bin/varnam-ibus-engine -ibus -s "+schemeID+" -lang "+*engineLang,
"ibus-varnam-"+schemeID)
avroenginedesc := ibus.SmallEngineDesc(
engineCode,
engineName,
engineName+" Input Method",
*engineLang,
"AGPL-3.0",
"Subin Siby",
installPrefix+"/share/varnam/ibus/icons/"+engineCode+".png",
"en",
installPrefix+"/bin/varnam-ibus-engine -prefs -s "+schemeID+" -lang "+*engineLang,
"1.6.0")
component.AddEngine(avroenginedesc)
return component
}
func main() {
if *debug {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.CommandLine.VisitAll(func(f *flag.Flag) {
format := " -%s: %s\n"
fmt.Fprintf(os.Stderr, format, f.Name, f.Usage)
})
}
flag.Parse()
if *schemeIDFlag == "" {
log.Fatal("Need a scheme ID. Pass it to -s option")
}
if *engineLang == "" {
log.Fatal("Need a language identifier. Pass it to -lang option")
}
schemeID = *schemeIDFlag
engineName += "-" + schemeID
engineCode += "-" + schemeID
busName += "." + schemeID
if strings.Contains(schemeID, "inscript") {
inscriptMode = true
}
if *generatexml != "" {
if *prefix == "" {
log.Fatal("Install prefix location needed")
}
installPrefix = *prefix
c := makeComponent()
if *generatexml == "-" {
c.OutputXML(os.Stdout)
} else {
f, err := os.Create(*generatexml)
if err != nil {
panic(err)
}
c.OutputXML(f)
f.Close()
}
} else if *embeded {
bus := ibus.NewBus()
fmt.Println("Got Bus, Running Embeded")
conn := bus.GetDbusConn()
ibus.NewFactory(conn, VarnamEngineCreator)
bus.RequestName(busName, 0)
select {}
} else if *standalone {
bus := ibus.NewBus()
fmt.Println("Got Bus, Running Standalone")
conn := bus.GetDbusConn()
ibus.NewFactory(conn, VarnamEngineCreator)
bus.RegisterComponent(makeComponent())
fmt.Println("Setting Global Engine to me")
bus.CallMethod("SetGlobalEngine", 0, "varnam")
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
select {
case <-c:
}
} else if *prefs {
showPrefs()
} else {
Usage()
os.Exit(1)
}
}