Leaked source code of windows server 2003
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.

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