Compare commits
No commits in common. "b64e6d27d42cd09dac56ac93346df96db0275ee2" and "b6c47b738345387e792799ab05bd6b6462143f52" have entirely different histories.
b64e6d27d4
...
b6c47b7383
6
calls.go
6
calls.go
@ -7,13 +7,11 @@ import (
|
|||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/itd/internal/utils"
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCallNotifs(ctx context.Context, dev *infinitime.Device) error {
|
func initCallNotifs(ctx context.Context, dev *infinitime.Device) error {
|
||||||
// Connect to system bus. This connection is for method calls.
|
// Connect to system bus. This connection is for method calls.
|
||||||
conn, err := utils.NewSystemBusConn(ctx)
|
conn, err := newSystemBusConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -31,7 +29,7 @@ func initCallNotifs(ctx context.Context, dev *infinitime.Device) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to system bus. This connection is for monitoring.
|
// Connect to system bus. This connection is for monitoring.
|
||||||
monitorConn, err := utils.NewSystemBusConn(ctx)
|
monitorConn, err := newSystemBusConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package utils
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSystemBusConn(ctx context.Context) (*dbus.Conn, error) {
|
func newSystemBusConn(ctx context.Context) (*dbus.Conn, error) {
|
||||||
// Connect to dbus session bus
|
// Connect to dbus session bus
|
||||||
conn, err := dbus.SystemBusPrivate(dbus.WithContext(ctx))
|
conn, err := dbus.SystemBusPrivate(dbus.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -23,7 +23,7 @@ func NewSystemBusConn(ctx context.Context) (*dbus.Conn, error) {
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSessionBusConn(ctx context.Context) (*dbus.Conn, error) {
|
func newSessionBusConn(ctx context.Context) (*dbus.Conn, error) {
|
||||||
// Connect to dbus session bus
|
// Connect to dbus session bus
|
||||||
conn, err := dbus.SessionBusPrivate(dbus.WithContext(ctx))
|
conn, err := dbus.SessionBusPrivate(dbus.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
2
main.go
2
main.go
@ -146,7 +146,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize music controls
|
// Initialize music controls
|
||||||
err = initMusicCtrl(ctx, dev)
|
err = initMusicCtrl(dev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error initializing music control")
|
log.Error().Err(err).Msg("Error initializing music control")
|
||||||
}
|
}
|
||||||
|
5
maps.go
5
maps.go
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/itd/internal/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -20,7 +19,7 @@ const (
|
|||||||
|
|
||||||
func initPureMaps(ctx context.Context, dev *infinitime.Device) error {
|
func initPureMaps(ctx context.Context, dev *infinitime.Device) error {
|
||||||
// Connect to session bus. This connection is for method calls.
|
// Connect to session bus. This connection is for method calls.
|
||||||
conn, err := utils.NewSessionBusConn(ctx)
|
conn, err := newSessionBusConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -31,7 +30,7 @@ func initPureMaps(ctx context.Context, dev *infinitime.Device) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to session bus. This connection is for method calls.
|
// Connect to session bus. This connection is for method calls.
|
||||||
monitorConn, err := utils.NewSessionBusConn(ctx)
|
monitorConn, err := newSessionBusConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
36
music.go
36
music.go
@ -19,31 +19,29 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
|
"go.arsenm.dev/infinitime/pkg/player"
|
||||||
"go.arsenm.dev/itd/translit"
|
"go.arsenm.dev/itd/translit"
|
||||||
"go.arsenm.dev/itd/pkg/mpris"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initMusicCtrl(ctx context.Context, dev *infinitime.Device) error {
|
func initMusicCtrl(dev *infinitime.Device) error {
|
||||||
mpris.Init(ctx)
|
player.Init()
|
||||||
|
|
||||||
maps := k.Strings("notifs.translit.use")
|
maps := k.Strings("notifs.translit.use")
|
||||||
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
|
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
|
||||||
|
|
||||||
mpris.OnChange(func(ct mpris.ChangeType, val string) {
|
player.OnChange(func(ct player.ChangeType, val string) {
|
||||||
newVal := translit.Transliterate(val, maps...)
|
newVal := translit.Transliterate(val, maps...)
|
||||||
if !firmwareUpdating {
|
if !firmwareUpdating {
|
||||||
switch ct {
|
switch ct {
|
||||||
case mpris.ChangeTypeStatus:
|
case player.ChangeTypeStatus:
|
||||||
dev.Music.SetStatus(val == "Playing")
|
dev.Music.SetStatus(val == "Playing")
|
||||||
case mpris.ChangeTypeTitle:
|
case player.ChangeTypeTitle:
|
||||||
dev.Music.SetTrack(newVal)
|
dev.Music.SetTrack(newVal)
|
||||||
case mpris.ChangeTypeAlbum:
|
case player.ChangeTypeAlbum:
|
||||||
dev.Music.SetAlbum(newVal)
|
dev.Music.SetAlbum(newVal)
|
||||||
case mpris.ChangeTypeArtist:
|
case player.ChangeTypeArtist:
|
||||||
dev.Music.SetArtist(newVal)
|
dev.Music.SetArtist(newVal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,17 +58,17 @@ func initMusicCtrl(ctx context.Context, dev *infinitime.Device) error {
|
|||||||
// Perform appropriate action based on event
|
// Perform appropriate action based on event
|
||||||
switch musicEvt {
|
switch musicEvt {
|
||||||
case infinitime.MusicEventPlay:
|
case infinitime.MusicEventPlay:
|
||||||
mpris.Play()
|
player.Play()
|
||||||
case infinitime.MusicEventPause:
|
case infinitime.MusicEventPause:
|
||||||
mpris.Pause()
|
player.Pause()
|
||||||
case infinitime.MusicEventNext:
|
case infinitime.MusicEventNext:
|
||||||
mpris.Next()
|
player.Next()
|
||||||
case infinitime.MusicEventPrev:
|
case infinitime.MusicEventPrev:
|
||||||
mpris.Prev()
|
player.Prev()
|
||||||
case infinitime.MusicEventVolUp:
|
case infinitime.MusicEventVolUp:
|
||||||
mpris.VolUp(uint(k.Int("music.vol.interval")))
|
player.VolUp(uint(k.Int("music.vol.interval")))
|
||||||
case infinitime.MusicEventVolDown:
|
case infinitime.MusicEventVolDown:
|
||||||
mpris.VolDown(uint(k.Int("music.vol.interval")))
|
player.VolDown(uint(k.Int("music.vol.interval")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -26,12 +26,11 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/itd/translit"
|
"go.arsenm.dev/itd/translit"
|
||||||
"go.arsenm.dev/itd/internal/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initNotifRelay(ctx context.Context, dev *infinitime.Device) error {
|
func initNotifRelay(ctx context.Context, dev *infinitime.Device) error {
|
||||||
// Connect to dbus session bus
|
// Connect to dbus session bus
|
||||||
bus, err := utils.NewSessionBusConn(ctx)
|
bus, err := newSessionBusConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,272 +0,0 @@
|
|||||||
package mpris
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
|
||||||
"go.arsenm.dev/itd/internal/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
method, monitor *dbus.Conn
|
|
||||||
monitorCh chan *dbus.Message
|
|
||||||
onChangeOnce sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
// Init makes required connections to DBus and
|
|
||||||
// initializes change monitoring channel
|
|
||||||
func Init(ctx context.Context) error {
|
|
||||||
// Connect to session bus for monitoring
|
|
||||||
monitorConn, err := utils.NewSessionBusConn(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Add match rule for PropertiesChanged on media player
|
|
||||||
monitorConn.AddMatchSignal(
|
|
||||||
dbus.WithMatchObjectPath("/org/mpris/MediaPlayer2"),
|
|
||||||
dbus.WithMatchInterface("org.freedesktop.DBus.Properties"),
|
|
||||||
dbus.WithMatchMember("PropertiesChanged"),
|
|
||||||
)
|
|
||||||
monitorCh = make(chan *dbus.Message, 10)
|
|
||||||
monitorConn.Eavesdrop(monitorCh)
|
|
||||||
|
|
||||||
// Connect to session bus for method calls
|
|
||||||
methodConn, err := utils.NewSessionBusConn(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
method, monitor = methodConn, monitorConn
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit closes all connections and channels
|
|
||||||
func Exit() {
|
|
||||||
close(monitorCh)
|
|
||||||
method.Close()
|
|
||||||
monitor.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Play uses MPRIS to play media
|
|
||||||
func Play() error {
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
call := player.Call("org.mpris.MediaPlayer2.Player.Play", 0)
|
|
||||||
if call.Err != nil {
|
|
||||||
return call.Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pause uses MPRIS to pause media
|
|
||||||
func Pause() error {
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
call := player.Call("org.mpris.MediaPlayer2.Player.Pause", 0)
|
|
||||||
if call.Err != nil {
|
|
||||||
return call.Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next uses MPRIS to skip to next media
|
|
||||||
func Next() error {
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
call := player.Call("org.mpris.MediaPlayer2.Player.Next", 0)
|
|
||||||
if call.Err != nil {
|
|
||||||
return call.Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prev uses MPRIS to skip to previous media
|
|
||||||
func Prev() error {
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
call := player.Call("org.mpris.MediaPlayer2.Player.Previous", 0)
|
|
||||||
if call.Err != nil {
|
|
||||||
return call.Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func VolUp(percent uint) error {
|
|
||||||
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
currentVal, err := player.GetProperty("org.mpris.MediaPlayer2.Player.Volume")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
newVal := currentVal.Value().(float64) + (float64(percent) / 100)
|
|
||||||
err = player.SetProperty("org.mpris.MediaPlayer2.Player.Volume", newVal)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func VolDown(percent uint) error {
|
|
||||||
|
|
||||||
player, err := getPlayerObj()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if player != nil {
|
|
||||||
currentVal, err := player.GetProperty("org.mpris.MediaPlayer2.Player.Volume")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
newVal := currentVal.Value().(float64) - (float64(percent) / 100)
|
|
||||||
err = player.SetProperty("org.mpris.MediaPlayer2.Player.Volume", newVal)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChangeType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
ChangeTypeTitle ChangeType = iota
|
|
||||||
ChangeTypeArtist
|
|
||||||
ChangeTypeAlbum
|
|
||||||
ChangeTypeStatus
|
|
||||||
)
|
|
||||||
|
|
||||||
func (ct ChangeType) String() string {
|
|
||||||
switch ct {
|
|
||||||
case ChangeTypeTitle:
|
|
||||||
return "Title"
|
|
||||||
case ChangeTypeAlbum:
|
|
||||||
return "Album"
|
|
||||||
case ChangeTypeArtist:
|
|
||||||
return "Artist"
|
|
||||||
case ChangeTypeStatus:
|
|
||||||
return "Status"
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnChange runs cb when a value changes
|
|
||||||
func OnChange(cb func(ChangeType, string)) {
|
|
||||||
go onChangeOnce.Do(func() {
|
|
||||||
// For every message on channel
|
|
||||||
for msg := range monitorCh {
|
|
||||||
// Parse PropertiesChanged
|
|
||||||
iface, changed, ok := parsePropertiesChanged(msg)
|
|
||||||
if !ok || iface != "org.mpris.MediaPlayer2.Player" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// For every property changed
|
|
||||||
for name, val := range changed {
|
|
||||||
// If metadata changed
|
|
||||||
if name == "Metadata" {
|
|
||||||
// Get fields
|
|
||||||
fields := val.Value().(map[string]dbus.Variant)
|
|
||||||
// For every field
|
|
||||||
for name, val := range fields {
|
|
||||||
// Handle each field appropriately
|
|
||||||
if strings.HasSuffix(name, "title") {
|
|
||||||
title := val.Value().(string)
|
|
||||||
if title == "" {
|
|
||||||
title = "Unknown " + ChangeTypeTitle.String()
|
|
||||||
}
|
|
||||||
cb(ChangeTypeTitle, title)
|
|
||||||
} else if strings.HasSuffix(name, "album") {
|
|
||||||
album := val.Value().(string)
|
|
||||||
if album == "" {
|
|
||||||
album = "Unknown " + ChangeTypeAlbum.String()
|
|
||||||
}
|
|
||||||
cb(ChangeTypeAlbum, album)
|
|
||||||
} else if strings.HasSuffix(name, "artist") {
|
|
||||||
var artists string
|
|
||||||
switch artistVal := val.Value().(type) {
|
|
||||||
case string:
|
|
||||||
artists = artistVal
|
|
||||||
case []string:
|
|
||||||
artists = strings.Join(artistVal, ", ")
|
|
||||||
}
|
|
||||||
if artists == "" {
|
|
||||||
artists = "Unknown " + ChangeTypeArtist.String()
|
|
||||||
}
|
|
||||||
cb(ChangeTypeArtist, artists)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if name == "PlaybackStatus" {
|
|
||||||
// Handle status change
|
|
||||||
cb(ChangeTypeStatus, val.Value().(string))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// getPlayerNames gets all DBus MPRIS player bus names
|
|
||||||
func getPlayerNames(conn *dbus.Conn) ([]string, error) {
|
|
||||||
var names []string
|
|
||||||
err := conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var players []string
|
|
||||||
for _, name := range names {
|
|
||||||
if strings.HasPrefix(name, "org.mpris.MediaPlayer2") {
|
|
||||||
players = append(players, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return players, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPlayerObj gets the object corresponding to the first
|
|
||||||
// bus name found in DBus
|
|
||||||
func getPlayerObj() (dbus.BusObject, error) {
|
|
||||||
players, err := getPlayerNames(method)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(players) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return method.Object(players[0], "/org/mpris/MediaPlayer2"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// parsePropertiesChanged parses a DBus PropertiesChanged signal
|
|
||||||
func parsePropertiesChanged(msg *dbus.Message) (iface string, changed map[string]dbus.Variant, ok bool) {
|
|
||||||
if len(msg.Body) != 3 {
|
|
||||||
return "", nil, false
|
|
||||||
}
|
|
||||||
iface, ok = msg.Body[0].(string)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
changed, ok = msg.Body[1].(map[string]dbus.Variant)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user