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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.mediasource_linux_title;
  18. import com.dmdirc.addons.nowplaying.MediaSource;
  19. import com.dmdirc.addons.nowplaying.MediaSourceState;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import static com.dmdirc.util.LogUtils.USER_ERROR;
  26. /**
  27. * Provides a media source for Linux players using the `xwininfo` command.
  28. */
  29. public class TitleMediaSource implements MediaSource {
  30. private static final Logger LOG = LoggerFactory.getLogger(TitleMediaSource.class);
  31. /** The command to use to get the title. */
  32. private final String command;
  33. /** The name of the player we're retrieving. */
  34. private final String name;
  35. /**
  36. * Creates a new title media source.
  37. *
  38. * @param command The command to be executed
  39. * @param name The name of the media source
  40. */
  41. public TitleMediaSource(final String command, final String name) {
  42. this.command = command;
  43. this.name = name;
  44. }
  45. @Override
  46. public MediaSourceState getState() {
  47. if (getInfo().isEmpty()) {
  48. return MediaSourceState.CLOSED;
  49. } else if (getInfo().indexOf('-') == -1) {
  50. return MediaSourceState.STOPPED;
  51. } else {
  52. return MediaSourceState.PLAYING;
  53. }
  54. }
  55. @Override
  56. public String getAppName() {
  57. return name;
  58. }
  59. @Override
  60. public String getArtist() {
  61. return getInfo().split("–", 2)[0].trim();
  62. }
  63. @Override
  64. public String getTitle() {
  65. final String[] info = getInfo().split("–", 2);
  66. if (info.length >= 2) {
  67. return info[1].trim();
  68. } else {
  69. return "";
  70. }
  71. }
  72. @Override
  73. public String getAlbum() {
  74. return "";
  75. }
  76. @Override
  77. public String getLength() {
  78. return "";
  79. }
  80. @Override
  81. public String getTime() {
  82. return "";
  83. }
  84. @Override
  85. public String getFormat() {
  86. return "";
  87. }
  88. @Override
  89. public String getBitrate() {
  90. return "";
  91. }
  92. private String getInfo() {
  93. final String[] args = {"/bin/bash", "-c", "xwininfo -root -tree | " + command};
  94. try {
  95. final Process process = Runtime.getRuntime().exec(args);
  96. try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
  97. BufferedReader input = new BufferedReader(reader)) {
  98. final String line = input.readLine();
  99. if (line != null) {
  100. return line;
  101. }
  102. } catch (IOException ex) {
  103. LOG.info(USER_ERROR, "Unable to retrieve media source info", ex);
  104. }
  105. } catch (IOException ex) {
  106. LOG.info(USER_ERROR, "Unable to retrieve media source info", ex);
  107. }
  108. return "";
  109. }
  110. }