ghettoptt/main.go

75 lines
1.5 KiB
Go
Raw Normal View History

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-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
"github.com/godbus/dbus/v5"
"github.com/holoplot/go-evdev"
)
2024-03-10 02:24:11 +00:00
const Version = "v0.1.1"
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()
// Connect to dbus
2024-03-09 19:34:21 +00:00
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)
}
2024-03-09 21:57:46 +00:00
defer input.Close()
input.NonBlock()
var done bool
// Listen for context cancellation
go func() {
<-ctx.Done()
done = true
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-03-09 21:57:46 +00:00
for !done {
2024-03-09 19:34:21 +00:00
ev, err := input.ReadOne()
if err != nil {
2024-03-09 21:57:46 +00:00
if errors.Is(err, os.ErrClosed) {
continue
}
slog.Error("Error reading fron input device", "error", err)
os.Exit(1)
2024-03-09 19:34:21 +00:00
}
if ev.Code == evdev.KEY_F24 && ev.Value == 1 {
2024-03-09 21:57:46 +00:00
slog.Info("PTT ON")
2024-03-10 02:20:32 +00:00
obj.Call("net.sourceforge.mumble.Mumble.startTalking", 0)
2024-03-09 19:34:21 +00:00
}
if ev.Code == evdev.KEY_F24 && ev.Value == 0 {
2024-03-09 21:57:46 +00:00
slog.Info("PTT OFF")
2024-03-10 02:20:32 +00:00
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
2024-03-09 19:34:21 +00:00
}
}
2024-03-10 02:20:32 +00:00
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
2024-03-09 21:57:46 +00:00
slog.Info("Exiting")
2024-03-09 19:34:21 +00:00
}