@@ -8,54 +8,84 @@ 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 ();
22-
23- 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' ),
20+ factory WakelockPlusLinuxPlugin ({
21+ @visibleForTesting DBusClient ? client,
22+ @visibleForTesting DBusRemoteObject ? object,
23+ @visibleForTesting Future <String > Function ()? appNameGetter,
24+ }) {
25+ final dbusClient = client ?? DBusClient .session ();
26+ final remoteObject =
27+ object ??
28+ DBusRemoteObject (
29+ dbusClient,
30+ name: 'org.freedesktop.portal.Desktop' ,
31+ path: DBusObjectPath ('/org/freedesktop/portal/desktop' ),
32+ );
33+ return WakelockPlusLinuxPlugin ._internal (
34+ dbusClient,
35+ remoteObject,
36+ appNameGetter,
3137 );
3238 }
3339
40+ WakelockPlusLinuxPlugin ._internal (
41+ this ._client,
42+ this ._object,
43+ this ._appNameGetter,
44+ );
45+
46+ final DBusClient _client;
47+ final DBusRemoteObject _object;
48+ final Future <String > Function ()? _appNameGetter;
49+ DBusObjectPath ? _requestHandle;
50+
3451 Future <String > get _appName =>
52+ _appNameGetter? .call () ??
3553 PackageInfo .fromPlatform ().then ((info) => info.appName);
3654
3755 @override
3856 Future <void > toggle ({required bool enable}) async {
3957 if (enable) {
40- _cookie = await _object
58+ final appName = await _appName;
59+ _requestHandle = await _object
4160 .callMethod (
42- 'org.freedesktop.ScreenSaver ' ,
61+ 'org.freedesktop.portal.Inhibit ' ,
4362 'Inhibit' ,
44- [DBusString (await _appName), const DBusString ('wakelock' )],
45- replySignature: DBusSignature .uint32,
63+ [
64+ const DBusString ('' ),
65+ const DBusUint32 (8 ),
66+ DBusDict .stringVariant ({
67+ 'reason' : DBusString ('$appName : wakelock active' ),
68+ }),
69+ ],
70+ replySignature: DBusSignature ('o' ),
4671 )
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! )],
72+ .then ((response) => response.returnValues.single.asObjectPath ());
73+ } else if (_requestHandle != null ) {
74+ final requestObject = DBusRemoteObject (
75+ _client,
76+ name: 'org.freedesktop.portal.Desktop' ,
77+ path: _requestHandle! ,
78+ );
79+ await requestObject.callMethod (
80+ 'org.freedesktop.portal.Request' ,
81+ 'Close' ,
82+ [],
5383 replySignature: DBusSignature .empty,
5484 );
55- _cookie = null ;
85+ _requestHandle = null ;
5686 }
5787 }
5888
5989 @override
60- Future <bool > get enabled async => _cookie != null ;
90+ Future <bool > get enabled async => _requestHandle != null ;
6191}
0 commit comments