# debugging with SDL\_CaptureMouse

**URL:** <https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014>\
**Category:** Game Development\
**Created:** [March 23, 2021, 7:41am UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014 "2021-03-23T07:41:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![audetto](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@audetto](https://discourse.libsdl.org/u/audetto)\
**Post date:** [March 23, 2021, 7:41am UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/1 "2021-03-23T07:41:21Z")

</div>

Hi

I have been debugging an app the uses SDL\_CaptureMouse.  
If a breakpoint is hit while SDL\_CaptureMouse is active, the mouse is not released and any interaction with the debugger gui or the rest of the desktop is extremely hard.

I am running on Ubuntu 20.10, GNOME, X11.  
I see that this functions eventually calls `X11_XGrabPointer`.

I think I can explain this behaviour “easily”, but I wonder what people have done so far to debug this type of applications.

Could / should gdb tell X11 that the app is being debugged and so the mouse pointer released, and re-acquired later?

Is there another explanation / solution?

---

<div class="post-metadata">

**Author:** ![brada](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@brada](https://discourse.libsdl.org/u/brada)\
**Post date:** [March 23, 2021, 11:29pm UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/2 "2021-03-23T23:29:27Z")

</div>

I think most people, myself included, use remote debugging in cases like this. Most often that means ssh and command line lldb/gdb, but you can run a debug server and connect to it from GUI tools as well.

I think if you think on it you will see why expecting gdb or any other debugger to do anything about this is impractical and would likely cause difficult to debug scenarios involving the location of the mouse.

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 24, 2021, 12:27am UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/3 "2021-03-24T00:27:36Z")

</div>

I was just gonna write an answer, but turns out, I already did =)  
See: [How to release the mouse when debugging in gdb](https://discourse.libsdl.org/t/how-to-release-the-mouse-when-debugging-in-gdb/21275)

The mentioned python script (which is not accessible anymore) looked like:

```python
#!/usr/bin/python
# release mouse when breaking into debugger for SDL2 programs
# by KittyCat, some comments + gdb.write() messages by Daniel Gibson
# put this next to the executable you wanna debug and name it
# <YourExecutableName>-gdb.py
# Make sure that it's in a subdir of an allowed GDB auto-loading safe-path,
# see https://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
# e.g. by executing in a terminal:
# $ echo "add-auto-load-safe-path /path/to/project" >> ~/.gdbinit

def release_mouse (event):
        gdb.write("GDB/SDL2: Releasing mouse\n")
        gdb.execute("call SDL_SetRelativeMouseMode(0)")
        
        # TODO: if you use SDL_SetWindowGrab(), you may have to write
        # function with no arguments to call it with your SDL_Window*
        # handle and call this as well, like:
        #gdb.execute("call DEBUG_UngrabMouse()")
        
        # TODO: the following doesn't seem to work, maybe it does for you.
        # either way, it needs xdotool installed.
        #gdb.execute("exec xdotool key XF86Ungrab")
        
 
gdb.events.stop.connect(release_mouse)

gdb.write("GDB/SDL2: installed release mouse for SDL2\n")

```

By the way, nowadays for the `SDL_SetWindowGrab()` case  
`gdb.execute("call SDL_SetWindowGrab(SDL_GetGrabbedWindow(), 0)")`  
_might_ work, but this is completely untested.

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 24, 2021, 1:12am UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/4 "2021-03-24T01:12:32Z")

</div>

I investigated that commented out `xdotool key XF86Ungrab` thing a bit, and it turns out that it doesn’t work because it’s disabled by default, because apparently it allows bypassing screensavers/screenlockers (and as far as I know X11 fixed this by disabling it by default instead of providing an API for screensavers to disable it while they’re locked…)

Anyway, it might work to, when htting a breakpoing, enable XF86Ungrab, send that key event with xdotool, disable it again. Would probably look like:

```python
#!/usr/bin/python
def release_mouse (event):
    # allow XF86Ungrab
    gdb.execute("setxkbmap -option grab:break_actions")
    # send that key to ungrab the cursor
    gdb.execute("exec xdotool key XF86Ungrab") 
    # this should reset xkbmap options to default (disallow XF86Ungrab again)
    gdb.execute("setxkbmap -option")

gdb.events.stop.connect(release_mouse)
gdb.write("GDB/X11: installed release mouse for X11\n")

```

Note that you still need xdotool installed and allow GDB auto-loading etc.

Also note that I didn’t test this yet 😛

Some information on this: [Who-T: XKB breaking grabs - CVE-2012-0064](http://who-t.blogspot.com/2012/01/xkb-breaking-grabs-cve-2012-0064.html)  
[An Easy But Serious Screensaver Security Problem In X.Org - Phoronix](https://www.phoronix.com/scan.php?page=news_item&px=MTA0NTA)  
[https://unix.stackexchange.com/a/40472](https://unix.stackexchange.com/a/40472)

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [March 25, 2021, 6:59pm UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/5 "2021-03-25T18:59:57Z")

</div>

Also, just want to chime in and say that SDL\_CaptureMouse is meant for _extremely_ short grabs that _need to track the mouse outside the window_…the use-case is you’re building a GUI toolkit on top of SDL and need to deal with things like dragging windows to resize them and size (indeed, this function was added so Unreal Engine 4 could do that very thing).

So most cases you shouldn’t need to be using this function to have to debug around it.

(you might have a perfectly valid reason, I’m just leaving this note for future readers.)

---

<div class="post-metadata">

**Author:** ![audetto](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/958977/32.png) [@audetto](https://discourse.libsdl.org/u/audetto)\
**Post date:** [March 25, 2021, 7:07pm UTC](https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014/6 "2021-03-25T19:07:57Z")

</div>

You hit a valid point.  
The usage comes form ImGui which is a GUI toolkit.

I am not able to tell you why they have decided to use it, nor which problems they were trying to solve.
