[glib] Simple program to catch keyboard, mouse events.



Hi all.

I'm trying to write simple program to read events from usb keyboard and
usb mouse in such manner:

// I counted number of bytes per event by with command
// sudo cat /dev/input/mouse0 | od -c
#define MOUSE_EVENT_SIZE 16
gboolean cb_mouse(GIOChannel *source, GIOCondition condition, gpointer
data)
{
    gchar ev[MOUSE_EVENT_SIZE];
    gsize bytes_read;

    g_io_channel_read_chars(source, ev, MOUSE_EVENT_SIZE, &bytes_read,
nullptr);
    cout << "event1\n";
}

// Counted in the same manner as for mouse0
#define KEYBOARD_PRESS_SIZE 72
gboolean cb2(GIOChannel *source, GIOCondition condition, gpointer data)
{
    gchar ev[KEYBOARD_PRESS_SIZE];
    gsize bytes_read;

    g_io_channel_read_chars(source, (gchar *)&ev, KEYBOARD_PRESS_SIZE,
&bytes_read, nullptr);
    cout << "event2\n";
}

int main()
{
    int fd1, fd2;
    GIOChannel *c1, *c2;

    c1 = g_io_channel_new_file("/dev/input/mouse0", "r", nullptr);
    c2 = g_io_channel_new_file("/dev/input/event11", "r", nullptr);

    g_io_channel_set_encoding(c1, NULL, NULL);
    g_io_channel_set_encoding(c2, NULL, NULL);

    g_io_add_watch_full(c1, G_PRIORITY_HIGH, G_IO_IN, cb1, NULL, NULL);
    g_io_add_watch_full(c2, G_PRIORITY_HIGH, G_IO_IN, cb2, NULL, NULL);

    GMainLoop *loop = g_main_loop_new(nullptr, false);

    g_main_loop_run(loop);

    // ... cleanup
}

When I'm launching the program with activate single channel (mouse or
keyboard) all works well.

There are troubles for two and more channels.

What I'm doing wrong?

PS: My target is to write program with >10 of such channels each channel
opens a file and read the data from it. All channels must work perfect
in context of one main loop.

Thanks.

________________________________

This e-mail and any attachment(s) are intended only for the recipient(s) named above and others who have been 
specifically authorized to receive them. They may contain confidential information. If you are not the 
intended recipient, please do not read this email or its attachment(s). Furthermore, you are hereby notified 
that any dissemination, distribution or copying of this e-mail and any attachment(s) is strictly prohibited. 
If you have received this e-mail in error, please immediately notify the sender by replying to this e-mail 
and then delete this e-mail and any attachment(s) or copies thereof from your system. Thank you.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]