Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
11 changes: 10 additions & 1 deletion linux/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -208,6 +212,11 @@ void OnHotkey(HotkeyCommand command)
{
Dispatcher.UIThread.Post(() =>
{
if (command == HotkeyCommand.Record)
{
ToggleRecording();
return;
}
if (_busy) return;
switch (command)
{
Expand Down
6 changes: 6 additions & 0 deletions linux/HotkeyChord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum HotkeyCommand
Snip,
SnipDelay,
Repeat,
Record,
}

/// <summary>
Expand Down Expand Up @@ -174,13 +175,15 @@ 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()
{
Snip = HotkeyChord.Parse(settings.SnipHotkey),
Delay = HotkeyChord.Parse(settings.DelayHotkey),
Repeat = HotkeyChord.Parse(settings.RepeatHotkey),
Record = HotkeyChord.Parse(settings.RecordHotkey),
UsePrintScreen = settings.UsePrintScreen,
};

Expand All @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions linux/SettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 19 additions & 4 deletions linux/X11Hotkeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ sealed class X11HotkeyService : IDisposable
static IntPtr _previousHandler;

readonly Action<HotkeyCommand> _onCommand;
readonly Action<string> _onConflict;
string _lastConflict = "";
readonly object _gate = new();
readonly Queue<Action> _commands = new();
readonly HashSet<int> _down = new();
Expand All @@ -143,8 +145,9 @@ sealed class X11HotkeyService : IDisposable
HotkeyBindings _bindings;
Action<HotkeyChord?> _listenCallback;

X11HotkeyService(HotkeyBindings bindings, Action<HotkeyCommand> onCommand)
X11HotkeyService(HotkeyBindings bindings, Action<HotkeyCommand> onCommand, Action<string> onConflict)
{
_onConflict = onConflict;
_bindings = bindings ?? new HotkeyBindings();
_onCommand = onCommand ?? (_ => { });
}
Expand All @@ -164,10 +167,10 @@ public static bool IsSupported
}
}

public static X11HotkeyService TryStart(HotkeyBindings bindings, Action<HotkeyCommand> onCommand)
public static X11HotkeyService TryStart(HotkeyBindings bindings, Action<HotkeyCommand> onCommand, Action<string> 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;
Expand Down Expand Up @@ -376,16 +379,28 @@ 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<HotkeyCommand>();
foreach (var grab in grabs)
{
int before = _errorCount;
X11Native.XGrabKey(_display, grab.Keycode, grab.Modifiers, _root, 0, GrabModeAsync, GrabModeAsync);
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)
Expand Down
37 changes: 35 additions & 2 deletions tests/OpenSnip.Hotkeys.Tests/HotkeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
28 changes: 25 additions & 3 deletions tests/OpenSnip.Hotkeys.Tests/X11HotkeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public async Task GrabbedChordFiresAndPlainKeysStayFree()
UsePrintScreen = false,
};
int hits = 0;
var commands = new System.Collections.Concurrent.ConcurrentQueue<HotkeyCommand>();
var listen = new List<HotkeyChord>();
var listenDone = new TaskCompletionSource<HotkeyChord>(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");
Expand Down Expand Up @@ -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<string>(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)
Expand Down
Loading