winwindowswm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* WindowsWM extension is based on AppleWM extension */
  2. /**************************************************************************
  3. Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
  4. Copyright (c) 2003 Torrey T. Lyons. All Rights Reserved.
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sub license, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice (including the
  13. next paragraph) shall be included in all copies or substantial portions
  14. of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  18. IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
  19. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. **************************************************************************/
  23. #ifdef HAVE_XWIN_CONFIG_H
  24. #include <xwin-config.h>
  25. #endif
  26. #include "win.h"
  27. #include "misc.h"
  28. #include "dixstruct.h"
  29. #include "extnsionst.h"
  30. #include "colormapst.h"
  31. #include "cursorstr.h"
  32. #include "scrnintstr.h"
  33. #include "servermd.h"
  34. #include "swaprep.h"
  35. #define _WINDOWSWM_SERVER_
  36. #include <X11/extensions/windowswmstr.h>
  37. #include "protocol-versions.h"
  38. static int WMErrorBase;
  39. static unsigned char WMReqCode = 0;
  40. static int WMEventBase = 0;
  41. static RESTYPE ClientType, eventResourceType; /* resource types for event masks */
  42. static XID eventResource;
  43. /* Currently selected events */
  44. static unsigned int eventMask = 0;
  45. static int WMFreeClient(void *data, XID id);
  46. static int WMFreeEvents(void *data, XID id);
  47. static void SNotifyEvent(xWindowsWMNotifyEvent * from,
  48. xWindowsWMNotifyEvent * to);
  49. typedef struct _WMEvent *WMEventPtr;
  50. typedef struct _WMEvent {
  51. WMEventPtr next;
  52. ClientPtr client;
  53. XID clientResource;
  54. unsigned int mask;
  55. } WMEventRec;
  56. static int
  57. ProcWindowsWMQueryVersion(ClientPtr client)
  58. {
  59. xWindowsWMQueryVersionReply rep;
  60. REQUEST_SIZE_MATCH(xWindowsWMQueryVersionReq);
  61. rep.type = X_Reply;
  62. rep.length = 0;
  63. rep.sequenceNumber = client->sequence;
  64. rep.majorVersion = SERVER_WINDOWSWM_MAJOR_VERSION;
  65. rep.minorVersion = SERVER_WINDOWSWM_MINOR_VERSION;
  66. rep.patchVersion = SERVER_WINDOWSWM_PATCH_VERSION;
  67. if (client->swapped) {
  68. swaps(&rep.sequenceNumber);
  69. swapl(&rep.length);
  70. }
  71. WriteToClient(client, sizeof(xWindowsWMQueryVersionReply), &rep);
  72. return Success;
  73. }
  74. /* events */
  75. static inline void
  76. updateEventMask(WMEventPtr * pHead)
  77. {
  78. WMEventPtr pCur;
  79. eventMask = 0;
  80. for (pCur = *pHead; pCur != NULL; pCur = pCur->next)
  81. eventMask |= pCur->mask;
  82. }
  83. /*ARGSUSED*/ static int
  84. WMFreeClient(void *data, XID id)
  85. {
  86. WMEventPtr pEvent;
  87. WMEventPtr *pHead, pCur, pPrev;
  88. pEvent = (WMEventPtr) data;
  89. dixLookupResourceByType((void *) &pHead, eventResource, eventResourceType,
  90. NullClient, DixUnknownAccess);
  91. if (pHead) {
  92. pPrev = 0;
  93. for (pCur = *pHead; pCur && pCur != pEvent; pCur = pCur->next)
  94. pPrev = pCur;
  95. if (pCur) {
  96. if (pPrev)
  97. pPrev->next = pEvent->next;
  98. else
  99. *pHead = pEvent->next;
  100. }
  101. updateEventMask(pHead);
  102. }
  103. free((void *) pEvent);
  104. return 1;
  105. }
  106. /*ARGSUSED*/ static int
  107. WMFreeEvents(void *data, XID id)
  108. {
  109. WMEventPtr *pHead, pCur, pNext;
  110. pHead = (WMEventPtr *) data;
  111. for (pCur = *pHead; pCur; pCur = pNext) {
  112. pNext = pCur->next;
  113. FreeResource(pCur->clientResource, ClientType);
  114. free((void *) pCur);
  115. }
  116. free((void *) pHead);
  117. eventMask = 0;
  118. return 1;
  119. }
  120. static int
  121. ProcWindowsWMSelectInput(ClientPtr client)
  122. {
  123. REQUEST(xWindowsWMSelectInputReq);
  124. WMEventPtr pEvent, pNewEvent, *pHead;
  125. XID clientResource;
  126. REQUEST_SIZE_MATCH(xWindowsWMSelectInputReq);
  127. dixLookupResourceByType((void *) &pHead, eventResource, eventResourceType,
  128. client, DixWriteAccess);
  129. if (stuff->mask != 0) {
  130. if (pHead) {
  131. /* check for existing entry. */
  132. for (pEvent = *pHead; pEvent; pEvent = pEvent->next) {
  133. if (pEvent->client == client) {
  134. pEvent->mask = stuff->mask;
  135. updateEventMask(pHead);
  136. return Success;
  137. }
  138. }
  139. }
  140. /* build the entry */
  141. pNewEvent = malloc(sizeof(WMEventRec));
  142. if (!pNewEvent)
  143. return BadAlloc;
  144. pNewEvent->next = 0;
  145. pNewEvent->client = client;
  146. pNewEvent->mask = stuff->mask;
  147. /*
  148. * add a resource that will be deleted when
  149. * the client goes away
  150. */
  151. clientResource = FakeClientID(client->index);
  152. pNewEvent->clientResource = clientResource;
  153. if (!AddResource(clientResource, ClientType, (void *) pNewEvent))
  154. return BadAlloc;
  155. /*
  156. * create a resource to contain a pointer to the list
  157. * of clients selecting input. This must be indirect as
  158. * the list may be arbitrarily rearranged which cannot be
  159. * done through the resource database.
  160. */
  161. if (!pHead) {
  162. pHead = malloc(sizeof(WMEventPtr));
  163. if (!pHead ||
  164. !AddResource(eventResource, eventResourceType, (void *) pHead))
  165. {
  166. FreeResource(clientResource, RT_NONE);
  167. return BadAlloc;
  168. }
  169. *pHead = 0;
  170. }
  171. pNewEvent->next = *pHead;
  172. *pHead = pNewEvent;
  173. updateEventMask(pHead);
  174. }
  175. else if (stuff->mask == 0) {
  176. /* delete the interest */
  177. if (pHead) {
  178. pNewEvent = 0;
  179. for (pEvent = *pHead; pEvent; pEvent = pEvent->next) {
  180. if (pEvent->client == client)
  181. break;
  182. pNewEvent = pEvent;
  183. }
  184. if (pEvent) {
  185. FreeResource(pEvent->clientResource, ClientType);
  186. if (pNewEvent)
  187. pNewEvent->next = pEvent->next;
  188. else
  189. *pHead = pEvent->next;
  190. free(pEvent);
  191. updateEventMask(pHead);
  192. }
  193. }
  194. }
  195. else {
  196. client->errorValue = stuff->mask;
  197. return BadValue;
  198. }
  199. return Success;
  200. }
  201. /*
  202. * deliver the event
  203. */
  204. void
  205. winWindowsWMSendEvent(int type, unsigned int mask, int which, int arg,
  206. Window window, int x, int y, int w, int h)
  207. {
  208. WMEventPtr *pHead, pEvent;
  209. ClientPtr client;
  210. xWindowsWMNotifyEvent se;
  211. #if CYGMULTIWINDOW_DEBUG
  212. ErrorF("winWindowsWMSendEvent %d %d %d %d, %d %d - %d %d\n",
  213. type, mask, which, arg, x, y, w, h);
  214. #endif
  215. dixLookupResourceByType((void *) &pHead, eventResource, eventResourceType,
  216. NullClient, DixUnknownAccess);
  217. if (!pHead)
  218. return;
  219. for (pEvent = *pHead; pEvent; pEvent = pEvent->next) {
  220. client = pEvent->client;
  221. #if CYGMULTIWINDOW_DEBUG
  222. ErrorF("winWindowsWMSendEvent - %p\n", client);
  223. #endif
  224. if ((pEvent->mask & mask) == 0) {
  225. continue;
  226. }
  227. #if CYGMULTIWINDOW_DEBUG
  228. ErrorF("winWindowsWMSendEvent - send\n");
  229. #endif
  230. se.type = type + WMEventBase;
  231. se.kind = which;
  232. se.window = window;
  233. se.arg = arg;
  234. se.x = x;
  235. se.y = y;
  236. se.w = w;
  237. se.h = h;
  238. se.time = currentTime.milliseconds;
  239. WriteEventsToClient(client, 1, (xEvent *) &se);
  240. }
  241. }
  242. /* general utility functions */
  243. static int
  244. ProcWindowsWMDisableUpdate(ClientPtr client)
  245. {
  246. REQUEST_SIZE_MATCH(xWindowsWMDisableUpdateReq);
  247. //winDisableUpdate();
  248. return Success;
  249. }
  250. static int
  251. ProcWindowsWMReenableUpdate(ClientPtr client)
  252. {
  253. REQUEST_SIZE_MATCH(xWindowsWMReenableUpdateReq);
  254. //winEnableUpdate();
  255. return Success;
  256. }
  257. /* window functions */
  258. static int
  259. ProcWindowsWMSetFrontProcess(ClientPtr client)
  260. {
  261. REQUEST_SIZE_MATCH(xWindowsWMSetFrontProcessReq);
  262. //QuartzMessageMainThread(kWindowsSetFrontProcess, NULL, 0);
  263. return Success;
  264. }
  265. /* frame functions */
  266. static int
  267. ProcWindowsWMFrameGetRect(ClientPtr client)
  268. {
  269. xWindowsWMFrameGetRectReply rep;
  270. RECT rcNew;
  271. REQUEST(xWindowsWMFrameGetRectReq);
  272. #if CYGMULTIWINDOW_DEBUG
  273. ErrorF("ProcWindowsWMFrameGetRect %d %d\n",
  274. (sizeof(xWindowsWMFrameGetRectReq) >> 2), (int) client->req_len);
  275. #endif
  276. REQUEST_SIZE_MATCH(xWindowsWMFrameGetRectReq);
  277. rep.type = X_Reply;
  278. rep.length = 0;
  279. rep.sequenceNumber = client->sequence;
  280. if (stuff->frame_rect != 0) {
  281. ErrorF("ProcWindowsWMFrameGetRect - stuff->frame_rect != 0\n");
  282. return BadValue;
  283. }
  284. /* Store the origin, height, and width in a rectangle structure */
  285. SetRect(&rcNew, stuff->ix, stuff->iy,
  286. stuff->ix + stuff->iw, stuff->iy + stuff->ih);
  287. #if CYGMULTIWINDOW_DEBUG
  288. ErrorF("ProcWindowsWMFrameGetRect - %d %d %d %d\n",
  289. stuff->ix, stuff->iy, stuff->ix + stuff->iw, stuff->iy + stuff->ih);
  290. #endif
  291. /*
  292. * Calculate the required size of the Windows window rectangle,
  293. * given the size of the Windows window client area.
  294. */
  295. AdjustWindowRectEx(&rcNew, stuff->frame_style, FALSE,
  296. stuff->frame_style_ex);
  297. rep.x = rcNew.left;
  298. rep.y = rcNew.top;
  299. rep.w = rcNew.right - rcNew.left;
  300. rep.h = rcNew.bottom - rcNew.top;
  301. #if CYGMULTIWINDOW_DEBUG
  302. ErrorF("ProcWindowsWMFrameGetRect - %d %d %d %d\n",
  303. rep.x, rep.y, rep.w, rep.h);
  304. #endif
  305. WriteToClient(client, sizeof(xWindowsWMFrameGetRectReply), &rep);
  306. return Success;
  307. }
  308. static int
  309. ProcWindowsWMFrameDraw(ClientPtr client)
  310. {
  311. REQUEST(xWindowsWMFrameDrawReq);
  312. WindowPtr pWin;
  313. win32RootlessWindowPtr pRLWinPriv;
  314. RECT rcNew;
  315. int nCmdShow, rc;
  316. RegionRec newShape;
  317. REQUEST_SIZE_MATCH(xWindowsWMFrameDrawReq);
  318. #if CYGMULTIWINDOW_DEBUG
  319. ErrorF("ProcWindowsWMFrameDraw\n");
  320. #endif
  321. rc = dixLookupWindow(&pWin, stuff->window, client, DixReadAccess);
  322. if (rc != Success)
  323. return rc;
  324. #if CYGMULTIWINDOW_DEBUG
  325. ErrorF("ProcWindowsWMFrameDraw - Window found\n");
  326. #endif
  327. pRLWinPriv = (win32RootlessWindowPtr) RootlessFrameForWindow(pWin, TRUE);
  328. if (pRLWinPriv == 0)
  329. return BadWindow;
  330. #if CYGMULTIWINDOW_DEBUG
  331. ErrorF("ProcWindowsWMFrameDraw - HWND %p 0x%08x 0x%08x\n",
  332. pRLWinPriv->hWnd, (int) stuff->frame_style,
  333. (int) stuff->frame_style_ex);
  334. ErrorF("ProcWindowsWMFrameDraw - %d %d %d %d\n",
  335. stuff->ix, stuff->iy, stuff->iw, stuff->ih);
  336. #endif
  337. /* Store the origin, height, and width in a rectangle structure */
  338. SetRect(&rcNew, stuff->ix, stuff->iy,
  339. stuff->ix + stuff->iw, stuff->iy + stuff->ih);
  340. /*
  341. * Calculate the required size of the Windows window rectangle,
  342. * given the size of the Windows window client area.
  343. */
  344. AdjustWindowRectEx(&rcNew, stuff->frame_style, FALSE,
  345. stuff->frame_style_ex);
  346. /* Set the window extended style flags */
  347. if (!SetWindowLongPtr(pRLWinPriv->hWnd, GWL_EXSTYLE, stuff->frame_style_ex)) {
  348. return BadValue;
  349. }
  350. /* Set the window standard style flags */
  351. if (!SetWindowLongPtr(pRLWinPriv->hWnd, GWL_STYLE, stuff->frame_style)) {
  352. return BadValue;
  353. }
  354. /* Flush the window style */
  355. if (!SetWindowPos(pRLWinPriv->hWnd, NULL,
  356. rcNew.left, rcNew.top,
  357. rcNew.right - rcNew.left, rcNew.bottom - rcNew.top,
  358. SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE)) {
  359. return BadValue;
  360. }
  361. if (!IsWindowVisible(pRLWinPriv->hWnd))
  362. nCmdShow = SW_HIDE;
  363. else
  364. nCmdShow = SW_SHOWNA;
  365. ShowWindow(pRLWinPriv->hWnd, nCmdShow);
  366. if (wBoundingShape(pWin) != NULL) {
  367. /* wBoundingShape is relative to *inner* origin of window.
  368. Translate by borderWidth to get the outside-relative position. */
  369. RegionNull(&newShape);
  370. RegionCopy(&newShape, wBoundingShape(pWin));
  371. RegionTranslate(&newShape, pWin->borderWidth, pWin->borderWidth);
  372. winMWExtWMReshapeFrame(pRLWinPriv, &newShape);
  373. RegionUninit(&newShape);
  374. }
  375. #if CYGMULTIWINDOW_DEBUG
  376. ErrorF("ProcWindowsWMFrameDraw - done\n");
  377. #endif
  378. return Success;
  379. }
  380. static int
  381. ProcWindowsWMFrameSetTitle(ClientPtr client)
  382. {
  383. unsigned int title_length, title_max;
  384. char *title_bytes;
  385. REQUEST(xWindowsWMFrameSetTitleReq);
  386. WindowPtr pWin;
  387. win32RootlessWindowPtr pRLWinPriv;
  388. int rc;
  389. #if CYGMULTIWINDOW_DEBUG
  390. ErrorF("ProcWindowsWMFrameSetTitle\n");
  391. #endif
  392. REQUEST_AT_LEAST_SIZE(xWindowsWMFrameSetTitleReq);
  393. rc = dixLookupWindow(&pWin, stuff->window, client, DixReadAccess);
  394. if (rc != Success)
  395. return rc;
  396. #if CYGMULTIWINDOW_DEBUG
  397. ErrorF("ProcWindowsWMFrameSetTitle - Window found\n");
  398. #endif
  399. title_length = stuff->title_length;
  400. title_max = (stuff->length << 2) - sizeof(xWindowsWMFrameSetTitleReq);
  401. if (title_max < title_length)
  402. return BadValue;
  403. #if CYGMULTIWINDOW_DEBUG
  404. ErrorF("ProcWindowsWMFrameSetTitle - length is valid\n");
  405. #endif
  406. title_bytes = malloc(title_length + 1);
  407. strncpy(title_bytes, (char *) &stuff[1], title_length);
  408. title_bytes[title_length] = '\0';
  409. pRLWinPriv = (win32RootlessWindowPtr) RootlessFrameForWindow(pWin, FALSE);
  410. if (pRLWinPriv == 0) {
  411. free(title_bytes);
  412. return BadWindow;
  413. }
  414. /* Flush the window style */
  415. SetWindowText(pRLWinPriv->hWnd, title_bytes);
  416. free(title_bytes);
  417. #if CYGMULTIWINDOW_DEBUG
  418. ErrorF("ProcWindowsWMFrameSetTitle - done\n");
  419. #endif
  420. return Success;
  421. }
  422. /* dispatch */
  423. static int
  424. ProcWindowsWMDispatch(ClientPtr client)
  425. {
  426. REQUEST(xReq);
  427. switch (stuff->data) {
  428. case X_WindowsWMQueryVersion:
  429. return ProcWindowsWMQueryVersion(client);
  430. }
  431. if (!client->local)
  432. return WMErrorBase + WindowsWMClientNotLocal;
  433. switch (stuff->data) {
  434. case X_WindowsWMSelectInput:
  435. return ProcWindowsWMSelectInput(client);
  436. case X_WindowsWMDisableUpdate:
  437. return ProcWindowsWMDisableUpdate(client);
  438. case X_WindowsWMReenableUpdate:
  439. return ProcWindowsWMReenableUpdate(client);
  440. case X_WindowsWMSetFrontProcess:
  441. return ProcWindowsWMSetFrontProcess(client);
  442. case X_WindowsWMFrameGetRect:
  443. return ProcWindowsWMFrameGetRect(client);
  444. case X_WindowsWMFrameDraw:
  445. return ProcWindowsWMFrameDraw(client);
  446. case X_WindowsWMFrameSetTitle:
  447. return ProcWindowsWMFrameSetTitle(client);
  448. default:
  449. return BadRequest;
  450. }
  451. }
  452. static void
  453. SNotifyEvent(xWindowsWMNotifyEvent * from, xWindowsWMNotifyEvent * to)
  454. {
  455. to->type = from->type;
  456. to->kind = from->kind;
  457. cpswaps(from->sequenceNumber, to->sequenceNumber);
  458. cpswapl(from->window, to->window);
  459. cpswapl(from->time, to->time);
  460. cpswapl(from->arg, to->arg);
  461. }
  462. static int
  463. SProcWindowsWMQueryVersion(ClientPtr client)
  464. {
  465. REQUEST(xWindowsWMQueryVersionReq);
  466. swaps(&stuff->length);
  467. return ProcWindowsWMQueryVersion(client);
  468. }
  469. static int
  470. SProcWindowsWMDispatch(ClientPtr client)
  471. {
  472. REQUEST(xReq);
  473. /* It is bound to be non-local when there is byte swapping */
  474. if (!client->local)
  475. return WMErrorBase + WindowsWMClientNotLocal;
  476. /* only local clients are allowed WM access */
  477. switch (stuff->data) {
  478. case X_WindowsWMQueryVersion:
  479. return SProcWindowsWMQueryVersion(client);
  480. default:
  481. return BadRequest;
  482. }
  483. }
  484. void
  485. winWindowsWMExtensionInit(void)
  486. {
  487. ExtensionEntry *extEntry;
  488. ClientType = CreateNewResourceType(WMFreeClient, "WMClient");
  489. eventResourceType = CreateNewResourceType(WMFreeEvents, "WMEvent");
  490. eventResource = FakeClientID(0);
  491. if (ClientType && eventResourceType &&
  492. (extEntry = AddExtension(WINDOWSWMNAME,
  493. WindowsWMNumberEvents,
  494. WindowsWMNumberErrors,
  495. ProcWindowsWMDispatch,
  496. SProcWindowsWMDispatch,
  497. NULL, StandardMinorOpcode))) {
  498. size_t i;
  499. WMReqCode = (unsigned char) extEntry->base;
  500. WMErrorBase = extEntry->errorBase;
  501. WMEventBase = extEntry->eventBase;
  502. for (i = 0; i < WindowsWMNumberEvents; i++)
  503. EventSwapVector[WMEventBase + i] = (EventSwapPtr) SNotifyEvent;
  504. }
  505. }