Created
December 26, 2017 03:09
-
-
Save AntoineTurmel/287b133bed371c5cb35c7784bd2927f0 to your computer and use it in GitHub Desktop.
rough script to display artist/title and use media keys on musikCube/Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WebSocket = require('ws'); | |
var EventEmitter = require('events').EventEmitter; | |
const ws = new WebSocket('ws://127.0.0.1:7905'); | |
var g15composer = require('g15composer').init('/home/antoine/dev/musikG15/pipe', 'notifications'); | |
ws.on('open', function open() { | |
ws.send('{ "name": "authenticate", "type": "request", "id": "foobar", "options": { "password": "" }}'); | |
}); | |
ws.on('message', function incoming(data) { | |
//console.log(data); | |
var json = JSON.parse(data); | |
//console.log(json.name); | |
if(json.options){ | |
if(json.options.playing_track){ | |
console.log(json.options.playing_track.title); | |
if (json.options.state == "paused") { | |
g15composer.push("musikCube - paused", json.options.playing_track.artist + " - " + json.options.playing_track.title ); | |
} else { | |
g15composer.push("musikCube", json.options.playing_track.artist + " - " + json.options.playing_track.title ); | |
} | |
} | |
} | |
}); | |
function listenDbus () { | |
var e = new EventEmitter(); | |
var DBus = require('dbus'); | |
var dbus = new DBus(); | |
var bus = dbus.getBus('session'); | |
bus.getInterface('org.gnome.SettingsDaemon', '/org/gnome/SettingsDaemon/MediaKeys', 'org.gnome.SettingsDaemon.MediaKeys', function(err, iface) { | |
e.emit('connected'); | |
iface.on('MediaPlayerKeyPressed', function (n, value) { | |
switch (value) { | |
case 'Play': e.emit('play'); return; | |
case 'Next': e.emit('next'); return; | |
case 'Previous': e.emit('back'); return; | |
case 'Stop': e.emit('stop'); return; | |
} | |
}); | |
iface.GrabMediaPlayerKeys(0, 'org.gnome.SettingsDaemon.MediaKeys'); | |
}); | |
return e; | |
} | |
function listen () { | |
if (process.platform == 'linux') { | |
return listenDbus(); | |
} else { | |
throw new Error('unsupported platform', process.platform) | |
} | |
} | |
var e = listen(); | |
e.on('connected', function () { | |
console.log('connected'); | |
}) | |
e.on('play', function () { | |
console.log('play'); | |
ws.send('{ "name": "pause_or_resume", "type": "request", "id": "foobar", "options": {}}'); | |
}) | |
e.on('next', function () { | |
console.log('next'); | |
ws.send('{ "name": "next", "type": "request", "id": "foobar", "options": {}}'); | |
}) | |
e.on('back', function () { | |
console.log('back'); | |
ws.send('{ "name": "previous", "type": "request", "id": "foobar", "options": {}}'); | |
}) | |
e.on('stop', function () { | |
console.log('back'); | |
ws.send('{ "name": "stop", "type": "request", "id": "foobar", "options": {}}'); | |
}) |
Author
AntoineTurmel
commented
Dec 26, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment