user1511417
user1511417

Reputation: 1940

Can you track more than one mouse with SDL2?

Is it possible to handle two mouses via SDL 2?

On a SDL_MOUSEBUTTONDOWN-event, event.button.which is zero no matter what mouse is used. (I have two mouses connected on my OpenSuse maschine via USB.)

If yes, how can you do it?

Upvotes: 0

Views: 1343

Answers (1)

mindriot
mindriot

Reputation: 5678

The SDL wiki says:

Please note that this ONLY discusses "mice" with the notion of the desktop GUI. You (usually) have one system cursor, and the OS hides the hardware details from you. If you plug in 10 mice, all ten move that one cursor. For many applications and games this is perfect, and this API has served hundreds of SDL programs well since its birth.

It looks like you may be out of luck, at least using plain SDL. Since you are in Linux (and presumably using Xorg), you could have a look into how xinput handles multiple inputs; the source code for testing XI2 events may be particularly useful.

For instance, on my machine xinput shows a whole bunch of devices:

$ xinput
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Logitech USB-PS/2 Optical Mouse           id=11   [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=14   [slave  pointer  (2)]
⎜   ↳ TPPS/2 IBM TrackPoint                     id=16   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ Integrated Camera                         id=10   [slave  keyboard (3)]
    ↳ HID 046a:0011                             id=12   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
    ↳ ThinkPad Extra Buttons                    id=15   [slave  keyboard (3)]

I can then use the test-xi2 command to get events for just one device (e.g. my USB mouse) like this:

$ xinput test-xi2 11
...
EVENT type 17 (RawMotion)
device: 11 (11)
detail: 0
valuators:
flags: 
      0: -5.25 (-3.00)
      1: 12.75 (8.00)
...

This outputs only pointer movements caused by my mouse, but not those caused e.g. by the trackpad. If xinput is an option for you, the source code might get you on the right track to implement your own mechanism.

Upvotes: 2

Related Questions