Skip to content

Commit

Permalink
Drop dev as a global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jun 3, 2021
1 parent add8275 commit e765fd7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
18 changes: 9 additions & 9 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
}
if dc.Background != "" {
bgpath := findImage(filepath.Dir(abs), dc.Background)
if err := d.loadBackground(bgpath); err != nil {
if err := d.loadBackground(dev, bgpath); err != nil {
return nil, err
}
}
Expand All @@ -56,7 +56,7 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
}

for i := uint8(0); i < dev.Columns*dev.Rows; i++ {
bg := d.backgroundForKey(i)
bg := d.backgroundForKey(dev, i)

var w Widget
if k, found := keyMap[i]; found {
Expand All @@ -72,7 +72,7 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
}

// loads a background image.
func (d *Deck) loadBackground(bg string) error {
func (d *Deck) loadBackground(dev *streamdeck.Device, bg string) error {
f, err := os.Open(bg)
if err != nil {
return err
Expand Down Expand Up @@ -101,7 +101,7 @@ func (d *Deck) loadBackground(bg string) error {
}

// returns the background image for an individual key.
func (d Deck) backgroundForKey(key uint8) image.Image {
func (d Deck) backgroundForKey(dev *streamdeck.Device, key uint8) image.Image {
padding := int(dev.Padding)
pixels := int(dev.Pixels)
bg := image.NewRGBA(image.Rect(0, 0, pixels, pixels))
Expand Down Expand Up @@ -194,7 +194,7 @@ func executeCommand(cmd string) {
}

// triggerAction triggers an action.
func (d *Deck) triggerAction(index uint8, hold bool) {
func (d *Deck) triggerAction(dev *streamdeck.Device, index uint8, hold bool) {
for _, w := range d.Widgets {
if w.Key() == index {
var a *ActionConfig
Expand All @@ -207,7 +207,7 @@ func (d *Deck) triggerAction(index uint8, hold bool) {
if a != nil {
// log.Println("Executing overloaded action")
if a.Deck != "" {
d, err := LoadDeck(&dev, filepath.Dir(d.File), a.Deck)
d, err := LoadDeck(dev, filepath.Dir(d.File), a.Deck)
if err != nil {
log.Fatal(err)
}
Expand All @@ -217,7 +217,7 @@ func (d *Deck) triggerAction(index uint8, hold bool) {
}

deck = d
deck.updateWidgets()
deck.updateWidgets(dev)
}
if a.Keycode != "" {
emulateKeyPresses(a.Keycode)
Expand All @@ -239,9 +239,9 @@ func (d *Deck) triggerAction(index uint8, hold bool) {
}

// updateWidgets updates/repaints all the widgets.
func (d *Deck) updateWidgets() {
func (d *Deck) updateWidgets(dev *streamdeck.Device) {
for _, w := range d.Widgets {
if err := w.Update(&dev); err != nil {
if err := w.Update(dev); err != nil {
log.Fatalf("error: %v", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var (

// maxPointSize returns the maximum point size we can use to fit text inside
// width and height, as well as the resulting text-width in pixels.
func maxPointSize(text string, c *freetype.Context, width, height int) (float64, int) {
func maxPointSize(text string, c *freetype.Context, dpi uint, width, height int) (float64, int) {
// never let the font size exceed the requested height
fontsize := float64(height<<6) / float64(dev.DPI) / (64.0 / 72.0)
fontsize := float64(height<<6) / float64(dpi) / (64.0 / 72.0)

// offset initial loop iteration
fontsize++
Expand Down Expand Up @@ -56,9 +56,9 @@ func fontByName(font string) *truetype.Font {
}
}

func ftContext(img *image.RGBA, ttf *truetype.Font, fontsize float64) *freetype.Context {
func ftContext(img *image.RGBA, ttf *truetype.Font, dpi uint, fontsize float64) *freetype.Context {
c := freetype.NewContext()
c.SetDPI(float64(dev.DPI))
c.SetDPI(float64(dpi))
c.SetFont(ttf)
c.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
c.SetDst(img)
Expand Down
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

var (
dev streamdeck.Device
deck *Deck

dbusConn *dbus.Conn
Expand Down Expand Up @@ -48,10 +47,10 @@ func handleActiveWindowChanged(dev streamdeck.Device, event ActiveWindowChangedE
if len(recentWindows) > keys {
recentWindows = recentWindows[0:keys]
}
deck.updateWidgets()
deck.updateWidgets(&dev)
}

func handleWindowClosed(_ streamdeck.Device, event WindowClosedEvent) {
func handleWindowClosed(dev streamdeck.Device, event WindowClosedEvent) {
i := 0
for _, rw := range recentWindows {
if rw.ID == event.Window.ID {
Expand All @@ -62,7 +61,7 @@ func handleWindowClosed(_ streamdeck.Device, event WindowClosedEvent) {
i++
}
recentWindows = recentWindows[:i]
deck.updateWidgets()
deck.updateWidgets(&dev)
}

func main() {
Expand Down Expand Up @@ -90,7 +89,7 @@ func main() {
fmt.Println("No Stream Deck devices found.")
return
}
dev = d[0]
dev := d[0]

err = dev.Open()
if err != nil {
Expand All @@ -112,7 +111,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
deck.updateWidgets()
deck.updateWidgets(&dev)

if *brightness > 100 {
*brightness = 100
Expand Down Expand Up @@ -140,7 +139,7 @@ func main() {
for {
select {
case <-time.After(900 * time.Millisecond):
deck.updateWidgets()
deck.updateWidgets(&dev)

case k, ok := <-kch:
if !ok {
Expand All @@ -163,7 +162,7 @@ func main() {
// key was released
if time.Since(keyTimestamps[k.Index]) < 200*time.Millisecond {
// log.Println("Triggering short action")
deck.triggerAction(k.Index, false)
deck.triggerAction(&dev, k.Index, false)
}
}
if !state && k.Pressed {
Expand All @@ -175,7 +174,7 @@ func main() {
if state, ok := keyStates.Load(k.Index); ok && state.(bool) {
// key still pressed
// log.Println("Triggering long action")
deck.triggerAction(k.Index, true)
deck.triggerAction(&dev, k.Index, true)
}
}()
}
Expand Down
14 changes: 8 additions & 6 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (w *BaseWidget) Update(dev *streamdeck.Device) error {
}

// NewBaseWidget returns a new BaseWidget.
func NewBaseWidget(index uint8, action *ActionConfig, actionHold *ActionConfig, bg image.Image) *BaseWidget {
func NewBaseWidget(index uint8, action, actionHold *ActionConfig, bg image.Image) *BaseWidget {
return &BaseWidget{
key: index,
action: action,
Expand All @@ -75,7 +75,7 @@ func NewBaseWidget(index uint8, action *ActionConfig, actionHold *ActionConfig,
}

// NewWidget initializes a widget.
func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionConfig, bg image.Image, config map[string]string) Widget {
func NewWidget(index uint8, id string, action, actionHold *ActionConfig, bg image.Image, config map[string]string) Widget {
bw := NewBaseWidget(index, action, actionHold, bg)

switch id {
Expand Down Expand Up @@ -174,18 +174,20 @@ func drawImage(img *image.RGBA, path string, size int, pt image.Point) error {
return nil
}

func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, text string, fontsize float64, pt image.Point) {
c := ftContext(img, ttf, fontsize)
func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, text string, dpi uint, fontsize float64, pt image.Point) {
c := ftContext(img, ttf, dpi, fontsize)

if fontsize <= 0 {
// pick biggest available height to fit the string
fontsize, _ = maxPointSize(text, ftContext(img, ttf, fontsize), bounds.Dx(), bounds.Dy())
fontsize, _ = maxPointSize(text,
ftContext(img, ttf, dpi, fontsize), dpi,
bounds.Dx(), bounds.Dy())
c.SetFontSize(fontsize)
}

if pt.X < 0 {
// center horizontally
extent, _ := ftContext(img, ttf, fontsize).DrawString(text, freetype.Pt(0, 0))
extent, _ := ftContext(img, ttf, dpi, fontsize).DrawString(text, freetype.Pt(0, 0))
actwidth := extent.X.Floor()
xcenter := float64(bounds.Dx())/2.0 - (float64(actwidth) / 2.0)
pt = image.Pt(bounds.Min.X+int(xcenter), pt.Y)
Expand Down
1 change: 1 addition & 0 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
bounds,
ttfFont,
w.label,
dev.DPI,
w.fontsize,
image.Pt(-1, -1))
} else if w.icon != "" {
Expand Down
1 change: 1 addition & 0 deletions widget_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) error {
drawString(img, image.Rect(0, lower, size, upper),
font,
str,
dev.DPI,
-1,
image.Pt(-1, -1))
}
Expand Down
2 changes: 2 additions & 0 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
bounds,
ttfFont,
strconv.FormatInt(int64(value), 10),
dev.DPI,
13,
image.Pt(-1, -1))

Expand All @@ -97,6 +98,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
bounds,
ttfFont,
"% "+label,
dev.DPI,
-1,
image.Pt(-1, -1))

Expand Down

0 comments on commit e765fd7

Please sign in to comment.