modify inputeventsource,read event in thread

This commit is contained in:
侯歌 2022-01-21 18:29:06 +08:00
parent dafacdc740
commit 193a78a0ae
4 changed files with 54 additions and 33 deletions

View File

@ -21,6 +21,7 @@ public:
int main(int argc,const char*argv[]){ int main(int argc,const char*argv[]){
App app(argc,argv); App app(argc,argv);
AnimationHandler::getInstance().setFrameDelay(200);
Window*w=new Window(0,0,1280,600); Window*w=new Window(0,0,1280,600);
MyPageAdapter*gpAdapter=new MyPageAdapter(); MyPageAdapter*gpAdapter=new MyPageAdapter();

View File

@ -4,13 +4,20 @@
static int mPageCount=5; static int mPageCount=5;
class MyPageAdapter:public PagerAdapter{ class MyPageAdapter:public PagerAdapter{
public: public:
int getCount(){return 12;} int getCount(){return 8;}
bool isViewFromObject(View* view, void*object) { return view==object;} bool isViewFromObject(View* view, void*object) { return view==object;}
void* instantiateItem(ViewGroup* container, int position) { void* instantiateItem(ViewGroup* container, int position) {
SimpleMonthView*sm=new SimpleMonthView(100,100); if(position<getCount()/2){
sm->setMonthParams(23,Calendar::MAY+position,2021,-1,1,31); SimpleMonthView*sm=new SimpleMonthView(100,100);
container->addView(sm); sm->setMonthParams(23,Calendar::MAY+position,2021,-1,1,31);
return sm; container->addView(sm);
return sm;
}else{
View*sm=new View(100,100);
sm->setBackground(new ColorDrawable(0xFF000000|(0xFF<<((position%3)*8))));
container->addView(sm);
return sm;
}
} }
void destroyItem(ViewGroup* container, int position,void* object){ void destroyItem(ViewGroup* container, int position,void* object){
container->removeView((View*)object); container->removeView((View*)object);
@ -20,6 +27,7 @@ public:
int main(int argc,const char*argv[]){ int main(int argc,const char*argv[]){
App app(argc,argv); App app(argc,argv);
AnimationHandler::getInstance().setFrameDelay(100);
Window*w=new Window(0,0,800,600); Window*w=new Window(0,0,800,600);
HorizontalScrollView* hs=new HorizontalScrollView(800,400); HorizontalScrollView* hs=new HorizontalScrollView(800,400);
LinearLayout*layout=new LinearLayout(400,100); LinearLayout*layout=new LinearLayout(400,100);
@ -42,7 +50,7 @@ int main(int argc,const char*argv[]){
} }
ViewPager*pager=new ViewPager(800,560); ViewPager*pager=new ViewPager(800,560);
MyPageAdapter*gpAdapter=new MyPageAdapter(); MyPageAdapter*gpAdapter=new MyPageAdapter();
pager->setOffscreenPageLimit(3); pager->setOffscreenPageLimit(8);
pager->setAdapter(gpAdapter); pager->setAdapter(gpAdapter);
ViewPager::OnPageChangeListener listener; ViewPager::OnPageChangeListener listener;
listener.onPageSelected=[&](int position){ listener.onPageSelected=[&](int position){

View File

@ -21,6 +21,19 @@ InputEventSource::InputEventSource(const std::string&file){
frecord.open(file); frecord.open(file);
isplayback=false; isplayback=false;
lasteventTime=SystemClock::uptimeMillis(); lasteventTime=SystemClock::uptimeMillis();
auto func=[this](){
while(1){
std::this_thread::sleep_for(std::chrono::microseconds(10));
std::lock_guard<std::mutex> lock(mtxEvents);
INPUTEVENT es[32];
int count=InputGetEvents(es,32,0);
LOGV_IF(count,"rcv %d rawEvents",count);
for(int i=0;i<count;i++)
mRawEvents.push(es[i]);
}
};
std::thread th(func);
th.detach();
} }
InputEventSource::~InputEventSource(){ InputEventSource::~InputEventSource(){
@ -54,13 +67,13 @@ std::shared_ptr<InputDevice>InputEventSource::getdevice(int fd){
dev.reset(new MouseDevice(fd)); dev.reset(new MouseDevice(fd));
dev->setEventConsumeListener([&](const InputEvent&e){ dev->setEventConsumeListener([&](const InputEvent&e){
MotionEvent*mt=MotionEvent::obtain((MotionEvent&)e); MotionEvent*mt=MotionEvent::obtain((MotionEvent&)e);
events.push(mt); mInputEvents.push(mt);
}); });
}else if(tmpdev.getClasses()&(INPUT_DEVICE_CLASS_KEYBOARD)){ }else if(tmpdev.getClasses()&(INPUT_DEVICE_CLASS_KEYBOARD)){
dev.reset(new KeyDevice(fd)); dev.reset(new KeyDevice(fd));
dev->setEventConsumeListener([&](const InputEvent&e){ dev->setEventConsumeListener([&](const InputEvent&e){
KeyEvent*key=KeyEvent::obtain((KeyEvent&)e); KeyEvent*key=KeyEvent::obtain((KeyEvent&)e);
events.push(key); mInputEvents.push(key);
}); });
} }
devices.emplace(fd,dev); devices.emplace(fd,dev);
@ -70,18 +83,16 @@ std::shared_ptr<InputDevice>InputEventSource::getdevice(int fd){
} }
int InputEventSource::checkEvents(){ int InputEventSource::checkEvents(){
INPUTEVENT es[32]; std::lock_guard<std::mutex> lock(mtxEvents);
if(events.size()>0)return TRUE; process();
int count=InputGetEvents(es,32,1); return mInputEvents.size()>0;
process(es,count);
return count;
} }
int InputEventSource::handleEvents(){ int InputEventSource::handleEvents(){
std::unique_lock<std::mutex> lock(mtxEvents); std::lock_guard<std::mutex> lock(mtxEvents);
if(events.size()==0)return false; if(mInputEvents.size()==0)return false;
while(events.size()){ while(mInputEvents.size()){
InputEvent* e=events.front(); InputEvent* e=mInputEvents.front();
WindowManager::getInstance().processEvent(*e); WindowManager::getInstance().processEvent(*e);
if((!isplayback)&& frecord.is_open() && dynamic_cast<KeyEvent*>(e) ){ if((!isplayback)&& frecord.is_open() && dynamic_cast<KeyEvent*>(e) ){
nsecs_t eventTime=SystemClock::uptimeMillis(); nsecs_t eventTime=SystemClock::uptimeMillis();
@ -92,31 +103,31 @@ int InputEventSource::handleEvents(){
lasteventTime=eventTime; lasteventTime=eventTime;
} }
e->recycle(); e->recycle();
events.pop(); mInputEvents.pop();
} }
return 0; return 0;
} }
int InputEventSource::process(const INPUTEVENT*inevents,int count){ int InputEventSource::process(){
LOGV_IF(count,"%p recv %d events ",this,count); LOGD_IF(mRawEvents.size(),"%p recv %d events ",this,mRawEvents.size());
std::unique_lock<std::mutex> lock(mtxEvents); while(mRawEvents.size()){
for(int i=0;i<count;i++){ const INPUTEVENT e=mRawEvents.front();
const INPUTEVENT*e=inevents+i; struct timeval tv={e.tv_sec,e.tv_usec};
struct timeval tv={e->tv_sec,e->tv_usec}; std::shared_ptr<InputDevice>dev=getdevice(e.device);
std::shared_ptr<InputDevice>dev=getdevice(e->device); mRawEvents.pop();
if(dev==nullptr){ if(dev==nullptr){
LOGD("%d,%d,%d device=%d ",e->type,e->code,e->value,e->device); LOGD("%d,%d,%d device=%d ",e.type,e.code,e.value,e.device);
continue; continue;
} }
dev->putRawEvent(tv,e->type,e->code,e->value); dev->putRawEvent(tv,e.type,e.code,e.value);
} }
return 0; return 0;
} }
int InputEventSource::pushEvent(InputEvent*evt){ int InputEventSource::pushEvent(InputEvent*evt){
std::unique_lock<std::mutex> lock(mtxEvents); std::lock_guard<std::mutex> lock(mtxEvents);
events.push(evt); mInputEvents.push(evt);
return events.size(); return mInputEvents.size();
} }
void InputEventSource::playback(const std::string&fname){ void InputEventSource::playback(const std::string&fname){
@ -150,7 +161,7 @@ void InputEventSource::playback(const std::string&fname){
int keycode=KeyEvent::getKeyCodeFromLabel(word.c_str()); int keycode=KeyEvent::getKeyCodeFromLabel(word.c_str());
KeyEvent*key=KeyEvent::obtain(evttime,evttime,action,keycode,1,0/*metastate*/, KeyEvent*key=KeyEvent::obtain(evttime,evttime,action,keycode,1,0/*metastate*/,
0/*deviceid*/,keycode/*scancode*/,0/*flags*/,0/*source*/); 0/*deviceid*/,keycode/*scancode*/,0/*flags*/,0/*source*/);
events.push(key); mInputEvents.push(key);
} }
if(in.gcount()==0){ if(in.gcount()==0){
in.close(); in.close();

View File

@ -24,13 +24,15 @@ protected:
bool isplayback; bool isplayback;
nsecs_t lasteventTime; nsecs_t lasteventTime;
std::ofstream frecord; std::ofstream frecord;
std::queue<InputEvent*>events; std::queue<InputEvent*>mInputEvents;
std::queue<INPUTEVENT>mRawEvents;
#if ENABLED_GESTURE #if ENABLED_GESTURE
std::unique_ptr<GRT::GestureRecognitionPipeline>pipeline; std::unique_ptr<GRT::GestureRecognitionPipeline>pipeline;
#endif #endif
std::unordered_map<int,std::shared_ptr<InputDevice>>devices; std::unordered_map<int,std::shared_ptr<InputDevice>>devices;
std::shared_ptr<InputDevice>getdevice(int fd); std::shared_ptr<InputDevice>getdevice(int fd);
int pushEvent(InputEvent*evt); int pushEvent(InputEvent*evt);
int process();
public: public:
InputEventSource(const std::string&recordfile=std::string() ); InputEventSource(const std::string&recordfile=std::string() );
~InputEventSource(); ~InputEventSource();
@ -38,7 +40,6 @@ public:
void playback(const std::string&fname); void playback(const std::string&fname);
int checkEvents()override; int checkEvents()override;
int handleEvents()override; int handleEvents()override;
int process(const INPUTEVENT*es,int count);
}; };
} }
#endif #endif