xkbcommon.h 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809
  1. /*
  2. * For MIT-open-group:
  3. * Copyright 1985, 1987, 1990, 1998 The Open Group
  4. * Copyright 2008 Dan Nicholson
  5. *
  6. * For HPND:
  7. * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
  8. * SPDX-License-Identifier: HPND
  9. *
  10. * For MIT:
  11. * Copyright © 2009-2012 Daniel Stone
  12. * Copyright © 2012 Intel Corporation
  13. * Copyright © 2012 Ran Benita
  14. * Copyright © 2023-2026 Pierre Le Marre
  15. *
  16. * SPDX-License-Identifier: MIT-open-group AND HPND AND MIT
  17. *
  18. * Author: Daniel Stone <daniel@fooishbar.org>
  19. */
  20. #ifndef _XKBCOMMON_H_
  21. #define _XKBCOMMON_H_
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <xkbcommon/xkbcommon-errors.h>
  27. #include <xkbcommon/xkbcommon-names.h>
  28. #include <xkbcommon/xkbcommon-keysyms.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #if defined(__GNUC__) && !defined(__CYGWIN__)
  33. # define XKB_EXPORT __attribute__((visibility("default")))
  34. #elif defined(_WIN32)
  35. # define XKB_EXPORT __declspec(dllexport)
  36. #else
  37. # define XKB_EXPORT
  38. #endif
  39. /**
  40. * @file
  41. * Main libxkbcommon API.
  42. *
  43. * @brief Core API for keyboard keymap compilation and state processing.
  44. *
  45. * This header provides the primary public API for libxkbcommon. It exposes
  46. * facilities for:
  47. */
  48. /**
  49. * @struct xkb_context
  50. * @ingroup context
  51. * Opaque top level library context object.
  52. *
  53. * The context contains various general library data and state, like
  54. * logging level and include paths.
  55. *
  56. * Objects are created in a specific context, and multiple contexts may
  57. * coexist simultaneously. Objects from different contexts are completely
  58. * separated and do not share any memory or state.
  59. */
  60. struct xkb_context;
  61. /**
  62. * @struct xkb_keymap
  63. * @ingroup keymap
  64. * Opaque compiled keymap object.
  65. *
  66. * The keymap object holds all of the static keyboard information obtained
  67. * from compiling XKB files.
  68. *
  69. * A keymap is immutable after it is created (besides reference counts, etc.);
  70. * if you need to change it, you must create a new one.
  71. */
  72. struct xkb_keymap;
  73. /**
  74. * @struct xkb_machine
  75. * @ingroup state
  76. * Opaque XKB state machine object.
  77. *
  78. * `xkb_machine` is a [Mealy machine]<!-- -->: it is a finite-state machine that
  79. * takes a stream of raw key events – a pair ([keycode], [direction]) – as input,
  80. * and produces a stream of atomic [XKB events](@ref xkb_event) as output. Output
  81. * depends on *both* the input and the current internal state (active modifiers,
  82. * current layout, etc.).
  83. *
  84. * This is the authoritative object for *server-side* XKB processing.
  85. *
  86. * @note To query the resulting keyboard state (active modifiers, current
  87. * layout, LED states, etc.), pair this object with an `xkb_state` updated via
  88. * `xkb_state::xkb_state_update_event()`. The `xkb_state` object is the
  89. * *observable state* of the machine and provides the full query API.
  90. *
  91. * See @ref server-client-state for details.
  92. *
  93. * See the [example for a Wayland server](@ref quick-guide-wayland-server)
  94. * in the quick guide.
  95. *
  96. * @since 1.14.0
  97. *
  98. * [Mealy machine]: https://en.wikipedia.org/wiki/Mealy_machine
  99. * [keycode]: @ref xkb_keycode_t
  100. * [direction]: @ref xkb_key_direction
  101. * [keyboard events]: @ref xkb_event
  102. */
  103. struct xkb_machine;
  104. /**
  105. * @struct xkb_state
  106. * @ingroup state
  107. * Opaque keyboard state object.
  108. *
  109. * State objects contain the active state of a keyboard (or keyboards), such
  110. * as the currently effective layout and the active modifiers. Depending on
  111. * the use case, the state can be driven by raw key events or updated from
  112. * server serializations, and always exposes a query API for keysyms,
  113. * modifiers, layout and LEDs.
  114. *
  115. * This object serves 3 roles:
  116. * <dl>
  117. * <dt>*Client* API</dt>
  118. * <dd>
  119. * Update the state from server serializations via `xkb_state_update_mask()`,
  120. * then query it (keysyms, modifiers, layout, LEDs).
  121. *
  122. * Use the constructor `xkb_state_new_with_mode()` with
  123. * `::XKB_STATE_MODE_CLIENT`.
  124. *
  125. * See the [examples](@ref quick-guide-clients) in the quick guide.
  126. * </dd>
  127. * <dt>Server query companion</dt>
  128. * <dd>
  129. * Update via `xkb_state_update_event()` to expose the full query API
  130. * alongside an [`xkb_machine`](@ref xkb_machine): `xkb_machine` is the
  131. * [Mealy machine] that processes keyboard input; `xkb_state` is its
  132. * *observable state*, exposing the query API.
  133. *
  134. * Use the constructor `xkb_state_new_with_mode()` with
  135. * `::XKB_STATE_MODE_SERVER_QUERY`.
  136. *
  137. * See [examples](@ref quick-guide-wayland-server) in the quick guide.
  138. * </dd>
  139. * <dt>Legacy *server* API</dt>
  140. * <dd>
  141. * `xkb_state` is a [Mealy machine]<!-- -->: it is a finite-state machine that
  142. * takes a stream of raw key events – a pair ([keycode], [direction]) – as input,
  143. * and produces `xkb_state_component` delta with the previous state. Output
  144. * depends on *both* the input and the current internal state (active modifiers,
  145. * current layout, etc.).
  146. *
  147. * - Create it using the constructor `xkb_state_new_with_mode()` with
  148. * `::XKB_STATE_MODE_SERVER` or the legacy `xkb_state_new()`.
  149. * - Update it via `xkb_state_update_key()` and `xkb_state_update_synthetic()`.
  150. * - Query it directly via the API common to the client and companion use cases.
  151. *
  152. * @deprecated Since 1.14.0, prefer `xkb_machine` for new server
  153. * applications.
  154. * </dd>
  155. * </dl>
  156. *
  157. * See @ref server-client-state and @ref xkb_state_mode for further details.
  158. *
  159. * [Mealy machine]: https://en.wikipedia.org/wiki/Mealy_machine
  160. */
  161. struct xkb_state;
  162. /**
  163. * A number used to represent a physical key on a keyboard.
  164. *
  165. * A standard PC-compatible keyboard might have 102 keys. An appropriate
  166. * keymap would assign each of them a keycode, by which the user should
  167. * refer to the key throughout the library.
  168. *
  169. * Historically, the X11 protocol, and consequentially the XKB protocol,
  170. * assign only 8 bits for keycodes. This limits the number of different
  171. * keys that can be used simultaneously in a single keymap to 256
  172. * (disregarding other limitations). This library does not share this limit;
  173. * keycodes beyond 255 (*extended* keycodes) are not treated specially.
  174. * Keymaps and applications which are compatible with X11 should not use
  175. * these keycodes.
  176. *
  177. * The values of specific keycodes are determined by the keymap and the
  178. * underlying input system. For example, with an X11-compatible keymap
  179. * and Linux evdev scan codes (see `linux/input.h`), a fixed offset is used:
  180. *
  181. * The keymap defines a canonical name for each key, plus possible aliases.
  182. * Historically, the XKB protocol restricts these names to at most 4 (ASCII)
  183. * characters, but this library does not share this limit.
  184. *
  185. * @code
  186. * xkb_keycode_t keycode_A = KEY_A + 8;
  187. * @endcode
  188. *
  189. * @sa xkb_keycode_is_legal_ext() xkb_keycode_is_legal_x11()
  190. */
  191. typedef uint32_t xkb_keycode_t;
  192. /**
  193. * A number used to represent the symbols generated from a key on a keyboard.
  194. *
  195. * A key, represented by a keycode, may generate different symbols according
  196. * to keyboard state. For example, on a QWERTY keyboard, pressing the key
  197. * labled \<A\> generates the symbol ‘a’. If the Shift key is held, it
  198. * generates the symbol ‘A’. If a different layout is used, say Greek,
  199. * it generates the symbol ‘α’. And so on.
  200. *
  201. * Each such symbol is represented by a *keysym* (short for “key symbol”).
  202. * Note that keysyms are somewhat more general, in that they can also represent
  203. * some “function”, such as “Left” or “Right” for the arrow keys. For more
  204. * information, see: @ref keysym-encoding "".
  205. *
  206. * Specifically named keysyms can be found in the
  207. * xkbcommon/xkbcommon-keysyms.h header file. Their name does not include
  208. * the `XKB_KEY_` prefix.
  209. *
  210. * Besides those, any Unicode/ISO&nbsp;10646 character in the range `U+0100` to
  211. * `U+10FFFF` can be represented by a keysym value in the range `0x01000100` to
  212. * `0x0110FFFF`. The name of Unicode keysyms is `U<codepoint>`, e.g. `UA1B2`.
  213. *
  214. * The name of other unnamed keysyms is the hexadecimal representation of
  215. * their value, e.g. `0xabcd1234`.
  216. *
  217. * Keysym names are case-sensitive.
  218. *
  219. * @note **Encoding:** Keysyms are 32-bit integers with the 3 most significant
  220. * bits always set to zero. Thus valid keysyms are in the range
  221. * `0 .. 0x1fffffff` = @ref XKB_KEYSYM_MAX.
  222. * See @ref keysym-encoding "" for further details.
  223. *
  224. * [encoding]: https://www.x.org/releases/current/doc/xproto/x11protocol.html#keysym_encoding
  225. *
  226. * @ingroup keysyms
  227. * @sa `::XKB_KEYSYM_MAX`
  228. * @sa @ref keysym-encoding
  229. * @sa @ref predefined-keysyms
  230. */
  231. typedef uint32_t xkb_keysym_t;
  232. /**
  233. * Index of a keyboard layout.
  234. *
  235. * The layout index is a state component which determines which <em>keyboard
  236. * layout</em> is active. These may be different alphabets, different key
  237. * arrangements, etc.
  238. *
  239. * Layout indices are consecutive. The first layout has index 0.
  240. *
  241. * Each layout is not required to have a name, and the names are not
  242. * guaranteed to be unique (though they are usually provided and unique).
  243. * Therefore, it is not safe to use the name as a unique identifier for a
  244. * layout. Layout names are case-sensitive.
  245. *
  246. * Layout names are specified in the layout’s definition, for example
  247. * “English (US)”. These are different from the (conventionally) short names
  248. * which are used to locate the layout, for example `us` or `us(intl)`. These
  249. * names are not present in a compiled keymap.
  250. *
  251. * If the user selects layouts from a list generated from the XKB registry
  252. * (using libxkbregistry or directly), and this metadata is needed later on, it
  253. * is recommended to store it along with the keymap.
  254. *
  255. * Layouts are also called *groups* by XKB.
  256. *
  257. * @sa xkb_keymap::xkb_keymap_num_layouts()
  258. * @sa xkb_keymap::xkb_keymap_num_layouts_for_key()
  259. */
  260. typedef uint32_t xkb_layout_index_t;
  261. /** A mask of layout indices. */
  262. typedef uint32_t xkb_layout_mask_t;
  263. /**
  264. * Index of a shift level.
  265. *
  266. * Any key, in any layout, can have several <em>shift levels</em>. Each
  267. * shift level can assign different keysyms to the key. The shift level
  268. * to use is chosen according to the current keyboard state; for example,
  269. * if no keys are pressed, the first level may be used; if the Left Shift
  270. * key is pressed, the second; if Num Lock is pressed, the third; and
  271. * many such combinations are possible (see `xkb_mod_index_t`).
  272. *
  273. * Level indices are consecutive. The first level has index 0.
  274. */
  275. typedef uint32_t xkb_level_index_t;
  276. /**
  277. * Index of a modifier.
  278. *
  279. * A @e modifier is a state component which changes the way keys are
  280. * interpreted. A keymap defines a set of modifiers, such as Alt, Shift,
  281. * Num Lock or Meta, and specifies which keys may @e activate which
  282. * modifiers (in a many-to-many relationship, i.e. a key can activate
  283. * several modifiers, and a modifier may be activated by several keys.
  284. * Different keymaps do this differently).
  285. *
  286. * When retrieving the keysyms for a key, the active modifier set is
  287. * consulted; this determines the correct shift level to use within the
  288. * currently active layout (see `xkb_level_index_t`).
  289. *
  290. * Modifier indices are consecutive. The first modifier has index 0.
  291. *
  292. * Each modifier must have a name, and the names are unique. Therefore, it
  293. * is safe to use the name as a unique identifier for a modifier. The names
  294. * of some common modifiers are provided in the `xkbcommon/xkbcommon-names.h`
  295. * header file. Modifier names are case-sensitive.
  296. *
  297. * @sa `xkb_keymap::xkb_keymap_num_mods()`
  298. * @sa `xkb_mod_mask_t`
  299. */
  300. typedef uint32_t xkb_mod_index_t;
  301. /**
  302. * @parblock
  303. * A mask of [modifier encodings](@ref modifiers-encoding), i.e. a mask
  304. * of [real modifiers] indices.
  305. * @endparblock
  306. *
  307. * @warning A [modifier encoding](@ref modifiers-encoding) is **opaque**.
  308. *
  309. * @warning Computing a modifier mask from its index works for [real modifiers]
  310. * but does *not* work in general for [virtual modifiers].
  311. * Therefore the encoding of a modifier should be retrieved *only* using
  312. * `xkb_keymap::xkb_keymap_mod_get_mask()` or
  313. * `xkb_keymap::xkb_keymap_mod_get_mask2()`.
  314. *
  315. * @sa `xkb_keymap::xkb_keymap_mod_get_mask()`
  316. * @sa `xkb_keymap::xkb_keymap_mod_get_mask2()`
  317. *
  318. * [real modifiers]: @ref real-modifier-def
  319. * [virtual modifiers]: @ref virtual-modifier-def
  320. */
  321. typedef uint32_t xkb_mod_mask_t;
  322. /**
  323. * Index of a keyboard LED.
  324. *
  325. * LEDs are logical objects which may be @e active or @e inactive. They
  326. * typically correspond to the lights on the keyboard. Their state is
  327. * determined by the current keyboard state.
  328. *
  329. * LED indices are non-consecutive. The first LED has index 0.
  330. *
  331. * Each LED must have a name, and the names are unique. Therefore,
  332. * it is safe to use the name as a unique identifier for a LED. The names
  333. * of some common LEDs are provided in the `xkbcommon/xkbcommon-names.h`
  334. * header file. LED names are case-sensitive.
  335. *
  336. * @warning A given keymap may specify an exact index for a given LED.
  337. * Therefore, LED indexing is not necessarily sequential, as opposed to
  338. * modifiers and layouts. This means that when iterating over the LEDs
  339. * in a keymap using e.g. `xkb_keymap::xkb_keymap_num_leds()`, some indices might
  340. * be invalid.
  341. * Given such an index, functions like `xkb_keymap::xkb_keymap_led_get_name()`
  342. * will return `NULL`, and `xkb_state::xkb_state_led_index_is_active()` will
  343. * return -1.
  344. *
  345. * LEDs are also called *indicators* by XKB.
  346. *
  347. * @sa `xkb_keymap::xkb_keymap_num_leds()`
  348. */
  349. typedef uint32_t xkb_led_index_t;
  350. /** A mask of LED indices. */
  351. typedef uint32_t xkb_led_mask_t;
  352. /** Invalid keycode */
  353. #define XKB_KEYCODE_INVALID (0xffffffff)
  354. /** Invalid layout index */
  355. #define XKB_LAYOUT_INVALID (0xffffffff)
  356. /** Invalid level index */
  357. #define XKB_LEVEL_INVALID (0xffffffff)
  358. /** Invalid modifier index */
  359. #define XKB_MOD_INVALID (0xffffffff)
  360. /** Invalid LED index */
  361. #define XKB_LED_INVALID (0xffffffff)
  362. /** Maximum legal keycode */
  363. #define XKB_KEYCODE_MAX (0xffffffff - 1)
  364. /**
  365. * Maximum keysym value
  366. *
  367. * @since 1.6.0
  368. * @sa xkb_keysym_t
  369. * @ingroup keysyms
  370. */
  371. #define XKB_KEYSYM_MAX 0x1fffffff
  372. /**
  373. * Test whether a value is a valid extended keycode.
  374. * @sa xkb_keycode_t
  375. **/
  376. #define xkb_keycode_is_legal_ext(key) ((key) <= XKB_KEYCODE_MAX)
  377. /**
  378. * Test whether a value is a valid X11 keycode.
  379. * @sa xkb_keycode_t
  380. */
  381. #define xkb_keycode_is_legal_x11(key) ((key) >= 8 && (key) <= 255)
  382. /**
  383. * @defgroup rules-api Rules
  384. * Utility functions related to *rules*, whose purpose is introduced in:
  385. * @ref xkb-the-config "".
  386. *
  387. * @{
  388. */
  389. /**
  390. * @struct xkb_rmlvo_builder
  391. * Opaque [RMLVO] configuration object.
  392. *
  393. * It denotes the configuration values by which a user picks a keymap.
  394. *
  395. * @see [Introduction to RMLVO][RMLVO]
  396. * @see @ref rules-api ""
  397. * @since 1.11.0
  398. *
  399. * [RMLVO]: @ref RMLVO-intro
  400. */
  401. struct xkb_rmlvo_builder;
  402. /**
  403. * @enum xkb_rmlvo_builder_flags
  404. * Flags for `xkb_rmlvo_builder_new()`.
  405. *
  406. * @since 1.11.0
  407. */
  408. enum xkb_rmlvo_builder_flags {
  409. /**
  410. * Do not apply any flags.
  411. *
  412. * @since 1.11.0
  413. */
  414. XKB_RMLVO_BUILDER_NO_FLAGS = 0
  415. };
  416. /**
  417. * Create a new [RMLVO] builder.
  418. *
  419. * @param[in] context The context in which to create the builder.
  420. * @param[in] rules The ruleset.
  421. * If `NULL` or the empty string `""`, a default value is used.
  422. * If the `XKB_DEFAULT_RULES` environment variable is set, it is used
  423. * as the default. Otherwise the system default is used.
  424. * @param[in] model The keyboard model.
  425. * If `NULL` or the empty string `""`, a default value is used.
  426. * If the `XKB_DEFAULT_MODEL` environment variable is set, it is used
  427. * as the default. Otherwise the system default is used.
  428. * @param[in] flags Optional flags for the builder, or 0.
  429. *
  430. * @returns A `xkb_rmlvo_builder`, or `NULL` if the compilation failed.
  431. *
  432. * @see `xkb_rule_names` for a detailed description of @p rules and @p model.
  433. * @since 1.11.0
  434. * @memberof xkb_rmlvo_builder
  435. *
  436. * [RMLVO]: @ref RMLVO-intro
  437. */
  438. XKB_EXPORT struct xkb_rmlvo_builder*
  439. xkb_rmlvo_builder_new(struct xkb_context *context,
  440. const char *rules, const char *model,
  441. enum xkb_rmlvo_builder_flags flags);
  442. /**
  443. * Append a layout to the given [RMLVO] builder.
  444. *
  445. * @param[in,out] rmlvo The builder to modify.
  446. * @param[in] layout The name of the layout.
  447. * @param[in] variant The name of the layout variant, or `NULL` to
  448. * select the default variant.
  449. * @param[in] options An array of options to apply only to this
  450. * layout, or `NULL` if there is no such options.
  451. * @param[in] options_len The length of @p options.
  452. *
  453. * @note The options are only effectual if the corresponding ruleset has the
  454. * proper rules to handle them as *layout-specific* options.
  455. * @note See `rxkb_option_is_layout_specific()` to query whether an option
  456. * supports the layout-specific feature.
  457. *
  458. * @returns `true` if the call succeeded, otherwise `false`.
  459. *
  460. * @since 1.11.0
  461. * @memberof xkb_rmlvo_builder
  462. *
  463. * [RMLVO]: @ref RMLVO-intro
  464. */
  465. XKB_EXPORT bool
  466. xkb_rmlvo_builder_append_layout(struct xkb_rmlvo_builder *rmlvo,
  467. const char *layout, const char *variant,
  468. const char* const* options, size_t options_len);
  469. /**
  470. * Append an option to the given [RMLVO] builder.
  471. *
  472. * @param[in,out] rmlvo The builder to modify.
  473. * @param[in] option The name of the option.
  474. *
  475. * @returns `true` if the call succeeded, otherwise `false`.
  476. *
  477. * @since 1.11.0
  478. * @memberof xkb_rmlvo_builder
  479. *
  480. * [RMLVO]: @ref RMLVO-intro
  481. */
  482. XKB_EXPORT bool
  483. xkb_rmlvo_builder_append_option(struct xkb_rmlvo_builder *rmlvo,
  484. const char *option);
  485. /**
  486. * Take a new reference on a [RMLVO] builder.
  487. *
  488. * @param[in] rmlvo The builder to reference.
  489. *
  490. * @returns The passed in builder.
  491. *
  492. * @since 1.11.0
  493. * @memberof xkb_rmlvo_builder
  494. *
  495. * [RMLVO]: @ref RMLVO-intro
  496. */
  497. XKB_EXPORT struct xkb_rmlvo_builder *
  498. xkb_rmlvo_builder_ref(struct xkb_rmlvo_builder *rmlvo);
  499. /**
  500. * Release a reference on a [RMLVO] builder, and possibly free it.
  501. *
  502. * @param[in] rmlvo The builder. If it is `NULL`, this function does nothing.
  503. *
  504. * @since 1.11.0
  505. * @memberof xkb_rmlvo_builder
  506. *
  507. * [RMLVO]: @ref RMLVO-intro
  508. */
  509. XKB_EXPORT void
  510. xkb_rmlvo_builder_unref(struct xkb_rmlvo_builder *rmlvo);
  511. /**
  512. * @struct xkb_rule_names
  513. * Names to compile a keymap with, also known as [RMLVO].
  514. *
  515. * The names are the common configuration values by which a user picks
  516. * a keymap.
  517. *
  518. * If the entire struct is `NULL`, then each field is taken to be `NULL`.
  519. * You should prefer passing `NULL` instead of choosing your own defaults.
  520. *
  521. * @see [Introduction to RMLVO][RMLVO]
  522. * @see @ref rules-api ""
  523. *
  524. * [RMLVO]: @ref RMLVO-intro
  525. */
  526. struct xkb_rule_names {
  527. /**
  528. * The rules file to use. The rules file describes how to interpret
  529. * the values of the model, layout, variant and options fields.
  530. *
  531. * If `NULL` or the empty string `""`, a default value is used.
  532. * If the `XKB_DEFAULT_RULES` environment variable is set, it is used
  533. * as the default. Otherwise the system default is used.
  534. */
  535. const char *rules;
  536. /**
  537. * The keyboard model by which to interpret keycodes and LEDs.
  538. *
  539. * If `NULL` or the empty string `""`, a default value is used.
  540. * If the `XKB_DEFAULT_MODEL` environment variable is set, it is used
  541. * as the default. Otherwise the system default is used.
  542. */
  543. const char *model;
  544. /**
  545. * A comma separated list of layouts (languages) to include in the
  546. * keymap.
  547. *
  548. * If `NULL` or the empty string `""`, a default value is used.
  549. * If the `XKB_DEFAULT_LAYOUT` environment variable is set, it is used
  550. * as the default. Otherwise the system default is used.
  551. */
  552. const char *layout;
  553. /**
  554. * A comma separated list of variants, one per layout, which may
  555. * modify or augment the respective layout in various ways.
  556. *
  557. * Generally, should either be empty or have the same number of values
  558. * as the number of layouts. You may use empty values as in `intl,,neo`.
  559. *
  560. * If `NULL` or the empty string `""`, and a default value is also used
  561. * for the layout, a default value is used. Otherwise no variant is
  562. * used.
  563. * If the `XKB_DEFAULT_VARIANT` environment variable is set, it is used
  564. * as the default. Otherwise the system default is used.
  565. */
  566. const char *variant;
  567. /**
  568. * A comma separated list of options, through which the user specifies
  569. * non-layout related preferences, like which key combinations are used
  570. * for switching layouts, or which key is the Compose key.
  571. *
  572. * If `NULL`, a default value is used. If the empty string `""`, no
  573. * options are used.
  574. * If the `XKB_DEFAULT_OPTIONS` environment variable is set, it is used
  575. * as the default. Otherwise the system default is used.
  576. *
  577. * Each option can additionally have a *layout index specifier*, so that it
  578. * applies only if matching the given layout. The index is specified by
  579. * appending `!` immediately after the option name, then the 1-indexed
  580. * target layout in decimal format: e.g. `ns:option!2`. When no layout is
  581. * specified, it matches any layout.
  582. *
  583. * @note The layout index specifier is only effectual if the corresponding
  584. * ruleset has the proper rules to handle the option as *layout-specific*.
  585. * @note See `rxkb_option_is_layout_specific()` to query whether an option
  586. * supports the layout-specific feature.
  587. *
  588. * @since 1.11.0: Layout index specifier using `!`.
  589. */
  590. const char *options;
  591. };
  592. /**
  593. * @struct xkb_component_names
  594. * Keymap components, also known as [KcCGST].
  595. *
  596. * The components are the result of the [RMLVO] resolution.
  597. *
  598. * @see [Introduction to RMLVO][RMLVO]
  599. * @see [Introduction to KcCGST][KcCGST]
  600. * @see @ref rules-api ""
  601. *
  602. * [RMLVO]: @ref RMLVO-intro
  603. * [KcCGST]: @ref KcCGST-intro
  604. */
  605. struct xkb_component_names {
  606. char *keycodes;
  607. char *compatibility;
  608. char *geometry;
  609. char *symbols;
  610. char *types;
  611. };
  612. /**
  613. * Resolve [RMLVO] names to [KcCGST] components.
  614. *
  615. * This function is used primarily for *debugging*. See
  616. * `xkb_keymap::xkb_keymap_new_from_names2()` for creating keymaps from
  617. * [RMLVO] names.
  618. *
  619. * @param[in] context The context in which to resolve the names.
  620. * @param[in] rmlvo_in The [RMLVO] names to use.
  621. * @param[out] rmlvo_out The [RMLVO] names actually used after resolving
  622. * missing values.
  623. * @param[out] components_out The [KcCGST] components resulting of the [RMLVO]
  624. * resolution.
  625. *
  626. * @c rmlvo_out and @c components_out can be omitted by using `NULL`, but not
  627. * both.
  628. *
  629. * If @c components_out is not `NULL`, it is filled with dynamically-allocated
  630. * strings that should be freed by the caller.
  631. *
  632. * @returns `true` if the [RMLVO] names could be resolved, `false` otherwise.
  633. *
  634. * @see [Introduction to RMLVO][RMLVO]
  635. * @see [Introduction to KcCGST][KcCGST]
  636. * @see xkb_rule_names
  637. * @see xkb_component_names
  638. * @see xkb_keymap::xkb_keymap_new_from_names2()
  639. *
  640. * @since 1.9.0
  641. * @memberof xkb_component_names
  642. *
  643. * [RMLVO]: @ref RMLVO-intro
  644. * [KcCGST]: @ref KcCGST-intro
  645. */
  646. XKB_EXPORT bool
  647. xkb_components_names_from_rules(struct xkb_context *context,
  648. const struct xkb_rule_names *rmlvo_in,
  649. struct xkb_rule_names *rmlvo_out,
  650. struct xkb_component_names *components_out);
  651. /** @} */
  652. /**
  653. * @defgroup keysyms Keysyms
  654. * Utility functions related to [*keysyms*](@ref xkb_keysym_t) (short for
  655. * “key symbols”).
  656. *
  657. * @sa keysym-encoding
  658. * @sa predefined-keysyms
  659. *
  660. * @{
  661. */
  662. /**
  663. * @page keysym-transformations Keysym Transformations
  664. *
  665. * Keysym translation is subject to several *keysym transformations*,
  666. * as described in the XKB specification. These are:
  667. *
  668. * <dl>
  669. * <dt>Capitalization transformation</dt>
  670. * <dd>
  671. * If the **Caps Lock** [modifier] is
  672. * active and was not consumed by the translation process, keysyms
  673. * are transformed to their upper-case form (if applicable).
  674. * Similarly, the UTF-8/UTF-32 string produced is capitalized.
  675. *
  676. * This is described in:
  677. * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
  678. * </dd>
  679. * <dt>Control transformation</dt>
  680. * <dd>
  681. * If the **Control** [modifier] is active and was not consumed by the
  682. * translation process, the string produced is transformed to its matching
  683. * [ASCII control character]<!-- --> (if applicable). Keysyms are not affected.
  684. *
  685. * This is described in:
  686. * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
  687. * </dd>
  688. * </dl>
  689. *
  690. * Each relevant function discusses which transformations it performs.
  691. *
  692. * These transformations are not applicable when a key produces multiple
  693. * keysyms.
  694. *
  695. * [modifier]: @ref modifier-def
  696. * [ASCII control character]: https://en.wikipedia.org/wiki/C0_and_C1_control_codes#ASCII
  697. */
  698. /**
  699. * Get the name of a keysym.
  700. *
  701. * For a description of how keysyms are named, see @ref xkb_keysym_t.
  702. *
  703. * @param[in] keysym The keysym.
  704. * @param[out] buffer A string buffer to write the name into.
  705. * @param[in] size Capacity of the buffer.
  706. *
  707. * @warning If the buffer passed is too small, the string is truncated
  708. * (though still `NULL`-terminated); a size of at least 64 bytes is recommended.
  709. *
  710. * @returns The number of bytes in the name, excluding the `NULL` byte. If
  711. * the keysym is invalid, returns -1.
  712. *
  713. * You may check if truncation has occurred by comparing the return value
  714. * with the length of buffer, similarly to the `snprintf(3)` function.
  715. *
  716. * @sa `xkb_keysym_t`
  717. */
  718. XKB_EXPORT int
  719. xkb_keysym_get_name(xkb_keysym_t keysym, char *buffer, size_t size);
  720. /**
  721. * @enum xkb_keysym_flags
  722. * Flags for xkb_keysym_from_name().
  723. */
  724. enum xkb_keysym_flags {
  725. /** Do not apply any flags. */
  726. XKB_KEYSYM_NO_FLAGS = 0,
  727. /** Find keysym by case-insensitive search. */
  728. XKB_KEYSYM_CASE_INSENSITIVE = (1 << 0)
  729. };
  730. /**
  731. * Get a keysym from its name.
  732. *
  733. * @param[in] name The name of a keysym. See remarks in `xkb_keysym_get_name()`;
  734. * this function will accept any name returned by that function.
  735. * @param[in] flags A set of flags controlling how the search is done. If
  736. * invalid flags are passed, this will fail with `XKB_KEY_NoSymbol`.
  737. *
  738. * If you use the `::XKB_KEYSYM_CASE_INSENSITIVE` flag and two keysym names
  739. * differ only by case, then the lower-case keysym name is returned. For
  740. * instance, for `XKB_KEY_a` and `XKB_KEY_A`, this function would return
  741. * `XKB_KEY_a` for the case-insensitive search. If this functionality is needed,
  742. * it is recommended to first call this function without this flag; and if that
  743. * fails, only then to try with this flag, while possibly warning the user
  744. * he had misspelled the name, and might get wrong results.
  745. *
  746. * Case folding is done according to the C locale; the current locale is not
  747. * consulted.
  748. *
  749. * @returns The keysym. If the name is invalid, returns `XKB_KEY_NoSymbol`.
  750. *
  751. * @sa xkb_keysym_t
  752. * @since 1.9.0: Enable support for [C0 and C1 control characters] in the Unicode
  753. * notation.
  754. *
  755. * [C0 and C1 control characters]: https://en.wikipedia.org/wiki/C0_and_C1_control_codes
  756. */
  757. XKB_EXPORT xkb_keysym_t
  758. xkb_keysym_from_name(const char *name, enum xkb_keysym_flags flags);
  759. /**
  760. * Get the keysym corresponding to a *single* Unicode/UTF-8 encoded codepoint.
  761. *
  762. * @param[in] buffer A buffer to read the UTF-8 encoded codepoint from.
  763. * @param[in] size Capacity of @p buffer.
  764. * @returns The keysym corresponding to the specified Unicode
  765. * codepoint, or `XKB_KEY_NoSymbol` if there is none.
  766. *
  767. * This function is the inverse of `xkb_keysym_to_utf8()`. In cases
  768. * where a single codepoint corresponds to multiple keysyms, returns
  769. * the keysym with the lowest value.
  770. *
  771. * Unicode codepoints which do not have a special (legacy) keysym
  772. * encoding use a direct encoding scheme. These keysyms don’t usually
  773. * have an associated keysym constant (`XKB_KEY_*`).
  774. *
  775. * @sa `xkb_keysym_to_utf8()`
  776. * @since 1.14.0
  777. */
  778. XKB_EXPORT xkb_keysym_t
  779. xkb_utf8_to_keysym(const char *buffer, size_t size);
  780. /**
  781. * Get the Unicode/UTF-8 representation of a keysym.
  782. *
  783. * @param[in] keysym The keysym.
  784. * @param[out] buffer A buffer to write the UTF-8 string into.
  785. * @param[in] size Capacity of @p buffer. Must be at least 5.
  786. *
  787. * @returns The number of bytes written to the buffer (including the
  788. * terminating byte). If the keysym does not have a Unicode
  789. * representation, returns 0. If the buffer is too small, returns -1.
  790. *
  791. * This function does not perform any @ref keysym-transformations.
  792. * Therefore, prefer to use `xkb_state::xkb_state_key_get_utf8()` if possible.
  793. *
  794. * @sa `xkb_state::xkb_state_key_get_utf8()`
  795. */
  796. XKB_EXPORT int
  797. xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size);
  798. /**
  799. * Get the Unicode/UTF-32 representation of a keysym.
  800. *
  801. * @returns The Unicode/UTF-32 representation of keysym, which is also
  802. * compatible with UCS-4. If the keysym does not have a Unicode
  803. * representation, returns 0.
  804. *
  805. * This function does not perform any @ref keysym-transformations.
  806. * Therefore, prefer to use `xkb_state::xkb_state_key_get_utf32()` if possible.
  807. *
  808. * @sa `xkb_state::xkb_state_key_get_utf32()`
  809. */
  810. XKB_EXPORT uint32_t
  811. xkb_keysym_to_utf32(xkb_keysym_t keysym);
  812. /**
  813. * Get the keysym corresponding to a Unicode/UTF-32 codepoint.
  814. *
  815. * @returns The keysym corresponding to the specified Unicode
  816. * codepoint, or `XKB_KEY_NoSymbol` if there is none.
  817. *
  818. * This function is the inverse of `xkb_keysym_to_utf32()`. In cases
  819. * where a single codepoint corresponds to multiple keysyms, returns
  820. * the keysym with the lowest value.
  821. *
  822. * Unicode codepoints which do not have a special (legacy) keysym
  823. * encoding use a direct encoding scheme. These keysyms don’t usually
  824. * have an associated keysym constant (`XKB_KEY_*`).
  825. *
  826. * @sa `xkb_keysym_to_utf32()`
  827. * @since 1.0.0
  828. * @since 1.9.0: Enable support for all noncharacters.
  829. */
  830. XKB_EXPORT xkb_keysym_t
  831. xkb_utf32_to_keysym(uint32_t codepoint);
  832. /**
  833. * Convert a keysym to its *uppercase* form.
  834. *
  835. * If there is no such form, the keysym is returned unchanged.
  836. *
  837. * The conversion rules are the *simple* (i.e. one-to-one) Unicode case
  838. * mappings (with some exceptions, see hereinafter) and do not depend
  839. * on the locale. If you need the special case mappings (i.e. not
  840. * one-to-one or locale-dependent), prefer to work with the Unicode
  841. * representation instead, when possible.
  842. *
  843. * Exceptions to the Unicode mappings:
  844. *
  845. * | Lower keysym | Lower letter | Upper keysym | Upper letter | Comment |
  846. * | ------------ | ------------ | ------------ | ------------ | ------- |
  847. * | `ssharp` | `U+00DF`: ß | `SSHARP` | `U+1E9E`: ẞ | [Council for German Orthography] |
  848. *
  849. * [Council for German Orthography]: https://www.rechtschreibrat.com/regeln-und-woerterverzeichnis/
  850. *
  851. * @since 0.8.0: Initial implementation, based on `libX11`.
  852. * @since 1.8.0: Use Unicode 16.0 mappings for complete Unicode coverage.
  853. * @since 1.12.0: Update to Unicode 17.0.
  854. */
  855. XKB_EXPORT xkb_keysym_t
  856. xkb_keysym_to_upper(xkb_keysym_t keysym);
  857. /**
  858. * Convert a keysym to its *lowercase* form.
  859. *
  860. * If there is no such form, the keysym is returned unchanged.
  861. *
  862. * The conversion rules are the *simple* (i.e. one-to-one) Unicode case
  863. * mappings and do not depend on the locale. If you need the special
  864. * case mappings (i.e. not one-to-one or locale-dependent), prefer to
  865. * work with the Unicode representation instead, when possible.
  866. *
  867. * @since 0.8.0: Initial implementation, based on `libX11`.
  868. * @since 1.8.0: Use Unicode 16.0 mappings for complete Unicode coverage.
  869. * @since 1.12.0: Update to Unicode 17.0.
  870. */
  871. XKB_EXPORT xkb_keysym_t
  872. xkb_keysym_to_lower(xkb_keysym_t keysym);
  873. /** @} */
  874. /**
  875. * @defgroup context Library Context
  876. * Creating, destroying and using library contexts.
  877. *
  878. * Every keymap compilation request must have a context associated with
  879. * it. The context keeps around state such as the include path.
  880. *
  881. * @{
  882. */
  883. /**
  884. * @page envvars Environment Variables
  885. *
  886. * The user may set some environment variables which affect the library:
  887. *
  888. * - `XKB_CONFIG_ROOT`, `XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH`,
  889. * `XKB_CONFIG_VERSIONED_EXTENSIONS_PATH`, `XKB_CONFIG_EXTRA_PATH`,
  890. * `XDG_CONFIG_DIR`, `HOME` - see @ref include-path.
  891. * - `XKB_LOG_LEVEL` - see `xkb_context::xkb_context_set_log_level()`.
  892. * - `XKB_LOG_VERBOSITY` - see `xkb_context::xkb_context_set_log_verbosity()`.
  893. * - `XKB_DEFAULT_RULES`, `XKB_DEFAULT_MODEL`, `XKB_DEFAULT_LAYOUT`,
  894. * `XKB_DEFAULT_VARIANT`, `XKB_DEFAULT_OPTIONS` - see `xkb_rule_names`.
  895. */
  896. /**
  897. * @enum xkb_context_flags
  898. * Flags for context creation.
  899. */
  900. enum xkb_context_flags {
  901. /** Do not apply any context flags. */
  902. XKB_CONTEXT_NO_FLAGS = 0,
  903. /**
  904. * Create this context with an empty include path.
  905. *
  906. * This may be useful e.g.:
  907. * - to have full control over the included paths;
  908. * - for clients that do not need to access the XKB directories, e.g.
  909. * if only retrieving keymap from the Wayland or X server. It avoids
  910. * potential issues with directory access permissions.
  911. */
  912. XKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0),
  913. /**
  914. * Don’t take RMLVO names from the environment.
  915. *
  916. * @since 0.3.0
  917. */
  918. XKB_CONTEXT_NO_ENVIRONMENT_NAMES = (1 << 1),
  919. /**
  920. * Disable the use of secure_getenv for this context, so that privileged
  921. * processes can use environment variables. Client uses at their own risk.
  922. *
  923. * @since 1.5.0
  924. */
  925. XKB_CONTEXT_NO_SECURE_GETENV = (1 << 2)
  926. };
  927. /**
  928. * Create a new context.
  929. *
  930. * @param[in] flags Optional flags for the context, or 0.
  931. *
  932. * @returns A new context, or `NULL` on failure.
  933. *
  934. * @memberof xkb_context
  935. */
  936. XKB_EXPORT struct xkb_context *
  937. xkb_context_new(enum xkb_context_flags flags);
  938. /**
  939. * Take a new reference on a context.
  940. *
  941. * @param[in] context The context object.
  942. *
  943. * @returns The passed in context.
  944. *
  945. * @memberof xkb_context
  946. */
  947. XKB_EXPORT struct xkb_context *
  948. xkb_context_ref(struct xkb_context *context);
  949. /**
  950. * Release a reference on a context, and possibly free it.
  951. *
  952. * @param[in] context The context. If it is `NULL`, this function does nothing.
  953. *
  954. * @memberof xkb_context
  955. */
  956. XKB_EXPORT void
  957. xkb_context_unref(struct xkb_context *context);
  958. /**
  959. * Store custom user data in the context.
  960. *
  961. * This may be useful in conjunction with `xkb_context_set_log_fn()`
  962. * or other callbacks.
  963. *
  964. * @param[in,out] context The context object.
  965. * @param[in] user_data User data object.
  966. *
  967. * @memberof xkb_context
  968. */
  969. XKB_EXPORT void
  970. xkb_context_set_user_data(struct xkb_context *context, void *user_data);
  971. /**
  972. * Retrieves stored user data from the context.
  973. *
  974. * @param[in,out] context The context object.
  975. *
  976. * @returns The stored user data. If the user data wasn’t set, or the
  977. * passed in context is `NULL`, returns `NULL`.
  978. *
  979. * This may be useful to access private user data from callbacks like a
  980. * custom logging function.
  981. *
  982. * @memberof xkb_context
  983. **/
  984. XKB_EXPORT void *
  985. xkb_context_get_user_data(struct xkb_context *context);
  986. /** @} */
  987. /**
  988. * @defgroup include-path Include Paths
  989. * Manipulating the include paths in a context.
  990. *
  991. * The include paths are the file-system paths that are searched when an
  992. * include statement is encountered during keymap compilation.
  993. *
  994. * The default include paths are, in that lookup order:
  995. *
  996. * <dl>
  997. * <dt>User</dt>
  998. * <dd>
  999. * - The path `$XDG_CONFIG_HOME/xkb`, where `$XDG_CONFIG_HOME` is the value of
  1000. * the environment variable `XDG_CONFIG_HOME`, with the usual fallback to
  1001. * `$HOME/.config/` if unset.
  1002. *
  1003. * See @ref custom-configuration "" for further information.
  1004. * - @deprecated The *legacy* path `$HOME/.xkb`, where `$HOME` is the value of
  1005. * the environment variable `HOME`.
  1006. * <!-- [HACK] blank required by Doxygen -->
  1007. *
  1008. * </dd>
  1009. * <dt>System</dt>
  1010. * <dd>
  1011. * - The `XKB_CONFIG_EXTRA_PATH` environment variable, if defined, otherwise the
  1012. * system configuration directory, defined at library configuration time
  1013. * (usually `/etc/xkb`).
  1014. *
  1015. * One can adapt the @ref custom-configuration "" instructions by replacing
  1016. * `$XDG_CONFIG_HOME` with the system configuration directory in the
  1017. * file locations.
  1018. * - Each subdirectory of each XKB extensions directory (versioned, then
  1019. * unversioned if no corresponding versioned subdirectory), listed in
  1020. * lexicographic order. The extensions directories are defined by the
  1021. * environment variables `XKB_CONFIG_VERSIONED_EXTENSIONS_PATH` and
  1022. * `XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH` and default to the system XKB
  1023. * root extensions directories, defined at library configuration time (usually
  1024. * `/usr/share/xkeyboard-config-<VERSION>.d` and
  1025. * `/usr/share/xkeyboard-config.d`).
  1026. *
  1027. * See @ref packaging-keyboard-layouts "" for further information.
  1028. * - The `XKB_CONFIG_ROOT` environment variable, if defined, otherwise
  1029. * the system XKB root, defined at library configuration time
  1030. * (usually `/usr/share/xkeyboard-config-<VERSION>` or `/usr/share/X11/xkb`).
  1031. *
  1032. * @warning Do not modify the system XKB root files, because they will be
  1033. * overwritten by any update of the `xkeyboard-config`/`xkb-data` package.
  1034. * - Since 1.12.2: if the previous path failed, it fallbacks to the *legacy X11
  1035. * path* defined at compilation time (usually `/usr/share/X11/xkb`). This
  1036. * fallback is skipped is `XKB_CONFIG_ROOT` is explicitly set to an empty
  1037. * string.
  1038. * </dd>
  1039. * </dl>
  1040. *
  1041. * @{
  1042. */
  1043. /**
  1044. * Append a new entry to the context’s include path.
  1045. *
  1046. * @returns 1 on success, or 0 if the include path could not be added or is
  1047. * inaccessible.
  1048. *
  1049. * @memberof xkb_context
  1050. */
  1051. XKB_EXPORT int
  1052. xkb_context_include_path_append(struct xkb_context *context, const char *path);
  1053. /**
  1054. * Append the default include paths to the context’s include path.
  1055. *
  1056. * @returns 1 on success, or 0 if no default include path could be added.
  1057. *
  1058. * @memberof xkb_context
  1059. */
  1060. XKB_EXPORT int
  1061. xkb_context_include_path_append_default(struct xkb_context *context);
  1062. /**
  1063. * Reset the context’s include path to the default.
  1064. *
  1065. * Removes all entries from the context’s include path, and inserts the
  1066. * default paths.
  1067. *
  1068. * @returns 1 on success, or 0 if the primary include path could not be added.
  1069. *
  1070. * @memberof xkb_context
  1071. */
  1072. XKB_EXPORT int
  1073. xkb_context_include_path_reset_defaults(struct xkb_context *context);
  1074. /**
  1075. * Remove all entries from the context’s include path.
  1076. *
  1077. * @memberof xkb_context
  1078. */
  1079. XKB_EXPORT void
  1080. xkb_context_include_path_clear(struct xkb_context *context);
  1081. /**
  1082. * Get the number of paths in the context’s include path.
  1083. *
  1084. * @memberof xkb_context
  1085. */
  1086. XKB_EXPORT unsigned int
  1087. xkb_context_num_include_paths(struct xkb_context *context);
  1088. /**
  1089. * Get a specific include path from the context’s include path.
  1090. *
  1091. * @returns The include path at the specified index. If the index is
  1092. * invalid, returns `NULL`.
  1093. *
  1094. * @memberof xkb_context
  1095. */
  1096. XKB_EXPORT const char *
  1097. xkb_context_include_path_get(struct xkb_context *context, unsigned int index);
  1098. /** @} */
  1099. /**
  1100. * @defgroup logging Logging Handling
  1101. * Manipulating how logging from this library is handled.
  1102. *
  1103. * @{
  1104. */
  1105. /**
  1106. * @enum xkb_log_level
  1107. * Specifies a logging level.
  1108. */
  1109. enum xkb_log_level {
  1110. XKB_LOG_LEVEL_CRITICAL = 10, /**< Log critical internal errors only. */
  1111. XKB_LOG_LEVEL_ERROR = 20, /**< Log all errors. */
  1112. XKB_LOG_LEVEL_WARNING = 30, /**< Log warnings and errors. */
  1113. XKB_LOG_LEVEL_INFO = 40, /**< Log information, warnings, and errors. */
  1114. XKB_LOG_LEVEL_DEBUG = 50 /**< Log everything. */
  1115. };
  1116. /**
  1117. * Set the current logging level.
  1118. *
  1119. * @param[in,out] context The context in which to set the logging level.
  1120. * @param[in] level The logging level to use. Only messages from this
  1121. * level and below will be logged.
  1122. *
  1123. * The default level is `::XKB_LOG_LEVEL_ERROR`. The environment variable
  1124. * `XKB_LOG_LEVEL`, if set in the time the context was created, overrides the
  1125. * default value. It may be specified as a level number or name.
  1126. *
  1127. * @memberof xkb_context
  1128. */
  1129. XKB_EXPORT void
  1130. xkb_context_set_log_level(struct xkb_context *context,
  1131. enum xkb_log_level level);
  1132. /**
  1133. * Get the current logging level.
  1134. *
  1135. * @memberof xkb_context
  1136. */
  1137. XKB_EXPORT enum xkb_log_level
  1138. xkb_context_get_log_level(struct xkb_context *context);
  1139. /**
  1140. * Sets the current logging verbosity.
  1141. *
  1142. * The library can generate a number of warnings which are not helpful to
  1143. * ordinary users of the library. The verbosity may be increased if more
  1144. * information is desired (e.g. when developing a new keymap).
  1145. *
  1146. * The default verbosity is 0. The environment variable `XKB_LOG_VERBOSITY`,
  1147. * if set in the time the context was created, overrides the default value.
  1148. *
  1149. * @param[in,out] context The context in which to use the set verbosity.
  1150. * @param[in] verbosity The verbosity to use. Currently used values are
  1151. * 1 to 10, higher values being more verbose. 0 would result in no verbose
  1152. * messages being logged.
  1153. *
  1154. * Most verbose messages are of level `::XKB_LOG_LEVEL_WARNING` or lower.
  1155. *
  1156. * @memberof xkb_context
  1157. */
  1158. XKB_EXPORT void
  1159. xkb_context_set_log_verbosity(struct xkb_context *context, int verbosity);
  1160. /**
  1161. * Get the current logging verbosity of the context.
  1162. *
  1163. * @memberof xkb_context
  1164. */
  1165. XKB_EXPORT int
  1166. xkb_context_get_log_verbosity(struct xkb_context *context);
  1167. /**
  1168. * Set a custom function to handle logging messages.
  1169. *
  1170. * @param[in,out] context The context in which to use the set logging function.
  1171. * @param[in] log_fn The function that will be called for logging messages.
  1172. * Passing `NULL` restores the default function, which logs to stderr.
  1173. *
  1174. * By default, log messages from this library are printed to stderr. This
  1175. * function allows you to replace the default behavior with a custom
  1176. * handler. The handler is only called with messages which match the
  1177. * current logging level and verbosity settings for the context.
  1178. * level is the logging level of the message. @a format and @a args are
  1179. * the same as in the `vprintf(3)` function.
  1180. *
  1181. * You may use `xkb_context::xkb_context_set_user_data()` on the context, and
  1182. * then call `xkb_context::xkb_context_get_user_data()` from within the logging
  1183. * function to provide it with additional private context.
  1184. *
  1185. * @memberof xkb_context
  1186. */
  1187. XKB_EXPORT void
  1188. xkb_context_set_log_fn(struct xkb_context *context,
  1189. void (*log_fn)(struct xkb_context *context,
  1190. enum xkb_log_level level,
  1191. const char *format, va_list args));
  1192. /** @} */
  1193. /**
  1194. * @defgroup keymap Keymap Creation
  1195. * Creating and destroying keymaps.
  1196. *
  1197. * @{
  1198. */
  1199. /**
  1200. * @enum xkb_keymap_compile_flags
  1201. * Flags for keymap compilation.
  1202. */
  1203. enum xkb_keymap_compile_flags {
  1204. /** Do not apply any flags. */
  1205. XKB_KEYMAP_COMPILE_NO_FLAGS = 0,
  1206. /**
  1207. * Make the parser operate in *strict* mode.
  1208. *
  1209. * This is useful mainly for debugging.
  1210. *
  1211. * When this flag is set, the following will raise an error:
  1212. * - field type mismatch (e.g. a number instead of a string)
  1213. * - unknown global variable
  1214. * - unknown statement field
  1215. * - unknown declaration
  1216. * - unknown compound statement
  1217. * - unknown action/action parameter
  1218. * - invalid action parameter value
  1219. * - TODO
  1220. *
  1221. * @since 1.14.0
  1222. */
  1223. XKB_KEYMAP_COMPILE_STRICT_MODE = (1 << 0)
  1224. };
  1225. /** @} */
  1226. /**
  1227. * @defgroup xkb_keymap_format_enum Keymap formats
  1228. * @ingroup keymap keymap-serialization
  1229. * @brief Keymap formats for parsing and serializing keymaps
  1230. * <!-- this group enables displaying keymap formats in multiple groups -->
  1231. */
  1232. /**
  1233. * @enum xkb_keymap_format
  1234. * The possible keymap formats.
  1235. *
  1236. * See @ref keymap-text-format-v1-v2 "" for the complete description of the
  1237. * formats and @ref keymap-support "" for detailed differences between the
  1238. * formats.
  1239. *
  1240. * @remark A keymap can be parsed in one format and serialized in another,
  1241. * thanks to automatic fallback mechanisms.
  1242. *
  1243. * <table>
  1244. * <caption>
  1245. * Keymap format to use depending on the target protocol
  1246. * </caption>
  1247. * <thead>
  1248. * <tr>
  1249. * <th colspan="2">Protocol</th>
  1250. * <th colspan="2">libxkbcommon keymap format</th>
  1251. * </tr>
  1252. * <tr>
  1253. * <th>Name</th>
  1254. * <th>Keymap format</th>
  1255. * <th>Parsing</th>
  1256. * <th>Serialization</th>
  1257. * </tr>
  1258. * </thead>
  1259. * <tbody>
  1260. * <tr>
  1261. * <th>X11</th>
  1262. * <td>XKB</td>
  1263. * <td>
  1264. * `::XKB_KEYMAP_FORMAT_TEXT_V1`
  1265. * </td>
  1266. * <td>
  1267. * *Always* use `::XKB_KEYMAP_FORMAT_TEXT_V1`, since the other formats are
  1268. * incompatible.
  1269. * </td>
  1270. * </tr>
  1271. * <tr>
  1272. * <th>Wayland</th>
  1273. * <td><code>[xkb_v1]</code></td>
  1274. * <td>
  1275. * <dl>
  1276. * <dt>Wayland compositors<dt>
  1277. * <dd>
  1278. * The format depends on the keyboard layout database (usually [xkeyboard-config]).
  1279. * Note that since v2 is a superset of v1, compositors are encouraged to use
  1280. * `::XKB_KEYMAP_FORMAT_TEXT_V2` whenever possible.
  1281. * </dd>
  1282. * <dt>Client apps</dt>
  1283. * <dd>
  1284. * Clients should use `::XKB_KEYMAP_FORMAT_TEXT_V1` to parse the keymap sent
  1285. * by a Wayland compositor, at least until `::XKB_KEYMAP_FORMAT_TEXT_V2`
  1286. * stabilizes.
  1287. * </dd>
  1288. * </td>
  1289. * <td>
  1290. * At the time of writing (July 2025), the Wayland <code>[xkb_v1]</code> keymap
  1291. * format is only defined as “libxkbcommon compatible”. In theory it enables
  1292. * flexibility, but the set of supported features varies depending on the
  1293. * libxkbcommon version and libxkbcommon keymap format used. Unfortunately there
  1294. * is currently no Wayland API for keymap format *negotiation*.
  1295. *
  1296. * Therefore the **recommended** serialization format is
  1297. * `::XKB_KEYMAP_FORMAT_TEXT_V1`, in order to ensure maximum compatibility for
  1298. * interchange.
  1299. *
  1300. * Serializing using `::XKB_KEYMAP_FORMAT_TEXT_V2` should be considered
  1301. * **experimental**, as some clients may fail to parse the resulting string.
  1302. * </td>
  1303. * </tr>
  1304. * </tbody>
  1305. * </table>
  1306. *
  1307. * @ingroup xkb_keymap_format_enum
  1308. *
  1309. * [xkb_v1]: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_keyboard-enum-keymap_format
  1310. * [xkeyboard-config]: https://gitlab.freedesktop.org/xkeyboard-config/xkeyboard-config
  1311. */
  1312. enum xkb_keymap_format {
  1313. /**
  1314. * The classic XKB text format, as generated by `xkbcomp -xkb`.
  1315. *
  1316. * @important This format should *always* be used when *serializing* a
  1317. * keymap for **X11**.
  1318. *
  1319. * @important For the **Wayland** <code>[xkb_v1]</code> format, it is
  1320. * advised to use this format as well for serializing, in order to ensure
  1321. * maximum compatibility for interchange.
  1322. *
  1323. * @note In case serializing a keymap with *more than 4 layouts*, use
  1324. * `xkb_keymap::xkb_keymap_serialize()` and select the layouts to serialize
  1325. * using `xkb_keymap_serialize_config::layouts`.
  1326. *
  1327. * [xkb_v1]: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_keyboard-enum-keymap_format
  1328. */
  1329. XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
  1330. /**
  1331. * Xkbcommon extensions of the classic XKB text format, **incompatible with
  1332. * X11**.
  1333. *
  1334. * @important Do *not* use when *serializing* a keymap for **X11**
  1335. * (incompatible).
  1336. *
  1337. * @important Considered *experimental* when *serializing* for **Wayland**:
  1338. * at the time of writing (July 2025), there is only one XKB keymap format
  1339. * <code>[xkb_v1]</code> in Wayland and no Wayland API for keymap format
  1340. * *negotiation*, so the clients may not be able to parse the keymap if it
  1341. * uses v2-specific features. Therefore a compositor may *parse* keymaps
  1342. * using `::XKB_KEYMAP_FORMAT_TEXT_V2` but it should serialize them using
  1343. * `::XKB_KEYMAP_FORMAT_TEXT_V1` and rely on the automatic *fallback*
  1344. * mechanisms.
  1345. *
  1346. * @since 1.11.0
  1347. *
  1348. * [xkb_v1]: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_keyboard-enum-keymap_format
  1349. */
  1350. XKB_KEYMAP_FORMAT_TEXT_V2 = 2
  1351. };
  1352. /**
  1353. * @addtogroup keymap
  1354. * @{
  1355. */
  1356. /**
  1357. * Create a keymap from a [RMLVO] builder.
  1358. *
  1359. * The primary keymap entry point: creates a new XKB keymap from a set of
  1360. * [RMLVO] \(Rules + Model + Layouts + Variants + Options) names.
  1361. *
  1362. * @param[in] rmlvo The [RMLVO] builder to use. See `xkb_rmlvo_builder`.
  1363. * @param[in] format The text format of the keymap file to compile.
  1364. * @param[in] flags Optional flags for the keymap, or 0.
  1365. *
  1366. * @returns A keymap compiled according to the [RMLVO] names, or `NULL` if
  1367. * the compilation failed.
  1368. *
  1369. * @since 1.11.0
  1370. * @since 1.14.0 Parser is lenient by default.
  1371. * @sa `xkb_keymap_new_from_names2()`
  1372. * @sa `xkb_rmlvo_builder`
  1373. * @memberof xkb_keymap
  1374. *
  1375. * [RMLVO]: @ref RMLVO-intro
  1376. */
  1377. XKB_EXPORT struct xkb_keymap *
  1378. xkb_keymap_new_from_rmlvo(const struct xkb_rmlvo_builder *rmlvo,
  1379. enum xkb_keymap_format format,
  1380. enum xkb_keymap_compile_flags flags);
  1381. /**
  1382. * Create a keymap from [RMLVO] names.
  1383. *
  1384. * Same as `xkb_keymap_new_from_names2()`, but with the keymap format fixed to:
  1385. * `::XKB_KEYMAP_FORMAT_TEXT_V2`.
  1386. *
  1387. * @deprecated Use `xkb_keymap_new_from_names2()` instead.
  1388. * @since 1.11.0: Deprecated
  1389. * @since 1.11.0: Use internally `::XKB_KEYMAP_FORMAT_TEXT_V2` instead of
  1390. * `::XKB_KEYMAP_FORMAT_TEXT_V1`
  1391. * @since 1.14.0 Parser is lenient by default.
  1392. * @sa `xkb_keymap_new_from_names2()`
  1393. * @sa `xkb_rule_names`
  1394. * @sa `xkb_keymap_new_from_rmlvo()`
  1395. * @memberof xkb_keymap
  1396. *
  1397. * [RMLVO]: @ref RMLVO-intro
  1398. */
  1399. XKB_EXPORT struct xkb_keymap *
  1400. xkb_keymap_new_from_names(struct xkb_context *context,
  1401. const struct xkb_rule_names *names,
  1402. enum xkb_keymap_compile_flags flags);
  1403. /**
  1404. * Create a keymap from [RMLVO] names.
  1405. *
  1406. * The primary keymap entry point: creates a new XKB keymap from a set of
  1407. * [RMLVO] \(Rules + Model + Layouts + Variants + Options) names.
  1408. *
  1409. * @param[in] context The context in which to create the keymap.
  1410. * @param[in] names The [RMLVO] names to use. See `xkb_rule_names`.
  1411. * @param[in] format The text format of the keymap file to compile.
  1412. * @param[in] flags Optional flags for the keymap, or 0.
  1413. *
  1414. * @returns A keymap compiled according to the [RMLVO] names, or `NULL` if
  1415. * the compilation failed.
  1416. *
  1417. * @since 1.11.0
  1418. * @since 1.14.0 Parser is lenient by default.
  1419. * @sa `xkb_rule_names`
  1420. * @sa `xkb_keymap_new_from_rmlvo()`
  1421. * @memberof xkb_keymap
  1422. *
  1423. * [RMLVO]: @ref RMLVO-intro
  1424. */
  1425. XKB_EXPORT struct xkb_keymap *
  1426. xkb_keymap_new_from_names2(struct xkb_context *context,
  1427. const struct xkb_rule_names *names,
  1428. enum xkb_keymap_format format,
  1429. enum xkb_keymap_compile_flags flags);
  1430. /**
  1431. * Create a keymap from a keymap file.
  1432. *
  1433. * @param[in] context The context in which to create the keymap.
  1434. * @param[in] file The keymap file to compile.
  1435. * @param[in] format The text format of the keymap file to compile.
  1436. * @param[in] flags Optional flags for the keymap, or 0.
  1437. *
  1438. * @returns A keymap compiled from the given XKB keymap file, or `NULL` if
  1439. * the compilation failed.
  1440. *
  1441. * The file must contain a complete keymap. For example, in the
  1442. * `::XKB_KEYMAP_FORMAT_TEXT_V1` format, this means the file must contain one
  1443. * top level `%xkb_keymap` section, which in turn contains other required
  1444. * sections.
  1445. *
  1446. * @since 1.14.0 Parser is lenient by default.
  1447. *
  1448. * @memberof xkb_keymap
  1449. */
  1450. XKB_EXPORT struct xkb_keymap *
  1451. xkb_keymap_new_from_file(struct xkb_context *context, FILE *file,
  1452. enum xkb_keymap_format format,
  1453. enum xkb_keymap_compile_flags flags);
  1454. /**
  1455. * Create a keymap from a keymap string.
  1456. *
  1457. * This is just like `xkb_keymap_new_from_file()`, but instead of a file, gets
  1458. * the keymap as one enormous string.
  1459. *
  1460. * @returns A keymap compiled from the given string, or `NULL` if
  1461. * the compilation failed.
  1462. *
  1463. * @since 1.14.0 Parser is lenient by default.
  1464. * @see `xkb_keymap_new_from_file()`
  1465. * @memberof xkb_keymap
  1466. */
  1467. XKB_EXPORT struct xkb_keymap *
  1468. xkb_keymap_new_from_string(struct xkb_context *context, const char *string,
  1469. enum xkb_keymap_format format,
  1470. enum xkb_keymap_compile_flags flags);
  1471. /**
  1472. * Create a keymap from a memory buffer.
  1473. *
  1474. * This is just like `xkb_keymap_new_from_string()`, but takes a @p length
  1475. * argument so the input string does not have to be zero-terminated.
  1476. *
  1477. * @returns A keymap compiled from the given buffer, or `NULL` if
  1478. * the compilation failed.
  1479. *
  1480. * @since 0.3.0
  1481. * @since 1.14.0 Parser is lenient by default.
  1482. * @see `xkb_keymap_new_from_string()`
  1483. * @memberof xkb_keymap
  1484. */
  1485. XKB_EXPORT struct xkb_keymap *
  1486. xkb_keymap_new_from_buffer(struct xkb_context *context, const char *buffer,
  1487. size_t length, enum xkb_keymap_format format,
  1488. enum xkb_keymap_compile_flags flags);
  1489. /**
  1490. * Take a new reference on a keymap.
  1491. *
  1492. * @returns The passed in keymap.
  1493. *
  1494. * @memberof xkb_keymap
  1495. */
  1496. XKB_EXPORT struct xkb_keymap *
  1497. xkb_keymap_ref(struct xkb_keymap *keymap);
  1498. /**
  1499. * Release a reference on a keymap, and possibly free it.
  1500. *
  1501. * @param[in] keymap The keymap. If it is `NULL`, this function does nothing.
  1502. *
  1503. * @memberof xkb_keymap
  1504. */
  1505. XKB_EXPORT void
  1506. xkb_keymap_unref(struct xkb_keymap *keymap);
  1507. /** @} */
  1508. /**
  1509. * @defgroup keymap-serialization Keymap Serialization
  1510. * Serializing keymaps.
  1511. *
  1512. * @{
  1513. */
  1514. /**
  1515. * Get the keymap as a string in the format from which it was created.
  1516. * @sa `xkb_keymap::xkb_keymap_get_as_string()`
  1517. **/
  1518. #define XKB_KEYMAP_USE_ORIGINAL_FORMAT ((enum xkb_keymap_format) -1)
  1519. /**
  1520. * @enum xkb_keymap_serialize_flags
  1521. * Flags to control keymap serialization.
  1522. *
  1523. * @since 1.12.0
  1524. */
  1525. enum xkb_keymap_serialize_flags {
  1526. /**
  1527. * Do not apply any flags
  1528. *
  1529. * @since 1.12.0
  1530. */
  1531. XKB_KEYMAP_SERIALIZE_NO_FLAGS = 0,
  1532. /**
  1533. * Enable pretty-printing
  1534. *
  1535. * @since 1.12.0
  1536. */
  1537. XKB_KEYMAP_SERIALIZE_PRETTY = (1 << 0),
  1538. /**
  1539. * Do not drop unused bits (key types, compatibility entries)
  1540. *
  1541. * @since 1.12.0
  1542. */
  1543. XKB_KEYMAP_SERIALIZE_KEEP_UNUSED = (1 << 1),
  1544. /**
  1545. * Make the serializer operate in *strict* mode.
  1546. *
  1547. * This is useful mainly for debugging.
  1548. *
  1549. * When this flag is set, the following will raise an error:
  1550. * - Exceeded layout count for the corresponding format
  1551. * (see `::XKB_ERROR_LAYOUT_COUNT_LIMIT_EXCEEDED`)
  1552. *
  1553. * @since 1.14.0
  1554. */
  1555. XKB_KEYMAP_SERIALIZE_STRICT_MODE = (1 << 2),
  1556. /**
  1557. * Force default values to be explicit.
  1558. *
  1559. * This is useful mainly for debugging.
  1560. *
  1561. * @since 1.14.0
  1562. */
  1563. XKB_KEYMAP_SERIALIZE_EXPLICIT_DEFAULT_VALUES = (1 << 3),
  1564. /**
  1565. * Force [virtual modifier] encoding to be explicit.
  1566. *
  1567. * This is useful mainly for debugging.
  1568. *
  1569. * @since 1.14.0
  1570. *
  1571. * [virtual modifiers]: @ref virtual-modifier-def
  1572. */
  1573. XKB_KEYMAP_SERIALIZE_EXPLICIT_VMODS = (1 << 4),
  1574. /**
  1575. * Force key values to be explicit.
  1576. *
  1577. * This is useful mainly for debugging, as it may increase considerably
  1578. * the size of the serialization.
  1579. *
  1580. * This is useful mainly for debugging.
  1581. *
  1582. * @since 1.14.0
  1583. */
  1584. XKB_KEYMAP_SERIALIZE_EXPLICIT_KEY_VALUES = (1 << 5),
  1585. };
  1586. /**
  1587. * @struct xkb_keymap_serialize_config
  1588. *
  1589. * Serialization configuration for `xkb_keymap::xkb_keymap_serialize()`.
  1590. *
  1591. * @sa `::xkb_keymap_serialize_result`
  1592. * @since 1.14.0
  1593. */
  1594. struct xkb_keymap_serialize_config {
  1595. /**
  1596. * Size of this structure, for forward-compatibility.
  1597. *
  1598. * @sa `::XKB_ERROR_ABI_INVALID_STRUCT_SIZE`
  1599. * @sa `::XKB_ERROR_ABI_BACKWARD_COMPAT`
  1600. * @sa `::XKB_ERROR_ABI_FORWARD_COMPAT`
  1601. *
  1602. * @since 1.14.0
  1603. */
  1604. size_t size;
  1605. /**
  1606. * Mask of [serialization flags].
  1607. *
  1608. * @sa `xkb_keymap_serialize_flags`
  1609. *
  1610. * @since 1.14.0
  1611. *
  1612. * [serialization flags]: @ref xkb_keymap_serialize_flags
  1613. */
  1614. uint32_t flags;
  1615. /**
  1616. * Target [keymap format].
  1617. *
  1618. * @sa `xkb_keymap_format`
  1619. *
  1620. * @since 1.14.0
  1621. *
  1622. * [keymap format]: @ref xkb_keymap_format
  1623. */
  1624. uint32_t format;
  1625. /**
  1626. * Mask of layouts to serialize.
  1627. *
  1628. * If `0`, then all the keymap layouts are serialized.
  1629. *
  1630. * @since 1.14.0
  1631. */
  1632. xkb_layout_mask_t layouts;
  1633. /**
  1634. * @private
  1635. *
  1636. * Reserved for future extensions.
  1637. *
  1638. * @pre Must be set to `0` by the caller.
  1639. */
  1640. uint32_t reserved;
  1641. };
  1642. /**
  1643. * @struct xkb_keymap_serialize_result
  1644. *
  1645. * Result of `xkb_keymap::xkb_keymap_serialize()`
  1646. *
  1647. * @sa `::xkb_keymap_serialize_config`
  1648. * @since 1.14.0
  1649. */
  1650. struct xkb_keymap_serialize_result {
  1651. /**
  1652. * Size of this structure, for forward-compatibility.
  1653. *
  1654. * @sa `::XKB_ERROR_ABI_INVALID_STRUCT_SIZE`
  1655. * @sa `::XKB_ERROR_ABI_BACKWARD_COMPAT`
  1656. * @sa `::XKB_ERROR_ABI_FORWARD_COMPAT`
  1657. *
  1658. * @since 1.14.0
  1659. */
  1660. size_t size;
  1661. /**
  1662. * A newly *allocated* keymap serialization, or `NULL` on failure.
  1663. *
  1664. * The caller of `xkb_keymap::xkb_keymap_serialize()` must free it.
  1665. *
  1666. * @since 1.14.0
  1667. */
  1668. char *serialized;
  1669. /**
  1670. * Length of #serialized, in bytes, including any terminating `NUL` byte.
  1671. *
  1672. * Valid only if the function returns `::XKB_SUCCESS`; otherwise unspecified.
  1673. *
  1674. * @since 1.14.0
  1675. */
  1676. size_t length;
  1677. /**
  1678. * Mask of the original layouts actually included in #serialized.
  1679. *
  1680. * Valid only if the function returns `::XKB_SUCCESS`; otherwise unspecified.
  1681. *
  1682. * @sa `xkb_keymap_serialize_config::layouts`
  1683. *
  1684. * @since 1.14.0
  1685. */
  1686. xkb_layout_mask_t layouts;
  1687. /**
  1688. * @private
  1689. *
  1690. * Reserved for future extensions.
  1691. *
  1692. * @pre Must be set to `0` by the caller.
  1693. */
  1694. uint32_t reserved;
  1695. };
  1696. /**
  1697. * Serialize a compiled keymap to a string.
  1698. *
  1699. * On success, returns a newly *allocated* serialized keymap in
  1700. * [`result->serialized`][serialized], together with additional metadata.
  1701. * It is suitable to use with `xkb_keymap_new_from_string2()`.
  1702. *
  1703. * Use this function instead of `xkb_keymap_get_as_string()` or
  1704. * `xkb_keymap_get_as_string2()` when more control on serializing
  1705. * or its result is required.
  1706. *
  1707. * @note This function enables to serialize an X11-<em>incompatible</em> keymap
  1708. * with more than 4 layouts to an X11-<em>compatible</em> keymap with up to 4
  1709. * layouts:
  1710. * - set [`config->format`][format] to `::XKB_KEYMAP_FORMAT_TEXT_V1`,
  1711. * - set up to 4 bits in [`config->layouts`][layouts] to select a subset of
  1712. * layouts to serialized.
  1713. *
  1714. * @param[in] keymap The keymap to serialize.
  1715. * @param[in] config Configuration guiding the serialization.
  1716. * @param[in,out] result Result of the serialization.
  1717. *
  1718. * @pre @p config must point to a zero-initialized struct with
  1719. * [`size`](@ref xkb_keymap_serialize_config::size) set to `sizeof(*config)`.
  1720. *
  1721. * @pre @p result must point to a zero-initialized struct with
  1722. * [`size`](@ref xkb_keymap_serialize_result::size) set to `sizeof(*result)`.
  1723. *
  1724. * @invariant The library writes only to fields of @p result that fall
  1725. * within `result->size`.
  1726. *
  1727. * @post If the return value is `::XKB_SUCCESS`, the caller is responsible
  1728. * for freeing [`result->serialized`][serialized].
  1729. *
  1730. * @post Otherwise, [`result->serialized`][serialized] is set to `NULL` and
  1731. * all fields of @p result beyond it are left unspecified.
  1732. *
  1733. * @returns `::XKB_SUCCESS` on success; otherwise an
  1734. * [error code](@ref xkb_error_code).
  1735. *
  1736. * @since 1.14.0
  1737. * @memberof xkb_keymap
  1738. *
  1739. * [format]: @ref xkb_keymap_serialize_config::format
  1740. * [layouts]: @ref xkb_keymap_serialize_config::layouts
  1741. * [serialized]: @ref xkb_keymap_serialize_result::serialized
  1742. */
  1743. XKB_EXPORT enum xkb_error_code
  1744. xkb_keymap_serialize(const struct xkb_keymap *keymap,
  1745. const struct xkb_keymap_serialize_config *config,
  1746. struct xkb_keymap_serialize_result *result);
  1747. /**
  1748. * Get the compiled keymap as a string.
  1749. *
  1750. * Same as `xkb_keymap::xkb_keymap_get_as_string2()` using
  1751. * `::XKB_KEYMAP_SERIALIZE_NO_FLAGS`.
  1752. *
  1753. * @since 1.12.0: Drop unused types and compatibility entries and do not
  1754. * pretty-print.
  1755. *
  1756. * @sa `xkb_keymap::xkb_keymap_serialize()`
  1757. * @sa `xkb_keymap::xkb_keymap_get_as_string2()`
  1758. * @memberof xkb_keymap
  1759. */
  1760. XKB_EXPORT char *
  1761. xkb_keymap_get_as_string(struct xkb_keymap *keymap,
  1762. enum xkb_keymap_format format);
  1763. /**
  1764. * Get the compiled keymap as a string.
  1765. *
  1766. * @param[in] keymap The keymap to get as a string.
  1767. * @param[in] format The keymap format to use for the string. You can pass
  1768. * in the special value `::XKB_KEYMAP_USE_ORIGINAL_FORMAT` to use the format
  1769. * from which the keymap was originally created. When used as an *interchange*
  1770. * format such as Wayland <code>[xkb_v1]</code>, the format should be explicit.
  1771. * @param[in] flags Optional flags to control the serialization, or 0.
  1772. *
  1773. * @returns The keymap as a `NULL`-terminated string, or `NULL` if unsuccessful.
  1774. *
  1775. * The returned string may be fed back into `xkb_keymap_new_from_string()`
  1776. * to get the exact same keymap (possibly in another process, etc.).
  1777. *
  1778. * The returned string is *dynamically allocated* and should be freed by the
  1779. * caller.
  1780. *
  1781. * @since 1.12.0
  1782. *
  1783. * @sa `xkb_keymap_serialize()`
  1784. * @sa `xkb_keymap_get_as_string()`
  1785. * @sa `xkb_keymap_new_from_string()`
  1786. * @memberof xkb_keymap
  1787. *
  1788. * [xkb_v1]: https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_keyboard-enum-keymap_format
  1789. */
  1790. XKB_EXPORT char *
  1791. xkb_keymap_get_as_string2(struct xkb_keymap *keymap,
  1792. enum xkb_keymap_format format,
  1793. enum xkb_keymap_serialize_flags flags);
  1794. /** @} */
  1795. /**
  1796. * @defgroup components Keymap Components
  1797. * Enumeration of state components in a keymap.
  1798. *
  1799. * @{
  1800. */
  1801. /**
  1802. * Get the minimum keycode in the keymap.
  1803. *
  1804. * @sa xkb_keycode_t
  1805. * @memberof xkb_keymap
  1806. * @since 0.3.1
  1807. */
  1808. XKB_EXPORT xkb_keycode_t
  1809. xkb_keymap_min_keycode(struct xkb_keymap *keymap);
  1810. /**
  1811. * Get the maximum keycode in the keymap.
  1812. *
  1813. * @sa xkb_keycode_t
  1814. * @memberof xkb_keymap
  1815. * @since 0.3.1
  1816. */
  1817. XKB_EXPORT xkb_keycode_t
  1818. xkb_keymap_max_keycode(struct xkb_keymap *keymap);
  1819. /**
  1820. * @struct xkb_keymap_key_iterator
  1821. * Iterator over a keymap’s keys.
  1822. *
  1823. * @sa `xkb_keycode_t`
  1824. * @sa `xkb_keymap_key_iterator_new()`
  1825. * @sa `xkb_keymap_key_iterator_destroy()`
  1826. * @since 1.14.0
  1827. */
  1828. struct xkb_keymap_key_iterator;
  1829. /**
  1830. * @enum xkb_keymap_key_iterator_flags
  1831. * Flags for `xkb_keymap_key_iterator_new()`.
  1832. *
  1833. * @since 1.14.0
  1834. */
  1835. enum xkb_keymap_key_iterator_flags {
  1836. /**
  1837. * Do not apply any flags.
  1838. *
  1839. * @since 1.14.0
  1840. */
  1841. XKB_KEYMAP_KEY_ITERATOR_NO_FLAGS = 0,
  1842. /**
  1843. * Iterate keys in *descending* order.
  1844. *
  1845. * @since 1.14.0
  1846. */
  1847. XKB_KEYMAP_KEY_ITERATOR_DESCENDING_ORDER = (1 << 0),
  1848. /**
  1849. * @parblock
  1850. * Skip *unbound* keys, i.e. keys with no groups.
  1851. * @endparblock
  1852. *
  1853. * @since 1.14.0
  1854. */
  1855. XKB_KEYMAP_KEY_ITERATOR_SKIP_UNBOUND = (1 << 1),
  1856. };
  1857. /**
  1858. * Create a new iterator over a keymap’s keys.
  1859. *
  1860. * Intended use:
  1861. *
  1862. * ```c
  1863. * struct xkb_keymap_key_iterator *iter = xkb_keymap_key_iterator_new(keymap, 0);
  1864. * xkb_keycode_t kc;
  1865. * while ((kc = xkb_keymap_key_iterator_next(iter)) != XKB_KEYCODE_INVALID) {
  1866. * // ...
  1867. * }
  1868. * xkb_keymap_key_iterator_destroy(iter);
  1869. * ```
  1870. *
  1871. * @param[in] keymap The keymap to iterate over.
  1872. * @param[in] flags Flags to control the iterator behavior, or 0.
  1873. *
  1874. * @returns A new keys iterator, or `NULL` on failure.
  1875. *
  1876. * @sa `xkb_keymap_key_iterator`
  1877. * @sa `xkb_keymap_key_iterator_flags`
  1878. * @sa `xkb_keymap_key_iterator_next()`
  1879. * @sa `xkb_keymap_key_iterator_destroy()`
  1880. * @since 1.14.0
  1881. * @memberof xkb_keymap_key_iterator
  1882. */
  1883. XKB_EXPORT struct xkb_keymap_key_iterator *
  1884. xkb_keymap_key_iterator_new(struct xkb_keymap *keymap,
  1885. enum xkb_keymap_key_iterator_flags flags);
  1886. /**
  1887. * Free a keymap’s keys iterator.
  1888. *
  1889. * @param[in] iter The iterator to free. If it is `NULL`, do nothing.
  1890. *
  1891. * @sa `xkb_keymap_key_iterator_new()`
  1892. * @since 1.14.0
  1893. * @memberof xkb_keymap_key_iterator
  1894. */
  1895. XKB_EXPORT void
  1896. xkb_keymap_key_iterator_destroy(struct xkb_keymap_key_iterator *iter);
  1897. /**
  1898. * Get the next [keycode] from a keymap’s keys iterator.
  1899. *
  1900. * The keycodes are returned in *ascending* order unless
  1901. * `::XKB_KEYMAP_KEY_ITERATOR_DESCENDING_ORDER` was used to create the iterator.
  1902. *
  1903. * If a keymap is sparse, this function may be called fewer than
  1904. * `max_keycode - min_keycode + 1` times.
  1905. *
  1906. * @param[in,out] iter The iterator to use.
  1907. *
  1908. * @returns A valid [keycode], otherwise `::XKB_KEYCODE_INVALID` in case there
  1909. * are no more entries.
  1910. *
  1911. * @sa `xkb_keycode_t`
  1912. * @since 1.14.0
  1913. * @memberof xkb_keymap_key_iterator
  1914. *
  1915. * [keycode]: @ref xkb_keycode_t
  1916. */
  1917. XKB_EXPORT xkb_keycode_t
  1918. xkb_keymap_key_iterator_next(struct xkb_keymap_key_iterator *iter);
  1919. /**
  1920. * The iterator used by `xkb_keymap_key_for_each()`.
  1921. *
  1922. * @sa `xkb_keymap_key_for_each()`
  1923. * @memberof xkb_keymap
  1924. * @since 0.3.1
  1925. */
  1926. typedef void
  1927. (*xkb_keymap_key_iter_t)(struct xkb_keymap *keymap, xkb_keycode_t key,
  1928. void *data);
  1929. /**
  1930. * Run a specified function for every valid keycode in the keymap. If a
  1931. * keymap is sparse, this function may be called fewer than
  1932. * (max_keycode - min_keycode + 1) times with success.
  1933. *
  1934. * @sa `xkb_keymap_key_iterator`, which offers more control on the iteration.
  1935. * @sa `xkb_keymap_min_keycode()`
  1936. * @sa `xkb_keymap_max_keycode()`
  1937. * @sa `xkb_keycode_t`
  1938. * @memberof xkb_keymap
  1939. * @since 0.3.1
  1940. */
  1941. XKB_EXPORT void
  1942. xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
  1943. void *data);
  1944. /**
  1945. * Find the name of the key with the given keycode.
  1946. *
  1947. * This function always returns the canonical name of the key (see
  1948. * description in `xkb_keycode_t`).
  1949. *
  1950. * @param[in] keymap The keymap to query.
  1951. * @param[in] key The key to query.
  1952. *
  1953. * @returns The key name. If no key with this keycode exists,
  1954. * returns `NULL`.
  1955. *
  1956. * @sa xkb_keycode_t
  1957. * @memberof xkb_keymap
  1958. * @since 0.6.0
  1959. */
  1960. XKB_EXPORT const char *
  1961. xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t key);
  1962. /**
  1963. * Find the keycode of the key with the given name.
  1964. *
  1965. * The name can be either a canonical name or an alias.
  1966. *
  1967. * @returns The keycode. If no key with this name exists,
  1968. * returns `::XKB_KEYCODE_INVALID`.
  1969. *
  1970. * @sa xkb_keycode_t
  1971. * @memberof xkb_keymap
  1972. * @since 0.6.0
  1973. */
  1974. XKB_EXPORT xkb_keycode_t
  1975. xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name);
  1976. /**
  1977. * Get the number of modifiers in the keymap.
  1978. *
  1979. * @sa xkb_mod_index_t
  1980. * @memberof xkb_keymap
  1981. */
  1982. XKB_EXPORT xkb_mod_index_t
  1983. xkb_keymap_num_mods(struct xkb_keymap *keymap);
  1984. /**
  1985. * Get the name of a modifier by index.
  1986. *
  1987. * @returns The name. If the index is invalid, returns `NULL`.
  1988. *
  1989. * @sa xkb_mod_index_t
  1990. * @memberof xkb_keymap
  1991. */
  1992. XKB_EXPORT const char *
  1993. xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx);
  1994. /**
  1995. * Get the index of a modifier by name.
  1996. *
  1997. * @returns The index. If no modifier with this name exists, returns
  1998. * `::XKB_MOD_INVALID`.
  1999. *
  2000. * @sa xkb_mod_index_t
  2001. * @memberof xkb_keymap
  2002. */
  2003. XKB_EXPORT xkb_mod_index_t
  2004. xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name);
  2005. /**
  2006. * Get the encoding of a modifier by name.
  2007. *
  2008. * In X11 terminology it corresponds to the mapping to the <em>[real modifiers]</em>.
  2009. *
  2010. * @returns The encoding of a modifier. Note that it may be 0 if the name does
  2011. * not exist or if the modifier is not mapped.
  2012. *
  2013. * @since 1.10.0
  2014. * @sa `xkb_keymap_mod_get_mask2()`
  2015. * @memberof xkb_keymap
  2016. *
  2017. * [real modifiers]: @ref real-modifier-def
  2018. */
  2019. XKB_EXPORT xkb_mod_mask_t
  2020. xkb_keymap_mod_get_mask(struct xkb_keymap *keymap, const char *name);
  2021. /**
  2022. * Get the encoding of a modifier by index.
  2023. *
  2024. * In X11 terminology it corresponds to the mapping to the <em>[real modifiers]</em>.
  2025. *
  2026. * @returns The encoding of a modifier. Note that it may be 0 if the modifier is
  2027. * not mapped.
  2028. *
  2029. * @since 1.11.0
  2030. * @sa `xkb_keymap_mod_get_mask()`
  2031. * @memberof xkb_keymap
  2032. *
  2033. * [real modifiers]: @ref real-modifier-def
  2034. */
  2035. XKB_EXPORT xkb_mod_mask_t
  2036. xkb_keymap_mod_get_mask2(struct xkb_keymap *keymap, xkb_mod_index_t idx);
  2037. /**
  2038. * Get the number of layouts in the keymap.
  2039. *
  2040. * @sa `xkb_layout_index_t`
  2041. * @sa `xkb_rule_names`
  2042. * @sa `xkb_keymap_num_layouts_for_key()`
  2043. * @memberof xkb_keymap
  2044. */
  2045. XKB_EXPORT xkb_layout_index_t
  2046. xkb_keymap_num_layouts(struct xkb_keymap *keymap);
  2047. /**
  2048. * Get the name of a layout by index.
  2049. *
  2050. * @returns The name. If the index is invalid, or the layout does not have
  2051. * a name, returns `NULL`.
  2052. *
  2053. * @sa xkb_layout_index_t
  2054. * For notes on layout names.
  2055. * @memberof xkb_keymap
  2056. */
  2057. XKB_EXPORT const char *
  2058. xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx);
  2059. /**
  2060. * Get the index of a layout by name.
  2061. *
  2062. * @returns The index. If no layout exists with this name, returns
  2063. * `::XKB_LAYOUT_INVALID`. If more than one layout in the keymap has this name,
  2064. * returns the lowest index among them.
  2065. *
  2066. * @sa `xkb_layout_index_t` for notes on layout names.
  2067. * @memberof xkb_keymap
  2068. */
  2069. XKB_EXPORT xkb_layout_index_t
  2070. xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name);
  2071. /**
  2072. * Get the number of LEDs in the keymap.
  2073. *
  2074. * @warning The range [ 0...`xkb_keymap_num_leds()` ) includes all of the LEDs
  2075. * in the keymap, but may also contain inactive LEDs. When iterating over
  2076. * this range, you need the handle this case when calling functions such as
  2077. * `xkb_keymap_led_get_name()` or `xkb_state::xkb_state_led_index_is_active()`.
  2078. *
  2079. * @sa xkb_led_index_t
  2080. * @memberof xkb_keymap
  2081. */
  2082. XKB_EXPORT xkb_led_index_t
  2083. xkb_keymap_num_leds(struct xkb_keymap *keymap);
  2084. /**
  2085. * Get the name of a LED by index.
  2086. *
  2087. * @returns The name. If the index is invalid, returns `NULL`.
  2088. *
  2089. * @memberof xkb_keymap
  2090. */
  2091. XKB_EXPORT const char *
  2092. xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx);
  2093. /**
  2094. * Get the index of a LED by name.
  2095. *
  2096. * @returns The index. If no LED with this name exists, returns
  2097. * `::XKB_LED_INVALID`.
  2098. *
  2099. * @memberof xkb_keymap
  2100. */
  2101. XKB_EXPORT xkb_led_index_t
  2102. xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name);
  2103. /**
  2104. * Get the number of layouts for a specific key.
  2105. *
  2106. * This number can be different from `xkb_keymap_num_layouts()`, but is always
  2107. * smaller. It is the appropriate value to use when iterating over the
  2108. * layouts of a key.
  2109. *
  2110. * @param[in] keymap The keymap to query.
  2111. * @param[in] key The key to query.
  2112. *
  2113. * @returns The number of layouts corresponding to the given key if it is valid
  2114. * in the given keymap, otherwise 0 if the key is undefined or unbound.
  2115. *
  2116. * @sa xkb_layout_index_t
  2117. * @memberof xkb_keymap
  2118. */
  2119. XKB_EXPORT xkb_layout_index_t
  2120. xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t key);
  2121. /**
  2122. * Get the number of shift levels for a specific key and layout.
  2123. *
  2124. * If @c layout is out of range for this key (that is, larger or equal to
  2125. * the value returned by `xkb_keymap_num_layouts_for_key()`), it is brought
  2126. * back into range in a manner consistent with
  2127. * `xkb_state::xkb_state_key_get_layout()`.
  2128. *
  2129. * @sa xkb_level_index_t
  2130. * @memberof xkb_keymap
  2131. */
  2132. XKB_EXPORT xkb_level_index_t
  2133. xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
  2134. xkb_layout_index_t layout);
  2135. /**
  2136. * Retrieves every possible modifier mask that produces the specified
  2137. * shift level for a specific key and layout.
  2138. *
  2139. * This API is useful for inverse key transformation; i.e. finding out
  2140. * which modifiers need to be active in order to be able to type the
  2141. * keysym(s) corresponding to the specific key code, layout and level.
  2142. *
  2143. * @warning It returns only up to masks_size modifier masks. If the
  2144. * buffer passed is too small, some of the possible modifier combinations
  2145. * will not be returned.
  2146. *
  2147. * @param[in] keymap The keymap.
  2148. * @param[in] key The keycode of the key.
  2149. * @param[in] layout The layout for which to get modifiers.
  2150. * @param[in] level The shift level in the layout for which to get the
  2151. * modifiers. This should be smaller than:
  2152. * @code xkb_keymap_num_levels_for_key(keymap, key) @endcode
  2153. * @param[out] masks_out A buffer in which the requested masks should be
  2154. * stored.
  2155. * @param[in] masks_size The capacity of the buffer pointed to by
  2156. * @p masks_out.
  2157. *
  2158. * If @c layout is out of range for this key (that is, larger or equal to
  2159. * the value returned by `xkb_keymap_num_layouts_for_key()`), it is brought
  2160. * back into range in a manner consistent with
  2161. * `xkb_state::xkb_state_key_get_layout()`.
  2162. *
  2163. * @returns The number of modifier masks stored in the masks_out array.
  2164. * If the key is not in the keymap or if the specified shift level cannot
  2165. * be reached it returns 0 and does not modify the @p masks_out buffer.
  2166. *
  2167. * @sa xkb_level_index_t
  2168. * @sa xkb_mod_mask_t
  2169. * @memberof xkb_keymap
  2170. * @since 1.0.0
  2171. */
  2172. XKB_EXPORT size_t
  2173. xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
  2174. xkb_keycode_t key,
  2175. xkb_layout_index_t layout,
  2176. xkb_level_index_t level,
  2177. xkb_mod_mask_t *masks_out,
  2178. size_t masks_size);
  2179. /**
  2180. * Get the keysyms obtained from pressing a key in a given layout and
  2181. * shift level.
  2182. *
  2183. * This function is like `xkb_state::xkb_state_key_get_syms()`, only the layout
  2184. * and shift level are not derived from the keyboard state but are instead
  2185. * specified explicitly.
  2186. *
  2187. * @param[in] keymap The keymap.
  2188. * @param[in] key The keycode of the key.
  2189. * @param[in] layout The layout for which to get the keysyms.
  2190. * @param[in] level The shift level in the layout for which to get the
  2191. * keysyms. This should be smaller than:
  2192. * @code xkb_keymap_num_levels_for_key(keymap, key) @endcode
  2193. * @param[out] syms_out An immutable array of keysyms corresponding to the
  2194. * key in the given layout and shift level.
  2195. *
  2196. * If @c layout is out of range for this key (that is, larger or equal to
  2197. * the value returned by `xkb_keymap_num_layouts_for_key()`), it is brought
  2198. * back into range in a manner consistent with
  2199. * `xkb_state::xkb_state_key_get_layout()`.
  2200. *
  2201. * @returns The number of keysyms in the syms_out array. If no keysyms
  2202. * are produced by the key in the given layout and shift level, returns 0
  2203. * and sets @p syms_out to `NULL`.
  2204. *
  2205. * @sa `xkb_state::xkb_state_key_get_syms()`
  2206. * @memberof xkb_keymap
  2207. */
  2208. XKB_EXPORT int
  2209. xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
  2210. xkb_keycode_t key,
  2211. xkb_layout_index_t layout,
  2212. xkb_level_index_t level,
  2213. const xkb_keysym_t **syms_out);
  2214. /**
  2215. * Determine whether a key should repeat or not.
  2216. *
  2217. * A keymap may specify different repeat behaviors for different keys.
  2218. * Most keys should generally exhibit repeat behavior; for example, holding
  2219. * the `a` key down in a text editor should normally insert a single ‘a’
  2220. * character every few milliseconds, until the key is released. However,
  2221. * there are keys which should not or do not need to be repeated. For
  2222. * example, repeating modifier keys such as Left/Right Shift or Caps Lock
  2223. * is not generally useful or desired.
  2224. *
  2225. * @returns 1 if the key should repeat, 0 otherwise.
  2226. *
  2227. * @memberof xkb_keymap
  2228. */
  2229. XKB_EXPORT int
  2230. xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key);
  2231. /** @} */
  2232. /**
  2233. * @defgroup state Keyboard State
  2234. * Creating, destroying and manipulating keyboard state objects.
  2235. *
  2236. * @{
  2237. */
  2238. /**
  2239. * @page server-client-state Server State and Client State
  2240. * @parblock
  2241. *
  2242. * There are two distinct actors in most window-system architectures:
  2243. *
  2244. * <dl>
  2245. * <dt>Server</dt>
  2246. * <dd>
  2247. * For example: a Wayland compositor, an X11 server or an evdev listener.
  2248. *
  2249. * Servers maintain the XKB state for a device according to input events from
  2250. * the device, such as key presses and releases, and out-of-band events from
  2251. * the user, like UI layout switchers.
  2252. * </dd>
  2253. * <dt>Client</dt>
  2254. * <dd>
  2255. * For example: a Wayland client or an X11 client.
  2256. *
  2257. * Clients do not listen to input from the device; instead, whenever the
  2258. * server state changes, the server serializes the state and notifies the
  2259. * clients that the state has changed; the clients then update the state
  2260. * from the serialization.
  2261. * </dd>
  2262. * </dl>
  2263. *
  2264. * There are two corresponding APIs:
  2265. *
  2266. * <dl>
  2267. * <dt>`xkb_machine`: the *server* API</dt>
  2268. * <dd>
  2269. * This is the recommended API for **server** applications. It enables the full
  2270. * feature set that libxkbcommon supports.
  2271. *
  2272. * `xkb_machine` is a [Mealy machine]<!-- -->: it is a finite-state machine that takes a
  2273. * stream of raw key events – a pair ([keycode], [direction]) – as input, and
  2274. * produces a stream of atomic [XKB events](@ref xkb_event) as output.
  2275. *
  2276. * The observable state of the machine is exposed via a companion `xkb_state`
  2277. * object:
  2278. * - Create it with `xkb_state::xkb_state_new_with_mode()` using
  2279. * `::XKB_STATE_MODE_SERVER_QUERY`.
  2280. * - Update it with `xkb_state::xkb_state_update_event()`.
  2281. * - Query it (keysyms, modifiers, layout, LEDs) via the `xkb_state` query API.
  2282. *
  2283. * Note that the `xkb_machine` API supports events other than state
  2284. * components changes, such as key press/release events, so that it enables
  2285. * handling most of the XKB [key actions](@ref key-action-def).
  2286. *
  2287. * See the [example for a Wayland server](@ref quick-guide-wayland-server)
  2288. * in the quick guide.
  2289. *
  2290. * @since 1.14.0
  2291. * </dd>
  2292. * <dt>`xkb_state`: the *client* API (and legacy server API)</dt>
  2293. * <dd>
  2294. * This is the API for **client** applications and the *legacy API* for
  2295. * **server** applications.
  2296. *
  2297. * <dl>
  2298. * <dt>*Client* applications</dt>
  2299. * <dd>
  2300. * Create the state object with `xkb_state::xkb_state_new_with_mode()` using
  2301. * `::XKB_STATE_MODE_CLIENT`, then update it via
  2302. * `xkb_state::xkb_state_update_mask()` from server serializations.
  2303. * </dd>
  2304. * <dt>*Server* applications not using `xkb_machine`</dt>
  2305. * <dd>
  2306. * Create the state object with `xkb_state::xkb_state_new_with_mode()` using
  2307. * `::XKB_STATE_MODE_SERVER`, or with the legacy
  2308. * `xkb_state::xkb_state_new()` constructor, then update it via
  2309. * `xkb_state::xkb_state_update_key()` for key events and
  2310. * `xkb_state::xkb_state_update_synthetic()` for out-of-band inputs such as
  2311. * layout switchers.
  2312. * </dd>
  2313. * </dl>
  2314. *
  2315. * @warning Some entry points in the `xkb_state` API are only meant for servers
  2316. * and some are only meant for clients. Thus it is recommended to use
  2317. * `xkb_state::xkb_state_new_with_mode()` because it enforces correct usage at
  2318. * runtime and logs misuse as `::XKB_ERROR_UNEXPECTED_STATE_MODE`.
  2319. * Using `xkb_state::xkb_state_new()` does not enforce this: mixing entry
  2320. * points may lead to *incorrect state*.
  2321. *
  2322. * @note Since version 1.14.0, *server* applications should use the
  2323. * `xkb_machine` API, which supports more features.
  2324. *
  2325. * See the [examples for clients](@ref quick-guide-clients) in the quick guide.
  2326. * </dd>
  2327. * </dl>
  2328. *
  2329. * @endparblock
  2330. *
  2331. * [Mealy machine]: https://en.wikipedia.org/wiki/Mealy_machine
  2332. * [keycode]: @ref xkb_keycode_t
  2333. * [direction]: @ref xkb_key_direction
  2334. * [state machine]: @ref xkb_machine
  2335. * [keyboard events]: @ref xkb_event
  2336. * [event batch]: @ref xkb_events
  2337. * [event]: @ref xkb_event
  2338. */
  2339. /**
  2340. * @struct xkb_event
  2341. * Opaque keyboard state event object.
  2342. *
  2343. * Events are produced by `xkb_machine::xkb_machine_process_key()` and
  2344. * `xkb_machine::xkb_machine_process_synthetic()` and collected into an
  2345. * `xkb_events` batch. Each event represents one atomic state change or key
  2346. * action within a frame.
  2347. *
  2348. * Inspect the event type with `xkb_event::xkb_event_get_type()`, then extract
  2349. * data with the appropriate `xkb_event::xkb_event_get_*()` or
  2350. * `xkb_event::xkb_event_serialize_*()` functions.
  2351. *
  2352. * @warning Event pointers are only valid until the next call to
  2353. * `xkb_machine::xkb_machine_process_key()` or
  2354. * `xkb_machine::xkb_machine_process_synthetic()` on the
  2355. * same state machine. Do not store them beyond that point.
  2356. *
  2357. * @since 1.14.0
  2358. *
  2359. * @sa `xkb_event_type`
  2360. * @sa `xkb_events`
  2361. */
  2362. struct xkb_event;
  2363. /**
  2364. * @enum xkb_event_type
  2365. * Denotes the type of a [state event](@ref xkb_event).
  2366. *
  2367. * @since 1.14.0
  2368. */
  2369. enum xkb_event_type {
  2370. /**
  2371. * **Key _down_** event
  2372. *
  2373. * @since 1.14.0
  2374. */
  2375. XKB_EVENT_TYPE_KEY_DOWN = 1,
  2376. /**
  2377. * **Key _repeated_** event
  2378. *
  2379. * @since 1.14.0
  2380. */
  2381. XKB_EVENT_TYPE_KEY_REPEATED,
  2382. /**
  2383. * **Key _up_** event
  2384. *
  2385. * @since 1.14.0
  2386. */
  2387. XKB_EVENT_TYPE_KEY_UP,
  2388. /**
  2389. * **Components** change event
  2390. *
  2391. * @since 1.14.0
  2392. */
  2393. XKB_EVENT_TYPE_COMPONENTS_CHANGE,
  2394. };
  2395. /**
  2396. * Get the [type](@ref xkb_event_type) of an event.
  2397. *
  2398. * @param[in] event The event to process.
  2399. *
  2400. * @returns The event’s type.
  2401. *
  2402. * @since 1.14.0
  2403. *
  2404. * @memberof xkb_event
  2405. */
  2406. XKB_EXPORT enum xkb_event_type
  2407. xkb_event_get_type(const struct xkb_event *event);
  2408. /**
  2409. * Get the keycode associated to a [state event](@ref xkb_event) of type
  2410. * `::XKB_EVENT_TYPE_KEY_DOWN`, `::XKB_EVENT_TYPE_KEY_REPEATED` or
  2411. * `::XKB_EVENT_TYPE_KEY_UP`.
  2412. *
  2413. * @param[in] event The event object to process.
  2414. *
  2415. * @pre The event must be of one of the following types:
  2416. * - `::XKB_EVENT_TYPE_KEY_DOWN`
  2417. * - `::XKB_EVENT_TYPE_KEY_REPEATED`
  2418. * - `::XKB_EVENT_TYPE_KEY_UP`
  2419. * Otherwise the result is *undefined*.
  2420. *
  2421. * @returns The keycode corresponding to the event.
  2422. *
  2423. * @since 1.14.0
  2424. *
  2425. * @memberof xkb_event
  2426. */
  2427. XKB_EXPORT xkb_keycode_t
  2428. xkb_event_get_keycode(const struct xkb_event *event);
  2429. /**
  2430. * @enum xkb_state_component
  2431. * Component types for state objects, which belong to the following categories:
  2432. *
  2433. * - [modifier],
  2434. * - [layout],
  2435. * - [indicator] \(LED),
  2436. * - [keyboard global control].
  2437. *
  2438. * This enum is bitmaskable, e.g.
  2439. * `(::XKB_STATE_MODS_DEPRESSED | ::XKB_STATE_MODS_LATCHED)`
  2440. * is valid to exclude locked modifiers.
  2441. *
  2442. * In XKB, the `DEPRESSED` components are also known as *base*.
  2443. *
  2444. * [modifier]: @ref modifier-def
  2445. * [layout]: @ref layout-def
  2446. * [indicator]: @ref indicator-def
  2447. * [keyboard global control]: @ref xkb_keyboard_control_flags
  2448. */
  2449. enum xkb_state_component {
  2450. /**
  2451. * @parblock
  2452. * [Depressed modifiers], i.e. a key is physically holding them.
  2453. * @endparblock
  2454. *
  2455. * [Depressed modifiers]: @ref depressed-mod-def
  2456. */
  2457. XKB_STATE_MODS_DEPRESSED = (1 << 0),
  2458. /**
  2459. * @parblock
  2460. * [Latched modifiers], i.e. will be unset after the next non-modifier
  2461. * key press.
  2462. * @endparblock
  2463. *
  2464. * [Latched modifiers]: @ref latched-mod-def
  2465. */
  2466. XKB_STATE_MODS_LATCHED = (1 << 1),
  2467. /**
  2468. * @parblock
  2469. * [Locked modifiers], i.e. will be unset after the key provoking the
  2470. * lock has been pressed again.
  2471. * @endparblock
  2472. *
  2473. * [Locked modifiers]: @ref locked-mod-def
  2474. */
  2475. XKB_STATE_MODS_LOCKED = (1 << 2),
  2476. /**
  2477. * @parblock
  2478. * [Effective modifiers], i.e. currently active and affect key
  2479. * processing (derived from the other state components).
  2480. * @endparblock
  2481. * Use this unless you explicitly care how the state came about.
  2482. *
  2483. * [Effective modifiers]: @ref effective-modifier-encoding
  2484. */
  2485. XKB_STATE_MODS_EFFECTIVE = (1 << 3),
  2486. /**
  2487. * @parblock
  2488. * [Depressed layout], i.e. a key is physically holding it.
  2489. * @endparblock
  2490. *
  2491. * [Depressed layout]: @ref depressed-group-def
  2492. */
  2493. XKB_STATE_LAYOUT_DEPRESSED = (1 << 4),
  2494. /**
  2495. * @parblock
  2496. * [Latched layout], i.e. will be unset after the next non-modifier
  2497. * key press.
  2498. * @endparblock
  2499. *
  2500. * [Latched layout]: @ref latched-group-def
  2501. */
  2502. XKB_STATE_LAYOUT_LATCHED = (1 << 5),
  2503. /**
  2504. * @parblock
  2505. * [Locked layout], i.e. will be unset after the key provoking the lock
  2506. * has been pressed again.
  2507. * @endparblock
  2508. *
  2509. * [Locked layout]: @ref locked-group-def
  2510. */
  2511. XKB_STATE_LAYOUT_LOCKED = (1 << 6),
  2512. /**
  2513. * @parblock
  2514. * Effective layout, i.e. currently active and affects key processing
  2515. * (derived from the other state components).
  2516. * @endparblock
  2517. * Use this unless you explicitly care how the state came about.
  2518. */
  2519. XKB_STATE_LAYOUT_EFFECTIVE = (1 << 7),
  2520. /**
  2521. * [LEDs] \(derived from the other state components).
  2522. *
  2523. * [LEDs]: @ref indicator-def
  2524. */
  2525. XKB_STATE_LEDS = (1 << 8),
  2526. /**
  2527. * Effective [keyboard controls]
  2528. *
  2529. * @since 1.14.0
  2530. *
  2531. * [keyboard controls]: @ref xkb_keyboard_control_flags
  2532. */
  2533. XKB_STATE_CONTROLS = (1 << 9)
  2534. };
  2535. /**
  2536. * Get the [state components](@ref xkb_state_component) changes corresponding
  2537. * to a [state event](@ref xkb_event) of type
  2538. * `::XKB_EVENT_TYPE_COMPONENTS_CHANGE` .
  2539. *
  2540. * @param[in] event The event object to process.
  2541. *
  2542. * @pre The event must be of type `::XKB_EVENT_TYPE_COMPONENTS_CHANGE`.
  2543. * Otherwise the result is *undefined*.
  2544. *
  2545. * @returns The corresponding mask of state components that have changed.
  2546. * If nothing in the state has changed, returns 0.
  2547. *
  2548. * @since 1.14.0
  2549. *
  2550. * @memberof xkb_event
  2551. */
  2552. XKB_EXPORT enum xkb_state_component
  2553. xkb_event_get_changed_components(const struct xkb_event *event);
  2554. /**
  2555. * @enum xkb_keyboard_control_flags
  2556. * _Boolean_ **global keyboard controls**, which affect the way libxkbcommon
  2557. * handles the keyboard as a whole.
  2558. *
  2559. * This enumeration is bit-maskable.
  2560. *
  2561. * @since 1.14.0
  2562. */
  2563. enum xkb_keyboard_control_flags {
  2564. /**
  2565. * Do not apply any control.
  2566. *
  2567. * @since 1.14.0
  2568. */
  2569. XKB_KEYBOARD_CONTROL_NO_FLAGS = 0,
  2570. /**
  2571. * **Sticky keys** is an accessibility feature primarily aimed at helping
  2572. * people that find it difficult or impossible to press two keys at once.
  2573. *
  2574. * The `::XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS` control makes it easier for
  2575. * them to type by changing the behavior of the *modifier* and *group switch*
  2576. * keys. When *sticky keys* are enabled, <em>[set]</em> modifiers/group
  2577. * switch are transformed into their corresponding <em>[latch]</em> version:
  2578. * e.g. the user can first press a modifier, release it, then press another
  2579. * key.
  2580. *
  2581. * @sa `::XKB_A11Y_STICKY_KEYS_LATCH_TO_LOCK`
  2582. * @since 1.14.0
  2583. *
  2584. * [set]: @ref depressed-mod-def
  2585. * [latch]: @ref latched-mod-def
  2586. */
  2587. XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS = (1 << 0),
  2588. /**
  2589. * Enable the [keyboard overlay](@ref key-behavior-overlay) **1**.
  2590. *
  2591. * @since 1.14.0
  2592. */
  2593. XKB_KEYBOARD_CONTROL_OVERLAY1 = (1 << 1),
  2594. /**
  2595. * Enable the [keyboard overlay](@ref key-behavior-overlay) **2**.
  2596. *
  2597. * @since 1.14.0
  2598. */
  2599. XKB_KEYBOARD_CONTROL_OVERLAY2 = (1 << 2),
  2600. /**
  2601. * Enable the [keyboard overlay](@ref key-behavior-overlay) **3**.
  2602. *
  2603. * @since 1.14.0
  2604. */
  2605. XKB_KEYBOARD_CONTROL_OVERLAY3 = (1 << 3),
  2606. /**
  2607. * Enable the [keyboard overlay](@ref key-behavior-overlay) **4**.
  2608. *
  2609. * @since 1.14.0
  2610. */
  2611. XKB_KEYBOARD_CONTROL_OVERLAY4 = (1 << 4),
  2612. /**
  2613. * Enable the [keyboard overlay](@ref key-behavior-overlay) **5**.
  2614. *
  2615. * @since 1.14.0
  2616. */
  2617. XKB_KEYBOARD_CONTROL_OVERLAY5 = (1 << 5),
  2618. /**
  2619. * Enable the [keyboard overlay](@ref key-behavior-overlay) **6**.
  2620. *
  2621. * @since 1.14.0
  2622. */
  2623. XKB_KEYBOARD_CONTROL_OVERLAY6 = (1 << 6),
  2624. /**
  2625. * Enable the [keyboard overlay](@ref key-behavior-overlay) **7**.
  2626. *
  2627. * @since 1.14.0
  2628. */
  2629. XKB_KEYBOARD_CONTROL_OVERLAY7 = (1 << 7),
  2630. /**
  2631. * Enable the [keyboard overlay](@ref key-behavior-overlay) **8**.
  2632. *
  2633. * @since 1.14.0
  2634. */
  2635. XKB_KEYBOARD_CONTROL_OVERLAY8 = (1 << 8),
  2636. };
  2637. /**
  2638. * Serialization of the *boolean* [global keyboard controls]
  2639. * corresponding to a [state event](@ref xkb_event) of type
  2640. * `::XKB_EVENT_TYPE_COMPONENTS_CHANGE` .
  2641. *
  2642. * @param[in] event The event object to process.
  2643. * @param[in] components A mask of the keyboard control state components to
  2644. * serialize. State components other than `::XKB_STATE_CONTROLS` are ignored.
  2645. *
  2646. * @pre The event must be of type `::XKB_EVENT_TYPE_COMPONENTS_CHANGE`.
  2647. * Otherwise the result is *undefined*.
  2648. *
  2649. * @returns The corresponding [control mask](@ref xkb_keyboard_control_flags)
  2650. * representing the given components of the *boolean controls* state.
  2651. *
  2652. * @since 1.14.0
  2653. *
  2654. * @memberof xkb_event
  2655. *
  2656. * [global keyboard controls]: @ref xkb_keyboard_control_flags
  2657. */
  2658. XKB_EXPORT enum xkb_keyboard_control_flags
  2659. xkb_event_serialize_enabled_controls(const struct xkb_event *event,
  2660. enum xkb_state_component components);
  2661. /**
  2662. * Serialization of the [modifiers](@ref xkb_mod_mask_t)
  2663. * corresponding to a [state event](@ref xkb_event) of type
  2664. * `::XKB_EVENT_TYPE_COMPONENTS_CHANGE` .
  2665. *
  2666. * @param[in] event The event object to process.
  2667. * @param[in] components A mask of the modifier state components to serialize.
  2668. * State components other than `XKB_STATE_MODS_*` are ignored.
  2669. * If `::XKB_STATE_MODS_EFFECTIVE` is included, all other state components are
  2670. * ignored.
  2671. *
  2672. * @pre The event must be of type `::XKB_EVENT_TYPE_COMPONENTS_CHANGE`.
  2673. * Otherwise the result is *undefined*.
  2674. *
  2675. * @returns The corresponding [modifier mask](@ref xkb_mod_mask_t) representing
  2676. * the given components of the *modifier* state.
  2677. *
  2678. * @since 1.14.0
  2679. *
  2680. * @memberof xkb_event
  2681. */
  2682. XKB_EXPORT xkb_mod_mask_t
  2683. xkb_event_serialize_mods(const struct xkb_event *event,
  2684. enum xkb_state_component components);
  2685. /**
  2686. * Serialization of the [layout](@ref xkb_layout_index_t)
  2687. * corresponding to a [state event](@ref xkb_event) of type
  2688. * `::XKB_EVENT_TYPE_COMPONENTS_CHANGE` .
  2689. *
  2690. * @param[in] event The event object to process.
  2691. * @param[in] components A mask of the layout state components to serialize.
  2692. * State components other than `XKB_STATE_LAYOUT_*` are ignored.
  2693. * If `::XKB_STATE_LAYOUT_EFFECTIVE` is included, all other state components are
  2694. * ignored.
  2695. *
  2696. * @pre The event must be of type `::XKB_EVENT_TYPE_COMPONENTS_CHANGE`.
  2697. * Otherwise the result is *undefined*.
  2698. *
  2699. * @returns The corresponding [layout index](@ref xkb_layout_index_t)
  2700. * representing the given components of the *layout* state.
  2701. *
  2702. * @since 1.14.0
  2703. *
  2704. * @memberof xkb_event
  2705. */
  2706. XKB_EXPORT xkb_layout_index_t
  2707. xkb_event_serialize_layout(const struct xkb_event *event,
  2708. enum xkb_state_component components);
  2709. /**
  2710. * @struct xkb_events
  2711. * Opaque keyboard event collection object.
  2712. *
  2713. * An `xkb_events` batch collects [keyboard events](@ref xkb_event)
  2714. * produced atomically by a single call to an `process_*` function such as
  2715. * `xkb_machine::xkb_machine_process_key()`. Events are consumed
  2716. * sequentially via `xkb_events_next()`. The collection is reset on each
  2717. * `process_*` call.
  2718. *
  2719. * @since 1.14.0
  2720. *
  2721. * @sa `xkb_events_new_batch()`
  2722. * @sa `xkb_events_next()`
  2723. * @sa `xkb_events_destroy()`
  2724. * @sa `xkb_machine::xkb_machine_process_key()`
  2725. * @sa `xkb_machine::xkb_machine_process_synthetic()`
  2726. */
  2727. struct xkb_events;
  2728. /**
  2729. * @enum xkb_events_flags
  2730. *
  2731. * Flags for `xkb_events::xkb_events_new_batch()`.
  2732. *
  2733. * @since 1.14.0
  2734. */
  2735. enum xkb_events_flags {
  2736. /**
  2737. * Do not apply any flags.
  2738. *
  2739. * @since 1.14.0
  2740. */
  2741. XKB_EVENTS_NO_FLAGS = 0
  2742. };
  2743. /**
  2744. * Create a new [event](@ref xkb_event) batch.
  2745. *
  2746. * @param[in] context The context in which to create the batch.
  2747. * @param[in] flags Optional flags for the batch, or 0.
  2748. *
  2749. * @returns A new event batch, or `NULL` on failure.
  2750. *
  2751. * @since 1.14.0
  2752. *
  2753. * @sa `xkb_events_destroy()`
  2754. * @sa `xkb_events_next()`
  2755. * @sa `xkb_machine::xkb_machine_process_key()`
  2756. *
  2757. * @memberof xkb_events
  2758. */
  2759. XKB_EXPORT struct xkb_events *
  2760. xkb_events_new_batch(struct xkb_context *context, enum xkb_events_flags flags);
  2761. /**
  2762. * Free an event collection.
  2763. *
  2764. * @param[in] events
  2765. * The event collection to free.
  2766. * If it is `NULL`, this function does nothing.
  2767. *
  2768. * @since 1.14.0
  2769. *
  2770. * @sa `xkb_events_new_batch()`
  2771. *
  2772. * @memberof xkb_events
  2773. */
  2774. XKB_EXPORT void
  2775. xkb_events_destroy(struct xkb_events *events);
  2776. /**
  2777. * Get the next event from an event collection.
  2778. *
  2779. * @param[in] events The event collection.
  2780. *
  2781. * @returns The next event, or `NULL` if there are no more events to read.
  2782. *
  2783. * @since 1.14.0
  2784. *
  2785. * @memberof xkb_events
  2786. */
  2787. XKB_EXPORT const struct xkb_event *
  2788. xkb_events_next(struct xkb_events *events);
  2789. /**
  2790. * @struct xkb_machine_builder
  2791. * Opaque builder object to configure an `xkb_machine`.
  2792. *
  2793. * Create with `xkb_machine_builder_new()`, configure with the
  2794. * `xkb_machine_builder_*` functions, then build the state machine with
  2795. * `xkb_machine::xkb_machine_new()`.
  2796. * The builder object may be reused to create multiple `xkb_machine` objects
  2797. * and destroyed when no longer needed. If a single `xkb_machine` object is
  2798. * built, then the builder may be destroyed immediately after
  2799. * `xkb_machine::xkb_machine_new()` returns.
  2800. *
  2801. * @since 1.14.0
  2802. *
  2803. * @sa `xkb_machine_builder::xkb_machine_builder_new()`
  2804. * @sa `xkb_machine::xkb_machine_new()`
  2805. */
  2806. struct xkb_machine_builder;
  2807. /**
  2808. * @enum xkb_machine_builder_flags
  2809. * Flags for `xkb_machine_builder::xkb_machine_builder_new()`.
  2810. *
  2811. * @since 1.14.0
  2812. */
  2813. enum xkb_machine_builder_flags {
  2814. /**
  2815. * Do not apply any flags.
  2816. *
  2817. * @since 1.14.0
  2818. */
  2819. XKB_MACHINE_BUILDER_NO_FLAGS = 0,
  2820. };
  2821. /**
  2822. * Create a new `xkb_machine` builder object.
  2823. * `xkb_machine` objects can then be created from the builder using
  2824. * `xkb_machine::xkb_machine_new()`.
  2825. *
  2826. * @param[in] keymap The keymap which the state machine will use.
  2827. * @param[in] flags Flags to control the builder behavior, or 0.
  2828. *
  2829. * @returns A new `xkb_machine` builder object, or `NULL` on failure.
  2830. *
  2831. * @since 1.14.0
  2832. *
  2833. * @sa `xkb_machine_builder_destroy()`
  2834. * @sa `xkb_machine::xkb_machine_new()`
  2835. *
  2836. * @memberof xkb_machine_builder
  2837. */
  2838. XKB_EXPORT struct xkb_machine_builder *
  2839. xkb_machine_builder_new(struct xkb_keymap *keymap,
  2840. enum xkb_machine_builder_flags flags);
  2841. /**
  2842. * Free a `xkb_machine` builder object.
  2843. *
  2844. * @param[in] builder The `xkb_machine` builder. If it is `NULL`, this function
  2845. * does nothing.
  2846. *
  2847. * @since 1.14.0
  2848. *
  2849. * @sa `xkb_machine_builder_new()`
  2850. *
  2851. * @memberof xkb_machine_builder
  2852. */
  2853. XKB_EXPORT void
  2854. xkb_machine_builder_destroy(struct xkb_machine_builder *builder);
  2855. /**
  2856. * Get the keymap which a `xkb_machine_builder` object is using.
  2857. *
  2858. * @param[in] builder The state machine builder object.
  2859. *
  2860. * @returns The keymap which was passed to `xkb_machine_builder_new()` when
  2861. * creating this `xkb_machine_builder` object.
  2862. *
  2863. * @warning This function does not take a new reference on the keymap; you must
  2864. * explicitly reference it yourself if you plan to use it beyond the
  2865. * lifetime of the `xkb_machine_builder` object.
  2866. *
  2867. * @since 1.14.0
  2868. *
  2869. * @memberof xkb_machine_builder
  2870. */
  2871. XKB_EXPORT struct xkb_keymap *
  2872. xkb_machine_builder_get_keymap(const struct xkb_machine_builder *builder);
  2873. /**
  2874. * @enum xkb_a11y_flags
  2875. * Flags for
  2876. * `xkb_machine_builder::xkb_machine_builder_update_a11y_flags()`.
  2877. *
  2878. * These flags configure the accessibility (*a11y*) features.
  2879. *
  2880. * @since 1.14.0
  2881. */
  2882. enum xkb_a11y_flags {
  2883. /**
  2884. * Do not apply any flags.
  2885. *
  2886. * @since 1.14.0
  2887. */
  2888. XKB_A11Y_NO_FLAGS = 0,
  2889. /**
  2890. * If both `::XKB_A11Y_STICKY_KEYS_NO_SIMULTANEOUS_KEYS` and
  2891. * `::XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS` are activated, they enable
  2892. * users to deactivate [sticky keys] whenever two keys or more are pressed
  2893. * simultaneously.
  2894. *
  2895. * @sa `::XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS`
  2896. * @since 1.14.0
  2897. *
  2898. * [sticky keys]: @ref XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS
  2899. * @since 1.14.0
  2900. */
  2901. XKB_A11Y_STICKY_KEYS_NO_SIMULTANEOUS_KEYS = (1 << 0),
  2902. /**
  2903. * If both `::XKB_A11Y_STICKY_KEYS_LATCH_TO_LOCK` and
  2904. * `::XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS` are activated, they enable
  2905. * users to [lock] modifier keys without requiring special locking keys.
  2906. * The user can press a [latch] modifier twice in a row to lock it, and
  2907. * then unlock it by pressing it one more time.
  2908. *
  2909. * @sa `::XKB_KEYBOARD_CONTROL_A11Y_STICKY_KEYS`
  2910. * @since 1.14.0
  2911. *
  2912. * [latch]: @ref latched-mod-def
  2913. * [lock]: @ref locked-mod-def
  2914. */
  2915. XKB_A11Y_STICKY_KEYS_LATCH_TO_LOCK = (1 << 1),
  2916. /**
  2917. * Without this option, the [latch] keys are only triggers if keys are
  2918. * strictly *sequentially tapped*, e.g.:
  2919. * 1. `ISO_Level2_Latch` ↓
  2920. * 2. `ISO_Level2_Latch` ↑
  2921. * 3. `A` ↓
  2922. * 4. `A` ↑
  2923. *
  2924. * If one wants *multiple* active latches, they must be tapped in sequence:
  2925. * e.g.:
  2926. * 1. `ISO_Level2_Latch` ↓
  2927. * 2. `ISO_Level2_Latch` ↑
  2928. * 3. `ISO_Level3_Latch` ↓
  2929. * 4. `ISO_Level3_Latch` ↑
  2930. * 5. `A` ↓
  2931. * 6. `A` ↑
  2932. *
  2933. * This option relaxes the strict sequence requirement and enables operating
  2934. * keys that do not break latches *simultaneously* with a [latch] key, e.g.:
  2935. * 1. `ISO_Level2_Latch` ↓
  2936. * 2. `ISO_Level3_Latch` ↓
  2937. * 3. `ISO_Level2_Latch` ↑
  2938. * 4. `ISO_Level3_Latch` ↑
  2939. * 5. `A` ↓
  2940. * 6. `A` ↑
  2941. *
  2942. * This is an extension to the X11 XKB protocol and is enabled by default
  2943. * when using `::XKB_KEYMAP_FORMAT_TEXT_V2`.
  2944. *
  2945. * @since 1.14.0
  2946. *
  2947. * [latch]: @ref latched-mod-def
  2948. */
  2949. XKB_A11Y_LATCH_SIMULTANEOUS_KEYS = (1 << 2),
  2950. };
  2951. /**
  2952. * Update the accessibility flags of an `xkb_machine_builder` object.
  2953. *
  2954. * @param[in,out] builder The `xkb_machine` builder object to modify.
  2955. * @param[in] affect Accessibility flags to modify.
  2956. * @param[in] flags Accessibility flags to set or unset.
  2957. * Flags in @p affect but not in @p flags are cleared.
  2958. * Flags outside @p affect are not changed.
  2959. *
  2960. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  2961. *
  2962. * @since 1.14.0
  2963. *
  2964. * @memberof xkb_machine_builder
  2965. */
  2966. XKB_EXPORT enum xkb_error_code
  2967. xkb_machine_builder_update_a11y_flags(
  2968. struct xkb_machine_builder *builder,
  2969. enum xkb_a11y_flags affect,
  2970. enum xkb_a11y_flags flags
  2971. );
  2972. /**
  2973. * Remap a modifier combination, e.g. to make `Control+Alt` act as
  2974. * `LevelThree` (`AltGr`). This helps improve *compatibility* across platforms.
  2975. *
  2976. * The remapping takes effect only using
  2977. * `xkb_machine::xkb_machine_process_key()` and under certain
  2978. * conditions:
  2979. *
  2980. * - The corresponding *effective* modifiers are active.
  2981. * - The key being processed has a type that does *not* use any of the *source*
  2982. * modifiers.
  2983. * - There is no other remapping entry with the source modifiers being a
  2984. * superset of this entry. E.g. `Control+Alt` has priority over `Control`.
  2985. *
  2986. * @param[in,out] builder The `xkb_machine` builder object to modify.
  2987. * @param[in] source Modifier combination to remap, using their [encoding].
  2988. * Must be non-zero, unless both @p source and @p target
  2989. * are 0 to clear all entries.
  2990. * @param[in] target Modifier combination to remap to, using their
  2991. * [encoding], or 0 to remove the entry for @p source.
  2992. * If both @p source and @p target are 0, all entries are
  2993. * cleared.
  2994. *
  2995. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  2996. *
  2997. * Example:
  2998. *
  2999. * ```c
  3000. * struct xkb_keymap *keymap = xkb_machine_builder_get_keymap(builder);
  3001. * // Remap Control+Alt to LevelThree (AltGr)
  3002. * const xkb_mod_mask_t ctrl = xkb_keymap_mod_get_mask(keymap, XKB_MOD_NAME_CTRL);
  3003. * const xkb_mod_mask_t alt = xkb_keymap_mod_get_mask(keymap, XKB_VMOD_NAME_ALT);
  3004. * const xkb_mod_mask_t level3 = xkb_keymap_mod_get_mask(keymap, XKB_VMOD_NAME_LEVEL3);
  3005. * if (xkb_machine_builder_remap_mods(builder, ctrl | alt, level3)) {
  3006. * // handle error
  3007. * …
  3008. * }
  3009. * ```
  3010. *
  3011. * @since 1.14.0
  3012. *
  3013. * @memberof xkb_machine_builder
  3014. *
  3015. * [encoding]: @ref modifiers-encoding
  3016. */
  3017. XKB_EXPORT enum xkb_error_code
  3018. xkb_machine_builder_remap_mods(
  3019. struct xkb_machine_builder *builder,
  3020. xkb_mod_mask_t source,
  3021. xkb_mod_mask_t target
  3022. );
  3023. /**
  3024. * Set the modifiers that trigger the keyboard shortcut overrides.
  3025. *
  3026. * When any of the specified modifiers is active, the effective layout
  3027. * is substituted according to the mapping set by
  3028. * `xkb_machine_builder_remap_shortcut_layout()`.
  3029. * This ensures a consistent user experience with keyboard shortcuts
  3030. * across the layouts.
  3031. *
  3032. * @param[in,out] builder The `xkb_machine` builder object to modify.
  3033. * @param[in] affect Modifiers to consider, using their [encoding].
  3034. * @param[in] mask Modifiers to set or unset, using their [encoding].
  3035. * Modifiers in @p affect but not in @p mask are cleared.
  3036. * Modifiers outside @p affect are not changed.
  3037. *
  3038. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  3039. *
  3040. * @sa `xkb_machine_builder_remap_shortcut_layout()`
  3041. * @sa `xkb_keymap::xkb_keymap_mod_get_mask2()`
  3042. * @since 1.14.0
  3043. * @memberof xkb_machine_builder
  3044. *
  3045. * [encoding]: @ref modifiers-encoding
  3046. */
  3047. XKB_EXPORT enum xkb_error_code
  3048. xkb_machine_builder_update_shortcut_mods(struct xkb_machine_builder *builder,
  3049. xkb_mod_mask_t affect,
  3050. xkb_mod_mask_t mask);
  3051. /**
  3052. * Set a layout substitution for the shortcut layout override.
  3053. *
  3054. * When any modifier set via `xkb_machine_builder_update_shortcut_mods()` is
  3055. * active, the effective layout @p source is substituted with layout @p target
  3056. * in key processing. This allows shortcuts defined in layout @p target
  3057. * (typically a Latin layout) to remain reachable when layout @p source is
  3058. * active.
  3059. *
  3060. * @param[in,out] builder The `xkb_machine` builder object to modify.
  3061. * @param[in] source Source layout to substitute.
  3062. * @param[in] target Target layout to use instead of @p source.
  3063. *
  3064. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  3065. *
  3066. * @since 1.14.0
  3067. * @sa `xkb_machine_builder_update_shortcut_mods()`
  3068. * @memberof xkb_machine_builder
  3069. */
  3070. XKB_EXPORT enum xkb_error_code
  3071. xkb_machine_builder_remap_shortcut_layout(struct xkb_machine_builder *builder,
  3072. xkb_layout_index_t source,
  3073. xkb_layout_index_t target);
  3074. /**
  3075. * Create a new keyboard state machine object.
  3076. *
  3077. * This entry point is intended for *server* applications; *client* applications
  3078. * should not run a state machine locally: instead they should use the
  3079. * `xkb_state` API and process server state update using
  3080. * `xkb_state::xkb_state_update_mask()`.
  3081. * See @ref server-client-state for further information.
  3082. *
  3083. * @param[in] builder The [builder](@ref xkb_machine_builder) object from which
  3084. * to create the state machine.
  3085. *
  3086. * @returns A new keyboard state machine object, or `NULL` on failure.
  3087. *
  3088. * @since 1.14.0
  3089. *
  3090. * @sa `xkb_machine_builder::xkb_machine_builder_new()`
  3091. *
  3092. * @memberof xkb_machine
  3093. */
  3094. XKB_EXPORT struct xkb_machine *
  3095. xkb_machine_new(const struct xkb_machine_builder *builder);
  3096. /**
  3097. * Take a new reference on a `xkb_machine` object.
  3098. *
  3099. * @param[in] machine The state machine.
  3100. *
  3101. * @returns The passed in object.
  3102. *
  3103. * @since 1.14.0
  3104. *
  3105. * @memberof xkb_machine
  3106. */
  3107. XKB_EXPORT struct xkb_machine *
  3108. xkb_machine_ref(struct xkb_machine *machine);
  3109. /**
  3110. * Release a reference on a `xkb_machine` object, and possibly free it.
  3111. *
  3112. * @param[in] machine The state machine. If it is `NULL`, this function does nothing.
  3113. *
  3114. * @since 1.14.0
  3115. *
  3116. * @memberof xkb_machine
  3117. */
  3118. XKB_EXPORT void
  3119. xkb_machine_unref(struct xkb_machine *machine);
  3120. /**
  3121. * Get the keymap which a `xkb_machine` object is using.
  3122. *
  3123. * @param[in] machine The state machine.
  3124. *
  3125. * @returns The keymap which was used to create the `xkb_machine` object, i.e.
  3126. * the keymap passed to `xkb_machine_builder::xkb_machine_builder_new()` when
  3127. * creating the corresponding [builder](@ref xkb_machine_builder) used in
  3128. * `xkb_machine_new()`.
  3129. *
  3130. * @warning This function does not take a new reference on the keymap; you must
  3131. * explicitly reference it yourself if you plan to use it beyond the
  3132. * lifetime of the `xkb_machine` object.
  3133. *
  3134. * @since 1.14.0
  3135. *
  3136. * @memberof xkb_machine
  3137. */
  3138. XKB_EXPORT struct xkb_keymap *
  3139. xkb_machine_get_keymap(const struct xkb_machine *machine);
  3140. /**
  3141. * @enum xkb_key_direction
  3142. * Specifies the direction of the key (press / release) or a repetition.
  3143. */
  3144. enum xkb_key_direction {
  3145. /** The key was *released*. */
  3146. XKB_KEY_UP,
  3147. /** The key was *pressed*. */
  3148. XKB_KEY_DOWN,
  3149. /**
  3150. * The key was *repeated*.
  3151. *
  3152. * This should be used by the compositor only if it handles key repetition
  3153. * itself.
  3154. *
  3155. * @since 1.14.0
  3156. */
  3157. XKB_KEY_REPEATED
  3158. };
  3159. /**
  3160. * Process a key event – a pair ([keycode], [direction]) – through the XKB
  3161. * [state machine], and collect the resulting [keyboard events] into an
  3162. * [event batch].
  3163. *
  3164. * The produced events form a single *frame*.
  3165. *
  3166. * Use this function for in-band (device) inputs.
  3167. * Use `xkb_machine_process_synthetic()` instead to update the state machine in
  3168. * response to out-of-band (non-device) inputs, such as UI layout switchers or
  3169. * accessibility settings changes.
  3170. *
  3171. * A series of calls to this function should be consistent; that is, a call
  3172. * with `::XKB_KEY_DOWN` for a key should be matched by an `::XKB_KEY_UP`; if a
  3173. * key is pressed twice, it should be released twice; etc. Otherwise (e.g. due
  3174. * to missed input events), situations like “stuck modifiers” may occur.
  3175. *
  3176. * @param[in,out] machine The XKB [state machine] object.
  3177. * @param[in] key The keycode of the key being operated.
  3178. * @param[in] direction The direction of the key operation.
  3179. * @param[out] events The event batch to collect events into. It will be
  3180. * reset before collecting.
  3181. *
  3182. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  3183. *
  3184. * @since 1.14.0
  3185. *
  3186. * @sa `xkb_machine_process_synthetic()`
  3187. *
  3188. * @memberof xkb_machine
  3189. *
  3190. * [keycode]: @ref xkb_keycode_t
  3191. * [direction]: @ref xkb_key_direction
  3192. * [state machine]: @ref xkb_machine
  3193. * [keyboard events]: @ref xkb_event
  3194. * [event batch]: @ref xkb_events
  3195. */
  3196. XKB_EXPORT enum xkb_error_code
  3197. xkb_machine_process_key(struct xkb_machine *machine,
  3198. xkb_keycode_t key, enum xkb_key_direction direction,
  3199. struct xkb_events *events);
  3200. /**
  3201. * @struct xkb_state_components_update
  3202. * Latched and locked state components for an out-of-band state update.
  3203. *
  3204. * Carries the modifier, layout and boolean controls assignments for
  3205. * `xkb_state_update`.
  3206. * Used to update latched and locked modifiers and layouts atomically via
  3207. * `xkb_machine::xkb_machine_process_synthetic()`.
  3208. *
  3209. * Which fields are considered is determined by `components`:
  3210. * - `::XKB_STATE_MODS_LATCHED` → `affect_latched_mods`, `latched_mods`
  3211. * - `::XKB_STATE_MODS_LOCKED` → `affect_locked_mods`, `locked_mods`
  3212. * - `::XKB_STATE_LAYOUT_LATCHED` → `latched_layout`
  3213. * - `::XKB_STATE_LAYOUT_LOCKED` → `locked_layout`
  3214. * - `::XKB_STATE_CONTROLS` → `affect_controls`, `controls`
  3215. *
  3216. * @note This struct uses a **size-based versioning** scheme to allow
  3217. * forward and compatibility between callers and the library:
  3218. * <dl>
  3219. * <dt>Older callers (smaller struct)</dt>
  3220. * <dd>
  3221. * Trailing fields unknown to the caller default to zero in the library.
  3222. * </dd>
  3223. * <dt>Newer callers (larger struct)</dt>
  3224. * <dd>
  3225. * Accepted only if all trailing bytes unknown to the library are zero.
  3226. * </dd>
  3227. * </dl>
  3228. *
  3229. * @pre The struct MUST be initialized with `memset()` before setting any
  3230. * fields:
  3231. * ```c
  3232. * struct xkb_state_components_update update;
  3233. * memset(&update, 0, sizeof(update));
  3234. * update.size = sizeof(update);
  3235. * update.components = …;
  3236. * ```
  3237. *
  3238. * @invariant #size MUST be explicitly set to
  3239. * `sizeof(struct xkb_state_components_update)`.
  3240. * @invariant All bytes of the struct, including padding, MUST remain zero
  3241. * except for *explicitly* assigned fields.
  3242. *
  3243. * @since 1.14.0
  3244. *
  3245. * @sa `xkb_state_update`
  3246. */
  3247. struct xkb_state_components_update {
  3248. /**
  3249. * Size of this structure, for forward-compatibility.
  3250. *
  3251. * @sa `::XKB_ERROR_ABI_INVALID_STRUCT_SIZE`
  3252. * @sa `::XKB_ERROR_ABI_BACKWARD_COMPAT`
  3253. * @sa `::XKB_ERROR_ABI_FORWARD_COMPAT`
  3254. *
  3255. * @since 1.14.0
  3256. */
  3257. size_t size;
  3258. /**
  3259. * Mask of [state components](@ref xkb_state_component) to update.
  3260. *
  3261. * The following components are meaningful:
  3262. * - `::XKB_STATE_MODS_LATCHED`
  3263. * - `::XKB_STATE_MODS_LOCKED`
  3264. * - `::XKB_STATE_LAYOUT_LATCHED`
  3265. * - `::XKB_STATE_LAYOUT_LOCKED`
  3266. * - `::XKB_STATE_CONTROLS`
  3267. *
  3268. * Other components are ignored.
  3269. *
  3270. * @sa `xkb_state_component`
  3271. *
  3272. * @since 1.14.0
  3273. */
  3274. uint32_t components;
  3275. /**
  3276. * Mask of [latched modifiers] to affect.
  3277. *
  3278. * Only modifiers present in this mask are considered when updating
  3279. * `latched_mods`. Only considered if `::XKB_STATE_MODS_LATCHED` is
  3280. * set in `components`.
  3281. *
  3282. * @since 1.14.0
  3283. *
  3284. * [latched modifiers]: @ref latched-mod-def
  3285. */
  3286. xkb_mod_mask_t affect_latched_mods;
  3287. /**
  3288. * Modifiers to set as [latched] or unlatched.
  3289. *
  3290. * Only modifiers in `affect_latched_mods` are considered. Only
  3291. * considered if `::XKB_STATE_MODS_LATCHED` is set in `components`.
  3292. *
  3293. * @since 1.14.0
  3294. *
  3295. * [latched]: @ref latched-mod-def
  3296. */
  3297. xkb_mod_mask_t latched_mods;
  3298. /**
  3299. * Mask of [locked modifiers] to affect.
  3300. *
  3301. * Only modifiers present in this mask are considered when updating
  3302. * `locked_mods`. Only considered if `::XKB_STATE_MODS_LOCKED` is
  3303. * set in `components`.
  3304. *
  3305. * @since 1.14.0
  3306. *
  3307. * [locked modifiers]: @ref locked-mod-def
  3308. */
  3309. xkb_mod_mask_t affect_locked_mods;
  3310. /**
  3311. * Modifiers to set as [locked] or unlocked.
  3312. *
  3313. * Only modifiers in `affect_locked_mods` are considered. Only
  3314. * considered if `::XKB_STATE_MODS_LOCKED` is set in `components`.
  3315. *
  3316. * @since 1.14.0
  3317. *
  3318. * [locked]: @ref locked-mod-def
  3319. */
  3320. xkb_mod_mask_t locked_mods;
  3321. /**
  3322. * Layout to latch.
  3323. *
  3324. * May be out of range (including negative); the layout is brought into
  3325. * range according to the current out-of-range layout policy. Only
  3326. * considered if `::XKB_STATE_LAYOUT_LATCHED` is set in `components`.
  3327. *
  3328. * @sa `xkb_layout_index_t`
  3329. *
  3330. * @since 1.14.0
  3331. */
  3332. int32_t latched_layout;
  3333. /**
  3334. * Layout to lock.
  3335. *
  3336. * May be out of range (including negative); the layout is brought into
  3337. * range according to the current out-of-range layout policy. Only
  3338. * considered if `::XKB_STATE_LAYOUT_LOCKED` is set in `components`.
  3339. *
  3340. * @sa `xkb_layout_index_t`
  3341. *
  3342. * @since 1.14.0
  3343. */
  3344. int32_t locked_layout;
  3345. /**
  3346. * Mask of boolean [keyboard controls] to affect.
  3347. *
  3348. * Only controls present in this mask are considered when updating
  3349. * `controls`. Only considered if `::XKB_STATE_CONTROLS` is set in
  3350. * `components`.
  3351. *
  3352. * @sa `xkb_keyboard_control_flags`
  3353. *
  3354. * @since 1.14.0
  3355. *
  3356. * [keyboard controls]: @ref xkb_keyboard_control_flags
  3357. */
  3358. uint32_t affect_controls;
  3359. /**
  3360. * Mask of boolean [keyboard controls] to enable or disable.
  3361. *
  3362. * Only controls in `affect_controls` are considered. Only considered
  3363. * if `::XKB_STATE_CONTROLS` is set in `components`.
  3364. *
  3365. * @sa `xkb_keyboard_control_flags`
  3366. *
  3367. * @since 1.14.0
  3368. *
  3369. * [keyboard controls]: @ref xkb_keyboard_control_flags
  3370. */
  3371. uint32_t controls;
  3372. /**
  3373. * @private
  3374. *
  3375. * Reserved for future extensions.
  3376. *
  3377. * @pre Must be set to `0` by the caller.
  3378. */
  3379. uint32_t reserved;
  3380. };
  3381. /**
  3382. * @enum xkb_layout_out_of_range_policy
  3383. * Policies defining how to bring out-of-range layout indices into range.
  3384. *
  3385. * @since 1.14.0
  3386. */
  3387. enum xkb_layout_out_of_range_policy {
  3388. /**
  3389. * Wrap into range using integer modulus (default).
  3390. *
  3391. * @since 1.14.0
  3392. */
  3393. XKB_LAYOUT_OUT_OF_RANGE_WRAP = 0,
  3394. /**
  3395. * Clamp into range, i.e. invalid indices are corrected to the closest
  3396. * valid bound (0 or highest layout index).
  3397. *
  3398. * @since 1.14.0
  3399. */
  3400. XKB_LAYOUT_OUT_OF_RANGE_CLAMP,
  3401. /**
  3402. * Redirect to a specific [layout index](@ref xkb_layout_index_t).
  3403. *
  3404. * @since 1.14.0
  3405. */
  3406. XKB_LAYOUT_OUT_OF_RANGE_REDIRECT
  3407. };
  3408. /**
  3409. * @struct xkb_layout_policy_update
  3410. * Configures the policy used to bring out-of-range layout indices into range.
  3411. *
  3412. * If `policy` is `::XKB_LAYOUT_OUT_OF_RANGE_REDIRECT`, `redirect` specifies
  3413. * the target layout index; otherwise `redirect` is ignored.
  3414. *
  3415. * @note This struct uses a **size-based versioning** scheme to allow
  3416. * forward and backward compatibility between callers and the library:
  3417. * <dl>
  3418. * <dt>Older callers (smaller struct)</dt>
  3419. * <dd>
  3420. * Trailing fields unknown to the caller default to zero in the library.
  3421. * </dd>
  3422. * <dt>Newer callers (larger struct)</dt>
  3423. * <dd>
  3424. * Accepted only if all trailing bytes unknown to the library are zero.
  3425. * </dd>
  3426. * </dl>
  3427. *
  3428. * @pre The struct MUST be initialized with `memset()` before setting any
  3429. * fields:
  3430. * ```c
  3431. * struct xkb_layout_policy_update update;
  3432. * memset(&update, 0, sizeof(update));
  3433. * update.size = sizeof(update);
  3434. * update.policy = …;
  3435. * ```
  3436. *
  3437. * @invariant #size MUST be explicitly set to
  3438. * `sizeof(struct xkb_layout_policy_update)`.
  3439. * @invariant All bytes of the struct, including padding, MUST remain zero
  3440. * except for *explicitly* assigned fields.
  3441. *
  3442. * @since 1.14.0
  3443. *
  3444. * @sa `xkb_layout_out_of_range_policy`
  3445. * @sa `xkb_state_update::layout_policy`
  3446. */
  3447. struct xkb_layout_policy_update {
  3448. /**
  3449. * Size of this structure, for forward-compatibility.
  3450. *
  3451. * @sa `::XKB_ERROR_ABI_INVALID_STRUCT_SIZE`
  3452. * @sa `::XKB_ERROR_ABI_BACKWARD_COMPAT`
  3453. * @sa `::XKB_ERROR_ABI_FORWARD_COMPAT`
  3454. *
  3455. * @since 1.14.0
  3456. */
  3457. size_t size;
  3458. /**
  3459. * [Policy] to use to handle out-of-range layout indices.
  3460. *
  3461. * @sa `xkb_layout_out_of_range_policy`
  3462. *
  3463. * @since 1.14.0
  3464. *
  3465. * [Policy]: @ref xkb_layout_out_of_range_policy
  3466. */
  3467. uint32_t policy;
  3468. /**
  3469. * Layout index to redirect to when `policy` is
  3470. * `::XKB_LAYOUT_OUT_OF_RANGE_REDIRECT`. Ignored otherwise.
  3471. *
  3472. * @since 1.14.0
  3473. */
  3474. xkb_layout_index_t redirect;
  3475. };
  3476. /**
  3477. * @struct xkb_state_update
  3478. * Request to process an out-of-band atomic update through an `xkb_machine` or
  3479. * `xkb_state`.
  3480. *
  3481. * Used with `xkb_state::xkb_state_update_synthetic()` and
  3482. * `xkb_machine::xkb_machine_process_synthetic()` to atomically
  3483. * apply any combination of:
  3484. * - Latched and locked modifier and layout changes (via `components`)
  3485. * - Boolean keyboard control changes (via `components`)
  3486. * - Parameterized keyboard control changes (via `layout_policy`)
  3487. *
  3488. * A `NULL` pointer means “not set / no change”.
  3489. *
  3490. * @note This struct uses a **size-based versioning** scheme to allow
  3491. * forward and backward compatibility between callers and the library:
  3492. * <dl>
  3493. * <dt>Older callers (smaller struct)</dt>
  3494. * <dd>
  3495. * Trailing fields unknown to the caller default to zero in the library.
  3496. * </dd>
  3497. * <dt>Newer callers (larger struct)</dt>
  3498. * <dd>
  3499. * Accepted only if all trailing bytes unknown to the library are zero.
  3500. * </dd>
  3501. * </dl>
  3502. *
  3503. * @pre The struct MUST be initialized with `memset()` before setting any
  3504. * fields:
  3505. * ```c
  3506. * struct xkb_state_update update;
  3507. * memset(&update, 0, sizeof(update));
  3508. * update.size = sizeof(update);
  3509. * update.components = …;
  3510. * ```
  3511. *
  3512. * @invariant #size MUST be explicitly set to `sizeof(struct xkb_state_update)`.
  3513. * @invariant All bytes of the struct, including padding, MUST remain zero
  3514. * except for *explicitly* assigned fields.
  3515. *
  3516. * @sa `xkb_state::xkb_state_update_synthetic()`
  3517. * @sa `xkb_machine::xkb_machine_process_synthetic()`
  3518. * @sa `xkb_state_components_update`
  3519. * @sa `xkb_layout_policy_update`
  3520. *
  3521. * @since 1.14.0
  3522. */
  3523. struct xkb_state_update {
  3524. /**
  3525. * Size of this structure, for forward-compatibility.
  3526. *
  3527. * @sa `::XKB_ERROR_ABI_INVALID_STRUCT_SIZE`
  3528. * @sa `::XKB_ERROR_ABI_BACKWARD_COMPAT`
  3529. * @sa `::XKB_ERROR_ABI_FORWARD_COMPAT`
  3530. *
  3531. * @since 1.14.0
  3532. */
  3533. size_t size;
  3534. /**
  3535. * Components updates, or `NULL` for no change.
  3536. *
  3537. * @sa `xkb_state_component`
  3538. *
  3539. * @since 1.14.0
  3540. */
  3541. const struct xkb_state_components_update *components;
  3542. /**
  3543. * Out-of-range layout policy update, or `NULL` for no change.
  3544. *
  3545. * @sa `xkb_layout_out_of_range_policy`
  3546. *
  3547. * @since 1.14.0
  3548. */
  3549. const struct xkb_layout_policy_update *layout_policy;
  3550. };
  3551. /**
  3552. * Process a *synthetic* (out-of-band) atomic update through the XKB
  3553. * [state machine], and collect the resulting [keyboard events] into an
  3554. * [event batch].
  3555. *
  3556. * Use this function to update the state machine in response to
  3557. * out-of-band (non-device) inputs, such as UI layout switchers or
  3558. * accessibility settings changes.
  3559. * Use `xkb_machine_process_key()` instead for in-band (device) inputs.
  3560. *
  3561. * All changes specified in @p update are applied atomically as a single
  3562. * *frame*: the resulting events reflect the **net** state change at the
  3563. * end of the frame, not intermediate steps. In particular, a
  3564. * `::XKB_EVENT_TYPE_COMPONENTS_CHANGE` event in the batch represents the
  3565. * cumulative state change for the entire frame — individual intermediate
  3566. * state transitions are not observable.
  3567. *
  3568. * Only latched, locked and control components can be updated out-of-band;
  3569. * depressed components can only change through key presses via
  3570. * `xkb_machine_process_key()`.
  3571. *
  3572. * @par Layout out of range
  3573. * @parblock
  3574. * If the effective layout, after taking into account the depressed, latched
  3575. * and locked layout, is out of range (negative or greater than the maximum
  3576. * layout index), it is brought into range according to the current
  3577. * out-of-range layout policy (see `xkb_layout_out_of_range_policy`).
  3578. * @endparblock
  3579. *
  3580. * @param[in,out] machine The XKB [state machine] object.
  3581. * @param[in] update The update to apply.
  3582. * Must have `xkb_state_update::size` set.
  3583. * @param[out] events The event batch to collect events into. It will be
  3584. * reset before collecting.
  3585. *
  3586. * @returns `::XKB_SUCCESS` on success, otherwise an error code.
  3587. *
  3588. * @since 1.14.0
  3589. *
  3590. * @sa `xkb_state_update`
  3591. * @sa `xkb_machine_process_key()`
  3592. * @memberof xkb_machine
  3593. *
  3594. * [state machine]: @ref xkb_machine
  3595. * [keyboard events]: @ref xkb_event
  3596. * [event batch]: @ref xkb_events
  3597. */
  3598. XKB_EXPORT enum xkb_error_code
  3599. xkb_machine_process_synthetic(struct xkb_machine *machine,
  3600. const struct xkb_state_update *update,
  3601. struct xkb_events *events);
  3602. /**
  3603. * @enum xkb_state_mode
  3604. * Mode for creating a [keyboard state object](@ref xkb_state).
  3605. *
  3606. * @since 1.14.0
  3607. * @sa `xkb_state::xkb_state_new_with_mode()`
  3608. */
  3609. enum xkb_state_mode {
  3610. /**
  3611. * State driven by **serialized state updates** via
  3612. * `xkb_state::xkb_state_update_mask()`.
  3613. *
  3614. * Use this mode for *client* applications.
  3615. *
  3616. * This is the *recommended* mode for new client applications, as it creates
  3617. * `xkb_state` objects with much *smaller* memory footprint than with
  3618. * `xkb_state::xkb_state_new()`.
  3619. *
  3620. * @important `xkb_state` objects created with this mode cannot be used
  3621. * with the following API:
  3622. * - `xkb_state::xkb_state_update_event()`
  3623. * - `xkb_state::xkb_state_update_key()`
  3624. * - `xkb_state::xkb_state_update_synthetic()`
  3625. * - `xkb_state::xkb_state_update_latched_locked()` *(deprecated)*
  3626. *
  3627. * @since 1.14.0
  3628. */
  3629. XKB_STATE_MODE_CLIENT = 0,
  3630. /**
  3631. * State driven by <strong>[XKB events]</strong> via
  3632. * `xkb_state::xkb_state_update_event()`.
  3633. *
  3634. * Use this mode for an observable state companion to an `xkb_machine` in
  3635. * *server* applications using the `xkb_machine` API.
  3636. *
  3637. * This is the *recommended* mode for new server applications, as it creates
  3638. * `xkb_state` objects with much *smaller* memory footprint than with
  3639. * `xkb_state::xkb_state_new()`.
  3640. *
  3641. * @important `xkb_state` objects created with this mode cannot be used
  3642. * with the following API:
  3643. * - `xkb_state::xkb_state_update_mask()`
  3644. * - `xkb_state::xkb_state_update_key()`
  3645. * - `xkb_state::xkb_state_update_synthetic()`
  3646. * - `xkb_state::xkb_state_update_latched_locked()` *(deprecated)*
  3647. *
  3648. * @since 1.14.0
  3649. *
  3650. * [XKB events]: @ref xkb_event
  3651. */
  3652. XKB_STATE_MODE_SERVER_QUERY = 1,
  3653. /**
  3654. * State driven directly by **key events**, via
  3655. * `xkb_state::xkb_state_update_key()` or by *synthetic input events*, via
  3656. * `xkb_state::xkb_state_update_synthetic()`.
  3657. *
  3658. * Use this mode for *server* applications that do not use the preferred
  3659. * full-featured `xkb_machine` API.
  3660. *
  3661. * @important Contrary to `xkb_state::xkb_state_new()`, `xkb_state` objects
  3662. * created with this mode cannot be used with the following API:
  3663. * - `xkb_state::xkb_state_update_mask()`
  3664. * - `xkb_state::xkb_state_update_event()`
  3665. *
  3666. * @warning Prefer `xkb_machine` for new server applications.
  3667. * This mode exists for *compatibility* with code predating the
  3668. * `xkb_machine` API and may be deprecated in a future release.
  3669. *
  3670. * @since 1.14.0
  3671. */
  3672. XKB_STATE_MODE_SERVER = 2,
  3673. };
  3674. /**
  3675. * Create a new keyboard state object with an explicit mode.
  3676. *
  3677. * This entry point is intended for both server and client applications.
  3678. * It enables using the optimal implementation for the intended use.
  3679. *
  3680. * @param[in] keymap The keymap which the state will use.
  3681. * @param[in] mode The [state mode](@ref xkb_state_mode) to use.
  3682. *
  3683. * @returns A new keyboard state object, or `NULL` on failure.
  3684. *
  3685. * @since 1.14.0
  3686. * @sa `xkb_state_mode`
  3687. * @memberof xkb_state
  3688. */
  3689. XKB_EXPORT struct xkb_state *
  3690. xkb_state_new_with_mode(struct xkb_keymap *keymap, enum xkb_state_mode mode);
  3691. /**
  3692. * Create a new keyboard state object.
  3693. *
  3694. * @note This is the legacy constructor, predating the `xkb_machine` API.
  3695. * It imposes no restrictions on which update functions may be called,
  3696. * making it easy to accidentally mix incompatible update paths. Prefer
  3697. * `xkb_state::xkb_state_new_with_mode()` for new code, which enforces
  3698. * correct API usage at runtime and optimal performance.
  3699. *
  3700. * @param[in] keymap The keymap which the state will use.
  3701. *
  3702. * @returns A new keyboard state object, or `NULL` on failure.
  3703. *
  3704. * @memberof xkb_state
  3705. */
  3706. XKB_EXPORT struct xkb_state *
  3707. xkb_state_new(struct xkb_keymap *keymap);
  3708. /**
  3709. * Take a new reference on a keyboard state object.
  3710. *
  3711. * @param[in] state The [state](@ref xkb_state) to reference.
  3712. *
  3713. * @returns The passed-in object.
  3714. *
  3715. * @memberof xkb_state
  3716. */
  3717. XKB_EXPORT struct xkb_state *
  3718. xkb_state_ref(struct xkb_state *state);
  3719. /**
  3720. * Release a reference on a keyboard state object, and possibly free it.
  3721. *
  3722. * @param[in] state The state. If it is `NULL`, this function does nothing.
  3723. *
  3724. * @memberof xkb_state
  3725. */
  3726. XKB_EXPORT void
  3727. xkb_state_unref(struct xkb_state *state);
  3728. /**
  3729. * Get the keymap which a keyboard state object is using.
  3730. *
  3731. * @param[in] state The keyboard state object.
  3732. *
  3733. * @returns The keymap which was passed to `xkb_state_new()` or
  3734. * `xkb_state_new_with_mode()` when creating this state object.
  3735. *
  3736. * @warning This function does not take a new reference on the keymap; you must
  3737. * explicitly reference it yourself if you plan to use it beyond the
  3738. * lifetime of the state.
  3739. *
  3740. * @memberof xkb_state
  3741. */
  3742. XKB_EXPORT struct xkb_keymap *
  3743. xkb_state_get_keymap(struct xkb_state *state);
  3744. /**
  3745. * Update a keyboard state from a set of explicit masks.
  3746. *
  3747. * This entry point is intended for *client* applications; see @ref
  3748. * server-client-state for details. *Server* applications should use
  3749. * either the recommended modern `xkb_machine` API with the corresponding
  3750. * `xkb_state_update_event()` or the legacy `xkb_state_update_synthetic()`
  3751. * API instead.
  3752. *
  3753. * @param[in,out] state The keyboard state object.
  3754. * @param[in] depressed_mods Modifiers to set as depressed.
  3755. * @param[in] latched_mods Modifiers to set as latched.
  3756. * @param[in] locked_mods Modifiers to set as locked.
  3757. * @param[in] depressed_layout Layout to set as depressed.
  3758. * @param[in] latched_layout Layout to set as latched.
  3759. * @param[in] locked_layout Layout to set as locked.
  3760. *
  3761. * @pre *All* parameters must represent a *consistent* snapshot of the server
  3762. * keyboard state at a single point in time. In case the server provides only a
  3763. * single layout state (e.g. the effective layout in the Wayland protocol), it
  3764. * should be used for @p locked_layout, while @p depressed_layout and
  3765. * @p latched_layout are both set to `0`.
  3766. *
  3767. * @important If @p state was not created with `::XKB_STATE_MODE_CLIENT` or
  3768. * `xkb_state_new()`, the call is *rejected* without updating the state,
  3769. * and the misuse is logged as `::XKB_ERROR_UNEXPECTED_STATE_MODE`.
  3770. * The return value is `0` in this case, which is indistinguishable from
  3771. * a no-op update.
  3772. *
  3773. * @returns A mask of state components that have changed as a result of
  3774. * the update. If nothing in the state has changed, returns 0.
  3775. *
  3776. * @sa `xkb_state_component`
  3777. * @sa `xkb_state_update_synthetic()`
  3778. * @sa `xkb_state_update_event()`
  3779. *
  3780. * @memberof xkb_state
  3781. */
  3782. XKB_EXPORT enum xkb_state_component
  3783. xkb_state_update_mask(struct xkb_state *state,
  3784. xkb_mod_mask_t depressed_mods,
  3785. xkb_mod_mask_t latched_mods,
  3786. xkb_mod_mask_t locked_mods,
  3787. xkb_layout_index_t depressed_layout,
  3788. xkb_layout_index_t latched_layout,
  3789. xkb_layout_index_t locked_layout);
  3790. /**
  3791. * Update the keyboard state [components](@ref xkb_state_component) from an
  3792. * [event](@ref xkb_event).
  3793. *
  3794. * This entry point is intended for *server* applications and should not be used
  3795. * by *client* applications; see @ref server-client-state for details.
  3796. *
  3797. * It enables server applications to use `xkb_state` as the observable state
  3798. * companion to an `xkb_machine`: feed each event produced by
  3799. * `xkb_machine::xkb_machine_process_key()` or
  3800. * `xkb_machine::xkb_machine_process_synthetic()` into this
  3801. * function to keep the observable state in sync.
  3802. *
  3803. * @param[in,out] state The keyboard state object.
  3804. * @param[in] event The state event to update from.
  3805. *
  3806. * @important If @p state was not created with `::XKB_STATE_MODE_SERVER_QUERY`
  3807. * or `xkb_state_new()`, the call is *rejected* without updating the state,
  3808. * and the misuse is logged as `::XKB_ERROR_UNEXPECTED_STATE_MODE`.
  3809. * The return value is `0` in this case, which is indistinguishable from
  3810. * a no-op update.
  3811. *
  3812. * @returns A mask of state components that have changed as a result of
  3813. * the update. If nothing in the state has changed, returns 0.
  3814. *
  3815. * @since 1.14.0
  3816. *
  3817. * @memberof xkb_state
  3818. */
  3819. XKB_EXPORT enum xkb_state_component
  3820. xkb_state_update_event(struct xkb_state *state,
  3821. const struct xkb_event *event);
  3822. /**
  3823. * Update the keyboard state to reflect a given key being pressed or
  3824. * released.
  3825. *
  3826. * This entry point is intended for *server* applications and should not be used
  3827. * by *client* applications; see @ref server-client-state for details.
  3828. *
  3829. * A series of calls to this function should be consistent; that is, a call
  3830. * with `::XKB_KEY_DOWN` for a key should be matched by an `::XKB_KEY_UP`; if a
  3831. * key is pressed twice, it should be released twice; etc. Otherwise (e.g. due
  3832. * to missed input events), situations like “stuck modifiers” may occur.
  3833. *
  3834. * This function is often used in conjunction with the function
  3835. * `xkb_state_key_get_syms()` (or `xkb_state_key_get_one_sym()`), for example,
  3836. * when handling a key event. In this case, you should prefer to get the
  3837. * keysyms *before* updating the key, such that the keysyms reported for
  3838. * the key event are not affected by the event itself. This is the
  3839. * conventional behavior.
  3840. *
  3841. * @note This is the legacy server entry point and only supports a restricted
  3842. * set of libxkbcommon features. Since 1.14.0, prefer `xkb_machine` for new
  3843. * server applications to enable the full feature set.
  3844. *
  3845. * @param[in,out] state The keyboard state object.
  3846. * @param[in] key The key being operated.
  3847. * @param[in] direction The direction of the key operation.
  3848. *
  3849. * @important If @p state was not created with `::XKB_STATE_MODE_SERVER` or
  3850. * `xkb_state_new()`, the call is *rejected* without updating the state,
  3851. * and the misuse is logged as `::XKB_ERROR_UNEXPECTED_STATE_MODE`.
  3852. * The return value is `0` in this case, which is indistinguishable from
  3853. * a no-op update.
  3854. *
  3855. * @returns A mask of state components that have changed as a result of
  3856. * the update. If nothing in the state has changed, returns 0.
  3857. *
  3858. * @memberof xkb_state
  3859. *
  3860. * @sa `xkb_state_update_mask()`
  3861. */
  3862. XKB_EXPORT enum xkb_state_component
  3863. xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
  3864. enum xkb_key_direction direction);
  3865. /**
  3866. * Apply a *synthetic* (out-of-band) atomic update to the keyboard state.
  3867. *
  3868. * This entry point is intended for *server* applications and should not be used
  3869. * by *client* applications; see @ref server-client-state for details.
  3870. *
  3871. * - Use this function to update the keyboard state in response to
  3872. * out-of-band (non-device) inputs, such as UI layout switchers or
  3873. * accessibility settings changes.
  3874. * - Use `xkb_state_update_key()` instead for in-band (device) inputs.
  3875. * - Use `xkb_state_update_event()` instead when updating from an
  3876. * [event](@ref xkb_event) produced by `xkb_machine`.
  3877. *
  3878. * Only latched, locked and control components can be updated out-of-band;
  3879. * depressed components can only change through key presses via
  3880. * `xkb_state_update_key()`.
  3881. *
  3882. * @par Layout out of range
  3883. * @parblock
  3884. * If the effective layout, after taking into account the depressed, latched
  3885. * and locked layout, is out of range (negative or greater than the maximum
  3886. * layout index), it is brought into range according to the current
  3887. * out-of-range layout policy (see `xkb_layout_out_of_range_policy`).
  3888. * @endparblock
  3889. *
  3890. * @note This entry point serves the legacy server use case and only supports a
  3891. * restricted set of libxkbcommon features. Since 1.14.0, prefer `xkb_machine`
  3892. * for new server applications to enable the full feature set.
  3893. *
  3894. * @param[in,out] state The keyboard state object.
  3895. * @param[in] update The update to apply.
  3896. * Must have `xkb_state_update::size` set.
  3897. * @param[out] changed A pointer to store the mask of state components that
  3898. * have changed as a result of the update, or `NULL` to
  3899. * ignore. Set to 0 if nothing in the state has changed.
  3900. *
  3901. * @returns
  3902. * - `::XKB_SUCCESS` on success;
  3903. * - `::XKB_ERROR_UNEXPECTED_STATE_MODE` without updating the state if @p state
  3904. * was not created with `::XKB_STATE_MODE_SERVER` or `xkb_state_new()`.
  3905. * - Otherwise another [error code](@ref xkb_error_code).
  3906. *
  3907. * @note This function returns an error code rather than a state component
  3908. * delta (unlike the other `xkb_state_update_*` functions), in order to align
  3909. * with the `xkb_machine::xkb_machine_process_synthetic()` API. The delta
  3910. * is optionally available via the @p changed parameter.
  3911. *
  3912. * @since 1.14.0
  3913. *
  3914. * @sa `xkb_state_update`
  3915. * @sa `xkb_state_update_key()`
  3916. * @sa `xkb_machine::xkb_machine_process_synthetic()`
  3917. * @memberof xkb_state
  3918. */
  3919. XKB_EXPORT enum xkb_error_code
  3920. xkb_state_update_synthetic(struct xkb_state *state,
  3921. const struct xkb_state_update *update,
  3922. enum xkb_state_component *changed);
  3923. /**
  3924. * Update the keyboard state to change the latched and locked state of
  3925. * the modifiers and layout.
  3926. *
  3927. * @deprecated Use `xkb_state_update_synthetic()` instead.
  3928. *
  3929. * This entry point is intended for *server* applications and should not be used
  3930. * by *client* applications; see @ref server-client-state for details.
  3931. *
  3932. * Use this function to update the latched and locked state according to
  3933. * out-of-band (non-device) inputs, such as UI layout switchers.
  3934. *
  3935. * @par Layout out of range
  3936. * @parblock
  3937. * If the effective layout, after taking into account the depressed, latched and
  3938. * locked layout, is out of range (negative or greater than the maximum layout),
  3939. * it is brought into range. Currently, the layout is wrapped using integer
  3940. * modulus (with negative values wrapping from the end). The wrapping behavior
  3941. * can be configured using `xkb_state_update_synthetic()`.
  3942. * @endparblock
  3943. *
  3944. * @param[in,out] state The keyboard state object.
  3945. * @param[in] affect_latched_mods See @p latched_mods.
  3946. * @param[in] latched_mods
  3947. * Modifiers to set as latched or unlatched. Only modifiers in
  3948. * @p affect_latched_mods are considered.
  3949. * @param[in] affect_latched_layout See @p latched_layout.
  3950. * @param[in] latched_layout
  3951. * Layout to latch. Only considered if @p affect_latched_layout is `true`.
  3952. * May be out of range (including negative) -- see note above.
  3953. * @param[in] affect_locked_mods See @p locked_mods.
  3954. * @param[in] locked_mods
  3955. * Modifiers to set as locked or unlocked. Only modifiers in
  3956. * @p affect_locked_mods are considered.
  3957. * @param[in] affect_locked_layout See @p locked_layout.
  3958. * @param[in] locked_layout
  3959. * Layout to lock. Only considered if @p affect_locked_layout is `true`.
  3960. * May be out of range (including negative) -- see note above.
  3961. *
  3962. * @important If @p state was not created with `::XKB_STATE_MODE_SERVER` or
  3963. * `xkb_state_new()`, the call is *rejected* without updating the state,
  3964. * and the misuse is logged as `::XKB_ERROR_UNEXPECTED_STATE_MODE`.
  3965. * The return value is `0` in this case, which is indistinguishable from
  3966. * a no-op update.
  3967. *
  3968. * @returns A mask of state components that have changed as a result of
  3969. * the update. If nothing in the state has changed, returns 0.
  3970. *
  3971. * @memberof xkb_state
  3972. *
  3973. * @sa `xkb_state_update_synthetic()`
  3974. */
  3975. XKB_EXPORT enum xkb_state_component
  3976. xkb_state_update_latched_locked(struct xkb_state *state,
  3977. xkb_mod_mask_t affect_latched_mods,
  3978. xkb_mod_mask_t latched_mods,
  3979. bool affect_latched_layout,
  3980. int32_t latched_layout,
  3981. xkb_mod_mask_t affect_locked_mods,
  3982. xkb_mod_mask_t locked_mods,
  3983. bool affect_locked_layout,
  3984. int32_t locked_layout);
  3985. /**
  3986. * Get the keysyms obtained from pressing a particular key in a given
  3987. * keyboard state.
  3988. *
  3989. * Get the keysyms for a key according to the current active layout,
  3990. * modifiers and shift level for the key, as determined by a keyboard
  3991. * state.
  3992. *
  3993. * @param[in] state The keyboard state object.
  3994. * @param[in] key The keycode of the key.
  3995. * @param[out] syms_out An immutable array of keysyms corresponding the
  3996. * key in the given keyboard state.
  3997. *
  3998. * As an extension to XKB, this function can return more than one keysym.
  3999. * If you do not want to handle this case, you can use
  4000. * `xkb_state_key_get_one_sym()` for a simpler interface.
  4001. *
  4002. * @returns The number of keysyms in the syms_out array. If no keysyms
  4003. * are produced by the key in the given keyboard state, returns 0 and sets
  4004. * syms_out to `NULL`.
  4005. *
  4006. * This function performs Capitalization @ref keysym-transformations.
  4007. *
  4008. * @memberof xkb_state
  4009. *
  4010. * @since 1.9.0 This function now performs @ref keysym-transformations.
  4011. */
  4012. XKB_EXPORT int
  4013. xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
  4014. const xkb_keysym_t **syms_out);
  4015. /**
  4016. * Get the Unicode/UTF-8 string obtained from pressing a particular key
  4017. * in a given keyboard state.
  4018. *
  4019. * @param[in] state The keyboard state object.
  4020. * @param[in] key The keycode of the key.
  4021. * @param[out] buffer A buffer to write the string into.
  4022. * @param[in] size Capacity of the buffer.
  4023. *
  4024. * @warning If the buffer passed is too small, the string is truncated
  4025. * (though still `NULL`-terminated).
  4026. *
  4027. * @returns The number of bytes required for the string, excluding the
  4028. * `NULL` byte. If there is nothing to write, returns 0.
  4029. *
  4030. * You may check if truncation has occurred by comparing the return value
  4031. * with the size of @p buffer, similarly to the `snprintf(3)` function.
  4032. * You may safely pass `NULL` and 0 to @p buffer and @p size to find the
  4033. * required size (without the `NULL`-byte).
  4034. *
  4035. * This function performs Capitalization and Control @ref
  4036. * keysym-transformations.
  4037. *
  4038. * @memberof xkb_state
  4039. * @since 0.4.1
  4040. */
  4041. XKB_EXPORT int
  4042. xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t key,
  4043. char *buffer, size_t size);
  4044. /**
  4045. * Get the Unicode/UTF-32 codepoint obtained from pressing a particular
  4046. * key in a a given keyboard state.
  4047. *
  4048. * @param[in] state The keyboard state object.
  4049. * @param[in] key The keycode of the key.
  4050. *
  4051. * @returns The UTF-32 representation for the key, if it consists of only
  4052. * a single codepoint. Otherwise, returns 0.
  4053. *
  4054. * This function performs Capitalization and Control @ref
  4055. * keysym-transformations.
  4056. *
  4057. * @memberof xkb_state
  4058. * @since 0.4.1
  4059. */
  4060. XKB_EXPORT uint32_t
  4061. xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t key);
  4062. /**
  4063. * Get the single keysym obtained from pressing a particular key in a
  4064. * given keyboard state.
  4065. *
  4066. * This function is similar to `xkb_state_key_get_syms()`, but intended
  4067. * for users which cannot or do not want to handle the case where
  4068. * multiple keysyms are returned (in which case this function is
  4069. * preferred).
  4070. *
  4071. * @param[in] state The keyboard state object.
  4072. * @param[in] key The keycode of the key.
  4073. *
  4074. * @returns The keysym. If the key does not have exactly one keysym,
  4075. * returns `XKB_KEY_NoSymbol`.
  4076. *
  4077. * This function performs Capitalization @ref keysym-transformations.
  4078. *
  4079. * @sa xkb_state_key_get_syms()
  4080. * @memberof xkb_state
  4081. */
  4082. XKB_EXPORT xkb_keysym_t
  4083. xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t key);
  4084. /**
  4085. * Get the effective layout index for a key in a given keyboard state.
  4086. *
  4087. * @param[in] state The keyboard state object.
  4088. * @param[in] key The keycode of the key.
  4089. *
  4090. * @returns The layout index for the key in the given keyboard state. If
  4091. * the given keycode is invalid, or if the key is not included in any
  4092. * layout at all, returns `::XKB_LAYOUT_INVALID`.
  4093. *
  4094. * @invariant If the returned layout is valid, the following always holds:
  4095. * @code
  4096. * xkb_state_key_get_layout(state, key) < xkb_keymap_num_layouts_for_key(keymap, key)
  4097. * @endcode
  4098. *
  4099. * @memberof xkb_state
  4100. */
  4101. XKB_EXPORT xkb_layout_index_t
  4102. xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t key);
  4103. /**
  4104. * Get the effective shift level for a key in a given keyboard state and
  4105. * layout.
  4106. *
  4107. * @param[in] state The keyboard state.
  4108. * @param[in] key The keycode of the key.
  4109. * @param[in] layout The layout for which to get the shift level. This must be
  4110. * smaller than:
  4111. * @code xkb_keymap_num_layouts_for_key(keymap, key) @endcode
  4112. * usually it would be:
  4113. * @code xkb_state_key_get_layout(state, key) @endcode
  4114. *
  4115. * @return The shift level index. If the key or layout are invalid,
  4116. * returns `::XKB_LEVEL_INVALID`.
  4117. *
  4118. * @invariant If the returned level is valid, the following always holds:
  4119. * @code
  4120. * xkb_state_key_get_level(state, key, layout) < xkb_keymap_num_levels_for_key(keymap, key, layout)
  4121. * @endcode
  4122. *
  4123. * @memberof xkb_state
  4124. */
  4125. XKB_EXPORT xkb_level_index_t
  4126. xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t key,
  4127. xkb_layout_index_t layout);
  4128. /**
  4129. * @enum xkb_state_match
  4130. * Match flags for `xkb_state::xkb_state_mod_indices_are_active()` and
  4131. * `xkb_state::xkb_state_mod_names_are_active()`, specifying the conditions for a
  4132. * successful match. `::XKB_STATE_MATCH_NON_EXCLUSIVE` is bitmaskable with
  4133. * the other modes.
  4134. */
  4135. enum xkb_state_match {
  4136. /** Returns `true` if any of the modifiers are active. */
  4137. XKB_STATE_MATCH_ANY = (1 << 0),
  4138. /** Returns `true` if all of the modifiers are active. */
  4139. XKB_STATE_MATCH_ALL = (1 << 1),
  4140. /**
  4141. * @parblock
  4142. * Makes matching non-exclusive, i.e. will not return `false` if a
  4143. * modifier not specified in the arguments is active.
  4144. * @endparblock
  4145. */
  4146. XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16)
  4147. };
  4148. /**
  4149. * Serialization of the *boolean* [global keyboard controls], to be used on the
  4150. * server side of serialization.
  4151. *
  4152. * This entry point is intended for *server* applications; see @ref
  4153. * server-client-state for details.
  4154. *
  4155. * @param[in] state The keyboard state.
  4156. * @param[in] components A mask of the keyboard control state components to
  4157. * serialize. State components other than `::XKB_STATE_CONTROLS` are ignored.
  4158. *
  4159. * @returns A `xkb_keyboard_control_flags` mask representing the enabled
  4160. * keyboard controls for the given @p components.
  4161. *
  4162. * @since 1.14.0
  4163. *
  4164. * @memberof xkb_state
  4165. *
  4166. * [global keyboard controls]: @ref xkb_keyboard_control_flags
  4167. */
  4168. XKB_EXPORT enum xkb_keyboard_control_flags
  4169. xkb_state_serialize_enabled_controls(const struct xkb_state *state,
  4170. enum xkb_state_component components);
  4171. /**
  4172. * The counterpart to `xkb_state::xkb_state_update_mask()` for modifiers, to be
  4173. * used on the server side of serialization.
  4174. *
  4175. * This entry point is intended for *server* applications; see @ref
  4176. * server-client-state for details. *Client* applications should use the
  4177. * `xkb_state_mod_*_is_active` API.
  4178. *
  4179. * @warning The serialization is lossy and will not survive round trips.
  4180. * It must only be used to feed *client* state objects created with either
  4181. * `::XKB_STATE_MODE_CLIENT` or `xkb_state_new()`, and must not be used to
  4182. * update the *server* state.
  4183. *
  4184. * @param[in] state The keyboard state.
  4185. * @param[in] components A mask of the modifier state components to serialize.
  4186. * State components other than `XKB_STATE_MODS_*` are ignored.
  4187. * If `::XKB_STATE_MODS_EFFECTIVE` is included, all other state components are
  4188. * ignored.
  4189. *
  4190. * @returns A `xkb_mod_mask_t` representing the given components of the
  4191. * modifier state.
  4192. *
  4193. * @memberof xkb_state
  4194. */
  4195. XKB_EXPORT xkb_mod_mask_t
  4196. xkb_state_serialize_mods(struct xkb_state *state,
  4197. enum xkb_state_component components);
  4198. /**
  4199. * The counterpart to `xkb_state::xkb_state_update_mask()` for layouts, to be
  4200. * used on the server side of serialization.
  4201. *
  4202. * This entry point is intended for *server* applications; see @ref
  4203. * server-client-state for details. *Client* applications should use the
  4204. * xkb_state_layout_*_is_active API.
  4205. *
  4206. * @warning The serialization is lossy and will not survive round trips.
  4207. * It must only be used to feed *client* state objects created with either
  4208. * `::XKB_STATE_MODE_CLIENT` or `xkb_state_new()`, and must not be used to
  4209. * update the *server* state.
  4210. *
  4211. * @param[in] state The keyboard state.
  4212. * @param[in] components A mask of the layout state components to serialize.
  4213. * State components other than `XKB_STATE_LAYOUT_*` are ignored.
  4214. * If `::XKB_STATE_LAYOUT_EFFECTIVE` is included, all other state components are
  4215. * ignored.
  4216. *
  4217. * @returns A layout index representing the given components of the
  4218. * layout state.
  4219. *
  4220. * @memberof xkb_state
  4221. */
  4222. XKB_EXPORT xkb_layout_index_t
  4223. xkb_state_serialize_layout(struct xkb_state *state,
  4224. enum xkb_state_component components);
  4225. /**
  4226. * Test whether a modifier is active in a given keyboard state by name.
  4227. *
  4228. * @warning For [virtual modifiers], this function may *overmatch* in case
  4229. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4230. *
  4231. * @param[in] state The keyboard state object.
  4232. * @param[in] name The modifier name, as a `NULL`-terminated string.
  4233. * @param[in] type The component of the state against which to match the
  4234. * given modifiers.
  4235. *
  4236. * @returns 1 if the modifier is active, 0 if it is not. If the modifier
  4237. * name does not exist in the keymap, returns -1.
  4238. *
  4239. * @memberof xkb_state
  4240. *
  4241. * @since 0.1.0: Works only with *real* modifiers
  4242. * @since 1.8.0: Works also with *virtual* modifiers
  4243. *
  4244. * [virtual modifiers]: @ref virtual-modifier-def
  4245. * [real modifiers]: @ref real-modifier-def
  4246. */
  4247. XKB_EXPORT int
  4248. xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
  4249. enum xkb_state_component type);
  4250. /**
  4251. * Test whether a set of modifiers are active in a given keyboard state by
  4252. * name.
  4253. *
  4254. * @warning For [virtual modifiers], this function may *overmatch* in case
  4255. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4256. *
  4257. * @param[in] state The keyboard state.
  4258. * @param[in] type The component of the state against which to match the
  4259. * given modifiers.
  4260. * @param[in] match The manner by which to match the state against the
  4261. * given modifiers.
  4262. * @param[in] ... The set of of modifier names to test, terminated by a `NULL`
  4263. * argument (sentinel).
  4264. *
  4265. * @returns 1 if the modifiers are active, 0 if they are not. If any of
  4266. * the modifier names do not exist in the keymap, returns -1. If @p match
  4267. * contains invalid flags, returns -2.
  4268. *
  4269. * @memberof xkb_state
  4270. *
  4271. * @since 0.1.0: Works only with *real* modifiers
  4272. * @since 1.8.0: Works also with *virtual* modifiers
  4273. * @since 1.14.0: Reject invalid @p match flags
  4274. *
  4275. * [virtual modifiers]: @ref virtual-modifier-def
  4276. * [real modifiers]: @ref real-modifier-def
  4277. */
  4278. XKB_EXPORT int
  4279. xkb_state_mod_names_are_active(struct xkb_state *state,
  4280. enum xkb_state_component type,
  4281. enum xkb_state_match match,
  4282. ...);
  4283. /**
  4284. * Test whether a modifier is active in a given keyboard state by index.
  4285. *
  4286. * @warning For [virtual modifiers], this function may *overmatch* in case
  4287. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4288. *
  4289. * @param[in] state The keyboard state.
  4290. * @param[in] idx The index of the modifier to test.
  4291. * @param[in] type The component of the state against which to match the
  4292. * given modifiers.
  4293. *
  4294. * @returns 1 if the modifier is active, 0 if it is not. If the modifier
  4295. * index is invalid in the keymap, returns -1.
  4296. *
  4297. * @memberof xkb_state
  4298. *
  4299. * @since 0.1.0: Works only with *real* modifiers
  4300. * @since 1.8.0: Works also with *virtual* modifiers
  4301. *
  4302. * [virtual modifiers]: @ref virtual-modifier-def
  4303. * [real modifiers]: @ref real-modifier-def
  4304. */
  4305. XKB_EXPORT int
  4306. xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
  4307. enum xkb_state_component type);
  4308. /**
  4309. * Test whether a set of modifiers are active in a given keyboard state by
  4310. * index.
  4311. *
  4312. * @warning For [virtual modifiers], this function may *overmatch* in case
  4313. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4314. *
  4315. * @param[in] state The keyboard state.
  4316. * @param[in] type The component of the state against which to match the
  4317. * given modifiers.
  4318. * @param[in] match The manner by which to match the state against the
  4319. * given modifiers.
  4320. * @param[in] ... The set of of modifier indices to test, terminated by a
  4321. * `::XKB_MOD_INVALID` argument (sentinel).
  4322. *
  4323. * @returns 1 if the modifiers are active, 0 if they are not. If any of
  4324. * the modifier indices are invalid in the keymap, returns -1. If @p match
  4325. * contains invalid flags, returns -2.
  4326. *
  4327. * @memberof xkb_state
  4328. *
  4329. * @since 0.1.0: Works only with *real* modifiers
  4330. * @since 1.8.0: Works also with *virtual* modifiers
  4331. * @since 1.14.0: Reject invalid @p match flags
  4332. *
  4333. * [virtual modifiers]: @ref virtual-modifier-def
  4334. * [real modifiers]: @ref real-modifier-def
  4335. */
  4336. XKB_EXPORT int
  4337. xkb_state_mod_indices_are_active(struct xkb_state *state,
  4338. enum xkb_state_component type,
  4339. enum xkb_state_match match,
  4340. ...);
  4341. /**
  4342. * @page consumed-modifiers Consumed Modifiers
  4343. * @parblock
  4344. *
  4345. * Some functions, like `xkb_state::xkb_state_key_get_syms()`, look at the state
  4346. * of the modifiers in the keymap and derive from it the correct shift level
  4347. * to use for the key. For example, in a US layout, pressing the key
  4348. * labeled `<A>` while the Shift modifier is active, generates the keysym
  4349. * `A`. In this case, the Shift modifier is said to be *consumed*.
  4350. * However, the Num Lock modifier does not affect this translation at all,
  4351. * even if it is active, so it is not consumed by this translation.
  4352. *
  4353. * It may be desirable for some application to not reuse consumed modifiers
  4354. * for further processing, e.g. for hotkeys or keyboard shortcuts. To
  4355. * understand why, consider some requirements from a standard shortcut
  4356. * mechanism, and how they are implemented:
  4357. *
  4358. * 1. The shortcut’s modifiers must match exactly to the state. For
  4359. * example, it is possible to bind separate actions to `<Alt><Tab>`
  4360. * and to `<Alt><Shift><Tab>`. Further, if only `<Alt><Tab>` is
  4361. * bound to an action, pressing `<Alt><Shift><Tab>` should not
  4362. * trigger the shortcut.
  4363. * Effectively, this means that the modifiers are compared using the
  4364. * equality operator (`==`).
  4365. *
  4366. * 2. Only relevant modifiers are considered for the matching. For example,
  4367. * Caps Lock and Num Lock should not generally affect the matching, e.g.
  4368. * when matching `<Alt><Tab>` against the state, it does not matter
  4369. * whether Num Lock is active or not. These relevant, or *significant*,
  4370. * modifiers usually include Alt, Control, Shift, Super and similar.
  4371. * Effectively, this means that non-significant modifiers are masked out,
  4372. * before doing the comparison as described above.
  4373. *
  4374. * 3. The matching must be independent of the layout/keymap. For example,
  4375. * the `<Plus>` (+) symbol is found on the first level on some layouts,
  4376. * but requires holding Shift on others. If you simply bind the action
  4377. * to the `<Plus>` keysym, it would work for the unshifted kind, but
  4378. * not for the others, because the match against Shift would fail. If
  4379. * you bind the action to `<Shift><Plus>`, only the shifted kind would
  4380. * work. So what is needed is to recognize that Shift is used up in the
  4381. * translation of the keysym itself, and therefore should not be included
  4382. * in the matching.
  4383. * Effectively, this means that consumed modifiers (Shift in this example)
  4384. * are masked out as well, before doing the comparison.
  4385. *
  4386. * In summary, this is approximately how the matching would be performed:
  4387. *
  4388. * ```c
  4389. * (keysym == shortcut_keysym) &&
  4390. * ((state_mods & ~consumed_mods & significant_mods) == shortcut_mods)
  4391. * ```
  4392. *
  4393. * @c state_mods are the modifiers reported by
  4394. * `xkb_state::xkb_state_mod_index_is_active()` and similar functions.
  4395. * @c consumed_mods are the modifiers reported by
  4396. * `xkb_state::xkb_state_mod_index_is_consumed()` and similar functions.
  4397. * @c significant_mods are decided upon by the application/toolkit/user;
  4398. * it is up to them to decide whether these are configurable or hard-coded.
  4399. *
  4400. * @endparblock
  4401. */
  4402. /**
  4403. * @enum xkb_consumed_mode
  4404. * Consumed modifiers mode.
  4405. *
  4406. * There are several possible methods for deciding which modifiers are
  4407. * consumed and which are not, each applicable for different systems or
  4408. * situations. The mode selects the method to use.
  4409. *
  4410. * Keep in mind that in all methods, the keymap may decide to *preserve*
  4411. * a modifier, meaning it is not reported as consumed even if it would
  4412. * have otherwise.
  4413. */
  4414. enum xkb_consumed_mode {
  4415. /**
  4416. * This is the mode defined in the XKB specification and used by libX11.
  4417. *
  4418. * A modifier is consumed if and only if it *may affect* key translation.
  4419. *
  4420. * For example, if `Control+Alt+<Backspace>` produces some assigned keysym,
  4421. * then when pressing just `<Backspace>`, `Control` and `Alt` are consumed,
  4422. * even though they are not active, since if they *were* active they would
  4423. * have affected key translation.
  4424. */
  4425. XKB_CONSUMED_MODE_XKB,
  4426. /**
  4427. * This is the mode used by the GTK+ toolkit.
  4428. *
  4429. * The mode consists of the following two independent heuristics:
  4430. *
  4431. * - The currently active set of modifiers, excluding modifiers which do
  4432. * not affect the key (as described for @ref XKB_CONSUMED_MODE_XKB), are
  4433. * considered consumed, if the keysyms produced when all of them are
  4434. * active are different from the keysyms produced when no modifiers are
  4435. * active.
  4436. *
  4437. * - A single modifier is considered consumed if the keysyms produced for
  4438. * the key when it is the only active modifier are different from the
  4439. * keysyms produced when no modifiers are active.
  4440. */
  4441. XKB_CONSUMED_MODE_GTK
  4442. };
  4443. /**
  4444. * Get the mask of modifiers consumed by translating a given key.
  4445. *
  4446. * @param[in] state The keyboard state.
  4447. * @param[in] key The keycode of the key.
  4448. * @param[in] mode The consumed modifiers mode to use;
  4449. * see [enum description](@ref xkb_consumed_mode).
  4450. *
  4451. * @returns a mask of the consumed [real modifiers] modifiers.
  4452. *
  4453. * @memberof xkb_state
  4454. * @since 0.7.0
  4455. *
  4456. * [real modifiers]: @ref real-modifier-def
  4457. */
  4458. XKB_EXPORT xkb_mod_mask_t
  4459. xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t key,
  4460. enum xkb_consumed_mode mode);
  4461. /**
  4462. * Same as `xkb_state_key_get_consumed_mods2()` with mode `::XKB_CONSUMED_MODE_XKB`.
  4463. *
  4464. * @memberof xkb_state
  4465. * @since 0.4.1
  4466. */
  4467. XKB_EXPORT xkb_mod_mask_t
  4468. xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t key);
  4469. /**
  4470. * Test whether a modifier is consumed by keyboard state translation for
  4471. * a key.
  4472. *
  4473. * @warning For [virtual modifiers], this function may *overmatch* in case
  4474. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4475. *
  4476. * @param[in] state The keyboard state.
  4477. * @param[in] key The keycode of the key.
  4478. * @param[in] idx The index of the modifier to check.
  4479. * @param[in] mode The consumed modifiers mode to use; see enum description.
  4480. *
  4481. * @returns 1 if the modifier is consumed, 0 if it is not. If the modifier
  4482. * index is not valid in the keymap, returns -1.
  4483. *
  4484. * @sa xkb_state_mod_mask_remove_consumed()
  4485. * @sa xkb_state_key_get_consumed_mods()
  4486. * @memberof xkb_state
  4487. * @since 0.7.0: Works only with *real* modifiers
  4488. * @since 1.8.0: Works also with *virtual* modifiers
  4489. *
  4490. * [virtual modifiers]: @ref virtual-modifier-def
  4491. * [real modifiers]: @ref real-modifier-def
  4492. */
  4493. XKB_EXPORT int
  4494. xkb_state_mod_index_is_consumed2(struct xkb_state *state,
  4495. xkb_keycode_t key,
  4496. xkb_mod_index_t idx,
  4497. enum xkb_consumed_mode mode);
  4498. /**
  4499. * Same as `xkb_state_mod_index_is_consumed2()` with mode `::XKB_CONSUMED_MODE_XKB`.
  4500. *
  4501. * @warning For [virtual modifiers], this function may *overmatch* in case
  4502. * there are virtual modifiers with overlapping mappings to [real modifiers].
  4503. *
  4504. * @memberof xkb_state
  4505. * @since 0.4.1: Works only with *real* modifiers
  4506. * @since 1.8.0: Works also with *virtual* modifiers
  4507. *
  4508. * [virtual modifiers]: @ref virtual-modifier-def
  4509. * [real modifiers]: @ref real-modifier-def
  4510. */
  4511. XKB_EXPORT int
  4512. xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t key,
  4513. xkb_mod_index_t idx);
  4514. /**
  4515. * Remove consumed modifiers from a modifier mask for a key.
  4516. *
  4517. * @deprecated Use `xkb_state_key_get_consumed_mods2()` instead.
  4518. *
  4519. * Takes the given modifier mask, and removes all modifiers which are
  4520. * consumed for that particular key (as in `xkb_state_mod_index_is_consumed()`).
  4521. *
  4522. * @returns a mask of [real modifiers] modifiers.
  4523. *
  4524. * @sa xkb_state_mod_index_is_consumed()
  4525. * @memberof xkb_state
  4526. * @since 0.5.0: Works only with *real* modifiers
  4527. * @since 1.8.0: Works also with *virtual* modifiers
  4528. *
  4529. * [real modifiers]: @ref real-modifier-def
  4530. */
  4531. XKB_EXPORT xkb_mod_mask_t
  4532. xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t key,
  4533. xkb_mod_mask_t mask);
  4534. /**
  4535. * Test whether a layout is active in a given keyboard state by name.
  4536. *
  4537. * @param[in] state The keyboard state.
  4538. * @param[in] name The layout name to test (`NULL`-terminated string).
  4539. * @param[in] type The component of the state against which to match the
  4540. * given layout.
  4541. *
  4542. * @returns 1 if the layout is active, 0 if it is not. If no layout with
  4543. * this name exists in the keymap, return -1.
  4544. *
  4545. * If multiple layouts in the keymap have this name, the one with the lowest
  4546. * index is tested.
  4547. *
  4548. * @sa xkb_layout_index_t
  4549. * @memberof xkb_state
  4550. */
  4551. XKB_EXPORT int
  4552. xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
  4553. enum xkb_state_component type);
  4554. /**
  4555. * Test whether a layout is active in a given keyboard state by index.
  4556. *
  4557. * @param[in] state The keyboard state.
  4558. * @param[in] idx The layout index to test.
  4559. * @param[in] type The component of the state against which to match the
  4560. * given layout.
  4561. *
  4562. * @returns 1 if the layout is active, 0 if it is not. If the layout index
  4563. * is not valid in the keymap, returns -1.
  4564. *
  4565. * @sa xkb_layout_index_t
  4566. * @memberof xkb_state
  4567. */
  4568. XKB_EXPORT int
  4569. xkb_state_layout_index_is_active(struct xkb_state *state,
  4570. xkb_layout_index_t idx,
  4571. enum xkb_state_component type);
  4572. /**
  4573. * Test whether a LED is active in a given keyboard state by name.
  4574. *
  4575. * @param[in] state The keyboard state.
  4576. * @param[in] name The LED name to test (`NULL`-terminated string).
  4577. *
  4578. * @returns 1 if the LED is active, 0 if it not. If no LED with this name
  4579. * exists in the keymap, returns -1.
  4580. *
  4581. * @sa xkb_led_index_t
  4582. * @memberof xkb_state
  4583. */
  4584. XKB_EXPORT int
  4585. xkb_state_led_name_is_active(struct xkb_state *state, const char *name);
  4586. /**
  4587. * Test whether a LED is active in a given keyboard state by index.
  4588. *
  4589. * @param[in] state The keyboard state.
  4590. * @param[in] idx The LED index to test.
  4591. *
  4592. * @returns 1 if the LED is active, 0 if it not. If the LED index is not
  4593. * valid in the keymap, returns -1.
  4594. *
  4595. * @sa xkb_led_index_t
  4596. * @memberof xkb_state
  4597. */
  4598. XKB_EXPORT int
  4599. xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx);
  4600. /** @} */
  4601. /* Leave this include last, so it can pick up our types, etc. */
  4602. #include <xkbcommon/xkbcommon-compat.h>
  4603. #ifdef __cplusplus
  4604. } /* extern "C" */
  4605. #endif
  4606. #endif /* _XKBCOMMON_H_ */