How to tell when a 'virtual' file changes, or when data moves through a /dev/ file?
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.
How to tell when a 'virtual' file changes, or when data moves through a /dev/ file?
I'm making an embedded linux thing and I want to detect simple digital input to detect an event. I thought it would be cool just to use a USB mouse. All I have to do is poll for mouse clicks.
I can't find a nice simple commandline program for polling mouse button states, so I figured I could just watch /dev/mouse. If I cat /dev/mouse, and I move the mouse around or click it, my screen fills up with mojibake, so I know there is data coming in. I just don't know how to actually use this to poll the mouse with perl or bash, because /dev/mouse is not a normal file.
I want to look at /dev/mouse and detect when any data comes through it. I don't care what the data is.
I don't know much about pipes, but would this work: I could make a named pipe and redirect its output to a temp file. Then, when I run my perl program, I can just test for the existence of the temporary file. If it exists, there has been a mouse click. Then I can just rm the temp file with my perl program or bash and poll again. The temp file will be recreated whenever the mouse is clicked. Would this work? Is there a better way?
You really can't expect to do it that way. The window manager is going to be dealing with those mouse clicks, and your software can register with the WM (whichever one it may be) to receive them.
It's already working, in that I can cat /dev/mouse and get the data. I just need a programmatic way to register when data comes through, and I can't think of a smart way to do that, even using scratch files.
If I can direct /dev/mouse to a file, I can just test the file for length. If it's nonzero, there has been a mouse click. Then I can rm the file and re-touch it.
But there has no be a correct way to simply poll the mouse button at the kernel driver or libusb level.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.