@@ -8,54 +8,72 @@ import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interfac
88/// The Linux implementation of the [WakelockPlusPlatformInterface] .
99///
1010/// This class implements the `wakelock_plus` plugin functionality for Linux
11- /// using the `org.freedesktop.ScreenSaver ` D-Bus API
12- /// (see https://specifications.freedesktop.org/idle-inhibit-spec/latest/re01.html ).
11+ /// using the `org.freedesktop.portal.Inhibit ` D-Bus API
12+ /// (see https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Inhibit ).
1313class WakelockPlusLinuxPlugin extends WakelockPlusPlatformInterface {
1414 /// Registers this class as the default instance of [WakelockPlatformInterface] .
1515 static void registerWith () {
1616 WakelockPlusPlatformInterface .instance = WakelockPlusLinuxPlugin ();
1717 }
1818
1919 /// Constructs an instance of [WakelockPlusLinuxPlugin] .
20- WakelockPlusLinuxPlugin ({@visibleForTesting DBusRemoteObject ? object})
21- : _object = object ?? _createRemoteObject ();
20+ factory WakelockPlusLinuxPlugin ({
21+ @visibleForTesting DBusClient ? client,
22+ @visibleForTesting DBusRemoteObject ? object,
23+ }) {
24+ final dbusClient = client ?? DBusClient .session ();
25+ final remoteObject = object ??
26+ DBusRemoteObject (
27+ dbusClient,
28+ name: 'org.freedesktop.portal.Desktop' ,
29+ path: DBusObjectPath ('/org/freedesktop/portal/desktop' ),
30+ );
31+ return WakelockPlusLinuxPlugin ._internal (dbusClient, remoteObject);
32+ }
33+
34+ WakelockPlusLinuxPlugin ._internal (this ._client, this ._object);
2235
36+ final DBusClient _client;
2337 final DBusRemoteObject _object;
24- int ? _cookie;
25-
26- static DBusRemoteObject _createRemoteObject () {
27- return DBusRemoteObject (
28- DBusClient .session (),
29- name: 'org.freedesktop.ScreenSaver' ,
30- path: DBusObjectPath ('/org/freedesktop/ScreenSaver' ),
31- );
32- }
38+ DBusObjectPath ? _requestHandle;
3339
3440 Future <String > get _appName =>
3541 PackageInfo .fromPlatform ().then ((info) => info.appName);
3642
3743 @override
3844 Future <void > toggle ({required bool enable}) async {
3945 if (enable) {
40- _cookie = await _object
46+ final appName = await _appName;
47+ _requestHandle = await _object
4148 .callMethod (
42- 'org.freedesktop.ScreenSaver ' ,
49+ 'org.freedesktop.portal.Inhibit ' ,
4350 'Inhibit' ,
44- [DBusString (await _appName), const DBusString ('wakelock' )],
45- replySignature: DBusSignature .uint32,
51+ [
52+ const DBusString ('' ),
53+ const DBusUint32 (8 ),
54+ DBusDict .stringVariant ({
55+ 'reason' : DBusString ('$appName : wakelock active' ),
56+ }),
57+ ],
58+ replySignature: DBusSignature ('o' ),
4659 )
47- .then ((response) => response.returnValues.single.asUint32 ());
48- } else if (_cookie != null ) {
49- await _object.callMethod (
50- 'org.freedesktop.ScreenSaver' ,
51- 'UnInhibit' ,
52- [DBusUint32 (_cookie! )],
60+ .then ((response) => response.returnValues.single.asObjectPath ());
61+ } else if (_requestHandle != null ) {
62+ final requestObject = DBusRemoteObject (
63+ _client,
64+ name: 'org.freedesktop.portal.Desktop' ,
65+ path: _requestHandle! ,
66+ );
67+ await requestObject.callMethod (
68+ 'org.freedesktop.portal.Request' ,
69+ 'Close' ,
70+ [],
5371 replySignature: DBusSignature .empty,
5472 );
55- _cookie = null ;
73+ _requestHandle = null ;
5674 }
5775 }
5876
5977 @override
60- Future <bool > get enabled async => _cookie != null ;
78+ Future <bool > get enabled async => _requestHandle != null ;
6179}
0 commit comments