LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2012, 03:40 PM   #1
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Rep: Reputation: 15
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?

Last edited by Daravon; 08-14-2012 at 03:53 PM.
 
Old 08-14-2012, 04:28 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 08-14-2012, 08:10 PM   #3
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
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.
 
Old 08-14-2012, 10:48 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Test if select/poll works on /dev/mouse.
 
Old 08-15-2012, 08:26 AM   #5
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
I might try select/poll yet, but right now, this works!

Code:
#include <stdio.h>
main(){
    printf("%s", "Hello World\n");
    int dat;
    FILE *mouse;
    mouse = fopen("/dev/input/mice", "r");
    //mouse = fopen("temp", "rb");
    dat = fgetc(mouse);
    printf("%i", dat);
    return (fclose(mouse));
}
This is pretty much what I need, since I just need to detect any activity on the mouse.
~
 
Old 08-15-2012, 08:55 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would this help:
Code:
cat /dev/mouse | od # man od
i do this sometimes to examine the output of /dev/dsp.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create File Shell Syntax and pass data to the file FirstBorn Linux - General 22 07-31-2008 09:48 PM
Retrieving semi-formatted data: Kernel Panic (cannot find file or dir /dev/root) majorGrey Linux - General 2 09-05-2007 04:02 AM
[SOLVED] system disk moves from /dev/sda to /dev/sdm gdevaucorbeil Linux - Hardware 4 03-10-2007 09:30 AM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration