2024-03-09 19:34:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-09 21:57:46 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"log/slog"
|
2024-03-18 11:19:16 +00:00
|
|
|
"net/http"
|
2024-03-09 19:34:21 +00:00
|
|
|
"os"
|
2024-03-09 21:57:46 +00:00
|
|
|
"os/signal"
|
2024-10-10 21:13:00 +00:00
|
|
|
"time"
|
2024-03-09 19:34:21 +00:00
|
|
|
|
2024-03-18 11:19:16 +00:00
|
|
|
"git.t-juice.club/torjus/ghettoptt/bus"
|
2024-10-10 20:40:29 +00:00
|
|
|
"git.t-juice.club/torjus/ghettoptt/debouncer"
|
2024-03-18 11:19:16 +00:00
|
|
|
"git.t-juice.club/torjus/ghettoptt/metrics"
|
2024-03-09 19:34:21 +00:00
|
|
|
"github.com/holoplot/go-evdev"
|
2024-03-18 11:19:16 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2024-03-09 19:34:21 +00:00
|
|
|
)
|
|
|
|
|
2024-10-10 21:13:00 +00:00
|
|
|
const Version = "v0.1.4"
|
2024-03-10 02:24:11 +00:00
|
|
|
|
2024-03-09 19:34:21 +00:00
|
|
|
func main() {
|
2024-03-09 21:57:46 +00:00
|
|
|
// Setup logger
|
|
|
|
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
|
|
|
|
logger := slog.New(h)
|
|
|
|
slog.SetDefault(logger)
|
|
|
|
|
|
|
|
// Setup signal handler
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer stop()
|
|
|
|
|
2024-03-18 11:19:16 +00:00
|
|
|
// Setup metrics
|
|
|
|
m := metrics.NewMumbleMetricsCollector()
|
2024-03-09 19:34:21 +00:00
|
|
|
|
2024-03-18 11:19:16 +00:00
|
|
|
mbus := bus.NewMumbleBus(m)
|
|
|
|
defer mbus.Close()
|
2024-03-09 19:34:21 +00:00
|
|
|
|
|
|
|
// Start reading input events
|
2024-06-03 01:53:06 +00:00
|
|
|
input, err := evdev.OpenWithFlags("/dev/input/by-id/usb-SteelSeries_SteelSeries_Rival_710_Gaming_Mouse-if02-event-kbd", os.O_RDONLY)
|
2024-03-09 19:34:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-03-09 21:57:46 +00:00
|
|
|
defer input.Close()
|
2024-10-10 23:17:33 +00:00
|
|
|
if err := input.NonBlock(); err != nil {
|
|
|
|
slog.Error("Failed to set non-blocking mode", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-03-09 21:57:46 +00:00
|
|
|
|
2024-03-18 11:19:16 +00:00
|
|
|
// Start metrics server
|
|
|
|
srvCtx, srvCancel := context.WithCancel(context.Background())
|
|
|
|
defer srvCancel()
|
|
|
|
go func() {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
|
|
srv := http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
Addr: ":8989",
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2024-10-10 23:17:33 +00:00
|
|
|
_ = srv.Shutdown(context.Background())
|
2024-03-18 11:19:16 +00:00
|
|
|
srvCancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
slog.Info("Starting metrics server", "addr", srv.Addr)
|
2024-10-10 23:17:33 +00:00
|
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
slog.Warn("Failed to start metrics server", "error", err)
|
|
|
|
}
|
2024-03-18 11:19:16 +00:00
|
|
|
}()
|
|
|
|
|
2024-03-09 21:57:46 +00:00
|
|
|
// Listen for context cancellation
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
input.Close()
|
|
|
|
}()
|
2024-03-09 19:34:21 +00:00
|
|
|
|
2024-03-09 21:57:46 +00:00
|
|
|
// Start listening for PTT key
|
2024-03-10 02:24:11 +00:00
|
|
|
slog.Info("Starting event listener", "version", Version)
|
2024-10-10 20:40:29 +00:00
|
|
|
eventCh := make(chan bool)
|
|
|
|
doneCtx, doneCancel := context.WithCancel(srvCtx)
|
|
|
|
defer doneCancel()
|
|
|
|
|
2024-10-10 21:13:00 +00:00
|
|
|
debouncer := debouncer.New(eventCh, 400*time.Millisecond)
|
2024-10-10 20:40:29 +00:00
|
|
|
go func() {
|
|
|
|
var done bool
|
|
|
|
for !done {
|
|
|
|
ev, err := input.ReadOne()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrClosed) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
slog.Error("Error reading from input device", "error", err)
|
2024-10-10 23:17:33 +00:00
|
|
|
_ = mbus.StopTalking()
|
2024-10-10 20:40:29 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if ev.Code == evdev.KEY_F24 && ev.Value == 1 {
|
|
|
|
slog.Debug("PTT ON")
|
|
|
|
eventCh <- true
|
|
|
|
}
|
|
|
|
if ev.Code == evdev.KEY_F24 && ev.Value == 0 {
|
|
|
|
slog.Debug("PTT OFF")
|
|
|
|
eventCh <- false
|
|
|
|
}
|
|
|
|
if doneCtx.Err() != nil {
|
|
|
|
close(eventCh)
|
|
|
|
done = true
|
2024-03-09 21:57:46 +00:00
|
|
|
}
|
2024-03-09 19:34:21 +00:00
|
|
|
}
|
2024-10-10 20:40:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for v := range debouncer.C {
|
|
|
|
if v {
|
2024-10-10 23:17:33 +00:00
|
|
|
_ = mbus.StartTalking()
|
2024-10-10 20:40:29 +00:00
|
|
|
slog.Info("Started talking")
|
|
|
|
} else {
|
2024-10-10 23:17:33 +00:00
|
|
|
_ = mbus.StopTalking()
|
2024-10-10 20:40:29 +00:00
|
|
|
slog.Info("Stopped talking")
|
2024-03-09 19:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-10 20:40:29 +00:00
|
|
|
|
2024-10-10 23:17:33 +00:00
|
|
|
_ = mbus.StopTalking()
|
2024-03-18 11:19:16 +00:00
|
|
|
<-srvCtx.Done()
|
2024-03-09 21:57:46 +00:00
|
|
|
slog.Info("Exiting")
|
2024-03-09 19:34:21 +00:00
|
|
|
}
|