UNPKG

4.99 kBJavaScriptView Raw
1/*
2
3BSD LICENSED
4
5Copyright (c) 2011, Janne Julkunen
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without modification,
9are permitted provided that the following conditions are met:
10
11* Redistributions of source code must retain the above copyright notice, this
12list of conditions and the following disclaimer.
13
14* Redistributions in binary form must reproduce the above copyright notice,
15this list of conditions and the following disclaimer in the documentation
16and/or other materials provided with the distribution.
17
18* Neither the name of the Enlightened Linux Solutions nor the names of its
19contributors may be used to endorse or promote products derived from this
20software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31OF THE POSSIBILITY OF SUCH DAMAGE.
32
33*/
34
35var exec = require('child_process').exec;
36
37var savedVolume = 0; // Go around a bug in rhythmbox-client
38
39var currentState = "paused"; // Go around having no paused info
40
41//var favorites = '[{"name": "Goth Metal World", "type": "Radio", "url": "http://85.200.99.56:7999/listen.pls"},' +
42// '{"name": "Amorphis - Skyforger", "type": "Song", "url": "/home/sconix/Music/Amorphis/Skyforger/01 - Sampo.mp3"}]';
43
44exports.setup = function(cb) {
45 var child = exec("rhythmbox-client --help", function(error, stdout, stderr) {
46 if(error)
47 cb(null);
48 else
49 cb("rhythmbox", "Rhythmbox", "Music Player");
50 });
51};
52
53exports.execute = function(req, res) {
54 console.log("Executing rhythmbox command: " + req.params[0]);
55
56 if(req.params[0] != "close")
57 var execute_string = "pgrep rhythmbox";
58 else
59 var execute_string = "rhythmbox-client --quit";
60
61 var child = exec(execute_string, function(error, stdout, stderr) {
62 if((stdout.length > 0) || (req.params[0] == "start")) {
63 var execute_string = "";
64
65 if(req.params[0] == "output/mute") {
66 if(req.param("state") == "true") {
67 var execute_string = "rhythmbox-client --mute;";
68 } else {
69 var execute_string = "rhythmbox-client --unmute; rhythmbox-client --set-volume " +
70 (savedVolume / 100) + ";";
71 }
72 /* } else if(req.params[0] == "url") {
73 var execute_string = "rhythmbox-client --play-uri=" + req.param("url") + ";";
74 } else if(req.params[0] == "queue") {
75 var execute_string = "rhythmbox-client --enqueue \"" + req.param("url") + "\";";*/
76 } else if(req.params[0] == "output/volume") {
77 var execute_string = "rhythmbox-client --unmute; rhythmbox-client --set-volume " +
78 (req.param("level") / 100) + ";";
79 } else if(req.params[0] == "playback/state") {
80 if(req.param("action") == "play")
81 currentState = "playing";
82 else
83 currentState = "paused";
84
85 var execute_string = "rhythmbox-client --" + req.param("action") + ";";
86 } else if(req.params[0] == "playback/song") {
87 if(req.param("action") == "prev")
88 var execute_string = "rhythmbox-client --previous;";
89 else if(req.param("action") == "next")
90 var execute_string = "rhythmbox-client --next;";
91 }
92
93 execute_string += "rhythmbox-client --print-volume;";
94
95 execute_string += "rhythmbox-client --print-playing-format='%ta;%tt;%td;%te'";
96
97 var child = exec(execute_string, function(error, stdout, stderr) {
98 res.header('Content-Type', 'text/javascript');
99
100 if(error !== null) {
101 res.send({"state": "unknown", "volume": 0, "mute": true});
102 } else {
103 var output = stdout.split("\n");
104
105 if(output[0].slice(0, 17) == "Playback is muted") {
106 var mute = true;
107
108 var volume = Math.round(output[1].slice(19, 27) * 100);
109
110 var status = output[2].split(";");
111 } else {
112 var mute = false;
113
114 var volume = Math.round(output[0].slice(19, 27) * 100);
115
116 var status = output[1].split(";");
117
118 savedVolume = volume;
119 }
120
121 if(status[0].slice(0, 11) == "Not playing") {
122 res.send({"state": "stopped", "volume": volume, "mute": mute});
123else {
124 res.send({"state": currentState, "artist": escape(status[0]),
125 "title": escape(status[1]), "duration": escape(status[2]),
126 "elapsed": escape(status[3]), "volume": volume, "mute": mute});
127 }
128 }
129 });
130 } else {
131 res.send({"state": "closed", "volume": 0, "mute": true});
132 }
133 });
134};
135