litest.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296
  1. /*
  2. * Copyright © 2013 Red Hat, Inc.
  3. * Copyright © 2013 Marcin Slusarz <marcin.slusarz@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include <dirent.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <fnmatch.h>
  29. #include <getopt.h>
  30. #include <libudev.h>
  31. #include <poll.h>
  32. #include <signal.h>
  33. #include <stdarg.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/ptrace.h>
  38. #include <sys/resource.h>
  39. #include <sys/stat.h>
  40. #include <sys/sysinfo.h>
  41. #include <sys/timerfd.h>
  42. #include <sys/types.h>
  43. #include <sys/wait.h>
  44. #include <time.h>
  45. #include <unistd.h>
  46. #include "linux/input.h"
  47. #ifdef HAVE_LIBSYSTEMD
  48. #include <systemd/sd-bus.h>
  49. #endif
  50. #ifdef __FreeBSD__
  51. #include <termios.h>
  52. #endif
  53. #include <libevdev/libevdev.h>
  54. #include <linux/kd.h>
  55. #include <valgrind/valgrind.h>
  56. #include "util-backtrace.h"
  57. #include "util-files.h"
  58. #include "util-libinput.h"
  59. #include "util-mem.h"
  60. #include "builddir.h"
  61. #include "libinput-util.h"
  62. #include "litest-int.h"
  63. #include "litest-runner.h"
  64. #include "litest.h"
  65. #include "quirks.h"
  66. #define evbit(t, c) ((t) << 16U | (c & 0xffff))
  67. #define UDEV_RULES_D "/run/udev/rules.d"
  68. #define UDEV_FUZZ_OVERRIDE_RULE_FILE \
  69. UDEV_RULES_D \
  70. "/91-litest-fuzz-override-REMOVEME-XXXXXX.rules"
  71. #define UDEV_TEST_DEVICE_RULE_FILE \
  72. UDEV_RULES_D \
  73. "/91-litest-test-device-REMOVEME-XXXXXXX.rules"
  74. #define UDEV_DEVICE_GROUPS_FILE \
  75. UDEV_RULES_D \
  76. "/80-libinput-device-groups-litest-XXXXXX.rules"
  77. bool verbose = false;
  78. bool in_debugger = false;
  79. bool run_deviceless = false;
  80. static bool use_system_rules_quirks = false;
  81. static bool exit_first = false;
  82. static FILE *outfile = NULL;
  83. static const char *filter_test = NULL;
  84. static const char *filter_device = NULL;
  85. static const char *filter_group = NULL;
  86. static int filter_rangeval = INT_MIN;
  87. bool use_colors = false;
  88. struct param_filter {
  89. char name[64];
  90. char glob[64];
  91. };
  92. struct param_filter filter_params[8]; /* name=NULL terminated */
  93. static struct quirks_context *quirks_context;
  94. struct created_file {
  95. struct list link;
  96. char *path;
  97. };
  98. static void
  99. created_file_destroy(struct created_file *f)
  100. {
  101. list_remove(&f->link);
  102. free(f->path);
  103. free(f);
  104. }
  105. static void
  106. created_file_unlink(struct created_file *f)
  107. {
  108. unlink(f->path);
  109. rmdir(f->path);
  110. }
  111. struct suite *current_suite = NULL;
  112. static void
  113. litest_init_udev_rules(struct list *created_files_list);
  114. static void
  115. litest_remove_udev_rules(struct list *created_files_list);
  116. static void
  117. litest_print_event(struct libinput_event *event, const char *message);
  118. enum quirks_setup_mode {
  119. QUIRKS_SETUP_USE_SRCDIR,
  120. QUIRKS_SETUP_ONLY_DEVICE,
  121. QUIRKS_SETUP_FULL,
  122. };
  123. static void
  124. litest_setup_quirks(struct list *created_files_list, enum quirks_setup_mode mode);
  125. /* defined for the litest selftest */
  126. #ifndef LITEST_DISABLE_BACKTRACE_LOGGING
  127. #define litest_log(...) fprintf(stderr, __VA_ARGS__)
  128. #define litest_vlog(format_, args_) vfprintf(stderr, format_, args_)
  129. #else
  130. #define litest_log(...) \
  131. { /* __VA_ARGS__ */ \
  132. }
  133. #define litest_vlog(...) \
  134. { /* __VA_ARGS__ */ \
  135. }
  136. #endif
  137. LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
  138. void
  139. _litest_checkpoint(const char *func,
  140. int line,
  141. const char *color,
  142. const char *format,
  143. ...)
  144. {
  145. char buf[1024];
  146. va_list args;
  147. va_start(args, format);
  148. if (verbose) {
  149. vsnprintf(buf, sizeof(buf), format, args);
  150. fprintf(stderr,
  151. "%s%s():%d - %s%s%s\n",
  152. use_colors ? ANSI_BRIGHT_BLUE : "",
  153. func,
  154. line,
  155. use_colors ? color : "",
  156. buf,
  157. use_colors ? ANSI_NORMAL : "");
  158. }
  159. va_end(args);
  160. }
  161. void
  162. litest_backtrace(const char *func)
  163. {
  164. #ifndef LITEST_DISABLE_BACKTRACE_LOGGING
  165. if (RUNNING_ON_VALGRIND) {
  166. fprintf(stderr, "Using valgrind, omitting backtrace\n");
  167. return;
  168. }
  169. char buf[256];
  170. snprintf(buf, sizeof(buf), "in %s", func);
  171. backtrace_print(stderr,
  172. use_colors,
  173. "in litest_backtrace",
  174. "in litest_runner_test_run",
  175. func ? buf : NULL);
  176. #endif
  177. }
  178. LIBINPUT_ATTRIBUTE_PRINTF(5, 6)
  179. __attribute__((noreturn)) void
  180. litest_fail_condition(const char *file,
  181. int line,
  182. const char *func,
  183. const char *condition,
  184. const char *message,
  185. ...)
  186. {
  187. litest_log("FAILED: %s\n", condition);
  188. if (message) {
  189. char buf[1024];
  190. va_list args;
  191. va_start(args, message);
  192. vsnprintf(buf, sizeof(buf), message, args);
  193. va_end(args);
  194. litest_log("%s\n", buf);
  195. }
  196. litest_log("in %s() (%s:%d)\n", func, file ? file : "???", line);
  197. litest_backtrace(func);
  198. litest_runner_abort();
  199. }
  200. __attribute__((noreturn)) void
  201. litest_fail_comparison_int(const char *file,
  202. int line,
  203. const char *func,
  204. const char *operator,
  205. int a,
  206. int b,
  207. const char *astr,
  208. const char *bstr)
  209. {
  210. litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
  211. litest_log("Resolved to: %d %s %d\n", a, operator, b);
  212. litest_log("in %s() (%s:%d)\n", func, file, line);
  213. litest_backtrace(func);
  214. litest_runner_abort();
  215. }
  216. __attribute__((noreturn)) void
  217. litest_fail_comparison_double(const char *file,
  218. int line,
  219. const char *func,
  220. const char *operator,
  221. double a,
  222. double b,
  223. const char *astr,
  224. const char *bstr)
  225. {
  226. litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
  227. litest_log("Resolved to: %.3f %s %.3f\n", a, operator, b);
  228. litest_log("in %s() (%s:%d)\n", func, file, line);
  229. litest_backtrace(func);
  230. litest_runner_abort();
  231. }
  232. __attribute__((noreturn)) void
  233. litest_fail_comparison_ptr(const char *file,
  234. int line,
  235. const char *func,
  236. const char *comparison)
  237. {
  238. litest_log("FAILED COMPARISON: %s\n", comparison);
  239. litest_log("in %s() (%s:%d)\n", func, file, line);
  240. litest_backtrace(func);
  241. litest_runner_abort();
  242. }
  243. __attribute__((noreturn)) void
  244. litest_fail_comparison_str(const char *file,
  245. int line,
  246. const char *func,
  247. const char *comparison,
  248. const char *operator,
  249. const char *astr,
  250. const char *bstr)
  251. {
  252. litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
  253. litest_log("Resolved to: %s %s %s\n", astr, operator, bstr);
  254. litest_log("in %s() (%s:%d)\n", func, file, line);
  255. litest_backtrace(func);
  256. litest_runner_abort();
  257. }
  258. struct litest_parameter_value {
  259. size_t refcnt;
  260. struct list link; /* litest_parameter->values */
  261. struct multivalue value;
  262. };
  263. struct litest_parameter {
  264. size_t refcnt;
  265. struct list link; /* litest_parameters.params */
  266. char name[128];
  267. char type; /* One of u, i, d, c, s, b */
  268. struct list values; /* litest_parameter_value */
  269. };
  270. struct litest_parameters {
  271. size_t refcnt;
  272. struct list params; /* struct litest_parameter */
  273. };
  274. static struct litest_parameter_value *
  275. litest_parameter_value_new(void)
  276. {
  277. struct litest_parameter_value *pv = zalloc(sizeof *pv);
  278. list_init(&pv->link);
  279. pv->refcnt = 1;
  280. return pv;
  281. }
  282. static inline void
  283. litest_parameter_add_string(struct litest_parameter *p, const char *s)
  284. {
  285. assert(p->type == 's');
  286. struct litest_parameter_value *pv = litest_parameter_value_new();
  287. pv->value = multivalue_new_string(s);
  288. list_append(&p->values, &pv->link);
  289. }
  290. static inline void
  291. litest_parameter_add_char(struct litest_parameter *p, char c)
  292. {
  293. assert(p->type == 'c');
  294. struct litest_parameter_value *pv = litest_parameter_value_new();
  295. pv->value = multivalue_new_char(c);
  296. list_append(&p->values, &pv->link);
  297. }
  298. static inline void
  299. litest_parameter_add_bool(struct litest_parameter *p, bool b)
  300. {
  301. assert(p->type == 'b');
  302. struct litest_parameter_value *pv = litest_parameter_value_new();
  303. pv->value = multivalue_new_bool(b);
  304. list_append(&p->values, &pv->link);
  305. }
  306. static inline void
  307. litest_parameter_add_u32(struct litest_parameter *p, uint32_t u)
  308. {
  309. assert(p->type == 'u');
  310. struct litest_parameter_value *pv = litest_parameter_value_new();
  311. pv->value = multivalue_new_u32(u);
  312. list_append(&p->values, &pv->link);
  313. }
  314. static inline void
  315. litest_parameter_add_i32(struct litest_parameter *p, int32_t i)
  316. {
  317. assert(p->type == 'i');
  318. struct litest_parameter_value *pv = litest_parameter_value_new();
  319. pv->value = multivalue_new_i32(i);
  320. list_append(&p->values, &pv->link);
  321. }
  322. static void
  323. litest_parameter_add_double(struct litest_parameter *p, double d)
  324. {
  325. assert(p->type == 'd');
  326. struct litest_parameter_value *pv = litest_parameter_value_new();
  327. pv->value = multivalue_new_double(d);
  328. list_append(&p->values, &pv->link);
  329. }
  330. static inline void
  331. litest_parameter_add_named_i32(struct litest_parameter *p,
  332. const struct litest_named_i32 i)
  333. {
  334. assert(p->type == 'I');
  335. struct litest_parameter_value *pv = litest_parameter_value_new();
  336. pv->value = multivalue_new_named_i32(i.value, i.name);
  337. list_append(&p->values, &pv->link);
  338. }
  339. #if 0
  340. static struct litest_parameter_value *
  341. litest_parameter_value_ref(struct litest_parameter_value *pv) {
  342. assert(pv);
  343. assert(pv->refcnt > 0);
  344. pv->refcnt++;
  345. return pv;
  346. }
  347. #endif
  348. static struct litest_parameter_value *
  349. litest_parameter_value_unref(struct litest_parameter_value *pv)
  350. {
  351. if (pv) {
  352. assert(pv->refcnt > 0);
  353. if (--pv->refcnt == 0) {
  354. list_remove(&pv->link);
  355. free(pv);
  356. }
  357. }
  358. return NULL;
  359. }
  360. static struct litest_parameter *
  361. litest_parameter_new(const char *name, char type)
  362. {
  363. struct litest_parameter *p = zalloc(sizeof *p);
  364. switch (type) {
  365. case 'b':
  366. case 'c':
  367. case 'd':
  368. case 'i':
  369. case 'I':
  370. case 's':
  371. case 'u':
  372. break;
  373. default:
  374. assert(!"Type not yet implemented");
  375. }
  376. list_init(&p->link);
  377. list_init(&p->values);
  378. snprintf(p->name, sizeof(p->name), "%s", name);
  379. p->type = type;
  380. p->refcnt = 1;
  381. return p;
  382. }
  383. static struct litest_parameter *
  384. litest_parameter_ref(struct litest_parameter *p)
  385. {
  386. assert(p);
  387. assert(p->refcnt > 0);
  388. p->refcnt++;
  389. return p;
  390. }
  391. static struct litest_parameter *
  392. litest_parameter_unref(struct litest_parameter *p)
  393. {
  394. if (p) {
  395. assert(p->refcnt > 0);
  396. if (--p->refcnt == 0) {
  397. struct litest_parameter_value *pv;
  398. list_for_each_safe(pv, &p->values, link) {
  399. litest_parameter_value_unref(pv);
  400. }
  401. list_remove(&p->link);
  402. free(p);
  403. }
  404. }
  405. return NULL;
  406. }
  407. DEFINE_UNREF_CLEANUP_FUNC(litest_parameter);
  408. static void
  409. litest_parameters_add(struct litest_parameters *ps, struct litest_parameter *param)
  410. {
  411. struct litest_parameter *p;
  412. list_for_each(p, &ps->params, link) {
  413. assert(!streq(p->name, param->name));
  414. }
  415. litest_parameter_ref(param);
  416. list_append(&ps->params, &param->link);
  417. }
  418. struct litest_parameters *
  419. _litest_parameters_new(const char *name, ...)
  420. {
  421. struct litest_parameters *ps = zalloc(sizeof *ps);
  422. list_init(&ps->params);
  423. ps->refcnt = 1;
  424. va_list args;
  425. va_start(args, name);
  426. while (name) {
  427. char type = va_arg(args, int);
  428. _unref_(litest_parameter) *param = litest_parameter_new(name, type);
  429. if (type == 'b') {
  430. litest_parameter_add_bool(param, true);
  431. litest_parameter_add_bool(param, false);
  432. } else {
  433. unsigned int nargs = va_arg(args, unsigned int);
  434. for (unsigned int _ = 0; _ < nargs; _++) {
  435. switch (type) {
  436. case 'c': {
  437. char b = va_arg(args, int);
  438. litest_parameter_add_char(param, b);
  439. break;
  440. }
  441. case 'u': {
  442. uint32_t b = va_arg(args, uint32_t);
  443. litest_parameter_add_u32(param, b);
  444. break;
  445. }
  446. case 'i': {
  447. int32_t b = va_arg(args, int32_t);
  448. litest_parameter_add_i32(param, b);
  449. break;
  450. }
  451. case 'd': {
  452. double b = va_arg(args, double);
  453. litest_parameter_add_double(param, b);
  454. break;
  455. }
  456. case 's': {
  457. const char *s = va_arg(args, const char *);
  458. litest_parameter_add_string(param, s);
  459. break;
  460. }
  461. case 'I': {
  462. struct litest_named_i32 p =
  463. va_arg(args, struct litest_named_i32);
  464. litest_parameter_add_named_i32(param, p);
  465. break;
  466. }
  467. default:
  468. litest_abort_msg(
  469. "Unhandled parameter type '%c'",
  470. type);
  471. break;
  472. }
  473. }
  474. }
  475. litest_parameters_add(ps, param);
  476. name = va_arg(args, const char *);
  477. }
  478. va_end(args);
  479. return ps;
  480. }
  481. struct litest_parameters *
  482. litest_parameters_ref(struct litest_parameters *p)
  483. {
  484. assert(p);
  485. assert(p->refcnt > 0);
  486. p->refcnt++;
  487. return p;
  488. }
  489. struct litest_parameters *
  490. litest_parameters_unref(struct litest_parameters *params)
  491. {
  492. if (params) {
  493. assert(params->refcnt > 0);
  494. if (--params->refcnt == 0) {
  495. struct litest_parameter *p;
  496. list_for_each_safe(p, &params->params, link) {
  497. litest_parameter_unref(p);
  498. }
  499. free(params);
  500. }
  501. }
  502. return NULL;
  503. }
  504. static inline int
  505. _permutate(struct litest_parameters_permutation *permutation,
  506. struct list *next_param,
  507. void *list_head,
  508. litest_parameters_permutation_func_t func,
  509. void *userdata)
  510. {
  511. if (next_param->next == list_head) {
  512. func(permutation, userdata);
  513. return 0;
  514. }
  515. struct litest_parameter_value *pv;
  516. struct litest_parameter *param = list_first_entry(next_param, param, link);
  517. list_for_each(pv, &param->values, link) {
  518. struct litest_parameters_permutation_value v = {
  519. .value = pv->value,
  520. };
  521. memcpy(v.name, param->name, min(sizeof(v.name), sizeof(param->name)));
  522. list_append(&permutation->values, &v.link);
  523. int rc = _permutate(permutation,
  524. &param->link,
  525. list_head,
  526. func,
  527. userdata);
  528. if (rc)
  529. return rc;
  530. list_remove(&v.link);
  531. }
  532. return 0;
  533. }
  534. /**
  535. * Calls the given function func with each permutation of
  536. * the given test parameters.
  537. */
  538. int
  539. litest_parameters_permutations(struct litest_parameters *params,
  540. litest_parameters_permutation_func_t func,
  541. void *userdata)
  542. {
  543. struct litest_parameters_permutation permutation;
  544. list_init(&permutation.values);
  545. return _permutate(&permutation,
  546. &params->params,
  547. &params->params,
  548. func,
  549. userdata);
  550. }
  551. static struct litest_device *current_device;
  552. struct litest_device *
  553. litest_current_device(void)
  554. {
  555. return current_device;
  556. }
  557. int
  558. _litest_dispatch(struct libinput *li, const char *func, int line)
  559. {
  560. static int dispatch_counter = 0;
  561. ++dispatch_counter;
  562. _litest_checkpoint(func,
  563. line,
  564. ANSI_MAGENTA,
  565. "┌──────────────────── dispatch %3d ────────────────────┐",
  566. dispatch_counter);
  567. int rc = libinput_dispatch(li);
  568. enum libinput_event_type type = libinput_next_event_type(li);
  569. const char *evtype =
  570. type == LIBINPUT_EVENT_NONE ? "NONE" : litest_event_type_str(type);
  571. _litest_checkpoint(
  572. func,
  573. line,
  574. ANSI_MAGENTA,
  575. "└──────────────────── /dispatch %3d ────────────────────┘ pending %s",
  576. dispatch_counter,
  577. evtype);
  578. return rc;
  579. }
  580. static void
  581. grab_device(struct litest_device *device, bool mode)
  582. {
  583. struct libinput *li = libinput_device_get_context(device->libinput_device);
  584. struct litest_context *ctx = libinput_get_user_data(li);
  585. _unref_(udev_device) * udev_device;
  586. const char *devnode;
  587. struct path *p;
  588. udev_device = libinput_device_get_udev_device(device->libinput_device);
  589. litest_assert_ptr_notnull(udev_device);
  590. devnode = udev_device_get_devnode(udev_device);
  591. /* Note: in some tests we create multiple devices for the same path.
  592. * This will only grab the first device in the list but we're using
  593. * list_insert() so the first device is the latest that was
  594. * initialized, so we should be good.
  595. */
  596. list_for_each(p, &ctx->paths, link) {
  597. if (streq(p->path, devnode)) {
  598. int rc = ioctl(p->fd, EVIOCGRAB, (void *)mode ? 1 : 0);
  599. litest_assert_errno_success(rc);
  600. return;
  601. }
  602. }
  603. litest_abort_msg("Failed to find device %s to %sgrab",
  604. devnode,
  605. mode ? "" : "un");
  606. }
  607. void
  608. litest_grab_device(struct litest_device *device)
  609. {
  610. grab_device(device, true);
  611. }
  612. void
  613. litest_ungrab_device(struct litest_device *device)
  614. {
  615. grab_device(device, false);
  616. }
  617. void
  618. litest_set_current_device(struct litest_device *device)
  619. {
  620. current_device = device;
  621. }
  622. void
  623. litest_generic_device_teardown(void)
  624. {
  625. litest_device_destroy(current_device);
  626. current_device = NULL;
  627. }
  628. static struct list devices = LIST_INIT(devices); /* struct litest_test_device */
  629. void
  630. litest_add_test_device(struct list *device)
  631. {
  632. list_append(&devices, device);
  633. }
  634. static inline void
  635. litest_system(const char *command)
  636. {
  637. int ret;
  638. ret = system(command);
  639. if (ret == -1) {
  640. litest_abort_msg("Failed to execute: %s", command);
  641. } else if (WIFEXITED(ret)) {
  642. if (WEXITSTATUS(ret))
  643. litest_abort_msg("'%s' failed with %d",
  644. command,
  645. WEXITSTATUS(ret));
  646. } else if (WIFSIGNALED(ret)) {
  647. litest_abort_msg("'%s' terminated with signal %d",
  648. command,
  649. WTERMSIG(ret));
  650. }
  651. }
  652. static void
  653. litest_reload_udev_rules(void)
  654. {
  655. litest_system("udevadm control --reload-rules");
  656. }
  657. static bool
  658. filter_for_rangeval(const struct range *range, int rangeval)
  659. {
  660. return !range || filter_rangeval == INT_MIN || filter_rangeval == rangeval;
  661. }
  662. static void
  663. litest_add_tcase_for_device(struct suite *suite,
  664. const char *funcname,
  665. const void *func,
  666. const struct litest_test_device *dev,
  667. const struct range *range)
  668. {
  669. const struct range no_range = range_init_empty();
  670. if (run_deviceless)
  671. return;
  672. if (!range)
  673. range = &no_range;
  674. int rangeval = range->lower;
  675. do {
  676. if (filter_for_rangeval(range, rangeval)) {
  677. struct test *t;
  678. t = zalloc(sizeof(*t));
  679. t->name = safe_strdup(funcname);
  680. t->devname = safe_strdup(dev->shortname);
  681. t->func = func;
  682. t->setup = dev->setup;
  683. t->teardown = dev->teardown ? dev->teardown
  684. : litest_generic_device_teardown;
  685. if (range)
  686. t->range = *range;
  687. t->rangeval = rangeval;
  688. list_append(&suite->tests, &t->node);
  689. }
  690. } while (++rangeval < range->upper);
  691. }
  692. struct permutation_userdata {
  693. struct suite *suite;
  694. const char *funcname;
  695. const void *func;
  696. const struct litest_test_device *dev;
  697. char devname[64]; /* set if dev == NULL */
  698. const struct param_filter *param_filters; /* name=NULL terminated */
  699. };
  700. static int
  701. permutation_func(struct litest_parameters_permutation *permutation, void *userdata)
  702. {
  703. struct permutation_userdata *data = userdata;
  704. struct litest_test_parameters *params = litest_test_parameters_new();
  705. struct litest_parameters_permutation_value *pmv;
  706. bool filtered = false;
  707. list_for_each(pmv, &permutation->values, link) {
  708. const struct param_filter *f = data->param_filters;
  709. while (!filtered && strlen(f->name)) {
  710. if (streq(pmv->name, f->name)) {
  711. _autofree_ char *s = multivalue_as_str(&pmv->value);
  712. if (fnmatch(f->glob, s, 0) != 0)
  713. filtered = true;
  714. }
  715. f++;
  716. }
  717. if (filtered)
  718. break;
  719. struct litest_test_param *tp = zalloc(sizeof *tp);
  720. snprintf(tp->name, sizeof(tp->name), "%s", pmv->name);
  721. tp->value = multivalue_copy(&pmv->value);
  722. list_append(&params->test_params, &tp->link);
  723. }
  724. if (filtered) {
  725. litest_test_parameters_unref(params);
  726. return 0;
  727. }
  728. struct test *t;
  729. t = zalloc(sizeof(*t));
  730. t->name = safe_strdup(data->funcname);
  731. t->func = data->func;
  732. if (data->dev) {
  733. t->devname = safe_strdup(data->dev->shortname);
  734. t->setup = data->dev->setup;
  735. t->teardown = data->dev->teardown ? data->dev->teardown
  736. : litest_generic_device_teardown;
  737. } else {
  738. t->devname = safe_strdup(data->devname);
  739. t->setup = NULL;
  740. t->teardown = NULL;
  741. }
  742. t->rangeval = 0;
  743. t->params = params;
  744. list_append(&data->suite->tests, &t->node);
  745. return 0;
  746. }
  747. static void
  748. litest_add_tcase_for_device_with_params(struct suite *suite,
  749. const char *funcname,
  750. const void *func,
  751. const struct litest_test_device *dev,
  752. struct litest_parameters *params)
  753. {
  754. if (run_deviceless)
  755. return;
  756. struct permutation_userdata data = {
  757. .suite = suite,
  758. .funcname = funcname,
  759. .func = func,
  760. .dev = dev,
  761. .param_filters = filter_params,
  762. };
  763. litest_parameters_permutations(params, permutation_func, &data);
  764. }
  765. static void
  766. litest_add_tcase_no_device(struct suite *suite,
  767. const void *func,
  768. const char *funcname,
  769. const struct range *range)
  770. {
  771. const char *test_name = funcname;
  772. const struct range no_range = range_init_empty();
  773. if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
  774. return;
  775. if (run_deviceless)
  776. return;
  777. if (!range)
  778. range = &no_range;
  779. int rangeval = range->lower;
  780. do {
  781. if (filter_for_rangeval(range, rangeval)) {
  782. struct test *t;
  783. t = zalloc(sizeof(*t));
  784. t->name = safe_strdup(test_name);
  785. t->devname = safe_strdup("no device");
  786. t->func = func;
  787. if (range)
  788. t->range = *range;
  789. t->rangeval = rangeval;
  790. t->setup = NULL;
  791. t->teardown = NULL;
  792. list_append(&suite->tests, &t->node);
  793. }
  794. } while (++rangeval < range->upper);
  795. }
  796. static void
  797. litest_add_tcase_no_device_with_params(struct suite *suite,
  798. const void *func,
  799. const char *funcname,
  800. struct litest_parameters *params)
  801. {
  802. const char *test_name = funcname;
  803. if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
  804. return;
  805. if (run_deviceless)
  806. return;
  807. struct permutation_userdata data = {
  808. .suite = suite,
  809. .funcname = funcname,
  810. .func = func,
  811. .param_filters = filter_params,
  812. };
  813. snprintf(data.devname, sizeof(data.devname), "no device");
  814. litest_parameters_permutations(params, permutation_func, &data);
  815. }
  816. static void
  817. litest_add_tcase_deviceless(struct suite *suite,
  818. const void *func,
  819. const char *funcname,
  820. const struct range *range)
  821. {
  822. const char *test_name = funcname;
  823. const struct range no_range = range_init_empty();
  824. if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
  825. return;
  826. if (!range)
  827. range = &no_range;
  828. int rangeval = range->lower;
  829. do {
  830. if (filter_for_rangeval(range, rangeval)) {
  831. struct test *t;
  832. t = zalloc(sizeof(*t));
  833. t->deviceless = true;
  834. t->name = safe_strdup(test_name);
  835. t->devname = safe_strdup("deviceless");
  836. t->func = func;
  837. if (range)
  838. t->range = *range;
  839. t->rangeval = rangeval;
  840. t->setup = NULL;
  841. t->teardown = NULL;
  842. list_append(&suite->tests, &t->node);
  843. }
  844. } while (++rangeval < range->upper);
  845. }
  846. static void
  847. litest_add_tcase_deviceless_with_params(struct suite *suite,
  848. const void *func,
  849. const char *funcname,
  850. struct litest_parameters *params)
  851. {
  852. const char *test_name = funcname;
  853. if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
  854. return;
  855. struct permutation_userdata data = {
  856. .suite = suite,
  857. .funcname = funcname,
  858. .func = func,
  859. .param_filters = filter_params,
  860. };
  861. snprintf(data.devname, sizeof(data.devname), "deviceless");
  862. litest_parameters_permutations(params, permutation_func, &data);
  863. }
  864. static void
  865. litest_add_tcase(const char *filename,
  866. const char *funcname,
  867. const void *func,
  868. int64_t required,
  869. int64_t excluded,
  870. const struct range *range,
  871. struct litest_parameters *params)
  872. {
  873. bool added = false;
  874. litest_assert(required >= LITEST_DEVICELESS);
  875. litest_assert(excluded >= LITEST_DEVICELESS);
  876. if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
  877. return;
  878. struct suite *suite = current_suite;
  879. if (filter_group && fnmatch(filter_group, suite->name, 0) != 0)
  880. return;
  881. if (required == LITEST_DEVICELESS && excluded == LITEST_DEVICELESS) {
  882. if (params)
  883. litest_add_tcase_deviceless_with_params(suite,
  884. func,
  885. funcname,
  886. params);
  887. else
  888. litest_add_tcase_deviceless(suite, func, funcname, range);
  889. added = true;
  890. } else if (required == LITEST_DISABLE_DEVICE &&
  891. excluded == LITEST_DISABLE_DEVICE) {
  892. if (params)
  893. litest_add_tcase_no_device_with_params(suite,
  894. func,
  895. funcname,
  896. params);
  897. else
  898. litest_add_tcase_no_device(suite, func, funcname, range);
  899. added = true;
  900. } else if (required != LITEST_ANY || excluded != LITEST_ANY) {
  901. struct litest_test_device *dev;
  902. list_for_each(dev, &devices, node) {
  903. if (dev->features & LITEST_IGNORED)
  904. continue;
  905. if (filter_device &&
  906. fnmatch(filter_device, dev->shortname, 0) != 0)
  907. continue;
  908. if ((dev->features & required) != required ||
  909. (dev->features & excluded) != 0)
  910. continue;
  911. if (params) {
  912. litest_add_tcase_for_device_with_params(suite,
  913. funcname,
  914. func,
  915. dev,
  916. params);
  917. } else {
  918. litest_add_tcase_for_device(suite,
  919. funcname,
  920. func,
  921. dev,
  922. range);
  923. }
  924. added = true;
  925. }
  926. } else {
  927. struct litest_test_device *dev;
  928. list_for_each(dev, &devices, node) {
  929. if (dev->features & LITEST_IGNORED)
  930. continue;
  931. if (filter_device &&
  932. fnmatch(filter_device, dev->shortname, 0) != 0)
  933. continue;
  934. if (params) {
  935. litest_add_tcase_for_device_with_params(suite,
  936. funcname,
  937. func,
  938. dev,
  939. params);
  940. } else {
  941. litest_add_tcase_for_device(suite,
  942. funcname,
  943. func,
  944. dev,
  945. range);
  946. }
  947. added = true;
  948. }
  949. }
  950. if (!added && filter_test == NULL && filter_device == NULL &&
  951. filter_group == NULL) {
  952. fprintf(stderr,
  953. "Test '%s' does not match any devices. Aborting.\n",
  954. funcname);
  955. abort();
  956. }
  957. }
  958. void
  959. _litest_add_no_device(const char *name, const char *funcname, const void *func)
  960. {
  961. _litest_add(name, funcname, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
  962. }
  963. void
  964. _litest_add_parametrized_no_device(const char *name,
  965. const char *funcname,
  966. const void *func,
  967. struct litest_parameters *params)
  968. {
  969. _litest_add_parametrized(name,
  970. funcname,
  971. func,
  972. LITEST_DISABLE_DEVICE,
  973. LITEST_DISABLE_DEVICE,
  974. params);
  975. }
  976. void
  977. _litest_add_ranged_no_device(const char *name,
  978. const char *funcname,
  979. const void *func,
  980. const struct range *range)
  981. {
  982. _litest_add_ranged(name,
  983. funcname,
  984. func,
  985. LITEST_DISABLE_DEVICE,
  986. LITEST_DISABLE_DEVICE,
  987. range);
  988. }
  989. void
  990. _litest_add_deviceless(const char *name, const char *funcname, const void *func)
  991. {
  992. _litest_add_ranged(name,
  993. funcname,
  994. func,
  995. LITEST_DEVICELESS,
  996. LITEST_DEVICELESS,
  997. NULL);
  998. }
  999. void
  1000. _litest_add_parametrized_deviceless(const char *name,
  1001. const char *funcname,
  1002. const void *func,
  1003. struct litest_parameters *params)
  1004. {
  1005. _litest_add_parametrized(name,
  1006. funcname,
  1007. func,
  1008. LITEST_DISABLE_DEVICE,
  1009. LITEST_DISABLE_DEVICE,
  1010. params);
  1011. }
  1012. void
  1013. _litest_add(const char *name,
  1014. const char *funcname,
  1015. const void *func,
  1016. int64_t required,
  1017. int64_t excluded)
  1018. {
  1019. _litest_add_ranged(name, funcname, func, required, excluded, NULL);
  1020. }
  1021. void
  1022. _litest_add_ranged(const char *name,
  1023. const char *funcname,
  1024. const void *func,
  1025. int64_t required,
  1026. int64_t excluded,
  1027. const struct range *range)
  1028. {
  1029. litest_add_tcase(name, funcname, func, required, excluded, range, NULL);
  1030. }
  1031. void
  1032. _litest_add_parametrized(const char *name,
  1033. const char *funcname,
  1034. const void *func,
  1035. int64_t required,
  1036. int64_t excluded,
  1037. struct litest_parameters *params)
  1038. {
  1039. litest_add_tcase(name, funcname, func, required, excluded, NULL, params);
  1040. }
  1041. void
  1042. _litest_add_for_device(const char *name,
  1043. const char *funcname,
  1044. const void *func,
  1045. enum litest_device_type type)
  1046. {
  1047. _litest_add_ranged_for_device(name, funcname, func, type, NULL);
  1048. }
  1049. void
  1050. _litest_add_ranged_for_device(const char *filename,
  1051. const char *funcname,
  1052. const void *func,
  1053. enum litest_device_type type,
  1054. const struct range *range)
  1055. {
  1056. struct litest_test_device *dev;
  1057. bool device_filtered = false;
  1058. litest_assert(type < LITEST_NO_DEVICE);
  1059. if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
  1060. return;
  1061. struct suite *s = current_suite;
  1062. if (filter_group && fnmatch(filter_group, s->name, 0) != 0)
  1063. return;
  1064. list_for_each(dev, &devices, node) {
  1065. if (filter_device && fnmatch(filter_device, dev->shortname, 0) != 0) {
  1066. device_filtered = true;
  1067. continue;
  1068. }
  1069. if (dev->type == type) {
  1070. litest_add_tcase_for_device(s, funcname, func, dev, range);
  1071. return;
  1072. }
  1073. }
  1074. /* only abort if no filter was set, that's a bug */
  1075. if (!device_filtered)
  1076. litest_abort_msg("Invalid test device type");
  1077. }
  1078. void
  1079. _litest_add_parametrized_for_device(const char *filename,
  1080. const char *funcname,
  1081. const void *func,
  1082. enum litest_device_type type,
  1083. struct litest_parameters *params)
  1084. {
  1085. struct litest_test_device *dev;
  1086. bool device_filtered = false;
  1087. litest_assert(type < LITEST_NO_DEVICE);
  1088. if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
  1089. return;
  1090. struct suite *s = current_suite;
  1091. if (filter_group && fnmatch(filter_group, s->name, 0) != 0)
  1092. return;
  1093. list_for_each(dev, &devices, node) {
  1094. if (filter_device && fnmatch(filter_device, dev->shortname, 0) != 0) {
  1095. device_filtered = true;
  1096. continue;
  1097. }
  1098. if (dev->type == type) {
  1099. litest_add_tcase_for_device_with_params(s,
  1100. funcname,
  1101. func,
  1102. dev,
  1103. params);
  1104. return;
  1105. }
  1106. }
  1107. /* only abort if no filter was set, that's a bug */
  1108. if (!device_filtered)
  1109. litest_abort_msg("Invalid test device type");
  1110. }
  1111. static int
  1112. is_actual_error(const char *error, size_t index, void *data)
  1113. {
  1114. /* We don't want to abort on these errors */
  1115. if (((RUNNING_ON_VALGRIND || in_debugger) &&
  1116. strstr(error, "scheduled expiry is in the past")) ||
  1117. strstr(error, "event processing lagging behind")) {
  1118. return 0;
  1119. }
  1120. return 1;
  1121. }
  1122. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  1123. static void
  1124. litest_log_handler(struct libinput *libinput,
  1125. enum libinput_log_priority pri,
  1126. const char *format,
  1127. va_list args)
  1128. {
  1129. const char *priority = NULL;
  1130. const char *color = ""; // NOLINT: deadcode.DeadStores
  1131. const char *color_reset = ANSI_NORMAL;
  1132. switch (pri) {
  1133. case LIBINPUT_LOG_PRIORITY_INFO:
  1134. priority = "info ";
  1135. color = ANSI_BOLD;
  1136. break;
  1137. case LIBINPUT_LOG_PRIORITY_ERROR:
  1138. priority = "error";
  1139. color = ANSI_BRIGHT_RED;
  1140. break;
  1141. case LIBINPUT_LOG_PRIORITY_DEBUG:
  1142. priority = "debug";
  1143. color = ANSI_NORMAL;
  1144. break;
  1145. default:
  1146. abort();
  1147. }
  1148. _autofree_ char *msg = strdup_vprintf(format, args);
  1149. if (!use_colors)
  1150. color_reset = "";
  1151. else if (strstr(format, "tap:"))
  1152. color = ANSI_BLUE;
  1153. else if (strstr(format, "thumb state:"))
  1154. color = ANSI_YELLOW;
  1155. else if (strstr(format, "button state:"))
  1156. color = ANSI_MAGENTA;
  1157. else if (strstr(format, "touch-size:") || strstr(format, "pressure:"))
  1158. color = ANSI_GREEN;
  1159. else if (strstr(format, "palm:") || strstr(format, "thumb:"))
  1160. color = ANSI_CYAN;
  1161. else if (strstr(format, "edge-scroll:"))
  1162. color = ANSI_BRIGHT_GREEN;
  1163. else if (strstr(format, "gesture:"))
  1164. color = ANSI_YELLOW;
  1165. else if (strstr(msg, "Plugin:"))
  1166. color = ANSI_BRIGHT_CYAN;
  1167. fprintf(stderr, "%slitest %s %s%s", color, priority, msg, color_reset);
  1168. if (strstr(msg, "client bug: ") || strstr(msg, "libinput bug: ") ||
  1169. strstr(msg, "plugin bug: ")) {
  1170. /* valgrind is too slow and some of our offsets are too
  1171. * short, don't abort if during a valgrind run we get a
  1172. * negative offset */
  1173. if ((RUNNING_ON_VALGRIND || in_debugger) &&
  1174. strstr(format, "scheduled expiry is in the past")) {
  1175. /* noop */
  1176. } else if (strstr(format, "event processing lagging behind")) {
  1177. /* noop */
  1178. } else {
  1179. litest_abort_msg("libinput bug triggered, aborting.");
  1180. }
  1181. }
  1182. if (strstr(msg, "Touch jump detected and discarded")) {
  1183. litest_abort_msg("libinput touch jump triggered, aborting.");
  1184. }
  1185. }
  1186. static void
  1187. litest_init_device_udev_rules(struct litest_test_device *dev, FILE *f)
  1188. {
  1189. const struct key_value_str *kv;
  1190. static int count;
  1191. bool need_keyboard_builtin = false;
  1192. if (dev->udev_properties[0].key == NULL)
  1193. return;
  1194. count++;
  1195. fprintf(f, "# %s\n", dev->shortname);
  1196. fprintf(f, "ACTION==\"remove\", GOTO=\"rule%d_end\"\n", count);
  1197. fprintf(f, "KERNEL!=\"event*\", GOTO=\"rule%d_end\"\n", count);
  1198. fprintf(f, "ATTRS{name}==\"litest %s*\"", dev->name);
  1199. kv = dev->udev_properties;
  1200. while (kv->key) {
  1201. fprintf(f, ", \\\n\tENV{%s}=\"%s\"", kv->key, kv->value);
  1202. if (strstartswith(kv->key, "EVDEV_ABS_"))
  1203. need_keyboard_builtin = true;
  1204. kv++;
  1205. }
  1206. fprintf(f, "\n");
  1207. /* Special case: the udev keyboard builtin is only run for hwdb
  1208. * matches but we don't set any up in litest. So instead scan the
  1209. * device's udev properties for any EVDEV_ABS properties and where
  1210. * they exist, force a (re-)run of the keyboard builtin to set up
  1211. * the evdev device correctly.
  1212. * This needs to be done as separate rule apparently, otherwise the
  1213. * ENV variables aren't set yet by the time the builtin runs.
  1214. */
  1215. if (need_keyboard_builtin) {
  1216. fprintf(f,
  1217. ""
  1218. "ATTRS{name}==\"litest %s*\","
  1219. " IMPORT{builtin}=\"keyboard\"\n",
  1220. dev->name);
  1221. }
  1222. fprintf(f, "LABEL=\"rule%d_end\"\n\n", count);
  1223. ;
  1224. }
  1225. static void
  1226. litest_init_all_device_udev_rules(struct list *created_files)
  1227. {
  1228. struct created_file *file = zalloc(sizeof(*file));
  1229. struct litest_test_device *dev;
  1230. FILE *f;
  1231. int fd;
  1232. char *path = strdup_printf("%s/99-litest-XXXXXX.rules", UDEV_RULES_D);
  1233. litest_assert_ptr_notnull(path);
  1234. fd = mkstemps(path, 6);
  1235. litest_assert_errno_success(fd);
  1236. f = fdopen(fd, "w");
  1237. litest_assert_notnull(f);
  1238. list_for_each(dev, &devices, node)
  1239. litest_init_device_udev_rules(dev, f);
  1240. fclose(f);
  1241. file->path = path;
  1242. list_insert(created_files, &file->link);
  1243. }
  1244. static int
  1245. open_restricted(const char *path, int flags, void *userdata)
  1246. {
  1247. const char prefix[] = "/dev/input/event";
  1248. struct litest_context *ctx = userdata;
  1249. struct path *p;
  1250. int fd;
  1251. litest_assert_ptr_notnull(ctx);
  1252. fd = open(path, flags);
  1253. if (fd < 0)
  1254. return -errno;
  1255. if (strstartswith(path, prefix)) {
  1256. p = zalloc(sizeof *p);
  1257. p->path = safe_strdup(path);
  1258. p->fd = fd;
  1259. /* We specifically insert here so that the most-recently
  1260. * opened path is the first one in the list. This helps when
  1261. * we have multiple test devices with the same device path,
  1262. * the fd of the most recent device is the first one to get
  1263. * grabbed
  1264. */
  1265. list_insert(&ctx->paths, &p->link);
  1266. }
  1267. return fd;
  1268. }
  1269. static void
  1270. close_restricted(int fd, void *userdata)
  1271. {
  1272. struct litest_context *ctx = userdata;
  1273. struct path *p;
  1274. list_for_each_safe(p, &ctx->paths, link) {
  1275. if (p->fd != fd)
  1276. continue;
  1277. list_remove(&p->link);
  1278. free(p->path);
  1279. free(p);
  1280. }
  1281. close(fd);
  1282. }
  1283. static struct libinput_interface interface = {
  1284. .open_restricted = open_restricted,
  1285. .close_restricted = close_restricted,
  1286. };
  1287. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  1288. static inline void
  1289. quirk_log_handler(struct libinput *unused,
  1290. enum libinput_log_priority priority,
  1291. const char *format,
  1292. va_list args)
  1293. {
  1294. if (priority < LIBINPUT_LOG_PRIORITY_ERROR)
  1295. return;
  1296. vfprintf(stderr, format, args);
  1297. }
  1298. static enum litest_runner_result
  1299. init_quirks(void *userdata)
  1300. {
  1301. const char *data_path = getenv("LIBINPUT_QUIRKS_DIR");
  1302. if (!data_path)
  1303. data_path = LIBINPUT_QUIRKS_DIR;
  1304. quirks_context = quirks_init_subsystem(data_path,
  1305. NULL,
  1306. quirk_log_handler,
  1307. NULL,
  1308. QLOG_LIBINPUT_LOGGING);
  1309. return LITEST_PASS;
  1310. }
  1311. static void
  1312. teardown_quirks(void *userdata)
  1313. {
  1314. quirks_context_unref(quirks_context);
  1315. }
  1316. static int
  1317. litest_run_suite(struct list *suites, int njobs)
  1318. {
  1319. size_t ntests = 0;
  1320. enum litest_runner_result result = LITEST_SKIP;
  1321. struct suite *s;
  1322. _destroy_(litest_runner) *runner = litest_runner_new();
  1323. litest_runner_set_num_parallel(runner, njobs > 0 ? njobs : 0);
  1324. if (outfile)
  1325. litest_runner_set_output_file(runner, outfile);
  1326. litest_runner_set_verbose(runner, verbose);
  1327. litest_runner_set_use_colors(runner, use_colors);
  1328. litest_runner_set_timeout(runner, 30);
  1329. litest_runner_set_exit_on_fail(runner, exit_first);
  1330. litest_runner_set_setup_funcs(runner, init_quirks, teardown_quirks, NULL);
  1331. list_for_each(s, suites, node) {
  1332. struct test *t;
  1333. list_for_each(t, &s->tests, node) {
  1334. struct litest_runner_test_description tdesc = { 0 };
  1335. if (range_is_valid(&t->range)) {
  1336. snprintf(tdesc.name,
  1337. sizeof(tdesc.name),
  1338. "%s:%s:%s:%d",
  1339. s->name,
  1340. t->name,
  1341. t->devname,
  1342. t->rangeval);
  1343. } else if (t->params) {
  1344. char buf[256] = { 0 };
  1345. struct litest_test_param *tp;
  1346. bool is_first = true;
  1347. list_for_each(tp, &t->params->test_params, link) {
  1348. _autofree_ char *val =
  1349. multivalue_as_str(&tp->value);
  1350. snprintf(buf + strlen(buf),
  1351. sizeof(buf) - strlen(buf),
  1352. "%s%s:%s",
  1353. is_first ? "" : ",",
  1354. tp->name,
  1355. val);
  1356. is_first = false;
  1357. }
  1358. snprintf(tdesc.name,
  1359. sizeof(tdesc.name),
  1360. "%s:%s:%s:%s",
  1361. s->name,
  1362. t->name,
  1363. t->devname,
  1364. buf);
  1365. } else {
  1366. snprintf(tdesc.name,
  1367. sizeof(tdesc.name),
  1368. "%s:%s:%s",
  1369. s->name,
  1370. t->name,
  1371. t->devname);
  1372. }
  1373. tdesc.func = t->func;
  1374. tdesc.setup = t->setup;
  1375. tdesc.teardown = t->teardown;
  1376. tdesc.args.range = t->range;
  1377. tdesc.rangeval = t->rangeval;
  1378. tdesc.params = t->params;
  1379. litest_runner_add_test(runner, &tdesc);
  1380. ntests++;
  1381. }
  1382. }
  1383. if (ntests > 0)
  1384. result = litest_runner_run_tests(runner);
  1385. return result;
  1386. }
  1387. static inline int
  1388. inhibit(void)
  1389. {
  1390. int lock_fd = -1;
  1391. #ifdef HAVE_LIBSYSTEMD
  1392. _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
  1393. _unref_(sd_bus_message) *m = NULL;
  1394. _unref_(sd_bus) *bus = NULL;
  1395. int rc;
  1396. if (run_deviceless)
  1397. return -1;
  1398. rc = sd_bus_open_system(&bus);
  1399. if (rc != 0) {
  1400. fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
  1401. goto out;
  1402. }
  1403. rc = sd_bus_call_method(
  1404. bus,
  1405. "org.freedesktop.login1",
  1406. "/org/freedesktop/login1",
  1407. "org.freedesktop.login1.Manager",
  1408. "Inhibit",
  1409. &error,
  1410. &m,
  1411. "ssss",
  1412. "sleep:shutdown:handle-lid-switch:handle-power-key:handle-suspend-key:"
  1413. "handle-hibernate-key",
  1414. "libinput test-suite runner",
  1415. "testing in progress",
  1416. "block");
  1417. if (rc < 0) {
  1418. fprintf(stderr, "Warning: inhibit failed: %s\n", error.message);
  1419. goto out;
  1420. }
  1421. rc = sd_bus_message_read(m, "h", &lock_fd);
  1422. if (rc < 0) {
  1423. fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
  1424. goto out;
  1425. }
  1426. lock_fd = dup(lock_fd);
  1427. out:
  1428. sd_bus_close(bus);
  1429. #endif
  1430. return lock_fd;
  1431. }
  1432. static int
  1433. disable_tty(void)
  1434. {
  1435. int tty_mode = -1;
  1436. if (isatty(STDIN_FILENO) && ioctl(STDIN_FILENO, KDGKBMODE, &tty_mode) == 0) {
  1437. #ifdef __linux__
  1438. ioctl(STDIN_FILENO, KDSKBMODE, K_OFF);
  1439. #elif __FreeBSD__
  1440. ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);
  1441. /* Put the tty into raw mode */
  1442. struct termios tios;
  1443. if (tcgetattr(STDIN_FILENO, &tios))
  1444. fprintf(stderr,
  1445. "Failed to get terminal attribute: %d - %s\n",
  1446. errno,
  1447. strerror(errno));
  1448. cfmakeraw(&tios);
  1449. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
  1450. fprintf(stderr,
  1451. "Failed to set terminal attribute: %d - %s\n",
  1452. errno,
  1453. strerror(errno));
  1454. #endif
  1455. }
  1456. return tty_mode;
  1457. }
  1458. static void
  1459. restore_tty(int tty_mode)
  1460. {
  1461. if (tty_mode != -1) {
  1462. ioctl(STDIN_FILENO, KDSKBMODE, tty_mode);
  1463. #ifdef __FreeBSD__
  1464. /* Put the tty into "sane" mode */
  1465. struct termios tios;
  1466. if (tcgetattr(STDIN_FILENO, &tios))
  1467. fprintf(stderr,
  1468. "Failed to get terminal attribute: %d - %s\n",
  1469. errno,
  1470. strerror(errno));
  1471. cfmakesane(&tios);
  1472. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
  1473. fprintf(stderr,
  1474. "Failed to set terminal attribute: %d - %s\n",
  1475. errno,
  1476. strerror(errno));
  1477. #endif
  1478. }
  1479. }
  1480. enum litest_runner_result
  1481. litest_run(struct list *suites, int njobs)
  1482. {
  1483. const struct rlimit corelimit = { 0, 0 };
  1484. int inhibit_lock_fd;
  1485. int tty_mode = -1;
  1486. setenv("LIBINPUT_RUNNING_TEST_SUITE", "1", 1);
  1487. if (setrlimit(RLIMIT_CORE, &corelimit) != 0)
  1488. perror("WARNING: Core dumps not disabled");
  1489. struct list created_files_list = LIST_INIT(created_files_list);
  1490. if (run_deviceless) {
  1491. litest_setup_quirks(&created_files_list, QUIRKS_SETUP_USE_SRCDIR);
  1492. } else {
  1493. enum quirks_setup_mode mode;
  1494. litest_init_udev_rules(&created_files_list);
  1495. mode = use_system_rules_quirks ? QUIRKS_SETUP_ONLY_DEVICE
  1496. : QUIRKS_SETUP_FULL;
  1497. litest_setup_quirks(&created_files_list, mode);
  1498. }
  1499. /* If we're running 'normally' on the VT, disable the keyboard to
  1500. * avoid messing up our host. But if we're inside gdb or running
  1501. * without forking, leave it as-is.
  1502. */
  1503. if (!run_deviceless && njobs > 1 && !in_debugger)
  1504. tty_mode = disable_tty();
  1505. inhibit_lock_fd = inhibit();
  1506. enum litest_runner_result result = litest_run_suite(suites, njobs);
  1507. close(inhibit_lock_fd);
  1508. restore_tty(tty_mode);
  1509. litest_remove_udev_rules(&created_files_list);
  1510. return result;
  1511. }
  1512. static struct input_absinfo *
  1513. merge_absinfo(const struct input_absinfo *orig, const struct input_absinfo *override)
  1514. {
  1515. struct input_absinfo *abs;
  1516. unsigned int nelem, i;
  1517. size_t sz = ABS_MAX + 1;
  1518. if (!orig)
  1519. return NULL;
  1520. abs = zalloc(sz * sizeof(*abs));
  1521. litest_assert_ptr_notnull(abs);
  1522. nelem = 0;
  1523. while (orig[nelem].value != -1) {
  1524. abs[nelem] = orig[nelem];
  1525. nelem++;
  1526. litest_assert_int_lt(nelem, sz);
  1527. }
  1528. /* just append, if the same axis is present twice, libevdev will
  1529. only use the last value anyway */
  1530. i = 0;
  1531. while (override && override[i].value != -1) {
  1532. abs[nelem++] = override[i++];
  1533. litest_assert_int_lt(nelem, sz);
  1534. }
  1535. litest_assert_int_lt(nelem, sz);
  1536. abs[nelem].value = -1;
  1537. return abs;
  1538. }
  1539. static int *
  1540. merge_events(const int *orig, const int *override)
  1541. {
  1542. int *events;
  1543. unsigned int nelem, i;
  1544. size_t sz = KEY_MAX * 3;
  1545. if (!orig)
  1546. return NULL;
  1547. events = zalloc(sz * sizeof(int));
  1548. litest_assert_ptr_notnull(events);
  1549. nelem = 0;
  1550. while (orig[nelem] != -1) {
  1551. events[nelem] = orig[nelem];
  1552. nelem++;
  1553. litest_assert_int_lt(nelem, sz);
  1554. }
  1555. /* just append, if the same axis is present twice, libevdev will
  1556. * ignore the double definition anyway */
  1557. i = 0;
  1558. while (override && override[i] != -1) {
  1559. events[nelem++] = override[i++];
  1560. litest_assert_int_le(nelem, sz);
  1561. }
  1562. litest_assert_int_lt(nelem, sz);
  1563. events[nelem] = -1;
  1564. return events;
  1565. }
  1566. static inline struct created_file *
  1567. litest_copy_file(const char *dest, const char *src, const char *header, bool is_file)
  1568. {
  1569. int in, out, length;
  1570. struct created_file *file;
  1571. file = zalloc(sizeof(*file));
  1572. file->path = safe_strdup(dest);
  1573. if (strstr(dest, "XXXXXX")) {
  1574. int suffixlen;
  1575. suffixlen = file->path + strlen(file->path) - rindex(file->path, '.');
  1576. out = mkstemps(file->path, suffixlen);
  1577. } else {
  1578. out = open(file->path, O_CREAT | O_WRONLY, 0644);
  1579. }
  1580. if (out == -1)
  1581. litest_abort_msg("Failed to write to file %s (%s)",
  1582. file->path,
  1583. strerror(errno));
  1584. litest_assert_errno_success(chmod(file->path, 0644));
  1585. if (header) {
  1586. length = strlen(header);
  1587. litest_assert_int_eq(write(out, header, length), length);
  1588. }
  1589. if (is_file) {
  1590. in = open(src, O_RDONLY);
  1591. if (in == -1)
  1592. litest_abort_msg("Failed to open file %s (%s)",
  1593. src,
  1594. strerror(errno));
  1595. /* lazy, just check for error and empty file copy */
  1596. litest_assert_int_gt(litest_send_file(out, in), 0);
  1597. close(in);
  1598. } else {
  1599. size_t written = write(out, src, strlen(src));
  1600. litest_assert_int_eq(written, strlen(src));
  1601. }
  1602. close(out);
  1603. return file;
  1604. }
  1605. static inline void
  1606. litest_install_model_quirks(struct list *created_files_list)
  1607. {
  1608. const char *warning =
  1609. "#################################################################\n"
  1610. "# WARNING: REMOVE THIS FILE\n"
  1611. "# This is a run-time file for the libinput test suite and\n"
  1612. "# should be removed on exit. If the test-suite is not currently \n"
  1613. "# running, remove this file\n"
  1614. "#################################################################\n\n";
  1615. struct created_file *file;
  1616. const char *test_device_udev_rule =
  1617. "KERNELS==\"*input*\", "
  1618. "ATTRS{name}==\"litest *\", "
  1619. "ENV{LIBINPUT_TEST_DEVICE}=\"1\"";
  1620. file = litest_copy_file(UDEV_TEST_DEVICE_RULE_FILE,
  1621. test_device_udev_rule,
  1622. warning,
  1623. false);
  1624. list_insert(created_files_list, &file->link);
  1625. /* Only install the litest device rule when we're running as system
  1626. * test suite, we expect the others to be in place already */
  1627. if (use_system_rules_quirks)
  1628. return;
  1629. file = litest_copy_file(UDEV_DEVICE_GROUPS_FILE,
  1630. LIBINPUT_DEVICE_GROUPS_RULES_FILE,
  1631. warning,
  1632. true);
  1633. list_insert(created_files_list, &file->link);
  1634. file = litest_copy_file(UDEV_FUZZ_OVERRIDE_RULE_FILE,
  1635. LIBINPUT_FUZZ_OVERRIDE_UDEV_RULES_FILE,
  1636. warning,
  1637. true);
  1638. list_insert(created_files_list, &file->link);
  1639. }
  1640. static char *
  1641. litest_init_device_quirk_file(const char *data_dir, struct litest_test_device *dev)
  1642. {
  1643. int fd;
  1644. FILE *f;
  1645. char path[PATH_MAX];
  1646. static int count;
  1647. if (!dev->quirk_file)
  1648. return NULL;
  1649. snprintf(path,
  1650. sizeof(path),
  1651. "%s/99-%03d-%s.quirks",
  1652. data_dir,
  1653. ++count,
  1654. dev->shortname);
  1655. fd = open(path, O_CREAT | O_WRONLY, 0644);
  1656. litest_assert_errno_success(fd);
  1657. f = fdopen(fd, "w");
  1658. litest_assert_notnull(f);
  1659. litest_assert_int_ge(fputs(dev->quirk_file, f), 0);
  1660. fclose(f);
  1661. return safe_strdup(path);
  1662. }
  1663. static int
  1664. is_quirks_file(const struct dirent *dir)
  1665. {
  1666. return strendswith(dir->d_name, ".quirks");
  1667. }
  1668. /**
  1669. * Install the quirks from the quirks/ source directory.
  1670. */
  1671. static void
  1672. litest_install_source_quirks(struct list *created_files_list, const char *dirname)
  1673. {
  1674. _autofree_ struct dirent **namelist;
  1675. int ndev;
  1676. ndev = scandir(LIBINPUT_QUIRKS_SRCDIR, &namelist, is_quirks_file, versionsort);
  1677. litest_assert_int_ge(ndev, 0);
  1678. for (int idx = 0; idx < ndev; idx++) {
  1679. struct created_file *file;
  1680. char *filename;
  1681. char dest[PATH_MAX];
  1682. char src[PATH_MAX];
  1683. _autofree_ struct dirent *entry = namelist[idx];
  1684. filename = entry->d_name;
  1685. snprintf(src, sizeof(src), "%s/%s", LIBINPUT_QUIRKS_SRCDIR, filename);
  1686. snprintf(dest, sizeof(dest), "%s/%s", dirname, filename);
  1687. file = litest_copy_file(dest, src, NULL, true);
  1688. list_append(created_files_list, &file->link);
  1689. }
  1690. }
  1691. /**
  1692. * Install the quirks from the various litest test devices
  1693. */
  1694. static void
  1695. litest_install_device_quirks(struct list *created_files_list, const char *dirname)
  1696. {
  1697. struct litest_test_device *dev;
  1698. list_for_each(dev, &devices, node) {
  1699. char *path;
  1700. path = litest_init_device_quirk_file(dirname, dev);
  1701. if (path) {
  1702. struct created_file *file = zalloc(sizeof(*file));
  1703. file->path = path;
  1704. list_insert(created_files_list, &file->link);
  1705. }
  1706. }
  1707. }
  1708. static void
  1709. litest_setup_quirks(struct list *created_files_list, enum quirks_setup_mode mode)
  1710. {
  1711. struct created_file *file = NULL;
  1712. const char *dirname;
  1713. char tmpdir[] = "/run/litest-XXXXXX";
  1714. switch (mode) {
  1715. case QUIRKS_SETUP_USE_SRCDIR:
  1716. if (builddir_lookup(NULL))
  1717. dirname = LIBINPUT_QUIRKS_SRCDIR;
  1718. else
  1719. dirname = LIBINPUT_QUIRKS_DIR;
  1720. break;
  1721. case QUIRKS_SETUP_ONLY_DEVICE:
  1722. dirname = LIBINPUT_QUIRKS_DIR;
  1723. litest_install_device_quirks(created_files_list, dirname);
  1724. break;
  1725. case QUIRKS_SETUP_FULL:
  1726. litest_assert_notnull(mkdtemp(tmpdir));
  1727. litest_assert_errno_success(chmod(tmpdir, 0755));
  1728. file = zalloc(sizeof *file);
  1729. file->path = safe_strdup(tmpdir);
  1730. dirname = tmpdir;
  1731. litest_install_source_quirks(created_files_list, dirname);
  1732. litest_install_device_quirks(created_files_list, dirname);
  1733. list_append(created_files_list, &file->link);
  1734. break;
  1735. }
  1736. setenv("LIBINPUT_QUIRKS_DIR", dirname, 1);
  1737. }
  1738. static inline void
  1739. litest_init_udev_rules(struct list *created_files)
  1740. {
  1741. litest_assert_neg_errno_success(mkdir_p(UDEV_RULES_D));
  1742. litest_install_model_quirks(created_files);
  1743. litest_init_all_device_udev_rules(created_files);
  1744. litest_reload_udev_rules();
  1745. }
  1746. static inline void
  1747. litest_remove_udev_rules(struct list *created_files_list)
  1748. {
  1749. struct created_file *f;
  1750. bool reload_udev;
  1751. reload_udev = !list_empty(created_files_list);
  1752. list_for_each_safe(f, created_files_list, link) {
  1753. created_file_unlink(f);
  1754. created_file_destroy(f);
  1755. }
  1756. if (reload_udev)
  1757. litest_reload_udev_rules();
  1758. }
  1759. /**
  1760. * Creates a uinput device but does not add it to a libinput context
  1761. */
  1762. struct litest_device *
  1763. litest_create(enum litest_device_type which,
  1764. const char *name_override,
  1765. struct input_id *id_override,
  1766. const struct input_absinfo *abs_override,
  1767. const int *events_override)
  1768. {
  1769. struct litest_device *d = NULL;
  1770. struct litest_test_device *dev;
  1771. const char *name;
  1772. const struct input_id *id;
  1773. _autofree_ struct input_absinfo *abs;
  1774. _autofree_ int *events;
  1775. int *e;
  1776. const char *path;
  1777. int fd, rc;
  1778. bool found = false;
  1779. bool create_device = true;
  1780. list_for_each(dev, &devices, node) {
  1781. if (dev->type == which) {
  1782. found = true;
  1783. break;
  1784. }
  1785. }
  1786. if (!found)
  1787. litest_abort_msg("Invalid device type %d", which);
  1788. d = zalloc(sizeof(*d));
  1789. d->which = which;
  1790. /* device has custom create method */
  1791. if (dev->create) {
  1792. create_device = dev->create(d);
  1793. if (abs_override || events_override) {
  1794. litest_abort_msg("Custom create cannot be overridden");
  1795. }
  1796. }
  1797. abs = merge_absinfo(dev->absinfo, abs_override);
  1798. events = merge_events(dev->events, events_override);
  1799. name = name_override ? name_override : dev->name;
  1800. id = id_override ? id_override : dev->id;
  1801. if (create_device) {
  1802. d->uinput = litest_create_uinput_device_from_description(name,
  1803. id,
  1804. abs,
  1805. events);
  1806. d->interface = dev->interface;
  1807. for (e = events; *e != -1; e += 2) {
  1808. unsigned int type = *e, code = *(e + 1);
  1809. if (type == INPUT_PROP_MAX && code == INPUT_PROP_SEMI_MT) {
  1810. d->semi_mt.is_semi_mt = true;
  1811. break;
  1812. }
  1813. }
  1814. }
  1815. path = libevdev_uinput_get_devnode(d->uinput);
  1816. litest_assert_ptr_notnull(path);
  1817. fd = open(path, O_RDWR | O_NONBLOCK);
  1818. litest_assert_errno_success(fd);
  1819. rc = libevdev_new_from_fd(fd, &d->evdev);
  1820. litest_assert_neg_errno_success(rc);
  1821. return d;
  1822. }
  1823. struct libinput *
  1824. litest_create_context_with_plugindir(const char *plugindir)
  1825. {
  1826. struct libinput *libinput;
  1827. struct litest_context *ctx;
  1828. ctx = zalloc(sizeof *ctx);
  1829. list_init(&ctx->paths);
  1830. libinput = libinput_path_create_context(&interface, ctx);
  1831. litest_assert_notnull(libinput);
  1832. libinput_log_set_handler(libinput, litest_log_handler);
  1833. if (verbose)
  1834. libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
  1835. if (plugindir)
  1836. libinput_plugin_system_append_path(libinput, plugindir);
  1837. return libinput;
  1838. }
  1839. struct libinput *
  1840. litest_create_context(void)
  1841. {
  1842. struct libinput *li = litest_create_context_with_plugindir(NULL);
  1843. libinput_plugin_system_load_plugins(li, LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
  1844. return li;
  1845. }
  1846. void
  1847. litest_destroy_context(struct libinput *li)
  1848. {
  1849. if (li) {
  1850. _autofree_ struct litest_context *ctx = libinput_get_user_data(li);
  1851. litest_assert_ptr_notnull(ctx);
  1852. libinput_unref(li);
  1853. struct path *p;
  1854. list_for_each_safe(p, &ctx->paths, link) {
  1855. litest_abort_msg("Device paths should be removed by now");
  1856. }
  1857. }
  1858. }
  1859. void
  1860. litest_context_set_user_data(struct libinput *li, void *data)
  1861. {
  1862. struct litest_user_data *litest_data = libinput_get_user_data(li);
  1863. litest_assert_ptr_notnull(litest_data);
  1864. litest_data->private = data;
  1865. }
  1866. void *
  1867. litest_context_get_user_data(struct libinput *li)
  1868. {
  1869. struct litest_user_data *litest_data = libinput_get_user_data(li);
  1870. litest_assert_ptr_notnull(litest_data);
  1871. return litest_data->private;
  1872. }
  1873. void
  1874. litest_disable_log_handler(struct libinput *libinput)
  1875. {
  1876. libinput_log_set_handler(libinput, NULL);
  1877. }
  1878. void
  1879. litest_restore_log_handler(struct libinput *libinput)
  1880. {
  1881. libinput_log_set_handler(libinput, litest_log_handler);
  1882. if (verbose)
  1883. libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
  1884. }
  1885. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  1886. static void
  1887. litest_bug_log_handler(struct libinput *libinput,
  1888. enum libinput_log_priority pri,
  1889. const char *format,
  1890. va_list args)
  1891. {
  1892. if (pri != LIBINPUT_LOG_PRIORITY_ERROR || strstr(format, "client bug: ") ||
  1893. strstr(format, "libinput bug: ") || strstr(format, "kernel bug: "))
  1894. return;
  1895. /* messages from plugins don't have the string in the format, it's one of the
  1896. * args... */
  1897. _autofree_ char *msg = strdup_vprintf(format, args);
  1898. if (strstr(msg, "plugin bug:"))
  1899. return;
  1900. litest_abort_msg("Expected bug statement in log msg ('%s'), aborting.", msg);
  1901. }
  1902. void
  1903. litest_set_log_handler_bug(struct libinput *libinput)
  1904. {
  1905. libinput_log_set_handler(libinput, litest_bug_log_handler);
  1906. }
  1907. struct litest_device *
  1908. litest_add_device_with_overrides(struct libinput *libinput,
  1909. enum litest_device_type which,
  1910. const char *name_override,
  1911. struct input_id *id_override,
  1912. const struct input_absinfo *abs_override,
  1913. const int *events_override)
  1914. {
  1915. struct litest_device *d;
  1916. const char *path;
  1917. d = litest_create(which,
  1918. name_override,
  1919. id_override,
  1920. abs_override,
  1921. events_override);
  1922. path = libevdev_uinput_get_devnode(d->uinput);
  1923. litest_assert_ptr_notnull(path);
  1924. d->libinput = libinput;
  1925. d->libinput_device = libinput_path_add_device(d->libinput, path);
  1926. litest_assert_ptr_notnull(d->libinput_device);
  1927. _unref_(udev_device) *ud = libinput_device_get_udev_device(d->libinput_device);
  1928. d->quirks = quirks_fetch_for_device(quirks_context, ud);
  1929. libinput_device_ref(d->libinput_device);
  1930. if (d->interface) {
  1931. unsigned int code;
  1932. code = ABS_X;
  1933. if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
  1934. code = ABS_MT_POSITION_X;
  1935. if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
  1936. d->interface->min[ABS_X] =
  1937. libevdev_get_abs_minimum(d->evdev, code);
  1938. d->interface->max[ABS_X] =
  1939. libevdev_get_abs_maximum(d->evdev, code);
  1940. }
  1941. code = ABS_Y;
  1942. if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
  1943. code = ABS_MT_POSITION_Y;
  1944. if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
  1945. d->interface->min[ABS_Y] =
  1946. libevdev_get_abs_minimum(d->evdev, code);
  1947. d->interface->max[ABS_Y] =
  1948. libevdev_get_abs_maximum(d->evdev, code);
  1949. }
  1950. d->interface->tool_type = BTN_TOOL_PEN;
  1951. }
  1952. return d;
  1953. }
  1954. struct litest_device *
  1955. litest_add_device(struct libinput *libinput, enum litest_device_type which)
  1956. {
  1957. return litest_add_device_with_overrides(libinput,
  1958. which,
  1959. NULL,
  1960. NULL,
  1961. NULL,
  1962. NULL);
  1963. }
  1964. struct litest_device *
  1965. litest_create_device_with_overrides(enum litest_device_type which,
  1966. const char *name_override,
  1967. struct input_id *id_override,
  1968. const struct input_absinfo *abs_override,
  1969. const int *events_override)
  1970. {
  1971. struct litest_device *dev =
  1972. litest_add_device_with_overrides(litest_create_context(),
  1973. which,
  1974. name_override,
  1975. id_override,
  1976. abs_override,
  1977. events_override);
  1978. dev->owns_context = true;
  1979. return dev;
  1980. }
  1981. struct litest_device *
  1982. litest_create_device(enum litest_device_type which)
  1983. {
  1984. return litest_create_device_with_overrides(which, NULL, NULL, NULL, NULL);
  1985. }
  1986. static struct udev_monitor *
  1987. udev_setup_monitor(void)
  1988. {
  1989. _unref_(udev) *udev = udev_new();
  1990. _unref_(udev_monitor) *udev_monitor = NULL;
  1991. int rc;
  1992. litest_assert_notnull(udev);
  1993. udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
  1994. litest_assert_notnull(udev_monitor);
  1995. udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", NULL);
  1996. /* remove O_NONBLOCK */
  1997. rc = fcntl(udev_monitor_get_fd(udev_monitor), F_SETFL, 0);
  1998. litest_assert_errno_success(rc);
  1999. litest_assert_int_eq(udev_monitor_enable_receiving(udev_monitor), 0);
  2000. return steal(&udev_monitor);
  2001. }
  2002. static struct udev_device *
  2003. udev_wait_for_device_event(struct udev_monitor *udev_monitor,
  2004. const char *udev_event,
  2005. const char *syspath)
  2006. {
  2007. /* blocking, we don't want to continue until udev is ready */
  2008. while (1) {
  2009. _unref_(udev_device) *udev_device = NULL;
  2010. const char *udev_syspath = NULL;
  2011. const char *udev_action;
  2012. udev_device = udev_monitor_receive_device(udev_monitor);
  2013. if (!udev_device) {
  2014. if (errno == EAGAIN)
  2015. continue;
  2016. litest_abort_msg(
  2017. "Failed to receive udev device from monitor: %s (%d)",
  2018. strerror(errno),
  2019. errno);
  2020. }
  2021. udev_action = udev_device_get_action(udev_device);
  2022. if (!udev_action || !streq(udev_action, udev_event)) {
  2023. continue;
  2024. }
  2025. udev_syspath = udev_device_get_syspath(udev_device);
  2026. if (strstartswith(udev_syspath, syspath))
  2027. return steal(&udev_device);
  2028. }
  2029. }
  2030. void
  2031. litest_device_destroy(struct litest_device *d)
  2032. {
  2033. _unref_(udev_monitor) *udev_monitor = NULL;
  2034. _unref_(udev_device) *udev_device = NULL;
  2035. char path[PATH_MAX];
  2036. if (!d)
  2037. return;
  2038. udev_monitor = udev_setup_monitor();
  2039. snprintf(path,
  2040. sizeof(path),
  2041. "%s/event",
  2042. libevdev_uinput_get_syspath(d->uinput));
  2043. litest_assert_int_eq(d->skip_ev_syn, 0);
  2044. quirks_unref(d->quirks);
  2045. if (d->libinput_device) {
  2046. libinput_path_remove_device(d->libinput_device);
  2047. libinput_device_unref(d->libinput_device);
  2048. }
  2049. if (d->owns_context) {
  2050. libinput_dispatch(d->libinput);
  2051. litest_destroy_context(d->libinput);
  2052. }
  2053. close(libevdev_get_fd(d->evdev));
  2054. libevdev_free(d->evdev);
  2055. libevdev_uinput_destroy(d->uinput);
  2056. free(d->private);
  2057. memset(d, 0, sizeof(*d));
  2058. free(d);
  2059. udev_device = // NOLINT: deadcode.DeadStores
  2060. udev_wait_for_device_event(udev_monitor, "remove", path);
  2061. }
  2062. void
  2063. litest_event_unchecked(struct litest_device *d,
  2064. unsigned int type,
  2065. unsigned int code,
  2066. int value)
  2067. {
  2068. int ret = libevdev_uinput_write_event(d->uinput, type, code, value);
  2069. litest_assert_neg_errno_success(ret);
  2070. }
  2071. void
  2072. litest_event(struct litest_device *d, unsigned int type, unsigned int code, int value)
  2073. {
  2074. if (!libevdev_has_event_code(d->evdev, type, code))
  2075. return;
  2076. if (type == EV_SYN && code == SYN_REPORT) {
  2077. if (d->skip_ev_syn)
  2078. return;
  2079. for (size_t i = 0; i < d->frame.nevents; i++) {
  2080. struct input_event *e = &d->frame.events[i];
  2081. int ret = libevdev_uinput_write_event(d->uinput,
  2082. e->type,
  2083. e->code,
  2084. e->value);
  2085. litest_assert_neg_errno_success(ret);
  2086. }
  2087. int ret = libevdev_uinput_write_event(d->uinput,
  2088. EV_SYN,
  2089. SYN_REPORT,
  2090. value);
  2091. litest_assert_neg_errno_success(ret);
  2092. d->frame.nevents = 0;
  2093. } else {
  2094. size_t i;
  2095. if (type == EV_SYN || (type == EV_ABS && code >= ABS_MT_SLOT)) {
  2096. i = d->frame.nevents;
  2097. } else {
  2098. for (i = 0; i < d->frame.nevents; i++) {
  2099. if (d->frame.events[i].type == type &&
  2100. d->frame.events[i].code == code)
  2101. break;
  2102. }
  2103. }
  2104. litest_assert_int_lt(i, ARRAY_LENGTH(d->frame.events));
  2105. d->frame.events[i] = (struct input_event){
  2106. .type = type,
  2107. .code = code,
  2108. .value = value,
  2109. };
  2110. if (i >= d->frame.nevents)
  2111. d->frame.nevents++;
  2112. }
  2113. }
  2114. static bool
  2115. axis_replacement_value(struct litest_device *d,
  2116. struct axis_replacement *axes,
  2117. int32_t evcode,
  2118. int32_t *value)
  2119. {
  2120. struct axis_replacement *axis = axes;
  2121. if (!axes)
  2122. return false;
  2123. while (axis->evcode != -1) {
  2124. if (axis->evcode == evcode) {
  2125. switch (evcode) {
  2126. case ABS_MISC:
  2127. case ABS_MT_SLOT:
  2128. case ABS_MT_TRACKING_ID:
  2129. case ABS_MT_TOOL_TYPE:
  2130. *value = axis->value;
  2131. break;
  2132. default:
  2133. *value = litest_scale(d, evcode, axis->value);
  2134. break;
  2135. }
  2136. return true;
  2137. }
  2138. axis++;
  2139. }
  2140. return false;
  2141. }
  2142. int
  2143. litest_auto_assign_value(struct litest_device *d,
  2144. const struct input_event *ev,
  2145. int slot,
  2146. double x,
  2147. double y,
  2148. struct axis_replacement *axes,
  2149. bool touching)
  2150. {
  2151. static int tracking_id;
  2152. int value = ev->value;
  2153. if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
  2154. return value;
  2155. switch (ev->code) {
  2156. case ABS_X:
  2157. case ABS_MT_POSITION_X:
  2158. value = litest_scale(d, ABS_X, x);
  2159. break;
  2160. case ABS_Y:
  2161. case ABS_MT_POSITION_Y:
  2162. value = litest_scale(d, ABS_Y, y);
  2163. break;
  2164. case ABS_MT_TRACKING_ID:
  2165. value = ++tracking_id;
  2166. break;
  2167. case ABS_MT_SLOT:
  2168. value = slot;
  2169. break;
  2170. case ABS_MT_DISTANCE:
  2171. value = touching ? 0 : 1;
  2172. break;
  2173. case ABS_MT_TOOL_TYPE:
  2174. if (!axis_replacement_value(d, axes, ev->code, &value))
  2175. value = MT_TOOL_FINGER;
  2176. break;
  2177. default:
  2178. if (!axis_replacement_value(d, axes, ev->code, &value) &&
  2179. d->interface->get_axis_default) {
  2180. int error = d->interface->get_axis_default(d, ev->code, &value);
  2181. if (error) {
  2182. litest_abort_msg(
  2183. "Failed to get default axis value for %s (%d)",
  2184. libevdev_event_code_get_name(EV_ABS, ev->code),
  2185. ev->code);
  2186. }
  2187. }
  2188. break;
  2189. }
  2190. return value;
  2191. }
  2192. static void
  2193. send_btntool(struct litest_device *d, bool hover)
  2194. {
  2195. litest_event(d, EV_KEY, BTN_TOUCH, d->ntouches_down != 0 && !hover);
  2196. litest_event(d, EV_KEY, BTN_TOOL_FINGER, d->ntouches_down == 1);
  2197. litest_event(d, EV_KEY, BTN_TOOL_DOUBLETAP, d->ntouches_down == 2);
  2198. litest_event(d, EV_KEY, BTN_TOOL_TRIPLETAP, d->ntouches_down == 3);
  2199. litest_event(d, EV_KEY, BTN_TOOL_QUADTAP, d->ntouches_down == 4);
  2200. litest_event(d, EV_KEY, BTN_TOOL_QUINTTAP, d->ntouches_down == 5);
  2201. }
  2202. static void
  2203. slot_start(struct litest_device *d,
  2204. unsigned int slot,
  2205. double x,
  2206. double y,
  2207. struct axis_replacement *axes,
  2208. bool touching,
  2209. bool filter_abs_xy)
  2210. {
  2211. struct input_event *ev;
  2212. litest_assert_int_ge(d->ntouches_down, 0);
  2213. d->ntouches_down++;
  2214. send_btntool(d, !touching);
  2215. /* If the test device overrides touch_down and says it didn't
  2216. * handle the event, let's continue normally */
  2217. if (d->interface->touch_down && d->interface->touch_down(d, slot, x, y))
  2218. return;
  2219. for (ev = d->interface->touch_down_events;
  2220. ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
  2221. ev++) {
  2222. int value = litest_auto_assign_value(d, ev, slot, x, y, axes, touching);
  2223. if (value == LITEST_AUTO_ASSIGN)
  2224. continue;
  2225. if (filter_abs_xy && ev->type == EV_ABS &&
  2226. (ev->code == ABS_X || ev->code == ABS_Y))
  2227. continue;
  2228. litest_event(d, ev->type, ev->code, value);
  2229. }
  2230. }
  2231. static void
  2232. slot_move(struct litest_device *d,
  2233. unsigned int slot,
  2234. double x,
  2235. double y,
  2236. struct axis_replacement *axes,
  2237. bool touching,
  2238. bool filter_abs_xy)
  2239. {
  2240. struct input_event *ev;
  2241. if (d->interface->touch_move && d->interface->touch_move(d, slot, x, y))
  2242. return;
  2243. for (ev = d->interface->touch_move_events;
  2244. ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
  2245. ev++) {
  2246. int value = litest_auto_assign_value(d, ev, slot, x, y, axes, touching);
  2247. if (value == LITEST_AUTO_ASSIGN)
  2248. continue;
  2249. if (filter_abs_xy && ev->type == EV_ABS &&
  2250. (ev->code == ABS_X || ev->code == ABS_Y))
  2251. continue;
  2252. litest_event(d, ev->type, ev->code, value);
  2253. }
  2254. }
  2255. static void
  2256. touch_up(struct litest_device *d, unsigned int slot)
  2257. {
  2258. struct input_event *ev;
  2259. struct input_event up[] = {
  2260. { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
  2261. { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
  2262. { .type = EV_ABS, .code = ABS_MT_PRESSURE, .value = 0 },
  2263. { .type = EV_ABS, .code = ABS_MT_TOUCH_MAJOR, .value = 0 },
  2264. { .type = EV_ABS, .code = ABS_MT_TOUCH_MINOR, .value = 0 },
  2265. { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
  2266. { .type = -1, .code = -1 }
  2267. };
  2268. litest_assert_int_gt(d->ntouches_down, 0);
  2269. d->ntouches_down--;
  2270. send_btntool(d, false);
  2271. if (d->interface->touch_up && d->interface->touch_up(d, slot)) {
  2272. return;
  2273. } else if (d->interface->touch_up_events) {
  2274. ev = d->interface->touch_up_events;
  2275. } else
  2276. ev = up;
  2277. for (/* */; ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1; ev++) {
  2278. int value = litest_auto_assign_value(d, ev, slot, 0, 0, NULL, false);
  2279. litest_event(d, ev->type, ev->code, value);
  2280. }
  2281. }
  2282. static void
  2283. litest_slot_start(struct litest_device *d,
  2284. unsigned int slot,
  2285. double x,
  2286. double y,
  2287. struct axis_replacement *axes,
  2288. bool touching)
  2289. {
  2290. double t, l, r = 0, b = 0; /* top, left, right, bottom */
  2291. bool filter_abs_xy = false;
  2292. if (!d->semi_mt.is_semi_mt) {
  2293. slot_start(d, slot, x, y, axes, touching, filter_abs_xy);
  2294. return;
  2295. }
  2296. if (d->ntouches_down >= 2 || slot > 1)
  2297. return;
  2298. slot = d->ntouches_down;
  2299. if (d->ntouches_down == 0) {
  2300. l = x;
  2301. t = y;
  2302. } else {
  2303. int other = (slot + 1) % 2;
  2304. l = min(x, d->semi_mt.touches[other].x);
  2305. t = min(y, d->semi_mt.touches[other].y);
  2306. r = max(x, d->semi_mt.touches[other].x);
  2307. b = max(y, d->semi_mt.touches[other].y);
  2308. }
  2309. litest_with_event_frame(d) {
  2310. if (d->ntouches_down == 0)
  2311. slot_start(d, 0, l, t, axes, touching, filter_abs_xy);
  2312. else
  2313. slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
  2314. if (slot == 1) {
  2315. filter_abs_xy = true;
  2316. slot_start(d, 1, r, b, axes, touching, filter_abs_xy);
  2317. }
  2318. }
  2319. d->semi_mt.touches[slot].x = x;
  2320. d->semi_mt.touches[slot].y = y;
  2321. }
  2322. void
  2323. litest_touch_sequence(struct litest_device *d,
  2324. unsigned int slot,
  2325. double x_from,
  2326. double y_from,
  2327. double x_to,
  2328. double y_to,
  2329. int steps)
  2330. {
  2331. litest_touch_down(d, slot, x_from, y_from);
  2332. litest_touch_move_to(d, slot, x_from, y_from, x_to, y_to, steps);
  2333. litest_touch_up(d, slot);
  2334. }
  2335. void
  2336. litest_touch_down(struct litest_device *d, unsigned int slot, double x, double y)
  2337. {
  2338. litest_slot_start(d, slot, x, y, NULL, true);
  2339. }
  2340. void
  2341. litest_touch_down_extended(struct litest_device *d,
  2342. unsigned int slot,
  2343. double x,
  2344. double y,
  2345. struct axis_replacement *axes)
  2346. {
  2347. litest_slot_start(d, slot, x, y, axes, true);
  2348. }
  2349. static void
  2350. litest_slot_move(struct litest_device *d,
  2351. unsigned int slot,
  2352. double x,
  2353. double y,
  2354. struct axis_replacement *axes,
  2355. bool touching)
  2356. {
  2357. double t, l, r = 0, b = 0; /* top, left, right, bottom */
  2358. bool filter_abs_xy = false;
  2359. if (!d->semi_mt.is_semi_mt) {
  2360. slot_move(d, slot, x, y, axes, touching, filter_abs_xy);
  2361. return;
  2362. }
  2363. if (d->ntouches_down > 2 || slot > 1)
  2364. return;
  2365. if (d->ntouches_down == 1) {
  2366. l = x;
  2367. t = y;
  2368. } else {
  2369. int other = (slot + 1) % 2;
  2370. l = min(x, d->semi_mt.touches[other].x);
  2371. t = min(y, d->semi_mt.touches[other].y);
  2372. r = max(x, d->semi_mt.touches[other].x);
  2373. b = max(y, d->semi_mt.touches[other].y);
  2374. }
  2375. litest_with_event_frame(d) {
  2376. slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
  2377. if (d->ntouches_down == 2) {
  2378. filter_abs_xy = true;
  2379. slot_move(d, 1, r, b, axes, touching, filter_abs_xy);
  2380. }
  2381. }
  2382. d->semi_mt.touches[slot].x = x;
  2383. d->semi_mt.touches[slot].y = y;
  2384. }
  2385. void
  2386. litest_touch_up(struct litest_device *d, unsigned int slot)
  2387. {
  2388. if (!d->semi_mt.is_semi_mt) {
  2389. touch_up(d, slot);
  2390. return;
  2391. }
  2392. if (d->ntouches_down > 2 || slot > 1)
  2393. return;
  2394. litest_with_event_frame(d) {
  2395. touch_up(d, d->ntouches_down - 1);
  2396. /* if we have one finger left, send x/y coords for that finger left.
  2397. this is likely to happen with a real touchpad */
  2398. if (d->ntouches_down == 1) {
  2399. bool touching = true;
  2400. bool filter_abs_xy = false;
  2401. int other = (slot + 1) % 2;
  2402. slot_move(d,
  2403. 0,
  2404. d->semi_mt.touches[other].x,
  2405. d->semi_mt.touches[other].y,
  2406. NULL,
  2407. touching,
  2408. filter_abs_xy);
  2409. }
  2410. }
  2411. }
  2412. void
  2413. litest_touch_move(struct litest_device *d, unsigned int slot, double x, double y)
  2414. {
  2415. litest_slot_move(d, slot, x, y, NULL, true);
  2416. }
  2417. void
  2418. litest_touch_move_extended(struct litest_device *d,
  2419. unsigned int slot,
  2420. double x,
  2421. double y,
  2422. struct axis_replacement *axes)
  2423. {
  2424. litest_slot_move(d, slot, x, y, axes, true);
  2425. }
  2426. void
  2427. litest_touch_move_to(struct litest_device *d,
  2428. unsigned int slot,
  2429. double x_from,
  2430. double y_from,
  2431. double x_to,
  2432. double y_to,
  2433. int steps)
  2434. {
  2435. litest_touch_move_to_extended(d, slot, x_from, y_from, x_to, y_to, NULL, steps);
  2436. }
  2437. void
  2438. litest_touch_move_to_extended(struct litest_device *d,
  2439. unsigned int slot,
  2440. double x_from,
  2441. double y_from,
  2442. double x_to,
  2443. double y_to,
  2444. struct axis_replacement *axes,
  2445. int steps)
  2446. {
  2447. int sleep_ms = 10;
  2448. for (int i = 1; i < steps; i++) {
  2449. litest_touch_move_extended(d,
  2450. slot,
  2451. x_from + (x_to - x_from) / steps * i,
  2452. y_from + (y_to - y_from) / steps * i,
  2453. axes);
  2454. libinput_dispatch(d->libinput);
  2455. msleep(sleep_ms);
  2456. libinput_dispatch(d->libinput);
  2457. }
  2458. litest_touch_move_extended(d, slot, x_to, y_to, axes);
  2459. }
  2460. static int
  2461. auto_assign_tablet_value(struct litest_device *d,
  2462. const struct input_event *ev,
  2463. int x,
  2464. int y,
  2465. struct axis_replacement *axes)
  2466. {
  2467. static int tracking_id;
  2468. int value = ev->value;
  2469. if (ev->type != EV_ABS)
  2470. return value;
  2471. switch (ev->code) {
  2472. case ABS_MT_TRACKING_ID:
  2473. value = ++tracking_id;
  2474. break;
  2475. case ABS_X:
  2476. case ABS_MT_POSITION_X:
  2477. value = litest_scale(d, ABS_X, x);
  2478. break;
  2479. case ABS_Y:
  2480. case ABS_MT_POSITION_Y:
  2481. value = litest_scale(d, ABS_Y, y);
  2482. break;
  2483. default:
  2484. if (!axis_replacement_value(d, axes, ev->code, &value) &&
  2485. value == LITEST_AUTO_ASSIGN && d->interface->get_axis_default) {
  2486. int error = d->interface->get_axis_default(d, ev->code, &value);
  2487. if (error) {
  2488. litest_abort_msg(
  2489. "Failed to get default axis value for %s (%d)",
  2490. libevdev_event_code_get_name(EV_ABS, ev->code),
  2491. ev->code);
  2492. }
  2493. }
  2494. break;
  2495. }
  2496. return value;
  2497. }
  2498. static int
  2499. tablet_ignore_event(const struct input_event *ev, int value)
  2500. {
  2501. return value == -1 && (ev->code == ABS_PRESSURE || ev->code == ABS_DISTANCE);
  2502. }
  2503. void
  2504. litest_tablet_set_tool_type(struct litest_device *d, unsigned int code)
  2505. {
  2506. switch (code) {
  2507. case BTN_TOOL_PEN:
  2508. case BTN_TOOL_RUBBER:
  2509. case BTN_TOOL_BRUSH:
  2510. case BTN_TOOL_PENCIL:
  2511. case BTN_TOOL_AIRBRUSH:
  2512. case BTN_TOOL_MOUSE:
  2513. case BTN_TOOL_LENS:
  2514. break;
  2515. default:
  2516. abort();
  2517. }
  2518. d->interface->tool_type = code;
  2519. }
  2520. static void
  2521. litest_tool_event(struct litest_device *d, int value)
  2522. {
  2523. unsigned int tool = d->interface->tool_type;
  2524. litest_event(d, EV_KEY, tool, value);
  2525. }
  2526. void
  2527. litest_tablet_proximity_in(struct litest_device *d,
  2528. double x,
  2529. double y,
  2530. struct axis_replacement *axes)
  2531. {
  2532. struct input_event *ev;
  2533. /* If the test device overrides proximity_in and says it didn't
  2534. * handle the event, let's continue normally */
  2535. if (d->interface->tablet_proximity_in &&
  2536. d->interface->tablet_proximity_in(d, d->interface->tool_type, &x, &y, axes))
  2537. return;
  2538. ev = d->interface->tablet_proximity_in_events;
  2539. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2540. int value;
  2541. switch (evbit(ev->type, ev->code)) {
  2542. case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
  2543. litest_tool_event(d, ev->value);
  2544. break;
  2545. default:
  2546. value = auto_assign_tablet_value(d, ev, x, y, axes);
  2547. if (!tablet_ignore_event(ev, value))
  2548. litest_event(d, ev->type, ev->code, value);
  2549. }
  2550. ev++;
  2551. }
  2552. }
  2553. void
  2554. litest_tablet_proximity_out(struct litest_device *d)
  2555. {
  2556. struct input_event *ev;
  2557. /* If the test device overrides proximity_out and says it didn't
  2558. * handle the event, let's continue normally */
  2559. if (d->interface->tablet_proximity_out &&
  2560. d->interface->tablet_proximity_out(d, d->interface->tool_type))
  2561. return;
  2562. ev = d->interface->tablet_proximity_out_events;
  2563. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2564. int value;
  2565. switch (evbit(ev->type, ev->code)) {
  2566. case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
  2567. litest_tool_event(d, ev->value);
  2568. break;
  2569. case evbit(EV_ABS, ABS_MT_TRACKING_ID):
  2570. if (ev->value == -1) {
  2571. litest_event(d, ev->type, ev->code, ev->value);
  2572. break;
  2573. }
  2574. _fallthrough_;
  2575. default:
  2576. value = auto_assign_tablet_value(d, ev, 0, 0, NULL);
  2577. if (!tablet_ignore_event(ev, value))
  2578. litest_event(d, ev->type, ev->code, value);
  2579. break;
  2580. }
  2581. ev++;
  2582. }
  2583. }
  2584. void
  2585. litest_tablet_motion(struct litest_device *d,
  2586. double x,
  2587. double y,
  2588. struct axis_replacement *axes)
  2589. {
  2590. struct input_event *ev;
  2591. /* If the test device overrides proximity_out and says it didn't
  2592. * handle the event, let's continue normally */
  2593. if (d->interface->tablet_motion && d->interface->tablet_motion(d, &x, &y, axes))
  2594. return;
  2595. ev = d->interface->tablet_motion_events;
  2596. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2597. int value = auto_assign_tablet_value(d, ev, x, y, axes);
  2598. if (!tablet_ignore_event(ev, value))
  2599. litest_event(d, ev->type, ev->code, value);
  2600. ev++;
  2601. }
  2602. }
  2603. void
  2604. litest_tablet_tip_down(struct litest_device *d,
  2605. double x,
  2606. double y,
  2607. struct axis_replacement *axes)
  2608. {
  2609. /* If the test device overrides tip_down and says it didn't
  2610. * handle the event, let's continue normally */
  2611. if (d->interface->tablet_tip_down &&
  2612. d->interface->tablet_tip_down(d, &x, &y, axes))
  2613. return;
  2614. litest_event(d, EV_KEY, BTN_TOUCH, 1);
  2615. litest_tablet_motion(d, x, y, axes);
  2616. }
  2617. void
  2618. litest_tablet_tip_up(struct litest_device *d,
  2619. double x,
  2620. double y,
  2621. struct axis_replacement *axes)
  2622. {
  2623. /* If the test device overrides tip_down and says it didn't
  2624. * handle the event, let's continue normally */
  2625. if (d->interface->tablet_tip_up && d->interface->tablet_tip_up(d, &x, &y, axes))
  2626. return;
  2627. litest_event(d, EV_KEY, BTN_TOUCH, 0);
  2628. litest_tablet_motion(d, x, y, axes);
  2629. }
  2630. void
  2631. litest_touch_move_two_touches(struct litest_device *d,
  2632. double x0,
  2633. double y0,
  2634. double x1,
  2635. double y1,
  2636. double dx,
  2637. double dy,
  2638. int steps)
  2639. {
  2640. int sleep_ms = 10;
  2641. for (int i = 1; i < steps; i++) {
  2642. litest_with_event_frame(d) {
  2643. litest_touch_move(d,
  2644. 0,
  2645. x0 + dx / steps * i,
  2646. y0 + dy / steps * i);
  2647. litest_touch_move(d,
  2648. 1,
  2649. x1 + dx / steps * i,
  2650. y1 + dy / steps * i);
  2651. }
  2652. libinput_dispatch(d->libinput);
  2653. msleep(sleep_ms);
  2654. libinput_dispatch(d->libinput);
  2655. }
  2656. litest_with_event_frame(d) {
  2657. litest_touch_move(d, 0, x0 + dx, y0 + dy);
  2658. litest_touch_move(d, 1, x1 + dx, y1 + dy);
  2659. }
  2660. }
  2661. void
  2662. litest_touch_move_three_touches(struct litest_device *d,
  2663. double x0,
  2664. double y0,
  2665. double x1,
  2666. double y1,
  2667. double x2,
  2668. double y2,
  2669. double dx,
  2670. double dy,
  2671. int steps)
  2672. {
  2673. int sleep_ms = 10;
  2674. for (int i = 1; i <= steps; i++) {
  2675. double step_x = dx / steps * i;
  2676. double step_y = dy / steps * i;
  2677. litest_with_event_frame(d) {
  2678. litest_touch_move(d, 0, x0 + step_x, y0 + step_y);
  2679. litest_touch_move(d, 1, x1 + step_x, y1 + step_y);
  2680. litest_touch_move(d, 2, x2 + step_x, y2 + step_y);
  2681. }
  2682. libinput_dispatch(d->libinput);
  2683. msleep(sleep_ms);
  2684. }
  2685. libinput_dispatch(d->libinput);
  2686. }
  2687. void
  2688. litest_hover_start(struct litest_device *d, unsigned int slot, double x, double y)
  2689. {
  2690. struct axis_replacement axes[] = {
  2691. { ABS_MT_PRESSURE, 0 },
  2692. { ABS_PRESSURE, 0 },
  2693. { -1, -1 },
  2694. };
  2695. litest_slot_start(d, slot, x, y, axes, 0);
  2696. }
  2697. void
  2698. litest_hover_end(struct litest_device *d, unsigned int slot)
  2699. {
  2700. struct input_event *ev;
  2701. struct input_event up[] = {
  2702. { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
  2703. { .type = EV_ABS, .code = ABS_MT_DISTANCE, .value = 1 },
  2704. { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
  2705. { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
  2706. { .type = -1, .code = -1 }
  2707. };
  2708. litest_assert_int_gt(d->ntouches_down, 0);
  2709. d->ntouches_down--;
  2710. send_btntool(d, true);
  2711. if (d->interface->touch_up) {
  2712. d->interface->touch_up(d, slot);
  2713. return;
  2714. } else if (d->interface->touch_up_events) {
  2715. ev = d->interface->touch_up_events;
  2716. } else
  2717. ev = up;
  2718. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2719. int value = litest_auto_assign_value(d, ev, slot, 0, 0, NULL, false);
  2720. litest_event(d, ev->type, ev->code, value);
  2721. ev++;
  2722. }
  2723. }
  2724. void
  2725. litest_hover_move(struct litest_device *d, unsigned int slot, double x, double y)
  2726. {
  2727. struct axis_replacement axes[] = {
  2728. { ABS_MT_PRESSURE, 0 },
  2729. { ABS_PRESSURE, 0 },
  2730. { -1, -1 },
  2731. };
  2732. litest_slot_move(d, slot, x, y, axes, false);
  2733. }
  2734. void
  2735. litest_hover_move_to(struct litest_device *d,
  2736. unsigned int slot,
  2737. double x_from,
  2738. double y_from,
  2739. double x_to,
  2740. double y_to,
  2741. int steps)
  2742. {
  2743. int sleep_ms = 10;
  2744. for (int i = 0; i < steps - 1; i++) {
  2745. litest_hover_move(d,
  2746. slot,
  2747. x_from + (x_to - x_from) / steps * i,
  2748. y_from + (y_to - y_from) / steps * i);
  2749. libinput_dispatch(d->libinput);
  2750. msleep(sleep_ms);
  2751. libinput_dispatch(d->libinput);
  2752. }
  2753. litest_hover_move(d, slot, x_to, y_to);
  2754. }
  2755. void
  2756. litest_hover_move_two_touches(struct litest_device *d,
  2757. double x0,
  2758. double y0,
  2759. double x1,
  2760. double y1,
  2761. double dx,
  2762. double dy,
  2763. int steps)
  2764. {
  2765. int sleep_ms = 10;
  2766. for (int i = 0; i < steps - 1; i++) {
  2767. litest_with_event_frame(d) {
  2768. litest_hover_move(d,
  2769. 0,
  2770. x0 + dx / steps * i,
  2771. y0 + dy / steps * i);
  2772. litest_hover_move(d,
  2773. 1,
  2774. x1 + dx / steps * i,
  2775. y1 + dy / steps * i);
  2776. }
  2777. libinput_dispatch(d->libinput);
  2778. msleep(sleep_ms);
  2779. libinput_dispatch(d->libinput);
  2780. }
  2781. litest_with_event_frame(d) {
  2782. litest_hover_move(d, 0, x0 + dx, y0 + dy);
  2783. litest_hover_move(d, 1, x1 + dx, y1 + dy);
  2784. }
  2785. }
  2786. void
  2787. litest_button_click(struct litest_device *d, unsigned int button, bool is_press)
  2788. {
  2789. struct input_event click[] = {
  2790. { .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
  2791. { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
  2792. };
  2793. ARRAY_FOR_EACH(click, ev)
  2794. litest_event(d, ev->type, ev->code, ev->value);
  2795. }
  2796. void
  2797. litest_button_click_debounced(struct litest_device *d,
  2798. struct libinput *li,
  2799. unsigned int button,
  2800. bool is_press)
  2801. {
  2802. litest_button_click(d, button, is_press);
  2803. litest_timeout_debounce(li);
  2804. }
  2805. void
  2806. litest_button_scroll(struct litest_device *dev,
  2807. unsigned int button,
  2808. double dx,
  2809. double dy)
  2810. {
  2811. struct libinput *li = dev->libinput;
  2812. litest_button_click_debounced(dev, li, button, 1);
  2813. litest_timeout_buttonscroll(li);
  2814. litest_event(dev, EV_REL, REL_X, dx);
  2815. litest_event(dev, EV_REL, REL_Y, dy);
  2816. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  2817. litest_button_click_debounced(dev, li, button, 0);
  2818. libinput_dispatch(li);
  2819. }
  2820. void
  2821. litest_button_scroll_locked(struct litest_device *dev,
  2822. unsigned int button,
  2823. double dx,
  2824. double dy)
  2825. {
  2826. struct libinput *li = dev->libinput;
  2827. litest_button_click_debounced(dev, li, button, 1);
  2828. litest_button_click_debounced(dev, li, button, 0);
  2829. litest_timeout_buttonscroll(li);
  2830. litest_event(dev, EV_REL, REL_X, dx);
  2831. litest_event(dev, EV_REL, REL_Y, dy);
  2832. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  2833. libinput_dispatch(li);
  2834. }
  2835. void
  2836. litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
  2837. {
  2838. struct input_event click[] = {
  2839. { .type = EV_KEY, .code = key, .value = is_press ? 1 : 0 },
  2840. { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
  2841. };
  2842. ARRAY_FOR_EACH(click, ev)
  2843. litest_event(d, ev->type, ev->code, ev->value);
  2844. }
  2845. void
  2846. litest_switch_action(struct litest_device *dev,
  2847. enum libinput_switch sw,
  2848. enum libinput_switch_state state)
  2849. {
  2850. unsigned int code;
  2851. switch (sw) {
  2852. case LIBINPUT_SWITCH_LID:
  2853. code = SW_LID;
  2854. break;
  2855. case LIBINPUT_SWITCH_TABLET_MODE:
  2856. code = SW_TABLET_MODE;
  2857. break;
  2858. case LIBINPUT_SWITCH_KEYPAD_SLIDE:
  2859. code = SW_KEYPAD_SLIDE;
  2860. break;
  2861. default:
  2862. litest_abort_msg("Invalid switch %d", sw);
  2863. break;
  2864. }
  2865. litest_event(dev, EV_SW, code, state);
  2866. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  2867. }
  2868. static int
  2869. litest_scale_axis(const struct litest_device *d, unsigned int axis, double val)
  2870. {
  2871. const struct input_absinfo *abs;
  2872. litest_assert_double_ge(val, 0.0);
  2873. /* major/minor must be able to beyond 100% for large fingers */
  2874. if (axis != ABS_MT_TOUCH_MAJOR && axis != ABS_MT_TOUCH_MINOR) {
  2875. litest_assert_double_le(val, 100.0);
  2876. }
  2877. abs = libevdev_get_abs_info(d->evdev, axis);
  2878. litest_assert_notnull(abs);
  2879. return (abs->maximum - abs->minimum) * val / 100.0 + abs->minimum;
  2880. }
  2881. static inline int
  2882. litest_scale_range(int min, int max, double val)
  2883. {
  2884. litest_assert_int_ge((int)val, 0);
  2885. litest_assert_int_le((int)val, 100);
  2886. return (max - min) * val / 100.0 + min;
  2887. }
  2888. int
  2889. litest_scale(const struct litest_device *d, unsigned int axis, double val)
  2890. {
  2891. int min, max;
  2892. litest_assert_double_ge(val, 0.0);
  2893. /* major/minor must be able to beyond 100% for large fingers */
  2894. if (axis != ABS_MT_TOUCH_MAJOR && axis != ABS_MT_TOUCH_MINOR)
  2895. litest_assert_double_le(val, 100.0);
  2896. if (axis <= ABS_Y) {
  2897. min = d->interface->min[axis];
  2898. max = d->interface->max[axis];
  2899. return litest_scale_range(min, max, val);
  2900. } else {
  2901. return litest_scale_axis(d, axis, val);
  2902. }
  2903. }
  2904. static inline int
  2905. auto_assign_pad_value(struct litest_device *dev, struct input_event *ev, double value)
  2906. {
  2907. const struct input_absinfo *abs;
  2908. if (ev->value != LITEST_AUTO_ASSIGN)
  2909. return value;
  2910. if (ev->type == EV_REL) {
  2911. switch (ev->code) {
  2912. case REL_WHEEL:
  2913. case REL_HWHEEL:
  2914. case REL_DIAL:
  2915. assert(fmod(value, 120.0) ==
  2916. 0.0); /* Fractions not supported yet */
  2917. return value / 120.0;
  2918. default:
  2919. return value;
  2920. }
  2921. } else if (ev->type != EV_ABS) {
  2922. return value;
  2923. }
  2924. abs = libevdev_get_abs_info(dev->evdev, ev->code);
  2925. litest_assert_notnull(abs);
  2926. if (ev->code == ABS_RX || ev->code == ABS_RY) {
  2927. double min = abs->minimum != 0 ? log2(abs->minimum) : 0,
  2928. max = abs->maximum != 0 ? log2(abs->maximum) : 0;
  2929. /* Value 0 is reserved for finger up, so a value of 0% is
  2930. * actually 1 */
  2931. if (value == 0.0) {
  2932. return 1;
  2933. } else {
  2934. value = litest_scale_range(min, max, value);
  2935. return pow(2, value);
  2936. }
  2937. } else {
  2938. return litest_scale_range(abs->minimum, abs->maximum, value);
  2939. }
  2940. }
  2941. static void
  2942. litest_pad_events(struct litest_device *d, struct input_event *evs, double value)
  2943. {
  2944. while (evs && (int16_t)evs->type != -1 && (int16_t)evs->code != -1) {
  2945. if (evs->type == EV_SYN && evs->code == SYN_REPORT)
  2946. value = 0;
  2947. value = auto_assign_pad_value(d, evs, value);
  2948. litest_event(d, evs->type, evs->code, value);
  2949. evs++;
  2950. }
  2951. }
  2952. void
  2953. litest_pad_ring_start(struct litest_device *d, double value)
  2954. {
  2955. litest_pad_events(d, d->interface->pad_ring_start_events, value);
  2956. }
  2957. void
  2958. litest_pad_ring_change(struct litest_device *d, double value)
  2959. {
  2960. litest_pad_events(d, d->interface->pad_ring_change_events, value);
  2961. }
  2962. void
  2963. litest_pad_ring_end(struct litest_device *d)
  2964. {
  2965. struct input_event *ev;
  2966. ev = d->interface->pad_ring_end_events;
  2967. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2968. litest_event(d, ev->type, ev->code, ev->value);
  2969. ev++;
  2970. }
  2971. }
  2972. void
  2973. litest_pad_strip_start(struct litest_device *d, double value)
  2974. {
  2975. litest_pad_events(d, d->interface->pad_strip_start_events, value);
  2976. }
  2977. void
  2978. litest_pad_strip_change(struct litest_device *d, double value)
  2979. {
  2980. litest_pad_events(d, d->interface->pad_strip_change_events, value);
  2981. }
  2982. void
  2983. litest_pad_strip_end(struct litest_device *d)
  2984. {
  2985. struct input_event *ev;
  2986. ev = d->interface->pad_strip_end_events;
  2987. while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
  2988. litest_event(d, ev->type, ev->code, ev->value);
  2989. ev++;
  2990. }
  2991. }
  2992. void
  2993. litest_wait_for_event(struct libinput *li)
  2994. {
  2995. return litest_wait_for_event_of_type(li, -1);
  2996. }
  2997. void
  2998. _litest_wait_for_event_of_type(struct libinput *li, const char *func, int lineno, ...)
  2999. {
  3000. va_list args;
  3001. enum libinput_event_type types[32] = { LIBINPUT_EVENT_NONE };
  3002. size_t ntypes = 0;
  3003. enum libinput_event_type type;
  3004. struct pollfd fds;
  3005. va_start(args, lineno);
  3006. type = va_arg(args, int);
  3007. while ((int)type != -1) {
  3008. litest_assert_int_gt(type, 0U);
  3009. litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
  3010. types[ntypes++] = type;
  3011. type = va_arg(args, int);
  3012. }
  3013. va_end(args);
  3014. fds.fd = libinput_get_fd(li);
  3015. fds.events = POLLIN;
  3016. fds.revents = 0;
  3017. const int timeout = 2000;
  3018. usec_t expiry = usec_from_uint64_t(0);
  3019. int rc = now_in_us(&expiry);
  3020. expiry = usec_add_millis(expiry, timeout);
  3021. litest_assert_errno_success(rc);
  3022. while (1) {
  3023. size_t i;
  3024. enum libinput_event_type type;
  3025. while ((type = libinput_next_event_type(li)) == LIBINPUT_EVENT_NONE) {
  3026. int rc = poll(&fds, 1, timeout);
  3027. litest_assert_errno_success(rc);
  3028. litest_assert_int_gt(rc, 0);
  3029. litest_dispatch(li);
  3030. }
  3031. if (type == LIBINPUT_EVENT_NONE) {
  3032. usec_t now;
  3033. now_in_us(&now);
  3034. if (usec_cmp(now, expiry) > 0) {
  3035. _litest_abort_msg(
  3036. NULL,
  3037. lineno,
  3038. func,
  3039. "Waited >%dms for events, but no events are pending",
  3040. timeout);
  3041. }
  3042. }
  3043. /* no event mask means wait for any event */
  3044. if (ntypes == 0)
  3045. return;
  3046. for (i = 0; i < ntypes; i++) {
  3047. if (type == types[i])
  3048. return;
  3049. }
  3050. _destroy_(libinput_event) *event = libinput_get_event(li);
  3051. if (verbose) {
  3052. litest_print_event(event, "Discarding event while waiting: ");
  3053. }
  3054. }
  3055. }
  3056. void
  3057. litest_drain_events(struct libinput *li)
  3058. {
  3059. do {
  3060. libinput_dispatch(li);
  3061. _destroy_(libinput_event) *event = libinput_get_event(li);
  3062. if (!event)
  3063. break;
  3064. if (verbose) {
  3065. litest_print_event(event, "litest: draining event: ");
  3066. }
  3067. } while (true);
  3068. }
  3069. void
  3070. _litest_drain_events_of_type(struct libinput *li, ...)
  3071. {
  3072. enum libinput_event_type type;
  3073. enum libinput_event_type types[32] = { LIBINPUT_EVENT_NONE };
  3074. size_t ntypes = 0;
  3075. va_list args;
  3076. va_start(args, li);
  3077. type = va_arg(args, int);
  3078. while ((int)type != -1) {
  3079. litest_assert_int_gt(type, 0U);
  3080. litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
  3081. types[ntypes++] = type;
  3082. type = va_arg(args, int);
  3083. }
  3084. va_end(args);
  3085. libinput_dispatch(li);
  3086. type = libinput_next_event_type(li);
  3087. while (type != LIBINPUT_EVENT_NONE) {
  3088. bool found = false;
  3089. type = libinput_next_event_type(li);
  3090. for (size_t i = 0; i < ntypes; i++) {
  3091. if (type == types[i]) {
  3092. found = true;
  3093. break;
  3094. }
  3095. }
  3096. if (!found)
  3097. return;
  3098. _destroy_(libinput_event) *event = libinput_get_event(li);
  3099. if (verbose)
  3100. litest_print_event(event, "litest: draining typed event: ");
  3101. libinput_dispatch(li);
  3102. }
  3103. }
  3104. const char *
  3105. litest_event_type_str(enum libinput_event_type type)
  3106. {
  3107. const char *str = NULL;
  3108. switch (type) {
  3109. case LIBINPUT_EVENT_NONE:
  3110. abort();
  3111. case LIBINPUT_EVENT_DEVICE_ADDED:
  3112. str = "ADDED";
  3113. break;
  3114. case LIBINPUT_EVENT_DEVICE_REMOVED:
  3115. str = "REMOVED";
  3116. break;
  3117. case LIBINPUT_EVENT_KEYBOARD_KEY:
  3118. str = "KEY";
  3119. break;
  3120. case LIBINPUT_EVENT_POINTER_MOTION:
  3121. str = "MOTION";
  3122. break;
  3123. case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
  3124. str = "ABSOLUTE";
  3125. break;
  3126. case LIBINPUT_EVENT_POINTER_BUTTON:
  3127. str = "BUTTON";
  3128. break;
  3129. case LIBINPUT_EVENT_POINTER_AXIS:
  3130. str = "AXIS";
  3131. break;
  3132. case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
  3133. str = "SCROLL_WHEEL";
  3134. break;
  3135. case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
  3136. str = "SCROLL_FINGER";
  3137. break;
  3138. case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
  3139. str = "SCROLL_CONTINUOUS";
  3140. break;
  3141. case LIBINPUT_EVENT_TOUCH_DOWN:
  3142. str = "TOUCH DOWN";
  3143. break;
  3144. case LIBINPUT_EVENT_TOUCH_UP:
  3145. str = "TOUCH UP";
  3146. break;
  3147. case LIBINPUT_EVENT_TOUCH_MOTION:
  3148. str = "TOUCH MOTION";
  3149. break;
  3150. case LIBINPUT_EVENT_TOUCH_CANCEL:
  3151. str = "TOUCH CANCEL";
  3152. break;
  3153. case LIBINPUT_EVENT_TOUCH_FRAME:
  3154. str = "TOUCH FRAME";
  3155. break;
  3156. case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
  3157. str = "GESTURE SWIPE BEGIN";
  3158. break;
  3159. case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
  3160. str = "GESTURE SWIPE UPDATE";
  3161. break;
  3162. case LIBINPUT_EVENT_GESTURE_SWIPE_END:
  3163. str = "GESTURE SWIPE END";
  3164. break;
  3165. case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
  3166. str = "GESTURE PINCH BEGIN";
  3167. break;
  3168. case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
  3169. str = "GESTURE PINCH UPDATE";
  3170. break;
  3171. case LIBINPUT_EVENT_GESTURE_PINCH_END:
  3172. str = "GESTURE PINCH END";
  3173. break;
  3174. case LIBINPUT_EVENT_GESTURE_HOLD_BEGIN:
  3175. str = "GESTURE HOLD BEGIN";
  3176. break;
  3177. case LIBINPUT_EVENT_GESTURE_HOLD_END:
  3178. str = "GESTURE HOLD END";
  3179. break;
  3180. case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
  3181. str = "TABLET TOOL AXIS";
  3182. break;
  3183. case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
  3184. str = "TABLET TOOL PROX";
  3185. break;
  3186. case LIBINPUT_EVENT_TABLET_TOOL_TIP:
  3187. str = "TABLET TOOL TIP";
  3188. break;
  3189. case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
  3190. str = "TABLET TOOL BUTTON";
  3191. break;
  3192. case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
  3193. str = "TABLET PAD BUTTON";
  3194. break;
  3195. case LIBINPUT_EVENT_TABLET_PAD_RING:
  3196. str = "TABLET PAD RING";
  3197. break;
  3198. case LIBINPUT_EVENT_TABLET_PAD_STRIP:
  3199. str = "TABLET PAD STRIP";
  3200. break;
  3201. case LIBINPUT_EVENT_TABLET_PAD_KEY:
  3202. str = "TABLET PAD KEY";
  3203. break;
  3204. case LIBINPUT_EVENT_TABLET_PAD_DIAL:
  3205. str = "TABLET PAD DIAL";
  3206. break;
  3207. case LIBINPUT_EVENT_SWITCH_TOGGLE:
  3208. str = "SWITCH TOGGLE";
  3209. break;
  3210. }
  3211. return str;
  3212. }
  3213. static const char *
  3214. litest_event_get_type_str(struct libinput_event *event)
  3215. {
  3216. return litest_event_type_str(libinput_event_get_type(event));
  3217. }
  3218. static void
  3219. litest_print_event(struct libinput_event *event, const char *message)
  3220. {
  3221. _autofree_ char *event_str = libinput_event_to_str(event, 0, NULL);
  3222. fprintf(stderr, "litest: %s %s\n", message, event_str);
  3223. }
  3224. void
  3225. _litest_assert_event_type_is_one_of(struct libinput_event *event,
  3226. const char *func,
  3227. int lineno,
  3228. ...)
  3229. {
  3230. va_list args;
  3231. enum libinput_event_type expected_type;
  3232. enum libinput_event_type actual_type = libinput_event_get_type(event);
  3233. bool match = false;
  3234. va_start(args, lineno);
  3235. expected_type = va_arg(args, int);
  3236. while ((int)expected_type != -1 && !match) {
  3237. match = (actual_type == expected_type);
  3238. expected_type = va_arg(args, int);
  3239. }
  3240. va_end(args);
  3241. if (match)
  3242. return;
  3243. fprintf(stderr,
  3244. "FAILED EVENT TYPE: %s: have %s (%d) but want ",
  3245. libinput_device_get_name(libinput_event_get_device(event)),
  3246. litest_event_get_type_str(event),
  3247. libinput_event_get_type(event));
  3248. va_start(args, lineno);
  3249. expected_type = va_arg(args, int);
  3250. while ((int)expected_type != -1) {
  3251. fprintf(stderr,
  3252. "%s (%d)",
  3253. litest_event_type_str(expected_type),
  3254. expected_type);
  3255. expected_type = va_arg(args, int);
  3256. if ((int)expected_type != -1)
  3257. fprintf(stderr, " || ");
  3258. }
  3259. va_end(args);
  3260. fprintf(stderr, "\n");
  3261. litest_print_event(event, "Wrong event is:");
  3262. litest_backtrace(func);
  3263. litest_runner_abort();
  3264. }
  3265. void
  3266. _litest_assert_event_type(struct libinput_event *event,
  3267. enum libinput_event_type want,
  3268. const char *func,
  3269. int lineno)
  3270. {
  3271. _litest_assert_event_type_is_one_of(event, func, lineno, want, -1);
  3272. }
  3273. void
  3274. _litest_assert_event_type_not_one_of(struct libinput_event *event,
  3275. const char *func,
  3276. int lineno,
  3277. ...)
  3278. {
  3279. va_list args;
  3280. enum libinput_event_type not_expected_type;
  3281. enum libinput_event_type actual_type = libinput_event_get_type(event);
  3282. bool match = false;
  3283. va_start(args, lineno);
  3284. not_expected_type = va_arg(args, int);
  3285. while ((int)not_expected_type != -1 && !match) {
  3286. match = (actual_type == not_expected_type);
  3287. not_expected_type = va_arg(args, int);
  3288. }
  3289. va_end(args);
  3290. if (!match)
  3291. return;
  3292. fprintf(stderr,
  3293. "FAILED EVENT TYPE: %s: have %s (%d) but didn't want that\n",
  3294. libinput_device_get_name(libinput_event_get_device(event)),
  3295. litest_event_get_type_str(event),
  3296. libinput_event_get_type(event));
  3297. litest_print_event(event, "\nWrong event is: ");
  3298. litest_backtrace(func);
  3299. litest_runner_abort();
  3300. }
  3301. void
  3302. _litest_assert_empty_queue(struct libinput *li, const char *func, int line)
  3303. {
  3304. bool empty_queue = true;
  3305. struct libinput_event *event;
  3306. _litest_checkpoint(func, line, ANSI_BRIGHT_CYAN, "asserting empty queue");
  3307. libinput_dispatch(li);
  3308. while ((event = libinput_get_event(li))) {
  3309. empty_queue = false;
  3310. litest_print_event(event, "Unexpected event: ");
  3311. libinput_event_destroy(event);
  3312. libinput_dispatch(li);
  3313. }
  3314. litest_assert(empty_queue);
  3315. }
  3316. static struct libevdev_uinput *
  3317. litest_create_uinput(const char *name,
  3318. const struct input_id *id,
  3319. const struct input_absinfo *abs_info,
  3320. const int *events)
  3321. {
  3322. struct libevdev_uinput *uinput;
  3323. _free_(libevdev) *dev = libevdev_new();
  3324. int type, code;
  3325. int rc;
  3326. const struct input_absinfo *abs;
  3327. const struct input_absinfo default_abs = { .value = 0,
  3328. .minimum = 0,
  3329. .maximum = 100,
  3330. .fuzz = 0,
  3331. .flat = 0,
  3332. .resolution = 100 };
  3333. /* See kernel commit 206f533a0a7c ("Input: uinput - reject requests with
  3334. * unreasonable number of slots") */
  3335. const struct input_absinfo default_abs_mt_slot = { .value = 0,
  3336. .minimum = 0,
  3337. .maximum = 64,
  3338. .fuzz = 0,
  3339. .flat = 0,
  3340. .resolution = 100 };
  3341. char buf[512];
  3342. litest_assert_ptr_notnull(dev);
  3343. snprintf(buf, sizeof(buf), "litest %s", name);
  3344. libevdev_set_name(dev, buf);
  3345. if (id) {
  3346. libevdev_set_id_bustype(dev, id->bustype);
  3347. libevdev_set_id_vendor(dev, id->vendor);
  3348. libevdev_set_id_product(dev, id->product);
  3349. libevdev_set_id_version(dev, id->version);
  3350. }
  3351. abs = abs_info;
  3352. while (abs && abs->value != -1) {
  3353. struct input_absinfo a = *abs;
  3354. /* abs_info->value is used for the code and may be outside
  3355. of [min, max] */
  3356. a.value = abs->minimum;
  3357. rc = libevdev_enable_event_code(dev, EV_ABS, abs->value, &a);
  3358. litest_assert_int_eq(rc, 0);
  3359. abs++;
  3360. }
  3361. while (events && (type = *events++) != -1 && (code = *events++) != -1) {
  3362. if (type == INPUT_PROP_MAX) {
  3363. rc = libevdev_enable_property(dev, code);
  3364. } else {
  3365. const struct input_absinfo *abs = (code == ABS_MT_SLOT)
  3366. ? &default_abs_mt_slot
  3367. : &default_abs;
  3368. rc = libevdev_enable_event_code(dev,
  3369. type,
  3370. code,
  3371. type == EV_ABS ? abs : NULL);
  3372. }
  3373. litest_assert_int_eq(rc, 0);
  3374. }
  3375. rc = libevdev_uinput_create_from_device(dev,
  3376. LIBEVDEV_UINPUT_OPEN_MANAGED,
  3377. &uinput);
  3378. litest_assert_msg(rc == 0,
  3379. "Failed to create uinput device: %s\n",
  3380. strerror(-rc));
  3381. return uinput;
  3382. }
  3383. struct libevdev_uinput *
  3384. litest_create_uinput_device_from_description(const char *name,
  3385. const struct input_id *id,
  3386. const struct input_absinfo *abs_info,
  3387. const int *events)
  3388. {
  3389. struct libevdev_uinput *uinput;
  3390. const char *syspath;
  3391. char path[PATH_MAX];
  3392. _unref_(udev_monitor) *udev_monitor = udev_setup_monitor();
  3393. _unref_(udev_device) *udev_device = NULL;
  3394. uinput = litest_create_uinput(name, id, abs_info, events);
  3395. syspath = libevdev_uinput_get_syspath(uinput);
  3396. snprintf(path, sizeof(path), "%s/event", syspath);
  3397. udev_device = udev_wait_for_device_event(udev_monitor, "add", path);
  3398. litest_assert(udev_device_get_property_value(udev_device, "ID_INPUT"));
  3399. return uinput;
  3400. }
  3401. static struct libevdev_uinput *
  3402. litest_create_uinput_abs_device_v(const char *name,
  3403. struct input_id *id,
  3404. const struct input_absinfo *abs,
  3405. va_list args)
  3406. {
  3407. int events[KEY_MAX * 2 + 2]; /* increase this if not sufficient */
  3408. int *event = events;
  3409. int type, code;
  3410. while ((type = va_arg(args, int)) != -1 && (code = va_arg(args, int)) != -1) {
  3411. *event++ = type;
  3412. *event++ = code;
  3413. litest_assert(event < &events[ARRAY_LENGTH(events) - 2]);
  3414. }
  3415. *event++ = -1;
  3416. *event++ = -1;
  3417. return litest_create_uinput_device_from_description(name, id, abs, events);
  3418. }
  3419. struct libevdev_uinput *
  3420. litest_create_uinput_abs_device(const char *name,
  3421. struct input_id *id,
  3422. const struct input_absinfo *abs,
  3423. ...)
  3424. {
  3425. struct libevdev_uinput *uinput;
  3426. va_list args;
  3427. va_start(args, abs);
  3428. uinput = litest_create_uinput_abs_device_v(name, id, abs, args);
  3429. va_end(args);
  3430. return uinput;
  3431. }
  3432. struct libevdev_uinput *
  3433. litest_create_uinput_device(const char *name, struct input_id *id, ...)
  3434. {
  3435. struct libevdev_uinput *uinput;
  3436. va_list args;
  3437. va_start(args, id);
  3438. uinput = litest_create_uinput_abs_device_v(name, id, NULL, args);
  3439. va_end(args);
  3440. return uinput;
  3441. }
  3442. struct libinput_event_pointer *
  3443. litest_is_button_event(struct libinput_event *event,
  3444. unsigned int button,
  3445. enum libinput_button_state state)
  3446. {
  3447. struct libinput_event_pointer *ptrev;
  3448. enum libinput_event_type type = LIBINPUT_EVENT_POINTER_BUTTON;
  3449. litest_assert_ptr_notnull(event);
  3450. litest_assert_event_type(event, type);
  3451. ptrev = libinput_event_get_pointer_event(event);
  3452. litest_assert_int_eq(libinput_event_pointer_get_button(ptrev), button);
  3453. litest_assert_int_eq(libinput_event_pointer_get_button_state(ptrev), state);
  3454. return ptrev;
  3455. }
  3456. struct libinput_event_pointer *
  3457. litest_is_axis_event(struct libinput_event *event,
  3458. enum libinput_event_type axis_type,
  3459. enum libinput_pointer_axis axis,
  3460. enum libinput_pointer_axis_source source)
  3461. {
  3462. struct libinput_event_pointer *ptrev;
  3463. litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
  3464. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
  3465. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
  3466. litest_assert_ptr_notnull(event);
  3467. litest_assert_event_type_is_one_of(event,
  3468. LIBINPUT_EVENT_POINTER_AXIS,
  3469. axis_type);
  3470. ptrev = libinput_event_get_pointer_event(event);
  3471. litest_assert(libinput_event_pointer_has_axis(ptrev, axis));
  3472. if (source != 0)
  3473. litest_assert_int_eq(litest_event_pointer_get_axis_source(ptrev),
  3474. source);
  3475. return ptrev;
  3476. }
  3477. bool
  3478. litest_is_high_res_axis_event(struct libinput_event *event)
  3479. {
  3480. litest_assert_event_type_is_one_of(event,
  3481. LIBINPUT_EVENT_POINTER_AXIS,
  3482. LIBINPUT_EVENT_POINTER_SCROLL_WHEEL,
  3483. LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
  3484. LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
  3485. return (libinput_event_get_type(event) != LIBINPUT_EVENT_POINTER_AXIS);
  3486. }
  3487. struct libinput_event_pointer *
  3488. litest_is_motion_event(struct libinput_event *event)
  3489. {
  3490. struct libinput_event_pointer *ptrev;
  3491. enum libinput_event_type type = LIBINPUT_EVENT_POINTER_MOTION;
  3492. double x, y, ux, uy;
  3493. litest_assert_ptr_notnull(event);
  3494. litest_assert_event_type(event, type);
  3495. ptrev = libinput_event_get_pointer_event(event);
  3496. x = libinput_event_pointer_get_dx(ptrev);
  3497. y = libinput_event_pointer_get_dy(ptrev);
  3498. ux = libinput_event_pointer_get_dx_unaccelerated(ptrev);
  3499. uy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
  3500. /* No 0 delta motion events */
  3501. litest_assert(x != 0.0 || y != 0.0 || ux != 0.0 || uy != 0.0);
  3502. return ptrev;
  3503. }
  3504. void
  3505. _litest_assert_key_event(struct libinput *li,
  3506. unsigned int key,
  3507. enum libinput_key_state state,
  3508. const char *func,
  3509. int lineno)
  3510. {
  3511. litest_wait_for_event(li);
  3512. _destroy_(libinput_event) *event = libinput_get_event(li);
  3513. litest_is_keyboard_event(event, key, state);
  3514. }
  3515. void
  3516. _litest_assert_button_event(struct libinput *li,
  3517. unsigned int button,
  3518. enum libinput_button_state state,
  3519. const char *func,
  3520. int line)
  3521. {
  3522. _litest_checkpoint(func,
  3523. line,
  3524. ANSI_CYAN,
  3525. "asserting button event %s (%d) state %d",
  3526. libevdev_event_code_get_name(EV_KEY, button),
  3527. button,
  3528. state);
  3529. litest_wait_for_event(li);
  3530. _destroy_(libinput_event) *event = libinput_get_event(li);
  3531. litest_is_button_event(event, button, state);
  3532. }
  3533. struct libinput_event_touch *
  3534. litest_is_touch_event(struct libinput_event *event, enum libinput_event_type type)
  3535. {
  3536. struct libinput_event_touch *touch;
  3537. litest_assert_ptr_notnull(event);
  3538. if (type == 0)
  3539. type = libinput_event_get_type(event);
  3540. switch (type) {
  3541. case LIBINPUT_EVENT_TOUCH_DOWN:
  3542. case LIBINPUT_EVENT_TOUCH_UP:
  3543. case LIBINPUT_EVENT_TOUCH_MOTION:
  3544. case LIBINPUT_EVENT_TOUCH_FRAME:
  3545. case LIBINPUT_EVENT_TOUCH_CANCEL:
  3546. litest_assert_event_type(event, type);
  3547. break;
  3548. default:
  3549. litest_abort_msg("%s: invalid touch type %d", __func__, type);
  3550. }
  3551. touch = libinput_event_get_touch_event(event);
  3552. return touch;
  3553. }
  3554. struct libinput_event_keyboard *
  3555. litest_is_keyboard_event(struct libinput_event *event,
  3556. unsigned int key,
  3557. enum libinput_key_state state)
  3558. {
  3559. struct libinput_event_keyboard *kevent;
  3560. enum libinput_event_type type = LIBINPUT_EVENT_KEYBOARD_KEY;
  3561. litest_assert_ptr_notnull(event);
  3562. litest_assert_event_type(event, type);
  3563. kevent = libinput_event_get_keyboard_event(event);
  3564. litest_assert_ptr_notnull(kevent);
  3565. litest_assert_int_eq(libinput_event_keyboard_get_key(kevent), key);
  3566. litest_assert_int_eq(libinput_event_keyboard_get_key_state(kevent), state);
  3567. return kevent;
  3568. }
  3569. struct libinput_event_gesture *
  3570. litest_is_gesture_event(struct libinput_event *event,
  3571. enum libinput_event_type type,
  3572. int nfingers)
  3573. {
  3574. struct libinput_event_gesture *gevent;
  3575. litest_assert_ptr_notnull(event);
  3576. litest_assert_event_type(event, type);
  3577. gevent = libinput_event_get_gesture_event(event);
  3578. litest_assert_ptr_notnull(gevent);
  3579. if (nfingers != -1)
  3580. litest_assert_int_eq(libinput_event_gesture_get_finger_count(gevent),
  3581. nfingers);
  3582. return gevent;
  3583. }
  3584. void
  3585. _litest_assert_gesture_event(struct libinput *li,
  3586. enum libinput_event_type type,
  3587. int nfingers,
  3588. const char *func,
  3589. int line)
  3590. {
  3591. _litest_checkpoint(func,
  3592. line,
  3593. ANSI_CYAN,
  3594. "asserting gesture event %s %dfg",
  3595. litest_event_type_str(type),
  3596. nfingers);
  3597. litest_wait_for_event(li);
  3598. _destroy_(libinput_event) *event = libinput_get_event(li);
  3599. litest_is_gesture_event(event, type, nfingers);
  3600. }
  3601. struct libinput_event_tablet_tool *
  3602. litest_is_tablet_event(struct libinput_event *event, enum libinput_event_type type)
  3603. {
  3604. struct libinput_event_tablet_tool *tevent;
  3605. litest_assert_ptr_notnull(event);
  3606. litest_assert_event_type(event, type);
  3607. tevent = libinput_event_get_tablet_tool_event(event);
  3608. litest_assert_ptr_notnull(tevent);
  3609. return tevent;
  3610. }
  3611. void
  3612. _litest_assert_tablet_button_event(struct libinput *li,
  3613. unsigned int button,
  3614. enum libinput_button_state state,
  3615. const char *func,
  3616. int lineno)
  3617. {
  3618. struct libinput_event_tablet_tool *tev;
  3619. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_BUTTON;
  3620. _litest_checkpoint(func,
  3621. lineno,
  3622. ANSI_CYAN,
  3623. "asserting tablet button event button %d down: %s",
  3624. button,
  3625. yesno(state));
  3626. litest_wait_for_event(li);
  3627. _destroy_(libinput_event) *event = libinput_get_event(li);
  3628. litest_assert_notnull(event);
  3629. litest_assert_event_type(event, type);
  3630. tev = libinput_event_get_tablet_tool_event(event);
  3631. litest_assert_int_eq(libinput_event_tablet_tool_get_button(tev), button);
  3632. litest_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev), state);
  3633. }
  3634. struct libinput_event_tablet_tool *
  3635. litest_is_proximity_event(struct libinput_event *event,
  3636. enum libinput_tablet_tool_proximity_state state)
  3637. {
  3638. struct libinput_event_tablet_tool *tev;
  3639. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY;
  3640. litest_assert_notnull(event);
  3641. litest_assert_event_type(event, type);
  3642. tev = libinput_event_get_tablet_tool_event(event);
  3643. litest_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tev),
  3644. state);
  3645. return tev;
  3646. }
  3647. double
  3648. litest_event_pointer_get_value(struct libinput_event_pointer *ptrev,
  3649. enum libinput_pointer_axis axis)
  3650. {
  3651. struct libinput_event *event;
  3652. enum libinput_event_type type;
  3653. event = libinput_event_pointer_get_base_event(ptrev);
  3654. type = libinput_event_get_type(event);
  3655. switch (type) {
  3656. case LIBINPUT_EVENT_POINTER_AXIS:
  3657. return libinput_event_pointer_get_axis_value(ptrev, axis);
  3658. case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
  3659. return libinput_event_pointer_get_scroll_value_v120(ptrev, axis);
  3660. case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
  3661. case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
  3662. return libinput_event_pointer_get_scroll_value(ptrev, axis);
  3663. default:
  3664. abort();
  3665. }
  3666. }
  3667. enum libinput_pointer_axis_source
  3668. litest_event_pointer_get_axis_source(struct libinput_event_pointer *ptrev)
  3669. {
  3670. struct libinput_event *event;
  3671. enum libinput_event_type type;
  3672. event = libinput_event_pointer_get_base_event(ptrev);
  3673. type = libinput_event_get_type(event);
  3674. if (type == LIBINPUT_EVENT_POINTER_AXIS)
  3675. return libinput_event_pointer_get_axis_source(ptrev);
  3676. switch (type) {
  3677. case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
  3678. return LIBINPUT_POINTER_AXIS_SOURCE_WHEEL;
  3679. case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
  3680. return LIBINPUT_POINTER_AXIS_SOURCE_FINGER;
  3681. case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
  3682. return LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS;
  3683. default:
  3684. abort();
  3685. }
  3686. }
  3687. void
  3688. _litest_assert_tablet_axis_event(struct libinput *li, const char *func, int lineno)
  3689. {
  3690. _litest_checkpoint(func, lineno, ANSI_CYAN, "asserting axis event");
  3691. litest_wait_for_event(li);
  3692. _destroy_(libinput_event) *event = libinput_get_event(li);
  3693. litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_AXIS);
  3694. }
  3695. void
  3696. _litest_assert_tablet_proximity_event(struct libinput *li,
  3697. enum libinput_tablet_tool_proximity_state state,
  3698. const char *func,
  3699. int lineno)
  3700. {
  3701. _litest_checkpoint(func,
  3702. lineno,
  3703. ANSI_CYAN,
  3704. "asserting proximity %s event",
  3705. state ? "in" : "out");
  3706. litest_wait_for_event(li);
  3707. _destroy_(libinput_event) *event = libinput_get_event(li);
  3708. litest_is_proximity_event(event, state);
  3709. }
  3710. void
  3711. _litest_assert_tablet_tip_event(struct libinput *li,
  3712. enum libinput_tablet_tool_tip_state state,
  3713. const char *func,
  3714. int lineno)
  3715. {
  3716. struct libinput_event_tablet_tool *tev;
  3717. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_TIP;
  3718. _litest_checkpoint(func,
  3719. lineno,
  3720. ANSI_CYAN,
  3721. "asserting tip %s event",
  3722. state ? "down" : "up");
  3723. litest_wait_for_event(li);
  3724. _destroy_(libinput_event) *event = libinput_get_event(li);
  3725. litest_assert_notnull(event);
  3726. litest_assert_event_type(event, type);
  3727. tev = libinput_event_get_tablet_tool_event(event);
  3728. litest_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tev), state);
  3729. }
  3730. struct libinput_event_tablet_pad *
  3731. litest_is_pad_button_event(struct libinput_event *event,
  3732. unsigned int button,
  3733. enum libinput_button_state state)
  3734. {
  3735. struct libinput_event_tablet_pad *p;
  3736. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_BUTTON;
  3737. litest_assert_ptr_notnull(event);
  3738. litest_assert_event_type(event, type);
  3739. p = libinput_event_get_tablet_pad_event(event);
  3740. litest_assert_ptr_notnull(p);
  3741. litest_assert_int_eq(libinput_event_tablet_pad_get_button_number(p), button);
  3742. litest_assert_int_eq(libinput_event_tablet_pad_get_button_state(p), state);
  3743. return p;
  3744. }
  3745. struct libinput_event_tablet_pad *
  3746. litest_is_pad_dial_event(struct libinput_event *event, unsigned int number)
  3747. {
  3748. struct libinput_event_tablet_pad *p;
  3749. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_DIAL;
  3750. litest_assert_ptr_notnull(event);
  3751. litest_assert_event_type(event, type);
  3752. p = libinput_event_get_tablet_pad_event(event);
  3753. litest_assert_int_eq(libinput_event_tablet_pad_get_dial_number(p), number);
  3754. return p;
  3755. }
  3756. struct libinput_event_tablet_pad *
  3757. litest_is_pad_ring_event(struct libinput_event *event,
  3758. unsigned int number,
  3759. enum libinput_tablet_pad_ring_axis_source source)
  3760. {
  3761. struct libinput_event_tablet_pad *p;
  3762. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_RING;
  3763. litest_assert_ptr_notnull(event);
  3764. litest_assert_event_type(event, type);
  3765. p = libinput_event_get_tablet_pad_event(event);
  3766. litest_assert_int_eq(libinput_event_tablet_pad_get_ring_number(p), number);
  3767. litest_assert_int_eq(libinput_event_tablet_pad_get_ring_source(p), source);
  3768. return p;
  3769. }
  3770. struct libinput_event_tablet_pad *
  3771. litest_is_pad_strip_event(struct libinput_event *event,
  3772. unsigned int number,
  3773. enum libinput_tablet_pad_strip_axis_source source)
  3774. {
  3775. struct libinput_event_tablet_pad *p;
  3776. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_STRIP;
  3777. litest_assert_ptr_notnull(event);
  3778. litest_assert_event_type(event, type);
  3779. p = libinput_event_get_tablet_pad_event(event);
  3780. litest_assert_int_eq(libinput_event_tablet_pad_get_strip_number(p), number);
  3781. litest_assert_int_eq(libinput_event_tablet_pad_get_strip_source(p), source);
  3782. return p;
  3783. }
  3784. struct libinput_event_tablet_pad *
  3785. litest_is_pad_key_event(struct libinput_event *event,
  3786. unsigned int key,
  3787. enum libinput_key_state state)
  3788. {
  3789. struct libinput_event_tablet_pad *p;
  3790. enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_KEY;
  3791. litest_assert(event != NULL);
  3792. litest_assert_event_type(event, type);
  3793. p = libinput_event_get_tablet_pad_event(event);
  3794. litest_assert(p != NULL);
  3795. litest_assert_int_eq(libinput_event_tablet_pad_get_key(p), key);
  3796. litest_assert_int_eq(libinput_event_tablet_pad_get_key_state(p), state);
  3797. return p;
  3798. }
  3799. struct libinput_event_switch *
  3800. litest_is_switch_event(struct libinput_event *event,
  3801. enum libinput_switch sw,
  3802. enum libinput_switch_state state)
  3803. {
  3804. struct libinput_event_switch *swev;
  3805. enum libinput_event_type type = LIBINPUT_EVENT_SWITCH_TOGGLE;
  3806. litest_assert_notnull(event);
  3807. litest_assert_event_type(event, type);
  3808. swev = libinput_event_get_switch_event(event);
  3809. litest_assert_int_eq(libinput_event_switch_get_switch(swev), sw);
  3810. litest_assert_int_eq(libinput_event_switch_get_switch_state(swev), state);
  3811. return swev;
  3812. }
  3813. void
  3814. _litest_assert_switch_event(struct libinput *li,
  3815. enum libinput_switch sw,
  3816. enum libinput_switch_state state,
  3817. const char *func,
  3818. int lineno)
  3819. {
  3820. litest_wait_for_event(li);
  3821. _destroy_(libinput_event) *event = libinput_get_event(li);
  3822. litest_is_switch_event(event, sw, state);
  3823. }
  3824. void
  3825. _litest_assert_pad_button_event(struct libinput *li,
  3826. unsigned int button,
  3827. enum libinput_button_state state,
  3828. const char *func,
  3829. int lineno)
  3830. {
  3831. litest_wait_for_event(li);
  3832. _destroy_(libinput_event) *event = libinput_get_event(li);
  3833. litest_is_pad_button_event(event, button, state);
  3834. }
  3835. void
  3836. _litest_assert_pad_key_event(struct libinput *li,
  3837. unsigned int key,
  3838. enum libinput_key_state state,
  3839. const char *func,
  3840. int lineno)
  3841. {
  3842. litest_wait_for_event(li);
  3843. _destroy_(libinput_event) *event = libinput_get_event(li);
  3844. litest_is_pad_key_event(event, key, state);
  3845. }
  3846. void
  3847. litest_assert_scroll(struct libinput *li,
  3848. enum libinput_event_type axis_type,
  3849. enum libinput_pointer_axis axis,
  3850. int minimum_movement)
  3851. {
  3852. struct libinput_event *event;
  3853. struct libinput_event_pointer *ptrev;
  3854. bool last_hi_res_event_found, last_low_res_event_found;
  3855. int value;
  3856. int nevents = 0;
  3857. litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
  3858. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
  3859. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
  3860. last_hi_res_event_found = false;
  3861. last_low_res_event_found = false;
  3862. event = libinput_get_event(li);
  3863. litest_assert_ptr_notnull(event);
  3864. while (event) {
  3865. int min = minimum_movement;
  3866. ptrev = litest_is_axis_event(event, axis_type, axis, 0);
  3867. nevents++;
  3868. /* Due to how the hysteresis works on touchpad
  3869. * events, the first event is reduced by the
  3870. * hysteresis margin that can cause the first event
  3871. * go under the minimum we expect for all other
  3872. * events */
  3873. if (nevents == 1)
  3874. min = minimum_movement / 2;
  3875. value = litest_event_pointer_get_value(ptrev, axis);
  3876. if (litest_is_high_res_axis_event(event)) {
  3877. litest_assert(!last_hi_res_event_found);
  3878. if (axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL)
  3879. min *= 120;
  3880. if (value == 0)
  3881. last_hi_res_event_found = true;
  3882. } else {
  3883. litest_assert(!last_low_res_event_found);
  3884. if (value == 0)
  3885. last_low_res_event_found = true;
  3886. }
  3887. if (value != 0) {
  3888. if (minimum_movement > 0)
  3889. litest_assert_int_ge(value, min);
  3890. else
  3891. litest_assert_int_le(value, min);
  3892. }
  3893. libinput_event_destroy(event);
  3894. event = libinput_get_event(li);
  3895. }
  3896. litest_assert(last_low_res_event_found);
  3897. litest_assert(last_hi_res_event_found);
  3898. }
  3899. void
  3900. litest_assert_axis_end_sequence(struct libinput *li,
  3901. enum libinput_event_type axis_type,
  3902. enum libinput_pointer_axis axis,
  3903. enum libinput_pointer_axis_source source)
  3904. {
  3905. struct libinput_event_pointer *ptrev;
  3906. bool last_hi_res_event_found, last_low_res_event_found;
  3907. double val;
  3908. int i;
  3909. litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
  3910. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
  3911. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
  3912. last_hi_res_event_found = false;
  3913. last_low_res_event_found = false;
  3914. /* both high and low scroll end events must be sent */
  3915. for (i = 0; i < 2; i++) {
  3916. _destroy_(libinput_event) *event = libinput_get_event(li);
  3917. ptrev = litest_is_axis_event(event, axis_type, axis, source);
  3918. val = litest_event_pointer_get_value(ptrev, axis);
  3919. litest_assert(val == 0.0);
  3920. if (litest_is_high_res_axis_event(event)) {
  3921. litest_assert(!last_hi_res_event_found);
  3922. last_hi_res_event_found = true;
  3923. } else {
  3924. litest_assert(!last_low_res_event_found);
  3925. last_low_res_event_found = true;
  3926. }
  3927. }
  3928. litest_assert(last_low_res_event_found);
  3929. litest_assert(last_hi_res_event_found);
  3930. }
  3931. void
  3932. _litest_assert_only_typed_events(struct libinput *li,
  3933. enum libinput_event_type type,
  3934. const char *func,
  3935. int line)
  3936. {
  3937. struct libinput_event *event;
  3938. litest_assert(type != LIBINPUT_EVENT_NONE);
  3939. _litest_checkpoint(func,
  3940. line,
  3941. ANSI_CYAN,
  3942. "asserting only typed events %s",
  3943. litest_event_type_str(type));
  3944. libinput_dispatch(li);
  3945. event = libinput_get_event(li);
  3946. litest_assert_notnull(event);
  3947. while (event) {
  3948. litest_assert_event_type(event, type);
  3949. libinput_event_destroy(event);
  3950. libinput_dispatch(li);
  3951. event = libinput_get_event(li);
  3952. }
  3953. }
  3954. void
  3955. litest_assert_only_axis_events(struct libinput *li, enum libinput_event_type axis_type)
  3956. {
  3957. struct libinput_event *event;
  3958. litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
  3959. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
  3960. axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
  3961. libinput_dispatch(li);
  3962. event = libinput_get_event(li);
  3963. litest_assert_notnull(event);
  3964. while (event) {
  3965. litest_assert_event_type_is_one_of(event,
  3966. LIBINPUT_EVENT_POINTER_AXIS,
  3967. axis_type);
  3968. libinput_event_destroy(event);
  3969. libinput_dispatch(li);
  3970. event = libinput_get_event(li);
  3971. }
  3972. }
  3973. void
  3974. litest_assert_no_typed_events(struct libinput *li, enum libinput_event_type type)
  3975. {
  3976. struct libinput_event *event;
  3977. litest_assert(type != LIBINPUT_EVENT_NONE);
  3978. libinput_dispatch(li);
  3979. event = libinput_get_event(li);
  3980. while (event) {
  3981. litest_assert_int_ne(libinput_event_get_type(event), type);
  3982. libinput_event_destroy(event);
  3983. libinput_dispatch(li);
  3984. event = libinput_get_event(li);
  3985. }
  3986. }
  3987. void
  3988. litest_assert_touch_sequence(struct libinput *li)
  3989. {
  3990. struct libinput_event *event;
  3991. struct libinput_event_touch *tev;
  3992. int slot;
  3993. event = libinput_get_event(li);
  3994. tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_DOWN);
  3995. slot = libinput_event_touch_get_slot(tev);
  3996. libinput_event_destroy(event);
  3997. event = libinput_get_event(li);
  3998. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
  3999. libinput_event_destroy(event);
  4000. event = libinput_get_event(li);
  4001. do {
  4002. tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
  4003. litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
  4004. libinput_event_destroy(event);
  4005. event = libinput_get_event(li);
  4006. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
  4007. libinput_event_destroy(event);
  4008. event = libinput_get_event(li);
  4009. litest_assert_notnull(event);
  4010. } while (libinput_event_get_type(event) != LIBINPUT_EVENT_TOUCH_UP);
  4011. tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_UP);
  4012. litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
  4013. libinput_event_destroy(event);
  4014. event = libinput_get_event(li);
  4015. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
  4016. libinput_event_destroy(event);
  4017. }
  4018. void
  4019. litest_assert_touch_motion_frame(struct libinput *li)
  4020. {
  4021. struct libinput_event *event;
  4022. /* expect at least one, but maybe more */
  4023. event = libinput_get_event(li);
  4024. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
  4025. libinput_event_destroy(event);
  4026. event = libinput_get_event(li);
  4027. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
  4028. libinput_event_destroy(event);
  4029. event = libinput_get_event(li);
  4030. while (event) {
  4031. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
  4032. libinput_event_destroy(event);
  4033. event = libinput_get_event(li);
  4034. litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
  4035. libinput_event_destroy(event);
  4036. event = libinput_get_event(li);
  4037. }
  4038. }
  4039. void
  4040. litest_assert_touch_down_frame(struct libinput *li)
  4041. {
  4042. _destroy_(libinput_event) *down = libinput_get_event(li);
  4043. litest_is_touch_event(down, LIBINPUT_EVENT_TOUCH_DOWN);
  4044. _destroy_(libinput_event) *frame = libinput_get_event(li);
  4045. litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
  4046. }
  4047. void
  4048. litest_assert_touch_up_frame(struct libinput *li)
  4049. {
  4050. _destroy_(libinput_event) *up = libinput_get_event(li);
  4051. litest_is_touch_event(up, LIBINPUT_EVENT_TOUCH_UP);
  4052. _destroy_(libinput_event) *frame = libinput_get_event(li);
  4053. litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
  4054. }
  4055. void
  4056. litest_assert_touch_cancel(struct libinput *li)
  4057. {
  4058. _destroy_(libinput_event) *cancel = libinput_get_event(li);
  4059. litest_is_touch_event(cancel, LIBINPUT_EVENT_TOUCH_CANCEL);
  4060. _destroy_(libinput_event) *frame = libinput_get_event(li);
  4061. litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
  4062. }
  4063. void
  4064. _litest_timeout(struct libinput *li, const char *func, int lineno, int millis)
  4065. {
  4066. if (li)
  4067. _litest_dispatch(li, func, lineno);
  4068. msleep(millis);
  4069. if (li)
  4070. _litest_dispatch(li, func, lineno);
  4071. }
  4072. void
  4073. _litest_assert_logcapture_no_errors(struct litest_logcapture *capture,
  4074. const char *file,
  4075. const char *func,
  4076. int line)
  4077. {
  4078. litest_assert_ptr_notnull(capture);
  4079. if (capture->errors &&
  4080. strv_for_each((const char **)capture->errors, is_actual_error, NULL)) {
  4081. _autofree_ char *errors = strv_join(capture->errors, "\n");
  4082. _litest_abort_msg(file,
  4083. line,
  4084. func,
  4085. "Unexpected errors in log capture:\n%s\n",
  4086. errors);
  4087. }
  4088. }
  4089. void
  4090. litest_logcapture_destroy(struct litest_logcapture *c)
  4091. {
  4092. strv_free(c->errors);
  4093. strv_free(c->infos);
  4094. strv_free(c->debugs);
  4095. strv_free(c->bugs);
  4096. free(c);
  4097. }
  4098. LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
  4099. static void
  4100. litest_log_handler_msgcapture(struct libinput *libinput,
  4101. enum libinput_log_priority pri,
  4102. const char *format,
  4103. va_list args)
  4104. {
  4105. struct litest_user_data *user_data = libinput_get_user_data(libinput);
  4106. struct litest_logcapture *capture = user_data->private;
  4107. const char *priority = NULL;
  4108. const char *color =
  4109. use_colors ? ANSI_RGB(255, 255, 70) ANSI_RGB_BG(40, 40, 40) : "";
  4110. switch (pri) {
  4111. case LIBINPUT_LOG_PRIORITY_ERROR:
  4112. priority = "error ";
  4113. break;
  4114. case LIBINPUT_LOG_PRIORITY_INFO:
  4115. priority = "info ";
  4116. break;
  4117. case LIBINPUT_LOG_PRIORITY_DEBUG:
  4118. priority = "debug ";
  4119. break;
  4120. }
  4121. _autofree_ char *message = strdup_vprintf(format, args);
  4122. fprintf(stderr,
  4123. "%slitest captured: %-6s%s %s",
  4124. color,
  4125. priority,
  4126. use_colors ? ANSI_NORMAL : "",
  4127. message);
  4128. if (strstr(message, "kernel bug: ") || strstr(message, "client bug: ") ||
  4129. strstr(message, "libinput bug: ") || strstr(message, "plugin bug: ")) {
  4130. capture->bugs = strv_append_strdup(capture->bugs, message);
  4131. }
  4132. switch (pri) {
  4133. case LIBINPUT_LOG_PRIORITY_ERROR:
  4134. capture->errors = strv_append_take(capture->errors, &message);
  4135. break;
  4136. case LIBINPUT_LOG_PRIORITY_INFO:
  4137. capture->infos = strv_append_take(capture->infos, &message);
  4138. break;
  4139. case LIBINPUT_LOG_PRIORITY_DEBUG:
  4140. capture->debugs = strv_append_take(capture->debugs, &message);
  4141. break;
  4142. }
  4143. }
  4144. struct litest_logcapture *
  4145. litest_logcapture_setup(struct libinput *li)
  4146. {
  4147. struct litest_logcapture *c = zalloc(sizeof(*c));
  4148. litest_context_set_user_data(li, c);
  4149. libinput_log_set_handler(li, litest_log_handler_msgcapture);
  4150. return c;
  4151. }
  4152. struct litest_logcapture *
  4153. litest_logcapture_remove(struct libinput *li, struct litest_logcapture *capture)
  4154. {
  4155. litest_restore_log_handler(li);
  4156. litest_logcapture_destroy(capture);
  4157. return NULL;
  4158. }
  4159. #define litest_with_logcapture(li_, capture_) \
  4160. for (struct litest_logcapture *capture_ = litest_logcapture_setup(li_); \
  4161. capture_ != NULL; capture_ = litest_logcapture_remove(li_, capture_))
  4162. void
  4163. _litest_assert_strv_substring(char **strv,
  4164. char *substring,
  4165. const char *file,
  4166. const char *func,
  4167. int line)
  4168. {
  4169. if (!strv) {
  4170. _litest_abort_msg(file,
  4171. line,
  4172. func,
  4173. "Expected substring '%s' but strv is NULL",
  4174. substring);
  4175. }
  4176. bool found = strv_find_substring(strv, substring, NULL);
  4177. if (!found) {
  4178. _autofree_ char *strv_str = strv_join(strv, "', '");
  4179. _litest_abort_msg(file,
  4180. line,
  4181. func,
  4182. "Expected substring '%s' not found in strv: ['%s']",
  4183. substring,
  4184. strv_str);
  4185. }
  4186. }
  4187. void
  4188. _litest_assert_strv_no_substring(char **strv,
  4189. char *substring,
  4190. const char *file,
  4191. const char *func,
  4192. int line)
  4193. {
  4194. if (!strv)
  4195. return;
  4196. size_t index;
  4197. bool found = strv_find_substring(strv, substring, &index);
  4198. if (found) {
  4199. _autofree_ char *strv_str = strv_join(strv, "', '");
  4200. _litest_abort_msg(
  4201. file,
  4202. line,
  4203. func,
  4204. "Unexpected substring '%s' present at index %zd in strv: ['%s']",
  4205. substring,
  4206. index,
  4207. strv_str);
  4208. }
  4209. }
  4210. void
  4211. litest_push_event_frame(struct litest_device *dev)
  4212. {
  4213. litest_assert_int_ge(dev->skip_ev_syn, 0);
  4214. dev->skip_ev_syn++;
  4215. }
  4216. void
  4217. litest_pop_event_frame(struct litest_device *dev)
  4218. {
  4219. litest_assert_int_gt(dev->skip_ev_syn, 0);
  4220. dev->skip_ev_syn--;
  4221. if (dev->skip_ev_syn == 0)
  4222. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  4223. }
  4224. void
  4225. litest_filter_event(struct litest_device *dev, unsigned int type, unsigned int code)
  4226. {
  4227. libevdev_disable_event_code(dev->evdev, type, code);
  4228. }
  4229. void
  4230. litest_unfilter_event(struct litest_device *dev, unsigned int type, unsigned int code)
  4231. {
  4232. /* would need an non-NULL argument for re-enabling, so simply abort
  4233. * until we need to be more sophisticated */
  4234. litest_assert_int_ne(type, (unsigned int)EV_ABS);
  4235. libevdev_enable_event_code(dev->evdev, type, code, NULL);
  4236. }
  4237. static void
  4238. send_abs_xy(struct litest_device *d, double x, double y)
  4239. {
  4240. struct input_event e;
  4241. int val;
  4242. e.type = EV_ABS;
  4243. e.code = ABS_X;
  4244. e.value = LITEST_AUTO_ASSIGN;
  4245. val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
  4246. litest_event(d, EV_ABS, ABS_X, val);
  4247. e.code = ABS_Y;
  4248. val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
  4249. litest_event(d, EV_ABS, ABS_Y, val);
  4250. }
  4251. static void
  4252. send_abs_mt_xy(struct litest_device *d, double x, double y)
  4253. {
  4254. struct input_event e;
  4255. int val;
  4256. e.type = EV_ABS;
  4257. e.code = ABS_MT_POSITION_X;
  4258. e.value = LITEST_AUTO_ASSIGN;
  4259. val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
  4260. litest_event(d, EV_ABS, ABS_MT_POSITION_X, val);
  4261. e.code = ABS_MT_POSITION_Y;
  4262. e.value = LITEST_AUTO_ASSIGN;
  4263. val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
  4264. litest_event(d, EV_ABS, ABS_MT_POSITION_Y, val);
  4265. }
  4266. void
  4267. litest_semi_mt_touch_down(struct litest_device *d,
  4268. struct litest_semi_mt *semi_mt,
  4269. unsigned int slot,
  4270. double x,
  4271. double y)
  4272. {
  4273. double t, l, r = 0, b = 0; /* top, left, right, bottom */
  4274. if (d->ntouches_down > 2 || slot > 1)
  4275. return;
  4276. if (d->ntouches_down == 1) {
  4277. l = x;
  4278. t = y;
  4279. } else {
  4280. int other = (slot + 1) % 2;
  4281. l = min(x, semi_mt->touches[other].x);
  4282. t = min(y, semi_mt->touches[other].y);
  4283. r = max(x, semi_mt->touches[other].x);
  4284. b = max(y, semi_mt->touches[other].y);
  4285. }
  4286. send_abs_xy(d, l, t);
  4287. litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
  4288. if (d->ntouches_down == 1)
  4289. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
  4290. send_abs_mt_xy(d, l, t);
  4291. if (d->ntouches_down == 2) {
  4292. litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
  4293. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
  4294. send_abs_mt_xy(d, r, b);
  4295. }
  4296. litest_event(d, EV_SYN, SYN_REPORT, 0);
  4297. semi_mt->touches[slot].x = x;
  4298. semi_mt->touches[slot].y = y;
  4299. }
  4300. void
  4301. litest_semi_mt_touch_move(struct litest_device *d,
  4302. struct litest_semi_mt *semi_mt,
  4303. unsigned int slot,
  4304. double x,
  4305. double y)
  4306. {
  4307. double t, l, r = 0, b = 0; /* top, left, right, bottom */
  4308. if (d->ntouches_down > 2 || slot > 1)
  4309. return;
  4310. if (d->ntouches_down == 1) {
  4311. l = x;
  4312. t = y;
  4313. } else {
  4314. int other = (slot + 1) % 2;
  4315. l = min(x, semi_mt->touches[other].x);
  4316. t = min(y, semi_mt->touches[other].y);
  4317. r = max(x, semi_mt->touches[other].x);
  4318. b = max(y, semi_mt->touches[other].y);
  4319. }
  4320. send_abs_xy(d, l, t);
  4321. litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
  4322. send_abs_mt_xy(d, l, t);
  4323. if (d->ntouches_down == 2) {
  4324. litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
  4325. send_abs_mt_xy(d, r, b);
  4326. }
  4327. litest_event(d, EV_SYN, SYN_REPORT, 0);
  4328. semi_mt->touches[slot].x = x;
  4329. semi_mt->touches[slot].y = y;
  4330. }
  4331. void
  4332. litest_semi_mt_touch_up(struct litest_device *d,
  4333. struct litest_semi_mt *semi_mt,
  4334. unsigned int slot)
  4335. {
  4336. /* note: ntouches_down is decreased before we get here */
  4337. if (d->ntouches_down >= 2 || slot > 1)
  4338. return;
  4339. litest_event(d, EV_ABS, ABS_MT_SLOT, d->ntouches_down);
  4340. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, -1);
  4341. /* if we have one finger left, send x/y coords for that finger left.
  4342. this is likely to happen with a real touchpad */
  4343. if (d->ntouches_down == 1) {
  4344. int other = (slot + 1) % 2;
  4345. send_abs_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
  4346. litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
  4347. send_abs_mt_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
  4348. }
  4349. litest_event(d, EV_SYN, SYN_REPORT, 0);
  4350. }
  4351. enum litest_mode
  4352. litest_parse_argv(int argc, char **argv, int *njobs_out)
  4353. {
  4354. enum {
  4355. OPT_EXIT_FIRST,
  4356. OPT_FILTER_TEST,
  4357. OPT_FILTER_DEVICE,
  4358. OPT_FILTER_GROUP,
  4359. OPT_FILTER_RANGEVAL,
  4360. OPT_FILTER_DEVICELESS,
  4361. OPT_FILTER_PARAMETER,
  4362. OPT_OUTPUT_FILE,
  4363. OPT_JOBS,
  4364. OPT_LIST,
  4365. OPT_VERBOSE,
  4366. };
  4367. static const struct option opts[] = {
  4368. { "filter-test", 1, 0, OPT_FILTER_TEST },
  4369. { "filter-device", 1, 0, OPT_FILTER_DEVICE },
  4370. { "filter-group", 1, 0, OPT_FILTER_GROUP },
  4371. { "filter-rangeval", 1, 0, OPT_FILTER_RANGEVAL },
  4372. { "filter-deviceless", 0, 0, OPT_FILTER_DEVICELESS },
  4373. { "filter-parameter", 1, 0, OPT_FILTER_PARAMETER },
  4374. { "output-file", 1, 0, OPT_OUTPUT_FILE },
  4375. { "exitfirst", 0, 0, OPT_EXIT_FIRST },
  4376. { "jobs", 1, 0, OPT_JOBS },
  4377. { "list", 0, 0, OPT_LIST },
  4378. { "verbose", 0, 0, OPT_VERBOSE },
  4379. { "help", 0, 0, 'h' },
  4380. { 0, 0, 0, 0 }
  4381. };
  4382. enum {
  4383. JOBS_DEFAULT,
  4384. JOBS_NONE,
  4385. JOBS_SINGLE,
  4386. JOBS_CUSTOM
  4387. } want_jobs = JOBS_DEFAULT;
  4388. char *jobs_env;
  4389. int jobs = 0;
  4390. /* If we are not running from the builddir, we assume we're running
  4391. * against the system as installed */
  4392. if (!builddir_lookup(NULL))
  4393. use_system_rules_quirks = true;
  4394. if (in_debugger)
  4395. want_jobs = JOBS_NONE;
  4396. if ((jobs_env = getenv("LITEST_JOBS"))) {
  4397. if (!safe_atoi(jobs_env, &jobs)) {
  4398. fprintf(stderr,
  4399. "LITEST_JOBS environment variable must be positive integer\n");
  4400. exit(EXIT_FAILURE);
  4401. }
  4402. }
  4403. while (1) {
  4404. int c;
  4405. int option_index = 0;
  4406. c = getopt_long(argc, argv, "j:x", opts, &option_index);
  4407. if (c == -1)
  4408. break;
  4409. switch (c) {
  4410. default:
  4411. case 'h':
  4412. printf("Usage: %s [--verbose] [--jobs] [--filter-...]\n"
  4413. "\n"
  4414. "Options:\n"
  4415. " -x | --exitfirst\n"
  4416. " Exit instantly on first failed test\n"
  4417. " --filter-test=.... \n"
  4418. " Glob to filter on test names\n"
  4419. " --filter-device=.... \n"
  4420. " Glob to filter on device names\n"
  4421. " --filter-group=.... \n"
  4422. " Glob to filter on test groups\n"
  4423. " --filter-rangeval=N \n"
  4424. " Only run tests with the given range value\n"
  4425. " --filter-deviceless \n"
  4426. " Only run tests that do not create test devices\n"
  4427. " --filter-parameter=param1:glob,param2:glob,... \n"
  4428. " Glob(s) to filter on the given parameters in their string "
  4429. "representation.\n"
  4430. " Boolean parameters are filtered via 'true' and 'false'.\n"
  4431. " --verbose\n"
  4432. " Enable verbose output\n"
  4433. " --jobs 8\n"
  4434. " Number of parallel test suites to run (default: 8).\n"
  4435. " This overrides the LITEST_JOBS environment variable.\n"
  4436. " --list\n"
  4437. " List all tests\n"
  4438. "\n"
  4439. "See the libinput-test-suite(1) man page for details.\n",
  4440. program_invocation_short_name);
  4441. exit(c != 'h');
  4442. break;
  4443. case OPT_FILTER_TEST:
  4444. filter_test = optarg;
  4445. if (want_jobs == JOBS_DEFAULT)
  4446. want_jobs = JOBS_SINGLE;
  4447. break;
  4448. case OPT_FILTER_DEVICE:
  4449. filter_device = optarg;
  4450. if (want_jobs == JOBS_DEFAULT)
  4451. want_jobs = JOBS_SINGLE;
  4452. break;
  4453. case OPT_FILTER_DEVICELESS:
  4454. run_deviceless = true;
  4455. break;
  4456. case OPT_FILTER_GROUP:
  4457. filter_group = optarg;
  4458. break;
  4459. case OPT_FILTER_RANGEVAL:
  4460. filter_rangeval = atoi(optarg);
  4461. break;
  4462. case OPT_FILTER_PARAMETER: {
  4463. size_t nelems;
  4464. _autostrvfree_ char **params =
  4465. strv_from_string(optarg, ",", &nelems);
  4466. const size_t max_filters = ARRAY_LENGTH(filter_params) - 1;
  4467. if (nelems >= max_filters) {
  4468. fprintf(stderr,
  4469. "Only %zd parameter filters are supported\n",
  4470. max_filters);
  4471. exit(1);
  4472. }
  4473. for (size_t i = 0; i < nelems; i++) {
  4474. size_t n;
  4475. _autostrvfree_ char **strv =
  4476. strv_from_string(params[i], ":", &n);
  4477. assert(n == 2);
  4478. const char *name = strv[0];
  4479. const char *glob = strv[1];
  4480. struct param_filter *f = &filter_params[i];
  4481. snprintf(f->name, sizeof(f->name), "%s", name);
  4482. snprintf(f->glob, sizeof(f->glob), "%s", glob);
  4483. }
  4484. break;
  4485. }
  4486. case 'j':
  4487. case OPT_JOBS:
  4488. jobs = atoi(optarg);
  4489. want_jobs = JOBS_CUSTOM;
  4490. break;
  4491. case OPT_LIST:
  4492. return LITEST_MODE_LIST;
  4493. case OPT_VERBOSE:
  4494. verbose = true;
  4495. break;
  4496. case OPT_OUTPUT_FILE:
  4497. outfile = fopen(optarg, "w+");
  4498. if (!outfile) {
  4499. fprintf(stderr, "Failed to open %s: %m\n", optarg);
  4500. exit(1);
  4501. }
  4502. break;
  4503. case 'x':
  4504. case OPT_EXIT_FIRST:
  4505. exit_first = true;
  4506. break;
  4507. }
  4508. }
  4509. switch (want_jobs) {
  4510. case JOBS_SINGLE:
  4511. jobs = 1;
  4512. break;
  4513. case JOBS_NONE:
  4514. jobs = 0;
  4515. break;
  4516. default:
  4517. break;
  4518. }
  4519. *njobs_out = jobs;
  4520. return LITEST_MODE_TEST;
  4521. }