events.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. /************************************************************
  2. Copyright 1987, 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 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  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 Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL 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. /* The panoramix components contained the following notice */
  37. /*****************************************************************
  38. Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
  39. Permission is hereby granted, free of charge, to any person obtaining a copy
  40. of this software and associated documentation files (the "Software"), to deal
  41. in the Software without restriction, including without limitation the rights
  42. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. copies of the Software.
  44. The above copyright notice and this permission notice shall be included in
  45. all copies or substantial portions of the Software.
  46. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  47. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  49. DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
  50. BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
  51. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  52. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  53. Except as contained in this notice, the name of Digital Equipment Corporation
  54. shall not be used in advertising or otherwise to promote the sale, use or other
  55. dealings in this Software without prior written authorization from Digital
  56. Equipment Corporation.
  57. ******************************************************************/
  58. /*****************************************************************
  59. Copyright 2003-2005 Sun Microsystems, Inc.
  60. All rights reserved.
  61. Permission is hereby granted, free of charge, to any person obtaining a
  62. copy of this software and associated documentation files (the
  63. "Software"), to deal in the Software without restriction, including
  64. without limitation the rights to use, copy, modify, merge, publish,
  65. distribute, and/or sell copies of the Software, and to permit persons
  66. to whom the Software is furnished to do so, provided that the above
  67. copyright notice(s) and this permission notice appear in all copies of
  68. the Software and that both the above copyright notice(s) and this
  69. permission notice appear in supporting documentation.
  70. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  71. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  72. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
  73. OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  74. HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  75. INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  76. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  77. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  78. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  79. Except as contained in this notice, the name of a copyright holder
  80. shall not be used in advertising or otherwise to promote the sale, use
  81. or other dealings in this Software without prior written authorization
  82. of the copyright holder.
  83. ******************************************************************/
  84. #ifdef HAVE_DIX_CONFIG_H
  85. #include <dix-config.h>
  86. #endif
  87. #include <X11/X.h>
  88. #include "misc.h"
  89. #include "resource.h"
  90. #include <X11/Xproto.h>
  91. #include "windowstr.h"
  92. #include "inputstr.h"
  93. #include "scrnintstr.h"
  94. #include "cursorstr.h"
  95. #include "dixstruct.h"
  96. #include "globals.h"
  97. #include <X11/extensions/XIproto.h>
  98. #include "exevents.h"
  99. #include "extnsionst.h"
  100. #include "dixevents.h"
  101. #include "dixgrabs.h"
  102. #include "dispatch.h"
  103. #define EXTENSION_EVENT_BASE 64
  104. #define NoSuchEvent 0x80000000 /* so doesn't match NoEventMask */
  105. #define StructureAndSubMask ( StructureNotifyMask | SubstructureNotifyMask )
  106. #define AllButtonsMask ( \
  107. Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
  108. #define MotionMask ( \
  109. PointerMotionMask | Button1MotionMask | \
  110. Button2MotionMask | Button3MotionMask | Button4MotionMask | \
  111. Button5MotionMask | ButtonMotionMask )
  112. #define PropagateMask ( \
  113. KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | \
  114. MotionMask )
  115. #define PointerGrabMask ( \
  116. ButtonPressMask | ButtonReleaseMask | \
  117. EnterWindowMask | LeaveWindowMask | \
  118. PointerMotionHintMask | KeymapStateMask | \
  119. MotionMask )
  120. #define AllModifiersMask ( \
  121. ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask | \
  122. Mod3Mask | Mod4Mask | Mod5Mask )
  123. #define AllEventMasks (lastEventMask|(lastEventMask-1))
  124. /*
  125. * The following relies on the fact that the Button<n>MotionMasks are equal
  126. * to the corresponding Button<n>Masks from the current modifier/button state.
  127. */
  128. #define Motion_Filter(class) (PointerMotionMask | \
  129. (class)->state | (class)->motionMask)
  130. #define WID(w) ((w) ? ((w)->drawable.id) : 0)
  131. #define XE_KBPTR (xE->u.keyButtonPointer)
  132. #define rClient(obj) (clients[CLIENT_ID((obj)->resource)])
  133. _X_EXPORT CallbackListPtr EventCallback;
  134. _X_EXPORT CallbackListPtr DeviceEventCallback;
  135. #define DNPMCOUNT 8
  136. Mask DontPropagateMasks[DNPMCOUNT];
  137. static int DontPropagateRefCnts[DNPMCOUNT];
  138. #ifdef DEBUG
  139. static debug_events = 0;
  140. #endif
  141. _X_EXPORT InputInfo inputInfo;
  142. static struct {
  143. QdEventPtr pending, *pendtail;
  144. DeviceIntPtr replayDev; /* kludgy rock to put flag for */
  145. WindowPtr replayWin; /* ComputeFreezes */
  146. Bool playingEvents;
  147. TimeStamp time;
  148. } syncEvents;
  149. /*
  150. * The window trace information is used to avoid having to compute all the
  151. * windows between the root and the current pointer window each time a button
  152. * or key goes down. The grabs on each of those windows must be checked.
  153. */
  154. static WindowPtr *spriteTrace = (WindowPtr *) NULL;
  155. #define ROOT spriteTrace[0]
  156. static int spriteTraceSize = 0;
  157. static int spriteTraceGood;
  158. static struct {
  159. CursorPtr current;
  160. BoxRec hotLimits; /* logical constraints of hot spot */
  161. Bool confined; /* confined to screen */
  162. RegionPtr hotShape; /* additional logical shape constraint */
  163. BoxRec physLimits; /* physical constraints of hot spot */
  164. WindowPtr win; /* window of logical position */
  165. HotSpot hot; /* logical pointer position */
  166. HotSpot hotPhys; /* physical pointer position */
  167. } sprite; /* info about the cursor sprite */
  168. static void DoEnterLeaveEvents(WindowPtr fromWin, WindowPtr toWin, int mode);
  169. static WindowPtr XYToWindow(int x, int y);
  170. extern int lastEvent;
  171. static Mask lastEventMask;
  172. #define CantBeFiltered NoEventMask
  173. static Mask filters[128];
  174. static const Mask initialFilters[128] = {
  175. NoSuchEvent, /* 0 */
  176. NoSuchEvent, /* 1 */
  177. KeyPressMask, /* KeyPress */
  178. KeyReleaseMask, /* KeyRelease */
  179. ButtonPressMask, /* ButtonPress */
  180. ButtonReleaseMask, /* ButtonRelease */
  181. PointerMotionMask, /* MotionNotify (initial state) */
  182. EnterWindowMask, /* EnterNotify */
  183. LeaveWindowMask, /* LeaveNotify */
  184. FocusChangeMask, /* FocusIn */
  185. FocusChangeMask, /* FocusOut */
  186. KeymapStateMask, /* KeymapNotify */
  187. ExposureMask, /* Expose */
  188. CantBeFiltered, /* GraphicsExpose */
  189. CantBeFiltered, /* NoExpose */
  190. VisibilityChangeMask, /* VisibilityNotify */
  191. SubstructureNotifyMask, /* CreateNotify */
  192. StructureAndSubMask, /* DestroyNotify */
  193. StructureAndSubMask, /* UnmapNotify */
  194. StructureAndSubMask, /* MapNotify */
  195. SubstructureRedirectMask, /* MapRequest */
  196. StructureAndSubMask, /* ReparentNotify */
  197. StructureAndSubMask, /* ConfigureNotify */
  198. SubstructureRedirectMask, /* ConfigureRequest */
  199. StructureAndSubMask, /* GravityNotify */
  200. ResizeRedirectMask, /* ResizeRequest */
  201. StructureAndSubMask, /* CirculateNotify */
  202. SubstructureRedirectMask, /* CirculateRequest */
  203. PropertyChangeMask, /* PropertyNotify */
  204. CantBeFiltered, /* SelectionClear */
  205. CantBeFiltered, /* SelectionRequest */
  206. CantBeFiltered, /* SelectionNotify */
  207. ColormapChangeMask, /* ColormapNotify */
  208. CantBeFiltered, /* ClientMessage */
  209. CantBeFiltered /* MappingNotify */
  210. };
  211. static CARD8 criticalEvents[32] = {
  212. 0x7c /* key and button events */
  213. };
  214. void
  215. SetMaskForEvent(Mask mask, int event)
  216. {
  217. if ((event < LASTEvent) || (event >= 128))
  218. FatalError("SetMaskForEvent: bogus event number");
  219. filters[event] = mask;
  220. }
  221. _X_EXPORT void
  222. SetCriticalEvent(int event)
  223. {
  224. if (event >= 128)
  225. FatalError("SetCriticalEvent: bogus event number");
  226. criticalEvents[event >> 3] |= 1 << (event & 7);
  227. }
  228. static void
  229. SyntheticMotion(int x, int y)
  230. {
  231. xEvent xE;
  232. xE.u.keyButtonPointer.rootX = x;
  233. xE.u.keyButtonPointer.rootY = y;
  234. if (syncEvents.playingEvents)
  235. xE.u.keyButtonPointer.time = syncEvents.time.milliseconds;
  236. else
  237. xE.u.keyButtonPointer.time = currentTime.milliseconds;
  238. xE.u.u.type = MotionNotify;
  239. (*inputInfo.pointer->public.processInputProc) (&xE, inputInfo.pointer, 1);
  240. }
  241. static void
  242. ConfineToShape(RegionPtr shape, int *px, int *py)
  243. {
  244. BoxRec box;
  245. int x = *px, y = *py;
  246. int incx = 1, incy = 1;
  247. if (POINT_IN_REGION(shape, x, y, &box))
  248. return;
  249. box = *REGION_EXTENTS(shape);
  250. /* this is rather crude */
  251. do {
  252. x += incx;
  253. if (x >= box.x2) {
  254. incx = -1;
  255. x = *px - 1;
  256. }
  257. else if (x < box.x1) {
  258. incx = 1;
  259. x = *px;
  260. y += incy;
  261. if (y >= box.y2) {
  262. incy = -1;
  263. y = *py - 1;
  264. }
  265. else if (y < box.y1)
  266. return; /* should never get here! */
  267. }
  268. } while (!POINT_IN_REGION(shape, x, y, &box));
  269. *px = x;
  270. *py = y;
  271. }
  272. static void
  273. CheckPhysLimits(CursorPtr cursor,
  274. Bool generateEvents, Bool confineToScreen, ScreenPtr pScreen)
  275. {
  276. HotSpot new;
  277. if (!cursor)
  278. return;
  279. new = sprite.hotPhys;
  280. if (pScreen)
  281. new.pScreen = pScreen;
  282. else
  283. pScreen = new.pScreen;
  284. (*pScreen->CursorLimits) (pScreen, cursor, &sprite.hotLimits,
  285. &sprite.physLimits);
  286. sprite.confined = confineToScreen;
  287. (*pScreen->ConstrainCursor) (pScreen, &sprite.physLimits);
  288. if (new.x < sprite.physLimits.x1)
  289. new.x = sprite.physLimits.x1;
  290. else if (new.x >= sprite.physLimits.x2)
  291. new.x = sprite.physLimits.x2 - 1;
  292. if (new.y < sprite.physLimits.y1)
  293. new.y = sprite.physLimits.y1;
  294. else if (new.y >= sprite.physLimits.y2)
  295. new.y = sprite.physLimits.y2 - 1;
  296. if (sprite.hotShape)
  297. ConfineToShape(sprite.hotShape, &new.x, &new.y);
  298. if ((pScreen != sprite.hotPhys.pScreen) ||
  299. (new.x != sprite.hotPhys.x) || (new.y != sprite.hotPhys.y)) {
  300. if (pScreen != sprite.hotPhys.pScreen)
  301. sprite.hotPhys = new;
  302. (*pScreen->SetCursorPosition) (pScreen, new.x, new.y, generateEvents);
  303. if (!generateEvents)
  304. SyntheticMotion(new.x, new.y);
  305. }
  306. }
  307. static void
  308. CheckVirtualMotion(register QdEventPtr qe, register WindowPtr pWin)
  309. {
  310. if (qe) {
  311. sprite.hot.pScreen = qe->pScreen;
  312. sprite.hot.x = qe->event->u.keyButtonPointer.rootX;
  313. sprite.hot.y = qe->event->u.keyButtonPointer.rootY;
  314. pWin = inputInfo.pointer->grab ? inputInfo.pointer->grab->confineTo :
  315. NullWindow;
  316. }
  317. if (pWin) {
  318. BoxRec lims;
  319. if (sprite.hot.pScreen != pWin->drawable.pScreen) {
  320. sprite.hot.pScreen = pWin->drawable.pScreen;
  321. sprite.hot.x = sprite.hot.y = 0;
  322. }
  323. lims = *REGION_EXTENTS(&pWin->borderSize);
  324. if (sprite.hot.x < lims.x1)
  325. sprite.hot.x = lims.x1;
  326. else if (sprite.hot.x >= lims.x2)
  327. sprite.hot.x = lims.x2 - 1;
  328. if (sprite.hot.y < lims.y1)
  329. sprite.hot.y = lims.y1;
  330. else if (sprite.hot.y >= lims.y2)
  331. sprite.hot.y = lims.y2 - 1;
  332. if (wBoundingShape(pWin))
  333. ConfineToShape(&pWin->borderSize, &sprite.hot.x, &sprite.hot.y);
  334. if (qe) {
  335. qe->pScreen = sprite.hot.pScreen;
  336. qe->event->u.keyButtonPointer.rootX = sprite.hot.x;
  337. qe->event->u.keyButtonPointer.rootY = sprite.hot.y;
  338. }
  339. }
  340. ROOT = WindowTable[sprite.hot.pScreen->myNum];
  341. }
  342. static void
  343. ConfineCursorToWindow(WindowPtr pWin, Bool generateEvents, Bool confineToScreen)
  344. {
  345. ScreenPtr pScreen = pWin->drawable.pScreen;
  346. if (syncEvents.playingEvents) {
  347. CheckVirtualMotion((QdEventPtr) NULL, pWin);
  348. SyntheticMotion(sprite.hot.x, sprite.hot.y);
  349. }
  350. else {
  351. sprite.hotLimits = *REGION_EXTENTS(&pWin->borderSize);
  352. sprite.hotShape = wBoundingShape(pWin) ? &pWin->borderSize : NullRegion;
  353. CheckPhysLimits(sprite.current, generateEvents, confineToScreen,
  354. pScreen);
  355. }
  356. }
  357. _X_EXPORT Bool
  358. PointerConfinedToScreen()
  359. {
  360. return sprite.confined;
  361. }
  362. static void
  363. ChangeToCursor(CursorPtr cursor)
  364. {
  365. if (cursor != sprite.current) {
  366. if ((sprite.current->bits->xhot != cursor->bits->xhot) ||
  367. (sprite.current->bits->yhot != cursor->bits->yhot))
  368. CheckPhysLimits(cursor, FALSE, sprite.confined, (ScreenPtr) NULL);
  369. (*sprite.hotPhys.pScreen->DisplayCursor) (sprite.hotPhys.pScreen,
  370. cursor);
  371. FreeCursor(sprite.current, (Cursor) 0);
  372. sprite.current = cursor;
  373. sprite.current->refcnt++;
  374. }
  375. }
  376. /* returns true if b is a descendent of a */
  377. Bool
  378. IsParent(register WindowPtr a, register WindowPtr b)
  379. {
  380. for (b = b->parent; b; b = b->parent)
  381. if (b == a)
  382. return TRUE;
  383. return FALSE;
  384. }
  385. static void
  386. PostNewCursor(void)
  387. {
  388. WindowPtr win;
  389. GrabPtr grab = inputInfo.pointer->grab;
  390. if (syncEvents.playingEvents)
  391. return;
  392. if (grab) {
  393. if (grab->cursor) {
  394. ChangeToCursor(grab->cursor);
  395. return;
  396. }
  397. if (IsParent(grab->window, sprite.win))
  398. win = sprite.win;
  399. else
  400. win = grab->window;
  401. }
  402. else
  403. win = sprite.win;
  404. for (; win; win = win->parent)
  405. if (win->optional && win->optional->cursor != NullCursor) {
  406. ChangeToCursor(win->optional->cursor);
  407. return;
  408. }
  409. }
  410. _X_EXPORT WindowPtr
  411. GetCurrentRootWindow()
  412. {
  413. return ROOT;
  414. }
  415. _X_EXPORT WindowPtr
  416. GetSpriteWindow()
  417. {
  418. return sprite.win;
  419. }
  420. _X_EXPORT CursorPtr
  421. GetSpriteCursor()
  422. {
  423. return sprite.current;
  424. }
  425. _X_EXPORT void
  426. GetSpritePosition(int *px, int *py)
  427. {
  428. *px = sprite.hotPhys.x;
  429. *py = sprite.hotPhys.y;
  430. }
  431. #define TIMESLOP (5 * 60 * 1000) /* 5 minutes */
  432. static void
  433. MonthChangedOrBadTime(register xEvent *xE)
  434. {
  435. /* If the ddx/OS is careless about not processing timestamped events from
  436. * different sources in sorted order, then it's possible for time to go
  437. * backwards when it should not. Here we ensure a decent time.
  438. */
  439. if ((currentTime.milliseconds - XE_KBPTR.time) > TIMESLOP)
  440. currentTime.months++;
  441. else
  442. XE_KBPTR.time = currentTime.milliseconds;
  443. }
  444. #define NoticeTime(xE) { \
  445. if ((xE)->u.keyButtonPointer.time < currentTime.milliseconds) \
  446. MonthChangedOrBadTime(xE); \
  447. currentTime.milliseconds = (xE)->u.keyButtonPointer.time; \
  448. lastDeviceEventTime = currentTime; }
  449. void
  450. NoticeEventTime(register xEvent *xE)
  451. {
  452. if (!syncEvents.playingEvents)
  453. NoticeTime(xE);
  454. }
  455. /**************************************************************************
  456. * The following procedures deal with synchronous events *
  457. **************************************************************************/
  458. void
  459. EnqueueEvent(xEvent *xE, DeviceIntPtr device, int count)
  460. {
  461. QdEventPtr tail = *syncEvents.pendtail;
  462. QdEventPtr qe;
  463. xEvent *qxE;
  464. NoticeTime(xE);
  465. if (DeviceEventCallback) {
  466. DeviceEventInfoRec eventinfo;
  467. /* The RECORD spec says that the root window field of motion events
  468. * must be valid. At this point, it hasn't been filled in yet, so
  469. * we do it here. The long expression below is necessary to get
  470. * the current root window; the apparently reasonable alternative
  471. * GetCurrentRootWindow()->drawable.id doesn't give you the right
  472. * answer on the first motion event after a screen change because
  473. * the data that GetCurrentRootWindow relies on hasn't been
  474. * updated yet.
  475. */
  476. if (xE->u.u.type == MotionNotify)
  477. XE_KBPTR.root =
  478. WindowTable[sprite.hotPhys.pScreen->myNum]->drawable.id;
  479. eventinfo.events = xE;
  480. eventinfo.count = count;
  481. CallCallbacks(&DeviceEventCallback, (pointer) &eventinfo);
  482. }
  483. if (xE->u.u.type == MotionNotify) {
  484. sprite.hotPhys.x = XE_KBPTR.rootX;
  485. sprite.hotPhys.y = XE_KBPTR.rootY;
  486. /* do motion compression */
  487. if (tail &&
  488. (tail->event->u.u.type == MotionNotify) &&
  489. (tail->pScreen == sprite.hotPhys.pScreen)) {
  490. tail->event->u.keyButtonPointer.rootX = sprite.hotPhys.x;
  491. tail->event->u.keyButtonPointer.rootY = sprite.hotPhys.y;
  492. tail->event->u.keyButtonPointer.time = XE_KBPTR.time;
  493. tail->months = currentTime.months;
  494. return;
  495. }
  496. }
  497. qe = malloc(sizeof(QdEventRec) + (count * sizeof(xEvent)));
  498. if (!qe)
  499. return;
  500. qe->next = (QdEventPtr) NULL;
  501. qe->device = device;
  502. qe->pScreen = sprite.hotPhys.pScreen;
  503. qe->months = currentTime.months;
  504. qe->event = (xEvent *) (qe + 1);
  505. qe->evcount = count;
  506. for (qxE = qe->event; --count >= 0; qxE++, xE++)
  507. *qxE = *xE;
  508. if (tail)
  509. syncEvents.pendtail = &tail->next;
  510. *syncEvents.pendtail = qe;
  511. }
  512. static void
  513. PlayReleasedEvents(void)
  514. {
  515. QdEventPtr *prev, qe;
  516. DeviceIntPtr dev;
  517. prev = &syncEvents.pending;
  518. while ((qe = *prev)) {
  519. if (!qe->device->sync.frozen) {
  520. *prev = qe->next;
  521. if (*syncEvents.pendtail == *prev)
  522. syncEvents.pendtail = prev;
  523. if (qe->event->u.u.type == MotionNotify)
  524. CheckVirtualMotion(qe, NullWindow);
  525. syncEvents.time.months = qe->months;
  526. syncEvents.time.milliseconds = qe->event->u.keyButtonPointer.time;
  527. (*qe->device->public.processInputProc) (qe->event, qe->device,
  528. qe->evcount);
  529. free(qe);
  530. for (dev = inputInfo.devices; dev && dev->sync.frozen;
  531. dev = dev->next);
  532. if (!dev)
  533. break;
  534. /* Playing the event may have unfrozen another device. */
  535. /* So to play it safe, restart at the head of the queue */
  536. prev = &syncEvents.pending;
  537. }
  538. else
  539. prev = &qe->next;
  540. }
  541. }
  542. static void
  543. FreezeThaw(register DeviceIntPtr dev, Bool frozen)
  544. {
  545. dev->sync.frozen = frozen;
  546. if (frozen)
  547. dev->public.processInputProc = dev->public.enqueueInputProc;
  548. else
  549. dev->public.processInputProc = dev->public.realInputProc;
  550. }
  551. void
  552. ComputeFreezes()
  553. {
  554. DeviceIntPtr replayDev = syncEvents.replayDev;
  555. int i;
  556. WindowPtr w;
  557. xEvent *xE;
  558. int count;
  559. GrabPtr grab;
  560. DeviceIntPtr dev;
  561. for (dev = inputInfo.devices; dev; dev = dev->next)
  562. FreezeThaw(dev, dev->sync.other || (dev->sync.state >= FROZEN));
  563. if (syncEvents.playingEvents || (!replayDev && !syncEvents.pending))
  564. return;
  565. syncEvents.playingEvents = TRUE;
  566. if (replayDev) {
  567. xE = replayDev->sync.event;
  568. count = replayDev->sync.evcount;
  569. syncEvents.replayDev = (DeviceIntPtr) NULL;
  570. w = XYToWindow(XE_KBPTR.rootX, XE_KBPTR.rootY);
  571. for (i = 0; i < spriteTraceGood; i++) {
  572. if (syncEvents.replayWin == spriteTrace[i]) {
  573. if (!CheckDeviceGrabs(replayDev, xE, i + 1, count)) {
  574. if (replayDev->focus)
  575. DeliverFocusedEvent(replayDev, xE, w, count);
  576. else
  577. DeliverDeviceEvents(w, xE, NullGrab, NullWindow,
  578. replayDev, count);
  579. }
  580. goto playmore;
  581. }
  582. }
  583. /* must not still be in the same stack */
  584. if (replayDev->focus)
  585. DeliverFocusedEvent(replayDev, xE, w, count);
  586. else
  587. DeliverDeviceEvents(w, xE, NullGrab, NullWindow, replayDev, count);
  588. }
  589. playmore:
  590. for (dev = inputInfo.devices; dev; dev = dev->next) {
  591. if (!dev->sync.frozen) {
  592. PlayReleasedEvents();
  593. break;
  594. }
  595. }
  596. syncEvents.playingEvents = FALSE;
  597. /* the following may have been skipped during replay, so do it now */
  598. if ((grab = inputInfo.pointer->grab) &&grab->confineTo) {
  599. if (grab->confineTo->drawable.pScreen != sprite.hotPhys.pScreen)
  600. sprite.hotPhys.x = sprite.hotPhys.y = 0;
  601. ConfineCursorToWindow(grab->confineTo, TRUE, TRUE);
  602. }
  603. else
  604. ConfineCursorToWindow(WindowTable[sprite.hotPhys.pScreen->myNum],
  605. TRUE, FALSE);
  606. PostNewCursor();
  607. }
  608. void
  609. ScreenRestructured(ScreenPtr pScreen)
  610. {
  611. GrabPtr grab;
  612. if ((grab = inputInfo.pointer->grab) &&grab->confineTo) {
  613. if (grab->confineTo->drawable.pScreen != sprite.hotPhys.pScreen)
  614. sprite.hotPhys.x = sprite.hotPhys.y = 0;
  615. ConfineCursorToWindow(grab->confineTo, TRUE, TRUE);
  616. }
  617. else
  618. ConfineCursorToWindow(WindowTable[sprite.hotPhys.pScreen->myNum],
  619. TRUE, FALSE);
  620. }
  621. void
  622. CheckGrabForSyncs(register DeviceIntPtr thisDev, Bool thisMode, Bool otherMode)
  623. {
  624. GrabPtr grab = thisDev->grab;
  625. DeviceIntPtr dev;
  626. if (thisMode == GrabModeSync)
  627. thisDev->sync.state = FROZEN_NO_EVENT;
  628. else { /* free both if same client owns both */
  629. thisDev->sync.state = THAWED;
  630. if (thisDev->sync.other &&
  631. (CLIENT_BITS(thisDev->sync.other->resource) ==
  632. CLIENT_BITS(grab->resource)))
  633. thisDev->sync.other = NullGrab;
  634. }
  635. for (dev = inputInfo.devices; dev; dev = dev->next) {
  636. if (dev != thisDev) {
  637. if (otherMode == GrabModeSync)
  638. dev->sync.other = grab;
  639. else { /* free both if same client owns both */
  640. if (dev->sync.other &&
  641. (CLIENT_BITS(dev->sync.other->resource) ==
  642. CLIENT_BITS(grab->resource)))
  643. dev->sync.other = NullGrab;
  644. }
  645. }
  646. }
  647. ComputeFreezes();
  648. }
  649. void
  650. ActivatePointerGrab(register DeviceIntPtr mouse, register GrabPtr grab,
  651. TimeStamp time, Bool autoGrab)
  652. {
  653. WindowPtr oldWin = (mouse->grab) ? mouse->grab->window : sprite.win;
  654. if (grab->confineTo) {
  655. if (grab->confineTo->drawable.pScreen != sprite.hotPhys.pScreen)
  656. sprite.hotPhys.x = sprite.hotPhys.y = 0;
  657. ConfineCursorToWindow(grab->confineTo, FALSE, TRUE);
  658. }
  659. DoEnterLeaveEvents(oldWin, grab->window, NotifyGrab);
  660. mouse->valuator->motionHintWindow = NullWindow;
  661. if (syncEvents.playingEvents)
  662. mouse->grabTime = syncEvents.time;
  663. else
  664. mouse->grabTime = time;
  665. if (grab->cursor)
  666. grab->cursor->refcnt++;
  667. mouse->activeGrab = *grab;
  668. mouse->grab = &mouse->activeGrab;
  669. mouse->fromPassiveGrab = autoGrab;
  670. PostNewCursor();
  671. CheckGrabForSyncs(mouse, (Bool) grab->pointerMode,
  672. (Bool) grab->keyboardMode);
  673. }
  674. void
  675. DeactivatePointerGrab(register DeviceIntPtr mouse)
  676. {
  677. GrabPtr grab = mouse->grab;
  678. DeviceIntPtr dev;
  679. mouse->valuator->motionHintWindow = NullWindow;
  680. mouse->grab = NullGrab;
  681. mouse->sync.state = NOT_GRABBED;
  682. mouse->fromPassiveGrab = FALSE;
  683. for (dev = inputInfo.devices; dev; dev = dev->next) {
  684. if (dev->sync.other == grab)
  685. dev->sync.other = NullGrab;
  686. }
  687. DoEnterLeaveEvents(grab->window, sprite.win, NotifyUngrab);
  688. if (grab->confineTo)
  689. ConfineCursorToWindow(ROOT, FALSE, FALSE);
  690. PostNewCursor();
  691. if (grab->cursor)
  692. FreeCursor(grab->cursor, (Cursor) 0);
  693. ComputeFreezes();
  694. }
  695. void
  696. ActivateKeyboardGrab(register DeviceIntPtr keybd, GrabPtr grab, TimeStamp time,
  697. Bool passive)
  698. {
  699. WindowPtr oldWin;
  700. if (keybd->grab)
  701. oldWin = keybd->grab->window;
  702. else if (keybd->focus)
  703. oldWin = keybd->focus->win;
  704. else
  705. oldWin = sprite.win;
  706. if (oldWin == FollowKeyboardWin)
  707. oldWin = inputInfo.keyboard->focus->win;
  708. if (keybd->valuator)
  709. keybd->valuator->motionHintWindow = NullWindow;
  710. DoFocusEvents(keybd, oldWin, grab->window, NotifyGrab);
  711. if (syncEvents.playingEvents)
  712. keybd->grabTime = syncEvents.time;
  713. else
  714. keybd->grabTime = time;
  715. keybd->activeGrab = *grab;
  716. keybd->grab = &keybd->activeGrab;
  717. keybd->fromPassiveGrab = passive;
  718. CheckGrabForSyncs(keybd, (Bool) grab->keyboardMode,
  719. (Bool) grab->pointerMode);
  720. }
  721. void
  722. DeactivateKeyboardGrab(register DeviceIntPtr keybd)
  723. {
  724. GrabPtr grab = keybd->grab;
  725. DeviceIntPtr dev;
  726. WindowPtr focusWin = keybd->focus ? keybd->focus->win : sprite.win;
  727. if (focusWin == FollowKeyboardWin)
  728. focusWin = inputInfo.keyboard->focus->win;
  729. if (keybd->valuator)
  730. keybd->valuator->motionHintWindow = NullWindow;
  731. keybd->grab = NullGrab;
  732. keybd->sync.state = NOT_GRABBED;
  733. keybd->fromPassiveGrab = FALSE;
  734. for (dev = inputInfo.devices; dev; dev = dev->next) {
  735. if (dev->sync.other == grab)
  736. dev->sync.other = NullGrab;
  737. }
  738. DoFocusEvents(keybd, grab->window, focusWin, NotifyUngrab);
  739. ComputeFreezes();
  740. }
  741. void
  742. AllowSome(ClientPtr client, TimeStamp time, DeviceIntPtr thisDev, int newState)
  743. {
  744. Bool thisGrabbed, otherGrabbed, othersFrozen, thisSynced;
  745. TimeStamp grabTime;
  746. DeviceIntPtr dev;
  747. thisGrabbed = thisDev->grab && SameClient(thisDev->grab, client);
  748. thisSynced = FALSE;
  749. otherGrabbed = FALSE;
  750. othersFrozen = TRUE;
  751. grabTime = thisDev->grabTime;
  752. for (dev = inputInfo.devices; dev; dev = dev->next) {
  753. if (dev == thisDev)
  754. continue;
  755. if (dev->grab && SameClient(dev->grab, client)) {
  756. if (!(thisGrabbed || otherGrabbed) ||
  757. (CompareTimeStamps(dev->grabTime, grabTime) == LATER))
  758. grabTime = dev->grabTime;
  759. otherGrabbed = TRUE;
  760. if (thisDev->sync.other == dev->grab)
  761. thisSynced = TRUE;
  762. if (dev->sync.state < FROZEN)
  763. othersFrozen = FALSE;
  764. }
  765. else if (!dev->sync.other || !SameClient(dev->sync.other, client))
  766. othersFrozen = FALSE;
  767. }
  768. if (!((thisGrabbed && thisDev->sync.state >= FROZEN) || thisSynced))
  769. return;
  770. if ((CompareTimeStamps(time, currentTime) == LATER) ||
  771. (CompareTimeStamps(time, grabTime) == EARLIER))
  772. return;
  773. switch (newState) {
  774. case THAWED: /* Async */
  775. if (thisGrabbed)
  776. thisDev->sync.state = THAWED;
  777. if (thisSynced)
  778. thisDev->sync.other = NullGrab;
  779. ComputeFreezes();
  780. break;
  781. case FREEZE_NEXT_EVENT: /* Sync */
  782. if (thisGrabbed) {
  783. thisDev->sync.state = FREEZE_NEXT_EVENT;
  784. if (thisSynced)
  785. thisDev->sync.other = NullGrab;
  786. ComputeFreezes();
  787. }
  788. break;
  789. case THAWED_BOTH: /* AsyncBoth */
  790. if (othersFrozen) {
  791. for (dev = inputInfo.devices; dev; dev = dev->next) {
  792. if (dev->grab && SameClient(dev->grab, client))
  793. dev->sync.state = THAWED;
  794. if (dev->sync.other && SameClient(dev->sync.other, client))
  795. dev->sync.other = NullGrab;
  796. }
  797. ComputeFreezes();
  798. }
  799. break;
  800. case FREEZE_BOTH_NEXT_EVENT: /* SyncBoth */
  801. if (othersFrozen) {
  802. for (dev = inputInfo.devices; dev; dev = dev->next) {
  803. if (dev->grab && SameClient(dev->grab, client))
  804. dev->sync.state = FREEZE_BOTH_NEXT_EVENT;
  805. if (dev->sync.other && SameClient(dev->sync.other, client))
  806. dev->sync.other = NullGrab;
  807. }
  808. ComputeFreezes();
  809. }
  810. break;
  811. case NOT_GRABBED: /* Replay */
  812. if (thisGrabbed && thisDev->sync.state == FROZEN_WITH_EVENT) {
  813. if (thisSynced)
  814. thisDev->sync.other = NullGrab;
  815. syncEvents.replayDev = thisDev;
  816. syncEvents.replayWin = thisDev->grab->window;
  817. (*thisDev->DeactivateGrab) (thisDev);
  818. syncEvents.replayDev = (DeviceIntPtr) NULL;
  819. }
  820. break;
  821. case THAW_OTHERS: /* AsyncOthers */
  822. if (othersFrozen) {
  823. for (dev = inputInfo.devices; dev; dev = dev->next) {
  824. if (dev == thisDev)
  825. continue;
  826. if (dev->grab && SameClient(dev->grab, client))
  827. dev->sync.state = THAWED;
  828. if (dev->sync.other && SameClient(dev->sync.other, client))
  829. dev->sync.other = NullGrab;
  830. }
  831. ComputeFreezes();
  832. }
  833. break;
  834. }
  835. }
  836. int
  837. ProcAllowEvents(register ClientPtr client)
  838. {
  839. TimeStamp time;
  840. DeviceIntPtr mouse = inputInfo.pointer;
  841. DeviceIntPtr keybd = inputInfo.keyboard;
  842. REQUEST(xAllowEventsReq);
  843. REQUEST_SIZE_MATCH(xAllowEventsReq);
  844. time = ClientTimeToServerTime(stuff->time);
  845. switch (stuff->mode) {
  846. case ReplayPointer:
  847. AllowSome(client, time, mouse, NOT_GRABBED);
  848. break;
  849. case SyncPointer:
  850. AllowSome(client, time, mouse, FREEZE_NEXT_EVENT);
  851. break;
  852. case AsyncPointer:
  853. AllowSome(client, time, mouse, THAWED);
  854. break;
  855. case ReplayKeyboard:
  856. AllowSome(client, time, keybd, NOT_GRABBED);
  857. break;
  858. case SyncKeyboard:
  859. AllowSome(client, time, keybd, FREEZE_NEXT_EVENT);
  860. break;
  861. case AsyncKeyboard:
  862. AllowSome(client, time, keybd, THAWED);
  863. break;
  864. case SyncBoth:
  865. AllowSome(client, time, keybd, FREEZE_BOTH_NEXT_EVENT);
  866. break;
  867. case AsyncBoth:
  868. AllowSome(client, time, keybd, THAWED_BOTH);
  869. break;
  870. default:
  871. client->errorValue = stuff->mode;
  872. return BadValue;
  873. }
  874. return Success;
  875. }
  876. void
  877. ReleaseActiveGrabs(ClientPtr client)
  878. {
  879. DeviceIntPtr dev;
  880. Bool done;
  881. /* XXX CloseDownClient should remove passive grabs before
  882. * releasing active grabs.
  883. */
  884. do {
  885. done = TRUE;
  886. for (dev = inputInfo.devices; dev; dev = dev->next) {
  887. if (dev->grab && SameClient(dev->grab, client)) {
  888. (*dev->DeactivateGrab) (dev);
  889. done = FALSE;
  890. }
  891. }
  892. } while (!done);
  893. }
  894. /**************************************************************************
  895. * The following procedures deal with delivering events *
  896. **************************************************************************/
  897. _X_EXPORT int
  898. TryClientEvents(ClientPtr client, xEvent *pEvents, int count, Mask mask,
  899. Mask filter, GrabPtr grab)
  900. {
  901. int type;
  902. #ifdef DEBUG
  903. if (debug_events)
  904. ErrorF("Event([%d, %d], mask=0x%x), client=%d",
  905. pEvents->u.u.type, pEvents->u.u.detail, mask, client->index);
  906. #endif
  907. if ((client) && (client != serverClient) && (!client->clientGone) &&
  908. ((filter == CantBeFiltered) || (mask & filter))) {
  909. if (grab && !SameClient(grab, client))
  910. return -1; /* don't send, but notify caller */
  911. type = pEvents->u.u.type;
  912. if (type == MotionNotify) {
  913. if (mask & PointerMotionHintMask) {
  914. if (WID(inputInfo.pointer->valuator->motionHintWindow) ==
  915. pEvents->u.keyButtonPointer.event) {
  916. #ifdef DEBUG
  917. if (debug_events)
  918. ErrorF("\n");
  919. fprintf(stderr,
  920. "motionHintWindow == keyButtonPointer.event\n");
  921. #endif
  922. return 1; /* don't send, but pretend we did */
  923. }
  924. pEvents->u.u.detail = NotifyHint;
  925. }
  926. else {
  927. pEvents->u.u.detail = NotifyNormal;
  928. }
  929. }
  930. if (BitIsOn(criticalEvents, type)) {
  931. #ifdef SMART_SCHEDULE
  932. if (client->smart_priority < SMART_MAX_PRIORITY)
  933. client->smart_priority++;
  934. #endif
  935. SetCriticalOutputPending();
  936. }
  937. WriteEventsToClient(client, count, pEvents);
  938. #ifdef DEBUG
  939. if (debug_events)
  940. ErrorF(" delivered\n");
  941. #endif
  942. return 1;
  943. }
  944. else {
  945. #ifdef DEBUG
  946. if (debug_events)
  947. ErrorF("\n");
  948. #endif
  949. return 0;
  950. }
  951. }
  952. int
  953. DeliverEventsToWindow(register WindowPtr pWin, xEvent *pEvents, int count,
  954. Mask filter, GrabPtr grab, int mskidx)
  955. {
  956. int deliveries = 0, nondeliveries = 0;
  957. int attempt;
  958. InputClients *other;
  959. ClientPtr client = NullClient;
  960. Mask deliveryMask = 0; /* If a grab occurs due to a button press, then
  961. this mask is the mask of the grab. */
  962. int type = pEvents->u.u.type;
  963. /* CantBeFiltered means only window owner gets the event */
  964. if ((filter == CantBeFiltered) || !(type & EXTENSION_EVENT_BASE)) {
  965. /* if nobody ever wants to see this event, skip some work */
  966. if (filter != CantBeFiltered &&
  967. !((wOtherEventMasks(pWin) | pWin->eventMask) & filter))
  968. return 0;
  969. if ((attempt = TryClientEvents(wClient(pWin), pEvents, count,
  970. pWin->eventMask, filter, grab))) {
  971. if (attempt > 0) {
  972. deliveries++;
  973. client = wClient(pWin);
  974. deliveryMask = pWin->eventMask;
  975. }
  976. else
  977. nondeliveries--;
  978. }
  979. }
  980. if (filter != CantBeFiltered) {
  981. if (type & EXTENSION_EVENT_BASE) {
  982. OtherInputMasks *inputMasks;
  983. inputMasks = wOtherInputMasks(pWin);
  984. if (!inputMasks || !(inputMasks->inputEvents[mskidx] & filter))
  985. return 0;
  986. other = inputMasks->inputClients;
  987. }
  988. else
  989. other = (InputClients *) wOtherClients(pWin);
  990. for (; other; other = other->next) {
  991. if ((attempt = TryClientEvents(rClient(other), pEvents, count,
  992. other->mask[mskidx], filter, grab)))
  993. {
  994. if (attempt > 0) {
  995. deliveries++;
  996. client = rClient(other);
  997. deliveryMask = other->mask[mskidx];
  998. }
  999. else
  1000. nondeliveries--;
  1001. }
  1002. }
  1003. }
  1004. if ((type == ButtonPress) && deliveries && (!grab)) {
  1005. GrabRec tempGrab;
  1006. tempGrab.device = inputInfo.pointer;
  1007. tempGrab.resource = client->clientAsMask;
  1008. tempGrab.window = pWin;
  1009. tempGrab.ownerEvents =
  1010. (deliveryMask & OwnerGrabButtonMask) ? TRUE : FALSE;
  1011. tempGrab.eventMask = deliveryMask;
  1012. tempGrab.keyboardMode = GrabModeAsync;
  1013. tempGrab.pointerMode = GrabModeAsync;
  1014. tempGrab.confineTo = NullWindow;
  1015. tempGrab.cursor = NullCursor;
  1016. (*inputInfo.pointer->ActivateGrab) (inputInfo.pointer, &tempGrab,
  1017. currentTime, TRUE);
  1018. }
  1019. else if ((type == MotionNotify) && deliveries)
  1020. inputInfo.pointer->valuator->motionHintWindow = pWin;
  1021. if (deliveries)
  1022. return deliveries;
  1023. return nondeliveries;
  1024. }
  1025. /* If the event goes to dontClient, don't send it and return 0. if
  1026. send works, return 1 or if send didn't work, return 2.
  1027. Only works for core events.
  1028. */
  1029. int
  1030. MaybeDeliverEventsToClient(register WindowPtr pWin, xEvent *pEvents,
  1031. int count, Mask filter, ClientPtr dontClient)
  1032. {
  1033. OtherClients *other;
  1034. if (pWin->eventMask & filter) {
  1035. if (wClient(pWin) == dontClient)
  1036. return 0;
  1037. return TryClientEvents(wClient(pWin), pEvents, count,
  1038. pWin->eventMask, filter, NullGrab);
  1039. }
  1040. for (other = wOtherClients(pWin); other; other = other->next) {
  1041. if (other->mask & filter) {
  1042. if (SameClient(other, dontClient))
  1043. return 0;
  1044. return TryClientEvents(rClient(other), pEvents, count,
  1045. other->mask, filter, NullGrab);
  1046. }
  1047. }
  1048. return 2;
  1049. }
  1050. static void
  1051. FixUpEventFromWindow(xEvent *xE, WindowPtr pWin, Window child, Bool calcChild)
  1052. {
  1053. if (calcChild) {
  1054. WindowPtr w = spriteTrace[spriteTraceGood - 1];
  1055. /* If the search ends up past the root should the child field be
  1056. set to none or should the value in the argument be passed
  1057. through. It probably doesn't matter since everyone calls
  1058. this function with child == None anyway. */
  1059. while (w) {
  1060. /* If the source window is same as event window, child should be
  1061. none. Don't bother going all all the way back to the root. */
  1062. if (w == pWin) {
  1063. child = None;
  1064. break;
  1065. }
  1066. if (w->parent == pWin) {
  1067. child = w->drawable.id;
  1068. break;
  1069. }
  1070. w = w->parent;
  1071. }
  1072. }
  1073. XE_KBPTR.root = ROOT->drawable.id;
  1074. XE_KBPTR.event = pWin->drawable.id;
  1075. if (sprite.hot.pScreen == pWin->drawable.pScreen) {
  1076. XE_KBPTR.sameScreen = xTrue;
  1077. XE_KBPTR.child = child;
  1078. XE_KBPTR.eventX = XE_KBPTR.rootX - pWin->drawable.x;
  1079. XE_KBPTR.eventY = XE_KBPTR.rootY - pWin->drawable.y;
  1080. }
  1081. else {
  1082. XE_KBPTR.sameScreen = xFalse;
  1083. XE_KBPTR.child = None;
  1084. XE_KBPTR.eventX = 0;
  1085. XE_KBPTR.eventY = 0;
  1086. }
  1087. }
  1088. int
  1089. DeliverDeviceEvents(register WindowPtr pWin, register xEvent *xE, GrabPtr grab,
  1090. register WindowPtr stopAt, DeviceIntPtr dev, int count)
  1091. {
  1092. Window child = None;
  1093. int type = xE->u.u.type;
  1094. Mask filter = filters[type];
  1095. int deliveries = 0;
  1096. if (type & EXTENSION_EVENT_BASE) {
  1097. OtherInputMasks *inputMasks;
  1098. int mskidx = dev->id;
  1099. inputMasks = wOtherInputMasks(pWin);
  1100. if (inputMasks && !(filter & inputMasks->deliverableEvents[mskidx]))
  1101. return 0;
  1102. while (pWin) {
  1103. if (inputMasks && (inputMasks->inputEvents[mskidx] & filter)) {
  1104. FixUpEventFromWindow(xE, pWin, child, FALSE);
  1105. deliveries = DeliverEventsToWindow(pWin, xE, count, filter,
  1106. grab, mskidx);
  1107. if (deliveries > 0)
  1108. return deliveries;
  1109. }
  1110. if ((deliveries < 0) ||
  1111. (pWin == stopAt) ||
  1112. (inputMasks &&
  1113. (filter & inputMasks->dontPropagateMask[mskidx])))
  1114. return 0;
  1115. child = pWin->drawable.id;
  1116. pWin = pWin->parent;
  1117. if (pWin)
  1118. inputMasks = wOtherInputMasks(pWin);
  1119. }
  1120. }
  1121. else {
  1122. if (!(filter & pWin->deliverableEvents))
  1123. return 0;
  1124. while (pWin) {
  1125. if ((wOtherEventMasks(pWin) | pWin->eventMask) & filter) {
  1126. FixUpEventFromWindow(xE, pWin, child, FALSE);
  1127. deliveries = DeliverEventsToWindow(pWin, xE, count, filter,
  1128. grab, 0);
  1129. if (deliveries > 0)
  1130. return deliveries;
  1131. }
  1132. if ((deliveries < 0) ||
  1133. (pWin == stopAt) || (filter & wDontPropagateMask(pWin)))
  1134. return 0;
  1135. child = pWin->drawable.id;
  1136. pWin = pWin->parent;
  1137. }
  1138. }
  1139. return 0;
  1140. }
  1141. /* not useful for events that propagate up the tree or extension events */
  1142. _X_EXPORT int
  1143. DeliverEvents(register WindowPtr pWin, register xEvent *xE, int count,
  1144. register WindowPtr otherParent)
  1145. {
  1146. Mask filter;
  1147. int deliveries;
  1148. if (!count)
  1149. return 0;
  1150. filter = filters[xE->u.u.type];
  1151. if ((filter & SubstructureNotifyMask) && (xE->u.u.type != CreateNotify))
  1152. xE->u.destroyNotify.event = pWin->drawable.id;
  1153. if (filter != StructureAndSubMask)
  1154. return DeliverEventsToWindow(pWin, xE, count, filter, NullGrab, 0);
  1155. deliveries = DeliverEventsToWindow(pWin, xE, count, StructureNotifyMask,
  1156. NullGrab, 0);
  1157. if (pWin->parent) {
  1158. xE->u.destroyNotify.event = pWin->parent->drawable.id;
  1159. deliveries += DeliverEventsToWindow(pWin->parent, xE, count,
  1160. SubstructureNotifyMask, NullGrab,
  1161. 0);
  1162. if (xE->u.u.type == ReparentNotify) {
  1163. xE->u.destroyNotify.event = otherParent->drawable.id;
  1164. deliveries += DeliverEventsToWindow(otherParent, xE, count,
  1165. SubstructureNotifyMask,
  1166. NullGrab, 0);
  1167. }
  1168. }
  1169. return deliveries;
  1170. }
  1171. static Bool
  1172. PointInBorderSize(WindowPtr pWin, int x, int y)
  1173. {
  1174. BoxRec box;
  1175. if (POINT_IN_REGION(&pWin->borderSize, x, y, &box))
  1176. return TRUE;
  1177. return FALSE;
  1178. }
  1179. static WindowPtr
  1180. XYToWindow(int x, int y)
  1181. {
  1182. WindowPtr pWin;
  1183. BoxRec box;
  1184. spriteTraceGood = 1; /* root window still there */
  1185. pWin = ROOT->firstChild;
  1186. while (pWin) {
  1187. if ((pWin->mapped) &&
  1188. (x >= pWin->drawable.x - wBorderWidth(pWin)) &&
  1189. (x < pWin->drawable.x + (int) pWin->drawable.width +
  1190. wBorderWidth(pWin)) &&
  1191. (y >= pWin->drawable.y - wBorderWidth(pWin)) &&
  1192. (y < pWin->drawable.y + (int) pWin->drawable.height +
  1193. wBorderWidth(pWin))
  1194. /* When a window is shaped, a further check
  1195. * is made to see if the point is inside
  1196. * borderSize
  1197. */
  1198. && (!wBoundingShape(pWin) || PointInBorderSize(pWin, x, y))
  1199. && (!wInputShape(pWin) ||
  1200. POINT_IN_REGION(
  1201. wInputShape(pWin),
  1202. x - pWin->drawable.x,
  1203. y - pWin->drawable.y, &box))
  1204. ) {
  1205. if (spriteTraceGood >= spriteTraceSize) {
  1206. spriteTraceSize += 10;
  1207. Must_have_memory = TRUE; /* XXX */
  1208. spriteTrace =
  1209. (WindowPtr *) realloc(spriteTrace,
  1210. spriteTraceSize * sizeof(WindowPtr));
  1211. Must_have_memory = FALSE; /* XXX */
  1212. }
  1213. spriteTrace[spriteTraceGood++] = pWin;
  1214. pWin = pWin->firstChild;
  1215. }
  1216. else
  1217. pWin = pWin->nextSib;
  1218. }
  1219. return spriteTrace[spriteTraceGood - 1];
  1220. }
  1221. static Bool
  1222. CheckMotion(xEvent *xE)
  1223. {
  1224. WindowPtr prevSpriteWin = sprite.win;
  1225. if (xE && !syncEvents.playingEvents) {
  1226. if (sprite.hot.pScreen != sprite.hotPhys.pScreen) {
  1227. sprite.hot.pScreen = sprite.hotPhys.pScreen;
  1228. ROOT = WindowTable[sprite.hot.pScreen->myNum];
  1229. }
  1230. sprite.hot.x = XE_KBPTR.rootX;
  1231. sprite.hot.y = XE_KBPTR.rootY;
  1232. if (sprite.hot.x < sprite.physLimits.x1)
  1233. sprite.hot.x = sprite.physLimits.x1;
  1234. else if (sprite.hot.x >= sprite.physLimits.x2)
  1235. sprite.hot.x = sprite.physLimits.x2 - 1;
  1236. if (sprite.hot.y < sprite.physLimits.y1)
  1237. sprite.hot.y = sprite.physLimits.y1;
  1238. else if (sprite.hot.y >= sprite.physLimits.y2)
  1239. sprite.hot.y = sprite.physLimits.y2 - 1;
  1240. if (sprite.hotShape)
  1241. ConfineToShape(sprite.hotShape, &sprite.hot.x, &sprite.hot.y);
  1242. sprite.hotPhys = sprite.hot;
  1243. if ((sprite.hotPhys.x != XE_KBPTR.rootX) ||
  1244. (sprite.hotPhys.y != XE_KBPTR.rootY)) {
  1245. (*sprite.hotPhys.pScreen->SetCursorPosition) (sprite.hotPhys.
  1246. pScreen,
  1247. sprite.hotPhys.x,
  1248. sprite.hotPhys.y,
  1249. FALSE);
  1250. }
  1251. XE_KBPTR.rootX = sprite.hot.x;
  1252. XE_KBPTR.rootY = sprite.hot.y;
  1253. }
  1254. sprite.win = XYToWindow(sprite.hot.x, sprite.hot.y);
  1255. #ifdef notyet
  1256. if (!(sprite.win->deliverableEvents &
  1257. Motion_Filter(inputInfo.pointer->button))
  1258. ! syncEvents.playingEvents) {
  1259. /* XXX Do PointerNonInterestBox here */
  1260. }
  1261. #endif
  1262. if (sprite.win != prevSpriteWin) {
  1263. if (prevSpriteWin != NullWindow) {
  1264. if (!xE)
  1265. UpdateCurrentTimeIf();
  1266. DoEnterLeaveEvents(prevSpriteWin, sprite.win, NotifyNormal);
  1267. }
  1268. PostNewCursor();
  1269. return FALSE;
  1270. }
  1271. return TRUE;
  1272. }
  1273. _X_EXPORT void
  1274. WindowsRestructured()
  1275. {
  1276. (void) CheckMotion((xEvent *) NULL);
  1277. }
  1278. void
  1279. DefineInitialRootWindow(register WindowPtr win)
  1280. {
  1281. ScreenPtr pScreen = win->drawable.pScreen;
  1282. sprite.hotPhys.pScreen = pScreen;
  1283. sprite.hotPhys.x = pScreen->width / 2;
  1284. sprite.hotPhys.y = pScreen->height / 2;
  1285. sprite.hot = sprite.hotPhys;
  1286. sprite.hotLimits.x2 = pScreen->width;
  1287. sprite.hotLimits.y2 = pScreen->height;
  1288. sprite.win = win;
  1289. sprite.current = wCursor(win);
  1290. sprite.current->refcnt++;
  1291. spriteTraceGood = 1;
  1292. ROOT = win;
  1293. (*pScreen->CursorLimits) (pScreen, sprite.current, &sprite.hotLimits,
  1294. &sprite.physLimits);
  1295. sprite.confined = FALSE;
  1296. (*pScreen->ConstrainCursor) (pScreen, &sprite.physLimits);
  1297. (*pScreen->SetCursorPosition) (pScreen, sprite.hot.x, sprite.hot.y, FALSE);
  1298. (*pScreen->DisplayCursor) (pScreen, sprite.current);
  1299. }
  1300. /*
  1301. * This does not take any shortcuts, and even ignores its argument, since
  1302. * it does not happen very often, and one has to walk up the tree since
  1303. * this might be a newly instantiated cursor for an intermediate window
  1304. * between the one the pointer is in and the one that the last cursor was
  1305. * instantiated from.
  1306. */
  1307. void
  1308. WindowHasNewCursor(WindowPtr pWin)
  1309. {
  1310. PostNewCursor();
  1311. }
  1312. _X_EXPORT void
  1313. NewCurrentScreen(ScreenPtr newScreen, int x, int y)
  1314. {
  1315. sprite.hotPhys.x = x;
  1316. sprite.hotPhys.y = y;
  1317. if (newScreen != sprite.hotPhys.pScreen)
  1318. ConfineCursorToWindow(WindowTable[newScreen->myNum], TRUE, FALSE);
  1319. }
  1320. int
  1321. ProcWarpPointer(ClientPtr client)
  1322. {
  1323. WindowPtr dest = NULL;
  1324. int x, y;
  1325. ScreenPtr newScreen;
  1326. REQUEST(xWarpPointerReq);
  1327. REQUEST_SIZE_MATCH(xWarpPointerReq);
  1328. if (stuff->dstWid != None) {
  1329. dest = SecurityLookupWindow(stuff->dstWid, client, SecurityReadAccess);
  1330. if (!dest)
  1331. return BadWindow;
  1332. }
  1333. x = sprite.hotPhys.x;
  1334. y = sprite.hotPhys.y;
  1335. if (stuff->srcWid != None) {
  1336. int winX, winY;
  1337. XID winID = stuff->srcWid;
  1338. WindowPtr source;
  1339. source = SecurityLookupWindow(winID, client, SecurityReadAccess);
  1340. if (!source)
  1341. return BadWindow;
  1342. winX = source->drawable.x;
  1343. winY = source->drawable.y;
  1344. if (source->drawable.pScreen != sprite.hotPhys.pScreen ||
  1345. x < winX + stuff->srcX ||
  1346. y < winY + stuff->srcY ||
  1347. (stuff->srcWidth != 0 &&
  1348. winX + stuff->srcX + (int) stuff->srcWidth < x) ||
  1349. (stuff->srcHeight != 0 &&
  1350. winY + stuff->srcY + (int) stuff->srcHeight < y) ||
  1351. !PointInWindowIsVisible(source, x, y))
  1352. return Success;
  1353. }
  1354. if (dest) {
  1355. x = dest->drawable.x;
  1356. y = dest->drawable.y;
  1357. newScreen = dest->drawable.pScreen;
  1358. }
  1359. else
  1360. newScreen = sprite.hotPhys.pScreen;
  1361. x += stuff->dstX;
  1362. y += stuff->dstY;
  1363. if (x < 0)
  1364. x = 0;
  1365. else if (x >= newScreen->width)
  1366. x = newScreen->width - 1;
  1367. if (y < 0)
  1368. y = 0;
  1369. else if (y >= newScreen->height)
  1370. y = newScreen->height - 1;
  1371. if (newScreen == sprite.hotPhys.pScreen) {
  1372. if (x < sprite.physLimits.x1)
  1373. x = sprite.physLimits.x1;
  1374. else if (x >= sprite.physLimits.x2)
  1375. x = sprite.physLimits.x2 - 1;
  1376. if (y < sprite.physLimits.y1)
  1377. y = sprite.physLimits.y1;
  1378. else if (y >= sprite.physLimits.y2)
  1379. y = sprite.physLimits.y2 - 1;
  1380. if (sprite.hotShape)
  1381. ConfineToShape(sprite.hotShape, &x, &y);
  1382. (*newScreen->SetCursorPosition) (newScreen, x, y, TRUE);
  1383. }
  1384. else if (!PointerConfinedToScreen()) {
  1385. NewCurrentScreen(newScreen, x, y);
  1386. }
  1387. return Success;
  1388. }
  1389. static Bool
  1390. BorderSizeNotEmpty(WindowPtr pWin)
  1391. {
  1392. if (REGION_NOTEMPTY(&pWin->borderSize))
  1393. return TRUE;
  1394. return FALSE;
  1395. }
  1396. /* "CheckPassiveGrabsOnWindow" checks to see if the event passed in causes a
  1397. passive grab set on the window to be activated. */
  1398. static Bool
  1399. CheckPassiveGrabsOnWindow(WindowPtr pWin,
  1400. register DeviceIntPtr device,
  1401. register xEvent *xE, int count)
  1402. {
  1403. GrabPtr grab = wPassiveGrabs(pWin);
  1404. GrabRec tempGrab;
  1405. xEvent *dxE;
  1406. if (!grab)
  1407. return FALSE;
  1408. tempGrab.window = pWin;
  1409. tempGrab.device = device;
  1410. tempGrab.type = xE->u.u.type;
  1411. tempGrab.detail.exact = xE->u.u.detail;
  1412. tempGrab.detail.pMask = NULL;
  1413. tempGrab.modifiersDetail.pMask = NULL;
  1414. for (; grab; grab = grab->next) {
  1415. tempGrab.modifierDevice = grab->modifierDevice;
  1416. if ((device == grab->modifierDevice) && ((xE->u.u.type == KeyPress)
  1417. ))
  1418. tempGrab.modifiersDetail.exact =
  1419. grab->modifierDevice->key->prev_state;
  1420. else
  1421. tempGrab.modifiersDetail.exact =
  1422. grab->modifierDevice->key->state;
  1423. if (GrabMatchesSecond(&tempGrab, grab) &&
  1424. (!grab->confineTo ||
  1425. (grab->confineTo->realized &&
  1426. BorderSizeNotEmpty(grab->confineTo)))) {
  1427. (*device->ActivateGrab) (device, grab, currentTime, TRUE);
  1428. FixUpEventFromWindow(xE, grab->window, None, TRUE);
  1429. (void) TryClientEvents(rClient(grab), xE, count,
  1430. filters[xE->u.u.type],
  1431. filters[xE->u.u.type], grab);
  1432. if (device->sync.state == FROZEN_NO_EVENT) {
  1433. if (device->sync.evcount < count) {
  1434. Must_have_memory = TRUE; /* XXX */
  1435. device->sync.event = (xEvent *) realloc(device->sync.event,
  1436. count *
  1437. sizeof(xEvent));
  1438. Must_have_memory = FALSE; /* XXX */
  1439. }
  1440. device->sync.evcount = count;
  1441. for (dxE = device->sync.event; --count >= 0; dxE++, xE++)
  1442. *dxE = *xE;
  1443. device->sync.state = FROZEN_WITH_EVENT;
  1444. }
  1445. return TRUE;
  1446. }
  1447. }
  1448. return FALSE;
  1449. }
  1450. /**
  1451. "CheckDeviceGrabs" handles both keyboard and pointer events that may cause
  1452. a passive grab to be activated. If the event is a keyboard event, the
  1453. ancestors of the focus window are traced down and tried to see if they have
  1454. any passive grabs to be activated. If the focus window itself is reached and
  1455. it's descendants contain they pointer, the ancestors of the window that the
  1456. pointer is in are then traced down starting at the focus window, otherwise no
  1457. grabs are activated. If the event is a pointer event, the ancestors of the
  1458. window that the pointer is in are traced down starting at the root until
  1459. CheckPassiveGrabs causes a passive grab to activate or all the windows are
  1460. tried. PRH
  1461. */
  1462. Bool
  1463. CheckDeviceGrabs(register DeviceIntPtr device, register xEvent *xE,
  1464. int checkFirst, int count)
  1465. {
  1466. int i;
  1467. WindowPtr pWin = NULL;
  1468. FocusClassPtr focus = device->focus;
  1469. if (((xE->u.u.type == ButtonPress)
  1470. ) && (device->button->buttonsDown != 1))
  1471. return FALSE;
  1472. i = checkFirst;
  1473. if (focus) {
  1474. for (; i < focus->traceGood; i++) {
  1475. pWin = focus->trace[i];
  1476. if (pWin->optional &&
  1477. CheckPassiveGrabsOnWindow(pWin, device, xE, count))
  1478. return TRUE;
  1479. }
  1480. if ((focus->win == NoneWin) ||
  1481. (i >= spriteTraceGood) ||
  1482. ((i > checkFirst) && (pWin != spriteTrace[i - 1])))
  1483. return FALSE;
  1484. }
  1485. for (; i < spriteTraceGood; i++) {
  1486. pWin = spriteTrace[i];
  1487. if (pWin->optional &&
  1488. CheckPassiveGrabsOnWindow(pWin, device, xE, count))
  1489. return TRUE;
  1490. }
  1491. return FALSE;
  1492. }
  1493. void
  1494. DeliverFocusedEvent(DeviceIntPtr keybd, xEvent *xE, WindowPtr window, int count)
  1495. {
  1496. WindowPtr focus = keybd->focus->win;
  1497. int mskidx = 0;
  1498. if (focus == FollowKeyboardWin)
  1499. focus = inputInfo.keyboard->focus->win;
  1500. if (!focus)
  1501. return;
  1502. if (focus == PointerRootWin) {
  1503. DeliverDeviceEvents(window, xE, NullGrab, NullWindow, keybd, count);
  1504. return;
  1505. }
  1506. if ((focus == window) || IsParent(focus, window)) {
  1507. if (DeliverDeviceEvents(window, xE, NullGrab, focus, keybd, count))
  1508. return;
  1509. }
  1510. /* just deliver it to the focus window */
  1511. FixUpEventFromWindow(xE, focus, None, FALSE);
  1512. if (xE->u.u.type & EXTENSION_EVENT_BASE)
  1513. mskidx = keybd->id;
  1514. (void) DeliverEventsToWindow(focus, xE, count, filters[xE->u.u.type],
  1515. NullGrab, mskidx);
  1516. }
  1517. void
  1518. DeliverGrabbedEvent(register xEvent *xE, register DeviceIntPtr thisDev,
  1519. Bool deactivateGrab, int count)
  1520. {
  1521. GrabPtr grab = thisDev->grab;
  1522. int deliveries = 0;
  1523. DeviceIntPtr dev;
  1524. xEvent *dxE;
  1525. if (grab->ownerEvents) {
  1526. WindowPtr focus;
  1527. if (thisDev->focus) {
  1528. focus = thisDev->focus->win;
  1529. if (focus == FollowKeyboardWin)
  1530. focus = inputInfo.keyboard->focus->win;
  1531. }
  1532. else
  1533. focus = PointerRootWin;
  1534. if (focus == PointerRootWin)
  1535. deliveries = DeliverDeviceEvents(sprite.win, xE, grab, NullWindow,
  1536. thisDev, count);
  1537. else if (focus && (focus == sprite.win || IsParent(focus, sprite.win)))
  1538. deliveries = DeliverDeviceEvents(sprite.win, xE, grab, focus,
  1539. thisDev, count);
  1540. else if (focus)
  1541. deliveries = DeliverDeviceEvents(focus, xE, grab, focus,
  1542. thisDev, count);
  1543. }
  1544. if (!deliveries) {
  1545. FixUpEventFromWindow(xE, grab->window, None, TRUE);
  1546. deliveries = TryClientEvents(rClient(grab), xE, count,
  1547. (Mask) grab->eventMask,
  1548. filters[xE->u.u.type], grab);
  1549. if (deliveries && (xE->u.u.type == MotionNotify
  1550. ))
  1551. thisDev->valuator->motionHintWindow = grab->window;
  1552. }
  1553. if (deliveries && !deactivateGrab && (xE->u.u.type != MotionNotify
  1554. ))
  1555. switch (thisDev->sync.state) {
  1556. case FREEZE_BOTH_NEXT_EVENT:
  1557. for (dev = inputInfo.devices; dev; dev = dev->next) {
  1558. if (dev == thisDev)
  1559. continue;
  1560. FreezeThaw(dev, TRUE);
  1561. if ((dev->sync.state == FREEZE_BOTH_NEXT_EVENT) &&
  1562. (CLIENT_BITS(dev->grab->resource) ==
  1563. CLIENT_BITS(thisDev->grab->resource)))
  1564. dev->sync.state = FROZEN_NO_EVENT;
  1565. else
  1566. dev->sync.other = thisDev->grab;
  1567. }
  1568. /* fall through */
  1569. case FREEZE_NEXT_EVENT:
  1570. thisDev->sync.state = FROZEN_WITH_EVENT;
  1571. FreezeThaw(thisDev, TRUE);
  1572. if (thisDev->sync.evcount < count) {
  1573. Must_have_memory = TRUE; /* XXX */
  1574. thisDev->sync.event = (xEvent *) realloc(thisDev->sync.event,
  1575. count *
  1576. sizeof(xEvent));
  1577. Must_have_memory = FALSE; /* XXX */
  1578. }
  1579. thisDev->sync.evcount = count;
  1580. for (dxE = thisDev->sync.event; --count >= 0; dxE++, xE++)
  1581. *dxE = *xE;
  1582. break;
  1583. }
  1584. }
  1585. void
  1586. ProcessKeyboardEvent(register xEvent *xE, register DeviceIntPtr keybd,
  1587. int count)
  1588. {
  1589. int key, bit;
  1590. BYTE *kptr;
  1591. int i;
  1592. CARD8 modifiers;
  1593. CARD16 mask;
  1594. GrabPtr grab = keybd->grab;
  1595. Bool deactivateGrab = FALSE;
  1596. KeyClassPtr keyc = keybd->key;
  1597. if (!syncEvents.playingEvents) {
  1598. NoticeTime(xE);
  1599. if (DeviceEventCallback) {
  1600. DeviceEventInfoRec eventinfo;
  1601. eventinfo.events = xE;
  1602. eventinfo.count = count;
  1603. CallCallbacks(&DeviceEventCallback, (pointer) &eventinfo);
  1604. }
  1605. }
  1606. XE_KBPTR.state = (keyc->state | inputInfo.pointer->button->state);
  1607. XE_KBPTR.rootX = sprite.hot.x;
  1608. XE_KBPTR.rootY = sprite.hot.y;
  1609. key = xE->u.u.detail;
  1610. kptr = &keyc->down[key >> 3];
  1611. bit = 1 << (key & 7);
  1612. modifiers = keyc->modifierMap[key];
  1613. #ifdef DEBUG
  1614. if ((xkbDebugFlags & 0x4) &&
  1615. ((xE->u.u.type == KeyPress) || (xE->u.u.type == KeyRelease))) {
  1616. ErrorF("CoreProcessKbdEvent: Key %d %s\n", key,
  1617. (xE->u.u.type == KeyPress ? "down" : "up"));
  1618. }
  1619. #endif
  1620. switch (xE->u.u.type) {
  1621. case KeyPress:
  1622. if (*kptr & bit) { /* allow ddx to generate multiple downs */
  1623. if (!modifiers) {
  1624. xE->u.u.type = KeyRelease;
  1625. (*keybd->public.processInputProc) (xE, keybd, count);
  1626. xE->u.u.type = KeyPress;
  1627. /* release can have side effects, don't fall through */
  1628. (*keybd->public.processInputProc) (xE, keybd, count);
  1629. }
  1630. return;
  1631. }
  1632. inputInfo.pointer->valuator->motionHintWindow = NullWindow;
  1633. *kptr |= bit;
  1634. keyc->prev_state = keyc->state;
  1635. for (i = 0, mask = 1; modifiers; i++, mask <<= 1) {
  1636. if (mask & modifiers) {
  1637. /* This key affects modifier "i" */
  1638. keyc->modifierKeyCount[i]++;
  1639. keyc->state |= mask;
  1640. modifiers &= ~mask;
  1641. }
  1642. }
  1643. if (!grab && CheckDeviceGrabs(keybd, xE, 0, count)) {
  1644. keybd->activatingKey = key;
  1645. return;
  1646. }
  1647. break;
  1648. case KeyRelease:
  1649. if (!(*kptr & bit)) /* guard against duplicates */
  1650. return;
  1651. inputInfo.pointer->valuator->motionHintWindow = NullWindow;
  1652. *kptr &= ~bit;
  1653. keyc->prev_state = keyc->state;
  1654. for (i = 0, mask = 1; modifiers; i++, mask <<= 1) {
  1655. if (mask & modifiers) {
  1656. /* This key affects modifier "i" */
  1657. if (--keyc->modifierKeyCount[i] <= 0) {
  1658. keyc->state &= ~mask;
  1659. keyc->modifierKeyCount[i] = 0;
  1660. }
  1661. modifiers &= ~mask;
  1662. }
  1663. }
  1664. if (keybd->fromPassiveGrab && (key == keybd->activatingKey))
  1665. deactivateGrab = TRUE;
  1666. break;
  1667. default:
  1668. FatalError("Impossible keyboard event");
  1669. }
  1670. if (grab)
  1671. DeliverGrabbedEvent(xE, keybd, deactivateGrab, count);
  1672. else
  1673. DeliverFocusedEvent(keybd, xE, sprite.win, count);
  1674. if (deactivateGrab)
  1675. (*keybd->DeactivateGrab) (keybd);
  1676. }
  1677. void
  1678. ProcessPointerEvent(register xEvent *xE, register DeviceIntPtr mouse, int count)
  1679. {
  1680. GrabPtr grab = mouse->grab;
  1681. Bool deactivateGrab = FALSE;
  1682. ButtonClassPtr butc = mouse->button;
  1683. if (!syncEvents.playingEvents)
  1684. NoticeTime(xE)
  1685. XE_KBPTR.state = (butc->state | (
  1686. inputInfo.keyboard->key->state
  1687. ));
  1688. {
  1689. NoticeTime(xE);
  1690. if (DeviceEventCallback) {
  1691. DeviceEventInfoRec eventinfo;
  1692. /* see comment in EnqueueEvents regarding the next three lines */
  1693. if (xE->u.u.type == MotionNotify)
  1694. XE_KBPTR.root =
  1695. WindowTable[sprite.hotPhys.pScreen->myNum]->drawable.id;
  1696. eventinfo.events = xE;
  1697. eventinfo.count = count;
  1698. CallCallbacks(&DeviceEventCallback, (pointer) &eventinfo);
  1699. }
  1700. }
  1701. if (xE->u.u.type != MotionNotify) {
  1702. int key;
  1703. BYTE *kptr;
  1704. int bit;
  1705. XE_KBPTR.rootX = sprite.hot.x;
  1706. XE_KBPTR.rootY = sprite.hot.y;
  1707. key = xE->u.u.detail;
  1708. kptr = &butc->down[key >> 3];
  1709. bit = 1 << (key & 7);
  1710. switch (xE->u.u.type) {
  1711. case ButtonPress:
  1712. mouse->valuator->motionHintWindow = NullWindow;
  1713. if (!(*kptr & bit))
  1714. butc->buttonsDown++;
  1715. butc->motionMask = ButtonMotionMask;
  1716. *kptr |= bit;
  1717. if (xE->u.u.detail == 0)
  1718. return;
  1719. if (xE->u.u.detail <= 5)
  1720. butc->state |= (Button1Mask >> 1) << xE->u.u.detail;
  1721. filters[MotionNotify] = Motion_Filter(butc);
  1722. if (!grab)
  1723. if (CheckDeviceGrabs(mouse, xE, 0, count))
  1724. return;
  1725. break;
  1726. case ButtonRelease:
  1727. mouse->valuator->motionHintWindow = NullWindow;
  1728. if (*kptr & bit)
  1729. --butc->buttonsDown;
  1730. if (!butc->buttonsDown)
  1731. butc->motionMask = 0;
  1732. *kptr &= ~bit;
  1733. if (xE->u.u.detail == 0)
  1734. return;
  1735. if (xE->u.u.detail <= 5)
  1736. butc->state &= ~((Button1Mask >> 1) << xE->u.u.detail);
  1737. filters[MotionNotify] = Motion_Filter(butc);
  1738. if (!butc->state && mouse->fromPassiveGrab)
  1739. deactivateGrab = TRUE;
  1740. break;
  1741. default:
  1742. FatalError("bogus pointer event from ddx");
  1743. }
  1744. }
  1745. else if (!CheckMotion(xE))
  1746. return;
  1747. if (grab)
  1748. DeliverGrabbedEvent(xE, mouse, deactivateGrab, count);
  1749. else
  1750. DeliverDeviceEvents(sprite.win, xE, NullGrab, NullWindow, mouse, count);
  1751. if (deactivateGrab)
  1752. (*mouse->DeactivateGrab) (mouse);
  1753. }
  1754. #define AtMostOneClient \
  1755. (SubstructureRedirectMask | ResizeRedirectMask | ButtonPressMask)
  1756. void
  1757. RecalculateDeliverableEvents(pWin)
  1758. WindowPtr pWin;
  1759. {
  1760. OtherClients *others;
  1761. WindowPtr pChild;
  1762. pChild = pWin;
  1763. while (1) {
  1764. if (pChild->optional) {
  1765. pChild->optional->otherEventMasks = 0;
  1766. for (others = wOtherClients(pChild); others; others = others->next) {
  1767. pChild->optional->otherEventMasks |= others->mask;
  1768. }
  1769. }
  1770. pChild->deliverableEvents = pChild->eventMask |
  1771. wOtherEventMasks(pChild);
  1772. if (pChild->parent)
  1773. pChild->deliverableEvents |=
  1774. (pChild->parent->deliverableEvents &
  1775. ~wDontPropagateMask(pChild) & PropagateMask);
  1776. if (pChild->firstChild) {
  1777. pChild = pChild->firstChild;
  1778. continue;
  1779. }
  1780. while (!pChild->nextSib && (pChild != pWin))
  1781. pChild = pChild->parent;
  1782. if (pChild == pWin)
  1783. break;
  1784. pChild = pChild->nextSib;
  1785. }
  1786. }
  1787. /**
  1788. *
  1789. * \param value must conform to DeleteType
  1790. */
  1791. int
  1792. OtherClientGone(pointer value, XID id)
  1793. {
  1794. OtherClientsPtr other, prev;
  1795. WindowPtr pWin = (WindowPtr) value;
  1796. prev = 0;
  1797. for (other = wOtherClients(pWin); other; other = other->next) {
  1798. if (other->resource == id) {
  1799. if (prev)
  1800. prev->next = other->next;
  1801. else {
  1802. if (!(pWin->optional->otherClients = other->next))
  1803. CheckWindowOptionalNeed(pWin);
  1804. }
  1805. free(other);
  1806. RecalculateDeliverableEvents(pWin);
  1807. return (Success);
  1808. }
  1809. prev = other;
  1810. }
  1811. FatalError("client not on event list");
  1812. /*NOTREACHED*/ return -1; /* make compiler happy */
  1813. }
  1814. int
  1815. EventSelectForWindow(register WindowPtr pWin, register ClientPtr client,
  1816. Mask mask)
  1817. {
  1818. Mask check;
  1819. OtherClients *others;
  1820. if (mask & ~AllEventMasks) {
  1821. client->errorValue = mask;
  1822. return BadValue;
  1823. }
  1824. check = (mask & AtMostOneClient);
  1825. if (check & (pWin->eventMask | wOtherEventMasks(pWin))) { /* It is illegal for two different
  1826. clients to select on any of the
  1827. events for AtMostOneClient. However,
  1828. it is OK, for some client to
  1829. continue selecting on one of those
  1830. events. */
  1831. if ((wClient(pWin) != client) && (check & pWin->eventMask))
  1832. return BadAccess;
  1833. for (others = wOtherClients(pWin); others; others = others->next) {
  1834. if (!SameClient(others, client) && (check & others->mask))
  1835. return BadAccess;
  1836. }
  1837. }
  1838. if (wClient(pWin) == client) {
  1839. check = pWin->eventMask;
  1840. pWin->eventMask = mask;
  1841. }
  1842. else {
  1843. for (others = wOtherClients(pWin); others; others = others->next) {
  1844. if (SameClient(others, client)) {
  1845. check = others->mask;
  1846. if (mask == 0) {
  1847. FreeResource(others->resource, RT_NONE);
  1848. return Success;
  1849. }
  1850. else
  1851. others->mask = mask;
  1852. goto maskSet;
  1853. }
  1854. }
  1855. check = 0;
  1856. if (!pWin->optional && !MakeWindowOptional(pWin))
  1857. return BadAlloc;
  1858. others = malloc(sizeof(OtherClients));
  1859. if (!others)
  1860. return BadAlloc;
  1861. others->mask = mask;
  1862. others->resource = FakeClientID(client->index);
  1863. others->next = pWin->optional->otherClients;
  1864. pWin->optional->otherClients = others;
  1865. if (!AddResource(others->resource, RT_OTHERCLIENT, (pointer) pWin))
  1866. return BadAlloc;
  1867. }
  1868. maskSet:
  1869. if ((inputInfo.pointer->valuator->motionHintWindow == pWin) &&
  1870. (mask & PointerMotionHintMask) &&
  1871. !(check & PointerMotionHintMask) && !inputInfo.pointer->grab)
  1872. inputInfo.pointer->valuator->motionHintWindow = NullWindow;
  1873. RecalculateDeliverableEvents(pWin);
  1874. return Success;
  1875. }
  1876. int
  1877. EventSuppressForWindow(register WindowPtr pWin, register ClientPtr client,
  1878. Mask mask, Bool *checkOptional)
  1879. {
  1880. int i, free;
  1881. if (mask & ~PropagateMask) {
  1882. client->errorValue = mask;
  1883. return BadValue;
  1884. }
  1885. if (pWin->dontPropagate)
  1886. DontPropagateRefCnts[pWin->dontPropagate]--;
  1887. if (!mask)
  1888. i = 0;
  1889. else {
  1890. for (i = DNPMCOUNT, free = 0; --i > 0;) {
  1891. if (!DontPropagateRefCnts[i])
  1892. free = i;
  1893. else if (mask == DontPropagateMasks[i])
  1894. break;
  1895. }
  1896. if (!i && free) {
  1897. i = free;
  1898. DontPropagateMasks[i] = mask;
  1899. }
  1900. }
  1901. if (i || !mask) {
  1902. pWin->dontPropagate = i;
  1903. if (i)
  1904. DontPropagateRefCnts[i]++;
  1905. if (pWin->optional) {
  1906. pWin->optional->dontPropagateMask = mask;
  1907. *checkOptional = TRUE;
  1908. }
  1909. }
  1910. else {
  1911. if (!pWin->optional && !MakeWindowOptional(pWin)) {
  1912. if (pWin->dontPropagate)
  1913. DontPropagateRefCnts[pWin->dontPropagate]++;
  1914. return BadAlloc;
  1915. }
  1916. pWin->dontPropagate = 0;
  1917. pWin->optional->dontPropagateMask = mask;
  1918. }
  1919. RecalculateDeliverableEvents(pWin);
  1920. return Success;
  1921. }
  1922. static WindowPtr
  1923. CommonAncestor(register WindowPtr a, register WindowPtr b)
  1924. {
  1925. for (b = b->parent; b; b = b->parent)
  1926. if (IsParent(b, a))
  1927. return b;
  1928. return NullWindow;
  1929. }
  1930. static void
  1931. EnterLeaveEvent(int type,
  1932. int mode, int detail, register WindowPtr pWin, Window child)
  1933. {
  1934. xEvent event;
  1935. DeviceIntPtr keybd = inputInfo.keyboard;
  1936. WindowPtr focus;
  1937. DeviceIntPtr mouse = inputInfo.pointer;
  1938. GrabPtr grab = mouse->grab;
  1939. Mask mask;
  1940. if ((pWin == mouse->valuator->motionHintWindow) &&
  1941. (detail != NotifyInferior))
  1942. mouse->valuator->motionHintWindow = NullWindow;
  1943. if (grab) {
  1944. mask = (pWin == grab->window) ? grab->eventMask : 0;
  1945. if (grab->ownerEvents)
  1946. mask |= EventMaskForClient(pWin, rClient(grab));
  1947. }
  1948. else {
  1949. mask = pWin->eventMask | wOtherEventMasks(pWin);
  1950. }
  1951. if (mask & filters[type]) {
  1952. event.u.u.type = type;
  1953. event.u.u.detail = detail;
  1954. event.u.enterLeave.time = currentTime.milliseconds;
  1955. event.u.enterLeave.rootX = sprite.hot.x;
  1956. event.u.enterLeave.rootY = sprite.hot.y;
  1957. /* Counts on the same initial structure of crossing & button events! */
  1958. FixUpEventFromWindow(&event, pWin, None, FALSE);
  1959. /* Enter/Leave events always set child */
  1960. event.u.enterLeave.child = child;
  1961. event.u.enterLeave.flags = event.u.keyButtonPointer.sameScreen ?
  1962. ELFlagSameScreen : 0;
  1963. event.u.enterLeave.state = keybd->key->state | mouse->button->state;
  1964. event.u.enterLeave.mode = mode;
  1965. focus = keybd->focus->win;
  1966. if ((focus != NoneWin) &&
  1967. ((pWin == focus) || (focus == PointerRootWin) ||
  1968. IsParent(focus, pWin)))
  1969. event.u.enterLeave.flags |= ELFlagFocus;
  1970. if (grab)
  1971. (void) TryClientEvents(rClient(grab), &event, 1, mask,
  1972. filters[type], grab);
  1973. else
  1974. (void) DeliverEventsToWindow(pWin, &event, 1, filters[type],
  1975. NullGrab, 0);
  1976. }
  1977. if ((type == EnterNotify) && (mask & KeymapStateMask)) {
  1978. xKeymapEvent ke;
  1979. memmove((char *) &ke.map[0], (char *) &keybd->key->down[1], 31);
  1980. ke.type = KeymapNotify;
  1981. if (grab)
  1982. (void) TryClientEvents(rClient(grab), (xEvent *) &ke, 1, mask,
  1983. KeymapStateMask, grab);
  1984. else
  1985. (void) DeliverEventsToWindow(pWin, (xEvent *) &ke, 1,
  1986. KeymapStateMask, NullGrab, 0);
  1987. }
  1988. }
  1989. static void
  1990. EnterNotifies(WindowPtr ancestor, WindowPtr child, int mode, int detail)
  1991. {
  1992. WindowPtr parent = child->parent;
  1993. if (ancestor == parent)
  1994. return;
  1995. EnterNotifies(ancestor, parent, mode, detail);
  1996. EnterLeaveEvent(EnterNotify, mode, detail, parent, child->drawable.id);
  1997. }
  1998. static void
  1999. LeaveNotifies(WindowPtr child, WindowPtr ancestor, int mode, int detail)
  2000. {
  2001. WindowPtr pWin;
  2002. if (ancestor == child)
  2003. return;
  2004. for (pWin = child->parent; pWin != ancestor; pWin = pWin->parent) {
  2005. EnterLeaveEvent(LeaveNotify, mode, detail, pWin, child->drawable.id);
  2006. child = pWin;
  2007. }
  2008. }
  2009. static void
  2010. DoEnterLeaveEvents(WindowPtr fromWin, WindowPtr toWin, int mode)
  2011. {
  2012. if (fromWin == toWin)
  2013. return;
  2014. if (IsParent(fromWin, toWin)) {
  2015. EnterLeaveEvent(LeaveNotify, mode, NotifyInferior, fromWin, None);
  2016. EnterNotifies(fromWin, toWin, mode, NotifyVirtual);
  2017. EnterLeaveEvent(EnterNotify, mode, NotifyAncestor, toWin, None);
  2018. }
  2019. else if (IsParent(toWin, fromWin)) {
  2020. EnterLeaveEvent(LeaveNotify, mode, NotifyAncestor, fromWin, None);
  2021. LeaveNotifies(fromWin, toWin, mode, NotifyVirtual);
  2022. EnterLeaveEvent(EnterNotify, mode, NotifyInferior, toWin, None);
  2023. }
  2024. else { /* neither fromWin nor toWin is descendent of the other */
  2025. WindowPtr common = CommonAncestor(toWin, fromWin);
  2026. /* common == NullWindow ==> different screens */
  2027. EnterLeaveEvent(LeaveNotify, mode, NotifyNonlinear, fromWin, None);
  2028. LeaveNotifies(fromWin, common, mode, NotifyNonlinearVirtual);
  2029. EnterNotifies(common, toWin, mode, NotifyNonlinearVirtual);
  2030. EnterLeaveEvent(EnterNotify, mode, NotifyNonlinear, toWin, None);
  2031. }
  2032. }
  2033. static void
  2034. FocusEvent(DeviceIntPtr dev, int type, int mode, int detail,
  2035. register WindowPtr pWin)
  2036. {
  2037. xEvent event;
  2038. event.u.focus.mode = mode;
  2039. event.u.u.type = type;
  2040. event.u.u.detail = detail;
  2041. event.u.focus.window = pWin->drawable.id;
  2042. (void) DeliverEventsToWindow(pWin, &event, 1, filters[type], NullGrab, 0);
  2043. if ((type == FocusIn) &&
  2044. ((pWin->eventMask | wOtherEventMasks(pWin)) & KeymapStateMask)) {
  2045. xKeymapEvent ke;
  2046. memmove((char *) &ke.map[0], (char *) &dev->key->down[1], 31);
  2047. ke.type = KeymapNotify;
  2048. (void) DeliverEventsToWindow(pWin, (xEvent *) &ke, 1,
  2049. KeymapStateMask, NullGrab, 0);
  2050. }
  2051. }
  2052. /*
  2053. * recursive because it is easier
  2054. * no-op if child not descended from ancestor
  2055. */
  2056. static Bool
  2057. FocusInEvents(DeviceIntPtr dev,
  2058. WindowPtr ancestor, WindowPtr child, WindowPtr skipChild,
  2059. int mode, int detail, Bool doAncestor)
  2060. {
  2061. if (child == NullWindow)
  2062. return ancestor == NullWindow;
  2063. if (ancestor == child) {
  2064. if (doAncestor)
  2065. FocusEvent(dev, FocusIn, mode, detail, child);
  2066. return TRUE;
  2067. }
  2068. if (FocusInEvents(dev, ancestor, child->parent, skipChild, mode, detail,
  2069. doAncestor)) {
  2070. if (child != skipChild)
  2071. FocusEvent(dev, FocusIn, mode, detail, child);
  2072. return TRUE;
  2073. }
  2074. return FALSE;
  2075. }
  2076. /* dies horribly if ancestor is not an ancestor of child */
  2077. static void
  2078. FocusOutEvents(DeviceIntPtr dev,
  2079. WindowPtr child, WindowPtr ancestor,
  2080. int mode, int detail, Bool doAncestor)
  2081. {
  2082. WindowPtr pWin;
  2083. for (pWin = child; pWin != ancestor; pWin = pWin->parent)
  2084. FocusEvent(dev, FocusOut, mode, detail, pWin);
  2085. if (doAncestor)
  2086. FocusEvent(dev, FocusOut, mode, detail, ancestor);
  2087. }
  2088. void
  2089. DoFocusEvents(DeviceIntPtr dev, WindowPtr fromWin, WindowPtr toWin, int mode)
  2090. {
  2091. int out, in; /* for holding details for to/from
  2092. PointerRoot/None */
  2093. int i;
  2094. if (fromWin == toWin)
  2095. return;
  2096. out = (fromWin == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
  2097. in = (toWin == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
  2098. /* wrong values if neither, but then not referenced */
  2099. if ((toWin == NullWindow) || (toWin == PointerRootWin)) {
  2100. if ((fromWin == NullWindow) || (fromWin == PointerRootWin)) {
  2101. if (fromWin == PointerRootWin)
  2102. FocusOutEvents(dev, sprite.win, ROOT, mode, NotifyPointer,
  2103. TRUE);
  2104. /* Notify all the roots */
  2105. for (i = 0; i < screenInfo.numScreens; i++)
  2106. FocusEvent(dev, FocusOut, mode, out, WindowTable[i]);
  2107. }
  2108. else {
  2109. if (IsParent(fromWin, sprite.win))
  2110. FocusOutEvents(dev, sprite.win, fromWin, mode, NotifyPointer,
  2111. FALSE);
  2112. FocusEvent(dev, FocusOut, mode, NotifyNonlinear, fromWin);
  2113. /* next call catches the root too, if the screen changed */
  2114. FocusOutEvents(dev, fromWin->parent, NullWindow, mode,
  2115. NotifyNonlinearVirtual, FALSE);
  2116. }
  2117. /* Notify all the roots */
  2118. for (i = 0; i < screenInfo.numScreens; i++)
  2119. FocusEvent(dev, FocusIn, mode, in, WindowTable[i]);
  2120. if (toWin == PointerRootWin)
  2121. (void) FocusInEvents(dev, ROOT, sprite.win, NullWindow, mode,
  2122. NotifyPointer, TRUE);
  2123. }
  2124. else {
  2125. if ((fromWin == NullWindow) || (fromWin == PointerRootWin)) {
  2126. if (fromWin == PointerRootWin)
  2127. FocusOutEvents(dev, sprite.win, ROOT, mode, NotifyPointer,
  2128. TRUE);
  2129. for (i = 0; i < screenInfo.numScreens; i++)
  2130. FocusEvent(dev, FocusOut, mode, out, WindowTable[i]);
  2131. if (toWin->parent != NullWindow)
  2132. (void) FocusInEvents(dev, ROOT, toWin, toWin, mode,
  2133. NotifyNonlinearVirtual, TRUE);
  2134. FocusEvent(dev, FocusIn, mode, NotifyNonlinear, toWin);
  2135. if (IsParent(toWin, sprite.win))
  2136. (void) FocusInEvents(dev, toWin, sprite.win, NullWindow, mode,
  2137. NotifyPointer, FALSE);
  2138. }
  2139. else {
  2140. if (IsParent(toWin, fromWin)) {
  2141. FocusEvent(dev, FocusOut, mode, NotifyAncestor, fromWin);
  2142. FocusOutEvents(dev, fromWin->parent, toWin, mode,
  2143. NotifyVirtual, FALSE);
  2144. FocusEvent(dev, FocusIn, mode, NotifyInferior, toWin);
  2145. if ((IsParent(toWin, sprite.win)) &&
  2146. (sprite.win != fromWin) &&
  2147. (!IsParent(fromWin, sprite.win)) &&
  2148. (!IsParent(sprite.win, fromWin)))
  2149. (void) FocusInEvents(dev, toWin, sprite.win, NullWindow,
  2150. mode, NotifyPointer, FALSE);
  2151. }
  2152. else if (IsParent(fromWin, toWin)) {
  2153. if ((IsParent(fromWin, sprite.win)) &&
  2154. (sprite.win != fromWin) &&
  2155. (!IsParent(toWin, sprite.win)) &&
  2156. (!IsParent(sprite.win, toWin)))
  2157. FocusOutEvents(dev, sprite.win, fromWin, mode,
  2158. NotifyPointer, FALSE);
  2159. FocusEvent(dev, FocusOut, mode, NotifyInferior, fromWin);
  2160. (void) FocusInEvents(dev, fromWin, toWin, toWin, mode,
  2161. NotifyVirtual, FALSE);
  2162. FocusEvent(dev, FocusIn, mode, NotifyAncestor, toWin);
  2163. }
  2164. else {
  2165. /* neither fromWin or toWin is child of other */
  2166. WindowPtr common = CommonAncestor(toWin, fromWin);
  2167. /* common == NullWindow ==> different screens */
  2168. if (IsParent(fromWin, sprite.win))
  2169. FocusOutEvents(dev, sprite.win, fromWin, mode,
  2170. NotifyPointer, FALSE);
  2171. FocusEvent(dev, FocusOut, mode, NotifyNonlinear, fromWin);
  2172. if (fromWin->parent != NullWindow)
  2173. FocusOutEvents(dev, fromWin->parent, common, mode,
  2174. NotifyNonlinearVirtual, FALSE);
  2175. if (toWin->parent != NullWindow)
  2176. (void) FocusInEvents(dev, common, toWin, toWin, mode,
  2177. NotifyNonlinearVirtual, FALSE);
  2178. FocusEvent(dev, FocusIn, mode, NotifyNonlinear, toWin);
  2179. if (IsParent(toWin, sprite.win))
  2180. (void) FocusInEvents(dev, toWin, sprite.win, NullWindow,
  2181. mode, NotifyPointer, FALSE);
  2182. }
  2183. }
  2184. }
  2185. }
  2186. int
  2187. SetInputFocus(ClientPtr client,
  2188. DeviceIntPtr dev,
  2189. Window focusID, CARD8 revertTo, Time ctime, Bool followOK)
  2190. {
  2191. FocusClassPtr focus;
  2192. WindowPtr focusWin;
  2193. int mode;
  2194. TimeStamp time;
  2195. UpdateCurrentTime();
  2196. if ((revertTo != RevertToParent) &&
  2197. (revertTo != RevertToPointerRoot) &&
  2198. (revertTo != RevertToNone) &&
  2199. ((revertTo != RevertToFollowKeyboard) || !followOK)) {
  2200. client->errorValue = revertTo;
  2201. return BadValue;
  2202. }
  2203. time = ClientTimeToServerTime(ctime);
  2204. if ((focusID == None) || (focusID == PointerRoot))
  2205. focusWin = (WindowPtr) (long) focusID;
  2206. else if ((focusID == FollowKeyboard) && followOK)
  2207. focusWin = inputInfo.keyboard->focus->win;
  2208. else if (!(focusWin = SecurityLookupWindow(focusID, client,
  2209. SecurityReadAccess)))
  2210. return BadWindow;
  2211. else {
  2212. /* It is a match error to try to set the input focus to an
  2213. unviewable window. */
  2214. if (!focusWin->realized)
  2215. return (BadMatch);
  2216. }
  2217. focus = dev->focus;
  2218. if ((CompareTimeStamps(time, currentTime) == LATER) ||
  2219. (CompareTimeStamps(time, focus->time) == EARLIER))
  2220. return Success;
  2221. mode = (dev->grab) ? NotifyWhileGrabbed : NotifyNormal;
  2222. if (focus->win == FollowKeyboardWin)
  2223. DoFocusEvents(dev, inputInfo.keyboard->focus->win, focusWin, mode);
  2224. else
  2225. DoFocusEvents(dev, focus->win, focusWin, mode);
  2226. focus->time = time;
  2227. focus->revert = revertTo;
  2228. if (focusID == FollowKeyboard)
  2229. focus->win = FollowKeyboardWin;
  2230. else
  2231. focus->win = focusWin;
  2232. if ((focusWin == NoneWin) || (focusWin == PointerRootWin))
  2233. focus->traceGood = 0;
  2234. else {
  2235. int depth = 0;
  2236. WindowPtr pWin;
  2237. for (pWin = focusWin; pWin; pWin = pWin->parent)
  2238. depth++;
  2239. if (depth > focus->traceSize) {
  2240. focus->traceSize = depth + 1;
  2241. Must_have_memory = TRUE; /* XXX */
  2242. focus->trace = (WindowPtr *) realloc(focus->trace,
  2243. focus->traceSize *
  2244. sizeof(WindowPtr));
  2245. Must_have_memory = FALSE; /* XXX */
  2246. }
  2247. focus->traceGood = depth;
  2248. for (pWin = focusWin, depth--; pWin; pWin = pWin->parent, depth--)
  2249. focus->trace[depth] = pWin;
  2250. }
  2251. return Success;
  2252. }
  2253. int
  2254. ProcSetInputFocus(client)
  2255. ClientPtr client;
  2256. {
  2257. REQUEST(xSetInputFocusReq);
  2258. REQUEST_SIZE_MATCH(xSetInputFocusReq);
  2259. return SetInputFocus(client, inputInfo.keyboard, stuff->focus,
  2260. stuff->revertTo, stuff->time, FALSE);
  2261. }
  2262. int
  2263. ProcGetInputFocus(ClientPtr client)
  2264. {
  2265. xGetInputFocusReply rep = {0};
  2266. /* REQUEST(xReq); */
  2267. FocusClassPtr focus = inputInfo.keyboard->focus;
  2268. REQUEST_SIZE_MATCH(xReq);
  2269. rep.type = X_Reply;
  2270. rep.length = 0;
  2271. rep.sequenceNumber = client->sequence;
  2272. if (focus->win == NoneWin)
  2273. rep.focus = None;
  2274. else if (focus->win == PointerRootWin)
  2275. rep.focus = PointerRoot;
  2276. else
  2277. rep.focus = focus->win->drawable.id;
  2278. rep.revertTo = focus->revert;
  2279. WriteReplyToClient(client, sizeof(xGetInputFocusReply), &rep);
  2280. return Success;
  2281. }
  2282. int
  2283. ProcGrabPointer(ClientPtr client)
  2284. {
  2285. xGrabPointerReply rep;
  2286. DeviceIntPtr device = inputInfo.pointer;
  2287. GrabPtr grab;
  2288. WindowPtr pWin, confineTo;
  2289. CursorPtr cursor, oldCursor;
  2290. REQUEST(xGrabPointerReq);
  2291. TimeStamp time;
  2292. REQUEST_SIZE_MATCH(xGrabPointerReq);
  2293. UpdateCurrentTime();
  2294. if ((stuff->pointerMode != GrabModeSync) &&
  2295. (stuff->pointerMode != GrabModeAsync)) {
  2296. client->errorValue = stuff->pointerMode;
  2297. return BadValue;
  2298. }
  2299. if ((stuff->keyboardMode != GrabModeSync) &&
  2300. (stuff->keyboardMode != GrabModeAsync)) {
  2301. client->errorValue = stuff->keyboardMode;
  2302. return BadValue;
  2303. }
  2304. if ((stuff->ownerEvents != xFalse) && (stuff->ownerEvents != xTrue)) {
  2305. client->errorValue = stuff->ownerEvents;
  2306. return BadValue;
  2307. }
  2308. if (stuff->eventMask & ~PointerGrabMask) {
  2309. client->errorValue = stuff->eventMask;
  2310. return BadValue;
  2311. }
  2312. pWin = SecurityLookupWindow(stuff->grabWindow, client, SecurityReadAccess);
  2313. if (!pWin)
  2314. return BadWindow;
  2315. if (stuff->confineTo == None)
  2316. confineTo = NullWindow;
  2317. else {
  2318. confineTo = SecurityLookupWindow(stuff->confineTo, client,
  2319. SecurityReadAccess);
  2320. if (!confineTo)
  2321. return BadWindow;
  2322. }
  2323. if (stuff->cursor == None)
  2324. cursor = NullCursor;
  2325. else {
  2326. cursor = (CursorPtr) SecurityLookupIDByType(client, stuff->cursor,
  2327. RT_CURSOR,
  2328. SecurityReadAccess);
  2329. if (!cursor) {
  2330. client->errorValue = stuff->cursor;
  2331. return BadCursor;
  2332. }
  2333. }
  2334. /* at this point, some sort of reply is guaranteed. */
  2335. time = ClientTimeToServerTime(stuff->time);
  2336. rep.type = X_Reply;
  2337. rep.sequenceNumber = client->sequence;
  2338. rep.length = 0;
  2339. grab = device->grab;
  2340. if ((grab) && !SameClient(grab, client))
  2341. rep.status = AlreadyGrabbed;
  2342. else if ((!pWin->realized) ||
  2343. (confineTo &&
  2344. !(confineTo->realized && BorderSizeNotEmpty(confineTo))))
  2345. rep.status = GrabNotViewable;
  2346. else if (device->sync.frozen &&
  2347. device->sync.other && !SameClient(device->sync.other, client))
  2348. rep.status = GrabFrozen;
  2349. else if ((CompareTimeStamps(time, currentTime) == LATER) ||
  2350. (CompareTimeStamps(time, device->grabTime) == EARLIER))
  2351. rep.status = GrabInvalidTime;
  2352. else {
  2353. GrabRec tempGrab;
  2354. oldCursor = NullCursor;
  2355. if (grab) {
  2356. if (grab->confineTo && !confineTo)
  2357. ConfineCursorToWindow(ROOT, FALSE, FALSE);
  2358. oldCursor = grab->cursor;
  2359. }
  2360. tempGrab.cursor = cursor;
  2361. tempGrab.resource = client->clientAsMask;
  2362. tempGrab.ownerEvents = stuff->ownerEvents;
  2363. tempGrab.eventMask = stuff->eventMask;
  2364. tempGrab.confineTo = confineTo;
  2365. tempGrab.window = pWin;
  2366. tempGrab.keyboardMode = stuff->keyboardMode;
  2367. tempGrab.pointerMode = stuff->pointerMode;
  2368. tempGrab.device = device;
  2369. (*device->ActivateGrab) (device, &tempGrab, time, FALSE);
  2370. if (oldCursor)
  2371. FreeCursor(oldCursor, (Cursor) 0);
  2372. rep.status = GrabSuccess;
  2373. }
  2374. WriteReplyToClient(client, sizeof(xGrabPointerReply), &rep);
  2375. return Success;
  2376. }
  2377. int
  2378. ProcChangeActivePointerGrab(ClientPtr client)
  2379. {
  2380. DeviceIntPtr device = inputInfo.pointer;
  2381. GrabPtr grab = device->grab;
  2382. CursorPtr newCursor, oldCursor;
  2383. REQUEST(xChangeActivePointerGrabReq);
  2384. TimeStamp time;
  2385. REQUEST_SIZE_MATCH(xChangeActivePointerGrabReq);
  2386. if (stuff->eventMask & ~PointerGrabMask) {
  2387. client->errorValue = stuff->eventMask;
  2388. return BadValue;
  2389. }
  2390. if (stuff->cursor == None)
  2391. newCursor = NullCursor;
  2392. else {
  2393. newCursor = (CursorPtr) SecurityLookupIDByType(client, stuff->cursor,
  2394. RT_CURSOR,
  2395. SecurityReadAccess);
  2396. if (!newCursor) {
  2397. client->errorValue = stuff->cursor;
  2398. return BadCursor;
  2399. }
  2400. }
  2401. if (!grab)
  2402. return Success;
  2403. if (!SameClient(grab, client))
  2404. return Success;
  2405. time = ClientTimeToServerTime(stuff->time);
  2406. if ((CompareTimeStamps(time, currentTime) == LATER) ||
  2407. (CompareTimeStamps(time, device->grabTime) == EARLIER))
  2408. return Success;
  2409. oldCursor = grab->cursor;
  2410. grab->cursor = newCursor;
  2411. if (newCursor)
  2412. newCursor->refcnt++;
  2413. PostNewCursor();
  2414. if (oldCursor)
  2415. FreeCursor(oldCursor, (Cursor) 0);
  2416. grab->eventMask = stuff->eventMask;
  2417. return Success;
  2418. }
  2419. int
  2420. ProcUngrabPointer(ClientPtr client)
  2421. {
  2422. DeviceIntPtr device = inputInfo.pointer;
  2423. GrabPtr grab;
  2424. TimeStamp time;
  2425. REQUEST(xResourceReq);
  2426. REQUEST_SIZE_MATCH(xResourceReq);
  2427. UpdateCurrentTime();
  2428. grab = device->grab;
  2429. time = ClientTimeToServerTime(stuff->id);
  2430. if ((CompareTimeStamps(time, currentTime) != LATER) &&
  2431. (CompareTimeStamps(time, device->grabTime) != EARLIER) &&
  2432. (grab) && SameClient(grab, client))
  2433. (*device->DeactivateGrab) (device);
  2434. return Success;
  2435. }
  2436. int
  2437. GrabDevice(register ClientPtr client, register DeviceIntPtr dev,
  2438. unsigned this_mode, unsigned other_mode, Window grabWindow,
  2439. unsigned ownerEvents, Time ctime, Mask mask, CARD8 *status)
  2440. {
  2441. WindowPtr pWin;
  2442. GrabPtr grab;
  2443. TimeStamp time;
  2444. UpdateCurrentTime();
  2445. if ((this_mode != GrabModeSync) && (this_mode != GrabModeAsync)) {
  2446. client->errorValue = this_mode;
  2447. return BadValue;
  2448. }
  2449. if ((other_mode != GrabModeSync) && (other_mode != GrabModeAsync)) {
  2450. client->errorValue = other_mode;
  2451. return BadValue;
  2452. }
  2453. if ((ownerEvents != xFalse) && (ownerEvents != xTrue)) {
  2454. client->errorValue = ownerEvents;
  2455. return BadValue;
  2456. }
  2457. pWin = SecurityLookupWindow(grabWindow, client, SecurityReadAccess);
  2458. if (!pWin)
  2459. return BadWindow;
  2460. time = ClientTimeToServerTime(ctime);
  2461. grab = dev->grab;
  2462. if (grab && !SameClient(grab, client))
  2463. *status = AlreadyGrabbed;
  2464. else if (!pWin->realized)
  2465. *status = GrabNotViewable;
  2466. else if ((CompareTimeStamps(time, currentTime) == LATER) ||
  2467. (CompareTimeStamps(time, dev->grabTime) == EARLIER))
  2468. *status = GrabInvalidTime;
  2469. else if (dev->sync.frozen &&
  2470. dev->sync.other && !SameClient(dev->sync.other, client))
  2471. *status = GrabFrozen;
  2472. else {
  2473. GrabRec tempGrab;
  2474. tempGrab.window = pWin;
  2475. tempGrab.resource = client->clientAsMask;
  2476. tempGrab.ownerEvents = ownerEvents;
  2477. tempGrab.keyboardMode = this_mode;
  2478. tempGrab.pointerMode = other_mode;
  2479. tempGrab.eventMask = mask;
  2480. tempGrab.device = dev;
  2481. (*dev->ActivateGrab) (dev, &tempGrab, time, FALSE);
  2482. *status = GrabSuccess;
  2483. }
  2484. return Success;
  2485. }
  2486. int
  2487. ProcGrabKeyboard(ClientPtr client)
  2488. {
  2489. xGrabKeyboardReply rep;
  2490. REQUEST(xGrabKeyboardReq);
  2491. int result;
  2492. REQUEST_SIZE_MATCH(xGrabKeyboardReq);
  2493. result = GrabDevice(client, inputInfo.keyboard, stuff->keyboardMode,
  2494. stuff->pointerMode, stuff->grabWindow,
  2495. stuff->ownerEvents, stuff->time,
  2496. KeyPressMask | KeyReleaseMask, &rep.status);
  2497. if (result != Success)
  2498. return result;
  2499. rep.type = X_Reply;
  2500. rep.sequenceNumber = client->sequence;
  2501. rep.length = 0;
  2502. WriteReplyToClient(client, sizeof(xGrabKeyboardReply), &rep);
  2503. return Success;
  2504. }
  2505. int
  2506. ProcUngrabKeyboard(ClientPtr client)
  2507. {
  2508. DeviceIntPtr device = inputInfo.keyboard;
  2509. GrabPtr grab;
  2510. TimeStamp time;
  2511. REQUEST(xResourceReq);
  2512. REQUEST_SIZE_MATCH(xResourceReq);
  2513. UpdateCurrentTime();
  2514. grab = device->grab;
  2515. time = ClientTimeToServerTime(stuff->id);
  2516. if ((CompareTimeStamps(time, currentTime) != LATER) &&
  2517. (CompareTimeStamps(time, device->grabTime) != EARLIER) &&
  2518. (grab) && SameClient(grab, client))
  2519. (*device->DeactivateGrab) (device);
  2520. return Success;
  2521. }
  2522. int
  2523. ProcQueryPointer(ClientPtr client)
  2524. {
  2525. xQueryPointerReply rep;
  2526. WindowPtr pWin, t;
  2527. REQUEST(xResourceReq);
  2528. DeviceIntPtr mouse = inputInfo.pointer;
  2529. REQUEST_SIZE_MATCH(xResourceReq);
  2530. pWin = SecurityLookupWindow(stuff->id, client, SecurityReadAccess);
  2531. if (!pWin)
  2532. return BadWindow;
  2533. if (mouse->valuator->motionHintWindow)
  2534. MaybeStopHint(mouse, client);
  2535. memset(&rep, 0, sizeof(xQueryPointerReply));
  2536. rep.type = X_Reply;
  2537. rep.sequenceNumber = client->sequence;
  2538. rep.mask = mouse->button->state | inputInfo.keyboard->key->state;
  2539. rep.length = 0;
  2540. rep.root = (ROOT)->drawable.id;
  2541. rep.rootX = sprite.hot.x;
  2542. rep.rootY = sprite.hot.y;
  2543. rep.child = None;
  2544. if (sprite.hot.pScreen == pWin->drawable.pScreen) {
  2545. rep.sameScreen = xTrue;
  2546. rep.winX = sprite.hot.x - pWin->drawable.x;
  2547. rep.winY = sprite.hot.y - pWin->drawable.y;
  2548. for (t = sprite.win; t; t = t->parent)
  2549. if (t->parent == pWin) {
  2550. rep.child = t->drawable.id;
  2551. break;
  2552. }
  2553. }
  2554. else {
  2555. rep.sameScreen = xFalse;
  2556. rep.winX = 0;
  2557. rep.winY = 0;
  2558. }
  2559. WriteReplyToClient(client, sizeof(xQueryPointerReply), &rep);
  2560. return (Success);
  2561. }
  2562. void
  2563. InitEvents()
  2564. {
  2565. int i;
  2566. sprite.hot.pScreen = sprite.hotPhys.pScreen = (ScreenPtr) NULL;
  2567. inputInfo.numDevices = 0;
  2568. inputInfo.devices = (DeviceIntPtr) NULL;
  2569. inputInfo.off_devices = (DeviceIntPtr) NULL;
  2570. inputInfo.keyboard = (DeviceIntPtr) NULL;
  2571. inputInfo.pointer = (DeviceIntPtr) NULL;
  2572. memcpy(filters, initialFilters, 128 * sizeof(Mask));
  2573. if (spriteTraceSize == 0) {
  2574. spriteTraceSize = 32;
  2575. spriteTrace = malloc(32 * sizeof(WindowPtr));
  2576. if (!spriteTrace)
  2577. FatalError("failed to allocate spriteTrace");
  2578. }
  2579. spriteTraceGood = 0;
  2580. lastEventMask = OwnerGrabButtonMask;
  2581. filters[MotionNotify] = PointerMotionMask;
  2582. sprite.win = NullWindow;
  2583. sprite.current = NullCursor;
  2584. sprite.hotLimits.x1 = 0;
  2585. sprite.hotLimits.y1 = 0;
  2586. sprite.hotLimits.x2 = 0;
  2587. sprite.hotLimits.y2 = 0;
  2588. sprite.confined = FALSE;
  2589. syncEvents.replayDev = (DeviceIntPtr) NULL;
  2590. syncEvents.replayWin = NullWindow;
  2591. while (syncEvents.pending) {
  2592. QdEventPtr next = syncEvents.pending->next;
  2593. free(syncEvents.pending);
  2594. syncEvents.pending = next;
  2595. }
  2596. syncEvents.pendtail = &syncEvents.pending;
  2597. syncEvents.playingEvents = FALSE;
  2598. syncEvents.time.months = 0;
  2599. syncEvents.time.milliseconds = 0; /* hardly matters */
  2600. currentTime.months = 0;
  2601. currentTime.milliseconds = GetTimeInMillis();
  2602. lastDeviceEventTime = currentTime;
  2603. for (i = 0; i < DNPMCOUNT; i++) {
  2604. DontPropagateMasks[i] = 0;
  2605. DontPropagateRefCnts[i] = 0;
  2606. }
  2607. }
  2608. void
  2609. CloseDownEvents(void)
  2610. {
  2611. free(spriteTrace);
  2612. spriteTrace = NULL;
  2613. spriteTraceSize = 0;
  2614. }
  2615. int
  2616. ProcSendEvent(ClientPtr client)
  2617. {
  2618. WindowPtr pWin;
  2619. WindowPtr effectiveFocus = NullWindow; /* only set if dest==InputFocus */
  2620. REQUEST(xSendEventReq);
  2621. REQUEST_SIZE_MATCH(xSendEventReq);
  2622. /* The client's event type must be a core event type or one defined by an
  2623. extension. */
  2624. if (!((stuff->event.u.u.type > X_Reply &&
  2625. stuff->event.u.u.type < LASTEvent) ||
  2626. (stuff->event.u.u.type >= EXTENSION_EVENT_BASE &&
  2627. stuff->event.u.u.type < (unsigned) lastEvent))) {
  2628. client->errorValue = stuff->event.u.u.type;
  2629. return BadValue;
  2630. }
  2631. if (stuff->event.u.u.type == ClientMessage &&
  2632. stuff->event.u.u.detail != 8 &&
  2633. stuff->event.u.u.detail != 16 && stuff->event.u.u.detail != 32) {
  2634. client->errorValue = stuff->event.u.u.detail;
  2635. return BadValue;
  2636. }
  2637. if (stuff->eventMask & ~AllEventMasks) {
  2638. client->errorValue = stuff->eventMask;
  2639. return BadValue;
  2640. }
  2641. if (stuff->destination == PointerWindow)
  2642. pWin = sprite.win;
  2643. else if (stuff->destination == InputFocus) {
  2644. WindowPtr inputFocus = inputInfo.keyboard->focus->win;
  2645. if (inputFocus == NoneWin)
  2646. return Success;
  2647. /* If the input focus is PointerRootWin, send the event to where
  2648. the pointer is if possible, then perhaps propogate up to root. */
  2649. if (inputFocus == PointerRootWin)
  2650. inputFocus = ROOT;
  2651. if (IsParent(inputFocus, sprite.win)) {
  2652. effectiveFocus = inputFocus;
  2653. pWin = sprite.win;
  2654. }
  2655. else
  2656. effectiveFocus = pWin = inputFocus;
  2657. }
  2658. else
  2659. pWin = SecurityLookupWindow(stuff->destination, client,
  2660. SecurityReadAccess);
  2661. if (!pWin)
  2662. return BadWindow;
  2663. if ((stuff->propagate != xFalse) && (stuff->propagate != xTrue)) {
  2664. client->errorValue = stuff->propagate;
  2665. return BadValue;
  2666. }
  2667. stuff->event.u.u.type |= 0x80;
  2668. if (stuff->propagate) {
  2669. for (; pWin; pWin = pWin->parent) {
  2670. if (DeliverEventsToWindow(pWin, &stuff->event, 1, stuff->eventMask,
  2671. NullGrab, 0))
  2672. return Success;
  2673. if (pWin == effectiveFocus)
  2674. return Success;
  2675. stuff->eventMask &= ~wDontPropagateMask(pWin);
  2676. if (!stuff->eventMask)
  2677. break;
  2678. }
  2679. }
  2680. else
  2681. (void) DeliverEventsToWindow(pWin, &stuff->event, 1, stuff->eventMask,
  2682. NullGrab, 0);
  2683. return Success;
  2684. }
  2685. int
  2686. ProcUngrabKey(ClientPtr client)
  2687. {
  2688. REQUEST(xUngrabKeyReq);
  2689. WindowPtr pWin;
  2690. GrabRec tempGrab;
  2691. DeviceIntPtr keybd = inputInfo.keyboard;
  2692. REQUEST_SIZE_MATCH(xUngrabKeyReq);
  2693. pWin = SecurityLookupWindow(stuff->grabWindow, client, SecurityReadAccess);
  2694. if (!pWin)
  2695. return BadWindow;
  2696. if (((stuff->key > keybd->key->curKeySyms.maxKeyCode) ||
  2697. (stuff->key < keybd->key->curKeySyms.minKeyCode))
  2698. && (stuff->key != AnyKey)) {
  2699. client->errorValue = stuff->key;
  2700. return BadValue;
  2701. }
  2702. if ((stuff->modifiers != AnyModifier) &&
  2703. (stuff->modifiers & ~AllModifiersMask)) {
  2704. client->errorValue = stuff->modifiers;
  2705. return BadValue;
  2706. }
  2707. tempGrab.resource = client->clientAsMask;
  2708. tempGrab.device = keybd;
  2709. tempGrab.window = pWin;
  2710. tempGrab.modifiersDetail.exact = stuff->modifiers;
  2711. tempGrab.modifiersDetail.pMask = NULL;
  2712. tempGrab.modifierDevice = inputInfo.keyboard;
  2713. tempGrab.type = KeyPress;
  2714. tempGrab.detail.exact = stuff->key;
  2715. tempGrab.detail.pMask = NULL;
  2716. if (!DeletePassiveGrabFromList(&tempGrab))
  2717. return (BadAlloc);
  2718. return (Success);
  2719. }
  2720. int
  2721. ProcGrabKey(ClientPtr client)
  2722. {
  2723. WindowPtr pWin;
  2724. REQUEST(xGrabKeyReq);
  2725. GrabPtr grab;
  2726. DeviceIntPtr keybd = inputInfo.keyboard;
  2727. REQUEST_SIZE_MATCH(xGrabKeyReq);
  2728. if ((stuff->ownerEvents != xTrue) && (stuff->ownerEvents != xFalse)) {
  2729. client->errorValue = stuff->ownerEvents;
  2730. return (BadValue);
  2731. }
  2732. if ((stuff->pointerMode != GrabModeSync) &&
  2733. (stuff->pointerMode != GrabModeAsync)) {
  2734. client->errorValue = stuff->pointerMode;
  2735. return BadValue;
  2736. }
  2737. if ((stuff->keyboardMode != GrabModeSync) &&
  2738. (stuff->keyboardMode != GrabModeAsync)) {
  2739. client->errorValue = stuff->keyboardMode;
  2740. return BadValue;
  2741. }
  2742. if (((stuff->key > keybd->key->curKeySyms.maxKeyCode) ||
  2743. (stuff->key < keybd->key->curKeySyms.minKeyCode))
  2744. && (stuff->key != AnyKey)) {
  2745. client->errorValue = stuff->key;
  2746. return BadValue;
  2747. }
  2748. if ((stuff->modifiers != AnyModifier) &&
  2749. (stuff->modifiers & ~AllModifiersMask)) {
  2750. client->errorValue = stuff->modifiers;
  2751. return BadValue;
  2752. }
  2753. pWin = SecurityLookupWindow(stuff->grabWindow, client, SecurityReadAccess);
  2754. if (!pWin)
  2755. return BadWindow;
  2756. grab = CreateGrab(client->index, keybd, pWin,
  2757. (Mask) (KeyPressMask | KeyReleaseMask),
  2758. (Bool) stuff->ownerEvents, (Bool) stuff->keyboardMode,
  2759. (Bool) stuff->pointerMode, keybd, stuff->modifiers,
  2760. KeyPress, stuff->key, NullWindow, NullCursor);
  2761. if (!grab)
  2762. return BadAlloc;
  2763. return AddPassiveGrabToList(grab);
  2764. }
  2765. int
  2766. ProcGrabButton(ClientPtr client)
  2767. {
  2768. WindowPtr pWin, confineTo;
  2769. REQUEST(xGrabButtonReq);
  2770. CursorPtr cursor;
  2771. GrabPtr grab;
  2772. REQUEST_SIZE_MATCH(xGrabButtonReq);
  2773. if ((stuff->pointerMode != GrabModeSync) &&
  2774. (stuff->pointerMode != GrabModeAsync)) {
  2775. client->errorValue = stuff->pointerMode;
  2776. return BadValue;
  2777. }
  2778. if ((stuff->keyboardMode != GrabModeSync) &&
  2779. (stuff->keyboardMode != GrabModeAsync)) {
  2780. client->errorValue = stuff->keyboardMode;
  2781. return BadValue;
  2782. }
  2783. if ((stuff->modifiers != AnyModifier) &&
  2784. (stuff->modifiers & ~AllModifiersMask)) {
  2785. client->errorValue = stuff->modifiers;
  2786. return BadValue;
  2787. }
  2788. if ((stuff->ownerEvents != xFalse) && (stuff->ownerEvents != xTrue)) {
  2789. client->errorValue = stuff->ownerEvents;
  2790. return BadValue;
  2791. }
  2792. if (stuff->eventMask & ~PointerGrabMask) {
  2793. client->errorValue = stuff->eventMask;
  2794. return BadValue;
  2795. }
  2796. pWin = SecurityLookupWindow(stuff->grabWindow, client, SecurityReadAccess);
  2797. if (!pWin)
  2798. return BadWindow;
  2799. if (stuff->confineTo == None)
  2800. confineTo = NullWindow;
  2801. else {
  2802. confineTo = SecurityLookupWindow(stuff->confineTo, client,
  2803. SecurityReadAccess);
  2804. if (!confineTo)
  2805. return BadWindow;
  2806. }
  2807. if (stuff->cursor == None)
  2808. cursor = NullCursor;
  2809. else {
  2810. cursor = (CursorPtr) SecurityLookupIDByType(client, stuff->cursor,
  2811. RT_CURSOR,
  2812. SecurityReadAccess);
  2813. if (!cursor) {
  2814. client->errorValue = stuff->cursor;
  2815. return BadCursor;
  2816. }
  2817. }
  2818. grab = CreateGrab(client->index, inputInfo.pointer, pWin,
  2819. (Mask)stuff->eventMask, (Bool)stuff->ownerEvents,
  2820. (Bool)stuff->keyboardMode, (Bool)stuff->pointerMode,
  2821. inputInfo.keyboard, stuff->modifiers, ButtonPress,
  2822. stuff->button, confineTo, cursor);
  2823. if (!grab)
  2824. return BadAlloc;
  2825. return AddPassiveGrabToList(grab);
  2826. }
  2827. int
  2828. ProcUngrabButton(ClientPtr client)
  2829. {
  2830. REQUEST(xUngrabButtonReq);
  2831. WindowPtr pWin;
  2832. GrabRec tempGrab;
  2833. REQUEST_SIZE_MATCH(xUngrabButtonReq);
  2834. if ((stuff->modifiers != AnyModifier) &&
  2835. (stuff->modifiers & ~AllModifiersMask)) {
  2836. client->errorValue = stuff->modifiers;
  2837. return BadValue;
  2838. }
  2839. pWin = SecurityLookupWindow(stuff->grabWindow, client, SecurityReadAccess);
  2840. if (!pWin)
  2841. return BadWindow;
  2842. tempGrab.resource = client->clientAsMask;
  2843. tempGrab.device = inputInfo.pointer;
  2844. tempGrab.window = pWin;
  2845. tempGrab.modifiersDetail.exact = stuff->modifiers;
  2846. tempGrab.modifiersDetail.pMask = NULL;
  2847. tempGrab.modifierDevice = inputInfo.keyboard;
  2848. tempGrab.type = ButtonPress;
  2849. tempGrab.detail.exact = stuff->button;
  2850. tempGrab.detail.pMask = NULL;
  2851. if (!DeletePassiveGrabFromList(&tempGrab))
  2852. return (BadAlloc);
  2853. return (Success);
  2854. }
  2855. void
  2856. DeleteWindowFromAnyEvents(WindowPtr pWin, Bool freeResources)
  2857. {
  2858. WindowPtr parent;
  2859. DeviceIntPtr mouse = inputInfo.pointer;
  2860. DeviceIntPtr keybd = inputInfo.keyboard;
  2861. FocusClassPtr focus = keybd->focus;
  2862. OtherClientsPtr oc;
  2863. GrabPtr passive;
  2864. /* Deactivate any grabs performed on this window, before making any
  2865. input focus changes. */
  2866. if (mouse->grab &&
  2867. ((mouse->grab->window == pWin) || (mouse->grab->confineTo == pWin)))
  2868. (*mouse->DeactivateGrab) (mouse);
  2869. /* Deactivating a keyboard grab should cause focus events. */
  2870. if (keybd->grab && (keybd->grab->window == pWin))
  2871. (*keybd->DeactivateGrab) (keybd);
  2872. /* If the focus window is a root window (ie. has no parent) then don't
  2873. delete the focus from it. */
  2874. if ((pWin == focus->win) && (pWin->parent != NullWindow)) {
  2875. int focusEventMode = NotifyNormal;
  2876. /* If a grab is in progress, then alter the mode of focus events. */
  2877. if (keybd->grab)
  2878. focusEventMode = NotifyWhileGrabbed;
  2879. switch (focus->revert) {
  2880. case RevertToNone:
  2881. DoFocusEvents(keybd, pWin, NoneWin, focusEventMode);
  2882. focus->win = NoneWin;
  2883. focus->traceGood = 0;
  2884. break;
  2885. case RevertToParent:
  2886. parent = pWin;
  2887. do {
  2888. parent = parent->parent;
  2889. focus->traceGood--;
  2890. } while (!parent->realized
  2891. /* This would be a good protocol change -- windows being reparented
  2892. during SaveSet processing would cause the focus to revert to the
  2893. nearest enclosing window which will survive the death of the exiting
  2894. client, instead of ending up reverting to a dying window and thence
  2895. to None
  2896. */
  2897. #ifdef NOTDEF
  2898. || clients[CLIENT_ID(parent->drawable.id)]->clientGone
  2899. #endif
  2900. );
  2901. DoFocusEvents(keybd, pWin, parent, focusEventMode);
  2902. focus->win = parent;
  2903. focus->revert = RevertToNone;
  2904. break;
  2905. case RevertToPointerRoot:
  2906. DoFocusEvents(keybd, pWin, PointerRootWin, focusEventMode);
  2907. focus->win = PointerRootWin;
  2908. focus->traceGood = 0;
  2909. break;
  2910. }
  2911. }
  2912. if (mouse->valuator->motionHintWindow == pWin)
  2913. mouse->valuator->motionHintWindow = NullWindow;
  2914. if (freeResources) {
  2915. if (pWin->dontPropagate)
  2916. DontPropagateRefCnts[pWin->dontPropagate]--;
  2917. while ((oc = wOtherClients(pWin)))
  2918. FreeResource(oc->resource, RT_NONE);
  2919. while ((passive = wPassiveGrabs(pWin)))
  2920. FreeResource(passive->resource, RT_NONE);
  2921. }
  2922. }
  2923. /**
  2924. * Call this whenever some window at or below pWin has changed geometry
  2925. */
  2926. _X_EXPORT void
  2927. CheckCursorConfinement(WindowPtr pWin)
  2928. {
  2929. GrabPtr grab = inputInfo.pointer->grab;
  2930. WindowPtr confineTo;
  2931. if (grab && (confineTo = grab->confineTo)) {
  2932. if (!BorderSizeNotEmpty(confineTo))
  2933. (*inputInfo.pointer->DeactivateGrab) (inputInfo.pointer);
  2934. else if ((pWin == confineTo) || IsParent(pWin, confineTo))
  2935. ConfineCursorToWindow(confineTo, TRUE, TRUE);
  2936. }
  2937. }
  2938. Mask
  2939. EventMaskForClient(WindowPtr pWin, ClientPtr client)
  2940. {
  2941. OtherClientsPtr other;
  2942. if (wClient(pWin) == client)
  2943. return pWin->eventMask;
  2944. for (other = wOtherClients(pWin); other; other = other->next) {
  2945. if (SameClient(other, client))
  2946. return other->mask;
  2947. }
  2948. return 0;
  2949. }
  2950. int
  2951. ProcRecolorCursor(ClientPtr client)
  2952. {
  2953. CursorPtr pCursor;
  2954. int nscr;
  2955. ScreenPtr pscr;
  2956. Bool displayed;
  2957. REQUEST(xRecolorCursorReq);
  2958. REQUEST_SIZE_MATCH(xRecolorCursorReq);
  2959. pCursor = (CursorPtr) SecurityLookupIDByType(client, stuff->cursor,
  2960. RT_CURSOR,
  2961. SecurityWriteAccess);
  2962. if (!pCursor) {
  2963. client->errorValue = stuff->cursor;
  2964. return (BadCursor);
  2965. }
  2966. pCursor->foreRed = stuff->foreRed;
  2967. pCursor->foreGreen = stuff->foreGreen;
  2968. pCursor->foreBlue = stuff->foreBlue;
  2969. pCursor->backRed = stuff->backRed;
  2970. pCursor->backGreen = stuff->backGreen;
  2971. pCursor->backBlue = stuff->backBlue;
  2972. for (nscr = 0; nscr < screenInfo.numScreens; nscr++) {
  2973. pscr = screenInfo.screens[nscr];
  2974. displayed = (pscr == sprite.hotPhys.pScreen);
  2975. (*pscr->RecolorCursor) (pscr, pCursor,
  2976. (pCursor == sprite.current) && displayed);
  2977. }
  2978. return (Success);
  2979. }
  2980. _X_EXPORT void
  2981. WriteEventsToClient(ClientPtr pClient, int count, xEvent *events)
  2982. {
  2983. xEvent eventTo, *eventFrom;
  2984. int i;
  2985. if (!pClient || pClient == serverClient || pClient->clientGone)
  2986. return;
  2987. for (i = 0; i < count; i++)
  2988. if ((events[i].u.u.type & 0x7f) != KeymapNotify)
  2989. events[i].u.u.sequenceNumber = pClient->sequence;
  2990. if (EventCallback) {
  2991. EventInfoRec eventinfo;
  2992. eventinfo.client = pClient;
  2993. eventinfo.events = events;
  2994. eventinfo.count = count;
  2995. CallCallbacks(&EventCallback, (pointer) &eventinfo);
  2996. }
  2997. if (pClient->swapped) {
  2998. for (i = 0; i < count; i++) {
  2999. eventFrom = &events[i];
  3000. /* Remember to strip off the leading bit of type in case
  3001. this event was sent with "SendEvent." */
  3002. (*EventSwapVector[eventFrom->u.u.type & 0177])
  3003. (eventFrom, &eventTo);
  3004. (void) WriteToClient(pClient, sizeof(xEvent), (char *) &eventTo);
  3005. }
  3006. }
  3007. else {
  3008. (void) WriteToClient(pClient, count * sizeof(xEvent), (char *) events);
  3009. }
  3010. }