forked from qtile/qtile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 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
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,87 @@ | ||
# -*- coding: utf-8 -*- | ||
# depends on python-mpd | ||
|
||
|
||
# TODO: check if UI hangs in case of network issues and such | ||
# TODO: python-mpd supports idle proto, can widgets be push instead of pull? | ||
# TODO: a teardown hook so I can client.disconnect() ? | ||
# TODO: some kind of templating to make shown info configurable | ||
# TODO: best practice to handle failures? just write to stderr? | ||
|
||
from .. import hook, bar, manager | ||
import base | ||
from mpd import MPDClient, CommandError | ||
from socket import error as SocketError | ||
|
||
class Mpd(base._TextBox): | ||
""" | ||
An mpd widget | ||
""" | ||
defaults = manager.Defaults( | ||
("font", "Arial", "Mpd widget font"), | ||
("fontsize", None, "Mpd widget pixel size. Calculated if None."), | ||
("padding", None, "Mpd widget padding. Calculated if None."), | ||
("background", "000000", "Background colour"), | ||
("foreground", "ffffff", "Foreground colour") | ||
) | ||
def __init__(self, width=bar.CALCULATED, host='localhost', port=6600, password=False, **config): | ||
""" | ||
- host: host to connect to | ||
- port: port to connect to | ||
- password: password to use | ||
- width: A fixed width, or bar.CALCULATED to calculate the width | ||
automatically (which is recommended). | ||
""" | ||
self.host = host | ||
self.port = port | ||
self.password = password | ||
base._TextBox.__init__(self, " ", width, **config) | ||
self.client = MPDClient() | ||
CON_ID = {'host': self.host, 'port': self.port} | ||
if not self.mpdConnect(CON_ID): | ||
print >> stderr, 'fail to connect MPD server.' | ||
if self.password: | ||
if not self.mpdAuth(self.password): | ||
print >> stderr, 'Error trying to pass auth.' | ||
client.disconnect() | ||
|
||
def mpdConnect(self, con_id): | ||
""" | ||
Simple wrapper to connect MPD. | ||
""" | ||
try: | ||
self.client.connect(**con_id) | ||
except SocketError: | ||
return False | ||
return True | ||
|
||
def mpdAuth(self, secret): | ||
""" | ||
Authenticate | ||
""" | ||
try: | ||
self.client.password(secret) | ||
except CommandError: | ||
return False | ||
return True | ||
|
||
def _configure(self, qtile, bar): | ||
#TODO: don't do this if connection failed | ||
base._TextBox._configure(self, qtile, bar) | ||
hook.subscribe.tick(self.update) | ||
|
||
def update(self): | ||
song = self.client.currentsong() | ||
if song: | ||
artist = '' | ||
title = '' | ||
if 'artist' in song: | ||
artist = song['artist'] | ||
if 'title' in song: | ||
title = song['title'] | ||
playing = "%s - %s" % (artist, title) | ||
else: | ||
playing = ' - ' | ||
if self.text != playing: | ||
self.text = playing | ||
self.bar.draw() |