🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.os.linux.programowanie
Article #72569

/dev/uinput

#72569
From: zarafiq
Date: Wed, 17 Oct 2012 14:13
180 lines
5818 bytes
Cześć,

Przepisałem program, który znajduje się w tym PDFie:
http://www.einfochips.com/download/dash_jan_tip.pdf
(przytaczam go na dole) jednak symulowanie klawiatury
działa tylko w terminalu (Ctrl+Alt+F1), w gnome-terminal
nie działa. Myszka też nie działa. Próbowałem też
python-uinput (http://tjjr.fi/sw/python-uinput/),
pod gnome działa myszka, klawiatura nie działa.
Mój OS to Ubuntu 11.04. Czy ktoś miał doświadczenia
z uinput i przezwyciężył podobne trudności?

Pozdrawiam,
zarafiq


// uinput.c
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/* Globals */
static int uinp_fd = -1;
struct uinput_user_dev uinp; // uInput device structure
struct input_event event; // Input device structure

/* Setup the uinput device */
int setup_uinput_device()
{
    // Temporary variable
    int i = 0;

    // Open the input device
    uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
    if (uinp_fd == NULL) {
        printf("Unable to open /dev/uinput\n");
        return -1;
    }
    memset(&uinp,0,sizeof(uinp)); // Intialize the uInput device to NULL

    strncpy(uinp.name, "PolyVision Touch Screen", UINPUT_MAX_NAME_SIZE);
    uinp.id.version = 4;
    uinp.id.bustype = BUS_USB;

    // Setup the uinput device
    ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
    ioctl(uinp_fd, UI_SET_EVBIT, EV_REL);
    ioctl(uinp_fd, UI_SET_RELBIT, REL_X);
    ioctl(uinp_fd, UI_SET_RELBIT, REL_Y);
    for (i=0; i < 256; i++) {
        ioctl(uinp_fd, UI_SET_KEYBIT, i);
    }
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);
    ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);
    /* Create input device into input sub-system */
    write(uinp_fd, &uinp, sizeof(uinp));

    if (ioctl(uinp_fd, UI_DEV_CREATE))
    {
        printf("Unable to create UINPUT device.");
        return -1;
    }
    return 1;
}

void send_click_events( )
{
    // Move pointer to (100, 100) location
    memset(&event, 0, sizeof(event));

    gettimeofday(&event.time, NULL);
    event.type = EV_REL;
    event.code = REL_X;
    event.value = 100;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_REL;
    event.code = REL_Y;
    event.value = 100;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));

    // Report BUTTON CLICK - PRESS event
    memset(&event, 0, sizeof(event));

    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = BTN_LEFT;
    event.value = 1;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));

    // Report BUTTON CLICK - RELEASE event
    memset(&event, 0, sizeof(event));

    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = BTN_LEFT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));
}

void send_a_button()
{
    // Report BUTTON CLICK - PRESS event
    memset(&event, 0, sizeof(event));

    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = KEY_A;
    event.value = 1;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));

    // Report BUTTON CLICK - RELEASE event
    memset(&event, 0, sizeof(event));

    gettimeofday(&event.time, NULL);
    event.type = EV_KEY;
    event.code = KEY_A;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;
    event.value = 0;
    write(uinp_fd, &event, sizeof(event));
}

/* This function will open the uInput device. Please make
sure that you have inserted the uinput.ko into kernel. */
int main() {
    // Return an error if device not found.
    if (setup_uinput_device() < 0) {
        printf("Unable to find uinput device\n");
        return -1;
    }

    sleep(2);
    send_a_button(); // Send a "A" key
    send_click_events(); // Send mouse event

    /* Destroy the input device */
    ioctl(uinp_fd, UI_DEV_DESTROY);

    /* Close the UINPUT device */
    close(uinp_fd);
}
Technical Details
Message-ID: <e29f4d23-e7b2-4349-a526-152e7a960a82@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.cyf-kr.edu.pl!news.nask.pl!news.nask.org.pl!newsfeed.pionier.net.pl!news.glorb.com!l8no3101725qao.0!news-out.google.com!r17ni36921179qap.0!nntp.google.com!l8no3134915qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!news.cyf-kr.edu.pl!news.nask.pl!news.nask.org.pl!newsfeed.pionier.net.pl!news.glorb.com!l8no3101725qao.0!news-out.google.com!r17ni36921179qap.0!nntp.google.com!l8no3134915qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.os.linux.programowanie
Date: Wed, 17 Oct 2012 07:13:09 -0700 (PDT)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=155.136.80.37; posting-account=pDTFQwoAAADFrqDR2c2QL0OAFSvTAzk_
NNTP-Posting-Host: 155.136.80.37
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e29f4d23-e7b2-4349-a526-152e7a960a82@googlegroups.com>
Subject: /dev/uinput
From: zarafiq <dominik.jesiolowski@gmail.com>
Injection-Date: Wed, 17 Oct 2012 14:13:09 +0000
Lines: 182
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit