wakelock_windows.dart
1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'dart:async';
import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
import 'package:win32/win32.dart';
const _ES_CONTINUOUS = 0x80000000;
const _ES_DISPLAY_REQUIRED = 0x00000002;
/// The windows implementation of the [WakelockPlatformInterface].
///
/// This class implements the `wakelock` plugin functionality for windows using
/// SetThreadExecutionState from win32 api.
///
/// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate#parameters
///
class WakelockWindows extends WakelockPlatformInterface {
bool _isEnabled = false;
@override
Future<void> toggle({required bool enable}) async {
int response;
if (enable) {
response = SetThreadExecutionState(_ES_CONTINUOUS | _ES_DISPLAY_REQUIRED);
} else {
response = SetThreadExecutionState(_ES_CONTINUOUS);
}
// SetThreadExecutionState returns 0 if the operation failed
if (response != 0) {
_isEnabled = enable;
}
}
@override
Future<bool> get enabled async => _isEnabled;
@override
bool get isMock => false;
}