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

Support MPRIS loop and volume change #749

Merged
merged 8 commits into from
Aug 21, 2022
Merged

Support MPRIS loop and volume change #749

merged 8 commits into from
Aug 21, 2022

Conversation

foonathan
Copy link
Contributor

This PR supports MPRIS loop status on linux, enabling playerctl loop to fetch and playerctl loop None/Track/Playlist to set.

const delta = ((newIdx - curIdx) % 3 + 3) % 3;

manuallySwitchingStatus = true;
songControls.switchRepeat(delta);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach is a bit of a hack (it presses the button the appropriate number of times to go to the desired state), but I can't see a better way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've tried making it work using the youtube api, but its weird and doesn't visually change the button state

So your solution is probably the best bet

const newIdx = switches.indexOf(status);

// Get a delta in the range [0,2]
const delta = ((newIdx - curIdx) % 3 + 3) % 3;
Copy link
Collaborator

@Araxeus Araxeus Jun 14, 2022

Choose a reason for hiding this comment

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

What is the point of the first modulo? (newIdx - curIdx) can only be in the range (-2, 2) then adding 3 is the exact result we need

Can be simplified to just

const delta = (newIdx - curIdx + 3) % 3;
See calc

0 = (0 - 0) % 3 + 3) % 3
0 = (0 - 0 + 3) % 3

2 = (0 - 1) % 3 + 3) % 3
2 = (0 - 1 + 3) % 3

1 = (0 - 2) % 3 + 3) % 3
1 = (0 - 2 + 3) % 3

1 = (1 - 0) % 3 + 3) % 3
1 = (1 - 0 + 3) % 3

0 = (1 - 1) % 3 + 3) % 3
0 = (1 - 1 + 3) % 3

2 = (1 - 2) % 3 + 3) % 3
2 = (1 - 2 + 3) % 3

2 = (2 - 0) % 3 + 3) % 3
2 = (2 - 0 + 3) % 3

1 = (2 - 1) % 3 + 3) % 3
1 = (2 - 1 + 3) % 3

0 = (2 - 2) % 3 + 3) % 3
0 = (2 - 2 + 3) % 3

for (let a = 0; a <= 2; a++) {
    for (let b = 0; b <= 2; b++) {
        console.log(`${((a - b) % 3 + 3) % 3} = (${a} - ${b}) % 3 + 3) % 3`);
        console.log(`${(a - b + 3) % 3} = (${a} - ${b} + 3) % 3`);
    }
}

also, I personally think that short variable names aren't helpful, its better if they are clear and informative
could maybe be named currentIndex and targetIndex instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the point of the first modulo?

I originally had just (newIdx - curIdx) % 3, but that has the wrong sign. I then simply added the general code. Good catch.

@@ -20,7 +20,10 @@ module.exports = (win) => {
go1sBack: () => pressKey(win, "h", ["shift"]),
go1sForward: () => pressKey(win, "l", ["shift"]),
shuffle: () => pressKey(win, "s"),
switchRepeat: () => pressKey(win, "r"),
switchRepeat: (n = 1) => {
for (let i = 0; i != n; ++i)
Copy link
Collaborator

@Araxeus Araxeus Jun 14, 2022

Choose a reason for hiding this comment

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

++i is useless here since the value isn't used
i++ is more appropriate in this scenario (tho ultimately it's the same, the syntax is more clear)

also instead of i != n you should use i < n to avoid edge cases (this is standard)

Copy link
Contributor Author

@foonathan foonathan Jun 14, 2022

Choose a reason for hiding this comment

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

Funny. I come from a C++ background, and if someone wrote for (auto i = 0; i < n; i++) I would have requested it changed to the way I wrote it, as that's the standard there :D

@Araxeus
Copy link
Collaborator

Araxeus commented Jun 14, 2022

LGTM (but can't personally test it since I don't have access to Linux currently)

@foonathan
Copy link
Contributor Author

Anything I could do to help it get merged?

@Araxeus
Copy link
Collaborator

Araxeus commented Jun 14, 2022

@th-ch

@foonathan
Copy link
Contributor Author

I've just pushed another fix for a bug when using playerctl play-pause.

@Araxeus
Copy link
Collaborator

Araxeus commented Jun 22, 2022

In javascript we always use === instead of == 😋

(and !== instead of !=)

could you change those?

Copy link
Owner

@th-ch th-ch left a comment

Choose a reason for hiding this comment

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

Looks ok overall, could be worth applying Araxeus' suggestion on using === - feel free to apply the change, otherwise I can merge and do the fix afterwards

@foonathan
Copy link
Contributor Author

I've changed it. Do you want me to clean up the commits and force push?

@Araxeus
Copy link
Collaborator

Araxeus commented Jun 26, 2022

you should also change it in plugins/shortcuts/mpris.js lines 43-45-47

@foonathan
Copy link
Contributor Author

Volume change support is now implemented as well.

@foonathan foonathan changed the title Support MPRIS loop Support MPRIS loop and volume change Jul 11, 2022
@foonathan
Copy link
Contributor Author

Can this be merged?

Copy link
Owner

@th-ch th-ch left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the contribution! ✅

@th-ch th-ch merged commit f0bb328 into th-ch:master Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants