0

I would like to know a method to know if the mouse cursor is over the title bar or over the content of any application in kde plasma, executing a bash command.

1 Answers1

0

Technically, bash doesn't have a title bar as it is a text terminal application. Presumably you mean the title bar of the terminal window bash is running in. Luckily, it appears that the window ID of that terminal window is available as the environment variable $WINDOWID. (I'm not sure where this comes from, so it might not be portable.)

The command xdotool getwindowfocus will give you the window ID of the window with focus. So, code like the following would determine of the two match:

if [ `xdotool getwindowfocus ` = "$WINDOWID" ]  ; then 
  echo has focus
else
  echo does not have focus
fi

Of course, this doesn't tell you if it is in the title bar, just if the window has focus, which probably means the mouse is in the window at least. (But it might not if you are using click to focus.)

So if you actually want to check the mouse location rather than the window focus, this gets a bit more tricky. The command xdotool getmouselocation tries to get your mouse location. You can compare that against the window location you get from xdotool getwindowgeometry $WINDOWID and xwininfo -id $WINDOWID.

The output from these commands looks like this:

xdotool getmouselocation

x:403 y:196 screen:0 window:21356415

xdotool getwindowgeometry $WINDOWID

  Position: 163,280 (screen: 0)
  Geometry: 496x660

xwininfo -id $WINDOWID (excerpt)

  Absolute upper-left X:  153
  Absolute upper-left Y:  235
  Relative upper-left X:  10
  Relative upper-left Y:  45
  Width: 496
  Height: 660
  Corners:  +153+235  -3191+235  -3191-185  +153-185
  -geometry 80x47+143+190

Using the information from wininfo, it is obvious the mouse X location is within the window. The Mouse Y location is not within the absolute window coordinates. The relative coordinates indicate the window's position within the frame, so the title bar top left corner should actually be 10 pixels to the left and 45 pixels above the terminal's window. The mouse is in that range, so we can guess it is in the window's title bar. (This was tested in Gnome, results in KDE should be similar but not identical.)

Looking at the window geometry information from xdotool, it appears to have added the relative offset information to the window coordinates twice, and there doesn't appear to be a way to query that information with xdotool, so this appears to be buggy and wrong unfortunately.

So that leaves with you a possible method (but not the syntax, sorry) to figure this out.

Having said all that, if you are using Wayland, xdotool getmouselocation may not work. If your terminal is an X11 application running in Wayland, you will get valid mouse location and focus information when the mouse is over a focused terminal window. However, when the mouse is not in an X11 window, or in a Wayland window, the mouse coordinates do not update and will reflect the location when it was last in an X11 window. The getwindowfocus command gives an error (with no results) a Wayland window is focused. If your terminal window is a Wayland application, xdotool will be unable to get any useful information when the mouse is over the terminal window. So, assuming your terminal is an X11 app, it might be possible to combine the information from xdotool and xwininfo to figure it out in Wayland, but this is unreliable at best and will get no results at all at worst.

(Note: the reason this doesn't work in Wayland is because Wayland has decided that providing information like mouse location to all applications is a security risk as it leaks information. There is some justification for this minor inconvenience and not a lot of justification to change it back.)

user10489
  • 4,051
  • I want to thank you for the excellent idea that you have given me, the relative coordinates appear to me in 0, but I am using manually adjusted values to set the limits, so far I see that this is the solution and thank you for sharing your knowledge, I will be finishing the script and then I will tell you how it went. Thank you very much. – Sebastian E. Oct 30 '22 at 18:23
  • What you explained helped me 100% thank you very much. – Sebastian E. Oct 30 '22 at 23:42
  • Let me add that xeyes is a very useful tool for debugging these types of Wayland issues. – user10489 Oct 30 '22 at 23:55
  • Thank's for the information although wayland I think I will use it maybe in the future if it stabilizes, at the moment it is unusable. – Sebastian E. Oct 31 '22 at 01:04