-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for loading deck definitions, launching apps and displa…
…ying CPU usage
- Loading branch information
Showing
7 changed files
with
377 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
type DBusConfig struct { | ||
Object string `toml:"object,omitempty"` | ||
Path string `toml:"path,omitempty"` | ||
Method string `toml:"method,omitempty"` | ||
Value string `toml:"value,omitempty"` | ||
} | ||
|
||
type ActionConfig struct { | ||
Keycode string `toml:"keycode,omitempty"` | ||
Exec string `toml:"exec,omitempty"` | ||
Deck string `toml:"deck,omitempty"` | ||
DBus DBusConfig `toml:"dbus,omitempty"` | ||
} | ||
|
||
type WidgetConfig struct { | ||
ID string `toml:"id,omitempty"` | ||
Config map[string]string `toml:"config,omitempty"` | ||
} | ||
|
||
type KeyConfig struct { | ||
Index uint8 `toml:"index"` | ||
Widget WidgetConfig `toml:"widget"` | ||
Action *ActionConfig `toml:"action,omitempty"` | ||
} | ||
type Keys []KeyConfig | ||
|
||
type DeckConfig struct { | ||
Keys Keys `toml:"keys"` | ||
} | ||
|
||
// LoadConfig loads config from filename | ||
func LoadConfig(filename string) (DeckConfig, error) { | ||
config := DeckConfig{} | ||
|
||
b, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
_, err = toml.Decode(string(b), &config) | ||
return config, err | ||
} | ||
|
||
// Save writes config as json to filename | ||
func (c DeckConfig) Save(filename string) error { | ||
var b bytes.Buffer | ||
e := toml.NewEncoder(&b) | ||
err := e.Encode(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(filename, b.Bytes(), 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type Deck struct { | ||
Widgets []Widget | ||
} | ||
|
||
func LoadDeck(deck string) (*Deck, error) { | ||
d := Deck{} | ||
dc, err := LoadConfig(deck) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, k := range dc.Keys { | ||
w := NewWidget(k.Index, k.Widget.ID, k.Action, k.Widget.Config) | ||
d.Widgets = append(d.Widgets, w) | ||
} | ||
|
||
return &d, nil | ||
} | ||
|
||
func (d *Deck) triggerAction(index uint8) { | ||
for _, w := range d.Widgets { | ||
if w.Key() == index { | ||
a := w.Action() | ||
if a != nil { | ||
fmt.Println("Executing overwritten action") | ||
if a.Deck != "" { | ||
d, err := LoadDeck(a.Deck) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = dev.Clear() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
deck = d | ||
deck.updateWidgets() | ||
} | ||
} else { | ||
w.TriggerAction() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (d *Deck) updateWidgets() { | ||
for _, w := range d.Widgets { | ||
w.Update(&dev) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/golang/freetype" | ||
"github.com/golang/freetype/truetype" | ||
"github.com/muesli/streamdeck" | ||
) | ||
|
||
var ( | ||
ttfFont *truetype.Font | ||
) | ||
|
||
type Widget interface { | ||
Key() uint8 | ||
Update(dev *streamdeck.Device) | ||
Action() *ActionConfig | ||
TriggerAction() | ||
} | ||
|
||
type BaseWidget struct { | ||
key uint8 | ||
action *ActionConfig | ||
} | ||
|
||
func (w *BaseWidget) Key() uint8 { | ||
return w.key | ||
} | ||
|
||
func (w *BaseWidget) Action() *ActionConfig { | ||
return w.action | ||
} | ||
|
||
func NewWidget(index uint8, id string, action *ActionConfig, config map[string]string) Widget { | ||
bw := BaseWidget{index, action} | ||
|
||
switch id { | ||
case "recentWindow": | ||
i, err := strconv.ParseUint(config["window"], 10, 64) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return &RecentWindowWidget{BaseWidget: bw, window: uint8(i)} | ||
case "top": | ||
return &TopWidget{bw} | ||
case "launcher": | ||
return &LauncherWidget{BaseWidget: bw, launch: config["exec"], icon: config["icon"]} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
ttf, err := ioutil.ReadFile("/usr/share/fonts/TTF/Roboto-Medium.ttf") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ttfFont, err = freetype.ParseFont(ttf) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"image" | ||
"log" | ||
"os" | ||
"syscall" | ||
|
||
"github.com/muesli/streamdeck" | ||
"github.com/nfnt/resize" | ||
) | ||
|
||
type LauncherWidget struct { | ||
BaseWidget | ||
icon string | ||
launch string | ||
} | ||
|
||
func (w *LauncherWidget) Update(dev *streamdeck.Device) { | ||
f, err := os.Open(w.icon) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
img, _, err := image.Decode(f) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = dev.SetImage(w.key, resize.Resize(72, 72, img, resize.Lanczos3)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func (w *LauncherWidget) TriggerAction() { | ||
var sysproc = &syscall.SysProcAttr{Noctty: true} | ||
var attr = os.ProcAttr{ | ||
Dir: ".", | ||
Env: os.Environ(), | ||
Files: []*os.File{ | ||
os.Stdin, | ||
nil, | ||
nil, | ||
}, | ||
Sys: sysproc, | ||
} | ||
proc, err := os.StartProcess(w.launch, []string{w.launch}, &attr) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = proc.Release() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.