xtest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. Copyright 1992, 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. */
  22. #ifdef HAVE_DIX_CONFIG_H
  23. #include <dix-config.h>
  24. #endif
  25. #include <X11/X.h>
  26. #include <X11/Xproto.h>
  27. #include "misc.h"
  28. #include "os.h"
  29. #include "dixstruct.h"
  30. #include "extnsionst.h"
  31. #include "windowstr.h"
  32. #include "inputstr.h"
  33. #include "scrnintstr.h"
  34. #include "dixevents.h"
  35. #include "sleepuntil.h"
  36. #define _XTEST_SERVER_
  37. #include <X11/extensions/xtestproto.h>
  38. #include "extinit.h"
  39. #if 0
  40. static unsigned char XTestReqCode;
  41. #endif
  42. static void XTestResetProc(ExtensionEntry * /* extEntry */
  43. );
  44. static int XTestSwapFakeInput(ClientPtr /* client */ ,
  45. xReq * /* req */
  46. );
  47. static DISPATCH_PROC(ProcXTestCompareCursor);
  48. static DISPATCH_PROC(ProcXTestDispatch);
  49. static DISPATCH_PROC(ProcXTestFakeInput);
  50. static DISPATCH_PROC(ProcXTestGetVersion);
  51. static DISPATCH_PROC(ProcXTestGrabControl);
  52. static DISPATCH_PROC(SProcXTestCompareCursor);
  53. static DISPATCH_PROC(SProcXTestDispatch);
  54. static DISPATCH_PROC(SProcXTestFakeInput);
  55. static DISPATCH_PROC(SProcXTestGetVersion);
  56. static DISPATCH_PROC(SProcXTestGrabControl);
  57. void
  58. XTestExtensionInit(INITARGS)
  59. {
  60. #if 0
  61. ExtensionEntry *extEntry;
  62. if ((extEntry = AddExtension(XTestExtensionName, 0, 0,
  63. ProcXTestDispatch, SProcXTestDispatch,
  64. XTestResetProc, StandardMinorOpcode)) != 0)
  65. XTestReqCode = (unsigned char) extEntry->base;
  66. #else
  67. (void) AddExtension(XTestExtensionName, 0, 0,
  68. ProcXTestDispatch, SProcXTestDispatch,
  69. XTestResetProc, StandardMinorOpcode);
  70. #endif
  71. }
  72. /*ARGSUSED*/ static void
  73. XTestResetProc(extEntry)
  74. ExtensionEntry *extEntry;
  75. {
  76. }
  77. static int
  78. ProcXTestGetVersion(client)
  79. ClientPtr client;
  80. {
  81. xXTestGetVersionReply rep;
  82. REQUEST_SIZE_MATCH(xXTestGetVersionReq);
  83. rep.type = X_Reply;
  84. rep.length = 0;
  85. rep.sequenceNumber = client->sequence;
  86. rep.majorVersion = XTestMajorVersion;
  87. rep.minorVersion = XTestMinorVersion;
  88. if (client->swapped) {
  89. swaps(&rep.sequenceNumber);
  90. swaps(&rep.minorVersion);
  91. }
  92. WriteToClient(client, sizeof(xXTestGetVersionReply), (char *) &rep);
  93. return (client->noClientException);
  94. }
  95. static int
  96. ProcXTestCompareCursor(client)
  97. ClientPtr client;
  98. {
  99. REQUEST(xXTestCompareCursorReq);
  100. xXTestCompareCursorReply rep;
  101. WindowPtr pWin;
  102. CursorPtr pCursor;
  103. REQUEST_SIZE_MATCH(xXTestCompareCursorReq);
  104. pWin = (WindowPtr) LookupWindow(stuff->window, client);
  105. if (!pWin)
  106. return (BadWindow);
  107. if (stuff->cursor == None)
  108. pCursor = NullCursor;
  109. else if (stuff->cursor == XTestCurrentCursor)
  110. pCursor = GetSpriteCursor();
  111. else {
  112. pCursor = (CursorPtr) LookupIDByType(stuff->cursor, RT_CURSOR);
  113. if (!pCursor) {
  114. client->errorValue = stuff->cursor;
  115. return (BadCursor);
  116. }
  117. }
  118. rep.type = X_Reply;
  119. rep.length = 0;
  120. rep.sequenceNumber = client->sequence;
  121. rep.same = (wCursor(pWin) == pCursor);
  122. if (client->swapped) {
  123. swaps(&rep.sequenceNumber);
  124. }
  125. WriteToClient(client, sizeof(xXTestCompareCursorReply), (char *) &rep);
  126. return (client->noClientException);
  127. }
  128. static int
  129. ProcXTestFakeInput(client)
  130. ClientPtr client;
  131. {
  132. REQUEST(xXTestFakeInputReq);
  133. int nev;
  134. xEvent *ev;
  135. DeviceIntPtr dev = NULL;
  136. WindowPtr root;
  137. int type;
  138. nev = (stuff->length << 2) - sizeof(xReq);
  139. if ((nev % sizeof(xEvent)) || !nev)
  140. return BadLength;
  141. nev /= sizeof(xEvent);
  142. UpdateCurrentTime();
  143. ev = (xEvent *) &((xReq *) stuff)[1];
  144. type = ev->u.u.type & 0177;
  145. {
  146. if (nev != 1)
  147. return BadLength;
  148. switch (type) {
  149. case KeyPress:
  150. case KeyRelease:
  151. case MotionNotify:
  152. case ButtonPress:
  153. case ButtonRelease:
  154. break;
  155. default:
  156. client->errorValue = ev->u.u.type;
  157. return BadValue;
  158. }
  159. }
  160. if (ev->u.keyButtonPointer.time) {
  161. TimeStamp activateTime;
  162. CARD32 ms;
  163. activateTime = currentTime;
  164. ms = activateTime.milliseconds + ev->u.keyButtonPointer.time;
  165. if (ms < activateTime.milliseconds)
  166. activateTime.months++;
  167. activateTime.milliseconds = ms;
  168. ev->u.keyButtonPointer.time = 0;
  169. /* see mbuf.c:QueueDisplayRequest for code similar to this */
  170. if (!ClientSleepUntil(client, &activateTime, NULL, NULL)) {
  171. return BadAlloc;
  172. }
  173. /* swap the request back so we can simply re-execute it */
  174. if (client->swapped) {
  175. (void) XTestSwapFakeInput(client, (xReq *) stuff);
  176. swaps(&stuff->length);
  177. }
  178. ResetCurrentRequest(client);
  179. client->sequence--;
  180. return Success;
  181. }
  182. switch (type) {
  183. case KeyPress:
  184. case KeyRelease:
  185. dev = (DeviceIntPtr) LookupKeyboardDevice();
  186. if (ev->u.u.detail < dev->key->curKeySyms.minKeyCode ||
  187. ev->u.u.detail > dev->key->curKeySyms.maxKeyCode) {
  188. client->errorValue = ev->u.u.detail;
  189. return BadValue;
  190. }
  191. break;
  192. case MotionNotify:
  193. dev = (DeviceIntPtr) LookupPointerDevice();
  194. if (ev->u.keyButtonPointer.root == None)
  195. root = GetCurrentRootWindow();
  196. else {
  197. root = LookupWindow(ev->u.keyButtonPointer.root, client);
  198. if (!root)
  199. return BadWindow;
  200. if (root->parent) {
  201. client->errorValue = ev->u.keyButtonPointer.root;
  202. return BadValue;
  203. }
  204. }
  205. if (ev->u.u.detail == xTrue) {
  206. int x, y;
  207. GetSpritePosition(&x, &y);
  208. ev->u.keyButtonPointer.rootX += x;
  209. ev->u.keyButtonPointer.rootY += y;
  210. }
  211. else if (ev->u.u.detail != xFalse) {
  212. client->errorValue = ev->u.u.detail;
  213. return BadValue;
  214. }
  215. if (ev->u.keyButtonPointer.rootX < 0)
  216. ev->u.keyButtonPointer.rootX = 0;
  217. else if (ev->u.keyButtonPointer.rootX >= root->drawable.width)
  218. ev->u.keyButtonPointer.rootX = root->drawable.width - 1;
  219. if (ev->u.keyButtonPointer.rootY < 0)
  220. ev->u.keyButtonPointer.rootY = 0;
  221. else if (ev->u.keyButtonPointer.rootY >= root->drawable.height)
  222. ev->u.keyButtonPointer.rootY = root->drawable.height - 1;
  223. if (root != GetCurrentRootWindow())
  224. {
  225. NewCurrentScreen(root->drawable.pScreen,
  226. ev->u.keyButtonPointer.rootX,
  227. ev->u.keyButtonPointer.rootY);
  228. return client->noClientException;
  229. }
  230. (*root->drawable.pScreen->SetCursorPosition)
  231. (root->drawable.pScreen,
  232. ev->u.keyButtonPointer.rootX, ev->u.keyButtonPointer.rootY, FALSE);
  233. break;
  234. case ButtonPress:
  235. case ButtonRelease:
  236. dev = (DeviceIntPtr) LookupPointerDevice();
  237. if (!ev->u.u.detail || ev->u.u.detail > dev->button->numButtons) {
  238. client->errorValue = ev->u.u.detail;
  239. return BadValue;
  240. }
  241. break;
  242. }
  243. if (screenIsSaved == SCREEN_SAVER_ON)
  244. SaveScreens(SCREEN_SAVER_OFF, ScreenSaverReset);
  245. ev->u.keyButtonPointer.time = currentTime.milliseconds;
  246. (*dev->public.processInputProc) (ev, dev, nev);
  247. return client->noClientException;
  248. }
  249. static int
  250. ProcXTestGrabControl(client)
  251. ClientPtr client;
  252. {
  253. REQUEST(xXTestGrabControlReq);
  254. REQUEST_SIZE_MATCH(xXTestGrabControlReq);
  255. if ((stuff->impervious != xTrue) && (stuff->impervious != xFalse)) {
  256. client->errorValue = stuff->impervious;
  257. return (BadValue);
  258. }
  259. if (stuff->impervious)
  260. MakeClientGrabImpervious(client);
  261. else
  262. MakeClientGrabPervious(client);
  263. return (client->noClientException);
  264. }
  265. static int
  266. ProcXTestDispatch(client)
  267. ClientPtr client;
  268. {
  269. REQUEST(xReq);
  270. switch (stuff->data) {
  271. case X_XTestGetVersion:
  272. return ProcXTestGetVersion(client);
  273. case X_XTestCompareCursor:
  274. return ProcXTestCompareCursor(client);
  275. case X_XTestFakeInput:
  276. return ProcXTestFakeInput(client);
  277. case X_XTestGrabControl:
  278. return ProcXTestGrabControl(client);
  279. default:
  280. return BadRequest;
  281. }
  282. }
  283. static int
  284. SProcXTestGetVersion(client)
  285. ClientPtr client;
  286. {
  287. REQUEST(xXTestGetVersionReq);
  288. swaps(&stuff->length);
  289. REQUEST_SIZE_MATCH(xXTestGetVersionReq);
  290. swaps(&stuff->minorVersion);
  291. return ProcXTestGetVersion(client);
  292. }
  293. static int
  294. SProcXTestCompareCursor(client)
  295. ClientPtr client;
  296. {
  297. REQUEST(xXTestCompareCursorReq);
  298. swaps(&stuff->length);
  299. REQUEST_SIZE_MATCH(xXTestCompareCursorReq);
  300. swapl(&stuff->window);
  301. swapl(&stuff->cursor);
  302. return ProcXTestCompareCursor(client);
  303. }
  304. static int
  305. XTestSwapFakeInput(client, req)
  306. ClientPtr client;
  307. xReq *req;
  308. {
  309. int nev;
  310. xEvent *ev;
  311. xEvent sev;
  312. EventSwapPtr proc;
  313. nev = ((req->length << 2) - sizeof(xReq)) / sizeof(xEvent);
  314. for (ev = (xEvent *) &req[1]; --nev >= 0; ev++) {
  315. /* Swap event */
  316. proc = EventSwapVector[ev->u.u.type & 0177];
  317. /* no swapping proc; invalid event type? */
  318. if (!proc || proc == NotImplemented) {
  319. client->errorValue = ev->u.u.type;
  320. return BadValue;
  321. }
  322. (*proc) (ev, &sev);
  323. *ev = sev;
  324. }
  325. return Success;
  326. }
  327. static int
  328. SProcXTestFakeInput(client)
  329. ClientPtr client;
  330. {
  331. REQUEST(xReq);
  332. swaps(&stuff->length);
  333. int n = XTestSwapFakeInput(client, stuff);
  334. if (n != Success)
  335. return n;
  336. return ProcXTestFakeInput(client);
  337. }
  338. static int
  339. SProcXTestGrabControl(client)
  340. ClientPtr client;
  341. {
  342. REQUEST(xXTestGrabControlReq);
  343. swaps(&stuff->length);
  344. REQUEST_SIZE_MATCH(xXTestGrabControlReq);
  345. return ProcXTestGrabControl(client);
  346. }
  347. static int
  348. SProcXTestDispatch(client)
  349. ClientPtr client;
  350. {
  351. REQUEST(xReq);
  352. switch (stuff->data) {
  353. case X_XTestGetVersion:
  354. return SProcXTestGetVersion(client);
  355. case X_XTestCompareCursor:
  356. return SProcXTestCompareCursor(client);
  357. case X_XTestFakeInput:
  358. return SProcXTestFakeInput(client);
  359. case X_XTestGrabControl:
  360. return SProcXTestGrabControl(client);
  361. default:
  362. return BadRequest;
  363. }
  364. }