- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
140 lines (120 loc) · 4.81 KB
/
Copy pathProgram.cs
File metadata and controls
140 lines (120 loc) · 4.81 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
usingSystem.Drawing.Imaging;
usingSystem.Runtime.InteropServices;
usingTimer=System.Windows.Forms.Timer;
namespaceAwayBuster;
internalstaticclassProgram
{
/// <summary>
/// The interval in seconds between idle checks
/// </summary>
privateconstintTIMER_INTERVAL_SECONDS=60;
/// <summary>
/// The maximum allowed idle time in seconds before triggering mouse movement
/// </summary>
privateconstintMAX_IDLE_SECONDS=120;
/// <summary>
/// Retrieves the time elapsed since the last user input
/// </summary>
/// <returns>A TimeSpan representing the duration since the last user input</returns>
privatestaticTimeSpanRetrieveIdleTime()
{
varlastInputInfo=newLastInputInfo{cbSize=(uint)LastInputInfo.SizeOf};
if(!Interop.GetLastInputInfo(reflastInputInfo))
{
Console.WriteLine("GetLastInputInfo failed");
returnTimeSpan.Zero;
}
varelapsedTicks=Environment.TickCount-(int)lastInputInfo.dwTime;
returnelapsedTicks>0?newTimeSpan(0,0,0,0,elapsedTicks):TimeSpan.Zero;
}
/// <summary>
/// Simulates mouse movement to prevent system idle state
/// by moving the cursor right and then left by 1 pixel
/// </summary>
privatestaticvoidBustIdle()
{
varinputs=newInput[2];
inputs[0].type=Interop.INPUT_MOUSE;
inputs[0].U.mi.dx=1;
inputs[0].U.mi.dy=0;
inputs[0].U.mi.mouseData=0;
inputs[0].U.mi.dwFlags=Interop.MOUSEEVENTF_MOVE;
inputs[0].U.mi.time=0;
inputs[0].U.mi.dwExtraInfo=UIntPtr.Zero;
inputs[1].type=Interop.INPUT_MOUSE;
inputs[1].U.mi.dx=-1;
inputs[1].U.mi.dy=0;
inputs[1].U.mi.mouseData=0;
inputs[1].U.mi.dwFlags=Interop.MOUSEEVENTF_MOVE;
inputs[1].U.mi.time=0;
inputs[1].U.mi.dwExtraInfo=UIntPtr.Zero;
varnumberOfInputsSent=Interop.SendInput((uint)inputs.Length,inputs,Marshal.SizeOf<Input>());
if(numberOfInputsSent!=inputs.Length)Console.WriteLine("SendInput failed: {0}",Marshal.GetLastWin32Error());
}
/// <summary>
/// Creates a red-tinted version of the icon
/// </summary>
/// <param name="originalIcon">The original icon to tint</param>
/// <returns>A red-tinted copy of the original icon</returns>
privatestaticIconCreateRedTintedIcon(IconoriginalIcon)
{
usingvarbitmap=newBitmap(originalIcon.ToBitmap());
usingvargraphics=Graphics.FromImage(bitmap);
// Create a red color matrix
varcolorMatrix=newColorMatrix([
[1,0,0,0,0],
[0,0.5f,0,0,0],
[0,0,0.5f,0,0],
[0,0,0,1,0],
[0.5f,0,0,0,1]
]);
usingvarimageAttributes=newImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix);
graphics.DrawImage(bitmap,newRectangle(0,0,bitmap.Width,bitmap.Height),0,0,bitmap.Width,bitmap.Height,GraphicsUnit.Pixel,imageAttributes);
returnIcon.FromHandle(bitmap.GetHicon());
}
/// <summary>
/// Main entry point of the application. Sets up a system tray icon with a context menu
/// and initializes a timer to check for system idle state.
/// </summary>
[STAThread]
privatestaticvoidMain()
{
// Initialize timer for periodic idle checks
vartimer=newTimer();
timer.Interval=TIMER_INTERVAL_SECONDS*1000;
timer.Tick+=(_,_)=>
{
varidleTime=RetrieveIdleTime();
if(idleTime.TotalSeconds>MAX_IDLE_SECONDS)
BustIdle();
};
varicon=Icon.ExtractAssociatedIcon(Application.ExecutablePath)??SystemIcons.Application;
varredTintedIcon=CreateRedTintedIcon(icon);
// Set up system tray icon
vartrayIcon=newNotifyIcon
{
Icon=icon,
Text="AwayBuster",
Visible=true
};
// Create context menu with Enable/Disable and Exit options
varmenu=newContextMenuStrip();
varenableOption=newToolStripMenuItem("Enable"){CheckOnClick=true,Checked=true};
enableOption.CheckedChanged+=(_,_)=>
{
trayIcon.Icon=enableOption.Checked?icon:redTintedIcon;
timer.Enabled=enableOption.Checked;
};
menu.Items.Add(enableOption);
menu.Items.Add("Exit",null,(_,_)=>
{
trayIcon.Visible=false;
Application.Exit();
});
trayIcon.ContextMenuStrip=menu;
// Start the timer and run the winforms application
timer.Start();
Application.Run();
}
}