aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xmpris_to_text.py68
1 files changed, 24 insertions, 44 deletions
diff --git a/mpris_to_text.py b/mpris_to_text.py
index c5d65d8..2f75350 100755
--- a/mpris_to_text.py
+++ b/mpris_to_text.py
@@ -6,16 +6,10 @@ import threading
6import argparse 6import argparse
7import signal 7import signal
8 8
9import dbus
10import dbus.mainloop.glib
11from gi.repository import GLib 9from gi.repository import GLib
12 10from pydbus import SessionBus
13from blessed import Terminal 11from blessed import Terminal
14 12
15
16dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
17
18
19class MetaWriter: 13class MetaWriter:
20 filename = "" 14 filename = ""
21 output_format = "" 15 output_format = ""
@@ -65,7 +59,8 @@ class PlayerSelector(threading.Thread):
65 def __init__(self, meta_writer, menu=None): 59 def __init__(self, meta_writer, menu=None):
66 super().__init__() 60 super().__init__()
67 61
68 self.bus = dbus.SessionBus() 62 self.bus = SessionBus()
63 self.loop = GLib.MainLoop()
69 self.meta_writer = meta_writer 64 self.meta_writer = meta_writer
70 self.menu = menu 65 self.menu = menu
71 66
@@ -76,26 +71,21 @@ class PlayerSelector(threading.Thread):
76 self.players = {} 71 self.players = {}
77 self.players_indexes = [] 72 self.players_indexes = []
78 73
79 bus_path = self.bus.get_object( 74 bus_ctrl = self.bus.get(".DBus")
80 "org.freedesktop.DBus", "/org/freedesktop/DBus")
81 bus_proxy = dbus.Interface(bus_path, "org.freedesktop.DBus")
82 75
83 for service in bus_proxy.ListNames(): 76 for service in bus_ctrl.ListNames():
84 if self.service_regex.match(service): 77 if self.service_regex.match(service):
85 split_service = service.split(".") 78 split_service = service.split(".")
86 name_start_index = len( 79 name_start_index = len(
87 split_service[0]) + len(split_service[1]) + len(split_service[2]) + 3 80 split_service[0]) + len(split_service[1]) + len(split_service[2]) + 3
88 81
89 player_path = self.bus.get_object( 82 player = self.bus.get(service, "/org/mpris/MediaPlayer2")
90 service, "/org/mpris/MediaPlayer2") 83 player_name = ""
91 player_props = dbus.Interface( 84
92 player_path, "org.freedesktop.DBus.Properties")
93 player_name = service[name_start_index:]
94 try: 85 try:
95 player_name = player_props.Get( 86 player_name = player.Identity
96 "org.mpris.MediaPlayer2.Player", "Identity")
97 except dbus.exceptions.DBusException: 87 except dbus.exceptions.DBusException:
98 pass 88 player_name = service[name_start_index:]
99 89
100 self.players[service] = player_name 90 self.players[service] = player_name
101 self.players_indexes.append(service) 91 self.players_indexes.append(service)
@@ -115,38 +105,28 @@ class PlayerSelector(threading.Thread):
115 self.active_player = "" 105 self.active_player = ""
116 106
117 if self.signal_receiver is not None: 107 if self.signal_receiver is not None:
118 self.signal_receiver.remove() 108 self.signal_receiver.disconnect()
119 109
120 if self.active_player != "": 110 if self.active_player != "":
121 self.signal_receiver = self.bus.add_signal_receiver( 111 player = self.bus.get(self.active_player, "/org/mpris/MediaPlayer2")
122 handler_function=self.playing_song_changed,
123 bus_name=self.active_player,
124 dbus_interface="org.freedesktop.DBus.Properties",
125 signal_name="PropertiesChanged",
126 path="/org/mpris/MediaPlayer2"
127 )
128 112
129 player_path = self.bus.get_object( 113 self.signal_receiver = self.bus.subscribe(
130 self.active_player, "/org/mpris/MediaPlayer2") 114 sender=self.active_player,
131 player_props = dbus.Interface( 115 object="/org/mpris/MediaPlayer2",
132 player_path, "org.freedesktop.DBus.Properties") 116 signal="PropertiesChanged",
117 signal_fired=self.playing_song_changed
118 )
133 119
134 self.meta_writer.write_meta(player_props.Get( 120 self.meta_writer.write_meta(player.Metadata)
135 "org.mpris.MediaPlayer2.Player", "Metadata"))
136 else: 121 else:
137 self.meta_writer.write("") 122 self.meta_writer.write("")
138 123
139 def run(self): 124 def run(self):
140 bus_path = self.bus.get_object( 125 bus_ctrl = self.bus.get(".DBus")
141 "org.freedesktop.DBus", "/org/freedesktop/DBus") 126 bus_ctrl.NameOwnerChanged.connect(self.dbus_name_owner_changed)
142 bus_proxy = dbus.Interface(bus_path, "org.freedesktop.DBus")
143 bus_proxy.connect_to_signal(
144 "NameOwnerChanged", self.dbus_name_owner_changed)
145 127
146 self.get_players() 128 self.get_players()
147 self.menu.refresh() 129 self.menu.refresh()
148
149 self.loop = GLib.MainLoop()
150 self.loop.run() 130 self.loop.run()
151 131
152 def quit(self): 132 def quit(self):
@@ -157,9 +137,9 @@ class PlayerSelector(threading.Thread):
157 self.get_players() 137 self.get_players()
158 self.menu.refresh() 138 self.menu.refresh()
159 139
160 def playing_song_changed(self, player, changed_properties, invalidated_properties): 140 def playing_song_changed(self, sender, object, iface, signal, params):
161 if "Metadata" in changed_properties: 141 if "Metadata" in params[1]:
162 self.meta_writer.write_meta(changed_properties["Metadata"]) 142 self.meta_writer.write_meta(params[1]["Metadata"])
163 self.menu.refresh() 143 self.menu.refresh()
164 144
165 145