Skip to content

Commit 81f08ba

Browse files
committed
first commit
0 parents  commit 81f08ba

27 files changed

+2663
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
obj
2+
bin
3+
*.user
4+
*.suo

Caffeinated.csproj

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}</ProjectGuid>
9+
<OutputType>WinExe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Caffeinated</RootNamespace>
12+
<AssemblyName>Caffeinated</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>
15+
</TargetFrameworkProfile>
16+
<FileAlignment>512</FileAlignment>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
19+
<PlatformTarget>x86</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
29+
<PlatformTarget>x86</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<ApplicationIcon>Resources\cup-coffee-icon.ico</ApplicationIcon>
39+
</PropertyGroup>
40+
<PropertyGroup>
41+
<StartupObject>Caffeinated.AppContext</StartupObject>
42+
</PropertyGroup>
43+
<ItemGroup>
44+
<Reference Include="System" />
45+
<Reference Include="System.Core" />
46+
<Reference Include="System.Data" />
47+
<Reference Include="System.Data.Linq" />
48+
<Reference Include="System.Drawing" />
49+
<Reference Include="System.Windows.Forms" />
50+
<Reference Include="System.Xml" />
51+
<Reference Include="System.Xml.Linq" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Compile Include="Settings.cs" />
55+
<Compile Include="SettingsForm.cs">
56+
<SubType>Form</SubType>
57+
</Compile>
58+
<Compile Include="SettingsForm.Designer.cs">
59+
<DependentUpon>SettingsForm.cs</DependentUpon>
60+
</Compile>
61+
<Compile Include="Program.cs" />
62+
<Compile Include="Properties\AssemblyInfo.cs" />
63+
<Compile Include="Taskbar.cs" />
64+
<EmbeddedResource Include="SettingsForm.resx">
65+
<DependentUpon>SettingsForm.cs</DependentUpon>
66+
<SubType>Designer</SubType>
67+
</EmbeddedResource>
68+
<EmbeddedResource Include="Properties\Resources.resx">
69+
<Generator>ResXFileCodeGenerator</Generator>
70+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
71+
<SubType>Designer</SubType>
72+
</EmbeddedResource>
73+
<Compile Include="Properties\Resources.Designer.cs">
74+
<AutoGen>True</AutoGen>
75+
<DependentUpon>Resources.resx</DependentUpon>
76+
<DesignTime>True</DesignTime>
77+
</Compile>
78+
<None Include="app.config" />
79+
<None Include="Properties\Settings.settings">
80+
<Generator>SettingsSingleFileGenerator</Generator>
81+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
82+
</None>
83+
<Compile Include="Properties\Settings.Designer.cs">
84+
<AutoGen>True</AutoGen>
85+
<DependentUpon>Settings.settings</DependentUpon>
86+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
87+
</Compile>
88+
</ItemGroup>
89+
<ItemGroup>
90+
<COMReference Include="IWshRuntimeLibrary">
91+
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
92+
<VersionMajor>1</VersionMajor>
93+
<VersionMinor>0</VersionMinor>
94+
<Lcid>0</Lcid>
95+
<WrapperTool>tlbimp</WrapperTool>
96+
<Isolated>False</Isolated>
97+
<EmbedInteropTypes>True</EmbedInteropTypes>
98+
</COMReference>
99+
</ItemGroup>
100+
<ItemGroup>
101+
<None Include="Resources\cup-coffee-icon.ico" />
102+
</ItemGroup>
103+
<ItemGroup>
104+
<None Include="Resources\cup-coffee-icon-bw.ico" />
105+
</ItemGroup>
106+
<ItemGroup>
107+
<None Include="Resources\cup-coffee-icon-128.png" />
108+
</ItemGroup>
109+
<ItemGroup>
110+
<None Include="Resources\cup-coffee-icon-48.png" />
111+
</ItemGroup>
112+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
113+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
114+
Other similar extension points exist, see Microsoft.Common.targets.
115+
<Target Name="BeforeBuild">
116+
</Target>
117+
<Target Name="AfterBuild">
118+
</Target>
119+
-->
120+
</Project>

Caffeinated.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Caffeinated", "Caffeinated.csproj", "{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}.Debug|x86.ActiveCfg = Debug|x86
13+
{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}.Debug|x86.Build.0 = Debug|x86
14+
{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}.Release|x86.ActiveCfg = Release|x86
15+
{AAEC62D1-5C45-45F6-9225-193CD45C3E8F}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

Program.cs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Windows.Forms;
8+
using Caffeinated.Properties;
9+
10+
namespace Caffeinated {
11+
public class Duration {
12+
public int Minutes { get; set; }
13+
public string Description {
14+
get {
15+
return Duration.ToDescription(this.Minutes);
16+
}
17+
}
18+
19+
public static string ToDescription(int time) {
20+
if (time == 0) {
21+
return "Indefinitely";
22+
}
23+
int mins = time % 60;
24+
if (mins != 0) {
25+
return String.Format("{0} minutes", mins);
26+
}
27+
else {
28+
int hours = time / 60;
29+
if (hours == 1) {
30+
return "1 hour";
31+
}
32+
else {
33+
return String.Format("{0} hours", hours);
34+
}
35+
}
36+
}
37+
}
38+
39+
internal static class NativeMethods {
40+
[DllImport("kernel32.dll")]
41+
public static extern uint SetThreadExecutionState(uint esFlags);
42+
public const uint ES_CONTINUOUS = 0x80000000;
43+
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
44+
}
45+
46+
public class AppContext : ApplicationContext {
47+
private NotifyIcon notifyIcon;
48+
private IContainer components;
49+
private Icon onIcon;
50+
private Icon offIcon;
51+
private uint? oldState = null;
52+
private Timer timer;
53+
private SettingsForm settingsForm = null;
54+
55+
[STAThread]
56+
static void Main() {
57+
var context = new AppContext();
58+
Application.Run(context);
59+
}
60+
61+
public AppContext() {
62+
this.components = new Container();
63+
this.timer = new Timer(components);
64+
timer.Tick += new EventHandler(timer_Tick);
65+
66+
var contextMenu = new ContextMenu();
67+
68+
var exitItem = new MenuItem("E&xit");
69+
exitItem.Click += new EventHandler(this.exitItem_Click);
70+
71+
// we want the lower durations to be closer to the mouse. So,
72+
var times = Settings.Default.RealDurations;
73+
IEnumerable<int> sortedTimes = Enumerable.Empty<int>();
74+
if ((new Taskbar()).Position == TaskbarPosition.Top) {
75+
sortedTimes = times.OrderBy(i => i);
76+
}
77+
else {
78+
sortedTimes = times.OrderByDescending(i => i);
79+
}
80+
81+
var activateForItem = new MenuItem("&Activate for");
82+
foreach (var time in sortedTimes) {
83+
var item = new MenuItem(Duration.ToDescription(time));
84+
item.Tag = time;
85+
item.Click += new EventHandler(item_Click);
86+
activateForItem.MenuItems.Add(item);
87+
}
88+
89+
var settingsItem = new MenuItem("&Settings...");
90+
settingsItem.Click += new EventHandler(settingsItem_Click);
91+
92+
contextMenu.MenuItems.AddRange(
93+
new MenuItem[] {
94+
activateForItem,
95+
new MenuItem("-"),
96+
settingsItem,
97+
exitItem
98+
}
99+
);
100+
101+
this.offIcon = new Icon(
102+
Properties.Resources.cup_coffee_icon_bw,
103+
SystemInformation.SmallIconSize
104+
);
105+
this.onIcon = new Icon(
106+
Properties.Resources.cup_coffee_icon,
107+
SystemInformation.SmallIconSize
108+
);
109+
this.notifyIcon = new NotifyIcon(this.components);
110+
111+
notifyIcon.ContextMenu = contextMenu;
112+
113+
// tooltip
114+
notifyIcon.Text = "Caffeinated";
115+
notifyIcon.Visible = true;
116+
117+
// Handle the DoubleClick event to activate the form.
118+
notifyIcon.MouseClick += new MouseEventHandler(notifyIcon1_Click);
119+
120+
if (Settings.Default.ActivateAtLaunch) {
121+
activate(Settings.Default.DefaultDuration);
122+
}
123+
else {
124+
deactivate();
125+
}
126+
if (Settings.Default.ShowSettingsAtLaunch) {
127+
showSettings();
128+
}
129+
}
130+
131+
void settingsItem_Click(object sender, EventArgs e) {
132+
showSettings();
133+
}
134+
135+
void showSettings() {
136+
settingsForm = new SettingsForm();
137+
settingsForm.Show();
138+
}
139+
140+
void timer_Tick(object sender, EventArgs e) {
141+
deactivate();
142+
}
143+
144+
void item_Click(object sender, EventArgs e) {
145+
int time = (int)((MenuItem)sender).Tag;
146+
this.activate(time);
147+
}
148+
149+
void notifyIcon1_Click(object sender, MouseEventArgs e) {
150+
if (e.Button != MouseButtons.Left) return;
151+
if (this.isActive())
152+
this.deactivate();
153+
else
154+
this.activate(Settings.Default.DefaultDuration);
155+
}
156+
157+
void ShowError() {
158+
MessageBox.Show(
159+
"Call to SetThreadExecutionState failed.",
160+
"Caffeinated",
161+
MessageBoxButtons.OK
162+
);
163+
}
164+
165+
bool isActive() {
166+
return notifyIcon.Icon == onIcon;
167+
}
168+
169+
void activate(int duration) {
170+
var sleepDisabled = NativeMethods.ES_CONTINUOUS |
171+
NativeMethods.ES_SYSTEM_REQUIRED;
172+
oldState = NativeMethods.SetThreadExecutionState(sleepDisabled);
173+
if (oldState == 0) {
174+
ShowError();
175+
ExitThread();
176+
}
177+
if (duration > 0) {
178+
this.timer.Interval = duration * 60 * 1000;
179+
this.timer.Start();
180+
}
181+
this.notifyIcon.Icon = onIcon;
182+
}
183+
184+
void deactivate() {
185+
timer.Stop();
186+
if (oldState.HasValue) {
187+
uint result =
188+
NativeMethods.SetThreadExecutionState(oldState.Value);
189+
if (result == 0) {
190+
ShowError();
191+
}
192+
}
193+
this.notifyIcon.Icon = offIcon;
194+
}
195+
196+
private void exitItem_Click(object Sender, EventArgs e) {
197+
deactivate();
198+
ExitThread();
199+
}
200+
201+
protected override void Dispose(bool disposing) {
202+
if (disposing && components != null)
203+
components.Dispose();
204+
base.Dispose(disposing);
205+
}
206+
}
207+
}

Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Caffeinated")]
9+
[assembly: AssemblyDescription("Prevents Windows from sleeping.")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Desmond Brand")]
12+
[assembly: AssemblyProduct("Caffeinated")]
13+
[assembly: AssemblyCopyright("Copyright © Desmond Brand 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("32521a70-b807-4870-9738-8f4045ac9d36")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)