2022-11-24 22:37:24 +01:00
|
|
|
|
using CommonHelpers;
|
|
|
|
|
|
using ExternalHelpers;
|
|
|
|
|
|
using SteamController.Profiles;
|
|
|
|
|
|
using System.ComponentModel;
|
2022-11-25 07:48:01 +01:00
|
|
|
|
using System.Diagnostics;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SteamController
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class Controller : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
public const String Title = "Steam Controller";
|
|
|
|
|
|
public readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
Container components = new Container();
|
|
|
|
|
|
NotifyIcon notifyIcon;
|
|
|
|
|
|
StartupManager startupManager = new StartupManager(Title);
|
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
|
Context context = new Context()
|
|
|
|
|
|
{
|
|
|
|
|
|
Profiles = {
|
|
|
|
|
|
new Profiles.SteamShortcutsProfile(),
|
|
|
|
|
|
new Profiles.DesktopProfile(),
|
|
|
|
|
|
new Profiles.ProcessProfile(),
|
|
|
|
|
|
new Profiles.SteamDetectProfile(),
|
|
|
|
|
|
new Profiles.X360Profile(),
|
|
|
|
|
|
new Profiles.DebugProfile()
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
|
Thread? contextThread;
|
|
|
|
|
|
bool running = true;
|
2022-11-25 07:48:01 +01:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
int updatesReceived = 0;
|
|
|
|
|
|
int lastUpdatesReceived = 0;
|
|
|
|
|
|
TimeSpan lastUpdatesReset;
|
|
|
|
|
|
readonly TimeSpan updateResetInterval = TimeSpan.FromSeconds(1);
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
[DllImport("sas.dll")]
|
|
|
|
|
|
static extern void SendSAS(bool asUser);
|
|
|
|
|
|
|
|
|
|
|
|
public Controller()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instance.RunOnce(TitleWithVersion, "Global\\SteamController");
|
|
|
|
|
|
SendSAS(true);
|
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
|
var contextMenu = new ContextMenuStrip(components);
|
|
|
|
|
|
|
|
|
|
|
|
var enabledItem = new ToolStripMenuItem("&Enabled");
|
|
|
|
|
|
enabledItem.Checked = context.RequestEnable;
|
|
|
|
|
|
enabledItem.Click += delegate { enabledItem.Checked = context.RequestEnable = !context.RequestEnable; };
|
|
|
|
|
|
contextMenu.Items.Add(enabledItem);
|
|
|
|
|
|
|
|
|
|
|
|
var desktopModeItem = new ToolStripMenuItem("&Desktop Mode");
|
|
|
|
|
|
desktopModeItem.Checked = context.RequestDesktopMode;
|
|
|
|
|
|
desktopModeItem.Click += delegate { desktopModeItem.Checked = context.RequestDesktopMode = !context.RequestDesktopMode; };
|
|
|
|
|
|
contextMenu.Items.Add(desktopModeItem);
|
|
|
|
|
|
|
|
|
|
|
|
var steamDetectionItem = new ToolStripMenuItem("Auto-disable on &Steam");
|
|
|
|
|
|
steamDetectionItem.Checked = Settings.Default.EnableSteamDetection;
|
|
|
|
|
|
steamDetectionItem.Click += delegate
|
2022-11-24 22:37:24 +01:00
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
steamDetectionItem.Checked = Settings.Default.EnableSteamDetection = !Settings.Default.EnableSteamDetection;
|
|
|
|
|
|
Settings.Default.Save();
|
2022-11-24 22:37:24 +01:00
|
|
|
|
};
|
2022-11-25 07:48:01 +01:00
|
|
|
|
contextMenu.Items.Add(steamDetectionItem);
|
|
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
if (startupManager.IsAvailable)
|
|
|
|
|
|
{
|
|
|
|
|
|
var startupItem = new ToolStripMenuItem("Run On Startup");
|
|
|
|
|
|
startupItem.Checked = startupManager.Startup;
|
2022-11-25 07:48:01 +01:00
|
|
|
|
startupItem.Click += delegate { startupItem.Checked = startupManager.Startup = !startupManager.Startup; };
|
2022-11-24 22:37:24 +01:00
|
|
|
|
contextMenu.Items.Add(startupItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var helpItem = contextMenu.Items.Add("&Help");
|
2022-11-25 07:48:01 +01:00
|
|
|
|
helpItem.Click += delegate { Process.Start("explorer.exe", "http://github.com/ayufan-research/steam-deck-tools"); };
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
|
|
|
|
|
|
var exitItem = contextMenu.Items.Add("&Exit");
|
2022-11-25 07:48:01 +01:00
|
|
|
|
exitItem.Click += delegate { Application.Exit(); };
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
notifyIcon = new NotifyIcon(components);
|
|
|
|
|
|
notifyIcon.Icon = Resources.microsoft_xbox_controller_off;
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion;
|
|
|
|
|
|
notifyIcon.Visible = true;
|
|
|
|
|
|
notifyIcon.ContextMenuStrip = contextMenu;
|
|
|
|
|
|
|
|
|
|
|
|
var contextStateUpdate = new System.Windows.Forms.Timer(components);
|
|
|
|
|
|
contextStateUpdate.Interval = 250;
|
|
|
|
|
|
contextStateUpdate.Enabled = true;
|
|
|
|
|
|
contextStateUpdate.Tick += ContextStateUpdate_Tick;
|
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
|
contextThread = new Thread(ContextState_Update);
|
|
|
|
|
|
contextThread.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ContextState_Update(object? obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (running)
|
|
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
if (lastUpdatesReset + updateResetInterval < stopwatch.Elapsed)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastUpdatesReset = stopwatch.Elapsed;
|
|
|
|
|
|
lastUpdatesReceived = updatesReceived;
|
|
|
|
|
|
updatesReceived = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updatesReceived++;
|
|
|
|
|
|
|
|
|
|
|
|
lock (context)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!context.Enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ContextStateUpdate_Tick(object? sender, EventArgs e)
|
|
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
lock (context)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Tick();
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
|
|
if (!context.Mouse.Valid)
|
|
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Cannot send input.";
|
2022-11-24 22:37:24 +01:00
|
|
|
|
notifyIcon.Icon = Resources.microsoft_xbox_controller_off_red;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!context.X360.Valid)
|
|
|
|
|
|
{
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Missing ViGEm?";
|
|
|
|
|
|
notifyIcon.Icon = Resources.microsoft_xbox_controller_red;
|
|
|
|
|
|
}
|
2022-11-25 07:48:01 +01:00
|
|
|
|
else if (context.Enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
notifyIcon.Icon = context.DesktopMode ? Resources.monitor : Resources.microsoft_xbox_controller;
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (context.DisableDueToSteam)
|
2022-11-24 22:37:24 +01:00
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
notifyIcon.Icon = context.DesktopMode ? Resources.monitor_off : Resources.microsoft_xbox_controller_off;
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Steam Detected";
|
2022-11-24 22:37:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
notifyIcon.Icon = context.DesktopMode ? Resources.monitor_off : Resources.microsoft_xbox_controller_off;
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Disabled";
|
2022-11-24 22:37:24 +01:00
|
|
|
|
}
|
2022-11-25 07:48:01 +01:00
|
|
|
|
|
|
|
|
|
|
notifyIcon.Text += String.Format(". Updates: {0}/s", lastUpdatesReceived);
|
2022-11-24 22:37:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2022-11-25 07:48:01 +01:00
|
|
|
|
notifyIcon.Visible = false;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (contextThread != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
contextThread.Interrupt();
|
|
|
|
|
|
contextThread.Join();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (context) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|