-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshairport-display-qt.py
executable file
·382 lines (266 loc) · 11.4 KB
/
shairport-display-qt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env /usr/bin/python3
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QProgressBar, QDesktopWidget
from PyQt5 import uic
from PyQt5.QtCore import QTimer, Qt, QPropertyAnimation
from PyQt5.QtGui import QPixmap, QFont
import dbus
import dbus.mainloop.glib
import datetime
import signal
import sys
import os
import time
import logging
class ShairportSyncClient(QApplication):
def __init__(self, argv):
self.ArtPath = None
self.Title = None
self.Artist = None
self.Album = None
super().__init__(argv)
self.log = logging.getLogger("shairport-display")
self.format = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s', "%Y-%m-%d %H:%M:%S")
self.handler = logging.StreamHandler(stream=sys.stdout)
self.handler.setFormatter(self.format)
self.handler.setLevel(logging.DEBUG)
self.log.addHandler(self.handler)
self.log.setLevel(logging.DEBUG)
self.log.info("Starting application")
self.backlight = ""
for (topdir, backlights, _) in os.walk("/sys/class/backlight/"):
for backlight in backlights:
with open(topdir + backlight + "/max_brightness", "r") as f:
self.backlight = topdir + backlight
self.max_brightness = f.read()
if self.backlight:
self.log.debug("using backlight: '" + self.backlight + "'")
else:
self.log.debug("no backlight found, backlight control disabled")
self.properties_changed = None
self._setup_loop()
self._setup_bus()
self._setup_signals()
self.length = 0
self.progress = 0
self.duration = 500 # miliseconds
self.timer = None
self.window = uic.loadUi(os.path.dirname(argv[0]) + "/shairport-display.ui")
# self.window.resize(QDesktopWidget().availableGeometry().size());
self.window.setStyleSheet("background-color : white; color : black;");
self.window.setAutoFillBackground(True);
self.window.resizeEvent = self.onResize
self.window.keyPressEvent = self.keyPressEvent
self.window.resize(800, 480)
self.window.show()
self.Art = self.window.findChild(QLabel, 'CoverArt')
self.Title = self.window.findChild(QLabel, 'Title')
self.Title.setFont(QFont("Montserrat", 20, QFont.Bold))
self.Artist = self.window.findChild(QLabel, 'Artist')
self.Artist.setFont(QFont("Montserrat", 16, QFont.Normal))
self.Album = self.window.findChild(QLabel, 'Album')
self.Album.setFont(QFont("Montserrat", 16, QFont.Normal))
self.ProgressBar = self.window.findChild(QProgressBar, 'ProgressBar')
# self.ProgressBar.setStyleSheet("QProgressBar {border: 0px solid gray; height: 2px; max-height:2px;}")
# self.ProgressBar.setStyleSheet("QProgressBar::chunk {background: light blue;}")
# self.animation = QPropertyAnimation(self.ProgressBar, b"value")
self.ProgressBar.setRange(0, 100)
self.Remaining = self.window.findChild(QLabel, 'Remaining')
self.Remaining.setFont(QFont("Montserrat", 10, QFont.Normal))
self.Elapsed = self.window.findChild(QLabel, 'Elapsed')
self.Elapsed.setFont(QFont("Montserrat", 10, QFont.Normal))
self._clear_display()
self._initialize_display()
self.window.destroyed.connect(self.quit)
def onResize(self, event):
size = self.window.size();
self.log.info("width: %d", size.width())
self.log.info("height: %d", size.height())
if self.Title is not None:
self.Title.setMaximumWidth(int(size.width() / 2))
if self.Artist is not None:
self.Artist.setMaximumWidth(int(size.width() / 2))
if self.Album is not None:
self.Album.setMaximumWidth(int(size.width() / 2))
if self.ArtPath is not None:
pixmap = QPixmap(self.ArtPath)
if pixmap.width() >= pixmap.height():
self.Art.setPixmap(pixmap.scaledToWidth(int((size.width() / 2) - 40)))
else:
self.Art.setPixmap(pixmap.scaledToHeight(int((size.width() / 2) - 40)))
def event(self, e):
return QApplication.event(self, e)
def _tickEvent(self):
if self.length != 0:
# self.animation.setStartValue(self.progress / self.length * 100.0)
self.progress += self.duration / 1000.0
# self.animation.setEndValue(self.progress / self.length * 100.0)
# self.log.debug("progress: %f", self.progress / self.length * 100.0)
# self.log.debug("elapsed: %s", str(datetime.timedelta(seconds=self.progress)))
self.ProgressBar.setValue(int(self.progress / self.length * 100))
# self.animation.setDuration(self.duration)
# self.animation.start()
elapsed = round(self.progress)
elapsed_time = datetime.timedelta(seconds=elapsed)
remaining_time = datetime.timedelta(seconds=self.length - elapsed)
elapsed_formated = ':'.join(str(elapsed_time).split(':')[1:])
remaining_formated = ':'.join(str(remaining_time).split(':')[1:])
self.Elapsed.setText(elapsed_formated)
self.Remaining.setText("-" + remaining_formated)
return True
def quit(self, *args):
self.log.info("Stopping application")
self.properties_changed.remove()
self._set_backlight(False)
QApplication.quit()
def _setup_loop(self):
self._loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
def _setup_bus(self):
dbus.set_default_main_loop(self._loop)
if dbus.SystemBus().name_has_owner("org.gnome.ShairportSync"):
self.log.debug("shairport-sync dbus service is running on the system bus")
self._bus = dbus.SystemBus()
return
if dbus.SessionBus().name_has_owner("org.gnome.ShairportSync"):
self.log.debug("shairport-sync dbus service is running on the session bus")
self._bus = dbus.SessionBus()
return
self.log.error("shairport-sync dbus service is not running")
exit(1)
def _fullscreen_mode(self):
if self.window.windowState() & Qt.WindowState.WindowFullScreen:
self.log.debug("normal")
self.window.showNormal()
else:
self.log.debug("fullscreen")
self.window.showFullScreen()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Q:
self.quit()
if event.key() == Qt.Key_F:
self._fullscreen_mode()
def _set_backlight(self, power):
if self.backlight:
try:
with open(self.backlight + "/brightness", "w") as f:
if power:
f.write(self.max_brightness)
else:
f.write("0")
except FileNotFoundError:
self.log.warning("path: '" + self.backlight + "' does not exist")
except PermissionError:
self.log.warning("incorrect permissions for '" + self.backlight + "'")
def _initialize_display(self):
self._set_backlight(True)
try:
result = self._bus.call_blocking("org.gnome.ShairportSync", "/org/gnome/ShairportSync", "org.freedesktop.DBus.Properties", "Get", "ss", ["org.gnome.ShairportSync.RemoteControl", "Metadata"])
except dbus.exceptions.DBusException:
self.log.warning("shairport-sync is not running on the bus")
return
try:
metadata = { "art" : result['mpris:artUrl'].split("://")[-1],
"title" : result['xesam:title'],
"artist" : ", ".join(result['xesam:artist']),
"album" : result['xesam:album'],
"length" : result['mpris:length'],
}
except KeyError:
self.log.warning("no metadata available to initialize the display")
return
self._set_metadata(metadata)
def _setup_signals(self):
self.properties_changed = self._bus.add_signal_receiver(handler_function=self._display_metadata,
signal_name='PropertiesChanged',
dbus_interface='org.freedesktop.DBus.Properties',
bus_name='org.gnome.ShairportSync',
member_keyword='signal')
def _set_metadata(self, metadata):
self.log.debug("Metadata available")
for key in metadata:
self.log.info("%s: %s", key, metadata[key])
self.Title.setText(metadata["title"])
self.Artist.setText(metadata["artist"])
self.Album.setText(metadata["album"])
self.ProgressBar.setVisible(True)
self.Elapsed.setVisible(True)
self.Remaining.setVisible(True)
size = self.window.size();
self.Title.setMaximumWidth(int(size.width() / 2))
self.Artist.setMaximumWidth(int(size.width() / 2))
self.Album.setMaximumWidth(int(size.width() / 2))
self.ArtPath = metadata["art"]
pixmap = QPixmap(self.ArtPath)
if pixmap.width() >= pixmap.height():
self.Art.setPixmap(pixmap.scaledToWidth(int((size.width() / 2) - 40)))
else:
self.Art.setPixmap(pixmap.scaledToHeight(int((size.width() / 2) - 40)))
self.log.info("length: %s", str(datetime.timedelta(microseconds=metadata["length"])))
def _stop_timer(self):
if self.timer is not None:
self.log.debug("stopping timer")
self.timer.stop()
def _start_timer(self):
self.log.debug("starting timer")
self.timer = QTimer()
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self._tickEvent)
self.timer.start(self.duration)
def _clear_display(self):
self._set_backlight(False)
self.Art.clear()
self.Title.clear()
self.Artist.clear()
self.Album.clear()
self.Elapsed.clear();
self.Remaining.clear();
self.ProgressBar.setVisible(False);
def _display_metadata(self, *args, **kwargs):
interface = args[0]
data = args[1]
self.log.debug("Recieved signal for %s", interface)
if interface == "org.gnome.ShairportSync.RemoteControl":
if 'Metadata' in data:
try:
metadata = { "art" : data['Metadata']['mpris:artUrl'].split("://")[-1],
"title" : data['Metadata']['xesam:title'],
"artist" : ', '.join(data['Metadata']['xesam:artist']),
"album" : data['Metadata']['xesam:album'],
"length" : data['Metadata']['mpris:length'],
}
except KeyError:
self.log.warning("no metadata available to initialize the display")
return
self._set_metadata(metadata)
if 'ProgressString' in data:
start, current, end = [int(x) for x in data['ProgressString'].split('/')]
self.log.debug("start: %d", start)
self.log.debug("current: %d", current)
self.log.debug("end: %d", end)
self.length = round((end - start) / 44100)
elapsed = round((current - start) / 44100)
self.log.debug("length: %s", str(datetime.timedelta(seconds=self.length)))
self.log.debug("elapsed: %s", str(datetime.timedelta(seconds=elapsed)))
self.progress = elapsed
self._start_timer()
if 'PlayerState' in data:
state = data['PlayerState']
self.log.info("PlayerState: %s", state)
if state == "Stopped":
self._stop_timer()
elif state == "Playing":
self._start_timer()
elif state == "Paused":
pass
if interface == "org.gnome.ShairportSync":
if "Active" in data:
if data["Active"]:
self.log.info("device connected")
self._initialize_display()
else:
self.log.info("device disconnected")
self._clear_display()
if (__name__ == "__main__"):
client = ShairportSyncClient(sys.argv)
signal.signal(signal.SIGINT, lambda *args: client.quit())
client.startTimer(500)
client.exec_()