diff --git a/SlidePresenter/InputSender/LinuxInputSender.cs b/SlidePresenter/InputSender/LinuxInputSender.cs index cda05c8..011061c 100644 --- a/SlidePresenter/InputSender/LinuxInputSender.cs +++ b/SlidePresenter/InputSender/LinuxInputSender.cs @@ -1,13 +1,32 @@ #if OS_LINUX +using System.Diagnostics; + namespace ControllerSlidePresenter.InputSender; public class LinuxInputSender : IInputSender { - public void NextSlide() { + private const string PageUp = "0xff55"; + private const string PageDown = "0xff56"; + public void NextSlide() { + SendKeys(PageDown); } public void PreviousSlide() { + SendKeys(PageUp); + } + private static void SendKeys(string keycode) { + Process proc = new() { + StartInfo = { + FileName = "xdotool", + Arguments = $"key {keycode}", + UseShellExecute = false, + RedirectStandardError = false, + RedirectStandardInput = false, + RedirectStandardOutput = false + } + }; + proc.Start(); } } #endif \ No newline at end of file diff --git a/SlidePresenter/Linux.cs b/SlidePresenter/Linux.cs new file mode 100644 index 0000000..b13c885 --- /dev/null +++ b/SlidePresenter/Linux.cs @@ -0,0 +1,55 @@ +#if OS_LINUX +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace ControllerSlidePresenter; + +public static class Linux { + public static bool CanRun() { + if (getuid() != 0) { + Console.WriteLine("On Linux you need tu run as 'sudo'"); + return false; + } + if (!IsXdoToolInstalled()) + return false; + + return true; + } + + private static bool IsXdoToolInstalled() { + string result = RunShellCommand("which xdotool"); + if (!string.IsNullOrEmpty(result)) + return true; + + Console.WriteLine("xdotool is not installed."); + return false; + } + + private static string RunShellCommand(string command) { + try { + ProcessStartInfo procStartInfo = new("bash", "-c \"" + command + "\"") { + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + using Process process = new(); + process.StartInfo = procStartInfo; + process.Start(); + + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + return result.Trim(); + } + catch (Exception ex) { + Console.WriteLine("Error: " + ex.Message); + return string.Empty; + } + } + + + [DllImport("libc")] + private static extern uint getuid(); +} +#endif diff --git a/SlidePresenter/Program.cs b/SlidePresenter/Program.cs index a612818..53df152 100644 --- a/SlidePresenter/Program.cs +++ b/SlidePresenter/Program.cs @@ -3,6 +3,11 @@ namespace ControllerSlidePresenter { internal abstract class Program { private static async Task Main() { +#if OS_LINUX + if (!Linux.CanRun()) + return; +#endif + IGamepadReader? reader = ControllerSelector.GetReader(); if (reader == null) { Console.WriteLine("Invalid Controller Selected.");