Find the answer to your Linux question:
Results 1 to 9 of 9
I want to record and play mouse events on my Linux machine. Basically i store all the mouse events entered by the user into a text file. For this, i ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15

    Wink How to convert relative mouse co-ordinates to absolute co-ordinates?

    I want to record and play mouse events on my Linux machine.
    Basically i store all the mouse events entered by the user into a text file. For this, i am simply reading the device file: /dev/input/event1 using read() and storing the events into a text file. While playback i read() the text file and write() the events to the device file /dev/input/event1.


    Here's my code:

    Recording


    FILE *deviceConnection,*record_file;
    struct input_event myEvent;

    deviceConnection = fopen(argv[1], "rw");
    record_file = fopen(argv[2], "rw+");

    while (1) {
    printf(".");
    // read from the device file into the input_event structure */
    fread(&myEvent, sizeof(struct input_event), 1, deviceConnection);
    // Write the events to the text file */
    fwrite(&myEvent, sizeof(struct input_event), 1, record_file);
    fflush(record_file);
    }
    fclose(deviceConnection);
    fclose(record_file);
    Playback

    FILE *record_file;
    struct input_event myEvent;
    char filename[128] ;
    int devHandle = -1;

    devHandle = open(argv[1], O_RDWR);
    record_file = fopen(argv[2], "r+");

    while (!feof(record_file))
    {
    fread(&myEvent, sizeof(struct input_event), 1, record_file);
    if (myEvent.value != 0) {
    printf ("We are in Key Press \n");
    write(devHandle, &(myEvent), sizeof(struct input_event);
    }
    }

    close (devHandle);
    fclose(record_file);
    Now, i am able to record and play the mouse events but while playback the mouse events are played relative to the current mouse position. This is because a standard mouse supports only relative events (A mouse supports key events: EV_KEY and Relative events: EV_REL).

    Note: Absolute events (EV_ABS) are supported by pointing devices like joysticks etc.

    Now, i want to get absolute mouse co-ordinates so that the mouse events can be played accurately irrespective of the current mouse position.

    How can i achieve this? Can i convert the relative mouse position to absolute position?

  2. #2
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    I think to solve this problem i only need to save the exact mouse pointer position at the time when recording is started. Then i can simply record the relative events as i am doing now and compute the absolute co-ordinates by adding them to the initial mouse pointer position.

    The problem is how can i get the exact initial mouse co-ordinates when the recording is started...

    Any ideas?

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    As you note, there are no absolute mouse coordinates. Absolute coordinates are associated with not the mouse, but the on-screen visible pointer.

    If your program is not interested in the on-screen visible pointer, then the concept of absolute mouse coordinates is essentially meaningless. If your program is interested in the on-screen visible pointer, then you'll have to find some way of accessing that; it's basically a purely software construct.

    You're not interested by any chance in the normal UNIX/Linux X Windows environment, are you? Because if you are, then three things are true.
    1. With proper programming, you'll be able to get the absolute coordinates of the mouse at any time.
    2. It won't be through reading /dev/anything.
    3. The yellow brick road you are about to embark on is a long, arduous yellow brick road (but not rocket science).

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    Thanks for the reply!

    Basically i want to record and playback mouse events. In addition to the mouse i also want to record and play touchscreen events. So basically i am interested in the on-screen visible pointer.

    And you are right, i don't want to use the X Windows environment because my application will be running on an embedded linux machine. I can't use the X11 APIs, so, i am using the low level /dev stuff. I know it's possible to get the mouse pointer position thru X11 APIs.

    When i do the playback, the playback is done according to the current mouse position and not the exact absolute position which i had while recording.


    Now, how should i proceed with this?

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    basically i am interested in the on-screen visible pointer
    If you're using some sort of embedded system which has an on-screen visible pointer, you'll need to find way to access where that system's software has placed the pointer. Otherwise, there's no hope, because the location of the on-screen visible pointer is (normally) a purely software construct.

    Once you've done that, you can probably just monitor that to determine the absolute coordinates, rather than read the mouse device directly. Another advantage of this approach is that the software which maintains the on-screen visible pointer's location will probably no longer work correctly if you (rather than that software) read the mouse input.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    thanks for the quick reply.

    I'll try to investigate about the points you have mentioned about the embedded system.

    Actually we are using a customized uinput device (http://svn.navi.cx/misc/trunk/inputp...put/uinput.txt, http://www.einfochips.com/download/dash_jan_tip.pdf) for getting the touchscreen events from the device. We want to record and playback keyboard, mouse and touchscreen events. All the touchscreen events are delivered to /dev/input/mice and in order to play it back (simulate), we need to pass the events to /dev/uinput.

    I think this problem is coming only with a standard mouse. This is because a standard mouse doesn't support absolute events.

    Now, if i want to record and play the touchscreen events, the code should work fine since touchscreen device supports absolute coordinates. Isn't it?

    In the end, we might not use mouse at all and only use keyboard and touchscreen. Then my code should work fine, isn't it?

    Actually i do not have the hardware with me yet, so i am not able to test the code with touchscreen events.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Now, if i want to record and play the touchscreen events, the code should work fine since touchscreen device supports absolute coordinates, shouldn't it?

    In the end, we might not use mouse at all and only use keyboard and touchscreen. Then my code should work fine, shouldn't it?
    Yes. I think you won't have a problem. If you want to include a mouse, though, you'll have the problem you stated originally, and you'll have to find a way to solve it, pretty much as I mentioned.

    Good luck!
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    hey thanks for ur help bro

  9. #9
    Just Joined!
    Join Date
    Jan 2009
    Posts
    1

    Any luck montylee?

    montylee, I am looking for similar functionality for test on Linux based embedded systems. Were you able to get it working? Is the code available for download?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...