1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var exec = require('child_process').exec;
|
36 |
|
37 | var savedVolume = 0;
|
38 |
|
39 | var currentState = "paused";
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | exports.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 |
|
53 | exports.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 | |
73 |
|
74 |
|
75 |
|
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});
|
123 | } else {
|
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 |
|