Closed Bug 758848 Opened 12 years ago Closed 3 years ago

Add support for wake_notification and sleep_notification on Linux


(Core :: Widget: Gtk, enhancement, P3)




95 Branch
Tracking Status
firefox95 --- fixed


(Reporter: Fallen, Assigned: stransky)


(Blocks 1 open bug)



(2 files)

I recently found that wake_notification is only supported on Mac and Windows. I'd like to add support for this on Linux too, mainly to fix a calendar bug.

To do so it looks like I will need to call register_pm_notifier() which is from <linux/suspend.h>. It also looks like this header is not usually included in /usr/include, but it can be found in all 2.6 and 3.0 kernel headers. This is not supported in linux 2.4, where I could work around with CPU hotplug events. Is it required to support linux 2.4 also?

I have added code to widget/gtk2/nsToolkit.cpp that adds this notification. Is this the correct place to do so, or is there a more generic location for all linux variants?

I would appreciate if someone could tell me what the procedure is for including kernel headers like <linux/suspend.h>. Is it possible? Should I just forward define the functions and structs myself?
Since D-Bus is already a requirement, one could monitor such events with it. On a modern Linux system they appear in the /org/freedesktop/UPower path. On a suspend/wakeup cycle I see

signal sender=:1.9 -> dest=(null destination) serial=27929 path=/org/freedesktop/UPower;
   interface=org.freedesktop.UPower; member=Sleeping
signal sender=:1.9 -> dest=(null destination) serial=27930 path=/org/freedesktop/UPower;
   interface=org.freedesktop.UPower; member=NotifySleep
   string "suspend"

and then

signal sender=:1.9 -> dest=(null destination) serial=27931 path=/org/freedesktop/UPower;
   interface=org.freedesktop.UPower; member=Resuming
signal sender=:1.9 -> dest=(null destination) serial=27932 path=/org/freedesktop/UPower;
   interface=org.freedesktop.UPower; member=NotifyResume
   string "suspend"

On an older system there will be an equivalent with HAL.

Maybe this would be an alternative over using more direct kernel access.
Yes, register_pm_notifier looks like a kernel function.  I don't know if that is accessible from user space.

UPower vis D-Bus sounds like the modern way for apps to get these notifications.
No need to support older systems and further than gracefully failing to send the notifications.

This sounds like something that would sensibly go into Mozilla's "hal" via extension of hal/linux/UPowerClient.cpp.
A difficulty there is that code in that subdirectory should not use XPCOM, so there would have to be some code elsewhere to send the notification, but that could use (Mozilla's) hal.  nsToolkit is where the cocoa port sends these notifications so nsToolkit might be a sensible place for the gtk port.

Another thing to be aware of is that the synchronous (blocking) calls in hal/linux/UPowerClient.cpp are tolerated only because that code is not used (or at least not frequently used) on desktop products.

If the D-Bus code is run on startup, then it would need to use only async calls.
It may be easier to use
which is set up for async usage, but may still need extension.  See nsNetworkManagerListener for existing usage.

If it is possible to only send the notification when there are clients listening, and the clients don't register during startup, then sync D-Bus through hal would probably be fine.
Note that if this call is using hal, we might need/want to move the equivalent windows/mac calls to hal too.
I looked at UPower again for a bit yesterday, and put together a small test program to do what would be needed here (outside Mozilla code). The problem is that I didn't get the signals I listed in comment 1 any more, neither with my test program nor with dbus-monitor or upower -m.

The signals related to sleep/hibernation don't show up any more, although I am testing this on the same system... <> tells me that apparently I am not the only one. I sent an email to that list, maybe there will be a reply this time...
So, it turns out that UPower support for these suspend-related notifications is very unstable ( In fact, the plan is to remove them from UPower before v1.0 ( and (re-)implement them in some other library.

To me this seems to suggest that this should be wontfix'ed since there is no interface available on Linux (let alone across Unixes) that could be used.
Can't we use ACPI events?
My /var/log/acpid includes events for various buttons, but I don't see and events for the actual sleep and wake, which could result from any or no button.
Instead of just WONTFIXing this bug, I'd suggest finding another way to implement this. If those folks would rather implement the signals in logind, then we should hook into logind. Maybe dbus will then also provide support for those signals, and we can again just use the dbus signal.

Implementing this bug is still a valid request, regardless of what method is used to fulfill it.
Blocks: 682109
Blocks: Instantbird
I'm not going to be working on this any time soon, but it would probably still be a good idea to fix for platform parity.
Assignee: philipp → nobody
Severity: normal → enhancement
Priority: -- → P3

I noticed in Thunderbird (*) that notification sleep_notification and wake_notification don't occur for linux. Using a python script I can see that the DBUS logind does produce the expected result on sleep/hibernate and resume. However, I have no idea how to incorporate this dbus stuff into mozilla/tb.

I did find something that sounds similar here:

This has something to do with inhibiting screen savers, I think. There is also dbus code for scanning wifi.

Any hint on this appreciated.

(*) The issue with tb is that if check for new mail is turned off, on resume it is possible a connection that use imap IDLE will never received emails until folders are clicked.

Flags: needinfo?(philipp)

With this patch in place, thunderbird on linux now observes and detects the "sleep_notification" and "wake_notification" notification events when system hibernates (to disk) and resumes. I would hope this also works for Suspend/Sleep but haven't tried suspending to memory.

Given this is in toolkit it wouldn't even be specific to Thunderbird. I don't know my way around this code a lot, but I am happy to see progress on this issue. Karl, can you give Gene some advice to get this patch into a reviewable state?

Flags: needinfo?(philipp) → needinfo?(karlt)
Comment on attachment 9036012 [details] [diff] [review]
sleep-wake-notify.diff (it works, but very preliminary)

This looks like a good approach, thanks.

Please put the set-up code in a separate helper function to keep the complexity of each function reasonably simple.

Creating the dbus connection is a blocking operation IIRC, and so I'd like to delay that call to at least give as many other threads as possible a chance to run while waiting for the dbus connection.  The simplest way to do that may be to override nsAppShell::Run(), as done here:

I guess is the starting place for documentation of the patch process.
Flags: needinfo?(karlt)

Karl, Sorry for the delay in getting back to you and working on this again. Been busy with other TB items. Hopefully I or someone else can work on this asap. Thanks for the info.

Blocks: 1726379
See Also: → 1726379

Any chance of finishing this off, Gene?

Flags: needinfo?(gds)
Assignee: nobody → stransky
Pushed by
[Linux] Add support for wake_notification and sleep_notification on Linux r=emilio
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 95 Branch

Oh wow, I was not expecting to read my bugmail after the weekend and find this already finished. Thanks Martin! And thanks for the earlier patch, Gene.

Flags: needinfo?(gds)
Regressions: 1736822

Hi, it's not clear to me if this patch has an impact on end-user impact. Is there something worth mentioning in our release notes for Firefox 95? Thanks

Flags: needinfo?(stransky)

(In reply to Pascal Chevrel:pascalc from comment #21)

Hi, it's not clear to me if this patch has an impact on end-user impact. Is there something worth mentioning in our release notes for Firefox 95? Thanks

Frankly I have no idea. I see the NS_WIDGET_SLEEP_OBSERVER_TOPIC [1] is used in deeps of network stack but I can't say what it really does there.
This change is a bit mysterious to me.


Flags: needinfo?(stransky)
You need to log in before you can comment on or make changes to this bug.