grabs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 "windowstr.h"
  45. #include "inputstr.h"
  46. #include "cursorstr.h"
  47. #include "dixgrabs.h"
  48. #define BITMASK(i) (((Mask)1) << ((i) & 31))
  49. #define MASKIDX(i) ((i) >> 5)
  50. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  51. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  52. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  53. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  54. GrabPtr
  55. CreateGrab(int client, DeviceIntPtr device, WindowPtr window, Mask eventMask, Bool ownerEvents, Bool keyboardMode, Bool pointerMode, DeviceIntPtr modDevice, unsigned short modifiers, int type, KeyCode keybut, /* key or button */
  56. WindowPtr confineTo, CursorPtr cursor)
  57. {
  58. GrabPtr grab;
  59. grab = malloc(sizeof(GrabRec));
  60. if (!grab)
  61. return (GrabPtr) NULL;
  62. grab->resource = FakeClientID(client);
  63. grab->device = device;
  64. grab->coreGrab = ((device == inputInfo.keyboard) ||
  65. (device == inputInfo.pointer));
  66. grab->window = window;
  67. grab->eventMask = eventMask;
  68. grab->ownerEvents = ownerEvents;
  69. grab->keyboardMode = keyboardMode;
  70. grab->pointerMode = pointerMode;
  71. grab->modifiersDetail.exact = modifiers;
  72. grab->modifiersDetail.pMask = NULL;
  73. grab->modifierDevice = modDevice;
  74. grab->coreMods = ((modDevice == inputInfo.keyboard) ||
  75. (modDevice == inputInfo.pointer));
  76. grab->type = type;
  77. grab->detail.exact = keybut;
  78. grab->detail.pMask = NULL;
  79. grab->confineTo = confineTo;
  80. grab->cursor = cursor;
  81. if (cursor)
  82. cursor->refcnt++;
  83. return grab;
  84. }
  85. static void
  86. FreeGrab(GrabPtr pGrab)
  87. {
  88. if (pGrab->modifiersDetail.pMask != NULL)
  89. free(pGrab->modifiersDetail.pMask);
  90. if (pGrab->detail.pMask != NULL)
  91. free(pGrab->detail.pMask);
  92. if (pGrab->cursor)
  93. FreeCursor(pGrab->cursor, (Cursor) 0);
  94. free(pGrab);
  95. }
  96. int
  97. DeletePassiveGrab(pointer value, XID id)
  98. {
  99. GrabPtr g, prev;
  100. GrabPtr pGrab = (GrabPtr) value;
  101. /* it is OK if the grab isn't found */
  102. prev = 0;
  103. for (g = (wPassiveGrabs(pGrab->window)); g; g = g->next) {
  104. if (pGrab == g) {
  105. if (prev)
  106. prev->next = g->next;
  107. else if (!(pGrab->window->optional->passiveGrabs = g->next))
  108. CheckWindowOptionalNeed(pGrab->window);
  109. break;
  110. }
  111. prev = g;
  112. }
  113. FreeGrab(pGrab);
  114. return Success;
  115. }
  116. static Mask *
  117. DeleteDetailFromMask(Mask *pDetailMask, unsigned short detail)
  118. {
  119. Mask *mask;
  120. int i;
  121. mask = malloc(sizeof(Mask) * MasksPerDetailMask);
  122. if (mask) {
  123. if (pDetailMask)
  124. for (i = 0; i < MasksPerDetailMask; i++)
  125. mask[i] = pDetailMask[i];
  126. else
  127. for (i = 0; i < MasksPerDetailMask; i++)
  128. mask[i] = ~0L;
  129. BITCLEAR(mask, detail);
  130. }
  131. return mask;
  132. }
  133. static Bool
  134. IsInGrabMask(DetailRec firstDetail,
  135. DetailRec secondDetail, unsigned short exception)
  136. {
  137. if (firstDetail.exact == exception) {
  138. if (firstDetail.pMask == NULL)
  139. return TRUE;
  140. /* (at present) never called with two non-null pMasks */
  141. if (secondDetail.exact == exception)
  142. return FALSE;
  143. if (GETBIT(firstDetail.pMask, secondDetail.exact))
  144. return TRUE;
  145. }
  146. return FALSE;
  147. }
  148. static Bool
  149. IdenticalExactDetails(unsigned short firstExact,
  150. unsigned short secondExact, unsigned short exception)
  151. {
  152. if ((firstExact == exception) || (secondExact == exception))
  153. return FALSE;
  154. if (firstExact == secondExact)
  155. return TRUE;
  156. return FALSE;
  157. }
  158. static Bool
  159. DetailSupersedesSecond(DetailRec firstDetail,
  160. DetailRec secondDetail, unsigned short exception)
  161. {
  162. if (IsInGrabMask(firstDetail, secondDetail, exception))
  163. return TRUE;
  164. if (IdenticalExactDetails(firstDetail.exact, secondDetail.exact, exception))
  165. return TRUE;
  166. return FALSE;
  167. }
  168. static Bool
  169. GrabSupersedesSecond(GrabPtr pFirstGrab, GrabPtr pSecondGrab)
  170. {
  171. if (!DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  172. pSecondGrab->modifiersDetail,
  173. (unsigned short) AnyModifier))
  174. return FALSE;
  175. if (DetailSupersedesSecond(pFirstGrab->detail,
  176. pSecondGrab->detail, (unsigned short) AnyKey))
  177. return TRUE;
  178. return FALSE;
  179. }
  180. Bool
  181. GrabMatchesSecond(GrabPtr pFirstGrab, GrabPtr pSecondGrab)
  182. {
  183. if ((pFirstGrab->device != pSecondGrab->device) ||
  184. (pFirstGrab->modifierDevice != pSecondGrab->modifierDevice) ||
  185. (pFirstGrab->type != pSecondGrab->type))
  186. return FALSE;
  187. if (GrabSupersedesSecond(pFirstGrab, pSecondGrab) ||
  188. GrabSupersedesSecond(pSecondGrab, pFirstGrab))
  189. return TRUE;
  190. if (DetailSupersedesSecond(pSecondGrab->detail, pFirstGrab->detail,
  191. (unsigned short) AnyKey)
  192. &&
  193. DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  194. pSecondGrab->modifiersDetail,
  195. (unsigned short) AnyModifier))
  196. return TRUE;
  197. if (DetailSupersedesSecond(pFirstGrab->detail, pSecondGrab->detail,
  198. (unsigned short) AnyKey)
  199. &&
  200. DetailSupersedesSecond(pSecondGrab->modifiersDetail,
  201. pFirstGrab->modifiersDetail,
  202. (unsigned short) AnyModifier))
  203. return TRUE;
  204. return FALSE;
  205. }
  206. int
  207. AddPassiveGrabToList(GrabPtr pGrab)
  208. {
  209. GrabPtr grab;
  210. for (grab = wPassiveGrabs(pGrab->window); grab; grab = grab->next) {
  211. if (GrabMatchesSecond(pGrab, grab)) {
  212. if (CLIENT_BITS(pGrab->resource) != CLIENT_BITS(grab->resource)) {
  213. FreeGrab(pGrab);
  214. return BadAccess;
  215. }
  216. }
  217. }
  218. if (!pGrab->window->optional && !MakeWindowOptional(pGrab->window)) {
  219. FreeGrab(pGrab);
  220. return BadAlloc;
  221. }
  222. pGrab->next = pGrab->window->optional->passiveGrabs;
  223. pGrab->window->optional->passiveGrabs = pGrab;
  224. if (AddResource(pGrab->resource, RT_PASSIVEGRAB, (pointer) pGrab))
  225. return Success;
  226. return BadAlloc;
  227. }
  228. /* the following is kinda complicated, because we need to be able to back out
  229. * if any allocation fails
  230. */
  231. Bool
  232. DeletePassiveGrabFromList(GrabPtr pMinuendGrab)
  233. {
  234. GrabPtr grab;
  235. GrabPtr *deletes, *adds;
  236. Mask ***updates, **details;
  237. int i, ndels, nadds, nups;
  238. Bool ok;
  239. #define UPDATE(mask,exact) \
  240. if (!(details[nups] = DeleteDetailFromMask(mask, exact))) \
  241. ok = FALSE; \
  242. else \
  243. updates[nups++] = &(mask)
  244. i = 0;
  245. for (grab = wPassiveGrabs(pMinuendGrab->window); grab; grab = grab->next)
  246. i++;
  247. if (!i)
  248. return TRUE;
  249. deletes = (GrabPtr *) ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  250. adds = (GrabPtr *) ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  251. updates = (Mask ***) ALLOCATE_LOCAL(i * sizeof(Mask **));
  252. details = (Mask **) ALLOCATE_LOCAL(i * sizeof(Mask *));
  253. if (!deletes || !adds || !updates || !details) {
  254. if (details)
  255. DEALLOCATE_LOCAL(details);
  256. if (updates)
  257. DEALLOCATE_LOCAL(updates);
  258. if (adds)
  259. DEALLOCATE_LOCAL(adds);
  260. if (deletes)
  261. DEALLOCATE_LOCAL(deletes);
  262. return FALSE;
  263. }
  264. ndels = nadds = nups = 0;
  265. ok = TRUE;
  266. for (grab = wPassiveGrabs(pMinuendGrab->window);
  267. grab && ok; grab = grab->next) {
  268. if ((CLIENT_BITS(grab->resource) != CLIENT_BITS(pMinuendGrab->resource))
  269. || !GrabMatchesSecond(grab, pMinuendGrab))
  270. continue;
  271. if (GrabSupersedesSecond(pMinuendGrab, grab)) {
  272. deletes[ndels++] = grab;
  273. }
  274. else if ((grab->detail.exact == AnyKey)
  275. && (grab->modifiersDetail.exact != AnyModifier)) {
  276. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  277. }
  278. else if ((grab->modifiersDetail.exact == AnyModifier)
  279. && (grab->detail.exact != AnyKey)) {
  280. UPDATE(grab->modifiersDetail.pMask,
  281. pMinuendGrab->modifiersDetail.exact);
  282. }
  283. else if ((pMinuendGrab->detail.exact != AnyKey)
  284. && (pMinuendGrab->modifiersDetail.exact != AnyModifier)) {
  285. GrabPtr pNewGrab;
  286. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  287. pNewGrab = CreateGrab(CLIENT_ID(grab->resource), grab->device,
  288. grab->window, (Mask) grab->eventMask,
  289. (Bool) grab->ownerEvents,
  290. (Bool) grab->keyboardMode,
  291. (Bool) grab->pointerMode,
  292. grab->modifierDevice,
  293. AnyModifier, (int) grab->type,
  294. pMinuendGrab->detail.exact,
  295. grab->confineTo, grab->cursor);
  296. if (!pNewGrab)
  297. ok = FALSE;
  298. else if (!(pNewGrab->modifiersDetail.pMask =
  299. DeleteDetailFromMask(grab->modifiersDetail.pMask,
  300. pMinuendGrab->modifiersDetail.
  301. exact))
  302. || (!pNewGrab->window->optional &&
  303. !MakeWindowOptional(pNewGrab->window))) {
  304. FreeGrab(pNewGrab);
  305. ok = FALSE;
  306. }
  307. else if (!AddResource(pNewGrab->resource, RT_PASSIVEGRAB,
  308. (pointer) pNewGrab))
  309. ok = FALSE;
  310. else
  311. adds[nadds++] = pNewGrab;
  312. }
  313. else if (pMinuendGrab->detail.exact == AnyKey) {
  314. UPDATE(grab->modifiersDetail.pMask,
  315. pMinuendGrab->modifiersDetail.exact);
  316. }
  317. else {
  318. UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  319. }
  320. }
  321. if (!ok) {
  322. for (i = 0; i < nadds; i++)
  323. FreeResource(adds[i]->resource, RT_NONE);
  324. for (i = 0; i < nups; i++)
  325. free(details[i]);
  326. }
  327. else {
  328. for (i = 0; i < ndels; i++)
  329. FreeResource(deletes[i]->resource, RT_NONE);
  330. for (i = 0; i < nadds; i++) {
  331. grab = adds[i];
  332. grab->next = grab->window->optional->passiveGrabs;
  333. grab->window->optional->passiveGrabs = grab;
  334. }
  335. for (i = 0; i < nups; i++) {
  336. free(*updates[i]);
  337. *updates[i] = details[i];
  338. }
  339. }
  340. DEALLOCATE_LOCAL(details);
  341. DEALLOCATE_LOCAL(updates);
  342. DEALLOCATE_LOCAL(adds);
  343. DEALLOCATE_LOCAL(deletes);
  344. return ok;
  345. #undef UPDATE
  346. }