画鼠标和触摸屏光标流程

1. 画鼠标光标流程

    设置新的Mouse Cursor位置与画Mouse Cursor是异步的。

InputReader::loopOnce->
InputReader::processEventsLocked->
InputReader::processEventsForDeviceLocked->
InputDevice::process(numMappers=1)->
InputMapper::process(CursorInputMapper::process)->
CursorInputMapper::sync->
PointerController::move (偏移量)->
  PointerController::setPositionLocked(绝对值)
    PointerController::updatePointerLocked
       *SpriteController::openTransaction()
       *SpriteController::SpriteImpl::setLayer(int32_t layer)
       *SpriteController::SpriteImpl::setPosition(float x, float y) (绝对值,画光标的屏幕坐标)
          SpriteController::SpriteImpl::invalidateLocked->(如果位置有变,则调用此函数)
            SpriteController::invalidateSpriteLocked              
       *SpriteController::SpriteImpl::setAlpha(float alpha)
       *SpriteController::SpriteImpl::setVisible(bool visible)
       *SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon)  
       *SpriteController::closeTransaction()(发送MSG_UPDATE_SPRITES消息给SpriteController)


画光标:
SpriteController::handleMessage(const Message& message)->
   SpriteController::doUpdateSprites()(真正负责画光标) 


1.1 Log信息(移动鼠标)

D/InputReader(  369): InputReader::loopOnce call InputReader::processEventsLocked
D/InputReader(  369): InputReader::processEventsLocked call InputReader::processEventsForDeviceLocked
D/InputReader(  369): InputReader::processEventsForDeviceLocked call InputDevice::process
D/InputReader(  369): InputDevice::process call InputMapper::process, numMappers=1
D/InputReader(  369): CursorInputMapper::process call CursorInputMapper->sync
D/InputReader(  370): CursorInputMapper::sync call mPointerController->move
D/PointerController(  370): Move pointer by deltaX=-2.000, deltaY=0.000
D/PointerController(  370): PointerController::updatePointerLocked start...
D/Sprites (  370): openTransaction
D/Sprites (  370): setLayer,layer=0
D/Sprites (  370): setPosition,x=1050.746338,y=534.966919
D/Sprites (  370): invalidateLocked
D/Sprites (  370): invalidateSpriteLocked
D/Sprites (  370): setAlpha,alpha=1.000000
D/Sprites (  370): setVisible,old=1,visible=1
D/Sprites (  370): closeTransaction
D/Sprites (  370): closeTransaction, send MSG_UPDATE_SPRITES
D/PointerController(  370): :PointerController::updatePointerLocked end...
D/PointerController(  370): PointerController::updatePointerLocked start...
D/Sprites (  370): openTransaction
D/Sprites (  370): setLayer,layer=0
D/Sprites (  370): setPosition,x=1050.746338,y=534.966919
D/Sprites (  370): setAlpha,alpha=1.000000
D/Sprites (  370): setVisible,old=1,visible=1
D/Sprites (  370): closeTransaction
D/PointerController(  370): PointerController::updatePointerLocked end...
D/PointerController(  370): getPosition x=1050.746, y=534.967
D/Sprites (  370): handleMessage
D/Sprites (  370): doUpdateSprites

2. 画触摸屏光标流程

InputReader::loopOnce->
InputReader::processEventsLocked->
InputReader::processEventsForDeviceLocked->
InputDevice::process(numMappers=1)->
InputMapper::process(SingleTouchInputMapper::process->TouchInputMapper::process)->
TouchInputMapper::sync->
SingleTouchInputMapper::syncTouch(获取touch数据)->
TouchInputMapper::dispatchPointerUsage->
TouchInputMapper::dispatchPointerStylus->
PointerController::setPosition->
  PointerController::setPositionLocked(绝对值)
    PointerController::updatePointerLocked
       *SpriteController::openTransaction()
       *SpriteController::SpriteImpl::setLayer(int32_t layer)
       *SpriteController::SpriteImpl::setPosition(float x, float y) (绝对值,画光标的屏幕坐标)
          SpriteController::SpriteImpl::invalidateLocked->(如果位置有变,则调用此函数)
            SpriteController::invalidateSpriteLocked              
       *SpriteController::SpriteImpl::setAlpha(float alpha)
       *SpriteController::SpriteImpl::setVisible(bool visible)
       *SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon)  
       *SpriteController::closeTransaction()(发送MSG_UPDATE_SPRITES消息给SpriteController)


画光标:
SpriteController::handleMessage(const Message& message)->
   SpriteController::doUpdateSprites()(真正负责画光标) 



2.1 产生Touch Move和Touch Button的实例代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <android/log.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <sys/stat.h>
#include "uinput.h"


#define LOG_TAG "TOUCH_TEST"
#define ALOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
#define ALOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#define ALOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)

#define BUTTON_DOWN 1
#define BUTTON_UP   0


/* global variable */
static int uinput_fd = -1; 
struct uinput_user_dev uinput; // uInput device structure 

/* Setup the uinput device */ 
int setup_uinput_device() 
{ 
   //int i=0; 
   int ret;
   
   // Open the input device 
   uinput_fd = open("/dev/uinput", O_WRONLY | O_NDELAY); 
   if (uinput_fd < 0){ 
      ALOGE("Unable to open /dev/uinput/n"); 
      return -1; 
   } 
   
   memset(&uinput,0,sizeof(uinput)); // Intialize the uInput device to NULL 
   strncpy(uinput.name, "vtouch", UINPUT_MAX_NAME_SIZE); 
   uinput.id.version = 4; 
   uinput.id.bustype = BUS_USB; 


   uinput.absmin[ABS_X] = 0;
   uinput.absmax[ABS_X] = 1280; 
   uinput.absmin[ABS_Y] = 0;
   uinput.absmax[ABS_Y] = 720; 
   
   // Setup the uinput device 
   ret = ioctl(uinput_fd, UI_SET_EVBIT, EV_ABS); 
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY); 
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_ABSBIT, ABS_X);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   
   ret = ioctl(uinput_fd, UI_SET_ABSBIT, ABS_Y);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOUCH);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOOL_MOUSE);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOOL_PEN);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   ret = ioctl(uinput_fd, UI_SET_KEYBIT, KEY_BACK);
   if(ret){
      ALOGE("ioctl fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   /*    
    ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY);  
    ioctl(uinput_fd, UI_SET_EVBIT, EV_REL);  
     
    ioctl(uinput_fd, UI_SET_RELBIT, REL_X);  
    ioctl(uinput_fd, UI_SET_RELBIT, REL_Y);  
   
    for (i=0; i < 256; i++) {  
       ioctl(uinput_fd, UI_SET_KEYBIT, i);  
    }  
     
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MOUSE);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOUCH);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MOUSE);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_LEFT);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MIDDLE);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_RIGHT);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_FORWARD);  
    ioctl(uinput_fd, UI_SET_KEYBIT, BTN_BACK);  
    */  

   
   // Create input device into input sub-system 
   if(write(uinput_fd, &uinput, sizeof(uinput))== -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
      
   if(ioctl(uinput_fd, UI_DEV_CREATE)) 
   { 
      ALOGE("Unable to create UINPUT device."); 
      return -1; 
   } 
   
   return 0; 
} 

/* Destroy the uinput device */ 
int destroy_uinput_device() 
{
   // Destroy the uinput device
   ioctl(uinput_fd, UI_DEV_DESTROY); 
   
   // Close the uinput device 
   close(uinput_fd);
}

int write_touch_move(int x, int y) 
{ 
   struct input_event event; 
   int ret;
   
   // Move pointer to (0,0) location 
   memset(&event, 0, sizeof(event)); 

   gettimeofday(&event.time, NULL); 

   event.type = EV_ABS;
   event.code = ABS_X;
   event.value = x;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
      
   event.type = EV_ABS;
   event.code = ABS_Y;
   event.value = y;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   event.type = EV_KEY;
   event.code = BTN_TOOL_PEN; 
   event.value = 1;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   

   event.type = EV_SYN; 
   event.code = SYN_REPORT; 
   event.value = 0; 
   ret = write(uinput_fd, &event, sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   return 0;
} 


int write_touch_hide(void) 
{ 
   struct input_event event; 
   int ret;
   
   // Move pointer to (0,0) location 
   memset(&event, 0, sizeof(event)); 

   gettimeofday(&event.time, NULL); 

   event.type = EV_ABS;
   event.code = ABS_X;
   event.value = 0;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
      
   event.type = EV_ABS;
   event.code = ABS_Y;
   event.value = 0;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   event.type = EV_KEY;
   event.code = BTN_TOOL_PEN; 
   event.value = 0;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   

   event.type = EV_SYN; 
   event.code = SYN_REPORT; 
   event.value = 0; 
   ret = write(uinput_fd, &event, sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   return 0;
}



int write_key(int key, int down) 
{ 
   struct input_event event; 
   int ret;
   
   // Move pointer to (0,0) location 
   memset(&event, 0, sizeof(event)); 

   gettimeofday(&event.time, NULL); 

   event.type = EV_KEY;
   event.code = key; 
   event.value = down;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   

   event.type = EV_SYN; 
   event.code = SYN_REPORT; 
   event.value = 0; 
   ret = write(uinput_fd, &event, sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   return 0;
}

int write_touch_button(int x, int y, int down)  
{ 
   struct input_event event; 
   int ret;
   
   // Move pointer to (0,0) location 
   memset(&event, 0, sizeof(event)); 

   gettimeofday(&event.time, NULL); 

   event.type = EV_ABS;
   event.code = ABS_X;
   event.value = x;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
      
   event.type = EV_ABS;
   event.code = ABS_Y;
   event.value = y;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   
   event.type = EV_KEY;
   event.code = BTN_TOUCH; 
   event.value = down;
   ret = write(uinput_fd,&event,sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }

   event.type = EV_SYN; 
   event.code = SYN_REPORT; 
   event.value = 0; 
   ret = write(uinput_fd, &event, sizeof(event));
   if(ret == -1){
      ALOGE("write fail:%s(%d)",__FUNCTION__,__LINE__);
      return -1;   
   }
   return 0;
}



#define DIS_PIXEL 1
#define DUR_TIME  5

int main(int argc, char** argv) {
   int i;
   // Initialize uinput device
   if(setup_uinput_device()){
      ALOGE("setup uinput device fail");
      return -1;   
   }

   // Send gsensor event
   int write_count=0;
   int count =0;
   int x=0,y=0;

   count = 0;
   while(1){  // for touch screen
   
        x = 10;
        y = 10;
        
        while(x <1270){
            write_touch_move(x,y);
            x+=DIS_PIXEL;
            usleep(DUR_TIME*1000);
        }
        
        y+=DIS_PIXEL;
        while(y < 710){
            write_touch_move(x,y);
            y+=DIS_PIXEL;
            usleep(DUR_TIME*1000);            
        }

        x -=DIS_PIXEL;
        while(x > 10){
            write_touch_move(x,y);
            x-=DIS_PIXEL;
            usleep(DUR_TIME*1000);            
        }        

        /*
        y -=DIS_PIXEL;
        while(y > 10){
            write_touch_move(x,y);
            y-=DIS_PIXEL;
            usleep(DUR_TIME*1000);            
        } 
        */
        
        write_touch_hide();

        sleep(5);
        
        /*
        count++;
        if(count>5){
            while(1){
                sleep(60);
            }
                
        }
        */
   }   

   // Destroy uinput device 
   destroy_uinput_device();
   return 0;
}



2.2 log信息

D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=3,code=0,value=500
D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=3,code=1,value=500
D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=0,code=0,value=0
D/InputReader(  356): call sync...
D/InputReader(  356): sync mDeviceMode=3
D/InputReader(  356): SingleTouchInputMapper::syncTouch, isToolActive=1
D/InputReader(  356): syncTouch: pointerCount 1 -> 1, touching ids 0x00000000 -> 0x00000000, hovering ids 0x80000000 -> 0x80000000
D/InputReader(  356): sync Line:4107
D/InputReader(  356): sync Line:4110
D/InputReader(  356): sync Line:4127
D/InputReader(  356): sync Line:4139,pointerUsage=2
D/InputReader(  356): sync Line:4150,pointerUsage=2
D/InputReader(  356): dispatchPointerUsage mPointerUsage=2
D/Sprites (  356):  setPosition,x=499.609680,y=499.306519
D/Sprites (  356):  setPosition,x=499.609680,y=499.306519
D/Sprites (  356):  setPosition:x=499.609680,y=499.306519
D/Sprites (  356):  setPosition:hotx=6.000000,hoty=6.000000

D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=3,code=0,value=600
D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=3,code=1,value=600
D/InputReader(  356): SingleTouchInputMapper::process
D/InputReader(  356): type=0,code=0,value=0
D/InputReader(  356): call sync...
D/InputReader(  356): sync mDeviceMode=3
D/InputReader(  356): SingleTouchInputMapper::syncTouch, isToolActive=1
D/InputReader(  356): syncTouch: pointerCount 1 -> 1, touching ids 0x00000000 -> 0x00000000, hovering ids 0x80000000 -> 0x80000000
D/InputReader(  356): sync Line:4107
D/InputReader(  356): sync Line:4110
D/InputReader(  356): sync Line:4127
D/InputReader(  356): sync Line:4139,pointerUsage=2
D/InputReader(  356): sync Line:4150,pointerUsage=2
D/InputReader(  356): dispatchPointerUsage mPointerUsage=2
D/Sprites (  356):  setPosition,x=599.531616,y=599.167847
D/Sprites (  356):  setPosition,x=599.531616,y=599.167847
D/Sprites (  356):  setPosition:x=599.531616,y=599.167847
D/Sprites (  356):  setPosition:hotx=6.000000,hoty=6.000000




你可能感兴趣的:(画鼠标和触摸屏光标流程)