Team Fortress 2 Source Code as on 22/4/2020
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.

122 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // Class for sending idle messages to a window
  9. #include "stdafx.h"
  10. #include "win_idle.h"
  11. // Stub function to get into the object's main thread loop
  12. DWORD WINAPI CWinIdle::ThreadStub(LPVOID pIdle)
  13. {
  14. return ((CWinIdle *)pIdle)->RunIdle();
  15. }
  16. CWinIdle::CWinIdle() :
  17. m_hIdleThread(NULL),
  18. m_hIdleEvent(NULL),
  19. m_hStopEvent(NULL),
  20. m_hWnd(0),
  21. m_uMsg(0),
  22. m_dwDelay(0)
  23. {
  24. }
  25. CWinIdle::~CWinIdle()
  26. {
  27. EndIdle();
  28. }
  29. DWORD CWinIdle::RunIdle()
  30. {
  31. // Set up an event list
  32. HANDLE aEvents[2];
  33. aEvents[0] = m_hStopEvent;
  34. aEvents[1] = m_hIdleEvent;
  35. // Wait for a stop or idle event
  36. while (WaitForMultipleObjects(2, aEvents, FALSE, INFINITE) != WAIT_OBJECT_0)
  37. {
  38. // Send an idle message
  39. PostMessage(m_hWnd, m_uMsg, m_wParam, m_lParam);
  40. // Wait for a bit...
  41. Sleep(m_dwDelay);
  42. }
  43. return 0;
  44. }
  45. BOOL CWinIdle::StartIdle(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam, DWORD dwDelay)
  46. {
  47. // Make sure it's not already running
  48. if (m_hIdleThread)
  49. return FALSE;
  50. // Make sure they send in a valid handle..
  51. if (!hWnd)
  52. return FALSE;
  53. // Create the events
  54. m_hIdleEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  55. m_hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  56. // Make sure the events got created
  57. if ((!m_hIdleEvent) || (!m_hStopEvent))
  58. return FALSE;
  59. // Create the thread
  60. DWORD dwThreadID;
  61. m_hIdleThread = CreateThread(NULL, 0, CWinIdle::ThreadStub, (void *)this, 0, &dwThreadID);
  62. if (m_hIdleThread)
  63. {
  64. SetThreadPriority(m_hIdleThread, THREAD_PRIORITY_IDLE);
  65. m_hWnd = hWnd;
  66. m_uMsg = uMessage;
  67. m_wParam = wParam;
  68. m_lParam = lParam;
  69. m_dwDelay = dwDelay;
  70. }
  71. return m_hIdleThread != 0;
  72. }
  73. BOOL CWinIdle::EndIdle()
  74. {
  75. // Make sure it's running
  76. if (!m_hIdleThread)
  77. return FALSE;
  78. // Stop the idle thread
  79. SetEvent(m_hStopEvent);
  80. WaitForSingleObject(m_hIdleThread, INFINITE);
  81. CloseHandle(m_hIdleThread);
  82. // Get rid of the event objects
  83. CloseHandle(m_hIdleEvent);
  84. CloseHandle(m_hStopEvent);
  85. // Set everything back to 0
  86. m_hIdleEvent = 0;
  87. m_hStopEvent = 0;
  88. m_hIdleThread = 0;
  89. return TRUE;
  90. }
  91. void CWinIdle::NextIdle()
  92. {
  93. // Make sure the thread's running
  94. if (!m_hIdleThread)
  95. return;
  96. // Signal an idle message
  97. SetEvent(m_hIdleEvent);
  98. }