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.

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. Commented Jan 4, 2017 at 22:29
  • 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. Commented Apr 12, 2017 at 11:39
  • @paldepind 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". Commented Apr 12, 2017 at 19:30
  • Ok. Good luck with the extension if you go ahead with it. Commented Apr 12, 2017 at 19:49
  • I tried something similar using clutter. Ebassi confirmed it´s not possible using clutter events. stackoverflow.com/questions/34947627/… Did you succeed ? Commented Apr 14, 2017 at 16:14

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

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.
@BrianTheLion 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: stackoverflow.com/questions/33911776/…
Nice find! If somebody doesn't come back with a canonical answer in the JS then you get the points.
@BrianTheLion Thanks very much! I'll update my answer.
I'm waiting a bit to see if an authoritative answer comes through.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.