0
votes

I am trying to read mouse events from the /dev/input/mice file. I am able to parse the 3 byte mouse input for getting the three button states and the increments in X and Y coordinates. However, the mouse input when I scroll up is identical to that when I scroll down. How do I distinguish a scroll up event from a scroll down event? Are there any ioctls that can do any required configuration so that I get different inputs from the mouse on these two events?

The following is a simple program to see the input from a mouse when a mouse event occurs. Scroll up and scroll down events cause the same output to be printed by this program (namely, 8 0 0).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

int main(void) {

    int mouse_fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK);
    signed char input[4];
    ssize_t rd_cnt;

    if(mouse_fd < 0)
    {
        perror("Could not open /dev/input/mice");
        exit(EXIT_FAILURE);
    }

    while(true)
    {
        errno = 0;
        rd_cnt = read(mouse_fd, input, 4);
        if(rd_cnt <= 0 && errno != EAGAIN)
        {
            perror("Mouse read error:");
            exit(EXIT_FAILURE);
        }
        else
        {
            for(int i = 0; i < rd_cnt; i++)
            {
                printf("%d", input[i]);
                if(i == rd_cnt - 1)
                {
                    printf("\n");
                }
                else
                {
                    printf("\t");
                }
            }
        }
    }

    return 0;
}
1
Do we have anything like libinput? One example is a library for X which does all that. You may look into its sources.0andriy
Have you thought about using SDL2 for mouse input?Casey Williams
I recently found out that what I was looking for is how to use the input subsystem to get mouse events. My problem is solved. Thank you for your help anyway.Kenneth Goveas

1 Answers

0
votes

An alternative would be to use SDL2.

I've managed to mash together an example of reading mouse inputs with SDL, so take what you like from it.

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
    if(i<0){
        *p++ = '-';
        i *= -1;
    }
    int shifter = i;
    do{ //Move to where representation ends
        ++p;
        shifter = shifter/10;
    }while(shifter);
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    }while(i);
    return b;
}

int main()
{
    //initialize the window
    bool quit = false;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Mouse Events", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    // the event that will occur when a mouse event happens
    SDL_Event event;
    while(!quit)
    {

        while (SDL_PollEvent(&event)) //returns one is there is a pending event (if an event happens)
        {
            switch(event.type)
            {
                case SDL_QUIT: //if the window is exited
                    quit = true;
                    break;

                case SDL_MOUSEBUTTONDOWN:
                    switch (event.button.button)
                    {
                        case SDL_BUTTON_LEFT:
                            SDL_ShowSimpleMessageBox(0, "Click", "Left button was pressed!", window);
                            break;
                        case SDL_BUTTON_RIGHT:
                            SDL_ShowSimpleMessageBox(0, "Click", "Right button was pressed!", window);
                            break;
                    }
                    break;
                case SDL_MOUSEWHEEL:
                    if(event.wheel.y == -1) //negative means the scroll wheel has gone away from the user
                    {
                        SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled away from yourself!", window);
                    } else if (event.wheel.y == 1) //vice-versa
                    {
                        SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled towards yourself!", window);
                    }

            }

        }

        //do some SDL cleanup
        SDL_Rect dstrect = { 288, 208, 64, 64 };

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

}

The event.wheel type can be found here: https://wiki.libsdl.org/SDL_MouseWheelEvent

Hope this is of some use to you!

If you don't want to use SDL2, it may be worth have a look in the source of the library to see what it's doing.