Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New mpris #225

Merged
merged 5 commits into from Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -13,7 +13,7 @@
"plugin:promise/recommended"
],
"parserOptions": {
"ecmaVersion": 2016,
"ecmaVersion": 2018,
"sourceType": "module"
},
"globals": {
Expand All @@ -36,6 +36,7 @@
"no-console": "off",
"func-names": "off",
"class-methods-use-this": "off",
"no-bitwise": ["error", {"allow": ["~"]} ],
"no-unused-expressions": ["error", {"allowTernary": true}],
"prefer-destructuring": ["error", {"array": false}]
}
Expand Down
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Expand Up @@ -41,8 +41,6 @@ We welcome many ways of contributing to Headset:

which will install both global and OS-dependent packages.

Linux users also need to install appropriate dependencies for [dbus](https://www.npmjs.com/package/dbus#general)


4. Now you can run the app:

Expand Down Expand Up @@ -103,4 +101,4 @@ To submit a pull request:
Make sure to add relevant information, e.g.:
- which issue you are working on - you can link it using `#<issue-number>` in your message
- which parts of the feature you'd like some feedback on
- where you got stuck if you need help
- where you got stuck if you need help
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -71,7 +71,6 @@ $ git clone https://github.com/headsetapp/headset-electron.git
$ cd headset-electron/linux
$ npm install
```
On Linux, the `dbus` module also has it's own [dependencies](https://github.com/Shouqun/node-dbus#dependencies) that need to be installed.

4. Create your build:
```bash
Expand All @@ -85,6 +84,5 @@ electron-packager . \
--platform=linux \
--arch=x64
```
For Linux, a 32bit system is required in order to package Headset for `arch=ia32` due to `dbus` module own dependencies.

5. [Optional] For the Ubuntu build, we're using `electron-installer-debian` and for the Fedora build, we're using `electron-installer-redhat`. There might be an installer for your specific version, just have to google it.
72 changes: 57 additions & 15 deletions linux/lib/mprisService.js
Expand Up @@ -26,9 +26,11 @@ module.exports = (win, player, app) => {
});

mprisPlayer.playbackStatus = 'Stopped';
mprisPlayer.rate = 1 + 1e-15; // to avoid storing 1 as byte (nodejs dbus bug)
mprisPlayer.minimumRate = 1 + 1e-15;
mprisPlayer.maximumRate = 1 + 1e-15;
mprisPlayer.rate = 1;
mprisPlayer.canSeek = true;
mprisPlayer.canControl = true;
mprisPlayer.minimumRate = 1;
mprisPlayer.maximumRate = 1;
mprisPlayer.volume = 0.75;

mprisPlayer.on('raise', () => {
Expand All @@ -42,8 +44,8 @@ module.exports = (win, player, app) => {
});

mprisPlayer.on('rate', () => {
logger('Changing rate');
mprisPlayer.rate = 1 + 1e-15;
logger('Attempting to change rate');
mprisPlayer.rate = 1;
});

mprisPlayer.on('playpause', () => {
Expand Down Expand Up @@ -86,8 +88,8 @@ module.exports = (win, player, app) => {

mprisPlayer.on('volume', (volume) => {
if (mprisPlayer.playbackStatus !== 'Stopped') {
if (volume >= 1) { volume = 1 - 1e-15; }
if (volume <= 0) { volume = 1e-15; }
if (volume > 1) { volume = 1; }
if (volume < 0) { volume = 0; }

logger('Volume received, set to: %d', volume);
changeVolumeState(win, volume * 100);
Expand All @@ -96,36 +98,76 @@ module.exports = (win, player, app) => {
}
});

mprisPlayer.on('seek', (seek) => {
if (mprisPlayer.playbackStatus !== 'Stopped') {
if (seek < 0) seek = ~seek;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acrisci this is the only issue we have with mpris-service right now. I've already reported it here: dbusjs/mpris-service#27
We're waiting for the fix to come so I can remove this workaround and probably ship the new version

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just waiting on people to approve some other implementations before the release comes with this fix. Should be ready sometime in the next week.

logger(`Seek ${seek / 1e6} sec`);
win.webContents.send('player2Win', ['seekTo', (mprisPlayer.getPosition() + seek) / 1e6]); // in seconds
}
});

mprisPlayer.on('position', (arg) => {
if (mprisPlayer.playbackStatus !== 'Stopped') {
logger(`Go to position ${arg.position / 1e6} sec`);
win.webContents.send('player2Win', ['seekTo', arg.position / 1e6]); // in seconds
}
});

mprisPlayer.on('shuffle', (shuffle) => {
if (mprisPlayer.playbackStatus !== 'Stopped') {
logger(`Set shuffling: ${shuffle}`);
win.webContents.send('player2Win', ['setShuffle', shuffle]);
mprisPlayer.shuffle = shuffle;
}
});

mprisPlayer.on('loopStatus', (loop) => {
if (mprisPlayer.playbackStatus !== 'Stopped') {
logger(`Set looping to: ${loop}`);
mprisPlayer.loopStatus = loop;
let repeat = null;
if (loop === 'Track') repeat = 'one';
if (loop === 'Playlist') repeat = 'all';
if (loop === 'None') repeat = null;
win.webContents.send('player2Win', ['onRepeat', repeat]);
}
});

ipcMain.on('win2Player', (e, args) => {
switch (args[0]) {
case 'setVolume':
mprisPlayer.volume = (args[1] / 100) + 1e-15;
mprisPlayer.volume = (args[1] / 100);
break;
case 'trackInfo':
mprisPlayer.canSeek = false;
mprisPlayer.metadata = {
'xesam:artist': [args[1].artist],
'xesam:title': args[1].title,
'xesam:url': `https://www.youtube.com/watch?v=${args[1].id}`,
'mpris:trackid': mprisPlayer.objectPath('track/0'),
'mpris:artUrl': args[1].thumbnail,
'mpris:length': args[1].duration * 1e6, // in microseconds
};
logger(['Track Info:', mprisPlayer.metadata]);
break;
case 'seekTo': {
const delta = Math.round(args[1] * 1e6) - mprisPlayer.position;
mprisPlayer.seeked(delta);
case 'seekTo':
mprisPlayer.seeked(Math.round(args[1] * 1e6)); // in microseconds
break;
case 'shuffle':
mprisPlayer.shuffle = args[1];
break;
case 'repeat':
if (args[1] === 'one') mprisPlayer.loopStatus = 'Track';
if (args[1] === 'all') mprisPlayer.loopStatus = 'Playlist';
if (args[1] === null) mprisPlayer.loopStatus = 'None';
break;
}
default:
}
});

ipcMain.on('player2Win', (e, args) => {
switch (args[0]) {
case 'currentTime':
// int64 in microseconds. nodejs dbus doesn't support int64 (bug)
mprisPlayer.position = Math.round(args[1] * 1e6);
mprisPlayer.getPosition = () => Math.round(args[1] * 1e6); // in microseconds
break;
case 'onStateChange':
if (args[1] === 1) mprisPlayer.playbackStatus = 'Playing';
Expand Down
23 changes: 12 additions & 11 deletions linux/lib/registerMediaKeys.js
@@ -1,4 +1,4 @@
const DBus = require('dbus-native');
const DBus = require('dbus-next');
const debug = require('debug');
const { ipcMain } = require('electron');

Expand All @@ -19,7 +19,7 @@ function executeMediaKey(win, key) {
`);
}

function registerBindings(win, desktopEnv, bus) {
async function registerBindings(win, desktopEnv, bus) {
let serviceName = `org.${desktopEnv}.SettingsDaemon`;
let objectPath = `/org/${desktopEnv}/SettingsDaemon/MediaKeys`;
let interfaceName = `org.${desktopEnv}.SettingsDaemon.MediaKeys`;
Expand All @@ -30,11 +30,11 @@ function registerBindings(win, desktopEnv, bus) {
interfaceName = 'org.gnome.SettingsDaemon.MediaKeys';
}

bus.getService(serviceName).getInterface(objectPath, interfaceName, (err, iface) => {
// Error when gnome|gnome3|mate is not found
if (err) return;
try {
const settings = await bus.getProxyObject(serviceName, objectPath);
const mediaKeys = settings.getInterface(interfaceName);

iface.on('MediaPlayerKeyPressed', (n, keyName) => {
mediaKeys.on('MediaPlayerKeyPressed', (iface, keyName) => {
logger('Media key pressed: %o', keyName);
switch (keyName) {
case 'Next':
Expand All @@ -44,16 +44,17 @@ function registerBindings(win, desktopEnv, bus) {
executeMediaKey(win, 'play-previous');
break;
case 'Play':
if (track !== null) {
executeMediaKey(win, 'play-pause');
}
if (track !== null) executeMediaKey(win, 'play-pause');
break;
default:
}
});
iface.GrabMediaPlayerKeys('headset', 0);

mediaKeys.GrabMediaPlayerKeys('headset', 0);
logger('Grabbed media keys for %o', desktopEnv);
});
} catch (err) {
// Error if trying to grab keys in another desktop environment
}
}

module.exports = (win) => {
Expand Down