-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
377 lines (307 loc) · 8.83 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
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
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"log"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/ajstarks/openvg"
touchscreen "github.com/electrocatstudios/FTXXXX_Touchscreen_Driver"
"github.com/electrocatstudios/PiMenu/screenservice"
"google.golang.org/grpc"
)
var cur_screen = "main"
var imageCache PMImageCache
var interruptScreen InterruptScreen
func GetImageWithOpacity(img image.Image, opacity uint8) image.Image {
m := image.NewRGBA(img.Bounds())
mask := image.NewUniform(color.Alpha{opacity})
draw.DrawMask(m, m.Bounds(), img, image.Point{0, 0}, mask, image.Point{0, 0}, draw.Over)
return m
}
func DrawLine(s ScreenDetails, dl DisplayLine, offset openvg.VGfloat, opacity *uint8) {
if dl.Type == "null" || dl.Type == "" {
// Nothing to do here - just leave blank
return
} else if dl.Type == "text" {
if dl.Color != "" {
openvg.FillColor(dl.Color)
} else {
openvg.FillColor("rgb(255,255,255)")
}
openvg.TextMid(s.W2, offset, dl.Value, "sans", 30)
} else if dl.Type == "data" {
if dl.Value == "IMAGESERVER" {
interruptScreen.Lock.Lock()
if interruptScreen.IncomingImage != nil {
img := (*interruptScreen.IncomingImage)
left := openvg.VGfloat(0)
top := openvg.VGfloat(0)
openvg.Img(left, top, img)
} else {
openvg.TextMid(s.W2, 200, "No Image available", "sans", 30)
}
interruptScreen.Lock.Unlock()
} else {
if dl.Color != "" {
openvg.FillColor(dl.Color)
} else {
openvg.FillColor("rgb(255,255,255)")
}
dataString := GetDataString(dl.Value)
openvg.TextMid(s.W2, offset, dataString, "sans", 30)
}
} else if dl.Type == "image" {
img, err := GetImageFromString(dl.Value)
if err != nil {
fmt.Println(err)
return
}
force_height := s.Height
if img.Height != 0 {
force_height = img.Height
}
image_file, err := imageCache.GetImage(img.Filename, 0, force_height)
if err != nil {
fmt.Println(err)
return
}
left := openvg.VGfloat(0)
top := openvg.VGfloat(img.Y)
if img.X != 0 {
// We aren't centering this one - use given x val
left = openvg.VGfloat(img.X)
} else {
// Center the image
image_width := openvg.VGfloat(image_file.Bounds().Max.X)
left = openvg.VGfloat(s.W2 - (image_width / 2))
}
if opacity != nil && *opacity >= uint8(5) {
img_op := GetImageWithOpacity(image_file, *opacity)
openvg.Img(left, top, img_op)
} else if opacity != nil && *opacity < uint8(5) {
// Don't draw if less than 5 opacity - should be nearly invisible
} else {
openvg.Img(left, top, image_file)
}
} else if dl.Type == "gif" {
img, err := GetImageFromString(dl.Value)
if err != nil {
fmt.Println(err)
return
}
frame, err := imageCache.GetGifFrame(img.Filename, s)
// Center the gif
left := s.W2 - openvg.VGfloat((frame.Bounds().Max.X / 2))
top := openvg.VGfloat((s.Height / 2) - (frame.Bounds().Max.Y / 2))
if img.X != 0 {
left = openvg.VGfloat(img.X)
}
if img.Y != 0 {
top = openvg.VGfloat(img.Y)
}
openvg.Img(left, top, frame)
}
}
func RunCommand(cmd string, wait bool) error {
cmd_items := strings.Split(cmd, " ")
cmd_exec := exec.Command(cmd_items[0], strings.Join(cmd_items[1:], " "))
err := cmd_exec.Start()
if err != nil {
fmt.Printf("Failed to start command : %s\n", err)
return err
}
if wait {
err = cmd_exec.Wait()
if err != nil {
fmt.Printf("Failed to execute command: %s\n", err)
}
}
return err
}
func HandleTouches(t *touchscreen.TouchScreen, input Screen, defaultScreen string) (string, *uint8) {
numTouches, err := t.GetTouchesCount()
if err != nil {
fmt.Println(err)
return defaultScreen, nil
}
if numTouches < 1 {
if input.Timeout.Length == 0 {
// We don't have a time out so just return
return defaultScreen, nil
}
// See if we have timed out since last touch
curTime := time.Now()
diff := curTime.Sub(t.LastScreenChange).Seconds()
remain := float64(input.Timeout.Length) - diff
if int(remain) < 0 {
t.LastScreenChange = time.Now()
// We have timed out so return to previous screen
ret_op := uint8(0)
return input.Timeout.ReturnScreen, &ret_op
}
if remain < float64(input.Timeout.ShowCountDown) && input.Timeout.ShowCountDown != 0 {
seconds_remain := strconv.FormatInt(int64(remain), 10)
seconds_remain += "s"
openvg.FillColor("rgb(255,255,255)")
openvg.Text(10, 420, seconds_remain, "sans", 30)
}
// Apply opacity if appropriate
if input.Background.FadeOut && remain < 0 {
// Opacity should be zero while we load the new screen
opacity := uint8(0)
return defaultScreen, &opacity
} else if remain < 1 && input.Background.FadeOut {
opacity := uint8(255 * remain)
return defaultScreen, &opacity
} else if diff < 1 && input.Background.FadeIn {
opacity := uint8(255 * diff)
return defaultScreen, &opacity
} else {
return defaultScreen, nil
}
}
t.LastScreenChange = time.Now()
touch, _ := t.GetTouches()
for i := 0; i < len(input.Touches); i++ {
hitBox := input.Touches[i]
if touch.X > hitBox.X && touch.X < hitBox.X+hitBox.Width {
if touch.Y > hitBox.Y && touch.Y < hitBox.Y+hitBox.Height {
if hitBox.Command.Type == "menu" {
return hitBox.Command.Value, nil
} else if hitBox.Command.Type == "command" {
err := RunCommand(hitBox.Command.Value, false)
if err != nil {
return defaultScreen, nil
}
if hitBox.Command.ReturnScreen != "" {
return hitBox.Command.ReturnScreen, nil
} else {
return defaultScreen, nil
}
}
}
}
}
return defaultScreen, nil
}
/*return new screen based on touches if appropriate*/
func DrawScreen(t *touchscreen.TouchScreen, name string, input Screen, s ScreenDetails) string {
openvg.Start(s.Width, s.Height) // Start the picture
if input.Background.Color != "" {
openvg.BackgroundColor(input.Background.Color)
} else {
openvg.BackgroundColor("black")
}
ret, opacity := HandleTouches(t, input, name)
// Default fill color
openvg.FillColor("rgb(255,255,255)") // White text
DrawLine(s, input.Line1, 400, opacity)
DrawLine(s, input.Line2, 320, opacity)
DrawLine(s, input.Line3, 240, opacity)
DrawLine(s, input.Line4, 160, opacity)
DrawLine(s, input.Line5, 80, opacity)
openvg.End()
return ret
}
func monitorService(s *screenservice.Server) {
// Listen for incoming screens on the buffer
for {
if s.NumScreens() != 0 {
screen := s.GetScreen()
newScreen := GetScreenFromScreenRequest(screen)
interruptScreen.Lock.Lock()
if len(interruptScreen.Screens) == 0 {
interruptScreen.LastShown = time.Now()
}
interruptScreen.Screens = append(interruptScreen.Screens, newScreen)
interruptScreen.Lock.Unlock()
}
if s.HasImage() {
img := s.GetImage()
interruptScreen.Lock.Lock()
interruptScreen.IncomingImage = img
interruptScreen.Lock.Unlock()
s.RemoveImage()
}
time.Sleep(100 * time.Millisecond)
}
}
func runInterruptServer() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 7777))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := screenservice.Server{}
grpcServer := grpc.NewServer()
go monitorService(&s)
screenservice.RegisterScreenServerServer(grpcServer, &s)
// start the server
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}
func CheckFolders() bool {
// Check if screens and images folders exist - offer to download default screens
folder := "screens"
if _, err := os.Stat(folder); os.IsNotExist(err) {
return false
}
folder = "images"
if _, err := os.Stat(folder); os.IsNotExist(err) {
return false
}
return true
}
func main() {
fmt.Println("Starting Screen App")
var screenDetails ScreenDetails
screenDetails.Width, screenDetails.Height = openvg.Init()
screenDetails.W2 = openvg.VGfloat(screenDetails.Width / 2) // this is to center lines of text
t := touchscreen.TouchScreen{}
t.Init(touchscreen.FT62XX)
t.Debug = true
bFoldersExist := CheckFolders()
if !bFoldersExist {
// Note this is a blocking call - nothing else will be available
RunFirstTimeScreen(screenDetails, &t)
return
}
go runInterruptServer()
var prevscreen string
var screen Screen
var err error
for {
bFoundInterrupt := false
interruptScreen.Lock.Lock()
if len(interruptScreen.Screens) > 0 {
bFoundInterrupt = true
DrawScreen(&t, "interrupt", interruptScreen.Screens[0], screenDetails)
curTime := time.Now()
diff := curTime.Sub(interruptScreen.LastShown).Seconds()
if int(diff) > interruptScreen.Screens[0].Timeout.Length {
interruptScreen.Screens = interruptScreen.Screens[1:]
interruptScreen.LastShown = time.Now()
}
}
interruptScreen.Lock.Unlock()
if bFoundInterrupt {
continue
}
if cur_screen != prevscreen {
screen, err = GetScreenByName(cur_screen)
if err != nil {
panic(err)
}
prevscreen = cur_screen
}
cur_screen = DrawScreen(&t, cur_screen, screen, screenDetails)
time.Sleep(5 * time.Millisecond)
}
}