79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
"github.com/holoplot/go-evdev"
|
|
)
|
|
|
|
const Version = "v0.1.1"
|
|
|
|
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()
|
|
}()
|
|
|
|
var lastOn time.Time
|
|
// Start listening for PTT key
|
|
slog.Info("Starting event listener", "version", Version)
|
|
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")
|
|
lastOn = time.Now()
|
|
obj.Call("net.sourceforge.mumble.Mumble.startTalking", 0)
|
|
}
|
|
if ev.Code == evdev.KEY_F24 && ev.Value == 0 {
|
|
speakDuration := time.Since(lastOn).String()
|
|
slog.Info("PTT OFF", "duration", speakDuration)
|
|
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
|
|
}
|
|
}
|
|
obj.Call("net.sourceforge.mumble.Mumble.stopTalking", 0)
|
|
slog.Info("Exiting")
|
|
}
|