Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep monitor on setting + bug fix #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
24 changes: 13 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class AppContext : ApplicationContext {
private IContainer components;
private Icon onIcon;
private Icon offIcon;
private uint? oldState = null;
private Timer timer;
private SettingsForm settingsForm = null;
private AboutForm aboutForm = null;
Expand Down Expand Up @@ -179,9 +178,14 @@ bool isActive() {

void activate(int duration) {
var sleepDisabled = NativeMethods.ES_CONTINUOUS |
NativeMethods.ES_DISPLAY_REQUIRED;
oldState = NativeMethods.SetThreadExecutionState(sleepDisabled);
if (oldState == 0) {
NativeMethods.ES_SYSTEM_REQUIRED;

if (Settings.Default.KeepMonitorOn) {
sleepDisabled |= NativeMethods.ES_DISPLAY_REQUIRED;
}

uint previousState = NativeMethods.SetThreadExecutionState(sleepDisabled);
if (previousState == 0) {
ShowError();
ExitThread();
}
Expand All @@ -193,14 +197,12 @@ void activate(int duration) {
this.notifyIcon.Text = "Caffeinated: sleep not allowed!";
}

void deactivate() {
private void deactivate()
{
timer.Stop();
if (oldState.HasValue) {
uint result =
NativeMethods.SetThreadExecutionState(oldState.Value);
if (result == 0) {
ShowError();
}
uint result = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS);
if (result == 0) {
ShowError();
}
this.notifyIcon.Icon = offIcon;
this.notifyIcon.Text = "Caffeinated: sleep allowed";
Expand Down
16 changes: 14 additions & 2 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<Setting Name="Durations" Type="System.String" Scope="User">
<Value Profile="(Default)">0,5,10,15,30,60,180,300</Value>
</Setting>
<Setting Name="KeepMonitorOn" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
84 changes: 50 additions & 34 deletions SettingsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<setting name="Durations" serializeAs="String">
<value>0,5,10,15,30,60,180,300</value>
</setting>
<setting name="KeepMonitorOn" serializeAs="String">
<value>True</value>
</setting>
</Caffeinated.Properties.Settings>
</userSettings>
</configuration>