Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

3.0 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Shake to bring the pointer home: while capturing, a quick left-right shake of this PC's mouse or
// touchpad emits {"event":"shake"} and Node takes the input back here (kvm.js, kvm.shakeHome).
// Fed after the move has gone (lane or stdout), so it adds nothing in front of it: a few compares,
// no allocation. Same rules as mac-helper Shake.swift and linux-helper shake.c:
// a stroke is Stroke px one way before it turns; Reversals turns within WindowMs is a shake; a
// pause of GapMs or a short wiggle starts over; after one, CooldownMs of the rest doesn't count.
using System;
using System.Collections.Generic;

namespace MacShare
{
    // One detector's state: Shake.Home (while capturing) and Shake.Shelf (a file drag here, File Shelf).
    class Shake
    {
        // The shelf's is firmer: a file dragged about mustn't open it by chance; longer strokes, quicker.
        public static readonly Shake Home = new Shake(80, 4, 800, 200), Shelf = new Shake(120, 4, 750, 180);
        const int CooldownMs = 1000;
        readonly double Stroke;
        readonly int Reversals, WindowMs, GapMs;
        readonly int[] turns; // a ring of the last Reversals turns
        Shake(double stroke, int reversals, int windowMs, int gapMs) { Stroke = stroke; Reversals = reversals; WindowMs = windowMs; GapMs = gapMs; turns = new int[reversals]; }
        int dir, nTurns, head, last, quiet;
        double run;
        bool quieting;

        public void Reset() { dir = 0; run = 0; nTurns = 0; }

        // On the hook's thread, with the capture's emit.
        public static void Feed(double dx, double dy, Action<Dictionary<string, object>> emit)
        {
            if (Home.Step(dx, dy)) { Dictionary<string, object> e = new Dictionary<string, object>(); e["event"] = "shake"; emit(e); }
        }

        // True once per shake.
        public bool Step(double dx, double dy)
        {
            int now = Environment.TickCount; // wraps: compared by difference only
            if (quieting) { if (unchecked(now - quiet) < 0) return false; quieting = false; }
            if (unchecked(now - last) >= GapMs) Reset();
            last = now;
            double ax = Math.Abs(dx);
            if (ax < 1 || Math.Abs(dy) > ax * 2) return false; // mostly up or down: not a shake
            int d = dx > 0 ? 1 : -1;
            if (d == dir) { run += ax; return false; }
            if (dir != 0 && run >= Stroke)
            {
                turns[head] = now;
                head = (head + 1) % Reversals;
                if (nTurns < Reversals) nTurns++;
            }
            else if (dir != 0) nTurns = 0;
            dir = d;
            run = ax;
            // full ring: turns[head] is the oldest of the last Reversals
            if (nTurns == Reversals && unchecked(now - turns[head]) <= WindowMs)
            {
                Reset();
                quiet = unchecked(now + CooldownMs);
                quieting = true;
                return true;
            }
            return false;
        }
    }
}