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-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 20:40:29 +00:00
|
|
|
const Version = "v0.1.3"
|
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()
|
|
|
|
input.NonBlock()
|
|
|
|
|
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()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
srvCancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
slog.Info("Starting metrics server", "addr", srv.Addr)
|
|
|
|
srv.ListenAndServe()
|
|
|
|
}()
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
debouncer := debouncer.New(eventCh)
|
|
|
|
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)
|
|
|
|
mbus.StopTalking()
|
|
|
|
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-03-18 11:19:16 +00:00
|
|
|
mbus.StartTalking()
|
2024-10-10 20:40:29 +00:00
|
|
|
slog.Info("Started talking")
|
|
|
|
} else {
|
2024-03-18 11:19:16 +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-03-18 11:19:16 +00:00
|
|
|
mbus.StopTalking()
|
|
|
|
<-srvCtx.Done()
|
2024-03-09 21:57:46 +00:00
|
|
|
slog.Info("Exiting")
|
2024-03-09 19:34:21 +00:00
|
|
|
}
|