StephaneLenclud@193
|
1 |
//
|
StephaneLenclud@193
|
2 |
// Copyright (C) 2014-2016 Stéphane Lenclud.
|
StephaneLenclud@193
|
3 |
//
|
StephaneLenclud@193
|
4 |
// This file is part of SharpDisplayManager.
|
StephaneLenclud@193
|
5 |
//
|
StephaneLenclud@193
|
6 |
// SharpDisplayManager is free software: you can redistribute it and/or modify
|
StephaneLenclud@193
|
7 |
// it under the terms of the GNU General Public License as published by
|
StephaneLenclud@193
|
8 |
// the Free Software Foundation, either version 3 of the License, or
|
StephaneLenclud@193
|
9 |
// (at your option) any later version.
|
StephaneLenclud@193
|
10 |
//
|
StephaneLenclud@193
|
11 |
// SharpDisplayManager is distributed in the hope that it will be useful,
|
StephaneLenclud@193
|
12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
StephaneLenclud@193
|
13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
StephaneLenclud@193
|
14 |
// GNU General Public License for more details.
|
StephaneLenclud@193
|
15 |
//
|
StephaneLenclud@193
|
16 |
// You should have received a copy of the GNU General Public License
|
StephaneLenclud@193
|
17 |
// along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>.
|
StephaneLenclud@193
|
18 |
//
|
StephaneLenclud@193
|
19 |
|
StephaneLenclud@193
|
20 |
using System;
|
StephaneLenclud@192
|
21 |
using System.Collections.Generic;
|
StephaneLenclud@192
|
22 |
using System.ComponentModel;
|
StephaneLenclud@192
|
23 |
using System.Data;
|
StephaneLenclud@192
|
24 |
using System.Drawing;
|
StephaneLenclud@192
|
25 |
using System.Linq;
|
StephaneLenclud@192
|
26 |
using System.Text;
|
StephaneLenclud@192
|
27 |
using System.Threading.Tasks;
|
StephaneLenclud@192
|
28 |
using System.Windows.Forms;
|
StephaneLenclud@193
|
29 |
using System.Diagnostics;
|
StephaneLenclud@193
|
30 |
using SharpLib.Display;
|
StephaneLenclud@193
|
31 |
|
StephaneLenclud@192
|
32 |
|
StephaneLenclud@192
|
33 |
namespace SharpDisplayIdleClient
|
StephaneLenclud@192
|
34 |
{
|
StephaneLenclud@193
|
35 |
|
StephaneLenclud@193
|
36 |
/// <summary>
|
StephaneLenclud@193
|
37 |
/// Sharp Display Client designed to act as an idle client.
|
StephaneLenclud@193
|
38 |
/// It should take care of screen saving and other such concerns.
|
StephaneLenclud@193
|
39 |
/// </summary>
|
StephaneLenclud@192
|
40 |
public partial class FormClientIdle : Form
|
StephaneLenclud@192
|
41 |
{
|
StephaneLenclud@192
|
42 |
public StartParams Params { get; set; }
|
StephaneLenclud@192
|
43 |
|
StephaneLenclud@193
|
44 |
Client iClient;
|
StephaneLenclud@193
|
45 |
ContentAlignment iAlignment;
|
StephaneLenclud@193
|
46 |
TextField iTextField;
|
Stephane@197
|
47 |
//Used to determine if screen saver need to kick in
|
Stephane@197
|
48 |
private PowerManager.SettingNotifier iPowerSettingNotifier;
|
Stephane@197
|
49 |
private bool MonitorPowerOn;
|
StephaneLenclud@193
|
50 |
|
StephaneLenclud@193
|
51 |
public delegate void CloseDelegate();
|
StephaneLenclud@193
|
52 |
public delegate void CloseConnectionDelegate();
|
StephaneLenclud@193
|
53 |
|
StephaneLenclud@193
|
54 |
|
StephaneLenclud@192
|
55 |
public FormClientIdle()
|
StephaneLenclud@192
|
56 |
{
|
StephaneLenclud@192
|
57 |
InitializeComponent();
|
StephaneLenclud@192
|
58 |
}
|
StephaneLenclud@193
|
59 |
|
StephaneLenclud@193
|
60 |
/// <summary>
|
StephaneLenclud@193
|
61 |
///
|
StephaneLenclud@193
|
62 |
/// </summary>
|
StephaneLenclud@193
|
63 |
/// <param name="sender"></param>
|
StephaneLenclud@193
|
64 |
/// <param name="e"></param>
|
StephaneLenclud@193
|
65 |
private void FormClientIdle_Load(object sender, EventArgs e)
|
StephaneLenclud@193
|
66 |
{
|
StephaneLenclud@196
|
67 |
//Prevents showing in the Open Task view (Windows Key + Tab)
|
StephaneLenclud@196
|
68 |
Visible = false;
|
StephaneLenclud@196
|
69 |
|
StephaneLenclud@193
|
70 |
//Display client
|
StephaneLenclud@193
|
71 |
iClient = new Client();
|
StephaneLenclud@193
|
72 |
iClient.CloseOrderEvent += OnCloseOrder;
|
StephaneLenclud@193
|
73 |
iClient.Open();
|
StephaneLenclud@193
|
74 |
iClient.SetName("Idle");
|
StephaneLenclud@194
|
75 |
iClient.SetPriority(Priorities.Background);
|
StephaneLenclud@193
|
76 |
SetupDisplayClient();
|
StephaneLenclud@193
|
77 |
|
StephaneLenclud@193
|
78 |
//Timer
|
StephaneLenclud@193
|
79 |
iTimer.Interval = IntervalToNextMinute();
|
StephaneLenclud@193
|
80 |
iTimer.Start();
|
Stephane@197
|
81 |
|
Stephane@197
|
82 |
//Create our power setting notifier and register the event we are interested in
|
Stephane@197
|
83 |
iPowerSettingNotifier = new PowerManager.SettingNotifier(Handle);
|
Stephane@197
|
84 |
iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
|
Stephane@197
|
85 |
iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
|
Stephane@197
|
86 |
MonitorPowerOn = true;
|
Stephane@197
|
87 |
|
StephaneLenclud@193
|
88 |
}
|
StephaneLenclud@193
|
89 |
|
StephaneLenclud@193
|
90 |
/// <summary>
|
Stephane@197
|
91 |
/// Broadcast messages to subscribers.
|
Stephane@197
|
92 |
/// </summary>
|
Stephane@197
|
93 |
/// <param name="message"></param>
|
Stephane@197
|
94 |
protected override void WndProc(ref Message aMessage)
|
Stephane@197
|
95 |
{
|
Stephane@197
|
96 |
//Hook in our power manager
|
Stephane@197
|
97 |
if (iPowerSettingNotifier != null)
|
Stephane@197
|
98 |
{
|
Stephane@197
|
99 |
iPowerSettingNotifier.WndProc(ref aMessage);
|
Stephane@197
|
100 |
}
|
Stephane@197
|
101 |
|
Stephane@197
|
102 |
base.WndProc(ref aMessage);
|
Stephane@197
|
103 |
}
|
Stephane@197
|
104 |
|
Stephane@197
|
105 |
|
Stephane@197
|
106 |
|
Stephane@197
|
107 |
/// <summary>
|
Stephane@197
|
108 |
///
|
Stephane@197
|
109 |
/// </summary>
|
Stephane@197
|
110 |
private void OnMonitorPowerOn()
|
Stephane@197
|
111 |
{
|
Stephane@197
|
112 |
MonitorPowerOn = true;
|
Stephane@197
|
113 |
UpdateDisplay();
|
Stephane@197
|
114 |
}
|
Stephane@197
|
115 |
|
Stephane@197
|
116 |
/// <summary>
|
Stephane@197
|
117 |
///
|
Stephane@197
|
118 |
/// </summary>
|
Stephane@197
|
119 |
private void OnMonitorPowerOff()
|
Stephane@197
|
120 |
{
|
Stephane@197
|
121 |
MonitorPowerOn = false;
|
Stephane@197
|
122 |
UpdateDisplay();
|
Stephane@197
|
123 |
}
|
Stephane@197
|
124 |
|
Stephane@197
|
125 |
|
Stephane@197
|
126 |
/// <summary>
|
StephaneLenclud@193
|
127 |
///
|
StephaneLenclud@193
|
128 |
/// </summary>
|
StephaneLenclud@193
|
129 |
/// <returns></returns>
|
StephaneLenclud@193
|
130 |
public static int IntervalToNextMinute()
|
StephaneLenclud@193
|
131 |
{
|
StephaneLenclud@193
|
132 |
DateTime now = DateTime.Now;
|
StephaneLenclud@193
|
133 |
return 60000 - now.Millisecond;
|
StephaneLenclud@193
|
134 |
}
|
StephaneLenclud@193
|
135 |
|
StephaneLenclud@193
|
136 |
|
StephaneLenclud@193
|
137 |
/// <summary>
|
StephaneLenclud@193
|
138 |
///
|
StephaneLenclud@193
|
139 |
/// </summary>
|
StephaneLenclud@193
|
140 |
/// <returns></returns>
|
StephaneLenclud@193
|
141 |
public bool IsClientReady()
|
StephaneLenclud@193
|
142 |
{
|
StephaneLenclud@193
|
143 |
return (iClient != null && iClient.IsReady());
|
StephaneLenclud@193
|
144 |
}
|
StephaneLenclud@193
|
145 |
|
StephaneLenclud@193
|
146 |
/// <summary>
|
StephaneLenclud@193
|
147 |
///
|
StephaneLenclud@193
|
148 |
/// </summary>
|
StephaneLenclud@193
|
149 |
public void SetupDisplayClient()
|
StephaneLenclud@193
|
150 |
{
|
StephaneLenclud@193
|
151 |
//Setup our layout
|
StephaneLenclud@193
|
152 |
|
StephaneLenclud@193
|
153 |
//Set one column one line layout
|
StephaneLenclud@193
|
154 |
TableLayout layout = new TableLayout(1, 1);
|
StephaneLenclud@193
|
155 |
iClient.SetLayout(layout);
|
StephaneLenclud@193
|
156 |
|
StephaneLenclud@193
|
157 |
//Setup our fields
|
StephaneLenclud@193
|
158 |
iAlignment = ContentAlignment.MiddleCenter;
|
StephaneLenclud@193
|
159 |
iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
|
StephaneLenclud@193
|
160 |
|
StephaneLenclud@193
|
161 |
//Set our fields
|
StephaneLenclud@193
|
162 |
iClient.CreateFields(new DataField[]
|
StephaneLenclud@193
|
163 |
{
|
StephaneLenclud@193
|
164 |
iTextField
|
StephaneLenclud@193
|
165 |
});
|
StephaneLenclud@193
|
166 |
}
|
StephaneLenclud@193
|
167 |
|
StephaneLenclud@193
|
168 |
public void OnCloseOrder()
|
StephaneLenclud@193
|
169 |
{
|
StephaneLenclud@193
|
170 |
CloseThreadSafe();
|
StephaneLenclud@193
|
171 |
}
|
StephaneLenclud@193
|
172 |
|
StephaneLenclud@193
|
173 |
/// <summary>
|
StephaneLenclud@193
|
174 |
///
|
StephaneLenclud@193
|
175 |
/// </summary>
|
StephaneLenclud@193
|
176 |
public void CloseThreadSafe()
|
StephaneLenclud@193
|
177 |
{
|
StephaneLenclud@193
|
178 |
if (this.InvokeRequired)
|
StephaneLenclud@193
|
179 |
{
|
StephaneLenclud@193
|
180 |
//Not in the proper thread, invoke ourselves
|
StephaneLenclud@193
|
181 |
CloseDelegate d = new CloseDelegate(CloseThreadSafe);
|
StephaneLenclud@193
|
182 |
this.Invoke(d, new object[] { });
|
StephaneLenclud@193
|
183 |
}
|
StephaneLenclud@193
|
184 |
else
|
StephaneLenclud@193
|
185 |
{
|
StephaneLenclud@193
|
186 |
//We are in the proper thread
|
StephaneLenclud@193
|
187 |
Close();
|
StephaneLenclud@193
|
188 |
}
|
StephaneLenclud@193
|
189 |
}
|
StephaneLenclud@193
|
190 |
|
StephaneLenclud@193
|
191 |
/// <summary>
|
StephaneLenclud@193
|
192 |
///
|
StephaneLenclud@193
|
193 |
/// </summary>
|
StephaneLenclud@193
|
194 |
public void CloseConnectionThreadSafe()
|
StephaneLenclud@193
|
195 |
{
|
StephaneLenclud@193
|
196 |
if (this.InvokeRequired)
|
StephaneLenclud@193
|
197 |
{
|
StephaneLenclud@193
|
198 |
//Not in the proper thread, invoke ourselves
|
StephaneLenclud@193
|
199 |
CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
|
StephaneLenclud@193
|
200 |
this.Invoke(d, new object[] { });
|
StephaneLenclud@193
|
201 |
}
|
StephaneLenclud@193
|
202 |
else
|
StephaneLenclud@193
|
203 |
{
|
StephaneLenclud@193
|
204 |
//We are in the proper thread
|
StephaneLenclud@193
|
205 |
if (IsClientReady())
|
StephaneLenclud@193
|
206 |
{
|
StephaneLenclud@193
|
207 |
string sessionId = iClient.SessionId;
|
StephaneLenclud@193
|
208 |
Trace.TraceInformation("Closing client: " + sessionId);
|
StephaneLenclud@193
|
209 |
iClient.Close();
|
StephaneLenclud@193
|
210 |
Trace.TraceInformation("Closed client: " + sessionId);
|
StephaneLenclud@193
|
211 |
}
|
StephaneLenclud@193
|
212 |
|
StephaneLenclud@193
|
213 |
iClient = null;
|
StephaneLenclud@193
|
214 |
}
|
StephaneLenclud@193
|
215 |
}
|
StephaneLenclud@193
|
216 |
|
StephaneLenclud@193
|
217 |
/// <summary>
|
StephaneLenclud@193
|
218 |
///
|
StephaneLenclud@193
|
219 |
/// </summary>
|
StephaneLenclud@193
|
220 |
/// <param name="sender"></param>
|
StephaneLenclud@193
|
221 |
/// <param name="e"></param>
|
StephaneLenclud@193
|
222 |
private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
|
StephaneLenclud@193
|
223 |
{
|
StephaneLenclud@193
|
224 |
CloseConnectionThreadSafe();
|
StephaneLenclud@193
|
225 |
}
|
StephaneLenclud@193
|
226 |
|
StephaneLenclud@193
|
227 |
|
StephaneLenclud@193
|
228 |
/// <summary>
|
StephaneLenclud@193
|
229 |
///
|
StephaneLenclud@193
|
230 |
/// </summary>
|
StephaneLenclud@193
|
231 |
/// <param name="sender"></param>
|
StephaneLenclud@193
|
232 |
/// <param name="e"></param>
|
StephaneLenclud@193
|
233 |
private void iTimer_Tick(object sender, EventArgs e)
|
StephaneLenclud@193
|
234 |
{
|
StephaneLenclud@193
|
235 |
//Timer
|
StephaneLenclud@193
|
236 |
iTimer.Interval = IntervalToNextMinute();
|
StephaneLenclud@193
|
237 |
iTimer.Start();
|
StephaneLenclud@193
|
238 |
|
Stephane@197
|
239 |
UpdateDisplay();
|
Stephane@197
|
240 |
|
Stephane@197
|
241 |
}
|
Stephane@197
|
242 |
|
Stephane@197
|
243 |
/// <summary>
|
Stephane@197
|
244 |
///
|
Stephane@197
|
245 |
/// </summary>
|
Stephane@197
|
246 |
private void UpdateDisplay()
|
Stephane@197
|
247 |
{
|
StephaneLenclud@193
|
248 |
//
|
StephaneLenclud@193
|
249 |
if (String.IsNullOrEmpty(iTextField.Text))
|
StephaneLenclud@193
|
250 |
{
|
StephaneLenclud@193
|
251 |
//Time to show our time
|
StephaneLenclud@193
|
252 |
iTextField.Text = DateTime.Now.ToString("t");
|
StephaneLenclud@193
|
253 |
}
|
StephaneLenclud@193
|
254 |
else
|
StephaneLenclud@193
|
255 |
{
|
StephaneLenclud@194
|
256 |
//Do some screen saving
|
StephaneLenclud@193
|
257 |
iTextField.Text = "";
|
StephaneLenclud@193
|
258 |
}
|
StephaneLenclud@193
|
259 |
|
StephaneLenclud@193
|
260 |
// Update our field
|
StephaneLenclud@193
|
261 |
iClient.SetField(iTextField);
|
StephaneLenclud@194
|
262 |
|
StephaneLenclud@194
|
263 |
//Now make sure we save our screen from any running system monitor
|
Stephane@197
|
264 |
//We don't go into screen saving mode if our monitor is still on
|
Stephane@197
|
265 |
if (String.IsNullOrEmpty(iTextField.Text) && !MonitorPowerOn)
|
StephaneLenclud@194
|
266 |
{
|
StephaneLenclud@194
|
267 |
//If text it empty it means we need to cool down our display even if system monitor is running
|
StephaneLenclud@194
|
268 |
iClient.SetPriority(Priorities.SystemMonitor + 1);
|
StephaneLenclud@194
|
269 |
}
|
StephaneLenclud@194
|
270 |
else
|
StephaneLenclud@194
|
271 |
{
|
StephaneLenclud@194
|
272 |
//Switch back to background then, leaving system monitor if any doing its magic
|
StephaneLenclud@194
|
273 |
iClient.SetPriority(Priorities.Background);
|
StephaneLenclud@194
|
274 |
}
|
StephaneLenclud@193
|
275 |
}
|
StephaneLenclud@193
|
276 |
|
Stephane@197
|
277 |
/// <summary>
|
Stephane@197
|
278 |
///
|
Stephane@197
|
279 |
/// </summary>
|
Stephane@197
|
280 |
/// <param name="sender"></param>
|
Stephane@197
|
281 |
/// <param name="e"></param>
|
StephaneLenclud@193
|
282 |
private void FormClientIdle_Shown(object sender, EventArgs e)
|
StephaneLenclud@193
|
283 |
{
|
StephaneLenclud@193
|
284 |
//Visible = false;
|
StephaneLenclud@193
|
285 |
}
|
StephaneLenclud@192
|
286 |
}
|
StephaneLenclud@193
|
287 |
|
StephaneLenclud@192
|
288 |
}
|