Gippeumi
Gippeumi

Reputation: 261

(Linux) How to get initial position of mouse or set cursor position without x in c?

I am going to get Mouse Position in Console.

But x/y is about movement. So I have to calculate position. There's an way to get with X, but I am using framebuffer in my program, so I want how to get without X.

I got movement with this code.

#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#define MOUSEDEV "/dev/input/event0"
//#pragma pack(1)

int filedesc;

int x, y;

void readm(){
    struct input_event in;
    read(filedesc, &in, sizeof(struct input_event)); /* there was data to read */
    if(in.type == 3)
    printf("Input: Time: %d.%d Type: %d Code: %d Value: %d\n", in.time.tv_sec, in.time.tv_usec, in.type, in.code, in.value);
    usleep(1000);
}

int main(){
    filedesc = open(MOUSEDEV, O_RDWR );
    while(1) readm();
    return 0;
}

Upvotes: 0

Views: 1349

Answers (2)

By definition, a mouse gives relative movements (this is the way the mouse hardware works), not absolute position.

Use (or at least study) the GPM software to use the mouse outside of X.

Upvotes: 1

Joshua Clayton
Joshua Clayton

Reputation: 1729

A mouse tells you nothing except relative position in unspecified units.

You don't need to know.

You just need to decide on a starting point and track position from there.

Upvotes: 1

Related Questions