ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am currently playing with a USB mouse on an embedded version of Linux.
Is it possible to be notified when a USB mouse is plugged in?
The kernel has generic support for it, and I see some debug when the mouse is plugged in, but I was hoping for a more elegant solution than just polling the /dev/mouse device.
Do you have udev set up? You can have it execute a command on a certain peripheral event. I use it to mount partitions and images when USB drives are plugged in. Look here: http://reactivated.net/writing_udev_rules.html
ta0kira
Thanks. But I do not see how to udev. udev will create device nodes, but I am more looking for a coding solution. How do I detect the USB mouse within code, rather than getting a nice device node name?
In addition to creating a device, udev can execute a script. If you want your script to detect the connection rather than having it executed when the device is connected, have udev run a script that creates some indication that the device is connected which your script will check for. The same script can remove that indication when the device is no longer connected.
ta0kira
Ok. But my programme is not a script. The only way I can think is that the script would send some sort of signal (e.g. SIGUSER1) and have the programme pickit up. Can scripts do this?
Yes. You need to know the PID or the name it's called under. With the PID do kill -s SIGUSR1<pid> and with the script name do pkill -SIGUSR1<name>. You must be careful to name the program something unique which won't be a part of something else's name. For example, "server" would be a good name to avoid.
ta0kira
A better way to go with pkill would be pkill -SIGUSR1 ^<name>$. That will force only full-name matches and will send the signal to all processes under that name. I use pkill/pgrep constantly and they are very useful. You should also really check out that udev site. It's more versatile than you think.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.