Make the loop API a thin wrapper around internal methods
This commit is contained in:
parent
7325921070
commit
67115530b4
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"kitty/tools/tty"
|
"kitty/tools/tty"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@ -44,6 +45,7 @@ type Loop struct {
|
|||||||
timers, timers_temp []*timer
|
timers, timers_temp []*timer
|
||||||
timer_id_counter, write_msg_id_counter IdType
|
timer_id_counter, write_msg_id_counter IdType
|
||||||
wakeup_channel chan byte
|
wakeup_channel chan byte
|
||||||
|
signal_channel chan os.Signal
|
||||||
pending_writes []*write_msg
|
pending_writes []*write_msg
|
||||||
|
|
||||||
// Callbacks
|
// Callbacks
|
||||||
@ -72,45 +74,19 @@ type Loop struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(options ...func(self *Loop)) (*Loop, error) {
|
func New(options ...func(self *Loop)) (*Loop, error) {
|
||||||
l := Loop{controlling_term: nil, timers_temp: make([]*timer, 4)}
|
l := new_loop()
|
||||||
l.terminal_options.alternate_screen = true
|
|
||||||
l.terminal_options.restore_colors = true
|
|
||||||
l.escape_code_parser.HandleCSI = l.handle_csi
|
|
||||||
l.escape_code_parser.HandleOSC = l.handle_osc
|
|
||||||
l.escape_code_parser.HandleDCS = l.handle_dcs
|
|
||||||
l.escape_code_parser.HandleAPC = l.handle_apc
|
|
||||||
l.escape_code_parser.HandleSOS = l.handle_sos
|
|
||||||
l.escape_code_parser.HandlePM = l.handle_pm
|
|
||||||
l.escape_code_parser.HandleRune = l.handle_rune
|
|
||||||
for _, f := range options {
|
for _, f := range options {
|
||||||
f(&l)
|
f(l)
|
||||||
}
|
}
|
||||||
return &l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) AddTimer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
|
func (self *Loop) AddTimer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
|
||||||
if self.timers == nil {
|
return self.add_timer(interval, repeats, callback)
|
||||||
return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead")
|
|
||||||
}
|
|
||||||
self.timer_id_counter++
|
|
||||||
t := timer{interval: interval, repeats: repeats, callback: callback, id: self.timer_id_counter}
|
|
||||||
t.update_deadline(time.Now())
|
|
||||||
self.timers = append(self.timers, &t)
|
|
||||||
self.sort_timers()
|
|
||||||
return t.id, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) RemoveTimer(id IdType) bool {
|
func (self *Loop) RemoveTimer(id IdType) bool {
|
||||||
if self.timers == nil {
|
return self.remove_timer(id)
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i := 0; i < len(self.timers); i++ {
|
|
||||||
if self.timers[i].id == id {
|
|
||||||
self.timers = append(self.timers[:i], self.timers[i+1:]...)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) NoAlternateScreen() *Loop {
|
func (self *Loop) NoAlternateScreen() *Loop {
|
||||||
|
|||||||
@ -19,6 +19,21 @@ import (
|
|||||||
|
|
||||||
var SIGNULL unix.Signal
|
var SIGNULL unix.Signal
|
||||||
|
|
||||||
|
func new_loop() *Loop {
|
||||||
|
l := Loop{controlling_term: nil, timers_temp: make([]*timer, 4)}
|
||||||
|
l.terminal_options.alternate_screen = true
|
||||||
|
l.terminal_options.restore_colors = true
|
||||||
|
l.terminal_options.kitty_keyboard_mode = true
|
||||||
|
l.escape_code_parser.HandleCSI = l.handle_csi
|
||||||
|
l.escape_code_parser.HandleOSC = l.handle_osc
|
||||||
|
l.escape_code_parser.HandleDCS = l.handle_dcs
|
||||||
|
l.escape_code_parser.HandleAPC = l.handle_apc
|
||||||
|
l.escape_code_parser.HandleSOS = l.handle_sos
|
||||||
|
l.escape_code_parser.HandlePM = l.handle_pm
|
||||||
|
l.escape_code_parser.HandleRune = l.handle_rune
|
||||||
|
return &l
|
||||||
|
}
|
||||||
|
|
||||||
func is_temporary_error(err error) bool {
|
func is_temporary_error(err error) bool {
|
||||||
return errors.Is(err, unix.EINTR) || errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, io.ErrShortWrite)
|
return errors.Is(err, unix.EINTR) || errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, io.ErrShortWrite)
|
||||||
}
|
}
|
||||||
@ -60,7 +75,7 @@ func (self *Loop) handle_csi(raw []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) handle_key_event(ev *KeyEvent) error {
|
func (self *Loop) handle_key_event(ev *KeyEvent) error {
|
||||||
// self.DebugPrintln(ev)
|
self.DebugPrintln(ev)
|
||||||
if self.OnKeyEvent != nil {
|
if self.OnKeyEvent != nil {
|
||||||
err := self.OnKeyEvent(ev)
|
err := self.OnKeyEvent(ev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -163,6 +178,10 @@ func (self *Loop) on_SIGTERM() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) on_SIGTSTP() error {
|
func (self *Loop) on_SIGTSTP() error {
|
||||||
|
signal.Reset(unix.SIGTSTP)
|
||||||
|
unix.Kill(os.Getpid(), unix.SIGTSTP)
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
signal.Notify(self.signal_channel, unix.SIGTSTP)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,9 +192,9 @@ func (self *Loop) on_SIGHUP() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Loop) run() (err error) {
|
func (self *Loop) run() (err error) {
|
||||||
sigchnl := make(chan os.Signal, 256)
|
self.signal_channel = make(chan os.Signal, 256)
|
||||||
handled_signals := []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGTSTP, unix.SIGHUP, unix.SIGWINCH, unix.SIGPIPE}
|
handled_signals := []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGTSTP, unix.SIGHUP, unix.SIGWINCH, unix.SIGPIPE}
|
||||||
signal.Notify(sigchnl, handled_signals...)
|
signal.Notify(self.signal_channel, handled_signals...)
|
||||||
defer signal.Reset(handled_signals...)
|
defer signal.Reset(handled_signals...)
|
||||||
|
|
||||||
controlling_term, err := tty.OpenControllingTerm()
|
controlling_term, err := tty.OpenControllingTerm()
|
||||||
@ -277,7 +296,7 @@ func (self *Loop) run() (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case s := <-sigchnl:
|
case s := <-self.signal_channel:
|
||||||
err = self.on_signal(s.(unix.Signal))
|
err = self.on_signal(s.(unix.Signal))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -3,10 +3,36 @@
|
|||||||
package loop
|
package loop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (self *Loop) add_timer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
|
||||||
|
if self.timers == nil {
|
||||||
|
return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead")
|
||||||
|
}
|
||||||
|
self.timer_id_counter++
|
||||||
|
t := timer{interval: interval, repeats: repeats, callback: callback, id: self.timer_id_counter}
|
||||||
|
t.update_deadline(time.Now())
|
||||||
|
self.timers = append(self.timers, &t)
|
||||||
|
self.sort_timers()
|
||||||
|
return t.id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Loop) remove_timer(id IdType) bool {
|
||||||
|
if self.timers == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := 0; i < len(self.timers); i++ {
|
||||||
|
if self.timers[i].id == id {
|
||||||
|
self.timers = append(self.timers[:i], self.timers[i+1:]...)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (self *Loop) dispatch_timers(now time.Time) error {
|
func (self *Loop) dispatch_timers(now time.Time) error {
|
||||||
updated := false
|
updated := false
|
||||||
self.timers_temp = self.timers_temp[:0]
|
self.timers_temp = self.timers_temp[:0]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user