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

165 lines
4.6 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: timer.cpp
  6. * Content: Class to handle multimedia timers
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 10/05/99 rodtoll Added DPF_MODNAMEs
  12. * 01/14/2000 rodtoll Updated to use DWORD_PTR to allow proper 64-bit operation
  13. *
  14. ***************************************************************************/
  15. #include "dxvutilspch.h"
  16. // ORIGINAL HEADER:
  17. //
  18. // Timer.cpp
  19. //
  20. // This file is from the MSDN, Visual Studuio 6.0 Edition
  21. //
  22. // Article:
  23. // Streaming Wave Files With DirectSound
  24. //
  25. // Author:
  26. // Mark McCulley, Microsoft Corporation
  27. //
  28. #undef DPF_SUBCOMP
  29. #define DPF_SUBCOMP DN_SUBCOMP_VOICE
  30. static bool TimeKillSynchronousFlagAvailable( void );
  31. static MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent );
  32. // constructor
  33. #undef DPF_MODNAME
  34. #define DPF_MODNAME "Timer::Timer"
  35. Timer::Timer (void)
  36. {
  37. DPFX(DPFPREP, DVF_INFOLEVEL, "Timer::Timer\n\r");
  38. m_nIDTimer = NULL;
  39. }
  40. // Destructor
  41. #undef DPF_MODNAME
  42. #define DPF_MODNAME "Timer::~Timer"
  43. Timer::~Timer (void)
  44. {
  45. DPFX(DPFPREP, DVF_INFOLEVEL, "Timer::~Timer\n\r");
  46. if (m_nIDTimer)
  47. {
  48. timeKillEvent (m_nIDTimer);
  49. }
  50. }
  51. // Create
  52. #undef DPF_MODNAME
  53. #define DPF_MODNAME "Timer::Create"
  54. BOOL Timer::Create (UINT nPeriod, UINT nRes, DWORD_PTR dwUser, TIMERCALLBACK pfnCallback)
  55. {
  56. BOOL bRtn = SUCCESS; // assume success
  57. DPFX(DPFPREP, DVF_INFOLEVEL, "Timer::Create\n\r");
  58. DNASSERT (pfnCallback);
  59. DNASSERT (nPeriod > 10);
  60. DNASSERT (nPeriod >= nRes);
  61. m_nPeriod = nPeriod;
  62. m_nRes = nRes;
  63. m_dwUser = dwUser;
  64. m_pfnCallback = pfnCallback;
  65. if ((m_nIDTimer = CompatibleTimeSetEvent (m_nPeriod, m_nRes, TimeProc, (DWORD_PTR) this, TIME_PERIODIC | TIME_KILL_SYNCHRONOUS)) == NULL)
  66. {
  67. bRtn = FAILURE;
  68. }
  69. return (bRtn);
  70. }
  71. /******************************************************************************
  72. CompatibleTimeSetEvent
  73. CompatibleTimeSetEvent() unsets the TIME_KILL_SYNCHRONOUS flag before calling
  74. timeSetEvent() if the current operating system does not support the
  75. TIME_KILL_SYNCHRONOUS flag. TIME_KILL_SYNCHRONOUS is supported on Windows XP and
  76. later operating systems.
  77. Parameters:
  78. - The same parameters as timeSetEvent(). See timeSetEvent()'s documentation in
  79. the Platform SDK for more information.
  80. Return Value:
  81. - The same return value as timeSetEvent(). See timeSetEvent()'s documentation in
  82. the Platform SDK for more information.
  83. ******************************************************************************/
  84. MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent )
  85. {
  86. static bool fCheckedVersion = false;
  87. static bool fTimeKillSynchronousFlagAvailable = false;
  88. if( !fCheckedVersion ) {
  89. fTimeKillSynchronousFlagAvailable = TimeKillSynchronousFlagAvailable();
  90. fCheckedVersion = true;
  91. }
  92. if( !fTimeKillSynchronousFlagAvailable ) {
  93. fuEvent = fuEvent & (TIME_ONESHOT |
  94. TIME_PERIODIC |
  95. TIME_CALLBACK_FUNCTION |
  96. TIME_CALLBACK_EVENT_SET |
  97. TIME_CALLBACK_EVENT_PULSE);
  98. }
  99. return timeSetEvent( uDelay, uResolution, lpTimeProc, dwUser, fuEvent );
  100. }
  101. bool TimeKillSynchronousFlagAvailable( void )
  102. {
  103. OSVERSIONINFO osverinfo;
  104. osverinfo.dwOSVersionInfoSize = sizeof(osverinfo);
  105. if( GetVersionEx( &osverinfo ) ) {
  106. // Windows XP's major version is 5 and its' minor version is 1.
  107. // timeSetEvent() started supporting the TIME_KILL_SYNCHRONOUS flag
  108. // in Windows XP.
  109. if( (osverinfo.dwMajorVersion > 5) ||
  110. ( (osverinfo.dwMajorVersion == 5) && (osverinfo.dwMinorVersion >= 1) ) ) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. // Timer proc for multimedia timer callback set with timeSetTime().
  117. //
  118. // Calls procedure specified when Timer object was created. The
  119. // dwUser parameter contains "this" pointer for associated Timer object.
  120. //
  121. #undef DPF_MODNAME
  122. #define DPF_MODNAME "Timer::TimeProc"
  123. void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
  124. {
  125. // dwUser contains ptr to Timer object
  126. Timer * ptimer = (Timer *) dwUser;
  127. if( ptimer != NULL )
  128. {
  129. // Call user-specified callback and pass back user specified data
  130. (ptimer->m_pfnCallback) (ptimer->m_dwUser);
  131. }
  132. }