android 触摸事件传递(一)

来源:互联网 发布:编程判断整数的奇偶性 编辑:程序博客网 时间:2024/06/09 14:45

android 触摸事件传递

1、主要相关代码路径

基于展讯7.0源码

native
frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cppframeworks/native/services/inputflinger/EventHub.cppframeworks/native/services/inputflinger/InputDispatcher.cppframeworks/native/services/inputflinger/InputManager.cppframeworks/native/services/inputflinger/InputReader.cppframeworks/native/services/inputflinger/InputListener.cppframeworks/native/services/inputflinger/InputWindow.cpp
java
frameworks/base/services/java/com/android/server/SystemServer.javaframeworks/base/services/core/java/com/android/server/input/InputManagerService.java
RawEvent
struct RawEvent {    nsecs_t when;    int32_t deviceId;    int32_t type;    int32_t code;    int32_t value;};
NotifyArgs
struct NotifyArgs {    virtual ~NotifyArgs() { }    virtual void notify(const sp<InputListenerInterface>& listener) const = 0;};
/* Describes a configuration change event. */struct NotifyConfigurationChangedArgs : public NotifyArgs {}/* Describes a key event. */struct NotifyKeyArgs : public NotifyArgs {}/* Describes a motion event. */struct NotifyMotionArgs : public NotifyArgs {}/* Describes a switch event. */struct NotifySwitchArgs : public NotifyArgs {}/* Describes a device reset event, such as when a device is added, * reconfigured, or removed. */struct NotifyDeviceResetArgs : public NotifyArgs {}

InputManagerService 启动

/** * Starts a miscellaneous grab bag of stuff that has yet to be refactored * and organized. */    private void startOtherServices() {        ...        inputManager = new InputManagerService(context);        inputManager.setWindowManagerCallbacks(wm.getInputMonitor());        inputManager.start();        ...    }
public InputManagerService(Context context) {        this.mContext = context;        this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());        mUseDevInputEventForAudioJack =                context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);        //初始化native层                mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());        LocalServices.addService(InputManagerInternal.class, new LocalService());    }
native层初始化
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {    sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);    if (messageQueue == NULL) {        jniThrowRuntimeException(env, "MessageQueue is not initialized.");        return 0;    }    //创建NativeInputManager    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,            messageQueue->getLooper());    im->incStrong(0);    //NativeInputManager服务对象的指针    return reinterpret_cast<jlong>(im);}
NativeInputManager::NativeInputManager(jobject contextObj,        jobject serviceObj, const sp<Looper>& looper) :        mLooper(looper), mInteractive(true) {        .....        //创建eventhub    sp<EventHub> eventHub = new EventHub();    mInputManager = new InputManager(eventHub, this, this);}
创建事件分发和读取
InputManager::InputManager(        const sp<EventHubInterface>& eventHub,        const sp<InputReaderPolicyInterface>& readerPolicy,        const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {    mDispatcher = new InputDispatcher(dispatcherPolicy);    mReader = new InputReader(eventHub, readerPolicy, mDispatcher);    initialize();}void InputManager::initialize() {    mReaderThread = new InputReaderThread(mReader);    mDispatcherThread = new InputDispatcherThread(mDispatcher);}
在SystemServer启动时inputManager.start();将会启动input的线程工作
public void start() {        Slog.i(TAG, "Starting input manager");        nativeStart(mPtr);        .....}        
static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);    status_t result = im->getInputManager()->start();    if (result) {        jniThrowRuntimeException(env, "Input manager could not be started.");    }}
status_t InputManager::start() {    status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);    if (result) {        ALOGE("Could not start InputDispatcher thread due to error %d.", result);        return result;    }    result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);    if (result) {        ALOGE("Could not start InputReader thread due to error %d.", result);        mDispatcherThread->requestExit();        return result;    }    return OK;}