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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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 java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import static com.dmdirc.util.LogUtils.USER_ERROR;
  31. /**
  32. * Provides a media source for Linux players using the `xwininfo` command.
  33. */
  34. public class TitleMediaSource implements MediaSource {
  35. private static final Logger LOG = LoggerFactory.getLogger(TitleMediaSource.class);
  36. /** The command to use to get the title. */
  37. private final String command;
  38. /** The name of the player we're retrieving. */
  39. private final String name;
  40. /**
  41. * Creates a new title media source.
  42. *
  43. * @param command The command to be executed
  44. * @param name The name of the media source
  45. */
  46. public TitleMediaSource(final String command, final String name) {
  47. this.command = command;
  48. this.name = name;
  49. }
  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. @Override
  61. public String getAppName() {
  62. return name;
  63. }
  64. @Override
  65. public String getArtist() {
  66. return getInfo().split("–", 2)[0].trim();
  67. }
  68. @Override
  69. public String getTitle() {
  70. final String[] info = getInfo().split("–", 2);
  71. if (info.length >= 2) {
  72. return info[1].trim();
  73. } else {
  74. return "";
  75. }
  76. }
  77. @Override
  78. public String getAlbum() {
  79. return "";
  80. }
  81. @Override
  82. public String getLength() {
  83. return "";
  84. }
  85. @Override
  86. public String getTime() {
  87. return "";
  88. }
  89. @Override
  90. public String getFormat() {
  91. return "";
  92. }
  93. @Override
  94. public String getBitrate() {
  95. return "";
  96. }
  97. private String getInfo() {
  98. final String[] args = {"/bin/bash", "-c", "xwininfo -root -tree | " + command};
  99. try {
  100. final Process process = Runtime.getRuntime().exec(args);
  101. try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
  102. BufferedReader input = new BufferedReader(reader)) {
  103. final String line = input.readLine();
  104. if (line != null) {
  105. return line;
  106. }
  107. } catch (IOException ex) {
  108. LOG.info(USER_ERROR, "Unable to retrieve media source info", ex);
  109. }
  110. } catch (IOException ex) {
  111. LOG.info(USER_ERROR, "Unable to retrieve media source info", ex);
  112. }
  113. return "";
  114. }
  115. }