Android系统 开机启动默认旋转主屏幕方向问题 --(二)

上篇文章Android系统 开机启动默认旋转主屏幕方向问题 --(一)讲到对系统显示界面进行旋转操作,并希望每次开机启动默认显示旋转后的方向。

但是会忽略一个问题:很多机器设备是有配 tp(触摸屏) 的,系统显示进行了对应角度的旋转,但是tp 还是保持出厂的状态,这样就会导致触摸错乱。

众所周知, 常见 tp(触摸屏) 有 I2C 和 USB 的两种

  1. I2C触摸屏:
    i2c的触摸是要配置相对应的驱动和dts,系统显示旋转相应角度后,tp的 x,y 坐标是需要进行交换或者镜像变换,配置好后就能做到与系统显示方向一致(驱动会解析处理)。内部原理此处暂不做过多分析。
  2. USB触摸屏:
    usb的触摸有点不一样,作为usb输入设备,是要遵循usb hid协议,即插即用;可以深入研究下usb协议,总之usb触摸是没有x,y坐标来配置的。

针对于usb触摸屏,想要和系统显示保持一致,可采取以下方法:

方法一:改usb hid底层, kernel/drivers/hid/hid-multitouch.c

思路:根据usb触摸屏的vid,pid参数判断(用于设备多个usb触摸的情况,如果设备只有一个usb接口触摸屏,可不用判断)将触摸屏的坐标进行变换。

diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 9de379c1b3fd..0c08b397792f 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -164,6 +164,8 @@ static void mt_post_parse(struct mt_device *td);
 #define MT_USB_DEVICE(v, p)    HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
 #define MT_BT_DEVICE(v, p)     HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
 
+static int vendor_id,vendor_temp,vendor_logical_max;
+

@@ -415,6 +417,7 @@ static void set_abs(struct input_dev *input, unsigned int code,
        int fuzz = snratio ? (fmax - fmin) / snratio : 0;
        input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
        input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
+       vendor_logical_max = fmax;
 }
 
 static void mt_store_field(struct hid_usage *usage, struct mt_device *td,
@@ -640,6 +643,13 @@ static void mt_complete_slot(struct mt_device *td, struct input_dev *input)
                active = (s->touch_state || s->inrange_state) &&
                                                        s->confidence_state;
 
+        		/*Identifier: bus=0x0003, vendor=0x222a, product=0x0001, version=0x0110 */
+               if(vendor_id == 0x222a){
    
    
+                       vendor_temp = s->x;
+                       s->x = vendor_logical_max - s->y;
+                       s->y = vendor_temp;
+               }
+
                input_mt_slot(input, slotnum);
                input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
                if (active) {
    
    
@@ -764,6 +774,7 @@ static void mt_touch_report(struct hid_device *hid, struct hid_report *report)
        struct hid_field *field;
        unsigned count;
        int r, n;
+       vendor_id = hid->vendor;
 
        /*
         * Includes multi-packet support where subsequent

方法二:改安卓input event上层, framework/native/services/inputflinger/InputReader.cpp

思路:

  1. 添加系统属性(persist.sys.hwrotation),然后根据系统属性对触摸方向进行旋转。
  2. persist.sys.panel.flip 设置触摸的初始方向,当然也可以在设置中添加一个触摸屏旋转选项来方便客户操作,只需修改这个值即可。
diff --git a/services/inputflinger/InputReader.cpp b/services/inputflinger/InputReader.cpp
index c1a36ff79..851b98027 100644
--- a/services/inputflinger/InputReader.cpp
+++ b/services/inputflinger/InputReader.cpp
@@ -3814,6 +3814,29 @@ void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
    
    
      bool viewportChanged = mViewport != newViewport;
      if (viewportChanged) {
    
    
          mViewport = newViewport;
          
+        char buffer_orientation[PROP_VALUE_MAX];
+        memset(buffer_orientation, 0, sizeof(buffer_orientation));
+        property_get("persist.sys.panel.flip", buffer_orientation, "270");
+        int cmpRet = atoi(buffer_orientation);
+        ALOGE("persist.sys.hwrotation~~~~~~~~~~~~~~~~~~~~~~~~~ = %d",cmpRet);
+        ALOGE("fy-1:mViewport.orientation----------------------- = %d",mViewport.orientation);
+        if (cmpRet == 0)
+        {
    
    
+            mViewport.orientation = mViewport.orientation + DISPLAY_ORIENTATION_0;
+        }
+        else if(cmpRet == 90)
+        {
    
    
+            mViewport.orientation = mViewport.orientation  + DISPLAY_ORIENTATION_90;
+        }
+        else if(cmpRet == 180)
+        {
    
    
+            mViewport.orientation = mViewport.orientation + DISPLAY_ORIENTATION_180;
+        }
+        else if(cmpRet == 270)
+        {
    
    
+            mViewport.orientation = mViewport.orientation + DISPLAY_ORIENTATION_270;
+        }
+        ALOGE("fy-2:mViewport.orientation----------------------- = %d",mViewport.orientation);

          if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
    
    
              // Convert rotated viewport to natural surface coordinates.

mViewport.orientation本身也是有一个值,比如APP是竖屏,系统是横屏,打开APP的时候系统会自动旋转为竖屏,此时mViewport.orientation也会跟着变化。
所以mViewport.orientation最后的值为mViewport.orientation + DISPLAY_ORIENTATION_X

猜你喜欢

转载自blog.csdn.net/weixin_45639314/article/details/130078701