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