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.

229 lines
5.6 KiB

  1. //
  2. // kes.cpp
  3. //
  4. #include "private.h"
  5. #include "helpers.h"
  6. #include "kes.h"
  7. //+---------------------------------------------------------------------------
  8. //
  9. // IUnknown
  10. //
  11. //----------------------------------------------------------------------------
  12. STDAPI CKeyEventSink::QueryInterface(REFIID riid, void **ppvObj)
  13. {
  14. *ppvObj = NULL;
  15. if (IsEqualIID(riid, IID_IUnknown) ||
  16. IsEqualIID(riid, IID_ITfKeyEventSink))
  17. {
  18. *ppvObj = this;
  19. }
  20. if (*ppvObj)
  21. {
  22. AddRef();
  23. return S_OK;
  24. }
  25. return E_NOINTERFACE;
  26. }
  27. STDAPI_(ULONG) CKeyEventSink::AddRef()
  28. {
  29. return ++_cRef;
  30. }
  31. STDAPI_(ULONG) CKeyEventSink::Release()
  32. {
  33. _cRef--;
  34. Assert(_cRef >= 0);
  35. if (_cRef == 0)
  36. {
  37. delete this;
  38. return 0;
  39. }
  40. return _cRef;
  41. }
  42. //+---------------------------------------------------------------------------
  43. //
  44. // ctor
  45. //
  46. //----------------------------------------------------------------------------
  47. CKeyEventSink::CKeyEventSink(KESCALLBACK pfnCallback, void *pv)
  48. {
  49. Dbg_MemSetThisName(TEXT("CKeyEventSink"));
  50. _pfnPreKeyCallback = NULL;
  51. _pfnCallback = pfnCallback;
  52. _pv = pv;
  53. _cRef = 1;
  54. }
  55. CKeyEventSink::CKeyEventSink(KESCALLBACK pfnCallback, KESPREKEYCALLBACK pfnPreKeyCallback, void *pv)
  56. {
  57. Dbg_MemSetThisName(TEXT("CKeyEventSink"));
  58. _pfnCallback = pfnCallback;
  59. _pfnPreKeyCallback = pfnPreKeyCallback;
  60. _pv = pv;
  61. _cRef = 1;
  62. }
  63. //+---------------------------------------------------------------------------
  64. //
  65. // dtor
  66. //
  67. //----------------------------------------------------------------------------
  68. CKeyEventSink::~CKeyEventSink()
  69. {
  70. }
  71. //+---------------------------------------------------------------------------
  72. //
  73. // OnTestKeyDown
  74. //
  75. //----------------------------------------------------------------------------
  76. STDAPI CKeyEventSink::OnTestKeyDown(ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
  77. {
  78. return _pfnCallback(KES_CODE_KEYDOWN | KES_CODE_TEST, pic, wParam, lParam, pfEaten, _pv);
  79. }
  80. //+---------------------------------------------------------------------------
  81. //
  82. // OnKeyDown
  83. //
  84. //----------------------------------------------------------------------------
  85. STDAPI CKeyEventSink::OnKeyDown(ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
  86. {
  87. return _pfnCallback(KES_CODE_KEYDOWN, pic, wParam, lParam, pfEaten, _pv);
  88. }
  89. //+---------------------------------------------------------------------------
  90. //
  91. // OnTestKeyUp
  92. //
  93. //----------------------------------------------------------------------------
  94. STDAPI CKeyEventSink::OnTestKeyUp(ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
  95. {
  96. return _pfnCallback(KES_CODE_KEYUP | KES_CODE_TEST, pic, wParam, lParam, pfEaten, _pv);
  97. }
  98. //+---------------------------------------------------------------------------
  99. //
  100. // OnKeyUp
  101. //
  102. //----------------------------------------------------------------------------
  103. STDAPI CKeyEventSink::OnKeyUp(ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
  104. {
  105. return _pfnCallback(KES_CODE_KEYUP, pic, wParam, lParam, pfEaten, _pv);
  106. }
  107. //+---------------------------------------------------------------------------
  108. //
  109. // SetFocus
  110. //
  111. //----------------------------------------------------------------------------
  112. STDAPI CKeyEventSink::OnSetFocus(BOOL fForeground)
  113. {
  114. return _pfnCallback(KES_CODE_FOCUS, NULL, fForeground, 0, NULL, _pv);
  115. }
  116. //+---------------------------------------------------------------------------
  117. //
  118. // OnPreservedKey
  119. //
  120. //----------------------------------------------------------------------------
  121. STDAPI CKeyEventSink::OnPreservedKey(ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
  122. {
  123. if (!_pfnPreKeyCallback)
  124. return E_NOTIMPL;
  125. return _pfnPreKeyCallback(pic, rguid, pfEaten, _pv);
  126. }
  127. //+---------------------------------------------------------------------------
  128. //
  129. // Register
  130. //
  131. //----------------------------------------------------------------------------
  132. HRESULT CKeyEventSink::_Register(ITfThreadMgr *ptim, TfClientId tid, const KESPRESERVEDKEY *pprekey)
  133. {
  134. HRESULT hr;
  135. ITfKeystrokeMgr *pKeyMgr;
  136. Assert(_pfnPreKeyCallback);
  137. if (FAILED(ptim->QueryInterface(IID_ITfKeystrokeMgr, (void **)&pKeyMgr)))
  138. return E_FAIL;
  139. hr = E_FAIL;
  140. while (pprekey->pguid)
  141. {
  142. if (FAILED(pKeyMgr->PreserveKey(tid,
  143. *pprekey->pguid,
  144. &pprekey->tfpk,
  145. pprekey->psz,
  146. wcslen(pprekey->psz))))
  147. goto Exit;
  148. pprekey++;
  149. }
  150. ptim->AddRef();
  151. hr = S_OK;
  152. Exit:
  153. SafeRelease(pKeyMgr);
  154. return hr;
  155. }
  156. //+---------------------------------------------------------------------------
  157. //
  158. // CKeyEventSink::Unadvise
  159. //
  160. //----------------------------------------------------------------------------
  161. HRESULT CKeyEventSink::_Unregister(ITfThreadMgr *ptim, TfClientId tid, const KESPRESERVEDKEY *pprekey)
  162. {
  163. HRESULT hr;
  164. ITfKeystrokeMgr *pKeyMgr = NULL;
  165. hr = E_FAIL;
  166. Assert(_pfnPreKeyCallback);
  167. if (FAILED(ptim->QueryInterface(IID_ITfKeystrokeMgr, (void **)&pKeyMgr)))
  168. goto Exit;
  169. while (pprekey->pguid)
  170. {
  171. if (FAILED(pKeyMgr->UnpreserveKey(*pprekey->pguid, &pprekey->tfpk)))
  172. goto Exit;
  173. pprekey++;
  174. }
  175. hr = S_OK;
  176. SafeRelease(ptim);
  177. Exit:
  178. SafeRelease(pKeyMgr);
  179. return hr;
  180. }