Files
multiboxer/DD2Switcher/Program.cs

222 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DD2Switcher;
internal static class Program {
private static int NumProc = 9;
private static Process[] windows = new Process[NumProc];
private static int ActiveIndex = -1;
private static readonly IntPtr defaultAffinity = new(0xFF000000);
private static readonly IntPtr fullAffinity = new(0xFFFFFFFF);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
private static void AdjustAffinities() {
for (int i = 0; i < NumProc; i++) {
var window = windows[i];
if (window == null) continue;
if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null;
continue;
}
if (i != ActiveIndex) {
try {
window.ProcessorAffinity = defaultAffinity;
}
catch (Exception e) {
windows[i] = null;
}
}
}
var active = windows[ActiveIndex];
if (active != null) {
try {
active.ProcessorAffinity = fullAffinity;
}
catch (Exception e) {
windows[ActiveIndex] = null;
}
}
}
private static void AdjustPriorities() {
for (int i = 0; i < NumProc; i++) {
var window = windows[i];
if (window == null) continue;
if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null;
continue;
}
if (i != ActiveIndex) {
try {
window.PriorityClass = ProcessPriorityClass.Idle;
}
catch (Exception e) {
windows[i] = null;
}
}
}
var active = windows[ActiveIndex];
if (active != null) {
try {
active.PriorityClass = ProcessPriorityClass.High;
}
catch (Exception e) {
windows[ActiveIndex] = null;
}
}
}
private static Process GetForegroundProcess() {
var foregroundWindow = GetForegroundWindow();
var process = Process.GetProcesses();
Process foregroundProcess = null;
foreach (var p in process)
if (foregroundWindow == p.MainWindowHandle) {
foregroundProcess = p;
break;
}
if (foregroundProcess == null) return null;
return foregroundProcess;
}
private static void TrackWindow() {
Console.WriteLine("Toggling foreground window as tracked...");
var foregroundProcess = GetForegroundProcess();
if (foregroundProcess == null) return;
Console.WriteLine($"Foreground process: {foregroundProcess}");
for (int i = 0; i < 9; i++) {
var window = windows[i];
if (window != null && window.Id == foregroundProcess.Id) {
Console.WriteLine($"Removing foreground window from tracked at index {i}...");
windows[i] = null;
return;
}
}
for (int i = 0; i < 9; i++) {
var window = windows[i];
if (window == null) {
Console.WriteLine($"Adding foreground window to tracked at index {i}...");
windows[i] = foregroundProcess;
return;
}
}
}
private static void Swap(int index) {
Console.WriteLine($"Swapping window at index {index}");
var process = GetForegroundProcess();
if (process == null) return;
Console.WriteLine($"Foreground process: {process}");
bool found = false;
for (int i = 0; i < 9; i++) {
var window = windows[i];
if (window != null && window.Id == process.Id) {
found = true;
break;
}
}
if (!found) {
for (int i = 0; i < 9; i++) {
var window = windows[i];
if (window == null) {
Console.WriteLine($"Adding foreground window to tracked at index {i}...");
windows[i] = process;
}
}
}
for (int i = 0; i < 9; i++) {
var window = windows[i];
if (window != null && window.Id == process.Id) {
windows[i] = windows[index];
windows[index] = window;
Console.WriteLine($"Swapped window at index {i} to {index}");
return;
}
}
}
private static void TabTo(int index) {
if (index >= NumProc) return;
Console.WriteLine($"Tab to window at index {index}");
var window = windows[index];
if (window == null || window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null;
}
else {
SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index;
AdjustAffinities();
AdjustPriorities();
}
}
[STAThread]
private static void Main() {
//AllocConsole();
var processes = Process.GetProcesses();
var currentProcess = Process.GetCurrentProcess();
foreach (var process in processes)
if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) {
process.Kill();
Process.GetCurrentProcess().Kill();
}
for (int i = 0; i < 9; i++) {
windows[i] = null;
HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt | KeyModifiers.Shift);
}
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) {
TrackWindow();
return;
}
if (e.Modifiers == KeyModifiers.Alt) {
TabTo(e.Key - Keys.D1);
}
else if (e.Modifiers == (KeyModifiers.Alt | KeyModifiers.Shift)) {
Swap(e.Key - Keys.D1);
}
}
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
while (true)
System.Threading.Thread.Sleep(100000);
}
}