InputReader.cpp 278 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "InputReader"
  17. //#define LOG_NDEBUG 0
  18. // Log debug messages for each raw event received from the EventHub.
  19. #define DEBUG_RAW_EVENTS 0
  20. // Log debug messages about touch screen filtering hacks.
  21. #define DEBUG_HACKS 0
  22. // Log debug messages about virtual key processing.
  23. #define DEBUG_VIRTUAL_KEYS 0
  24. // Log debug messages about pointers.
  25. #define DEBUG_POINTERS 0
  26. // Log debug messages about pointer assignment calculations.
  27. #define DEBUG_POINTER_ASSIGNMENT 0
  28. // Log debug messages about gesture detection.
  29. #define DEBUG_GESTURES 0
  30. // Log debug messages about the vibrator.
  31. #define DEBUG_VIBRATOR 0
  32. // Log debug messages about fusing stylus data.
  33. #define DEBUG_STYLUS_FUSION 0
  34. #include "InputReader.h"
  35. #include <cutils/log.h>
  36. #include <input/Keyboard.h>
  37. #include <input/VirtualKeyMap.h>
  38. #include <inttypes.h>
  39. #include <stddef.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <errno.h>
  43. #include <limits.h>
  44. #include <math.h>
  45. #define INDENT " "
  46. #define INDENT2 " "
  47. #define INDENT3 " "
  48. #define INDENT4 " "
  49. #define INDENT5 " "
  50. namespace android {
  51. // --- Constants ---
  52. // Maximum number of slots supported when using the slot-based Multitouch Protocol B.
  53. static const size_t MAX_SLOTS = 32;
  54. // Maximum amount of latency to add to touch events while waiting for data from an
  55. // external stylus.
  56. static const nsecs_t EXTERNAL_STYLUS_DATA_TIMEOUT = ms2ns(72);
  57. // Maximum amount of time to wait on touch data before pushing out new pressure data.
  58. static const nsecs_t TOUCH_DATA_TIMEOUT = ms2ns(20);
  59. // Artificial latency on synthetic events created from stylus data without corresponding touch
  60. // data.
  61. static const nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
  62. // --- Static Functions ---
  63. template<typename T>
  64. inline static T abs(const T& value) {
  65. return value < 0 ? - value : value;
  66. }
  67. template<typename T>
  68. inline static T min(const T& a, const T& b) {
  69. return a < b ? a : b;
  70. }
  71. template<typename T>
  72. inline static void swap(T& a, T& b) {
  73. T temp = a;
  74. a = b;
  75. b = temp;
  76. }
  77. inline static float avg(float x, float y) {
  78. return (x + y) / 2;
  79. }
  80. inline static float distance(float x1, float y1, float x2, float y2) {
  81. return hypotf(x1 - x2, y1 - y2);
  82. }
  83. inline static int32_t signExtendNybble(int32_t value) {
  84. return value >= 8 ? value - 16 : value;
  85. }
  86. static inline const char* toString(bool value) {
  87. return value ? "true" : "false";
  88. }
  89. static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
  90. const int32_t map[][4], size_t mapSize, int32_t rotationMapOffset) {
  91. if (orientation != DISPLAY_ORIENTATION_0) {
  92. for (size_t i = rotationMapOffset; i < mapSize; i++) {
  93. if (value == map[i][0]) {
  94. return map[i][orientation];
  95. }
  96. }
  97. }
  98. return value;
  99. }
  100. static const int32_t keyCodeRotationMap[][4] = {
  101. // key codes enumerated counter-clockwise with the original (unrotated) key first
  102. // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
  103. // volume keys - tablet
  104. { AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN },
  105. { AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_UP },
  106. // volume keys - phone or hybrid
  107. { AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP },
  108. { AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_UP, AKEYCODE_VOLUME_DOWN },
  109. // dpad keys - common
  110. { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
  111. { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
  112. { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
  113. { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
  114. };
  115. static const size_t keyCodeRotationMapSize =
  116. sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
  117. static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation, int32_t rotationMapOffset) {
  118. return rotateValueUsingRotationMap(keyCode, orientation,
  119. keyCodeRotationMap, keyCodeRotationMapSize, rotationMapOffset);
  120. }
  121. static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
  122. float temp;
  123. switch (orientation) {
  124. case DISPLAY_ORIENTATION_90:
  125. temp = *deltaX;
  126. *deltaX = *deltaY;
  127. *deltaY = -temp;
  128. break;
  129. case DISPLAY_ORIENTATION_180:
  130. *deltaX = -*deltaX;
  131. *deltaY = -*deltaY;
  132. break;
  133. case DISPLAY_ORIENTATION_270:
  134. temp = *deltaX;
  135. *deltaX = -*deltaY;
  136. *deltaY = temp;
  137. break;
  138. }
  139. }
  140. static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
  141. return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
  142. }
  143. // Returns true if the pointer should be reported as being down given the specified
  144. // button states. This determines whether the event is reported as a touch event.
  145. static bool isPointerDown(int32_t buttonState) {
  146. return buttonState &
  147. (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
  148. | AMOTION_EVENT_BUTTON_TERTIARY);
  149. }
  150. static float calculateCommonVector(float a, float b) {
  151. if (a > 0 && b > 0) {
  152. return a < b ? a : b;
  153. } else if (a < 0 && b < 0) {
  154. return a > b ? a : b;
  155. } else {
  156. return 0;
  157. }
  158. }
  159. static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
  160. nsecs_t when, int32_t deviceId, uint32_t source,
  161. uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
  162. int32_t buttonState, int32_t keyCode) {
  163. if (
  164. (action == AKEY_EVENT_ACTION_DOWN
  165. && !(lastButtonState & buttonState)
  166. && (currentButtonState & buttonState))
  167. || (action == AKEY_EVENT_ACTION_UP
  168. && (lastButtonState & buttonState)
  169. && !(currentButtonState & buttonState))) {
  170. NotifyKeyArgs args(when, deviceId, source, policyFlags,
  171. action, 0, keyCode, 0, context->getGlobalMetaState(), when);
  172. context->getListener()->notifyKey(&args);
  173. }
  174. }
  175. static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
  176. nsecs_t when, int32_t deviceId, uint32_t source,
  177. uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
  178. synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
  179. lastButtonState, currentButtonState,
  180. AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
  181. synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
  182. lastButtonState, currentButtonState,
  183. AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
  184. }
  185. // --- InputReaderConfiguration ---
  186. bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const {
  187. const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay;
  188. if (viewport.displayId >= 0) {
  189. *outViewport = viewport;
  190. return true;
  191. }
  192. return false;
  193. }
  194. void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) {
  195. DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay;
  196. v = viewport;
  197. }
  198. // -- TouchAffineTransformation --
  199. void TouchAffineTransformation::applyTo(float& x, float& y) const {
  200. float newX, newY;
  201. newX = x * x_scale + y * x_ymix + x_offset;
  202. newY = x * y_xmix + y * y_scale + y_offset;
  203. x = newX;
  204. y = newY;
  205. }
  206. // --- InputReader ---
  207. InputReader::InputReader(const sp<EventHubInterface>& eventHub,
  208. const sp<InputReaderPolicyInterface>& policy,
  209. const sp<InputListenerInterface>& listener) :
  210. mContext(this), mEventHub(eventHub), mPolicy(policy),
  211. mGlobalMetaState(0), mGeneration(1),
  212. mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
  213. mConfigurationChangesToRefresh(0) {
  214. mQueuedListener = new QueuedInputListener(listener);
  215. { // acquire lock
  216. AutoMutex _l(mLock);
  217. refreshConfigurationLocked(0);
  218. updateGlobalMetaStateLocked();
  219. } // release lock
  220. }
  221. InputReader::~InputReader() {
  222. for (size_t i = 0; i < mDevices.size(); i++) {
  223. delete mDevices.valueAt(i);
  224. }
  225. }
  226. void InputReader::loopOnce() {
  227. int32_t oldGeneration;
  228. int32_t timeoutMillis;
  229. bool inputDevicesChanged = false;
  230. Vector<InputDeviceInfo> inputDevices;
  231. { // acquire lock
  232. AutoMutex _l(mLock);
  233. oldGeneration = mGeneration;
  234. timeoutMillis = -1;
  235. uint32_t changes = mConfigurationChangesToRefresh;
  236. if (changes) {
  237. mConfigurationChangesToRefresh = 0;
  238. timeoutMillis = 0;
  239. refreshConfigurationLocked(changes);
  240. } else if (mNextTimeout != LLONG_MAX) {
  241. nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
  242. timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
  243. }
  244. } // release lock
  245. size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
  246. { // acquire lock
  247. AutoMutex _l(mLock);
  248. mReaderIsAliveCondition.broadcast();
  249. if (count) {
  250. processEventsLocked(mEventBuffer, count);
  251. }
  252. if (mNextTimeout != LLONG_MAX) {
  253. nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
  254. if (now >= mNextTimeout) {
  255. #if DEBUG_RAW_EVENTS
  256. ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
  257. #endif
  258. mNextTimeout = LLONG_MAX;
  259. timeoutExpiredLocked(now);
  260. }
  261. }
  262. if (oldGeneration != mGeneration) {
  263. inputDevicesChanged = true;
  264. getInputDevicesLocked(inputDevices);
  265. }
  266. } // release lock
  267. // Send out a message that the describes the changed input devices.
  268. if (inputDevicesChanged) {
  269. mPolicy->notifyInputDevicesChanged(inputDevices);
  270. }
  271. // Flush queued events out to the listener.
  272. // This must happen outside of the lock because the listener could potentially call
  273. // back into the InputReader's methods, such as getScanCodeState, or become blocked
  274. // on another thread similarly waiting to acquire the InputReader lock thereby
  275. // resulting in a deadlock. This situation is actually quite plausible because the
  276. // listener is actually the input dispatcher, which calls into the window manager,
  277. // which occasionally calls into the input reader.
  278. mQueuedListener->flush();
  279. }
  280. void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
  281. for (const RawEvent* rawEvent = rawEvents; count;) {
  282. int32_t type = rawEvent->type;
  283. size_t batchSize = 1;
  284. if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
  285. int32_t deviceId = rawEvent->deviceId;
  286. while (batchSize < count) {
  287. if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
  288. || rawEvent[batchSize].deviceId != deviceId) {
  289. break;
  290. }
  291. batchSize += 1;
  292. }
  293. #if DEBUG_RAW_EVENTS
  294. ALOGD("BatchSize: %d Count: %d", batchSize, count);
  295. #endif
  296. processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
  297. } else {
  298. switch (rawEvent->type) {
  299. case EventHubInterface::DEVICE_ADDED:
  300. addDeviceLocked(rawEvent->when, rawEvent->deviceId);
  301. break;
  302. case EventHubInterface::DEVICE_REMOVED:
  303. removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
  304. break;
  305. case EventHubInterface::FINISHED_DEVICE_SCAN:
  306. handleConfigurationChangedLocked(rawEvent->when);
  307. break;
  308. default:
  309. ALOG_ASSERT(false); // can't happen
  310. break;
  311. }
  312. }
  313. count -= batchSize;
  314. rawEvent += batchSize;
  315. }
  316. }
  317. void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
  318. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  319. if (deviceIndex >= 0) {
  320. ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
  321. return;
  322. }
  323. InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
  324. uint32_t classes = mEventHub->getDeviceClasses(deviceId);
  325. int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);
  326. InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes);
  327. device->configure(when, &mConfig, 0);
  328. device->reset(when);
  329. if (device->isIgnored()) {
  330. ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
  331. identifier.name.string());
  332. } else {
  333. ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
  334. identifier.name.string(), device->getSources());
  335. }
  336. mDevices.add(deviceId, device);
  337. bumpGenerationLocked();
  338. if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
  339. notifyExternalStylusPresenceChanged();
  340. }
  341. }
  342. void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
  343. InputDevice* device = NULL;
  344. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  345. if (deviceIndex < 0) {
  346. ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
  347. return;
  348. }
  349. device = mDevices.valueAt(deviceIndex);
  350. mDevices.removeItemsAt(deviceIndex, 1);
  351. bumpGenerationLocked();
  352. if (device->isIgnored()) {
  353. ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
  354. device->getId(), device->getName().string());
  355. } else {
  356. ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
  357. device->getId(), device->getName().string(), device->getSources());
  358. }
  359. if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
  360. notifyExternalStylusPresenceChanged();
  361. }
  362. device->reset(when);
  363. delete device;
  364. }
  365. InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
  366. const InputDeviceIdentifier& identifier, uint32_t classes) {
  367. InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
  368. controllerNumber, identifier, classes);
  369. // External devices.
  370. if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
  371. device->setExternal(true);
  372. }
  373. // Devices with mics.
  374. if (classes & INPUT_DEVICE_CLASS_MIC) {
  375. device->setMic(true);
  376. }
  377. // Switch-like devices.
  378. if (classes & INPUT_DEVICE_CLASS_SWITCH) {
  379. device->addMapper(new SwitchInputMapper(device));
  380. }
  381. // Vibrator-like devices.
  382. if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
  383. device->addMapper(new VibratorInputMapper(device));
  384. }
  385. // Keyboard-like devices.
  386. uint32_t keyboardSource = 0;
  387. int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
  388. if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
  389. keyboardSource |= AINPUT_SOURCE_KEYBOARD;
  390. }
  391. if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
  392. keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
  393. }
  394. if (classes & INPUT_DEVICE_CLASS_DPAD) {
  395. keyboardSource |= AINPUT_SOURCE_DPAD;
  396. }
  397. if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
  398. keyboardSource |= AINPUT_SOURCE_GAMEPAD;
  399. }
  400. if (keyboardSource != 0) {
  401. device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
  402. }
  403. // Cursor-like devices.
  404. if (classes & INPUT_DEVICE_CLASS_CURSOR) {
  405. device->addMapper(new CursorInputMapper(device));
  406. }
  407. // Touchscreens and touchpad devices.
  408. if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
  409. device->addMapper(new MultiTouchInputMapper(device));
  410. } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
  411. device->addMapper(new SingleTouchInputMapper(device));
  412. }
  413. // Joystick-like devices.
  414. if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
  415. device->addMapper(new JoystickInputMapper(device));
  416. }
  417. // External stylus-like devices.
  418. if (classes & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
  419. device->addMapper(new ExternalStylusInputMapper(device));
  420. }
  421. return device;
  422. }
  423. void InputReader::processEventsForDeviceLocked(int32_t deviceId,
  424. const RawEvent* rawEvents, size_t count) {
  425. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  426. if (deviceIndex < 0) {
  427. ALOGW("Discarding event for unknown deviceId %d.", deviceId);
  428. return;
  429. }
  430. InputDevice* device = mDevices.valueAt(deviceIndex);
  431. if (device->isIgnored()) {
  432. //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
  433. return;
  434. }
  435. device->process(rawEvents, count);
  436. }
  437. void InputReader::timeoutExpiredLocked(nsecs_t when) {
  438. for (size_t i = 0; i < mDevices.size(); i++) {
  439. InputDevice* device = mDevices.valueAt(i);
  440. if (!device->isIgnored()) {
  441. device->timeoutExpired(when);
  442. }
  443. }
  444. }
  445. void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
  446. // Reset global meta state because it depends on the list of all configured devices.
  447. updateGlobalMetaStateLocked();
  448. // Enqueue configuration changed.
  449. NotifyConfigurationChangedArgs args(when);
  450. mQueuedListener->notifyConfigurationChanged(&args);
  451. }
  452. void InputReader::refreshConfigurationLocked(uint32_t changes) {
  453. mPolicy->getReaderConfiguration(&mConfig);
  454. mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
  455. if (changes) {
  456. ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
  457. nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
  458. if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
  459. mEventHub->requestReopenDevices();
  460. } else {
  461. for (size_t i = 0; i < mDevices.size(); i++) {
  462. InputDevice* device = mDevices.valueAt(i);
  463. device->configure(now, &mConfig, changes);
  464. }
  465. }
  466. }
  467. }
  468. void InputReader::updateGlobalMetaStateLocked() {
  469. mGlobalMetaState = 0;
  470. for (size_t i = 0; i < mDevices.size(); i++) {
  471. InputDevice* device = mDevices.valueAt(i);
  472. mGlobalMetaState |= device->getMetaState();
  473. }
  474. }
  475. int32_t InputReader::getGlobalMetaStateLocked() {
  476. return mGlobalMetaState;
  477. }
  478. void InputReader::notifyExternalStylusPresenceChanged() {
  479. refreshConfigurationLocked(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
  480. }
  481. void InputReader::getExternalStylusDevicesLocked(Vector<InputDeviceInfo>& outDevices) {
  482. for (size_t i = 0; i < mDevices.size(); i++) {
  483. InputDevice* device = mDevices.valueAt(i);
  484. if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS && !device->isIgnored()) {
  485. outDevices.push();
  486. device->getDeviceInfo(&outDevices.editTop());
  487. }
  488. }
  489. }
  490. void InputReader::dispatchExternalStylusState(const StylusState& state) {
  491. for (size_t i = 0; i < mDevices.size(); i++) {
  492. InputDevice* device = mDevices.valueAt(i);
  493. device->updateExternalStylusState(state);
  494. }
  495. }
  496. void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
  497. mDisableVirtualKeysTimeout = time;
  498. }
  499. bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
  500. InputDevice* device, int32_t keyCode, int32_t scanCode) {
  501. if (now < mDisableVirtualKeysTimeout) {
  502. ALOGI("Dropping virtual key from device %s because virtual keys are "
  503. "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
  504. device->getName().string(),
  505. (mDisableVirtualKeysTimeout - now) * 0.000001,
  506. keyCode, scanCode);
  507. return true;
  508. } else {
  509. return false;
  510. }
  511. }
  512. void InputReader::fadePointerLocked() {
  513. for (size_t i = 0; i < mDevices.size(); i++) {
  514. InputDevice* device = mDevices.valueAt(i);
  515. device->fadePointer();
  516. }
  517. }
  518. void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
  519. if (when < mNextTimeout) {
  520. mNextTimeout = when;
  521. mEventHub->wake();
  522. }
  523. }
  524. int32_t InputReader::bumpGenerationLocked() {
  525. return ++mGeneration;
  526. }
  527. void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
  528. AutoMutex _l(mLock);
  529. getInputDevicesLocked(outInputDevices);
  530. }
  531. void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
  532. outInputDevices.clear();
  533. size_t numDevices = mDevices.size();
  534. for (size_t i = 0; i < numDevices; i++) {
  535. InputDevice* device = mDevices.valueAt(i);
  536. if (!device->isIgnored()) {
  537. outInputDevices.push();
  538. device->getDeviceInfo(&outInputDevices.editTop());
  539. }
  540. }
  541. }
  542. int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
  543. int32_t keyCode) {
  544. AutoMutex _l(mLock);
  545. return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
  546. }
  547. int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
  548. int32_t scanCode) {
  549. AutoMutex _l(mLock);
  550. return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
  551. }
  552. int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
  553. AutoMutex _l(mLock);
  554. return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
  555. }
  556. int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
  557. GetStateFunc getStateFunc) {
  558. int32_t result = AKEY_STATE_UNKNOWN;
  559. if (deviceId >= 0) {
  560. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  561. if (deviceIndex >= 0) {
  562. InputDevice* device = mDevices.valueAt(deviceIndex);
  563. if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
  564. result = (device->*getStateFunc)(sourceMask, code);
  565. }
  566. }
  567. } else {
  568. size_t numDevices = mDevices.size();
  569. for (size_t i = 0; i < numDevices; i++) {
  570. InputDevice* device = mDevices.valueAt(i);
  571. if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
  572. // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
  573. // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
  574. int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
  575. if (currentResult >= AKEY_STATE_DOWN) {
  576. return currentResult;
  577. } else if (currentResult == AKEY_STATE_UP) {
  578. result = currentResult;
  579. }
  580. }
  581. }
  582. }
  583. return result;
  584. }
  585. bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
  586. size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
  587. AutoMutex _l(mLock);
  588. memset(outFlags, 0, numCodes);
  589. return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
  590. }
  591. bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
  592. size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
  593. bool result = false;
  594. if (deviceId >= 0) {
  595. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  596. if (deviceIndex >= 0) {
  597. InputDevice* device = mDevices.valueAt(deviceIndex);
  598. if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
  599. result = device->markSupportedKeyCodes(sourceMask,
  600. numCodes, keyCodes, outFlags);
  601. }
  602. }
  603. } else {
  604. size_t numDevices = mDevices.size();
  605. for (size_t i = 0; i < numDevices; i++) {
  606. InputDevice* device = mDevices.valueAt(i);
  607. if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
  608. result |= device->markSupportedKeyCodes(sourceMask,
  609. numCodes, keyCodes, outFlags);
  610. }
  611. }
  612. }
  613. return result;
  614. }
  615. void InputReader::requestRefreshConfiguration(uint32_t changes) {
  616. AutoMutex _l(mLock);
  617. if (changes) {
  618. bool needWake = !mConfigurationChangesToRefresh;
  619. mConfigurationChangesToRefresh |= changes;
  620. if (needWake) {
  621. mEventHub->wake();
  622. }
  623. }
  624. }
  625. void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
  626. ssize_t repeat, int32_t token) {
  627. AutoMutex _l(mLock);
  628. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  629. if (deviceIndex >= 0) {
  630. InputDevice* device = mDevices.valueAt(deviceIndex);
  631. device->vibrate(pattern, patternSize, repeat, token);
  632. }
  633. }
  634. void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
  635. AutoMutex _l(mLock);
  636. ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
  637. if (deviceIndex >= 0) {
  638. InputDevice* device = mDevices.valueAt(deviceIndex);
  639. device->cancelVibrate(token);
  640. }
  641. }
  642. void InputReader::dump(String8& dump) {
  643. AutoMutex _l(mLock);
  644. mEventHub->dump(dump);
  645. dump.append("\n");
  646. dump.append("Input Reader State:\n");
  647. for (size_t i = 0; i < mDevices.size(); i++) {
  648. mDevices.valueAt(i)->dump(dump);
  649. }
  650. dump.append(INDENT "Configuration:\n");
  651. dump.append(INDENT2 "ExcludedDeviceNames: [");
  652. for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
  653. if (i != 0) {
  654. dump.append(", ");
  655. }
  656. dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
  657. }
  658. dump.append("]\n");
  659. dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
  660. mConfig.virtualKeyQuietTime * 0.000001f);
  661. dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
  662. "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
  663. mConfig.pointerVelocityControlParameters.scale,
  664. mConfig.pointerVelocityControlParameters.lowThreshold,
  665. mConfig.pointerVelocityControlParameters.highThreshold,
  666. mConfig.pointerVelocityControlParameters.acceleration);
  667. dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
  668. "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
  669. mConfig.wheelVelocityControlParameters.scale,
  670. mConfig.wheelVelocityControlParameters.lowThreshold,
  671. mConfig.wheelVelocityControlParameters.highThreshold,
  672. mConfig.wheelVelocityControlParameters.acceleration);
  673. dump.appendFormat(INDENT2 "PointerGesture:\n");
  674. dump.appendFormat(INDENT3 "Enabled: %s\n",
  675. toString(mConfig.pointerGesturesEnabled));
  676. dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
  677. mConfig.pointerGestureQuietInterval * 0.000001f);
  678. dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
  679. mConfig.pointerGestureDragMinSwitchSpeed);
  680. dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
  681. mConfig.pointerGestureTapInterval * 0.000001f);
  682. dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
  683. mConfig.pointerGestureTapDragInterval * 0.000001f);
  684. dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
  685. mConfig.pointerGestureTapSlop);
  686. dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
  687. mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
  688. dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
  689. mConfig.pointerGestureMultitouchMinDistance);
  690. dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
  691. mConfig.pointerGestureSwipeTransitionAngleCosine);
  692. dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
  693. mConfig.pointerGestureSwipeMaxWidthRatio);
  694. dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
  695. mConfig.pointerGestureMovementSpeedRatio);
  696. dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
  697. mConfig.pointerGestureZoomSpeedRatio);
  698. }
  699. void InputReader::monitor() {
  700. // Acquire and release the lock to ensure that the reader has not deadlocked.
  701. mLock.lock();
  702. mEventHub->wake();
  703. mReaderIsAliveCondition.wait(mLock);
  704. mLock.unlock();
  705. // Check the EventHub
  706. mEventHub->monitor();
  707. }
  708. // --- InputReader::ContextImpl ---
  709. InputReader::ContextImpl::ContextImpl(InputReader* reader) :
  710. mReader(reader) {
  711. }
  712. void InputReader::ContextImpl::updateGlobalMetaState() {
  713. // lock is already held by the input loop
  714. mReader->updateGlobalMetaStateLocked();
  715. }
  716. int32_t InputReader::ContextImpl::getGlobalMetaState() {
  717. // lock is already held by the input loop
  718. return mReader->getGlobalMetaStateLocked();
  719. }
  720. void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
  721. // lock is already held by the input loop
  722. mReader->disableVirtualKeysUntilLocked(time);
  723. }
  724. bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
  725. InputDevice* device, int32_t keyCode, int32_t scanCode) {
  726. // lock is already held by the input loop
  727. return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
  728. }
  729. void InputReader::ContextImpl::fadePointer() {
  730. // lock is already held by the input loop
  731. mReader->fadePointerLocked();
  732. }
  733. void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
  734. // lock is already held by the input loop
  735. mReader->requestTimeoutAtTimeLocked(when);
  736. }
  737. int32_t InputReader::ContextImpl::bumpGeneration() {
  738. // lock is already held by the input loop
  739. return mReader->bumpGenerationLocked();
  740. }
  741. void InputReader::ContextImpl::getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
  742. // lock is already held by whatever called refreshConfigurationLocked
  743. mReader->getExternalStylusDevicesLocked(outDevices);
  744. }
  745. void InputReader::ContextImpl::dispatchExternalStylusState(const StylusState& state) {
  746. mReader->dispatchExternalStylusState(state);
  747. }
  748. InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
  749. return mReader->mPolicy.get();
  750. }
  751. InputListenerInterface* InputReader::ContextImpl::getListener() {
  752. return mReader->mQueuedListener.get();
  753. }
  754. EventHubInterface* InputReader::ContextImpl::getEventHub() {
  755. return mReader->mEventHub.get();
  756. }
  757. // --- InputReaderThread ---
  758. InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
  759. Thread(/*canCallJava*/ true), mReader(reader) {
  760. }
  761. InputReaderThread::~InputReaderThread() {
  762. }
  763. bool InputReaderThread::threadLoop() {
  764. mReader->loopOnce();
  765. return true;
  766. }
  767. // --- InputDevice ---
  768. InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
  769. int32_t controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes) :
  770. mContext(context), mId(id), mGeneration(generation), mControllerNumber(controllerNumber),
  771. mIdentifier(identifier), mClasses(classes),
  772. mSources(0), mIsExternal(false), mHasMic(false), mDropUntilNextSync(false) {
  773. }
  774. InputDevice::~InputDevice() {
  775. size_t numMappers = mMappers.size();
  776. for (size_t i = 0; i < numMappers; i++) {
  777. delete mMappers[i];
  778. }
  779. mMappers.clear();
  780. }
  781. void InputDevice::dump(String8& dump) {
  782. InputDeviceInfo deviceInfo;
  783. getDeviceInfo(& deviceInfo);
  784. dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
  785. deviceInfo.getDisplayName().string());
  786. dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
  787. dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
  788. dump.appendFormat(INDENT2 "HasMic: %s\n", toString(mHasMic));
  789. dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
  790. dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
  791. const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
  792. if (!ranges.isEmpty()) {
  793. dump.append(INDENT2 "Motion Ranges:\n");
  794. for (size_t i = 0; i < ranges.size(); i++) {
  795. const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
  796. const char* label = getAxisLabel(range.axis);
  797. char name[32];
  798. if (label) {
  799. strncpy(name, label, sizeof(name));
  800. name[sizeof(name) - 1] = '\0';
  801. } else {
  802. snprintf(name, sizeof(name), "%d", range.axis);
  803. }
  804. dump.appendFormat(INDENT3 "%s: source=0x%08x, "
  805. "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
  806. name, range.source, range.min, range.max, range.flat, range.fuzz,
  807. range.resolution);
  808. }
  809. }
  810. size_t numMappers = mMappers.size();
  811. for (size_t i = 0; i < numMappers; i++) {
  812. InputMapper* mapper = mMappers[i];
  813. mapper->dump(dump);
  814. }
  815. }
  816. void InputDevice::addMapper(InputMapper* mapper) {
  817. mMappers.add(mapper);
  818. }
  819. void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
  820. mSources = 0;
  821. if (!isIgnored()) {
  822. if (!changes) { // first time only
  823. mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
  824. }
  825. if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
  826. if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
  827. sp<KeyCharacterMap> keyboardLayout =
  828. mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
  829. if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
  830. bumpGeneration();
  831. }
  832. }
  833. }
  834. if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
  835. if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
  836. String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
  837. if (mAlias != alias) {
  838. mAlias = alias;
  839. bumpGeneration();
  840. }
  841. }
  842. }
  843. size_t numMappers = mMappers.size();
  844. for (size_t i = 0; i < numMappers; i++) {
  845. InputMapper* mapper = mMappers[i];
  846. mapper->configure(when, config, changes);
  847. mSources |= mapper->getSources();
  848. }
  849. }
  850. }
  851. void InputDevice::reset(nsecs_t when) {
  852. size_t numMappers = mMappers.size();
  853. for (size_t i = 0; i < numMappers; i++) {
  854. InputMapper* mapper = mMappers[i];
  855. mapper->reset(when);
  856. }
  857. mContext->updateGlobalMetaState();
  858. notifyReset(when);
  859. }
  860. void InputDevice::process(const RawEvent* rawEvents, size_t count) {
  861. // Process all of the events in order for each mapper.
  862. // We cannot simply ask each mapper to process them in bulk because mappers may
  863. // have side-effects that must be interleaved. For example, joystick movement events and
  864. // gamepad button presses are handled by different mappers but they should be dispatched
  865. // in the order received.
  866. size_t numMappers = mMappers.size();
  867. for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
  868. #if DEBUG_RAW_EVENTS
  869. ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
  870. rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
  871. rawEvent->when);
  872. #endif
  873. if (mDropUntilNextSync) {
  874. if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
  875. mDropUntilNextSync = false;
  876. #if DEBUG_RAW_EVENTS
  877. ALOGD("Recovered from input event buffer overrun.");
  878. #endif
  879. } else {
  880. #if DEBUG_RAW_EVENTS
  881. ALOGD("Dropped input event while waiting for next input sync.");
  882. #endif
  883. }
  884. } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
  885. ALOGI("Detected input event buffer overrun for device %s.", getName().string());
  886. mDropUntilNextSync = true;
  887. reset(rawEvent->when);
  888. } else {
  889. for (size_t i = 0; i < numMappers; i++) {
  890. InputMapper* mapper = mMappers[i];
  891. mapper->process(rawEvent);
  892. }
  893. }
  894. }
  895. }
  896. void InputDevice::timeoutExpired(nsecs_t when) {
  897. size_t numMappers = mMappers.size();
  898. for (size_t i = 0; i < numMappers; i++) {
  899. InputMapper* mapper = mMappers[i];
  900. mapper->timeoutExpired(when);
  901. }
  902. }
  903. void InputDevice::updateExternalStylusState(const StylusState& state) {
  904. size_t numMappers = mMappers.size();
  905. for (size_t i = 0; i < numMappers; i++) {
  906. InputMapper* mapper = mMappers[i];
  907. mapper->updateExternalStylusState(state);
  908. }
  909. }
  910. void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
  911. outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias,
  912. mIsExternal, mHasMic);
  913. size_t numMappers = mMappers.size();
  914. for (size_t i = 0; i < numMappers; i++) {
  915. InputMapper* mapper = mMappers[i];
  916. mapper->populateDeviceInfo(outDeviceInfo);
  917. }
  918. }
  919. int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
  920. return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
  921. }
  922. int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
  923. return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
  924. }
  925. int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
  926. return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
  927. }
  928. int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
  929. int32_t result = AKEY_STATE_UNKNOWN;
  930. size_t numMappers = mMappers.size();
  931. for (size_t i = 0; i < numMappers; i++) {
  932. InputMapper* mapper = mMappers[i];
  933. if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
  934. // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
  935. // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
  936. int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
  937. if (currentResult >= AKEY_STATE_DOWN) {
  938. return currentResult;
  939. } else if (currentResult == AKEY_STATE_UP) {
  940. result = currentResult;
  941. }
  942. }
  943. }
  944. return result;
  945. }
  946. bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  947. const int32_t* keyCodes, uint8_t* outFlags) {
  948. bool result = false;
  949. size_t numMappers = mMappers.size();
  950. for (size_t i = 0; i < numMappers; i++) {
  951. InputMapper* mapper = mMappers[i];
  952. if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
  953. result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
  954. }
  955. }
  956. return result;
  957. }
  958. void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
  959. int32_t token) {
  960. size_t numMappers = mMappers.size();
  961. for (size_t i = 0; i < numMappers; i++) {
  962. InputMapper* mapper = mMappers[i];
  963. mapper->vibrate(pattern, patternSize, repeat, token);
  964. }
  965. }
  966. void InputDevice::cancelVibrate(int32_t token) {
  967. size_t numMappers = mMappers.size();
  968. for (size_t i = 0; i < numMappers; i++) {
  969. InputMapper* mapper = mMappers[i];
  970. mapper->cancelVibrate(token);
  971. }
  972. }
  973. void InputDevice::cancelTouch(nsecs_t when) {
  974. size_t numMappers = mMappers.size();
  975. for (size_t i = 0; i < numMappers; i++) {
  976. InputMapper* mapper = mMappers[i];
  977. mapper->cancelTouch(when);
  978. }
  979. }
  980. int32_t InputDevice::getMetaState() {
  981. int32_t result = 0;
  982. size_t numMappers = mMappers.size();
  983. for (size_t i = 0; i < numMappers; i++) {
  984. InputMapper* mapper = mMappers[i];
  985. result |= mapper->getMetaState();
  986. }
  987. return result;
  988. }
  989. void InputDevice::fadePointer() {
  990. size_t numMappers = mMappers.size();
  991. for (size_t i = 0; i < numMappers; i++) {
  992. InputMapper* mapper = mMappers[i];
  993. mapper->fadePointer();
  994. }
  995. }
  996. void InputDevice::bumpGeneration() {
  997. mGeneration = mContext->bumpGeneration();
  998. }
  999. void InputDevice::notifyReset(nsecs_t when) {
  1000. NotifyDeviceResetArgs args(when, mId);
  1001. mContext->getListener()->notifyDeviceReset(&args);
  1002. }
  1003. // --- CursorButtonAccumulator ---
  1004. CursorButtonAccumulator::CursorButtonAccumulator() {
  1005. clearButtons();
  1006. }
  1007. void CursorButtonAccumulator::reset(InputDevice* device) {
  1008. mBtnLeft = device->isKeyPressed(BTN_LEFT);
  1009. mBtnRight = device->isKeyPressed(BTN_RIGHT);
  1010. mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
  1011. mBtnBack = device->isKeyPressed(BTN_BACK);
  1012. mBtnSide = device->isKeyPressed(BTN_SIDE);
  1013. mBtnForward = device->isKeyPressed(BTN_FORWARD);
  1014. mBtnExtra = device->isKeyPressed(BTN_EXTRA);
  1015. mBtnTask = device->isKeyPressed(BTN_TASK);
  1016. }
  1017. void CursorButtonAccumulator::clearButtons() {
  1018. mBtnLeft = 0;
  1019. mBtnRight = 0;
  1020. mBtnMiddle = 0;
  1021. mBtnBack = 0;
  1022. mBtnSide = 0;
  1023. mBtnForward = 0;
  1024. mBtnExtra = 0;
  1025. mBtnTask = 0;
  1026. }
  1027. void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
  1028. if (rawEvent->type == EV_KEY) {
  1029. switch (rawEvent->code) {
  1030. case BTN_LEFT:
  1031. mBtnLeft = rawEvent->value;
  1032. break;
  1033. case BTN_RIGHT:
  1034. mBtnRight = rawEvent->value;
  1035. break;
  1036. case BTN_MIDDLE:
  1037. mBtnMiddle = rawEvent->value;
  1038. break;
  1039. case BTN_BACK:
  1040. mBtnBack = rawEvent->value;
  1041. break;
  1042. case BTN_SIDE:
  1043. mBtnSide = rawEvent->value;
  1044. break;
  1045. case BTN_FORWARD:
  1046. mBtnForward = rawEvent->value;
  1047. break;
  1048. case BTN_EXTRA:
  1049. mBtnExtra = rawEvent->value;
  1050. break;
  1051. case BTN_TASK:
  1052. mBtnTask = rawEvent->value;
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. uint32_t CursorButtonAccumulator::getButtonState() const {
  1058. uint32_t result = 0;
  1059. if (mBtnLeft) {
  1060. result |= AMOTION_EVENT_BUTTON_PRIMARY;
  1061. }
  1062. if (mBtnRight) {
  1063. result |= AMOTION_EVENT_BUTTON_SECONDARY;
  1064. }
  1065. if (mBtnMiddle) {
  1066. result |= AMOTION_EVENT_BUTTON_TERTIARY;
  1067. }
  1068. if (mBtnBack || mBtnSide) {
  1069. result |= AMOTION_EVENT_BUTTON_BACK;
  1070. }
  1071. if (mBtnForward || mBtnExtra) {
  1072. result |= AMOTION_EVENT_BUTTON_FORWARD;
  1073. }
  1074. return result;
  1075. }
  1076. // --- CursorMotionAccumulator ---
  1077. CursorMotionAccumulator::CursorMotionAccumulator() {
  1078. clearRelativeAxes();
  1079. }
  1080. void CursorMotionAccumulator::reset(InputDevice* device) {
  1081. clearRelativeAxes();
  1082. }
  1083. void CursorMotionAccumulator::clearRelativeAxes() {
  1084. mRelX = 0;
  1085. mRelY = 0;
  1086. }
  1087. void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
  1088. if (rawEvent->type == EV_REL) {
  1089. switch (rawEvent->code) {
  1090. case REL_X:
  1091. mRelX = rawEvent->value;
  1092. break;
  1093. case REL_Y:
  1094. mRelY = rawEvent->value;
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. void CursorMotionAccumulator::finishSync() {
  1100. clearRelativeAxes();
  1101. }
  1102. // --- CursorScrollAccumulator ---
  1103. CursorScrollAccumulator::CursorScrollAccumulator() :
  1104. mHaveRelWheel(false), mHaveRelHWheel(false) {
  1105. clearRelativeAxes();
  1106. }
  1107. void CursorScrollAccumulator::configure(InputDevice* device) {
  1108. mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
  1109. mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
  1110. }
  1111. void CursorScrollAccumulator::reset(InputDevice* device) {
  1112. clearRelativeAxes();
  1113. }
  1114. void CursorScrollAccumulator::clearRelativeAxes() {
  1115. mRelWheel = 0;
  1116. mRelHWheel = 0;
  1117. }
  1118. void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
  1119. if (rawEvent->type == EV_REL) {
  1120. switch (rawEvent->code) {
  1121. case REL_WHEEL:
  1122. mRelWheel = rawEvent->value;
  1123. break;
  1124. case REL_HWHEEL:
  1125. mRelHWheel = rawEvent->value;
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. void CursorScrollAccumulator::finishSync() {
  1131. clearRelativeAxes();
  1132. }
  1133. // --- TouchButtonAccumulator ---
  1134. TouchButtonAccumulator::TouchButtonAccumulator() :
  1135. mHaveBtnTouch(false), mHaveStylus(false) {
  1136. clearButtons();
  1137. }
  1138. void TouchButtonAccumulator::configure(InputDevice* device) {
  1139. mHaveBtnTouch = device->hasKey(BTN_TOUCH);
  1140. mHaveStylus = device->hasKey(BTN_TOOL_PEN)
  1141. || device->hasKey(BTN_TOOL_RUBBER)
  1142. || device->hasKey(BTN_TOOL_BRUSH)
  1143. || device->hasKey(BTN_TOOL_PENCIL)
  1144. || device->hasKey(BTN_TOOL_AIRBRUSH);
  1145. }
  1146. void TouchButtonAccumulator::reset(InputDevice* device) {
  1147. mBtnTouch = device->isKeyPressed(BTN_TOUCH);
  1148. mBtnStylus = device->isKeyPressed(BTN_STYLUS);
  1149. // BTN_0 is what gets mapped for the HID usage Digitizers.SecondaryBarrelSwitch
  1150. mBtnStylus2 =
  1151. device->isKeyPressed(BTN_STYLUS2) || device->isKeyPressed(BTN_0);
  1152. mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
  1153. mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
  1154. mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
  1155. mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
  1156. mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
  1157. mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
  1158. mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
  1159. mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
  1160. mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
  1161. mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
  1162. mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
  1163. }
  1164. void TouchButtonAccumulator::clearButtons() {
  1165. mBtnTouch = 0;
  1166. mBtnStylus = 0;
  1167. mBtnStylus2 = 0;
  1168. mBtnToolFinger = 0;
  1169. mBtnToolPen = 0;
  1170. mBtnToolRubber = 0;
  1171. mBtnToolBrush = 0;
  1172. mBtnToolPencil = 0;
  1173. mBtnToolAirbrush = 0;
  1174. mBtnToolMouse = 0;
  1175. mBtnToolLens = 0;
  1176. mBtnToolDoubleTap = 0;
  1177. mBtnToolTripleTap = 0;
  1178. mBtnToolQuadTap = 0;
  1179. }
  1180. void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
  1181. if (rawEvent->type == EV_KEY) {
  1182. switch (rawEvent->code) {
  1183. case BTN_TOUCH:
  1184. mBtnTouch = rawEvent->value;
  1185. break;
  1186. case BTN_STYLUS:
  1187. mBtnStylus = rawEvent->value;
  1188. break;
  1189. case BTN_STYLUS2:
  1190. case BTN_0:// BTN_0 is what gets mapped for the HID usage Digitizers.SecondaryBarrelSwitch
  1191. mBtnStylus2 = rawEvent->value;
  1192. break;
  1193. case BTN_TOOL_FINGER:
  1194. mBtnToolFinger = rawEvent->value;
  1195. break;
  1196. case BTN_TOOL_PEN:
  1197. mBtnToolPen = rawEvent->value;
  1198. break;
  1199. case BTN_TOOL_RUBBER:
  1200. mBtnToolRubber = rawEvent->value;
  1201. break;
  1202. case BTN_TOOL_BRUSH:
  1203. mBtnToolBrush = rawEvent->value;
  1204. break;
  1205. case BTN_TOOL_PENCIL:
  1206. mBtnToolPencil = rawEvent->value;
  1207. break;
  1208. case BTN_TOOL_AIRBRUSH:
  1209. mBtnToolAirbrush = rawEvent->value;
  1210. break;
  1211. case BTN_TOOL_MOUSE:
  1212. mBtnToolMouse = rawEvent->value;
  1213. break;
  1214. case BTN_TOOL_LENS:
  1215. mBtnToolLens = rawEvent->value;
  1216. break;
  1217. case BTN_TOOL_DOUBLETAP:
  1218. mBtnToolDoubleTap = rawEvent->value;
  1219. break;
  1220. case BTN_TOOL_TRIPLETAP:
  1221. mBtnToolTripleTap = rawEvent->value;
  1222. break;
  1223. case BTN_TOOL_QUADTAP:
  1224. mBtnToolQuadTap = rawEvent->value;
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. uint32_t TouchButtonAccumulator::getButtonState() const {
  1230. uint32_t result = 0;
  1231. if (mBtnStylus) {
  1232. result |= AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
  1233. }
  1234. if (mBtnStylus2) {
  1235. result |= AMOTION_EVENT_BUTTON_STYLUS_SECONDARY;
  1236. }
  1237. return result;
  1238. }
  1239. int32_t TouchButtonAccumulator::getToolType() const {
  1240. if (mBtnToolMouse || mBtnToolLens) {
  1241. return AMOTION_EVENT_TOOL_TYPE_MOUSE;
  1242. }
  1243. if (mBtnToolRubber) {
  1244. return AMOTION_EVENT_TOOL_TYPE_ERASER;
  1245. }
  1246. if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
  1247. return AMOTION_EVENT_TOOL_TYPE_STYLUS;
  1248. }
  1249. if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
  1250. return AMOTION_EVENT_TOOL_TYPE_FINGER;
  1251. }
  1252. return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
  1253. }
  1254. bool TouchButtonAccumulator::isToolActive() const {
  1255. return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
  1256. || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
  1257. || mBtnToolMouse || mBtnToolLens
  1258. || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
  1259. }
  1260. bool TouchButtonAccumulator::isHovering() const {
  1261. return mHaveBtnTouch && !mBtnTouch;
  1262. }
  1263. bool TouchButtonAccumulator::hasStylus() const {
  1264. return mHaveStylus;
  1265. }
  1266. // --- RawPointerAxes ---
  1267. RawPointerAxes::RawPointerAxes() {
  1268. clear();
  1269. }
  1270. void RawPointerAxes::clear() {
  1271. x.clear();
  1272. y.clear();
  1273. pressure.clear();
  1274. touchMajor.clear();
  1275. touchMinor.clear();
  1276. toolMajor.clear();
  1277. toolMinor.clear();
  1278. orientation.clear();
  1279. distance.clear();
  1280. tiltX.clear();
  1281. tiltY.clear();
  1282. trackingId.clear();
  1283. slot.clear();
  1284. }
  1285. // --- RawPointerData ---
  1286. RawPointerData::RawPointerData() {
  1287. clear();
  1288. }
  1289. void RawPointerData::clear() {
  1290. pointerCount = 0;
  1291. clearIdBits();
  1292. }
  1293. void RawPointerData::copyFrom(const RawPointerData& other) {
  1294. pointerCount = other.pointerCount;
  1295. hoveringIdBits = other.hoveringIdBits;
  1296. touchingIdBits = other.touchingIdBits;
  1297. for (uint32_t i = 0; i < pointerCount; i++) {
  1298. pointers[i] = other.pointers[i];
  1299. int id = pointers[i].id;
  1300. idToIndex[id] = other.idToIndex[id];
  1301. }
  1302. }
  1303. void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
  1304. float x = 0, y = 0;
  1305. uint32_t count = touchingIdBits.count();
  1306. if (count) {
  1307. for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
  1308. uint32_t id = idBits.clearFirstMarkedBit();
  1309. const Pointer& pointer = pointerForId(id);
  1310. x += pointer.x;
  1311. y += pointer.y;
  1312. }
  1313. x /= count;
  1314. y /= count;
  1315. }
  1316. *outX = x;
  1317. *outY = y;
  1318. }
  1319. // --- CookedPointerData ---
  1320. CookedPointerData::CookedPointerData() {
  1321. clear();
  1322. }
  1323. void CookedPointerData::clear() {
  1324. pointerCount = 0;
  1325. hoveringIdBits.clear();
  1326. touchingIdBits.clear();
  1327. }
  1328. void CookedPointerData::copyFrom(const CookedPointerData& other) {
  1329. pointerCount = other.pointerCount;
  1330. hoveringIdBits = other.hoveringIdBits;
  1331. touchingIdBits = other.touchingIdBits;
  1332. for (uint32_t i = 0; i < pointerCount; i++) {
  1333. pointerProperties[i].copyFrom(other.pointerProperties[i]);
  1334. pointerCoords[i].copyFrom(other.pointerCoords[i]);
  1335. int id = pointerProperties[i].id;
  1336. idToIndex[id] = other.idToIndex[id];
  1337. }
  1338. }
  1339. // --- SingleTouchMotionAccumulator ---
  1340. SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
  1341. clearAbsoluteAxes();
  1342. }
  1343. void SingleTouchMotionAccumulator::reset(InputDevice* device) {
  1344. mAbsX = device->getAbsoluteAxisValue(ABS_X);
  1345. mAbsY = device->getAbsoluteAxisValue(ABS_Y);
  1346. mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
  1347. mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
  1348. mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
  1349. mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
  1350. mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
  1351. }
  1352. void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
  1353. mAbsX = 0;
  1354. mAbsY = 0;
  1355. mAbsPressure = 0;
  1356. mAbsToolWidth = 0;
  1357. mAbsDistance = 0;
  1358. mAbsTiltX = 0;
  1359. mAbsTiltY = 0;
  1360. }
  1361. void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
  1362. if (rawEvent->type == EV_ABS) {
  1363. switch (rawEvent->code) {
  1364. case ABS_X:
  1365. mAbsX = rawEvent->value;
  1366. break;
  1367. case ABS_Y:
  1368. mAbsY = rawEvent->value;
  1369. break;
  1370. case ABS_PRESSURE:
  1371. mAbsPressure = rawEvent->value;
  1372. break;
  1373. case ABS_TOOL_WIDTH:
  1374. mAbsToolWidth = rawEvent->value;
  1375. break;
  1376. case ABS_DISTANCE:
  1377. mAbsDistance = rawEvent->value;
  1378. break;
  1379. case ABS_TILT_X:
  1380. mAbsTiltX = rawEvent->value;
  1381. break;
  1382. case ABS_TILT_Y:
  1383. mAbsTiltY = rawEvent->value;
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. // --- MultiTouchMotionAccumulator ---
  1389. MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
  1390. mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
  1391. mHaveStylus(false) {
  1392. }
  1393. MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
  1394. delete[] mSlots;
  1395. }
  1396. void MultiTouchMotionAccumulator::configure(InputDevice* device,
  1397. size_t slotCount, bool usingSlotsProtocol) {
  1398. mSlotCount = slotCount;
  1399. mUsingSlotsProtocol = usingSlotsProtocol;
  1400. mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
  1401. delete[] mSlots;
  1402. mSlots = new Slot[slotCount];
  1403. }
  1404. void MultiTouchMotionAccumulator::reset(InputDevice* device) {
  1405. // Unfortunately there is no way to read the initial contents of the slots.
  1406. // So when we reset the accumulator, we must assume they are all zeroes.
  1407. if (mUsingSlotsProtocol) {
  1408. // Query the driver for the current slot index and use it as the initial slot
  1409. // before we start reading events from the device. It is possible that the
  1410. // current slot index will not be the same as it was when the first event was
  1411. // written into the evdev buffer, which means the input mapper could start
  1412. // out of sync with the initial state of the events in the evdev buffer.
  1413. // In the extremely unlikely case that this happens, the data from
  1414. // two slots will be confused until the next ABS_MT_SLOT event is received.
  1415. // This can cause the touch point to "jump", but at least there will be
  1416. // no stuck touches.
  1417. int32_t initialSlot;
  1418. status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
  1419. ABS_MT_SLOT, &initialSlot);
  1420. if (status) {
  1421. ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
  1422. initialSlot = -1;
  1423. }
  1424. clearSlots(initialSlot);
  1425. } else {
  1426. clearSlots(-1);
  1427. }
  1428. }
  1429. void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
  1430. if (mSlots) {
  1431. for (size_t i = 0; i < mSlotCount; i++) {
  1432. mSlots[i].clear();
  1433. }
  1434. }
  1435. mCurrentSlot = initialSlot;
  1436. }
  1437. void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
  1438. if (rawEvent->type == EV_ABS) {
  1439. bool newSlot = false;
  1440. if (mUsingSlotsProtocol) {
  1441. if (rawEvent->code == ABS_MT_SLOT) {
  1442. mCurrentSlot = rawEvent->value;
  1443. newSlot = true;
  1444. }
  1445. } else if (mCurrentSlot < 0) {
  1446. mCurrentSlot = 0;
  1447. }
  1448. if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
  1449. #if DEBUG_POINTERS
  1450. if (newSlot) {
  1451. ALOGW("MultiTouch device emitted invalid slot index %d but it "
  1452. "should be between 0 and %d; ignoring this slot.",
  1453. mCurrentSlot, mSlotCount - 1);
  1454. }
  1455. #endif
  1456. } else {
  1457. Slot* slot = &mSlots[mCurrentSlot];
  1458. switch (rawEvent->code) {
  1459. case ABS_MT_POSITION_X:
  1460. slot->mInUse = true;
  1461. slot->mAbsMTPositionX = rawEvent->value;
  1462. break;
  1463. case ABS_MT_POSITION_Y:
  1464. slot->mInUse = true;
  1465. slot->mAbsMTPositionY = rawEvent->value;
  1466. break;
  1467. case ABS_MT_TOUCH_MAJOR:
  1468. slot->mInUse = true;
  1469. slot->mAbsMTTouchMajor = rawEvent->value;
  1470. break;
  1471. case ABS_MT_TOUCH_MINOR:
  1472. slot->mInUse = true;
  1473. slot->mAbsMTTouchMinor = rawEvent->value;
  1474. slot->mHaveAbsMTTouchMinor = true;
  1475. break;
  1476. case ABS_MT_WIDTH_MAJOR:
  1477. slot->mInUse = true;
  1478. slot->mAbsMTWidthMajor = rawEvent->value;
  1479. break;
  1480. case ABS_MT_WIDTH_MINOR:
  1481. slot->mInUse = true;
  1482. slot->mAbsMTWidthMinor = rawEvent->value;
  1483. slot->mHaveAbsMTWidthMinor = true;
  1484. break;
  1485. case ABS_MT_ORIENTATION:
  1486. slot->mInUse = true;
  1487. slot->mAbsMTOrientation = rawEvent->value;
  1488. break;
  1489. case ABS_MT_TRACKING_ID:
  1490. if (mUsingSlotsProtocol && rawEvent->value < 0) {
  1491. // The slot is no longer in use but it retains its previous contents,
  1492. // which may be reused for subsequent touches.
  1493. slot->mInUse = false;
  1494. } else {
  1495. slot->mInUse = true;
  1496. slot->mAbsMTTrackingId = rawEvent->value;
  1497. }
  1498. break;
  1499. case ABS_MT_PRESSURE:
  1500. slot->mInUse = true;
  1501. slot->mAbsMTPressure = rawEvent->value;
  1502. break;
  1503. case ABS_MT_DISTANCE:
  1504. slot->mInUse = true;
  1505. slot->mAbsMTDistance = rawEvent->value;
  1506. break;
  1507. case ABS_MT_TOOL_TYPE:
  1508. slot->mInUse = true;
  1509. slot->mAbsMTToolType = rawEvent->value;
  1510. slot->mHaveAbsMTToolType = true;
  1511. break;
  1512. }
  1513. }
  1514. } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
  1515. // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
  1516. mCurrentSlot += 1;
  1517. }
  1518. }
  1519. void MultiTouchMotionAccumulator::finishSync() {
  1520. if (!mUsingSlotsProtocol) {
  1521. clearSlots(-1);
  1522. }
  1523. }
  1524. bool MultiTouchMotionAccumulator::hasStylus() const {
  1525. return mHaveStylus;
  1526. }
  1527. // --- MultiTouchMotionAccumulator::Slot ---
  1528. MultiTouchMotionAccumulator::Slot::Slot() {
  1529. clear();
  1530. }
  1531. void MultiTouchMotionAccumulator::Slot::clear() {
  1532. mInUse = false;
  1533. mHaveAbsMTTouchMinor = false;
  1534. mHaveAbsMTWidthMinor = false;
  1535. mHaveAbsMTToolType = false;
  1536. mAbsMTPositionX = 0;
  1537. mAbsMTPositionY = 0;
  1538. mAbsMTTouchMajor = 0;
  1539. mAbsMTTouchMinor = 0;
  1540. mAbsMTWidthMajor = 0;
  1541. mAbsMTWidthMinor = 0;
  1542. mAbsMTOrientation = 0;
  1543. mAbsMTTrackingId = -1;
  1544. mAbsMTPressure = 0;
  1545. mAbsMTDistance = 0;
  1546. mAbsMTToolType = 0;
  1547. }
  1548. int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
  1549. if (mHaveAbsMTToolType) {
  1550. switch (mAbsMTToolType) {
  1551. case MT_TOOL_FINGER:
  1552. return AMOTION_EVENT_TOOL_TYPE_FINGER;
  1553. case MT_TOOL_PEN:
  1554. return AMOTION_EVENT_TOOL_TYPE_STYLUS;
  1555. }
  1556. }
  1557. return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
  1558. }
  1559. // --- InputMapper ---
  1560. InputMapper::InputMapper(InputDevice* device) :
  1561. mDevice(device), mContext(device->getContext()) {
  1562. }
  1563. InputMapper::~InputMapper() {
  1564. }
  1565. void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  1566. info->addSource(getSources());
  1567. }
  1568. void InputMapper::dump(String8& dump) {
  1569. }
  1570. void InputMapper::configure(nsecs_t when,
  1571. const InputReaderConfiguration* config, uint32_t changes) {
  1572. }
  1573. void InputMapper::reset(nsecs_t when) {
  1574. }
  1575. void InputMapper::timeoutExpired(nsecs_t when) {
  1576. }
  1577. int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
  1578. return AKEY_STATE_UNKNOWN;
  1579. }
  1580. int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
  1581. return AKEY_STATE_UNKNOWN;
  1582. }
  1583. int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
  1584. return AKEY_STATE_UNKNOWN;
  1585. }
  1586. bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  1587. const int32_t* keyCodes, uint8_t* outFlags) {
  1588. return false;
  1589. }
  1590. void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
  1591. int32_t token) {
  1592. }
  1593. void InputMapper::cancelVibrate(int32_t token) {
  1594. }
  1595. void InputMapper::cancelTouch(nsecs_t when) {
  1596. }
  1597. int32_t InputMapper::getMetaState() {
  1598. return 0;
  1599. }
  1600. void InputMapper::updateExternalStylusState(const StylusState& state) {
  1601. }
  1602. void InputMapper::fadePointer() {
  1603. }
  1604. status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
  1605. return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
  1606. }
  1607. void InputMapper::bumpGeneration() {
  1608. mDevice->bumpGeneration();
  1609. }
  1610. void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
  1611. const RawAbsoluteAxisInfo& axis, const char* name) {
  1612. if (axis.valid) {
  1613. dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
  1614. name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
  1615. } else {
  1616. dump.appendFormat(INDENT4 "%s: unknown range\n", name);
  1617. }
  1618. }
  1619. void InputMapper::dumpStylusState(String8& dump, const StylusState& state) {
  1620. dump.appendFormat(INDENT4 "When: %" PRId64 "\n", state.when);
  1621. dump.appendFormat(INDENT4 "Pressure: %f\n", state.pressure);
  1622. dump.appendFormat(INDENT4 "Button State: 0x%08x\n", state.buttons);
  1623. dump.appendFormat(INDENT4 "Tool Type: %" PRId32 "\n", state.toolType);
  1624. }
  1625. // --- SwitchInputMapper ---
  1626. SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
  1627. InputMapper(device), mSwitchValues(0), mUpdatedSwitchMask(0) {
  1628. }
  1629. SwitchInputMapper::~SwitchInputMapper() {
  1630. }
  1631. uint32_t SwitchInputMapper::getSources() {
  1632. return AINPUT_SOURCE_SWITCH;
  1633. }
  1634. void SwitchInputMapper::process(const RawEvent* rawEvent) {
  1635. switch (rawEvent->type) {
  1636. case EV_SW:
  1637. processSwitch(rawEvent->code, rawEvent->value);
  1638. break;
  1639. case EV_SYN:
  1640. if (rawEvent->code == SYN_REPORT) {
  1641. sync(rawEvent->when);
  1642. }
  1643. }
  1644. }
  1645. void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
  1646. if (switchCode >= 0 && switchCode < 32) {
  1647. if (switchValue) {
  1648. mSwitchValues |= 1 << switchCode;
  1649. } else {
  1650. mSwitchValues &= ~(1 << switchCode);
  1651. }
  1652. mUpdatedSwitchMask |= 1 << switchCode;
  1653. }
  1654. }
  1655. void SwitchInputMapper::sync(nsecs_t when) {
  1656. if (mUpdatedSwitchMask) {
  1657. uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask;
  1658. NotifySwitchArgs args(when, 0, updatedSwitchValues, mUpdatedSwitchMask);
  1659. getListener()->notifySwitch(&args);
  1660. mUpdatedSwitchMask = 0;
  1661. }
  1662. }
  1663. int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
  1664. return getEventHub()->getSwitchState(getDeviceId(), switchCode);
  1665. }
  1666. void SwitchInputMapper::dump(String8& dump) {
  1667. dump.append(INDENT2 "Switch Input Mapper:\n");
  1668. dump.appendFormat(INDENT3 "SwitchValues: %x\n", mSwitchValues);
  1669. }
  1670. // --- VibratorInputMapper ---
  1671. VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
  1672. InputMapper(device), mVibrating(false) {
  1673. }
  1674. VibratorInputMapper::~VibratorInputMapper() {
  1675. }
  1676. uint32_t VibratorInputMapper::getSources() {
  1677. return 0;
  1678. }
  1679. void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  1680. InputMapper::populateDeviceInfo(info);
  1681. info->setVibrator(true);
  1682. }
  1683. void VibratorInputMapper::process(const RawEvent* rawEvent) {
  1684. // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
  1685. }
  1686. void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
  1687. int32_t token) {
  1688. #if DEBUG_VIBRATOR
  1689. String8 patternStr;
  1690. for (size_t i = 0; i < patternSize; i++) {
  1691. if (i != 0) {
  1692. patternStr.append(", ");
  1693. }
  1694. patternStr.appendFormat("%lld", pattern[i]);
  1695. }
  1696. ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
  1697. getDeviceId(), patternStr.string(), repeat, token);
  1698. #endif
  1699. mVibrating = true;
  1700. memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
  1701. mPatternSize = patternSize;
  1702. mRepeat = repeat;
  1703. mToken = token;
  1704. mIndex = -1;
  1705. nextStep();
  1706. }
  1707. void VibratorInputMapper::cancelVibrate(int32_t token) {
  1708. #if DEBUG_VIBRATOR
  1709. ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
  1710. #endif
  1711. if (mVibrating && mToken == token) {
  1712. stopVibrating();
  1713. }
  1714. }
  1715. void VibratorInputMapper::timeoutExpired(nsecs_t when) {
  1716. if (mVibrating) {
  1717. if (when >= mNextStepTime) {
  1718. nextStep();
  1719. } else {
  1720. getContext()->requestTimeoutAtTime(mNextStepTime);
  1721. }
  1722. }
  1723. }
  1724. void VibratorInputMapper::nextStep() {
  1725. mIndex += 1;
  1726. if (size_t(mIndex) >= mPatternSize) {
  1727. if (mRepeat < 0) {
  1728. // We are done.
  1729. stopVibrating();
  1730. return;
  1731. }
  1732. mIndex = mRepeat;
  1733. }
  1734. bool vibratorOn = mIndex & 1;
  1735. nsecs_t duration = mPattern[mIndex];
  1736. if (vibratorOn) {
  1737. #if DEBUG_VIBRATOR
  1738. ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
  1739. getDeviceId(), duration);
  1740. #endif
  1741. getEventHub()->vibrate(getDeviceId(), duration);
  1742. } else {
  1743. #if DEBUG_VIBRATOR
  1744. ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
  1745. #endif
  1746. getEventHub()->cancelVibrate(getDeviceId());
  1747. }
  1748. nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
  1749. mNextStepTime = now + duration;
  1750. getContext()->requestTimeoutAtTime(mNextStepTime);
  1751. #if DEBUG_VIBRATOR
  1752. ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
  1753. #endif
  1754. }
  1755. void VibratorInputMapper::stopVibrating() {
  1756. mVibrating = false;
  1757. #if DEBUG_VIBRATOR
  1758. ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
  1759. #endif
  1760. getEventHub()->cancelVibrate(getDeviceId());
  1761. }
  1762. void VibratorInputMapper::dump(String8& dump) {
  1763. dump.append(INDENT2 "Vibrator Input Mapper:\n");
  1764. dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
  1765. }
  1766. // --- KeyboardInputMapper ---
  1767. KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
  1768. uint32_t source, int32_t keyboardType) :
  1769. InputMapper(device), mSource(source),
  1770. mKeyboardType(keyboardType) {
  1771. }
  1772. KeyboardInputMapper::~KeyboardInputMapper() {
  1773. }
  1774. uint32_t KeyboardInputMapper::getSources() {
  1775. return mSource;
  1776. }
  1777. void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  1778. InputMapper::populateDeviceInfo(info);
  1779. info->setKeyboardType(mKeyboardType);
  1780. info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
  1781. }
  1782. void KeyboardInputMapper::dump(String8& dump) {
  1783. dump.append(INDENT2 "Keyboard Input Mapper:\n");
  1784. dumpParameters(dump);
  1785. dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
  1786. dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
  1787. dump.appendFormat(INDENT3 "KeyDowns: %zu keys currently down\n", mKeyDowns.size());
  1788. dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
  1789. dump.appendFormat(INDENT3 "DownTime: %lld\n", (long long)mDownTime);
  1790. }
  1791. void KeyboardInputMapper::configure(nsecs_t when,
  1792. const InputReaderConfiguration* config, uint32_t changes) {
  1793. InputMapper::configure(when, config, changes);
  1794. if (!changes) { // first time only
  1795. // Configure basic parameters.
  1796. configureParameters();
  1797. }
  1798. if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
  1799. if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
  1800. DisplayViewport v;
  1801. if (config->getDisplayInfo(false /*external*/, &v)) {
  1802. mOrientation = v.orientation;
  1803. } else {
  1804. mOrientation = DISPLAY_ORIENTATION_0;
  1805. }
  1806. } else {
  1807. mOrientation = DISPLAY_ORIENTATION_0;
  1808. }
  1809. }
  1810. if (!changes || (changes & InputReaderConfiguration::CHANGE_VOLUME_KEYS_ROTATION)) {
  1811. // mode 0 (disabled) ~ offset 4
  1812. // mode 1 (phone) ~ offset 2
  1813. // mode 2 (tablet) ~ offset 0
  1814. mRotationMapOffset = 4 - 2 * config->volumeKeysRotationMode;
  1815. }
  1816. }
  1817. void KeyboardInputMapper::configureParameters() {
  1818. mParameters.orientationAware = !getDevice()->isExternal();
  1819. getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
  1820. mParameters.orientationAware);
  1821. mParameters.hasAssociatedDisplay = false;
  1822. if (mParameters.orientationAware) {
  1823. mParameters.hasAssociatedDisplay = true;
  1824. }
  1825. mParameters.handlesKeyRepeat = false;
  1826. getDevice()->getConfiguration().tryGetProperty(String8("keyboard.handlesKeyRepeat"),
  1827. mParameters.handlesKeyRepeat);
  1828. }
  1829. void KeyboardInputMapper::dumpParameters(String8& dump) {
  1830. dump.append(INDENT3 "Parameters:\n");
  1831. dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
  1832. toString(mParameters.hasAssociatedDisplay));
  1833. dump.appendFormat(INDENT4 "OrientationAware: %s\n",
  1834. toString(mParameters.orientationAware));
  1835. dump.appendFormat(INDENT4 "HandlesKeyRepeat: %s\n",
  1836. toString(mParameters.handlesKeyRepeat));
  1837. }
  1838. void KeyboardInputMapper::reset(nsecs_t when) {
  1839. mMetaState = AMETA_NONE;
  1840. mDownTime = 0;
  1841. mKeyDowns.clear();
  1842. mCurrentHidUsage = 0;
  1843. resetLedState();
  1844. InputMapper::reset(when);
  1845. }
  1846. void KeyboardInputMapper::process(const RawEvent* rawEvent) {
  1847. switch (rawEvent->type) {
  1848. case EV_KEY: {
  1849. int32_t scanCode = rawEvent->code;
  1850. int32_t usageCode = mCurrentHidUsage;
  1851. mCurrentHidUsage = 0;
  1852. if (isKeyboardOrGamepadKey(scanCode)) {
  1853. processKey(rawEvent->when, rawEvent->value != 0, scanCode, usageCode);
  1854. }
  1855. break;
  1856. }
  1857. case EV_MSC: {
  1858. if (rawEvent->code == MSC_SCAN) {
  1859. mCurrentHidUsage = rawEvent->value;
  1860. }
  1861. break;
  1862. }
  1863. case EV_SYN: {
  1864. if (rawEvent->code == SYN_REPORT) {
  1865. mCurrentHidUsage = 0;
  1866. }
  1867. }
  1868. }
  1869. }
  1870. bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
  1871. return scanCode < BTN_MOUSE
  1872. || scanCode >= KEY_OK
  1873. || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
  1874. || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
  1875. }
  1876. void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t scanCode,
  1877. int32_t usageCode) {
  1878. int32_t keyCode;
  1879. int32_t keyMetaState;
  1880. uint32_t policyFlags;
  1881. if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, mMetaState,
  1882. &keyCode, &keyMetaState, &policyFlags)) {
  1883. keyCode = AKEYCODE_UNKNOWN;
  1884. keyMetaState = mMetaState;
  1885. policyFlags = 0;
  1886. }
  1887. if (down) {
  1888. // Rotate key codes according to orientation if needed.
  1889. if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
  1890. keyCode = rotateKeyCode(keyCode, mOrientation, mRotationMapOffset);
  1891. }
  1892. // Add key down.
  1893. ssize_t keyDownIndex = findKeyDown(scanCode);
  1894. if (keyDownIndex >= 0) {
  1895. // key repeat, be sure to use same keycode as before in case of rotation
  1896. keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
  1897. } else {
  1898. // key down
  1899. if ((policyFlags & POLICY_FLAG_VIRTUAL)
  1900. && mContext->shouldDropVirtualKey(when,
  1901. getDevice(), keyCode, scanCode)) {
  1902. return;
  1903. }
  1904. if (policyFlags & POLICY_FLAG_GESTURE) {
  1905. mDevice->cancelTouch(when);
  1906. }
  1907. mKeyDowns.push();
  1908. KeyDown& keyDown = mKeyDowns.editTop();
  1909. keyDown.keyCode = keyCode;
  1910. keyDown.scanCode = scanCode;
  1911. }
  1912. mDownTime = when;
  1913. } else {
  1914. // Remove key down.
  1915. ssize_t keyDownIndex = findKeyDown(scanCode);
  1916. if (keyDownIndex >= 0) {
  1917. // key up, be sure to use same keycode as before in case of rotation
  1918. keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
  1919. mKeyDowns.removeAt(size_t(keyDownIndex));
  1920. } else {
  1921. // key was not actually down
  1922. ALOGI("Dropping key up from device %s because the key was not down. "
  1923. "keyCode=%d, scanCode=%d",
  1924. getDeviceName().string(), keyCode, scanCode);
  1925. return;
  1926. }
  1927. }
  1928. int32_t oldMetaState = mMetaState;
  1929. int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
  1930. bool metaStateChanged = oldMetaState != newMetaState;
  1931. if (metaStateChanged) {
  1932. mMetaState = newMetaState;
  1933. updateLedState(false);
  1934. // If global meta state changed send it along with the key.
  1935. // If it has not changed then we'll use what keymap gave us,
  1936. // since key replacement logic might temporarily reset a few
  1937. // meta bits for given key.
  1938. keyMetaState = newMetaState;
  1939. }
  1940. nsecs_t downTime = mDownTime;
  1941. // Key down on external an keyboard should wake the device.
  1942. // We don't do this for internal keyboards to prevent them from waking up in your pocket.
  1943. // For internal keyboards, the key layout file should specify the policy flags for
  1944. // each wake key individually.
  1945. // TODO: Use the input device configuration to control this behavior more finely.
  1946. if (down && getDevice()->isExternal()) {
  1947. policyFlags |= POLICY_FLAG_WAKE;
  1948. }
  1949. if (mParameters.handlesKeyRepeat) {
  1950. policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
  1951. }
  1952. if (metaStateChanged) {
  1953. getContext()->updateGlobalMetaState();
  1954. }
  1955. if (down && !isMetaKey(keyCode)) {
  1956. getContext()->fadePointer();
  1957. }
  1958. NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
  1959. down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
  1960. AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState, downTime);
  1961. getListener()->notifyKey(&args);
  1962. }
  1963. ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
  1964. size_t n = mKeyDowns.size();
  1965. for (size_t i = 0; i < n; i++) {
  1966. if (mKeyDowns[i].scanCode == scanCode) {
  1967. return i;
  1968. }
  1969. }
  1970. return -1;
  1971. }
  1972. int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
  1973. return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
  1974. }
  1975. int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
  1976. return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
  1977. }
  1978. bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  1979. const int32_t* keyCodes, uint8_t* outFlags) {
  1980. return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
  1981. }
  1982. int32_t KeyboardInputMapper::getMetaState() {
  1983. return mMetaState;
  1984. }
  1985. void KeyboardInputMapper::resetLedState() {
  1986. initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
  1987. initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
  1988. initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
  1989. updateLedState(true);
  1990. }
  1991. void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
  1992. ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
  1993. ledState.on = false;
  1994. }
  1995. void KeyboardInputMapper::updateLedState(bool reset) {
  1996. updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK,
  1997. AMETA_CAPS_LOCK_ON, reset);
  1998. updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK,
  1999. AMETA_NUM_LOCK_ON, reset);
  2000. updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK,
  2001. AMETA_SCROLL_LOCK_ON, reset);
  2002. }
  2003. void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
  2004. int32_t led, int32_t modifier, bool reset) {
  2005. if (ledState.avail) {
  2006. bool desiredState = (mMetaState & modifier) != 0;
  2007. if (reset || ledState.on != desiredState) {
  2008. getEventHub()->setLedState(getDeviceId(), led, desiredState);
  2009. ledState.on = desiredState;
  2010. }
  2011. }
  2012. }
  2013. // --- CursorInputMapper ---
  2014. CursorInputMapper::CursorInputMapper(InputDevice* device) :
  2015. InputMapper(device) {
  2016. }
  2017. CursorInputMapper::~CursorInputMapper() {
  2018. }
  2019. uint32_t CursorInputMapper::getSources() {
  2020. return mSource;
  2021. }
  2022. void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  2023. InputMapper::populateDeviceInfo(info);
  2024. if (mParameters.mode == Parameters::MODE_POINTER) {
  2025. float minX, minY, maxX, maxY;
  2026. if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
  2027. info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f);
  2028. info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f);
  2029. }
  2030. } else {
  2031. info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f);
  2032. info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f);
  2033. }
  2034. info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  2035. if (mCursorScrollAccumulator.haveRelativeVWheel()) {
  2036. info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  2037. }
  2038. if (mCursorScrollAccumulator.haveRelativeHWheel()) {
  2039. info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  2040. }
  2041. }
  2042. void CursorInputMapper::dump(String8& dump) {
  2043. dump.append(INDENT2 "Cursor Input Mapper:\n");
  2044. dumpParameters(dump);
  2045. dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
  2046. dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
  2047. dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
  2048. dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
  2049. dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
  2050. toString(mCursorScrollAccumulator.haveRelativeVWheel()));
  2051. dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
  2052. toString(mCursorScrollAccumulator.haveRelativeHWheel()));
  2053. dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
  2054. dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
  2055. dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
  2056. dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
  2057. dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
  2058. dump.appendFormat(INDENT3 "DownTime: %lld\n", (long long)mDownTime);
  2059. }
  2060. void CursorInputMapper::configure(nsecs_t when,
  2061. const InputReaderConfiguration* config, uint32_t changes) {
  2062. InputMapper::configure(when, config, changes);
  2063. if (!changes) { // first time only
  2064. mCursorScrollAccumulator.configure(getDevice());
  2065. // Configure basic parameters.
  2066. configureParameters();
  2067. // Configure device mode.
  2068. switch (mParameters.mode) {
  2069. case Parameters::MODE_POINTER:
  2070. mSource = AINPUT_SOURCE_MOUSE;
  2071. mXPrecision = 1.0f;
  2072. mYPrecision = 1.0f;
  2073. mXScale = 1.0f;
  2074. mYScale = 1.0f;
  2075. mPointerController = getPolicy()->obtainPointerController(getDeviceId());
  2076. break;
  2077. case Parameters::MODE_NAVIGATION:
  2078. mSource = AINPUT_SOURCE_TRACKBALL;
  2079. mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
  2080. mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
  2081. mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
  2082. mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
  2083. break;
  2084. }
  2085. mVWheelScale = 1.0f;
  2086. mHWheelScale = 1.0f;
  2087. }
  2088. if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
  2089. mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
  2090. mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
  2091. mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
  2092. }
  2093. if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
  2094. if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
  2095. DisplayViewport v;
  2096. if (config->getDisplayInfo(false /*external*/, &v)) {
  2097. mOrientation = v.orientation;
  2098. } else {
  2099. mOrientation = DISPLAY_ORIENTATION_0;
  2100. }
  2101. } else {
  2102. mOrientation = DISPLAY_ORIENTATION_0;
  2103. }
  2104. bumpGeneration();
  2105. }
  2106. }
  2107. void CursorInputMapper::configureParameters() {
  2108. mParameters.mode = Parameters::MODE_POINTER;
  2109. String8 cursorModeString;
  2110. if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
  2111. if (cursorModeString == "navigation") {
  2112. mParameters.mode = Parameters::MODE_NAVIGATION;
  2113. } else if (cursorModeString != "pointer" && cursorModeString != "default") {
  2114. ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
  2115. }
  2116. }
  2117. mParameters.orientationAware = false;
  2118. getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
  2119. mParameters.orientationAware);
  2120. mParameters.hasAssociatedDisplay = false;
  2121. if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
  2122. mParameters.hasAssociatedDisplay = true;
  2123. }
  2124. }
  2125. void CursorInputMapper::dumpParameters(String8& dump) {
  2126. dump.append(INDENT3 "Parameters:\n");
  2127. dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
  2128. toString(mParameters.hasAssociatedDisplay));
  2129. switch (mParameters.mode) {
  2130. case Parameters::MODE_POINTER:
  2131. dump.append(INDENT4 "Mode: pointer\n");
  2132. break;
  2133. case Parameters::MODE_NAVIGATION:
  2134. dump.append(INDENT4 "Mode: navigation\n");
  2135. break;
  2136. default:
  2137. ALOG_ASSERT(false);
  2138. }
  2139. dump.appendFormat(INDENT4 "OrientationAware: %s\n",
  2140. toString(mParameters.orientationAware));
  2141. }
  2142. void CursorInputMapper::reset(nsecs_t when) {
  2143. mButtonState = 0;
  2144. mDownTime = 0;
  2145. mPointerVelocityControl.reset();
  2146. mWheelXVelocityControl.reset();
  2147. mWheelYVelocityControl.reset();
  2148. mCursorButtonAccumulator.reset(getDevice());
  2149. mCursorMotionAccumulator.reset(getDevice());
  2150. mCursorScrollAccumulator.reset(getDevice());
  2151. InputMapper::reset(when);
  2152. }
  2153. void CursorInputMapper::process(const RawEvent* rawEvent) {
  2154. mCursorButtonAccumulator.process(rawEvent);
  2155. mCursorMotionAccumulator.process(rawEvent);
  2156. mCursorScrollAccumulator.process(rawEvent);
  2157. if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
  2158. sync(rawEvent->when);
  2159. }
  2160. }
  2161. void CursorInputMapper::sync(nsecs_t when) {
  2162. int32_t lastButtonState = mButtonState;
  2163. int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
  2164. mButtonState = currentButtonState;
  2165. bool wasDown = isPointerDown(lastButtonState);
  2166. bool down = isPointerDown(currentButtonState);
  2167. bool downChanged;
  2168. if (!wasDown && down) {
  2169. mDownTime = when;
  2170. downChanged = true;
  2171. } else if (wasDown && !down) {
  2172. downChanged = true;
  2173. } else {
  2174. downChanged = false;
  2175. }
  2176. nsecs_t downTime = mDownTime;
  2177. bool buttonsChanged = currentButtonState != lastButtonState;
  2178. int32_t buttonsPressed = currentButtonState & ~lastButtonState;
  2179. int32_t buttonsReleased = lastButtonState & ~currentButtonState;
  2180. float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
  2181. float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
  2182. bool moved = deltaX != 0 || deltaY != 0;
  2183. // Rotate delta according to orientation if needed.
  2184. if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
  2185. && (deltaX != 0.0f || deltaY != 0.0f)) {
  2186. rotateDelta(mOrientation, &deltaX, &deltaY);
  2187. }
  2188. // Move the pointer.
  2189. PointerProperties pointerProperties;
  2190. pointerProperties.clear();
  2191. pointerProperties.id = 0;
  2192. pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
  2193. PointerCoords pointerCoords;
  2194. pointerCoords.clear();
  2195. float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
  2196. float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
  2197. bool scrolled = vscroll != 0 || hscroll != 0;
  2198. mWheelYVelocityControl.move(when, NULL, &vscroll);
  2199. mWheelXVelocityControl.move(when, &hscroll, NULL);
  2200. mPointerVelocityControl.move(when, &deltaX, &deltaY);
  2201. int32_t displayId;
  2202. if (mPointerController != NULL) {
  2203. if (moved || scrolled || buttonsChanged) {
  2204. mPointerController->setPresentation(
  2205. PointerControllerInterface::PRESENTATION_POINTER);
  2206. if (moved) {
  2207. mPointerController->move(deltaX, deltaY);
  2208. }
  2209. if (buttonsChanged) {
  2210. mPointerController->setButtonState(currentButtonState);
  2211. }
  2212. mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
  2213. }
  2214. float x, y;
  2215. mPointerController->getPosition(&x, &y);
  2216. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
  2217. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  2218. displayId = ADISPLAY_ID_DEFAULT;
  2219. } else {
  2220. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
  2221. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
  2222. displayId = ADISPLAY_ID_NONE;
  2223. }
  2224. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
  2225. // Moving an external trackball or mouse should wake the device.
  2226. // We don't do this for internal cursor devices to prevent them from waking up
  2227. // the device in your pocket.
  2228. // TODO: Use the input device configuration to control this behavior more finely.
  2229. uint32_t policyFlags = 0;
  2230. if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
  2231. policyFlags |= POLICY_FLAG_WAKE;
  2232. }
  2233. // Synthesize key down from buttons if needed.
  2234. synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
  2235. policyFlags, lastButtonState, currentButtonState);
  2236. // Send motion event.
  2237. if (downChanged || moved || scrolled || buttonsChanged) {
  2238. int32_t metaState = mContext->getGlobalMetaState();
  2239. int32_t buttonState = lastButtonState;
  2240. int32_t motionEventAction;
  2241. if (downChanged) {
  2242. motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
  2243. } else if (down || mPointerController == NULL) {
  2244. motionEventAction = AMOTION_EVENT_ACTION_MOVE;
  2245. } else {
  2246. motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
  2247. }
  2248. if (buttonsReleased) {
  2249. BitSet32 released(buttonsReleased);
  2250. while (!released.isEmpty()) {
  2251. int32_t actionButton = BitSet32::valueForBit(released.clearFirstMarkedBit());
  2252. buttonState &= ~actionButton;
  2253. NotifyMotionArgs releaseArgs(when, getDeviceId(), mSource, policyFlags,
  2254. AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0,
  2255. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  2256. displayId, 1, &pointerProperties, &pointerCoords,
  2257. mXPrecision, mYPrecision, downTime);
  2258. getListener()->notifyMotion(&releaseArgs);
  2259. }
  2260. }
  2261. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  2262. motionEventAction, 0, 0, metaState, currentButtonState,
  2263. AMOTION_EVENT_EDGE_FLAG_NONE,
  2264. displayId, 1, &pointerProperties, &pointerCoords,
  2265. mXPrecision, mYPrecision, downTime);
  2266. getListener()->notifyMotion(&args);
  2267. if (buttonsPressed) {
  2268. BitSet32 pressed(buttonsPressed);
  2269. while (!pressed.isEmpty()) {
  2270. int32_t actionButton = BitSet32::valueForBit(pressed.clearFirstMarkedBit());
  2271. buttonState |= actionButton;
  2272. NotifyMotionArgs pressArgs(when, getDeviceId(), mSource, policyFlags,
  2273. AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0,
  2274. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  2275. displayId, 1, &pointerProperties, &pointerCoords,
  2276. mXPrecision, mYPrecision, downTime);
  2277. getListener()->notifyMotion(&pressArgs);
  2278. }
  2279. }
  2280. ALOG_ASSERT(buttonState == currentButtonState);
  2281. // Send hover move after UP to tell the application that the mouse is hovering now.
  2282. if (motionEventAction == AMOTION_EVENT_ACTION_UP
  2283. && mPointerController != NULL) {
  2284. NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
  2285. AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
  2286. metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  2287. displayId, 1, &pointerProperties, &pointerCoords,
  2288. mXPrecision, mYPrecision, downTime);
  2289. getListener()->notifyMotion(&hoverArgs);
  2290. }
  2291. // Send scroll events.
  2292. if (scrolled) {
  2293. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
  2294. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
  2295. NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
  2296. AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState, currentButtonState,
  2297. AMOTION_EVENT_EDGE_FLAG_NONE,
  2298. displayId, 1, &pointerProperties, &pointerCoords,
  2299. mXPrecision, mYPrecision, downTime);
  2300. getListener()->notifyMotion(&scrollArgs);
  2301. }
  2302. }
  2303. // Synthesize key up from buttons if needed.
  2304. synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
  2305. policyFlags, lastButtonState, currentButtonState);
  2306. mCursorMotionAccumulator.finishSync();
  2307. mCursorScrollAccumulator.finishSync();
  2308. }
  2309. int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
  2310. if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
  2311. return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
  2312. } else {
  2313. return AKEY_STATE_UNKNOWN;
  2314. }
  2315. }
  2316. void CursorInputMapper::fadePointer() {
  2317. if (mPointerController != NULL) {
  2318. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  2319. }
  2320. }
  2321. // --- TouchInputMapper ---
  2322. TouchInputMapper::TouchInputMapper(InputDevice* device) :
  2323. InputMapper(device),
  2324. mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
  2325. mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
  2326. mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
  2327. }
  2328. TouchInputMapper::~TouchInputMapper() {
  2329. }
  2330. uint32_t TouchInputMapper::getSources() {
  2331. return mSource;
  2332. }
  2333. void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  2334. InputMapper::populateDeviceInfo(info);
  2335. if (mDeviceMode != DEVICE_MODE_DISABLED) {
  2336. info->addMotionRange(mOrientedRanges.x);
  2337. info->addMotionRange(mOrientedRanges.y);
  2338. info->addMotionRange(mOrientedRanges.pressure);
  2339. if (mOrientedRanges.haveSize) {
  2340. info->addMotionRange(mOrientedRanges.size);
  2341. }
  2342. if (mOrientedRanges.haveTouchSize) {
  2343. info->addMotionRange(mOrientedRanges.touchMajor);
  2344. info->addMotionRange(mOrientedRanges.touchMinor);
  2345. }
  2346. if (mOrientedRanges.haveToolSize) {
  2347. info->addMotionRange(mOrientedRanges.toolMajor);
  2348. info->addMotionRange(mOrientedRanges.toolMinor);
  2349. }
  2350. if (mOrientedRanges.haveOrientation) {
  2351. info->addMotionRange(mOrientedRanges.orientation);
  2352. }
  2353. if (mOrientedRanges.haveDistance) {
  2354. info->addMotionRange(mOrientedRanges.distance);
  2355. }
  2356. if (mOrientedRanges.haveTilt) {
  2357. info->addMotionRange(mOrientedRanges.tilt);
  2358. }
  2359. if (mCursorScrollAccumulator.haveRelativeVWheel()) {
  2360. info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
  2361. 0.0f);
  2362. }
  2363. if (mCursorScrollAccumulator.haveRelativeHWheel()) {
  2364. info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
  2365. 0.0f);
  2366. }
  2367. if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
  2368. const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
  2369. const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
  2370. info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
  2371. x.fuzz, x.resolution);
  2372. info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
  2373. y.fuzz, y.resolution);
  2374. info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
  2375. x.fuzz, x.resolution);
  2376. info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
  2377. y.fuzz, y.resolution);
  2378. }
  2379. info->setButtonUnderPad(mParameters.hasButtonUnderPad);
  2380. }
  2381. }
  2382. void TouchInputMapper::dump(String8& dump) {
  2383. dump.append(INDENT2 "Touch Input Mapper:\n");
  2384. dumpParameters(dump);
  2385. dumpVirtualKeys(dump);
  2386. dumpRawPointerAxes(dump);
  2387. dumpCalibration(dump);
  2388. dumpAffineTransformation(dump);
  2389. dumpSurface(dump);
  2390. dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
  2391. dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
  2392. dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
  2393. dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
  2394. dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
  2395. dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
  2396. dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
  2397. dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
  2398. dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
  2399. dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
  2400. dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
  2401. dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
  2402. dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
  2403. dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
  2404. dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
  2405. dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
  2406. dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
  2407. dump.appendFormat(INDENT3 "Last Raw Button State: 0x%08x\n", mLastRawState.buttonState);
  2408. dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
  2409. mLastRawState.rawPointerData.pointerCount);
  2410. for (uint32_t i = 0; i < mLastRawState.rawPointerData.pointerCount; i++) {
  2411. const RawPointerData::Pointer& pointer = mLastRawState.rawPointerData.pointers[i];
  2412. dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
  2413. "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
  2414. "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
  2415. "toolType=%d, isHovering=%s\n", i,
  2416. pointer.id, pointer.x, pointer.y, pointer.pressure,
  2417. pointer.touchMajor, pointer.touchMinor,
  2418. pointer.toolMajor, pointer.toolMinor,
  2419. pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
  2420. pointer.toolType, toString(pointer.isHovering));
  2421. }
  2422. dump.appendFormat(INDENT3 "Last Cooked Button State: 0x%08x\n", mLastCookedState.buttonState);
  2423. dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
  2424. mLastCookedState.cookedPointerData.pointerCount);
  2425. for (uint32_t i = 0; i < mLastCookedState.cookedPointerData.pointerCount; i++) {
  2426. const PointerProperties& pointerProperties =
  2427. mLastCookedState.cookedPointerData.pointerProperties[i];
  2428. const PointerCoords& pointerCoords = mLastCookedState.cookedPointerData.pointerCoords[i];
  2429. dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
  2430. "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
  2431. "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
  2432. "toolType=%d, isHovering=%s\n", i,
  2433. pointerProperties.id,
  2434. pointerCoords.getX(),
  2435. pointerCoords.getY(),
  2436. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
  2437. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
  2438. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
  2439. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
  2440. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
  2441. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
  2442. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
  2443. pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
  2444. pointerProperties.toolType,
  2445. toString(mLastCookedState.cookedPointerData.isHovering(i)));
  2446. }
  2447. dump.append(INDENT3 "Stylus Fusion:\n");
  2448. dump.appendFormat(INDENT4 "ExternalStylusConnected: %s\n",
  2449. toString(mExternalStylusConnected));
  2450. dump.appendFormat(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
  2451. dump.appendFormat(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
  2452. mExternalStylusFusionTimeout);
  2453. dump.append(INDENT3 "External Stylus State:\n");
  2454. dumpStylusState(dump, mExternalStylusState);
  2455. if (mDeviceMode == DEVICE_MODE_POINTER) {
  2456. dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
  2457. dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
  2458. mPointerXMovementScale);
  2459. dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
  2460. mPointerYMovementScale);
  2461. dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
  2462. mPointerXZoomScale);
  2463. dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
  2464. mPointerYZoomScale);
  2465. dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
  2466. mPointerGestureMaxSwipeWidth);
  2467. }
  2468. }
  2469. void TouchInputMapper::configure(nsecs_t when,
  2470. const InputReaderConfiguration* config, uint32_t changes) {
  2471. InputMapper::configure(when, config, changes);
  2472. mConfig = *config;
  2473. if (!changes) { // first time only
  2474. // Configure basic parameters.
  2475. configureParameters();
  2476. // Configure common accumulators.
  2477. mCursorScrollAccumulator.configure(getDevice());
  2478. mTouchButtonAccumulator.configure(getDevice());
  2479. // Configure absolute axis information.
  2480. configureRawPointerAxes();
  2481. // Prepare input device calibration.
  2482. parseCalibration();
  2483. resolveCalibration();
  2484. }
  2485. if (!changes || (changes & InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION)) {
  2486. // Update location calibration to reflect current settings
  2487. updateAffineTransformation();
  2488. }
  2489. if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
  2490. // Update pointer speed.
  2491. mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
  2492. mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
  2493. mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
  2494. }
  2495. bool resetNeeded = false;
  2496. if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
  2497. | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
  2498. | InputReaderConfiguration::CHANGE_SHOW_TOUCHES
  2499. | InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE
  2500. | InputReaderConfiguration::CHANGE_STYLUS_ICON_ENABLED))) {
  2501. // Configure device sources, surface dimensions, orientation and
  2502. // scaling factors.
  2503. configureSurface(when, &resetNeeded);
  2504. }
  2505. if (changes && resetNeeded) {
  2506. // Send reset, unless this is the first time the device has been configured,
  2507. // in which case the reader will call reset itself after all mappers are ready.
  2508. getDevice()->notifyReset(when);
  2509. }
  2510. }
  2511. void TouchInputMapper::resolveExternalStylusPresence() {
  2512. Vector<InputDeviceInfo> devices;
  2513. mContext->getExternalStylusDevices(devices);
  2514. mExternalStylusConnected = !devices.isEmpty();
  2515. if (!mExternalStylusConnected) {
  2516. resetExternalStylus();
  2517. }
  2518. }
  2519. void TouchInputMapper::configureParameters() {
  2520. // Use the pointer presentation mode for devices that do not support distinct
  2521. // multitouch. The spot-based presentation relies on being able to accurately
  2522. // locate two or more fingers on the touch pad.
  2523. mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
  2524. ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
  2525. String8 gestureModeString;
  2526. if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
  2527. gestureModeString)) {
  2528. if (gestureModeString == "pointer") {
  2529. mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
  2530. } else if (gestureModeString == "spots") {
  2531. mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
  2532. } else if (gestureModeString != "default") {
  2533. ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
  2534. }
  2535. }
  2536. if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
  2537. // The device is a touch screen.
  2538. mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
  2539. } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
  2540. // The device is a pointing device like a track pad.
  2541. mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
  2542. } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
  2543. || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
  2544. // The device is a cursor device with a touch pad attached.
  2545. // By default don't use the touch pad to move the pointer.
  2546. mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
  2547. } else {
  2548. // The device is a touch pad of unknown purpose.
  2549. mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
  2550. }
  2551. mParameters.hasButtonUnderPad=
  2552. getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_BUTTONPAD);
  2553. String8 deviceTypeString;
  2554. if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
  2555. deviceTypeString)) {
  2556. if (deviceTypeString == "touchScreen") {
  2557. mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
  2558. } else if (deviceTypeString == "touchPad") {
  2559. mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
  2560. } else if (deviceTypeString == "touchNavigation") {
  2561. mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION;
  2562. } else if (deviceTypeString == "pointer") {
  2563. mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
  2564. } else if (deviceTypeString == "gesture") {
  2565. mParameters.deviceType = Parameters::DEVICE_TYPE_GESTURE_SENSOR;
  2566. } else if (deviceTypeString != "default") {
  2567. ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
  2568. }
  2569. }
  2570. mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
  2571. getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
  2572. mParameters.orientationAware);
  2573. mParameters.hasAssociatedDisplay = false;
  2574. mParameters.associatedDisplayIsExternal = false;
  2575. if (mParameters.orientationAware
  2576. || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
  2577. || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
  2578. mParameters.hasAssociatedDisplay = true;
  2579. mParameters.associatedDisplayIsExternal =
  2580. mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
  2581. && getDevice()->isExternal();
  2582. }
  2583. // Initial downs on external touch devices should wake the device.
  2584. // Normally we don't do this for internal touch screens to prevent them from waking
  2585. // up in your pocket but you can enable it using the input device configuration.
  2586. mParameters.wake = getDevice()->isExternal();
  2587. getDevice()->getConfiguration().tryGetProperty(String8("touch.wake"),
  2588. mParameters.wake);
  2589. }
  2590. void TouchInputMapper::dumpParameters(String8& dump) {
  2591. dump.append(INDENT3 "Parameters:\n");
  2592. switch (mParameters.gestureMode) {
  2593. case Parameters::GESTURE_MODE_POINTER:
  2594. dump.append(INDENT4 "GestureMode: pointer\n");
  2595. break;
  2596. case Parameters::GESTURE_MODE_SPOTS:
  2597. dump.append(INDENT4 "GestureMode: spots\n");
  2598. break;
  2599. default:
  2600. assert(false);
  2601. }
  2602. switch (mParameters.deviceType) {
  2603. case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
  2604. dump.append(INDENT4 "DeviceType: touchScreen\n");
  2605. break;
  2606. case Parameters::DEVICE_TYPE_TOUCH_PAD:
  2607. dump.append(INDENT4 "DeviceType: touchPad\n");
  2608. break;
  2609. case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION:
  2610. dump.append(INDENT4 "DeviceType: touchNavigation\n");
  2611. break;
  2612. case Parameters::DEVICE_TYPE_POINTER:
  2613. dump.append(INDENT4 "DeviceType: pointer\n");
  2614. break;
  2615. case Parameters::DEVICE_TYPE_GESTURE_SENSOR:
  2616. dump.append(INDENT4 "DeviceType: gesture\n");
  2617. break;
  2618. default:
  2619. ALOG_ASSERT(false);
  2620. }
  2621. dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
  2622. toString(mParameters.hasAssociatedDisplay),
  2623. toString(mParameters.associatedDisplayIsExternal));
  2624. dump.appendFormat(INDENT4 "OrientationAware: %s\n",
  2625. toString(mParameters.orientationAware));
  2626. }
  2627. void TouchInputMapper::configureRawPointerAxes() {
  2628. mRawPointerAxes.clear();
  2629. }
  2630. void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
  2631. dump.append(INDENT3 "Raw Touch Axes:\n");
  2632. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
  2633. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
  2634. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
  2635. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
  2636. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
  2637. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
  2638. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
  2639. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
  2640. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
  2641. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
  2642. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
  2643. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
  2644. dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
  2645. }
  2646. bool TouchInputMapper::hasExternalStylus() const {
  2647. return mExternalStylusConnected;
  2648. }
  2649. void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
  2650. int32_t oldDeviceMode = mDeviceMode;
  2651. resolveExternalStylusPresence();
  2652. // Determine device mode.
  2653. if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
  2654. && mConfig.pointerGesturesEnabled) {
  2655. mSource = AINPUT_SOURCE_MOUSE;
  2656. mDeviceMode = DEVICE_MODE_POINTER;
  2657. if (hasStylus()) {
  2658. mSource |= AINPUT_SOURCE_STYLUS;
  2659. }
  2660. } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
  2661. && mParameters.hasAssociatedDisplay) {
  2662. mSource = AINPUT_SOURCE_TOUCHSCREEN;
  2663. mDeviceMode = DEVICE_MODE_DIRECT;
  2664. if (hasStylus()) {
  2665. mSource |= AINPUT_SOURCE_STYLUS;
  2666. }
  2667. if (hasExternalStylus()) {
  2668. mSource |= AINPUT_SOURCE_BLUETOOTH_STYLUS;
  2669. }
  2670. } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) {
  2671. mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
  2672. mDeviceMode = DEVICE_MODE_NAVIGATION;
  2673. } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_GESTURE_SENSOR) {
  2674. mSource = AINPUT_SOURCE_GESTURE_SENSOR;
  2675. mDeviceMode = DEVICE_MODE_UNSCALED;
  2676. } else {
  2677. mSource = AINPUT_SOURCE_TOUCHPAD;
  2678. mDeviceMode = DEVICE_MODE_UNSCALED;
  2679. }
  2680. // Ensure we have valid X and Y axes.
  2681. if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
  2682. ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
  2683. "The device will be inoperable.", getDeviceName().string());
  2684. mDeviceMode = DEVICE_MODE_DISABLED;
  2685. return;
  2686. }
  2687. // Raw width and height in the natural orientation.
  2688. int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
  2689. int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
  2690. // Get associated display dimensions.
  2691. DisplayViewport newViewport;
  2692. if (mParameters.hasAssociatedDisplay) {
  2693. if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
  2694. ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
  2695. "display. The device will be inoperable until the display size "
  2696. "becomes available.",
  2697. getDeviceName().string());
  2698. mDeviceMode = DEVICE_MODE_DISABLED;
  2699. return;
  2700. }
  2701. } else {
  2702. newViewport.setNonDisplayViewport(rawWidth, rawHeight);
  2703. }
  2704. bool viewportChanged = mViewport != newViewport;
  2705. if (viewportChanged) {
  2706. mViewport = newViewport;
  2707. if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
  2708. // Convert rotated viewport to natural surface coordinates.
  2709. int32_t naturalLogicalWidth, naturalLogicalHeight;
  2710. int32_t naturalPhysicalWidth, naturalPhysicalHeight;
  2711. int32_t naturalPhysicalLeft, naturalPhysicalTop;
  2712. int32_t naturalDeviceWidth, naturalDeviceHeight;
  2713. switch (mViewport.orientation) {
  2714. case DISPLAY_ORIENTATION_90:
  2715. naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
  2716. naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
  2717. naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
  2718. naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
  2719. naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
  2720. naturalPhysicalTop = mViewport.physicalLeft;
  2721. naturalDeviceWidth = mViewport.deviceHeight;
  2722. naturalDeviceHeight = mViewport.deviceWidth;
  2723. break;
  2724. case DISPLAY_ORIENTATION_180:
  2725. naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
  2726. naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
  2727. naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
  2728. naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
  2729. naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
  2730. naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
  2731. naturalDeviceWidth = mViewport.deviceWidth;
  2732. naturalDeviceHeight = mViewport.deviceHeight;
  2733. break;
  2734. case DISPLAY_ORIENTATION_270:
  2735. naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
  2736. naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
  2737. naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
  2738. naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
  2739. naturalPhysicalLeft = mViewport.physicalTop;
  2740. naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
  2741. naturalDeviceWidth = mViewport.deviceHeight;
  2742. naturalDeviceHeight = mViewport.deviceWidth;
  2743. break;
  2744. case DISPLAY_ORIENTATION_0:
  2745. default:
  2746. naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
  2747. naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
  2748. naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
  2749. naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
  2750. naturalPhysicalLeft = mViewport.physicalLeft;
  2751. naturalPhysicalTop = mViewport.physicalTop;
  2752. naturalDeviceWidth = mViewport.deviceWidth;
  2753. naturalDeviceHeight = mViewport.deviceHeight;
  2754. break;
  2755. }
  2756. mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
  2757. mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
  2758. mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
  2759. mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
  2760. mSurfaceOrientation = mParameters.orientationAware ?
  2761. mViewport.orientation : DISPLAY_ORIENTATION_0;
  2762. } else {
  2763. mSurfaceWidth = rawWidth;
  2764. mSurfaceHeight = rawHeight;
  2765. mSurfaceLeft = 0;
  2766. mSurfaceTop = 0;
  2767. mSurfaceOrientation = DISPLAY_ORIENTATION_0;
  2768. }
  2769. }
  2770. // If moving between pointer modes, need to reset some state.
  2771. bool deviceModeChanged = mDeviceMode != oldDeviceMode;
  2772. if (deviceModeChanged) {
  2773. mOrientedRanges.clear();
  2774. }
  2775. // Create pointer controller if needed.
  2776. if (mDeviceMode == DEVICE_MODE_POINTER ||
  2777. (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
  2778. if (mPointerController == NULL) {
  2779. mPointerController = getPolicy()->obtainPointerController(getDeviceId());
  2780. }
  2781. } else {
  2782. mPointerController.clear();
  2783. }
  2784. if (viewportChanged || deviceModeChanged) {
  2785. ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
  2786. "display id %d",
  2787. getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
  2788. mSurfaceOrientation, mDeviceMode, mViewport.displayId);
  2789. // Configure X and Y factors.
  2790. mXScale = float(mSurfaceWidth) / rawWidth;
  2791. mYScale = float(mSurfaceHeight) / rawHeight;
  2792. mXTranslate = -mSurfaceLeft;
  2793. mYTranslate = -mSurfaceTop;
  2794. mXPrecision = 1.0f / mXScale;
  2795. mYPrecision = 1.0f / mYScale;
  2796. mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
  2797. mOrientedRanges.x.source = mSource;
  2798. mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
  2799. mOrientedRanges.y.source = mSource;
  2800. configureVirtualKeys();
  2801. // Scale factor for terms that are not oriented in a particular axis.
  2802. // If the pixels are square then xScale == yScale otherwise we fake it
  2803. // by choosing an average.
  2804. mGeometricScale = avg(mXScale, mYScale);
  2805. // Size of diagonal axis.
  2806. float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
  2807. // Size factors.
  2808. if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
  2809. if (mRawPointerAxes.touchMajor.valid
  2810. && mRawPointerAxes.touchMajor.maxValue != 0) {
  2811. mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
  2812. } else if (mRawPointerAxes.toolMajor.valid
  2813. && mRawPointerAxes.toolMajor.maxValue != 0) {
  2814. mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
  2815. } else {
  2816. mSizeScale = 0.0f;
  2817. }
  2818. mOrientedRanges.haveTouchSize = true;
  2819. mOrientedRanges.haveToolSize = true;
  2820. mOrientedRanges.haveSize = true;
  2821. mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
  2822. mOrientedRanges.touchMajor.source = mSource;
  2823. mOrientedRanges.touchMajor.min = 0;
  2824. mOrientedRanges.touchMajor.max = diagonalSize;
  2825. mOrientedRanges.touchMajor.flat = 0;
  2826. mOrientedRanges.touchMajor.fuzz = 0;
  2827. mOrientedRanges.touchMajor.resolution = 0;
  2828. mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
  2829. mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
  2830. mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
  2831. mOrientedRanges.toolMajor.source = mSource;
  2832. mOrientedRanges.toolMajor.min = 0;
  2833. mOrientedRanges.toolMajor.max = diagonalSize;
  2834. mOrientedRanges.toolMajor.flat = 0;
  2835. mOrientedRanges.toolMajor.fuzz = 0;
  2836. mOrientedRanges.toolMajor.resolution = 0;
  2837. mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
  2838. mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
  2839. mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
  2840. mOrientedRanges.size.source = mSource;
  2841. mOrientedRanges.size.min = 0;
  2842. mOrientedRanges.size.max = 1.0;
  2843. mOrientedRanges.size.flat = 0;
  2844. mOrientedRanges.size.fuzz = 0;
  2845. mOrientedRanges.size.resolution = 0;
  2846. } else {
  2847. mSizeScale = 0.0f;
  2848. }
  2849. // Pressure factors.
  2850. mPressureScale = 0;
  2851. if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
  2852. || mCalibration.pressureCalibration
  2853. == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
  2854. if (mCalibration.havePressureScale) {
  2855. mPressureScale = mCalibration.pressureScale;
  2856. } else if (mRawPointerAxes.pressure.valid
  2857. && mRawPointerAxes.pressure.maxValue != 0) {
  2858. mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
  2859. }
  2860. }
  2861. mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
  2862. mOrientedRanges.pressure.source = mSource;
  2863. mOrientedRanges.pressure.min = 0;
  2864. mOrientedRanges.pressure.max = 1.0;
  2865. mOrientedRanges.pressure.flat = 0;
  2866. mOrientedRanges.pressure.fuzz = 0;
  2867. mOrientedRanges.pressure.resolution = 0;
  2868. // Tilt
  2869. mTiltXCenter = 0;
  2870. mTiltXScale = 0;
  2871. mTiltYCenter = 0;
  2872. mTiltYScale = 0;
  2873. mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
  2874. if (mHaveTilt) {
  2875. mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
  2876. mRawPointerAxes.tiltX.maxValue);
  2877. mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
  2878. mRawPointerAxes.tiltY.maxValue);
  2879. mTiltXScale = M_PI / 180;
  2880. mTiltYScale = M_PI / 180;
  2881. mOrientedRanges.haveTilt = true;
  2882. mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
  2883. mOrientedRanges.tilt.source = mSource;
  2884. mOrientedRanges.tilt.min = 0;
  2885. mOrientedRanges.tilt.max = M_PI_2;
  2886. mOrientedRanges.tilt.flat = 0;
  2887. mOrientedRanges.tilt.fuzz = 0;
  2888. mOrientedRanges.tilt.resolution = 0;
  2889. }
  2890. // Orientation
  2891. mOrientationScale = 0;
  2892. if (mHaveTilt) {
  2893. mOrientedRanges.haveOrientation = true;
  2894. mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
  2895. mOrientedRanges.orientation.source = mSource;
  2896. mOrientedRanges.orientation.min = -M_PI;
  2897. mOrientedRanges.orientation.max = M_PI;
  2898. mOrientedRanges.orientation.flat = 0;
  2899. mOrientedRanges.orientation.fuzz = 0;
  2900. mOrientedRanges.orientation.resolution = 0;
  2901. } else if (mCalibration.orientationCalibration !=
  2902. Calibration::ORIENTATION_CALIBRATION_NONE) {
  2903. if (mCalibration.orientationCalibration
  2904. == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
  2905. if (mRawPointerAxes.orientation.valid) {
  2906. if (mRawPointerAxes.orientation.maxValue > 0) {
  2907. mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
  2908. } else if (mRawPointerAxes.orientation.minValue < 0) {
  2909. mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
  2910. } else {
  2911. mOrientationScale = 0;
  2912. }
  2913. }
  2914. }
  2915. mOrientedRanges.haveOrientation = true;
  2916. mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
  2917. mOrientedRanges.orientation.source = mSource;
  2918. mOrientedRanges.orientation.min = -M_PI_2;
  2919. mOrientedRanges.orientation.max = M_PI_2;
  2920. mOrientedRanges.orientation.flat = 0;
  2921. mOrientedRanges.orientation.fuzz = 0;
  2922. mOrientedRanges.orientation.resolution = 0;
  2923. }
  2924. // Distance
  2925. mDistanceScale = 0;
  2926. if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
  2927. if (mCalibration.distanceCalibration
  2928. == Calibration::DISTANCE_CALIBRATION_SCALED) {
  2929. if (mCalibration.haveDistanceScale) {
  2930. mDistanceScale = mCalibration.distanceScale;
  2931. } else {
  2932. mDistanceScale = 1.0f;
  2933. }
  2934. }
  2935. mOrientedRanges.haveDistance = true;
  2936. mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
  2937. mOrientedRanges.distance.source = mSource;
  2938. mOrientedRanges.distance.min =
  2939. mRawPointerAxes.distance.minValue * mDistanceScale;
  2940. mOrientedRanges.distance.max =
  2941. mRawPointerAxes.distance.maxValue * mDistanceScale;
  2942. mOrientedRanges.distance.flat = 0;
  2943. mOrientedRanges.distance.fuzz =
  2944. mRawPointerAxes.distance.fuzz * mDistanceScale;
  2945. mOrientedRanges.distance.resolution = 0;
  2946. }
  2947. // Compute oriented precision, scales and ranges.
  2948. // Note that the maximum value reported is an inclusive maximum value so it is one
  2949. // unit less than the total width or height of surface.
  2950. switch (mSurfaceOrientation) {
  2951. case DISPLAY_ORIENTATION_90:
  2952. case DISPLAY_ORIENTATION_270:
  2953. mOrientedXPrecision = mYPrecision;
  2954. mOrientedYPrecision = mXPrecision;
  2955. mOrientedRanges.x.min = mYTranslate;
  2956. mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
  2957. mOrientedRanges.x.flat = 0;
  2958. mOrientedRanges.x.fuzz = 0;
  2959. mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
  2960. mOrientedRanges.y.min = mXTranslate;
  2961. mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
  2962. mOrientedRanges.y.flat = 0;
  2963. mOrientedRanges.y.fuzz = 0;
  2964. mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
  2965. break;
  2966. default:
  2967. mOrientedXPrecision = mXPrecision;
  2968. mOrientedYPrecision = mYPrecision;
  2969. mOrientedRanges.x.min = mXTranslate;
  2970. mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
  2971. mOrientedRanges.x.flat = 0;
  2972. mOrientedRanges.x.fuzz = 0;
  2973. mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
  2974. mOrientedRanges.y.min = mYTranslate;
  2975. mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
  2976. mOrientedRanges.y.flat = 0;
  2977. mOrientedRanges.y.fuzz = 0;
  2978. mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
  2979. break;
  2980. }
  2981. // Location
  2982. updateAffineTransformation();
  2983. if (mDeviceMode == DEVICE_MODE_POINTER) {
  2984. // Compute pointer gesture detection parameters.
  2985. float rawDiagonal = hypotf(rawWidth, rawHeight);
  2986. float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
  2987. // Scale movements such that one whole swipe of the touch pad covers a
  2988. // given area relative to the diagonal size of the display when no acceleration
  2989. // is applied.
  2990. // Assume that the touch pad has a square aspect ratio such that movements in
  2991. // X and Y of the same number of raw units cover the same physical distance.
  2992. mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
  2993. * displayDiagonal / rawDiagonal;
  2994. mPointerYMovementScale = mPointerXMovementScale;
  2995. // Scale zooms to cover a smaller range of the display than movements do.
  2996. // This value determines the area around the pointer that is affected by freeform
  2997. // pointer gestures.
  2998. mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
  2999. * displayDiagonal / rawDiagonal;
  3000. mPointerYZoomScale = mPointerXZoomScale;
  3001. // Max width between pointers to detect a swipe gesture is more than some fraction
  3002. // of the diagonal axis of the touch pad. Touches that are wider than this are
  3003. // translated into freeform gestures.
  3004. mPointerGestureMaxSwipeWidth =
  3005. mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
  3006. // Abort current pointer usages because the state has changed.
  3007. abortPointerUsage(when, 0 /*policyFlags*/);
  3008. }
  3009. // Inform the dispatcher about the changes.
  3010. *outResetNeeded = true;
  3011. bumpGeneration();
  3012. }
  3013. }
  3014. void TouchInputMapper::dumpSurface(String8& dump) {
  3015. dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
  3016. "logicalFrame=[%d, %d, %d, %d], "
  3017. "physicalFrame=[%d, %d, %d, %d], "
  3018. "deviceSize=[%d, %d]\n",
  3019. mViewport.displayId, mViewport.orientation,
  3020. mViewport.logicalLeft, mViewport.logicalTop,
  3021. mViewport.logicalRight, mViewport.logicalBottom,
  3022. mViewport.physicalLeft, mViewport.physicalTop,
  3023. mViewport.physicalRight, mViewport.physicalBottom,
  3024. mViewport.deviceWidth, mViewport.deviceHeight);
  3025. dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
  3026. dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
  3027. dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
  3028. dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
  3029. dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
  3030. }
  3031. void TouchInputMapper::configureVirtualKeys() {
  3032. Vector<VirtualKeyDefinition> virtualKeyDefinitions;
  3033. getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
  3034. mVirtualKeys.clear();
  3035. if (virtualKeyDefinitions.size() == 0) {
  3036. return;
  3037. }
  3038. mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
  3039. int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
  3040. int32_t touchScreenTop = mRawPointerAxes.y.minValue;
  3041. int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
  3042. int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
  3043. for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
  3044. const VirtualKeyDefinition& virtualKeyDefinition =
  3045. virtualKeyDefinitions[i];
  3046. mVirtualKeys.add();
  3047. VirtualKey& virtualKey = mVirtualKeys.editTop();
  3048. virtualKey.scanCode = virtualKeyDefinition.scanCode;
  3049. int32_t keyCode;
  3050. int32_t dummyKeyMetaState;
  3051. uint32_t flags;
  3052. if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, 0,
  3053. &keyCode, &dummyKeyMetaState, &flags)) {
  3054. ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
  3055. virtualKey.scanCode);
  3056. mVirtualKeys.pop(); // drop the key
  3057. continue;
  3058. }
  3059. virtualKey.keyCode = keyCode;
  3060. virtualKey.flags = flags;
  3061. // convert the key definition's display coordinates into touch coordinates for a hit box
  3062. int32_t halfWidth = virtualKeyDefinition.width / 2;
  3063. int32_t halfHeight = virtualKeyDefinition.height / 2;
  3064. virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
  3065. * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
  3066. virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
  3067. * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
  3068. virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
  3069. * touchScreenHeight / mSurfaceHeight + touchScreenTop;
  3070. virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
  3071. * touchScreenHeight / mSurfaceHeight + touchScreenTop;
  3072. }
  3073. }
  3074. void TouchInputMapper::dumpVirtualKeys(String8& dump) {
  3075. if (!mVirtualKeys.isEmpty()) {
  3076. dump.append(INDENT3 "Virtual Keys:\n");
  3077. for (size_t i = 0; i < mVirtualKeys.size(); i++) {
  3078. const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
  3079. dump.appendFormat(INDENT4 "%zu: scanCode=%d, keyCode=%d, "
  3080. "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
  3081. i, virtualKey.scanCode, virtualKey.keyCode,
  3082. virtualKey.hitLeft, virtualKey.hitRight,
  3083. virtualKey.hitTop, virtualKey.hitBottom);
  3084. }
  3085. }
  3086. }
  3087. void TouchInputMapper::parseCalibration() {
  3088. const PropertyMap& in = getDevice()->getConfiguration();
  3089. Calibration& out = mCalibration;
  3090. // Size
  3091. out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
  3092. String8 sizeCalibrationString;
  3093. if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
  3094. if (sizeCalibrationString == "none") {
  3095. out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
  3096. } else if (sizeCalibrationString == "geometric") {
  3097. out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
  3098. } else if (sizeCalibrationString == "diameter") {
  3099. out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
  3100. } else if (sizeCalibrationString == "box") {
  3101. out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
  3102. } else if (sizeCalibrationString == "area") {
  3103. out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
  3104. } else if (sizeCalibrationString != "default") {
  3105. ALOGW("Invalid value for touch.size.calibration: '%s'",
  3106. sizeCalibrationString.string());
  3107. }
  3108. }
  3109. out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
  3110. out.sizeScale);
  3111. out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
  3112. out.sizeBias);
  3113. out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
  3114. out.sizeIsSummed);
  3115. // Pressure
  3116. out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
  3117. String8 pressureCalibrationString;
  3118. if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
  3119. if (pressureCalibrationString == "none") {
  3120. out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
  3121. } else if (pressureCalibrationString == "physical") {
  3122. out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
  3123. } else if (pressureCalibrationString == "amplitude") {
  3124. out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
  3125. } else if (pressureCalibrationString != "default") {
  3126. ALOGW("Invalid value for touch.pressure.calibration: '%s'",
  3127. pressureCalibrationString.string());
  3128. }
  3129. }
  3130. out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
  3131. out.pressureScale);
  3132. // Orientation
  3133. out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
  3134. String8 orientationCalibrationString;
  3135. if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
  3136. if (orientationCalibrationString == "none") {
  3137. out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
  3138. } else if (orientationCalibrationString == "interpolated") {
  3139. out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
  3140. } else if (orientationCalibrationString == "vector") {
  3141. out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
  3142. } else if (orientationCalibrationString != "default") {
  3143. ALOGW("Invalid value for touch.orientation.calibration: '%s'",
  3144. orientationCalibrationString.string());
  3145. }
  3146. }
  3147. // Distance
  3148. out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
  3149. String8 distanceCalibrationString;
  3150. if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
  3151. if (distanceCalibrationString == "none") {
  3152. out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
  3153. } else if (distanceCalibrationString == "scaled") {
  3154. out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
  3155. } else if (distanceCalibrationString != "default") {
  3156. ALOGW("Invalid value for touch.distance.calibration: '%s'",
  3157. distanceCalibrationString.string());
  3158. }
  3159. }
  3160. out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
  3161. out.distanceScale);
  3162. out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT;
  3163. String8 coverageCalibrationString;
  3164. if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
  3165. if (coverageCalibrationString == "none") {
  3166. out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
  3167. } else if (coverageCalibrationString == "box") {
  3168. out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX;
  3169. } else if (coverageCalibrationString != "default") {
  3170. ALOGW("Invalid value for touch.coverage.calibration: '%s'",
  3171. coverageCalibrationString.string());
  3172. }
  3173. }
  3174. }
  3175. void TouchInputMapper::resolveCalibration() {
  3176. // Size
  3177. if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
  3178. if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
  3179. mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
  3180. }
  3181. } else {
  3182. mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
  3183. }
  3184. // Pressure
  3185. if (mRawPointerAxes.pressure.valid) {
  3186. if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
  3187. mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
  3188. }
  3189. } else {
  3190. mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
  3191. }
  3192. // Orientation
  3193. if (mRawPointerAxes.orientation.valid) {
  3194. if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
  3195. mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
  3196. }
  3197. } else {
  3198. mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
  3199. }
  3200. // Distance
  3201. if (mRawPointerAxes.distance.valid) {
  3202. if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
  3203. mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
  3204. }
  3205. } else {
  3206. mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
  3207. }
  3208. // Coverage
  3209. if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) {
  3210. mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
  3211. }
  3212. }
  3213. void TouchInputMapper::dumpCalibration(String8& dump) {
  3214. dump.append(INDENT3 "Calibration:\n");
  3215. // Size
  3216. switch (mCalibration.sizeCalibration) {
  3217. case Calibration::SIZE_CALIBRATION_NONE:
  3218. dump.append(INDENT4 "touch.size.calibration: none\n");
  3219. break;
  3220. case Calibration::SIZE_CALIBRATION_GEOMETRIC:
  3221. dump.append(INDENT4 "touch.size.calibration: geometric\n");
  3222. break;
  3223. case Calibration::SIZE_CALIBRATION_DIAMETER:
  3224. dump.append(INDENT4 "touch.size.calibration: diameter\n");
  3225. break;
  3226. case Calibration::SIZE_CALIBRATION_BOX:
  3227. dump.append(INDENT4 "touch.size.calibration: box\n");
  3228. break;
  3229. case Calibration::SIZE_CALIBRATION_AREA:
  3230. dump.append(INDENT4 "touch.size.calibration: area\n");
  3231. break;
  3232. default:
  3233. ALOG_ASSERT(false);
  3234. }
  3235. if (mCalibration.haveSizeScale) {
  3236. dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
  3237. mCalibration.sizeScale);
  3238. }
  3239. if (mCalibration.haveSizeBias) {
  3240. dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
  3241. mCalibration.sizeBias);
  3242. }
  3243. if (mCalibration.haveSizeIsSummed) {
  3244. dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
  3245. toString(mCalibration.sizeIsSummed));
  3246. }
  3247. // Pressure
  3248. switch (mCalibration.pressureCalibration) {
  3249. case Calibration::PRESSURE_CALIBRATION_NONE:
  3250. dump.append(INDENT4 "touch.pressure.calibration: none\n");
  3251. break;
  3252. case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
  3253. dump.append(INDENT4 "touch.pressure.calibration: physical\n");
  3254. break;
  3255. case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
  3256. dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
  3257. break;
  3258. default:
  3259. ALOG_ASSERT(false);
  3260. }
  3261. if (mCalibration.havePressureScale) {
  3262. dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
  3263. mCalibration.pressureScale);
  3264. }
  3265. // Orientation
  3266. switch (mCalibration.orientationCalibration) {
  3267. case Calibration::ORIENTATION_CALIBRATION_NONE:
  3268. dump.append(INDENT4 "touch.orientation.calibration: none\n");
  3269. break;
  3270. case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
  3271. dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
  3272. break;
  3273. case Calibration::ORIENTATION_CALIBRATION_VECTOR:
  3274. dump.append(INDENT4 "touch.orientation.calibration: vector\n");
  3275. break;
  3276. default:
  3277. ALOG_ASSERT(false);
  3278. }
  3279. // Distance
  3280. switch (mCalibration.distanceCalibration) {
  3281. case Calibration::DISTANCE_CALIBRATION_NONE:
  3282. dump.append(INDENT4 "touch.distance.calibration: none\n");
  3283. break;
  3284. case Calibration::DISTANCE_CALIBRATION_SCALED:
  3285. dump.append(INDENT4 "touch.distance.calibration: scaled\n");
  3286. break;
  3287. default:
  3288. ALOG_ASSERT(false);
  3289. }
  3290. if (mCalibration.haveDistanceScale) {
  3291. dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
  3292. mCalibration.distanceScale);
  3293. }
  3294. switch (mCalibration.coverageCalibration) {
  3295. case Calibration::COVERAGE_CALIBRATION_NONE:
  3296. dump.append(INDENT4 "touch.coverage.calibration: none\n");
  3297. break;
  3298. case Calibration::COVERAGE_CALIBRATION_BOX:
  3299. dump.append(INDENT4 "touch.coverage.calibration: box\n");
  3300. break;
  3301. default:
  3302. ALOG_ASSERT(false);
  3303. }
  3304. }
  3305. void TouchInputMapper::dumpAffineTransformation(String8& dump) {
  3306. dump.append(INDENT3 "Affine Transformation:\n");
  3307. dump.appendFormat(INDENT4 "X scale: %0.3f\n", mAffineTransform.x_scale);
  3308. dump.appendFormat(INDENT4 "X ymix: %0.3f\n", mAffineTransform.x_ymix);
  3309. dump.appendFormat(INDENT4 "X offset: %0.3f\n", mAffineTransform.x_offset);
  3310. dump.appendFormat(INDENT4 "Y xmix: %0.3f\n", mAffineTransform.y_xmix);
  3311. dump.appendFormat(INDENT4 "Y scale: %0.3f\n", mAffineTransform.y_scale);
  3312. dump.appendFormat(INDENT4 "Y offset: %0.3f\n", mAffineTransform.y_offset);
  3313. }
  3314. void TouchInputMapper::updateAffineTransformation() {
  3315. mAffineTransform = getPolicy()->getTouchAffineTransformation(mDevice->getDescriptor(),
  3316. mSurfaceOrientation);
  3317. }
  3318. void TouchInputMapper::reset(nsecs_t when) {
  3319. mCursorButtonAccumulator.reset(getDevice());
  3320. mCursorScrollAccumulator.reset(getDevice());
  3321. mTouchButtonAccumulator.reset(getDevice());
  3322. mPointerVelocityControl.reset();
  3323. mWheelXVelocityControl.reset();
  3324. mWheelYVelocityControl.reset();
  3325. mRawStatesPending.clear();
  3326. mCurrentRawState.clear();
  3327. mCurrentCookedState.clear();
  3328. mLastRawState.clear();
  3329. mLastCookedState.clear();
  3330. mPointerUsage = POINTER_USAGE_NONE;
  3331. mSentHoverEnter = false;
  3332. mHavePointerIds = false;
  3333. mCurrentMotionAborted = false;
  3334. mDownTime = 0;
  3335. mCurrentVirtualKey.down = false;
  3336. mPointerGesture.reset();
  3337. mPointerSimple.reset();
  3338. resetExternalStylus();
  3339. if (mPointerController != NULL) {
  3340. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  3341. mPointerController->clearSpots();
  3342. }
  3343. InputMapper::reset(when);
  3344. }
  3345. void TouchInputMapper::resetExternalStylus() {
  3346. mExternalStylusState.clear();
  3347. mExternalStylusId = -1;
  3348. mExternalStylusFusionTimeout = LLONG_MAX;
  3349. mExternalStylusDataPending = false;
  3350. }
  3351. void TouchInputMapper::clearStylusDataPendingFlags() {
  3352. mExternalStylusDataPending = false;
  3353. mExternalStylusFusionTimeout = LLONG_MAX;
  3354. }
  3355. void TouchInputMapper::process(const RawEvent* rawEvent) {
  3356. mCursorButtonAccumulator.process(rawEvent);
  3357. mCursorScrollAccumulator.process(rawEvent);
  3358. mTouchButtonAccumulator.process(rawEvent);
  3359. if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
  3360. sync(rawEvent->when);
  3361. }
  3362. }
  3363. void TouchInputMapper::sync(nsecs_t when) {
  3364. const RawState* last = mRawStatesPending.isEmpty() ?
  3365. &mCurrentRawState : &mRawStatesPending.top();
  3366. // Push a new state.
  3367. mRawStatesPending.push();
  3368. RawState* next = &mRawStatesPending.editTop();
  3369. next->clear();
  3370. next->when = when;
  3371. // Sync button state.
  3372. next->buttonState = mTouchButtonAccumulator.getButtonState()
  3373. | mCursorButtonAccumulator.getButtonState();
  3374. // Sync scroll
  3375. next->rawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
  3376. next->rawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
  3377. mCursorScrollAccumulator.finishSync();
  3378. // Sync touch
  3379. syncTouch(when, next);
  3380. // Assign pointer ids.
  3381. if (!mHavePointerIds) {
  3382. assignPointerIds(last, next);
  3383. }
  3384. #if DEBUG_RAW_EVENTS
  3385. ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
  3386. "hovering ids 0x%08x -> 0x%08x",
  3387. last->rawPointerData.pointerCount,
  3388. next->rawPointerData.pointerCount,
  3389. last->rawPointerData.touchingIdBits.value,
  3390. next->rawPointerData.touchingIdBits.value,
  3391. last->rawPointerData.hoveringIdBits.value,
  3392. next->rawPointerData.hoveringIdBits.value);
  3393. #endif
  3394. processRawTouches(false /*timeout*/);
  3395. }
  3396. void TouchInputMapper::processRawTouches(bool timeout) {
  3397. if (mDeviceMode == DEVICE_MODE_DISABLED) {
  3398. // Drop all input if the device is disabled.
  3399. mCurrentRawState.clear();
  3400. mRawStatesPending.clear();
  3401. return;
  3402. }
  3403. // Drain any pending touch states. The invariant here is that the mCurrentRawState is always
  3404. // valid and must go through the full cook and dispatch cycle. This ensures that anything
  3405. // touching the current state will only observe the events that have been dispatched to the
  3406. // rest of the pipeline.
  3407. const size_t N = mRawStatesPending.size();
  3408. size_t count;
  3409. for(count = 0; count < N; count++) {
  3410. const RawState& next = mRawStatesPending[count];
  3411. // A failure to assign the stylus id means that we're waiting on stylus data
  3412. // and so should defer the rest of the pipeline.
  3413. if (assignExternalStylusId(next, timeout)) {
  3414. break;
  3415. }
  3416. // All ready to go.
  3417. clearStylusDataPendingFlags();
  3418. mCurrentRawState.copyFrom(next);
  3419. if (mCurrentRawState.when < mLastRawState.when) {
  3420. mCurrentRawState.when = mLastRawState.when;
  3421. }
  3422. cookAndDispatch(mCurrentRawState.when);
  3423. }
  3424. if (count != 0) {
  3425. mRawStatesPending.removeItemsAt(0, count);
  3426. }
  3427. if (mExternalStylusDataPending) {
  3428. if (timeout) {
  3429. nsecs_t when = mExternalStylusFusionTimeout - STYLUS_DATA_LATENCY;
  3430. clearStylusDataPendingFlags();
  3431. mCurrentRawState.copyFrom(mLastRawState);
  3432. #if DEBUG_STYLUS_FUSION
  3433. ALOGD("Timeout expired, synthesizing event with new stylus data");
  3434. #endif
  3435. cookAndDispatch(when);
  3436. } else if (mExternalStylusFusionTimeout == LLONG_MAX) {
  3437. mExternalStylusFusionTimeout = mExternalStylusState.when + TOUCH_DATA_TIMEOUT;
  3438. getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
  3439. }
  3440. }
  3441. }
  3442. void TouchInputMapper::cookAndDispatch(nsecs_t when) {
  3443. // Always start with a clean state.
  3444. mCurrentCookedState.clear();
  3445. // Apply stylus buttons to current raw state.
  3446. applyExternalStylusButtonState(when);
  3447. // Handle policy on initial down or hover events.
  3448. bool initialDown = mLastRawState.rawPointerData.pointerCount == 0
  3449. && mCurrentRawState.rawPointerData.pointerCount != 0;
  3450. uint32_t policyFlags = 0;
  3451. bool buttonsPressed = mCurrentRawState.buttonState & ~mLastRawState.buttonState;
  3452. if (initialDown || buttonsPressed) {
  3453. // If this is a touch screen, hide the pointer on an initial down.
  3454. if (mDeviceMode == DEVICE_MODE_DIRECT) {
  3455. getContext()->fadePointer();
  3456. }
  3457. if (mParameters.wake) {
  3458. policyFlags |= POLICY_FLAG_WAKE;
  3459. }
  3460. }
  3461. // Consume raw off-screen touches before cooking pointer data.
  3462. // If touches are consumed, subsequent code will not receive any pointer data.
  3463. if (consumeRawTouches(when, policyFlags)) {
  3464. mCurrentRawState.rawPointerData.clear();
  3465. }
  3466. // Cook pointer data. This call populates the mCurrentCookedState.cookedPointerData structure
  3467. // with cooked pointer data that has the same ids and indices as the raw data.
  3468. // The following code can use either the raw or cooked data, as needed.
  3469. cookPointerData();
  3470. // Apply stylus pressure to current cooked state.
  3471. applyExternalStylusTouchState(when);
  3472. // Synthesize key down from raw buttons if needed.
  3473. synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
  3474. policyFlags, mLastCookedState.buttonState, mCurrentCookedState.buttonState);
  3475. // Dispatch the touches either directly or by translation through a pointer on screen.
  3476. if (mDeviceMode == DEVICE_MODE_POINTER) {
  3477. for (BitSet32 idBits(mCurrentRawState.rawPointerData.touchingIdBits);
  3478. !idBits.isEmpty(); ) {
  3479. uint32_t id = idBits.clearFirstMarkedBit();
  3480. const RawPointerData::Pointer& pointer =
  3481. mCurrentRawState.rawPointerData.pointerForId(id);
  3482. if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
  3483. || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
  3484. mCurrentCookedState.stylusIdBits.markBit(id);
  3485. } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
  3486. || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  3487. mCurrentCookedState.fingerIdBits.markBit(id);
  3488. } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
  3489. mCurrentCookedState.mouseIdBits.markBit(id);
  3490. }
  3491. }
  3492. for (BitSet32 idBits(mCurrentRawState.rawPointerData.hoveringIdBits);
  3493. !idBits.isEmpty(); ) {
  3494. uint32_t id = idBits.clearFirstMarkedBit();
  3495. const RawPointerData::Pointer& pointer =
  3496. mCurrentRawState.rawPointerData.pointerForId(id);
  3497. if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
  3498. || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
  3499. mCurrentCookedState.stylusIdBits.markBit(id);
  3500. }
  3501. }
  3502. // Stylus takes precedence over all tools, then mouse, then finger.
  3503. PointerUsage pointerUsage = mPointerUsage;
  3504. if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
  3505. mCurrentCookedState.mouseIdBits.clear();
  3506. mCurrentCookedState.fingerIdBits.clear();
  3507. pointerUsage = POINTER_USAGE_STYLUS;
  3508. } else if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
  3509. mCurrentCookedState.fingerIdBits.clear();
  3510. pointerUsage = POINTER_USAGE_MOUSE;
  3511. } else if (!mCurrentCookedState.fingerIdBits.isEmpty() ||
  3512. isPointerDown(mCurrentRawState.buttonState)) {
  3513. pointerUsage = POINTER_USAGE_GESTURES;
  3514. }
  3515. dispatchPointerUsage(when, policyFlags, pointerUsage);
  3516. } else {
  3517. if (mDeviceMode == DEVICE_MODE_DIRECT
  3518. && mConfig.showTouches && mPointerController != NULL) {
  3519. mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
  3520. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  3521. mPointerController->setButtonState(mCurrentRawState.buttonState);
  3522. mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
  3523. mCurrentCookedState.cookedPointerData.idToIndex,
  3524. mCurrentCookedState.cookedPointerData.touchingIdBits);
  3525. }
  3526. if (!mCurrentMotionAborted) {
  3527. dispatchButtonRelease(when, policyFlags);
  3528. dispatchHoverExit(when, policyFlags);
  3529. dispatchTouches(when, policyFlags);
  3530. dispatchHoverEnterAndMove(when, policyFlags);
  3531. dispatchButtonPress(when, policyFlags);
  3532. }
  3533. if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
  3534. mCurrentMotionAborted = false;
  3535. }
  3536. }
  3537. // Synthesize key up from raw buttons if needed.
  3538. synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
  3539. policyFlags, mLastCookedState.buttonState, mCurrentCookedState.buttonState);
  3540. // Clear some transient state.
  3541. mCurrentRawState.rawVScroll = 0;
  3542. mCurrentRawState.rawHScroll = 0;
  3543. // Copy current touch to last touch in preparation for the next cycle.
  3544. mLastRawState.copyFrom(mCurrentRawState);
  3545. mLastCookedState.copyFrom(mCurrentCookedState);
  3546. }
  3547. void TouchInputMapper::applyExternalStylusButtonState(nsecs_t when) {
  3548. if (mDeviceMode == DEVICE_MODE_DIRECT && hasExternalStylus() && mExternalStylusId != -1) {
  3549. mCurrentRawState.buttonState |= mExternalStylusState.buttons;
  3550. }
  3551. }
  3552. void TouchInputMapper::applyExternalStylusTouchState(nsecs_t when) {
  3553. CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
  3554. const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
  3555. if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
  3556. float pressure = mExternalStylusState.pressure;
  3557. if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
  3558. const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
  3559. pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
  3560. }
  3561. PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
  3562. coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
  3563. PointerProperties& properties =
  3564. currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
  3565. if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  3566. properties.toolType = mExternalStylusState.toolType;
  3567. }
  3568. }
  3569. }
  3570. bool TouchInputMapper::assignExternalStylusId(const RawState& state, bool timeout) {
  3571. if (mDeviceMode != DEVICE_MODE_DIRECT || !hasExternalStylus()) {
  3572. return false;
  3573. }
  3574. const bool initialDown = mLastRawState.rawPointerData.pointerCount == 0
  3575. && state.rawPointerData.pointerCount != 0;
  3576. if (initialDown) {
  3577. if (mExternalStylusState.pressure != 0.0f) {
  3578. #if DEBUG_STYLUS_FUSION
  3579. ALOGD("Have both stylus and touch data, beginning fusion");
  3580. #endif
  3581. mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
  3582. } else if (timeout) {
  3583. #if DEBUG_STYLUS_FUSION
  3584. ALOGD("Timeout expired, assuming touch is not a stylus.");
  3585. #endif
  3586. resetExternalStylus();
  3587. } else {
  3588. if (mExternalStylusFusionTimeout == LLONG_MAX) {
  3589. mExternalStylusFusionTimeout = state.when + EXTERNAL_STYLUS_DATA_TIMEOUT;
  3590. }
  3591. #if DEBUG_STYLUS_FUSION
  3592. ALOGD("No stylus data but stylus is connected, requesting timeout "
  3593. "(%" PRId64 "ms)", mExternalStylusFusionTimeout);
  3594. #endif
  3595. getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
  3596. return true;
  3597. }
  3598. }
  3599. // Check if the stylus pointer has gone up.
  3600. if (mExternalStylusId != -1 &&
  3601. !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
  3602. #if DEBUG_STYLUS_FUSION
  3603. ALOGD("Stylus pointer is going up");
  3604. #endif
  3605. mExternalStylusId = -1;
  3606. }
  3607. return false;
  3608. }
  3609. void TouchInputMapper::timeoutExpired(nsecs_t when) {
  3610. if (mDeviceMode == DEVICE_MODE_POINTER) {
  3611. if (mPointerUsage == POINTER_USAGE_GESTURES) {
  3612. dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
  3613. }
  3614. } else if (mDeviceMode == DEVICE_MODE_DIRECT) {
  3615. if (mExternalStylusFusionTimeout < when) {
  3616. processRawTouches(true /*timeout*/);
  3617. } else if (mExternalStylusFusionTimeout != LLONG_MAX) {
  3618. getContext()->requestTimeoutAtTime(mExternalStylusFusionTimeout);
  3619. }
  3620. }
  3621. }
  3622. void TouchInputMapper::updateExternalStylusState(const StylusState& state) {
  3623. mExternalStylusState.copyFrom(state);
  3624. if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX) {
  3625. // We're either in the middle of a fused stream of data or we're waiting on data before
  3626. // dispatching the initial down, so go ahead and dispatch now that we have fresh stylus
  3627. // data.
  3628. mExternalStylusDataPending = true;
  3629. processRawTouches(false /*timeout*/);
  3630. }
  3631. }
  3632. bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
  3633. // Check for release of a virtual key.
  3634. if (mCurrentVirtualKey.down) {
  3635. if (mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
  3636. // Pointer went up while virtual key was down.
  3637. mCurrentVirtualKey.down = false;
  3638. if (!mCurrentVirtualKey.ignored) {
  3639. #if DEBUG_VIRTUAL_KEYS
  3640. ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
  3641. mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
  3642. #endif
  3643. dispatchVirtualKey(when, policyFlags,
  3644. AKEY_EVENT_ACTION_UP,
  3645. AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
  3646. }
  3647. return true;
  3648. }
  3649. if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
  3650. uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
  3651. const RawPointerData::Pointer& pointer =
  3652. mCurrentRawState.rawPointerData.pointerForId(id);
  3653. const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
  3654. if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
  3655. // Pointer is still within the space of the virtual key.
  3656. return true;
  3657. }
  3658. }
  3659. // Pointer left virtual key area or another pointer also went down.
  3660. // Send key cancellation but do not consume the touch yet.
  3661. // This is useful when the user swipes through from the virtual key area
  3662. // into the main display surface.
  3663. mCurrentVirtualKey.down = false;
  3664. if (!mCurrentVirtualKey.ignored) {
  3665. #if DEBUG_VIRTUAL_KEYS
  3666. ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
  3667. mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
  3668. #endif
  3669. dispatchVirtualKey(when, policyFlags,
  3670. AKEY_EVENT_ACTION_UP,
  3671. AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
  3672. | AKEY_EVENT_FLAG_CANCELED);
  3673. }
  3674. }
  3675. if (mLastRawState.rawPointerData.touchingIdBits.isEmpty()
  3676. && !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
  3677. // Pointer just went down. Check for virtual key press or off-screen touches.
  3678. uint32_t id = mCurrentRawState.rawPointerData.touchingIdBits.firstMarkedBit();
  3679. const RawPointerData::Pointer& pointer = mCurrentRawState.rawPointerData.pointerForId(id);
  3680. if (!isPointInsideSurface(pointer.x, pointer.y)) {
  3681. // If exactly one pointer went down, check for virtual key hit.
  3682. // Otherwise we will drop the entire stroke.
  3683. if (mCurrentRawState.rawPointerData.touchingIdBits.count() == 1) {
  3684. const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
  3685. if (virtualKey) {
  3686. mCurrentVirtualKey.down = true;
  3687. mCurrentVirtualKey.downTime = when;
  3688. mCurrentVirtualKey.keyCode = virtualKey->keyCode;
  3689. mCurrentVirtualKey.scanCode = virtualKey->scanCode;
  3690. mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
  3691. when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
  3692. if (!mCurrentVirtualKey.ignored) {
  3693. #if DEBUG_VIRTUAL_KEYS
  3694. ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
  3695. mCurrentVirtualKey.keyCode,
  3696. mCurrentVirtualKey.scanCode);
  3697. #endif
  3698. dispatchVirtualKey(when, policyFlags,
  3699. AKEY_EVENT_ACTION_DOWN,
  3700. AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
  3701. }
  3702. }
  3703. }
  3704. return true;
  3705. }
  3706. }
  3707. // Disable all virtual key touches that happen within a short time interval of the
  3708. // most recent touch within the screen area. The idea is to filter out stray
  3709. // virtual key presses when interacting with the touch screen.
  3710. //
  3711. // Problems we're trying to solve:
  3712. //
  3713. // 1. While scrolling a list or dragging the window shade, the user swipes down into a
  3714. // virtual key area that is implemented by a separate touch panel and accidentally
  3715. // triggers a virtual key.
  3716. //
  3717. // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
  3718. // area and accidentally triggers a virtual key. This often happens when virtual keys
  3719. // are layed out below the screen near to where the on screen keyboard's space bar
  3720. // is displayed.
  3721. if (mConfig.virtualKeyQuietTime > 0 &&
  3722. !mCurrentRawState.rawPointerData.touchingIdBits.isEmpty()) {
  3723. mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
  3724. }
  3725. return false;
  3726. }
  3727. void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
  3728. int32_t keyEventAction, int32_t keyEventFlags) {
  3729. int32_t keyCode = mCurrentVirtualKey.keyCode;
  3730. int32_t scanCode = mCurrentVirtualKey.scanCode;
  3731. nsecs_t downTime = mCurrentVirtualKey.downTime;
  3732. int32_t metaState = mContext->getGlobalMetaState();
  3733. policyFlags |= POLICY_FLAG_VIRTUAL;
  3734. NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
  3735. keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
  3736. getListener()->notifyKey(&args);
  3737. }
  3738. void TouchInputMapper::abortTouches(nsecs_t when, uint32_t policyFlags) {
  3739. BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
  3740. if (!currentIdBits.isEmpty()) {
  3741. int32_t metaState = getContext()->getGlobalMetaState();
  3742. int32_t buttonState = mCurrentCookedState.buttonState;
  3743. dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_CANCEL, 0, 0,
  3744. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  3745. mCurrentCookedState.cookedPointerData.pointerProperties,
  3746. mCurrentCookedState.cookedPointerData.pointerCoords,
  3747. mCurrentCookedState.cookedPointerData.idToIndex,
  3748. currentIdBits, -1,
  3749. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3750. mCurrentMotionAborted = true;
  3751. }
  3752. }
  3753. void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
  3754. BitSet32 currentIdBits = mCurrentCookedState.cookedPointerData.touchingIdBits;
  3755. BitSet32 lastIdBits = mLastCookedState.cookedPointerData.touchingIdBits;
  3756. int32_t metaState = getContext()->getGlobalMetaState();
  3757. int32_t buttonState = mCurrentCookedState.buttonState;
  3758. if (currentIdBits == lastIdBits) {
  3759. if (!currentIdBits.isEmpty()) {
  3760. // No pointer id changes so this is a move event.
  3761. // The listener takes care of batching moves so we don't have to deal with that here.
  3762. dispatchMotion(when, policyFlags, mSource,
  3763. AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState,
  3764. AMOTION_EVENT_EDGE_FLAG_NONE,
  3765. mCurrentCookedState.cookedPointerData.pointerProperties,
  3766. mCurrentCookedState.cookedPointerData.pointerCoords,
  3767. mCurrentCookedState.cookedPointerData.idToIndex,
  3768. currentIdBits, -1,
  3769. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3770. }
  3771. } else {
  3772. // There may be pointers going up and pointers going down and pointers moving
  3773. // all at the same time.
  3774. BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
  3775. BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
  3776. BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
  3777. BitSet32 dispatchedIdBits(lastIdBits.value);
  3778. // Update last coordinates of pointers that have moved so that we observe the new
  3779. // pointer positions at the same time as other pointers that have just gone up.
  3780. bool moveNeeded = updateMovedPointers(
  3781. mCurrentCookedState.cookedPointerData.pointerProperties,
  3782. mCurrentCookedState.cookedPointerData.pointerCoords,
  3783. mCurrentCookedState.cookedPointerData.idToIndex,
  3784. mLastCookedState.cookedPointerData.pointerProperties,
  3785. mLastCookedState.cookedPointerData.pointerCoords,
  3786. mLastCookedState.cookedPointerData.idToIndex,
  3787. moveIdBits);
  3788. if (buttonState != mLastCookedState.buttonState) {
  3789. moveNeeded = true;
  3790. }
  3791. // Dispatch pointer up events.
  3792. while (!upIdBits.isEmpty()) {
  3793. uint32_t upId = upIdBits.clearFirstMarkedBit();
  3794. dispatchMotion(when, policyFlags, mSource,
  3795. AMOTION_EVENT_ACTION_POINTER_UP, 0, 0, metaState, buttonState, 0,
  3796. mLastCookedState.cookedPointerData.pointerProperties,
  3797. mLastCookedState.cookedPointerData.pointerCoords,
  3798. mLastCookedState.cookedPointerData.idToIndex,
  3799. dispatchedIdBits, upId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3800. dispatchedIdBits.clearBit(upId);
  3801. }
  3802. // Dispatch move events if any of the remaining pointers moved from their old locations.
  3803. // Although applications receive new locations as part of individual pointer up
  3804. // events, they do not generally handle them except when presented in a move event.
  3805. if (moveNeeded && !moveIdBits.isEmpty()) {
  3806. ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
  3807. dispatchMotion(when, policyFlags, mSource,
  3808. AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, 0,
  3809. mCurrentCookedState.cookedPointerData.pointerProperties,
  3810. mCurrentCookedState.cookedPointerData.pointerCoords,
  3811. mCurrentCookedState.cookedPointerData.idToIndex,
  3812. dispatchedIdBits, -1, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3813. }
  3814. // Dispatch pointer down events using the new pointer locations.
  3815. while (!downIdBits.isEmpty()) {
  3816. uint32_t downId = downIdBits.clearFirstMarkedBit();
  3817. dispatchedIdBits.markBit(downId);
  3818. if (dispatchedIdBits.count() == 1) {
  3819. // First pointer is going down. Set down time.
  3820. mDownTime = when;
  3821. }
  3822. dispatchMotion(when, policyFlags, mSource,
  3823. AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState, 0,
  3824. mCurrentCookedState.cookedPointerData.pointerProperties,
  3825. mCurrentCookedState.cookedPointerData.pointerCoords,
  3826. mCurrentCookedState.cookedPointerData.idToIndex,
  3827. dispatchedIdBits, downId, mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3828. }
  3829. }
  3830. }
  3831. void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
  3832. if (mSentHoverEnter &&
  3833. (mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()
  3834. || !mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty())) {
  3835. int32_t metaState = getContext()->getGlobalMetaState();
  3836. dispatchMotion(when, policyFlags, mSource,
  3837. AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState, mLastCookedState.buttonState, 0,
  3838. mLastCookedState.cookedPointerData.pointerProperties,
  3839. mLastCookedState.cookedPointerData.pointerCoords,
  3840. mLastCookedState.cookedPointerData.idToIndex,
  3841. mLastCookedState.cookedPointerData.hoveringIdBits, -1,
  3842. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3843. mSentHoverEnter = false;
  3844. }
  3845. }
  3846. void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
  3847. if (mCurrentCookedState.cookedPointerData.touchingIdBits.isEmpty()
  3848. && !mCurrentCookedState.cookedPointerData.hoveringIdBits.isEmpty()) {
  3849. int32_t metaState = getContext()->getGlobalMetaState();
  3850. if (!mSentHoverEnter) {
  3851. dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_HOVER_ENTER,
  3852. 0, 0, metaState, mCurrentRawState.buttonState, 0,
  3853. mCurrentCookedState.cookedPointerData.pointerProperties,
  3854. mCurrentCookedState.cookedPointerData.pointerCoords,
  3855. mCurrentCookedState.cookedPointerData.idToIndex,
  3856. mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
  3857. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3858. mSentHoverEnter = true;
  3859. }
  3860. dispatchMotion(when, policyFlags, mSource,
  3861. AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState,
  3862. mCurrentRawState.buttonState, 0,
  3863. mCurrentCookedState.cookedPointerData.pointerProperties,
  3864. mCurrentCookedState.cookedPointerData.pointerCoords,
  3865. mCurrentCookedState.cookedPointerData.idToIndex,
  3866. mCurrentCookedState.cookedPointerData.hoveringIdBits, -1,
  3867. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3868. }
  3869. }
  3870. void TouchInputMapper::dispatchButtonRelease(nsecs_t when, uint32_t policyFlags) {
  3871. BitSet32 releasedButtons(mLastCookedState.buttonState & ~mCurrentCookedState.buttonState);
  3872. const BitSet32& idBits = findActiveIdBits(mLastCookedState.cookedPointerData);
  3873. const int32_t metaState = getContext()->getGlobalMetaState();
  3874. int32_t buttonState = mLastCookedState.buttonState;
  3875. while (!releasedButtons.isEmpty()) {
  3876. int32_t actionButton = BitSet32::valueForBit(releasedButtons.clearFirstMarkedBit());
  3877. buttonState &= ~actionButton;
  3878. dispatchMotion(when, policyFlags, mSource,
  3879. AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton,
  3880. 0, metaState, buttonState, 0,
  3881. mCurrentCookedState.cookedPointerData.pointerProperties,
  3882. mCurrentCookedState.cookedPointerData.pointerCoords,
  3883. mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
  3884. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3885. }
  3886. }
  3887. void TouchInputMapper::dispatchButtonPress(nsecs_t when, uint32_t policyFlags) {
  3888. BitSet32 pressedButtons(mCurrentCookedState.buttonState & ~mLastCookedState.buttonState);
  3889. const BitSet32& idBits = findActiveIdBits(mCurrentCookedState.cookedPointerData);
  3890. const int32_t metaState = getContext()->getGlobalMetaState();
  3891. int32_t buttonState = mLastCookedState.buttonState;
  3892. while (!pressedButtons.isEmpty()) {
  3893. int32_t actionButton = BitSet32::valueForBit(pressedButtons.clearFirstMarkedBit());
  3894. buttonState |= actionButton;
  3895. dispatchMotion(when, policyFlags, mSource, AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton,
  3896. 0, metaState, buttonState, 0,
  3897. mCurrentCookedState.cookedPointerData.pointerProperties,
  3898. mCurrentCookedState.cookedPointerData.pointerCoords,
  3899. mCurrentCookedState.cookedPointerData.idToIndex, idBits, -1,
  3900. mOrientedXPrecision, mOrientedYPrecision, mDownTime);
  3901. }
  3902. }
  3903. const BitSet32& TouchInputMapper::findActiveIdBits(const CookedPointerData& cookedPointerData) {
  3904. if (!cookedPointerData.touchingIdBits.isEmpty()) {
  3905. return cookedPointerData.touchingIdBits;
  3906. }
  3907. return cookedPointerData.hoveringIdBits;
  3908. }
  3909. void TouchInputMapper::cookPointerData() {
  3910. uint32_t currentPointerCount = mCurrentRawState.rawPointerData.pointerCount;
  3911. mCurrentCookedState.cookedPointerData.clear();
  3912. mCurrentCookedState.cookedPointerData.pointerCount = currentPointerCount;
  3913. mCurrentCookedState.cookedPointerData.hoveringIdBits =
  3914. mCurrentRawState.rawPointerData.hoveringIdBits;
  3915. mCurrentCookedState.cookedPointerData.touchingIdBits =
  3916. mCurrentRawState.rawPointerData.touchingIdBits;
  3917. if (mCurrentCookedState.cookedPointerData.pointerCount == 0) {
  3918. mCurrentCookedState.buttonState = 0;
  3919. } else {
  3920. mCurrentCookedState.buttonState = mCurrentRawState.buttonState;
  3921. }
  3922. // Walk through the the active pointers and map device coordinates onto
  3923. // surface coordinates and adjust for display orientation.
  3924. for (uint32_t i = 0; i < currentPointerCount; i++) {
  3925. const RawPointerData::Pointer& in = mCurrentRawState.rawPointerData.pointers[i];
  3926. // Size
  3927. float touchMajor, touchMinor, toolMajor, toolMinor, size;
  3928. switch (mCalibration.sizeCalibration) {
  3929. case Calibration::SIZE_CALIBRATION_GEOMETRIC:
  3930. case Calibration::SIZE_CALIBRATION_DIAMETER:
  3931. case Calibration::SIZE_CALIBRATION_BOX:
  3932. case Calibration::SIZE_CALIBRATION_AREA:
  3933. if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
  3934. touchMajor = in.touchMajor;
  3935. touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
  3936. toolMajor = in.toolMajor;
  3937. toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
  3938. size = mRawPointerAxes.touchMinor.valid
  3939. ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
  3940. } else if (mRawPointerAxes.touchMajor.valid) {
  3941. toolMajor = touchMajor = in.touchMajor;
  3942. toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
  3943. ? in.touchMinor : in.touchMajor;
  3944. size = mRawPointerAxes.touchMinor.valid
  3945. ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
  3946. } else if (mRawPointerAxes.toolMajor.valid) {
  3947. touchMajor = toolMajor = in.toolMajor;
  3948. touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
  3949. ? in.toolMinor : in.toolMajor;
  3950. size = mRawPointerAxes.toolMinor.valid
  3951. ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
  3952. } else {
  3953. ALOG_ASSERT(false, "No touch or tool axes. "
  3954. "Size calibration should have been resolved to NONE.");
  3955. touchMajor = 0;
  3956. touchMinor = 0;
  3957. toolMajor = 0;
  3958. toolMinor = 0;
  3959. size = 0;
  3960. }
  3961. if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
  3962. uint32_t touchingCount =
  3963. mCurrentRawState.rawPointerData.touchingIdBits.count();
  3964. if (touchingCount > 1) {
  3965. touchMajor /= touchingCount;
  3966. touchMinor /= touchingCount;
  3967. toolMajor /= touchingCount;
  3968. toolMinor /= touchingCount;
  3969. size /= touchingCount;
  3970. }
  3971. }
  3972. if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
  3973. touchMajor *= mGeometricScale;
  3974. touchMinor *= mGeometricScale;
  3975. toolMajor *= mGeometricScale;
  3976. toolMinor *= mGeometricScale;
  3977. } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
  3978. touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
  3979. touchMinor = touchMajor;
  3980. toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
  3981. toolMinor = toolMajor;
  3982. } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
  3983. touchMinor = touchMajor;
  3984. toolMinor = toolMajor;
  3985. }
  3986. mCalibration.applySizeScaleAndBias(&touchMajor);
  3987. mCalibration.applySizeScaleAndBias(&touchMinor);
  3988. mCalibration.applySizeScaleAndBias(&toolMajor);
  3989. mCalibration.applySizeScaleAndBias(&toolMinor);
  3990. size *= mSizeScale;
  3991. break;
  3992. default:
  3993. touchMajor = 0;
  3994. touchMinor = 0;
  3995. toolMajor = 0;
  3996. toolMinor = 0;
  3997. size = 0;
  3998. break;
  3999. }
  4000. // Pressure
  4001. float pressure;
  4002. switch (mCalibration.pressureCalibration) {
  4003. case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
  4004. case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
  4005. pressure = in.pressure * mPressureScale;
  4006. break;
  4007. default:
  4008. pressure = in.isHovering ? 0 : 1;
  4009. break;
  4010. }
  4011. // Tilt and Orientation
  4012. float tilt;
  4013. float orientation;
  4014. if (mHaveTilt) {
  4015. float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
  4016. float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
  4017. orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
  4018. tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
  4019. } else {
  4020. tilt = 0;
  4021. switch (mCalibration.orientationCalibration) {
  4022. case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
  4023. orientation = in.orientation * mOrientationScale;
  4024. break;
  4025. case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
  4026. int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
  4027. int32_t c2 = signExtendNybble(in.orientation & 0x0f);
  4028. if (c1 != 0 || c2 != 0) {
  4029. orientation = atan2f(c1, c2) * 0.5f;
  4030. float confidence = hypotf(c1, c2);
  4031. float scale = 1.0f + confidence / 16.0f;
  4032. touchMajor *= scale;
  4033. touchMinor /= scale;
  4034. toolMajor *= scale;
  4035. toolMinor /= scale;
  4036. } else {
  4037. orientation = 0;
  4038. }
  4039. break;
  4040. }
  4041. default:
  4042. orientation = 0;
  4043. }
  4044. }
  4045. // Distance
  4046. float distance;
  4047. switch (mCalibration.distanceCalibration) {
  4048. case Calibration::DISTANCE_CALIBRATION_SCALED:
  4049. distance = in.distance * mDistanceScale;
  4050. break;
  4051. default:
  4052. distance = 0;
  4053. }
  4054. // Coverage
  4055. int32_t rawLeft, rawTop, rawRight, rawBottom;
  4056. switch (mCalibration.coverageCalibration) {
  4057. case Calibration::COVERAGE_CALIBRATION_BOX:
  4058. rawLeft = (in.toolMinor & 0xffff0000) >> 16;
  4059. rawRight = in.toolMinor & 0x0000ffff;
  4060. rawBottom = in.toolMajor & 0x0000ffff;
  4061. rawTop = (in.toolMajor & 0xffff0000) >> 16;
  4062. break;
  4063. default:
  4064. rawLeft = rawTop = rawRight = rawBottom = 0;
  4065. break;
  4066. }
  4067. // Adjust X,Y coords for device calibration
  4068. // TODO: Adjust coverage coords?
  4069. float xTransformed = in.x, yTransformed = in.y;
  4070. mAffineTransform.applyTo(xTransformed, yTransformed);
  4071. // Adjust X, Y, and coverage coords for surface orientation.
  4072. float x, y;
  4073. float left, top, right, bottom;
  4074. switch (mSurfaceOrientation) {
  4075. case DISPLAY_ORIENTATION_90:
  4076. x = float(yTransformed - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4077. y = float(mRawPointerAxes.x.maxValue - xTransformed) * mXScale + mXTranslate;
  4078. left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4079. right = float(rawBottom- mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4080. bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
  4081. top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
  4082. orientation -= M_PI_2;
  4083. if (mOrientedRanges.haveOrientation && orientation < mOrientedRanges.orientation.min) {
  4084. orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
  4085. }
  4086. break;
  4087. case DISPLAY_ORIENTATION_180:
  4088. x = float(mRawPointerAxes.x.maxValue - xTransformed) * mXScale + mXTranslate;
  4089. y = float(mRawPointerAxes.y.maxValue - yTransformed) * mYScale + mYTranslate;
  4090. left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
  4091. right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
  4092. bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
  4093. top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
  4094. orientation -= M_PI;
  4095. if (mOrientedRanges.haveOrientation && orientation < mOrientedRanges.orientation.min) {
  4096. orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
  4097. }
  4098. break;
  4099. case DISPLAY_ORIENTATION_270:
  4100. x = float(mRawPointerAxes.y.maxValue - yTransformed) * mYScale + mYTranslate;
  4101. y = float(xTransformed - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4102. left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
  4103. right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
  4104. bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4105. top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4106. orientation += M_PI_2;
  4107. if (mOrientedRanges.haveOrientation && orientation > mOrientedRanges.orientation.max) {
  4108. orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
  4109. }
  4110. break;
  4111. default:
  4112. x = float(xTransformed - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4113. y = float(yTransformed - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4114. left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4115. right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
  4116. bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4117. top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
  4118. break;
  4119. }
  4120. // Write output coords.
  4121. PointerCoords& out = mCurrentCookedState.cookedPointerData.pointerCoords[i];
  4122. out.clear();
  4123. out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
  4124. out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  4125. out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
  4126. out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
  4127. out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
  4128. out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
  4129. out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
  4130. out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
  4131. out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
  4132. if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
  4133. out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
  4134. out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
  4135. out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
  4136. out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
  4137. } else {
  4138. out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
  4139. out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
  4140. }
  4141. // Write output properties.
  4142. PointerProperties& properties =
  4143. mCurrentCookedState.cookedPointerData.pointerProperties[i];
  4144. uint32_t id = in.id;
  4145. properties.clear();
  4146. properties.id = id;
  4147. properties.toolType = in.toolType;
  4148. // Write id index.
  4149. mCurrentCookedState.cookedPointerData.idToIndex[id] = i;
  4150. }
  4151. }
  4152. void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
  4153. PointerUsage pointerUsage) {
  4154. if (pointerUsage != mPointerUsage) {
  4155. abortPointerUsage(when, policyFlags);
  4156. mPointerUsage = pointerUsage;
  4157. }
  4158. switch (mPointerUsage) {
  4159. case POINTER_USAGE_GESTURES:
  4160. dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
  4161. break;
  4162. case POINTER_USAGE_STYLUS:
  4163. dispatchPointerStylus(when, policyFlags);
  4164. break;
  4165. case POINTER_USAGE_MOUSE:
  4166. dispatchPointerMouse(when, policyFlags);
  4167. break;
  4168. default:
  4169. break;
  4170. }
  4171. }
  4172. void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
  4173. switch (mPointerUsage) {
  4174. case POINTER_USAGE_GESTURES:
  4175. abortPointerGestures(when, policyFlags);
  4176. break;
  4177. case POINTER_USAGE_STYLUS:
  4178. abortPointerStylus(when, policyFlags);
  4179. break;
  4180. case POINTER_USAGE_MOUSE:
  4181. abortPointerMouse(when, policyFlags);
  4182. break;
  4183. default:
  4184. break;
  4185. }
  4186. mPointerUsage = POINTER_USAGE_NONE;
  4187. }
  4188. void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
  4189. bool isTimeout) {
  4190. // Update current gesture coordinates.
  4191. bool cancelPreviousGesture, finishPreviousGesture;
  4192. bool sendEvents = preparePointerGestures(when,
  4193. &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
  4194. if (!sendEvents) {
  4195. return;
  4196. }
  4197. if (finishPreviousGesture) {
  4198. cancelPreviousGesture = false;
  4199. }
  4200. // Update the pointer presentation and spots.
  4201. if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
  4202. mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
  4203. if (finishPreviousGesture || cancelPreviousGesture) {
  4204. mPointerController->clearSpots();
  4205. }
  4206. mPointerController->setSpots(mPointerGesture.currentGestureCoords,
  4207. mPointerGesture.currentGestureIdToIndex,
  4208. mPointerGesture.currentGestureIdBits);
  4209. } else {
  4210. mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
  4211. }
  4212. // Show or hide the pointer if needed.
  4213. switch (mPointerGesture.currentGestureMode) {
  4214. case PointerGesture::NEUTRAL:
  4215. case PointerGesture::QUIET:
  4216. if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
  4217. && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
  4218. || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
  4219. // Remind the user of where the pointer is after finishing a gesture with spots.
  4220. unfadePointer(PointerControllerInterface::TRANSITION_GRADUAL);
  4221. }
  4222. break;
  4223. case PointerGesture::TAP:
  4224. case PointerGesture::TAP_DRAG:
  4225. case PointerGesture::BUTTON_CLICK_OR_DRAG:
  4226. case PointerGesture::HOVER:
  4227. case PointerGesture::PRESS:
  4228. // Unfade the pointer when the current gesture manipulates the
  4229. // area directly under the pointer.
  4230. mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
  4231. break;
  4232. case PointerGesture::SWIPE:
  4233. case PointerGesture::FREEFORM:
  4234. // Fade the pointer when the current gesture manipulates a different
  4235. // area and there are spots to guide the user experience.
  4236. if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
  4237. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  4238. } else {
  4239. mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
  4240. }
  4241. break;
  4242. }
  4243. // Send events!
  4244. int32_t metaState = getContext()->getGlobalMetaState();
  4245. int32_t buttonState = mCurrentCookedState.buttonState;
  4246. // Update last coordinates of pointers that have moved so that we observe the new
  4247. // pointer positions at the same time as other pointers that have just gone up.
  4248. bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
  4249. || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
  4250. || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
  4251. || mPointerGesture.currentGestureMode == PointerGesture::PRESS
  4252. || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
  4253. || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
  4254. bool moveNeeded = false;
  4255. if (down && !cancelPreviousGesture && !finishPreviousGesture
  4256. && !mPointerGesture.lastGestureIdBits.isEmpty()
  4257. && !mPointerGesture.currentGestureIdBits.isEmpty()) {
  4258. BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
  4259. & mPointerGesture.lastGestureIdBits.value);
  4260. moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
  4261. mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
  4262. mPointerGesture.lastGestureProperties,
  4263. mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
  4264. movedGestureIdBits);
  4265. if (buttonState != mLastCookedState.buttonState) {
  4266. moveNeeded = true;
  4267. }
  4268. }
  4269. // Send motion events for all pointers that went up or were canceled.
  4270. BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
  4271. if (!dispatchedGestureIdBits.isEmpty()) {
  4272. if (cancelPreviousGesture) {
  4273. dispatchMotion(when, policyFlags, mSource,
  4274. AMOTION_EVENT_ACTION_CANCEL, 0, 0, metaState, buttonState,
  4275. AMOTION_EVENT_EDGE_FLAG_NONE,
  4276. mPointerGesture.lastGestureProperties,
  4277. mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
  4278. dispatchedGestureIdBits, -1, 0,
  4279. 0, mPointerGesture.downTime);
  4280. dispatchedGestureIdBits.clear();
  4281. } else {
  4282. BitSet32 upGestureIdBits;
  4283. if (finishPreviousGesture) {
  4284. upGestureIdBits = dispatchedGestureIdBits;
  4285. } else {
  4286. upGestureIdBits.value = dispatchedGestureIdBits.value
  4287. & ~mPointerGesture.currentGestureIdBits.value;
  4288. }
  4289. while (!upGestureIdBits.isEmpty()) {
  4290. uint32_t id = upGestureIdBits.clearFirstMarkedBit();
  4291. dispatchMotion(when, policyFlags, mSource,
  4292. AMOTION_EVENT_ACTION_POINTER_UP, 0, 0,
  4293. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  4294. mPointerGesture.lastGestureProperties,
  4295. mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
  4296. dispatchedGestureIdBits, id,
  4297. 0, 0, mPointerGesture.downTime);
  4298. dispatchedGestureIdBits.clearBit(id);
  4299. }
  4300. }
  4301. }
  4302. // Send motion events for all pointers that moved.
  4303. if (moveNeeded) {
  4304. dispatchMotion(when, policyFlags, mSource,
  4305. AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState,
  4306. AMOTION_EVENT_EDGE_FLAG_NONE,
  4307. mPointerGesture.currentGestureProperties,
  4308. mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
  4309. dispatchedGestureIdBits, -1,
  4310. 0, 0, mPointerGesture.downTime);
  4311. }
  4312. // Send motion events for all pointers that went down.
  4313. if (down) {
  4314. BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
  4315. & ~dispatchedGestureIdBits.value);
  4316. while (!downGestureIdBits.isEmpty()) {
  4317. uint32_t id = downGestureIdBits.clearFirstMarkedBit();
  4318. dispatchedGestureIdBits.markBit(id);
  4319. if (dispatchedGestureIdBits.count() == 1) {
  4320. mPointerGesture.downTime = when;
  4321. }
  4322. dispatchMotion(when, policyFlags, mSource,
  4323. AMOTION_EVENT_ACTION_POINTER_DOWN, 0, 0, metaState, buttonState, 0,
  4324. mPointerGesture.currentGestureProperties,
  4325. mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
  4326. dispatchedGestureIdBits, id,
  4327. 0, 0, mPointerGesture.downTime);
  4328. }
  4329. }
  4330. // Send motion events for hover.
  4331. if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
  4332. dispatchMotion(when, policyFlags, mSource,
  4333. AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
  4334. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  4335. mPointerGesture.currentGestureProperties,
  4336. mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
  4337. mPointerGesture.currentGestureIdBits, -1,
  4338. 0, 0, mPointerGesture.downTime);
  4339. } else if (dispatchedGestureIdBits.isEmpty()
  4340. && !mPointerGesture.lastGestureIdBits.isEmpty()) {
  4341. // Synthesize a hover move event after all pointers go up to indicate that
  4342. // the pointer is hovering again even if the user is not currently touching
  4343. // the touch pad. This ensures that a view will receive a fresh hover enter
  4344. // event after a tap.
  4345. float x, y;
  4346. mPointerController->getPosition(&x, &y);
  4347. PointerProperties pointerProperties;
  4348. pointerProperties.clear();
  4349. pointerProperties.id = 0;
  4350. pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  4351. PointerCoords pointerCoords;
  4352. pointerCoords.clear();
  4353. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
  4354. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  4355. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  4356. AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0,
  4357. metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  4358. mViewport.displayId, 1, &pointerProperties, &pointerCoords,
  4359. 0, 0, mPointerGesture.downTime);
  4360. getListener()->notifyMotion(&args);
  4361. }
  4362. // Update state.
  4363. mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
  4364. if (!down) {
  4365. mPointerGesture.lastGestureIdBits.clear();
  4366. } else {
  4367. mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
  4368. for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
  4369. uint32_t id = idBits.clearFirstMarkedBit();
  4370. uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
  4371. mPointerGesture.lastGestureProperties[index].copyFrom(
  4372. mPointerGesture.currentGestureProperties[index]);
  4373. mPointerGesture.lastGestureCoords[index].copyFrom(
  4374. mPointerGesture.currentGestureCoords[index]);
  4375. mPointerGesture.lastGestureIdToIndex[id] = index;
  4376. }
  4377. }
  4378. }
  4379. void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
  4380. // Cancel previously dispatches pointers.
  4381. if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
  4382. int32_t metaState = getContext()->getGlobalMetaState();
  4383. int32_t buttonState = mCurrentRawState.buttonState;
  4384. dispatchMotion(when, policyFlags, mSource,
  4385. AMOTION_EVENT_ACTION_CANCEL, 0, 0, metaState, buttonState,
  4386. AMOTION_EVENT_EDGE_FLAG_NONE,
  4387. mPointerGesture.lastGestureProperties,
  4388. mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
  4389. mPointerGesture.lastGestureIdBits, -1,
  4390. 0, 0, mPointerGesture.downTime);
  4391. }
  4392. // Reset the current pointer gesture.
  4393. mPointerGesture.reset();
  4394. mPointerVelocityControl.reset();
  4395. // Remove any current spots.
  4396. if (mPointerController != NULL) {
  4397. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  4398. mPointerController->clearSpots();
  4399. }
  4400. }
  4401. bool TouchInputMapper::preparePointerGestures(nsecs_t when,
  4402. bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
  4403. *outCancelPreviousGesture = false;
  4404. *outFinishPreviousGesture = false;
  4405. // Handle TAP timeout.
  4406. if (isTimeout) {
  4407. #if DEBUG_GESTURES
  4408. ALOGD("Gestures: Processing timeout");
  4409. #endif
  4410. if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
  4411. if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
  4412. // The tap/drag timeout has not yet expired.
  4413. getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
  4414. + mConfig.pointerGestureTapDragInterval);
  4415. } else {
  4416. // The tap is finished.
  4417. #if DEBUG_GESTURES
  4418. ALOGD("Gestures: TAP finished");
  4419. #endif
  4420. *outFinishPreviousGesture = true;
  4421. mPointerGesture.activeGestureId = -1;
  4422. mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
  4423. mPointerGesture.currentGestureIdBits.clear();
  4424. mPointerVelocityControl.reset();
  4425. return true;
  4426. }
  4427. }
  4428. // We did not handle this timeout.
  4429. return false;
  4430. }
  4431. const uint32_t currentFingerCount = mCurrentCookedState.fingerIdBits.count();
  4432. const uint32_t lastFingerCount = mLastCookedState.fingerIdBits.count();
  4433. // Update the velocity tracker.
  4434. {
  4435. VelocityTracker::Position positions[MAX_POINTERS];
  4436. uint32_t count = 0;
  4437. for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty(); count++) {
  4438. uint32_t id = idBits.clearFirstMarkedBit();
  4439. const RawPointerData::Pointer& pointer =
  4440. mCurrentRawState.rawPointerData.pointerForId(id);
  4441. positions[count].x = pointer.x * mPointerXMovementScale;
  4442. positions[count].y = pointer.y * mPointerYMovementScale;
  4443. }
  4444. mPointerGesture.velocityTracker.addMovement(when,
  4445. mCurrentCookedState.fingerIdBits, positions);
  4446. }
  4447. // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
  4448. // to NEUTRAL, then we should not generate tap event.
  4449. if (mPointerGesture.lastGestureMode != PointerGesture::HOVER
  4450. && mPointerGesture.lastGestureMode != PointerGesture::TAP
  4451. && mPointerGesture.lastGestureMode != PointerGesture::TAP_DRAG) {
  4452. mPointerGesture.resetTap();
  4453. }
  4454. // Pick a new active touch id if needed.
  4455. // Choose an arbitrary pointer that just went down, if there is one.
  4456. // Otherwise choose an arbitrary remaining pointer.
  4457. // This guarantees we always have an active touch id when there is at least one pointer.
  4458. // We keep the same active touch id for as long as possible.
  4459. bool activeTouchChanged = false;
  4460. int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
  4461. int32_t activeTouchId = lastActiveTouchId;
  4462. if (activeTouchId < 0) {
  4463. if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
  4464. activeTouchChanged = true;
  4465. activeTouchId = mPointerGesture.activeTouchId =
  4466. mCurrentCookedState.fingerIdBits.firstMarkedBit();
  4467. mPointerGesture.firstTouchTime = when;
  4468. }
  4469. } else if (!mCurrentCookedState.fingerIdBits.hasBit(activeTouchId)) {
  4470. activeTouchChanged = true;
  4471. if (!mCurrentCookedState.fingerIdBits.isEmpty()) {
  4472. activeTouchId = mPointerGesture.activeTouchId =
  4473. mCurrentCookedState.fingerIdBits.firstMarkedBit();
  4474. } else {
  4475. activeTouchId = mPointerGesture.activeTouchId = -1;
  4476. }
  4477. }
  4478. // Determine whether we are in quiet time.
  4479. bool isQuietTime = false;
  4480. if (activeTouchId < 0) {
  4481. mPointerGesture.resetQuietTime();
  4482. } else {
  4483. isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
  4484. if (!isQuietTime) {
  4485. if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
  4486. || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
  4487. || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
  4488. && currentFingerCount < 2) {
  4489. // Enter quiet time when exiting swipe or freeform state.
  4490. // This is to prevent accidentally entering the hover state and flinging the
  4491. // pointer when finishing a swipe and there is still one pointer left onscreen.
  4492. isQuietTime = true;
  4493. } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
  4494. && currentFingerCount >= 2
  4495. && !isPointerDown(mCurrentRawState.buttonState)) {
  4496. // Enter quiet time when releasing the button and there are still two or more
  4497. // fingers down. This may indicate that one finger was used to press the button
  4498. // but it has not gone up yet.
  4499. isQuietTime = true;
  4500. }
  4501. if (isQuietTime) {
  4502. mPointerGesture.quietTime = when;
  4503. }
  4504. }
  4505. }
  4506. // Switch states based on button and pointer state.
  4507. if (isQuietTime) {
  4508. // Case 1: Quiet time. (QUIET)
  4509. #if DEBUG_GESTURES
  4510. ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
  4511. + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
  4512. #endif
  4513. if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
  4514. *outFinishPreviousGesture = true;
  4515. }
  4516. mPointerGesture.activeGestureId = -1;
  4517. mPointerGesture.currentGestureMode = PointerGesture::QUIET;
  4518. mPointerGesture.currentGestureIdBits.clear();
  4519. mPointerVelocityControl.reset();
  4520. } else if (isPointerDown(mCurrentRawState.buttonState)) {
  4521. // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
  4522. // The pointer follows the active touch point.
  4523. // Emit DOWN, MOVE, UP events at the pointer location.
  4524. //
  4525. // Only the active touch matters; other fingers are ignored. This policy helps
  4526. // to handle the case where the user places a second finger on the touch pad
  4527. // to apply the necessary force to depress an integrated button below the surface.
  4528. // We don't want the second finger to be delivered to applications.
  4529. //
  4530. // For this to work well, we need to make sure to track the pointer that is really
  4531. // active. If the user first puts one finger down to click then adds another
  4532. // finger to drag then the active pointer should switch to the finger that is
  4533. // being dragged.
  4534. #if DEBUG_GESTURES
  4535. ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
  4536. "currentFingerCount=%d", activeTouchId, currentFingerCount);
  4537. #endif
  4538. // Reset state when just starting.
  4539. if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
  4540. *outFinishPreviousGesture = true;
  4541. mPointerGesture.activeGestureId = 0;
  4542. }
  4543. // Switch pointers if needed.
  4544. // Find the fastest pointer and follow it.
  4545. if (activeTouchId >= 0 && currentFingerCount > 1) {
  4546. int32_t bestId = -1;
  4547. float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
  4548. for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty(); ) {
  4549. uint32_t id = idBits.clearFirstMarkedBit();
  4550. float vx, vy;
  4551. if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
  4552. float speed = hypotf(vx, vy);
  4553. if (speed > bestSpeed) {
  4554. bestId = id;
  4555. bestSpeed = speed;
  4556. }
  4557. }
  4558. }
  4559. if (bestId >= 0 && bestId != activeTouchId) {
  4560. mPointerGesture.activeTouchId = activeTouchId = bestId;
  4561. activeTouchChanged = true;
  4562. #if DEBUG_GESTURES
  4563. ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
  4564. "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
  4565. #endif
  4566. }
  4567. }
  4568. if (activeTouchId >= 0 && mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
  4569. const RawPointerData::Pointer& currentPointer =
  4570. mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
  4571. const RawPointerData::Pointer& lastPointer =
  4572. mLastRawState.rawPointerData.pointerForId(activeTouchId);
  4573. float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
  4574. float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
  4575. rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
  4576. mPointerVelocityControl.move(when, &deltaX, &deltaY);
  4577. // Move the pointer using a relative motion.
  4578. // When using spots, the click will occur at the position of the anchor
  4579. // spot and all other spots will move there.
  4580. mPointerController->move(deltaX, deltaY);
  4581. } else {
  4582. mPointerVelocityControl.reset();
  4583. }
  4584. float x, y;
  4585. mPointerController->getPosition(&x, &y);
  4586. mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
  4587. mPointerGesture.currentGestureIdBits.clear();
  4588. mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
  4589. mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
  4590. mPointerGesture.currentGestureProperties[0].clear();
  4591. mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
  4592. mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  4593. mPointerGesture.currentGestureCoords[0].clear();
  4594. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
  4595. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  4596. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
  4597. } else if (currentFingerCount == 0) {
  4598. // Case 3. No fingers down and button is not pressed. (NEUTRAL)
  4599. if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
  4600. *outFinishPreviousGesture = true;
  4601. }
  4602. // Watch for taps coming out of HOVER or TAP_DRAG mode.
  4603. // Checking for taps after TAP_DRAG allows us to detect double-taps.
  4604. bool tapped = false;
  4605. if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
  4606. || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
  4607. && lastFingerCount == 1) {
  4608. if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
  4609. float x, y;
  4610. mPointerController->getPosition(&x, &y);
  4611. if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
  4612. && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
  4613. #if DEBUG_GESTURES
  4614. ALOGD("Gestures: TAP");
  4615. #endif
  4616. mPointerGesture.tapUpTime = when;
  4617. getContext()->requestTimeoutAtTime(when
  4618. + mConfig.pointerGestureTapDragInterval);
  4619. mPointerGesture.activeGestureId = 0;
  4620. mPointerGesture.currentGestureMode = PointerGesture::TAP;
  4621. mPointerGesture.currentGestureIdBits.clear();
  4622. mPointerGesture.currentGestureIdBits.markBit(
  4623. mPointerGesture.activeGestureId);
  4624. mPointerGesture.currentGestureIdToIndex[
  4625. mPointerGesture.activeGestureId] = 0;
  4626. mPointerGesture.currentGestureProperties[0].clear();
  4627. mPointerGesture.currentGestureProperties[0].id =
  4628. mPointerGesture.activeGestureId;
  4629. mPointerGesture.currentGestureProperties[0].toolType =
  4630. AMOTION_EVENT_TOOL_TYPE_FINGER;
  4631. mPointerGesture.currentGestureCoords[0].clear();
  4632. mPointerGesture.currentGestureCoords[0].setAxisValue(
  4633. AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
  4634. mPointerGesture.currentGestureCoords[0].setAxisValue(
  4635. AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
  4636. mPointerGesture.currentGestureCoords[0].setAxisValue(
  4637. AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
  4638. tapped = true;
  4639. } else {
  4640. #if DEBUG_GESTURES
  4641. ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
  4642. x - mPointerGesture.tapX,
  4643. y - mPointerGesture.tapY);
  4644. #endif
  4645. }
  4646. } else {
  4647. #if DEBUG_GESTURES
  4648. if (mPointerGesture.tapDownTime != LLONG_MIN) {
  4649. ALOGD("Gestures: Not a TAP, %0.3fms since down",
  4650. (when - mPointerGesture.tapDownTime) * 0.000001f);
  4651. } else {
  4652. ALOGD("Gestures: Not a TAP, incompatible mode transitions");
  4653. }
  4654. #endif
  4655. }
  4656. }
  4657. mPointerVelocityControl.reset();
  4658. if (!tapped) {
  4659. #if DEBUG_GESTURES
  4660. ALOGD("Gestures: NEUTRAL");
  4661. #endif
  4662. mPointerGesture.activeGestureId = -1;
  4663. mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
  4664. mPointerGesture.currentGestureIdBits.clear();
  4665. }
  4666. } else if (currentFingerCount == 1) {
  4667. // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
  4668. // The pointer follows the active touch point.
  4669. // When in HOVER, emit HOVER_MOVE events at the pointer location.
  4670. // When in TAP_DRAG, emit MOVE events at the pointer location.
  4671. ALOG_ASSERT(activeTouchId >= 0);
  4672. mPointerGesture.currentGestureMode = PointerGesture::HOVER;
  4673. if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
  4674. if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
  4675. float x, y;
  4676. mPointerController->getPosition(&x, &y);
  4677. if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
  4678. && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
  4679. mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
  4680. } else {
  4681. #if DEBUG_GESTURES
  4682. ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
  4683. x - mPointerGesture.tapX,
  4684. y - mPointerGesture.tapY);
  4685. #endif
  4686. }
  4687. } else {
  4688. #if DEBUG_GESTURES
  4689. ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
  4690. (when - mPointerGesture.tapUpTime) * 0.000001f);
  4691. #endif
  4692. }
  4693. } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
  4694. mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
  4695. }
  4696. if (mLastCookedState.fingerIdBits.hasBit(activeTouchId)) {
  4697. const RawPointerData::Pointer& currentPointer =
  4698. mCurrentRawState.rawPointerData.pointerForId(activeTouchId);
  4699. const RawPointerData::Pointer& lastPointer =
  4700. mLastRawState.rawPointerData.pointerForId(activeTouchId);
  4701. float deltaX = (currentPointer.x - lastPointer.x)
  4702. * mPointerXMovementScale;
  4703. float deltaY = (currentPointer.y - lastPointer.y)
  4704. * mPointerYMovementScale;
  4705. rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
  4706. mPointerVelocityControl.move(when, &deltaX, &deltaY);
  4707. // Move the pointer using a relative motion.
  4708. // When using spots, the hover or drag will occur at the position of the anchor spot.
  4709. mPointerController->move(deltaX, deltaY);
  4710. } else {
  4711. mPointerVelocityControl.reset();
  4712. }
  4713. bool down;
  4714. if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
  4715. #if DEBUG_GESTURES
  4716. ALOGD("Gestures: TAP_DRAG");
  4717. #endif
  4718. down = true;
  4719. } else {
  4720. #if DEBUG_GESTURES
  4721. ALOGD("Gestures: HOVER");
  4722. #endif
  4723. if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
  4724. *outFinishPreviousGesture = true;
  4725. }
  4726. mPointerGesture.activeGestureId = 0;
  4727. down = false;
  4728. }
  4729. float x, y;
  4730. mPointerController->getPosition(&x, &y);
  4731. mPointerGesture.currentGestureIdBits.clear();
  4732. mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
  4733. mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
  4734. mPointerGesture.currentGestureProperties[0].clear();
  4735. mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
  4736. mPointerGesture.currentGestureProperties[0].toolType =
  4737. AMOTION_EVENT_TOOL_TYPE_FINGER;
  4738. mPointerGesture.currentGestureCoords[0].clear();
  4739. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
  4740. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  4741. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
  4742. down ? 1.0f : 0.0f);
  4743. if (lastFingerCount == 0 && currentFingerCount != 0) {
  4744. mPointerGesture.resetTap();
  4745. mPointerGesture.tapDownTime = when;
  4746. mPointerGesture.tapX = x;
  4747. mPointerGesture.tapY = y;
  4748. }
  4749. } else {
  4750. // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
  4751. // We need to provide feedback for each finger that goes down so we cannot wait
  4752. // for the fingers to move before deciding what to do.
  4753. //
  4754. // The ambiguous case is deciding what to do when there are two fingers down but they
  4755. // have not moved enough to determine whether they are part of a drag or part of a
  4756. // freeform gesture, or just a press or long-press at the pointer location.
  4757. //
  4758. // When there are two fingers we start with the PRESS hypothesis and we generate a
  4759. // down at the pointer location.
  4760. //
  4761. // When the two fingers move enough or when additional fingers are added, we make
  4762. // a decision to transition into SWIPE or FREEFORM mode accordingly.
  4763. ALOG_ASSERT(activeTouchId >= 0);
  4764. bool settled = when >= mPointerGesture.firstTouchTime
  4765. + mConfig.pointerGestureMultitouchSettleInterval;
  4766. if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
  4767. && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
  4768. && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
  4769. *outFinishPreviousGesture = true;
  4770. } else if (!settled && currentFingerCount > lastFingerCount) {
  4771. // Additional pointers have gone down but not yet settled.
  4772. // Reset the gesture.
  4773. #if DEBUG_GESTURES
  4774. ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
  4775. "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
  4776. + mConfig.pointerGestureMultitouchSettleInterval - when)
  4777. * 0.000001f);
  4778. #endif
  4779. *outCancelPreviousGesture = true;
  4780. } else {
  4781. // Continue previous gesture.
  4782. mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
  4783. }
  4784. if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
  4785. mPointerGesture.currentGestureMode = PointerGesture::PRESS;
  4786. mPointerGesture.activeGestureId = 0;
  4787. mPointerGesture.referenceIdBits.clear();
  4788. mPointerVelocityControl.reset();
  4789. // Use the centroid and pointer location as the reference points for the gesture.
  4790. #if DEBUG_GESTURES
  4791. ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
  4792. "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
  4793. + mConfig.pointerGestureMultitouchSettleInterval - when)
  4794. * 0.000001f);
  4795. #endif
  4796. mCurrentRawState.rawPointerData.getCentroidOfTouchingPointers(
  4797. &mPointerGesture.referenceTouchX,
  4798. &mPointerGesture.referenceTouchY);
  4799. mPointerController->getPosition(&mPointerGesture.referenceGestureX,
  4800. &mPointerGesture.referenceGestureY);
  4801. }
  4802. // Clear the reference deltas for fingers not yet included in the reference calculation.
  4803. for (BitSet32 idBits(mCurrentCookedState.fingerIdBits.value
  4804. & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
  4805. uint32_t id = idBits.clearFirstMarkedBit();
  4806. mPointerGesture.referenceDeltas[id].dx = 0;
  4807. mPointerGesture.referenceDeltas[id].dy = 0;
  4808. }
  4809. mPointerGesture.referenceIdBits = mCurrentCookedState.fingerIdBits;
  4810. // Add delta for all fingers and calculate a common movement delta.
  4811. float commonDeltaX = 0, commonDeltaY = 0;
  4812. BitSet32 commonIdBits(mLastCookedState.fingerIdBits.value
  4813. & mCurrentCookedState.fingerIdBits.value);
  4814. for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
  4815. bool first = (idBits == commonIdBits);
  4816. uint32_t id = idBits.clearFirstMarkedBit();
  4817. const RawPointerData::Pointer& cpd = mCurrentRawState.rawPointerData.pointerForId(id);
  4818. const RawPointerData::Pointer& lpd = mLastRawState.rawPointerData.pointerForId(id);
  4819. PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
  4820. delta.dx += cpd.x - lpd.x;
  4821. delta.dy += cpd.y - lpd.y;
  4822. if (first) {
  4823. commonDeltaX = delta.dx;
  4824. commonDeltaY = delta.dy;
  4825. } else {
  4826. commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
  4827. commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
  4828. }
  4829. }
  4830. // Consider transitions from PRESS to SWIPE or MULTITOUCH.
  4831. if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
  4832. float dist[MAX_POINTER_ID + 1];
  4833. int32_t distOverThreshold = 0;
  4834. for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
  4835. uint32_t id = idBits.clearFirstMarkedBit();
  4836. PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
  4837. dist[id] = hypotf(delta.dx * mPointerXZoomScale,
  4838. delta.dy * mPointerYZoomScale);
  4839. if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
  4840. distOverThreshold += 1;
  4841. }
  4842. }
  4843. // Only transition when at least two pointers have moved further than
  4844. // the minimum distance threshold.
  4845. if (distOverThreshold >= 2) {
  4846. if (currentFingerCount > 2) {
  4847. // There are more than two pointers, switch to FREEFORM.
  4848. #if DEBUG_GESTURES
  4849. ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
  4850. currentFingerCount);
  4851. #endif
  4852. *outCancelPreviousGesture = true;
  4853. mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
  4854. } else {
  4855. // There are exactly two pointers.
  4856. BitSet32 idBits(mCurrentCookedState.fingerIdBits);
  4857. uint32_t id1 = idBits.clearFirstMarkedBit();
  4858. uint32_t id2 = idBits.firstMarkedBit();
  4859. const RawPointerData::Pointer& p1 =
  4860. mCurrentRawState.rawPointerData.pointerForId(id1);
  4861. const RawPointerData::Pointer& p2 =
  4862. mCurrentRawState.rawPointerData.pointerForId(id2);
  4863. float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
  4864. if (mutualDistance > mPointerGestureMaxSwipeWidth) {
  4865. // There are two pointers but they are too far apart for a SWIPE,
  4866. // switch to FREEFORM.
  4867. #if DEBUG_GESTURES
  4868. ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
  4869. mutualDistance, mPointerGestureMaxSwipeWidth);
  4870. #endif
  4871. *outCancelPreviousGesture = true;
  4872. mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
  4873. } else {
  4874. // There are two pointers. Wait for both pointers to start moving
  4875. // before deciding whether this is a SWIPE or FREEFORM gesture.
  4876. float dist1 = dist[id1];
  4877. float dist2 = dist[id2];
  4878. if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
  4879. && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
  4880. // Calculate the dot product of the displacement vectors.
  4881. // When the vectors are oriented in approximately the same direction,
  4882. // the angle betweeen them is near zero and the cosine of the angle
  4883. // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
  4884. PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
  4885. PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
  4886. float dx1 = delta1.dx * mPointerXZoomScale;
  4887. float dy1 = delta1.dy * mPointerYZoomScale;
  4888. float dx2 = delta2.dx * mPointerXZoomScale;
  4889. float dy2 = delta2.dy * mPointerYZoomScale;
  4890. float dot = dx1 * dx2 + dy1 * dy2;
  4891. float cosine = dot / (dist1 * dist2); // denominator always > 0
  4892. if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
  4893. // Pointers are moving in the same direction. Switch to SWIPE.
  4894. #if DEBUG_GESTURES
  4895. ALOGD("Gestures: PRESS transitioned to SWIPE, "
  4896. "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
  4897. "cosine %0.3f >= %0.3f",
  4898. dist1, mConfig.pointerGestureMultitouchMinDistance,
  4899. dist2, mConfig.pointerGestureMultitouchMinDistance,
  4900. cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
  4901. #endif
  4902. mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
  4903. } else {
  4904. // Pointers are moving in different directions. Switch to FREEFORM.
  4905. #if DEBUG_GESTURES
  4906. ALOGD("Gestures: PRESS transitioned to FREEFORM, "
  4907. "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
  4908. "cosine %0.3f < %0.3f",
  4909. dist1, mConfig.pointerGestureMultitouchMinDistance,
  4910. dist2, mConfig.pointerGestureMultitouchMinDistance,
  4911. cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
  4912. #endif
  4913. *outCancelPreviousGesture = true;
  4914. mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
  4915. }
  4916. }
  4917. }
  4918. }
  4919. }
  4920. } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
  4921. // Switch from SWIPE to FREEFORM if additional pointers go down.
  4922. // Cancel previous gesture.
  4923. if (currentFingerCount > 2) {
  4924. #if DEBUG_GESTURES
  4925. ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
  4926. currentFingerCount);
  4927. #endif
  4928. *outCancelPreviousGesture = true;
  4929. mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
  4930. }
  4931. }
  4932. // Move the reference points based on the overall group motion of the fingers
  4933. // except in PRESS mode while waiting for a transition to occur.
  4934. if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
  4935. && (commonDeltaX || commonDeltaY)) {
  4936. for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
  4937. uint32_t id = idBits.clearFirstMarkedBit();
  4938. PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
  4939. delta.dx = 0;
  4940. delta.dy = 0;
  4941. }
  4942. mPointerGesture.referenceTouchX += commonDeltaX;
  4943. mPointerGesture.referenceTouchY += commonDeltaY;
  4944. commonDeltaX *= mPointerXMovementScale;
  4945. commonDeltaY *= mPointerYMovementScale;
  4946. rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
  4947. mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
  4948. mPointerGesture.referenceGestureX += commonDeltaX;
  4949. mPointerGesture.referenceGestureY += commonDeltaY;
  4950. }
  4951. // Report gestures.
  4952. if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
  4953. || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
  4954. // PRESS or SWIPE mode.
  4955. #if DEBUG_GESTURES
  4956. ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
  4957. "activeGestureId=%d, currentTouchPointerCount=%d",
  4958. activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
  4959. #endif
  4960. ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
  4961. mPointerGesture.currentGestureIdBits.clear();
  4962. mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
  4963. mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
  4964. mPointerGesture.currentGestureProperties[0].clear();
  4965. mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
  4966. mPointerGesture.currentGestureProperties[0].toolType =
  4967. AMOTION_EVENT_TOOL_TYPE_FINGER;
  4968. mPointerGesture.currentGestureCoords[0].clear();
  4969. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
  4970. mPointerGesture.referenceGestureX);
  4971. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
  4972. mPointerGesture.referenceGestureY);
  4973. mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
  4974. } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
  4975. // FREEFORM mode.
  4976. #if DEBUG_GESTURES
  4977. ALOGD("Gestures: FREEFORM activeTouchId=%d,"
  4978. "activeGestureId=%d, currentTouchPointerCount=%d",
  4979. activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
  4980. #endif
  4981. ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
  4982. mPointerGesture.currentGestureIdBits.clear();
  4983. BitSet32 mappedTouchIdBits;
  4984. BitSet32 usedGestureIdBits;
  4985. if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
  4986. // Initially, assign the active gesture id to the active touch point
  4987. // if there is one. No other touch id bits are mapped yet.
  4988. if (!*outCancelPreviousGesture) {
  4989. mappedTouchIdBits.markBit(activeTouchId);
  4990. usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
  4991. mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
  4992. mPointerGesture.activeGestureId;
  4993. } else {
  4994. mPointerGesture.activeGestureId = -1;
  4995. }
  4996. } else {
  4997. // Otherwise, assume we mapped all touches from the previous frame.
  4998. // Reuse all mappings that are still applicable.
  4999. mappedTouchIdBits.value = mLastCookedState.fingerIdBits.value
  5000. & mCurrentCookedState.fingerIdBits.value;
  5001. usedGestureIdBits = mPointerGesture.lastGestureIdBits;
  5002. // Check whether we need to choose a new active gesture id because the
  5003. // current went went up.
  5004. for (BitSet32 upTouchIdBits(mLastCookedState.fingerIdBits.value
  5005. & ~mCurrentCookedState.fingerIdBits.value);
  5006. !upTouchIdBits.isEmpty(); ) {
  5007. uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
  5008. uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
  5009. if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
  5010. mPointerGesture.activeGestureId = -1;
  5011. break;
  5012. }
  5013. }
  5014. }
  5015. #if DEBUG_GESTURES
  5016. ALOGD("Gestures: FREEFORM follow up "
  5017. "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
  5018. "activeGestureId=%d",
  5019. mappedTouchIdBits.value, usedGestureIdBits.value,
  5020. mPointerGesture.activeGestureId);
  5021. #endif
  5022. BitSet32 idBits(mCurrentCookedState.fingerIdBits);
  5023. for (uint32_t i = 0; i < currentFingerCount; i++) {
  5024. uint32_t touchId = idBits.clearFirstMarkedBit();
  5025. uint32_t gestureId;
  5026. if (!mappedTouchIdBits.hasBit(touchId)) {
  5027. gestureId = usedGestureIdBits.markFirstUnmarkedBit();
  5028. mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
  5029. #if DEBUG_GESTURES
  5030. ALOGD("Gestures: FREEFORM "
  5031. "new mapping for touch id %d -> gesture id %d",
  5032. touchId, gestureId);
  5033. #endif
  5034. } else {
  5035. gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
  5036. #if DEBUG_GESTURES
  5037. ALOGD("Gestures: FREEFORM "
  5038. "existing mapping for touch id %d -> gesture id %d",
  5039. touchId, gestureId);
  5040. #endif
  5041. }
  5042. mPointerGesture.currentGestureIdBits.markBit(gestureId);
  5043. mPointerGesture.currentGestureIdToIndex[gestureId] = i;
  5044. const RawPointerData::Pointer& pointer =
  5045. mCurrentRawState.rawPointerData.pointerForId(touchId);
  5046. float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
  5047. * mPointerXZoomScale;
  5048. float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
  5049. * mPointerYZoomScale;
  5050. rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
  5051. mPointerGesture.currentGestureProperties[i].clear();
  5052. mPointerGesture.currentGestureProperties[i].id = gestureId;
  5053. mPointerGesture.currentGestureProperties[i].toolType =
  5054. AMOTION_EVENT_TOOL_TYPE_FINGER;
  5055. mPointerGesture.currentGestureCoords[i].clear();
  5056. mPointerGesture.currentGestureCoords[i].setAxisValue(
  5057. AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
  5058. mPointerGesture.currentGestureCoords[i].setAxisValue(
  5059. AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
  5060. mPointerGesture.currentGestureCoords[i].setAxisValue(
  5061. AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
  5062. }
  5063. if (mPointerGesture.activeGestureId < 0) {
  5064. mPointerGesture.activeGestureId =
  5065. mPointerGesture.currentGestureIdBits.firstMarkedBit();
  5066. #if DEBUG_GESTURES
  5067. ALOGD("Gestures: FREEFORM new "
  5068. "activeGestureId=%d", mPointerGesture.activeGestureId);
  5069. #endif
  5070. }
  5071. }
  5072. }
  5073. mPointerController->setButtonState(mCurrentRawState.buttonState);
  5074. #if DEBUG_GESTURES
  5075. ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
  5076. "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
  5077. "lastGestureMode=%d, lastGestureIdBits=0x%08x",
  5078. toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
  5079. mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
  5080. mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
  5081. for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
  5082. uint32_t id = idBits.clearFirstMarkedBit();
  5083. uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
  5084. const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
  5085. const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
  5086. ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
  5087. "x=%0.3f, y=%0.3f, pressure=%0.3f",
  5088. id, index, properties.toolType,
  5089. coords.getAxisValue(AMOTION_EVENT_AXIS_X),
  5090. coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
  5091. coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
  5092. }
  5093. for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
  5094. uint32_t id = idBits.clearFirstMarkedBit();
  5095. uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
  5096. const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
  5097. const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
  5098. ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
  5099. "x=%0.3f, y=%0.3f, pressure=%0.3f",
  5100. id, index, properties.toolType,
  5101. coords.getAxisValue(AMOTION_EVENT_AXIS_X),
  5102. coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
  5103. coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
  5104. }
  5105. #endif
  5106. return true;
  5107. }
  5108. void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
  5109. mPointerSimple.currentCoords.clear();
  5110. mPointerSimple.currentProperties.clear();
  5111. bool down, hovering;
  5112. if (!mCurrentCookedState.stylusIdBits.isEmpty()) {
  5113. uint32_t id = mCurrentCookedState.stylusIdBits.firstMarkedBit();
  5114. uint32_t index = mCurrentCookedState.cookedPointerData.idToIndex[id];
  5115. float x = mCurrentCookedState.cookedPointerData.pointerCoords[index].getX();
  5116. float y = mCurrentCookedState.cookedPointerData.pointerCoords[index].getY();
  5117. mPointerController->setPosition(x, y);
  5118. hovering = mCurrentCookedState.cookedPointerData.hoveringIdBits.hasBit(id);
  5119. down = !hovering;
  5120. mPointerController->getPosition(&x, &y);
  5121. mPointerSimple.currentCoords.copyFrom(
  5122. mCurrentCookedState.cookedPointerData.pointerCoords[index]);
  5123. mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
  5124. mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  5125. mPointerSimple.currentProperties.id = 0;
  5126. mPointerSimple.currentProperties.toolType =
  5127. mCurrentCookedState.cookedPointerData.pointerProperties[index].toolType;
  5128. mLastStylusTime = when;
  5129. } else {
  5130. down = false;
  5131. hovering = false;
  5132. }
  5133. dispatchPointerSimple(when, policyFlags, down, hovering);
  5134. }
  5135. void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
  5136. abortPointerSimple(when, policyFlags);
  5137. }
  5138. void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
  5139. mPointerSimple.currentCoords.clear();
  5140. mPointerSimple.currentProperties.clear();
  5141. bool down, hovering;
  5142. if (!mCurrentCookedState.mouseIdBits.isEmpty()) {
  5143. uint32_t id = mCurrentCookedState.mouseIdBits.firstMarkedBit();
  5144. uint32_t currentIndex = mCurrentRawState.rawPointerData.idToIndex[id];
  5145. if (mLastCookedState.mouseIdBits.hasBit(id)) {
  5146. uint32_t lastIndex = mCurrentRawState.rawPointerData.idToIndex[id];
  5147. float deltaX = (mCurrentRawState.rawPointerData.pointers[currentIndex].x
  5148. - mLastRawState.rawPointerData.pointers[lastIndex].x)
  5149. * mPointerXMovementScale;
  5150. float deltaY = (mCurrentRawState.rawPointerData.pointers[currentIndex].y
  5151. - mLastRawState.rawPointerData.pointers[lastIndex].y)
  5152. * mPointerYMovementScale;
  5153. rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
  5154. mPointerVelocityControl.move(when, &deltaX, &deltaY);
  5155. mPointerController->move(deltaX, deltaY);
  5156. } else {
  5157. mPointerVelocityControl.reset();
  5158. }
  5159. down = isPointerDown(mCurrentRawState.buttonState);
  5160. hovering = !down;
  5161. float x, y;
  5162. mPointerController->getPosition(&x, &y);
  5163. mPointerSimple.currentCoords.copyFrom(
  5164. mCurrentCookedState.cookedPointerData.pointerCoords[currentIndex]);
  5165. mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
  5166. mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
  5167. mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
  5168. hovering ? 0.0f : 1.0f);
  5169. mPointerSimple.currentProperties.id = 0;
  5170. mPointerSimple.currentProperties.toolType =
  5171. mCurrentCookedState.cookedPointerData.pointerProperties[currentIndex].toolType;
  5172. } else {
  5173. mPointerVelocityControl.reset();
  5174. down = false;
  5175. hovering = false;
  5176. }
  5177. dispatchPointerSimple(when, policyFlags, down, hovering);
  5178. }
  5179. void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
  5180. abortPointerSimple(when, policyFlags);
  5181. mPointerVelocityControl.reset();
  5182. }
  5183. void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
  5184. bool down, bool hovering) {
  5185. int32_t metaState = getContext()->getGlobalMetaState();
  5186. if (mPointerController != NULL) {
  5187. if (down || hovering) {
  5188. mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
  5189. mPointerController->clearSpots();
  5190. mPointerController->setButtonState(mCurrentRawState.buttonState);
  5191. unfadePointer(PointerControllerInterface::TRANSITION_IMMEDIATE);
  5192. } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
  5193. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  5194. }
  5195. }
  5196. if (rejectPalm(when)) { // stylus is currently active
  5197. mPointerSimple.reset();
  5198. return;
  5199. }
  5200. if (mPointerSimple.down && !down) {
  5201. mPointerSimple.down = false;
  5202. // Send up.
  5203. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5204. AMOTION_EVENT_ACTION_UP, 0, 0, metaState, mLastRawState.buttonState, 0,
  5205. mViewport.displayId,
  5206. 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
  5207. mOrientedXPrecision, mOrientedYPrecision,
  5208. mPointerSimple.downTime);
  5209. getListener()->notifyMotion(&args);
  5210. }
  5211. if (mPointerSimple.hovering && !hovering) {
  5212. mPointerSimple.hovering = false;
  5213. // Send hover exit.
  5214. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5215. AMOTION_EVENT_ACTION_HOVER_EXIT, 0, 0, metaState, mLastRawState.buttonState, 0,
  5216. mViewport.displayId,
  5217. 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
  5218. mOrientedXPrecision, mOrientedYPrecision,
  5219. mPointerSimple.downTime);
  5220. getListener()->notifyMotion(&args);
  5221. }
  5222. if (down) {
  5223. if (!mPointerSimple.down) {
  5224. mPointerSimple.down = true;
  5225. mPointerSimple.downTime = when;
  5226. // Send down.
  5227. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5228. AMOTION_EVENT_ACTION_DOWN, 0, 0, metaState, mCurrentRawState.buttonState, 0,
  5229. mViewport.displayId,
  5230. 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
  5231. mOrientedXPrecision, mOrientedYPrecision,
  5232. mPointerSimple.downTime);
  5233. getListener()->notifyMotion(&args);
  5234. }
  5235. // Send move.
  5236. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5237. AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, mCurrentRawState.buttonState, 0,
  5238. mViewport.displayId,
  5239. 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
  5240. mOrientedXPrecision, mOrientedYPrecision,
  5241. mPointerSimple.downTime);
  5242. getListener()->notifyMotion(&args);
  5243. }
  5244. if (hovering) {
  5245. if (!mPointerSimple.hovering) {
  5246. mPointerSimple.hovering = true;
  5247. // Send hover enter.
  5248. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5249. AMOTION_EVENT_ACTION_HOVER_ENTER, 0, 0, metaState,
  5250. mCurrentRawState.buttonState, 0,
  5251. mViewport.displayId,
  5252. 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
  5253. mOrientedXPrecision, mOrientedYPrecision,
  5254. mPointerSimple.downTime);
  5255. getListener()->notifyMotion(&args);
  5256. }
  5257. // Send hover move.
  5258. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5259. AMOTION_EVENT_ACTION_HOVER_MOVE, 0, 0, metaState,
  5260. mCurrentRawState.buttonState, 0,
  5261. mViewport.displayId,
  5262. 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
  5263. mOrientedXPrecision, mOrientedYPrecision,
  5264. mPointerSimple.downTime);
  5265. getListener()->notifyMotion(&args);
  5266. }
  5267. if (mCurrentRawState.rawVScroll || mCurrentRawState.rawHScroll) {
  5268. float vscroll = mCurrentRawState.rawVScroll;
  5269. float hscroll = mCurrentRawState.rawHScroll;
  5270. mWheelYVelocityControl.move(when, NULL, &vscroll);
  5271. mWheelXVelocityControl.move(when, &hscroll, NULL);
  5272. // Send scroll.
  5273. PointerCoords pointerCoords;
  5274. pointerCoords.copyFrom(mPointerSimple.currentCoords);
  5275. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
  5276. pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
  5277. NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
  5278. AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState, mCurrentRawState.buttonState, 0,
  5279. mViewport.displayId,
  5280. 1, &mPointerSimple.currentProperties, &pointerCoords,
  5281. mOrientedXPrecision, mOrientedYPrecision,
  5282. mPointerSimple.downTime);
  5283. getListener()->notifyMotion(&args);
  5284. }
  5285. // Save state.
  5286. if (down || hovering) {
  5287. mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
  5288. mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
  5289. } else {
  5290. mPointerSimple.reset();
  5291. }
  5292. }
  5293. void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
  5294. mPointerSimple.currentCoords.clear();
  5295. mPointerSimple.currentProperties.clear();
  5296. dispatchPointerSimple(when, policyFlags, false, false);
  5297. }
  5298. void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
  5299. int32_t action, int32_t actionButton, int32_t flags,
  5300. int32_t metaState, int32_t buttonState, int32_t edgeFlags,
  5301. const PointerProperties* properties, const PointerCoords* coords,
  5302. const uint32_t* idToIndex, BitSet32 idBits, int32_t changedId,
  5303. float xPrecision, float yPrecision, nsecs_t downTime) {
  5304. if (rejectPalm(when)) return;
  5305. PointerCoords pointerCoords[MAX_POINTERS];
  5306. PointerProperties pointerProperties[MAX_POINTERS];
  5307. uint32_t pointerCount = 0;
  5308. while (!idBits.isEmpty()) {
  5309. uint32_t id = idBits.clearFirstMarkedBit();
  5310. uint32_t index = idToIndex[id];
  5311. pointerProperties[pointerCount].copyFrom(properties[index]);
  5312. pointerCoords[pointerCount].copyFrom(coords[index]);
  5313. if (changedId >= 0 && id == uint32_t(changedId)) {
  5314. action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  5315. }
  5316. pointerCount += 1;
  5317. }
  5318. ALOG_ASSERT(pointerCount != 0);
  5319. if (changedId >= 0 && pointerCount == 1) {
  5320. // Replace initial down and final up action.
  5321. // We can compare the action without masking off the changed pointer index
  5322. // because we know the index is 0.
  5323. if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
  5324. action = AMOTION_EVENT_ACTION_DOWN;
  5325. } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
  5326. action = AMOTION_EVENT_ACTION_UP;
  5327. } else {
  5328. // Can't happen.
  5329. ALOG_ASSERT(false);
  5330. }
  5331. }
  5332. NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
  5333. action, actionButton, flags, metaState, buttonState, edgeFlags,
  5334. mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
  5335. xPrecision, yPrecision, downTime);
  5336. getListener()->notifyMotion(&args);
  5337. }
  5338. bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
  5339. const PointerCoords* inCoords, const uint32_t* inIdToIndex,
  5340. PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
  5341. BitSet32 idBits) const {
  5342. bool changed = false;
  5343. while (!idBits.isEmpty()) {
  5344. uint32_t id = idBits.clearFirstMarkedBit();
  5345. uint32_t inIndex = inIdToIndex[id];
  5346. uint32_t outIndex = outIdToIndex[id];
  5347. const PointerProperties& curInProperties = inProperties[inIndex];
  5348. const PointerCoords& curInCoords = inCoords[inIndex];
  5349. PointerProperties& curOutProperties = outProperties[outIndex];
  5350. PointerCoords& curOutCoords = outCoords[outIndex];
  5351. if (curInProperties != curOutProperties) {
  5352. curOutProperties.copyFrom(curInProperties);
  5353. changed = true;
  5354. }
  5355. if (curInCoords != curOutCoords) {
  5356. curOutCoords.copyFrom(curInCoords);
  5357. changed = true;
  5358. }
  5359. }
  5360. return changed;
  5361. }
  5362. void TouchInputMapper::fadePointer() {
  5363. if (mPointerController != NULL) {
  5364. mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
  5365. }
  5366. }
  5367. void TouchInputMapper::unfadePointer(PointerControllerInterface::Transition transition) {
  5368. if (mPointerController != NULL &&
  5369. !(mPointerUsage == POINTER_USAGE_STYLUS && !mConfig.stylusIconEnabled)) {
  5370. mPointerController->unfade(transition);
  5371. }
  5372. }
  5373. nsecs_t TouchInputMapper::mLastStylusTime = 0;
  5374. bool TouchInputMapper::rejectPalm(nsecs_t when) {
  5375. return (when - mLastStylusTime < mConfig.stylusPalmRejectionTime) &&
  5376. mPointerSimple.currentProperties.toolType != AMOTION_EVENT_TOOL_TYPE_STYLUS &&
  5377. mPointerSimple.currentProperties.toolType != AMOTION_EVENT_TOOL_TYPE_ERASER;
  5378. }
  5379. void TouchInputMapper::cancelTouch(nsecs_t when) {
  5380. abortPointerUsage(when, 0 /*policyFlags*/);
  5381. abortTouches(when, 0 /* policyFlags*/);
  5382. }
  5383. bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
  5384. return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
  5385. && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
  5386. }
  5387. const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
  5388. int32_t x, int32_t y) {
  5389. size_t numVirtualKeys = mVirtualKeys.size();
  5390. for (size_t i = 0; i < numVirtualKeys; i++) {
  5391. const VirtualKey& virtualKey = mVirtualKeys[i];
  5392. #if DEBUG_VIRTUAL_KEYS
  5393. ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
  5394. "left=%d, top=%d, right=%d, bottom=%d",
  5395. x, y,
  5396. virtualKey.keyCode, virtualKey.scanCode,
  5397. virtualKey.hitLeft, virtualKey.hitTop,
  5398. virtualKey.hitRight, virtualKey.hitBottom);
  5399. #endif
  5400. if (virtualKey.isHit(x, y)) {
  5401. return & virtualKey;
  5402. }
  5403. }
  5404. return NULL;
  5405. }
  5406. void TouchInputMapper::assignPointerIds(const RawState* last, RawState* current) {
  5407. uint32_t currentPointerCount = current->rawPointerData.pointerCount;
  5408. uint32_t lastPointerCount = last->rawPointerData.pointerCount;
  5409. current->rawPointerData.clearIdBits();
  5410. if (currentPointerCount == 0) {
  5411. // No pointers to assign.
  5412. return;
  5413. }
  5414. if (lastPointerCount == 0) {
  5415. // All pointers are new.
  5416. for (uint32_t i = 0; i < currentPointerCount; i++) {
  5417. uint32_t id = i;
  5418. current->rawPointerData.pointers[i].id = id;
  5419. current->rawPointerData.idToIndex[id] = i;
  5420. current->rawPointerData.markIdBit(id, current->rawPointerData.isHovering(i));
  5421. }
  5422. return;
  5423. }
  5424. if (currentPointerCount == 1 && lastPointerCount == 1
  5425. && current->rawPointerData.pointers[0].toolType
  5426. == last->rawPointerData.pointers[0].toolType) {
  5427. // Only one pointer and no change in count so it must have the same id as before.
  5428. uint32_t id = last->rawPointerData.pointers[0].id;
  5429. current->rawPointerData.pointers[0].id = id;
  5430. current->rawPointerData.idToIndex[id] = 0;
  5431. current->rawPointerData.markIdBit(id, current->rawPointerData.isHovering(0));
  5432. return;
  5433. }
  5434. // General case.
  5435. // We build a heap of squared euclidean distances between current and last pointers
  5436. // associated with the current and last pointer indices. Then, we find the best
  5437. // match (by distance) for each current pointer.
  5438. // The pointers must have the same tool type but it is possible for them to
  5439. // transition from hovering to touching or vice-versa while retaining the same id.
  5440. PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
  5441. uint32_t heapSize = 0;
  5442. for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
  5443. currentPointerIndex++) {
  5444. for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
  5445. lastPointerIndex++) {
  5446. const RawPointerData::Pointer& currentPointer =
  5447. current->rawPointerData.pointers[currentPointerIndex];
  5448. const RawPointerData::Pointer& lastPointer =
  5449. last->rawPointerData.pointers[lastPointerIndex];
  5450. if (currentPointer.toolType == lastPointer.toolType) {
  5451. int64_t deltaX = currentPointer.x - lastPointer.x;
  5452. int64_t deltaY = currentPointer.y - lastPointer.y;
  5453. uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
  5454. // Insert new element into the heap (sift up).
  5455. heap[heapSize].currentPointerIndex = currentPointerIndex;
  5456. heap[heapSize].lastPointerIndex = lastPointerIndex;
  5457. heap[heapSize].distance = distance;
  5458. heapSize += 1;
  5459. }
  5460. }
  5461. }
  5462. // Heapify
  5463. for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
  5464. startIndex -= 1;
  5465. for (uint32_t parentIndex = startIndex; ;) {
  5466. uint32_t childIndex = parentIndex * 2 + 1;
  5467. if (childIndex >= heapSize) {
  5468. break;
  5469. }
  5470. if (childIndex + 1 < heapSize
  5471. && heap[childIndex + 1].distance < heap[childIndex].distance) {
  5472. childIndex += 1;
  5473. }
  5474. if (heap[parentIndex].distance <= heap[childIndex].distance) {
  5475. break;
  5476. }
  5477. swap(heap[parentIndex], heap[childIndex]);
  5478. parentIndex = childIndex;
  5479. }
  5480. }
  5481. #if DEBUG_POINTER_ASSIGNMENT
  5482. ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
  5483. for (size_t i = 0; i < heapSize; i++) {
  5484. ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
  5485. i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
  5486. heap[i].distance);
  5487. }
  5488. #endif
  5489. // Pull matches out by increasing order of distance.
  5490. // To avoid reassigning pointers that have already been matched, the loop keeps track
  5491. // of which last and current pointers have been matched using the matchedXXXBits variables.
  5492. // It also tracks the used pointer id bits.
  5493. BitSet32 matchedLastBits(0);
  5494. BitSet32 matchedCurrentBits(0);
  5495. BitSet32 usedIdBits(0);
  5496. bool first = true;
  5497. for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
  5498. while (heapSize > 0) {
  5499. if (first) {
  5500. // The first time through the loop, we just consume the root element of
  5501. // the heap (the one with smallest distance).
  5502. first = false;
  5503. } else {
  5504. // Previous iterations consumed the root element of the heap.
  5505. // Pop root element off of the heap (sift down).
  5506. heap[0] = heap[heapSize];
  5507. for (uint32_t parentIndex = 0; ;) {
  5508. uint32_t childIndex = parentIndex * 2 + 1;
  5509. if (childIndex >= heapSize) {
  5510. break;
  5511. }
  5512. if (childIndex + 1 < heapSize
  5513. && heap[childIndex + 1].distance < heap[childIndex].distance) {
  5514. childIndex += 1;
  5515. }
  5516. if (heap[parentIndex].distance <= heap[childIndex].distance) {
  5517. break;
  5518. }
  5519. swap(heap[parentIndex], heap[childIndex]);
  5520. parentIndex = childIndex;
  5521. }
  5522. #if DEBUG_POINTER_ASSIGNMENT
  5523. ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
  5524. for (size_t i = 0; i < heapSize; i++) {
  5525. ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
  5526. i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
  5527. heap[i].distance);
  5528. }
  5529. #endif
  5530. }
  5531. heapSize -= 1;
  5532. uint32_t currentPointerIndex = heap[0].currentPointerIndex;
  5533. if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
  5534. uint32_t lastPointerIndex = heap[0].lastPointerIndex;
  5535. if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
  5536. matchedCurrentBits.markBit(currentPointerIndex);
  5537. matchedLastBits.markBit(lastPointerIndex);
  5538. uint32_t id = last->rawPointerData.pointers[lastPointerIndex].id;
  5539. current->rawPointerData.pointers[currentPointerIndex].id = id;
  5540. current->rawPointerData.idToIndex[id] = currentPointerIndex;
  5541. current->rawPointerData.markIdBit(id,
  5542. current->rawPointerData.isHovering(currentPointerIndex));
  5543. usedIdBits.markBit(id);
  5544. #if DEBUG_POINTER_ASSIGNMENT
  5545. ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
  5546. lastPointerIndex, currentPointerIndex, id, heap[0].distance);
  5547. #endif
  5548. break;
  5549. }
  5550. }
  5551. // Assign fresh ids to pointers that were not matched in the process.
  5552. for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
  5553. uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
  5554. uint32_t id = usedIdBits.markFirstUnmarkedBit();
  5555. current->rawPointerData.pointers[currentPointerIndex].id = id;
  5556. current->rawPointerData.idToIndex[id] = currentPointerIndex;
  5557. current->rawPointerData.markIdBit(id,
  5558. current->rawPointerData.isHovering(currentPointerIndex));
  5559. #if DEBUG_POINTER_ASSIGNMENT
  5560. ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
  5561. currentPointerIndex, id);
  5562. #endif
  5563. }
  5564. }
  5565. int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
  5566. if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
  5567. return AKEY_STATE_VIRTUAL;
  5568. }
  5569. size_t numVirtualKeys = mVirtualKeys.size();
  5570. for (size_t i = 0; i < numVirtualKeys; i++) {
  5571. const VirtualKey& virtualKey = mVirtualKeys[i];
  5572. if (virtualKey.keyCode == keyCode) {
  5573. return AKEY_STATE_UP;
  5574. }
  5575. }
  5576. return AKEY_STATE_UNKNOWN;
  5577. }
  5578. int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
  5579. if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
  5580. return AKEY_STATE_VIRTUAL;
  5581. }
  5582. size_t numVirtualKeys = mVirtualKeys.size();
  5583. for (size_t i = 0; i < numVirtualKeys; i++) {
  5584. const VirtualKey& virtualKey = mVirtualKeys[i];
  5585. if (virtualKey.scanCode == scanCode) {
  5586. return AKEY_STATE_UP;
  5587. }
  5588. }
  5589. return AKEY_STATE_UNKNOWN;
  5590. }
  5591. bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
  5592. const int32_t* keyCodes, uint8_t* outFlags) {
  5593. size_t numVirtualKeys = mVirtualKeys.size();
  5594. for (size_t i = 0; i < numVirtualKeys; i++) {
  5595. const VirtualKey& virtualKey = mVirtualKeys[i];
  5596. for (size_t i = 0; i < numCodes; i++) {
  5597. if (virtualKey.keyCode == keyCodes[i]) {
  5598. outFlags[i] = 1;
  5599. }
  5600. }
  5601. }
  5602. return true;
  5603. }
  5604. // --- SingleTouchInputMapper ---
  5605. SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
  5606. TouchInputMapper(device) {
  5607. }
  5608. SingleTouchInputMapper::~SingleTouchInputMapper() {
  5609. }
  5610. void SingleTouchInputMapper::reset(nsecs_t when) {
  5611. mSingleTouchMotionAccumulator.reset(getDevice());
  5612. TouchInputMapper::reset(when);
  5613. }
  5614. void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
  5615. TouchInputMapper::process(rawEvent);
  5616. mSingleTouchMotionAccumulator.process(rawEvent);
  5617. }
  5618. void SingleTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
  5619. if (mTouchButtonAccumulator.isToolActive()) {
  5620. outState->rawPointerData.pointerCount = 1;
  5621. outState->rawPointerData.idToIndex[0] = 0;
  5622. bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
  5623. && (mTouchButtonAccumulator.isHovering()
  5624. || (mRawPointerAxes.pressure.valid
  5625. && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
  5626. outState->rawPointerData.markIdBit(0, isHovering);
  5627. RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[0];
  5628. outPointer.id = 0;
  5629. outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
  5630. outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
  5631. outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
  5632. outPointer.touchMajor = 0;
  5633. outPointer.touchMinor = 0;
  5634. outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
  5635. outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
  5636. outPointer.orientation = 0;
  5637. outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
  5638. outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
  5639. outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
  5640. outPointer.toolType = mTouchButtonAccumulator.getToolType();
  5641. if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  5642. outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  5643. }
  5644. outPointer.isHovering = isHovering;
  5645. }
  5646. }
  5647. void SingleTouchInputMapper::configureRawPointerAxes() {
  5648. TouchInputMapper::configureRawPointerAxes();
  5649. getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
  5650. getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
  5651. getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
  5652. getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
  5653. getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
  5654. getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
  5655. getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
  5656. }
  5657. bool SingleTouchInputMapper::hasStylus() const {
  5658. return mTouchButtonAccumulator.hasStylus();
  5659. }
  5660. // --- MultiTouchInputMapper ---
  5661. MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
  5662. TouchInputMapper(device) {
  5663. }
  5664. MultiTouchInputMapper::~MultiTouchInputMapper() {
  5665. }
  5666. void MultiTouchInputMapper::reset(nsecs_t when) {
  5667. mMultiTouchMotionAccumulator.reset(getDevice());
  5668. mPointerIdBits.clear();
  5669. TouchInputMapper::reset(when);
  5670. }
  5671. void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
  5672. TouchInputMapper::process(rawEvent);
  5673. mMultiTouchMotionAccumulator.process(rawEvent);
  5674. }
  5675. void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
  5676. size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
  5677. size_t outCount = 0;
  5678. BitSet32 newPointerIdBits;
  5679. for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
  5680. const MultiTouchMotionAccumulator::Slot* inSlot =
  5681. mMultiTouchMotionAccumulator.getSlot(inIndex);
  5682. if (!inSlot->isInUse()) {
  5683. continue;
  5684. }
  5685. if (outCount >= MAX_POINTERS) {
  5686. #if DEBUG_POINTERS
  5687. ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
  5688. "ignoring the rest.",
  5689. getDeviceName().string(), MAX_POINTERS);
  5690. #endif
  5691. break; // too many fingers!
  5692. }
  5693. RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
  5694. outPointer.x = inSlot->getX();
  5695. outPointer.y = inSlot->getY();
  5696. outPointer.pressure = inSlot->getPressure();
  5697. outPointer.touchMajor = inSlot->getTouchMajor();
  5698. outPointer.touchMinor = inSlot->getTouchMinor();
  5699. outPointer.toolMajor = inSlot->getToolMajor();
  5700. outPointer.toolMinor = inSlot->getToolMinor();
  5701. outPointer.orientation = inSlot->getOrientation();
  5702. outPointer.distance = inSlot->getDistance();
  5703. outPointer.tiltX = 0;
  5704. outPointer.tiltY = 0;
  5705. outPointer.toolType = inSlot->getToolType();
  5706. if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  5707. outPointer.toolType = mTouchButtonAccumulator.getToolType();
  5708. if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  5709. outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
  5710. }
  5711. }
  5712. bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
  5713. && (mTouchButtonAccumulator.isHovering()
  5714. || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
  5715. outPointer.isHovering = isHovering;
  5716. // Assign pointer id using tracking id if available.
  5717. mHavePointerIds = true;
  5718. int32_t trackingId = inSlot->getTrackingId();
  5719. int32_t id = -1;
  5720. if (trackingId >= 0) {
  5721. for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
  5722. uint32_t n = idBits.clearFirstMarkedBit();
  5723. if (mPointerTrackingIdMap[n] == trackingId) {
  5724. id = n;
  5725. }
  5726. }
  5727. if (id < 0 && !mPointerIdBits.isFull()) {
  5728. id = mPointerIdBits.markFirstUnmarkedBit();
  5729. mPointerTrackingIdMap[id] = trackingId;
  5730. }
  5731. }
  5732. if (id < 0) {
  5733. mHavePointerIds = false;
  5734. outState->rawPointerData.clearIdBits();
  5735. newPointerIdBits.clear();
  5736. } else {
  5737. outPointer.id = id;
  5738. outState->rawPointerData.idToIndex[id] = outCount;
  5739. outState->rawPointerData.markIdBit(id, isHovering);
  5740. newPointerIdBits.markBit(id);
  5741. }
  5742. outCount += 1;
  5743. }
  5744. outState->rawPointerData.pointerCount = outCount;
  5745. mPointerIdBits = newPointerIdBits;
  5746. mMultiTouchMotionAccumulator.finishSync();
  5747. }
  5748. void MultiTouchInputMapper::configureRawPointerAxes() {
  5749. TouchInputMapper::configureRawPointerAxes();
  5750. getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
  5751. getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
  5752. getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
  5753. getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
  5754. getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
  5755. getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
  5756. getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
  5757. getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
  5758. getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
  5759. getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
  5760. getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
  5761. if (mRawPointerAxes.trackingId.valid
  5762. && mRawPointerAxes.slot.valid
  5763. && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
  5764. size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
  5765. if (slotCount > MAX_SLOTS) {
  5766. ALOGW("MultiTouch Device %s reported %zu slots but the framework "
  5767. "only supports a maximum of %zu slots at this time.",
  5768. getDeviceName().string(), slotCount, MAX_SLOTS);
  5769. slotCount = MAX_SLOTS;
  5770. }
  5771. mMultiTouchMotionAccumulator.configure(getDevice(),
  5772. slotCount, true /*usingSlotsProtocol*/);
  5773. } else {
  5774. mMultiTouchMotionAccumulator.configure(getDevice(),
  5775. MAX_POINTERS, false /*usingSlotsProtocol*/);
  5776. }
  5777. }
  5778. bool MultiTouchInputMapper::hasStylus() const {
  5779. return mMultiTouchMotionAccumulator.hasStylus()
  5780. || mTouchButtonAccumulator.hasStylus();
  5781. }
  5782. // --- ExternalStylusInputMapper
  5783. ExternalStylusInputMapper::ExternalStylusInputMapper(InputDevice* device) :
  5784. InputMapper(device) {
  5785. }
  5786. uint32_t ExternalStylusInputMapper::getSources() {
  5787. return AINPUT_SOURCE_STYLUS;
  5788. }
  5789. void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  5790. InputMapper::populateDeviceInfo(info);
  5791. info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS,
  5792. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  5793. }
  5794. void ExternalStylusInputMapper::dump(String8& dump) {
  5795. dump.append(INDENT2 "External Stylus Input Mapper:\n");
  5796. dump.append(INDENT3 "Raw Stylus Axes:\n");
  5797. dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
  5798. dump.append(INDENT3 "Stylus State:\n");
  5799. dumpStylusState(dump, mStylusState);
  5800. }
  5801. void ExternalStylusInputMapper::configure(nsecs_t when,
  5802. const InputReaderConfiguration* config, uint32_t changes) {
  5803. getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
  5804. mTouchButtonAccumulator.configure(getDevice());
  5805. }
  5806. void ExternalStylusInputMapper::reset(nsecs_t when) {
  5807. InputDevice* device = getDevice();
  5808. mSingleTouchMotionAccumulator.reset(device);
  5809. mTouchButtonAccumulator.reset(device);
  5810. InputMapper::reset(when);
  5811. }
  5812. void ExternalStylusInputMapper::process(const RawEvent* rawEvent) {
  5813. mSingleTouchMotionAccumulator.process(rawEvent);
  5814. mTouchButtonAccumulator.process(rawEvent);
  5815. if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
  5816. sync(rawEvent->when);
  5817. }
  5818. }
  5819. void ExternalStylusInputMapper::sync(nsecs_t when) {
  5820. mStylusState.clear();
  5821. mStylusState.when = when;
  5822. mStylusState.toolType = mTouchButtonAccumulator.getToolType();
  5823. if (mStylusState.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
  5824. mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
  5825. }
  5826. int32_t pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
  5827. if (mRawPressureAxis.valid) {
  5828. mStylusState.pressure = float(pressure) / mRawPressureAxis.maxValue;
  5829. } else if (mTouchButtonAccumulator.isToolActive()) {
  5830. mStylusState.pressure = 1.0f;
  5831. } else {
  5832. mStylusState.pressure = 0.0f;
  5833. }
  5834. mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
  5835. mContext->dispatchExternalStylusState(mStylusState);
  5836. }
  5837. // --- JoystickInputMapper ---
  5838. JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
  5839. InputMapper(device) {
  5840. }
  5841. JoystickInputMapper::~JoystickInputMapper() {
  5842. }
  5843. uint32_t JoystickInputMapper::getSources() {
  5844. return AINPUT_SOURCE_JOYSTICK;
  5845. }
  5846. void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
  5847. InputMapper::populateDeviceInfo(info);
  5848. for (size_t i = 0; i < mAxes.size(); i++) {
  5849. const Axis& axis = mAxes.valueAt(i);
  5850. addMotionRange(axis.axisInfo.axis, axis, info);
  5851. if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
  5852. addMotionRange(axis.axisInfo.highAxis, axis, info);
  5853. }
  5854. }
  5855. }
  5856. void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis,
  5857. InputDeviceInfo* info) {
  5858. info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK,
  5859. axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
  5860. /* In order to ease the transition for developers from using the old axes
  5861. * to the newer, more semantically correct axes, we'll continue to register
  5862. * the old axes as duplicates of their corresponding new ones. */
  5863. int32_t compatAxis = getCompatAxis(axisId);
  5864. if (compatAxis >= 0) {
  5865. info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK,
  5866. axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
  5867. }
  5868. }
  5869. /* A mapping from axes the joystick actually has to the axes that should be
  5870. * artificially created for compatibility purposes.
  5871. * Returns -1 if no compatibility axis is needed. */
  5872. int32_t JoystickInputMapper::getCompatAxis(int32_t axis) {
  5873. switch(axis) {
  5874. case AMOTION_EVENT_AXIS_LTRIGGER:
  5875. return AMOTION_EVENT_AXIS_BRAKE;
  5876. case AMOTION_EVENT_AXIS_RTRIGGER:
  5877. return AMOTION_EVENT_AXIS_GAS;
  5878. }
  5879. return -1;
  5880. }
  5881. void JoystickInputMapper::dump(String8& dump) {
  5882. dump.append(INDENT2 "Joystick Input Mapper:\n");
  5883. dump.append(INDENT3 "Axes:\n");
  5884. size_t numAxes = mAxes.size();
  5885. for (size_t i = 0; i < numAxes; i++) {
  5886. const Axis& axis = mAxes.valueAt(i);
  5887. const char* label = getAxisLabel(axis.axisInfo.axis);
  5888. if (label) {
  5889. dump.appendFormat(INDENT4 "%s", label);
  5890. } else {
  5891. dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
  5892. }
  5893. if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
  5894. label = getAxisLabel(axis.axisInfo.highAxis);
  5895. if (label) {
  5896. dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
  5897. } else {
  5898. dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
  5899. axis.axisInfo.splitValue);
  5900. }
  5901. } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
  5902. dump.append(" (invert)");
  5903. }
  5904. dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
  5905. axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
  5906. dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
  5907. "highScale=%0.5f, highOffset=%0.5f\n",
  5908. axis.scale, axis.offset, axis.highScale, axis.highOffset);
  5909. dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
  5910. "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
  5911. mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
  5912. axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
  5913. }
  5914. }
  5915. void JoystickInputMapper::configure(nsecs_t when,
  5916. const InputReaderConfiguration* config, uint32_t changes) {
  5917. InputMapper::configure(when, config, changes);
  5918. if (!changes) { // first time only
  5919. // Collect all axes.
  5920. for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
  5921. if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
  5922. & INPUT_DEVICE_CLASS_JOYSTICK)) {
  5923. continue; // axis must be claimed by a different device
  5924. }
  5925. RawAbsoluteAxisInfo rawAxisInfo;
  5926. getAbsoluteAxisInfo(abs, &rawAxisInfo);
  5927. if (rawAxisInfo.valid) {
  5928. // Map axis.
  5929. AxisInfo axisInfo;
  5930. bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
  5931. if (!explicitlyMapped) {
  5932. // Axis is not explicitly mapped, will choose a generic axis later.
  5933. axisInfo.mode = AxisInfo::MODE_NORMAL;
  5934. axisInfo.axis = -1;
  5935. }
  5936. // Apply flat override.
  5937. int32_t rawFlat = axisInfo.flatOverride < 0
  5938. ? rawAxisInfo.flat : axisInfo.flatOverride;
  5939. // Calculate scaling factors and limits.
  5940. Axis axis;
  5941. if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
  5942. float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
  5943. float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
  5944. axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
  5945. scale, 0.0f, highScale, 0.0f,
  5946. 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
  5947. rawAxisInfo.resolution * scale);
  5948. } else if (isCenteredAxis(axisInfo.axis)) {
  5949. float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
  5950. float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
  5951. axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
  5952. scale, offset, scale, offset,
  5953. -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
  5954. rawAxisInfo.resolution * scale);
  5955. } else {
  5956. float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
  5957. axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
  5958. scale, 0.0f, scale, 0.0f,
  5959. 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
  5960. rawAxisInfo.resolution * scale);
  5961. }
  5962. // To eliminate noise while the joystick is at rest, filter out small variations
  5963. // in axis values up front.
  5964. axis.filter = axis.fuzz ? axis.fuzz : axis.flat * 0.25f;
  5965. mAxes.add(abs, axis);
  5966. }
  5967. }
  5968. // If there are too many axes, start dropping them.
  5969. // Prefer to keep explicitly mapped axes.
  5970. if (mAxes.size() > PointerCoords::MAX_AXES) {
  5971. ALOGI("Joystick '%s' has %zu axes but the framework only supports a maximum of %d.",
  5972. getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
  5973. pruneAxes(true);
  5974. pruneAxes(false);
  5975. }
  5976. // Assign generic axis ids to remaining axes.
  5977. int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
  5978. size_t numAxes = mAxes.size();
  5979. for (size_t i = 0; i < numAxes; i++) {
  5980. Axis& axis = mAxes.editValueAt(i);
  5981. if (axis.axisInfo.axis < 0) {
  5982. while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
  5983. && haveAxis(nextGenericAxisId)) {
  5984. nextGenericAxisId += 1;
  5985. }
  5986. if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
  5987. axis.axisInfo.axis = nextGenericAxisId;
  5988. nextGenericAxisId += 1;
  5989. } else {
  5990. ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
  5991. "have already been assigned to other axes.",
  5992. getDeviceName().string(), mAxes.keyAt(i));
  5993. mAxes.removeItemsAt(i--);
  5994. numAxes -= 1;
  5995. }
  5996. }
  5997. }
  5998. }
  5999. }
  6000. bool JoystickInputMapper::haveAxis(int32_t axisId) {
  6001. size_t numAxes = mAxes.size();
  6002. for (size_t i = 0; i < numAxes; i++) {
  6003. const Axis& axis = mAxes.valueAt(i);
  6004. if (axis.axisInfo.axis == axisId
  6005. || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
  6006. && axis.axisInfo.highAxis == axisId)) {
  6007. return true;
  6008. }
  6009. }
  6010. return false;
  6011. }
  6012. void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
  6013. size_t i = mAxes.size();
  6014. while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
  6015. if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
  6016. continue;
  6017. }
  6018. ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
  6019. getDeviceName().string(), mAxes.keyAt(i));
  6020. mAxes.removeItemsAt(i);
  6021. }
  6022. }
  6023. bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
  6024. switch (axis) {
  6025. case AMOTION_EVENT_AXIS_X:
  6026. case AMOTION_EVENT_AXIS_Y:
  6027. case AMOTION_EVENT_AXIS_Z:
  6028. case AMOTION_EVENT_AXIS_RX:
  6029. case AMOTION_EVENT_AXIS_RY:
  6030. case AMOTION_EVENT_AXIS_RZ:
  6031. case AMOTION_EVENT_AXIS_HAT_X:
  6032. case AMOTION_EVENT_AXIS_HAT_Y:
  6033. case AMOTION_EVENT_AXIS_ORIENTATION:
  6034. case AMOTION_EVENT_AXIS_RUDDER:
  6035. case AMOTION_EVENT_AXIS_WHEEL:
  6036. return true;
  6037. default:
  6038. return false;
  6039. }
  6040. }
  6041. void JoystickInputMapper::reset(nsecs_t when) {
  6042. // Recenter all axes.
  6043. size_t numAxes = mAxes.size();
  6044. for (size_t i = 0; i < numAxes; i++) {
  6045. Axis& axis = mAxes.editValueAt(i);
  6046. axis.resetValue();
  6047. }
  6048. InputMapper::reset(when);
  6049. }
  6050. void JoystickInputMapper::process(const RawEvent* rawEvent) {
  6051. switch (rawEvent->type) {
  6052. case EV_ABS: {
  6053. ssize_t index = mAxes.indexOfKey(rawEvent->code);
  6054. if (index >= 0) {
  6055. Axis& axis = mAxes.editValueAt(index);
  6056. float newValue, highNewValue;
  6057. switch (axis.axisInfo.mode) {
  6058. case AxisInfo::MODE_INVERT:
  6059. newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
  6060. * axis.scale + axis.offset;
  6061. highNewValue = 0.0f;
  6062. break;
  6063. case AxisInfo::MODE_SPLIT:
  6064. if (rawEvent->value < axis.axisInfo.splitValue) {
  6065. newValue = (axis.axisInfo.splitValue - rawEvent->value)
  6066. * axis.scale + axis.offset;
  6067. highNewValue = 0.0f;
  6068. } else if (rawEvent->value > axis.axisInfo.splitValue) {
  6069. newValue = 0.0f;
  6070. highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
  6071. * axis.highScale + axis.highOffset;
  6072. } else {
  6073. newValue = 0.0f;
  6074. highNewValue = 0.0f;
  6075. }
  6076. break;
  6077. default:
  6078. newValue = rawEvent->value * axis.scale + axis.offset;
  6079. highNewValue = 0.0f;
  6080. break;
  6081. }
  6082. axis.newValue = newValue;
  6083. axis.highNewValue = highNewValue;
  6084. }
  6085. break;
  6086. }
  6087. case EV_SYN:
  6088. switch (rawEvent->code) {
  6089. case SYN_REPORT:
  6090. sync(rawEvent->when, false /*force*/);
  6091. break;
  6092. }
  6093. break;
  6094. }
  6095. }
  6096. void JoystickInputMapper::sync(nsecs_t when, bool force) {
  6097. if (!filterAxes(force)) {
  6098. return;
  6099. }
  6100. int32_t metaState = mContext->getGlobalMetaState();
  6101. int32_t buttonState = 0;
  6102. PointerProperties pointerProperties;
  6103. pointerProperties.clear();
  6104. pointerProperties.id = 0;
  6105. pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
  6106. PointerCoords pointerCoords;
  6107. pointerCoords.clear();
  6108. size_t numAxes = mAxes.size();
  6109. for (size_t i = 0; i < numAxes; i++) {
  6110. const Axis& axis = mAxes.valueAt(i);
  6111. setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
  6112. if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
  6113. setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
  6114. axis.highCurrentValue);
  6115. }
  6116. }
  6117. // Moving a joystick axis should not wake the device because joysticks can
  6118. // be fairly noisy even when not in use. On the other hand, pushing a gamepad
  6119. // button will likely wake the device.
  6120. // TODO: Use the input device configuration to control this behavior more finely.
  6121. uint32_t policyFlags = 0;
  6122. NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
  6123. AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
  6124. ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
  6125. getListener()->notifyMotion(&args);
  6126. }
  6127. void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords,
  6128. int32_t axis, float value) {
  6129. pointerCoords->setAxisValue(axis, value);
  6130. /* In order to ease the transition for developers from using the old axes
  6131. * to the newer, more semantically correct axes, we'll continue to produce
  6132. * values for the old axes as mirrors of the value of their corresponding
  6133. * new axes. */
  6134. int32_t compatAxis = getCompatAxis(axis);
  6135. if (compatAxis >= 0) {
  6136. pointerCoords->setAxisValue(compatAxis, value);
  6137. }
  6138. }
  6139. bool JoystickInputMapper::filterAxes(bool force) {
  6140. bool atLeastOneSignificantChange = force;
  6141. size_t numAxes = mAxes.size();
  6142. for (size_t i = 0; i < numAxes; i++) {
  6143. Axis& axis = mAxes.editValueAt(i);
  6144. if (force || hasValueChangedSignificantly(axis.filter,
  6145. axis.newValue, axis.currentValue, axis.min, axis.max)) {
  6146. axis.currentValue = axis.newValue;
  6147. atLeastOneSignificantChange = true;
  6148. }
  6149. if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
  6150. if (force || hasValueChangedSignificantly(axis.filter,
  6151. axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
  6152. axis.highCurrentValue = axis.highNewValue;
  6153. atLeastOneSignificantChange = true;
  6154. }
  6155. }
  6156. }
  6157. return atLeastOneSignificantChange;
  6158. }
  6159. bool JoystickInputMapper::hasValueChangedSignificantly(
  6160. float filter, float newValue, float currentValue, float min, float max) {
  6161. if (newValue != currentValue) {
  6162. // Filter out small changes in value unless the value is converging on the axis
  6163. // bounds or center point. This is intended to reduce the amount of information
  6164. // sent to applications by particularly noisy joysticks (such as PS3).
  6165. if (fabs(newValue - currentValue) > filter
  6166. || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
  6167. || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
  6168. || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
  6169. return true;
  6170. }
  6171. }
  6172. return false;
  6173. }
  6174. bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
  6175. float filter, float newValue, float currentValue, float thresholdValue) {
  6176. float newDistance = fabs(newValue - thresholdValue);
  6177. if (newDistance < filter) {
  6178. float oldDistance = fabs(currentValue - thresholdValue);
  6179. if (newDistance < oldDistance) {
  6180. return true;
  6181. }
  6182. }
  6183. return false;
  6184. }
  6185. } // namespace android