Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4602 lines
160 KiB

  1. #include "precomp.h"
  2. //
  3. // IM.CPP
  4. // Input Manager
  5. //
  6. // Copyright(c) Microsoft 1997-
  7. //
  8. #include <confreg.h>
  9. #define MLZ_FILE_ZONE ZONE_INPUT
  10. //
  11. // IM_ShareStarting()
  12. //
  13. BOOL ASShare::IM_ShareStarting(void)
  14. {
  15. BOOL rc = FALSE;
  16. HKEY hkeyBandwidth;
  17. UINT i;
  18. BYTE tmpVK;
  19. DebugEntry(ASShare::IM_ShareStarting);
  20. //
  21. // Find out the scan codes for the left and right shift keys.
  22. //
  23. //
  24. // SFR 2537: Get the scan codes for this keyboard for the left-right
  25. // variants of SHIFT.
  26. //
  27. // We do not do this for the left-right variants of CONTROL and ALT (ie
  28. // menu) because they are extended keys.
  29. //
  30. // The scan codes are used in the keyboard hook (when sending) and in
  31. // the network translate to OS routine (when receiving), to
  32. // distinguish between the left-right variants of VK_SHIFT, where
  33. // Windows only reports a single value.
  34. //
  35. // This method is pretty long
  36. //
  37. m_imScanVKLShift = (BYTE) MapVirtualKey(VK_SHIFT, 0);
  38. for (i = 0; i < 256; i++)
  39. {
  40. tmpVK = (BYTE)MapVirtualKey(i, 1);
  41. if ( (tmpVK == VK_SHIFT) && (i != m_imScanVKLShift) )
  42. {
  43. m_imScanVKRShift = (BYTE)i;
  44. break;
  45. }
  46. }
  47. TRACE_OUT(( "Left/Right VK_SHIFT: scan codes = %02X, %02X",
  48. m_imScanVKLShift, m_imScanVKRShift));
  49. //
  50. // Check the user-reported bandwidth to decide if we should optimize
  51. // input for bandwidth or latency.
  52. // BUGBUG will want to vary this via flow control instead in future
  53. //
  54. m_imInControlMouseWithhold = 0;
  55. if (ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER,AUDIO_KEY,&hkeyBandwidth))
  56. {
  57. DWORD dwBandwidth = BW_DEFAULT;
  58. DWORD dwType = REG_DWORD;
  59. DWORD cbData = sizeof(dwBandwidth);
  60. if ( ERROR_SUCCESS == RegQueryValueEx(hkeyBandwidth,
  61. REGVAL_TYPICALBANDWIDTH, NULL, &dwType,
  62. (LPBYTE)&dwBandwidth, &cbData) )
  63. {
  64. if ( BW_144KBS == dwBandwidth )
  65. {
  66. m_imInControlMouseWithhold = IM_LOCAL_MOUSE_WITHHOLD;
  67. }
  68. }
  69. RegCloseKey(hkeyBandwidth);
  70. }
  71. //
  72. // Find out if this is a DBCS enabled system - if it is then we'll need
  73. // to load IMM32.DLL.
  74. //
  75. ASSERT(m_imImmLib == NULL);
  76. ASSERT(m_imImmGVK == NULL);
  77. if (GetSystemMetrics(SM_DBCSENABLED))
  78. {
  79. //
  80. // DBCS system, so load IMM32.DLL
  81. //
  82. m_imImmLib = LoadLibrary("imm32.dll");
  83. if (!m_imImmLib)
  84. {
  85. ERROR_OUT(( "Failed to load imm32.dll"));
  86. DC_QUIT;
  87. }
  88. //
  89. // Now attempt to find the entry point in this DLL.
  90. //
  91. m_imImmGVK = (IMMGVK) GetProcAddress(m_imImmLib, "ImmGetVirtualKey");
  92. if (!m_imImmGVK)
  93. {
  94. ERROR_OUT(( "Failed to fixup <ImmGetVirtualKey>"));
  95. DC_QUIT;
  96. }
  97. }
  98. rc = TRUE;
  99. DC_EXIT_POINT:
  100. DebugExitBOOL(ASShare::IM_ShareStarting, rc);
  101. return(rc);
  102. }
  103. //
  104. // IM_ShareEnded()
  105. //
  106. void ASShare::IM_ShareEnded(void)
  107. {
  108. DebugEntry(ASShare::IM_ShareEnded);
  109. // Free imm32 dll
  110. m_imImmGVK = NULL;
  111. if (m_imImmLib)
  112. {
  113. FreeLibrary(m_imImmLib);
  114. m_imImmLib = NULL;
  115. }
  116. DebugExitVOID(ASShare::IM_ShareEnded);
  117. }
  118. //
  119. // IM_Controlled()
  120. //
  121. // Called when we start/stop being controlled.
  122. //
  123. BOOL ASShare::IM_Controlled(ASPerson * pasControlledBy)
  124. {
  125. BOOL rc;
  126. DebugEntry(ASShare::IM_Controlled);
  127. if (pasControlledBy)
  128. {
  129. // Incoming injected input queues should be empty
  130. ASSERT(m_imControlledEventQ.numEvents == 0);
  131. ASSERT(m_imControlledEventQ.head == 0);
  132. ASSERT(m_imControlledOSQ.numEvents == 0);
  133. ASSERT(m_imControlledOSQ.head == 0);
  134. //
  135. // Reset CONTROLLED vars
  136. //
  137. m_imfControlledMouseButtonsReversed = (GetSystemMetrics(SM_SWAPBUTTON) != 0);
  138. m_imfControlledMouseClipped = FALSE;
  139. m_imfControlledPaceInjection = FALSE;
  140. m_imfControlledNewEvent = TRUE;
  141. m_imControlledNumEventsPending = 0;
  142. m_imControlledNumEventsReturned = 0;
  143. m_imControlledLastLowLevelMouseEventTime = GetTickCount();
  144. m_imControlledLastMouseRemoteTime = 0;
  145. m_imControlledLastMouseLocalTime = 0;
  146. m_imControlledLastIncompleteConversion = 0;
  147. m_imControlledMouseBacklog = 0;
  148. GetCursorPos(&m_imControlledLastMousePos);
  149. // Get current keyboard state
  150. GetKeyboardState(m_aimControlledKeyStates);
  151. // Save it so we can put it back when done being controlled
  152. ASSERT(sizeof(m_aimControlledSavedKeyStates) == sizeof(m_aimControlledKeyStates));
  153. CopyMemory(m_aimControlledSavedKeyStates, m_aimControlledKeyStates, sizeof(m_aimControlledKeyStates));
  154. // Clear original keyboard state
  155. ZeroMemory(m_aimControlledKeyStates, sizeof(m_aimControlledKeyStates));
  156. SetKeyboardState(m_aimControlledKeyStates);
  157. //
  158. // On the other side, the remote will start sending us events to
  159. // bring our keyboard in sync with his. Then real input events.
  160. //
  161. }
  162. else
  163. {
  164. //
  165. // We're no longer controlled. Clear the remote queues.
  166. //
  167. m_imControlledOSQ.head = 0;
  168. m_imControlledOSQ.numEvents = 0;
  169. m_imControlledEventQ.numEvents = 0;
  170. //
  171. // Put back our saved keyboard state
  172. //
  173. SetKeyboardState(m_aimControlledSavedKeyStates);
  174. }
  175. // Install controlled input hooks
  176. rc = OSI_InstallControlledHooks((pasControlledBy != NULL), (m_pasLocal->hetCount == HET_DESKTOPSHARED));
  177. if (!rc)
  178. {
  179. ERROR_OUT(("IM_Controlled: Couldn't install controlled hooks"));
  180. DC_QUIT;
  181. }
  182. g_lpimSharedData->imControlled = (pasControlledBy != NULL);
  183. DC_EXIT_POINT:
  184. DebugExitBOOL(ASShare:IM_Controlled, rc);
  185. return(rc);
  186. }
  187. //
  188. // IM_InControl()
  189. //
  190. // Called when we start/stop being in control. We must observe high-level
  191. // keyboard events.
  192. //
  193. void ASShare::IM_InControl(ASPerson * pasInControlOf)
  194. {
  195. DebugEntry(ASShare::IM_InControl);
  196. if (pasInControlOf)
  197. {
  198. //
  199. // Set up InControl vars.
  200. //
  201. // Get current key state
  202. GetKeyboardState(m_aimInControlKeyStates);
  203. m_imfInControlEventIsPending = FALSE;
  204. m_imfInControlCtrlDown = FALSE;
  205. m_imfInControlShiftDown = FALSE;
  206. m_imfInControlMenuDown = FALSE;
  207. m_imfInControlCapsLock = FALSE;
  208. m_imfInControlNumLock = FALSE;
  209. m_imfInControlScrollLock = FALSE;
  210. m_imfInControlConsumeMenuUp = FALSE;
  211. m_imfInControlConsumeEscapeUp = FALSE;
  212. m_imfInControlNewEvent = TRUE;
  213. m_imInControlMouseDownCount = 0;
  214. m_imInControlMouseDownTime = 0;
  215. m_imInControlMouseSpoilRate = 0;
  216. m_imInControlNumEventsPending = 0;
  217. m_imInControlNumEventsReturned = 0;
  218. m_imInControlNextHotKeyEntry = 0;
  219. //
  220. // Send mouse move with our current position to the dude we're in
  221. // control of.
  222. //
  223. ValidateView(pasInControlOf);
  224. ASSERT(pasInControlOf->m_caControlledBy == m_pasLocal);
  225. }
  226. else
  227. {
  228. // Clear outgoing queues
  229. m_imInControlEventQ.head = 0;
  230. m_imInControlEventQ.numEvents = 0;
  231. }
  232. DebugExitVOID(ASShare::IM_InControl);
  233. }
  234. //
  235. // IM_Periodic
  236. //
  237. void ASShare::IM_Periodic(void)
  238. {
  239. POINT cursorPos;
  240. UINT timeDelta;
  241. DebugEntry(ASShare::IM_Periodic);
  242. if (m_pasLocal->m_caInControlOf)
  243. {
  244. //
  245. // Send outgoing input to person we're in control of
  246. //
  247. IMFlushOutgoingEvents();
  248. }
  249. else if (m_pasLocal->m_caControlledBy)
  250. {
  251. ASSERT(m_pHost);
  252. //
  253. // Playback input from person in control of us
  254. //
  255. IMMaybeInjectEvents();
  256. //
  257. // Get the current cursor position - we always need this.
  258. //
  259. GetCursorPos(&cursorPos);
  260. //
  261. // First check if we think that a cursor clip will have affected the
  262. // position when we replayed a remote event.
  263. //
  264. if (m_imfControlledMouseClipped)
  265. {
  266. RECT cursorClip;
  267. //
  268. // Get the current clip and the current cursor position.
  269. //
  270. GetClipCursor(&cursorClip);
  271. if ((cursorPos.x == cursorClip.left) ||
  272. (cursorPos.x == (cursorClip.right-1)) ||
  273. (cursorPos.y == cursorClip.top) ||
  274. (cursorPos.y == (cursorClip.bottom-1)))
  275. {
  276. WARNING_OUT(("CM_ApplicationMovedCursor {%04d, %04d}",
  277. cursorPos.x, cursorPos.y));
  278. //
  279. // We thought the cursor was going to be clipped and now we
  280. // find it is right at the edge of the clip so tell the CM to
  281. // tell its peers about the cursor being moved.
  282. //
  283. m_pHost->CM_ApplicationMovedCursor();
  284. m_imfControlledMouseClipped = FALSE;
  285. }
  286. }
  287. // We are being controlled by somebody else.
  288. // So now's the time to decide if a SetCursorPos has
  289. // happened. For us to believe that a SetCursorPos has actually
  290. // occurred, the elapsed time since the last low-level input event
  291. // was injected must be greater than IM_EVENT_PERCOLATE_TIME
  292. // and the cursor must be in a different position to that which we
  293. // currently believe it to be.
  294. //
  295. if ((cursorPos.x != m_imControlledLastMousePos.x) ||
  296. (cursorPos.y != m_imControlledLastMousePos.y))
  297. {
  298. TRACE_OUT(( "GCP gives (%d,%d), last mouse event is (%d,%d)",
  299. cursorPos.x,
  300. cursorPos.y,
  301. m_imControlledLastMousePos.x,
  302. m_imControlledLastMousePos.y));
  303. //
  304. // Get the current tick count.
  305. //
  306. timeDelta = GetTickCount() - m_imControlledLastLowLevelMouseEventTime;
  307. if (timeDelta > IM_EVENT_PERCOLATE_TIME)
  308. {
  309. //
  310. // Looks like a SetCursorPos has occured - tell CM.
  311. //
  312. WARNING_OUT(("CM_ApplicationMovedCursor {%04d, %04d}",
  313. cursorPos.x, cursorPos.y));
  314. m_pHost->CM_ApplicationMovedCursor();
  315. //
  316. // Update the last high level mouse position.
  317. //
  318. m_imControlledLastMousePos.x = cursorPos.x;
  319. m_imControlledLastMousePos.y = cursorPos.y;
  320. }
  321. }
  322. }
  323. DebugExitVOID(ASShare::IM_Periodic);
  324. }
  325. //
  326. // IM_ReceivedPacket()
  327. //
  328. // A null packet pointer can be used to trigger the injection of another
  329. // pending event
  330. //
  331. //
  332. // DESCRIPTION:
  333. //
  334. // Called when an IM events packet arrives at the PR. The IM will accept
  335. // the incoming packet. It may copy it to an internal queue rather than
  336. // process it immediately. IM events packets contain a series of
  337. // piggybacked IM events.
  338. //
  339. // PARAMETERS:
  340. //
  341. // personID - the source of the packet
  342. //
  343. // pPacket - a pointer to the packet
  344. //
  345. // RETURNS: NONE
  346. //
  347. void ASShare::IM_ReceivedPacket
  348. (
  349. ASPerson * pasFrom,
  350. PS20DATAPACKET pPacket
  351. )
  352. {
  353. LPIMPACKET pIMPacket;
  354. UINT i;
  355. DebugEntry(ASShare::IM_ReceivedPacket);
  356. if (!pasFrom)
  357. {
  358. TRACE_OUT(("Simply inject any pending events in"));
  359. DC_QUIT;
  360. }
  361. ValidatePerson(pasFrom);
  362. pIMPacket = (PIMPACKET)pPacket;
  363. // If this person isn't in control of us, blow this off
  364. if (pasFrom->m_caInControlOf != m_pasLocal)
  365. {
  366. PIMEVENT pimEvent;
  367. if (pasFrom->cpcCaps.general.version >= CAPS_VERSION_30)
  368. {
  369. WARNING_OUT(("Ignoring IM packet from [%d], not in control of us", pasFrom->mcsID));
  370. DC_QUIT;
  371. }
  372. //
  373. // 2.x COMPAT: not-controlled folks send IM packets as broadcasts.
  374. // Fake a mouse move move. Skip through all the events in the
  375. // packet to the last mouse move/click/pos info.
  376. //
  377. // Note that we don't have to fill in all the S20, S20DATAPACKET,
  378. // and DATAPACKET header info.
  379. //
  380. pimEvent = NULL;
  381. for (i = 0; i < pIMPacket->numEvents; i++)
  382. {
  383. if (pIMPacket->aEvents[i].type == IM_TYPE_3BUTTON)
  384. {
  385. pimEvent = &(pIMPacket->aEvents[i]);
  386. }
  387. }
  388. if (pimEvent)
  389. {
  390. // Pass fake packet with mouse pos to cursor manager
  391. TRACE_OUT(("Handling 2.x mouse event to {%04d, %04d}",
  392. pimEvent->data.mouse.x, pimEvent->data.mouse.y));
  393. CM_UpdateShadowCursor(pasFrom, pasFrom->cmShadowOff,
  394. pimEvent->data.mouse.x, pimEvent->data.mouse.y,
  395. pasFrom->cmHotSpot.x, pasFrom->cmHotSpot.y);
  396. }
  397. // Now we're done.
  398. DC_QUIT;
  399. }
  400. //
  401. // For each packet in the piggybacked packets array...
  402. //
  403. TRACE_OUT(("IM_ReceivedPacket: Processing packet with %d events",
  404. pIMPacket->numEvents));
  405. for (i = 0; i < pIMPacket->numEvents; i++)
  406. {
  407. switch (pIMPacket->aEvents[i].type)
  408. {
  409. case IM_TYPE_ASCII:
  410. case IM_TYPE_VK1:
  411. case IM_TYPE_VK2:
  412. case IM_TYPE_3BUTTON:
  413. {
  414. IMAppendNetEvent(&(pIMPacket->aEvents[i]));
  415. break;
  416. }
  417. default:
  418. //
  419. // Unexpected events are not error - we just ignore then
  420. // for future compatibility
  421. //
  422. TRACE_OUT(("Person [%d] unrecognised IM type (%04X) - event discarded",
  423. pasFrom->mcsID, pIMPacket->aEvents[i].type));
  424. break;
  425. }
  426. }
  427. DC_EXIT_POINT:
  428. //
  429. // Our final action is to feed one of the new events into USER.
  430. // We do NOT feed them all in at once because we want to simulate
  431. // typing them in, otherwise the amount of spoiling we see is
  432. // totally dependent upon the network latency and piggybacking.
  433. //
  434. ValidatePerson(m_pasLocal);
  435. if (m_pasLocal->m_caControlledBy)
  436. {
  437. //
  438. // @@@JPB: Temporary - want to inject as many events as possible -
  439. // this should be moved to a loop within IMMaybeInjectEvents...
  440. //
  441. // This greatly improves responsiveness when handling a large
  442. // number of input events in a short space of time (e.g. pounding
  443. // on the keyboard) - very little overrun.
  444. //
  445. for (i = 0; i < 10; i++)
  446. {
  447. IMMaybeInjectEvents();
  448. }
  449. //
  450. // Go into TURBO scheduling if this is a real input packet.
  451. //
  452. if (pPacket != NULL)
  453. {
  454. SCH_ContinueScheduling(SCH_MODE_TURBO);
  455. }
  456. }
  457. DebugExitVOID(ASShare::IM_ReceivedPacket);
  458. }
  459. //
  460. // IMGetHighLevelKeyState
  461. //
  462. // DESCRIPTION:
  463. //
  464. // Called by the IEM when it is converting a local event to a network event
  465. // to determine the state of the local keyboard when the event was
  466. // generated.
  467. //
  468. // PARAMETERS:
  469. //
  470. // vk - the key
  471. //
  472. // RETURNS:
  473. //
  474. // Flags - bit 7 set/reset key down/up, bit 0 toggle
  475. //
  476. //
  477. BYTE ASShare::IMGetHighLevelKeyState(UINT vk)
  478. {
  479. int keyState;
  480. BYTE rc;
  481. DebugEntry(ASShare::IMGetHighLevelKeyState);
  482. keyState = GetKeyState(vk);
  483. rc = (BYTE) (((keyState & 0x8000) >> 8) | keyState & 0x0001);
  484. DebugExitDWORD(ASShare::IMGetHighLevelKeyState, rc);
  485. return(rc);
  486. }
  487. //
  488. // FUNCTION: IMFlushOutgoingEvents
  489. //
  490. // DESCRIPTION:
  491. //
  492. // Called to send new IMEVENTs (as they are generated and periodically).
  493. // This function will send as many IMEVENTs from the current backlog as
  494. // possible.
  495. //
  496. // PARAMETERS: NONE
  497. //
  498. // RETURNS: NONE
  499. //
  500. //
  501. void ASShare::IMFlushOutgoingEvents(void)
  502. {
  503. UINT i;
  504. UINT sizeOfPacket;
  505. PIMPACKET pIMPacket;
  506. UINT lastEvent;
  507. UINT secondLastEvent;
  508. UINT elapsedTime;
  509. UINT time;
  510. UINT eventsToSend;
  511. UINT curTime;
  512. BOOL holdPacket;
  513. #ifdef _DEBUG
  514. UINT sentSize;
  515. #endif // _DEBUG
  516. DebugEntry(ASShare::IMFlushOutgoingEvents);
  517. ValidateView(m_pasLocal->m_caInControlOf);
  518. //
  519. // Try to convert the input into a bunch of IMEVENTs
  520. //
  521. while (m_imfInControlEventIsPending && (m_imInControlEventQ.numEvents < IM_SIZE_EVENTQ))
  522. {
  523. //
  524. // There is space to try and convert the pending packet.
  525. //
  526. m_imfInControlEventIsPending = (IMTranslateOutgoing(&m_imInControlPendingEvent,
  527. &m_imInControlEventQ.events[CIRCULAR_INDEX(m_imInControlEventQ.head,
  528. m_imInControlEventQ.numEvents, IM_SIZE_EVENTQ)]) != FALSE);
  529. if (m_imfInControlEventIsPending)
  530. {
  531. //
  532. // We have added a packet to the queue - update our queue
  533. // tracking variables.
  534. //
  535. m_imInControlEventQ.numEvents++;
  536. }
  537. }
  538. //
  539. // Mouse handling has been improved in the following ways
  540. // - withhold generation of packets while we are purely handling
  541. // mouse moves and we are within the LOCAL_MOUSE_WITHHOLD range
  542. // While we are doing this spoil them to the highest frequency
  543. // we are permitted to generate (SAMPLING_GAP_HIGH)
  544. // - if we exceed the withholding threshhold but remain within queue
  545. // size/2 spoil down to the intermediate range
  546. // (SAMPLING_GAP_MEDIUM)
  547. // - otherwise spoil down to the low range
  548. //
  549. // We spoil the events by hanging on to the last event for a while, if
  550. // it was a mouse move, so that we can use it for subsequent spoiling.
  551. // Whenever we get a non-mouse message then we spoil the lot to
  552. // eliminate latency, on clicks, for example.
  553. //
  554. //
  555. // Calculate the mouse spoil rate - do we need more than just the high
  556. // rate spoiling?
  557. //
  558. if (m_imInControlEventQ.numEvents > m_imInControlMouseWithhold + 1)
  559. {
  560. //
  561. // Are we into intermediate or low spoiling?
  562. //
  563. if (m_imInControlEventQ.numEvents < (IM_SIZE_EVENTQ +
  564. m_imInControlMouseWithhold) / 2)
  565. {
  566. TRACE_OUT(( "Mouse spoil rate to MEDIUM"));
  567. m_imInControlMouseSpoilRate = IM_LOCAL_MOUSE_SAMPLING_GAP_MEDIUM_MS;
  568. }
  569. else
  570. {
  571. TRACE_OUT(( "Mouse spoil rate to LOW"));
  572. m_imInControlMouseSpoilRate = IM_LOCAL_MOUSE_SAMPLING_GAP_LOW_MS;
  573. }
  574. }
  575. else
  576. {
  577. //
  578. // Spoil at the normal high rate
  579. //
  580. if (m_imInControlMouseSpoilRate != IM_LOCAL_MOUSE_SAMPLING_GAP_HIGH_MS)
  581. {
  582. TRACE_OUT(( "Mouse spoil rate to HIGH"));
  583. m_imInControlMouseSpoilRate = IM_LOCAL_MOUSE_SAMPLING_GAP_HIGH_MS;
  584. }
  585. }
  586. //
  587. // Firstly get a pointer to lastEvent for use here and in send arm
  588. // below (We wont use it if m_imInControlEventQ.numEvents == 0)
  589. //
  590. lastEvent = CIRCULAR_INDEX(m_imInControlEventQ.head,
  591. m_imInControlEventQ.numEvents - 1, IM_SIZE_EVENTQ);
  592. //
  593. // Now perform the spoiling, if necessary
  594. //
  595. if (m_imInControlEventQ.numEvents > 1)
  596. {
  597. if (lastEvent == 0)
  598. {
  599. secondLastEvent = IM_SIZE_EVENTQ - 1;
  600. }
  601. else
  602. {
  603. secondLastEvent = lastEvent - 1;
  604. }
  605. elapsedTime = m_imInControlEventQ.events[lastEvent].timeMS
  606. - m_imInControlEventQ.events[secondLastEvent].timeMS;
  607. TRACE_OUT(( "Inter packet time %d, sampling gap %ld",
  608. elapsedTime,m_imInControlMouseSpoilRate));
  609. if ((elapsedTime < m_imInControlMouseSpoilRate) &&
  610. (m_imInControlEventQ.events[lastEvent].type == IM_TYPE_3BUTTON) &&
  611. (m_imInControlEventQ.events[secondLastEvent].type == IM_TYPE_3BUTTON) &&
  612. (m_imInControlEventQ.events[lastEvent].data.mouse.flags &
  613. IM_FLAG_MOUSE_MOVE) &&
  614. (m_imInControlEventQ.events[secondLastEvent].data.mouse.flags &
  615. IM_FLAG_MOUSE_MOVE))
  616. {
  617. TRACE_OUT(( "spoil mouse move from pos %u", secondLastEvent));
  618. time = m_imInControlEventQ.events[secondLastEvent].timeMS;
  619. m_imInControlEventQ.events[secondLastEvent] =
  620. m_imInControlEventQ.events[lastEvent];
  621. m_imInControlEventQ.events[secondLastEvent].timeMS = time;
  622. m_imInControlEventQ.numEvents--;
  623. lastEvent = secondLastEvent;
  624. }
  625. }
  626. //
  627. // If we have any events queued up and we are not waiting for a mouse
  628. // button up event then try to send them. (Note we do not wait for a
  629. // mouse up event if the queue is full because if we got a mouse up
  630. // when the queue was full then we would have nowhere to put it!)
  631. //
  632. curTime = GetTickCount();
  633. if ((m_imInControlEventQ.numEvents != 0) &&
  634. ((m_imfInControlEventIsPending ||
  635. (m_imInControlMouseDownCount == 0) ||
  636. (curTime - m_imInControlMouseDownTime > IM_MOUSE_UP_WAIT_TIME))))
  637. {
  638. //
  639. // If there are mouse move messages on the queue and they are not
  640. // so old that we should send them anyway then hold them to allow
  641. // some spoiling to take place.
  642. //
  643. holdPacket = FALSE;
  644. if (m_imInControlEventQ.numEvents <= m_imInControlMouseWithhold)
  645. {
  646. if ((m_imInControlEventQ.events[lastEvent].type == IM_TYPE_3BUTTON) &&
  647. (m_imInControlEventQ.events[lastEvent].data.mouse.flags &
  648. IM_FLAG_MOUSE_MOVE))
  649. {
  650. if (curTime < (m_imInControlEventQ.events[m_imInControlEventQ.head].timeMS +
  651. IM_LOCAL_WITHHOLD_DELAY))
  652. {
  653. holdPacket = TRUE;
  654. }
  655. }
  656. }
  657. if (m_imInControlEventQ.numEvents <= IM_LOCAL_KEYBOARD_WITHHOLD)
  658. {
  659. //
  660. // If the message indicates the key is down then wait, either
  661. // for the release we know is coming, or intil it has auto
  662. // repeated for a while or until the buffer is full.
  663. //
  664. if (((m_imInControlEventQ.events[lastEvent].type == IM_TYPE_ASCII) ||
  665. (m_imInControlEventQ.events[lastEvent].type == IM_TYPE_VK1) ||
  666. (m_imInControlEventQ.events[lastEvent].type == IM_TYPE_VK2)) &&
  667. (m_imInControlEventQ.events[lastEvent].data.keyboard.flags &
  668. IM_FLAG_KEYBOARD_DOWN))
  669. {
  670. curTime = GetTickCount();
  671. if (curTime < (m_imInControlEventQ.events[m_imInControlEventQ.head].timeMS +
  672. IM_LOCAL_WITHHOLD_DELAY))
  673. {
  674. holdPacket = TRUE;
  675. }
  676. }
  677. }
  678. if (!holdPacket)
  679. {
  680. UINT_PTR destID;
  681. TRACE_OUT(( "Sending all %d packets",m_imInControlEventQ.numEvents));
  682. eventsToSend = m_imInControlEventQ.numEvents;
  683. m_imInControlEventQ.numEvents = 0;
  684. destID = m_pasLocal->m_caInControlOf->mcsID;
  685. sizeOfPacket = sizeof(IMPACKET) + (eventsToSend-1)*sizeof(IMEVENT);
  686. pIMPacket = (PIMPACKET)SC_AllocPkt(PROT_STR_INPUT, destID, sizeOfPacket);
  687. if (!pIMPacket)
  688. {
  689. //
  690. // Failed to send this packet - keep the data on the queue
  691. // until the next time we are called. To prevent the loss
  692. // of data, just make sure that the local packet list is
  693. // not overwritten by restoring the current out packets
  694. // count.
  695. //
  696. WARNING_OUT(("Failed to alloc IM packet, size %u", sizeOfPacket));
  697. m_imInControlEventQ.numEvents = eventsToSend;
  698. }
  699. else
  700. {
  701. TRACE_OUT(( "NetAllocPkt successful for %d packets size %d",
  702. eventsToSend, sizeOfPacket));
  703. //
  704. // Fill in the packet header.
  705. //
  706. pIMPacket->header.data.dataType = DT_IM;
  707. //
  708. // Construct the contents of the IM specific part of the
  709. // packet.
  710. //
  711. pIMPacket->numEvents = (TSHR_UINT16)eventsToSend;
  712. for (i = 0; i < eventsToSend; i++)
  713. {
  714. pIMPacket->aEvents[i] = m_imInControlEventQ.events[m_imInControlEventQ.head];
  715. m_imInControlEventQ.head =
  716. CIRCULAR_INDEX(m_imInControlEventQ.head, 1,
  717. IM_SIZE_EVENTQ);
  718. }
  719. //
  720. // Now send the packet.
  721. //
  722. #ifdef _DEBUG
  723. sentSize =
  724. #endif // _DEBUG
  725. DCS_CompressAndSendPacket(PROT_STR_INPUT, destID,
  726. &(pIMPacket->header), sizeOfPacket);
  727. TRACE_OUT(("IM packet size: %08d, sent %08d", sizeOfPacket, sentSize));
  728. }
  729. }
  730. }
  731. DebugExitVOID(ASShare::IMFlushOutgoingEvents);
  732. }
  733. //
  734. // IMSpoilEvents()
  735. //
  736. // Called when outgoing IM packets get backlogged, we spoil every other
  737. // mouse move to shrink the number of events and therefore the size of the
  738. // IM packet(s).
  739. //
  740. void ASShare::IMSpoilEvents(void)
  741. {
  742. UINT lastEvent;
  743. UINT i;
  744. UINT j;
  745. UINT k;
  746. BOOL discard = TRUE;
  747. DebugEntry(ASShare::IMSpoilEvents);
  748. WARNING_OUT(( "Major spoiling due to IM packet queue backlog!"));
  749. i = CIRCULAR_INDEX(m_imInControlEventQ.head,
  750. m_imInControlEventQ.numEvents - 1, IM_SIZE_EVENTQ);
  751. while (i != m_imInControlEventQ.head)
  752. {
  753. if ((m_imInControlEventQ.events[i].type == IM_TYPE_3BUTTON) &&
  754. (m_imInControlEventQ.events[i].data.mouse.flags & IM_FLAG_MOUSE_MOVE))
  755. {
  756. if (discard)
  757. {
  758. TRACE_OUT(( "spoil mouse move from pos %u", i));
  759. j = CIRCULAR_INDEX(i, 1, IM_SIZE_EVENTQ);
  760. k = i;
  761. lastEvent = CIRCULAR_INDEX(m_imInControlEventQ.head,
  762. m_imInControlEventQ.numEvents - 1, IM_SIZE_EVENTQ);
  763. while (k != lastEvent)
  764. {
  765. //
  766. // Shuffle the entries along the queue.
  767. //
  768. m_imInControlEventQ.events[k] = m_imInControlEventQ.events[j];
  769. k = CIRCULAR_INDEX(k, 1, IM_SIZE_EVENTQ);
  770. j = CIRCULAR_INDEX(j, 1, IM_SIZE_EVENTQ);
  771. }
  772. m_imInControlEventQ.numEvents--;
  773. discard = FALSE;
  774. }
  775. else
  776. {
  777. discard = TRUE;
  778. }
  779. }
  780. //
  781. // Move on to the next event infront of this one.
  782. //
  783. if (i > 0)
  784. {
  785. i = i - 1;
  786. }
  787. else
  788. {
  789. i = IM_SIZE_EVENTQ - 1;
  790. }
  791. }
  792. DebugExitVOID(ASShare::IMSpoilEvents);
  793. }
  794. //
  795. // IMAppendNetEvent()
  796. //
  797. // Add the incoming event to the remote network queue, doing basic
  798. // translation like mouse button swapping. Ignore unrecognized events.
  799. //
  800. void ASShare::IMAppendNetEvent(PIMEVENT pIMEvent)
  801. {
  802. int i;
  803. BOOL discard = TRUE;
  804. DebugEntry(ASShare::IMAppendNetEvent);
  805. switch (pIMEvent->type)
  806. {
  807. case IM_TYPE_3BUTTON:
  808. if (!(pIMEvent->data.mouse.flags & IM_FLAG_MOUSE_MOVE))
  809. {
  810. //
  811. // Swap the mouse buttons if necessary.
  812. //
  813. if (m_imfControlledMouseButtonsReversed &&
  814. (pIMEvent->data.mouse.flags &
  815. (TSHR_UINT16)(IM_FLAG_MOUSE_BUTTON1 |
  816. IM_FLAG_MOUSE_BUTTON2)))
  817. {
  818. pIMEvent->data.mouse.flags ^=
  819. (TSHR_UINT16)(IM_FLAG_MOUSE_BUTTON1 |
  820. IM_FLAG_MOUSE_BUTTON2);
  821. }
  822. }
  823. break;
  824. }
  825. //
  826. // Now put the IMEVENT into our queue.
  827. // Before we try to add the current packet we will try to inject some
  828. // more events (and therefore make space on the network event queue)
  829. //
  830. if (m_imControlledEventQ.numEvents >= IM_SIZE_EVENTQ)
  831. {
  832. //
  833. // Our network event queue is full - discard every other mouse
  834. // move event in the queue.
  835. //
  836. WARNING_OUT(( "Major spoiling due to network event queue backlog!"));
  837. for (i = m_imControlledEventQ.numEvents - 1; i >= 0; i--)
  838. {
  839. if (IM_IS_MOUSE_MOVE(m_imControlledEventQ.events[i].data.mouse.flags))
  840. {
  841. if (discard)
  842. {
  843. //
  844. // Remove this mouse move event by moving all events
  845. // after it down one.
  846. //
  847. WARNING_OUT(("Discard mouse move to {%d, %d}",
  848. (UINT)(m_imControlledEventQ.events[i].data.mouse.x),
  849. (UINT)(m_imControlledEventQ.events[i].data.mouse.y)));
  850. UT_MoveMemory(&(m_imControlledEventQ.events[i]),
  851. &(m_imControlledEventQ.events[i+1]),
  852. sizeof(IMEVENT) *
  853. (m_imControlledEventQ.numEvents-1-i) );
  854. m_imControlledEventQ.numEvents--;
  855. discard = FALSE;
  856. }
  857. else
  858. {
  859. discard = TRUE;
  860. }
  861. }
  862. }
  863. }
  864. if (m_imControlledEventQ.numEvents + 1 >= IM_SIZE_EVENTQ)
  865. {
  866. //
  867. // We've done our best and can't find any space.
  868. //
  869. WARNING_OUT(( "IM packet dropped %04X", pIMEvent->type));
  870. }
  871. else
  872. {
  873. //
  874. // Add this event to the queue
  875. //
  876. m_imControlledEventQ.events[m_imControlledEventQ.numEvents] = *pIMEvent;
  877. m_imControlledEventQ.numEvents++;
  878. }
  879. DebugExitVOID(ASShare::IMAppendNetEvent);
  880. }
  881. //
  882. // IM_OutgoingMouseInput()
  883. //
  884. // Called to send mouse moves and clicks to the remote host.
  885. // Called from the view window code.
  886. //
  887. void ASShare::IM_OutgoingMouseInput
  888. (
  889. ASPerson * pasHost,
  890. LPPOINT pMousePos,
  891. UINT message,
  892. UINT dwExtra
  893. )
  894. {
  895. IMEVENT imEvent;
  896. DebugEntry(ASShare::IM_OutgoingMouseInput);
  897. ValidateView(pasHost);
  898. ASSERT(pasHost->m_caControlledBy == m_pasLocal);
  899. ASSERT(!pasHost->m_caControlPaused);
  900. GetKeyboardState(m_aimInControlKeyStates);
  901. //
  902. // Create the event.
  903. //
  904. imEvent.type = IM_TYPE_3BUTTON;
  905. //
  906. // We should only get WM_MOUSE* messages.
  907. //
  908. ASSERT(message >= WM_MOUSEFIRST);
  909. ASSERT(message <= WM_MOUSELAST);
  910. //
  911. // Convert to bit flags.
  912. //
  913. switch (message)
  914. {
  915. case WM_MOUSEMOVE:
  916. imEvent.data.mouse.flags = IM_FLAG_MOUSE_MOVE;
  917. break;
  918. case WM_LBUTTONDOWN:
  919. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON1 |
  920. IM_FLAG_MOUSE_DOWN;
  921. break;
  922. case WM_LBUTTONDBLCLK:
  923. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON1 |
  924. IM_FLAG_MOUSE_DOUBLE |
  925. IM_FLAG_MOUSE_DOWN;
  926. break;
  927. case WM_LBUTTONUP:
  928. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON1;
  929. break;
  930. case WM_RBUTTONDOWN:
  931. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON2 |
  932. IM_FLAG_MOUSE_DOWN;
  933. break;
  934. case WM_RBUTTONDBLCLK:
  935. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON2 |
  936. IM_FLAG_MOUSE_DOUBLE |
  937. IM_FLAG_MOUSE_DOWN;
  938. break;
  939. case WM_RBUTTONUP:
  940. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON2;
  941. break;
  942. case WM_MBUTTONDOWN:
  943. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON3 |
  944. IM_FLAG_MOUSE_DOWN;
  945. break;
  946. case WM_MBUTTONDBLCLK:
  947. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON3 |
  948. IM_FLAG_MOUSE_DOUBLE |
  949. IM_FLAG_MOUSE_DOWN;
  950. break;
  951. case WM_MBUTTONUP:
  952. imEvent.data.mouse.flags = IM_FLAG_MOUSE_BUTTON3;
  953. break;
  954. case WM_MOUSEWHEEL:
  955. //
  956. // LAURABU BOGUSBOGUS
  957. //
  958. // The HIWORD of wParam represents the # of clicks the wheel
  959. // has turned.
  960. //
  961. // But what about Win95? NT and Win95 Magellan mouse work
  962. // differently.
  963. //
  964. imEvent.data.mouse.flags = IM_FLAG_MOUSE_WHEEL;
  965. //
  966. // Check for overflows. If the wheel delta is outside the
  967. // values that can be sent by the protocol, send the maximum
  968. // values.
  969. //
  970. if ((TSHR_INT16)HIWORD(dwExtra) >
  971. (IM_FLAG_MOUSE_ROTATION_MASK - IM_FLAG_MOUSE_DIRECTION))
  972. {
  973. ERROR_OUT(( "Mouse wheel overflow %hd", HIWORD(dwExtra)));
  974. imEvent.data.mouse.flags |=
  975. (IM_FLAG_MOUSE_ROTATION_MASK - IM_FLAG_MOUSE_DIRECTION);
  976. }
  977. else if ((TSHR_INT16)HIWORD(dwExtra) < -IM_FLAG_MOUSE_DIRECTION)
  978. {
  979. ERROR_OUT(( "Mouse wheel underflow %hd", HIWORD(dwExtra)));
  980. imEvent.data.mouse.flags |= IM_FLAG_MOUSE_DIRECTION;
  981. }
  982. else
  983. {
  984. imEvent.data.mouse.flags |=
  985. (HIWORD(dwExtra) & IM_FLAG_MOUSE_ROTATION_MASK);
  986. }
  987. //
  988. // Win95 boxes need to know whether the middle mouse button is
  989. // up or down.
  990. //
  991. if (LOWORD(dwExtra) & MK_MBUTTON)
  992. {
  993. imEvent.data.mouse.flags |= IM_FLAG_MOUSE_DOWN;
  994. }
  995. break;
  996. default:
  997. imEvent.data.mouse.flags = IM_FLAG_MOUSE_MOVE;
  998. ERROR_OUT(( "Unrecognised mouse event - %#x", message));
  999. break;
  1000. }
  1001. TRACE_OUT(( "Mouse event flags %hx", imEvent.data.mouse.flags));
  1002. imEvent.data.mouse.x = (TSHR_INT16)(pMousePos->x);
  1003. imEvent.data.mouse.y = (TSHR_INT16)(pMousePos->y);
  1004. imEvent.timeMS = GetTickCount();
  1005. //
  1006. // If this is a mouse down event then we will wait a while before
  1007. // sending the packet for a mouse up event so that a single click
  1008. // can be sent in one packet to avoid timing problems on the remote
  1009. // side - with for example a scroll bar scrolling multiple lines
  1010. // instead of just one line.
  1011. //
  1012. if ((message == WM_LBUTTONDOWN) ||
  1013. (message == WM_RBUTTONDOWN) ||
  1014. (message == WM_MBUTTONDOWN) ||
  1015. (message == WM_LBUTTONDBLCLK) ||
  1016. (message == WM_RBUTTONDBLCLK) ||
  1017. (message == WM_MBUTTONDBLCLK))
  1018. {
  1019. m_imInControlMouseDownCount++;
  1020. m_imInControlMouseDownTime = GetTickCount();
  1021. }
  1022. else if ((message == WM_LBUTTONUP) ||
  1023. (message == WM_RBUTTONUP) ||
  1024. (message == WM_MBUTTONUP))
  1025. {
  1026. --m_imInControlMouseDownCount;
  1027. if (m_imInControlMouseDownCount < 0)
  1028. {
  1029. TRACE_OUT(("Unmatched button down for %d", message));
  1030. m_imInControlMouseDownCount = 0;
  1031. }
  1032. }
  1033. //
  1034. // Try to send the packet.
  1035. //
  1036. if (!IMConvertAndSendEvent(pasHost, &imEvent))
  1037. {
  1038. WARNING_OUT(("Couldn't send mouse packet from local node"));
  1039. }
  1040. DebugExitVOID(ASShare::IM_OutgoingMouseInput);
  1041. }
  1042. //
  1043. // IM_OutgoingKeyboardInput()
  1044. //
  1045. // Called to key downs, ups, and chars to the remote host.
  1046. // Called from the view window code.
  1047. //
  1048. void ASShare::IM_OutgoingKeyboardInput
  1049. (
  1050. ASPerson * pasHost,
  1051. UINT wParam,
  1052. UINT lParam
  1053. )
  1054. {
  1055. IMEVENT imEvent;
  1056. int rc;
  1057. int retFlags;
  1058. WORD result[2];
  1059. UINT i;
  1060. BOOL fSwallowDeadKey;
  1061. UINT mainVK;
  1062. DebugEntry(ASShare::IM_OutgoingKeyboardInput);
  1063. ValidateView(pasHost);
  1064. ASSERT(pasHost->m_caControlledBy = m_pasLocal);
  1065. ASSERT(!pasHost->m_caControlPaused);
  1066. GetKeyboardState(m_aimInControlKeyStates);
  1067. //
  1068. // Trace out the parameters once we've got this far.
  1069. //
  1070. TRACE_OUT(( "wParam - %04X, lParam - %08lX", wParam, lParam));
  1071. //
  1072. // Create the event.
  1073. //
  1074. imEvent.data.keyboard.flags = (TSHR_UINT16)
  1075. (HIWORD(lParam) & IM_MASK_KEYBOARD_SYSFLAGS);
  1076. imEvent.timeMS = GetTickCount();
  1077. imEvent.data.keyboard.keyCode = LOBYTE(wParam);
  1078. retFlags = CA_SEND_EVENT | CA_ALLOW_EVENT;
  1079. if ((wParam == VK_LWIN) || (wParam == VK_RWIN))
  1080. {
  1081. //
  1082. // The Windows keys give control to the local user interface.
  1083. //
  1084. // The keys are defined to do the following by the spec "New key
  1085. // support for Microsoft Windows Operating Systems and
  1086. // Applications"
  1087. //
  1088. // Left Windows key - set focus to Win95 user interface
  1089. // Right Windows key - as left
  1090. // Both Windows keys - Log-on key for Windows NT
  1091. // Windows key + any other - reserved for system hot keys
  1092. //
  1093. // Thus it does not make any sense to send these keys to the remote
  1094. // system at all.
  1095. //
  1096. retFlags &= ~CA_SEND_EVENT;
  1097. }
  1098. else if ((wParam == VK_PROCESSKEY) && (m_imImmGVK != NULL))
  1099. {
  1100. //
  1101. // An IME has processed this key - we want to find out what the
  1102. // original key was so call <ImmGetVirtualKey>.
  1103. //
  1104. ValidateView(pasHost);
  1105. wParam = m_imImmGVK(pasHost->m_pView->m_viewClient);
  1106. TRACE_OUT(( "Translated wP from VK_PROCESSKEY to %#lx", wParam));
  1107. }
  1108. if (retFlags & CA_SEND_EVENT)
  1109. {
  1110. //
  1111. // First check if this is a dead-key up stroke - if it is then
  1112. // don't call ToAscii as the shift state may have changed and we'll
  1113. // get the wrong accent or no accent at all. Assume that if the VK
  1114. // is a potential dead key VK (disregarding shift state) and
  1115. // m_imInControlNumDeadKeysDown is > 0 that this is a dead key - swallow
  1116. // it.
  1117. //
  1118. fSwallowDeadKey = FALSE;
  1119. if ((m_imInControlNumDeadKeysDown != 0) &&
  1120. (imEvent.data.keyboard.flags & IM_FLAG_KEYBOARD_RELEASE))
  1121. {
  1122. for (i = 0; i < m_imInControlNumDeadKeys; i++)
  1123. {
  1124. if (m_aimInControlDeadKeys[i] == (BYTE)imEvent.data.keyboard.keyCode)
  1125. {
  1126. //
  1127. // Assume this is a dead key up and therefore we don't
  1128. // want to pass it through ToAscii or generate any
  1129. // events based on it.
  1130. //
  1131. m_imInControlNumDeadKeysDown--;
  1132. TRACE_OUT(( "m_imInControlNumDeadKeysDown - %d",
  1133. m_imInControlNumDeadKeysDown));
  1134. fSwallowDeadKey = TRUE;
  1135. }
  1136. }
  1137. }
  1138. if (!fSwallowDeadKey)
  1139. {
  1140. //
  1141. // Find out if we can translate this virtual key into the
  1142. // Windows character set.
  1143. //
  1144. //
  1145. // Now try to convert this to an Ascii character.
  1146. //
  1147. rc = ToAscii(wParam,
  1148. LOBYTE(HIWORD(lParam)),
  1149. m_aimInControlKeyStates,
  1150. &result[0],
  1151. !(!(HIWORD(lParam) & KF_MENUMODE)));
  1152. if ((rc == 1) && (LOBYTE(result[0]) <= ' '))
  1153. {
  1154. //
  1155. // Don't use the results of ToAscii if its less than space
  1156. // (32) or space itself as Windows claims that the
  1157. // characters below this in the Windows character set are
  1158. // not supported and ToAscii will convert space plus
  1159. // modifiers to an ascii space and when we replay it
  1160. // VkKeyScan will tell us that ascii space shouldn't have
  1161. // any modifiers so we will undo any modifiers. This will
  1162. // clobber apps which interpret Ctrl-Space, Shift-Space.
  1163. //
  1164. rc = 0;
  1165. }
  1166. //
  1167. // Some Ascii characters can be generated from more than one
  1168. // key. (Eg '-' is on the main keyboard and the number pad).
  1169. // Convert this ASCII character back to a VK_ value. If it is
  1170. // different from the VK_ we started with, then do not send the
  1171. // key press as ASCII (Ie only send the 'main' way of entering
  1172. // an ASCII value as ASCII).
  1173. //
  1174. // Oprah1943: revert to the VK only if the ASCII code is less
  1175. // than 0x80. This avoids losing the diacritic in a dead-key
  1176. // sequence. VkKeyScan for the key down following the dead-key
  1177. // up returns the dead-key VK rather than that of the keystroke
  1178. // (wParam).
  1179. //
  1180. if (rc == 1)
  1181. {
  1182. mainVK = VkKeyScan(LOBYTE(result[0]));
  1183. if ( (LOBYTE(mainVK) != LOBYTE(wParam)) &&
  1184. (LOBYTE(result[0]) < 0x80) )
  1185. {
  1186. TRACE_OUT((
  1187. "Not MAIN VK pressed=0x%02hx main=0x%02hx ('%c'/%02hx)",
  1188. (TSHR_UINT16)LOBYTE(wParam),
  1189. (TSHR_UINT16)LOBYTE(mainVK),
  1190. (char)LOBYTE(result[0]),
  1191. (UINT)LOBYTE(result[0])));
  1192. rc = 0;
  1193. }
  1194. }
  1195. //
  1196. // If ToAscii converts this to a dead key then don't send any
  1197. // packets at all.
  1198. //
  1199. if (rc != -1)
  1200. {
  1201. if (rc == 1)
  1202. {
  1203. TRACE_OUT(( "ToAscii rc=1, result - %02X",
  1204. LOBYTE(result[0])));
  1205. //
  1206. // Succesfully converted to an Ascii key.
  1207. //
  1208. imEvent.type = IM_TYPE_ASCII;
  1209. imEvent.data.keyboard.keyCode = LOBYTE(result[0]);
  1210. //
  1211. // Try to send the packet.
  1212. //
  1213. if (!IMConvertAndSendEvent(pasHost, &imEvent))
  1214. {
  1215. WARNING_OUT(( "dropped local key press %u",
  1216. (UINT)imEvent.data.keyboard.keyCode));
  1217. }
  1218. }
  1219. else if (rc == 2)
  1220. {
  1221. TRACE_OUT(( "ToAscii rc=2, result - %04X", result[0]));
  1222. //
  1223. // Succesfully converted to two Ascii keys. If this is
  1224. // a key down then we will return a key down and key up
  1225. // for the `dead' character first then the key down.
  1226. // If its a key up then just return the key up.
  1227. //
  1228. if (!(imEvent.data.keyboard.flags &
  1229. IM_FLAG_KEYBOARD_RELEASE))
  1230. {
  1231. //
  1232. // This is the key down - so generate a fake
  1233. // keyboard press for the dead key.
  1234. //
  1235. IMGenerateFakeKeyPress(IM_TYPE_ASCII,
  1236. LOBYTE(result[0]),
  1237. imEvent.data.keyboard.flags);
  1238. }
  1239. //
  1240. // Now return the current keystroke.
  1241. //
  1242. imEvent.type = IM_TYPE_ASCII;
  1243. imEvent.data.keyboard.keyCode = LOBYTE(result[1]);
  1244. //
  1245. // Try to send the packet.
  1246. //
  1247. if (!IMConvertAndSendEvent(pasHost, &imEvent))
  1248. {
  1249. WARNING_OUT(( "dropped local key press %u",
  1250. (UINT)imEvent.data.keyboard.keyCode));
  1251. }
  1252. }
  1253. else
  1254. {
  1255. //
  1256. // Check for keys that we want to convert.
  1257. //
  1258. if (LOBYTE(wParam) == VK_KANJI)
  1259. {
  1260. //
  1261. // We only see a down press for VK_KANJI so we
  1262. // fake a complete key press so that the remote
  1263. // does not get confused.
  1264. //
  1265. IMGenerateFakeKeyPress(IM_TYPE_VK1,
  1266. VK_KANJI,
  1267. imEvent.data.keyboard.flags);
  1268. }
  1269. else
  1270. {
  1271. //
  1272. // No conversion - use the VK itself.
  1273. //
  1274. imEvent.type = IM_TYPE_VK1;
  1275. imEvent.data.keyboard.keyCode = LOBYTE(wParam);
  1276. //
  1277. // SFR 2537: If this is a right shift VK (which we
  1278. // can detect via the scan code in lParam), set the
  1279. // right_variant keyboard flag. We do not do this
  1280. // for the right-variants of CONTROL and ALT (ie
  1281. // menu) because they are extended keys - already
  1282. // catered for by the extended flag.
  1283. //
  1284. if ( (m_imScanVKRShift != 0) &&
  1285. (m_imScanVKRShift == LOBYTE(HIWORD(lParam))) )
  1286. {
  1287. imEvent.data.keyboard.flags |=
  1288. IM_FLAG_KEYBOARD_RIGHT;
  1289. }
  1290. //
  1291. // Try to send the packet.
  1292. //
  1293. if (!IMConvertAndSendEvent(pasHost, &imEvent))
  1294. {
  1295. WARNING_OUT(( "dropped local key press %u",
  1296. (UINT)imEvent.data.keyboard.keyCode));
  1297. }
  1298. }
  1299. }
  1300. }
  1301. else
  1302. {
  1303. //
  1304. // This is a dead key - add it to our array of dead keys if
  1305. // we haven't already heard about it.
  1306. //
  1307. IMMaybeAddDeadKey(
  1308. (BYTE)imEvent.data.keyboard.keyCode);
  1309. m_imInControlNumDeadKeysDown++;
  1310. TRACE_OUT(( "m_imInControlNumDeadKeysDown - %d",
  1311. m_imInControlNumDeadKeysDown));
  1312. }
  1313. }
  1314. }
  1315. DebugExitVOID(ASShare::IM_OutgoingKeyboardInput);
  1316. }
  1317. //
  1318. // FUNCTION: IMGenerateFakeKeyPress(...)
  1319. //
  1320. // DESCRIPTION:
  1321. //
  1322. // Generates a fake keyboard press.
  1323. //
  1324. // PARAMETERS:
  1325. //
  1326. // type - packet type to generate.
  1327. // key - key to generate press for.
  1328. // flags - flags on keyboard press.
  1329. //
  1330. // RETURNS:
  1331. //
  1332. // Nothing.
  1333. //
  1334. //
  1335. void ASShare::IMGenerateFakeKeyPress
  1336. (
  1337. TSHR_UINT16 type,
  1338. TSHR_UINT16 key,
  1339. TSHR_UINT16 flags
  1340. )
  1341. {
  1342. IMEVENT imEventFake;
  1343. DebugEntry(ASShare::IMGenerateFakeKeyPress);
  1344. TRACE_OUT(( "Faking keyboard press:%#hx type:%#hx", key, type));
  1345. //
  1346. // Generate the key down first of all.
  1347. //
  1348. ZeroMemory(&imEventFake, sizeof(imEventFake));
  1349. imEventFake.type = type;
  1350. imEventFake.timeMS = GetTickCount();
  1351. imEventFake.data.keyboard.keyCode = key;
  1352. //
  1353. // Try to send the packet.
  1354. //
  1355. if (!IMConvertAndSendEvent(m_pasLocal->m_caInControlOf, &imEventFake))
  1356. {
  1357. WARNING_OUT(( "Dropped local key press %hu (flags: %#hx)",
  1358. imEventFake.data.keyboard.keyCode,
  1359. imEventFake.data.keyboard.flags));
  1360. }
  1361. //
  1362. // Set the release and down flags in order to fake the up.
  1363. //
  1364. imEventFake.data.keyboard.flags = IM_FLAG_KEYBOARD_DOWN | IM_FLAG_KEYBOARD_RELEASE;
  1365. //
  1366. // Try to send the packet.
  1367. //
  1368. if (!IMConvertAndSendEvent(m_pasLocal->m_caInControlOf, &imEventFake))
  1369. {
  1370. WARNING_OUT(( "Dropped local key press %hu (flags: %#hx)",
  1371. imEventFake.data.keyboard.keyCode,
  1372. imEventFake.data.keyboard.flags));
  1373. }
  1374. DebugExitVOID(ASShare::IMGenerateFakeKeyPress);
  1375. }
  1376. //
  1377. // FUNCTION: IMConvertAndSendEvent
  1378. //
  1379. // DESCRIPTION:
  1380. //
  1381. // Called with an IMEVENT this function will try to queue (and even send
  1382. // if possible) the packet. If it fails it will return FALSE - the caller
  1383. // should discard the packet. If it succeeds it will return TRUE.
  1384. //
  1385. // If pasFor is us, it means to send to everybody (and coords are relative
  1386. // to sender's screen).
  1387. //
  1388. // If pasFor is a remote, it means that the IM packet is meant for just
  1389. // that person and the coords are relative to pasFor's screen.
  1390. //
  1391. //
  1392. // PARAMETERS:
  1393. //
  1394. // pIMEvent - the IMEVENT to convert and send
  1395. //
  1396. // RETURNS: TRUE or FALSE - success or failure
  1397. //
  1398. //
  1399. BOOL ASShare::IMConvertAndSendEvent
  1400. (
  1401. ASPerson * pasFor,
  1402. PIMEVENT pIMEvent
  1403. )
  1404. {
  1405. BOOL rc = FALSE;
  1406. DebugEntry(ASShare::IMConvertAndSendEvent);
  1407. //
  1408. // If there is already a pending packet then see if we can flush some
  1409. // packets onto the network.
  1410. //
  1411. if (m_imfInControlEventIsPending)
  1412. {
  1413. IMFlushOutgoingEvents();
  1414. }
  1415. //
  1416. // If there is still a pending packet then see if we can spoil some
  1417. // events.
  1418. //
  1419. if (m_imfInControlEventIsPending)
  1420. {
  1421. TRACE_OUT(( "trying to drop mouse move events"));
  1422. IMSpoilEvents();
  1423. IMFlushOutgoingEvents();
  1424. }
  1425. //
  1426. // Now see if we are able to accept a new packet
  1427. //
  1428. if (m_imfInControlEventIsPending)
  1429. {
  1430. //
  1431. // If there is still a previous IMEVENT which we are in the
  1432. // process of converting then we are not ready to receive any more
  1433. // packets.
  1434. //
  1435. TRACE_OUT(( "can't queue packet"));
  1436. DC_QUIT;
  1437. }
  1438. //
  1439. // Now set up the new packet and try to flush the packets again.
  1440. //
  1441. m_imfInControlEventIsPending = TRUE;
  1442. m_imInControlPendingEvent = *pIMEvent;
  1443. IMFlushOutgoingEvents();
  1444. rc = TRUE;
  1445. DC_EXIT_POINT:
  1446. DebugExitBOOL(ASShare::IMConvertAndSendEvent, rc);
  1447. return(rc);
  1448. }
  1449. //
  1450. // FUNCTION: IMMaybeAddDeadKey
  1451. //
  1452. // DESCRIPTION:
  1453. //
  1454. // Called whenever ToAscii tells us about a dead key. If we haven't
  1455. // got it in our table already then we will add it. We create the table
  1456. // incrementally because we have found that some keyboard drivers don't
  1457. // cope very well with being queried with all possible VKs to find the
  1458. // dead keys. Note that this will not cope with someone switching their
  1459. // keyboard driver whilst DC-Share is running.
  1460. //
  1461. // PARAMETERS:
  1462. //
  1463. // vk - the VK in question
  1464. //
  1465. // RETURNS: NONE
  1466. //
  1467. //
  1468. void ASShare::IMMaybeAddDeadKey(BYTE vk)
  1469. {
  1470. UINT i;
  1471. DebugEntry(IMMaybeAddDeadKey);
  1472. //
  1473. // First see if we already know about this key.
  1474. //
  1475. for (i = 0; i < m_imInControlNumDeadKeys; i++)
  1476. {
  1477. if (m_aimInControlDeadKeys[i] == vk)
  1478. {
  1479. DC_QUIT;
  1480. }
  1481. }
  1482. //
  1483. // Add this key if there's space in the array.
  1484. //
  1485. if (m_imInControlNumDeadKeys < IM_MAX_DEAD_KEYS)
  1486. {
  1487. TRACE_OUT(( "Add %02X", (TSHR_UINT16)vk));
  1488. m_aimInControlDeadKeys[m_imInControlNumDeadKeys++] = vk;
  1489. }
  1490. DC_EXIT_POINT:
  1491. DebugExitVOID(ASShare::IMMaybeAddDeadKey);
  1492. }
  1493. //
  1494. // IMConvertIMEventToOSEvent()
  1495. // Converts incoming event to something we can playback.
  1496. //
  1497. // PARAMETERS:
  1498. //
  1499. // pIMEvent - the IMEVENT to be converted
  1500. //
  1501. // pOSEvent - the IMOSEVENT to be created
  1502. //
  1503. //
  1504. UINT ASShare::IMConvertIMEventToOSEvent
  1505. (
  1506. PIMEVENT pIMEvent,
  1507. LPIMOSEVENT pOSEvent
  1508. )
  1509. {
  1510. int mouseX;
  1511. int mouseY;
  1512. int realMouseX;
  1513. int realMouseY;
  1514. RECT cursorClip;
  1515. UINT rc = (IM_IMQUEUEREMOVE | IM_OSQUEUEINJECT);
  1516. DebugEntry(ASShare::IMConvertIMEventToOSEvent);
  1517. switch (pIMEvent->type)
  1518. {
  1519. case IM_TYPE_3BUTTON:
  1520. //
  1521. // Fill in common fields. Note that we claim to be a 3 button
  1522. // mouse so that we can replay events from remote three button
  1523. // mice and we always give absolute coordinates.
  1524. //
  1525. pOSEvent->type = IM_MOUSE_EVENT;
  1526. pOSEvent->flags = 0;
  1527. pOSEvent->time = pIMEvent->timeMS;
  1528. pOSEvent->event.mouse.cButtons = 3;
  1529. pOSEvent->event.mouse.mouseData = 0;
  1530. pOSEvent->event.mouse.dwExtraInfo = 0;
  1531. //
  1532. // First check for a wheel rotate, since this is easy to
  1533. // process. (It cannot include any mouse movement as well).
  1534. //
  1535. if (pIMEvent->data.mouse.flags & IM_FLAG_MOUSE_WHEEL)
  1536. {
  1537. if (pIMEvent->data.mouse.flags &
  1538. (IM_FLAG_MOUSE_BUTTON1 |
  1539. IM_FLAG_MOUSE_BUTTON2 |
  1540. IM_FLAG_MOUSE_BUTTON3))
  1541. {
  1542. //
  1543. // Using any of the button flags along with the wheel
  1544. // flag is currently undefined - for forward
  1545. // compatability we therefore ignore such an event by
  1546. // converting it into a NULL injected event.
  1547. //
  1548. // (We do not sg_lpimSharedData->imply discard it, since the logic to
  1549. // discard events does not seem to work).
  1550. //
  1551. pOSEvent->event.mouse.flags = 0;
  1552. pOSEvent->event.mouse.pt.x = 0;
  1553. pOSEvent->event.mouse.pt.y = 0;
  1554. }
  1555. else
  1556. {
  1557. //
  1558. // This is a wheel movement.
  1559. //
  1560. // Note that the protocol has sent whether the mouse's
  1561. // middle button is depressed or released, but we don't
  1562. // need that info for NT, so just ignore it.
  1563. //
  1564. pOSEvent->event.mouse.flags = MOUSEEVENTF_WHEEL;
  1565. pOSEvent->event.mouse.mouseData =
  1566. (pIMEvent->data.mouse.flags & IM_FLAG_MOUSE_ROTATION_MASK);
  1567. pOSEvent->event.mouse.pt.x = 0;
  1568. pOSEvent->event.mouse.pt.y = 0;
  1569. //
  1570. // Sign extend the rotation amount up to the full 32
  1571. // bits
  1572. //
  1573. if (pOSEvent->event.mouse.mouseData & IM_FLAG_MOUSE_DIRECTION)
  1574. {
  1575. pOSEvent->event.mouse.mouseData |=
  1576. ~IM_FLAG_MOUSE_ROTATION_MASK;
  1577. }
  1578. }
  1579. break;
  1580. }
  1581. //
  1582. // We are left now with non wheel-rotate events.
  1583. //
  1584. pOSEvent->event.mouse.flags = MOUSEEVENTF_ABSOLUTE;
  1585. //
  1586. // We must convert from virtual desktop coordinates to local
  1587. // screen coordinates here and we must also prevent the
  1588. // position wrapping if we try to replay a mouse move to an
  1589. // off-screen position.
  1590. //
  1591. realMouseX = pIMEvent->data.mouse.x;
  1592. realMouseY = pIMEvent->data.mouse.y;
  1593. //
  1594. // Now lg_lpimSharedData->imit to the size of the real screen.
  1595. //
  1596. mouseX = min((m_pasLocal->cpcCaps.screen.capsScreenWidth-1), max(0, realMouseX));
  1597. mouseY = min((m_pasLocal->cpcCaps.screen.capsScreenHeight-1), max(0, realMouseY));
  1598. //
  1599. // Work out if this event will be clipped by the clip cursor
  1600. //
  1601. GetClipCursor(&cursorClip);
  1602. if ((mouseX < cursorClip.left) ||
  1603. (mouseX >= cursorClip.right) ||
  1604. (mouseY < cursorClip.top) ||
  1605. (mouseY >= cursorClip.bottom))
  1606. {
  1607. //
  1608. // This event will actually be clipped because of the
  1609. // current clip cursor. Remember this.
  1610. //
  1611. m_imfControlledMouseClipped = TRUE;
  1612. }
  1613. else
  1614. {
  1615. m_imfControlledMouseClipped = FALSE;
  1616. //
  1617. // If we clamp the mouse position before replaying then we
  1618. // must remember the real packet and make the current
  1619. // packet into a move so that we don't click down/up at the
  1620. // wrong place.
  1621. //
  1622. if ((mouseX != realMouseX) || (mouseY != realMouseY))
  1623. {
  1624. //
  1625. // The mouse position we've recieved is off the
  1626. // local physical screen. Now that we no longer have
  1627. // desktop scrolling, we simply clamp it rather than
  1628. // inject it at the edge and wait for the scroll.
  1629. //
  1630. // We turn mouse down-clicks into moves and let
  1631. // up-clicks pass through (in case the mouse button
  1632. // has been pressed within the real screen).
  1633. //
  1634. // Note that the mouse position has already been
  1635. // adjusted so that it is within the real screen.
  1636. //
  1637. if (pIMEvent->data.mouse.flags & IM_FLAG_MOUSE_DOWN)
  1638. {
  1639. pIMEvent->data.mouse.flags = IM_FLAG_MOUSE_MOVE;
  1640. }
  1641. }
  1642. }
  1643. //
  1644. // Store the mouse position.
  1645. //
  1646. pOSEvent->event.mouse.pt.x = mouseX;
  1647. pOSEvent->event.mouse.pt.y = mouseY;
  1648. //
  1649. // Add more flags as appropriate.
  1650. //
  1651. if (pIMEvent->data.mouse.flags & IM_FLAG_MOUSE_MOVE)
  1652. {
  1653. pOSEvent->event.mouse.flags |= MOUSEEVENTF_MOVE;
  1654. }
  1655. else
  1656. {
  1657. switch (pIMEvent->data.mouse.flags &
  1658. ( IM_FLAG_MOUSE_BUTTON1 |
  1659. IM_FLAG_MOUSE_BUTTON2 |
  1660. IM_FLAG_MOUSE_BUTTON3 |
  1661. IM_FLAG_MOUSE_DOWN ))
  1662. {
  1663. case IM_FLAG_MOUSE_BUTTON1 | IM_FLAG_MOUSE_DOWN:
  1664. pOSEvent->event.mouse.flags |= MOUSEEVENTF_LEFTDOWN;
  1665. break;
  1666. case IM_FLAG_MOUSE_BUTTON1:
  1667. pOSEvent->event.mouse.flags |= MOUSEEVENTF_LEFTUP;
  1668. break;
  1669. case IM_FLAG_MOUSE_BUTTON2 | IM_FLAG_MOUSE_DOWN:
  1670. pOSEvent->event.mouse.flags |= MOUSEEVENTF_RIGHTDOWN;
  1671. break;
  1672. case IM_FLAG_MOUSE_BUTTON2:
  1673. pOSEvent->event.mouse.flags |= MOUSEEVENTF_RIGHTUP;
  1674. break;
  1675. case IM_FLAG_MOUSE_BUTTON3 | IM_FLAG_MOUSE_DOWN:
  1676. pOSEvent->event.mouse.flags |= MOUSEEVENTF_MIDDLEDOWN;
  1677. break;
  1678. case IM_FLAG_MOUSE_BUTTON3:
  1679. pOSEvent->event.mouse.flags |= MOUSEEVENTF_MIDDLEUP;
  1680. break;
  1681. default:
  1682. //
  1683. // If we don't recognise this then don't play it
  1684. // back
  1685. //
  1686. ERROR_OUT(("Unrecognised mouse flags (%04X)",
  1687. pIMEvent->data.mouse.flags));
  1688. rc = IM_IMQUEUEREMOVE;
  1689. break;
  1690. }
  1691. }
  1692. break;
  1693. case IM_TYPE_VK1:
  1694. //
  1695. // Common fields.
  1696. //
  1697. pOSEvent->flags = 0;
  1698. if (pIMEvent->data.keyboard.flags & IM_FLAG_KEYBOARD_UPDATESTATE)
  1699. pOSEvent->flags |= IM_FLAG_UPDATESTATE;
  1700. pOSEvent->time = pIMEvent->timeMS;
  1701. //
  1702. // Now handle normal keyboard events.
  1703. //
  1704. pOSEvent->type = IM_KEYBOARD_EVENT;
  1705. //
  1706. // AX is the scancode in AL and 00h (press) or 80h (release) in
  1707. // AH. Map the DC protocol VK to the equivalent OS VK.
  1708. // AL = the scancode for the VK).
  1709. //
  1710. pOSEvent->event.keyboard.vkCode = LOBYTE(pIMEvent->data.keyboard.keyCode);
  1711. pOSEvent->event.keyboard.flags = 0;
  1712. if (IS_IM_KEY_RELEASE(pIMEvent->data.keyboard.flags))
  1713. {
  1714. pOSEvent->event.keyboard.flags |= KEYEVENTF_KEYUP;
  1715. }
  1716. //
  1717. // SFR 2537: If the flags indicate that the received VK is the
  1718. // right-variant, do not map the VK to a scan code, but rather
  1719. // directly use the already acquired right-variant scan code
  1720. // for the VK. (For the moment, the only case we support is
  1721. // for Windows, where this is an issue for SHIFT).
  1722. //
  1723. if ( IS_IM_KEY_RIGHT(pIMEvent->data.keyboard.flags) &&
  1724. (pIMEvent->data.keyboard.keyCode == VK_SHIFT) )
  1725. {
  1726. pOSEvent->event.keyboard.scanCode = m_imScanVKRShift;
  1727. }
  1728. else
  1729. {
  1730. pOSEvent->event.keyboard.scanCode =
  1731. (WORD)MapVirtualKey(pIMEvent->data.keyboard.keyCode, 0);
  1732. }
  1733. if (pIMEvent->data.keyboard.flags & IM_FLAG_KEYBOARD_EXTENDED)
  1734. {
  1735. pOSEvent->event.keyboard.flags |= KEYEVENTF_EXTENDEDKEY;
  1736. }
  1737. pOSEvent->event.keyboard.dwExtraInfo = 0;
  1738. break;
  1739. default:
  1740. ERROR_OUT(("Unrecognized imEvent (%d)", pIMEvent->type));
  1741. //
  1742. // Discard the event (remove from the IM queue and don't inject
  1743. // into the OS).
  1744. //
  1745. rc = IM_IMQUEUEREMOVE;
  1746. break;
  1747. }
  1748. DebugExitDWORD(ASShare::IMConvertIMEventToOSEvent, rc);
  1749. return(rc);
  1750. }
  1751. //
  1752. // IMTranslateOutgoing()
  1753. //
  1754. // DESCRIPTION:
  1755. //
  1756. // Converts locally generated sequences of IMEVENTs into transmitted
  1757. // sequences of IMEVENTs. Does a 1 to (0-n) translation. Handles
  1758. // buffering modifier keys and translating DC-Share hot-key sequences.
  1759. //
  1760. // When the CA has decided an IMEVENT should be sent this function is
  1761. // called by the IM with a pointer to that packet in pIMEventIn.
  1762. // IMTranslateOutgoing can then return TRUE and fill in the packet at
  1763. // pIMEventOut or return FALSE. If IMTranslateOutgoing returns TRUE the IM
  1764. // will call it again with the same packet. The IMEVENTs returned are
  1765. // sent across the network by the IM.
  1766. //
  1767. // PARAMETERS:
  1768. //
  1769. // pIMEventIn - pointer to IMEVENT
  1770. //
  1771. // pIMEventOut - pointer to IMEVENT
  1772. //
  1773. // RETURNS:
  1774. //
  1775. // TRUE - packet returned (call function again)
  1776. //
  1777. // FALSE - no packet returned (don't call function again)
  1778. //
  1779. //
  1780. BOOL ASShare::IMTranslateOutgoing
  1781. (
  1782. LPIMEVENT pIMEventIn,
  1783. LPIMEVENT pIMEventOut
  1784. )
  1785. {
  1786. UINT hotKeyArrayIndex;
  1787. UINT hotKeyValue;
  1788. BOOL fHotKeyFound;
  1789. BOOL rc = FALSE;
  1790. DebugEntry(ASShare::IMTranslateOutgoing);
  1791. //
  1792. // Here we need to tell the remote system about certain keys which are
  1793. // consumed locally so that it can make good decisions about whether
  1794. // and how to replay them. We want to keep the remote system in step
  1795. // with the current modifier and toggle key state on our system (as it
  1796. // is possible that either a modifier/toggle key event occurred whilst
  1797. // a local app was active and was therefore never sent) We also want to
  1798. // recognise certain `hot key' sequences and send further packets as a
  1799. // result of these.
  1800. //
  1801. // The keys we comsume locally are:
  1802. //
  1803. // Esc down or up when Ctrl is down - operates task list locally
  1804. //
  1805. // Tab down or up when Alt is down - operates task switcher locally
  1806. //
  1807. // Esc down or up when Alt is pressed - switches to next window locally
  1808. //
  1809. // Esc up when corresponding Esc down occurred when Alt was down - as
  1810. // above
  1811. //
  1812. // The sequences we want to produce hot keys from are:
  1813. //
  1814. // Alt + 9?? on the numeric keypad
  1815. //
  1816. // To detect hotkeys we keep a record of the last four keypresses and
  1817. // when we detect an Alt up we check if they form a valid sequence.
  1818. //
  1819. // The keystrokes which form part of the hotkey are sent to the remote
  1820. // system so if they have some meaning on a remote system then that
  1821. // system must decide whether to buffer them to determine if they are
  1822. // part of a hotkey or play them back anyway - on Windows we play them
  1823. // back anyway as they are a legitimate key sequence when controlling a
  1824. // Windows app - the number typed on the numeric keypad has a % 256
  1825. // applied to it.
  1826. //
  1827. // This means that for each incoming event we may want to generate 0 or
  1828. // more outgoing events. To do this we have a structure which looks
  1829. // roughly like this:
  1830. //
  1831. // IF m_m_imfInControlNewEvent
  1832. // calculate an array of events which we want to return
  1833. // set m_m_imfInControlNewEvent to FALSE
  1834. // set number of events returned to 0
  1835. // ENDIF
  1836. //
  1837. // IF !m_m_imfInControlNewEvent
  1838. // IF this is the last event to return
  1839. // set m_m_imfInControlNewEvent to TRUE
  1840. // ENDIF
  1841. // return current event
  1842. // ENDIF
  1843. //
  1844. //
  1845. if (m_imfInControlNewEvent)
  1846. {
  1847. //
  1848. // This is the first time we have seen this event so accumulate
  1849. // our list of events to generate.
  1850. //
  1851. //
  1852. // Do tracing
  1853. //
  1854. if (pIMEventIn->type == IM_TYPE_ASCII)
  1855. {
  1856. TRACE_OUT(( "IN ASCII code 0x%04X, flags 0x%04X",
  1857. pIMEventIn->data.keyboard.keyCode, pIMEventIn->data.keyboard.flags));
  1858. }
  1859. else if (pIMEventIn->type == IM_TYPE_VK1)
  1860. {
  1861. TRACE_OUT(( "IN VKEY code %04X, flags %04X",
  1862. pIMEventIn->data.keyboard.keyCode, pIMEventIn->data.keyboard.flags));
  1863. }
  1864. else if ((pIMEventIn->type == IM_TYPE_3BUTTON) &&
  1865. !(pIMEventIn->data.mouse.flags & IM_FLAG_MOUSE_MOVE))
  1866. {
  1867. TRACE_OUT(( "IN 3BTTN flags %04X (%d,%d)",
  1868. pIMEventIn->data.mouse.flags, pIMEventIn->data.mouse.x,
  1869. pIMEventIn->data.mouse.y));
  1870. }
  1871. else if (pIMEventIn->type == IM_TYPE_3BUTTON)
  1872. {
  1873. TRACE_OUT(( "IN 3BTTN flags %04X (%d,%d)",
  1874. pIMEventIn->data.mouse.flags, pIMEventIn->data.mouse.x,
  1875. pIMEventIn->data.mouse.y));
  1876. }
  1877. else if (pIMEventIn->type == IM_TYPE_VK_ASCII)
  1878. {
  1879. TRACE_OUT(("IN VK_ASC code %04X, flags %04X",
  1880. pIMEventIn->data.keyboard.keyCode, pIMEventIn->data.keyboard.flags));
  1881. }
  1882. else
  1883. {
  1884. ERROR_OUT(("Invalid IM type %d", pIMEventIn->type));
  1885. }
  1886. //
  1887. // Start from the beginning of our returned events array.
  1888. //
  1889. m_imInControlNumEventsPending = 0;
  1890. m_imInControlNumEventsReturned = 0;
  1891. //
  1892. // First get our flags for the modifiers and locks we think we have
  1893. // sent to the remote side up to date allowing for this event.
  1894. //
  1895. if (pIMEventIn->type == IM_TYPE_VK1)
  1896. {
  1897. switch (pIMEventIn->data.keyboard.keyCode)
  1898. {
  1899. case VK_CONTROL:
  1900. if (IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  1901. {
  1902. m_imfInControlCtrlDown = FALSE;
  1903. }
  1904. else
  1905. {
  1906. m_imfInControlCtrlDown = TRUE;
  1907. }
  1908. break;
  1909. case VK_SHIFT:
  1910. if (IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  1911. {
  1912. m_imfInControlShiftDown = FALSE;
  1913. }
  1914. else
  1915. {
  1916. m_imfInControlShiftDown = TRUE;
  1917. }
  1918. break;
  1919. case VK_MENU:
  1920. if (IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  1921. {
  1922. m_imfInControlMenuDown = FALSE;
  1923. }
  1924. else
  1925. {
  1926. m_imfInControlMenuDown = TRUE;
  1927. }
  1928. break;
  1929. case VK_CAPITAL:
  1930. if (IS_IM_KEY_PRESS(pIMEventIn->data.keyboard.flags))
  1931. {
  1932. m_imfInControlCapsLock = !m_imfInControlCapsLock;
  1933. }
  1934. break;
  1935. case VK_NUMLOCK:
  1936. if (IS_IM_KEY_PRESS(pIMEventIn->data.keyboard.flags))
  1937. {
  1938. m_imfInControlNumLock = !m_imfInControlNumLock;
  1939. }
  1940. break;
  1941. case VK_SCROLL:
  1942. if (IS_IM_KEY_PRESS(pIMEventIn->data.keyboard.flags))
  1943. {
  1944. m_imfInControlScrollLock = !m_imfInControlScrollLock;
  1945. }
  1946. break;
  1947. default:
  1948. break;
  1949. }
  1950. }
  1951. //
  1952. // Now check the current state versus our remembered state and
  1953. // prepare to insert events if necessary. Do this for any events
  1954. // (ie including mouse events) as mouse clicks can have different
  1955. // effects depending on the current modifer state.
  1956. //
  1957. //
  1958. // First the modifiers. IMGetHighLevelKeyState will return us the
  1959. // keyboard state including the event we are currently processing
  1960. // because it is adjusted before the keyboard hook. The top most
  1961. // bit is set of the key is down otherwise it is reset.
  1962. //
  1963. if (IMGetHighLevelKeyState(VK_CONTROL) & 0x80)
  1964. {
  1965. if (!m_imfInControlCtrlDown)
  1966. {
  1967. //
  1968. // The key is down locally but we last told the remote
  1969. // machine it was up.
  1970. //
  1971. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  1972. IEM_EVENT_CTRL_DOWN;
  1973. m_imfInControlCtrlDown = TRUE;
  1974. }
  1975. }
  1976. else
  1977. {
  1978. if (m_imfInControlCtrlDown)
  1979. {
  1980. //
  1981. // The key is up locally but we last told the remote
  1982. // machine it was down.
  1983. //
  1984. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  1985. IEM_EVENT_CTRL_UP;
  1986. m_imfInControlCtrlDown = FALSE;
  1987. }
  1988. }
  1989. //
  1990. // Do the same for shift and menu (alt).
  1991. //
  1992. if (IMGetHighLevelKeyState(VK_SHIFT) & 0x80)
  1993. {
  1994. if (!m_imfInControlShiftDown)
  1995. {
  1996. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  1997. IEM_EVENT_SHIFT_DOWN;
  1998. m_imfInControlShiftDown = TRUE;
  1999. }
  2000. }
  2001. else
  2002. {
  2003. if (m_imfInControlShiftDown)
  2004. {
  2005. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2006. IEM_EVENT_SHIFT_UP;
  2007. m_imfInControlShiftDown = FALSE;
  2008. }
  2009. }
  2010. if (IMGetHighLevelKeyState(VK_MENU) & 0x80)
  2011. {
  2012. if (!m_imfInControlMenuDown)
  2013. {
  2014. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2015. IEM_EVENT_MENU_DOWN;
  2016. m_imfInControlMenuDown = TRUE;
  2017. }
  2018. }
  2019. else
  2020. {
  2021. if (m_imfInControlMenuDown)
  2022. {
  2023. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2024. IEM_EVENT_MENU_UP;
  2025. m_imfInControlMenuDown = FALSE;
  2026. }
  2027. }
  2028. //
  2029. // Now handle the toggles. The least significant bit is set when
  2030. // the toggle is on, reset otherwise.
  2031. //
  2032. if ((IMGetHighLevelKeyState(VK_CAPITAL) & IM_KEY_STATE_FLAG_TOGGLE) ?
  2033. !m_imfInControlCapsLock : m_imfInControlCapsLock)
  2034. {
  2035. //
  2036. // The current caps lock state and what we've sent to the
  2037. // remote system are out of synch - fix it.
  2038. //
  2039. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2040. IEM_EVENT_CAPS_LOCK_DOWN;
  2041. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2042. IEM_EVENT_CAPS_LOCK_UP;
  2043. m_imfInControlCapsLock = !m_imfInControlCapsLock;
  2044. }
  2045. //
  2046. // Do the same for Num lock and Scroll lock.
  2047. //
  2048. if ((IMGetHighLevelKeyState(VK_NUMLOCK) & 0x01) ?
  2049. !m_imfInControlNumLock : m_imfInControlNumLock)
  2050. {
  2051. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2052. IEM_EVENT_NUM_LOCK_DOWN;
  2053. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2054. IEM_EVENT_NUM_LOCK_UP;
  2055. m_imfInControlNumLock = !m_imfInControlNumLock;
  2056. }
  2057. if ((IMGetHighLevelKeyState(VK_SCROLL) & 0x01) ?
  2058. !m_imfInControlScrollLock : m_imfInControlScrollLock)
  2059. {
  2060. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2061. IEM_EVENT_SCROLL_LOCK_DOWN;
  2062. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2063. IEM_EVENT_SCROLL_LOCK_UP;
  2064. m_imfInControlScrollLock = !m_imfInControlScrollLock;
  2065. }
  2066. //
  2067. // Now we will do the appropriate processing for each type of
  2068. // packet we expect. We only expect to receive
  2069. //
  2070. // IM_TYPE_VK1
  2071. // IM_TYPE_ASCII
  2072. // IM_TYPE_3BUTTON
  2073. //
  2074. //
  2075. if (pIMEventIn->type == IM_TYPE_VK1)
  2076. {
  2077. //
  2078. // Now process a VK packet generated from the real keyboard.
  2079. // Check for Escape, Tab and Menu and decide whether to forward
  2080. // them or consume them first.
  2081. //
  2082. if (pIMEventIn->data.keyboard.keyCode == VK_ESCAPE)
  2083. {
  2084. //
  2085. // This is the escape key - check the current shift status
  2086. // to see whether we should flag this as consumed locally.
  2087. //
  2088. if (IMGetHighLevelKeyState(VK_MENU) & 0x80)
  2089. {
  2090. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2091. IEM_EVENT_CONSUMED;
  2092. //
  2093. // Also remember to consume the next Menu Up keystroke.
  2094. //
  2095. m_imfInControlConsumeMenuUp = TRUE;
  2096. if (!IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  2097. {
  2098. //
  2099. // If this is an escape press then remember that we
  2100. // should consume the corresponding up stroke
  2101. // regardless of shift state.
  2102. //
  2103. m_imfInControlConsumeEscapeUp = TRUE;
  2104. }
  2105. }
  2106. else if (m_imfInControlConsumeEscapeUp &&
  2107. IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  2108. {
  2109. //
  2110. // This is the up stroke corresponding to a down
  2111. // stroke we consumed so consume it too.
  2112. //
  2113. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2114. IEM_EVENT_CONSUMED;
  2115. m_imfInControlConsumeEscapeUp = FALSE;
  2116. }
  2117. else
  2118. {
  2119. //
  2120. // This Escape is not one of our special cases so
  2121. // forward it unchanged.
  2122. //
  2123. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2124. IEM_EVENT_FORWARD;
  2125. }
  2126. }
  2127. else if (pIMEventIn->data.keyboard.keyCode == VK_TAB)
  2128. {
  2129. //
  2130. // This is the Tab key - check for current shift status to
  2131. // see whether we should flag this as consumed locally.
  2132. //
  2133. if (IMGetHighLevelKeyState(VK_MENU) & 0x80)
  2134. {
  2135. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2136. IEM_EVENT_CONSUMED;
  2137. //
  2138. // Also remember to consume the next Menu Up keystroke.
  2139. //
  2140. m_imfInControlConsumeMenuUp = TRUE;
  2141. }
  2142. else
  2143. {
  2144. //
  2145. // This Tab is not our special case so forward it
  2146. // unchanged.
  2147. //
  2148. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2149. IEM_EVENT_FORWARD;
  2150. }
  2151. }
  2152. else if ((pIMEventIn->data.keyboard.keyCode == VK_MENU) &&
  2153. IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  2154. {
  2155. //
  2156. // This is a menu up - check for one we should consume or
  2157. // for hotkeys.
  2158. //
  2159. if (m_imfInControlConsumeMenuUp)
  2160. {
  2161. //
  2162. // This is a menu up we want to consume - do so.
  2163. //
  2164. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2165. IEM_EVENT_CONSUMED;
  2166. m_imfInControlConsumeMenuUp = FALSE;
  2167. }
  2168. else
  2169. {
  2170. //
  2171. // This is a VK_MENU release
  2172. // hot key sequence in our array of last four key
  2173. // presses. Start looking at the next entry (the array
  2174. // is circular). A valid sequence is
  2175. //
  2176. // VK_MENU
  2177. // numeric pad 9
  2178. // numeric pad number
  2179. // numeric pad number
  2180. //
  2181. //
  2182. fHotKeyFound = FALSE;
  2183. hotKeyArrayIndex = m_imInControlNextHotKeyEntry;
  2184. if (m_aimInControlHotKeyArray[hotKeyArrayIndex] == VK_MENU)
  2185. {
  2186. hotKeyArrayIndex = (hotKeyArrayIndex+1)%4;
  2187. if (m_aimInControlHotKeyArray[hotKeyArrayIndex] == 9)
  2188. {
  2189. hotKeyArrayIndex = (hotKeyArrayIndex+1)%4;
  2190. if (m_aimInControlHotKeyArray[hotKeyArrayIndex] <= 9)
  2191. {
  2192. hotKeyValue =
  2193. 10*m_aimInControlHotKeyArray[hotKeyArrayIndex];
  2194. hotKeyArrayIndex = (hotKeyArrayIndex+1)%4;
  2195. if (m_aimInControlHotKeyArray[hotKeyArrayIndex] <= 9)
  2196. {
  2197. //
  2198. // This is a valid hot key - add a
  2199. // consumed VK_MENU and then a hot key
  2200. // packet.
  2201. //
  2202. hotKeyValue +=
  2203. m_aimInControlHotKeyArray[hotKeyArrayIndex];
  2204. m_aimInControlEventsToReturn[
  2205. m_imInControlNumEventsPending++] =
  2206. IEM_EVENT_CONSUMED;
  2207. m_aimInControlEventsToReturn[
  2208. m_imInControlNumEventsPending++] =
  2209. IEM_EVENT_HOTKEY_BASE + hotKeyValue;
  2210. TRACE_OUT(("Hotkey found %d", hotKeyValue));
  2211. fHotKeyFound = TRUE;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. if (!fHotKeyFound)
  2217. {
  2218. //
  2219. // This was not a hotkey so send the menu up as
  2220. // normal.
  2221. //
  2222. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2223. IEM_EVENT_FORWARD;
  2224. }
  2225. }
  2226. }
  2227. else if (IS_IM_KEY_PRESS(pIMEventIn->data.keyboard.flags))
  2228. {
  2229. //
  2230. // Keep a record of the last four key presses (not
  2231. // including auto
  2232. // VK_MENU up event to determine if we have found a hotkey
  2233. // sequence.
  2234. //
  2235. //
  2236. // This is a key press and it is not a repeat. Throw out
  2237. // extended keys here so that we're not confused by the
  2238. // grey cursor keys.
  2239. //
  2240. if (pIMEventIn->data.keyboard.flags &
  2241. IM_FLAG_KEYBOARD_EXTENDED)
  2242. {
  2243. //
  2244. // An extended key breaks the sequence.
  2245. //
  2246. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 0xFF;
  2247. }
  2248. else
  2249. {
  2250. //
  2251. // Add an entry to our array for this key. We add
  2252. // VK_MENUs and add and translate numeric keypad keys
  2253. // anything else breaks the sequencs.
  2254. //
  2255. switch (pIMEventIn->data.keyboard.keyCode)
  2256. {
  2257. case VK_MENU:
  2258. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = VK_MENU;
  2259. break;
  2260. case VK_NUMPAD0:
  2261. case VK_INSERT:
  2262. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 0;
  2263. break;
  2264. case VK_NUMPAD1:
  2265. case VK_END:
  2266. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 1;
  2267. break;
  2268. case VK_NUMPAD2:
  2269. case VK_DOWN:
  2270. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 2;
  2271. break;
  2272. case VK_NUMPAD3:
  2273. case VK_NEXT:
  2274. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 3;
  2275. break;
  2276. case VK_NUMPAD4:
  2277. case VK_LEFT:
  2278. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 4;
  2279. break;
  2280. case VK_NUMPAD5:
  2281. case VK_CLEAR:
  2282. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 5;
  2283. break;
  2284. case VK_NUMPAD6:
  2285. case VK_RIGHT:
  2286. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 6;
  2287. break;
  2288. case VK_NUMPAD7:
  2289. case VK_HOME:
  2290. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 7;
  2291. break;
  2292. case VK_NUMPAD8:
  2293. case VK_UP:
  2294. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 8;
  2295. break;
  2296. case VK_NUMPAD9:
  2297. case VK_PRIOR:
  2298. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 9;
  2299. break;
  2300. default:
  2301. //
  2302. // Any unrecognised key breaks a sequence.
  2303. //
  2304. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 0xFF;
  2305. break;
  2306. }
  2307. }
  2308. //
  2309. // Wrap the hot key array at 4 entries.
  2310. //
  2311. m_imInControlNextHotKeyEntry = (m_imInControlNextHotKeyEntry+1)%4;
  2312. //
  2313. // Forward the event.
  2314. //
  2315. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2316. IEM_EVENT_FORWARD;
  2317. }
  2318. else
  2319. {
  2320. //
  2321. // Just forward the event as its not any of our special
  2322. // cases.
  2323. //
  2324. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2325. IEM_EVENT_FORWARD;
  2326. }
  2327. }
  2328. else if (pIMEventIn->type == IM_TYPE_VK_ASCII)
  2329. {
  2330. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2331. IEM_EVENT_FORWARD;
  2332. }
  2333. else if (pIMEventIn->type == IM_TYPE_ASCII)
  2334. {
  2335. //
  2336. // Any IM_TYPE_ASCII breaks the hot key sequence.
  2337. //
  2338. m_aimInControlHotKeyArray[m_imInControlNextHotKeyEntry] = 0xFF;
  2339. m_imInControlNextHotKeyEntry = (m_imInControlNextHotKeyEntry+1)%4;
  2340. //
  2341. // Then just forward the thing without doing anything clever.
  2342. //
  2343. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2344. IEM_EVENT_FORWARD;
  2345. }
  2346. else if (pIMEventIn->type == IM_TYPE_3BUTTON)
  2347. {
  2348. //
  2349. // To be nice and clean we would ideally have a completely new
  2350. // event for the wheeled Microsoft mouse. However to maintain
  2351. // backwards compatibility, we send the event out in such a way
  2352. // that old incompatible systems interpret it as a NULL mouse
  2353. // move.
  2354. //
  2355. if (pIMEventIn->data.mouse.flags & IM_FLAG_MOUSE_WHEEL)
  2356. {
  2357. //
  2358. // This is a wheel rotatation.
  2359. //
  2360. // We massage this event so that new systems can see it for
  2361. // what it truly is - a wheel rotation, but old systems
  2362. // (which check the MOUSE_MOVE flag first, and ignore all
  2363. // other flags if set) see it as a mouse move.
  2364. //
  2365. // We did not set the MOUSE_MOVE flag when we first
  2366. // generated this event, since we did not want to trigger
  2367. // any of the sending side mouse move processing which
  2368. // would otherwise have been invoked.
  2369. //
  2370. pIMEventIn->data.mouse.flags |= IM_FLAG_MOUSE_MOVE;
  2371. }
  2372. //
  2373. // Forward the event
  2374. //
  2375. m_aimInControlEventsToReturn[m_imInControlNumEventsPending++] =
  2376. IEM_EVENT_FORWARD;
  2377. }
  2378. //
  2379. // Now we are going into a loop to return the m_iemLocalEvents we
  2380. // have queued up. We will return the first one below and then be
  2381. // called again until we have returned them all and return FALSE.
  2382. //
  2383. m_imfInControlNewEvent = FALSE;
  2384. m_imInControlNumEventsReturned = 0;
  2385. }
  2386. if (!m_imfInControlNewEvent)
  2387. {
  2388. if (m_imInControlNumEventsReturned == m_imInControlNumEventsPending)
  2389. {
  2390. //
  2391. // There are no more m_aiemLocalEvents to return.
  2392. //
  2393. TRACE_OUT(( "NO MORE EVENTS"));
  2394. m_imfInControlNewEvent = TRUE;
  2395. DC_QUIT;
  2396. }
  2397. else
  2398. {
  2399. //
  2400. // Return the next event.
  2401. //
  2402. if (m_aimInControlEventsToReturn[m_imInControlNumEventsReturned] >=
  2403. IEM_EVENT_HOTKEY_BASE)
  2404. {
  2405. TRACE_OUT(( "HOTKEY "));
  2406. //
  2407. // Return a hotkey event.
  2408. //
  2409. pIMEventOut->type = IM_TYPE_VK2;
  2410. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  2411. (m_aimInControlEventsToReturn[m_imInControlNumEventsReturned] -
  2412. IEM_EVENT_HOTKEY_BASE);
  2413. pIMEventOut->data.keyboard.flags = 0;
  2414. }
  2415. else
  2416. {
  2417. //
  2418. // Return a non-hotkey event.
  2419. //
  2420. switch (m_aimInControlEventsToReturn[m_imInControlNumEventsReturned])
  2421. {
  2422. case IEM_EVENT_CTRL_DOWN:
  2423. TRACE_OUT(( "CTRL DWN"));
  2424. //
  2425. // Set up a Ctrl down event.
  2426. //
  2427. pIMEventOut->type = IM_TYPE_VK1;
  2428. pIMEventOut->data.keyboard.keyCode = VK_CONTROL;
  2429. pIMEventOut->data.keyboard.flags = 0;
  2430. break;
  2431. case IEM_EVENT_CTRL_UP:
  2432. TRACE_OUT(( "CTRL UP "));
  2433. //
  2434. // Set up a Ctrl up event with the quiet flag set
  2435. // - this means it should have no effect (other
  2436. // than to release the control key).
  2437. //
  2438. pIMEventOut->type = IM_TYPE_VK1;
  2439. pIMEventOut->data.keyboard.keyCode = VK_CONTROL;
  2440. pIMEventOut->data.keyboard.flags =
  2441. IM_FLAG_KEYBOARD_DOWN |
  2442. IM_FLAG_KEYBOARD_RELEASE |
  2443. IM_FLAG_KEYBOARD_QUIET;
  2444. break;
  2445. case IEM_EVENT_SHIFT_DOWN:
  2446. TRACE_OUT(( "SHFT DWN"));
  2447. //
  2448. // Set up a Shift down event.
  2449. //
  2450. pIMEventOut->type = IM_TYPE_VK1;
  2451. pIMEventOut->data.keyboard.keyCode = VK_SHIFT;
  2452. pIMEventOut->data.keyboard.flags = 0;
  2453. break;
  2454. case IEM_EVENT_SHIFT_UP:
  2455. TRACE_OUT(( "SHFT UP "));
  2456. //
  2457. // Set up a Shift up event with the quiet flag set
  2458. // - this means it should have no effect (other
  2459. // than to release the shift key).
  2460. //
  2461. pIMEventOut->type = IM_TYPE_VK1;
  2462. pIMEventOut->data.keyboard.keyCode = VK_SHIFT;
  2463. pIMEventOut->data.keyboard.flags =
  2464. IM_FLAG_KEYBOARD_DOWN |
  2465. IM_FLAG_KEYBOARD_RELEASE |
  2466. IM_FLAG_KEYBOARD_QUIET;
  2467. break;
  2468. case IEM_EVENT_MENU_DOWN:
  2469. TRACE_OUT(( "MENU DWN"));
  2470. //
  2471. // Set up a Menu down event.
  2472. //
  2473. pIMEventOut->type = IM_TYPE_VK1;
  2474. pIMEventOut->data.keyboard.keyCode = VK_MENU;
  2475. break;
  2476. case IEM_EVENT_MENU_UP:
  2477. TRACE_OUT(( "MENU UP "));
  2478. //
  2479. // Set up a Ctrl down event with the quiet flag set
  2480. // - ths is means it should have no effect (other
  2481. // than to release the menu key).
  2482. //
  2483. pIMEventOut->type = IM_TYPE_VK1;
  2484. pIMEventOut->data.keyboard.keyCode = VK_MENU;
  2485. pIMEventOut->data.keyboard.flags =
  2486. IM_FLAG_KEYBOARD_DOWN |
  2487. IM_FLAG_KEYBOARD_RELEASE |
  2488. IM_FLAG_KEYBOARD_QUIET;
  2489. break;
  2490. case IEM_EVENT_CAPS_LOCK_DOWN:
  2491. TRACE_OUT(( "CAPS DWN"));
  2492. //
  2493. // Send a caps lock down.
  2494. //
  2495. pIMEventOut->type = IM_TYPE_VK1;
  2496. pIMEventOut->data.keyboard.keyCode = VK_CAPITAL;
  2497. pIMEventOut->data.keyboard.flags = 0;
  2498. break;
  2499. case IEM_EVENT_CAPS_LOCK_UP:
  2500. TRACE_OUT(( "CAPS UP "));
  2501. //
  2502. // Send a caps lock up.
  2503. //
  2504. pIMEventOut->type = IM_TYPE_VK1;
  2505. pIMEventOut->data.keyboard.keyCode = VK_CAPITAL;
  2506. pIMEventOut->data.keyboard.flags =
  2507. IM_FLAG_KEYBOARD_DOWN |
  2508. IM_FLAG_KEYBOARD_RELEASE;
  2509. break;
  2510. case IEM_EVENT_NUM_LOCK_DOWN:
  2511. TRACE_OUT(( "NUM DOWN"));
  2512. //
  2513. // Send a num lock down - num lock is an extended
  2514. // key.
  2515. //
  2516. pIMEventOut->type = IM_TYPE_VK1;
  2517. pIMEventOut->data.keyboard.keyCode = VK_NUMLOCK;
  2518. pIMEventOut->data.keyboard.flags =
  2519. IM_FLAG_KEYBOARD_EXTENDED;
  2520. break;
  2521. case IEM_EVENT_NUM_LOCK_UP:
  2522. //
  2523. // Send a num lock up - num lock is an extended
  2524. // key.
  2525. //
  2526. TRACE_OUT(( "NUM UP "));
  2527. pIMEventOut->type = IM_TYPE_VK1;
  2528. pIMEventOut->data.keyboard.keyCode = VK_NUMLOCK;
  2529. pIMEventOut->data.keyboard.flags =
  2530. IM_FLAG_KEYBOARD_DOWN |
  2531. IM_FLAG_KEYBOARD_RELEASE |
  2532. IM_FLAG_KEYBOARD_EXTENDED;
  2533. break;
  2534. case IEM_EVENT_SCROLL_LOCK_DOWN:
  2535. //
  2536. // Send a scroll lock down.
  2537. //
  2538. TRACE_OUT(( "SCROLDWN"));
  2539. pIMEventOut->type = IM_TYPE_VK1;
  2540. pIMEventOut->data.keyboard.keyCode = VK_SCROLL;
  2541. pIMEventOut->data.keyboard.flags = 0;
  2542. break;
  2543. case IEM_EVENT_SCROLL_LOCK_UP:
  2544. //
  2545. // Send a scroll lock up.
  2546. //
  2547. TRACE_OUT(( "SCROLLUP"));
  2548. pIMEventOut->type = IM_TYPE_VK1;
  2549. pIMEventOut->data.keyboard.keyCode = VK_SCROLL;
  2550. pIMEventOut->data.keyboard.flags =
  2551. IM_FLAG_KEYBOARD_DOWN |
  2552. IM_FLAG_KEYBOARD_RELEASE;
  2553. break;
  2554. case IEM_EVENT_FORWARD:
  2555. //
  2556. // Just copy the packet.
  2557. //
  2558. TRACE_OUT(( "FORWARD"));
  2559. *pIMEventOut = *pIMEventIn;
  2560. break;
  2561. case IEM_EVENT_CONSUMED:
  2562. //
  2563. // Copy the packet and set the flag.
  2564. //
  2565. TRACE_OUT(( "CONSUMED"));
  2566. *pIMEventOut = *pIMEventIn;
  2567. pIMEventOut->data.keyboard.flags |=
  2568. IM_FLAG_KEYBOARD_QUIET;
  2569. break;
  2570. default:
  2571. ERROR_OUT(( "Invalid code path"));
  2572. break;
  2573. }
  2574. }
  2575. m_imInControlNumEventsReturned++;
  2576. //
  2577. // Do tracing
  2578. //
  2579. if (pIMEventOut->type == IM_TYPE_ASCII)
  2580. {
  2581. TRACE_OUT(( "OUT ASCII code %04X, flags %04X",
  2582. pIMEventOut->data.keyboard.keyCode, pIMEventOut->data.keyboard.flags));
  2583. }
  2584. else if (pIMEventOut->type == IM_TYPE_VK1)
  2585. {
  2586. TRACE_OUT(( "OUT VK1 code %04X, flags %04X",
  2587. pIMEventOut->data.keyboard.keyCode, pIMEventOut->data.keyboard.flags));
  2588. }
  2589. else if (pIMEventOut->type == IM_TYPE_VK2)
  2590. {
  2591. TRACE_OUT(( "OUT VK2 code - %04X, flags - %04X",
  2592. pIMEventOut->data.keyboard.keyCode, pIMEventOut->data.keyboard.flags));
  2593. }
  2594. else if ((pIMEventOut->type == IM_TYPE_3BUTTON) &&
  2595. !(pIMEventOut->data.mouse.flags & IM_FLAG_MOUSE_MOVE))
  2596. {
  2597. TRACE_OUT(( "OUT 3BTTN flags - %04X (%d,%d)",
  2598. pIMEventOut->data.mouse.flags, pIMEventOut->data.mouse.x,
  2599. pIMEventOut->data.mouse.y));
  2600. }
  2601. else if (pIMEventOut->type == IM_TYPE_3BUTTON)
  2602. {
  2603. TRACE_OUT(( "OUT 3BTTN flags - %04X (%d,%d)",
  2604. pIMEventOut->data.mouse.flags, pIMEventOut->data.mouse.x,
  2605. pIMEventOut->data.mouse.y));
  2606. }
  2607. else
  2608. {
  2609. ERROR_OUT(("Invalid IM type %d", pIMEventOut->type));
  2610. }
  2611. rc = TRUE;
  2612. }
  2613. }
  2614. DC_EXIT_POINT:
  2615. DebugExitVOID(ASShare::IMTranslateOutgoing);
  2616. return(rc);
  2617. }
  2618. //
  2619. // IMTranslateIncoming()
  2620. //
  2621. // DESCRIPTION:
  2622. //
  2623. // Converts remotely generated sequences of IMEVENTs into sequences of
  2624. // IMEVENTs for replay. Does a 1 to (0-n) translation. Handles faking
  2625. // keys using ALT and keypad.
  2626. //
  2627. // When an IMEVENT is received and is ready to be replayed this function
  2628. // is called with a pointer to that packet in pIMEventIn.
  2629. // IMTranslateIncoming can then return TRUE and fill in the packet at
  2630. // pIMEventOut or return FALSE. If IMTranslateIncoming returns TRUE the
  2631. // IM will call it again with the same packet. The IMEVENTs returned are
  2632. // played back on the local machine using the journal playback hook by the
  2633. // IM.
  2634. //
  2635. // PARAMETERS:
  2636. //
  2637. // pIMEventIn - pointer to IMEVENT
  2638. //
  2639. // pIMEventOut - pointer to IMEVENT
  2640. //
  2641. // personID - the ID of the person this event was received from
  2642. //
  2643. // RETURNS:
  2644. //
  2645. // TRUE - packet returned (call function again)
  2646. //
  2647. // FALSE - no packet returned (don't call function again)
  2648. //
  2649. //
  2650. //
  2651. BOOL ASShare::IMTranslateIncoming
  2652. (
  2653. PIMEVENT pIMEventIn,
  2654. PIMEVENT pIMEventOut
  2655. )
  2656. {
  2657. BYTE curKbState;
  2658. BYTE rcVkKeyScanKbState;
  2659. UINT keyCode;
  2660. TSHR_UINT16 rcVkKeyScan;
  2661. BOOL bTranslateOEM;
  2662. char chAnsi;
  2663. char chOEM;
  2664. char chNewAnsi;
  2665. UINT position;
  2666. UINT digit;
  2667. UINT i;
  2668. DebugEntry(ASShare::IMTranslateIncoming);
  2669. //
  2670. // In this function we will receive several types of events
  2671. //
  2672. // IM_TYPE_VK1 - processed
  2673. // IM_TYPE_ASCII - processed
  2674. // IM_TYPE_VK2 - ignored (discarded)
  2675. // IM_TYPE_3BUTTON - processed
  2676. //
  2677. // For IM_TYPE_VK1:
  2678. //
  2679. // If it has the consumed locally flag set then try and play it back
  2680. // without anything happening. This means that for an Alt up we make
  2681. // sure that there have been some keyboard events between the Alt down
  2682. // and this event.
  2683. //
  2684. // For IM_TYPE_ASCII:
  2685. //
  2686. // Try to convert this to a VK to playback. If we are succesful then
  2687. // playback one or more key strokes to get into the correct shift state
  2688. // then play back the VK and then undo any shift states. If we can't
  2689. // convert to a VK then fake a sequence of Alt + numeric keypad keys to
  2690. // get the key in.
  2691. //
  2692. // For IM_TYPE_VK2:
  2693. //
  2694. // Discard unceremoniously.
  2695. //
  2696. // For IM_TYPE_3BUTTON:
  2697. //
  2698. // Play back directly.
  2699. //
  2700. //
  2701. keyCode = pIMEventIn->data.keyboard.keyCode;
  2702. if (m_imfControlledNewEvent)
  2703. {
  2704. //
  2705. // The first time we have seen a new event - accumulate an array
  2706. // of events we want to return.
  2707. //
  2708. //
  2709. // Start from the beginning of our returned events array.
  2710. //
  2711. m_imControlledNumEventsPending = 0;
  2712. m_imControlledNumEventsReturned = 0;
  2713. if (pIMEventIn->type == IM_TYPE_VK1)
  2714. {
  2715. //
  2716. // Handle VK1s first. Special cases are VK_MENU, VK_TAB and
  2717. // VK_ESC. We recognise VK_MENU down key strokes and remember
  2718. // when they happened so that we can possibly fiddle with
  2719. // VK_MENU up keystrokes later to go into menu mode. We check
  2720. // on VK_TAB for the IM_FLAG_KEYBOARD_QUIET flag and if it is
  2721. // set then we don't replay anything
  2722. // First translate the virtual key code from the DC-Share
  2723. // protocol code to the OS virtual key code
  2724. //
  2725. if (keyCode == VK_MENU)
  2726. {
  2727. if (!IS_IM_KEY_RELEASE(pIMEventIn->data.keyboard.flags))
  2728. {
  2729. //
  2730. // This is a VK_MENU press - return it without
  2731. // interfering.
  2732. //
  2733. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2734. IEM_EVENT_REPLAY;
  2735. }
  2736. else
  2737. {
  2738. //
  2739. // Handle VK_MENU up events
  2740. //
  2741. // If the menu up has the `quiet' flag set then
  2742. // insert a couple of shift key events to prevent it
  2743. // having any effect. There are two cases we're
  2744. // covering here where an Alt-UP can have some effect.
  2745. //
  2746. // 1. Alt-Down, Alt-Up causes the system menu button to
  2747. // be highlighted.
  2748. //
  2749. // 2. Entering characters from the numeric keypad takes
  2750. // effect on the Alt-Up.
  2751. //
  2752. // Both of these effects can be negated by adding the
  2753. // shift key strokes.
  2754. //
  2755. if (pIMEventIn->data.keyboard.flags &
  2756. IM_FLAG_KEYBOARD_QUIET)
  2757. {
  2758. //
  2759. // We need to `silence' this key - to do this we
  2760. // will insert to shift key strokes first
  2761. //
  2762. if (m_aimControlledControllerKeyStates[VK_SHIFT] & 0x80)
  2763. {
  2764. //
  2765. // Shift is currently down - insert an up then
  2766. // a down
  2767. //
  2768. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2769. IEM_EVENT_SHIFT_UP;
  2770. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2771. IEM_EVENT_SHIFT_DOWN;
  2772. }
  2773. else
  2774. {
  2775. //
  2776. // Shift is currently up - insert a down then
  2777. // an up
  2778. //
  2779. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2780. IEM_EVENT_SHIFT_DOWN;
  2781. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2782. IEM_EVENT_SHIFT_UP;
  2783. }
  2784. }
  2785. //
  2786. // Replay the menu up key stroke.
  2787. //
  2788. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2789. IEM_EVENT_REPLAY;
  2790. }
  2791. }
  2792. else if ((pIMEventIn->data.keyboard.flags &
  2793. IM_FLAG_KEYBOARD_QUIET) &&
  2794. ((keyCode == VK_TAB) ||
  2795. (keyCode == VK_ESCAPE)))
  2796. {
  2797. //
  2798. // Just get out of here - we don't want to play this back
  2799. //
  2800. return(FALSE);
  2801. }
  2802. else
  2803. {
  2804. //
  2805. // All other VKs just get replayed
  2806. //
  2807. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2808. IEM_EVENT_REPLAY;
  2809. }
  2810. }
  2811. else if (pIMEventIn->type == IM_TYPE_ASCII)
  2812. {
  2813. //
  2814. // For ASCII packets we need to find out how we can replay them
  2815. // on our local keyboard. If we can replay them directly or
  2816. // with shift or ctrl (but not with ALT), then we will do so,
  2817. // otherwise we will simulate Alt + numeric keypad to replay
  2818. // them. If we have to generate fake modifier key strokes
  2819. // ourselves then we will replay the whole key stroke on the
  2820. // incoming key down. If we don't need to generate fake key
  2821. // strokes then we will play the down and up keystrokes as they
  2822. // come in.
  2823. //
  2824. // We do not allow VK combinations involving ALT as this messes
  2825. // up remote international keyboard support. For example, if
  2826. // the remote keyboard is UK and we are (say) Spanish,
  2827. // VKKeyScan says we can do the "UK pound" character as
  2828. // Ctrl+Alt+3. While this works in Windows, and for DOS Boxes
  2829. // on standard keyboards, DOS Boxes with enhanced keyboards
  2830. // require ALTGR+3 (nb Windows seems to treat ALTGR as Ctrl+Alt
  2831. // anyway - at least for VKs and Async state). There is no VK
  2832. // for ALTGR, so do an ALT-nnn sequence for these cases.
  2833. //
  2834. rcVkKeyScan = VkKeyScan((char)keyCode);
  2835. TRACE_OUT(( "co_vk_key_scan of X%02x returns rcVkKeyScan X%02x",
  2836. keyCode, rcVkKeyScan));
  2837. if ((rcVkKeyScan != 0xffff) && !(rcVkKeyScan & 0x0400))
  2838. {
  2839. //
  2840. // This can be replayed using a combination of modifiers on
  2841. // this keyboard.
  2842. //
  2843. rcVkKeyScanKbState = HIBYTE(rcVkKeyScan);
  2844. //
  2845. // The high byte of rcVkKeyScan contains three bit flags
  2846. // which signify which modifiers ar required to generate
  2847. // this character. They are
  2848. //
  2849. // bit 0 - Shift
  2850. // bit 1 - Ctrl
  2851. // bit 2 - Alt (Menu)
  2852. //
  2853. // We will construct an equivalent set of flags which
  2854. // describes the current state of these modifiers.
  2855. //
  2856. curKbState = 0;
  2857. if (m_aimControlledControllerKeyStates[VK_SHIFT] & 0x80)
  2858. {
  2859. curKbState |= IEM_SHIFT_DOWN;
  2860. }
  2861. if (m_aimControlledControllerKeyStates[VK_CONTROL] & 0x80)
  2862. {
  2863. curKbState |= IEM_CTRL_DOWN;
  2864. }
  2865. if (m_aimControlledControllerKeyStates[VK_MENU] & 0x80)
  2866. {
  2867. curKbState |= IEM_MENU_DOWN;
  2868. //
  2869. // If the Alt key is down currently in this person's
  2870. // context then (in general
  2871. // it. This means accelerators which need to be
  2872. // shifted will work as we won't release the Alt key in
  2873. // order to generate the key strokes.
  2874. //
  2875. // However, if the ALT key is being held down in
  2876. // combination with SHIFT and CTRL to generate a
  2877. // character (e.g. CTRL-ALT-SHIFT-4 on a US keyboard
  2878. // to generate a � character) then we will allow the
  2879. // ALT key up before we play back the true character.
  2880. //
  2881. if ((curKbState & (IEM_SHIFT_DOWN | IEM_CTRL_DOWN)) !=
  2882. (IEM_SHIFT_DOWN | IEM_CTRL_DOWN))
  2883. {
  2884. rcVkKeyScanKbState |= IEM_MENU_DOWN;
  2885. }
  2886. }
  2887. if ((m_aimControlledControllerKeyStates[VK_CAPITAL] & 0x01) &&
  2888. ((LOBYTE(rcVkKeyScan) >= 'A') &&
  2889. ((LOBYTE(rcVkKeyScan) <= 'Z'))))
  2890. {
  2891. //
  2892. // If caps-lock is enabled then the effect of a shift
  2893. // down on VKs A thru Z is reversed. This logic ( 'A'
  2894. // <= x <= 'Z' is encoded in the keyboard.drv so it
  2895. // should be pretty safe).
  2896. //
  2897. curKbState ^= IEM_SHIFT_DOWN;
  2898. }
  2899. if (curKbState == rcVkKeyScanKbState)
  2900. {
  2901. //
  2902. // We are already in the correct shift state so just
  2903. // replay the VK.
  2904. //
  2905. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2906. IEM_EVENT_REPLAY_VK;
  2907. m_imControlledVKToReplay = LOBYTE(rcVkKeyScan);
  2908. }
  2909. else
  2910. {
  2911. //
  2912. // We need to generate some fake modifiers - only do
  2913. // this on a key press.
  2914. //
  2915. if (pIMEventIn->data.keyboard.flags &
  2916. IM_FLAG_KEYBOARD_RELEASE)
  2917. {
  2918. return(FALSE);
  2919. }
  2920. //
  2921. // Insert modifiers to get into the correct state.
  2922. //
  2923. m_imControlledNumEventsPending += IMInsertModifierKeystrokes(
  2924. curKbState,
  2925. rcVkKeyScanKbState,
  2926. &(m_aimControlledEventsToReturn[m_imControlledNumEventsPending]));
  2927. //
  2928. // Now insert the VK itself - a down and up.
  2929. //
  2930. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2931. IEM_EVENT_REPLAY_VK_DOWN;
  2932. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  2933. IEM_EVENT_REPLAY_VK_UP;
  2934. //
  2935. // Remeber the VK we want to replay when we come across
  2936. // IEM_EVENT_REPLAY_VK_DOWN/UP.
  2937. //
  2938. m_imControlledVKToReplay = LOBYTE(rcVkKeyScan);
  2939. //
  2940. // Now insert the modifiers to get back to the current
  2941. // state.
  2942. //
  2943. m_imControlledNumEventsPending += IMInsertModifierKeystrokes(
  2944. rcVkKeyScanKbState,
  2945. curKbState,
  2946. &(m_aimControlledEventsToReturn[m_imControlledNumEventsPending]));
  2947. //
  2948. // Now we have a complete set of events ready to replay
  2949. // so go for it.
  2950. //
  2951. }
  2952. }
  2953. else
  2954. {
  2955. //
  2956. // We can't replay directly, so will have to simulate an
  2957. // Alt+keypad sequence.
  2958. //
  2959. TRACE_OUT(( "FAKE AN ALT-nnn SEQUENCE IF WINDOWS"));
  2960. //
  2961. // We only do this sort of stuff on a key-press.
  2962. //
  2963. if (pIMEventIn->data.keyboard.flags &
  2964. IM_FLAG_KEYBOARD_RELEASE)
  2965. {
  2966. return(FALSE);
  2967. }
  2968. //
  2969. // The following code relies on keyCode being less than 999
  2970. // and we should receive a keycode > 255 so get out now if
  2971. // we have.
  2972. //
  2973. if (keyCode > 255)
  2974. {
  2975. return(FALSE);
  2976. }
  2977. //
  2978. // First get modifiers into correct state - create bit
  2979. // flags for current modifier state.
  2980. //
  2981. curKbState = 0;
  2982. //
  2983. // For windows we have a character to input that cannot
  2984. // be replayed by pressing a key...replay by injecting
  2985. // alt-nnn.
  2986. //
  2987. if (m_aimControlledControllerKeyStates[VK_SHIFT] & 0x80)
  2988. {
  2989. curKbState |= IEM_SHIFT_DOWN;
  2990. }
  2991. if (m_aimControlledControllerKeyStates[VK_CONTROL] & 0x80)
  2992. {
  2993. curKbState |= IEM_CTRL_DOWN;
  2994. }
  2995. if (m_aimControlledControllerKeyStates[VK_MENU] & 0x80)
  2996. {
  2997. curKbState |= IEM_MENU_DOWN;
  2998. }
  2999. //
  3000. // If necessary, reset all modifiers.
  3001. //
  3002. if (curKbState)
  3003. {
  3004. m_imControlledNumEventsPending += IMInsertModifierKeystrokes(
  3005. curKbState,
  3006. 0,
  3007. &(m_aimControlledEventsToReturn[m_imControlledNumEventsPending]));
  3008. }
  3009. //
  3010. // Now determine whether we can do the ALT-nnn keypad
  3011. // sequence using an OEM keycode or whether we have to use
  3012. // an ANSI (Windows) keycode.
  3013. //
  3014. // The issue here is that:
  3015. //
  3016. // - hosted Windows applications (or rather Windows itself)
  3017. // can distinguish between, and handle correctly, ANSI
  3018. // keycodes and OEM keycodes (where the latter vary
  3019. // depending on the keyboard type). For example,
  3020. // ALT-0163 is the ANSI "UK pound" on all keyboards,
  3021. // and on US national keyboards ALT-156 is the OEM
  3022. // keycode for "UK pound".
  3023. //
  3024. // - hosted DOS Boxes only understand OEM keycodes.
  3025. //
  3026. // So (for example), if we have a remote UK keyboard
  3027. // controlling local Windows and DOS Box applications, and
  3028. // we generate ALT-nnn using the OEM keycode (and without a
  3029. // leading zero), both the Windows and DOS Box applications
  3030. // interpret it as "UK pound" (Hoorah!). In contrast, if
  3031. // we generate ALT-nnn using the ANSI keycode (with a
  3032. // leading zero), the Windows applications still do "UK
  3033. // pound", BUT the DOS Box does an "u acute".
  3034. //
  3035. // As far as we can tell (eg by examining the DDK keyboard
  3036. // driver source for AnsiToOem), there should always be a
  3037. // translation. However, it is possible that the ANSI to
  3038. // OEM translation is not 1<->1. We therefore check this
  3039. // by doing a second translation back from OEM to ANSI. If
  3040. // this does not give us the original character we use the
  3041. // original ANSI code and play it back with a ALT-0nnn
  3042. // sequence.
  3043. //
  3044. chAnsi = (char)pIMEventIn->data.keyboard.keyCode;
  3045. AnsiToOemBuff(&chAnsi, &chOEM, 1);
  3046. OemToAnsiBuff(&chOEM, &chNewAnsi, 1);
  3047. TRACE_OUT(( "Ansi: %02x OEM: %02x NewAnsi: %02x",
  3048. (BYTE)chAnsi,
  3049. (BYTE)chOEM,
  3050. (BYTE)chNewAnsi ));
  3051. bTranslateOEM = (chAnsi == chNewAnsi);
  3052. keyCode = (bTranslateOEM)
  3053. ? (UINT)(BYTE)chOEM
  3054. : pIMEventIn->data.keyboard.keyCode;
  3055. //
  3056. // Now insert a VK_MENU down.
  3057. //
  3058. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3059. IEM_EVENT_MENU_DOWN;
  3060. //
  3061. // Now insert the numeric keypad keystrokes. If we're
  3062. // doing an ANSI ALT
  3063. //
  3064. if (!bTranslateOEM)
  3065. {
  3066. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3067. IEM_EVENT_KEYPAD0_DOWN;
  3068. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3069. IEM_EVENT_KEYPAD0_UP;
  3070. }
  3071. //
  3072. // Add keystrokes for hundreds, tens and units, taking care
  3073. // to discard leading (but not trailing) zeros if we're
  3074. // doing an OEM sequence (which would confuse Windows into
  3075. // thinking an OEM ALT-nnn sequence was an ANSI sequence).
  3076. //
  3077. position = 100;
  3078. for (i=0 ; i<3 ; i++)
  3079. {
  3080. //
  3081. // Insert the correct digit for this position.
  3082. //
  3083. digit = keyCode / position;
  3084. if (!(digit == 0 && bTranslateOEM))
  3085. {
  3086. bTranslateOEM = FALSE;
  3087. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3088. IEM_EVENT_KEYPAD0_DOWN + digit;
  3089. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3090. IEM_EVENT_KEYPAD0_UP + digit;
  3091. }
  3092. //
  3093. // Move to next position.
  3094. //
  3095. keyCode %= position;
  3096. position /= 10;
  3097. }
  3098. //
  3099. // Now insert a VK_MENU up.
  3100. //
  3101. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] =
  3102. IEM_EVENT_MENU_UP;
  3103. //
  3104. // If necessary, get the modifiers back to the state they
  3105. // were in previously.
  3106. //
  3107. if (curKbState != 0)
  3108. {
  3109. m_imControlledNumEventsPending += IMInsertModifierKeystrokes(
  3110. 0,
  3111. curKbState,
  3112. &(m_aimControlledEventsToReturn[m_imControlledNumEventsPending]));
  3113. }
  3114. //
  3115. // Now we have a buffer full of keystrokes - go for it.
  3116. //
  3117. }
  3118. }
  3119. else if (pIMEventIn->type == IM_TYPE_VK2)
  3120. {
  3121. //
  3122. // Hot keys are thrown away - this is easy.
  3123. //
  3124. return(FALSE);
  3125. }
  3126. else if (pIMEventIn->type == IM_TYPE_3BUTTON)
  3127. {
  3128. //
  3129. // Mouse events are just replayed.
  3130. //
  3131. m_aimControlledEventsToReturn[m_imControlledNumEventsPending++] = IEM_EVENT_REPLAY;
  3132. }
  3133. else
  3134. {
  3135. //
  3136. // Unknown events are thrown away - this is easy.
  3137. //
  3138. return(FALSE);
  3139. }
  3140. //
  3141. // Now we have events to return.
  3142. //
  3143. m_imfControlledNewEvent = FALSE;
  3144. m_imControlledNumEventsReturned = 0;
  3145. }
  3146. if (!m_imfControlledNewEvent)
  3147. {
  3148. if (m_imControlledNumEventsReturned == m_imControlledNumEventsPending)
  3149. {
  3150. //
  3151. // There are no more events to return.
  3152. //
  3153. m_imfControlledNewEvent = TRUE;
  3154. return(FALSE);
  3155. }
  3156. else
  3157. {
  3158. TRACE_OUT(("Event to return: %u",
  3159. m_aimControlledEventsToReturn[m_imControlledNumEventsReturned]));
  3160. if ((m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] >=
  3161. IEM_EVENT_KEYPAD0_DOWN) &&
  3162. (m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] <=
  3163. (IEM_EVENT_KEYPAD0_DOWN+9)))
  3164. {
  3165. //
  3166. // Return a keypad down event.
  3167. //
  3168. pIMEventOut->type = IM_TYPE_VK1;
  3169. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  3170. (VK_NUMPAD0 +
  3171. (m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] -
  3172. IEM_EVENT_KEYPAD0_DOWN));
  3173. pIMEventOut->data.keyboard.flags = IM_FLAG_KEYBOARD_ALT_DOWN;
  3174. }
  3175. else if ((m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] >=
  3176. IEM_EVENT_KEYPAD0_UP) &&
  3177. (m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] <=
  3178. (IEM_EVENT_KEYPAD0_UP+9)))
  3179. {
  3180. //
  3181. // Return a keypad up event.
  3182. //
  3183. pIMEventOut->type = IM_TYPE_VK1;
  3184. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  3185. (VK_NUMPAD0 +
  3186. (m_aimControlledEventsToReturn[m_imControlledNumEventsReturned] -
  3187. IEM_EVENT_KEYPAD0_UP));
  3188. pIMEventOut->data.keyboard.flags = IM_FLAG_KEYBOARD_DOWN |
  3189. IM_FLAG_KEYBOARD_RELEASE |
  3190. IM_FLAG_KEYBOARD_ALT_DOWN;
  3191. }
  3192. else
  3193. {
  3194. switch (m_aimControlledEventsToReturn[m_imControlledNumEventsReturned])
  3195. {
  3196. case IEM_EVENT_CTRL_DOWN:
  3197. //
  3198. // Set up a Ctrl down event.
  3199. //
  3200. pIMEventOut->type = IM_TYPE_VK1;
  3201. pIMEventOut->data.keyboard.keyCode =
  3202. VK_CONTROL;
  3203. pIMEventOut->data.keyboard.flags = 0;
  3204. break;
  3205. case IEM_EVENT_CTRL_UP:
  3206. //
  3207. // Set up a Ctrl up event.
  3208. //
  3209. pIMEventOut->type = IM_TYPE_VK1;
  3210. pIMEventOut->data.keyboard.keyCode =
  3211. VK_CONTROL;
  3212. pIMEventOut->data.keyboard.flags =
  3213. IM_FLAG_KEYBOARD_DOWN | IM_FLAG_KEYBOARD_RELEASE;
  3214. break;
  3215. case IEM_EVENT_SHIFT_DOWN:
  3216. //
  3217. // Set up a Shift down event.
  3218. //
  3219. pIMEventOut->type = IM_TYPE_VK1;
  3220. pIMEventOut->data.keyboard.keyCode =
  3221. VK_SHIFT;
  3222. pIMEventOut->data.keyboard.flags = 0;
  3223. break;
  3224. case IEM_EVENT_SHIFT_UP:
  3225. //
  3226. // Set up a Shift up event.
  3227. //
  3228. pIMEventOut->type = IM_TYPE_VK1;
  3229. pIMEventOut->data.keyboard.keyCode =
  3230. VK_SHIFT;
  3231. pIMEventOut->data.keyboard.flags =
  3232. IM_FLAG_KEYBOARD_DOWN | IM_FLAG_KEYBOARD_RELEASE;
  3233. break;
  3234. case IEM_EVENT_MENU_DOWN:
  3235. //
  3236. // Set up a Menu down event.
  3237. //
  3238. pIMEventOut->type = IM_TYPE_VK1;
  3239. pIMEventOut->data.keyboard.keyCode = VK_MENU;
  3240. pIMEventOut->data.keyboard.flags = 0;
  3241. break;
  3242. case IEM_EVENT_MENU_UP:
  3243. //
  3244. // Set up a Menu up event.
  3245. //
  3246. pIMEventOut->type = IM_TYPE_VK1;
  3247. pIMEventOut->data.keyboard.keyCode = VK_MENU;
  3248. pIMEventOut->data.keyboard.flags =
  3249. IM_FLAG_KEYBOARD_DOWN | IM_FLAG_KEYBOARD_RELEASE;
  3250. break;
  3251. case IEM_EVENT_REPLAY:
  3252. //
  3253. // Just copy the packet.
  3254. //
  3255. *pIMEventOut = *pIMEventIn;
  3256. break;
  3257. case IEM_EVENT_REPLAY_VK:
  3258. //
  3259. // Replay the VK from m_imControlledVKToReplay using the
  3260. // flags on the incoming packet.
  3261. //
  3262. *pIMEventOut = *pIMEventIn;
  3263. pIMEventOut->type = IM_TYPE_VK1;
  3264. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  3265. m_imControlledVKToReplay;
  3266. break;
  3267. case IEM_EVENT_REPLAY_VK_UP:
  3268. //
  3269. // Replay an up key event for the VK in
  3270. // m_imControlledVKToReplay.
  3271. //
  3272. pIMEventOut->type = IM_TYPE_VK1;
  3273. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  3274. m_imControlledVKToReplay;
  3275. pIMEventOut->data.keyboard.flags =
  3276. IM_FLAG_KEYBOARD_DOWN | IM_FLAG_KEYBOARD_RELEASE;
  3277. break;
  3278. case IEM_EVENT_REPLAY_VK_DOWN:
  3279. //
  3280. // Replay a down key event for the VK in
  3281. // m_imControlledVKToReplay.
  3282. //
  3283. pIMEventOut->type = IM_TYPE_VK1;
  3284. pIMEventOut->data.keyboard.keyCode = (TSHR_UINT16)
  3285. m_imControlledVKToReplay;
  3286. pIMEventOut->data.keyboard.flags = 0;
  3287. break;
  3288. case IEM_EVENT_NORMAL:
  3289. //
  3290. // Play back the event but force it to be normal.
  3291. //
  3292. *pIMEventOut = *pIMEventIn;
  3293. pIMEventOut->data.keyboard.flags &=
  3294. (TSHR_UINT16)~IM_FLAG_KEYBOARD_ALT_DOWN;
  3295. break;
  3296. case IEM_EVENT_SYSTEM:
  3297. //
  3298. // Play back the event but force it to be system.
  3299. //
  3300. *pIMEventOut = *pIMEventIn;
  3301. pIMEventOut->data.keyboard.flags |=
  3302. IM_FLAG_KEYBOARD_ALT_DOWN;
  3303. break;
  3304. default:
  3305. ERROR_OUT(( "Invalid code path"));
  3306. break;
  3307. }
  3308. }
  3309. }
  3310. m_imControlledNumEventsReturned++;
  3311. //
  3312. // If we're going to playback a NUMLOCK event, make sure we force
  3313. // the keyboard LEDs to be accurate.
  3314. //
  3315. if ((pIMEventOut->type == IM_TYPE_VK1) &&
  3316. (pIMEventOut->data.keyboard.keyCode == VK_NUMLOCK) &&
  3317. IS_IM_KEY_PRESS(pIMEventOut->data.keyboard.flags))
  3318. {
  3319. TRACE_OUT(("Playing back NUMLOCK; add IM_FLAG_KEYBOARD_UPDATESTATE"));
  3320. pIMEventOut->data.keyboard.flags |= IM_FLAG_KEYBOARD_UPDATESTATE;
  3321. }
  3322. return(TRUE);
  3323. }
  3324. DebugExitBOOL(ASShare::IMTranslateIncoming, FALSE);
  3325. return(FALSE);
  3326. }
  3327. //
  3328. // FUNCTION: IMInsertModifierKeystrokes
  3329. //
  3330. // DESCRIPTION:
  3331. //
  3332. // This function inserts various modifier keystrokes into the supplied
  3333. // buffer to move from one modifier state to another.
  3334. //
  3335. // PARAMETERS:
  3336. //
  3337. // curKbState - the current modifier state (bit 0 - Shift, bit 1 - Ctrl,
  3338. // bit 2 - Menu).
  3339. //
  3340. // targetKbState - the state we want the modifiers to be in
  3341. //
  3342. // pEventQueue - a pointer to an array where the required events can be
  3343. // inserted
  3344. //
  3345. // RETURNS: the number of events inserted
  3346. //
  3347. //
  3348. UINT ASShare::IMInsertModifierKeystrokes
  3349. (
  3350. BYTE curKbState,
  3351. BYTE targetKbState,
  3352. LPUINT pEventQueue
  3353. )
  3354. {
  3355. UINT kbDelta;
  3356. UINT events = 0;
  3357. DebugEntry(ASShare::IMInsertModifierKeystrokes);
  3358. //
  3359. // Find out which modifiers are different.
  3360. //
  3361. kbDelta = curKbState ^ targetKbState;
  3362. TRACE_OUT(( "Keyboard delat %x", kbDelta));
  3363. //
  3364. // Now generate the right events to get us into the correct modifier
  3365. // state.
  3366. //
  3367. if (kbDelta & IEM_SHIFT_DOWN)
  3368. {
  3369. //
  3370. // Shift state is different - do we need an up or down.
  3371. //
  3372. if (curKbState & IEM_SHIFT_DOWN)
  3373. {
  3374. //
  3375. // We need an up.
  3376. //
  3377. pEventQueue[events++] = IEM_EVENT_SHIFT_UP;
  3378. }
  3379. else
  3380. {
  3381. //
  3382. // We need a down.
  3383. //
  3384. pEventQueue[events++] = IEM_EVENT_SHIFT_DOWN;
  3385. }
  3386. }
  3387. //
  3388. // Same process for Ctrl and Alt.
  3389. //
  3390. if (kbDelta & IEM_CTRL_DOWN)
  3391. {
  3392. if (curKbState & IEM_CTRL_DOWN)
  3393. {
  3394. pEventQueue[events++] = IEM_EVENT_CTRL_UP;
  3395. }
  3396. else
  3397. {
  3398. pEventQueue[events++] = IEM_EVENT_CTRL_DOWN;
  3399. }
  3400. }
  3401. if (kbDelta & IEM_MENU_DOWN)
  3402. {
  3403. if (curKbState & IEM_MENU_DOWN)
  3404. {
  3405. pEventQueue[events++] = IEM_EVENT_MENU_UP;
  3406. }
  3407. else
  3408. {
  3409. pEventQueue[events++] = IEM_EVENT_MENU_DOWN;
  3410. }
  3411. }
  3412. DebugExitDWORD(ASShare::IMInsertModifierKeystrokes, events);
  3413. return(events);
  3414. }
  3415. //
  3416. // IMInjectEvent()
  3417. //
  3418. // DESCRIPTION:
  3419. //
  3420. // Called by IMMaybeInjectEvents when it is ready to inject an event.
  3421. // Given a pointer to a IMOSEVENT this function formats it correctly and
  3422. // calls the appropriate USER callback. It also updates the async key
  3423. // state arrays for the source queue and USER and sets m_imLastInjectTime to
  3424. // the tick count at which the event was injected. We protect against
  3425. // injecting up key strokes/mouse buttons when USER does not think the
  3426. // key/button is down in this function. It is quite possible (given the
  3427. // potential variety of CAs) that the IM will be asked to inject an up
  3428. // event when there has been no corresponding down event. This should be
  3429. // harmless as it is possible for this to happen in real life (ie the
  3430. // system message queue is full when the down event happens but there is
  3431. // space when the up event happens). However, it is quite unlikely and it
  3432. // is more likely that injecting these unmatched events will confuse
  3433. // applications.
  3434. //
  3435. // PARAMETERS:
  3436. //
  3437. // pEvent - pointer to an IMOSEVENT.
  3438. //
  3439. // THIS WORKS FOR NT AND WIN95.
  3440. //
  3441. BOOL ASShare::IMInjectEvent(LPIMOSEVENT pEvent)
  3442. {
  3443. UINT clickTime;
  3444. TSHR_UINT16 flags;
  3445. TSHR_UINT16 flagsAfter;
  3446. LPMSEV pMouseEvent;
  3447. DebugEntry(IMInjectEvent);
  3448. //
  3449. // Now inject the event.
  3450. //
  3451. switch (pEvent->type)
  3452. {
  3453. case IM_MOUSE_EVENT:
  3454. //
  3455. // Set up a pointer to the mouse event data.
  3456. //
  3457. pMouseEvent = &(pEvent->event.mouse);
  3458. //
  3459. // Check whether this is an unmatched up event
  3460. //
  3461. if ((IM_MEV_BUTTON1_UP(*pEvent) &&
  3462. IM_KEY_STATE_IS_UP(m_aimControlledKeyStates[VK_LBUTTON])) ||
  3463. (IM_MEV_BUTTON2_UP(*pEvent) &&
  3464. IM_KEY_STATE_IS_UP(m_aimControlledKeyStates[VK_RBUTTON])) ||
  3465. (IM_MEV_BUTTON3_UP(*pEvent) &&
  3466. IM_KEY_STATE_IS_UP(m_aimControlledKeyStates[VK_MBUTTON])))
  3467. {
  3468. //
  3469. // This is an unmatched up event so just discard it here
  3470. //
  3471. TRACE_OUT(("IMInjectEvent: discarding unmatched mouse up event"));
  3472. DC_QUIT;
  3473. }
  3474. //
  3475. // Store the injection time of this event.
  3476. //
  3477. m_imControlledLastLowLevelMouseEventTime = GetTickCount();
  3478. //
  3479. // Store the mouse position - only consider absolute mouse
  3480. // moves. (Note that for the cases in which we inject a
  3481. // relative mouse event we always set the co-ordinate change to
  3482. // 0).
  3483. //
  3484. if (pMouseEvent->flags & MOUSEEVENTF_ABSOLUTE)
  3485. {
  3486. m_imControlledLastMousePos.x = pMouseEvent->pt.x;
  3487. m_imControlledLastMousePos.y = pMouseEvent->pt.y;
  3488. TRACE_OUT(( "Updating mouse position (%d:%d)",
  3489. m_imControlledLastMousePos.x,
  3490. m_imControlledLastMousePos.y));
  3491. }
  3492. //
  3493. // Inject the event.
  3494. //
  3495. TRACE_OUT(("IMInjectEvent: MOUSE parameters are:"));
  3496. TRACE_OUT((" flags 0x%08x", pMouseEvent->flags));
  3497. TRACE_OUT((" time 0x%08x", m_imControlledLastLowLevelMouseEventTime));
  3498. TRACE_OUT((" position (%d, %d)", pMouseEvent->pt.x, pMouseEvent->pt.y));
  3499. TRACE_OUT((" mouseData %d", pMouseEvent->mouseData));
  3500. TRACE_OUT((" dwExtra %d", pMouseEvent->dwExtraInfo));
  3501. //
  3502. // Finally scale the logical screen co-ordinates to the full
  3503. // 16-bit range (0..65535).
  3504. //
  3505. ASSERT(m_pasLocal->cpcCaps.screen.capsScreenWidth);
  3506. ASSERT(m_pasLocal->cpcCaps.screen.capsScreenHeight);
  3507. pMouseEvent->pt.x = IM_MOUSEPOS_LOG_TO_OS(pMouseEvent->pt.x,
  3508. m_pasLocal->cpcCaps.screen.capsScreenWidth);
  3509. pMouseEvent->pt.y = IM_MOUSEPOS_LOG_TO_OS(pMouseEvent->pt.y,
  3510. m_pasLocal->cpcCaps.screen.capsScreenHeight);
  3511. OSI_InjectMouseEvent(pMouseEvent->flags, pMouseEvent->pt.x,
  3512. pMouseEvent->pt.y, pMouseEvent->mouseData, pMouseEvent->dwExtraInfo);
  3513. break;
  3514. case IM_KEYBOARD_EVENT:
  3515. //
  3516. // Check whether this is an unmatched up event
  3517. //
  3518. if (IM_KEV_KEYUP(*pEvent) &&
  3519. IM_KEY_STATE_IS_UP(m_aimControlledKeyStates[IM_KEV_VKCODE(*pEvent)]))
  3520. {
  3521. //
  3522. // This is an unmatched up event so just discard it.
  3523. //
  3524. TRACE_OUT(("IMInjectEvent: discarding unmatched key up event %04hX",
  3525. IM_KEV_VKCODE(*pEvent)));
  3526. DC_QUIT;
  3527. }
  3528. //
  3529. // Inject the event.
  3530. //
  3531. TRACE_OUT(("IMInjectEvent: KEYBD parameters are:"));
  3532. TRACE_OUT((" flags 0x%08x", pEvent->event.keyboard.flags));
  3533. TRACE_OUT((" virtkey %u", pEvent->event.keyboard.vkCode));
  3534. TRACE_OUT((" scan code %u", pEvent->event.keyboard.scanCode));
  3535. OSI_InjectKeyboardEvent(pEvent->event.keyboard.flags,
  3536. pEvent->event.keyboard.vkCode, pEvent->event.keyboard.scanCode,
  3537. pEvent->event.keyboard.dwExtraInfo);
  3538. if (pEvent->flags & IM_FLAG_UPDATESTATE)
  3539. {
  3540. BYTE kbState[256];
  3541. TRACE_OUT(("Updating keyboard LED state after playing back toggle"));
  3542. GetKeyboardState(kbState);
  3543. SetKeyboardState(kbState);
  3544. }
  3545. break;
  3546. default:
  3547. //
  3548. // We do nothing for unexpected events - this allow us to add
  3549. // more events later that can be sent to back level systems
  3550. // where they will be safely ignored
  3551. //
  3552. TRACE_OUT(( "Unexpected event %d", pEvent->type));
  3553. DC_QUIT;
  3554. }
  3555. //
  3556. // If we get here successfully then we want to update our copy of the
  3557. // async key state so set the flag.
  3558. //
  3559. IMUpdateAsyncArray(m_aimControlledKeyStates, pEvent);
  3560. DC_EXIT_POINT:
  3561. DebugExitBOOL(ASShare::IMInjectEvent, TRUE);
  3562. return(TRUE);
  3563. }
  3564. //
  3565. // FUNCTION: IMInjectingEvents
  3566. //
  3567. BOOL ASShare::IMInjectingEvents(void)
  3568. {
  3569. LPIMOSEVENT pNextEvent;
  3570. IMOSEVENT mouseMoveEvent;
  3571. UINT tick;
  3572. UINT targetTime;
  3573. UINT targetDelta;
  3574. BOOL rc = TRUE;
  3575. DebugEntry(ASShare::IMInjectingEvents);
  3576. if (m_pasLocal->m_caControlledBy && m_imControlledOSQ.numEvents)
  3577. {
  3578. pNextEvent = m_imControlledOSQ.events + m_imControlledOSQ.head;
  3579. //
  3580. // First check if this is a remote mouse event being injected too
  3581. // soon after the previous one. We used to only do this for mouse
  3582. // move events to prevent them all being spoiled if they were
  3583. // injected too quickly. However, we now do it for all mouse
  3584. // events because of a bug in Windows USER whereby if the mouse
  3585. // press which brings up a menu is processed after the
  3586. // corresponding mouse release has been passed to USER (so that the
  3587. // async state of the mouse button is up) then the menu is brought
  3588. // up in the position it is brought up in if it is selected via the
  3589. // keyboard rather than the position it is brought up in if it is
  3590. // selected by the mouse. (These positions are only different when
  3591. // the menu cannot be placed completely below or above the menu
  3592. // bar). This can then lead to the mouse release selecting an item
  3593. // from the menu.
  3594. //
  3595. tick = GetTickCount();
  3596. if (m_imfControlledPaceInjection &&
  3597. (pNextEvent->type == IM_MOUSE_EVENT))
  3598. {
  3599. //
  3600. // This is a remote mouse event so check that now is a good
  3601. // time to inject it Smooth out the backlog adjustment so that
  3602. // packet bursts do not get spoiled too much. Set an absolute
  3603. // lg_lpimSharedData->imit on injection delay of the low sample rate so that
  3604. // timestamp anomolies do not cause us to withhold messages
  3605. //
  3606. //
  3607. // The target delta between last and current events is
  3608. // calculated from the remote timestamps
  3609. //
  3610. targetDelta = abs((int)(pNextEvent->time -
  3611. m_imControlledLastMouseRemoteTime));
  3612. if (targetDelta > IM_LOCAL_MOUSE_SAMPLING_GAP_LOW_MS)
  3613. {
  3614. targetDelta = IM_LOCAL_MOUSE_SAMPLING_GAP_LOW_MS;
  3615. }
  3616. //
  3617. // The target injection time is based on the last injection
  3618. // time and our target delta, adjusted for any backlog we are
  3619. // seeing. Because packeting gives a jerky backlog we need to
  3620. // smooth our adjustment out (only modify by backlog/8)
  3621. //
  3622. targetTime = m_imControlledLastMouseLocalTime +
  3623. targetDelta - (m_imControlledMouseBacklog/8);
  3624. TRACE_OUT(( "Last tremote %#lx, this tremote %#lx, backlog %#lx",
  3625. m_imControlledLastMouseRemoteTime,
  3626. pNextEvent->time,
  3627. m_imControlledMouseBacklog));
  3628. TRACE_OUT(( "Last tlocal %#lx, tick %#lx, targetTime %#lx",
  3629. m_imControlledLastMouseLocalTime,
  3630. tick,
  3631. targetTime));
  3632. //
  3633. // Now inject the events - ignore them if they are too early
  3634. //
  3635. if (IM_MEV_ABS_MOVE(*pNextEvent) && (tick < targetTime))
  3636. {
  3637. //
  3638. // If values seem wild (for example this is the first mouse
  3639. // event ever) then reset them
  3640. //
  3641. if (targetTime > tick + 1000)
  3642. {
  3643. m_imControlledLastMouseRemoteTime = pNextEvent->time;
  3644. m_imControlledLastMouseLocalTime = tick;
  3645. m_imControlledMouseBacklog = 0;
  3646. TRACE_OUT(( "Wild values - reset"));
  3647. }
  3648. else
  3649. {
  3650. //
  3651. // This is too early - get out of the loop.
  3652. //
  3653. rc = FALSE;
  3654. DC_QUIT;
  3655. }
  3656. }
  3657. else
  3658. {
  3659. //
  3660. // We will inject this event (and remember when we did it
  3661. // so we don't inject the next one to quickly). Calculate
  3662. // the backlog because we may have to make up for a
  3663. // processing delay If this event is long (1000 mS) after
  3664. // our projected event time then assume a pause in movement
  3665. // and reset the backlog to avoid progressive erosion.
  3666. // Otherwise calculate the new backlog.
  3667. //
  3668. // Perf - don't reset backlog unless the time has expired.
  3669. // Restting just because we see a click means that we
  3670. // actually increase the latency by assuming that mouse
  3671. // messages queued behind the tick are not backlogged.
  3672. //
  3673. if (tick < (targetTime + 1000))
  3674. {
  3675. m_imControlledMouseBacklog += ( tick -
  3676. m_imControlledLastMouseLocalTime -
  3677. targetDelta );
  3678. }
  3679. else
  3680. {
  3681. m_imControlledMouseBacklog = 0;
  3682. TRACE_OUT(( "Non move/big gap in move"));
  3683. }
  3684. m_imControlledLastMouseRemoteTime = pNextEvent->time;
  3685. m_imControlledLastMouseLocalTime = tick;
  3686. }
  3687. }
  3688. else
  3689. {
  3690. //
  3691. // This is not a remote mouse event. Reset the
  3692. // m_imNextRemoteMouseEvent to zero so we don't hold up the next
  3693. // remote mouse event.
  3694. //
  3695. m_imControlledLastMouseRemoteTime = pNextEvent->time;
  3696. m_imControlledLastMouseLocalTime = tick;
  3697. m_imControlledMouseBacklog = 0;
  3698. TRACE_OUT(( "Local/non-paced/non-mouse - reset"));
  3699. }
  3700. //
  3701. // Only inject the event if IM_FLAG_DONT_REPLAY is not set
  3702. //
  3703. if (!(pNextEvent->flags & IM_FLAG_DONT_REPLAY))
  3704. {
  3705. //
  3706. // If the event is a mouse click then we always inject a mouse
  3707. // move event g_lpimSharedData->immediately before it to ensure that the current
  3708. // position is correct before the click is injected.
  3709. //
  3710. // This is because USER does not handle combined "move and
  3711. // click" events correctly (it appears to treat them as "click
  3712. // and move", generating a mouse move event AFTER the click
  3713. // event, rather than before). Under normal Windows operation
  3714. // it appears (from observation) that movement events and click
  3715. // events are generated separately (i.e. a click event will
  3716. // never have the movement flag set). However, incoming mouse
  3717. // click events may have positions that are different from the
  3718. // last mouse move event so we must inject the extra move event
  3719. // to keep USER happy.
  3720. //
  3721. if ( (pNextEvent->type == IM_MOUSE_EVENT) &&
  3722. (IM_MEV_BUTTON_DOWN(*pNextEvent) ||
  3723. IM_MEV_BUTTON_UP(*pNextEvent)) )
  3724. {
  3725. TRACE_OUT(( "Mouse clk: injecting extra"));
  3726. //
  3727. // Take a copy of the event.
  3728. //
  3729. mouseMoveEvent = *pNextEvent;
  3730. //
  3731. // Turn the mouse click event into a mouse move event with
  3732. // the absolute/relative flag unchanged.
  3733. //
  3734. mouseMoveEvent.event.mouse.flags &= MOUSEEVENTF_ABSOLUTE;
  3735. mouseMoveEvent.event.mouse.flags |= MOUSEEVENTF_MOVE;
  3736. //
  3737. // Inject the additional move event.
  3738. //
  3739. IMInjectEvent(&mouseMoveEvent);
  3740. //
  3741. // As the position is now correct, we turn the click into a
  3742. // relative event with an unchanged position.
  3743. //
  3744. pNextEvent->event.mouse.flags &= ~MOUSEEVENTF_ABSOLUTE;
  3745. pNextEvent->event.mouse.pt.x = 0;
  3746. pNextEvent->event.mouse.pt.y = 0;
  3747. //
  3748. // If this is a mouse down click then flag the injection
  3749. // heuristic as active. We deactivate the heuristic when
  3750. // the mouse is released so that dragging over menus can be
  3751. // done without delay. (We keep the heuristic active when
  3752. // mouse is depressed because most drawing apps perform
  3753. // freehand drawing in this way.
  3754. //
  3755. if (IM_MEV_BUTTON_DOWN(*pNextEvent))
  3756. {
  3757. TRACE_OUT(( "Injection pacing active"));
  3758. m_imfControlledPaceInjection = TRUE;
  3759. }
  3760. else
  3761. {
  3762. TRACE_OUT(( "Injection pacing inactive"));
  3763. m_imfControlledPaceInjection = FALSE;
  3764. }
  3765. }
  3766. //
  3767. // Inject the real event.
  3768. //
  3769. TRACE_OUT(( "Injecting the evnt now"));
  3770. IMInjectEvent(pNextEvent);
  3771. }
  3772. IMUpdateAsyncArray(m_aimControlledControllerKeyStates, pNextEvent);
  3773. ASSERT(m_imControlledOSQ.numEvents);
  3774. m_imControlledOSQ.numEvents--;
  3775. m_imControlledOSQ.head = CIRCULAR_INDEX(m_imControlledOSQ.head, 1,
  3776. IM_SIZE_OSQ);
  3777. //
  3778. // We only inject a single keyboard event per pass to prevent
  3779. // excessive spoiling of repeated events. Having got them here it
  3780. // seems a shame to spoil them. Spoil down to 5 so we don't get
  3781. // excessive overrun following a key repeat sequence.
  3782. //
  3783. if ((pNextEvent->type == IM_KEYBOARD_EVENT) &&
  3784. (m_imControlledOSQ.numEvents < 5))
  3785. {
  3786. TRACE_OUT(( "Keyboard event so leaving loop"));
  3787. rc = FALSE;
  3788. }
  3789. }
  3790. else
  3791. {
  3792. //
  3793. // We're done.
  3794. //
  3795. rc = FALSE;
  3796. }
  3797. DC_EXIT_POINT:
  3798. DebugExitBOOL(ASShare::IMInjectingEvents, rc);
  3799. return(rc);
  3800. }
  3801. //
  3802. // IMMaybeInjectEvents()
  3803. //
  3804. // DESCRIPTION:
  3805. //
  3806. // This is called whenever the IM believes there may be an opportunity to
  3807. // inject more events into USER via the input event callbacks. The two
  3808. // main reasons for this are:
  3809. //
  3810. // 1. We have received a new event in the mouse or keyboard hooks. This
  3811. // will normally imply that an event has been removed from the system
  3812. // message queue so there will be at least one free slot on it.
  3813. //
  3814. // 2. We have added a new event (or events) to either the local or remote
  3815. // USER event queues. This means there will be at least one event waiting
  3816. // to be injected.
  3817. //
  3818. // This function is also called periodically (via IM_Periodic) to keep
  3819. // things moving.
  3820. //
  3821. // In order for an event to be injected there must be
  3822. //
  3823. // - an event waiting (with IM_FLAG_DONT_REPLAY reset)
  3824. // - a space on the USER system message queue
  3825. // - a new time stamp (if we are switching event sources).
  3826. //
  3827. // This function works as a state machine. It always starts in a specified
  3828. // state and will then take various actions and then possibly enter a new
  3829. // state. It continues to loop through this process until it cannot take
  3830. // any actions in one of its states at which point it returns.
  3831. //
  3832. // There are four states (each of which is further qualified by whether it
  3833. // refers to local or remote events). The states are:
  3834. //
  3835. // IM_INJECTING_EVENTS - we are injecting events into USER from the
  3836. // appropriate queue.
  3837. //
  3838. // IM_WAITING_FOR_TICK - we are waiting for a timer tick to give us a new
  3839. // timestamp before injecting events
  3840. //
  3841. // IM_DEVICE_TO_NEW_SOURCE - we are injecting fake events to bring the
  3842. // state of the keyboard and mouse (as seen by USER) into line with the
  3843. // state of the new source of input.
  3844. //
  3845. void ASShare::IMMaybeInjectEvents(void)
  3846. {
  3847. IMEVENT eventIn;
  3848. IMEVENT eventOut;
  3849. IMOSEVENT OSEvent;
  3850. BOOL replay;
  3851. UINT rcConvert;
  3852. UINT now;
  3853. HWND hwndDest;
  3854. HWND hwndParent;
  3855. POINT ptMousePos;
  3856. LPIMOSEVENT pNextEvent;
  3857. DebugEntry(IMMaybeInjectEvents);
  3858. ASSERT(m_pasLocal->m_caControlledBy);
  3859. //
  3860. // Check whether we should wait before converting events. We need to
  3861. // do this to prevent us being swamped with mouse move events when
  3862. // we're waiting for the desktop to scroll.
  3863. //
  3864. now = GetTickCount();
  3865. if (IN_TIME_RANGE(m_imControlledLastIncompleteConversion,
  3866. m_imControlledLastIncompleteConversion + IM_MIN_RECONVERSION_INTERVAL_MS, now))
  3867. {
  3868. goto IM_DISCARD;
  3869. }
  3870. //
  3871. // NOW TRANSLATE NETWORK EVENTS TO OS EVENTS
  3872. // We'll discard or inject them when the time is right.
  3873. // But don't do translation if there are still OS events left
  3874. // waiting to be injected from the previous packet.
  3875. //
  3876. if (m_imControlledEventQ.numEvents && !m_imControlledOSQ.numEvents)
  3877. {
  3878. //
  3879. // Get the event from the front of the network event queue.
  3880. //
  3881. eventIn = m_imControlledEventQ.events[0];
  3882. replay = FALSE;
  3883. switch (eventIn.type)
  3884. {
  3885. case IM_TYPE_3BUTTON:
  3886. {
  3887. // Always allow mouse moves
  3888. if (!(eventIn.data.mouse.flags & IM_FLAG_MOUSE_DOWN))
  3889. {
  3890. replay = TRUE;
  3891. }
  3892. else
  3893. {
  3894. //
  3895. // Allow click events to shared windows or
  3896. // if a different desktop/screensaver is around
  3897. //
  3898. ptMousePos.x = eventIn.data.mouse.x;
  3899. ptMousePos.y = eventIn.data.mouse.y;
  3900. hwndDest = WindowFromPoint(ptMousePos);
  3901. if (HET_WindowIsHosted(hwndDest) ||
  3902. OSI_IsWindowScreenSaver(hwndDest))
  3903. {
  3904. replay = TRUE;
  3905. }
  3906. }
  3907. break;
  3908. }
  3909. case IM_TYPE_VK1:
  3910. case IM_TYPE_VK2:
  3911. case IM_TYPE_ASCII:
  3912. {
  3913. hwndDest = GetForegroundWindow();
  3914. if (HET_WindowIsHosted(hwndDest) ||
  3915. OSI_IsWindowScreenSaver(hwndDest))
  3916. {
  3917. replay = TRUE;
  3918. }
  3919. break;
  3920. }
  3921. default:
  3922. ERROR_OUT(("Bogus NETWORK event being translated"));
  3923. break;
  3924. }
  3925. //
  3926. // After this while loop we test rcConvert to see whether the
  3927. // input packet can now be removed (has been fully processed).
  3928. // We only SET rcConvert if IMTranslateIncoming returns TRUE,
  3929. // yet IM_TR specifically returns FALSE to indicate that the
  3930. // input packet does not contain an event and is to be
  3931. // discarded. To fix this - set rcConvert here.
  3932. //
  3933. rcConvert = IM_IMQUEUEREMOVE;
  3934. while (IMTranslateIncoming(&eventIn, &eventOut))
  3935. {
  3936. rcConvert = IMConvertIMEventToOSEvent(&eventOut, &OSEvent);
  3937. //
  3938. // Inject the event into the OS queue (if required).
  3939. //
  3940. if (rcConvert & IM_OSQUEUEINJECT)
  3941. {
  3942. if (!replay)
  3943. {
  3944. OSEvent.flags |= IM_FLAG_DONT_REPLAY;
  3945. }
  3946. // Add to playback queue
  3947. // Is the queue filled up?
  3948. if (m_imControlledOSQ.numEvents == IM_SIZE_OSQ)
  3949. {
  3950. ERROR_OUT(("Failed to add OS event to queue"));
  3951. }
  3952. else
  3953. {
  3954. // Put this element at the tail.
  3955. m_imControlledOSQ.events[CIRCULAR_INDEX(m_imControlledOSQ.head,
  3956. m_imControlledOSQ.numEvents, IM_SIZE_OSQ)] =
  3957. OSEvent;
  3958. m_imControlledOSQ.numEvents++;
  3959. }
  3960. }
  3961. }
  3962. //
  3963. // The following test is not ideal as it relies on the fact
  3964. // that any events for which IMConvertIMEventToUSEREvent does
  3965. // not set IM_IMQUEUEREMOVE had a one-one mapping.
  3966. //
  3967. // However, we know that this is always the case with mouse
  3968. // events, which are the only events that will be cause this
  3969. // flag to be unset.
  3970. //
  3971. if (rcConvert & IM_IMQUEUEREMOVE)
  3972. {
  3973. //
  3974. // Remove this from the network queue
  3975. //
  3976. m_imControlledEventQ.numEvents--;
  3977. UT_MoveMemory(&(m_imControlledEventQ.events[0]),
  3978. &(m_imControlledEventQ.events[1]),
  3979. sizeof(IMEVENT) * m_imControlledEventQ.numEvents);
  3980. }
  3981. else
  3982. {
  3983. //
  3984. // Remember this so we don't flood the input injection with
  3985. // events when we don't remove the network event from the
  3986. // queue.
  3987. //
  3988. TRACE_OUT(( "do not shuffle"));
  3989. m_imControlledLastIncompleteConversion = GetTickCount();
  3990. }
  3991. }
  3992. IM_DISCARD:
  3993. //
  3994. // Get rid of all discarded events. Update the remote controller's
  3995. // key state array to reflect it. But since we aren't going to replay
  3996. // these, don't update our local key state table.
  3997. //
  3998. while (m_imControlledOSQ.numEvents > 0)
  3999. {
  4000. pNextEvent = m_imControlledOSQ.events + m_imControlledOSQ.head;
  4001. if (!(pNextEvent->flags & IM_FLAG_DONT_REPLAY))
  4002. {
  4003. // We're done.
  4004. break;
  4005. }
  4006. IMUpdateAsyncArray(m_aimControlledControllerKeyStates, pNextEvent);
  4007. ASSERT(m_imControlledOSQ.numEvents);
  4008. m_imControlledOSQ.numEvents--;
  4009. m_imControlledOSQ.head = CIRCULAR_INDEX(m_imControlledOSQ.head, 1,
  4010. IM_SIZE_OSQ);
  4011. }
  4012. //
  4013. // NOW INJECT OS EVENTS into system
  4014. //
  4015. while (IMInjectingEvents())
  4016. {
  4017. ;
  4018. }
  4019. DebugExitVOID(ASShare::IMMaybeInjectEvents);
  4020. }
  4021. //
  4022. // FUNCTION: IMUpdateAsyncArray
  4023. //
  4024. // DESCRIPTION:
  4025. //
  4026. // Called with the address of one of our async key state arrays and a
  4027. // IMOSEVENT this function updates the async key state array according to
  4028. // the contents of the IMOSEVENT.
  4029. //
  4030. // PARAMETERS:
  4031. //
  4032. // paimKeyStates - pointer to async key state array.
  4033. //
  4034. // pEvent - pointer to IMOSEVENT.
  4035. //
  4036. // RETURNS: NONE
  4037. //
  4038. //
  4039. void ASShare::IMUpdateAsyncArray
  4040. (
  4041. LPBYTE paimKeyStates,
  4042. LPIMOSEVENT pEvent
  4043. )
  4044. {
  4045. UINT flags;
  4046. UINT vkCode;
  4047. DebugEntry(ASShare::IMUpdateAsyncArray);
  4048. switch (pEvent->type)
  4049. {
  4050. case IM_MOUSE_EVENT:
  4051. //
  4052. // Update the async key state arrays for this event. Note that
  4053. // we treat each event as independent - this is how Windows
  4054. // treats them and if all the up/down flags are set Windows
  4055. // will generate six mouse message! (and in down,up order).
  4056. //
  4057. flags = pEvent->event.mouse.flags;
  4058. if (flags & MOUSEEVENTF_LEFTDOWN)
  4059. {
  4060. IM_SET_VK_DOWN(paimKeyStates[VK_LBUTTON]);
  4061. }
  4062. if (flags & MOUSEEVENTF_LEFTUP)
  4063. {
  4064. IM_SET_VK_UP(paimKeyStates[VK_LBUTTON]);
  4065. }
  4066. if (flags & MOUSEEVENTF_RIGHTDOWN)
  4067. {
  4068. IM_SET_VK_DOWN(paimKeyStates[VK_RBUTTON]);
  4069. }
  4070. if (flags & MOUSEEVENTF_RIGHTUP)
  4071. {
  4072. IM_SET_VK_UP(paimKeyStates[VK_RBUTTON]);
  4073. }
  4074. if (flags & MOUSEEVENTF_MIDDLEDOWN)
  4075. {
  4076. IM_SET_VK_DOWN(paimKeyStates[VK_MBUTTON]);
  4077. }
  4078. if (flags & MOUSEEVENTF_MIDDLEUP)
  4079. {
  4080. IM_SET_VK_UP(paimKeyStates[VK_MBUTTON]);
  4081. }
  4082. break;
  4083. case IM_KEYBOARD_EVENT:
  4084. //
  4085. // Update the async key state arrays.
  4086. //
  4087. vkCode = IM_KEV_VKCODE(*pEvent);
  4088. if (IM_KEV_KEYUP(*pEvent))
  4089. {
  4090. IM_SET_VK_UP(paimKeyStates[vkCode]);
  4091. }
  4092. else
  4093. {
  4094. //
  4095. // This is a key down event - check if it is a press or a
  4096. // repeat.
  4097. //
  4098. if (IM_KEY_STATE_IS_UP(paimKeyStates[vkCode]))
  4099. {
  4100. //
  4101. // This is a key press as the key was previously up -
  4102. // alter the toggle state. We keep the toggle state
  4103. // for all keys although we currently only worry about
  4104. // it for the `known' toggles.
  4105. //
  4106. IM_TOGGLE_VK(paimKeyStates[vkCode]);
  4107. }
  4108. IM_SET_VK_DOWN(paimKeyStates[vkCode]);
  4109. }
  4110. break;
  4111. default:
  4112. //
  4113. // Just ignore unexpected events.
  4114. //
  4115. ERROR_OUT(( "Unexpected event %u", pEvent->type));
  4116. break;
  4117. }
  4118. DebugExitVOID(ASShare::IMUpdateAsyncArray);
  4119. }