Skip to content

Commit e9e8c3a

Browse files
committed
Use xdotool to get foreground window on linux x11
1 parent 8908209 commit e9e8c3a

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

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: 2 additions & 3 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; } = "*";
9-
}
7+
public string ForegroundWindow => "*";
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
{ FileName = "/bin/bash", Arguments = "-c \"xdotool getwindowfocus getwindowpid\"", RedirectStandardOutput = true };
14+
using var xdoProc = new Process();
15+
xdoProc.StartInfo = startInfo;
16+
xdoProc.Start();
17+
var pid = xdoProc.StandardOutput.ReadToEnd().TrimEnd();
18+
xdoProc.WaitForExit();
19+
20+
if (string.IsNullOrWhiteSpace(pid))
21+
{
22+
return "";
23+
}
24+
25+
startInfo = new ProcessStartInfo
26+
{ FileName = "/bin/bash", Arguments = $"-c \"ls -l /proc/{pid}/exe\"", RedirectStandardOutput = true };
27+
using var proc = new Process();
28+
proc.StartInfo = startInfo;
29+
proc.Start();
30+
var path = proc.StandardOutput.ReadToEnd().Split("-> ")[1].Trim();
31+
proc.WaitForExit();
32+
return path;
33+
}
34+
}

YMouseButtonControl/DependencyInjection/ServicesBootstrapper.cs

Lines changed: 28 additions & 2 deletions
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")]
@@ -73,4 +99,4 @@ private static void RegisterMacOsServices(IServiceCollection services)
7399
.AddScoped<ICurrentWindowService, MacOS.Services.CurrentWindowService>()
74100
.AddScoped<IBackgroundTasksRunner, MacOS.Services.BackgroundTasksRunner>();
75101
}
76-
}
102+
}

0 commit comments

Comments
 (0)