diff --git a/PARITY.md b/PARITY.md index 1de802f..72cb700 100644 --- a/PARITY.md +++ b/PARITY.md @@ -11,9 +11,11 @@ Status of each Windows feature on Linux. This file describes the tree it ships w | Region snip on a frozen screen | working | `linux/Capture.cs`, `linux/OverlayWindow.cs` | | Click grabs the monitor | working | `linux/OverlayWindow.cs` | | Click grabs the window under the cursor | partial. X11 only | `linux/OverlayWindow.cs`, `src/CaptureOverlay.cs` | -| `F` selects the monitor | partial. Sets the selection; Enter or the mouse confirms. Windows completes immediately. While recording, `F` completes the monitor. | `linux/OverlayWindow.cs` | -| `A` selects all monitors | partial. Same as `F` for a snip. While recording, `A` completes the one monitor under the cursor. | `linux/OverlayWindow.cs` | -| Enter confirms, arrows nudge, Esc and right-click cancel | working | `linux/OverlayWindow.cs` | +| `F` captures the monitor under the cursor immediately | working | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` | +| `A` captures all monitors immediately | working. Recording selects the monitor under the cursor. | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` | +| Enter confirms the selection or monitor; arrows nudge 1 px, or 10 with Shift | working. X11 warps the pointer; Wayland moves the overlay cursor. | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` | +| Esc and right-click cancel the drag first, then the snip | working | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` | +| Mixed-DPI placement | partial. The overlay uses one scale for the whole desktop. | `linux/OverlayWindow.cs` | | Magnifier with coordinates | working | `linux/OverlayWindow.cs` | | Magnifier color | working | `linux/OverlayWindow.cs` | | X11 capture | working | `linux/Capture.cs` | @@ -21,13 +23,14 @@ Status of each Windows feature on Linux. This file describes the tree it ships w | Copy PNG to the clipboard | working. `xclip` on X11, `wl-copy` on Wayland, then Avalonia. | `linux/ClipboardService.cs` | | Auto-save PNG or JPG | working | `linux/CapturedFrame.cs`, `linux/SettingsWindow.cs` | | Monthly folders | working | `src/Settings.cs` | -| Preview thumbnail | working | `linux/ThumbnailWindow.cs` | +| Preview thumbnail | working. It does not take focus and opens on the snipped monitor. | `linux/ThumbnailWindow.cs`, `linux/SnipGeometry.cs` | | Click the preview to annotate | working | `linux/ThumbnailWindow.cs` | | Drag the preview into another app | working | `linux/ThumbnailWindow.cs`, `src/SmallWindows.cs` | | Annotation editor | working | `src/EditorForm.cs` | | Open the editor after a snip | working | `src/Settings.cs` | | Pin a snip, including from the clipboard | partial. Pin from clipboard or file; editor pin remains Windows-only | `src/SmallWindows.cs` | -| Delayed snip | working | `linux/CountdownWindow.cs`, `linux/App.cs` | +| Delayed snip | working. The countdown sits at the bottom of the monitor under the pointer, does not take focus, and closes before capture. X11 clicks pass through it; Wayland clicks do not. | `linux/CountdownWindow.cs`, `linux/X11ClickThrough.cs` | +| Tray snip waits for the menu to close | working | `linux/App.cs`, `linux/SnipGesture.cs` | | Repeat the last region | working | `linux/App.cs` | ## Screen recording diff --git a/README.md b/README.md index 8944321..36f3f56 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ On Wayland, bind `opensnip --snip`, `opensnip --snip-delay`, or `opensnip --repe | `A` | All monitors | | `Enter` | Grab what's selected | | Arrow keys | Nudge the cursor 1 px (`Shift` for 10 px) | -| `Esc` / right-click | Cancel | +| `Esc` / right-click | Cancel the current drag, or cancel | On X11 the outline follows the window under the pointer. On Wayland a click grabs the monitor. diff --git a/linux/App.cs b/linux/App.cs index e64873a..6a35b16 100644 --- a/linux/App.cs +++ b/linux/App.cs @@ -25,6 +25,7 @@ sealed class App : Application readonly List _pins = new(); readonly List _editors = new(); PixelRect? _lastRegion; + PixelRect? _lastScreen; bool _trayAvailable; bool _busy; bool _exiting; @@ -130,12 +131,12 @@ async Task StartAsyncCore() void CreateTray() { var menu = new NativeMenu(); - menu.Add(Item("Snip", () => StartSnip())); + menu.Add(Item("Snip", () => StartSnip(0, SnipTiming.MenuSettleMs))); + _delayItem = Item($"Snip after delay ({Settings.DelaySeconds}s)", () => StartSnip(Settings.DelaySeconds, SnipTiming.MenuSettleMs)); _recordItem = Item("Record video", () => _ = DelayedRecord()); menu.Add(_recordItem); - _delayItem = Item($"Snip after delay ({Settings.DelaySeconds}s)", () => StartSnip(Settings.DelaySeconds)); menu.Add(_delayItem); - menu.Add(Item("Repeat last region", RepeatLast)); + menu.Add(Item("Repeat last region", () => RepeatLast(SnipTiming.MenuSettleMs))); menu.Add(new NativeMenuItemSeparator()); menu.Add(Item("Pin image from clipboard", () => _ = PinAsync(null))); menu.Add(Item("Open image in editor…", () => _ = OpenImageAsync())); @@ -155,7 +156,7 @@ void CreateTray() _tray.Clicked += (_, _) => { if (_recording != null) ToggleRecording(); - else StartSnip(); + else StartSnip(0, SnipTiming.MenuSettleMs); }; TrayIcon.SetIcons(this, new TrayIcons { _tray }); UpdateTrayText(); @@ -480,7 +481,7 @@ async Task OnRecordingSavedAsync(RecordingSession session, string path, Captured ShowThumbnail(preview, path, Math.Max(Settings.PreviewSeconds, 8), play: true); } - public async void StartSnip(int delaySeconds = 0) + public async void StartSnip(int delaySeconds = 0, int settleMs = 0) { if (_recording != null || _busy) return; _busy = true; @@ -488,7 +489,7 @@ public async void StartSnip(int delaySeconds = 0) { bool hadThumbnail = _thumbnail != null; CloseThumbnail(); - if (hadThumbnail) await Task.Delay(100); + await SettleAsync(settleMs, hadThumbnail); if (delaySeconds > 0) await CountdownWindow.RunAsync(delaySeconds); var frame = await CaptureService.CaptureAsync(); @@ -497,8 +498,9 @@ public async void StartSnip(int delaySeconds = 0) if (region is not { } selected) return; _lastRegion = selected; + _lastScreen = SnipGeometry.ToScreen(selected, overlay.VirtualBounds, frame.Width, frame.Height); var cropped = frame.Crop(selected.X, selected.Y, selected.Width, selected.Height); - await DeliverAsync(cropped); + await DeliverAsync(cropped, _lastScreen.Value); } catch (CaptureException ex) { @@ -516,12 +518,12 @@ public async void StartSnip(int delaySeconds = 0) } } - public async void RepeatLast() + public async void RepeatLast(int settleMs = 0) { if (_recording != null || _busy) return; if (_lastRegion is not { } region) { - StartSnip(); + StartSnip(0, settleMs); return; } @@ -531,7 +533,7 @@ public async void RepeatLast() { bool hadThumbnail = _thumbnail != null; CloseThumbnail(); - if (hadThumbnail) await Task.Delay(100); + await SettleAsync(settleMs, hadThumbnail); var frame = await CaptureService.CaptureAsync(); if (region.X < 0 || region.Y < 0 || region.Right > frame.Width || region.Bottom > frame.Height) { @@ -539,7 +541,8 @@ public async void RepeatLast() return; } var cropped = frame.Crop(region.X, region.Y, region.Width, region.Height); - await DeliverAsync(cropped); + var screen = _lastScreen ?? SnipGeometry.ToScreen(region, new PixelRect(0, 0, frame.Width, frame.Height), frame.Width, frame.Height); + await DeliverAsync(cropped, screen); } catch (CaptureException ex) { @@ -558,6 +561,14 @@ public async void RepeatLast() } } + static async Task SettleAsync(int settleMs, bool hadThumbnail) + { + // The tray menu and our own preview are still on screen for a moment. + int wait = settleMs; + if (hadThumbnail) wait = Math.Max(wait, SnipTiming.ThumbnailClearMs); + if (wait > 0) await Task.Delay(wait); + } + static PixelRect[] WindowsFor(CapturedFrame frame) { // Rectangles are root-window pixels, so they only match a full-root capture. @@ -566,7 +577,7 @@ static PixelRect[] WindowsFor(CapturedFrame frame) return snap.Windows; } - async Task DeliverAsync(CapturedFrame frame) + async Task DeliverAsync(CapturedFrame frame, PixelRect screenRect) { bool copied = false; if (Settings.CopyToClipboard) @@ -604,11 +615,11 @@ async Task DeliverAsync(CapturedFrame frame) catch (Exception ex) { Log.Error(ex); - ShowThumbnail(frame, saved); + ShowThumbnail(frame, saved, anchor: screenRect); } } else if (Settings.ShowThumbnail || (!copied && saved == null)) - ShowThumbnail(frame, saved); + ShowThumbnail(frame, saved, anchor: screenRect); } public void OpenEditor(CapturedFrame frame) @@ -708,10 +719,10 @@ public async Task OpenImageAsync() } } - void ShowThumbnail(CapturedFrame frame, string path, int? seconds = null, bool play = false) + void ShowThumbnail(CapturedFrame frame, string path, int? seconds = null, bool play = false, PixelRect? anchor = null) { CloseThumbnail(); - var thumbnail = new ThumbnailWindow(frame, path, seconds ?? Settings.PreviewSeconds, play); + var thumbnail = new ThumbnailWindow(frame, path, seconds ?? Settings.PreviewSeconds, play, anchor); thumbnail.Closed += (_, _) => { if (ReferenceEquals(_thumbnail, thumbnail)) _thumbnail = null; diff --git a/linux/CountdownWindow.cs b/linux/CountdownWindow.cs index a3e101a..9d1ca5c 100644 --- a/linux/CountdownWindow.cs +++ b/linux/CountdownWindow.cs @@ -2,6 +2,7 @@ using Avalonia.Controls; using Avalonia.Layout; using Avalonia.Media; +using Avalonia.Threading; namespace OpenSnip; @@ -24,6 +25,8 @@ public CountdownWindow(int seconds) CanResize = false; Topmost = true; ShowInTaskbar = false; + ShowActivated = false; + Focusable = false; Width = 128; Height = 128; WindowStartupLocation = WindowStartupLocation.Manual; @@ -36,18 +39,47 @@ public CountdownWindow(int seconds) Child = _number, }; _number.Text = seconds.ToString(); + Place(); + } + + protected override void OnOpened(EventArgs e) + { + base.OnOpened(e); + Place(); + PassClicksThrough(); + // Avalonia may map the window after OnOpened. Apply the shape again once that settles. + Dispatcher.UIThread.Post(PassClicksThrough, DispatcherPriority.Background); + } - var screen = Screens.Primary - ?? Screens.All.FirstOrDefault(candidate => candidate.IsPrimary) - ?? Screens.All.FirstOrDefault(); - if (screen != null) + void Place() + { + var screen = ScreenUnderPointer(); + if (screen == null) return; + double scale = screen.Scaling <= 0 ? 1 : screen.Scaling; + int width = (int)Math.Round(Width * scale); + int height = (int)Math.Round(Height * scale); + int margin = (int)Math.Round(40 * scale); + Position = SnipGeometry.BottomCenter(screen.WorkingArea, width, height, margin); + } + + Avalonia.Platform.Screen ScreenUnderPointer() + { + var screens = Screens?.All; + if (screens == null || screens.Count == 0) return null; + if (X11Cursor.TryGet() is { } pointer) { - var area = screen.WorkingArea; - double scale = screen.Scaling <= 0 ? 1 : screen.Scaling; - Position = new PixelPoint( - (int)(area.X + area.Width / 2.0 - Width * scale / 2), - (int)(area.Y + area.Height / 2.0 - Height * scale / 2)); + var hit = Screens.ScreenFromPoint(new PixelPoint(pointer.X, pointer.Y)); + if (hit != null) return hit; } + return Screens.Primary + ?? screens.FirstOrDefault(candidate => candidate.IsPrimary) + ?? screens[0]; + } + + void PassClicksThrough() + { + if (TryGetPlatformHandle() is { Handle: var xid } && xid != IntPtr.Zero) + X11ClickThrough.TryEnable(xid); } public static async Task RunAsync(int seconds) @@ -65,8 +97,10 @@ public static async Task RunAsync(int seconds) } finally { - window.Close(); + // Hide first so the compositor can drop the window before the screen is captured. + try { window.Hide(); } catch { } + await Task.Delay(SnipTiming.CountdownClearMs); + try { window.Close(); } catch { } } - await Task.Delay(80); } } diff --git a/linux/OverlayWindow.cs b/linux/OverlayWindow.cs index 2a82850..5224fc1 100644 --- a/linux/OverlayWindow.cs +++ b/linux/OverlayWindow.cs @@ -43,6 +43,8 @@ public OverlayWindow(CapturedFrame frame, Settings settings, PixelRect[] windows Closed += (_, _) => _result.TrySetResult(null); } + public PixelRect VirtualBounds => _virtualBounds; + public Task SelectAsync() { Show(); @@ -92,7 +94,9 @@ sealed class CaptureCanvas : Control bool _dragging; bool _hasPointer; bool _keyboardCursor; + IPointer _pointer; Point _start; + Point _end; Point _cursor; Rect? _selection; Rect? _hoverLocal; @@ -120,72 +124,104 @@ public CaptureCanvas(CapturedFrame frame, Settings settings, PixelRect virtualBo public bool HandleKey(KeyEventArgs e) { - switch (e.Key) + if (!TryMapKey(e.Key, out var key)) return false; + bool pastSlop = DragPastSlop(); + bool ready = pastSlop && SelectionReady; + Apply(SnipGesture.FromKey(key, e.KeyModifiers.HasFlag(KeyModifiers.Shift), _pressing, ready, pastSlop)); + return true; + } + + bool DragPastSlop() + { + if (!_pressing) return false; + return Math.Abs(_end.X - _start.X) > 3 || Math.Abs(_end.Y - _start.Y) > 3; + } + + static bool TryMapKey(Key key, out SnipKey mapped) + { + switch (key) { - case Key.Escape: + case Key.Escape: mapped = SnipKey.Escape; return true; + case Key.Enter: mapped = SnipKey.Enter; return true; + case Key.F: mapped = SnipKey.Monitor; return true; + case Key.A: mapped = SnipKey.All; return true; + case Key.Left: mapped = SnipKey.Left; return true; + case Key.Right: mapped = SnipKey.Right; return true; + case Key.Up: mapped = SnipKey.Up; return true; + case Key.Down: mapped = SnipKey.Down; return true; + default: mapped = default; return false; + } + } + + bool SelectionReady => _selection is { Width: >= 1, Height: >= 1 }; + + void Apply(SnipGesture gesture) + { + switch (gesture.Kind) + { + case SnipGestureKind.Cancel: _complete(null); - return true; - case Key.Enter: + break; + case SnipGestureKind.ResetDrag: + ResetDrag(); + break; + case SnipGestureKind.CompleteSelection: if (_selection is { } current) _complete(ToImageRect(current)); - else _complete(HitAt(CurrentCursor()).Image); - return true; - case Key.A: - if (_record) - { - _complete(ToImageRect(MonitorRect(CurrentCursor()))); - return true; - } - _pressing = false; - _dragging = false; - _selection = new Rect(0, 0, Bounds.Width, Bounds.Height); - ClearHover(); - _keyboardCursor = false; - InvalidateVisual(); - return true; - case Key.F: - if (_record) - { - _complete(ToImageRect(MonitorRect(CurrentCursor()))); - return true; - } - _pressing = false; - _dragging = false; - _selection = MonitorRect(CurrentCursor()); - ClearHover(); - _keyboardCursor = false; - InvalidateVisual(); - return true; - case Key.Left: - case Key.Right: - case Key.Up: - case Key.Down: - Nudge(e.Key, e.KeyModifiers.HasFlag(KeyModifiers.Shift)); - return true; + break; + case SnipGestureKind.CompleteMonitor: + _complete(ToImageRect(MonitorRect(CurrentCursor()))); + break; + case SnipGestureKind.CompleteAll: + _complete(_record ? ToImageRect(MonitorRect(CurrentCursor())) : new PixelRect(0, 0, _frame.Width, _frame.Height)); + break; + case SnipGestureKind.Nudge: + Nudge(gesture.Dx, gesture.Dy); + break; } - return false; } - void Nudge(Key key, bool large) + void ResetDrag() { - double step = large ? 10 : 1; - var delta = key switch + _pressing = false; + _dragging = false; + _selection = null; + try { _pointer?.Capture(null); } catch { } + _pointer = null; + InvalidateVisual(); + } + + void Nudge(int dx, int dy) + { + if (X11Cursor.TryGet() is { } pointer) { - Key.Left => new Vector(-step, 0), - Key.Right => new Vector(step, 0), - Key.Up => new Vector(0, -step), - _ => new Vector(0, step), - }; - if (!_hasPointer) + var screen = SnipGeometry.ClampTo(pointer.X + dx, pointer.Y + dy, _virtualBounds); + bool warped = X11Cursor.TryWarp(screen.X, screen.Y); + MoveCursor(ScreenToLocal(screen.X, screen.Y), keyboard: !warped); + return; + } + + var delta = SnipGeometry.ControlDelta(dx, dy, Bounds.Width, Bounds.Height, _frame.Width, _frame.Height); + var origin = _hasPointer ? _cursor : new Point(Bounds.Width / 2, Bounds.Height / 2); + MoveCursor(origin + new Vector(delta.X, delta.Y), keyboard: true); + } + + void MoveCursor(Point local, bool keyboard) + { + _cursor = ClampPoint(local); + _hasPointer = true; + _keyboardCursor = keyboard; + if (_pressing) { - _cursor = new Point(Bounds.Width / 2, Bounds.Height / 2); - _hasPointer = true; + _end = _cursor; + var rect = RectFromPoints(_start, _end); + if (rect.Width >= 3 || rect.Height >= 3) _dragging = true; + if (_dragging) + { + _selection = rect.Width >= 1 && rect.Height >= 1 ? rect : null; + ClearHover(); + } } - _cursor = ClampPoint(_cursor + delta); - if (_selection is { } current) - _selection = ClampRect(current.Translate(delta)); - else - UpdateHover(_cursor); - _keyboardCursor = true; + if (!_dragging) UpdateHover(_cursor); InvalidateVisual(); } @@ -196,7 +232,7 @@ protected override void OnPointerPressed(PointerPressedEventArgs e) var point = e.GetPosition(this); if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed) { - _complete(null); + Apply(SnipGesture.FromRightClick(_pressing)); e.Handled = true; return; } @@ -204,7 +240,8 @@ protected override void OnPointerPressed(PointerPressedEventArgs e) _pressing = true; _dragging = false; - _start = _cursor = ClampPoint(point); + _pointer = e.Pointer; + _start = _end = _cursor = ClampPoint(point); _hasPointer = true; _keyboardCursor = false; _selection = null; @@ -220,6 +257,8 @@ protected override void OnPointerMoved(PointerEventArgs e) var point = ClampPoint(e.GetPosition(this)); _cursor = point; _hasPointer = true; + _keyboardCursor = false; + _end = point; if (_pressing) { var rect = RectFromPoints(_start, point); @@ -243,6 +282,7 @@ protected override void OnPointerReleased(PointerReleasedEventArgs e) base.OnPointerReleased(e); if (!_pressing) return; _pressing = false; + _pointer = null; e.Pointer.Capture(null); var point = ClampPoint(e.GetPosition(this)); _cursor = point; @@ -273,17 +313,24 @@ Rect RectFromPoints(Point a, Point b) Point ClampPoint(Point point) => new(Math.Clamp(point.X, 0, Math.Max(0, Bounds.Width)), Math.Clamp(point.Y, 0, Math.Max(0, Bounds.Height))); - Rect ClampRect(Rect rect) + Point CurrentCursor() { - double x = Math.Clamp(rect.X, 0, Math.Max(0, Bounds.Width - rect.Width)); - double y = Math.Clamp(rect.Y, 0, Math.Max(0, Bounds.Height - rect.Height)); - return new Rect(x, y, rect.Width, rect.Height); + if (_hasPointer) return _cursor; + if (Bounds.Width > 1 && X11Cursor.TryGet() is { } pointer) + { + _cursor = ClampPoint(ScreenToLocal(pointer.X, pointer.Y)); + _hasPointer = true; + return _cursor; + } + if (Bounds.Width > 1) return new Point(Bounds.Width / 2, Bounds.Height / 2); + return new Point(0, 0); } - Point CurrentCursor() + Point ScreenToLocal(int screenX, int screenY) { - if (_hasPointer) return _cursor; - return new Point(Bounds.Width / 2, Bounds.Height / 2); + double sx = Bounds.Width / Math.Max(1, _virtualBounds.Width); + double sy = Bounds.Height / Math.Max(1, _virtualBounds.Height); + return new Point((screenX - _virtualBounds.X) * sx, (screenY - _virtualBounds.Y) * sy); } Rect MonitorRect(Point local) @@ -370,6 +417,7 @@ PixelRect ToImageRect(Rect local) public override void Render(DrawingContext context) { + if (!_hasPointer) CurrentCursor(); base.Render(context); var bounds = new Rect(0, 0, Bounds.Width, Bounds.Height); context.DrawImage(_bitmap, bounds); diff --git a/linux/SnipGeometry.cs b/linux/SnipGeometry.cs new file mode 100644 index 0000000..b7b5b09 --- /dev/null +++ b/linux/SnipGeometry.cs @@ -0,0 +1,118 @@ +using Avalonia; + +namespace OpenSnip; + +static class SnipGeometry +{ + public static PixelRect ToScreen(PixelRect image, PixelRect virtualBounds, int frameWidth, int frameHeight) + { + if (frameWidth <= 0 || frameHeight <= 0) return image; + if (frameWidth == virtualBounds.Width && frameHeight == virtualBounds.Height) + return new PixelRect(virtualBounds.X + image.X, virtualBounds.Y + image.Y, image.Width, image.Height); + + double sx = virtualBounds.Width / (double)frameWidth; + double sy = virtualBounds.Height / (double)frameHeight; + return new PixelRect( + virtualBounds.X + (int)Math.Round(image.X * sx), + virtualBounds.Y + (int)Math.Round(image.Y * sy), + Math.Max(1, (int)Math.Round(image.Width * sx)), + Math.Max(1, (int)Math.Round(image.Height * sy))); + } + + public static PixelRect ScreenToImage(PixelRect screen, PixelRect virtualBounds, int frameWidth, int frameHeight) + { + if (frameWidth <= 0 || frameHeight <= 0) return new PixelRect(0, 0, 1, 1); + if (virtualBounds.Width <= 0 || virtualBounds.Height <= 0) + return new PixelRect(0, 0, frameWidth, frameHeight); + + double sx = frameWidth / (double)virtualBounds.Width; + double sy = frameHeight / (double)virtualBounds.Height; + int x = (int)Math.Round((screen.X - virtualBounds.X) * sx); + int y = (int)Math.Round((screen.Y - virtualBounds.Y) * sy); + int width = Math.Max(1, (int)Math.Round(screen.Width * sx)); + int height = Math.Max(1, (int)Math.Round(screen.Height * sy)); + x = Math.Clamp(x, 0, Math.Max(0, frameWidth - 1)); + y = Math.Clamp(y, 0, Math.Max(0, frameHeight - 1)); + width = Math.Clamp(width, 1, frameWidth - x); + height = Math.Clamp(height, 1, frameHeight - y); + return new PixelRect(x, y, width, height); + } + + /// Screen that contains the most of , or the nearest when none overlap. + public static int BestScreen(PixelRect rect, IReadOnlyList screens) + { + if (screens == null || screens.Count == 0) return -1; + + int best = 0; + long bestArea = -1; + for (int i = 0; i < screens.Count; i++) + { + long area = OverlapArea(rect, screens[i]); + if (area > bestArea) + { + bestArea = area; + best = i; + } + } + if (bestArea > 0) return best; + + int closest = 0; + long bestDistance = long.MaxValue; + for (int i = 0; i < screens.Count; i++) + { + long distance = Separation(rect, screens[i]); + if (distance < bestDistance) + { + bestDistance = distance; + closest = i; + } + } + return closest; + } + + public static PixelPoint BottomCenter(PixelRect area, int width, int height, int margin) + { + width = Math.Max(1, width); + height = Math.Max(1, height); + int x = area.X + Math.Max(0, area.Width - width) / 2; + int y = area.Bottom - height - Math.Max(0, margin); + if (x < area.X) x = area.X; + if (y < area.Y) y = area.Y; + return new PixelPoint(x, y); + } + + public static PixelPoint ClampTo(int x, int y, PixelRect bounds) + { + int maxX = bounds.Width <= 1 ? bounds.X : bounds.Right - 1; + int maxY = bounds.Height <= 1 ? bounds.Y : bounds.Bottom - 1; + return new PixelPoint(Math.Clamp(x, bounds.X, maxX), Math.Clamp(y, bounds.Y, maxY)); + } + + public static (double X, double Y) ControlDelta(int dx, int dy, double controlWidth, double controlHeight, int imageWidth, int imageHeight) + { + double sx = controlWidth <= 1 || imageWidth <= 0 ? 1 : controlWidth / imageWidth; + double sy = controlHeight <= 1 || imageHeight <= 0 ? 1 : controlHeight / imageHeight; + return (dx * sx, dy * sy); + } + + static long OverlapArea(PixelRect a, PixelRect b) + { + int left = Math.Max(a.X, b.X); + int top = Math.Max(a.Y, b.Y); + int right = Math.Min(a.Right, b.Right); + int bottom = Math.Min(a.Bottom, b.Bottom); + if (right <= left || bottom <= top) return 0; + return (long)(right - left) * (bottom - top); + } + + static long Separation(PixelRect a, PixelRect b) + { + long dx = 0; + if (a.Right <= b.X) dx = b.X - a.Right; + else if (b.Right <= a.X) dx = a.X - b.Right; + long dy = 0; + if (a.Bottom <= b.Y) dy = b.Y - a.Bottom; + else if (b.Bottom <= a.Y) dy = a.Y - b.Bottom; + return dx + dy; + } +} diff --git a/linux/SnipGesture.cs b/linux/SnipGesture.cs new file mode 100644 index 0000000..2809ecb --- /dev/null +++ b/linux/SnipGesture.cs @@ -0,0 +1,75 @@ +namespace OpenSnip; + +enum SnipKey +{ + Escape, + Enter, + Monitor, + All, + Left, + Right, + Up, + Down, +} + +enum SnipGestureKind +{ + None, + Cancel, + ResetDrag, + CompleteSelection, + CompleteMonitor, + CompleteAll, + Nudge, +} + +/// What a selection key does. Matches the Windows overlay. +readonly record struct SnipGesture(SnipGestureKind Kind, int Dx, int Dy) +{ + public static SnipGesture FromKey(SnipKey key, bool shift, bool pointerDown, bool selectionReady, bool dragPastSlop = false) + { + int step = shift ? 10 : 1; + switch (key) + { + case SnipKey.Escape: + return pointerDown + ? new SnipGesture(SnipGestureKind.ResetDrag, 0, 0) + : new SnipGesture(SnipGestureKind.Cancel, 0, 0); + case SnipKey.Enter: + // A fresh press still confirms the monitor. Only a drag that moved, and never + // formed a rectangle, is ignored — that is what the Windows overlay does. + if (selectionReady) return new SnipGesture(SnipGestureKind.CompleteSelection, 0, 0); + if (dragPastSlop) return new SnipGesture(SnipGestureKind.None, 0, 0); + return new SnipGesture(SnipGestureKind.CompleteMonitor, 0, 0); + case SnipKey.Monitor: + return new SnipGesture(SnipGestureKind.CompleteMonitor, 0, 0); + case SnipKey.All: + return new SnipGesture(SnipGestureKind.CompleteAll, 0, 0); + case SnipKey.Left: + return new SnipGesture(SnipGestureKind.Nudge, -step, 0); + case SnipKey.Right: + return new SnipGesture(SnipGestureKind.Nudge, step, 0); + case SnipKey.Up: + return new SnipGesture(SnipGestureKind.Nudge, 0, -step); + default: + return new SnipGesture(SnipGestureKind.Nudge, 0, step); + } + } + + public static SnipGesture FromRightClick(bool pointerDown) => + pointerDown + ? new SnipGesture(SnipGestureKind.ResetDrag, 0, 0) + : new SnipGesture(SnipGestureKind.Cancel, 0, 0); +} + +static class SnipTiming +{ + /// Windows waits this long after a tray click so the menu is gone before the shot. + public const int MenuSettleMs = 250; + + /// Windows hides its own preview, then waits at least this long before capturing. + public const int ThumbnailClearMs = 100; + + /// Windows hides the countdown, then waits this long so it is not in the shot. + public const int CountdownClearMs = 120; +} diff --git a/linux/ThumbnailWindow.cs b/linux/ThumbnailWindow.cs index 1b83ba4..0417e4b 100644 --- a/linux/ThumbnailWindow.cs +++ b/linux/ThumbnailWindow.cs @@ -4,6 +4,7 @@ using Avalonia.Layout; using Avalonia.Media; using Avalonia.Media.Imaging; +using Avalonia.Platform; using Avalonia.Platform.Storage; using Avalonia.Threading; using Shapes = Avalonia.Controls.Shapes; @@ -12,6 +13,7 @@ namespace OpenSnip; sealed class ThumbnailWindow : Window { + readonly PixelRect? _anchor; readonly CapturedFrame _frame; readonly DispatcherTimer _timer; string _path; @@ -19,16 +21,18 @@ sealed class ThumbnailWindow : Window bool _pressed; bool _dragged; - public ThumbnailWindow(CapturedFrame frame, string savedPath, int seconds, bool play = false) + public ThumbnailWindow(CapturedFrame frame, string savedPath, int seconds, bool play = false, PixelRect? anchor = null) { _frame = frame; _path = savedPath; + _anchor = anchor; Title = "OpenSnip"; Icon = IconLoader.Icon; SystemDecorations = SystemDecorations.None; CanResize = false; Topmost = true; ShowInTaskbar = false; + ShowActivated = false; WindowStartupLocation = WindowStartupLocation.Manual; double maxWidth = 300; @@ -115,9 +119,7 @@ static Control PlayBadge() void PlaceBottomRight() { - var screen = Screens.Primary - ?? Screens.All.FirstOrDefault(candidate => candidate.IsPrimary) - ?? Screens.All.FirstOrDefault(); + var screen = ScreenForAnchor(); if (screen == null) return; var area = screen.WorkingArea; double scale = screen.Scaling <= 0 ? 1 : screen.Scaling; @@ -126,6 +128,19 @@ void PlaceBottomRight() (int)(area.Bottom - Height * scale - 16 * scale)); } + Screen ScreenForAnchor() + { + var screens = Screens?.All; + if (screens == null || screens.Count == 0) return Screens?.Primary; + if (_anchor is not { } anchor) return Screens.Primary ?? screens[0]; + + var bounds = new PixelRect[screens.Count]; + for (int i = 0; i < screens.Count; i++) bounds[i] = screens[i].Bounds; + int index = SnipGeometry.BestScreen(anchor, bounds); + if (index < 0 || index >= screens.Count) return Screens.Primary ?? screens[0]; + return screens[index]; + } + void OnPressed(object sender, PointerPressedEventArgs e) { var point = e.GetCurrentPoint(this); diff --git a/linux/X11ClickThrough.cs b/linux/X11ClickThrough.cs new file mode 100644 index 0000000..7f7415c --- /dev/null +++ b/linux/X11ClickThrough.cs @@ -0,0 +1,83 @@ +using System.Runtime.InteropServices; + +namespace OpenSnip; + +/// +/// Gives an X11 window an empty input shape so clicks fall through to whatever is underneath. +/// The delay countdown has to stay visible without blocking the menu the user is opening. +/// +static class X11ClickThrough +{ + const string X11 = "libX11.so.6"; + const string Xext = "libXext.so.6"; + const int ShapeSet = 0; + const int ShapeInput = 2; + + public static bool TryEnable(IntPtr xid) + { + if (xid == IntPtr.Zero || !X11Cursor.IsAvailable) return false; + IntPtr display = IntPtr.Zero; + try + { + display = XOpenDisplay(null); + if (display == IntPtr.Zero) return false; + if (XShapeQueryExtension(display, out _, out _) == 0) return false; + XShapeCombineRectangles(display, xid, ShapeInput, 0, 0, IntPtr.Zero, 0, ShapeSet, 0); + XSync(display, 0); + return InputRectangleCount(display, xid) == 0; + } + catch (Exception ex) when (ex is DllNotFoundException or EntryPointNotFoundException) + { + return false; + } + catch (Exception ex) + { + Log.Error(ex); + return false; + } + finally + { + if (display != IntPtr.Zero) + { + try { XCloseDisplay(display); } catch { } + } + } + } + + public static int? InputRectangleCount(IntPtr xid) + { + if (xid == IntPtr.Zero || !X11Cursor.IsAvailable) return null; + IntPtr display = IntPtr.Zero; + try + { + display = XOpenDisplay(null); + if (display == IntPtr.Zero) return null; + if (XShapeQueryExtension(display, out _, out _) == 0) return null; + return InputRectangleCount(display, xid); + } + catch (DllNotFoundException) { return null; } + catch (EntryPointNotFoundException) { return null; } + finally + { + if (display != IntPtr.Zero) + { + try { XCloseDisplay(display); } catch { } + } + } + } + + static int InputRectangleCount(IntPtr display, IntPtr xid) + { + IntPtr rects = XShapeGetRectangles(display, xid, ShapeInput, out int count, out _); + if (rects != IntPtr.Zero) XFree(rects); + return count; + } + + [DllImport(X11)] static extern IntPtr XOpenDisplay(string displayName); + [DllImport(X11)] static extern int XCloseDisplay(IntPtr display); + [DllImport(X11)] static extern int XSync(IntPtr display, int discard); + [DllImport(X11)] static extern int XFree(IntPtr data); + [DllImport(Xext)] static extern int XShapeQueryExtension(IntPtr display, out int eventBase, out int errorBase); + [DllImport(Xext)] static extern void XShapeCombineRectangles(IntPtr display, IntPtr window, int destKind, int xOff, int yOff, IntPtr rectangles, int count, int op, int ordering); + [DllImport(Xext)] static extern IntPtr XShapeGetRectangles(IntPtr display, IntPtr window, int kind, out int count, out int ordering); +} diff --git a/linux/X11Cursor.cs b/linux/X11Cursor.cs new file mode 100644 index 0000000..2cc53c4 --- /dev/null +++ b/linux/X11Cursor.cs @@ -0,0 +1,78 @@ +using System.Runtime.InteropServices; +using Avalonia; + +namespace OpenSnip; + +/// Moves the X11 pointer. Wayland does not allow this, so callers fall back. +static class X11Cursor +{ + const string Lib = "libX11.so.6"; + + public static bool IsAvailable => PlatformInfo.HasDisplay && !PlatformInfo.IsWayland; + + public static (int X, int Y)? TryGet() + { + if (!IsAvailable) return null; + if (!TryOpen(out IntPtr display)) return null; + try + { + IntPtr root = XDefaultRootWindow(display); + if (root == IntPtr.Zero) return null; + if (XQueryPointer(display, root, out _, out _, out int x, out int y, out _, out _, out _) == 0) + return null; + return (x, y); + } + catch (DllNotFoundException) { return null; } + catch (EntryPointNotFoundException) { return null; } + finally + { + try { XCloseDisplay(display); } catch { } + } + } + + public static bool TryWarp(int x, int y) + { + if (!IsAvailable) return false; + if (!TryOpen(out IntPtr display)) return false; + try + { + IntPtr root = XDefaultRootWindow(display); + if (root == IntPtr.Zero) return false; + XWarpPointer(display, IntPtr.Zero, root, 0, 0, 0, 0, x, y); + XSync(display, 0); + return true; + } + catch (DllNotFoundException) { return false; } + catch (EntryPointNotFoundException) { return false; } + finally + { + try { XCloseDisplay(display); } catch { } + } + } + + public static PixelPoint? TryClampWarp(int x, int y, PixelRect bounds) + { + var clamped = SnipGeometry.ClampTo(x, y, bounds); + if (!TryWarp(clamped.X, clamped.Y)) return null; + return clamped; + } + + static bool TryOpen(out IntPtr display) + { + display = IntPtr.Zero; + try + { + display = XOpenDisplay(null); + return display != IntPtr.Zero; + } + catch (DllNotFoundException) { return false; } + catch (EntryPointNotFoundException) { return false; } + } + + [DllImport(Lib)] static extern IntPtr XOpenDisplay(string displayName); + [DllImport(Lib)] static extern int XCloseDisplay(IntPtr display); + [DllImport(Lib)] static extern IntPtr XDefaultRootWindow(IntPtr display); + [DllImport(Lib)] static extern int XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int rootX, out int rootY, out int winX, out int winY, out uint mask); + [DllImport(Lib)] static extern int XWarpPointer(IntPtr display, IntPtr src, IntPtr dest, int srcX, int srcY, uint srcWidth, uint srcHeight, int destX, int destY); + [DllImport(Lib)] static extern int XSync(IntPtr display, int discard); +} diff --git a/tests/OpenSnip.Tests/SnipGeometryTests.cs b/tests/OpenSnip.Tests/SnipGeometryTests.cs new file mode 100644 index 0000000..24d0bf9 --- /dev/null +++ b/tests/OpenSnip.Tests/SnipGeometryTests.cs @@ -0,0 +1,91 @@ +using Avalonia; + +namespace OpenSnip.Tests; + +public class SnipGeometryTests +{ + [Fact] + public void Image_rect_maps_onto_a_virtual_desktop_whose_origin_is_not_zero() + { + var image = new PixelRect(10, 20, 30, 40); + var desktop = new PixelRect(-1920, -100, 3840, 1200); + var screen = SnipGeometry.ToScreen(image, desktop, 3840, 1200); + Assert.Equal(new PixelRect(-1910, -80, 30, 40), screen); + + var back = SnipGeometry.ScreenToImage(screen, desktop, 3840, 1200); + Assert.Equal(image, back); + } + + [Fact] + public void Screen_rect_scales_when_the_capture_and_desktop_differ() + { + var desktop = new PixelRect(0, 0, 200, 100); + var screen = new PixelRect(50, 0, 100, 100); + var image = SnipGeometry.ScreenToImage(screen, desktop, 100, 50); + Assert.Equal(new PixelRect(25, 0, 50, 50), image); + } + + [Fact] + public void Best_screen_is_the_one_with_the_largest_overlap() + { + var screens = new[] + { + new PixelRect(0, 0, 1920, 1080), + new PixelRect(1920, 0, 1920, 1080), + }; + var snip = new PixelRect(1800, 100, 400, 200); + Assert.Equal(1, SnipGeometry.BestScreen(snip, screens)); + } + + [Fact] + public void Best_screen_falls_back_to_the_nearest() + { + var screens = new[] + { + new PixelRect(0, 0, 100, 100), + new PixelRect(500, 0, 100, 100), + }; + Assert.Equal(1, SnipGeometry.BestScreen(new PixelRect(420, 10, 20, 20), screens)); + Assert.Equal(-1, SnipGeometry.BestScreen(new PixelRect(0, 0, 1, 1), Array.Empty())); + } + + [Fact] + public void Countdown_sits_at_the_bottom_center_of_the_working_area() + { + var area = new PixelRect(0, 40, 1920, 1040); + var point = SnipGeometry.BottomCenter(area, 128, 128, 40); + Assert.Equal((1920 - 128) / 2, point.X); + Assert.Equal(40 + 1040 - 128 - 40, point.Y); + } + + [Fact] + public void Countdown_stays_inside_a_short_working_area() + { + var point = SnipGeometry.BottomCenter(new PixelRect(10, 20, 80, 30), 128, 128, 40); + Assert.Equal(10, point.X); + Assert.Equal(20, point.Y); + } + + [Fact] + public void Pointer_nudge_clamps_to_the_desktop() + { + var desktop = new PixelRect(-10, 0, 100, 50); + Assert.Equal(new PixelPoint(89, 0), SnipGeometry.ClampTo(200, -5, desktop)); + Assert.Equal(new PixelPoint(-10, 49), SnipGeometry.ClampTo(-40, 80, desktop)); + } + + [Fact] + public void One_image_pixel_is_one_control_pixel_when_sizes_match() + { + var delta = SnipGeometry.ControlDelta(10, -1, 1920, 1080, 1920, 1080); + Assert.Equal(10, delta.X); + Assert.Equal(-1, delta.Y); + } + + [Fact] + public void Control_delta_shrinks_when_the_overlay_is_scaled() + { + var delta = SnipGeometry.ControlDelta(2, 0, 960, 540, 1920, 1080); + Assert.Equal(1, delta.X); + } +} diff --git a/tests/OpenSnip.Tests/SnipGestureTests.cs b/tests/OpenSnip.Tests/SnipGestureTests.cs new file mode 100644 index 0000000..82c9312 --- /dev/null +++ b/tests/OpenSnip.Tests/SnipGestureTests.cs @@ -0,0 +1,64 @@ +namespace OpenSnip.Tests; + +public class SnipGestureTests +{ + [Fact] + public void Monitor_key_captures_immediately() + { + var gesture = SnipGesture.FromKey(SnipKey.Monitor, shift: false, pointerDown: false, selectionReady: false); + Assert.Equal(SnipGestureKind.CompleteMonitor, gesture.Kind); + + var midDrag = SnipGesture.FromKey(SnipKey.Monitor, shift: false, pointerDown: true, selectionReady: true); + Assert.Equal(SnipGestureKind.CompleteMonitor, midDrag.Kind); + } + + [Fact] + public void All_key_captures_every_monitor_immediately() + { + var gesture = SnipGesture.FromKey(SnipKey.All, shift: true, pointerDown: true, selectionReady: true); + Assert.Equal(SnipGestureKind.CompleteAll, gesture.Kind); + } + + [Fact] + public void Escape_and_right_click_cancel_the_drag_before_the_snip() + { + Assert.Equal(SnipGestureKind.ResetDrag, SnipGesture.FromKey(SnipKey.Escape, false, pointerDown: true, false).Kind); + Assert.Equal(SnipGestureKind.Cancel, SnipGesture.FromKey(SnipKey.Escape, false, pointerDown: false, false).Kind); + Assert.Equal(SnipGestureKind.ResetDrag, SnipGesture.FromRightClick(pointerDown: true).Kind); + Assert.Equal(SnipGestureKind.Cancel, SnipGesture.FromRightClick(pointerDown: false).Kind); + } + + [Fact] + public void Enter_confirms_the_selection_or_the_monitor() + { + Assert.Equal(SnipGestureKind.CompleteSelection, SnipGesture.FromKey(SnipKey.Enter, false, pointerDown: true, selectionReady: true).Kind); + Assert.Equal(SnipGestureKind.CompleteMonitor, SnipGesture.FromKey(SnipKey.Enter, false, pointerDown: true, selectionReady: false).Kind); + Assert.Equal(SnipGestureKind.None, SnipGesture.FromKey(SnipKey.Enter, false, pointerDown: true, selectionReady: false, dragPastSlop: true).Kind); + Assert.Equal(SnipGestureKind.CompleteMonitor, SnipGesture.FromKey(SnipKey.Enter, false, pointerDown: false, selectionReady: false).Kind); + Assert.Equal(SnipGestureKind.CompleteSelection, SnipGesture.FromKey(SnipKey.Enter, false, pointerDown: false, selectionReady: true).Kind); + } + + [Fact] + public void Arrows_nudge_one_pixel_or_ten_with_shift() + { + var one = SnipGesture.FromKey(SnipKey.Left, shift: false, false, false); + Assert.Equal(SnipGestureKind.Nudge, one.Kind); + Assert.Equal(-1, one.Dx); + Assert.Equal(0, one.Dy); + + var ten = SnipGesture.FromKey(SnipKey.Down, shift: true, true, true); + Assert.Equal(0, ten.Dx); + Assert.Equal(10, ten.Dy); + + Assert.Equal(10, SnipGesture.FromKey(SnipKey.Right, true, false, false).Dx); + Assert.Equal(-10, SnipGesture.FromKey(SnipKey.Up, true, false, false).Dy); + } + + [Fact] + public void Tray_and_countdown_waits_match_windows() + { + Assert.Equal(250, SnipTiming.MenuSettleMs); + Assert.Equal(120, SnipTiming.CountdownClearMs); + Assert.Equal(100, SnipTiming.ThumbnailClearMs); + } +} diff --git a/tests/OpenSnip.Tests/X11InputTests.cs b/tests/OpenSnip.Tests/X11InputTests.cs new file mode 100644 index 0000000..faca251 --- /dev/null +++ b/tests/OpenSnip.Tests/X11InputTests.cs @@ -0,0 +1,107 @@ +using System.Runtime.InteropServices; + +namespace OpenSnip.Tests; + +public class X11InputTests +{ + const string Lib = "libX11.so.6"; + const ulong CwOverrideRedirect = 512; + + [Fact] + public void Arrow_nudge_moves_the_x11_pointer_and_back() + { + if (!X11Cursor.IsAvailable) return; + var start = X11Cursor.TryGet(); + if (start == null) return; + + try + { + Assert.True(X11Cursor.TryWarp(start.Value.X + 7, start.Value.Y + 3)); + var moved = X11Cursor.TryGet(); + Assert.NotNull(moved); + Assert.Equal(start.Value.X + 7, moved.Value.X); + Assert.Equal(start.Value.Y + 3, moved.Value.Y); + } + finally + { + X11Cursor.TryWarp(start.Value.X, start.Value.Y); + } + } + + [Fact] + public void Empty_input_shape_lets_the_pointer_fall_through() + { + if (!X11Cursor.IsAvailable) return; + + IntPtr display = XOpenDisplay(null); + if (display == IntPtr.Zero) return; + + var start = X11Cursor.TryGet(); + IntPtr back = IntPtr.Zero; + IntPtr front = IntPtr.Zero; + try + { + IntPtr root = XDefaultRootWindow(display); + back = CreateWindow(display, root, 80, 90, 40, 30, 0x0000FF); + front = CreateWindow(display, root, 80, 90, 40, 30, 0x00FF00); + XMapWindow(display, back); + XMapWindow(display, front); + XRaiseWindow(display, front); + XWarpPointer(display, IntPtr.Zero, root, 0, 0, 0, 0, 100, 100); + XSync(display, 0); + + Assert.Equal(front, ChildAt(display, root, 100, 100)); + Assert.True(X11ClickThrough.TryEnable(front)); + Assert.Equal(0, X11ClickThrough.InputRectangleCount(front)); + Assert.Equal(back, ChildAt(display, root, 100, 100)); + } + finally + { + if (front != IntPtr.Zero) XDestroyWindow(display, front); + if (back != IntPtr.Zero) XDestroyWindow(display, back); + XCloseDisplay(display); + if (start != null) X11Cursor.TryWarp(start.Value.X, start.Value.Y); + } + } + + static IntPtr ChildAt(IntPtr display, IntPtr root, int x, int y) + { + XQueryPointer(display, root, out _, out IntPtr child, out _, out _, out _, out _, out _); + if (child == IntPtr.Zero) return IntPtr.Zero; + // Walk down so a parent that merely contains the point is not the answer. + while (child != IntPtr.Zero) + { + if (XQueryPointer(display, child, out _, out IntPtr nested, out _, out _, out _, out _, out _) == 0) + break; + if (nested == IntPtr.Zero) return child; + child = nested; + } + return child; + } + + static IntPtr CreateWindow(IntPtr display, IntPtr root, int x, int y, uint width, uint height, ulong pixel) + { + IntPtr window = XCreateSimpleWindow(display, root, x, y, width, height, 0, 0, pixel); + var attrs = new XSetWindowAttributes { override_redirect = 1 }; + XChangeWindowAttributes(display, window, CwOverrideRedirect, ref attrs); + return window; + } + + [StructLayout(LayoutKind.Explicit, Size = 112)] + struct XSetWindowAttributes + { + [FieldOffset(88)] public int override_redirect; + } + + [DllImport(Lib)] static extern IntPtr XOpenDisplay(string name); + [DllImport(Lib)] static extern int XCloseDisplay(IntPtr display); + [DllImport(Lib)] static extern IntPtr XDefaultRootWindow(IntPtr display); + [DllImport(Lib)] static extern IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, uint width, uint height, uint border, ulong borderPixel, ulong background); + [DllImport(Lib)] static extern int XChangeWindowAttributes(IntPtr display, IntPtr window, ulong valueMask, ref XSetWindowAttributes attributes); + [DllImport(Lib)] static extern int XMapWindow(IntPtr display, IntPtr window); + [DllImport(Lib)] static extern int XRaiseWindow(IntPtr display, IntPtr window); + [DllImport(Lib)] static extern int XDestroyWindow(IntPtr display, IntPtr window); + [DllImport(Lib)] static extern int XWarpPointer(IntPtr display, IntPtr src, IntPtr dest, int srcX, int srcY, uint srcWidth, uint srcHeight, int destX, int destY); + [DllImport(Lib)] static extern int XSync(IntPtr display, int discard); + [DllImport(Lib)] static extern int XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int rootX, out int rootY, out int winX, out int winY, out uint mask); +}