wakepy icon indicating copy to clipboard operation
wakepy copied to clipboard

Using D-Bus for requesting a wakelock

Open NicoWeio opened this issue 2 years ago • 3 comments

Pros

  • It works great on my Manjaro PC.
  • It's nice and clean (IMHO).
  • It doesn't require superuser privileges.

Cons

  • It requires a cookie for removing the wakelock, which is not quite supported by the current architecture of wakepy.
    • This is why I didn't open a Pull Request in the first place. If you would like to, I could do so in the future.

Neutral

  • I'm not sure how good the support for this is compared to the current systemd method. Maybe even better? I haven't heard of distros without D-Bus, contrary to distros without systemd.
  • I have little knowledge of D-Bus etc., so this proposal comes in the sense of “it works for me”.

I've written a small proof of concept:

from contextlib import contextmanager
import dbus
import time


@contextmanager
def inhibit(application, reason):
    bus = dbus.SessionBus()
    obj = bus.get_object('org.freedesktop.PowerManagement.Inhibit',
                         '/org/freedesktop/PowerManagement/Inhibit')
    inhibitMethod = obj.get_dbus_method('Inhibit', 'org.freedesktop.PowerManagement.Inhibit')
    unInhibitMethod = obj.get_dbus_method('UnInhibit', 'org.freedesktop.PowerManagement.Inhibit')

    cookie = inhibitMethod(application, reason)
    try:
        yield cookie
    finally:
        unInhibitMethod(cookie)


print('Starting…')
with inhibit('TestApp', 'TestReason'):
    time.sleep(10)
print('Done!')

NicoWeio avatar Mar 28 '22 14:03 NicoWeio

Interesting! Thanks for reporting this @NicoWeio!

I usually myself use only Windows, so I should make some special setup to test this (dual-boot perhaps easiest?). Interested to hear what others think about this. Why do you think this would be not possible (or easy) to implement in current wakepy?

I'm also interested to hear if this need sudo privileges? The systemd approach needs sudo, and if this does not I find this very appealing.

fohrloop avatar Mar 30 '22 20:03 fohrloop

I also currently have close to zero understanding about D-Bus, so that's why possible other users' opinions are highly valued :)

fohrloop avatar Mar 30 '22 20:03 fohrloop

Dang, I forgot the best part: This works without superuser privileges! 🎉

In the current wakepy, I didn't see a mechanism for storing a cookie that the D-Bus interface requires for releasing the wakelock. But then again, I only had a glimpse at the code, and refactoring it probably isn't exactly hard.

As I mentioned earlier, neither do I have good knowledge about D-Bus, but if this works well for everyone, I can live with that. Dear fellow readers, please try it out! 🙃

PS: Since my semester break is about to end, it might take a while until I can work on this.

NicoWeio avatar Mar 30 '22 21:03 NicoWeio

This has finally got some speed, thanks to @Stehlampe2020 for the PR 22! I have merged a working version in master. It works without sudo priviledges using dbus. Before publishing a new version, I consider other alternative dbus packages than just the used dbus-python, from the perspective of what would be the easiest to install for the end users.

fohrloop avatar Feb 24 '23 20:02 fohrloop

I think the dbus-python package should be pre-installed on all distros featuring a Freedesktop-related desktop environment (tested to be so on Linux Mint with Cinnamon and Ubuntu with Unity as well as with GNOME).
If the package isn't available #22 will just fall back to the sudo-requiring solution you're familiar with.

Lampe2020 avatar Feb 24 '23 20:02 Lampe2020

I'm happy to announce that version 0.6.0 uses DBus by default! Thanks @NicoWeio for ideating and @Stehlampe2020 for supplying PR with implementation !

fohrloop avatar Feb 27 '23 22:02 fohrloop