diff --git a/PARITY.md b/PARITY.md index 8b15be0..20d7aa8 100644 --- a/PARITY.md +++ b/PARITY.md @@ -54,7 +54,7 @@ Status of each Windows feature on Linux. This file describes the tree it ships w | Copy the MP4 | partial. `text/uri-list` through `xclip` or `wl-copy`. | `linux/ClipboardService.cs`, `linux/FileUri.cs` | | Preview still that plays on click | working. Play badge; click opens the file. The preview stays at least 8 seconds. | `linux/ThumbnailWindow.cs`, `linux/App.cs` | | Drag the video preview out | implemented. MP4 storage item and `text/uri-list` for native drops; desktop interoperability needs manual verification on X11/Wayland. | `linux/ThumbnailWindow.cs`, `linux/FileUri.cs` | -| `Win+Shift+R` | missing. The tray item and `opensnip --record` start and stop a recording. A global key grab is not built in. | `linux/App.cs`, `linux/Cli.cs` | +| `Win+Shift+R` | working on X11. Configurable in Shortcuts; unavailable grabs report conflicts. Toggles recording like the tray. Wayland lists `opensnip --record` for desktop bindings. | `linux/App.cs`, `linux/HotkeyChord.cs`, `linux/X11Hotkeys.cs`, `linux/SettingsWindow.cs` | | Tray click stops a recording, otherwise snips | working | `linux/App.cs` | | Snip blocked while recording | working | `linux/App.cs` | | Open the recordings folder | working | `linux/App.cs` | diff --git a/linux/App.cs b/linux/App.cs index 0344a92..7957a43 100644 --- a/linux/App.cs +++ b/linux/App.cs @@ -72,7 +72,11 @@ async Task StartAsync() async Task StartAsyncCore() { - try { _hotkeys = X11HotkeyService.TryStart(HotkeyBindings.From(Settings), OnHotkey); } + try + { + _hotkeys = X11HotkeyService.TryStart(HotkeyBindings.From(Settings), OnHotkey, message => + Dispatcher.UIThread.Post(() => _ = ShowErrorAsync(message))); + } catch (Exception ex) { Log.Error(ex); } try { _trayAvailable = await DBus.TrayHostAvailableAsync(); } @@ -208,6 +212,11 @@ void OnHotkey(HotkeyCommand command) { Dispatcher.UIThread.Post(() => { + if (command == HotkeyCommand.Record) + { + ToggleRecording(); + return; + } if (_busy) return; switch (command) { diff --git a/linux/HotkeyChord.cs b/linux/HotkeyChord.cs index 64a8ea5..158f0f6 100644 --- a/linux/HotkeyChord.cs +++ b/linux/HotkeyChord.cs @@ -6,6 +6,7 @@ enum HotkeyCommand Snip, SnipDelay, Repeat, + Record, } /// @@ -174,6 +175,7 @@ sealed class HotkeyBindings public HotkeyChord Snip { get; init; } public HotkeyChord Delay { get; init; } public HotkeyChord Repeat { get; init; } + public HotkeyChord Record { get; init; } public bool UsePrintScreen { get; init; } public static HotkeyBindings From(Settings settings) => new() @@ -181,6 +183,7 @@ sealed class HotkeyBindings Snip = HotkeyChord.Parse(settings.SnipHotkey), Delay = HotkeyChord.Parse(settings.DelayHotkey), Repeat = HotkeyChord.Parse(settings.RepeatHotkey), + Record = HotkeyChord.Parse(settings.RecordHotkey), UsePrintScreen = settings.UsePrintScreen, }; @@ -194,6 +197,7 @@ sealed class HotkeyBindings if (!bindings.Snip.IsEmpty && chord == bindings.Snip) return HotkeyCommand.Snip; if (!bindings.Delay.IsEmpty && chord == bindings.Delay) return HotkeyCommand.SnipDelay; if (!bindings.Repeat.IsEmpty && chord == bindings.Repeat) return HotkeyCommand.Repeat; + if (!bindings.Record.IsEmpty && chord == bindings.Record) return HotkeyCommand.Record; if (bindings.UsePrintScreen && chord.Key == "PrintScreen" && !chord.Alt && !chord.Win) { if (chord.Ctrl) return HotkeyCommand.Repeat; @@ -217,6 +221,7 @@ void Add(HotkeyChord chord) Add(Snip); Add(Delay); Add(Repeat); + Add(Record); if (UsePrintScreen) { Add(new HotkeyChord("PrintScreen", false, false, false, false)); @@ -282,6 +287,7 @@ void Consider(HotkeyChord chord) Consider(bindings.Snip); Consider(bindings.Delay); Consider(bindings.Repeat); + Consider(bindings.Record); if (bindings.UsePrintScreen) { Consider(new HotkeyChord("PrintScreen", false, false, false, false)); diff --git a/linux/SettingsWindow.cs b/linux/SettingsWindow.cs index 9a1c451..c3e5256 100644 --- a/linux/SettingsWindow.cs +++ b/linux/SettingsWindow.cs @@ -418,6 +418,7 @@ Control BuildShortcuts() ShortcutRowFor(panel, "Snip", () => _s.SnipHotkey, v => _s.SnipHotkey = v); ShortcutRowFor(panel, "Snip after delay", () => _s.DelayHotkey, v => _s.DelayHotkey = v); ShortcutRowFor(panel, "Repeat last region", () => _s.RepeatHotkey, v => _s.RepeatHotkey = v); + ShortcutRowFor(panel, "Start / stop recording", () => _s.RecordHotkey, v => _s.RecordHotkey = v); _printScreen = Check("Also use the Print Screen key", _s.UsePrintScreen, v => _s.UsePrintScreen = v); panel.Children.Add(_printScreen); _shortcutStatus = new TextBlock @@ -545,6 +546,7 @@ void ResetToDefaults() _s.SnipHotkey = "Win+Shift+S"; _s.DelayHotkey = ""; _s.RepeatHotkey = ""; + _s.RecordHotkey = "Win+Shift+R"; _s.UsePrintScreen = true; Save(); foreach (var refresh in _refresh) refresh(); diff --git a/linux/X11Hotkeys.cs b/linux/X11Hotkeys.cs index 114a79c..65ff2e2 100644 --- a/linux/X11Hotkeys.cs +++ b/linux/X11Hotkeys.cs @@ -121,6 +121,8 @@ sealed class X11HotkeyService : IDisposable static IntPtr _previousHandler; readonly Action _onCommand; + readonly Action _onConflict; + string _lastConflict = ""; readonly object _gate = new(); readonly Queue _commands = new(); readonly HashSet _down = new(); @@ -143,8 +145,9 @@ sealed class X11HotkeyService : IDisposable HotkeyBindings _bindings; Action _listenCallback; - X11HotkeyService(HotkeyBindings bindings, Action onCommand) + X11HotkeyService(HotkeyBindings bindings, Action onCommand, Action onConflict) { + _onConflict = onConflict; _bindings = bindings ?? new HotkeyBindings(); _onCommand = onCommand ?? (_ => { }); } @@ -164,10 +167,10 @@ public static bool IsSupported } } - public static X11HotkeyService TryStart(HotkeyBindings bindings, Action onCommand) + public static X11HotkeyService TryStart(HotkeyBindings bindings, Action onCommand, Action onConflict = null) { if (!IsSupported) return null; - var service = new X11HotkeyService(bindings, onCommand); + var service = new X11HotkeyService(bindings, onCommand, onConflict); if (service.Start()) return service; service.Dispose(); return null; @@ -376,6 +379,7 @@ void ApplyBindings() X11Native.XUngrabKey(_display, 0, HotkeyGrabs.AnyModifier, _root); X11Native.XSync(_display, 0); var grabs = HotkeyGrabs.Plan(_bindings, LookupKeycode, _altMask, _superMask); + var conflicts = new HashSet(); foreach (var grab in grabs) { int before = _errorCount; @@ -383,9 +387,20 @@ void ApplyBindings() X11Native.XSync(_display, 0); if (_errorCount != before) { - // Another client already owns this combination. The others still work. + conflicts.Add(grab.Command); } } + string conflict = string.Join(", ", conflicts.OrderBy(command => command).Select(command => command switch + { + HotkeyCommand.Snip => "Snip", + HotkeyCommand.SnipDelay => "Snip after delay", + HotkeyCommand.Repeat => "Repeat last region", + HotkeyCommand.Record => "Start / stop recording", + _ => command.ToString(), + })); + if (conflict != _lastConflict && conflict.Length > 0) + _onConflict?.Invoke($"Shortcut unavailable: {conflict}. Change it in Settings → Shortcuts or release it in your desktop settings."); + _lastConflict = conflict; } int LookupKeycode(string key) diff --git a/tests/OpenSnip.Hotkeys.Tests/HotkeyTests.cs b/tests/OpenSnip.Hotkeys.Tests/HotkeyTests.cs index 1cd2b34..5d7af8b 100644 --- a/tests/OpenSnip.Hotkeys.Tests/HotkeyTests.cs +++ b/tests/OpenSnip.Hotkeys.Tests/HotkeyTests.cs @@ -72,14 +72,47 @@ public void CustomChordsBeatThePrintScreenFamily() } [Fact] - public void DefaultSummaryMatchesTheWindowsDefaults() + public void DefaultSummaryIncludesRecording() { Assert.Equal( - "Win + Shift + S, PrtSc, Shift + PrtSc, Ctrl + PrtSc", + "Win + Shift + S, Win + Shift + R, PrtSc, Shift + PrtSc, Ctrl + PrtSc", HotkeyBindings.From(new Settings()).Summary); Assert.Equal("off", new HotkeyBindings().Summary); } + [Fact] + public void RecordingUsesConfiguredChordAndLockVariants() + { + var settings = new Settings(); + var chord = HotkeyChord.Parse("Win+Shift+R"); + var bindings = HotkeyBindings.From(settings); + Assert.Equal(chord, bindings.Record); + Assert.Equal(HotkeyCommand.Record, HotkeyBindings.Match(chord, bindings)); + var grabs = HotkeyGrabs.Plan(bindings, key => key == "R" ? 27 : 0, 8, 64); + Assert.Equal(HotkeyGrabs.LockExtras.Length, grabs.Count); + Assert.All(grabs, grab => Assert.Equal(HotkeyCommand.Record, grab.Command)); + Assert.Contains(grabs, grab => grab.Modifiers == (64 | HotkeyGrabs.ShiftMask)); + + settings.RecordHotkey = "Ctrl+Alt+F8"; + bindings = HotkeyBindings.From(settings); + Assert.Null(HotkeyBindings.Match(chord, bindings)); + Assert.Equal(HotkeyCommand.Record, HotkeyBindings.Match(HotkeyChord.Parse(settings.RecordHotkey), bindings)); + settings.RecordHotkey = ""; + Assert.True(HotkeyBindings.From(settings).Record.IsEmpty); + settings.RecordHotkey = "R"; + Assert.Empty(HotkeyGrabs.Plan(HotkeyBindings.From(settings), key => key == "R" ? 27 : 0, 8, 64)); + } + + [Fact] + public void DuplicateRecordingChordKeepsExistingPriority() + { + var chord = HotkeyChord.Parse("Win+Shift+R"); + var bindings = new HotkeyBindings { Snip = chord, Record = chord }; + var grabs = HotkeyGrabs.Plan(bindings, _ => 27, 8, 64); + Assert.Equal(HotkeyGrabs.LockExtras.Length, grabs.Count); + Assert.All(grabs, grab => Assert.Equal(HotkeyCommand.Snip, grab.Command)); + } + [Fact] public void PlanGrabsLockVariantsAndSkipsUnsafeOrUnknownKeys() { diff --git a/tests/OpenSnip.Hotkeys.Tests/X11HotkeyTests.cs b/tests/OpenSnip.Hotkeys.Tests/X11HotkeyTests.cs index 8bcad97..bffb088 100644 --- a/tests/OpenSnip.Hotkeys.Tests/X11HotkeyTests.cs +++ b/tests/OpenSnip.Hotkeys.Tests/X11HotkeyTests.cs @@ -15,9 +15,10 @@ public async Task GrabbedChordFiresAndPlainKeysStayFree() UsePrintScreen = false, }; int hits = 0; + var commands = new System.Collections.Concurrent.ConcurrentQueue(); var listen = new List(); var listenDone = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using var service = X11HotkeyService.TryStart(bindings, _ => Interlocked.Increment(ref hits)); + using var service = X11HotkeyService.TryStart(bindings, command => { commands.Enqueue(command); Interlocked.Increment(ref hits); }); Assert.NotNull(service); int codeZ = service.KeycodeFor("Z"); @@ -61,19 +62,40 @@ public async Task GrabbedChordFiresAndPlainKeysStayFree() // The keys from capture must not stay "down" after the grab ends, or the new shortcut never fires. service.Update(new HotkeyBindings { - Snip = HotkeyChord.Parse("Ctrl+Shift+B"), + Record = HotkeyChord.Parse("Ctrl+Shift+B"), UsePrintScreen = false, }); Thread.Sleep(200); Press("ctrl+shift+b"); Wait(() => Volatile.Read(ref hits) >= 3); Assert.Equal(3, Volatile.Read(ref hits)); + Assert.Equal(HotkeyCommand.Record, commands.Last()); + Press("ctrl+shift+b"); + Wait(() => Volatile.Read(ref hits) >= 4); + Assert.Equal(HotkeyCommand.Record, commands.Last()); Press("ctrl+alt+shift+z"); Thread.Sleep(250); - Assert.Equal(3, Volatile.Read(ref hits)); + Assert.Equal(4, Volatile.Read(ref hits)); service.Dispose(); Assert.True(X11HotkeyService.ProbeGrabSucceeds(codeZ, mask)); + + // A desktop-owned recording chord reports a conflict without taking down the service. + var other = X11Native.XOpenDisplay(null); + Assert.NotEqual(IntPtr.Zero, other); + try + { + X11Native.XGrabKey(other, codeZ, mask, X11Native.XDefaultRootWindow(other), 0, 1, 1); + X11Native.XSync(other, 0); + var conflict = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using var blocked = X11HotkeyService.TryStart(new HotkeyBindings { Record = bindings.Snip }, + _ => { }, message => conflict.TrySetResult(message)); + Assert.NotNull(blocked); + var reported = await Task.WhenAny(conflict.Task, Task.Delay(TimeSpan.FromSeconds(2))); + Assert.Same(conflict.Task, reported); + Assert.Contains("Start / stop recording", await conflict.Task); + } + finally { X11Native.XCloseDisplay(other); } } static void Press(string combo)