Skip to content

Instantly share code, notes, and snippets.

@dyipon
Last active December 9, 2018 09:42
Show Gist options
  • Save dyipon/47fd16ede10c2c9b26a6367bec707809 to your computer and use it in GitHub Desktop.
Save dyipon/47fd16ede10c2c9b26a6367bec707809 to your computer and use it in GitHub Desktop.
Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PInvoke.Win32 {
public static class UserInput {
[DllImport("user32.dll", SetLastError=false)]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO {
public uint cbSize;
public int dwTime;
}
public static DateTime LastInput {
get {
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
return lastInput;
}
}
public static TimeSpan IdleTime {
get {
return DateTime.UtcNow.Subtract(LastInput);
}
}
public static int LastInputTicks {
get {
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
GetLastInputInfo(ref lii);
return lii.dwTime;
}
}
}
}
'@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Function ClearAndClose()
{
$Timer.Stop();
$Form.Close();
$Form.Dispose();
$Timer.Dispose();
$Label.Dispose();
$Button.Dispose();
$Script:CountDown=20
}
Function Button_Click()
{
$Script:a = "SKIP"
ClearAndClose
}
Function Timer_Tick()
{
$lastidle = [PInvoke.Win32.UserInput]::IdleTime
$timespan = [TimeSpan]::Parse($lastidle)
$totalSeconds = $timespan.TotalSeconds
$akksi = (Get-WmiObject win32_battery).estimatedChargeRemaining
$Label.Text = "$totalSeconds / $micropause másodperc pihi (akksi: $akksi)"
if ($totalSeconds -gt $micropause)
{
ClearAndClose
}
}
Function Timer_Tick_long()
{
$lastidle = [PInvoke.Win32.UserInput]::IdleTime
$timespan = [TimeSpan]::Parse($lastidle)
$totalSeconds = $timespan.TotalSeconds
$akksi = (Get-WmiObject win32_battery).estimatedChargeRemaining
$Label.Text = "$totalSeconds / $workingpause másodperc pihi (akksi: $akksi)"
if ($totalSeconds -gt $micropause)
{
ClearAndClose
}
}
$freq = 10
$micromin = 10
$workingmin = 50
$micropause = 20
$workingpause = 10 * 60
$maxmicro = $micromin * 60 / $freq
$maxworking = $workingmin * 60 / $freq
$Script:a = "Ok"
while (1){
Start-Sleep -Seconds $freq
$lastidle = [PInvoke.Win32.UserInput]::IdleTime
$timespan = [TimeSpan]::Parse($lastidle)
$totalSeconds = $timespan.TotalSeconds
"last activity: $totalSeconds"
if ($totalSeconds -gt $freq) {
$idle++
$working -= 0
$micro -= 0
} else {
$working++
$idle = 0
$micro++
}
if ($working -lt 0) { $working = 0 }
if ($micro -lt 0) { $micro = 0 }
if ($totalSeconds -gt $micropause) { $micro = 0 }
if ($totalSeconds -gt $workingpause) { $working = 0 }
if ($micro -gt $maxmicro ) {
if ($idle -gt 0) {
$micro = 0 } else {
$sw = [Diagnostics.Stopwatch]::StartNew()
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Attention redémarrage!!"
$Form.Size = New-Object System.Drawing.Size(250,100)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,5)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(55,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "SKIP"
$Button.DialogResult=[System.Windows.Forms.DialogResult]::OK
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Form.Controls.Add($Label)
$Form.Controls.Add($Button)
$Script:CountDown = 6
$Script:CountDown
$Button.Add_Click({Button_Click})
$Timer.Add_Tick({ Timer_Tick})
$Script:a = "Ok"
$Timer.Start()
$Form.ShowDialog()
$sw.Stop()
$sw.Elapsed.TotalSeconds
"eredmény: $Button.DialogResult"
if ( $sw.Elapsed.TotalSeconds -gt $micropause) { $micro = 0 }
if ($Script:a -eq "SKIP") { $micro = 0 }
}
}
if ($working -gt $maxworking ) {
if ($idle -gt 55) {
$micro = 0 } else {
$sw = [Diagnostics.Stopwatch]::StartNew()
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Attention redémarrage!!"
$Form.Size = New-Object System.Drawing.Size(250,100)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,5)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(55,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "SKIP"
$Button.DialogResult=[System.Windows.Forms.DialogResult]::OK
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Form.Controls.Add($Label)
$Form.Controls.Add($Button)
$Script:CountDown = 6
$Script:CountDown
$Button.Add_Click({Button_Click})
$Timer.Add_Tick({ Timer_Tick_long})
$Timer.Start()
$Form.ShowDialog()
$sw.Stop()
$sw.Elapsed.TotalSeconds
if ($Script:a -eq "SKIP") { $working -= 10 }
if ( $sw.Elapsed.TotalSeconds -gt $workingpause) { $working = 0 }
}
}
"idle: $idle, micro: $micro ($maxmicro), working: $working ($maxworking)"
$host.ui.RawUI.WindowTitle = "micro: $micro, rest: $working"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment