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.

2.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 computer'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 windows-helper Shake.cs:
// a stroke is STROKE px one way before it turns; REVERSALS turns within WINDOW_MS is a shake; a
// pause of GAP_MS or a short wiggle starts over; after one, COOLDOWN_MS of the rest doesn't count.
#include "helper.h"
#include <math.h>

#define STROKE 80.0
#define REVERSALS 4
#define WINDOW_MS 800.0
#define GAP_MS 200.0
#define COOLDOWN_MS 1000.0

// One detector's state: home (while capturing) and the File Shelf's (a file drag here, capture.c).
static ShakeState home;

void shake_reset(ShakeState *st) {
  st->dir = 0;
  st->run = 0;
  st->nturns = 0;
}

// 1 once per shake.
int shake_step(ShakeState *st, double dx, double dy) {
  if (!st->stroke) { st->stroke = STROKE; st->reversals = REVERSALS; st->window = WINDOW_MS; st->gap = GAP_MS; }
  double now = now_ms();
  if (now < st->quiet) return 0;
  if (now - st->last >= st->gap) shake_reset(st);
  st->last = now;
  double ax = fabs(dx);
  if (ax < 1 || fabs(dy) > ax * 2) return 0; // mostly up or down: not a shake
  int d = dx > 0 ? 1 : -1;
  if (d == st->dir) {
    st->run += ax;
    return 0;
  }
  if (st->dir && st->run >= st->stroke) {
    st->turns[st->head] = now;
    st->head = (st->head + 1) % st->reversals;
    if (st->nturns < st->reversals) st->nturns++;
  } else if (st->dir) st->nturns = 0;
  st->dir = d;
  st->run = ax;
  // full ring: turns[head] is the oldest of the last REVERSALS
  if (st->nturns == st->reversals && now - st->turns[st->head] <= st->window) {
    shake_reset(st);
    st->quiet = now + COOLDOWN_MS;
    return 1;
  }
  return 0;
}

void shake_feed(double dx, double dy) {
  if (shake_step(&home, dx, dy)) emit("{\"event\":\"shake\"}");
}