#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Issues with read and write to dev/events0


    Hello,
    I'm working with C and I'm having difficulty reading/writing to Linux uinput driver.

    I can create the device with no problem. I can write an input event to it as well.

    The problem is if I create a second function that acts as a key listener that uses "fd = open("/dev/input/event0", O_RDWR);" i can no longer write with my other function. If I simply do a read on the device that is open for writing it's very slow.

    I have never programmed C before so I'm sure I'm doing something wrong here. My goal is to create a key listener that will inject other types of events. For example, when you press 'W' it will cause a mouse click at an absolute position.

    Code:
    // pass data from java code to C code concerning: kernel path to input data, keyboard input (bool), or mouse input (bool)
    jint Java_net_pocketmagic_keyinjector_NativeInput_intCreate( JNIEnv* env,jobject thiz, jstring inputdev, jint keyboard, jint mouse, jint touch)
    {
    	
    // ? setup the uinput device
    	jboolean iscopy;
    	char szDev[255] = "";
    	const char *pszDev = (*env)->GetStringUTFChars(env, inputdev, &iscopy);
    	if (pszDev) strncpy(szDev, pszDev, 255);
    	(*env)->ReleaseStringUTFChars(env, inputdev, pszDev);
    	debug("intCreate call (%s)", szDev);
    	
    	struct uinput_dev dev; // linux uinput device structure
    	int fd_kb, aux;
    
    	fd_kb = open(szDev, O_RDWR);
    	if (fd_kb < 0) {
    		debug("Can't open input device:%s ", szDev);
    		return -1;
    	}
    
    	memset(&dev, 0, sizeof(dev)); // ? initialize the linux uinput device to NULL
    	strcpy(dev.name, "AndroidKeyInjector Input");
    	dev.id.bustype = 0x0003; // BUS_USB;
    	dev.id.vendor  = 0x0000;
    	dev.id.product = 0x0000;
    	dev.id.version = 0x0000;
    
    	if (write(fd_kb, &dev, sizeof(dev)) < 0) {
    		debug("Can't write device information");
    		close(fd_kb);
    		return -1;
    	}
    
    	ioctl(fd_kb, UI_DEV_CREATE);
    	debug("intCreate success: %d",  fd_kb);
    	return fd_kb;
    }
    
    void  Java_net_pocketmagic_keyinjector_NativeInput_intClose( JNIEnv* env,jobject thiz, jint fd_kb)
    {
    	close(fd_kb);
    	// ioctl(fd_kb, UI_DEV_DESTROY) testing removal of the uinput device when close is called
    }
    
    // pass data from java code to C code concerning: ???, event type, event code, event value
    void Java_net_pocketmagic_keyinjector_NativeInput_intSendEvent( JNIEnv* env,jobject thiz, int fd_kb, uint16_t type, uint16_t code, int32_t value)
    {
    	debug("intSendEvent call -fd_kb,type,code,value- (%d,%d,%d,%d)", fd_kb, type, code, value);
    	struct uinput_event event;
    	int len;
    
    	if (fd_kb <= fileno(stderr))
    		return;
    
    	memset(&event, 0, sizeof(event)); // initialize uinput event (with nulls?)
    	event.type = type;
    	event.code = code;
    	event.value = value;
    
    	len = write(fd_kb, &event, sizeof(event)); // write the event to the uinput
    	debug("intSendEvent done: len = %d",len);
    }
    
    
    /////////XXXXXXXX
    
    void Java_net_pocketmagic_keyinjector_NativeInput_intListener( JNIEnv* env,jobject thiz, int fd_kb, int on)
    {
    
    	// reads existing file that was opened with intCreate (fd_bk) for writing events; works with writes but the reads are very slow
    	/*
    	int lenx;
        struct input_event ev;
    
    	if (fd_kb <= fileno(stderr))
    		return;
    
        lenx = read(fd_kb, &ev, sizeof(ev));
    
        if(ev.type == 1)
        	debug("listen data :%d,%d", ev.code, ev.value);
    	*/
        
        // reads with this method are very fast but disables event injection with intSendEvent
    	int fd;
    	
        fd = open("/dev/input/event0", O_RDONLY);
        struct input_event ev;
    	read(fd, &ev, sizeof(ev));
        while (on)
        {
        read(fd, &ev, sizeof(ev));
    
        if(ev.type == 1)
        	debug("listen data :%d,%d", ev.code, ev.value);
        }
    
    
    }
    Under intListener I left both the methods I tried. The one that is commented out will read events and write events, but it seems much slower than the other method. This could also be a problem with android, but I thought a C programmer could atleast tell me the correct way to achieve this.

    Pay no attention to the JNI stuff. This is an Android project so I have to do some special stuff so that native code can execute in Android which is Java.

    Thanks!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Maybe it's better to ask this:

    What happens when a second "open" command is used on the same file?

    Should I use the "close" command before exiting the code? Or can I leave it open?

IMN logo majestic logo threadwatch logo seochat tools logo