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.

TitleMediaSource.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2006-2011 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_linux_title;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.util.StreamUtil;
  28. import java.io.BufferedReader;
  29. import java.io.IOException;
  30. import java.io.InputStreamReader;
  31. /**
  32. * Provides a media source for Linux players using the `xwininfo` command.
  33. */
  34. public class TitleMediaSource implements MediaSource {
  35. /** The command to use to get the title. */
  36. private final String command;
  37. /** The name of the player we're retrieving. */
  38. private final String name;
  39. /**
  40. * Creates a new title media source.
  41. *
  42. * @param command The command to be executed
  43. * @param name The name of the media source
  44. */
  45. public TitleMediaSource(final String command, final String name) {
  46. this.command = command;
  47. this.name = name;
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public MediaSourceState getState() {
  52. if (getInfo().isEmpty()) {
  53. return MediaSourceState.CLOSED;
  54. } else if (getInfo().indexOf('-') == -1) {
  55. return MediaSourceState.STOPPED;
  56. } else {
  57. return MediaSourceState.PLAYING;
  58. }
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public String getAppName() {
  63. return name;
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public String getArtist() {
  68. return getInfo().split("–", 2)[0].trim();
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public String getTitle() {
  73. final String[] info = getInfo().split("–", 2);
  74. if (info.length >= 2) {
  75. return info[1].trim();
  76. } else {
  77. return "";
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public String getAlbum() {
  83. return "";
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public String getLength() {
  88. return "";
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public String getTime() {
  93. return "";
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public String getFormat() {
  98. return "";
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public String getBitrate() {
  103. return "";
  104. }
  105. private String getInfo() {
  106. InputStreamReader reader = null;
  107. BufferedReader input = null;
  108. Process process;
  109. try {
  110. final String[] args = new String[]{
  111. "/bin/bash", "-c", "xwininfo -root -tree | " + command
  112. };
  113. process = Runtime.getRuntime().exec(args);
  114. reader = new InputStreamReader(process.getInputStream());
  115. input = new BufferedReader(reader);
  116. String line = "";
  117. while ((line = input.readLine()) != null) {
  118. return line;
  119. }
  120. } catch (IOException ex) {
  121. Logger.userError(ErrorLevel.LOW, "Unable to retrieve media source info",
  122. ex);
  123. } finally {
  124. StreamUtil.close(reader);
  125. StreamUtil.close(input);
  126. }
  127. return "";
  128. }
  129. }