Skip to content

Commit

Permalink
Merge pull request #23 from FaithBeam/xdotool
Browse files Browse the repository at this point in the history
Use xdotool to get foreground window on linux x11
  • Loading branch information
FaithBeam authored Oct 7, 2024
2 parents 8908209 + 3148287 commit 4e009fd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ This is an attempt at a cross-platform clone of X-Mouse-Button-Control.
## Usage

1. Download the latest release from the [Releases](https://github.com/FaithBeam/YMouseButtonControl/releases) page for your platform
* Alternatively download the latest build from the [Actions tab](https://github.com/FaithBeam/YMouseButtonControl/actions)
3. Extract the archive
4. Run YMouseButtonControl
* Alternatively, download the latest build from the [Actions tab](https://github.com/FaithBeam/YMouseButtonControl/actions)
2. Extract the archive
3. Run YMouseButtonControl

## OS Compatibility

Expand All @@ -24,6 +24,14 @@ Anything that can install .NET 8 should be able to run YMouseButtonControl
| Ubuntu | 20.04+ |
| macOS | 12.0+ |

## Linux Recommended Software

* xdotool (x11 only)
* ```sudo apt install xdotool```
* This is used to retrieve the foreground window name

If you don't install xdotool or use wayland, every window will match when doing a mouse press.

## Build

### Requirements
Expand Down
3 changes: 1 addition & 2 deletions YMouseButtonControl.Linux/Services/CurrentWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ namespace YMouseButtonControl.Linux.Services;

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; } = "*";
public string ForegroundWindow => "*";
}
42 changes: 42 additions & 0 deletions YMouseButtonControl.Linux/Services/CurrentWindowServiceX11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Diagnostics;
using YMouseButtonControl.Core.Services.Processes;

namespace YMouseButtonControl.Linux.Services;

public class CurrentWindowServiceX11 : ICurrentWindowService
{
public string ForegroundWindow => GetForegroundWindow();

private string GetForegroundWindow()
{
var startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "-c \"xdotool getwindowfocus getwindowpid\"",
RedirectStandardOutput = true,
};
using var xdoProc = new Process();
xdoProc.StartInfo = startInfo;
xdoProc.Start();
var pid = xdoProc.StandardOutput.ReadToEnd().TrimEnd();
xdoProc.WaitForExit();

if (string.IsNullOrWhiteSpace(pid))
{
return "";
}

startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"ls -l /proc/{pid}/exe\"",
RedirectStandardOutput = true,
};
using var proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
var path = proc.StandardOutput.ReadToEnd().Split("-> ")[1].Trim();
proc.WaitForExit();
return path;
}
}
28 changes: 27 additions & 1 deletion YMouseButtonControl/DependencyInjection/ServicesBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using Microsoft.Extensions.DependencyInjection;
using YMouseButtonControl.Core.Services.BackgroundTasks;
Expand Down Expand Up @@ -51,8 +52,33 @@ private static void RegisterLinuxServices(IServiceCollection services)
services
.AddScoped<IStartupInstallerService, Linux.Services.StartupInstallerService>()
.AddScoped<IProcessMonitorService, Linux.Services.ProcessMonitorService>()
.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>()
.AddScoped<IBackgroundTasksRunner, Linux.Services.BackgroundTasksRunner>();
if (Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") == "x11")
{
var startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "-c \"xdotool\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
};
using var proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit();
if (proc.ExitCode == 1)
{
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowServiceX11>();
}
else
{
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>();
}
}
else
{
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>();
}
}

[SupportedOSPlatform("windows5.1.2600")]
Expand Down

0 comments on commit 4e009fd

Please sign in to comment.