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

2

9

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.

Burkitt answered 4/1, 2017 at 22:27 Comment(6)
For the curious: Why would I want to do this? Because I drive window focus entirely with keyboard shortcuts and am tired of manually having to move my mouse across three displays when, obviously, I want it in the window where I'm currently focused.Burkitt
What happened to this @BrianTheLion? What you describe would be an awesome extension. I really miss this behavior from i3. Also, how do you manage focus with keyboard bindings? I'm currently using the "Put Windows" extension but I find that it is a bit buggy.Llamas
@Llamas I haven't taken any action on this. The answer that JosephFarah provided seems reasonable, but I was hoping that someone from GNOME would chime in with something "official".Burkitt
Ok. Good luck with the extension if you go ahead with it.Llamas
I tried something similar using clutter. Ebassi confirmed it´s not possible using clutter events. #34948127 Did you succeed ?Pillsbury
@ErwanDouaille I've taken no action since posting this question.Burkitt
K
3

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);
Kelvinkelwen answered 21/10, 2017 at 11:14 Comment(0)
O
0

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.

Oaf answered 9/1, 2017 at 1:16 Comment(6)
Thanks for the effort, but I'm really looking for an answer that keeps me in GNOME extension (JS) code. As it stands, the GNOME Shell JS API gives me everything in your solution except the very last part where the mouse actually moves.... which seems weird.Burkitt
@Burkitt sorry I can't help more! Is there any way to call xdotool from within the GNOME shell JS API? That would easily solve your problem. Edit: It looks like you can, based off of this question: #33912276Oaf
Nice find! If somebody doesn't come back with a canonical answer in the JS then you get the points.Burkitt
@Burkitt Thanks very much! I'll update my answer.Oaf
I'm waiting a bit to see if an authoritative answer comes through.Burkitt
@Burkitt not to be rude, but what happened to the bounty? the period ended yesterday.Oaf

© 2022 - 2024 — McMap. All rights reserved.