Add injection backends

This commit is contained in:
Thales Maciel 2026-02-06 11:50:30 -03:00
parent a7f50fed75
commit 9ee301fbeb
7 changed files with 290 additions and 4 deletions

View file

@ -13,6 +13,7 @@ import (
"lel/internal/audio"
"lel/internal/clip"
"lel/internal/config"
"lel/internal/inject"
"lel/internal/whisper"
"lel/internal/x11"
)
@ -29,6 +30,7 @@ type Daemon struct {
cfg config.Config
x11 *x11.Conn
log *log.Logger
inj inject.Backend
mu sync.Mutex
state State
@ -39,9 +41,9 @@ type Daemon struct {
stateCh chan State
}
func New(cfg config.Config, x *x11.Conn, logger *log.Logger) *Daemon {
func New(cfg config.Config, x *x11.Conn, logger *log.Logger, inj inject.Backend) *Daemon {
r := &audio.Recorder{Input: cfg.FfmpegInput}
return &Daemon{cfg: cfg, x11: x, log: logger, state: StateIdle, ffmpeg: r, stateCh: make(chan State, 4)}
return &Daemon{cfg: cfg, x11: x, log: logger, inj: inj, state: StateIdle, ffmpeg: r, stateCh: make(chan State, 4)}
}
func (d *Daemon) UpdateConfig(cfg config.Config) {
@ -53,6 +55,12 @@ func (d *Daemon) UpdateConfig(cfg config.Config) {
d.mu.Unlock()
}
func (d *Daemon) UpdateBackend(inj inject.Backend) {
d.mu.Lock()
d.inj = inj
d.mu.Unlock()
}
func (d *Daemon) State() State {
d.mu.Lock()
defer d.mu.Unlock()
@ -171,6 +179,14 @@ func (d *Daemon) stopAndProcess(reason string) {
status = "clipboard failed: " + err.Error()
return
}
injCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if d.inj != nil {
if err := d.inj.Inject(injCtx, text); err != nil {
d.log.Printf("inject failed: %v", err)
}
}
}
func (d *Daemon) setIdle(msg string) {