-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
224 lines (200 loc) · 7.15 KB
/
main.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
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
217
218
219
220
221
222
223
224
// Copyright (C) 2019 Evgeny Kuznetsov ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"embed"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/andlabs/ui"
"github.com/cloudfoundry/jibber_jabber"
"github.com/getlantern/systray"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
var (
logTrace *log.Logger
logInfo *log.Logger
logWarning *log.Logger
logError *log.Logger
version = "custom-build"
iconPath string
saveValues bool
noSaveValues bool
localizer *i18n.Localizer
config struct {
fnlock fnlockEndpoint
thresh threshEndpoint
threshPers threshEndpoint
kdblightTimeout kdblightTimeoutEndpoint
wait bool
useScripts bool
windowed bool
}
)
//go:embed assets/*
var assets embed.FS
func main() {
i18nInit()
parseFlags()
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "AppletVersion", Other: "matebook-applet version {{.Version}}"}, TemplateData: map[string]interface{}{"Version": version}}))
findFnlock()
findThresh()
findKdblightTimeout()
if saveValues {
logWarning.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "OptionSDeprecated", Other: "-s option is deprecated, applet is now saving values for persistence by default"}}))
}
if !noSaveValues {
logTrace.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "LookingForBatteryPers", Other: "looking for endpoint to save thresholds to..."}}))
for _, ep := range threshSaveEndpoints {
_, _, err := ep.get()
if err == nil {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FoundBatteryPers", Other: "Persistence thresholds values endpoint found."}}))
config.threshPers = ep
break
}
}
}
if config.thresh != nil || config.fnlock != nil {
if config.windowed {
if err := ui.Main(launchUI); err != nil {
logError.Println(err)
}
} else {
systray.Run(onReady, onExit)
}
} else {
logError.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "NothingToWorkWith", Other: "Neither a supported version of Huawei-WMI driver, nor any of the required scripts are properly installed, see README.md#installation-and-setup for instructions"}}))
}
}
// findFnlock finds working fnlock interface (if any)
func findFnlock() {
for _, fnlck := range fnlockEndpoints {
_, err := fnlck.get()
if err != nil {
continue
}
config.fnlock = fnlck
if fnlck.isWritable() {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FoundFnlock", Other: "Found writable fnlock endpoint, will use it"}}))
break
}
}
}
// findThresh finds working threshold interface (if any)
func findThresh() {
for _, thresh := range threshEndpoints {
_, _, err := thresh.get()
if err != nil {
continue
}
config.thresh = thresh
if thresh.isWritable() {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FoundBattery", Other: "Found writable battery thresholds endpoint, will use it"}}))
break
}
}
}
// findKdblightTimeout finds working kdblight_timeout interface (if any)
func findKdblightTimeout() {
for _, kdblightTimeout := range kdblightTimeoutEndpoints {
_, err := kdblightTimeout.get()
if err != nil {
continue
}
config.kdblightTimeout = kdblightTimeout
if kdblightTimeout.isWritable() {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "FoundKdblightTimeout",
Other: "Found writable kdblight_timeout endpoint, will use it",
},
}))
break
}
}
}
func parseFlags() {
verbose := flag.Bool("v", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagV", Other: "be verbose"}}))
verboseMore := flag.Bool("vv", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagVV", Other: "be very verbose"}}))
flag.StringVar(&iconPath, "icon", "", localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagIcon", Other: "path of a custom icon to use"}}))
flag.BoolVar(&config.wait, "wait", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagWait", Other: "wait for driver to set battery thresholds (obsolete)"}}))
flag.BoolVar(&noSaveValues, "n", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagN", Other: "do not save values"}}))
flag.BoolVar(&config.useScripts, "r", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagR", Other: "use fnlock and batpro scripts if all else fails"}}))
flag.BoolVar(&config.windowed, "w", false, localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "FlagW", Other: "windowed mode"}}))
flag.Parse()
switch {
case *verbose:
logInit(io.Discard, os.Stdout, os.Stdout, os.Stderr)
case *verboseMore:
logInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
default:
logInit(io.Discard, io.Discard, os.Stdout, os.Stderr)
}
}
func logInit(
traceHandle io.Writer,
infoHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
logTrace = log.New(traceHandle,
"TRACE: ",
log.Ldate|log.Ltime|log.Lshortfile)
logInfo = log.New(infoHandle,
"INFO: ",
log.Ldate|log.Ltime|log.Lshortfile)
logWarning = log.New(warningHandle,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
logError = log.New(errorHandle,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}
func i18nInit() {
bundle := i18nPrepare()
lang, err := jibber_jabber.DetectIETF()
if err != nil {
fmt.Println("could not detect locale")
}
fmt.Println(lang)
localizer = i18n.NewLocalizer(bundle, lang)
}
func i18nPrepare() *i18n.Bundle {
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustAddMessages(language.English, messages...)
if files, err := assets.ReadDir("assets/translations"); err == nil {
for _, file := range files {
if ok, err := filepath.Match("active.*.toml", file.Name()); ok && err == nil {
f, err := assets.Open(filepath.Join("assets/translations", file.Name()))
if err == nil {
b, err := io.ReadAll(f)
if err == nil {
_, err := bundle.ParseMessageFileBytes(b, file.Name())
if err != nil {
fmt.Printf("error reading translation file %s: %s", file.Name(), err)
}
}
f.Close()
}
}
}
}
return bundle
}