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.

Multi-device portals: also a way back after coming over an edge (#401)

Pengustof914ec9fc2c9
diff --git a/mac-helper/Sources/macshare-helper/Capture.swift b/mac-helper/Sources/macshare-helper/Capture.swift
index a565f859..bf2cb43e 100644
--- a/mac-helper/Sources/macshare-helper/Capture.swift
+++ b/mac-helper/Sources/macshare-helper/Capture.swift
@@ -142,6 +142,8 @@ final class Capture {
#if MULTI
// Came in through a portal: every portal from the driver to this Mac is a way back, each to its own place there.
var portalReturns: [PortalReturn] = []
+ // false: come over an ordinary edge, so the edges lead back too, beside the portals
+ var portalOnly = true
#endif
var portals: [Portal] = [] { didSet { portalPush.removeAll(); lastPortalPoint = nil } }
private var portalPush: [String: (push: Double, at: Double, since: Double)] = [:]
@@ -357,7 +359,7 @@ final class Capture {
#endif
var returnStops: [String] {
#if MULTI
- return portalReturns.isEmpty ? returnSides : []
+ return portalReturns.isEmpty || !portalOnly ? returnSides : []
#else
if portalReturn != nil { return [] }
if !stopsKnown || stopsSide != returnSide {
@@ -371,7 +373,7 @@ final class Capture {
var portalReturnStops: [PortalReturnStop] {
#if MULTI
- guard !returnSides.isEmpty else { return [] }
+ guard !returnSides.isEmpty || !portalReturns.isEmpty else { return [] }
return portalReturns.compactMap(Capture.stop)
#else
guard let portal = portalReturn, returnSide != nil, let stop = Capture.stop(portal) else { return [] }
@@ -401,7 +403,7 @@ final class Capture {
/// Return-edge check while driven; called for local moves and for injected relative moves.
/// Any armed side counts: the way back to the driver, or an edge that leads on to another device.
func checkReturn(_ p: CGPoint, dx: Double, dy: Double, flags: CGEventFlags) {
- guard !returnSides.isEmpty else { return }
+ guard !returnSides.isEmpty || !portalReturns.isEmpty else { return }
guard !returnRequiresModifier || allowsCrossing(required: crossingModifier, flags: flags) else { retBy.removeAll(); return }
// Came in through a portal: the ways back are the portals' ranges only, each with its own keys.
if !portalReturns.isEmpty {
@@ -418,8 +420,10 @@ final class Capture {
retBy[key] = st
if hit == nil, let off = off { hit = (i, portal.side, off, portalLocalOffset(p, stop: stop)) }
}
+ if hit == nil && !portalOnly { return checkEdges(p, dx: dx, dy: dy) }
guard let portal = hit else { return }
returnSides = []
+ portalReturns = []
lastCross = now()
Flash.shared.leave(at: p, side: portal.side)
var out: [String: Any] = ["event": "return", "side": portal.side, "offset": portal.offset,
@@ -430,6 +434,11 @@ final class Capture {
emit(out)
return
}
+ checkEdges(p, dx: dx, dy: dy)
+ }
+
+ /// The ordinary return edges: back to the driver, or on to the device behind one.
+ private func checkEdges(_ p: CGPoint, dx: Double, dy: Double) {
var hit: (side: String, offset: Double)? = nil
for side in returnSides {
var st = retBy[side] ?? (push: 0.0, at: 0.0, since: 0.0)
@@ -439,6 +448,7 @@ final class Capture {
}
guard let found = hit else { return }
returnSides = []
+ portalReturns = []
lastCross = now()
Flash.shared.leave(at: p, side: found.side)
var out: [String: Any] = ["event": "return", "side": found.side, "offset": found.offset]
diff --git a/mac-helper/Sources/macshare-helper/main.swift b/mac-helper/Sources/macshare-helper/main.swift
index 5ea09e6d..0dacf42d 100644
--- a/mac-helper/Sources/macshare-helper/main.swift
+++ b/mac-helper/Sources/macshare-helper/main.swift
@@ -195,6 +195,7 @@ func handle(_ line: String) {
// in order; a return names one by its index), or from an older app the one it came through (portal).
let listed = (msg["portals"] as? [[String: Any]]) ?? (msg["portal"] as? [String: Any]).map { [$0] } ?? []
Capture.shared.portalReturns = (msg["on"] as? Bool) == true ? listed.compactMap(portalReturn) : []
+ Capture.shared.portalOnly = (msg["portalOnly"] as? Bool) ?? true
let sides = (msg["sides"] as? [String]) ?? [(msg["side"] as? String) ?? "right"]
Capture.shared.armReturn(on: (msg["on"] as? Bool) ?? false, sides: sides, resistance: num("resistancePx", 120))
#else
diff --git a/mac/mesh/kvm.js b/mac/mesh/kvm.js
index 954c6b9a..6b9acffe 100644
--- a/mac/mesh/kvm.js
+++ b/mac/mesh/kvm.js
@@ -94,7 +94,8 @@ class Kvm extends EventEmitter {
this.drivenMouse = false; // driven with the mouse too (not keyboard only): the edges are watched
this.helperPortalVersion = 0; // the helper's portalCapabilities (0: an older helper, no portals)
this.portalIds = new Map(); // the helper's portal id -> { peer, rule }
- this.portalReturn = null; // driven, come in through a portal: { target } (the way back is its range)
+ this.portalReturn = null; // driven, come in through a portal: { target, id, returns } (the ways back are their ranges)
+ this.portalExits = []; // driven, come over an edge: the driver's portals here, ways back beside the edges
this.pendingPortal = null;
// Message types -> handler(m, fromId). The bus delivers them; onPartnerMessage() replays a phone bridge's.
@@ -337,11 +338,16 @@ class Kvm extends EventEmitter {
const offset = Portals.mapOffset(r, ev.offset);
// side + offset: where a computer without portals (or an older app) places the pointer instead
const along = Portals.desktopOffset(theirs, r.target, offset);
- // returns: every portal to that computer is a way back from it, each with the keys held to come back
+ this._take({ where: { portal: { target: r.target, offset, id: r.id, returns: this._portalExits(id) }, side: r.target.side, offset: along === null ? 0.5 : along } }, id, before);
+ }
+
+ // Every portal to computer id is also a way back from it, each with the keys held to come back (kvm.portalKeys).
+ _portalExits(id) {
+ if (!this.cfg.portalsOn) return [];
const back = this.cfg.portalKeys === 'back' || this.cfg.portalKeys === 'both';
- const returns = this.rulesFor(id).filter((x) => Portals.usableTarget(theirs, x.target))
+ const theirs = this._theirDisplays(id);
+ return this.rulesFor(id).filter((x) => Portals.usableTarget(theirs, x.target))
.map((x) => ({ id: x.id, target: x.target, modifiers: back ? x.modifiers : [] }));
- this._take({ where: { portal: { target: r.target, offset, id: r.id, returns }, side: r.target.side, offset: along === null ? 0.5 : along } }, id, before);
}
// Driven, a take through a portal: the helper places the pointer on that display's range first.
@@ -678,6 +684,8 @@ class Kvm extends EventEmitter {
this._setMode('driving', '');
this.input.releaseAll();
const take = { t: P.T.KVM_TAKE, route: this.route, mouse: !!k.mouse && !this.keyboardOnly, keyboard: !!k.keyboard, ...fields.where, ...(via ? { via } : {}) };
+ // Come over any other way: the portals there still lead back here (a take through one carries them in portal)
+ if (!take.portal && target) take.portalReturns = this._portalExits(target);
// Only to a device that takes drags (its drag.policy; the helper picks one up if any device does).
const takes = this.fileSend.peerDragOf(this.partner);
const plan = fields.paths && fields.paths.length && takes ? this.fileSend.planDrag(fields.paths) : null;
@@ -909,6 +917,8 @@ class Kvm extends EventEmitter {
}
this.portalReturn = m._portal && m._portal !== 'none'
? { target: m._portal, id: typeof m.portal.id === 'string' ? m.portal.id : '', returns: this._portalReturns(m.portal.returns, m._portal) } : null;
+ // Come over an ordinary edge (or a shortcut): the driver's portals here are ways back too, beside the edges
+ this.portalExits = this.portalReturn ? [] : this._portalReturns(m.portalReturns, null);
this.partner = from;
this.peerRoute = m.route;
if (m.drag) console.log(`[drag] in: the PC crossed with a drag of ${m.drag.count} item(s)`);
@@ -941,7 +951,7 @@ class Kvm extends EventEmitter {
const out = list.filter((x) => x && typeof x.id === 'string' && Portals.usableTarget(mine, x.target) &&
Array.isArray(x.modifiers) && x.modifiers.every((m) => PORTAL_MODIFIERS.includes(m)))
.map((x) => ({ id: x.id, target: Portals.endpoint(x.target), modifiers: [...new Set(x.modifiers)] }));
- return out.length ? out : [{ id: '', target, modifiers: [] }];
+ return out.length || !target ? out : [{ id: '', target, modifiers: [] }];
}
_armReturn() {
@@ -963,7 +973,9 @@ class Kvm extends EventEmitter {
const { sides, zones } = this.layoutSync.edgeConfig();
// side: the first one, for a helper from before several sides (it watches that one; a newer one reads sides)
this.returnSide = sides[0] || null;
- this.helper.send('armReturn', { on: true, side: sides[0] || null, sides, zones, resistancePx: this.cfg.resistancePx });
+ const exits = (this.portalExits || []).map((x) => ({ ...x.target, modifiers: x.modifiers }));
+ this.helper.send('armReturn', { on: true, side: sides[0] || null, sides, zones, resistancePx: this.cfg.resistancePx,
+ ...(exits.length ? { portals: exits, portalOnly: false } : {}) });
}
// The helper saw the pointer leave this Mac (driven) through msg.side. Back to the driver: kvm.home as
@@ -979,6 +991,12 @@ class Kvm extends EventEmitter {
const other = out && out.id && out.id !== this.portalReturn.id && Number.isFinite(msg.portalOffset);
return this._sendHome(undefined, '', null, other ? { id: out.id, offset: msg.portalOffset } : null);
}
+ // Come over an edge, out through one of the driver's portals: onto that portal's stretch there
+ const exit = Number.isInteger(msg.portal) && this.portalExits ? this.portalExits[msg.portal] : null;
+ if (exit && Number.isFinite(msg.portalOffset)) {
+ if (msg.moving && this.onMoveWindow) this.onMoveWindow(msg, this.partner);
+ return this._sendHome(undefined, '', null, { id: exit.id, offset: msg.portalOffset });
+ }
// An older (one-side) helper names none: it watches the first side we armed. Its offset is ours along that
// side and still needs mapping; sent as it was, the driver read it along its own edge (another monitor).
const side = E.SIDES.includes(msg.side) ? msg.side : this.returnSide || null;
@@ -1143,6 +1161,7 @@ class Kvm extends EventEmitter {
this.helper.send('releaseAll');
this.helper.send('armReturn', { on: false });
this.portalReturn = null;
+ this.portalExits = [];
this._setMode('home', message);
this.clipboard.sendIfChanged('switch');
}
diff --git a/mac/mesh/test/kvm-portals.test.js b/mac/mesh/test/kvm-portals.test.js
index 04f37c2d..9ddd340a 100644
--- a/mac/mesh/test/kvm-portals.test.js
+++ b/mac/mesh/test/kvm-portals.test.js
@@ -193,6 +193,17 @@ test('two portals: in through one, back through the other, the keys only coming
b.helper.emit('event', 'return', { side: 'bottom', offset: 0.3, portal: 0, portalOffset: 0.5 });
await waitFor(() => homes.length === 2, 'home again');
assert.strictEqual(homes[1].portal, undefined);
+
+ // Over an ordinary edge: the portals still lead back, beside the edges.
+ a.kvm._take({ where: { side: 'left', offset: 0.5 } }, B.id);
+ await waitFor(() => b.kvm.mode === 'driven', 'B driven without a portal');
+ const armEdge = b.helper.last('armReturn');
+ assert.strictEqual(armEdge.portalOnly, false);
+ assert.deepStrictEqual(armEdge.portals.map((x) => x.display), ['9', '8']);
+ b.helper.emit('event', 'return', { side: 'bottom', offset: 0.9, portal: 1, portalOffset: 0.5 });
+ await waitFor(() => homes.length === 3, 'home through a portal');
+ assert.deepStrictEqual(homes[2].portal, { id: 'p2', offset: 0.5 });
+ assert.strictEqual(a.helper.last('placePortal').offset, 0.75);
} finally {
close(A, B);
}