grabs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  22. All Rights Reserved
  23. Permission to use, copy, modify, and distribute this software and its
  24. documentation for any purpose and without fee is hereby granted,
  25. provided that the above copyright notice appear in all copies and that
  26. both that copyright notice and this permission notice appear in
  27. supporting documentation, and that the name of Digital not be
  28. used in advertising or publicity pertaining to distribution of the
  29. software without specific, written prior permission.
  30. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  31. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  32. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  33. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  34. WHETHER IN AN action OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  35. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  36. SOFTWARE.
  37. */
  38. #ifdef HAVE_DIX_CONFIG_H
  39. #include <dix-config.h>
  40. #endif
  41. #include <X11/X.h>
  42. #include "misc.h"
  43. #include <X11/Xproto.h>
  44. #include <X11/extensions/XI2.h>
  45. #include "windowstr.h"
  46. #include "inputstr.h"
  47. #include "cursorstr.h"
  48. #include "dixgrabs.h"
  49. #include "xace.h"
  50. #include "exevents.h"
  51. #include "exglobals.h"
  52. #include "inpututils.h"
  53. #include "client.h"
  54. #define BITMASK(i) (((Mask)1) << ((i) & 31))
  55. #define MASKIDX(i) ((i) >> 5)
  56. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  57. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  58. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  59. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  60. void
  61. PrintDeviceGrabInfo(DeviceIntPtr dev)
  62. {
  63. ClientPtr client;
  64. LocalClientCredRec *lcc;
  65. int i, j;
  66. GrabInfoPtr devGrab = &dev->deviceGrab;
  67. GrabPtr grab = devGrab->grab;
  68. Bool clientIdPrinted = FALSE;
  69. ErrorF("Active grab 0x%lx (%s) on device '%s' (%d):\n",
  70. (unsigned long) grab->resource,
  71. (grab->grabtype == XI2) ? "xi2" :
  72. ((grab->grabtype == CORE) ? "core" : "xi1"), dev->name, dev->id);
  73. client = clients[CLIENT_ID(grab->resource)];
  74. if (client) {
  75. pid_t clientpid = GetClientPid(client);
  76. const char *cmdname = GetClientCmdName(client);
  77. const char *cmdargs = GetClientCmdArgs(client);
  78. if ((clientpid > 0) && (cmdname != NULL)) {
  79. ErrorF(" client pid %ld %s %s\n",
  80. (long) clientpid, cmdname, cmdargs ? cmdargs : "");
  81. clientIdPrinted = TRUE;
  82. }
  83. else if (GetLocalClientCreds(client, &lcc) != -1) {
  84. ErrorF(" client pid %ld uid %ld gid %ld\n",
  85. (lcc->fieldsSet & LCC_PID_SET) ? (long) lcc->pid : 0,
  86. (lcc->fieldsSet & LCC_UID_SET) ? (long) lcc->euid : 0,
  87. (lcc->fieldsSet & LCC_GID_SET) ? (long) lcc->egid : 0);
  88. FreeLocalClientCreds(lcc);
  89. clientIdPrinted = TRUE;
  90. }
  91. }
  92. if (!clientIdPrinted) {
  93. ErrorF(" (no client information available for client %d)\n",
  94. CLIENT_ID(grab->resource));
  95. }
  96. /* XXX is this even correct? */
  97. if (devGrab->sync.other)
  98. ErrorF(" grab ID 0x%lx from paired device\n",
  99. (unsigned long) devGrab->sync.other->resource);
  100. ErrorF(" at %ld (from %s grab)%s (device %s, state %d)\n",
  101. (unsigned long) devGrab->grabTime.milliseconds,
  102. devGrab->fromPassiveGrab ? "passive" : "active",
  103. devGrab->implicitGrab ? " (implicit)" : "",
  104. devGrab->sync.frozen ? "frozen" : "thawed", devGrab->sync.state);
  105. if (grab->grabtype == CORE) {
  106. ErrorF(" core event mask 0x%lx\n",
  107. (unsigned long) grab->eventMask);
  108. }
  109. else if (grab->grabtype == XI) {
  110. ErrorF(" xi1 event mask 0x%lx\n",
  111. devGrab->implicitGrab ? (unsigned long) grab->deviceMask :
  112. (unsigned long) grab->eventMask);
  113. }
  114. else if (grab->grabtype == XI2) {
  115. for (i = 0; i < xi2mask_num_masks(grab->xi2mask); i++) {
  116. const unsigned char *mask;
  117. int print;
  118. print = 0;
  119. for (j = 0; j < XI2MASKSIZE; j++) {
  120. mask = xi2mask_get_one_mask(grab->xi2mask, i);
  121. if (mask[j]) {
  122. print = 1;
  123. break;
  124. }
  125. }
  126. if (!print)
  127. continue;
  128. ErrorF(" xi2 event mask for device %d: 0x", dev->id);
  129. for (j = 0; j < xi2mask_mask_size(grab->xi2mask); j++)
  130. ErrorF("%x", mask[j]);
  131. ErrorF("\n");
  132. }
  133. }
  134. if (devGrab->fromPassiveGrab) {
  135. ErrorF(" passive grab type %d, detail 0x%x, "
  136. "activating key %d\n", grab->type, grab->detail.exact,
  137. devGrab->activatingKey);
  138. }
  139. ErrorF(" owner-events %s, kb %d ptr %d, confine %lx, cursor 0x%lx\n",
  140. grab->ownerEvents ? "true" : "false",
  141. grab->keyboardMode, grab->pointerMode,
  142. grab->confineTo ? (unsigned long) grab->confineTo->drawable.id : 0,
  143. grab->cursor ? (unsigned long) grab->cursor->id : 0);
  144. }
  145. void
  146. UngrabAllDevices(Bool kill_client)
  147. {
  148. DeviceIntPtr dev;
  149. ClientPtr client;
  150. ErrorF("Ungrabbing all devices%s; grabs listed below:\n",
  151. kill_client ? " and killing their owners" : "");
  152. for (dev = inputInfo.devices; dev; dev = dev->next) {
  153. if (!dev->deviceGrab.grab)
  154. continue;
  155. PrintDeviceGrabInfo(dev);
  156. client = clients[CLIENT_ID(dev->deviceGrab.grab->resource)];
  157. if (!kill_client || !client || client->clientGone)
  158. dev->deviceGrab.DeactivateGrab(dev);
  159. if (kill_client)
  160. CloseDownClient(client);
  161. }
  162. ErrorF("End list of ungrabbed devices\n");
  163. }
  164. GrabPtr
  165. AllocGrab(const GrabPtr src)
  166. {
  167. GrabPtr grab = calloc(1, sizeof(GrabRec));
  168. if (grab) {
  169. grab->xi2mask = xi2mask_new();
  170. if (!grab->xi2mask) {
  171. free(grab);
  172. grab = NULL;
  173. }
  174. else if (src && !CopyGrab(grab, src)) {
  175. free(grab->xi2mask);
  176. free(grab);
  177. grab = NULL;
  178. }
  179. }
  180. return grab;
  181. }
  182. GrabPtr
  183. CreateGrab(int client, DeviceIntPtr device, DeviceIntPtr modDevice,
  184. WindowPtr window, enum InputLevel grabtype, GrabMask *mask,
  185. GrabParameters *param, int type,
  186. KeyCode keybut, /* key or button */
  187. WindowPtr confineTo, CursorPtr cursor)
  188. {
  189. GrabPtr grab;
  190. grab = AllocGrab(NULL);
  191. if (!grab)
  192. return (GrabPtr) NULL;
  193. grab->resource = FakeClientID(client);
  194. grab->device = device;
  195. grab->window = window;
  196. if (grabtype == CORE || grabtype == XI)
  197. grab->eventMask = mask->core; /* same for XI */
  198. else
  199. grab->eventMask = 0;
  200. grab->deviceMask = 0;
  201. grab->ownerEvents = param->ownerEvents;
  202. grab->keyboardMode = param->this_device_mode;
  203. grab->pointerMode = param->other_devices_mode;
  204. grab->modifiersDetail.exact = param->modifiers;
  205. grab->modifiersDetail.pMask = NULL;
  206. grab->modifierDevice = modDevice;
  207. grab->type = type;
  208. grab->grabtype = grabtype;
  209. grab->detail.exact = keybut;
  210. grab->detail.pMask = NULL;
  211. grab->confineTo = confineTo;
  212. grab->cursor = RefCursor(cursor);
  213. grab->next = NULL;
  214. if (grabtype == XI2)
  215. xi2mask_merge(grab->xi2mask, mask->xi2mask);
  216. return grab;
  217. }
  218. void
  219. FreeGrab(GrabPtr pGrab)
  220. {
  221. BUG_RETURN(!pGrab);
  222. free(pGrab->modifiersDetail.pMask);
  223. free(pGrab->detail.pMask);
  224. if (pGrab->cursor)
  225. FreeCursor(pGrab->cursor, (Cursor) 0);
  226. xi2mask_free(&pGrab->xi2mask);
  227. free(pGrab);
  228. }
  229. Bool
  230. CopyGrab(GrabPtr dst, const GrabPtr src)
  231. {
  232. Mask *mdetails_mask = NULL;
  233. Mask *details_mask = NULL;
  234. XI2Mask *xi2mask;
  235. if (src->modifiersDetail.pMask) {
  236. int len = MasksPerDetailMask * sizeof(Mask);
  237. mdetails_mask = malloc(len);
  238. if (!mdetails_mask)
  239. return FALSE;
  240. memcpy(mdetails_mask, src->modifiersDetail.pMask, len);
  241. }
  242. if (src->detail.pMask) {
  243. int len = MasksPerDetailMask * sizeof(Mask);
  244. details_mask = malloc(len);
  245. if (!details_mask) {
  246. free(mdetails_mask);
  247. return FALSE;
  248. }
  249. memcpy(details_mask, src->detail.pMask, len);
  250. }
  251. if (!dst->xi2mask) {
  252. xi2mask = xi2mask_new();
  253. if (!xi2mask) {
  254. free(mdetails_mask);
  255. free(details_mask);
  256. return FALSE;
  257. }
  258. }
  259. else {
  260. xi2mask = dst->xi2mask;
  261. xi2mask_zero(xi2mask, -1);
  262. }
  263. *dst = *src;
  264. dst->modifiersDetail.pMask = mdetails_mask;
  265. dst->detail.pMask = details_mask;
  266. dst->xi2mask = xi2mask;
  267. dst->cursor = RefCursor(src->cursor);
  268. xi2mask_merge(dst->xi2mask, src->xi2mask);
  269. return TRUE;
  270. }
  271. int
  272. DeletePassiveGrab(void *value, XID id)
  273. {
  274. GrabPtr g, prev;
  275. GrabPtr pGrab = (GrabPtr) value;
  276. /* it is OK if the grab isn't found */
  277. prev = 0;
  278. for (g = (wPassiveGrabs(pGrab->window)); g; g = g->next) {
  279. if (pGrab == g) {
  280. if (prev)
  281. prev->next = g->next;
  282. else if (!(pGrab->window->optional->passiveGrabs = g->next))
  283. CheckWindowOptionalNeed(pGrab->window);
  284. break;
  285. }
  286. prev = g;
  287. }
  288. FreeGrab(pGrab);
  289. return Success;
  290. }
  291. static Mask *
  292. DeleteDetailFromMask(Mask *pDetailMask, unsigned int detail)
  293. {
  294. Mask *mask;
  295. int i;
  296. mask = malloc(sizeof(Mask) * MasksPerDetailMask);
  297. if (mask) {
  298. if (pDetailMask)
  299. for (i = 0; i < MasksPerDetailMask; i++)
  300. mask[i] = pDetailMask[i];
  301. else
  302. for (i = 0; i < MasksPerDetailMask; i++)
  303. mask[i] = ~0L;
  304. BITCLEAR(mask, detail);
  305. }
  306. return mask;
  307. }
  308. static Bool
  309. IsInGrabMask(DetailRec firstDetail,
  310. DetailRec secondDetail, unsigned int exception)
  311. {
  312. if (firstDetail.exact == exception) {
  313. if (firstDetail.pMask == NULL)
  314. return TRUE;
  315. /* (at present) never called with two non-null pMasks */
  316. if (secondDetail.exact == exception)
  317. return FALSE;
  318. if (GETBIT(firstDetail.pMask, secondDetail.exact))
  319. return TRUE;
  320. }
  321. return FALSE;
  322. }
  323. static Bool
  324. IdenticalExactDetails(unsigned int firstExact,
  325. unsigned int secondExact, unsigned int exception)
  326. {
  327. if ((firstExact == exception) || (secondExact == exception))
  328. return FALSE;
  329. if (firstExact == secondExact)
  330. return TRUE;
  331. return FALSE;
  332. }
  333. static Bool
  334. DetailSupersedesSecond(DetailRec firstDetail,
  335. DetailRec secondDetail, unsigned int exception)
  336. {
  337. if (IsInGrabMask(firstDetail, secondDetail, exception))
  338. return TRUE;
  339. if (IdenticalExactDetails(firstDetail.exact, secondDetail.exact, exception))
  340. return TRUE;
  341. return FALSE;
  342. }
  343. static Bool
  344. GrabSupersedesSecond(GrabPtr pFirstGrab, GrabPtr pSecondGrab)
  345. {
  346. unsigned int any_modifier = (pFirstGrab->grabtype == XI2) ?
  347. (unsigned int) XIAnyModifier : (unsigned int) AnyModifier;
  348. if (!DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  349. pSecondGrab->modifiersDetail, any_modifier))
  350. return FALSE;
  351. if (DetailSupersedesSecond(pFirstGrab->detail,
  352. pSecondGrab->detail, (unsigned int) AnyKey))
  353. return TRUE;
  354. return FALSE;
  355. }
  356. /**
  357. * Compares two grabs and returns TRUE if the first grab matches the second
  358. * grab.
  359. *
  360. * A match is when
  361. * - the devices set for the grab are equal (this is optional).
  362. * - the event types for both grabs are equal.
  363. * - XXX
  364. *
  365. * @param ignoreDevice TRUE if the device settings on the grabs are to be
  366. * ignored.
  367. * @return TRUE if the grabs match or FALSE otherwise.
  368. */
  369. Bool
  370. GrabMatchesSecond(GrabPtr pFirstGrab, GrabPtr pSecondGrab, Bool ignoreDevice)
  371. {
  372. unsigned int any_modifier = (pFirstGrab->grabtype == XI2) ?
  373. (unsigned int) XIAnyModifier : (unsigned int) AnyModifier;
  374. if (pFirstGrab->grabtype != pSecondGrab->grabtype)
  375. return FALSE;
  376. if (pFirstGrab->grabtype == XI2) {
  377. if (pFirstGrab->device == inputInfo.all_devices ||
  378. pSecondGrab->device == inputInfo.all_devices) {
  379. /* do nothing */
  380. }
  381. else if (pFirstGrab->device == inputInfo.all_master_devices) {
  382. if (pSecondGrab->device != inputInfo.all_master_devices &&
  383. !IsMaster(pSecondGrab->device))
  384. return FALSE;
  385. }
  386. else if (pSecondGrab->device == inputInfo.all_master_devices) {
  387. if (pFirstGrab->device != inputInfo.all_master_devices &&
  388. !IsMaster(pFirstGrab->device))
  389. return FALSE;
  390. }
  391. else if (pSecondGrab->device != pFirstGrab->device)
  392. return FALSE;
  393. }
  394. else if (!ignoreDevice &&
  395. ((pFirstGrab->device != pSecondGrab->device) ||
  396. (pFirstGrab->modifierDevice != pSecondGrab->modifierDevice)))
  397. return FALSE;
  398. if (pFirstGrab->type != pSecondGrab->type)
  399. return FALSE;
  400. if (GrabSupersedesSecond(pFirstGrab, pSecondGrab) ||
  401. GrabSupersedesSecond(pSecondGrab, pFirstGrab))
  402. return TRUE;
  403. if (DetailSupersedesSecond(pSecondGrab->detail, pFirstGrab->detail,
  404. (unsigned int) AnyKey)
  405. &&
  406. DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  407. pSecondGrab->modifiersDetail, any_modifier))
  408. return TRUE;
  409. if (DetailSupersedesSecond(pFirstGrab->detail, pSecondGrab->detail,
  410. (unsigned int) AnyKey)
  411. &&
  412. DetailSupersedesSecond(pSecondGrab->modifiersDetail,
  413. pFirstGrab->modifiersDetail, any_modifier))
  414. return TRUE;
  415. return FALSE;
  416. }
  417. static Bool
  418. GrabsAreIdentical(GrabPtr pFirstGrab, GrabPtr pSecondGrab)
  419. {
  420. unsigned int any_modifier = (pFirstGrab->grabtype == XI2) ?
  421. (unsigned int) XIAnyModifier : (unsigned int) AnyModifier;
  422. if (pFirstGrab->grabtype != pSecondGrab->grabtype)
  423. return FALSE;
  424. if (pFirstGrab->device != pSecondGrab->device ||
  425. (pFirstGrab->modifierDevice != pSecondGrab->modifierDevice) ||
  426. (pFirstGrab->type != pSecondGrab->type))
  427. return FALSE;
  428. if (!(DetailSupersedesSecond(pFirstGrab->detail,
  429. pSecondGrab->detail,
  430. (unsigned int) AnyKey) &&
  431. DetailSupersedesSecond(pSecondGrab->detail,
  432. pFirstGrab->detail, (unsigned int) AnyKey)))
  433. return FALSE;
  434. if (!(DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  435. pSecondGrab->modifiersDetail,
  436. any_modifier) &&
  437. DetailSupersedesSecond(pSecondGrab->modifiersDetail,
  438. pFirstGrab->modifiersDetail, any_modifier)))
  439. return FALSE;
  440. return TRUE;
  441. }
  442. /**
  443. * Prepend the new grab to the list of passive grabs on the window.
  444. * Any previously existing grab that matches the new grab will be removed.
  445. * Adding a new grab that would override another client's grab will result in
  446. * a BadAccess.
  447. *
  448. * @return Success or X error code on failure.
  449. */
  450. int
  451. AddPassiveGrabToList(ClientPtr client, GrabPtr pGrab)
  452. {
  453. GrabPtr grab;
  454. Mask access_mode = DixGrabAccess;
  455. int rc;
  456. for (grab = wPassiveGrabs(pGrab->window); grab; grab = grab->next) {
  457. if (GrabMatchesSecond(pGrab, grab, (pGrab->grabtype == CORE))) {
  458. if (CLIENT_BITS(pGrab->resource) != CLIENT_BITS(grab->resource)) {
  459. FreeGrab(pGrab);
  460. return BadAccess;
  461. }
  462. }
  463. }
  464. if (pGrab->keyboardMode == GrabModeSync ||
  465. pGrab->pointerMode == GrabModeSync)
  466. access_mode |= DixFreezeAccess;
  467. rc = XaceHook(XACE_DEVICE_ACCESS, client, pGrab->device, access_mode);
  468. if (rc != Success)
  469. return rc;
  470. /* Remove all grabs that match the new one exactly */
  471. for (grab = wPassiveGrabs(pGrab->window); grab; grab = grab->next) {
  472. if (GrabsAreIdentical(pGrab, grab)) {
  473. DeletePassiveGrabFromList(grab);
  474. break;
  475. }
  476. }
  477. if (!pGrab->window->optional && !MakeWindowOptional(pGrab->window)) {
  478. FreeGrab(pGrab);
  479. return BadAlloc;
  480. }
  481. pGrab->next = pGrab->window->optional->passiveGrabs;
  482. pGrab->window->optional->passiveGrabs = pGrab;
  483. if (AddResource(pGrab->resource, RT_PASSIVEGRAB, (void *) pGrab))
  484. return Success;
  485. return BadAlloc;
  486. }
  487. /* the following is kinda complicated, because we need to be able to back out
  488. * if any allocation fails
  489. */
  490. Bool
  491. DeletePassiveGrabFromList(GrabPtr pMinuendGrab)
  492. {
  493. GrabPtr grab;
  494. GrabPtr *deletes, *adds;
  495. Mask ***updates, **details;
  496. int i, ndels, nadds, nups;
  497. Bool ok;
  498. unsigned int any_modifier;
  499. unsigned int any_key;
  500. #define UPDATE(mask,exact) \
  501. if (!(details[nups] = DeleteDetailFromMask(mask, exact))) \
  502. ok = FALSE; \
  503. else \
  504. updates[nups++] = &(mask)
  505. i = 0;
  506. for (grab = wPassiveGrabs(pMinuendGrab->window); grab; grab = grab->next)
  507. i++;
  508. if (!i)
  509. return TRUE;
  510. deletes = malloc(i * sizeof(GrabPtr));
  511. adds = malloc(i * sizeof(GrabPtr));
  512. updates = malloc(i * sizeof(Mask **));
  513. details = malloc(i * sizeof(Mask *));
  514. if (!deletes || !adds || !updates || !details) {
  515. free(details);
  516. free(updates);
  517. free(adds);
  518. free(deletes);
  519. return FALSE;
  520. }
  521. any_modifier = (pMinuendGrab->grabtype == XI2) ?
  522. (unsigned int) XIAnyModifier : (unsigned int) AnyModifier;
  523. any_key = (pMinuendGrab->grabtype == XI2) ?
  524. (unsigned int) XIAnyKeycode : (unsigned int) AnyKey;
  525. ndels = nadds = nups = 0;
  526. ok = TRUE;
  527. for (grab = wPassiveGrabs(pMinuendGrab->window);
  528. grab && ok; grab = grab->next) {
  529. if ((CLIENT_BITS(grab->resource) != CLIENT_BITS(pMinuendGrab->resource))
  530. || !GrabMatchesSecond(grab, pMinuendGrab, (grab->grabtype == CORE)))
  531. continue;
  532. if (GrabSupersedesSecond(pMinuendGrab, grab)) {
  533. deletes[ndels++] = grab;
  534. }
  535. else if ((grab->detail.exact == any_key)
  536. && (grab->modifiersDetail.exact != any_modifier)) {
  537. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  538. }
  539. else if ((grab->modifiersDetail.exact == any_modifier)
  540. && (grab->detail.exact != any_key)) {
  541. UPDATE(grab->modifiersDetail.pMask,
  542. pMinuendGrab->modifiersDetail.exact);
  543. }
  544. else if ((pMinuendGrab->detail.exact != any_key)
  545. && (pMinuendGrab->modifiersDetail.exact != any_modifier)) {
  546. GrabPtr pNewGrab;
  547. GrabParameters param;
  548. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  549. memset(&param, 0, sizeof(param));
  550. param.ownerEvents = grab->ownerEvents;
  551. param.this_device_mode = grab->keyboardMode;
  552. param.other_devices_mode = grab->pointerMode;
  553. param.modifiers = any_modifier;
  554. pNewGrab = CreateGrab(CLIENT_ID(grab->resource), grab->device,
  555. grab->modifierDevice, grab->window,
  556. grab->grabtype,
  557. (GrabMask *) &grab->eventMask,
  558. &param, (int) grab->type,
  559. pMinuendGrab->detail.exact,
  560. grab->confineTo, grab->cursor);
  561. if (!pNewGrab)
  562. ok = FALSE;
  563. else if (!(pNewGrab->modifiersDetail.pMask =
  564. DeleteDetailFromMask(grab->modifiersDetail.pMask,
  565. pMinuendGrab->modifiersDetail.
  566. exact))
  567. || (!pNewGrab->window->optional &&
  568. !MakeWindowOptional(pNewGrab->window))) {
  569. FreeGrab(pNewGrab);
  570. ok = FALSE;
  571. }
  572. else if (!AddResource(pNewGrab->resource, RT_PASSIVEGRAB,
  573. (void *) pNewGrab))
  574. ok = FALSE;
  575. else
  576. adds[nadds++] = pNewGrab;
  577. }
  578. else if (pMinuendGrab->detail.exact == any_key) {
  579. UPDATE(grab->modifiersDetail.pMask,
  580. pMinuendGrab->modifiersDetail.exact);
  581. }
  582. else {
  583. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  584. }
  585. }
  586. if (!ok) {
  587. for (i = 0; i < nadds; i++)
  588. FreeResource(adds[i]->resource, RT_NONE);
  589. for (i = 0; i < nups; i++)
  590. free(details[i]);
  591. }
  592. else {
  593. for (i = 0; i < ndels; i++)
  594. FreeResource(deletes[i]->resource, RT_NONE);
  595. for (i = 0; i < nadds; i++) {
  596. grab = adds[i];
  597. grab->next = grab->window->optional->passiveGrabs;
  598. grab->window->optional->passiveGrabs = grab;
  599. }
  600. for (i = 0; i < nups; i++) {
  601. free(*updates[i]);
  602. *updates[i] = details[i];
  603. }
  604. }
  605. free(details);
  606. free(updates);
  607. free(adds);
  608. free(deletes);
  609. return ok;
  610. #undef UPDATE
  611. }
  612. Bool
  613. GrabIsPointerGrab(GrabPtr grab)
  614. {
  615. return (grab->type == ButtonPress ||
  616. grab->type == DeviceButtonPress || grab->type == XI_ButtonPress);
  617. }
  618. Bool
  619. GrabIsKeyboardGrab(GrabPtr grab)
  620. {
  621. return (grab->type == KeyPress ||
  622. grab->type == DeviceKeyPress || grab->type == XI_KeyPress);
  623. }