Android App层通过JNI从驱动获取Input Event


1 概述

 

尝试在App层直接读取驱动的Input Event,获取触屏事件(本文获取的是电磁笔触屏事件),而不通过AndroidInput Framework.

 

 

2 架构



3 实现

3.1 JNI

共有以下几个文件:

 

3.1.1 input_pen.h

 

首先看input_pen.h

 

#ifndef _INPUT_PEN_H
#define _INPUT_PEN_H
 
 
#include <pthread.h>
#include <linux/input.h>
#include <sys/types.h>
#include <linux/types.h>
 
#ifdef _cplusplus
extern "C" {
#endif
 
//获取input_event数据的方法指针,由App层提供
    typedef void (*get_event_callback)(__u16 type, __u16 code, __s32 value );
 
//创建线程的函数指针,通过Java虚拟机创建
    typedef pthread_t (*create_thread_callback)(const char* name, void (*start)(void *), void* arg);
 
//释放线程资源的函数指针
    typedef int (*detach_thread_callback)(void);
 
//回调函数结构体
    typedef struct {
        get_event_callback get_event_cb;
        create_thread_callback create_thread_cb;
        detach_thread_callback detach_thread_cb;
    } input_callback;
 
    /*******************************************************************/
//public methods
 
//初始化函数
    unsigned char input_pen_init(input_callback *callback);
 
//退出函数
    void input_pen_exit();
 
 
/*******************************************************************/
 
 
#ifdef _cplusplus
}
#endif
 
#endif
 
 

 

3.1.2 input_pen.cpp

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stddef.h>
#include <linux/input.h>
 
#include "input_pen.h"
#include "debug.h"
 
 
 
//驱动路径
#define DEV_PATH "/dev/input/event1"
 
//最大侦听
#define MAX_EVENTS 1
 
//epoll_wait的最大时间
#define EPOLL_SLEEP 200
 
//线程是否由pthread创建,否则由JVM创建
#define USING_PTHREAD 0
 
 
#if USING_PTHREAD
#define LOGD(fmt, arg...) do{printf(fmt"\n", ##arg);}while(0)
#define LOGE LOGD
#endif
 
/**************************************************************************************/
 
//static variable
 
//当前驱动文件指针
static int fd = 0;
//epoll文件指针
static int epoll_fd = 0;
//epoll_event数组
static struct epoll_event events[MAX_EVENTS];
//回调函数
static input_callback *callbacks = NULL;
 
//标记线程是否已启动
static unsigned char start_flag = 0;
//标记线程是否已退出
static unsigned char exit_flag = 0;
//线程锁
static pthread_mutex_t exit_mutex;
 
/**************************************************************************************/
 
 
//set non-blocking for fd
static int set_non_blocking(int fd) {
    int opts;
 
    opts = fcntl(fd, F_GETFL);
    if (opts < 0) {
        LOGE("fcntl F_GETFL error: %s", strerror(errno));
        return -1;
    }
    opts = (opts | O_NONBLOCK);
    if (fcntl(fd, F_SETFL, opts) < 0) {
        LOGE("fcntl F_SETFL error: %s", strerror(errno));
        return -1;
    }
 
    return 0;
}
 
//register epoll events for fd
static void epoll_register( int  epoll_fd, int  fd ) {
    struct epoll_event  ev;
    int         ret;
 
    ev.events  = EPOLLIN;//interested in receiving data
    ev.data.fd = fd;
 
    do {
        //register events for fd
        ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );
    } while (ret < 0 && errno == EINTR);
 
}
 
 
 
//remove epoll events for fd
static void epoll_unregister( int  epoll_fd, int  fd ) {
    int  ret;
    do {
        ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NU
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值