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.

MPRISSource.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.mediasource_dbus;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * A media source for anything that's compatible with MRPIS.
  29. *
  30. * @author chris
  31. */
  32. public class MPRISSource implements MediaSource {
  33. /** The media source manager. */
  34. private final DBusMediaSource source;
  35. /** The service name. */
  36. private final String service;
  37. /** The name of the source. */
  38. private final String name;
  39. /** A temporary cache of track data. */
  40. private Map<String, String> data;
  41. /**
  42. * Creates a new MPRIS source for the specified service name.
  43. *
  44. * @param source The manager which owns this source
  45. * @param service The service name of the MRPIS service
  46. */
  47. public MPRISSource(final DBusMediaSource source, final String service) {
  48. this.source = source;
  49. this.service = service;
  50. final List<String> info = source.doDBusCall("org.mpris." + service, "/",
  51. "org.freedesktop.MediaPlayer.Identity");
  52. if (info.isEmpty()) {
  53. throw new IllegalArgumentException("No service with that name found");
  54. }
  55. this.name = info.get(0).replace(' ', '_');
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public MediaSourceState getState() {
  60. final char[] status = getStatus();
  61. if (status == null) {
  62. data = null;
  63. return MediaSourceState.CLOSED;
  64. }
  65. data = getTrackInfo();
  66. if (status[0] == '0') {
  67. return MediaSourceState.PLAYING;
  68. } else if (status[0] == '1') {
  69. return MediaSourceState.PAUSED;
  70. } else if (status[0] == '2') {
  71. return MediaSourceState.STOPPED;
  72. } else {
  73. return MediaSourceState.NOTKNOWN;
  74. }
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public String getAppName() {
  79. return name;
  80. }
  81. /**
  82. * Utility method to return the value of the specified key if it exists,
  83. * or "Unknown" if it doesn't.
  84. *
  85. * @param key The key to be retrieved
  86. * @return The value of the specified key or "Unknown".
  87. */
  88. protected String getData(final String key) {
  89. return data == null || !data.containsKey(key) ? "Unknown" : data.get(key);
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public String getArtist() {
  94. return getData("artist");
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. public String getTitle() {
  99. return getData("title");
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public String getAlbum() {
  104. return getData("album");
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public String getLength() {
  109. return getData("time");
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public String getTime() {
  114. return "Unknown";
  115. }
  116. /** {@inheritDoc} */
  117. @Override
  118. public String getFormat() {
  119. return "Unknown";
  120. }
  121. /** {@inheritDoc} */
  122. @Override
  123. public String getBitrate() {
  124. return getData("audio-bitrate");
  125. }
  126. /**
  127. * Retrieves a map of track information from the service.
  128. *
  129. * @return A map of metadata returned by the MPRIS service
  130. */
  131. protected Map<String, String> getTrackInfo() {
  132. // If only there were a standard...
  133. final List<String> list = source.doDBusCall("org.mpris." + service,
  134. "/Player", "org.freedesktop.MediaPlayer.GetMetadata");
  135. list.addAll(source.doDBusCall("org.mpris." + service,
  136. "/Player", "org.freedesktop.MediaPlayer.GetMetaData"));
  137. return DBusMediaSource.parseDictionary(list);
  138. }
  139. /**
  140. * Retrieves the 'status' result from the MPRIS service.
  141. *
  142. * @return The returned status or null if the service isn't running
  143. */
  144. protected char[] getStatus() {
  145. if (source.doDBusCall("org.mpris." + service, "/",
  146. "org.freedesktop.MediaPlayer.Identity").isEmpty()) {
  147. // Calling dbus-send can seemingly start applications that have
  148. // quit (not entirely sure how), so check that it's running first.
  149. return null;
  150. }
  151. final List<String> res = DBusMediaSource.getInfo(new String[]{
  152. "/usr/bin/dbus-send", "--print-reply", "--dest=org.mpris." + service,
  153. "/Player", "org.freedesktop.MediaPlayer.GetStatus"
  154. });
  155. if (res.isEmpty()) {
  156. return null;
  157. }
  158. final char[] result = new char[4];
  159. int i = 0;
  160. for (String line : res) {
  161. final String tline = line.trim();
  162. if (tline.startsWith("int32")) {
  163. result[i++] = tline.charAt(tline.length() - 1);
  164. }
  165. }
  166. return result;
  167. }
  168. }