Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -351,29 +351,40 @@ jobs:
libgtk-3-dev liblzma-dev xvfb dbus-x11 \
python3-dbus python3-gi

- name: Run Linux Unit Tests
run: |
cd wakelock_plus
flutter pub get
flutter test test/wakelock_plus_linux_plugin_test.dart

- name: Run Integration Tests
run: |
flutter config --enable-linux-desktop
cd wakelock_plus/example
flutter pub get

# We use xvfb-run (display) and dbus-run-session (bus).
# Inside, we run a Python mock of 'org.freedesktop.ScreenSaver' so the plugin logic succeeds.
# Inside, we run a Python mock of 'org.freedesktop.portal.Desktop' so the plugin logic succeeds.
xvfb-run dbus-run-session bash -c "
python3 -c \"
import dbus, dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
class Mock(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName('org.freedesktop.ScreenSaver', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/org/freedesktop/ScreenSaver')
@dbus.service.method('org.freedesktop.ScreenSaver', in_signature='ss', out_signature='u')
def Inhibit(self, a, r): return 1
@dbus.service.method('org.freedesktop.ScreenSaver', in_signature='u', out_signature='')
def UnInhibit(self, c): pass
class RequestMock(dbus.service.Object):
def __init__(self, bus):
dbus.service.Object.__init__(self, bus, '/org/freedesktop/portal/desktop/request/1_1/test')
@dbus.service.method('org.freedesktop.portal.Request', in_signature='', out_signature='')
def Close(self): pass
class DesktopMock(dbus.service.Object):
def __init__(self, bus):
bus_name = dbus.service.BusName('org.freedesktop.portal.Desktop', bus=bus)
dbus.service.Object.__init__(self, bus_name, '/org/freedesktop/portal/desktop')
@dbus.service.method('org.freedesktop.portal.Inhibit', in_signature='sua{sv}', out_signature='o')
def Inhibit(self, window, flags, options): return dbus.ObjectPath('/org/freedesktop/portal/desktop/request/1_1/test')
DBusGMainLoop(set_as_default=True)
Mock()
bus = dbus.SessionBus()
DesktopMock(bus)
RequestMock(bus)
GLib.MainLoop().run()\" &
sleep 5 && \
flutter drive \
Expand Down
80 changes: 55 additions & 25 deletions wakelock_plus/lib/src/wakelock_plus_linux_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,84 @@ import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interfac
/// The Linux implementation of the [WakelockPlusPlatformInterface].
///
/// This class implements the `wakelock_plus` plugin functionality for Linux
/// using the `org.freedesktop.ScreenSaver` D-Bus API
/// (see https://specifications.freedesktop.org/idle-inhibit-spec/latest/re01.html).
/// using the `org.freedesktop.portal.Inhibit` D-Bus API
/// (see https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Inhibit).
class WakelockPlusLinuxPlugin extends WakelockPlusPlatformInterface {
/// Registers this class as the default instance of [WakelockPlatformInterface].
static void registerWith() {
WakelockPlusPlatformInterface.instance = WakelockPlusLinuxPlugin();
}

/// Constructs an instance of [WakelockPlusLinuxPlugin].
WakelockPlusLinuxPlugin({@visibleForTesting DBusRemoteObject? object})
: _object = object ?? _createRemoteObject();

final DBusRemoteObject _object;
int? _cookie;

static DBusRemoteObject _createRemoteObject() {
return DBusRemoteObject(
DBusClient.session(),
name: 'org.freedesktop.ScreenSaver',
path: DBusObjectPath('/org/freedesktop/ScreenSaver'),
factory WakelockPlusLinuxPlugin({
@visibleForTesting DBusClient? client,
@visibleForTesting DBusRemoteObject? object,
@visibleForTesting Future<String> Function()? appNameGetter,
}) {
final dbusClient = client ?? DBusClient.session();
final remoteObject =
object ??
DBusRemoteObject(
dbusClient,
name: 'org.freedesktop.portal.Desktop',
path: DBusObjectPath('/org/freedesktop/portal/desktop'),
);
return WakelockPlusLinuxPlugin._internal(
dbusClient,
remoteObject,
appNameGetter,
);
}

WakelockPlusLinuxPlugin._internal(
this._client,
this._object,
this._appNameGetter,
);

final DBusClient _client;
final DBusRemoteObject _object;
final Future<String> Function()? _appNameGetter;
DBusObjectPath? _requestHandle;

Future<String> get _appName =>
_appNameGetter?.call() ??
PackageInfo.fromPlatform().then((info) => info.appName);

@override
Future<void> toggle({required bool enable}) async {
if (enable) {
_cookie = await _object
final appName = await _appName;
_requestHandle = await _object
.callMethod(
'org.freedesktop.ScreenSaver',
'org.freedesktop.portal.Inhibit',
'Inhibit',
[DBusString(await _appName), const DBusString('wakelock')],
replySignature: DBusSignature.uint32,
[
const DBusString(''),
const DBusUint32(8),
DBusDict.stringVariant({
'reason': DBusString('$appName: wakelock active'),
}),
],
replySignature: DBusSignature('o'),
)
.then((response) => response.returnValues.single.asUint32());
} else if (_cookie != null) {
await _object.callMethod(
'org.freedesktop.ScreenSaver',
'UnInhibit',
[DBusUint32(_cookie!)],
.then((response) => response.returnValues.single.asObjectPath());
} else if (_requestHandle != null) {
final requestObject = DBusRemoteObject(
_client,
name: 'org.freedesktop.portal.Desktop',
path: _requestHandle!,
);
await requestObject.callMethod(
'org.freedesktop.portal.Request',
'Close',
[],
replySignature: DBusSignature.empty,
);
_cookie = null;
_requestHandle = null;
}
}

@override
Future<bool> get enabled async => _cookie != null;
Future<bool> get enabled async => _requestHandle != null;
}
2 changes: 2 additions & 0 deletions wakelock_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dev_dependencies:
sdk: flutter
flutter_lints: ^6.0.0
pigeon: ^26.2.3 # dart run pigeon --input "pigeons/messages.dart"
mockito: ^5.4.4
build_runner: ^2.4.13

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
Loading