Skip to content

Commit

Permalink
Ability to control 12/24hr display and what appears on each screen
Browse files Browse the repository at this point in the history
  • Loading branch information
phaselden committed Jan 19, 2021
1 parent 9488ae7 commit bc7f8c2
Show file tree
Hide file tree
Showing 12 changed files with 713 additions and 115 deletions.
Binary file added Untitled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/FlipIt/DisplayType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ScreenSaver
{
public enum DisplayType
{
None = 0,
CurrentTime = 1,
WorldTime = 6
}
}
4 changes: 4 additions & 0 deletions src/FlipIt/FlipIt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="City.cs" />
<Compile Include="DisplayType.cs" />
<Compile Include="FlipItSettings.cs" />
<Compile Include="IniFile.cs" />
<Compile Include="Int32Extensions.cs" />
<Compile Include="RoundedRectangle.cs" />
<Compile Include="ScreenSetting.cs" />
<Compile Include="SettingsForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
18 changes: 18 additions & 0 deletions src/FlipIt/FlipItSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;

namespace ScreenSaver
{
public class FlipItSettings
{
public bool Display24HrTime { get; set; }
public int Scale { get; set; }

public List<ScreenSetting> ScreenSettings { get; set; } = new List<ScreenSetting>();

public ScreenSetting GetScreen(string screenDeviceName)
{
return ScreenSettings.SingleOrDefault(s => s.DeviceName == screenDeviceName);
}
}
}
98 changes: 98 additions & 0 deletions src/FlipIt/IniFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Initially from https://stackoverflow.com/a/14906422/1899

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace ScreenSaver
{
public class IniFile
{
private const int BufferSize = 1024;
private readonly string _path;

[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string section, string key, string value, string filePath);

[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string section, string key, string @default, StringBuilder retVal, int size, string filePath);

public IniFile(string iniPath)
{
_path = iniPath;
}

public string ReadString(string section, string key)
{
var retVal = new StringBuilder(BufferSize);
GetPrivateProfileString(section, key, "", retVal, BufferSize, _path);
return retVal.ToString();
}

public void WriteBool(string section, string key, bool value)
{
WriteString(section, key, value ? "1": "0");
}

public bool ReadBool(string section, string key, bool defaultValue)
{
var val = ReadString(section, key);
if (val == "")
return defaultValue;
Debug.Assert(val == "1" || val == "0");
return val == "1";
}

public int ReadInt(string section, string key, int defaultValue)
{
var val = ReadString(section, key);
if (val == "")
return defaultValue;
return Int32.Parse(val);
}

public void WriteInt(string section, string key, int value)
{
WriteString(section, key, value.ToString());
}

public string[] ReadSections()
{
var keyNames = ReadString(null, null);
return keyNames.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
}

public bool SectionExists(string section)
{
var keyNames = ReadSection(section);
return keyNames.Length > 0;
}

public string[] ReadSection(string section)
{
var keyNames = ReadString(section, null);
return keyNames.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries);
}

public void WriteString(string section, string key, string value)
{
var res = WritePrivateProfileString(section, key, value, _path);
}

public void DeleteKey(string section, string key)
{
WriteString(section, key, null);
}

public void DeleteSection(string section = null)
{
WriteString(section, null, null);
}

public bool KeyExists(string key, string section = null)
{
return ReadString(section, key).Length > 0;
}
}
}
29 changes: 19 additions & 10 deletions src/FlipIt/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public partial class MainForm : Form
private const int splitWidth = 4;
private const int cityBoxSplitWidth = 2;
private const int fontScaleFactor = 3;

private readonly bool isPrimaryScreen;

private readonly bool _display24HourTime;
private readonly ScreenSetting _screenSetting;
private Point mouseLocation;
private readonly bool previewMode = false;
private readonly bool showSeconds = false;
Expand Down Expand Up @@ -89,9 +90,10 @@ public MainForm()
InitializeComponent();
}

public MainForm(Rectangle bounds, bool isPrimaryScreen)
public MainForm(Rectangle bounds, bool display24HourTime, ScreenSetting screenSetting)
{
this.isPrimaryScreen = isPrimaryScreen;
_display24HourTime = display24HourTime;
_screenSetting = screenSetting;
InitializeComponent();
Bounds = bounds;
fontSize = bounds.Height / fontScaleFactor;
Expand Down Expand Up @@ -137,7 +139,7 @@ private void MainForm_Shown(object sender, EventArgs e)
//SystemTime.NowForTesting = new DateTime(2020, 6, 7, 1, 23, 45);
//SystemTime.NowForTesting = new DateTime(2020, 6, 7, 23, 45, 57);
}
}
}

private void moveTimer_Tick(object sender, EventArgs e)
{
Expand Down Expand Up @@ -170,11 +172,11 @@ private void DrawIt()
Gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
Gfx.SmoothingMode = SmoothingMode.HighQuality;

if (isPrimaryScreen || previewMode)
{
if (previewMode || _screenSetting.DisplayType == DisplayType.CurrentTime)
{
DrawCurrentTime();
}
else
else if (_screenSetting.DisplayType == DisplayType.WorldTime)
{
DrawCities();
}
Expand All @@ -193,8 +195,15 @@ private void DrawCurrentTime()
var x = (Width - width)/2;
var y = (Height - height)/2;

var pm = SystemTime.Now.Hour >= 12;
DrawIt(x, y, height, SystemTime.Now.ToString("%h"), pm ? null : "AM", pm ? "PM" : null); // The % avoids a FormatException https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#UsingSingleSpecifiers
if (!_display24HourTime)
{
var pm = SystemTime.Now.Hour >= 12;
DrawIt(x, y, height, SystemTime.Now.ToString("%h"), pm ? null : "AM", pm ? "PM" : null); // The % avoids a FormatException https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#UsingSingleSpecifiers
}
else
{
DrawIt(x, y, height, SystemTime.Now.ToString("HH"));
}

x += height + (height/20);
DrawIt(x, y, height, SystemTime.Now.ToString("mm"));
Expand Down
73 changes: 62 additions & 11 deletions src/FlipIt/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Originally based on project by Frank McCown in 2010 */

using System;
using System.IO;
using System.Windows.Forms;

namespace ScreenSaver
Expand All @@ -16,6 +17,8 @@ static void Main(string[] args)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var settings = LoadSettings();

if (args.Length > 0)
{
string firstArgument = args[0].ToLower().Trim();
Expand All @@ -31,9 +34,9 @@ static void Main(string[] args)
else if (args.Length > 1)
secondArgument = args[1];

if (firstArgument == "/c") // Configuration mode
if (firstArgument == "/c") // Configuration mode
{
Application.Run(new SettingsForm());
Application.Run(new SettingsForm(settings));
}
else if (firstArgument == "/p") // Preview mode
{
Expand All @@ -49,7 +52,7 @@ static void Main(string[] args)
}
else if (firstArgument == "/s") // Full-screen mode
{
ShowScreenSaver();
ShowScreenSaver(settings);
Application.Run();
}
else // Undefined argument
Expand All @@ -61,20 +64,68 @@ static void Main(string[] args)
}
else // No arguments - treat like /c
{
Application.Run(new SettingsForm());
Application.Run(new SettingsForm(settings));
}
}

/// <summary>
/// Display the form on each of the computer's monitors.
/// </summary>
static void ShowScreenSaver()
{
foreach (Screen screen in Screen.AllScreens)
{
MainForm screensaver = new MainForm(screen.Bounds, screen.Primary);
screensaver.Show();
static void ShowScreenSaver(FlipItSettings settings)
{
foreach (var screen in Screen.AllScreens)
{
var cleanDeviceName = CleanDeviceName(screen.DeviceName);
var screenSettings = settings.GetScreen(cleanDeviceName);
var form = new MainForm(screen.Bounds, settings.Display24HrTime, screenSettings);
form.Show();
}
}
}

private static FlipItSettings LoadSettings()
{
var allScreens = Screen.AllScreens;
IniFile iniFile = null;

var settings = new FlipItSettings();

var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "FlipIt");
var iniFilePath = Path.Combine(settingsFolder, "Settings.ini");
if (File.Exists(iniFilePath))
{
iniFile = new IniFile(iniFilePath);
settings.Display24HrTime = iniFile.ReadBool("General", "Display24Hr", false);
}
else
{
settings.Display24HrTime = false;
}

var screenNum = 0;
foreach (var screen in allScreens)
{
screenNum++;
var cleanDeviceName = CleanDeviceName(screen.DeviceName);
var sectionName = $"Screen {cleanDeviceName}";

var screenSetting = new ScreenSetting(screenNum, cleanDeviceName, screen.Bounds.Width, screen.Bounds.Height);
if (iniFile != null && iniFile.SectionExists(sectionName))
{
screenSetting.DisplayType = (DisplayType) iniFile.ReadInt(sectionName, "DisplayType", (int) DisplayType.CurrentTime);
}
else
{
screenSetting.DisplayType = DisplayType.CurrentTime;
}
settings.ScreenSettings.Add(screenSetting);
}

return settings;
}

private static string CleanDeviceName(string deviceName)
{
return deviceName.TrimStart(new[] {'\\', '.'});
}
}
}
23 changes: 23 additions & 0 deletions src/FlipIt/ScreenSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace ScreenSaver
{
public class ScreenSetting
{
public ScreenSetting(int screenNumber, string deviceName, int width, int height)
{
ScreenNumber = screenNumber;
DeviceName = deviceName;
Width = width;
Height = height;
}

public int ScreenNumber { get; }
public string DeviceName { get; }
public int Width { get; }
public int Height { get; }

public DisplayType DisplayType { get; set; }

public string ShortDescription => $"Screen {ScreenNumber}";
public string Description => $"{ShortDescription} - {Width} x {Height}";
}
}
Loading

0 comments on commit bc7f8c2

Please sign in to comment.