1

How is it possible to grab a device in C (let's say a mouse?) so that only a given process can use it?

For example it would allow the mouse to be used only on the current process.

I'm creation a virtual device and I write on "/dev/uinput" to send mouse movements. How can I restrict this virtual mouse to only one process.

anothertest
  • 979
  • 1
  • 9
  • 24
  • What are you trying to achieve, (suspect XY problem)? – Martin James Oct 23 '15 at 12:54
  • @MartinJames I've created a virtual mouse (uinput). I want to try to grab this mouse and make it unusable for other processes while I grab it with one process. – anothertest Oct 23 '15 at 12:56
  • I'm not a kernel programmer (yet), but in your device `open` call (in the kernel module), you can have a static bool/counter that counts how many times the device has been opened and if it has already been opened (and not closed) just disallow any open requests. – Joel C Oct 23 '15 at 15:50

1 Answers1

2

Are evtest or evemu insufficient for your testing needs? They are in standard repositories in most Linux distributions, and certainly suffice for casual testing.

I have posted some example code here as an answer on how to read barcodes from a specific input event device in Linux. It has timeout support, and grabs the input device (using ioctl(fd, EVIOCGRAB, 1) on the input event device file descriptor), so that the events are not propagated and other processes that try to grab the device fail with EBUSY.

It should not be too difficult to modify it to examine the event structures provided by your device in a way you find comfortable.

Community
  • 1
  • 1
Nominal Animal
  • 38,216
  • 5
  • 59
  • 86