ghettoptt/main.go

72 lines
1.4 KiB
Go

package main
import (
"context"
"errors"
"log/slog"
"os"
"os/signal"
"github.com/godbus/dbus/v5"
"github.com/holoplot/go-evdev"
)
func main() {
// 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()
// Connect to dbus
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
defer conn.Close()
obj := conn.Object("net.sourceforge.mumble.mumble", "/")
// Start reading input events
input, err := evdev.OpenWithFlags("/dev/input/event1", os.O_RDONLY)
if err != nil {
panic(err)
}
defer input.Close()
input.NonBlock()
var done bool
// Listen for context cancellation
go func() {
<-ctx.Done()
done = true
input.Close()
}()
// Start listening for PTT key
for !done {
ev, err := input.ReadOne()
if err != nil {
if errors.Is(err, os.ErrClosed) {
continue
}
slog.Error("Error reading fron input device", "error", err)
os.Exit(1)
}
if ev.Code == evdev.KEY_F24 && ev.Value == 1 {
slog.Info("PTT ON")
obj.Call("net.sourceforge.mumble.Mumble.startTalking", 0)
}
if ev.Code == evdev.KEY_F24 && ev.Value == 0 {
slog.Info("PTT OFF")
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
}
}
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
slog.Info("Exiting")
}