aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ui/ui.go
blob: 3680fb1af7bbfe1acd3fe8028c27b15673d47029 (plain) (blame)
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
package ui

import (
	"os"
	"os/signal"
	"sync/atomic"
	"syscall"

	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~rjarry/aerc/lib/log"
	"git.sr.ht/~rockorager/vaxis"
)

// Use unbuffered channels (always blocking unless somebody can read
// immediately) We are merely using this as a proxy to the internal vaxis event
// channel.
var Events = make(chan vaxis.Event)

var Quit = make(chan struct{})

var Callbacks = make(chan func(), 50)

// QueueFunc queues a function to be called in the main goroutine. This can be
// used to prevent race conditions from delayed functions
func QueueFunc(fn func()) {
	Callbacks <- fn
}

// Use a buffered channel of size 1 to avoid blocking callers of Invalidate()
var Redraw = make(chan bool, 1)

// Invalidate marks the entire UI as invalid and request a redraw as soon as
// possible. Invalidate can be called from any goroutine and will never block.
func Invalidate() {
	if atomic.SwapUint32(&state.dirty, 1) != 1 {
		Redraw <- true
	}
}

var state struct {
	content DrawableInteractive
	ctx     *Context
	vx      *vaxis.Vaxis
	popover *Popover
	dirty   uint32 // == 1 if render has been queued in Redraw channel
	// == 1 if suspend is pending
	suspending uint32
	refresh    uint32 // == 1 if a refresh has been queued
}

func Initialize(content DrawableInteractive) error {
	opts := vaxis.Options{
		DisableMouse: !config.Ui.MouseEnabled,
		CSIuBitMask:  vaxis.CSIuDisambiguate,
		WithTTY:      "/dev/tty",
	}
	vx, err := vaxis.New(opts)
	if err != nil {
		return err
	}

	vx.Window().Clear()
	vx.HideCursor()

	state.content = content
	state.vx = vx
	state.ctx = NewContext(state.vx, onPopover)
	vx.SetTitle("aerc")

	Invalidate()
	if beeper, ok := content.(DrawableInteractiveBeeper); ok {
		beeper.OnBeep(vx.Bell)
	}
	content.Focus(true)

	go func() {
		defer log.PanicHandler()
		for event := range vx.Events() {
			Events <- event
		}
	}()

	return nil
}

func onPopover(p *Popover) {
	state.popover = p
}

func Exit() {
	close(Quit)
}

var SuspendQueue = make(chan bool, 1)

func QueueSuspend() {
	if atomic.SwapUint32(&state.suspending, 1) != 1 {
		SuspendQueue <- true
	}
}

// SuspendScreen should be called from the main thread.
func SuspendScreen() {
	_ = state.vx.Suspend()
}

func ResumeScreen() {
	err := state.vx.Resume()
	if err != nil {
		log.Errorf("ui: cannot resume after suspend: %v", err)
	}
	Invalidate()
}

func Suspend() error {
	var err error
	if atomic.SwapUint32(&state.suspending, 0) != 0 {
		err = state.vx.Suspend()
		if err == nil {
			sigcont := make(chan os.Signal, 1)
			signal.Notify(sigcont, syscall.SIGCONT)
			err = syscall.Kill(0, syscall.SIGTSTP)
			if err == nil {
				<-sigcont
			}
			signal.Reset(syscall.SIGCONT)
			err = state.vx.Resume()
			state.content.Draw(state.ctx)
			state.vx.Render()
		}
	}
	return err
}

func Close() {
	state.vx.Close()
}

func QueueRefresh() {
	if atomic.SwapUint32(&state.refresh, 1) != 1 {
		Invalidate()
	}
}

func Render() {
	if atomic.SwapUint32(&state.dirty, 0) != 0 {
		state.vx.Window().Clear()
		// reset popover for the next Draw
		state.popover = nil
		state.vx.HideCursor()
		state.content.Draw(state.ctx)
		if state.popover != nil {
			// if the Draw resulted in a popover, draw it
			state.popover.Draw(state.ctx)
		}
		switch atomic.SwapUint32(&state.refresh, 0) {
		case 0:
			state.vx.Render()
		case 1:
			state.vx.Refresh()
		}
	}
}

func HandleEvent(event vaxis.Event) {
	switch event := event.(type) {
	case vaxis.Resize:
		state.ctx = NewContext(state.vx, onPopover)
		Invalidate()
	case vaxis.Redraw:
		Invalidate()
	default:
		// We never care about num or caps lock. Remove them so it
		// doesn't interefere with key matching
		if key, ok := event.(vaxis.Key); ok {
			key.Modifiers &^= vaxis.ModCapsLock
			key.Modifiers &^= vaxis.ModNumLock
			event = key
		}
		// if we have a popover, and it can handle the event, it does so
		if state.popover == nil || !state.popover.Event(event) {
			// otherwise, we send the event to the main content
			state.content.Event(event)
		}
	}
}