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.

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
BrianTheLion
  • 2,618
  • 2
  • 29
  • 46
  • 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. – BrianTheLion Jan 04 '17 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. – paldepind Apr 12 '17 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". – BrianTheLion Apr 12 '17 at 19:30
  • Ok. Good luck with the extension if you go ahead with it. – paldepind Apr 12 '17 at 19:49
  • I tried something similar using clutter. Ebassi confirmed it´s not possible using clutter events. http://stackoverflow.com/questions/34947627/how-to-create-clutter-events-with-gjs Did you succeed ? – Erwan Douaille Apr 14 '17 at 16:14
  • @ErwanDouaille I've taken no action since posting this question. – BrianTheLion Apr 14 '17 at 20:50

2 Answers2

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);
olejorgenb
  • 1,203
  • 14
  • 25
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.

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
  • 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 Jan 09 '17 at 01:43
  • @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: https://stackoverflow.com/questions/33911776/gnome-shell-extensions-how-to-run-a-command-with-pipes – Joseph Farah Jan 09 '17 at 01:48
  • Nice find! If somebody doesn't come back with a canonical answer in the JS then you get the points. – BrianTheLion Jan 09 '17 at 01:51
  • @BrianTheLion Thanks very much! I'll update my answer. – Joseph Farah Jan 09 '17 at 01:52
  • I'm waiting a bit to see if an authoritative answer comes through. – BrianTheLion Jan 09 '17 at 21:10
  • @BrianTheLion not to be rude, but what happened to the bounty? the period ended yesterday. – Joseph Farah Jan 17 '17 at 23:46