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
11 changes: 9 additions & 2 deletions wakelock_plus/lib/src/wakelock_plus_windows_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart';
import 'package:win32/win32.dart';

Expand All @@ -26,9 +27,15 @@ class WakelockPlusWindowsPlugin extends WakelockPlusPlatformInterface {
}

// SetThreadExecutionState returns 0 if the operation failed.
if (response != 0) {
_enabled = enable;
if (response == 0) {
final operation = enable ? 'enable' : 'disable';
throw PlatformException(
code: 'wakelock-error',
message: 'Failed to $operation wakelock.',
details: {'operation': operation},
);
}
_enabled = enable;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,47 @@ public class WakelockPlusMacosPlugin: NSObject, FlutterPlugin {
case "toggle":
let args = call.arguments as? Dictionary<String, Any>
let enable = args!["enable"] as! Bool
if enable {
enableWakelock()
let status = enable ? enableWakelock() : disableWakelock()
if status == kIOReturnSuccess {
result(true)
} else {
disableWakelock();
let operation = enable ? "enable" : "disable"
let description = String(cString: mach_error_string(status))
result(FlutterError(
code: "wakelock-error",
message: "Failed to \(operation) wakelock: \(description)",
details: [
"operation": operation,
"status": Int(status),
"description": description
]
))
}
result(true)
case "enabled":
result(wakelockEnabled)
default:
result(FlutterMethodNotImplemented)
}
}

func enableWakelock(reason: String = "Disabling display sleep") {
if !wakelockEnabled {
wakelockEnabled = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString,
IOPMAssertionLevel(kIOPMAssertionLevelOn),
reason as CFString,
&assertionID) == kIOReturnSuccess
}
func enableWakelock(reason: String = "Disabling display sleep") -> IOReturn {
guard !wakelockEnabled else { return kIOReturnSuccess }
let status = IOPMAssertionCreateWithName(
kIOPMAssertionTypeNoDisplaySleep as CFString,
IOPMAssertionLevel(kIOPMAssertionLevelOn),
reason as CFString,
&assertionID
)
wakelockEnabled = status == kIOReturnSuccess
return status
}

func disableWakelock() {
if wakelockEnabled {
IOPMAssertionRelease(assertionID)
func disableWakelock() -> IOReturn {
guard wakelockEnabled else { return kIOReturnSuccess }
let status = IOPMAssertionRelease(assertionID)
if status == kIOReturnSuccess {
wakelockEnabled = false
}
return status
}
}
Loading