Skip to content

Instantly share code, notes, and snippets.

@IlanFrumer
Created June 29, 2021 10:32
Show Gist options
  • Save IlanFrumer/abe58cd20966cd40adcfc6f7e8717cf8 to your computer and use it in GitHub Desktop.
Save IlanFrumer/abe58cd20966cd40adcfc6f7e8717cf8 to your computer and use it in GitHub Desktop.
MouseTab - Tab the mouse pointer netween monitors
#!/usr/bin/node
// @ts-check
import { execSync } from "child_process";
function getScreens() {
const screen_pattern = "([0-9]+)x([0-9]+)\\+([0-9]+)\\+([0-9]+)";
const screen_regexp = new RegExp(screen_pattern, "gm");
const screen_lines = execSync(
`xrandr --current | grep -oP "${screen_pattern}"`,
{
encoding: "utf-8",
}
).trim();
/**
* @type {{x:number;y:number;w:number;h:number}[]}
*/
const screens = [];
/**
* @type {RegExpExecArray}
*/
let match;
while ((match = screen_regexp.exec(screen_lines))) {
const w = +match[1];
const h = +match[2];
const x = +match[3];
const y = +match[4];
screens.push({ x, y, w, h });
}
screens.sort((a, b) => a.x - b.x || a.y - b.y);
return screens;
}
function getMouseLocation() {
const pointer_pattern = "x:([0-9]+) y:([0-9]+)";
const pointer_regexp = new RegExp(pointer_pattern);
const pointer = execSync(
`xdotool getmouselocation | grep -oP "${pointer_pattern}"`,
{
encoding: "utf-8",
}
).trim();
const match = pointer.match(pointer_regexp);
const x = +match[1];
const y = +match[2];
return { x, y };
}
function main() {
const screens = getScreens();
const total = screens.length;
const { x, y } = getMouseLocation();
const index = screens.findIndex(
(scr) => scr.x <= x && scr.x + scr.w > x && scr.y <= y && scr.y + scr.h > y
);
const active = screens[index];
const rx = (x - active.x) / active.w;
const ry = (y - active.y) / active.h;
const next = screens[index + 1 >= total ? 0 : index + 1];
const nx = Math.floor(next.x + next.w * rx);
const ny = Math.floor(next.y + next.h * ry);
if (next) {
const cursor = `find-cursor --size 200 --distance 10 --wait 80 --line-width 3 --color red -f`;
execSync(`xdotool mousemove ${nx} ${ny} && ${cursor}`);
}
}
main();
@IlanFrumer
Copy link
Author

Install Dependencies

yay -S xdotool xorg-xrandr find-cursor --noconfirm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment