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.

245 lines
7.6 KiB

  1. // SpBtnCtrl.cpp : Implement SpButtonControl which is to control the speech mode and status.
  2. //
  3. #include "private.h"
  4. #include "SpBtnCtrl.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. HRESULT SpButtonControl::SetDictationButton(BOOL fButtonDown, UINT uTimePressed)
  8. {
  9. return _SetButtonDown(DICTATION_BUTTON, fButtonDown, uTimePressed);
  10. }
  11. HRESULT SpButtonControl::SetCommandingButton(BOOL fButtonDown, UINT uTimePressed)
  12. {
  13. return _SetButtonDown(COMMANDING_BUTTON, fButtonDown, uTimePressed);
  14. }
  15. HRESULT SpButtonControl::_SetButtonDown(DWORD dwButton, BOOL fButtonDown, UINT uTimePressed)
  16. {
  17. BOOL fDictationOn = FALSE;
  18. BOOL fCommandingOn = FALSE;
  19. BOOL fMicrophoneOn = FALSE;
  20. DWORD dwMyState = dwButton ? TF_COMMANDING_ON : TF_DICTATION_ON;
  21. DWORD dwOtherState = dwButton ? TF_DICTATION_ON : TF_COMMANDING_ON;
  22. if (uTimePressed == 0)
  23. uTimePressed = GetTickCount();
  24. if (m_ulButtonDownTime[1 - dwButton])
  25. {
  26. // Other button pressed but not released.
  27. // In this scenario we ignore the second press since there is no perfect answer to what we could do instead.
  28. return S_OK;
  29. }
  30. fMicrophoneOn = GetMicrophoneOn( );
  31. fDictationOn = GetDictationOn( );
  32. fCommandingOn = GetCommandingOn( );
  33. BOOL fMyStateOn = dwButton ? fCommandingOn : fDictationOn;
  34. BOOL fOtherStateOn = dwButton ? fDictationOn : fCommandingOn;
  35. TraceMsg(TF_SPBUTTON, "uTimePressed=%d MicrophoneOnOff=%d", uTimePressed, fMicrophoneOn);
  36. TraceMsg(TF_SPBUTTON, "fDictationOn=%d,fCommandingOn=%d", fDictationOn,fCommandingOn);
  37. TraceMsg(TF_SPBUTTON, "fMyStateOn=%d, OtherStateOn=%d", fMyStateOn, fOtherStateOn);
  38. if (fButtonDown)
  39. {
  40. // Button has been pressed.
  41. if ( m_ulButtonDownTime[dwButton] )
  42. {
  43. TraceMsg(TF_SPBUTTON, "Double down event on speech button");
  44. return S_OK;
  45. }
  46. // Now we store the time to detect a press-and-hold.
  47. m_ulButtonDownTime[dwButton] = uTimePressed;
  48. if (fMicrophoneOn)
  49. {
  50. // Microphone is ON
  51. if (fCommandingOn && fDictationOn)
  52. {
  53. // Both dictation and commanding are on.
  54. // Switch microphone off, disable other state.
  55. m_fMicrophoneOnAtDown[dwButton] = TRUE;
  56. SetState(dwMyState);
  57. }
  58. if (fOtherStateOn)
  59. {
  60. // Leave microphone on, switch state.
  61. // Need to store other state to reset if it's a press-and-hold.
  62. m_fPreviouslyOtherStateOn[dwButton] = TRUE;
  63. SetState(dwMyState);
  64. }
  65. else if (fMyStateOn)
  66. {
  67. // Switch microphone off.
  68. m_fMicrophoneOnAtDown[dwButton] = TRUE;
  69. }
  70. else
  71. {
  72. // Microphone on but no state defined.
  73. // Switch microphone off, enable dictation.
  74. m_fMicrophoneOnAtDown[dwButton] = TRUE;
  75. SetState(dwMyState);
  76. }
  77. }
  78. else
  79. {
  80. // Microphone is OFF
  81. if (fCommandingOn && fDictationOn)
  82. {
  83. // Both dictation and commanding are on.
  84. // Switch microphone on, disable my state.
  85. SetState(dwMyState);
  86. SetMicrophoneOn(TRUE);
  87. }
  88. if (fOtherStateOn)
  89. {
  90. // Switch microphone on, switch state.
  91. SetState(dwMyState);
  92. SetMicrophoneOn(TRUE);
  93. }
  94. else if (fMyStateOn)
  95. {
  96. // Switch microphone on.
  97. SetMicrophoneOn(TRUE);
  98. }
  99. else
  100. {
  101. // Microphone off and no state defined.
  102. // Switch microphone on, enable my state.
  103. SetState(dwMyState);
  104. SetMicrophoneOn(TRUE);
  105. }
  106. }
  107. }
  108. else
  109. {
  110. // Button released.
  111. #ifdef DEBUG
  112. if ( m_ulButtonDownTime[dwButton] == 0 )
  113. TraceMsg(TF_SPBUTTON, "Speech button released without being pressed.");
  114. // Since the button has previously been pressed, the other state should not be enabled.
  115. if ( fOtherStateOn )
  116. TraceMsg(TF_SPBUTTON, "Other speech state incorrectly enabled on button release.");
  117. #endif
  118. // Will wrap after 49.7 days of continuous use.
  119. DWORD dwTimeElapsed = uTimePressed - m_ulButtonDownTime[dwButton];
  120. m_ulButtonDownTime[dwButton] = 0;
  121. // Is this a quick press or a press-and-hold action?
  122. if (dwTimeElapsed < PRESS_AND_HOLD)
  123. {
  124. // This is a quick release.
  125. if (m_fMicrophoneOnAtDown[dwButton])
  126. {
  127. // Microphone was on at button down. Need to switch microphone off.
  128. SetMicrophoneOn(FALSE);
  129. }
  130. m_fPreviouslyOtherStateOn[dwButton] = FALSE;
  131. m_fMicrophoneOnAtDown[dwButton] = FALSE;
  132. }
  133. else
  134. {
  135. // This is a press-and-hold.
  136. // We must either stop the microphone or return to other state.
  137. TraceMsg(TF_SPBUTTON, "press-and-hold button!");
  138. if (m_fPreviouslyOtherStateOn[dwButton])
  139. {
  140. // Other state was previously on. Leave microphone on, switch state.
  141. TraceMsg(TF_SPBUTTON, "Other state was previously on, leave Microphone On, switch state");
  142. SetState(dwOtherState);
  143. m_fPreviouslyOtherStateOn[dwButton] = FALSE;
  144. }
  145. else
  146. {
  147. // Other state was not previously on. Switch microphone off.
  148. TraceMsg(TF_SPBUTTON, "Other state was not previous on, switch microphone off");
  149. SetMicrophoneOn(FALSE);
  150. m_fMicrophoneOnAtDown[dwButton] = FALSE;
  151. }
  152. }
  153. }
  154. return S_OK;
  155. }
  156. BOOL SpButtonControl::GetDictationOn( )
  157. {
  158. DWORD dwGLobal;
  159. GetCompartmentDWORD(m_pimx->_tim, GUID_COMPARTMENT_SPEECH_GLOBALSTATE, &dwGLobal, TRUE);
  160. return (dwGLobal & TF_DICTATION_ON ) ? TRUE : FALSE;
  161. }
  162. BOOL SpButtonControl::GetCommandingOn( )
  163. {
  164. DWORD dwGLobal;
  165. GetCompartmentDWORD(m_pimx->_tim, GUID_COMPARTMENT_SPEECH_GLOBALSTATE, &dwGLobal, TRUE);
  166. return (dwGLobal & TF_COMMANDING_ON ) ? TRUE : FALSE;
  167. }
  168. HRESULT SpButtonControl::SetCommandingOn(void)
  169. {
  170. HRESULT hr;
  171. DWORD dw = TF_COMMANDING_ON;
  172. hr = SetCompartmentDWORD(m_pimx->_GetId( ), m_pimx->_tim, GUID_COMPARTMENT_SPEECH_GLOBALSTATE, dw, TRUE);
  173. return hr;
  174. }
  175. HRESULT SpButtonControl::SetDictationOn(void)
  176. {
  177. HRESULT hr;
  178. DWORD dw = TF_DICTATION_ON;
  179. hr = SetCompartmentDWORD(m_pimx->_GetId( ), m_pimx->_tim, GUID_COMPARTMENT_SPEECH_GLOBALSTATE, dw, TRUE);
  180. return hr;
  181. }
  182. HRESULT SpButtonControl::SetState(DWORD dwState)
  183. {
  184. HRESULT hr = S_OK;
  185. if (dwState == TF_DICTATION_ON)
  186. {
  187. hr = SetDictationOn();
  188. }
  189. else if (dwState == TF_COMMANDING_ON)
  190. {
  191. hr = SetCommandingOn();
  192. }
  193. else
  194. {
  195. TraceMsg(TF_SPBUTTON, "Unknown speech state requested.");
  196. Assert(0);
  197. hr = E_INVALIDARG;
  198. }
  199. return hr;
  200. }
  201. BOOL SpButtonControl::GetMicrophoneOn( )
  202. {
  203. Assert(m_pimx);
  204. return m_pimx->GetOnOff( );
  205. }
  206. void SpButtonControl::SetMicrophoneOn(BOOL fOn)
  207. {
  208. Assert(m_pimx);
  209. m_pimx->SetOnOff(fOn, TRUE);
  210. }