Can GNOME Shell extensions move the pointer? If so, how?

1.3k views Asked by At

I want to write an extension that does the opposite of the "focus-follows mouse" setting in GNOME Shell: I want to make my pointer move to the center of the currently focused window.

Can this be done in a GNOME Shell extension? I see some GNOME code that wraps xfixes cursor, but I can't find any references to programmatic pointer updates in either the core Javascript or any existing extensions. (Am I just bad at Google?)

Valid answers include (1) example code that does it or (2) citation of a canonical source that says it can't be done.

2

There are 2 answers

6
Joseph Farah On

Are you willing to write your own script? If you are, I have found three tools, which, if used together, can get the job done for you.

First, use xprop to get the PID of the window you have clicked on.

Next, use xwininfo to get the dimensions and position information of the window based on its process ID.

Finally, use xdotool to calculate the center position of said window and move the cursor to that exact position.

Hope this helps. I don't have enough time write now to write the script (sorry), but this should be enough to get you started.

EDIT: Based on your comment, you want to stay in GNOME js. Totally understandable. You can call xdotool (which is the most efficient way of changing the position of the cursor on screen) from within GNOME js by use of something like:

const Util = imports.misc.util;
Util.spawn(['/bin/bash', '-c', "xrandr --query | awk 'something'"]) # replace the code here wih your own

This code was found at this thread.

0
olejorgenb On

Found this code in overview.js

Gdk = imports.gi.Gdk
let display = Gdk.Display.get_default();
let deviceManager = display.get_device_manager();
let pointer = deviceManager.get_client_pointer();
let [screen, pointerX, pointerY] = pointer.get_position();
pointer.warp(screen, 10, 10);