Reading raw device data

This is a discussion on Reading raw device data within the Linux Programming forums, part of the Platform Specific Boards category; Greetings, I know you can read raw data from devices in linux... But is it first in best dressed? For ...

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Reading raw device data

    Greetings,

    I know you can read raw data from devices in linux... But is it first in best dressed?

    For example, dump.txt remains blank in the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE * device, * out;
    	int cc;
    	
    	if((device = fopen("/dev/input/mouse0", "rb")) == NULL)
    	{
    		perror("Failed to open the device");
    		return 1;
    	}
    
    	if((out = fopen("dump.txt", "wb")) == NULL)
    	{
    		perror("Failed to open dump.txt");
    		fclose(device);
    		return 1;
    	}
    
    	while(1)
    	{
    		cc = fgetc(device);
    		fputc(cc, out);
    	}
    
    	fclose(device);
    	fclose(out);
    
    	return 0;
    }
    I know my mouse is in use by X but, should I be able to read the raw data too?

    What am I doing wrong?

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    So does your program lock up in the infinite loop, or does it exit with an error message?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Nah it locks... I just used while(1) for testing...

    I've also tried
    Code:
    while((cc = fgetc(device)) != EOF)
    	fputc(cc, out);
    To no avail.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Do you know for sure whether it is looping or not?

    Try capturing say the first 10 characters and then exiting.

    You're not likely to ever get EOF from /dev/mouse.
    Also, internal buffering may be holding on to a lot of data unless you flush the output file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    This isn't going to work. Although two processes can open a single device, the bytes only ever go to one place. This means that between your process and the X server, it's a tossup who gets the next byte. If you get the byte, the X server does not, and vice versa.

    You need to find a better way to do whatever you are doing. More details would help.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Hmm, Thought so - I read somewhere that it was FIFO, First in First Out or something. Hence my question about whether it matters because X is also reading the file.

    I'm just trying to read raw mouse data... easily for that matter.

    I've tried connecting 2 mice, but X reads both as one (/dev/input/mouse0 and /dev/input/mouse1).

    Thanks for the help tho

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Are you people quite sure about this? I can use od to dump raw mouse/keyboard data from /dev/input/mouse*, /dev/input/mice and /dev/input/event* quite easily, without interfering with X's operation at all.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Maybe it uses open() rather than fopen() ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    By shutting down X I was able to read from /dev/input/mouse0 (a lot of data, using fopen()).

    However, using open() and read() worked flawlessly (both X and I get the bytes). Thanks everyone
    Last edited by zacs7; 05-09-2007 at 05:50 AM.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by CornedBee View Post
    Are you people quite sure about this? I can use od to dump raw mouse/keyboard data from /dev/input/mouse*, /dev/input/mice and /dev/input/event* quite easily, without interfering with X's operation at all.
    X uses hardware cursor acceleration, so you wouldn't notice any effect on mouse movement.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by brewbuck View Post
    X uses hardware cursor acceleration, so you wouldn't notice any effect on mouse movement.
    I don't get it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  3. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  4. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21