exevents.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071
  1. /************************************************************
  2. Copyright 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Hewlett-Packard not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ********************************************************/
  36. /*
  37. * Copyright © 2010 Collabora Ltd.
  38. * Copyright © 2011 Red Hat, Inc.
  39. *
  40. * Permission is hereby granted, free of charge, to any person obtaining a
  41. * copy of this software and associated documentation files (the "Software"),
  42. * to deal in the Software without restriction, including without limitation
  43. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  44. * and/or sell copies of the Software, and to permit persons to whom the
  45. * Software is furnished to do so, subject to the following conditions:
  46. *
  47. * The above copyright notice and this permission notice (including the next
  48. * paragraph) shall be included in all copies or substantial portions of the
  49. * Software.
  50. *
  51. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  52. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  53. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  54. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  55. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  56. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  57. * DEALINGS IN THE SOFTWARE.
  58. *
  59. * Author: Daniel Stone <daniel@fooishbar.org>
  60. */
  61. /********************************************************************
  62. *
  63. * Routines to register and initialize extension input devices.
  64. * This also contains ProcessOtherEvent, the routine called from DDX
  65. * to route extension events.
  66. *
  67. */
  68. #ifdef HAVE_DIX_CONFIG_H
  69. #include <dix-config.h>
  70. #endif
  71. #include "inputstr.h"
  72. #include <X11/X.h>
  73. #include <X11/Xproto.h>
  74. #include <X11/extensions/XI.h>
  75. #include <X11/extensions/XIproto.h>
  76. #include <X11/extensions/XI2proto.h>
  77. #include <X11/extensions/geproto.h>
  78. #include "windowstr.h"
  79. #include "miscstruct.h"
  80. #include "region.h"
  81. #include "exevents.h"
  82. #include "extnsionst.h"
  83. #include "exglobals.h"
  84. #include "dixevents.h" /* DeliverFocusedEvent */
  85. #include "dixgrabs.h" /* CreateGrab() */
  86. #include "scrnintstr.h"
  87. #include "listdev.h" /* for CopySwapXXXClass */
  88. #include "xace.h"
  89. #include "xiquerydevice.h" /* For List*Info */
  90. #include "eventconvert.h"
  91. #include "eventstr.h"
  92. #include "inpututils.h"
  93. #include "mi.h"
  94. #include <X11/extensions/XKBproto.h>
  95. #include "xkbsrv.h"
  96. #define WID(w) ((w) ? ((w)->drawable.id) : 0)
  97. #define AllModifiersMask ( \
  98. ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask | \
  99. Mod3Mask | Mod4Mask | Mod5Mask )
  100. #define AllButtonsMask ( \
  101. Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
  102. Bool ShouldFreeInputMasks(WindowPtr /* pWin */ ,
  103. Bool /* ignoreSelectedEvents */
  104. );
  105. static Bool MakeInputMasks(WindowPtr /* pWin */
  106. );
  107. /*
  108. * Only let the given client know of core events which will affect its
  109. * interpretation of input events, if the client's ClientPointer (or the
  110. * paired keyboard) is the current device.
  111. */
  112. int
  113. XIShouldNotify(ClientPtr client, DeviceIntPtr dev)
  114. {
  115. DeviceIntPtr current_ptr = PickPointer(client);
  116. DeviceIntPtr current_kbd = GetMaster(current_ptr, KEYBOARD_OR_FLOAT);
  117. if (dev == current_kbd || dev == current_ptr)
  118. return 1;
  119. return 0;
  120. }
  121. Bool
  122. IsPointerEvent(InternalEvent *event)
  123. {
  124. switch (event->any.type) {
  125. case ET_ButtonPress:
  126. case ET_ButtonRelease:
  127. case ET_Motion:
  128. /* XXX: enter/leave ?? */
  129. return TRUE;
  130. default:
  131. break;
  132. }
  133. return FALSE;
  134. }
  135. Bool
  136. IsTouchEvent(InternalEvent *event)
  137. {
  138. switch (event->any.type) {
  139. case ET_TouchBegin:
  140. case ET_TouchUpdate:
  141. case ET_TouchEnd:
  142. return TRUE;
  143. default:
  144. break;
  145. }
  146. return FALSE;
  147. }
  148. /**
  149. * @return the device matching the deviceid of the device set in the event, or
  150. * NULL if the event is not an XInput event.
  151. */
  152. DeviceIntPtr
  153. XIGetDevice(xEvent *xE)
  154. {
  155. DeviceIntPtr pDev = NULL;
  156. if (xE->u.u.type == DeviceButtonPress ||
  157. xE->u.u.type == DeviceButtonRelease ||
  158. xE->u.u.type == DeviceMotionNotify ||
  159. xE->u.u.type == ProximityIn ||
  160. xE->u.u.type == ProximityOut || xE->u.u.type == DevicePropertyNotify) {
  161. int rc;
  162. int id;
  163. id = ((deviceKeyButtonPointer *) xE)->deviceid & ~MORE_EVENTS;
  164. rc = dixLookupDevice(&pDev, id, serverClient, DixUnknownAccess);
  165. if (rc != Success)
  166. ErrorF("[dix] XIGetDevice failed on XACE restrictions (%d)\n", rc);
  167. }
  168. return pDev;
  169. }
  170. /**
  171. * Copy the device->key into master->key and send a mapping notify to the
  172. * clients if appropriate.
  173. * master->key needs to be allocated by the caller.
  174. *
  175. * Device is the slave device. If it is attached to a master device, we may
  176. * need to send a mapping notify to the client because it causes the MD
  177. * to change state.
  178. *
  179. * Mapping notify needs to be sent in the following cases:
  180. * - different slave device on same master
  181. * - different master
  182. *
  183. * XXX: They way how the code is we also send a map notify if the slave device
  184. * stays the same, but the master changes. This isn't really necessary though.
  185. *
  186. * XXX: this gives you funny behaviour with the ClientPointer. When a
  187. * MappingNotify is sent to the client, the client usually responds with a
  188. * GetKeyboardMapping. This will retrieve the ClientPointer's keyboard
  189. * mapping, regardless of which keyboard sent the last mapping notify request.
  190. * So depending on the CP setting, your keyboard may change layout in each
  191. * app...
  192. *
  193. * This code is basically the old SwitchCoreKeyboard.
  194. */
  195. void
  196. CopyKeyClass(DeviceIntPtr device, DeviceIntPtr master)
  197. {
  198. KeyClassPtr mk = master->key;
  199. if (device == master)
  200. return;
  201. mk->sourceid = device->id;
  202. if (!XkbDeviceApplyKeymap(master, device->key->xkbInfo->desc))
  203. FatalError("Couldn't pivot keymap from device to core!\n");
  204. }
  205. /**
  206. * Copies the feedback classes from device "from" into device "to". Classes
  207. * are duplicated (not just flipping the pointers). All feedback classes are
  208. * linked lists, the full list is duplicated.
  209. */
  210. static void
  211. DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
  212. {
  213. ClassesPtr classes;
  214. if (from->intfeed) {
  215. IntegerFeedbackPtr *i, it;
  216. if (!to->intfeed) {
  217. classes = to->unused_classes;
  218. to->intfeed = classes->intfeed;
  219. classes->intfeed = NULL;
  220. }
  221. i = &to->intfeed;
  222. for (it = from->intfeed; it; it = it->next) {
  223. if (!(*i)) {
  224. *i = calloc(1, sizeof(IntegerFeedbackClassRec));
  225. if (!(*i)) {
  226. ErrorF("[Xi] Cannot alloc memory for class copy.");
  227. return;
  228. }
  229. }
  230. (*i)->CtrlProc = it->CtrlProc;
  231. (*i)->ctrl = it->ctrl;
  232. i = &(*i)->next;
  233. }
  234. }
  235. else if (to->intfeed && !from->intfeed) {
  236. classes = to->unused_classes;
  237. classes->intfeed = to->intfeed;
  238. to->intfeed = NULL;
  239. }
  240. if (from->stringfeed) {
  241. StringFeedbackPtr *s, it;
  242. if (!to->stringfeed) {
  243. classes = to->unused_classes;
  244. to->stringfeed = classes->stringfeed;
  245. classes->stringfeed = NULL;
  246. }
  247. s = &to->stringfeed;
  248. for (it = from->stringfeed; it; it = it->next) {
  249. if (!(*s)) {
  250. *s = calloc(1, sizeof(StringFeedbackClassRec));
  251. if (!(*s)) {
  252. ErrorF("[Xi] Cannot alloc memory for class copy.");
  253. return;
  254. }
  255. }
  256. (*s)->CtrlProc = it->CtrlProc;
  257. (*s)->ctrl = it->ctrl;
  258. s = &(*s)->next;
  259. }
  260. }
  261. else if (to->stringfeed && !from->stringfeed) {
  262. classes = to->unused_classes;
  263. classes->stringfeed = to->stringfeed;
  264. to->stringfeed = NULL;
  265. }
  266. if (from->bell) {
  267. BellFeedbackPtr *b, it;
  268. if (!to->bell) {
  269. classes = to->unused_classes;
  270. to->bell = classes->bell;
  271. classes->bell = NULL;
  272. }
  273. b = &to->bell;
  274. for (it = from->bell; it; it = it->next) {
  275. if (!(*b)) {
  276. *b = calloc(1, sizeof(BellFeedbackClassRec));
  277. if (!(*b)) {
  278. ErrorF("[Xi] Cannot alloc memory for class copy.");
  279. return;
  280. }
  281. }
  282. (*b)->BellProc = it->BellProc;
  283. (*b)->CtrlProc = it->CtrlProc;
  284. (*b)->ctrl = it->ctrl;
  285. b = &(*b)->next;
  286. }
  287. }
  288. else if (to->bell && !from->bell) {
  289. classes = to->unused_classes;
  290. classes->bell = to->bell;
  291. to->bell = NULL;
  292. }
  293. if (from->leds) {
  294. LedFeedbackPtr *l, it;
  295. if (!to->leds) {
  296. classes = to->unused_classes;
  297. to->leds = classes->leds;
  298. classes->leds = NULL;
  299. }
  300. l = &to->leds;
  301. for (it = from->leds; it; it = it->next) {
  302. if (!(*l)) {
  303. *l = calloc(1, sizeof(LedFeedbackClassRec));
  304. if (!(*l)) {
  305. ErrorF("[Xi] Cannot alloc memory for class copy.");
  306. return;
  307. }
  308. }
  309. (*l)->CtrlProc = it->CtrlProc;
  310. (*l)->ctrl = it->ctrl;
  311. if ((*l)->xkb_sli)
  312. XkbFreeSrvLedInfo((*l)->xkb_sli);
  313. (*l)->xkb_sli = XkbCopySrvLedInfo(from, it->xkb_sli, NULL, *l);
  314. l = &(*l)->next;
  315. }
  316. }
  317. else if (to->leds && !from->leds) {
  318. classes = to->unused_classes;
  319. classes->leds = to->leds;
  320. to->leds = NULL;
  321. }
  322. }
  323. static void
  324. DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
  325. {
  326. ClassesPtr classes;
  327. /* XkbInitDevice (->XkbInitIndicatorMap->XkbFindSrvLedInfo) relies on the
  328. * kbdfeed to be set up properly, so let's do the feedback classes first.
  329. */
  330. if (from->kbdfeed) {
  331. KbdFeedbackPtr *k, it;
  332. if (!to->kbdfeed) {
  333. classes = to->unused_classes;
  334. to->kbdfeed = classes->kbdfeed;
  335. if (!to->kbdfeed)
  336. InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
  337. classes->kbdfeed = NULL;
  338. }
  339. k = &to->kbdfeed;
  340. for (it = from->kbdfeed; it; it = it->next) {
  341. if (!(*k)) {
  342. *k = calloc(1, sizeof(KbdFeedbackClassRec));
  343. if (!*k) {
  344. ErrorF("[Xi] Cannot alloc memory for class copy.");
  345. return;
  346. }
  347. }
  348. (*k)->BellProc = it->BellProc;
  349. (*k)->CtrlProc = it->CtrlProc;
  350. (*k)->ctrl = it->ctrl;
  351. if ((*k)->xkb_sli)
  352. XkbFreeSrvLedInfo((*k)->xkb_sli);
  353. (*k)->xkb_sli = XkbCopySrvLedInfo(from, it->xkb_sli, *k, NULL);
  354. k = &(*k)->next;
  355. }
  356. }
  357. else if (to->kbdfeed && !from->kbdfeed) {
  358. classes = to->unused_classes;
  359. classes->kbdfeed = to->kbdfeed;
  360. to->kbdfeed = NULL;
  361. }
  362. if (from->key) {
  363. if (!to->key) {
  364. classes = to->unused_classes;
  365. to->key = classes->key;
  366. if (!to->key)
  367. InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
  368. else
  369. classes->key = NULL;
  370. }
  371. CopyKeyClass(from, to);
  372. }
  373. else if (to->key && !from->key) {
  374. classes = to->unused_classes;
  375. classes->key = to->key;
  376. to->key = NULL;
  377. }
  378. /* If a SrvLedInfoPtr's flags are XkbSLI_IsDefault, the names and maps
  379. * pointer point into the xkbInfo->desc struct. XkbCopySrvLedInfo
  380. * didn't update the pointers so we need to do it manually here.
  381. */
  382. if (to->kbdfeed) {
  383. KbdFeedbackPtr k;
  384. for (k = to->kbdfeed; k; k = k->next) {
  385. if (!k->xkb_sli)
  386. continue;
  387. if (k->xkb_sli->flags & XkbSLI_IsDefault) {
  388. k->xkb_sli->names = to->key->xkbInfo->desc->names->indicators;
  389. k->xkb_sli->maps = to->key->xkbInfo->desc->indicators->maps;
  390. }
  391. }
  392. }
  393. /* We can't just copy over the focus class. When an app sets the focus,
  394. * it'll do so on the master device. Copying the SDs focus means losing
  395. * the focus.
  396. * So we only copy the focus class if the device didn't have one,
  397. * otherwise we leave it as it is.
  398. */
  399. if (from->focus) {
  400. if (!to->focus) {
  401. WindowPtr *oldTrace;
  402. classes = to->unused_classes;
  403. to->focus = classes->focus;
  404. if (!to->focus) {
  405. to->focus = calloc(1, sizeof(FocusClassRec));
  406. if (!to->focus)
  407. FatalError("[Xi] no memory for class shift.\n");
  408. }
  409. else
  410. classes->focus = NULL;
  411. oldTrace = to->focus->trace;
  412. memcpy(to->focus, from->focus, sizeof(FocusClassRec));
  413. to->focus->trace = realloc(oldTrace,
  414. to->focus->traceSize *
  415. sizeof(WindowPtr));
  416. if (!to->focus->trace && to->focus->traceSize)
  417. FatalError("[Xi] no memory for trace.\n");
  418. memcpy(to->focus->trace, from->focus->trace,
  419. from->focus->traceSize * sizeof(WindowPtr));
  420. to->focus->sourceid = from->id;
  421. }
  422. }
  423. else if (to->focus) {
  424. classes = to->unused_classes;
  425. classes->focus = to->focus;
  426. to->focus = NULL;
  427. }
  428. }
  429. /* FIXME: this should really be shared with the InitValuatorAxisClassRec and
  430. * similar */
  431. static void
  432. DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
  433. {
  434. ClassesPtr classes;
  435. /* Feedback classes must be copied first */
  436. if (from->ptrfeed) {
  437. PtrFeedbackPtr *p, it;
  438. if (!to->ptrfeed) {
  439. classes = to->unused_classes;
  440. to->ptrfeed = classes->ptrfeed;
  441. classes->ptrfeed = NULL;
  442. }
  443. p = &to->ptrfeed;
  444. for (it = from->ptrfeed; it; it = it->next) {
  445. if (!(*p)) {
  446. *p = calloc(1, sizeof(PtrFeedbackClassRec));
  447. if (!*p) {
  448. ErrorF("[Xi] Cannot alloc memory for class copy.");
  449. return;
  450. }
  451. }
  452. (*p)->CtrlProc = it->CtrlProc;
  453. (*p)->ctrl = it->ctrl;
  454. p = &(*p)->next;
  455. }
  456. }
  457. else if (to->ptrfeed && !from->ptrfeed) {
  458. classes = to->unused_classes;
  459. classes->ptrfeed = to->ptrfeed;
  460. to->ptrfeed = NULL;
  461. }
  462. if (from->valuator) {
  463. ValuatorClassPtr v;
  464. if (!to->valuator) {
  465. classes = to->unused_classes;
  466. to->valuator = classes->valuator;
  467. if (to->valuator)
  468. classes->valuator = NULL;
  469. }
  470. v = AllocValuatorClass(to->valuator, from->valuator->numAxes);
  471. if (!v)
  472. FatalError("[Xi] no memory for class shift.\n");
  473. to->valuator = v;
  474. memcpy(v->axes, from->valuator->axes, v->numAxes * sizeof(AxisInfo));
  475. v->sourceid = from->id;
  476. }
  477. else if (to->valuator && !from->valuator) {
  478. classes = to->unused_classes;
  479. classes->valuator = to->valuator;
  480. to->valuator = NULL;
  481. }
  482. if (from->button) {
  483. if (!to->button) {
  484. classes = to->unused_classes;
  485. to->button = classes->button;
  486. if (!to->button) {
  487. to->button = calloc(1, sizeof(ButtonClassRec));
  488. if (!to->button)
  489. FatalError("[Xi] no memory for class shift.\n");
  490. }
  491. else
  492. classes->button = NULL;
  493. }
  494. if (from->button->xkb_acts) {
  495. if (!to->button->xkb_acts) {
  496. to->button->xkb_acts = calloc(1, sizeof(XkbAction));
  497. if (!to->button->xkb_acts)
  498. FatalError("[Xi] not enough memory for xkb_acts.\n");
  499. }
  500. memcpy(to->button->xkb_acts, from->button->xkb_acts,
  501. sizeof(XkbAction));
  502. }
  503. else
  504. free(to->button->xkb_acts);
  505. memcpy(to->button->labels, from->button->labels,
  506. from->button->numButtons * sizeof(Atom));
  507. to->button->sourceid = from->id;
  508. }
  509. else if (to->button && !from->button) {
  510. classes = to->unused_classes;
  511. classes->button = to->button;
  512. to->button = NULL;
  513. }
  514. if (from->proximity) {
  515. if (!to->proximity) {
  516. classes = to->unused_classes;
  517. to->proximity = classes->proximity;
  518. if (!to->proximity) {
  519. to->proximity = calloc(1, sizeof(ProximityClassRec));
  520. if (!to->proximity)
  521. FatalError("[Xi] no memory for class shift.\n");
  522. }
  523. else
  524. classes->proximity = NULL;
  525. }
  526. memcpy(to->proximity, from->proximity, sizeof(ProximityClassRec));
  527. to->proximity->sourceid = from->id;
  528. }
  529. else if (to->proximity) {
  530. classes = to->unused_classes;
  531. classes->proximity = to->proximity;
  532. to->proximity = NULL;
  533. }
  534. if (from->touch) {
  535. TouchClassPtr t, f;
  536. if (!to->touch) {
  537. classes = to->unused_classes;
  538. to->touch = classes->touch;
  539. if (!to->touch) {
  540. int i;
  541. to->touch = calloc(1, sizeof(TouchClassRec));
  542. if (!to->touch)
  543. FatalError("[Xi] no memory for class shift.\n");
  544. to->touch->num_touches = from->touch->num_touches;
  545. to->touch->touches = calloc(to->touch->num_touches,
  546. sizeof(TouchPointInfoRec));
  547. for (i = 0; i < to->touch->num_touches; i++)
  548. TouchInitTouchPoint(to->touch, to->valuator, i);
  549. if (!to->touch)
  550. FatalError("[Xi] no memory for class shift.\n");
  551. }
  552. else
  553. classes->touch = NULL;
  554. }
  555. t = to->touch;
  556. f = from->touch;
  557. t->sourceid = f->sourceid;
  558. t->max_touches = f->max_touches;
  559. t->mode = f->mode;
  560. t->buttonsDown = f->buttonsDown;
  561. t->state = f->state;
  562. t->motionMask = f->motionMask;
  563. /* to->touches and to->num_touches are separate on the master,
  564. * don't copy */
  565. }
  566. /* Don't remove touch class if from->touch is non-existent. The to device
  567. * may have an active touch grab, so we need to keep the touch class record
  568. * around. */
  569. }
  570. /**
  571. * Copies the CONTENT of the classes of device from into the classes in device
  572. * to. From and to are identical after finishing.
  573. *
  574. * If to does not have classes from currenly has, the classes are stored in
  575. * to's devPrivates system. Later, we recover it again from there if needed.
  576. * Saves a few memory allocations.
  577. */
  578. void
  579. DeepCopyDeviceClasses(DeviceIntPtr from, DeviceIntPtr to,
  580. DeviceChangedEvent *dce)
  581. {
  582. OsBlockSIGIO();
  583. /* generic feedback classes, not tied to pointer and/or keyboard */
  584. DeepCopyFeedbackClasses(from, to);
  585. if ((dce->flags & DEVCHANGE_KEYBOARD_EVENT))
  586. DeepCopyKeyboardClasses(from, to);
  587. if ((dce->flags & DEVCHANGE_POINTER_EVENT))
  588. DeepCopyPointerClasses(from, to);
  589. OsReleaseSIGIO();
  590. }
  591. /**
  592. * Send an XI2 DeviceChangedEvent to all interested clients.
  593. */
  594. void
  595. XISendDeviceChangedEvent(DeviceIntPtr device, DeviceChangedEvent *dce)
  596. {
  597. xXIDeviceChangedEvent *dcce;
  598. int rc;
  599. rc = EventToXI2((InternalEvent *) dce, (xEvent **) &dcce);
  600. if (rc != Success) {
  601. ErrorF("[Xi] event conversion from DCE failed with code %d\n", rc);
  602. return;
  603. }
  604. /* we don't actually swap if there's a NullClient, swapping is done
  605. * later when event is delivered. */
  606. SendEventToAllWindows(device, XI_DeviceChangedMask, (xEvent *) dcce, 1);
  607. free(dcce);
  608. }
  609. static void
  610. ChangeMasterDeviceClasses(DeviceIntPtr device, DeviceChangedEvent *dce)
  611. {
  612. DeviceIntPtr slave;
  613. int rc;
  614. /* For now, we don't have devices that change physically. */
  615. if (!IsMaster(device))
  616. return;
  617. rc = dixLookupDevice(&slave, dce->sourceid, serverClient, DixReadAccess);
  618. if (rc != Success)
  619. return; /* Device has disappeared */
  620. if (IsMaster(slave))
  621. return;
  622. if (IsFloating(slave))
  623. return; /* set floating since the event */
  624. if (GetMaster(slave, MASTER_ATTACHED)->id != dce->masterid)
  625. return; /* not our slave anymore, don't care */
  626. /* FIXME: we probably need to send a DCE for the new slave now */
  627. device->public.devicePrivate = slave->public.devicePrivate;
  628. /* FIXME: the classes may have changed since we generated the event. */
  629. DeepCopyDeviceClasses(slave, device, dce);
  630. dce->deviceid = device->id;
  631. XISendDeviceChangedEvent(device, dce);
  632. }
  633. /**
  634. * Add state and motionMask to the filter for this event. The protocol
  635. * supports some extra masks for motion when a button is down:
  636. * ButtonXMotionMask and the DeviceButtonMotionMask to trigger only when at
  637. * least one button (or that specific button is down). These masks need to
  638. * be added to the filters for core/XI motion events.
  639. *
  640. * @param device The device to update the mask for
  641. * @param state The current button state mask
  642. * @param motion_mask The motion mask (DeviceButtonMotionMask or 0)
  643. */
  644. static void
  645. UpdateDeviceMotionMask(DeviceIntPtr device, unsigned short state,
  646. Mask motion_mask)
  647. {
  648. Mask mask;
  649. mask = DevicePointerMotionMask | state | motion_mask;
  650. SetMaskForEvent(device->id, mask, DeviceMotionNotify);
  651. mask = PointerMotionMask | state | motion_mask;
  652. SetMaskForEvent(device->id, mask, MotionNotify);
  653. }
  654. static void
  655. IncreaseButtonCount(DeviceIntPtr dev, int key, CARD8 *buttons_down,
  656. Mask *motion_mask, unsigned short *state)
  657. {
  658. if (dev->valuator)
  659. dev->valuator->motionHintWindow = NullWindow;
  660. (*buttons_down)++;
  661. *motion_mask = DeviceButtonMotionMask;
  662. if (dev->button->map[key] <= 5)
  663. *state |= (Button1Mask >> 1) << dev->button->map[key];
  664. }
  665. static void
  666. DecreaseButtonCount(DeviceIntPtr dev, int key, CARD8 *buttons_down,
  667. Mask *motion_mask, unsigned short *state)
  668. {
  669. if (dev->valuator)
  670. dev->valuator->motionHintWindow = NullWindow;
  671. if (*buttons_down >= 1 && !--(*buttons_down))
  672. *motion_mask = 0;
  673. if (dev->button->map[key] <= 5)
  674. *state &= ~((Button1Mask >> 1) << dev->button->map[key]);
  675. }
  676. /**
  677. * Update the device state according to the data in the event.
  678. *
  679. * return values are
  680. * DEFAULT ... process as normal
  681. * DONT_PROCESS ... return immediately from caller
  682. */
  683. #define DEFAULT 0
  684. #define DONT_PROCESS 1
  685. int
  686. UpdateDeviceState(DeviceIntPtr device, DeviceEvent *event)
  687. {
  688. int i;
  689. int key = 0, last_valuator;
  690. KeyClassPtr k = NULL;
  691. ButtonClassPtr b = NULL;
  692. ValuatorClassPtr v = NULL;
  693. TouchClassPtr t = NULL;
  694. /* This event is always the first we get, before the actual events with
  695. * the data. However, the way how the DDX is set up, "device" will
  696. * actually be the slave device that caused the event.
  697. */
  698. switch (event->type) {
  699. case ET_DeviceChanged:
  700. ChangeMasterDeviceClasses(device, (DeviceChangedEvent *) event);
  701. return DONT_PROCESS; /* event has been sent already */
  702. case ET_Motion:
  703. case ET_ButtonPress:
  704. case ET_ButtonRelease:
  705. case ET_KeyPress:
  706. case ET_KeyRelease:
  707. case ET_ProximityIn:
  708. case ET_ProximityOut:
  709. case ET_TouchBegin:
  710. case ET_TouchUpdate:
  711. case ET_TouchEnd:
  712. break;
  713. default:
  714. /* other events don't update the device */
  715. return DEFAULT;
  716. }
  717. k = device->key;
  718. v = device->valuator;
  719. b = device->button;
  720. t = device->touch;
  721. key = event->detail.key;
  722. /* Update device axis */
  723. /* Check valuators first */
  724. last_valuator = -1;
  725. for (i = 0; i < MAX_VALUATORS; i++) {
  726. if (BitIsOn(&event->valuators.mask, i)) {
  727. if (!v) {
  728. ErrorF("[Xi] Valuators reported for non-valuator device '%s'. "
  729. "Ignoring event.\n", device->name);
  730. return DONT_PROCESS;
  731. }
  732. else if (v->numAxes < i) {
  733. ErrorF("[Xi] Too many valuators reported for device '%s'. "
  734. "Ignoring event.\n", device->name);
  735. return DONT_PROCESS;
  736. }
  737. last_valuator = i;
  738. }
  739. }
  740. for (i = 0; i <= last_valuator && i < v->numAxes; i++) {
  741. /* XXX: Relative/Absolute mode */
  742. if (BitIsOn(&event->valuators.mask, i))
  743. v->axisVal[i] = event->valuators.data[i];
  744. }
  745. if (event->type == ET_KeyPress) {
  746. if (!k)
  747. return DONT_PROCESS;
  748. /* don't allow ddx to generate multiple downs, but repeats are okay */
  749. if (key_is_down(device, key, KEY_PROCESSED) && !event->key_repeat)
  750. return DONT_PROCESS;
  751. if (device->valuator)
  752. device->valuator->motionHintWindow = NullWindow;
  753. set_key_down(device, key, KEY_PROCESSED);
  754. }
  755. else if (event->type == ET_KeyRelease) {
  756. if (!k)
  757. return DONT_PROCESS;
  758. if (!key_is_down(device, key, KEY_PROCESSED)) /* guard against duplicates */
  759. return DONT_PROCESS;
  760. if (device->valuator)
  761. device->valuator->motionHintWindow = NullWindow;
  762. set_key_up(device, key, KEY_PROCESSED);
  763. }
  764. else if (event->type == ET_ButtonPress) {
  765. if (!b)
  766. return DONT_PROCESS;
  767. if (button_is_down(device, key, BUTTON_PROCESSED))
  768. return DONT_PROCESS;
  769. set_button_down(device, key, BUTTON_PROCESSED);
  770. if (!b->map[key])
  771. return DONT_PROCESS;
  772. IncreaseButtonCount(device, key, &b->buttonsDown, &b->motionMask,
  773. &b->state);
  774. UpdateDeviceMotionMask(device, b->state, b->motionMask);
  775. }
  776. else if (event->type == ET_ButtonRelease) {
  777. if (!b)
  778. return DONT_PROCESS;
  779. if (!button_is_down(device, key, BUTTON_PROCESSED))
  780. return DONT_PROCESS;
  781. if (IsMaster(device)) {
  782. DeviceIntPtr sd;
  783. /*
  784. * Leave the button down if any slave has the
  785. * button still down. Note that this depends on the
  786. * event being delivered through the slave first
  787. */
  788. for (sd = inputInfo.devices; sd; sd = sd->next) {
  789. if (IsMaster(sd) || GetMaster(sd, MASTER_POINTER) != device)
  790. continue;
  791. if (!sd->button)
  792. continue;
  793. for (i = 1; i <= sd->button->numButtons; i++)
  794. if (sd->button->map[i] == key &&
  795. button_is_down(sd, i, BUTTON_PROCESSED))
  796. return DONT_PROCESS;
  797. }
  798. }
  799. set_button_up(device, key, BUTTON_PROCESSED);
  800. if (!b->map[key])
  801. return DONT_PROCESS;
  802. DecreaseButtonCount(device, key, &b->buttonsDown, &b->motionMask,
  803. &b->state);
  804. UpdateDeviceMotionMask(device, b->state, b->motionMask);
  805. }
  806. else if (event->type == ET_ProximityIn)
  807. device->proximity->in_proximity = TRUE;
  808. else if (event->type == ET_ProximityOut)
  809. device->proximity->in_proximity = FALSE;
  810. else if (event->type == ET_TouchBegin) {
  811. BUG_RETURN_VAL(!b || !v, DONT_PROCESS);
  812. BUG_RETURN_VAL(!t, DONT_PROCESS);
  813. if (!b->map[key])
  814. return DONT_PROCESS;
  815. if (!(event->flags & TOUCH_POINTER_EMULATED) ||
  816. (event->flags & TOUCH_REPLAYING))
  817. return DONT_PROCESS;
  818. IncreaseButtonCount(device, key, &t->buttonsDown, &t->motionMask,
  819. &t->state);
  820. UpdateDeviceMotionMask(device, t->state, DeviceButtonMotionMask);
  821. }
  822. else if (event->type == ET_TouchEnd) {
  823. BUG_RETURN_VAL(!b || !v, DONT_PROCESS);
  824. BUG_RETURN_VAL(!t, DONT_PROCESS);
  825. if (t->buttonsDown <= 0 || !b->map[key])
  826. return DONT_PROCESS;
  827. if (!(event->flags & TOUCH_POINTER_EMULATED))
  828. return DONT_PROCESS;
  829. DecreaseButtonCount(device, key, &t->buttonsDown, &t->motionMask,
  830. &t->state);
  831. UpdateDeviceMotionMask(device, t->state, DeviceButtonMotionMask);
  832. }
  833. return DEFAULT;
  834. }
  835. /**
  836. * A client that does not have the TouchOwnership mask set may not receive a
  837. * TouchBegin event if there is at least one grab active.
  838. *
  839. * @return TRUE if the client selected for ownership events on the given
  840. * window for this device, FALSE otherwise
  841. */
  842. static inline Bool
  843. TouchClientWantsOwnershipEvents(ClientPtr client, DeviceIntPtr dev,
  844. WindowPtr win)
  845. {
  846. InputClients *iclient;
  847. nt_list_for_each_entry(iclient, wOtherInputMasks(win)->inputClients, next) {
  848. if (rClient(iclient) != client)
  849. continue;
  850. return xi2mask_isset(iclient->xi2mask, dev, XI_TouchOwnership);
  851. }
  852. return FALSE;
  853. }
  854. static void
  855. TouchSendOwnershipEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, int reason,
  856. XID resource)
  857. {
  858. int nev, i;
  859. InternalEvent *tel = InitEventList(GetMaximumEventsNum());
  860. nev = GetTouchOwnershipEvents(tel, dev, ti, reason, resource, 0);
  861. for (i = 0; i < nev; i++)
  862. mieqProcessDeviceEvent(dev, tel + i, NULL);
  863. FreeEventList(tel, GetMaximumEventsNum());
  864. }
  865. /**
  866. * Attempts to deliver a touch event to the given client.
  867. */
  868. static Bool
  869. DeliverOneTouchEvent(ClientPtr client, DeviceIntPtr dev, TouchPointInfoPtr ti,
  870. GrabPtr grab, WindowPtr win, InternalEvent *ev)
  871. {
  872. int err;
  873. xEvent *xi2;
  874. Mask filter;
  875. Window child = DeepestSpriteWin(&ti->sprite)->drawable.id;
  876. /* FIXME: owner event handling */
  877. /* If the client does not have the ownership mask set and is not
  878. * the current owner of the touch, only pretend we delivered */
  879. if (!grab && ti->num_grabs != 0 &&
  880. !TouchClientWantsOwnershipEvents(client, dev, win))
  881. return TRUE;
  882. /* If we fail here, we're going to leave a client hanging. */
  883. err = EventToXI2(ev, &xi2);
  884. if (err != Success)
  885. FatalError("[Xi] %s: XI2 conversion failed in %s"
  886. " (%d)\n", dev->name, __func__, err);
  887. FixUpEventFromWindow(&ti->sprite, xi2, win, child, FALSE);
  888. filter = GetEventFilter(dev, xi2);
  889. if (XaceHook(XACE_RECEIVE_ACCESS, client, win, xi2, 1) != Success)
  890. return FALSE;
  891. err = TryClientEvents(client, dev, xi2, 1, filter, filter, NullGrab);
  892. free(xi2);
  893. /* Returning the value from TryClientEvents isn't useful, since all our
  894. * resource-gone cleanups will update the delivery list anyway. */
  895. return TRUE;
  896. }
  897. static void
  898. ActivateEarlyAccept(DeviceIntPtr dev, TouchPointInfoPtr ti)
  899. {
  900. ClientPtr client;
  901. XID error;
  902. GrabPtr grab = ti->listeners[0].grab;
  903. BUG_RETURN(ti->listeners[0].type != LISTENER_GRAB &&
  904. ti->listeners[0].type != LISTENER_POINTER_GRAB);
  905. BUG_RETURN(!grab);
  906. client = rClient(grab);
  907. if (TouchAcceptReject(client, dev, XIAcceptTouch, ti->client_id,
  908. ti->listeners[0].window->drawable.id, &error) != Success)
  909. ErrorF("[Xi] Failed to accept touch grab after early acceptance.\n");
  910. }
  911. /**
  912. * Find the oldest touch that still has a pointer emulation client.
  913. *
  914. * Pointer emulation can only be performed for the oldest touch. Otherwise, the
  915. * order of events seen by the client will be wrong. This function helps us find
  916. * the next touch to be emulated.
  917. *
  918. * @param dev The device to find touches for.
  919. */
  920. static TouchPointInfoPtr
  921. FindOldestPointerEmulatedTouch(DeviceIntPtr dev)
  922. {
  923. TouchPointInfoPtr oldest = NULL;
  924. int i;
  925. for (i = 0; i < dev->touch->num_touches; i++) {
  926. TouchPointInfoPtr ti = dev->touch->touches + i;
  927. int j;
  928. if (!ti->active || !ti->emulate_pointer)
  929. continue;
  930. for (j = 0; j < ti->num_listeners; j++) {
  931. if (ti->listeners[j].type == LISTENER_POINTER_GRAB ||
  932. ti->listeners[j].type == LISTENER_POINTER_REGULAR)
  933. break;
  934. }
  935. if (j == ti->num_listeners)
  936. continue;
  937. if (!oldest) {
  938. oldest = ti;
  939. continue;
  940. }
  941. if (oldest->client_id - ti->client_id < UINT_MAX / 2)
  942. oldest = ti;
  943. }
  944. return oldest;
  945. }
  946. /**
  947. * If the current owner has rejected the event, deliver the
  948. * TouchOwnership/TouchBegin to the next item in the sprite stack.
  949. */
  950. static void
  951. TouchPuntToNextOwner(DeviceIntPtr dev, TouchPointInfoPtr ti,
  952. TouchOwnershipEvent *ev)
  953. {
  954. TouchListener *listener = &ti->listeners[0]; /* new owner */
  955. int accepted_early = listener->state == LISTENER_EARLY_ACCEPT;
  956. /* Deliver the ownership */
  957. if (listener->state == LISTENER_AWAITING_OWNER || accepted_early)
  958. DeliverTouchEvents(dev, ti, (InternalEvent *) ev,
  959. listener->listener);
  960. else if (listener->state == LISTENER_AWAITING_BEGIN) {
  961. /* We can't punt to a pointer listener unless all older pointer
  962. * emulated touches have been seen already. */
  963. if ((listener->type == LISTENER_POINTER_GRAB ||
  964. listener->type == LISTENER_POINTER_REGULAR) &&
  965. ti != FindOldestPointerEmulatedTouch(dev))
  966. return;
  967. TouchEventHistoryReplay(ti, dev, listener->listener);
  968. }
  969. /* New owner has Begin/Update but not end. If touch is pending_finish,
  970. * emulate the TouchEnd now */
  971. if (ti->pending_finish) {
  972. TouchEmitTouchEnd(dev, ti, 0, 0);
  973. /* If the last owner is not a touch grab, finalise the touch, we
  974. won't get more correspondence on this.
  975. */
  976. if (ti->num_listeners == 1 &&
  977. (ti->num_grabs == 0 ||
  978. listener->grab->grabtype != XI2 ||
  979. !xi2mask_isset(listener->grab->xi2mask, dev, XI_TouchBegin))) {
  980. TouchEndTouch(dev, ti);
  981. return;
  982. }
  983. }
  984. if (accepted_early)
  985. ActivateEarlyAccept(dev, ti);
  986. }
  987. /**
  988. * Check the oldest touch to see if it needs to be replayed to its pointer
  989. * owner.
  990. *
  991. * Touch event propagation is paused if it hits a pointer listener while an
  992. * older touch with a pointer listener is waiting on accept or reject. This
  993. * function will restart propagation of a paused touch if needed.
  994. *
  995. * @param dev The device to check touches for.
  996. */
  997. static void
  998. CheckOldestTouch(DeviceIntPtr dev)
  999. {
  1000. TouchPointInfoPtr oldest = FindOldestPointerEmulatedTouch(dev);
  1001. if (oldest && oldest->listeners[0].state == LISTENER_AWAITING_BEGIN)
  1002. TouchPuntToNextOwner(dev, oldest, NULL);
  1003. }
  1004. /**
  1005. * Process a touch rejection.
  1006. *
  1007. * @param sourcedev The source device of the touch sequence.
  1008. * @param ti The touchpoint info record.
  1009. * @param resource The resource of the client rejecting the touch.
  1010. * @param ev TouchOwnership event to send. Set to NULL if no event should be
  1011. * sent.
  1012. */
  1013. void
  1014. TouchRejected(DeviceIntPtr sourcedev, TouchPointInfoPtr ti, XID resource,
  1015. TouchOwnershipEvent *ev)
  1016. {
  1017. Bool was_owner = (resource == ti->listeners[0].listener);
  1018. int i;
  1019. /* Send a TouchEnd event to the resource being removed, but only if they
  1020. * haven't received one yet already */
  1021. for (i = 0; i < ti->num_listeners; i++) {
  1022. if (ti->listeners[i].listener == resource) {
  1023. if (ti->listeners[i].state != LISTENER_HAS_END)
  1024. TouchEmitTouchEnd(sourcedev, ti, TOUCH_REJECT, resource);
  1025. break;
  1026. }
  1027. }
  1028. /* Remove the resource from the listener list, updating
  1029. * ti->num_listeners, as well as ti->num_grabs if it was a grab. */
  1030. TouchRemoveListener(ti, resource);
  1031. /* If the current owner was removed and there are further listeners, deliver
  1032. * the TouchOwnership or TouchBegin event to the new owner. */
  1033. if (ev && ti->num_listeners > 0 && was_owner)
  1034. TouchPuntToNextOwner(sourcedev, ti, ev);
  1035. else if (ti->num_listeners == 0)
  1036. TouchEndTouch(sourcedev, ti);
  1037. CheckOldestTouch(sourcedev);
  1038. }
  1039. /**
  1040. * Processes a TouchOwnership event, indicating a grab has accepted the touch
  1041. * it currently owns, or a grab or selection has been removed. Will generate
  1042. * and send TouchEnd events to all clients removed from the delivery list, as
  1043. * well as possibly sending the new TouchOwnership event. May end the
  1044. * touchpoint if it is pending finish.
  1045. */
  1046. static void
  1047. ProcessTouchOwnershipEvent(TouchOwnershipEvent *ev,
  1048. DeviceIntPtr dev)
  1049. {
  1050. TouchPointInfoPtr ti = TouchFindByClientID(dev, ev->touchid);
  1051. if (!ti) {
  1052. DebugF("[Xi] %s: Failed to get event %d for touchpoint %d\n",
  1053. dev->name, ev->type, ev->touchid);
  1054. return;
  1055. }
  1056. if (ev->reason == XIRejectTouch)
  1057. TouchRejected(dev, ti, ev->resource, ev);
  1058. else if (ev->reason == XIAcceptTouch) {
  1059. int i;
  1060. /* For pointer-emulated listeners that ungrabbed the active grab,
  1061. * the state was forced to LISTENER_HAS_END. Still go
  1062. * through the motions of ending the touch if the listener has
  1063. * already seen the end. This ensures that the touch record is ended in
  1064. * the server.
  1065. */
  1066. if (ti->listeners[0].state == LISTENER_HAS_END)
  1067. TouchEmitTouchEnd(dev, ti, TOUCH_ACCEPT, ti->listeners[0].listener);
  1068. /* The touch owner has accepted the touch. Send TouchEnd events to
  1069. * everyone else, and truncate the list of listeners. */
  1070. for (i = 1; i < ti->num_listeners; i++)
  1071. TouchEmitTouchEnd(dev, ti, TOUCH_ACCEPT, ti->listeners[i].listener);
  1072. while (ti->num_listeners > 1)
  1073. TouchRemoveListener(ti, ti->listeners[1].listener);
  1074. /* Owner accepted after receiving end */
  1075. if (ti->listeners[0].state == LISTENER_HAS_END)
  1076. TouchEndTouch(dev, ti);
  1077. else
  1078. ti->listeners[0].state = LISTENER_HAS_ACCEPTED;
  1079. }
  1080. else { /* this is the very first ownership event for a grab */
  1081. DeliverTouchEvents(dev, ti, (InternalEvent *) ev, ev->resource);
  1082. }
  1083. }
  1084. /**
  1085. * Copy the event's valuator information into the touchpoint, we may need
  1086. * this for emulated TouchEnd events.
  1087. */
  1088. static void
  1089. TouchCopyValuatorData(DeviceEvent *ev, TouchPointInfoPtr ti)
  1090. {
  1091. int i;
  1092. for (i = 0; i < ARRAY_SIZE(ev->valuators.data); i++)
  1093. if (BitIsOn(ev->valuators.mask, i))
  1094. valuator_mask_set_double(ti->valuators, i, ev->valuators.data[i]);
  1095. }
  1096. /**
  1097. * Given a touch event and a potential listener, retrieve info needed for
  1098. * processing the event.
  1099. *
  1100. * @param dev The device generating the touch event.
  1101. * @param ti The touch point info record for the touch event.
  1102. * @param ev The touch event to process.
  1103. * @param listener The touch event listener that may receive the touch event.
  1104. * @param[out] client The client that should receive the touch event.
  1105. * @param[out] win The window to deliver the event on.
  1106. * @param[out] grab The grab to deliver the event through, if any.
  1107. * @param[out] mask The XI 2.x event mask of the grab or selection, if any.
  1108. * @return TRUE if an event should be delivered to the listener, FALSE
  1109. * otherwise.
  1110. */
  1111. static Bool
  1112. RetrieveTouchDeliveryData(DeviceIntPtr dev, TouchPointInfoPtr ti,
  1113. InternalEvent *ev, TouchListener * listener,
  1114. ClientPtr *client, WindowPtr *win, GrabPtr *grab,
  1115. XI2Mask **mask)
  1116. {
  1117. int rc;
  1118. InputClients *iclients = NULL;
  1119. *mask = NULL;
  1120. if (listener->type == LISTENER_GRAB ||
  1121. listener->type == LISTENER_POINTER_GRAB) {
  1122. *grab = listener->grab;
  1123. BUG_RETURN_VAL(!*grab, FALSE);
  1124. *client = rClient(*grab);
  1125. *win = (*grab)->window;
  1126. *mask = (*grab)->xi2mask;
  1127. }
  1128. else {
  1129. rc = dixLookupResourceByType((void **) win, listener->listener,
  1130. listener->resource_type,
  1131. serverClient, DixSendAccess);
  1132. if (rc != Success)
  1133. return FALSE;
  1134. if (listener->level == XI2) {
  1135. int evtype;
  1136. if (ti->emulate_pointer &&
  1137. listener->type == LISTENER_POINTER_REGULAR)
  1138. evtype = GetXI2Type(TouchGetPointerEventType(ev));
  1139. else
  1140. evtype = GetXI2Type(ev->any.type);
  1141. nt_list_for_each_entry(iclients,
  1142. wOtherInputMasks(*win)->inputClients, next)
  1143. if (xi2mask_isset(iclients->xi2mask, dev, evtype))
  1144. break;
  1145. BUG_RETURN_VAL(!iclients, FALSE);
  1146. *mask = iclients->xi2mask;
  1147. *client = rClient(iclients);
  1148. }
  1149. else if (listener->level == XI) {
  1150. int xi_type = GetXIType(TouchGetPointerEventType(ev));
  1151. Mask xi_filter = event_get_filter_from_type(dev, xi_type);
  1152. nt_list_for_each_entry(iclients,
  1153. wOtherInputMasks(*win)->inputClients, next)
  1154. if (iclients->mask[dev->id] & xi_filter)
  1155. break;
  1156. BUG_RETURN_VAL(!iclients, FALSE);
  1157. *client = rClient(iclients);
  1158. }
  1159. else {
  1160. int coretype = GetCoreType(TouchGetPointerEventType(ev));
  1161. Mask core_filter = event_get_filter_from_type(dev, coretype);
  1162. OtherClients *oclients;
  1163. /* all others */
  1164. nt_list_for_each_entry(oclients,
  1165. (OtherClients *) wOtherClients(*win), next)
  1166. if (oclients->mask & core_filter)
  1167. break;
  1168. /* if owner selected, oclients is NULL */
  1169. *client = oclients ? rClient(oclients) : wClient(*win);
  1170. }
  1171. *grab = NULL;
  1172. }
  1173. return TRUE;
  1174. }
  1175. static int
  1176. DeliverTouchEmulatedEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
  1177. InternalEvent *ev, TouchListener * listener,
  1178. ClientPtr client, WindowPtr win, GrabPtr grab,
  1179. XI2Mask *xi2mask)
  1180. {
  1181. InternalEvent motion, button;
  1182. InternalEvent *ptrev = &motion;
  1183. int nevents;
  1184. DeviceIntPtr kbd;
  1185. /* We don't deliver pointer events to non-owners */
  1186. if (!TouchResourceIsOwner(ti, listener->listener))
  1187. return !Success;
  1188. nevents = TouchConvertToPointerEvent(ev, &motion, &button);
  1189. BUG_RETURN_VAL(nevents == 0, BadValue);
  1190. if (nevents > 1)
  1191. ptrev = &button;
  1192. kbd = GetMaster(dev, KEYBOARD_OR_FLOAT);
  1193. event_set_state(dev, kbd, &ptrev->device_event);
  1194. ptrev->device_event.corestate = event_get_corestate(dev, kbd);
  1195. if (grab) {
  1196. /* this side-steps the usual activation mechanisms, but... */
  1197. if (ev->any.type == ET_TouchBegin && !dev->deviceGrab.grab)
  1198. ActivatePassiveGrab(dev, grab, ptrev, ev); /* also delivers the event */
  1199. else {
  1200. int deliveries = 0;
  1201. /* 'grab' is the passive grab, but if the grab isn't active,
  1202. * don't deliver */
  1203. if (!dev->deviceGrab.grab)
  1204. return !Success;
  1205. if (grab->ownerEvents) {
  1206. WindowPtr focus = NullWindow;
  1207. WindowPtr sprite_win = dev->spriteInfo->sprite->win;
  1208. deliveries = DeliverDeviceEvents(sprite_win, ptrev, grab, focus, dev);
  1209. }
  1210. if (!deliveries)
  1211. deliveries = DeliverOneGrabbedEvent(ptrev, dev, grab->grabtype);
  1212. /* We must accept the touch sequence once a pointer listener has
  1213. * received one event past ButtonPress. */
  1214. if (deliveries && ev->any.type != ET_TouchBegin &&
  1215. !(ev->device_event.flags & TOUCH_CLIENT_ID))
  1216. TouchListenerAcceptReject(dev, ti, 0, XIAcceptTouch);
  1217. if (ev->any.type == ET_TouchEnd &&
  1218. ti->num_listeners == 1 &&
  1219. !dev->button->buttonsDown &&
  1220. dev->deviceGrab.fromPassiveGrab && GrabIsPointerGrab(grab)) {
  1221. (*dev->deviceGrab.DeactivateGrab) (dev);
  1222. CheckOldestTouch(dev);
  1223. return Success;
  1224. }
  1225. }
  1226. }
  1227. else {
  1228. GrabPtr devgrab = dev->deviceGrab.grab;
  1229. DeliverDeviceEvents(win, ptrev, grab, win, dev);
  1230. /* FIXME: bad hack
  1231. * Implicit passive grab activated in response to this event. Store
  1232. * the event.
  1233. */
  1234. if (!devgrab && dev->deviceGrab.grab && dev->deviceGrab.implicitGrab) {
  1235. TouchListener *l;
  1236. GrabPtr g;
  1237. devgrab = dev->deviceGrab.grab;
  1238. g = AllocGrab(devgrab);
  1239. BUG_WARN(!g);
  1240. *dev->deviceGrab.sync.event = ev->device_event;
  1241. /* The listener array has a sequence of grabs and then one event
  1242. * selection. Implicit grab activation occurs through delivering an
  1243. * event selection. Thus, we update the last listener in the array.
  1244. */
  1245. l = &ti->listeners[ti->num_listeners - 1];
  1246. l->listener = g->resource;
  1247. l->grab = g;
  1248. //l->resource_type = RT_NONE;
  1249. if (devgrab->grabtype != XI2 || devgrab->type != XI_TouchBegin)
  1250. l->type = LISTENER_POINTER_GRAB;
  1251. else
  1252. l->type = LISTENER_GRAB;
  1253. }
  1254. }
  1255. if (ev->any.type == ET_TouchBegin)
  1256. listener->state = LISTENER_IS_OWNER;
  1257. else if (ev->any.type == ET_TouchEnd)
  1258. listener->state = LISTENER_HAS_END;
  1259. return Success;
  1260. }
  1261. static void
  1262. DeliverEmulatedMotionEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
  1263. InternalEvent *ev)
  1264. {
  1265. DeviceEvent motion;
  1266. if (ti->num_listeners) {
  1267. ClientPtr client;
  1268. WindowPtr win;
  1269. GrabPtr grab;
  1270. XI2Mask *mask;
  1271. if (ti->listeners[0].type != LISTENER_POINTER_REGULAR &&
  1272. ti->listeners[0].type != LISTENER_POINTER_GRAB)
  1273. return;
  1274. motion = ev->device_event;
  1275. motion.type = ET_TouchUpdate;
  1276. motion.detail.button = 0;
  1277. if (!RetrieveTouchDeliveryData(dev, ti, (InternalEvent*)&motion,
  1278. &ti->listeners[0], &client, &win, &grab,
  1279. &mask))
  1280. return;
  1281. /* There may be a pointer grab on the device */
  1282. if (!grab) {
  1283. grab = dev->deviceGrab.grab;
  1284. if (grab) {
  1285. win = grab->window;
  1286. mask = grab->xi2mask;
  1287. client = rClient(grab);
  1288. }
  1289. }
  1290. DeliverTouchEmulatedEvent(dev, ti, (InternalEvent*)&motion, &ti->listeners[0], client,
  1291. win, grab, mask);
  1292. }
  1293. else {
  1294. InternalEvent button;
  1295. int converted;
  1296. converted = TouchConvertToPointerEvent(ev, (InternalEvent*)&motion, &button);
  1297. BUG_WARN(converted == 0);
  1298. if (converted)
  1299. ProcessOtherEvent((InternalEvent*)&motion, dev);
  1300. }
  1301. }
  1302. /**
  1303. * Processes and delivers a TouchBegin, TouchUpdate, or a
  1304. * TouchEnd event.
  1305. *
  1306. * Due to having rather different delivery semantics (see the Xi 2.2 protocol
  1307. * spec for more information), this implements its own grab and event-selection
  1308. * delivery logic.
  1309. */
  1310. static void
  1311. ProcessTouchEvent(InternalEvent *ev, DeviceIntPtr dev)
  1312. {
  1313. TouchClassPtr t = dev->touch;
  1314. TouchPointInfoPtr ti;
  1315. uint32_t touchid;
  1316. int type = ev->any.type;
  1317. int emulate_pointer = ! !(ev->device_event.flags & TOUCH_POINTER_EMULATED);
  1318. DeviceIntPtr kbd;
  1319. if (!t)
  1320. return;
  1321. touchid = ev->device_event.touchid;
  1322. if (type == ET_TouchBegin && !(ev->device_event.flags & TOUCH_REPLAYING)) {
  1323. ti = TouchBeginTouch(dev, ev->device_event.sourceid, touchid,
  1324. emulate_pointer);
  1325. }
  1326. else
  1327. ti = TouchFindByClientID(dev, touchid);
  1328. /* Active pointer grab */
  1329. if (emulate_pointer && dev->deviceGrab.grab && !dev->deviceGrab.fromPassiveGrab &&
  1330. (dev->deviceGrab.grab->grabtype == CORE ||
  1331. dev->deviceGrab.grab->grabtype == XI ||
  1332. !xi2mask_isset(dev->deviceGrab.grab->xi2mask, dev, XI_TouchBegin)))
  1333. {
  1334. /* Active pointer grab on touch point and we get a TouchEnd - claim this
  1335. * touchpoint accepted, otherwise clients waiting for ownership will
  1336. * wait on this touchpoint until this client ungrabs, or the cows come
  1337. * home, whichever is earlier */
  1338. if (ti && type == ET_TouchEnd)
  1339. TouchListenerAcceptReject(dev, ti, 0, XIAcceptTouch);
  1340. else if (!ti && type != ET_TouchBegin) {
  1341. /* Under the following circumstances we create a new touch record for an
  1342. * existing touch:
  1343. *
  1344. * - The touch may be pointer emulated
  1345. * - An explicit grab is active on the device
  1346. * - The grab is a pointer grab
  1347. *
  1348. * This allows for an explicit grab to receive pointer events for an already
  1349. * active touch.
  1350. */
  1351. ti = TouchBeginTouch(dev, ev->device_event.sourceid, touchid,
  1352. emulate_pointer);
  1353. if (!ti) {
  1354. DebugF("[Xi] %s: Failed to create new dix record for explicitly "
  1355. "grabbed touchpoint %d\n",
  1356. dev->name, touchid);
  1357. return;
  1358. }
  1359. TouchBuildSprite(dev, ti, ev);
  1360. TouchSetupListeners(dev, ti, ev);
  1361. }
  1362. }
  1363. if (!ti) {
  1364. DebugF("[Xi] %s: Failed to get event %d for touchpoint %d\n",
  1365. dev->name, type, touchid);
  1366. return;
  1367. }
  1368. /* if emulate_pointer is set, emulate the motion event right
  1369. * here, so we can ignore it for button event emulation. TouchUpdate
  1370. * events which _only_ emulate motion just work normally */
  1371. if (emulate_pointer && ev->any.type != ET_TouchUpdate)
  1372. DeliverEmulatedMotionEvent(dev, ti, ev);
  1373. if (emulate_pointer && IsMaster(dev))
  1374. CheckMotion(&ev->device_event, dev);
  1375. kbd = GetMaster(dev, KEYBOARD_OR_FLOAT);
  1376. event_set_state(NULL, kbd, &ev->device_event);
  1377. ev->device_event.corestate = event_get_corestate(NULL, kbd);
  1378. /* Make sure we have a valid window trace for event delivery; must be
  1379. * called after event type mutation. Touch end events are always processed
  1380. * in order to end touch records. */
  1381. /* FIXME: check this */
  1382. if ((type == ET_TouchBegin &&
  1383. !(ev->device_event.flags & TOUCH_REPLAYING) &&
  1384. !TouchBuildSprite(dev, ti, ev)) ||
  1385. (type != ET_TouchEnd && ti->sprite.spriteTraceGood == 0))
  1386. return;
  1387. TouchCopyValuatorData(&ev->device_event, ti);
  1388. /* WARNING: the event type may change to TouchUpdate in
  1389. * DeliverTouchEvents if a TouchEnd was delivered to a grabbing
  1390. * owner */
  1391. DeliverTouchEvents(dev, ti, ev, ev->device_event.resource);
  1392. if (ev->any.type == ET_TouchEnd)
  1393. TouchEndTouch(dev, ti);
  1394. if (emulate_pointer)
  1395. UpdateDeviceState(dev, &ev->device_event);
  1396. }
  1397. static void
  1398. ProcessBarrierEvent(InternalEvent *e, DeviceIntPtr dev)
  1399. {
  1400. Mask filter;
  1401. WindowPtr pWin;
  1402. BarrierEvent *be = &e->barrier_event;
  1403. xEvent *ev;
  1404. int rc;
  1405. GrabPtr grab = dev->deviceGrab.grab;
  1406. if (!IsMaster(dev))
  1407. return;
  1408. if (dixLookupWindow(&pWin, be->window, serverClient, DixReadAccess) != Success)
  1409. return;
  1410. if (grab)
  1411. be->flags |= XIBarrierDeviceIsGrabbed;
  1412. rc = EventToXI2(e, &ev);
  1413. if (rc != Success) {
  1414. ErrorF("[Xi] event conversion from %s failed with code %d\n", __func__, rc);
  1415. return;
  1416. }
  1417. /* A client has a grab, deliver to this client if the grab_window is the
  1418. barrier window.
  1419. Otherwise, deliver normally to the client.
  1420. */
  1421. if (grab &&
  1422. CLIENT_ID(be->barrierid) == CLIENT_ID(grab->resource) &&
  1423. grab->window->drawable.id == be->window) {
  1424. DeliverGrabbedEvent(e, dev, FALSE);
  1425. } else {
  1426. filter = GetEventFilter(dev, ev);
  1427. DeliverEventsToWindow(dev, pWin, ev, 1,
  1428. filter, NullGrab);
  1429. }
  1430. free(ev);
  1431. }
  1432. /**
  1433. * Process DeviceEvents and DeviceChangedEvents.
  1434. */
  1435. static void
  1436. ProcessDeviceEvent(InternalEvent *ev, DeviceIntPtr device)
  1437. {
  1438. GrabPtr grab;
  1439. Bool deactivateDeviceGrab = FALSE;
  1440. int key = 0, rootX, rootY;
  1441. ButtonClassPtr b;
  1442. int ret = 0;
  1443. int corestate;
  1444. DeviceIntPtr mouse = NULL, kbd = NULL;
  1445. DeviceEvent *event = &ev->device_event;
  1446. if (IsPointerDevice(device)) {
  1447. kbd = GetMaster(device, KEYBOARD_OR_FLOAT);
  1448. mouse = device;
  1449. if (!kbd->key) /* can happen with floating SDs */
  1450. kbd = NULL;
  1451. }
  1452. else {
  1453. mouse = GetMaster(device, POINTER_OR_FLOAT);
  1454. kbd = device;
  1455. if (!mouse->valuator || !mouse->button) /* may be float. SDs */
  1456. mouse = NULL;
  1457. }
  1458. corestate = event_get_corestate(mouse, kbd);
  1459. event_set_state(mouse, kbd, event);
  1460. ret = UpdateDeviceState(device, event);
  1461. if (ret == DONT_PROCESS)
  1462. return;
  1463. b = device->button;
  1464. if (IsMaster(device) || IsFloating(device))
  1465. CheckMotion(event, device);
  1466. switch (event->type) {
  1467. case ET_Motion:
  1468. case ET_ButtonPress:
  1469. case ET_ButtonRelease:
  1470. case ET_KeyPress:
  1471. case ET_KeyRelease:
  1472. case ET_ProximityIn:
  1473. case ET_ProximityOut:
  1474. GetSpritePosition(device, &rootX, &rootY);
  1475. event->root_x = rootX;
  1476. event->root_y = rootY;
  1477. NoticeEventTime((InternalEvent *) event, device);
  1478. event->corestate = corestate;
  1479. key = event->detail.key;
  1480. break;
  1481. default:
  1482. break;
  1483. }
  1484. if (DeviceEventCallback && !syncEvents.playingEvents) {
  1485. DeviceEventInfoRec eventinfo;
  1486. SpritePtr pSprite = device->spriteInfo->sprite;
  1487. /* see comment in EnqueueEvents regarding the next three lines */
  1488. if (ev->any.type == ET_Motion)
  1489. ev->device_event.root = pSprite->hotPhys.pScreen->root->drawable.id;
  1490. eventinfo.device = device;
  1491. eventinfo.event = ev;
  1492. CallCallbacks(&DeviceEventCallback, (void *) &eventinfo);
  1493. }
  1494. grab = device->deviceGrab.grab;
  1495. switch (event->type) {
  1496. case ET_KeyPress:
  1497. if (!grab && CheckDeviceGrabs(device, event, 0))
  1498. return;
  1499. break;
  1500. case ET_KeyRelease:
  1501. if (grab && device->deviceGrab.fromPassiveGrab &&
  1502. (key == device->deviceGrab.activatingKey) &&
  1503. GrabIsKeyboardGrab(device->deviceGrab.grab))
  1504. deactivateDeviceGrab = TRUE;
  1505. break;
  1506. case ET_ButtonPress:
  1507. if (b->map[key] == 0) /* there's no button 0 */
  1508. return;
  1509. event->detail.button = b->map[key];
  1510. if (!grab && CheckDeviceGrabs(device, event, 0)) {
  1511. /* if a passive grab was activated, the event has been sent
  1512. * already */
  1513. return;
  1514. }
  1515. break;
  1516. case ET_ButtonRelease:
  1517. if (b->map[key] == 0) /* there's no button 0 */
  1518. return;
  1519. event->detail.button = b->map[key];
  1520. if (grab && !b->buttonsDown &&
  1521. device->deviceGrab.fromPassiveGrab &&
  1522. GrabIsPointerGrab(device->deviceGrab.grab))
  1523. deactivateDeviceGrab = TRUE;
  1524. default:
  1525. break;
  1526. }
  1527. if (grab)
  1528. DeliverGrabbedEvent((InternalEvent *) event, device,
  1529. deactivateDeviceGrab);
  1530. else if (device->focus && !IsPointerEvent(ev))
  1531. DeliverFocusedEvent(device, (InternalEvent *) event,
  1532. GetSpriteWindow(device));
  1533. else
  1534. DeliverDeviceEvents(GetSpriteWindow(device), (InternalEvent *) event,
  1535. NullGrab, NullWindow, device);
  1536. if (deactivateDeviceGrab == TRUE) {
  1537. (*device->deviceGrab.DeactivateGrab) (device);
  1538. if (!IsMaster (device) && !IsFloating (device)) {
  1539. int flags, num_events = 0;
  1540. InternalEvent dce;
  1541. flags = (IsPointerDevice (device)) ?
  1542. DEVCHANGE_POINTER_EVENT : DEVCHANGE_KEYBOARD_EVENT;
  1543. UpdateFromMaster (&dce, device, flags, &num_events);
  1544. BUG_WARN(num_events > 1);
  1545. if (num_events == 1)
  1546. ChangeMasterDeviceClasses(GetMaster (device, MASTER_ATTACHED),
  1547. &dce.changed_event);
  1548. }
  1549. }
  1550. event->detail.key = key;
  1551. }
  1552. /**
  1553. * Main device event processing function.
  1554. * Called from when processing the events from the event queue.
  1555. *
  1556. */
  1557. void
  1558. ProcessOtherEvent(InternalEvent *ev, DeviceIntPtr device)
  1559. {
  1560. verify_internal_event(ev);
  1561. switch (ev->any.type) {
  1562. case ET_RawKeyPress:
  1563. case ET_RawKeyRelease:
  1564. case ET_RawButtonPress:
  1565. case ET_RawButtonRelease:
  1566. case ET_RawMotion:
  1567. case ET_RawTouchBegin:
  1568. case ET_RawTouchUpdate:
  1569. case ET_RawTouchEnd:
  1570. DeliverRawEvent(&ev->raw_event, device);
  1571. break;
  1572. case ET_TouchBegin:
  1573. case ET_TouchUpdate:
  1574. case ET_TouchEnd:
  1575. ProcessTouchEvent(ev, device);
  1576. break;
  1577. case ET_TouchOwnership:
  1578. /* TouchOwnership events are handled separately from the rest, as they
  1579. * have more complex semantics. */
  1580. ProcessTouchOwnershipEvent(&ev->touch_ownership_event, device);
  1581. break;
  1582. case ET_BarrierHit:
  1583. case ET_BarrierLeave:
  1584. ProcessBarrierEvent(ev, device);
  1585. break;
  1586. default:
  1587. ProcessDeviceEvent(ev, device);
  1588. break;
  1589. }
  1590. }
  1591. static int
  1592. DeliverTouchBeginEvent(DeviceIntPtr dev, TouchPointInfoPtr ti,
  1593. InternalEvent *ev, TouchListener * listener,
  1594. ClientPtr client, WindowPtr win, GrabPtr grab,
  1595. XI2Mask *xi2mask)
  1596. {
  1597. enum TouchListenerState state;
  1598. int rc = Success;
  1599. Bool has_ownershipmask;
  1600. if (listener->type == LISTENER_POINTER_REGULAR ||
  1601. listener->type == LISTENER_POINTER_GRAB) {
  1602. rc = DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win,
  1603. grab, xi2mask);
  1604. if (rc == Success) {
  1605. listener->state = LISTENER_IS_OWNER;
  1606. /* async grabs cannot replay, so automatically accept this touch */
  1607. if (listener->type == LISTENER_POINTER_GRAB &&
  1608. dev->deviceGrab.grab &&
  1609. dev->deviceGrab.fromPassiveGrab &&
  1610. dev->deviceGrab.grab->pointerMode == GrabModeAsync)
  1611. ActivateEarlyAccept(dev, ti);
  1612. }
  1613. goto out;
  1614. }
  1615. has_ownershipmask = xi2mask_isset(xi2mask, dev, XI_TouchOwnership);
  1616. if (TouchResourceIsOwner(ti, listener->listener) || has_ownershipmask)
  1617. rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
  1618. if (!TouchResourceIsOwner(ti, listener->listener)) {
  1619. if (has_ownershipmask)
  1620. state = LISTENER_AWAITING_OWNER;
  1621. else
  1622. state = LISTENER_AWAITING_BEGIN;
  1623. }
  1624. else {
  1625. if (has_ownershipmask)
  1626. TouchSendOwnershipEvent(dev, ti, 0, listener->listener);
  1627. if (listener->type == LISTENER_REGULAR)
  1628. state = LISTENER_HAS_ACCEPTED;
  1629. else
  1630. state = LISTENER_IS_OWNER;
  1631. }
  1632. listener->state = state;
  1633. out:
  1634. return rc;
  1635. }
  1636. static int
  1637. DeliverTouchEndEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, InternalEvent *ev,
  1638. TouchListener * listener, ClientPtr client,
  1639. WindowPtr win, GrabPtr grab, XI2Mask *xi2mask)
  1640. {
  1641. int rc = Success;
  1642. if (listener->type == LISTENER_POINTER_REGULAR ||
  1643. listener->type == LISTENER_POINTER_GRAB) {
  1644. /* Note: If the active grab was ungrabbed, we already changed the
  1645. * state to LISTENER_HAS_END but still get here. So we mustn't
  1646. * actually send the event.
  1647. * This is part two of the hack in DeactivatePointerGrab
  1648. */
  1649. if (listener->state != LISTENER_HAS_END) {
  1650. rc = DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win,
  1651. grab, xi2mask);
  1652. /* Once we send a TouchEnd to a legacy listener, we're already well
  1653. * past the accepting/rejecting stage (can only happen on
  1654. * GrabModeSync + replay. This listener now gets the end event,
  1655. * and we can continue.
  1656. */
  1657. if (rc == Success)
  1658. listener->state = LISTENER_HAS_END;
  1659. }
  1660. goto out;
  1661. }
  1662. /* A client is waiting for the begin, don't give it a TouchEnd */
  1663. if (listener->state == LISTENER_AWAITING_BEGIN) {
  1664. listener->state = LISTENER_HAS_END;
  1665. goto out;
  1666. }
  1667. /* Event in response to reject */
  1668. if (ev->device_event.flags & TOUCH_REJECT ||
  1669. (ev->device_event.flags & TOUCH_ACCEPT && !TouchResourceIsOwner(ti, listener->listener))) {
  1670. /* Touch has been rejected, or accepted by its owner which is not this listener */
  1671. if (listener->state != LISTENER_HAS_END)
  1672. rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
  1673. listener->state = LISTENER_HAS_END;
  1674. }
  1675. else if (TouchResourceIsOwner(ti, listener->listener)) {
  1676. Bool normal_end = !(ev->device_event.flags & TOUCH_ACCEPT);
  1677. /* FIXME: what about early acceptance */
  1678. if (normal_end && listener->state != LISTENER_HAS_END)
  1679. rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
  1680. if ((ti->num_listeners > 1 ||
  1681. (ti->num_grabs > 0 && listener->state != LISTENER_HAS_ACCEPTED)) &&
  1682. (ev->device_event.flags & (TOUCH_ACCEPT | TOUCH_REJECT)) == 0) {
  1683. ev->any.type = ET_TouchUpdate;
  1684. ev->device_event.flags |= TOUCH_PENDING_END;
  1685. ti->pending_finish = TRUE;
  1686. }
  1687. if (normal_end)
  1688. listener->state = LISTENER_HAS_END;
  1689. }
  1690. out:
  1691. return rc;
  1692. }
  1693. static int
  1694. DeliverTouchEvent(DeviceIntPtr dev, TouchPointInfoPtr ti, InternalEvent *ev,
  1695. TouchListener * listener, ClientPtr client,
  1696. WindowPtr win, GrabPtr grab, XI2Mask *xi2mask)
  1697. {
  1698. Bool has_ownershipmask = FALSE;
  1699. int rc = Success;
  1700. if (xi2mask)
  1701. has_ownershipmask = xi2mask_isset(xi2mask, dev, XI_TouchOwnership);
  1702. if (ev->any.type == ET_TouchOwnership) {
  1703. ev->touch_ownership_event.deviceid = dev->id;
  1704. if (!TouchResourceIsOwner(ti, listener->listener))
  1705. goto out;
  1706. rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
  1707. listener->state = LISTENER_IS_OWNER;
  1708. }
  1709. else
  1710. ev->device_event.deviceid = dev->id;
  1711. if (ev->any.type == ET_TouchBegin) {
  1712. rc = DeliverTouchBeginEvent(dev, ti, ev, listener, client, win, grab,
  1713. xi2mask);
  1714. }
  1715. else if (ev->any.type == ET_TouchUpdate) {
  1716. if (listener->type == LISTENER_POINTER_REGULAR ||
  1717. listener->type == LISTENER_POINTER_GRAB)
  1718. DeliverTouchEmulatedEvent(dev, ti, ev, listener, client, win, grab,
  1719. xi2mask);
  1720. else if (TouchResourceIsOwner(ti, listener->listener) ||
  1721. has_ownershipmask)
  1722. rc = DeliverOneTouchEvent(client, dev, ti, grab, win, ev);
  1723. }
  1724. else if (ev->any.type == ET_TouchEnd)
  1725. rc = DeliverTouchEndEvent(dev, ti, ev, listener, client, win, grab,
  1726. xi2mask);
  1727. out:
  1728. return rc;
  1729. }
  1730. /**
  1731. * Delivers a touch events to all interested clients. For TouchBegin events,
  1732. * will update ti->listeners, ti->num_listeners, and ti->num_grabs.
  1733. * May also mutate ev (type and flags) upon successful delivery. If
  1734. * @resource is non-zero, will only attempt delivery to the owner of that
  1735. * resource.
  1736. *
  1737. * @return TRUE if the event was delivered at least once, FALSE otherwise
  1738. */
  1739. void
  1740. DeliverTouchEvents(DeviceIntPtr dev, TouchPointInfoPtr ti,
  1741. InternalEvent *ev, XID resource)
  1742. {
  1743. int i;
  1744. if (ev->any.type == ET_TouchBegin &&
  1745. !(ev->device_event.flags & (TOUCH_CLIENT_ID | TOUCH_REPLAYING)))
  1746. TouchSetupListeners(dev, ti, ev);
  1747. TouchEventHistoryPush(ti, &ev->device_event);
  1748. for (i = 0; i < ti->num_listeners; i++) {
  1749. GrabPtr grab = NULL;
  1750. ClientPtr client;
  1751. WindowPtr win;
  1752. XI2Mask *mask;
  1753. TouchListener *listener = &ti->listeners[i];
  1754. if (resource && listener->listener != resource)
  1755. continue;
  1756. if (!RetrieveTouchDeliveryData(dev, ti, ev, listener, &client, &win,
  1757. &grab, &mask))
  1758. continue;
  1759. DeliverTouchEvent(dev, ti, ev, listener, client, win, grab, mask);
  1760. }
  1761. }
  1762. int
  1763. InitProximityClassDeviceStruct(DeviceIntPtr dev)
  1764. {
  1765. ProximityClassPtr proxc;
  1766. BUG_RETURN_VAL(dev == NULL, FALSE);
  1767. BUG_RETURN_VAL(dev->proximity != NULL, FALSE);
  1768. proxc = (ProximityClassPtr) malloc(sizeof(ProximityClassRec));
  1769. if (!proxc)
  1770. return FALSE;
  1771. proxc->sourceid = dev->id;
  1772. proxc->in_proximity = TRUE;
  1773. dev->proximity = proxc;
  1774. return TRUE;
  1775. }
  1776. /**
  1777. * Initialise the device's valuators. The memory must already be allocated,
  1778. * this function merely inits the matching axis (specified through axnum) to
  1779. * sane values.
  1780. *
  1781. * It is a condition that (minval < maxval).
  1782. *
  1783. * @see InitValuatorClassDeviceStruct
  1784. */
  1785. Bool
  1786. InitValuatorAxisStruct(DeviceIntPtr dev, int axnum, Atom label, int minval,
  1787. int maxval, int resolution, int min_res, int max_res,
  1788. int mode)
  1789. {
  1790. AxisInfoPtr ax;
  1791. BUG_RETURN_VAL(dev == NULL, FALSE);
  1792. BUG_RETURN_VAL(dev->valuator == NULL, FALSE);
  1793. BUG_RETURN_VAL(axnum >= dev->valuator->numAxes, FALSE);
  1794. BUG_RETURN_VAL(minval > maxval && mode == Absolute, FALSE);
  1795. ax = dev->valuator->axes + axnum;
  1796. ax->min_value = minval;
  1797. ax->max_value = maxval;
  1798. ax->resolution = resolution;
  1799. ax->min_resolution = min_res;
  1800. ax->max_resolution = max_res;
  1801. ax->label = label;
  1802. ax->mode = mode;
  1803. if (mode & OutOfProximity)
  1804. dev->proximity->in_proximity = FALSE;
  1805. return SetScrollValuator(dev, axnum, SCROLL_TYPE_NONE, 0, SCROLL_FLAG_NONE);
  1806. }
  1807. /**
  1808. * Set the given axis number as a scrolling valuator.
  1809. */
  1810. Bool
  1811. SetScrollValuator(DeviceIntPtr dev, int axnum, enum ScrollType type,
  1812. double increment, int flags)
  1813. {
  1814. AxisInfoPtr ax;
  1815. int *current_ax;
  1816. InternalEvent dce;
  1817. DeviceIntPtr master;
  1818. BUG_RETURN_VAL(dev == NULL, FALSE);
  1819. BUG_RETURN_VAL(dev->valuator == NULL, FALSE);
  1820. BUG_RETURN_VAL(axnum >= dev->valuator->numAxes, FALSE);
  1821. switch (type) {
  1822. case SCROLL_TYPE_VERTICAL:
  1823. current_ax = &dev->valuator->v_scroll_axis;
  1824. break;
  1825. case SCROLL_TYPE_HORIZONTAL:
  1826. current_ax = &dev->valuator->h_scroll_axis;
  1827. break;
  1828. case SCROLL_TYPE_NONE:
  1829. ax = &dev->valuator->axes[axnum];
  1830. ax->scroll.type = type;
  1831. return TRUE;
  1832. default:
  1833. return FALSE;
  1834. }
  1835. if (increment == 0.0)
  1836. return FALSE;
  1837. if (*current_ax != -1 && axnum != *current_ax) {
  1838. ax = &dev->valuator->axes[*current_ax];
  1839. if (ax->scroll.type == type &&
  1840. (flags & SCROLL_FLAG_PREFERRED) &&
  1841. (ax->scroll.flags & SCROLL_FLAG_PREFERRED))
  1842. return FALSE;
  1843. }
  1844. *current_ax = axnum;
  1845. ax = &dev->valuator->axes[axnum];
  1846. ax->scroll.type = type;
  1847. ax->scroll.increment = increment;
  1848. ax->scroll.flags = flags;
  1849. master = GetMaster(dev, MASTER_ATTACHED);
  1850. CreateClassesChangedEvent(&dce, master, dev,
  1851. DEVCHANGE_POINTER_EVENT |
  1852. DEVCHANGE_DEVICE_CHANGE);
  1853. XISendDeviceChangedEvent(dev, &dce.changed_event);
  1854. /* if the current slave is us, update the master. If not, we'll update
  1855. * whenever the next slave switch happens anyway. CMDC sends the event
  1856. * for us */
  1857. if (master && master->lastSlave == dev)
  1858. ChangeMasterDeviceClasses(master, &dce.changed_event);
  1859. return TRUE;
  1860. }
  1861. int
  1862. CheckGrabValues(ClientPtr client, GrabParameters *param)
  1863. {
  1864. if (param->grabtype != CORE &&
  1865. param->grabtype != XI && param->grabtype != XI2) {
  1866. ErrorF("[Xi] grabtype is invalid. This is a bug.\n");
  1867. return BadImplementation;
  1868. }
  1869. if ((param->this_device_mode != GrabModeSync) &&
  1870. (param->this_device_mode != GrabModeAsync) &&
  1871. (param->this_device_mode != XIGrabModeTouch)) {
  1872. client->errorValue = param->this_device_mode;
  1873. return BadValue;
  1874. }
  1875. if ((param->other_devices_mode != GrabModeSync) &&
  1876. (param->other_devices_mode != GrabModeAsync) &&
  1877. (param->other_devices_mode != XIGrabModeTouch)) {
  1878. client->errorValue = param->other_devices_mode;
  1879. return BadValue;
  1880. }
  1881. if (param->modifiers != AnyModifier &&
  1882. param->modifiers != XIAnyModifier &&
  1883. (param->modifiers & ~AllModifiersMask)) {
  1884. client->errorValue = param->modifiers;
  1885. return BadValue;
  1886. }
  1887. if ((param->ownerEvents != xFalse) && (param->ownerEvents != xTrue)) {
  1888. client->errorValue = param->ownerEvents;
  1889. return BadValue;
  1890. }
  1891. return Success;
  1892. }
  1893. int
  1894. GrabButton(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
  1895. int button, GrabParameters *param, enum InputLevel grabtype,
  1896. GrabMask *mask)
  1897. {
  1898. WindowPtr pWin, confineTo;
  1899. CursorPtr cursor;
  1900. GrabPtr grab;
  1901. int rc, type = -1;
  1902. Mask access_mode = DixGrabAccess;
  1903. rc = CheckGrabValues(client, param);
  1904. if (rc != Success)
  1905. return rc;
  1906. if (param->confineTo == None)
  1907. confineTo = NullWindow;
  1908. else {
  1909. rc = dixLookupWindow(&confineTo, param->confineTo, client,
  1910. DixSetAttrAccess);
  1911. if (rc != Success)
  1912. return rc;
  1913. }
  1914. if (param->cursor == None)
  1915. cursor = NullCursor;
  1916. else {
  1917. rc = dixLookupResourceByType((void **) &cursor, param->cursor,
  1918. RT_CURSOR, client, DixUseAccess);
  1919. if (rc != Success) {
  1920. client->errorValue = param->cursor;
  1921. return rc;
  1922. }
  1923. access_mode |= DixForceAccess;
  1924. }
  1925. if (param->this_device_mode == GrabModeSync ||
  1926. param->other_devices_mode == GrabModeSync)
  1927. access_mode |= DixFreezeAccess;
  1928. rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
  1929. if (rc != Success)
  1930. return rc;
  1931. rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
  1932. if (rc != Success)
  1933. return rc;
  1934. if (grabtype == XI)
  1935. type = DeviceButtonPress;
  1936. else if (grabtype == XI2)
  1937. type = XI_ButtonPress;
  1938. grab = CreateGrab(client->index, dev, modifier_device, pWin, grabtype,
  1939. mask, param, type, button, confineTo, cursor);
  1940. if (!grab)
  1941. return BadAlloc;
  1942. return AddPassiveGrabToList(client, grab);
  1943. }
  1944. /**
  1945. * Grab the given key.
  1946. */
  1947. int
  1948. GrabKey(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr modifier_device,
  1949. int key, GrabParameters *param, enum InputLevel grabtype,
  1950. GrabMask *mask)
  1951. {
  1952. WindowPtr pWin;
  1953. GrabPtr grab;
  1954. KeyClassPtr k = dev->key;
  1955. Mask access_mode = DixGrabAccess;
  1956. int rc, type = -1;
  1957. rc = CheckGrabValues(client, param);
  1958. if (rc != Success)
  1959. return rc;
  1960. if ((dev->id != XIAllDevices && dev->id != XIAllMasterDevices) && k == NULL)
  1961. return BadMatch;
  1962. if (grabtype == XI) {
  1963. if ((key > k->xkbInfo->desc->max_key_code ||
  1964. key < k->xkbInfo->desc->min_key_code)
  1965. && (key != AnyKey)) {
  1966. client->errorValue = key;
  1967. return BadValue;
  1968. }
  1969. type = DeviceKeyPress;
  1970. }
  1971. else if (grabtype == XI2)
  1972. type = XI_KeyPress;
  1973. rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
  1974. if (rc != Success)
  1975. return rc;
  1976. if (param->this_device_mode == GrabModeSync ||
  1977. param->other_devices_mode == GrabModeSync)
  1978. access_mode |= DixFreezeAccess;
  1979. rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
  1980. if (rc != Success)
  1981. return rc;
  1982. grab = CreateGrab(client->index, dev, modifier_device, pWin, grabtype,
  1983. mask, param, type, key, NULL, NULL);
  1984. if (!grab)
  1985. return BadAlloc;
  1986. return AddPassiveGrabToList(client, grab);
  1987. }
  1988. /* Enter/FocusIn grab */
  1989. int
  1990. GrabWindow(ClientPtr client, DeviceIntPtr dev, int type,
  1991. GrabParameters *param, GrabMask *mask)
  1992. {
  1993. WindowPtr pWin;
  1994. CursorPtr cursor;
  1995. GrabPtr grab;
  1996. Mask access_mode = DixGrabAccess;
  1997. int rc;
  1998. rc = CheckGrabValues(client, param);
  1999. if (rc != Success)
  2000. return rc;
  2001. rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
  2002. if (rc != Success)
  2003. return rc;
  2004. if (param->cursor == None)
  2005. cursor = NullCursor;
  2006. else {
  2007. rc = dixLookupResourceByType((void **) &cursor, param->cursor,
  2008. RT_CURSOR, client, DixUseAccess);
  2009. if (rc != Success) {
  2010. client->errorValue = param->cursor;
  2011. return rc;
  2012. }
  2013. access_mode |= DixForceAccess;
  2014. }
  2015. if (param->this_device_mode == GrabModeSync ||
  2016. param->other_devices_mode == GrabModeSync)
  2017. access_mode |= DixFreezeAccess;
  2018. rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, access_mode);
  2019. if (rc != Success)
  2020. return rc;
  2021. grab = CreateGrab(client->index, dev, dev, pWin, XI2,
  2022. mask, param,
  2023. (type == XIGrabtypeEnter) ? XI_Enter : XI_FocusIn, 0,
  2024. NULL, cursor);
  2025. if (!grab)
  2026. return BadAlloc;
  2027. return AddPassiveGrabToList(client, grab);
  2028. }
  2029. /* Touch grab */
  2030. int
  2031. GrabTouch(ClientPtr client, DeviceIntPtr dev, DeviceIntPtr mod_dev,
  2032. GrabParameters *param, GrabMask *mask)
  2033. {
  2034. WindowPtr pWin;
  2035. GrabPtr grab;
  2036. int rc;
  2037. rc = CheckGrabValues(client, param);
  2038. if (rc != Success)
  2039. return rc;
  2040. rc = dixLookupWindow(&pWin, param->grabWindow, client, DixSetAttrAccess);
  2041. if (rc != Success)
  2042. return rc;
  2043. rc = XaceHook(XACE_DEVICE_ACCESS, client, dev, DixGrabAccess);
  2044. if (rc != Success)
  2045. return rc;
  2046. grab = CreateGrab(client->index, dev, mod_dev, pWin, XI2,
  2047. mask, param, XI_TouchBegin, 0, NullWindow, NullCursor);
  2048. if (!grab)
  2049. return BadAlloc;
  2050. return AddPassiveGrabToList(client, grab);
  2051. }
  2052. int
  2053. SelectForWindow(DeviceIntPtr dev, WindowPtr pWin, ClientPtr client,
  2054. Mask mask, Mask exclusivemasks)
  2055. {
  2056. int mskidx = dev->id;
  2057. int i, ret;
  2058. Mask check;
  2059. InputClientsPtr others;
  2060. check = (mask & exclusivemasks);
  2061. if (wOtherInputMasks(pWin)) {
  2062. if (check & wOtherInputMasks(pWin)->inputEvents[mskidx]) {
  2063. /* It is illegal for two different clients to select on any of
  2064. * the events for maskcheck. However, it is OK, for some client
  2065. * to continue selecting on one of those events.
  2066. */
  2067. for (others = wOtherInputMasks(pWin)->inputClients; others;
  2068. others = others->next) {
  2069. if (!SameClient(others, client) && (check &
  2070. others->mask[mskidx]))
  2071. return BadAccess;
  2072. }
  2073. }
  2074. for (others = wOtherInputMasks(pWin)->inputClients; others;
  2075. others = others->next) {
  2076. if (SameClient(others, client)) {
  2077. check = others->mask[mskidx];
  2078. others->mask[mskidx] = mask;
  2079. if (mask == 0) {
  2080. for (i = 0; i < EMASKSIZE; i++)
  2081. if (i != mskidx && others->mask[i] != 0)
  2082. break;
  2083. if (i == EMASKSIZE) {
  2084. RecalculateDeviceDeliverableEvents(pWin);
  2085. if (ShouldFreeInputMasks(pWin, FALSE))
  2086. FreeResource(others->resource, RT_NONE);
  2087. return Success;
  2088. }
  2089. }
  2090. goto maskSet;
  2091. }
  2092. }
  2093. }
  2094. check = 0;
  2095. if ((ret = AddExtensionClient(pWin, client, mask, mskidx)) != Success)
  2096. return ret;
  2097. maskSet:
  2098. if (dev->valuator)
  2099. if ((dev->valuator->motionHintWindow == pWin) &&
  2100. (mask & DevicePointerMotionHintMask) &&
  2101. !(check & DevicePointerMotionHintMask) && !dev->deviceGrab.grab)
  2102. dev->valuator->motionHintWindow = NullWindow;
  2103. RecalculateDeviceDeliverableEvents(pWin);
  2104. return Success;
  2105. }
  2106. static void
  2107. FreeInputClient(InputClientsPtr * other)
  2108. {
  2109. xi2mask_free(&(*other)->xi2mask);
  2110. free(*other);
  2111. *other = NULL;
  2112. }
  2113. static InputClientsPtr
  2114. AllocInputClient(void)
  2115. {
  2116. return calloc(1, sizeof(InputClients));
  2117. }
  2118. int
  2119. AddExtensionClient(WindowPtr pWin, ClientPtr client, Mask mask, int mskidx)
  2120. {
  2121. InputClientsPtr others;
  2122. if (!pWin->optional && !MakeWindowOptional(pWin))
  2123. return BadAlloc;
  2124. others = AllocInputClient();
  2125. if (!others)
  2126. return BadAlloc;
  2127. if (!pWin->optional->inputMasks && !MakeInputMasks(pWin))
  2128. goto bail;
  2129. others->xi2mask = xi2mask_new();
  2130. if (!others->xi2mask)
  2131. goto bail;
  2132. others->mask[mskidx] = mask;
  2133. others->resource = FakeClientID(client->index);
  2134. others->next = pWin->optional->inputMasks->inputClients;
  2135. pWin->optional->inputMasks->inputClients = others;
  2136. if (!AddResource(others->resource, RT_INPUTCLIENT, (void *) pWin))
  2137. goto bail;
  2138. return Success;
  2139. bail:
  2140. FreeInputClient(&others);
  2141. return BadAlloc;
  2142. }
  2143. static Bool
  2144. MakeInputMasks(WindowPtr pWin)
  2145. {
  2146. struct _OtherInputMasks *imasks;
  2147. imasks = calloc(1, sizeof(struct _OtherInputMasks));
  2148. if (!imasks)
  2149. return FALSE;
  2150. imasks->xi2mask = xi2mask_new();
  2151. if (!imasks->xi2mask) {
  2152. free(imasks);
  2153. return FALSE;
  2154. }
  2155. pWin->optional->inputMasks = imasks;
  2156. return TRUE;
  2157. }
  2158. static void
  2159. FreeInputMask(OtherInputMasks ** imask)
  2160. {
  2161. xi2mask_free(&(*imask)->xi2mask);
  2162. free(*imask);
  2163. *imask = NULL;
  2164. }
  2165. void
  2166. RecalculateDeviceDeliverableEvents(WindowPtr pWin)
  2167. {
  2168. InputClientsPtr others;
  2169. struct _OtherInputMasks *inputMasks; /* default: NULL */
  2170. WindowPtr pChild, tmp;
  2171. int i;
  2172. pChild = pWin;
  2173. while (1) {
  2174. if ((inputMasks = wOtherInputMasks(pChild)) != 0) {
  2175. xi2mask_zero(inputMasks->xi2mask, -1);
  2176. for (others = inputMasks->inputClients; others;
  2177. others = others->next) {
  2178. for (i = 0; i < EMASKSIZE; i++)
  2179. inputMasks->inputEvents[i] |= others->mask[i];
  2180. xi2mask_merge(inputMasks->xi2mask, others->xi2mask);
  2181. }
  2182. for (i = 0; i < EMASKSIZE; i++)
  2183. inputMasks->deliverableEvents[i] = inputMasks->inputEvents[i];
  2184. for (tmp = pChild->parent; tmp; tmp = tmp->parent)
  2185. if (wOtherInputMasks(tmp))
  2186. for (i = 0; i < EMASKSIZE; i++)
  2187. inputMasks->deliverableEvents[i] |=
  2188. (wOtherInputMasks(tmp)->deliverableEvents[i]
  2189. & ~inputMasks->dontPropagateMask[i] &
  2190. PropagateMask[i]);
  2191. }
  2192. if (pChild->firstChild) {
  2193. pChild = pChild->firstChild;
  2194. continue;
  2195. }
  2196. while (!pChild->nextSib && (pChild != pWin))
  2197. pChild = pChild->parent;
  2198. if (pChild == pWin)
  2199. break;
  2200. pChild = pChild->nextSib;
  2201. }
  2202. }
  2203. int
  2204. InputClientGone(WindowPtr pWin, XID id)
  2205. {
  2206. InputClientsPtr other, prev;
  2207. if (!wOtherInputMasks(pWin))
  2208. return Success;
  2209. prev = 0;
  2210. for (other = wOtherInputMasks(pWin)->inputClients; other;
  2211. other = other->next) {
  2212. if (other->resource == id) {
  2213. if (prev) {
  2214. prev->next = other->next;
  2215. FreeInputClient(&other);
  2216. }
  2217. else if (!(other->next)) {
  2218. if (ShouldFreeInputMasks(pWin, TRUE)) {
  2219. OtherInputMasks *mask = wOtherInputMasks(pWin);
  2220. mask->inputClients = other->next;
  2221. FreeInputMask(&mask);
  2222. pWin->optional->inputMasks = (OtherInputMasks *) NULL;
  2223. CheckWindowOptionalNeed(pWin);
  2224. FreeInputClient(&other);
  2225. }
  2226. else {
  2227. other->resource = FakeClientID(0);
  2228. if (!AddResource(other->resource, RT_INPUTCLIENT,
  2229. (void *) pWin))
  2230. return BadAlloc;
  2231. }
  2232. }
  2233. else {
  2234. wOtherInputMasks(pWin)->inputClients = other->next;
  2235. FreeInputClient(&other);
  2236. }
  2237. RecalculateDeviceDeliverableEvents(pWin);
  2238. return Success;
  2239. }
  2240. prev = other;
  2241. }
  2242. FatalError("client not on device event list");
  2243. }
  2244. /**
  2245. * Search for window in each touch trace for each device. Remove the window
  2246. * and all its subwindows from the trace when found. The initial window
  2247. * order is preserved.
  2248. */
  2249. void
  2250. WindowGone(WindowPtr win)
  2251. {
  2252. DeviceIntPtr dev;
  2253. for (dev = inputInfo.devices; dev; dev = dev->next) {
  2254. TouchClassPtr t = dev->touch;
  2255. int i;
  2256. if (!t)
  2257. continue;
  2258. for (i = 0; i < t->num_touches; i++) {
  2259. SpritePtr sprite = &t->touches[i].sprite;
  2260. int j;
  2261. for (j = 0; j < sprite->spriteTraceGood; j++) {
  2262. if (sprite->spriteTrace[j] == win) {
  2263. sprite->spriteTraceGood = j;
  2264. break;
  2265. }
  2266. }
  2267. }
  2268. }
  2269. }
  2270. int
  2271. SendEvent(ClientPtr client, DeviceIntPtr d, Window dest, Bool propagate,
  2272. xEvent *ev, Mask mask, int count)
  2273. {
  2274. WindowPtr pWin;
  2275. WindowPtr effectiveFocus = NullWindow; /* only set if dest==InputFocus */
  2276. WindowPtr spriteWin = GetSpriteWindow(d);
  2277. if (dest == PointerWindow)
  2278. pWin = spriteWin;
  2279. else if (dest == InputFocus) {
  2280. WindowPtr inputFocus;
  2281. if (!d->focus)
  2282. inputFocus = spriteWin;
  2283. else
  2284. inputFocus = d->focus->win;
  2285. if (inputFocus == FollowKeyboardWin)
  2286. inputFocus = inputInfo.keyboard->focus->win;
  2287. if (inputFocus == NoneWin)
  2288. return Success;
  2289. /* If the input focus is PointerRootWin, send the event to where
  2290. * the pointer is if possible, then perhaps propogate up to root. */
  2291. if (inputFocus == PointerRootWin)
  2292. inputFocus = GetCurrentRootWindow(d);
  2293. if (IsParent(inputFocus, spriteWin)) {
  2294. effectiveFocus = inputFocus;
  2295. pWin = spriteWin;
  2296. }
  2297. else
  2298. effectiveFocus = pWin = inputFocus;
  2299. }
  2300. else
  2301. dixLookupWindow(&pWin, dest, client, DixSendAccess);
  2302. if (!pWin)
  2303. return BadWindow;
  2304. if ((propagate != xFalse) && (propagate != xTrue)) {
  2305. client->errorValue = propagate;
  2306. return BadValue;
  2307. }
  2308. ev->u.u.type |= 0x80;
  2309. if (propagate) {
  2310. for (; pWin; pWin = pWin->parent) {
  2311. if (DeliverEventsToWindow(d, pWin, ev, count, mask, NullGrab))
  2312. return Success;
  2313. if (pWin == effectiveFocus)
  2314. return Success;
  2315. if (wOtherInputMasks(pWin))
  2316. mask &= ~wOtherInputMasks(pWin)->dontPropagateMask[d->id];
  2317. if (!mask)
  2318. break;
  2319. }
  2320. }
  2321. else if (!XaceHook(XACE_SEND_ACCESS, client, NULL, pWin, ev, count))
  2322. DeliverEventsToWindow(d, pWin, ev, count, mask, NullGrab);
  2323. return Success;
  2324. }
  2325. int
  2326. SetButtonMapping(ClientPtr client, DeviceIntPtr dev, int nElts, BYTE * map)
  2327. {
  2328. int i;
  2329. ButtonClassPtr b = dev->button;
  2330. if (b == NULL)
  2331. return BadMatch;
  2332. if (nElts != b->numButtons) {
  2333. client->errorValue = nElts;
  2334. return BadValue;
  2335. }
  2336. if (BadDeviceMap(&map[0], nElts, 1, 255, &client->errorValue))
  2337. return BadValue;
  2338. for (i = 0; i < nElts; i++)
  2339. if ((b->map[i + 1] != map[i]) && BitIsOn(b->down, i + 1))
  2340. return MappingBusy;
  2341. for (i = 0; i < nElts; i++)
  2342. b->map[i + 1] = map[i];
  2343. return Success;
  2344. }
  2345. int
  2346. ChangeKeyMapping(ClientPtr client,
  2347. DeviceIntPtr dev,
  2348. unsigned len,
  2349. int type,
  2350. KeyCode firstKeyCode,
  2351. CARD8 keyCodes, CARD8 keySymsPerKeyCode, KeySym * map)
  2352. {
  2353. KeySymsRec keysyms;
  2354. KeyClassPtr k = dev->key;
  2355. if (k == NULL)
  2356. return BadMatch;
  2357. if (len != (keyCodes * keySymsPerKeyCode))
  2358. return BadLength;
  2359. if ((firstKeyCode < k->xkbInfo->desc->min_key_code) ||
  2360. (firstKeyCode + keyCodes - 1 > k->xkbInfo->desc->max_key_code)) {
  2361. client->errorValue = firstKeyCode;
  2362. return BadValue;
  2363. }
  2364. if (keySymsPerKeyCode == 0) {
  2365. client->errorValue = 0;
  2366. return BadValue;
  2367. }
  2368. keysyms.minKeyCode = firstKeyCode;
  2369. keysyms.maxKeyCode = firstKeyCode + keyCodes - 1;
  2370. keysyms.mapWidth = keySymsPerKeyCode;
  2371. keysyms.map = map;
  2372. XkbApplyMappingChange(dev, &keysyms, firstKeyCode, keyCodes, NULL,
  2373. serverClient);
  2374. return Success;
  2375. }
  2376. static void
  2377. DeleteDeviceFromAnyExtEvents(WindowPtr pWin, DeviceIntPtr dev)
  2378. {
  2379. WindowPtr parent;
  2380. /* Deactivate any grabs performed on this window, before making
  2381. * any input focus changes.
  2382. * Deactivating a device grab should cause focus events. */
  2383. if (dev->deviceGrab.grab && (dev->deviceGrab.grab->window == pWin))
  2384. (*dev->deviceGrab.DeactivateGrab) (dev);
  2385. /* If the focus window is a root window (ie. has no parent)
  2386. * then don't delete the focus from it. */
  2387. if (dev->focus && (pWin == dev->focus->win) && (pWin->parent != NullWindow)) {
  2388. int focusEventMode = NotifyNormal;
  2389. /* If a grab is in progress, then alter the mode of focus events. */
  2390. if (dev->deviceGrab.grab)
  2391. focusEventMode = NotifyWhileGrabbed;
  2392. switch (dev->focus->revert) {
  2393. case RevertToNone:
  2394. if (!ActivateFocusInGrab(dev, pWin, NoneWin))
  2395. DoFocusEvents(dev, pWin, NoneWin, focusEventMode);
  2396. dev->focus->win = NoneWin;
  2397. dev->focus->traceGood = 0;
  2398. break;
  2399. case RevertToParent:
  2400. parent = pWin;
  2401. do {
  2402. parent = parent->parent;
  2403. dev->focus->traceGood--;
  2404. }
  2405. while (!parent->realized);
  2406. if (!ActivateFocusInGrab(dev, pWin, parent))
  2407. DoFocusEvents(dev, pWin, parent, focusEventMode);
  2408. dev->focus->win = parent;
  2409. dev->focus->revert = RevertToNone;
  2410. break;
  2411. case RevertToPointerRoot:
  2412. if (!ActivateFocusInGrab(dev, pWin, PointerRootWin))
  2413. DoFocusEvents(dev, pWin, PointerRootWin, focusEventMode);
  2414. dev->focus->win = PointerRootWin;
  2415. dev->focus->traceGood = 0;
  2416. break;
  2417. case RevertToFollowKeyboard:
  2418. {
  2419. DeviceIntPtr kbd = GetMaster(dev, MASTER_KEYBOARD);
  2420. if (!kbd || (kbd == dev && kbd != inputInfo.keyboard))
  2421. kbd = inputInfo.keyboard;
  2422. if (kbd->focus->win) {
  2423. if (!ActivateFocusInGrab(dev, pWin, kbd->focus->win))
  2424. DoFocusEvents(dev, pWin, kbd->focus->win, focusEventMode);
  2425. dev->focus->win = FollowKeyboardWin;
  2426. dev->focus->traceGood = 0;
  2427. }
  2428. else {
  2429. if (!ActivateFocusInGrab(dev, pWin, NoneWin))
  2430. DoFocusEvents(dev, pWin, NoneWin, focusEventMode);
  2431. dev->focus->win = NoneWin;
  2432. dev->focus->traceGood = 0;
  2433. }
  2434. }
  2435. break;
  2436. }
  2437. }
  2438. if (dev->valuator)
  2439. if (dev->valuator->motionHintWindow == pWin)
  2440. dev->valuator->motionHintWindow = NullWindow;
  2441. }
  2442. void
  2443. DeleteWindowFromAnyExtEvents(WindowPtr pWin, Bool freeResources)
  2444. {
  2445. int i;
  2446. DeviceIntPtr dev;
  2447. InputClientsPtr ic;
  2448. struct _OtherInputMasks *inputMasks;
  2449. for (dev = inputInfo.devices; dev; dev = dev->next) {
  2450. DeleteDeviceFromAnyExtEvents(pWin, dev);
  2451. }
  2452. for (dev = inputInfo.off_devices; dev; dev = dev->next)
  2453. DeleteDeviceFromAnyExtEvents(pWin, dev);
  2454. if (freeResources)
  2455. while ((inputMasks = wOtherInputMasks(pWin)) != 0) {
  2456. ic = inputMasks->inputClients;
  2457. for (i = 0; i < EMASKSIZE; i++)
  2458. inputMasks->dontPropagateMask[i] = 0;
  2459. FreeResource(ic->resource, RT_NONE);
  2460. }
  2461. }
  2462. int
  2463. MaybeSendDeviceMotionNotifyHint(deviceKeyButtonPointer *pEvents, Mask mask)
  2464. {
  2465. DeviceIntPtr dev;
  2466. dixLookupDevice(&dev, pEvents->deviceid & DEVICE_BITS, serverClient,
  2467. DixReadAccess);
  2468. if (!dev)
  2469. return 0;
  2470. if (pEvents->type == DeviceMotionNotify) {
  2471. if (mask & DevicePointerMotionHintMask) {
  2472. if (WID(dev->valuator->motionHintWindow) == pEvents->event) {
  2473. return 1; /* don't send, but pretend we did */
  2474. }
  2475. pEvents->detail = NotifyHint;
  2476. }
  2477. else {
  2478. pEvents->detail = NotifyNormal;
  2479. }
  2480. }
  2481. return 0;
  2482. }
  2483. void
  2484. CheckDeviceGrabAndHintWindow(WindowPtr pWin, int type,
  2485. deviceKeyButtonPointer *xE, GrabPtr grab,
  2486. ClientPtr client, Mask deliveryMask)
  2487. {
  2488. DeviceIntPtr dev;
  2489. dixLookupDevice(&dev, xE->deviceid & DEVICE_BITS, serverClient,
  2490. DixGrabAccess);
  2491. if (!dev)
  2492. return;
  2493. if (type == DeviceMotionNotify)
  2494. dev->valuator->motionHintWindow = pWin;
  2495. else if ((type == DeviceButtonPress) && (!grab) &&
  2496. (deliveryMask & DeviceButtonGrabMask)) {
  2497. GrabPtr tempGrab;
  2498. tempGrab = AllocGrab(NULL);
  2499. if (!tempGrab)
  2500. return;
  2501. tempGrab->device = dev;
  2502. tempGrab->resource = client->clientAsMask;
  2503. tempGrab->window = pWin;
  2504. tempGrab->ownerEvents =
  2505. (deliveryMask & DeviceOwnerGrabButtonMask) ? TRUE : FALSE;
  2506. tempGrab->eventMask = deliveryMask;
  2507. tempGrab->keyboardMode = GrabModeAsync;
  2508. tempGrab->pointerMode = GrabModeAsync;
  2509. tempGrab->confineTo = NullWindow;
  2510. tempGrab->cursor = NullCursor;
  2511. tempGrab->next = NULL;
  2512. (*dev->deviceGrab.ActivateGrab) (dev, tempGrab, currentTime, TRUE);
  2513. FreeGrab(tempGrab);
  2514. }
  2515. }
  2516. static Mask
  2517. DeviceEventMaskForClient(DeviceIntPtr dev, WindowPtr pWin, ClientPtr client)
  2518. {
  2519. InputClientsPtr other;
  2520. if (!wOtherInputMasks(pWin))
  2521. return 0;
  2522. for (other = wOtherInputMasks(pWin)->inputClients; other;
  2523. other = other->next) {
  2524. if (SameClient(other, client))
  2525. return other->mask[dev->id];
  2526. }
  2527. return 0;
  2528. }
  2529. void
  2530. MaybeStopDeviceHint(DeviceIntPtr dev, ClientPtr client)
  2531. {
  2532. WindowPtr pWin;
  2533. GrabPtr grab = dev->deviceGrab.grab;
  2534. pWin = dev->valuator->motionHintWindow;
  2535. if ((grab && SameClient(grab, client) &&
  2536. ((grab->eventMask & DevicePointerMotionHintMask) ||
  2537. (grab->ownerEvents &&
  2538. (DeviceEventMaskForClient(dev, pWin, client) &
  2539. DevicePointerMotionHintMask)))) ||
  2540. (!grab &&
  2541. (DeviceEventMaskForClient(dev, pWin, client) &
  2542. DevicePointerMotionHintMask)))
  2543. dev->valuator->motionHintWindow = NullWindow;
  2544. }
  2545. int
  2546. DeviceEventSuppressForWindow(WindowPtr pWin, ClientPtr client, Mask mask,
  2547. int maskndx)
  2548. {
  2549. struct _OtherInputMasks *inputMasks = wOtherInputMasks(pWin);
  2550. if (mask & ~PropagateMask[maskndx]) {
  2551. client->errorValue = mask;
  2552. return BadValue;
  2553. }
  2554. if (mask == 0) {
  2555. if (inputMasks)
  2556. inputMasks->dontPropagateMask[maskndx] = mask;
  2557. }
  2558. else {
  2559. if (!inputMasks)
  2560. AddExtensionClient(pWin, client, 0, 0);
  2561. inputMasks = wOtherInputMasks(pWin);
  2562. inputMasks->dontPropagateMask[maskndx] = mask;
  2563. }
  2564. RecalculateDeviceDeliverableEvents(pWin);
  2565. if (ShouldFreeInputMasks(pWin, FALSE))
  2566. FreeResource(inputMasks->inputClients->resource, RT_NONE);
  2567. return Success;
  2568. }
  2569. Bool
  2570. ShouldFreeInputMasks(WindowPtr pWin, Bool ignoreSelectedEvents)
  2571. {
  2572. int i;
  2573. Mask allInputEventMasks = 0;
  2574. struct _OtherInputMasks *inputMasks = wOtherInputMasks(pWin);
  2575. for (i = 0; i < EMASKSIZE; i++)
  2576. allInputEventMasks |= inputMasks->dontPropagateMask[i];
  2577. if (!ignoreSelectedEvents)
  2578. for (i = 0; i < EMASKSIZE; i++)
  2579. allInputEventMasks |= inputMasks->inputEvents[i];
  2580. if (allInputEventMasks == 0)
  2581. return TRUE;
  2582. else
  2583. return FALSE;
  2584. }
  2585. /***********************************************************************
  2586. *
  2587. * Walk through the window tree, finding all clients that want to know
  2588. * about the Event.
  2589. *
  2590. */
  2591. static void
  2592. FindInterestedChildren(DeviceIntPtr dev, WindowPtr p1, Mask mask,
  2593. xEvent *ev, int count)
  2594. {
  2595. WindowPtr p2;
  2596. while (p1) {
  2597. p2 = p1->firstChild;
  2598. DeliverEventsToWindow(dev, p1, ev, count, mask, NullGrab);
  2599. FindInterestedChildren(dev, p2, mask, ev, count);
  2600. p1 = p1->nextSib;
  2601. }
  2602. }
  2603. /***********************************************************************
  2604. *
  2605. * Send an event to interested clients in all windows on all screens.
  2606. *
  2607. */
  2608. void
  2609. SendEventToAllWindows(DeviceIntPtr dev, Mask mask, xEvent *ev, int count)
  2610. {
  2611. int i;
  2612. WindowPtr pWin, p1;
  2613. for (i = 0; i < screenInfo.numScreens; i++) {
  2614. pWin = screenInfo.screens[i]->root;
  2615. if (!pWin)
  2616. continue;
  2617. DeliverEventsToWindow(dev, pWin, ev, count, mask, NullGrab);
  2618. p1 = pWin->firstChild;
  2619. FindInterestedChildren(dev, p1, mask, ev, count);
  2620. }
  2621. }
  2622. /**
  2623. * Set the XI2 mask for the given client on the given window.
  2624. * @param dev The device to set the mask for.
  2625. * @param win The window to set the mask on.
  2626. * @param client The client setting the mask.
  2627. * @param len Number of bytes in mask.
  2628. * @param mask Event mask in the form of (1 << eventtype)
  2629. */
  2630. int
  2631. XISetEventMask(DeviceIntPtr dev, WindowPtr win, ClientPtr client,
  2632. unsigned int len, unsigned char *mask)
  2633. {
  2634. OtherInputMasks *masks;
  2635. InputClientsPtr others = NULL;
  2636. masks = wOtherInputMasks(win);
  2637. if (masks) {
  2638. for (others = wOtherInputMasks(win)->inputClients; others;
  2639. others = others->next) {
  2640. if (SameClient(others, client)) {
  2641. xi2mask_zero(others->xi2mask, dev->id);
  2642. break;
  2643. }
  2644. }
  2645. }
  2646. if (len && !others) {
  2647. if (AddExtensionClient(win, client, 0, 0) != Success)
  2648. return BadAlloc;
  2649. others = wOtherInputMasks(win)->inputClients;
  2650. }
  2651. if (others) {
  2652. xi2mask_zero(others->xi2mask, dev->id);
  2653. len = min(len, xi2mask_mask_size(others->xi2mask));
  2654. }
  2655. if (len) {
  2656. xi2mask_set_one_mask(others->xi2mask, dev->id, mask, len);
  2657. }
  2658. RecalculateDeviceDeliverableEvents(win);
  2659. return Success;
  2660. }