diff --git a/Program.cs b/Program.cs index 26aa5cc..a7f861b 100644 --- a/Program.cs +++ b/Program.cs @@ -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; @@ -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(); } @@ -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"; diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index 60db5ad..efa7f65 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.237 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace Caffeinated.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -70,5 +70,17 @@ public string Durations { this["Durations"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool KeepMonitorOn { + get { + return ((bool)(this["KeepMonitorOn"])); + } + set { + this["KeepMonitorOn"] = value; + } + } } } diff --git a/Properties/Settings.settings b/Properties/Settings.settings index 58864a0..1822b06 100644 --- a/Properties/Settings.settings +++ b/Properties/Settings.settings @@ -14,5 +14,8 @@ 0,5,10,15,30,60,180,300 + + True + \ No newline at end of file diff --git a/SettingsForm.Designer.cs b/SettingsForm.Designer.cs index 1735e0b..cb086ae 100644 --- a/SettingsForm.Designer.cs +++ b/SettingsForm.Designer.cs @@ -28,12 +28,13 @@ private void InitializeComponent() { this.label2 = new System.Windows.Forms.Label(); this.DefaultDurationBox = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); - this.SettingsAtLaunchChkBox = new System.Windows.Forms.CheckBox(); - this.ActivateChkBox = new System.Windows.Forms.CheckBox(); this.StartupChkBox = new System.Windows.Forms.CheckBox(); this.okBtn = new System.Windows.Forms.Button(); this.cancelBtn = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.monitorOnChkBox = new System.Windows.Forms.CheckBox(); + this.SettingsAtLaunchChkBox = new System.Windows.Forms.CheckBox(); + this.ActivateChkBox = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -44,8 +45,8 @@ private void InitializeComponent() { this.label1.Size = new System.Drawing.Size(317, 67); this.label1.TabIndex = 0; this.label1.Text = "Caffeinated is now running. You can find its icon in the notification area near t" + - "he clock. Click it to keep your PC awake by disabling automatic sleep. Click the" + - " icon again to allow automatic sleep."; + "he clock. Click it to keep your PC awake by disabling automatic sleep. Click the" + + " icon again to allow automatic sleep."; // // label2 // @@ -59,7 +60,7 @@ private void InitializeComponent() { // DefaultDurationBox // this.DefaultDurationBox.FormattingEnabled = true; - this.DefaultDurationBox.Location = new System.Drawing.Point(229, 194); + this.DefaultDurationBox.Location = new System.Drawing.Point(229, 219); this.DefaultDurationBox.Name = "DefaultDurationBox"; this.DefaultDurationBox.Size = new System.Drawing.Size(140, 23); this.DefaultDurationBox.TabIndex = 5; @@ -68,37 +69,12 @@ private void InitializeComponent() { // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(127, 197); + this.label3.Location = new System.Drawing.Point(127, 222); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(96, 15); this.label3.TabIndex = 6; this.label3.Text = "Default duration:"; // - // SettingsAtLaunchChkBox - // - this.SettingsAtLaunchChkBox.AutoSize = true; - this.SettingsAtLaunchChkBox.Checked = global::Caffeinated.Properties.Settings.Default.ShowSettingsAtLaunch; - this.SettingsAtLaunchChkBox.CheckState = System.Windows.Forms.CheckState.Checked; - this.SettingsAtLaunchChkBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Caffeinated.Properties.Settings.Default, "ShowSettingsAtLaunch", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); - this.SettingsAtLaunchChkBox.Location = new System.Drawing.Point(130, 169); - this.SettingsAtLaunchChkBox.Name = "SettingsAtLaunchChkBox"; - this.SettingsAtLaunchChkBox.Size = new System.Drawing.Size(196, 19); - this.SettingsAtLaunchChkBox.TabIndex = 4; - this.SettingsAtLaunchChkBox.Text = "Show this message upon launch"; - this.SettingsAtLaunchChkBox.UseVisualStyleBackColor = true; - // - // ActivateChkBox - // - this.ActivateChkBox.AutoSize = true; - this.ActivateChkBox.Checked = global::Caffeinated.Properties.Settings.Default.ActivateAtLaunch; - this.ActivateChkBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Caffeinated.Properties.Settings.Default, "ActivateAtLaunch", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); - this.ActivateChkBox.Location = new System.Drawing.Point(130, 144); - this.ActivateChkBox.Name = "ActivateChkBox"; - this.ActivateChkBox.Size = new System.Drawing.Size(139, 19); - this.ActivateChkBox.TabIndex = 3; - this.ActivateChkBox.Text = "Activate upon launch"; - this.ActivateChkBox.UseVisualStyleBackColor = true; - // // StartupChkBox // this.StartupChkBox.AutoSize = true; @@ -113,7 +89,7 @@ private void InitializeComponent() { // okBtn // this.okBtn.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.okBtn.Location = new System.Drawing.Point(263, 227); + this.okBtn.Location = new System.Drawing.Point(263, 248); this.okBtn.Name = "okBtn"; this.okBtn.Size = new System.Drawing.Size(87, 27); this.okBtn.TabIndex = 8; @@ -125,7 +101,7 @@ private void InitializeComponent() { // this.cancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.cancelBtn.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.cancelBtn.Location = new System.Drawing.Point(357, 227); + this.cancelBtn.Location = new System.Drawing.Point(357, 248); this.cancelBtn.Name = "cancelBtn"; this.cancelBtn.Size = new System.Drawing.Size(87, 27); this.cancelBtn.TabIndex = 9; @@ -142,13 +118,52 @@ private void InitializeComponent() { this.pictureBox1.TabIndex = 10; this.pictureBox1.TabStop = false; // + // monitorOnChkBox + // + this.monitorOnChkBox.AutoSize = true; + this.monitorOnChkBox.Checked = global::Caffeinated.Properties.Settings.Default.KeepMonitorOn; + this.monitorOnChkBox.CheckState = System.Windows.Forms.CheckState.Checked; + this.monitorOnChkBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Caffeinated.Properties.Settings.Default, "KeepMonitorOn", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); + this.monitorOnChkBox.Location = new System.Drawing.Point(130, 169); + this.monitorOnChkBox.Name = "monitorOnChkBox"; + this.monitorOnChkBox.Size = new System.Drawing.Size(115, 19); + this.monitorOnChkBox.TabIndex = 11; + this.monitorOnChkBox.Text = "Keep monitor on"; + this.monitorOnChkBox.UseVisualStyleBackColor = true; + // + // SettingsAtLaunchChkBox + // + this.SettingsAtLaunchChkBox.AutoSize = true; + this.SettingsAtLaunchChkBox.Checked = global::Caffeinated.Properties.Settings.Default.ShowSettingsAtLaunch; + this.SettingsAtLaunchChkBox.CheckState = System.Windows.Forms.CheckState.Checked; + this.SettingsAtLaunchChkBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Caffeinated.Properties.Settings.Default, "ShowSettingsAtLaunch", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); + this.SettingsAtLaunchChkBox.Location = new System.Drawing.Point(130, 194); + this.SettingsAtLaunchChkBox.Name = "SettingsAtLaunchChkBox"; + this.SettingsAtLaunchChkBox.Size = new System.Drawing.Size(196, 19); + this.SettingsAtLaunchChkBox.TabIndex = 4; + this.SettingsAtLaunchChkBox.Text = "Show this message upon launch"; + this.SettingsAtLaunchChkBox.UseVisualStyleBackColor = true; + // + // ActivateChkBox + // + this.ActivateChkBox.AutoSize = true; + this.ActivateChkBox.Checked = global::Caffeinated.Properties.Settings.Default.ActivateAtLaunch; + this.ActivateChkBox.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Caffeinated.Properties.Settings.Default, "ActivateAtLaunch", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); + this.ActivateChkBox.Location = new System.Drawing.Point(130, 144); + this.ActivateChkBox.Name = "ActivateChkBox"; + this.ActivateChkBox.Size = new System.Drawing.Size(139, 19); + this.ActivateChkBox.TabIndex = 3; + this.ActivateChkBox.Text = "Activate upon launch"; + this.ActivateChkBox.UseVisualStyleBackColor = true; + // // SettingsForm // this.AcceptButton = this.okBtn; this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.cancelBtn; - this.ClientSize = new System.Drawing.Size(456, 266); + this.ClientSize = new System.Drawing.Size(456, 286); + this.Controls.Add(this.monitorOnChkBox); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.cancelBtn); this.Controls.Add(this.okBtn); @@ -182,5 +197,6 @@ private void InitializeComponent() { private System.Windows.Forms.Button okBtn; private System.Windows.Forms.Button cancelBtn; private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.CheckBox monitorOnChkBox; } } \ No newline at end of file diff --git a/app.config b/app.config index 880e4a5..eb9e47b 100644 --- a/app.config +++ b/app.config @@ -19,6 +19,9 @@ 0,5,10,15,30,60,180,300 + + True +