#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int _keyfd[2] = {0};
static int number = 0;
static int _maxfd = 0;
#define INPUT_DEVICE_MAX 2
#define KEY_TYPE_RELEASE 0
#define KEY_TYPE_PRESS 1
#define KEY_TYPE_REPEAT 2
#define KEY_LONGPRESS_SEC 1
#define KEY_LONGPRESS_USEC (50 *1000)
int key_long_press(int keycode)
{
switch(keycode)
{
case KEY_VOLUMEDOWN:
{
printf("long press KEY_VOLUMEDOWN
");
}
break;
case KEY_VOLUMEUP:
{
printf("long press KEY_VOLUMEUP
");
}
break;
case KEY_HOME:
{
printf("long press KEY_HOME
");
}
break;
case KEY_MUTE:
{
printf("long press KEY_MUTE
");
}
break;
default:
break;
}
return 0;
}
int key_normal_press(int keycode)
{
switch(keycode)
{
case KEY_VOLUMEDOWN:
{
printf("normal press KEY_VOLUMEDOWN
");
}
break;
case KEY_VOLUMEUP:
{
printf("normal press KEY_VOLUMEUP
");
}
break;
case KEY_HOME:
{
printf("normal press KEY_HOME
");
}
break;
case KEY_MUTE:
{
printf("normal press KEY_MUTE
");
}
break;
default:
break;
}
return 0;
}
int main(int argc, char **argv)
{
char name[128] = {0};
int fd = -1;
int i = 0;
uint8_t is_longpress=0;
struct timeval keypress_timeval;
struct input_event event;
size_t num;
fd_set key_fd_set;
for (i=0; i < INPUT_DEVICE_MAX; i++)
{
memset(name, 0, sizeof(name));
snprintf(name, sizeof(name), "/dev/input/event%d", i);
if((fd = open(name, O_RDONLY, 0666)) >= 0) {
int version;
char buffer[256] = {0};
ioctl(fd, EVIOCGVERSION, &version);
ioctl(fd, EVIOCGNAME(sizeof(buffer)), buffer);
printf("====================================================
");
printf(" input name %s
", buffer);
printf(" input version %d.%d.%d
", ((version >> 16) & 0xff), ((version >> 8) & 0xff) , ((version) & 0xff));
_keyfd[number] = fd;
number++;
if (fd > _maxfd)
_maxfd = fd;
}
}
while(1)
{
FD_ZERO(&key_fd_set);
for(num = 0; num < number; num++)
{
FD_SET(_keyfd[num], &key_fd_set);
}
int ret = select(_maxfd + 1, &key_fd_set, NULL, NULL, NULL);
if(ret < 0){
break;
} else if (ret == 0){
continue;
}
for(i=0; i < number; i++){
if(FD_ISSET(_keyfd[i], &key_fd_set))
{
if(read(_keyfd[i], &event, sizeof(event)) > 0)
{
switch(event.type)
{
case EV_KEY:
{
int keycode = event.code & 0xff;
int keyvalue = event.value;
struct timeval time = event.time;
printf("keycode:%d, keyvalue:%d, tv_sec:%d, tv_usec:%d
", keycode, keyvalue, time.tv_sec, time.tv_usec);
if(KEY_TYPE_PRESS == keyvalue)
{
keypress_timeval = event.time;
} else if (KEY_TYPE_REPEAT == keyvalue) {
if((time.tv_sec > keypress_timeval.tv_sec + KEY_LONGPRESS_SEC)
&&(time.tv_usec > keypress_timeval.tv_usec + KEY_LONGPRESS_USEC))
{
is_longpress = 1;
keypress_timeval = event.time;
key_long_press(keycode);
}
} else if (KEY_TYPE_RELEASE == keyvalue) {
if(is_longpress){
is_longpress = 0;
break;
}
key_normal_press(keycode);
}
#if 0
if(keyvalue && keycode == KEY_VOLUMEDOWN){
printf("key KEY_VOLUMEDOWN
");
} else if ()
#endif
}
break;
case EV_SYN:
{
printf("key EV_SYN
");
}
break;
default:
break;
}
}
}
}
}
return 0;
}