2022-11-24 22:37:24 +01:00
|
|
|
|
using CommonHelpers;
|
|
|
|
|
|
using ExternalHelpers;
|
2022-11-25 10:56:17 +01:00
|
|
|
|
using SteamController.Helpers;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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 = {
|
2022-11-25 21:28:43 +01:00
|
|
|
|
new Profiles.DesktopProfile() { Name = "Desktop" },
|
|
|
|
|
|
new Profiles.SteamProfile() { Name = "Steam", Visible = false },
|
|
|
|
|
|
new Profiles.SteamWithShorcutsProfile() { Name = "Steam with Shortcuts", Visible = false },
|
|
|
|
|
|
new Profiles.X360Profile() { Name = "X360" },
|
|
|
|
|
|
new Profiles.X360RumbleProfile() { Name = "X360 with Rumble" }
|
2022-11-25 10:56:17 +01:00
|
|
|
|
},
|
|
|
|
|
|
Managers = {
|
|
|
|
|
|
new Managers.ProcessManager(),
|
|
|
|
|
|
new Managers.SteamManager()
|
2022-11-25 07:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-11-25 21:28:43 +01:00
|
|
|
|
SharedData<SteamControllerSetting> sharedData = SharedData<SteamControllerSetting>.CreateNew();
|
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
|
public Controller()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instance.RunOnce(TitleWithVersion, "Global\\SteamController");
|
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
|
var contextMenu = new ContextMenuStrip(components);
|
|
|
|
|
|
|
|
|
|
|
|
var enabledItem = new ToolStripMenuItem("&Enabled");
|
|
|
|
|
|
enabledItem.Checked = context.RequestEnable;
|
2022-11-25 10:56:17 +01:00
|
|
|
|
enabledItem.Click += delegate { context.RequestEnable = !context.RequestEnable; };
|
|
|
|
|
|
contextMenu.Opening += delegate { enabledItem.Checked = context.RequestEnable; };
|
2022-11-25 07:48:01 +01:00
|
|
|
|
contextMenu.Items.Add(enabledItem);
|
2022-11-25 21:28:43 +01:00
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
2022-11-25 07:48:01 +01:00
|
|
|
|
|
2022-11-25 21:28:43 +01:00
|
|
|
|
foreach (var profile in context.Profiles)
|
2022-11-24 22:37:24 +01:00
|
|
|
|
{
|
2022-11-25 21:28:43 +01:00
|
|
|
|
if (profile.Name == "" || !profile.Visible)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var profileItem = new ToolStripMenuItem(profile.Name);
|
|
|
|
|
|
profileItem.Click += delegate { lock (context) { context.SelectProfile(profile.Name); } };
|
|
|
|
|
|
contextMenu.Opening += delegate { profileItem.Checked = context.GetCurrentProfile() == profile; };
|
|
|
|
|
|
contextMenu.Items.Add(profileItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
2022-11-25 10:56:17 +01:00
|
|
|
|
var lizardMouseItem = new ToolStripMenuItem("Use Lizard &Mouse");
|
2022-11-25 21:28:43 +01:00
|
|
|
|
lizardMouseItem.Checked = DefaultGuideShortcutsProfile.SteamModeLizardMouse;
|
|
|
|
|
|
lizardMouseItem.Click += delegate { DefaultGuideShortcutsProfile.SteamModeLizardMouse = !DefaultGuideShortcutsProfile.SteamModeLizardMouse; };
|
|
|
|
|
|
contextMenu.Opening += delegate { lizardMouseItem.Checked = DefaultGuideShortcutsProfile.SteamModeLizardMouse; };
|
2022-11-25 10:56:17 +01:00
|
|
|
|
contextMenu.Items.Add(lizardMouseItem);
|
|
|
|
|
|
|
|
|
|
|
|
var lizardButtonsItem = new ToolStripMenuItem("Use Lizard &Buttons");
|
2022-11-25 21:28:43 +01:00
|
|
|
|
lizardButtonsItem.Checked = DefaultGuideShortcutsProfile.SteamModeLizardButtons;
|
|
|
|
|
|
lizardButtonsItem.Click += delegate { DefaultGuideShortcutsProfile.SteamModeLizardButtons = !DefaultGuideShortcutsProfile.SteamModeLizardButtons; };
|
|
|
|
|
|
contextMenu.Opening += delegate { lizardButtonsItem.Checked = DefaultGuideShortcutsProfile.SteamModeLizardButtons; };
|
2022-11-25 10:56:17 +01:00
|
|
|
|
contextMenu.Items.Add(lizardButtonsItem);
|
|
|
|
|
|
|
|
|
|
|
|
contextMenu.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
|
2022-11-25 21:28:43 +01:00
|
|
|
|
var steamDetectionItem = new ToolStripMenuItem("Auto-disable on &Steam");
|
|
|
|
|
|
steamDetectionItem.Checked = Settings.Default.EnableSteamDetection;
|
|
|
|
|
|
steamDetectionItem.Click += delegate
|
|
|
|
|
|
{
|
|
|
|
|
|
Settings.Default.EnableSteamDetection = !Settings.Default.EnableSteamDetection;
|
|
|
|
|
|
Settings.Default.Save();
|
|
|
|
|
|
};
|
|
|
|
|
|
contextMenu.Opening += delegate { steamDetectionItem.Checked = Settings.Default.EnableSteamDetection; };
|
|
|
|
|
|
contextMenu.Items.Add(steamDetectionItem);
|
|
|
|
|
|
|
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();
|
2022-11-25 10:56:17 +01:00
|
|
|
|
context.Debug();
|
2022-11-25 07:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!context.Enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 21:28:43 +01:00
|
|
|
|
private void SharedData_Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sharedData.GetValue(out var value) && value.DesiredProfile != "")
|
|
|
|
|
|
{
|
2022-11-26 12:15:38 +01:00
|
|
|
|
context.SelectProfile(value.DesiredProfile);
|
2022-11-25 21:28:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sharedData.SetValue(new SteamControllerSetting()
|
|
|
|
|
|
{
|
2022-11-26 12:15:38 +01:00
|
|
|
|
CurrentProfile = context.OrderedProfiles.FirstOrDefault((profile) => profile.Selected(context))?.Name ?? "",
|
2022-11-25 21:28:43 +01:00
|
|
|
|
SelectableProfiles = context.Profiles.Where((profile) => profile.Selected(context) || profile.Visible).JoinWithN((profile) => profile.Name),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-26 12:15:38 +01:00
|
|
|
|
SharedData_Update();
|
2022-11-25 07:48:01 +01:00
|
|
|
|
}
|
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 10:56:17 +01:00
|
|
|
|
else if (context.Enabled && context.SteamUsesController)
|
|
|
|
|
|
{
|
|
|
|
|
|
notifyIcon.Icon = context.DesktopMode ? Resources.monitor_off : Resources.microsoft_xbox_controller_off;
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Steam Detected";
|
|
|
|
|
|
}
|
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;
|
2022-11-25 21:28:43 +01:00
|
|
|
|
|
|
|
|
|
|
var profile = context.GetCurrentProfile();
|
|
|
|
|
|
if (profile is not null)
|
|
|
|
|
|
notifyIcon.Text = TitleWithVersion + ". Profile: " + profile.Name;
|
2022-11-25 07:48:01 +01:00
|
|
|
|
}
|
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) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|