-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerpulse.go
451 lines (407 loc) · 11.5 KB
/
powerpulse.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"C"
"encoding/json"
"io/ioutil"
"strings"
"sync"
"time"
"github.com/spf13/pflag"
)
var (
lock sync.Mutex
device *Device = nil
manifests = []string{
"./powerpulse.json",
"/data/local/tmp/powerpulse.json",
"/vendor/etc/powerpulse.json",
"/system/vendor/etc/powerpulse.json",
"/system/etc/powerpulse.json",
"/etc/powerpulse.json",
}
profileNow = ""
profileLast = ""
debug = true
verbose = true
booted = false
bootedProfile = false
)
//export PowerPulse_Stargaze
func PowerPulse_Stargaze() {
go stargaze()
}
func stargaze() {
Info("Stargazing for desired profile")
found := false
for profileName := range device.Profiles {
if profileNow == profileName {
found = true
break
}
}
if !found {
//Could be risky, but we're being asked to choose here - next time it will be the user's last choice if paths/powerpulse/profile is set, or continue being this if user lets be
//For any matter, it shouldn't hurt to start with the highest performing profile until the user limits it
profileNow = device.ProfileOrder[len(device.ProfileOrder)-1]
}
Debug("Stargazed and found %s", profileNow)
}
//export PowerPulse_SetProfile
func PowerPulse_SetProfile(profile *C.char) {
go setProfile(C.GoString(profile))
}
func setProfile(profile string) {
PowerPulse_Init()
if device == nil {
return
}
lock.Lock()
defer lock.Unlock()
Debug("Got past lock for setProfile(%s)", profile)
//When device.SetProfile is called but device.ProfileLock is true, profileNow is still updated with the new request
profileLast = profileNow
if !bootedProfile && device.ProfileBoot != "" && device.ProfileBootDuration.String() != "" {
Debug("Applying boot profile %s", device.ProfileBoot)
duration, err := device.ProfileBootDuration.Int64()
if err != nil {
Error("Error applying boot profile %s for duration %s: %v", device.ProfileBoot, device.ProfileBootDuration, err)
return
}
if err := device.SetProfile(device.ProfileBoot); err != nil {
Error("Error applying boot profile %s: %v", device.ProfileBoot, err)
return
}
bootedProfile = true
if profile == "" {
PowerPulse_Stargaze()
profile = profileNow
}
if duration > 0 {
device.ProfileLock = true
Debug("Deferring profile %s for %d seconds", profile, duration)
time.Sleep(time.Second * time.Duration(duration))
device.ProfileLock = false
}
}
//Check if we need to change course on which profile to apply
if profileNow != profileLast {
profile = profileNow
}
Info("Applying profile %s", profile)
if err := device.SetProfile(profile); err != nil {
Error("Error applying profile %s: %v", profile, err)
return
}
profileNow = profile
if err := device.CacheProfile(profile); err != nil {
Warn("Failed to cache profile %s for reboot: %v", profile, err)
}
}
//export PowerPulse_ResetProfile
func PowerPulse_ResetProfile() {
go resetProfile()
}
func resetProfile() {
PowerPulse_Init()
if device == nil {
return
}
lock.Lock()
defer lock.Unlock()
Debug("Got past lock for resetProfile()")
if profileLast != "" {
if err := device.SetProfile(profileLast); err != nil {
Error("Error resetting to profile %s: %v", profileLast, err)
return
}
profileTmp := profileNow
profileNow = profileLast
profileLast = profileTmp
}
}
//export PowerPulse_SetInteractive
func PowerPulse_SetInteractive(interactive bool) {
go setInteractive(interactive)
}
func setInteractive(interactive bool) {
for inputName, input := range device.Paths.Inputs {
if input.Path != "" {
switch input.Type {
case "touchkey", "touchscreen":
if input.Enabled != "" {
enabledPath := pathJoin(input.Path, input.Enabled)
if interactive {
Debug("Interacting with %s (%s)", inputName, input.Type)
device.write(enabledPath, "1")
} else {
Debug("Not interacting with %s (%s)", inputName, input.Type)
device.write(enabledPath, "0")
}
}
}
}
}
if profile := device.GetProfile("screen_off"); profile != nil {
if interactive {
Debug("Turning off screen off profile")
resetProfile()
} else {
Debug("Turning on screen off profile")
setProfile("screen_off")
}
}
}
//export PowerPulse_SetPowerHint
func PowerPulse_SetPowerHint(hint, data int32) {
go setPowerHint(hint, data)
}
func setPowerHint(hint, data int32) {
switch PowerHint(hint) {
case HINT_VSYNC:
if data > 0 {
Debug("PowerHint: VSYNC: on")
//TODO: device.Boosting = true; go device.BoostIf()
device.Boost(16666) //1 frame @60Hz
} else {
Debug("PowerHint: VSYNC: off")
//TODO: device.Boosting = false
}
return
case HINT_INTERACTION:
device.Boost(data * 1000)
return
case HINT_VIDEO_ENCODE:
Debug("PowerHint: DEPRECATED: VIDEO_ENCODE: %d", data)
return
case HINT_VIDEO_DECODE:
Debug("PowerHint: DEPRECATED: VIDEO_DECODE: %d", data)
return
case HINT_LOW_POWER:
if profile := device.GetProfile("battery_saver"); profile != nil {
if data > 0 {
Debug("Turning on battery saver profile")
setProfile("battery_saver")
} else {
Debug("Turning off battery saver profile")
resetProfile()
}
}
return
case HINT_LAUNCH:
if data > 0 {
//TODO: device.Boosting = true; go device.BoostIf()
device.Boost(3000000) //3 seconds
} else {
//TODO: device.Boosting = false
}
return
case HINT_SUSTAINED_PERFORMANCE, HINT_VR_MODE,
HINT_AUDIO_STREAMING, HINT_AUDIO_LOW_LATENCY,
HINT_CAMERA_LAUNCH, HINT_CAMERA_STREAMING, HINT_CAMERA_SHOT,
HINT_EXPENSIVE_RENDERING, HINT_LINEAGE_CPU_BOOST:
if profile := device.GetProfile("performance"); profile != nil {
if data > 0 {
Debug("Turning on performance profile")
setProfile("performance")
} else {
Debug("Turning off performance profile")
resetProfile()
}
}
return
case HINT_LINEAGE_SET_PROFILE:
switch data {
case -1:
setProfile("screen_off")
case 0:
setProfile("battery_saver")
case 3:
setProfile("efficiency")
case 1:
setProfile("balanced")
case 4:
setProfile("quick")
case 2:
setProfile("performance")
}
return
}
Debug("PowerHint: %d: %d (not supported)", hint, data)
}
//export PowerPulse_SetFeature
func PowerPulse_SetFeature(feature int32, activate bool) {
switch PowerFeature(feature) {
case FEATURE_DOUBLE_TAP_TO_WAKE: //POWER_FEATURE_DOUBLE_TAP_TO_WAKE
Debug("SetFeature: POWER_FEATURE_DOUBLE_TAP_TO_WAKE: %t", activate)
return
}
Debug("SetFeature: %d: %t (not supported)", feature, activate)
}
//export PowerPulse_GetFeature
func PowerPulse_GetFeature(feature int32) uint32 {
switch PowerFeature(feature) {
case FEATURE_DOUBLE_TAP_TO_WAKE:
Debug("GetFeature: DOUBLE_TAP_TO_WAKE: true")
return 1 //TODO: return from config
case FEATURE_SUPPORTED_PROFILES:
if len(device.ProfileOrder) > 0 {
Debug("GetFeature: SUPPORTED_PROFILES: true")
return uint32(len(device.ProfileOrder))
}
Debug("GetFeature: SUPPORTED_PROFILES: false")
return 0
}
Debug("GetFeature: %d: (not supported)", feature)
return 0
}
//export PowerPulse_Init
func PowerPulse_Init() {
go initialize()
}
func initialize() {
if booted {
return
}
booted = true
startTime := time.Now()
Info("Need to boot PowerPulse first, just a blip...")
reloadConfig()
deltaTime := time.Now().Sub(startTime).Milliseconds()
Info("PowerPulse finished init in %dms", deltaTime)
}
//export PowerPulse_ReloadConfig
func PowerPulse_ReloadConfig() {
go reloadConfig()
}
func reloadConfig() {
deviceJSON := make([]byte, 0)
for i := 0; i < len(manifests); i++ {
tmpJSON, err := ioutil.ReadFile(manifests[i])
if err == nil && len(tmpJSON) > 0 {
Info("Found manifest at %s", manifests[i])
deviceJSON = tmpJSON
break
}
}
device = &Device{}
if err := json.Unmarshal(deviceJSON, device); err != nil {
Error("Error parsing manifest: %v", err)
return
}
if device.Paths == nil {
device.Paths = &Paths{}
}
if err := device.Paths.Init(); err != nil {
Error("Error parsing paths from device manifest: %v", err)
return
}
pathsJSON, err := json.Marshal(device.Paths)
if err != nil {
Debug("DEBUG: Error marshalling paths for print: %v", err)
return
}
Debug(string(pathsJSON))
if len(device.Profiles) < 1 {
Error("Error reading device manifest: No profiles were found")
return
}
for profileName := range device.Profiles {
adjustedName := strings.ReplaceAll(strings.ToLower(profileName), " ", "_")
if adjustedName != profileName {
device.Profiles[adjustedName] = device.Profiles[profileName]
delete(device.Profiles, profileName)
Debug("Found profile %s as %s", profileName, adjustedName)
} else {
Debug("Found profile %s", adjustedName)
}
}
if device.ProfileBoot != "" {
device.ProfileBoot = strings.ReplaceAll(strings.ToLower(device.ProfileBoot), " ", "_")
}
if profileNow == "" {
if device.ProfileBoot != "" {
profileNow = strings.ReplaceAll(strings.ToLower(device.ProfileBoot), " ", "_")
}
if device.Paths.PowerPulse != nil && device.Paths.PowerPulse.Profile != "" {
buffer, err := ioutil.ReadFile(device.Paths.PowerPulse.Profile)
if err == nil && len(buffer) > 0 {
if buffer[len(buffer)-1] == '\n' { buffer = buffer[:len(buffer)-1] }
profileNow = strings.ReplaceAll(strings.ToLower(string(buffer)), " ", "_")
}
}
} else {
profileNow = strings.ReplaceAll(strings.ToLower(profileNow), " ", "_")
}
if device.ProfileInheritance == nil || len(device.ProfileInheritance) == 0 {
Debug("No profile inheritance was specified")
//Try to add any recognizable profiles
pi := make([]string, 0)
try := []string{"screen_off", "battery_saver", "efficiency", "balanced", "quick", "performance", "bootpulse"}
for i := 0; i < len(try); i++ {
if p := device.GetProfile(try[i]); p != nil {
Debug("Found profile %s", try[i])
pi = append(pi, try[i])
}
}
if profileNow != "" {
found := false
for i := 0; i < len(pi); i++ {
if pi[i] == profileNow {
found = true
break
}
}
if !found {
//Start with the configured boot profile, in case we inherit special settings (better to be safe than sorry!)
pi = append([]string{profileNow}, pi...)
}
}
device.ProfileInheritance = pi
}
Debug("Profile inheritance: %s", device.ProfileInheritance)
if device.ProfileOrder == nil || len(device.ProfileOrder) == 0 {
Debug("No profile order was specified")
//Try to add any recognizable profiles
po := make([]string, 0)
try := []string{"battery_saver", "efficiency", "balanced", "quick", "performance"}
for i := 0; i < len(try); i++ {
if p := device.GetProfile(try[i]); p != nil {
Debug("Found profile %s", try[i])
po = append(po, try[i])
}
}
if profileNow != "" {
found := false
for i := 0; i < len(po); i++ {
if po[i] == profileNow {
found = true
break
}
}
if !found {
//Start with the configured boot profile, in case we inherit special settings (better to be safe than sorry!)
po = append([]string{profileNow}, po...)
}
}
device.ProfileOrder = po
}
if len(device.ProfileOrder) == 0 {
Debug("No identifiable boot profile, please set your profile order and/or your boot profile!")
Error("Error reading device manifest!")
return
}
Debug("Profile order: %s", device.ProfileOrder)
}
func main() {
debug = false
verbose = false
pflag.StringArrayVarP(&manifests, "manifest", "m", manifests, "path to device manifest(s),comma-separated")
pflag.StringVarP(&profileNow, "profile", "p", profileNow, "profile override")
pflag.BoolVarP(&debug, "debug", "d", debug, "debug mode")
pflag.BoolVarP(&verbose, "verbose", "v", verbose, "verbose mode")
pflag.Parse()
initialize()
stargaze()
Info("Applying profile %s", profileNow)
setProfile(profileNow)
}