Skip to content

Commit

Permalink
Added device brightness control actions
Browse files Browse the repository at this point in the history
  • Loading branch information
henryso authored and muesli committed Feb 8, 2022
1 parent a8fdb93 commit 8dd5b7f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@ A list of available `keycodes` can be found here: [keycodes](https://github.com/
value = "value"
```

#### Device actions

Increase the brightness. If no value is specified, it will be increased by 10%.

```toml
[keys.action]
device = "brightness+5"
```

Decrease the brightness. If no value is specified, it will be decreased by 10%.

```toml
[keys.action]
device = "brightness-5"
```

Set the brightness to a specific value between 0 and 100.

```toml
[keys.action]
device = "brightness=50"
```

## More Decks!

[deckmaster-emojis](https://github.com/muesli/deckmaster-emojis), an Emoji keyboard deck
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ActionConfig struct {
Keycode string `toml:"keycode,omitempty"`
Exec string `toml:"exec,omitempty"`
Paste string `toml:"paste,omitempty"`
Device string `toml:"device,omitempty"`
DBus DBusConfig `toml:"dbus,omitempty"`
}

Expand Down
58 changes: 58 additions & 0 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/draw"
"math"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -249,6 +250,15 @@ func (d *Deck) triggerAction(dev *streamdeck.Device, index uint8, hold bool) {
if a.Exec != "" {
go executeCommand(a.Exec)
}
if a.Device != "" {
switch {
case strings.HasPrefix(a.Device, "brightness"):
d.adjustBrightness(dev, strings.TrimPrefix(a.Device, "brightness"))

default:
fmt.Fprintln(os.Stderr, "Unrecognized special action:", a.Device)
}
}
}
}

Expand All @@ -265,3 +275,51 @@ func (d *Deck) updateWidgets() {
}
}
}

// adjustBrightness adjusts the brightness.
func (d *Deck) adjustBrightness(dev *streamdeck.Device, value string) {
if len(value) == 0 {
fmt.Fprintln(os.Stderr, "No brightness value specified")
return
}

v := int64(math.MinInt64)
if len(value) > 1 {
nv, err := strconv.ParseInt(value[1:], 10, 64)
if err == nil {
v = nv
}
}

switch value[0] {
case '=': // brightness=[n]:
case '-': // brightness-[n]:
if v == math.MinInt64 {
v = 10
}
v = int64(*brightness) - v
case '+': // brightness+[n]:
if v == math.MinInt64 {
v = 10
}
v = int64(*brightness) + v
default:
v = math.MinInt64
}

if v == math.MinInt64 {
fmt.Fprintf(os.Stderr, "Could not grok the brightness from value '%s'\n", value)
return
}

if v < 1 {
v = 1
} else if v > 100 {
v = 100
}
if err := dev.SetBrightness(uint8(v)); err != nil {
fatalf("error: %v\n", err)
}

*brightness = uint(v)
}
8 changes: 4 additions & 4 deletions decks/main.deck
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@
id = "button"
[keys.widget.config]
icon = "assets/go-previous.png"
label = "Prev Act"
label = "Dim"
[keys.action]
exec = "kactivities-cli --previous-activity"
device = "brightness-10"

[[keys]]
index = 9
[keys.widget]
id = "button"
[keys.widget.config]
icon = "assets/go-next.png"
label = "Next Act"
label = "Brighten"
[keys.action]
exec = "kactivities-cli --next-activity"
device = "brightness+10"

[[keys]]
index = 10
Expand Down

0 comments on commit 8dd5b7f

Please sign in to comment.