In Google remote, the Android receiver receives the IR code from the socket and then simulates the IR code to System processing, which is the principle of the Google remote receiver.
How does the system end simulate input event?
Method One: Through the Instrumentation.sendkeydownupsync implementation, simple to use but the problem is Sendkeydownupsync issued an event, can not run to
Interceptkeybeforedispatching, also can not function Home,vol ...
Method Two: Through the Uinput Bridge, the principle is to use the kernel of the existing Uinput drive, through the kernel-driven uinput to send input event, but also easy to use KL,KCM customer system;
After comparison method Two is superior, the following gives the test code of method two ...
1, the main function, Setup_uinput_device complete the registration of the device, and then create a thread virtualinputdev_eventthread, the thread repeatedly issued keycode;
Copy Code code as follows:
int main ()
{
printf ("Enter process!!!! \ n ");
stvirtualinputdevdata *pkpddata = (stvirtualinputdevdata*) malloc (sizeof ( Stvirtualinputdevdata));
pkpddata->min_keycode = Umin_keycode;
pkpddata->max_keycode = Umax_keycode;
if (Setup_uinput_device (Pkpddata) < 0) {
printf ("Unable to find Uinput device\n");
Free (pkpddata);
return-1;
}
pthread_attr_t attr;
Pthread_attr_init (&ATTR);
Pthread_attr_setdetachstate (&attr, pthread_create_detached);
if (0!= pthread_create (&keypad_eventthreadid, &attr, Virtualinputdev_eventthread, (void *) 0)) {
printf ("Create keypadeventthread failed!! \ n ");
Exit (1);
}
Coverity server need set to ignore this.
while (1) {
Usleep (1000000); Sleep 1 Second
}
Free (pkpddata);
Pkpddata = 0;
Destroy the device
IOCTL (UINP_FD, Ui_dev_destroy);
Close (UINP_FD);
return 0;
}
2, Setup_uinput_device function, complete equipment registration, you can see is directly open the Uinput node, set the virtual device Name,verdor,product,bustype,
Finally through IOCTL (UINP_FD, ui_dev_create) Registration equipment
Copy Code code as follows:
int Setup_uinput_device (stvirtualinputdevdata* mstvirtualinputdevdata)
{
struct Uinput_user_dev uinp; UINPUT Device Structure
int i;
Open the input device
UINP_FD = open ("/dev/uinput", O_wronly | O_ndelay);
if (uinp_fd = = 0) {
printf ("Unable to open/dev/uinput\n");
return-1;
}
Intialize the uinput device to NULL
memset (&UINP, 0x00, sizeof (UINP));
strncpy (Uinp.name, "Virtualinputdev", sizeof (Uinp.name)-1);
Uinp.id.vendor = 0x1341;
Uinp.id.product = 0x0001;
Uinp.id.bustype = bus_virtual;
Keyboard
IOCTL (UINP_FD, Ui_set_evbit, Ev_key);
for (i = mstvirtualinputdevdata->min_keycode; i < mstvirtualinputdevdata->max_keycode; i++) {
IOCTL (UINP_FD, ui_set_keybit, i);
}
Create input device into input sub-system
if (Write (uinp_fd, &UINP, sizeof (UINP))!= sizeof (UINP)) {
printf ("returned fail.\n");
return-1;
}
if (IOCTL (UINP_FD, ui_dev_create)) {
printf ("IOCTL Ui_dev_create returned fail.\n");
return-1;
}
return 1;
}
3, Thread Virtualinputdev_eventthread, just repeat the key, hair key is through Write_event_to_device to complete the
Copy Code code as follows:
Static void* virtualinputdev_eventthread (void *driver_data)
{
unsigned char u8keycode,i=umin_keycode;
while (1) {
U8keycode = 0xFF;
/* Sleep a interval time * *
Usleep (2000000);//sleep 5 S
/* Fill event to uinput device. */
Write_event_to_device (i++, 0);
if (i==4) {
i = 0;
}
printf ("Virtualinputdev thread ... \ n");
I%= Umax_keycode;
}
printf ("Virtualinputdev thread died\n");
Pthread_exit (0);
return 0;
}
4, Write_event_to_device write event to uinput node
Copy Code code as follows:
void Write_event_to_device (unsigned char u8keycode, unsigned char u8repeat)
{
struct Input_event event; INPUT Device Structure
struct TIMESPEC s;
S.tv_nsec = 5000000L;
s.tv_sec = 0;
memset (&event, 0x00, sizeof (event));
Gettimeofday (&event.time, 0);
Event.type = Ev_key;
Event.code = U8keycode;
Event.value = 1;
Write (uinp_fd, &event, sizeof (event));
memset (&event, 0x00, sizeof (event));
Gettimeofday (&event.time, 0);
Event.type = Ev_key;
Event.code = U8keycode;
Event.value = 0;
Write (uinp_fd, &event, sizeof (event));
memset (&event, 0x00, sizeof (event));
Gettimeofday (&event.time, 0);
Event.type = Ev_syn;
Event.code = Syn_report;
Event.value = 0;
Write (uinp_fd, &event, sizeof (event));
}