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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

关于WebSocket协议状态码解析

《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步