1

How do I query (from code) the current icon/state of the mouse?

enter image description here

The mouse could be hovering over a link in a browser, or showing an I-beam from a terminal.
I don't care about the actual position of the mouse, or what application it is on-- just its visual state.

How can I query ubuntu for what my mouse currently looks like?

I'm on the following version of Ubuntu:

lsb_release --all
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:    16.04
Codename:   xenial

using gnome flashback:

$echo $XDG_CURRENT_DESKTOP
GNOME-Flashback:Unity
$echo $GDMSESSION
gnome-flashback-metacity

(not sure if any of that is relevant).

I was going to take a 32x32 screenshot starting from my mouse location, and then do a basic image recognition to see what the state is, but that doesn't work either! When you take a screenshot (with say, gnome-screenshot, or shutter), it automatically shows the mouse in its normal icon state despite its icon state when the screenshot is taken.

1 Answer 1

1

Known Bug

It seems to be a known bug:

https://bugs.launchpad.net/ubuntu/+source/gnome-screenshot/+bug/659399
https://bugzilla.gnome.org/show_bug.cgi?id=571602

According to the thread we need to install the package gnome-utils which actually doesn't help.


Solution

Using shutter and xdotool command line, I have been able to get a working solution:

  1. The script:
    #!/bin/bash

    # Get current cursor position using xdotool and eval them as variables
    eval $(xdotool getmouselocation --shell)
    
    # We need the X and Y coordinates. Minus 10 pixels to create some padding around the cursor
    xc=$(($X-10))
    yc=$(($Y-10))

    # Use shutter to capture a screenshot of the area around the cursor 32px by 32px and save it in the current directory
    shutter -s=$xc,$yc,32,32 -c -e -o './%y-%m-%d_$w_$h_$RRRR.png'
  1. This took the screenshot but using the default cursor pointer. BUT it gave me an error message:

    WARNING: XFIXES extension not found - using a default cursor image
    
  2. With that error, I had a direction. I quickly did an apt-cache search xfixes and this came up:

    subroot@subroot:~$ apt-cache search xfixes
    libxcb-xfixes0 - X C Binding, xfixes extension
    libxcb-xfixes0-dbg - X C Binding, xfixes extension, debugging symbols
    libxcb-xfixes0-dev - X C Binding, xfixes extension, development files
    libxfixes-dev - X11 miscellaneous 'fixes' extension library (development headers)
    libxfixes3 - X11 miscellaneous 'fixes' extension library
    libxfixes3-dbg - X11 miscellaneous 'fixes' extension library (debug package)
    libx11-protocol-other-perl - miscellaneous X11::Protocol helpers
    subroot@subroot:~$
    
  3. I just installed everything:

    subroot@subroot:~$ sudo apt install libxcb-xfixes0 libxfixes3 libxcb-xfixes0-dbg libxfixes-dev libxfixes3-dbg libxcb-xfixes0-dev libx11-protocol-other-perl
    . . .
    subroot@subroot:~$
    
  4. I then tried again and it worked: :)

    subroot@subroot:~$ sh cursor.sh
    

The result:

enter image description here enter image description here

Just play around with the dimensions, and screenshot size to get the perfect cursor pointer image. Cheers.

1
  • Thanks. I came up with a very similar solution using ffmpeg to capture a single 32x32 frame based on xdotool's reported mouse location. Then used image magic to dump a particular pixel color which is different among the mouse states I care about. It's unfortunate that there isn't a direct API call like there is in Windows, but this will do. Oct 5, 2017 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .