Debian Codegraph @

lib/ui/ui.go

raw
1 package ui
2
3 import (
4 "os"
5 "os/signal"
6 "sync/atomic"
7 "syscall"
8
9 "git.sr.ht/~rjarry/aerc/config"
10 "git.sr.ht/~rjarry/aerc/lib/log"
11 "go.rockorager.dev/vaxis"
12 )
13
14 // Use unbuffered channels (always blocking unless somebody can read
15 // immediately) We are merely using this as a proxy to the internal vaxis event
16 // channel.
17 var Events = make(chan vaxis.Event)
18
19 var Quit = make(chan struct{})
20
21 var Callbacks = make(chan func(), 50)
22
23 // QueueFunc queues a function to be called in the main goroutine. This can be
24 // used to prevent race conditions from delayed functions
25 func QueueFunc(fn func()) {
26 Callbacks <- fn
27 }
28
29 // Use a buffered channel of size 1 to avoid blocking callers of Invalidate()
30 var Redraw = make(chan bool, 1)
31
32 // Invalidate marks the entire UI as invalid and request a redraw as soon as
33 // possible. Invalidate can be called from any goroutine and will never block.
34 func Invalidate() {
35 if atomic.SwapUint32(&state.dirty, 1) != 1 {
36 Redraw <- true
37 }
38 }
39
40 var state struct {
41 content DrawableInteractive
42 ctx *Context
43 vx *vaxis.Vaxis
44 popover *Popover
45 dirty uint32 // == 1 if render has been queued in Redraw channel
46 // == 1 if suspend is pending
47 suspending uint32
48 refresh uint32 // == 1 if a refresh has been queued
49 }
50
51 func Initialize(content DrawableInteractive) error {
52 opts := vaxis.Options{
53 DisableMouse: !config.Ui().MouseEnabled,
54 CSIuBitMask: vaxis.CSIuDisambiguate,
55 WithTTY: "/dev/tty",
56 }
57 vx, err := vaxis.New(opts)
58 if err != nil {
59 return err
60 }
61
62 vx.Window().Clear()
63 vx.HideCursor()
64
65 state.content = content
66 state.vx = vx
67 state.ctx = NewContext(state.vx, onPopover)
68 vx.SetTitle("aerc")
69
70 Invalidate()
71 if beeper, ok := content.(DrawableInteractiveBeeper); ok {
72 beeper.OnBeep(vx.Bell)
73 }
74 content.Focus(true)
75
76 go func() {
77 defer log.PanicHandler()
78 for event := range vx.Events() {
79 Events <- event
80 }
81 }()
82
83 go func() {
84 defer log.PanicHandler()
85 sig := make(chan os.Signal, 1)
86 signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
87 <-sig
88 Exit()
89 }()
90
91 return nil
92 }
93
94 func onPopover(p *Popover) {
95 state.popover = p
96 }
97
98 func Exit() {
99 close(Quit)
100 }
101
102 var SuspendQueue = make(chan bool, 1)
103
104 func QueueSuspend() {
105 if atomic.SwapUint32(&state.suspending, 1) != 1 {
106 SuspendQueue <- true
107 }
108 }
109
110 // SuspendScreen should be called from the main thread.
111 func SuspendScreen() {
112 _ = state.vx.Suspend()
113 }
114
115 func ResumeScreen() {
116 err := state.vx.Resume()
117 if err != nil {
118 log.Errorf("ui: cannot resume after suspend: %v", err)
119 }
120 Invalidate()
121 }
122
123 func Suspend() error {
124 var err error
125 if atomic.SwapUint32(&state.suspending, 0) != 0 {
126 err = state.vx.Suspend()
127 if err == nil {
128 sigcont := make(chan os.Signal, 1)
129 signal.Notify(sigcont, syscall.SIGCONT)
130 err = syscall.Kill(0, syscall.SIGTSTP)
131 if err == nil {
132 <-sigcont
133 }
134 signal.Reset(syscall.SIGCONT)
135 err = state.vx.Resume()
136 state.content.Draw(state.ctx)
137 state.vx.Render()
138 }
139 }
140 return err
141 }
142
143 func Close() {
144 state.vx.Close()
145 }
146
147 func QueueRefresh() {
148 if atomic.SwapUint32(&state.refresh, 1) != 1 {
149 Invalidate()
150 }
151 }
152
153 func Vaxis() *vaxis.Vaxis {
154 return state.vx
155 }
156
157 func PushClipboard(text string) {
158 state.vx.ClipboardPush(text)
159 }
160
161 func Render() {
162 if atomic.SwapUint32(&state.dirty, 0) != 0 {
163 state.vx.Window().Clear()
164 // reset popover for the next Draw
165 state.popover = nil
166 state.vx.HideCursor()
167 state.content.Draw(state.ctx)
168 if state.popover != nil {
169 // if the Draw resulted in a popover, draw it
170 state.popover.Draw(state.ctx)
171 }
172 switch atomic.SwapUint32(&state.refresh, 0) {
173 case 0:
174 state.vx.Render()
175 case 1:
176 state.vx.Refresh()
177 }
178 }
179 }
180
181 func HandleEvent(event vaxis.Event) {
182 switch event := event.(type) {
183 case vaxis.Resize:
184 state.vx.Resize(event)
185 state.ctx = NewContext(state.vx, onPopover)
186 Invalidate()
187 case vaxis.Redraw:
188 Invalidate()
189 default:
190 // We never care about num or caps lock. Remove them so it
191 // doesn't interfere with key matching
192 if key, ok := event.(vaxis.Key); ok {
193 log.Tracef("registered key event: %#v", key)
194 key.Modifiers &^= vaxis.ModCapsLock
195 key.Modifiers &^= vaxis.ModNumLock
196 event = remapKeys(key)
197 }
198 // if we have a popover, and it can handle the event, it does so
199 if state.popover == nil || !state.popover.Event(event) {
200 // otherwise, we send the event to the main content
201 state.content.Event(event)
202 }
203 }
204 }
205
206 func remapKeys(key vaxis.Key) vaxis.Key {
207 // accept number block <CR> as regular <CR>
208 switch key.Keycode {
209 case vaxis.KeyKeyPadEnter:
210 key.Keycode = vaxis.KeyEnter
211 case vaxis.KeyKeyPadUp:
212 key.Keycode = vaxis.KeyUp
213 case vaxis.KeyKeyPadDown:
214 key.Keycode = vaxis.KeyDown
215 case vaxis.KeyKeyPadLeft:
216 key.Keycode = vaxis.KeyLeft
217 case vaxis.KeyKeyPadRight:
218 key.Keycode = vaxis.KeyRight
219 }
220 return key
221 }