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