Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified to reset and close the Stream Deck on panic or termination signal #57

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (d *Deck) updateWidgets() {

// fmt.Println("Repaint", w.Key())
if err := w.Update(); err != nil {
fatalf("error: %v\n", err)
fatalf("error: %v", err)
}
}
}
11 changes: 8 additions & 3 deletions fonts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"fmt"
"image"
"image/color"
"io/ioutil"
"os"

"github.com/flopp/go-findfont"
"github.com/golang/freetype"
Expand Down Expand Up @@ -86,16 +88,19 @@ func init() {
var err error
ttfFont, err = loadFont("Roboto-Regular.ttf")
if err != nil {
fatal(err)
fmt.Fprintln(os.Stderr, "Error loading font:", err)
os.Exit(1)
}

ttfThinFont, err = loadFont("Roboto-Thin.ttf")
if err != nil {
fatal(err)
fmt.Fprintln(os.Stderr, "Error loading font:", err)
os.Exit(1)
}

ttfBoldFont, err = loadFont("Roboto-Bold.ttf")
if err != nil {
fatal(err)
fmt.Fprintln(os.Stderr, "Error loading font:", err)
os.Exit(1)
}
}
70 changes: 50 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"

"github.com/bendahl/uinput"
Expand All @@ -19,6 +22,7 @@ var (

dbusConn *dbus.Conn
keyboard uinput.Keyboard
shutdown = make(chan error)

xorg *Xorg
recentWindows []Window
Expand All @@ -33,13 +37,11 @@ const (
)

func fatal(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
go func() { shutdown <- errors.New(fmt.Sprint(v...)) }()
}

func fatalf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
go func() { shutdown <- fmt.Errorf(format, a...) }()
}

func expandPath(base, path string) (string, error) {
Expand All @@ -59,13 +61,16 @@ func expandPath(base, path string) (string, error) {
return filepath.Abs(path)
}

func eventLoop(dev *streamdeck.Device, tch chan interface{}) {
func eventLoop(dev *streamdeck.Device, tch chan interface{}) error {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

var keyStates sync.Map
keyTimestamps := make(map[uint8]time.Time)

kch, err := dev.ReadKeys()
if err != nil {
fatal(err)
return err
}
for {
select {
Expand All @@ -74,9 +79,8 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {

case k, ok := <-kch:
if !ok {
err = dev.Open()
if err != nil {
fatal(err)
if err = dev.Open(); err != nil {
return err
}
continue
}
Expand Down Expand Up @@ -117,14 +121,30 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) {
case ActiveWindowChangedEvent:
handleActiveWindowChanged(dev, event)
}

case err := <-shutdown:
return err

case <-sigs:
fmt.Println("Shutting down...")
return nil
}
}
}

func closeDevice(dev *streamdeck.Device) {
if err := dev.Reset(); err != nil {
fmt.Fprintln(os.Stderr, "unable to reset Stream Deck")
}
if err := dev.Close(); err != nil {
fmt.Fprintln(os.Stderr, "unable to close Stream Deck")
}
}

func initDevice() (*streamdeck.Device, error) {
d, err := streamdeck.Devices()
if err != nil {
fatal(err)
return nil, err
}
if len(d) == 0 {
return nil, fmt.Errorf("no Stream Deck devices found")
Expand Down Expand Up @@ -154,38 +174,39 @@ func initDevice() (*streamdeck.Device, error) {
}
ver, err := dev.FirmwareVersion()
if err != nil {
return nil, err
return &dev, err
}
fmt.Printf("Found device with serial %s (%d buttons, firmware %s)\n",
dev.Serial, dev.Keys, ver)

if err := dev.Reset(); err != nil {
return nil, err
return &dev, err
}

if *brightness > 100 {
*brightness = 100
}
if err = dev.SetBrightness(uint8(*brightness)); err != nil {
return nil, err
return &dev, err
}

return &dev, nil
}

func main() {
flag.Parse()

func run() error {
// initialize device
dev, err := initDevice()
if dev != nil {
defer closeDevice(dev)
}
if err != nil {
fatal(err)
return fmt.Errorf("Unable to initialize Stream Deck: %s", err)
}

// initialize dbus connection
dbusConn, err = dbus.SessionBus()
if err != nil {
fatal(err)
return fmt.Errorf("Unable to connect to dbus: %s", err)
}

// initialize xorg connection and track window focus
Expand All @@ -211,9 +232,18 @@ func main() {
// load deck
deck, err = LoadDeck(dev, ".", *deckFile)
if err != nil {
fatal(err)
return fmt.Errorf("Can't load deck: %s", err)
}
deck.updateWidgets()

eventLoop(dev, tch)
return eventLoop(dev, tch)
}

func main() {
flag.Parse()

if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}