Detect if screensaver is active

Depending on your environment:

$ qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive

$ qdbus org.kde.screensaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive

$ qdbus org.gnome.ScreenSaver /ScreenSaver org.gnome.ScreenSaver.GetActive

The correct one should give a true or false statement. The incorrect one will give an error.

$ qdbus org.gnome.ScreenSaver /ScreenSaver org.gnome.ScreenSaver.GetActive
false

$ qdbus org.kde.screensaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive
Service 'org.kde.screensaver' does not exist.

(I'm running Gnome)


You aren't misunderstanding dbus-send.

It's just that Gnome-Shell is lying. That means a Gnome-Shell developer literally took the KDE API description as-is and only implemented the 2 methods that are part of the freedesktop idle inhibition specification. Other methods thus return with unimplemented error:

+        } else if (g_strcmp0 (method_name, "GetActive") == 0) {
+                goto unimplemented;
+        } else ...

Review comments that considered this approach being confusing were ignored.

This is still the status quo with Gnome-Shell 3.38.1 (e.g. on Fedora 33):

$ qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive
Error: org.freedesktop.DBus.Error.NotSupported
This method is not implemented
$ qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.GetActive
Error: org.freedesktop.DBus.Error.NotSupported
This method is not implemented

Thus, your best approach is to try multiple methods until you don't get an NotSupported error back.

For example to check for Gnome-Shell:

$ qdbus org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.GetActive                
false