Skip to content

Commit

Permalink
Fixed build error due to renaming DisplayConfig.cs function. Cleaned …
Browse files Browse the repository at this point in the history
…up monitor sleep implementation.
  • Loading branch information
Fahim-zzz committed Oct 22, 2024
1 parent a4f6da8 commit aa84313
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DAIRemote/DAIRemoteApplicationUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class DAIRemoteApplicationUI : Form
public DAIRemoteApplicationUI()
{
UDPServerHost udpServer = new UDPServerHost();
Thread udpThread = new Thread(() => udpServer.hostUDPServer());
Thread udpThread = new Thread(() => udpServer.HostUDPServer());
udpThread.IsBackground = true;
udpThread.Start();

Expand Down
28 changes: 11 additions & 17 deletions DAIRemote/SystemTray.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using DisplayProfileManager;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DAIRemote
{
Expand All @@ -13,7 +9,6 @@ public class TrayIconManager
private Form form;
private FileSystemWatcher profileDirWatcher;
private string profilesFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DAIRemote/DisplayProfiles");
private DisplayConfig displayConfig;

private Image aboutIcon;
private Image DAIRemoteLogo;
Expand All @@ -27,7 +22,6 @@ public class TrayIconManager
public TrayIconManager(Form form)
{
this.form = form;
displayConfig = new DisplayConfig();

aboutIcon = Image.FromFile("Resources/About.ico");
DAIRemoteLogo = Image.FromFile("Resources/DAIRemoteLogo.ico");
Expand All @@ -40,7 +34,7 @@ public TrayIconManager(Form form)

if (!Directory.Exists(profilesFolderPath))
{
Directory.CreateDirectory(profilesFolderPath);
Directory.CreateDirectory(profilesFolderPath);
}

InitializeTrayIcon();
Expand Down Expand Up @@ -71,7 +65,7 @@ private ContextMenuStrip CreateTrayMenu()
{
ContextMenuStrip menu = new ContextMenuStrip
{
ForeColor = System.Drawing.Color.Black,
ForeColor = Color.Black,
ShowImageMargin = true,
Font = new Font("Segoe UI Variable", 9, FontStyle.Regular),
AutoSize = true,
Expand All @@ -84,7 +78,6 @@ private ContextMenuStrip CreateTrayMenu()

private void OnProfilesChanged(object sender, FileSystemEventArgs e)
{

if (form.InvokeRequired)
{
form.BeginInvoke((MethodInvoker)delegate
Expand All @@ -109,14 +102,14 @@ private void PopulateTrayMenu(ContextMenuStrip menu)
Enabled = false,
};

ToolStripMenuItem deleteProfilesLabel = new ToolStripMenuItem("Select Profile To Delete")
ToolStripMenuItem deleteProfilesLabel = new ToolStripMenuItem("Select Profile to Delete")
{
Font = new Font("Segoe UI Variable", 9, FontStyle.Regular),
ForeColor = Color.Gray,
Enabled = false,
};

ToolStripMenuItem saveProfilesLabel = new ToolStripMenuItem("Select Profile To Save")
ToolStripMenuItem saveProfilesLabel = new ToolStripMenuItem("Overwrite profile")
{
Font = new Font("Segoe UI Variable", 9, FontStyle.Regular),
ForeColor = Color.Gray,
Expand All @@ -141,7 +134,7 @@ private void PopulateTrayMenu(ContextMenuStrip menu)
string fileName = Path.GetFileNameWithoutExtension(jsonProfile);
var jsonMenuItem = new ToolStripMenuItem(fileName, monitorIcon, (sender, e) =>
{
DisplayProfileManager.DisplayConfig.SetDisplaySettings(jsonProfile);
DisplayConfig.SetDisplaySettings(jsonProfile);
});

menu.Items.Insert(2, jsonMenuItem);
Expand All @@ -163,9 +156,10 @@ private void PopulateTrayMenu(ContextMenuStrip menu)
menu.Items.Add(new ToolStripSeparator());
deleteProfileMenuItem.DropDownItems.Insert(0, deleteProfilesLabel);
deleteProfileMenuItem.DropDownItems.Insert(1, new ToolStripSeparator());
saveProfileMenuItem.DropDownItems.Insert(0, saveProfilesLabel);
saveProfileMenuItem.DropDownItems.Insert(0, addNewProfile);
saveProfileMenuItem.DropDownItems.Insert(1, new ToolStripSeparator());
saveProfileMenuItem.DropDownItems.Insert(2, addNewProfile);
saveProfileMenuItem.DropDownItems.Insert(2, saveProfilesLabel);
saveProfileMenuItem.DropDownItems.Insert(3, new ToolStripSeparator());
menu.Items.Add(saveProfileMenuItem);
menu.Items.Add(deleteProfileMenuItem);

Expand Down Expand Up @@ -253,17 +247,17 @@ private void SaveNewProfile(string profilesFolderPath)

private void SaveProfile(string profilePath)
{
DisplayConfig.SaveDisplaySettings(profilePath);
DisplayConfig.SaveDisplaySettings(profilePath);
}

private void DeleteProfile(string profilePath)
{
File.Delete(profilePath);
File.Delete(profilePath);
}

private void TurnOffMonitors(object? sender, EventArgs e)
{
displayConfig.TurnOffMonitors();
DisplayConfig.DisplayToggleSleep(true);
}

private void OnAboutClick(object? sender, EventArgs e)
Expand Down
33 changes: 17 additions & 16 deletions DisplayProfileManager/DisplayConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,24 +1297,25 @@ public static bool SaveDisplaySettings(string fileName)
[DllImport("user32.dll")]
private static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MONITORPOWER = 0xF170;
private const int MONITOR_OFF = 2;
private const int MONITOR_ON = -1;
private const int HWND_BROADCAST = 0xFFFF;

public void TurnOffMonitors()
public static void DisplayToggleSleep(bool sleep = true)
{
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
}

public void TurnOnMonitors()
{
Task.Run(() =>
const int WM_SYSCOMMAND = 0x0112;
const int SC_MONITORPOWER = 0xF170;
const int HWND_BROADCAST = 0xFFFF;
if (sleep)
{
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
System.Threading.Thread.Sleep(100);
});
// Go into sleep mode (2)
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}
else
{
// Wakeup from sleep mode (-1)
Task.Run(() =>
{
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
Thread.Sleep(100);
});
}
}

static void Main(string[] args)
Expand Down

0 comments on commit aa84313

Please sign in to comment.