StephaneLenclud@193: //
StephaneLenclud@193: // Copyright (C) 2014-2016 Stéphane Lenclud.
StephaneLenclud@193: //
StephaneLenclud@193: // This file is part of SharpDisplayManager.
StephaneLenclud@193: //
StephaneLenclud@193: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@193: // it under the terms of the GNU General Public License as published by
StephaneLenclud@193: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@193: // (at your option) any later version.
StephaneLenclud@193: //
StephaneLenclud@193: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@193: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@193: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
StephaneLenclud@193: // GNU General Public License for more details.
StephaneLenclud@193: //
StephaneLenclud@193: // You should have received a copy of the GNU General Public License
StephaneLenclud@193: // along with SharpDisplayManager. If not, see .
StephaneLenclud@193: //
StephaneLenclud@193:
StephaneLenclud@193: using System;
StephaneLenclud@192: using System.Collections.Generic;
StephaneLenclud@192: using System.ComponentModel;
StephaneLenclud@192: using System.Data;
StephaneLenclud@192: using System.Drawing;
StephaneLenclud@192: using System.Linq;
StephaneLenclud@192: using System.Text;
StephaneLenclud@192: using System.Threading.Tasks;
StephaneLenclud@192: using System.Windows.Forms;
StephaneLenclud@193: using System.Diagnostics;
StephaneLenclud@193: using SharpLib.Display;
StephaneLenclud@193:
StephaneLenclud@192:
StephaneLenclud@225: namespace SharpDisplayClientIdle
StephaneLenclud@192: {
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: /// Sharp Display Client designed to act as an idle client.
StephaneLenclud@193: /// It should take care of screen saving and other such concerns.
StephaneLenclud@193: ///
StephaneLenclud@192: public partial class FormClientIdle : Form
StephaneLenclud@192: {
StephaneLenclud@192: public StartParams Params { get; set; }
StephaneLenclud@192:
StephaneLenclud@193: Client iClient;
StephaneLenclud@193: ContentAlignment iAlignment;
StephaneLenclud@193: TextField iTextField;
Stephane@197: //Used to determine if screen saver need to kick in
Stephane@197: private PowerManager.SettingNotifier iPowerSettingNotifier;
Stephane@197: private bool MonitorPowerOn;
StephaneLenclud@193:
StephaneLenclud@193: public delegate void CloseDelegate();
StephaneLenclud@193: public delegate void CloseConnectionDelegate();
StephaneLenclud@193:
StephaneLenclud@193:
StephaneLenclud@192: public FormClientIdle()
StephaneLenclud@192: {
StephaneLenclud@192: InitializeComponent();
StephaneLenclud@192: }
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: private void FormClientIdle_Load(object sender, EventArgs e)
StephaneLenclud@193: {
StephaneLenclud@196: //Prevents showing in the Open Task view (Windows Key + Tab)
StephaneLenclud@196: Visible = false;
StephaneLenclud@196:
StephaneLenclud@193: //Display client
StephaneLenclud@193: iClient = new Client();
StephaneLenclud@193: iClient.CloseOrderEvent += OnCloseOrder;
StephaneLenclud@193: iClient.Open();
StephaneLenclud@193: iClient.SetName("Idle");
StephaneLenclud@194: iClient.SetPriority(Priorities.Background);
StephaneLenclud@193: SetupDisplayClient();
StephaneLenclud@193:
StephaneLenclud@193: //Timer
StephaneLenclud@193: iTimer.Interval = IntervalToNextMinute();
StephaneLenclud@193: iTimer.Start();
Stephane@197:
Stephane@197: //Create our power setting notifier and register the event we are interested in
Stephane@197: iPowerSettingNotifier = new PowerManager.SettingNotifier(Handle);
Stephane@197: iPowerSettingNotifier.OnMonitorPowerOn += OnMonitorPowerOn;
Stephane@197: iPowerSettingNotifier.OnMonitorPowerOff += OnMonitorPowerOff;
Stephane@197: MonitorPowerOn = true;
Stephane@197:
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: ///
Stephane@197: /// Broadcast messages to subscribers.
Stephane@197: ///
Stephane@197: ///
Stephane@197: protected override void WndProc(ref Message aMessage)
Stephane@197: {
Stephane@197: //Hook in our power manager
Stephane@197: if (iPowerSettingNotifier != null)
Stephane@197: {
Stephane@197: iPowerSettingNotifier.WndProc(ref aMessage);
Stephane@197: }
Stephane@197:
Stephane@197: base.WndProc(ref aMessage);
Stephane@197: }
Stephane@197:
Stephane@197:
Stephane@197:
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
Stephane@197: private void OnMonitorPowerOn()
Stephane@197: {
Stephane@197: MonitorPowerOn = true;
Stephane@197: UpdateDisplay();
Stephane@197: }
Stephane@197:
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
Stephane@197: private void OnMonitorPowerOff()
Stephane@197: {
Stephane@197: MonitorPowerOn = false;
Stephane@197: UpdateDisplay();
Stephane@197: }
Stephane@197:
Stephane@197:
Stephane@197: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: public static int IntervalToNextMinute()
StephaneLenclud@193: {
StephaneLenclud@193: DateTime now = DateTime.Now;
StephaneLenclud@193: return 60000 - now.Millisecond;
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: public bool IsClientReady()
StephaneLenclud@193: {
StephaneLenclud@193: return (iClient != null && iClient.IsReady());
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: public void SetupDisplayClient()
StephaneLenclud@193: {
StephaneLenclud@193: //Setup our layout
StephaneLenclud@193:
StephaneLenclud@193: //Set one column one line layout
StephaneLenclud@193: TableLayout layout = new TableLayout(1, 1);
StephaneLenclud@193: iClient.SetLayout(layout);
StephaneLenclud@193:
StephaneLenclud@193: //Setup our fields
StephaneLenclud@193: iAlignment = ContentAlignment.MiddleCenter;
StephaneLenclud@193: iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
StephaneLenclud@193:
StephaneLenclud@193: //Set our fields
StephaneLenclud@193: iClient.CreateFields(new DataField[]
StephaneLenclud@193: {
StephaneLenclud@193: iTextField
StephaneLenclud@193: });
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: public void OnCloseOrder()
StephaneLenclud@193: {
StephaneLenclud@193: CloseThreadSafe();
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: public void CloseThreadSafe()
StephaneLenclud@193: {
StephaneLenclud@193: if (this.InvokeRequired)
StephaneLenclud@193: {
StephaneLenclud@193: //Not in the proper thread, invoke ourselves
StephaneLenclud@193: CloseDelegate d = new CloseDelegate(CloseThreadSafe);
StephaneLenclud@193: this.Invoke(d, new object[] { });
StephaneLenclud@193: }
StephaneLenclud@193: else
StephaneLenclud@193: {
StephaneLenclud@193: //We are in the proper thread
StephaneLenclud@193: Close();
StephaneLenclud@193: }
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: public void CloseConnectionThreadSafe()
StephaneLenclud@193: {
StephaneLenclud@193: if (this.InvokeRequired)
StephaneLenclud@193: {
StephaneLenclud@193: //Not in the proper thread, invoke ourselves
StephaneLenclud@193: CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
StephaneLenclud@193: this.Invoke(d, new object[] { });
StephaneLenclud@193: }
StephaneLenclud@193: else
StephaneLenclud@193: {
StephaneLenclud@193: //We are in the proper thread
StephaneLenclud@193: if (IsClientReady())
StephaneLenclud@193: {
StephaneLenclud@193: string sessionId = iClient.SessionId;
StephaneLenclud@193: Trace.TraceInformation("Closing client: " + sessionId);
StephaneLenclud@193: iClient.Close();
StephaneLenclud@193: Trace.TraceInformation("Closed client: " + sessionId);
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: iClient = null;
StephaneLenclud@193: }
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
StephaneLenclud@193: {
StephaneLenclud@193: CloseConnectionThreadSafe();
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193:
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: ///
StephaneLenclud@193: private void iTimer_Tick(object sender, EventArgs e)
StephaneLenclud@193: {
StephaneLenclud@193: //Timer
StephaneLenclud@193: iTimer.Interval = IntervalToNextMinute();
StephaneLenclud@193: iTimer.Start();
StephaneLenclud@193:
Stephane@197: UpdateDisplay();
Stephane@197:
Stephane@197: }
Stephane@197:
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
Stephane@197: private void UpdateDisplay()
Stephane@197: {
StephaneLenclud@193: //
StephaneLenclud@193: if (String.IsNullOrEmpty(iTextField.Text))
StephaneLenclud@193: {
StephaneLenclud@193: //Time to show our time
StephaneLenclud@193: iTextField.Text = DateTime.Now.ToString("t");
StephaneLenclud@193: }
StephaneLenclud@193: else
StephaneLenclud@193: {
StephaneLenclud@194: //Do some screen saving
StephaneLenclud@193: iTextField.Text = "";
StephaneLenclud@193: }
StephaneLenclud@193:
StephaneLenclud@193: // Update our field
StephaneLenclud@193: iClient.SetField(iTextField);
StephaneLenclud@194:
StephaneLenclud@194: //Now make sure we save our screen from any running system monitor
Stephane@197: //We don't go into screen saving mode if our monitor is still on
Stephane@197: if (String.IsNullOrEmpty(iTextField.Text) && !MonitorPowerOn)
StephaneLenclud@194: {
StephaneLenclud@194: //If text it empty it means we need to cool down our display even if system monitor is running
StephaneLenclud@194: iClient.SetPriority(Priorities.SystemMonitor + 1);
StephaneLenclud@194: }
StephaneLenclud@194: else
StephaneLenclud@194: {
StephaneLenclud@194: //Switch back to background then, leaving system monitor if any doing its magic
StephaneLenclud@194: iClient.SetPriority(Priorities.Background);
StephaneLenclud@194: }
StephaneLenclud@193: }
StephaneLenclud@193:
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
Stephane@197: ///
StephaneLenclud@193: private void FormClientIdle_Shown(object sender, EventArgs e)
StephaneLenclud@193: {
StephaneLenclud@193: //Visible = false;
StephaneLenclud@193: }
StephaneLenclud@192: }
StephaneLenclud@193:
StephaneLenclud@192: }