linux应用层获取输入事件

Linux输入系统从内核层次较详细的介绍了输入系统构架,linux输入设备驱动实列介绍了如何实现一个简单的输

如设备驱动。下面主要介绍如何通过非阻塞的方式,在应用层获取输入事件,代码如下:



#include <fcntl.h>
#include <stdlib.h>
#include <utils/Log.h>
#include <unistd.h>
#include <sys/mman.h>   
#include <sys/ioctl.h>
#include <cutils/fs.h>
#include <cutils/sockets.h>
#include <errno.h>

#include <sys/inotify.h>
#include <sys/limits.h>
#include <sys/poll.h>
#include <linux/input.h>

#include <cutils/list.h>
#include <cutils/android_reboot.h>
#include <cutils/sockets.h>
#include <cutils/iosched_policy.h>
#include <cutils/fs.h>
#include <termios.h>

#include <sys/system_properties.h>


#define LOGV(...) __android_log_print(ANDROID_LOG_SILENT, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)


#define DEVICE  "/dev/input/event0"

struct cmd{
	char command[32];
	char status[32];
	char recovery[1024];
}cmdRecovery;


int write_recoveryCmd(){
	//char cmdRecovery[] ="boot-recovery";


	char *revCmd="recovery\n--update_package=/storage/usbotg/usbotg-sda/mt8127_box_p1v1-ota-eng.innoplay.zip\n--locale=en_US";
	
	memset(&cmdRecovery ,0 ,sizeof(cmdRecovery));
	memcpy(cmdRecovery.command,"boot-recovery",sizeof("boot-recovery"));
	//memcpy(cmdRecovery.recovery,"recovery",sizeof("recovery"));
	//memcpy(cmdRecovery.recovery,revCmd,sizeof(*revCmd));
	memcpy(cmdRecovery.recovery,"recovery\n--update_package=/storage/usbotg/usbotg-sda/mt8127_box_p1v1-ota-eng.innoplay.zip\n--locale=en_US",sizeof("recovery\n--update_package=/storage/usbotg/usbotg-sda/mt8127_box_p1v1-ota-eng.innoplay.zip\n--locale=en_US"));
	
	int miscFd = open("/dev/misc", O_WRONLY | O_SYNC);
	if (miscFd<0)  {
		printf("Can't open /dev/misc (%s)\n", strerror(errno));
		return -1;
	}
	
	int count = write(miscFd,(char*) &cmdRecovery, sizeof(cmdRecovery));
	
	printf("sizeof(cmdRecovery)=%d count=%d  \n",sizeof(cmdRecovery),count);
	
    if (count != sizeof(cmdRecovery)) {
        printf("Failed writing /dev/misc (%s)\n",  strerror(errno));
        close(miscFd);
        return -1;
    }
	else{
		printf("the recovery len =%d \n",sizeof(*revCmd));
		printf("write the cmd =%s \n",cmdRecovery.recovery);
		sync();
	}
	
    close(miscFd);

	return 0;
}

int write_commandFile(){

	char *cmd="--update_package=/storage/usbotg/usbotg-sda/mt8127_box_p1v1-ota-eng.innoplay.zip\n--locale=en_US";

	int fd=open("/cache/recovery/command", O_RDWR | O_CREAT |O_SYNC, 0644);

	if(fd<0){
		LOGE("creat the command file failed!\n");
		return -1;
	}

	int n=write(fd,cmd,sizeof(*cmd)); //fix me !after wirte successfully,but nothing in the file

	if(n!=sizeof(*cmd)){
		printf("write the cmd failed! (%s) \n ", strerror(errno));
		return -1;
		close(fd);
	}
	
	sync();

	close(fd);

	return 0;
}


int read_recoveryCmd(){

	struct cmd readCmd;
	memset(&readCmd,0,sizeof(readCmd));
	int miscFd1 = open("/dev/misc", O_RDONLY);
	
	if (miscFd1<0)  {
		
		printf("Can't open /dev/misc (%s)\n", strerror(errno));
		return -1;
	}
	
	int n2=read(miscFd1,&readCmd,sizeof(readCmd));
	if(n2!=sizeof(readCmd)){
		
		printf("read error (%s)!", strerror(errno));
		close(miscFd1);
	}

	
	close(miscFd1);
	printf("cmd=%s \n",readCmd.command);
	printf("recovery =%s \n",readCmd.recovery);

	return 0;
}

int main(int argc,char *argv[]){
	

	int fd=0,pollRes=0,res=0;
	struct pollfd ufds;
    struct input_event event;
	
	fd = open(DEVICE, O_RDWR);
	if(fd<0){
		printf("open recovery key device failed!\n");
		return -1;
	}

	printf("in the recovery.c\n");


	memset(&ufds,0 ,sizeof(ufds));
	memset(&event,0 ,sizeof(event));

	ufds.fd=fd ;
	ufds.events = POLLIN;

	while(1){
		
		printf("in the recovery.c poll!\n");
		pollRes =poll(&ufds,1,-1);
		if(pollRes<0){
			printf("poll failed error= %s !\n", strerror(errno));
			return -1;
		}

		res = read(ufds.fd,&event,sizeof(event));
		if(res!= sizeof(event)){
			printf("read failed error= %s !\n", strerror(errno));
			return -1;
		}

		printf("event.code =%d event.value=%d \n",event.code , event.value);

		if((event.code==KEY_DOWN) ||(event.code==KEY_ENTER))
			break;
		
	}


	if(write_recoveryCmd()){

		printf("write recovery command error!\n");
		return -1;

	}
	
	//property_set("sys.shutdown.requested","1 recovery");
	//property_set("persist.sys.safemode","1");
	
	sleep(1);
	
	//property_set("sys.powerctl","reboot,recovery"); //will eraser the cmdRecovery.recovery
	
	system("reboot");
	

	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值