ADROID 2.1 架构解析 8 触摸屏

2024-01-02 18:58

本文主要是介绍ADROID 2.1 架构解析 8 触摸屏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

8 触摸屏

8.1 分类输入事件

文件:frameworks/base/libs/ui/EventHub.cpp

int EventHub::open_device(const char *deviceName)

{

       ...

       uint8_t key_bitmask[(KEY_MAX+1)/8];

    memset(key_bitmask, 0, sizeof(key_bitmask));

    LOGV("Getting keys...");

    if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {

        //LOGI("MAP/n");

        //for (int i=0; i<((KEY_MAX+1)/8); i++) {

        //    LOGI("%d: 0x%02x/n", i, key_bitmask[i]);

        //}

        for (int i=0; i<((BTN_MISC+7)/8); i++) {

            if (key_bitmask[i] != 0) {

                device->classes |= CLASS_KEYBOARD;

                break;

            }

        }

       if ((device->classes & CLASS_KEYBOARD) != 0) {

            device->keyBitmask = new uint8_t[sizeof(key_bitmask)];

            if (device->keyBitmask != NULL) {

                memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));

            } else {

                delete device;

                LOGE("out of memory allocating key bitmask");

                return -1;

            }

        }

    }

   

    // See if this is a trackball.

   if (test_bit(BTN_MOUSE, key_bitmask)) {

        uint8_t rel_bitmask[(REL_MAX+1)/8];

        memset(rel_bitmask, 0, sizeof(rel_bitmask));

        LOGV("Getting relative controllers...");

        if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0)

        {

            if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {

                device->classes |= CLASS_TRACKBALL;

            }

        }

    }

   

    uint8_t abs_bitmask[(ABS_MAX+1)/8];

    memset(abs_bitmask, 0, sizeof(abs_bitmask));

    LOGV("Getting absolute controllers...");

    ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);

   

    // Is this a new modern multi-touch driver?

if (test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)

            && test_bit(ABS_MT_POSITION_X, abs_bitmask)

            && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {

        device->classes |= CLASS_TOUCHSCREEN | CLASS_TOUCHSCREEN_MT;

       

    // Is this an old style single-touch driver?

} else if (test_bit(BTN_TOUCH, key_bitmask)

            && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {

        device->classes |= CLASS_TOUCHSCREEN;

}

...

}

输入事件有:键盘、轨迹球、单点触摸、多点触摸

8.2 输入事件服务

文件:frameworks/base/services/java/com/android/server/KeyInputQueue.java

8.2.1 获取事件

Thread mThread = new Thread("InputDeviceReader") {

        public void run() {

            if (DEBUG) Log.v(TAG, "InputDeviceReader.run()");

            android.os.Process.setThreadPriority(

                    android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);

        

            RawInputEvent ev = new RawInputEvent();

            while (true) {

                try {                  

                                   ...

                    // block, doesn't release the monitor

                    readEvent(ev);

                                   ...

调用readEvent,将输入事件读取到ev类,即RawInputEvent的变量里,readEvent对应jni的android_server_KeyInputQueue_readEvent

8.2.2 获取触摸点数据

// Process position events from multitouch protocol.

else if (ev.type == RawInputEvent.EV_ABS &&

                                (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {

                            if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {

                                di.mAbs.changed = true;

                                di.mAbs.mNextData[di.mAbs.mAddingPointerOffset

                                       + MotionEvent.SAMPLE_PRESSURE] = ev.value;

                            } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_X) {

                                di.mAbs.changed = true;

                                di.mAbs.mNextData[di.mAbs.mAddingPointerOffset

+ MotionEvent.SAMPLE_X] = ev.value;

                                if (DEBUG_POINTERS) Log.v(TAG, "MT @"

                                        + di.mAbs.mAddingPointerOffset

                                        + " X:" + ev.value);

                            } else if (ev.scancode == RawInputEvent.ABS_MT_POSITION_Y) {

                                di.mAbs.changed = true;

                                di.mAbs.mNextData[di.mAbs.mAddingPointerOffset

+ MotionEvent.SAMPLE_Y] = ev.value;

                                if (DEBUG_POINTERS) Log.v(TAG, "MT @"

                                        + di.mAbs.mAddingPointerOffset

                                        + " Y:" + ev.value);

                            } else if (ev.scancode == RawInputEvent.ABS_MT_WIDTH_MAJOR) {

                                di.mAbs.changed = true;

                                di.mAbs.mNextData[di.mAbs.mAddingPointerOffset

+ MotionEvent.SAMPLE_SIZE] = ev.value;

                            }

                       

                        // Process position events from single touch protocol.

                        } else if (ev.type == RawInputEvent.EV_ABS &&

                                (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {

                            if (ev.scancode == RawInputEvent.ABS_X) {

                                di.mAbs.changed = true;

                                di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;

                            } else if (ev.scancode == RawInputEvent.ABS_Y) {

                                di.mAbs.changed = true;

                                di.curTouchVals[MotionEvent.SAMPLE_Y] = ev.value;

                            } else if (ev.scancode == RawInputEvent.ABS_PRESSURE) {

                                di.mAbs.changed = true;

                                di.curTouchVals[MotionEvent.SAMPLE_PRESSURE] = ev.value;

                                di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA

+ MotionEvent.SAMPLE_PRESSURE] = ev.value;

                            } else if (ev.scancode == RawInputEvent.ABS_TOOL_WIDTH) {

                                di.mAbs.changed = true;

                               di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value;

di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA

                    + MotionEvent.SAMPLE_SIZE] = ev.value;

                            }

  

                        }

多点触摸和单点触摸的处理:保存多点或单点触摸的数据。

8.2.3 获取转换后触点数据

if (doMotion) {

                                        // XXX Need to be able to generate

                                        // multiple events here, for example

                                        // if two fingers change up/down state

                                        // at the same time.

                                        do {

                                           me = ms.generateAbsMotion(di, curTime,

curTimeNano, mDisplay,

mOrientation, mGlobalMetaState);

                                            if (DEBUG_POINTERS) Log.v(TAG, "Absolute: x="

                                                    + di.mAbs.mNextData[MotionEvent.SAMPLE_X]

                                                    + " y="

                                                    + di.mAbs.mNextData[MotionEvent.SAMPLE_Y]

                                                    + " ev=" + me);

                                            if (me != null) {

                                                if (WindowManagerPolicy.WATCH_POINTER) {

                                                    Log.i(TAG, "Enqueueing: " + me);

                                                }

                                                addLocked(di, curTimeNano, ev.flags,

                                                        RawInputEvent.CLASS_TOUCHSCREEN, me);

                                            }

                                        } while (ms.hasMore());

                                    }

获取转换后触摸点数据并加入到输入事件队列。

8.3 读取触摸点数据的流程

文件:frameworks/base/services/java/com/android/server/KeyInputQueue.java

       readEvent(ev);

di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;

文件:frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp

       { "readEvent",       "(Landroid/view/RawInputEvent;)Z",

            (void*) android_server_KeyInputQueue_readEvent },

..

static jboolean

android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz,

                                          jobject event)

{

       ...

    bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,

&flags, &value, &when);

       ...

}

文件:frameworks/base/libs/ui/EventHub.cpp

bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,

        int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,

        int32_t* outValue, nsecs_t* outWhen)

{

    ...

                            res = read(mFDs[i].fd, &iev, sizeof(iev));

 

                        ...

                       *outValue = iev.value;

    ..

}

8.4 触点数据转换

文件:frameworks/base/services/java/com/android/server/InutDevice.java

MotionEvent generateAbsMotion(InputDevice device, long curTime,

                long curTimeNano, Display display, int orientation,

                int metaState) {

           

            if (mSkipLastPointers) {

                mSkipLastPointers = false;

                mLastNumPointers = 0;

            }

           

            if (mNextNumPointers <= 0 && mLastNumPointers <= 0) {

                return null;

            }

           

            final int lastNumPointers = mLastNumPointers;

            final int nextNumPointers = mNextNumPointers;

            if (mNextNumPointers > MAX_POINTERS) {

                Log.w("InputDevice", "Number of pointers " + mNextNumPointers

                        + " exceeded maximum of " + MAX_POINTERS);

                mNextNumPointers = MAX_POINTERS;

            }

           

            int upOrDownPointer = updatePointerIdentifiers();

           

            final float[] reportData = mReportData;

            final int[] rawData;

            if (KeyInputQueue.BAD_TOUCH_HACK) {

                rawData = generateAveragedData(upOrDownPointer, lastNumPointers,

                        nextNumPointers);

            } else {

                rawData = mLastData;

            }

           

            final int numPointers = mLastNumPointers;

           

            if (DEBUG_POINTERS) Log.v("InputDevice", "Processing "

                    + numPointers + " pointers (going from " + lastNumPointers

                    + " to " + nextNumPointers + ")");

           

            for (int i=0; i<numPointers; i++) {

                final int pos = i * MotionEvent.NUM_SAMPLE_DATA;

                reportData[pos + MotionEvent.SAMPLE_X] = rawData[pos + MotionEvent.SAMPLE_X];

                reportData[pos + MotionEvent.SAMPLE_Y] = rawData[pos + MotionEvent.SAMPLE_Y];

                reportData[pos + MotionEvent.SAMPLE_PRESSURE] = rawData[pos + MotionEvent.SAMPLE_PRESSURE];

                reportData[pos + MotionEvent.SAMPLE_SIZE] = rawData[pos + MotionEvent.SAMPLE_SIZE];

            }

           

            int action;

            int edgeFlags = 0;

            if (nextNumPointers != lastNumPointers) {

                if (nextNumPointers > lastNumPointers) {

                    if (lastNumPointers == 0) {

                        action = MotionEvent.ACTION_DOWN;

                        mDownTime = curTime;

                    } else {

                        action = MotionEvent.ACTION_POINTER_DOWN

                                | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);

                    }

                } else {

                    if (numPointers == 1) {

                        action = MotionEvent.ACTION_UP;

                    } else {

                        action = MotionEvent.ACTION_POINTER_UP

                                | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT);

                    }

                }

                currentMove = null;

            } else {

                action = MotionEvent.ACTION_MOVE;

            }

           

            final int dispW = display.getWidth()-1;

            final int dispH = display.getHeight()-1;

            int w = dispW;

            int h = dispH;

            if (orientation == Surface.ROTATION_90

                    || orientation == Surface.ROTATION_270) {

                int tmp = w;

                w = h;

                h = tmp;

            }

           

            final AbsoluteInfo absX = device.absX;

            final AbsoluteInfo absY = device.absY;

            final AbsoluteInfo absPressure = device.absPressure;

            final AbsoluteInfo absSize = device.absSize;

            for (int i=0; i<numPointers; i++) {

                final int j = i * MotionEvent.NUM_SAMPLE_DATA;

           

                if (absX != null) {

                    reportData[j + MotionEvent.SAMPLE_X] =

                            ((reportData[j + MotionEvent.SAMPLE_X]-absX.minValue)

                                / absX.range) * w;

                }

                if (absY != null) {

                    reportData[j + MotionEvent.SAMPLE_Y] =

                            ((reportData[j + MotionEvent.SAMPLE_Y]-absY.minValue)

                                / absY.range) * h;

                }

                if (absPressure != null) {

                    reportData[j + MotionEvent.SAMPLE_PRESSURE] =

                            ((reportData[j + MotionEvent.SAMPLE_PRESSURE]-absPressure.minValue)

                                / (float)absPressure.range);

                }

                if (absSize != null) {

                    reportData[j + MotionEvent.SAMPLE_SIZE] =

                            ((reportData[j + MotionEvent.SAMPLE_SIZE]-absSize.minValue)

                                / (float)absSize.range);

                }

               

                switch (orientation) {

                    case Surface.ROTATION_90: {

                        final float temp = reportData[j + MotionEvent.SAMPLE_X];

                        reportData[j + MotionEvent.SAMPLE_X] = reportData[j + MotionEvent.SAMPLE_Y];

                        reportData[j + MotionEvent.SAMPLE_Y] = w-temp;

                        break;

                    }

                    case Surface.ROTATION_180: {

                        reportData[j + MotionEvent.SAMPLE_X] = w-reportData[j + MotionEvent.SAMPLE_X];

                        reportData[j + MotionEvent.SAMPLE_Y] = h-reportData[j + MotionEvent.SAMPLE_Y];

                        break;

                    }

                    case Surface.ROTATION_270: {

                        final float temp = reportData[j + MotionEvent.SAMPLE_X];

                        reportData[j + MotionEvent.SAMPLE_X] = h-reportData[j + MotionEvent.SAMPLE_Y];

                        reportData[j + MotionEvent.SAMPLE_Y] = temp;

                        break;

                    }

                }

            }

           

            // We only consider the first pointer when computing the edge

            // flags, since they are global to the event.

            if (action == MotionEvent.ACTION_DOWN) {

                if (reportData[MotionEvent.SAMPLE_X] <= 0) {

                    edgeFlags |= MotionEvent.EDGE_LEFT;

                } else if (reportData[MotionEvent.SAMPLE_X] >= dispW) {

                    edgeFlags |= MotionEvent.EDGE_RIGHT;

                }

                if (reportData[MotionEvent.SAMPLE_Y] <= 0) {

                    edgeFlags |= MotionEvent.EDGE_TOP;

                } else if (reportData[MotionEvent.SAMPLE_Y] >= dispH) {

                    edgeFlags |= MotionEvent.EDGE_BOTTOM;

                }

            }

           

            if (currentMove != null) {

                if (false) Log.i("InputDevice", "Adding batch x="

                        + reportData[MotionEvent.SAMPLE_X]

                        + " y=" + reportData[MotionEvent.SAMPLE_Y]

                        + " to " + currentMove);

                currentMove.addBatch(curTime, reportData, metaState);

                if (WindowManagerPolicy.WATCH_POINTER) {

                    Log.i("KeyInputQueue", "Updating: " + currentMove);

                }

                return null;

            }

           

            MotionEvent me = MotionEvent.obtainNano(mDownTime, curTime,

                    curTimeNano, action, numPointers, mPointerIds, reportData,

                    metaState, xPrecision, yPrecision, device.id, edgeFlags);

            if (action == MotionEvent.ACTION_MOVE) {

                currentMove = me;

            }

           

            if (nextNumPointers < lastNumPointers) {

                removeOldPointer(upOrDownPointer);

            }

           

            return me;

        }

将原始数据点转化为显示屏对应的数据,一般触摸屏校正就在这里对数据点进行较正的。

这篇关于ADROID 2.1 架构解析 8 触摸屏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/563359

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

mybatis的整体架构

mybatis的整体架构分为三层: 1.基础支持层 该层包括:数据源模块、事务管理模块、缓存模块、Binding模块、反射模块、类型转换模块、日志模块、资源加载模块、解析器模块 2.核心处理层 该层包括:配置解析、参数映射、SQL解析、SQL执行、结果集映射、插件 3.接口层 该层包括:SqlSession 基础支持层 该层保护mybatis的基础模块,它们为核心处理层提供了良好的支撑。

百度/小米/滴滴/京东,中台架构比较

小米中台建设实践 01 小米的三大中台建设:业务+数据+技术 业务中台--从业务说起 在中台建设中,需要规范化的服务接口、一致整合化的数据、容器化的技术组件以及弹性的基础设施。并结合业务情况,判定是否真的需要中台。 小米参考了业界优秀的案例包括移动中台、数据中台、业务中台、技术中台等,再结合其业务发展历程及业务现状,整理了中台架构的核心方法论,一是企业如何共享服务,二是如何为业务提供便利。

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

OWASP十大安全漏洞解析

OWASP(开放式Web应用程序安全项目)发布的“十大安全漏洞”列表是Web应用程序安全领域的权威指南,它总结了Web应用程序中最常见、最危险的安全隐患。以下是对OWASP十大安全漏洞的详细解析: 1. 注入漏洞(Injection) 描述:攻击者通过在应用程序的输入数据中插入恶意代码,从而控制应用程序的行为。常见的注入类型包括SQL注入、OS命令注入、LDAP注入等。 影响:可能导致数据泄

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

系统架构设计师: 信息安全技术

简简单单 Online zuozuo: 简简单单 Online zuozuo 简简单单 Online zuozuo 简简单单 Online zuozuo 简简单单 Online zuozuo :本心、输入输出、结果 简简单单 Online zuozuo : 文章目录 系统架构设计师: 信息安全技术前言信息安全的基本要素:信息安全的范围:安全措施的目标:访问控制技术要素:访问控制包括:等保

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro