My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spotify-dbus.el 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;;; spotify-dbus --- Dbus-specific code for Spotify.el
  2. ;; Copyright (C) 2014-2016 Daniel Fernandes Martins
  3. ;;; Commentary:
  4. ;; Somehow shuffeling, setting volume and loop status work not as expected.
  5. ;; Querying the attribute does not return the expected value and setting it
  6. ;; has no effect.
  7. ;; The dbus interface of spotify seems to be broken.
  8. ;;; Code:
  9. (require 'dbus)
  10. (defun spotify-dbus-call (method &rest args)
  11. "Call METHOD with optional ARGS via D-Bus on the Spotify service."
  12. (apply 'dbus-call-method-asynchronously
  13. :session
  14. "org.mpris.MediaPlayer2.spotify"
  15. "/org/mpris/MediaPlayer2"
  16. "org.mpris.MediaPlayer2.Player"
  17. method
  18. nil args))
  19. (defun spotify-dbus-get-property (property)
  20. "Get value of PROPERTY via D-Bus on the Spotify service."
  21. (dbus-get-property :session
  22. "org.mpris.MediaPlayer2.spotify"
  23. "/org/mpris/MediaPlayer2"
  24. "org.mpris.MediaPlayer2.Player"
  25. property))
  26. (defun spotify-dbus-set-property (property value)
  27. "Set PROPERTY to VALUE via D-Bus on the Spotify service."
  28. (dbus-set-property :session
  29. "org.mpris.MediaPlayer2.spotify"
  30. "/org/mpris/MediaPlayer2"
  31. "org.mpris.MediaPlayer2.Player"
  32. property
  33. value))
  34. (defun spotify-dbus-player-status ()
  35. "Updates the mode line to display the current Spotify player status."
  36. (let ((metadata (spotify-dbus-get-property "Metadata")))
  37. (if (and (spotify-connected-p) metadata)
  38. (let ((track-id (car (car (cdr (assoc "mpris:trackid" metadata)))))
  39. (artist (car (car (car (cdr (assoc "xesam:artist" metadata))))))
  40. (title (car (car (cdr (assoc "xesam:title" metadata)))))
  41. (track-n (car (car (cdr (assoc "xesam:trackNumber" metadata)))))
  42. (disc-n (car (car (cdr (assoc "xesam:discNumber" metadata)))))
  43. (length (/ (car (car (cdr (assoc "mpris:length" metadata)))) 1000)))
  44. (if (> track-n 0)
  45. (spotify-replace-mode-line-flags
  46. (concat track-id "\n"
  47. artist "\n"
  48. title "\n"
  49. (number-to-string track-n) "\n"
  50. (number-to-string disc-n) "\n"
  51. (number-to-string length) "\n"
  52. "-\n" ;; TODO: unable to get the player state via D-Bus
  53. "-\n" ;; TODO: unable to get the player position via D-Bus
  54. ))
  55. (spotify-update-mode-line "")))
  56. (spotify-update-mode-line ""))))
  57. (defun spotify-dbus-player-toggle-play ()
  58. "Toggle Play/Pause."
  59. (spotify-dbus-call "PlayPause"))
  60. (defun spotify-dbus-player-next-track ()
  61. "Play next track."
  62. (spotify-dbus-call "Next"))
  63. (defun spotify-dbus-player-previous-track ()
  64. "Play previous previous."
  65. (spotify-dbus-call "Previous"))
  66. ;; TODO: Currently not supported by the Spotify client D-Bus interface
  67. (defun spotify-dbus-toggle-repeat ()
  68. (message "Toggling repeat status not supported by the Spotify client"))
  69. ;; TODO: Currently not supported by the Spotify client D-Bus interface
  70. (defun spotify-dbus-toggle-shuffle ()
  71. (message "Toggline shuffle status not supported by the Spotify client"))
  72. ;; TODO: Synchronize this to work the same way as the apple version, if possible
  73. (defun spotify-dbus-player-play-track (track-id context-id)
  74. (when track-id (spotify-dbus-call "Pause"))
  75. (run-at-time "1 sec" nil 'spotify-dbus-call "OpenUri" (or track-id context-id)))
  76. (defun spotify-dbus-player-play ()
  77. (spotify-dbus-call "Play"))
  78. (defun spotify-dbus-player-pause ()
  79. (spotify-dbus-call "Pause"))
  80. (provide 'spotify-dbus)