Skip to content

Commit 4e009fd

Browse files
authored
Merge pull request #23 from FaithBeam/xdotool
Use xdotool to get foreground window on linux x11
2 parents 8908209 + 3148287 commit 4e009fd

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ This is an attempt at a cross-platform clone of X-Mouse-Button-Control.
77
## Usage
88

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

1414
## OS Compatibility
1515

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

27+
## Linux Recommended Software
28+
29+
* xdotool (x11 only)
30+
* ```sudo apt install xdotool```
31+
* This is used to retrieve the foreground window name
32+
33+
If you don't install xdotool or use wayland, every window will match when doing a mouse press.
34+
2735
## Build
2836

2937
### Requirements

YMouseButtonControl.Linux/Services/CurrentWindowService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ namespace YMouseButtonControl.Linux.Services;
44

55
public class CurrentWindowService : ICurrentWindowService
66
{
7-
// Match every window. If someone figures out how to get the current window in X11 and/or wayland, make a PR please
8-
public string ForegroundWindow { get; } = "*";
7+
public string ForegroundWindow => "*";
98
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Diagnostics;
2+
using YMouseButtonControl.Core.Services.Processes;
3+
4+
namespace YMouseButtonControl.Linux.Services;
5+
6+
public class CurrentWindowServiceX11 : ICurrentWindowService
7+
{
8+
public string ForegroundWindow => GetForegroundWindow();
9+
10+
private string GetForegroundWindow()
11+
{
12+
var startInfo = new ProcessStartInfo
13+
{
14+
FileName = "/bin/bash",
15+
Arguments = "-c \"xdotool getwindowfocus getwindowpid\"",
16+
RedirectStandardOutput = true,
17+
};
18+
using var xdoProc = new Process();
19+
xdoProc.StartInfo = startInfo;
20+
xdoProc.Start();
21+
var pid = xdoProc.StandardOutput.ReadToEnd().TrimEnd();
22+
xdoProc.WaitForExit();
23+
24+
if (string.IsNullOrWhiteSpace(pid))
25+
{
26+
return "";
27+
}
28+
29+
startInfo = new ProcessStartInfo
30+
{
31+
FileName = "/bin/bash",
32+
Arguments = $"-c \"ls -l /proc/{pid}/exe\"",
33+
RedirectStandardOutput = true,
34+
};
35+
using var proc = new Process();
36+
proc.StartInfo = startInfo;
37+
proc.Start();
38+
var path = proc.StandardOutput.ReadToEnd().Split("-> ")[1].Trim();
39+
proc.WaitForExit();
40+
return path;
41+
}
42+
}

YMouseButtonControl/DependencyInjection/ServicesBootstrapper.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Runtime.Versioning;
34
using Microsoft.Extensions.DependencyInjection;
45
using YMouseButtonControl.Core.Services.BackgroundTasks;
@@ -51,8 +52,33 @@ private static void RegisterLinuxServices(IServiceCollection services)
5152
services
5253
.AddScoped<IStartupInstallerService, Linux.Services.StartupInstallerService>()
5354
.AddScoped<IProcessMonitorService, Linux.Services.ProcessMonitorService>()
54-
.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>()
5555
.AddScoped<IBackgroundTasksRunner, Linux.Services.BackgroundTasksRunner>();
56+
if (Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") == "x11")
57+
{
58+
var startInfo = new ProcessStartInfo
59+
{
60+
FileName = "/bin/bash",
61+
Arguments = "-c \"xdotool\"",
62+
RedirectStandardOutput = true,
63+
RedirectStandardError = true,
64+
};
65+
using var proc = new Process();
66+
proc.StartInfo = startInfo;
67+
proc.Start();
68+
proc.WaitForExit();
69+
if (proc.ExitCode == 1)
70+
{
71+
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowServiceX11>();
72+
}
73+
else
74+
{
75+
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>();
76+
}
77+
}
78+
else
79+
{
80+
services.AddScoped<ICurrentWindowService, Linux.Services.CurrentWindowService>();
81+
}
5682
}
5783

5884
[SupportedOSPlatform("windows5.1.2600")]

0 commit comments

Comments
 (0)