/usr/share/decibel-audio-player/src/modules/DBus.py is in decibel-audio-player 1.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 383 384 385 386 387 388 389 390 391 392 393 394 | # -*- coding: utf-8 -*-
#
# Author: Ingelrest François (Francois.Ingelrest@gmail.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import dbus, dbus.service, gobject, media, modules, traceback
from tools import consts, log
MOD_INFO = ('D-Bus Support', 'D-Bus Support', '', [], True, False, consts.MODCAT_NONE)
# MPRIS caps constants
CAPS_CAN_GO_NEXT = 1
CAPS_CAN_GO_PREV = 2
CAPS_CAN_PAUSE = 4
CAPS_CAN_PLAY = 8
CAPS_CAN_SEEK = 16
CAPS_CAN_PROVIDE_METADATA = 32
CAPS_CAN_HAS_TRACKLIST = 64
class DBus(modules.Module):
""" Enable D-Bus support """
def __init__(self):
""" Constructor """
handlers = {
consts.MSG_EVT_PAUSED: self.onPaused,
consts.MSG_EVT_STOPPED: self.onStopped,
consts.MSG_EVT_UNPAUSED: self.onUnpaused,
consts.MSG_EVT_NEW_TRACK: self.onNewTrack,
consts.MSG_EVT_TRACK_MOVED: self.onCurrentTrackMoved,
consts.MSG_EVT_APP_STARTED: self.onAppStarted,
consts.MSG_EVT_NEW_TRACKLIST: self.onNewTracklist,
consts.MSG_EVT_VOLUME_CHANGED: self.onVolumeChanged,
consts.MSG_EVT_TRACK_POSITION: self.onNewTrackPosition,
consts.MSG_EVT_REPEAT_CHANGED: self.onRepeatChanged,
}
modules.Module.__init__(self, handlers)
def getMPRISCaps(self):
""" Return an integer sticking to the MPRIS caps definition """
caps = CAPS_CAN_HAS_TRACKLIST
if len(self.tracklist) != 0:
caps |= CAPS_CAN_PLAY
if self.currTrack is not None:
caps |= CAPS_CAN_PAUSE
caps |= CAPS_CAN_SEEK
caps |= CAPS_CAN_PROVIDE_METADATA
if self.canGoNext: caps |= CAPS_CAN_GO_NEXT
if self.canGoPrev: caps |= CAPS_CAN_GO_PREV
return caps
def getMPRISStatus(self):
""" Return a tuple sticking to the MPRIS status definition """
if self.currTrack is None: playStatus = 2
elif self.paused: playStatus = 1
else: playStatus = 0
if self.repeat: repeatStatus = 1
else: repeatStatus = 0
return (playStatus, 0, 0, repeatStatus)
# --== Message handlers ==--
def onAppStarted(self):
""" Initialize this module """
self.repeat = False
self.paused = False
self.tracklist = []
self.currTrack = None
self.canGoNext = False
self.canGoPrev = False
self.currVolume = 0
self.currPosition = 0
try:
self.sessionBus = dbus.SessionBus()
self.busName = dbus.service.BusName(consts.dbusService, bus=self.sessionBus)
# Create the three MPRIS objects
self.busObjectRoot = DBusObjectRoot(self.busName, self)
self.busObjectPlayer = DBusObjectPlayer(self.busName, self)
self.busObjectTracklist = DBusObjectTracklist(self.busName, self)
except:
self.sessionBus = None
log.logger.error('[%s] Error while initializing\n\n%s' % (MOD_INFO[modules.MODINFO_NAME], traceback.format_exc()))
def onNewTrack(self, track):
""" A new track is being played """
self.paused = False
self.currTrack = track
self.busObjectPlayer.CapsChange(self.getMPRISCaps())
self.busObjectPlayer.TrackChange(track.getMPRISMetadata())
self.busObjectPlayer.StatusChange(self.getMPRISStatus())
def onStopped(self):
""" Playback is stopped """
self.paused = False
self.currTrack = None
self.currPosition = 0
self.busObjectPlayer.CapsChange(self.getMPRISCaps())
self.busObjectPlayer.StatusChange(self.getMPRISStatus())
def onVolumeChanged(self, value):
""" The volume has been changed """
self.currVolume = value
def onNewTrackPosition(self, seconds):
""" New position in the current track """
self.currPosition = seconds
def onPaused(self):
""" The playback has been paused """
self.paused = True
self.busObjectPlayer.StatusChange(self.getMPRISStatus())
def onUnpaused(self):
""" The playback has been unpaused """
self.paused = False
self.busObjectPlayer.StatusChange(self.getMPRISStatus())
def onNewTracklist(self, tracks, playtime):
""" A new tracklist has been set """
self.tracklist = tracks
self.busObjectPlayer.CapsChange(self.getMPRISCaps())
self.busObjectTracklist.TrackListChange(len(tracks))
def onCurrentTrackMoved(self, hasNext, hasPrevious):
""" The position of the current track has moved in the playlist """
self.canGoNext = hasNext
self.canGoPrev = hasPrevious
self.busObjectPlayer.CapsChange(self.getMPRISCaps())
def onRepeatChanged(self, repeat):
""" Repeat has been enabled/disabled """
self.repeat = repeat
self.busObjectPlayer.StatusChange(self.getMPRISStatus())
class DBusObjectRoot(dbus.service.Object):
def __init__(self, busName, module):
""" Constructor """
dbus.service.Object.__init__(self, busName, '/')
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='s')
def Identity(self):
""" Returns a string containing the media player identification """
return '%s %s' % (consts.appName, consts.appVersion)
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Quit(self):
""" Makes the media player exit """
modules.postQuitMsg()
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='(qq)')
def MprisVersion(self):
""" Returns a struct that represents the version of the MPRIS spec being implemented """
return (1, 0)
class DBusObjectTracklist(dbus.service.Object):
def __init__(self, busName, module):
""" Constructor """
self.module = module
dbus.service.Object.__init__(self, busName, '/TrackList')
@dbus.service.method(consts.dbusInterface, in_signature='i', out_signature='a{sv}')
def GetMetadata(self, idx):
""" Gives all meta data available for element at given position in the TrackList, counting from 0 """
if idx >= 0 and idx < len(self.module.tracklist): return self.module.tracklist[idx].getMPRISMetadata()
else: return {}
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='i')
def GetCurrentTrack(self):
""" Return the position of current URI in the TrackList """
if self.module.currTrack is None: return -1
else: return self.module.currTrack.getPlaylistPos()-1
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='i')
def GetLength(self):
""" Number of elements in the TrackList """
return len(self.module.tracklist)
@dbus.service.method(consts.dbusInterface, in_signature='sb', out_signature='i')
def AddTrack(self, uri, playNow):
""" Appends an URI to the TrackList """
import urllib
decodedURI = urllib.unquote(uri)
if decodedURI.startswith('file://'):
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_ADD, {'tracks': [media.getTrackFromFile(decodedURI[7:])], 'playNow': playNow})
return 0
return 1
@dbus.service.method(consts.dbusInterface, in_signature='i', out_signature='')
def DelTrack(self, idx):
""" Removes an URI from the TrackList """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_DEL, {'idx': idx})
@dbus.service.method(consts.dbusInterface, in_signature='b', out_signature='')
def SetLoop(self, loop):
""" Toggle playlist loop """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_REPEAT, {'repeat': loop})
@dbus.service.method(consts.dbusInterface, in_signature='b', out_signature='')
def SetRandom(self, random):
""" Toggle playlist shuffle / random """
if random:
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_SHUFFLE)
@dbus.service.signal(consts.dbusInterface, signature='i')
def TrackListChange(self, length):
""" Signal is emitted when the tracklist content has changed """
pass
# These functions are not part of the MPRIS, but are useful
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Clear(self):
""" Clear the tracklist """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_CLR)
@dbus.service.method(consts.dbusInterface, in_signature='asb', out_signature='')
def AddTracks(self, uris, playNow):
""" Appends multiple URIs to the tracklist """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_ADD, {'tracks': media.getTracks([file for file in uris]), 'playNow': playNow})
@dbus.service.method(consts.dbusInterface, in_signature='asb', out_signature='')
def SetTracks(self, uris, playNow):
""" Replace the tracklist by the given URIs """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TRACKLIST_SET, {'tracks': media.getTracks([file for file in uris]), 'playNow': playNow})
class DBusObjectPlayer(dbus.service.Object):
def __init__(self, busName, module):
""" Constructor """
self.module = module
dbus.service.Object.__init__(self, busName, '/Player')
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Next(self):
""" Goes to the next element """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_NEXT)
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Prev(self):
""" Goes to the previous element """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_PREVIOUS)
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Pause(self):
""" If playing : pause. If paused : unpause """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_TOGGLE_PAUSE)
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Stop(self):
""" Stop playing """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_STOP)
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='')
def Play(self):
""" If playing : rewind to the beginning of current track, else : start playing """
if len(self.module.tracklist) != 0:
if self.module.currTrack is None: gobject.idle_add(modules.postMsg, consts.MSG_CMD_TOGGLE_PAUSE)
else: gobject.idle_add(modules.postMsg, consts.MSG_CMD_SEEK, {'seconds': 0})
@dbus.service.method(consts.dbusInterface, in_signature='b', out_signature='')
def Repeat(self, repeat):
""" Toggle the current track repeat """
# We don't support repeating only the current track
pass
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='(iiii)')
def GetStatus(self):
""" Return the status of media player as a struct of 4 ints """
return self.module.getMPRISStatus()
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='a{sv}')
def GetMetadata(self):
""" Gives all meta data available for the currently played element """
if self.module.currTrack is None: return {}
else: return self.module.currTrack.getMPRISMetadata()
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='i')
def GetCaps(self):
""" Return the media player's current capabilities """
return self.module.getMPRISCaps()
@dbus.service.method(consts.dbusInterface, in_signature='i', out_signature='')
def VolumeSet(self, volume):
""" Sets the volume (argument must be in [0;100]) """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_SET_VOLUME, {'value': volume / 100.0})
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='i')
def VolumeGet(self):
""" Returns the current volume (must be in [0;100]) """
return self.module.currVolume * 100
@dbus.service.method(consts.dbusInterface, in_signature='i', out_signature='')
def PositionSet(self, position):
""" Sets the playing position (argument must be in [0;<track_length>] in milliseconds) """
gobject.idle_add(modules.postMsg, consts.MSG_CMD_SEEK, {'seconds': position / 1000})
@dbus.service.method(consts.dbusInterface, in_signature='', out_signature='i')
def PositionGet(self):
""" Returns the playing position (will be [0;<track_length>] in milliseconds) """
return self.module.currPosition * 1000
@dbus.service.signal(consts.dbusInterface, signature='a{sv}')
def TrackChange(self, metadata):
""" Signal is emitted when the media player plays another track """
pass
@dbus.service.signal(consts.dbusInterface, signature='(iiii)')
def StatusChange(self, status):
""" Signal is emitted when the status of the media player change """
pass
@dbus.service.signal(consts.dbusInterface, signature='i')
def CapsChange(self, caps):
""" Signal is emitted when the media player changes capabilities """
pass
|