Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --managed option to disable timer #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/mpDris2.in.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'port': None,
'password': None,
'bus_name': None,
'managed': False,
# Library
'music_dir': '',
'cover_regex': None,
Expand Down Expand Up @@ -242,6 +243,7 @@ def __init__(self, params):
self._dbus = dbus
self._params = params
self._dbus_service = None
self._should_reconnect = not params['managed']

self._can_single = False
self._can_idle = False
Expand Down Expand Up @@ -328,8 +330,10 @@ def my_connect(self):
# Init internal state to throw events at start
self.init_state()

# Add periodic status check for sending MPRIS events
if not self._poll_id:
# If idle is not available, add periodic status check for sending MPRIS events
# Otherwise the timer will connect the socket if disconnected
# If reconnection is not necessary and idle is supported, this timer isn't enabled.
if not self._poll_id and (not self._can_idle or self._should_reconnect):
interval = 15 if self._can_idle else 1
self._poll_id = GLib.timeout_add_seconds(interval,
self.timer_callback)
Expand Down Expand Up @@ -436,13 +440,27 @@ def timer_callback(self):

def socket_callback(self, fd, event):
logger.debug("Socket event %r on fd %r" % (event, fd))
if event & GLib.IO_HUP:
self.reconnect()

def handle_disconnect():
if self._should_reconnect:
self.reconnect()
else:
logger.debug("Not reconnecting, quitting main loop")
loop.quit()
return True

if event & GLib.IO_HUP:
return handle_disconnect()

elif event & GLib.IO_IN:
if self._idling:
self._idling = False
data = fd._fetch_objects("changed")

try:
data = fd._fetch_objects("changed")
except mpd.base.ConnectionError:
return handle_disconnect()

logger.debug("Idle events: %r" % data)
updated = False
for item in data:
Expand Down Expand Up @@ -1394,6 +1412,7 @@ def usage(params):
['help', 'bus-name=', 'config=',
'debug', 'host=', 'music-dir=',
'use-journal', 'path=', 'port=',
'managed',
'version'])
except getopt.GetoptError as ex:
(msg, opt) = ex.args
Expand All @@ -1420,6 +1439,8 @@ def usage(params):
music_dir = arg
elif opt in ['--port']:
params['port'] = int(arg)
elif opt in ['--managed']:
params['managed'] = True
elif opt in ['-v', '--version']:
v = __version__
if __git_version__:
Expand Down Expand Up @@ -1564,9 +1585,10 @@ def usage(params):
logger.debug('Caught SIGINT, exiting.')

# Clean up
try:
mpd_wrapper.client.close()
mpd_wrapper.client.disconnect()
logger.debug('Exiting')
except mpd.ConnectionError:
logger.error('Failed to disconnect properly')
if mpd_wrapper.connected:
try:
mpd_wrapper.client.close()
mpd_wrapper.client.disconnect()
logger.debug('Exiting')
except mpd.ConnectionError:
logger.error('Failed to disconnect properly')
6 changes: 4 additions & 2 deletions src/mpDris2.service.in
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[Unit]
Description=mpDris2 - Music Player Daemon D-Bus bridge
Wants=mpd.service
BindsTo=mpd.service

[Service]
Restart=on-failure
ExecStart=@bindir@/mpDris2 --use-journal
ExecStart=@bindir@/mpDris2 --use-journal --managed
BusName=org.mpris.MediaPlayer2.mpd

[Install]
WantedBy=default.target
# WantedBy=daemon.target
WantedBy=mpd.service