annotate isshouni.js @ 6:377a9b42226a default tip

Add logging fonctions and don’t listen to messages from the room itself.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 23 Feb 2012 23:51:31 +0100
parents 4a063df30be9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 #!/usr/bin/env node
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 'use strict';
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 /**
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 Permission to use, copy, modify, and/or distribute this software for any
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 purpose with or without fee is hereby granted, provided that the above
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 copyright notice and this permission notice appear in all copies.
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 */
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
4
e6f5077c2ea9 Display an error message when there is no video, and fix exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
20 if (process.argv.length < 3) {
e6f5077c2ea9 Display an error message when there is no video, and fix exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
21 console.log('You should put at least a video on your command line.');
e6f5077c2ea9 Display an error message when there is no video, and fix exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
22 process.exit();
e6f5077c2ea9 Display an error message when there is no video, and fix exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
23 }
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 var config = require('./config');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 var args = ['-slave'].concat(process.argv.slice(2));
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 var tty = require('tty');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 var xmpp = require('node-xmpp');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 var JID = require('./jid');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 var spawn = require('child_process').spawn;
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 var cl = new xmpp.Client(config);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 var mplayer = spawn('mplayer', args);
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
35 var paused = false;
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
36 var connected = false;
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 var connect = function() {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick})
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 .c('x', {xmlns: 'http://jabber.org/protocol/muc'})
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 .c('history', {maxchars: '0'})
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 .up());
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 //TODO: handle join errors.
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 };
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 cl.on('online', connect);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 var disconnect = function() {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'}));
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 process.stdin.end();
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 cl.end();
5
4a063df30be9 Correctly exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
51
4a063df30be9 Correctly exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
52 tty.setRawMode(false);
4a063df30be9 Correctly exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
53 setTimeout(process.exit, 1000);
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 };
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
56 var send_message = function(name, arg, type) {
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 //console.log('SENT: ' + name + ' ' + arg);
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
58 if (!type)
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
59 type = 'groupchat';
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
60
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
61 var message = new xmpp.Element('message', {to: config.muc, type: type})
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 .c(name, {xmlns: 'urn:linkmauve:player'});
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if (arg)
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 message.t(arg);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 cl.send(message.up());
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 };
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 var send_command = function(text) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 //console.log(text);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 mplayer.stdin.write(text + '\n');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 };
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 cl.on('stanza', function(stanza) {
6
377a9b42226a Add logging fonctions and don’t listen to messages from the room itself.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
76 console.log(stanza.toString());
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
77
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
78 var type = stanza.attrs['type'];
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
79 var from = new JID(stanza.attrs['from']);
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
80 var id = stanza.attrs['id'] || '';
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
81
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
82 if (stanza.is('iq') && (type === 'get' || type === 'set'))
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
83 return cl.send(new xmpp.Element('iq', {to: from, id: id, type: 'error'})
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
84 .c('error', {type: 'cancel'})
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
85 .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'})
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
86 .up()
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
87 .up());
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
88
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
89 if (from.bare !== config.muc)
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 return;
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
92 if (stanza.is('presence') && type !== 'unavailable')
3
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
93 return on_presence(from, stanza);
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
94
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
95 if (stanza.is('message'))
3
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
96 return on_message(from, stanza);
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
97 });
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
98
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
99 var on_message = function(from, stanza) {
6
377a9b42226a Add logging fonctions and don’t listen to messages from the room itself.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
100 if (from.resource && from.resource === config.nick)
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 return;
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 var message = stanza.getChild('body');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 if (message)
3
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
105 message = message.getText();
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
106 if (message)
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
107 send_command('osd_show_text "' + from.resource + ': ' + message + '"');
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 var payload = stanza.getChild(null, 'urn:linkmauve:player');
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 if (!payload)
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 return;
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
6
377a9b42226a Add logging fonctions and don’t listen to messages from the room itself.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 5
diff changeset
113 send_command('osd_show_text "' + from.resource + ' did ' + payload.name + '"');
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
115 if (payload.name === 'pause') {
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 send_command('pause');
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
117 paused = !paused;
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
118 } else if (payload.name === 'seek')
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 send_command('seek ' + payload.getText());
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 else if (payload.name === 'synchro')
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 send_command('seek ' + payload.getText() + ' 2');
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
122 };
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
123
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
124 var on_presence = function(from, stanza) {
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
125 if (from.resource === config.nick) {
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
126 connected = true;
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
127 return;
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
128 }
3
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
129 if (!connected)
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
130 return;
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
131
5
4a063df30be9 Correctly exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
132 /*console.log('New participant: ' + from.resource);
3
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
133
0082e861cf16 Fix text message handling and new participant.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
134 if (paused)
5
4a063df30be9 Correctly exit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
135 send_message('paused', undefined, 'chat');*/
2
88eb49491a1e Reply to iq, and better split code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
136 };
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 cl.on('error', function(e) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 console.log(e);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 });
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 var commands = {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 '+': 'volume 1',
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 '-': 'volume -1',
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 'm': 'mute',
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 ' ': function() {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 send_message('pause');
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
148 paused = !paused;
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
149 console.log(paused);
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
150 if (paused)
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
151 send_command('get_time_pos');
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 return 'pause';
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 },
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 'q': function() {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 disconnect();
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 return 'quit';
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 },
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 };
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
160 var seek = function(time) {
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
161 send_message('seek', time);
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
162 send_command('seek ' + time);
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
163 };
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
164
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 process.stdin.resume();
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 tty.setRawMode(true);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 process.stdin.on('keypress', function(char, key) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 if (!char) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 if (key.name == 'right')
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
170 seek(10);
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 else if (key.name == 'left')
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
172 seek(-10);
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 else if (key.name == 'up')
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
174 seek(60);
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 else if (key.name == 'down')
1
617063bc96f7 Fix some sync issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
176 seek(-60);
0
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 } else if (char in commands) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 var command = commands[char];
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 if (typeof command === 'function')
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 command = command();
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 send_command(command);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 }
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 });
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 mplayer.stdout.on('data', function(data) {
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 //console.log('DATA: '+data);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 data = /(ANS_TIME_POSITION)=([0-9\.]+)/.exec(data);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 if (!data)
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 return;
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 var cmd = data[1];
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 var arg = data[2];
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 if (cmd === 'ANS_TIME_POSITION')
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 send_message('synchro', arg);
156c2fd7c626 First commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 });