-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockTimeoutController.ps1
More file actions
154 lines (128 loc) · 4.59 KB
/
LockTimeoutController.ps1
File metadata and controls
154 lines (128 loc) · 4.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet('OnLock', 'OnUnlock', 'PromoteOnWake')]
[string]$Action
)
$ErrorActionPreference = 'Stop'
$DataDir = Join-Path $env:LOCALAPPDATA 'Turn-off-screen-on-lock'
if (-not (Test-Path -LiteralPath $DataDir)) {
New-Item -ItemType Directory -Path $DataDir -Force | Out-Null
}
$StatePath = Join-Path $DataDir 'state.json'
function Save-State {
param(
[Parameter(Mandatory = $true)]
[hashtable]$State
)
$json = [pscustomobject]$State | ConvertTo-Json -Depth 5
Set-Content -LiteralPath $StatePath -Value $json -Encoding UTF8
}
function Get-State {
if (-not (Test-Path -LiteralPath $StatePath)) {
$initial = @{
status = 'unlocked'
generation = [guid]::NewGuid().Guid
lastActionUtc = (Get-Date).ToUniversalTime().ToString('o')
}
Save-State -State $initial
}
return Get-Content -LiteralPath $StatePath -Raw -Encoding UTF8 | ConvertFrom-Json
}
function Get-Config {
$DefaultBaseline = 1
$DefaultWake = 300
$ConfigPath = Join-Path $DataDir 'config.json'
if (-not (Test-Path -LiteralPath $ConfigPath)) {
$default = [pscustomobject]@{
baselineTimeoutSeconds = $DefaultBaseline
wakeTimeoutSeconds = $DefaultWake
}
$default | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $ConfigPath -Encoding UTF8
return @{ baselineTimeoutSeconds = $DefaultBaseline; wakeTimeoutSeconds = $DefaultWake }
}
try {
$raw = Get-Content -LiteralPath $ConfigPath -Raw -Encoding UTF8
if ([string]::IsNullOrWhiteSpace($raw)) {
return @{ baselineTimeoutSeconds = $DefaultBaseline; wakeTimeoutSeconds = $DefaultWake }
}
$parsed = $raw | ConvertFrom-Json
$baseline = $DefaultBaseline
if ($null -ne $parsed.baselineTimeoutSeconds) {
$val = [int]$parsed.baselineTimeoutSeconds
if ($val -ge 1 -and $val -le 86400) { $baseline = $val }
}
$wake = $DefaultWake
if ($null -ne $parsed.wakeTimeoutSeconds) {
$val = [int]$parsed.wakeTimeoutSeconds
if ($val -ge 1 -and $val -le 86400) { $wake = $val }
}
if ($wake -lt $baseline) { $wake = $baseline }
return @{ baselineTimeoutSeconds = $baseline; wakeTimeoutSeconds = $wake }
}
catch {
return @{ baselineTimeoutSeconds = $DefaultBaseline; wakeTimeoutSeconds = $DefaultWake }
}
}
function Set-VideoConLock {
param(
[Parameter(Mandatory = $true)]
[ValidateRange(0, 86400)]
[int]$Seconds
)
$output = & powercfg /setacvalueindex SCHEME_CURRENT SUB_VIDEO VIDEOCONLOCK $Seconds 2>&1
if ($LASTEXITCODE -ne 0) {
throw "powercfg /setacvalueindex failed (exit code $LASTEXITCODE). Output: $($output -join [Environment]::NewLine)"
}
$output = & powercfg /setdcvalueindex SCHEME_CURRENT SUB_VIDEO VIDEOCONLOCK $Seconds 2>&1
if ($LASTEXITCODE -ne 0) {
throw "powercfg /setdcvalueindex failed (exit code $LASTEXITCODE). Output: $($output -join [Environment]::NewLine)"
}
$output = & powercfg /setactive SCHEME_CURRENT 2>&1
if ($LASTEXITCODE -ne 0) {
throw "powercfg /setactive failed (exit code $LASTEXITCODE). Output: $($output -join [Environment]::NewLine)"
}
}
function New-State {
param(
[Parameter(Mandatory = $true)]
[ValidateSet('locked', 'unlocked')]
[string]$Status,
[Parameter(Mandatory = $true)]
[string]$GenerationValue
)
return @{
status = $Status
generation = $GenerationValue
lastActionUtc = (Get-Date).ToUniversalTime().ToString('o')
}
}
try {
switch ($Action) {
'OnLock' {
$newGeneration = [guid]::NewGuid().Guid
Save-State -State (New-State -Status 'locked' -GenerationValue $newGeneration)
}
'OnUnlock' {
$newGeneration = [guid]::NewGuid().Guid
Save-State -State (New-State -Status 'unlocked' -GenerationValue $newGeneration)
$config = Get-Config
Set-VideoConLock -Seconds $config.baselineTimeoutSeconds
}
'PromoteOnWake' {
$state = Get-State
if ($state.status -ne 'locked') {
break
}
$gen = [string]$state.generation
if ([string]::IsNullOrWhiteSpace($gen)) {
break
}
$config = Get-Config
Set-VideoConLock -Seconds $config.wakeTimeoutSeconds
}
}
}
catch {
throw
}