2024-02-08 15:59:56 +08:00
|
|
|
|
using System.Diagnostics;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
using System.Windows;
|
2024-11-09 06:30:47 +08:00
|
|
|
|
using WpfMaiTouchEmulator.Managers;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
|
|
|
|
namespace WpfMaiTouchEmulator;
|
2024-02-08 18:02:54 +08:00
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
2024-02-08 16:46:57 +08:00
|
|
|
|
private readonly MaiTouchSensorButtonStateManager buttonState;
|
2024-04-24 17:40:10 +08:00
|
|
|
|
private readonly MaiTouchComConnector connector;
|
2024-02-08 17:55:04 +08:00
|
|
|
|
private readonly VirtualComPortManager comPortManager;
|
|
|
|
|
private TouchPanel _touchPanel;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-04-24 17:40:10 +08:00
|
|
|
|
DataContext = new MainWindowViewModel()
|
|
|
|
|
{
|
|
|
|
|
IsDebugEnabled = Properties.Settings.Default.IsDebugEnabled,
|
|
|
|
|
IsAutomaticPortConnectingEnabled = Properties.Settings.Default.IsAutomaticPortConnectingEnabled,
|
|
|
|
|
IsAutomaticPositioningEnabled = Properties.Settings.Default.IsAutomaticPositioningEnabled,
|
|
|
|
|
IsExitWithSinmaiEnabled = Properties.Settings.Default.IsExitWithSinmaiEnabled,
|
2024-06-12 18:37:09 +08:00
|
|
|
|
IsRingButtonEmulationEnabled = Properties.Settings.Default.IsRingButtonEmulationEnabled
|
2024-04-24 17:40:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
Title = "Mai Touch Emulator";
|
|
|
|
|
buttonState = new MaiTouchSensorButtonStateManager(buttonStateValue);
|
2024-04-24 18:49:02 +08:00
|
|
|
|
connector = new MaiTouchComConnector(buttonState, (MainWindowViewModel)DataContext);
|
|
|
|
|
comPortManager = new VirtualComPortManager((MainWindowViewModel)DataContext);
|
2024-02-08 17:55:04 +08:00
|
|
|
|
connector.OnConnectStatusChange = (status) =>
|
|
|
|
|
{
|
|
|
|
|
connectionStateLabel.Content = status;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-10 07:58:13 +08:00
|
|
|
|
connector.OnConnectError = () =>
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
dataContext.IsAutomaticPortConnectingEnabled = false;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
connector.OnDataSent = (data) =>
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
SentLogBox.AppendText(data + Environment.NewLine);
|
|
|
|
|
SentLogBox.ScrollToEnd();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
connector.OnDataRecieved = (data) =>
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
RecievedLogBox.AppendText(data + Environment.NewLine);
|
|
|
|
|
RecievedLogBox.ScrollToEnd();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-10 07:47:46 +08:00
|
|
|
|
if (Properties.Settings.Default.FirstOpen)
|
|
|
|
|
{
|
2024-02-13 17:40:12 +08:00
|
|
|
|
Logger.Info("First open occurred");
|
2024-04-24 18:49:02 +08:00
|
|
|
|
ShowSetupInstructionsDialog();
|
2024-02-10 07:47:46 +08:00
|
|
|
|
Properties.Settings.Default.FirstOpen = false;
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 17:40:10 +08:00
|
|
|
|
|
2024-02-08 17:55:04 +08:00
|
|
|
|
Loaded += (s, e) => {
|
2024-02-13 17:40:12 +08:00
|
|
|
|
Logger.Info("Main window loaded, creating touch panel");
|
2024-02-08 17:55:04 +08:00
|
|
|
|
_touchPanel = new TouchPanel();
|
|
|
|
|
_touchPanel.onTouch = (value) => { buttonState.PressButton(value); };
|
|
|
|
|
_touchPanel.onRelease = (value) => { buttonState.ReleaseButton(value); };
|
2024-11-09 06:24:09 +08:00
|
|
|
|
_touchPanel.onInitialReposition = () => { WindowState = WindowState.Minimized; };
|
2024-02-08 17:55:04 +08:00
|
|
|
|
_touchPanel.Show();
|
|
|
|
|
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
2024-04-24 18:49:02 +08:00
|
|
|
|
_touchPanel.DataContext = dataContext;
|
2024-04-24 17:40:10 +08:00
|
|
|
|
|
2024-02-08 17:55:04 +08:00
|
|
|
|
_touchPanel.SetDebugMode(dataContext.IsDebugEnabled);
|
2024-04-15 16:11:21 +08:00
|
|
|
|
if (Properties.Settings.Default.IsAutomaticPositioningEnabled)
|
|
|
|
|
{
|
|
|
|
|
_touchPanel.DragWindowHandle.Visibility = Visibility.Hidden;
|
|
|
|
|
_touchPanel.ResizeGrip.Visibility = Visibility.Hidden;
|
|
|
|
|
}
|
2024-02-08 17:55:04 +08:00
|
|
|
|
AutomaticTouchPanelPositioningLoop();
|
2024-02-11 17:54:28 +08:00
|
|
|
|
AutomaticPortConnectingLoop();
|
2024-02-08 17:55:04 +08:00
|
|
|
|
ExitWithSinmaiLoop();
|
2024-02-08 16:46:57 +08:00
|
|
|
|
};
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
2024-02-08 16:46:57 +08:00
|
|
|
|
|
2024-04-24 17:40:10 +08:00
|
|
|
|
private async void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
|
2024-02-08 17:55:04 +08:00
|
|
|
|
{
|
2024-02-14 15:42:05 +08:00
|
|
|
|
e.Cancel = true;
|
|
|
|
|
await connector.Disconnect();
|
2024-11-09 06:24:09 +08:00
|
|
|
|
_touchPanel.Close();
|
2024-02-14 15:42:05 +08:00
|
|
|
|
Closing -= MainWindow_Closing;
|
2024-04-24 17:40:10 +08:00
|
|
|
|
e.Cancel = false;
|
2024-11-09 06:24:09 +08:00
|
|
|
|
Application.Current.Shutdown();
|
2024-02-08 15:59:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void ExitWithSinmaiLoop()
|
|
|
|
|
{
|
|
|
|
|
Process? sinamiProcess = null;
|
|
|
|
|
while (sinamiProcess == null)
|
|
|
|
|
{
|
|
|
|
|
var processes = Process.GetProcessesByName("Sinmai");
|
|
|
|
|
if (processes.Length > 0)
|
|
|
|
|
{
|
2024-02-13 17:40:12 +08:00
|
|
|
|
Logger.Info("Found sinmai process to exit alongside with");
|
2024-02-08 15:59:56 +08:00
|
|
|
|
sinamiProcess = processes[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
2024-02-14 15:42:05 +08:00
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
if (dataContext.IsExitWithSinmaiEnabled)
|
|
|
|
|
{
|
2024-02-14 15:42:05 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await sinamiProcess.WaitForExitAsync();
|
|
|
|
|
Logger.Info("Sinmai exited");
|
|
|
|
|
if (dataContext.IsExitWithSinmaiEnabled)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Disconnecting from COM port before shutting down");
|
|
|
|
|
await connector.Disconnect();
|
|
|
|
|
Logger.Info("Shutting down...");
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Failed to wait for sinmai to exit", ex);
|
|
|
|
|
dataContext.IsExitWithSinmaiEnabled = false;
|
|
|
|
|
Properties.Settings.Default.IsExitWithSinmaiEnabled = dataContext.IsExitWithSinmaiEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-04-24 18:49:02 +08:00
|
|
|
|
MessageBox.Show(dataContext.TxtFailedToSetupSinmaiExit, dataContext.TxtFailedToSetupSinmaiExitHeader, MessageBoxButton.OK, MessageBoxImage.Information);
|
2024-02-14 15:42:05 +08:00
|
|
|
|
}
|
2024-02-08 15:59:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 18:49:02 +08:00
|
|
|
|
private void ShowSetupInstructionsDialog()
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
MessageBox.Show(dataContext.TxtSetupInstructions, dataContext.TxtSetupInstructionsHeader, MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private async void AutomaticTouchPanelPositioningLoop()
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (dataContext.IsAutomaticPositioningEnabled)
|
|
|
|
|
{
|
|
|
|
|
_touchPanel.PositionTouchPanel();
|
|
|
|
|
}
|
2024-02-11 17:54:28 +08:00
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 17:54:28 +08:00
|
|
|
|
private async void AutomaticPortConnectingLoop()
|
2024-02-08 17:55:04 +08:00
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (dataContext.IsAutomaticPortConnectingEnabled)
|
|
|
|
|
{
|
2024-11-09 06:24:09 +08:00
|
|
|
|
connector.StartTouchSensorPolling();
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-11-09 06:24:09 +08:00
|
|
|
|
private void ConnectToPortButton_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-11-09 06:24:09 +08:00
|
|
|
|
connector.StartTouchSensorPolling();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void debugMode_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsDebugEnabled;
|
|
|
|
|
dataContext.IsDebugEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsDebugEnabled = dataContext.IsDebugEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 15:59:56 +08:00
|
|
|
|
_touchPanel.SetDebugMode(dataContext.IsDebugEnabled);
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void automaticTouchPanelPositioning_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsAutomaticPositioningEnabled;
|
|
|
|
|
dataContext.IsAutomaticPositioningEnabled = !enabled;
|
2024-04-15 16:11:21 +08:00
|
|
|
|
|
|
|
|
|
_touchPanel.DragWindowHandle.Visibility = dataContext.IsAutomaticPositioningEnabled ? Visibility.Hidden : Visibility.Visible;
|
|
|
|
|
_touchPanel.ResizeGrip.Visibility = dataContext.IsAutomaticPositioningEnabled ? Visibility.Hidden : Visibility.Visible;
|
|
|
|
|
|
2024-02-11 18:34:51 +08:00
|
|
|
|
Properties.Settings.Default.IsAutomaticPositioningEnabled = dataContext.IsAutomaticPositioningEnabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void automaticPortConnecting_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsAutomaticPortConnectingEnabled;
|
|
|
|
|
dataContext.IsAutomaticPortConnectingEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsAutomaticPortConnectingEnabled = dataContext.IsAutomaticPortConnectingEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void exitWithSinmai_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsExitWithSinmaiEnabled;
|
|
|
|
|
dataContext.IsExitWithSinmaiEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsExitWithSinmaiEnabled = dataContext.IsExitWithSinmaiEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
2024-02-08 17:55:04 +08:00
|
|
|
|
|
|
|
|
|
private async void buttonInstallComPort_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2024-02-10 07:47:46 +08:00
|
|
|
|
await comPortManager.InstallComPort();
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void buttonUninstallComPorts_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2024-02-10 07:47:46 +08:00
|
|
|
|
await comPortManager.UninstallVirtualPorts();
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 07:47:46 +08:00
|
|
|
|
private void buttonListComPorts_Click(object sender, RoutedEventArgs e)
|
2024-02-08 17:55:04 +08:00
|
|
|
|
{
|
2024-02-10 07:47:46 +08:00
|
|
|
|
var output = comPortManager.GetInstalledPorts();
|
2024-04-24 18:49:02 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
MessageBox.Show(string.Join("\n", output), dataContext.TxtCurrentlyInstalledPorts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void instructionsLabel_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ShowSetupInstructionsDialog();
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
2024-06-12 18:37:09 +08:00
|
|
|
|
|
|
|
|
|
private void emulateRingButtons_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsRingButtonEmulationEnabled;
|
|
|
|
|
dataContext.IsRingButtonEmulationEnabled = !enabled;
|
|
|
|
|
Properties.Settings.Default.IsRingButtonEmulationEnabled = dataContext.IsRingButtonEmulationEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
_touchPanel.SetEmulateRingButton(dataContext.IsRingButtonEmulationEnabled);
|
|
|
|
|
}
|
2024-02-15 02:40:57 +08:00
|
|
|
|
}
|