Mercurial > isshouni
diff isshouni.js @ 0:156c2fd7c626
First commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 22 Feb 2012 19:57:56 +0100 |
parents | |
children | 617063bc96f7 |
line wrap: on
line diff
new file mode 100755 --- /dev/null +++ b/isshouni.js @@ -0,0 +1,142 @@ +#!/usr/bin/env node +'use strict'; + +/** + Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + + +var config = require('./config'); +var args = ['-slave'].concat(process.argv.slice(2)); + +var tty = require('tty'); +var xmpp = require('node-xmpp'); +var JID = require('./jid'); +var spawn = require('child_process').spawn; + +var cl = new xmpp.Client(config); +var mplayer = spawn('mplayer', args); + +var connect = function() { + cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick}) + .c('x', {xmlns: 'http://jabber.org/protocol/muc'}) + .c('history', {maxchars: '0'}) + .up()); + //TODO: handle join errors. +}; +cl.on('online', connect); + +var disconnect = function() { + cl.send(new xmpp.Element('presence', {to: config.muc + '/' + config.nick, type: 'unavailable'})); + process.stdin.end(); + cl.end(); +}; + +var send_message = function(name, arg) { + //console.log('SENT: ' + name + ' ' + arg); + var message = new xmpp.Element('message', {to: config.muc, type: 'groupchat'}) + .c(name, {xmlns: 'urn:linkmauve:player'}); + + if (arg) + message.t(arg); + + cl.send(message.up()); +}; + +var send_command = function(text) { + //console.log(text); + mplayer.stdin.write(text + '\n'); +}; + +cl.on('stanza', function(stanza) { + //console.log(stanza.toString()); + if (!stanza.is('message') || stanza.attrs['type'] !== 'groupchat') + return; + + var from = new JID(stanza.attrs['from']); + if (from.bare !== config.muc) + return; + if (from.resource === config.nick) + return; + + var message = stanza.getChild('body'); + if (message) + send_command('osd_show_text "' + from.resource + ': ' + message.getText() + '"'); + + var payload = stanza.getChild(null, 'urn:linkmauve:player'); + if (!payload) + return; + + //send_command('osd_show_text "' + from.resource + ' did ' + payload.name + '"'); + + if (payload.name === 'pause') + send_command('pause'); + else if (payload.name === 'seek') + send_command('seek ' + payload.getText()); + else if (payload.name === 'synchro') + send_command('seek ' + payload.getText() + ' 2'); +}); + +cl.on('error', function(e) { + console.log(e); +}); + +var commands = { + '+': 'volume 1', + '-': 'volume -1', + 'm': 'mute', + ' ': function() { + send_message('pause'); + send_command('get_time_pos'); + return 'pause'; + }, + 'q': function() { + tty.setRawMode(false); + disconnect(); + return 'quit'; + }, +}; + +process.stdin.resume(); +tty.setRawMode(true); +process.stdin.on('keypress', function(char, key) { + if (!char) { + if (key.name == 'right') + send_command('seek 10'); + else if (key.name == 'left') + send_command('seek -10'); + else if (key.name == 'up') + send_command('seek 60'); + else if (key.name == 'down') + send_command('seek -60'); + } else if (char in commands) { + var command = commands[char]; + if (typeof command === 'function') + command = command(); + send_command(command); + } +}); + +mplayer.stdout.on('data', function(data) { + //console.log('DATA: '+data); + data = /(ANS_TIME_POSITION)=([0-9\.]+)/.exec(data); + if (!data) + return; + + var cmd = data[1]; + var arg = data[2]; + if (cmd === 'ANS_TIME_POSITION') + send_message('synchro', arg); +});