Skip to content

Commit

Permalink
Linux: Create linux specific project and services
Browse files Browse the repository at this point in the history
  • Loading branch information
FaithBeam committed Apr 19, 2024
1 parent f7d96c7 commit 8b57ca5
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 9 deletions.
28 changes: 28 additions & 0 deletions YMouseButtonControl.Services.Linux/BackgroundTasksRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using YMouseButtonControl.Core.KeyboardAndMouse;
using YMouseButtonControl.Core.KeyboardAndMouse.Interfaces;
using YMouseButtonControl.Core.Services.BackgroundTasks;

namespace YMouseButtonControl.Services.Linux;

public class BackgroundTasksRunner : IBackgroundTasksRunner
{
private readonly IMouseListener _mouseListener;
private readonly KeyboardSimulatorWorker _keyboardSimulatorWorker;

public BackgroundTasksRunner(
IMouseListener mouseListener,
KeyboardSimulatorWorker keyboardSimulatorWorker
)
{
_mouseListener = mouseListener;
_keyboardSimulatorWorker = keyboardSimulatorWorker;
_mouseListener.Run();
_keyboardSimulatorWorker.Run();
}

public void Dispose()
{
_mouseListener.Dispose();
_keyboardSimulatorWorker.Dispose();
}
}
9 changes: 9 additions & 0 deletions YMouseButtonControl.Services.Linux/CurrentWindowService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using YMouseButtonControl.Core.Processes;

namespace YMouseButtonControl.Services.Linux;

public class CurrentWindowService : ICurrentWindowService
{
// Match every window. If someone figures out how to get the current window in X11 and/or wayland, make a PR please
public string ForegroundWindow { get; } = "*";
}
14 changes: 14 additions & 0 deletions YMouseButtonControl.Services.Linux/ProcessMonitorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics;
using YMouseButtonControl.Core.Processes;
using YMouseButtonControl.Core.Services.Abstractions.Models;

namespace YMouseButtonControl.Services.Linux;

public class ProcessMonitorService : IProcessMonitorService
{
public IEnumerable<ProcessModel> GetProcesses() =>
Process
.GetProcesses()
.Select(x => new ProcessModel(x))
.Where(x => !string.IsNullOrWhiteSpace(x.Process.ProcessName));
}
45 changes: 45 additions & 0 deletions YMouseButtonControl.Services.Linux/SkipProfileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Serilog;
using YMouseButtonControl.Core.DataAccess.Models.Implementations;
using YMouseButtonControl.Core.KeyboardAndMouse.Interfaces;
using YMouseButtonControl.Core.Processes;
using YMouseButtonControl.Core.Services.Abstractions.Models.EventArgs;

namespace YMouseButtonControl.Services.Linux;

public class SkipProfileService(ICurrentWindowService currentWindowService) : ISkipProfileService
{
private readonly ILogger _myLog = Log.Logger.ForContext<SkipProfileService>();

// Returns whether this profile should be skipped on mouse events
public bool ShouldSkipProfile(Profile p, NewMouseHookEventArgs e)
{
// If the profile's checkbox is checked in the profiles list
if (!p.Checked)
{
_myLog.Information("Not checked");
return true;
}

if (p.Process == "*")
{
return false;
}

if (currentWindowService.ForegroundWindow.Contains(p.Process))
{
return false;
}

if (currentWindowService.ForegroundWindow == "*")
{
return false;
}

_myLog.Information(
"Foreground window: {ForegroundWindow}",
currentWindowService.ForegroundWindow
);
_myLog.Information("Couldn't find foreground window {Process}", p.Process);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\YMouseButtonControl.Core\YMouseButtonControl.Core.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions YMouseButtonControl.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YMouseButtonControl.Service
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YMouseButtonControl.Core.Tests", "YMouseButtonControl.Core.Tests\YMouseButtonControl.Core.Tests.csproj", "{3568CE4C-607E-4DB7-B724-CCA418B7D32A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YMouseButtonControl.Services.Linux", "YMouseButtonControl.Services.Linux\YMouseButtonControl.Services.Linux.csproj", "{EE958A9F-3768-44CE-A08E-F692CB403BB8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{3568CE4C-607E-4DB7-B724-CCA418B7D32A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3568CE4C-607E-4DB7-B724-CCA418B7D32A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3568CE4C-607E-4DB7-B724-CCA418B7D32A}.Release|Any CPU.Build.0 = Release|Any CPU
{EE958A9F-3768-44CE-A08E-F692CB403BB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE958A9F-3768-44CE-A08E-F692CB403BB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE958A9F-3768-44CE-A08E-F692CB403BB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE958A9F-3768-44CE-A08E-F692CB403BB8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
using SharpHook.Reactive;
using YMouseButtonControl.Core.KeyboardAndMouse;
using YMouseButtonControl.Core.KeyboardAndMouse.Interfaces;
using YMouseButtonControl.KeyboardAndMouse;
using YMouseButtonControl.KeyboardAndMouse.SharpHook.Implementations;
using YMouseButtonControl.KeyboardAndMouse.SharpHook.Implementations.SimulatedKeystrokesTypes;
using YMouseButtonControl.KeyboardAndMouse.SharpHook.Implementations.SimulatedMousePressTypes;
using YMouseButtonControl.Services.MacOS;

namespace YMouseButtonControl.DependencyInjection;

Expand All @@ -26,9 +24,13 @@ private static void RegisterOsSpecificKeyboardAndMouseServices(IServiceCollectio
{
services.AddSingleton<ISkipProfileService, Services.Windows.SkipProfileService>();
}
else if (OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
else if (OperatingSystem.IsMacOS())
{
services.AddSingleton<ISkipProfileService, SkipProfileService>();
services.AddSingleton<ISkipProfileService, Services.MacOS.SkipProfileService>();
}
else if (OperatingSystem.IsLinux())
{
services.AddSingleton<ISkipProfileService, Services.Linux.SkipProfileService>();
}
else
{
Expand Down
19 changes: 14 additions & 5 deletions YMouseButtonControl/DependencyInjection/ServicesBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using YMouseButtonControl.Core.Services.BackgroundTasks;
using YMouseButtonControl.Services.MacOS;
using YMouseButtonControl.Services.Windows;
using BackgroundTasksRunner = YMouseButtonControl.Services.MacOS.BackgroundTasksRunner;
using CurrentWindowService = YMouseButtonControl.Services.MacOS.CurrentWindowService;

namespace YMouseButtonControl.DependencyInjection;

Expand All @@ -31,16 +29,27 @@ private static void RegisterPlatformSpecificServices(IServiceCollection services
{
RegisterWindowsServices(services);
}
else if (OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
else if (OperatingSystem.IsMacOS())
{
RegisterMacOsServices(services);
}
else if (OperatingSystem.IsLinux())
{
RegisterLinuxServices(services);
}
else
{
throw new Exception("Unsupported operating system");
}
}

private static void RegisterLinuxServices(IServiceCollection services)
{
services.AddSingleton<IProcessMonitorService, Services.Linux.ProcessMonitorService>();
services.AddSingleton<ICurrentWindowService, Services.Linux.CurrentWindowService>();
services.AddSingleton<IBackgroundTasksRunner, Services.Linux.BackgroundTasksRunner>();
}

[SupportedOSPlatform("windows5.1.2600")]
private static void RegisterWindowsServices(IServiceCollection services)
{
Expand All @@ -52,7 +61,7 @@ private static void RegisterWindowsServices(IServiceCollection services)
private static void RegisterMacOsServices(IServiceCollection services)
{
services.AddSingleton<IProcessMonitorService, MacOsProcessMonitorService>();
services.AddSingleton<ICurrentWindowService, CurrentWindowService>();
services.AddSingleton<IBackgroundTasksRunner, BackgroundTasksRunner>();
services.AddSingleton<ICurrentWindowService, Services.MacOS.CurrentWindowService>();
services.AddSingleton<IBackgroundTasksRunner, Services.MacOS.BackgroundTasksRunner>();
}
}
1 change: 1 addition & 0 deletions YMouseButtonControl/YMouseButtonControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<ProjectReference Include="..\YMouseButtonControl.DataAccess.LiteDb\YMouseButtonControl.DataAccess.LiteDb.csproj" />
<ProjectReference Include="..\YMouseButtonControl.KeyboardAndMouse.SharpHook\YMouseButtonControl.KeyboardAndMouse.SharpHook.csproj" />
<ProjectReference Include="..\YMouseButtonControl.Services.Linux\YMouseButtonControl.Services.Linux.csproj" />
<ProjectReference Include="..\YMouseButtonControl.Services.MacOS\YMouseButtonControl.Services.MacOS.csproj" />
<ProjectReference Include="..\YMouseButtonControl.Services.Windows\YMouseButtonControl.Services.Windows.csproj" />
<ProjectReference Include="..\YMouseButtonControl.Core\YMouseButtonControl.Core.csproj" />
Expand Down

0 comments on commit 8b57ca5

Please sign in to comment.