Last active
September 18, 2020 07:43
-
-
Save JensMertelmeyer/a73bbc7c3a3a50ab34f2f603a8607122 to your computer and use it in GitHub Desktop.
PowerRequest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit PowerRequest platform; | |
interface uses WinApi.Windows, PowerRequest.Types; | |
type | |
IPowerRequest = interface | |
['{F8E4EC9C-082A-4FE5-98D2-1A5FCCB48A00}'] | |
procedure setIsActive(const isActive: Boolean); | |
function getIsActive(): Boolean; | |
function getDescription(): String; | |
end; | |
TPowerRequest = class abstract(TInterfacedObject, IPowerRequest) | |
private | |
procedure destroyPowerRequest(); | |
protected var | |
reasonContext: TReasonContext; | |
requestType: TPowerRequestType; | |
description: String; | |
powerRequest: THandle; | |
isActive: Boolean; | |
protected | |
function createPowerRequest(): THandle; | |
constructor Create( | |
const description: String; | |
const requestType: TPowerRequestType | |
); | |
public | |
destructor Destroy(); override; | |
procedure setIsActive(const isActive: Boolean); | |
function getIsActive(): Boolean; | |
function getDescription(): String; | |
end; | |
TSystemRequiredRequest = class(TPowerRequest) | |
public constructor Create(const description: String); | |
end; | |
TScreenRequiredRequest = class(TPowerRequest) | |
public constructor Create(const description: String); | |
end; | |
implementation uses System.SysUtils; | |
constructor TPowerRequest.Create( | |
const description: String; | |
const requestType: TPowerRequestType | |
); | |
begin | |
inherited Create(); | |
self.description := description; | |
self.requestType := requestType; | |
powerRequest := createPowerRequest(); | |
end; | |
function TPowerRequest.createPowerRequest(): THandle; | |
begin | |
reasonContext := Default(TReasonContext); | |
reasonContext.Version := POWER_REQUEST_CONTEXT_VERSION; | |
reasonContext.Flags := POWER_REQUEST_CONTEXT_SIMPLE_STRING; | |
reasonContext.SimpleReasonString := PChar(description); | |
Result := PowerCreateRequest(@reasonContext); | |
end; | |
destructor TPowerRequest.Destroy(); | |
begin | |
setIsActive(False); | |
destroyPowerRequest(); | |
inherited; | |
end; | |
procedure TPowerRequest.destroyPowerRequest(); | |
var | |
isValid: Boolean; | |
begin | |
isValid := (powerRequest <> INVALID_HANDLE_VALUE) and (powerRequest <> 0); | |
if(isValid) then | |
CloseHandle(powerRequest); | |
end; | |
function TPowerRequest.getIsActive(): Boolean; | |
begin | |
Result := isActive; | |
end; | |
function TPowerRequest.getDescription(): String; | |
begin | |
Result := description; | |
end; | |
procedure TPowerRequest.setIsActive(const isActive: Boolean); | |
begin | |
if(isActive = self.isActive) then | |
Exit; | |
if(isActive) then | |
begin | |
Win32Check( PowerSetRequest(powerRequest, requestType) ); | |
self.isActive := True; | |
end | |
else | |
begin | |
Win32Check( PowerClearRequest(powerRequest, requestType) ); | |
self.isActive := False; | |
end; | |
end; | |
{ TScreenRequiredRequest } | |
constructor TScreenRequiredRequest.Create(const description: String); | |
begin | |
inherited Create(description, TPowerRequestType.PowerRequestDisplayRequired); | |
end; | |
{ TSystemRequiredRequest } | |
constructor TSystemRequiredRequest.Create(const description: String); | |
begin | |
inherited Create(description, TPowerRequestType.PowerRequestSystemRequired); | |
end; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit PowerRequest.Types platform; | |
interface uses WinApi.Windows; | |
const | |
POWER_REQUEST_CONTEXT_VERSION = 0; | |
POWER_REQUEST_CONTEXT_DETAILED_STRING = 2; | |
POWER_REQUEST_CONTEXT_SIMPLE_STRING = 1; | |
{$If(RTLVersion < 34)} | |
type | |
TReasonContext = packed record | |
Version: ULONG; | |
Flags: DWORD; | |
case Boolean of | |
False: | |
(SimpleReasonString: PWideChar;); | |
True: | |
(Detailed: record | |
LocalizedReasonModule: HMODULE; | |
LocalizedReasonId: ULONG; | |
ReasonStringCount: ULONG; | |
ReasonStrings: PPWideChar; | |
end;); | |
end; | |
PReasonContext = ^TReasonContext; | |
{$EndIf} | |
type | |
{$MinEnumSize 4} | |
TPowerRequestType = ( | |
PowerRequestDisplayRequired = 0, | |
PowerRequestSystemRequired = 1, | |
PowerRequestAwayModeRequired = 2, | |
PowerRequestExecutionRequired = 3 | |
); | |
function PowerCreateRequest(REASON_CONTEXT: PReasonContext): THandle; stdcall; external kernel32 name 'PowerCreateRequest'; | |
function PowerSetRequest(PowerRequestHandle: THandle; PowerRequestType : TPowerRequestType): BOOL; stdcall; external kernel32 name 'PowerSetRequest'; | |
function PowerClearRequest(PowerRequestHandle: THandle; PowerRequestType : TPowerRequestType): BOOL; stdcall; external kernel32 name 'PowerClearRequest'; | |
implementation | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program PowerRequestsProject; | |
{$AppType Console} | |
uses | |
PowerRequest.Types in 'PowerRequest.Types.pas', | |
PowerRequest in 'PowerRequest.pas'; | |
{$R *.res} | |
procedure p(); | |
var | |
request: IPowerRequest; | |
begin | |
request := TSystemRequiredRequest.Create('Hello World'); | |
request.setIsActive(True); | |
ReadLn; | |
end; | |
begin | |
p(); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment