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.

123 lines
2.4 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. if (m_hIdleThread)
  28. OutputDebugString("!!CWinIdle Warning!! Idle thread not shut down!\n");
  29. }
  30. DWORD CWinIdle::RunIdle()
  31. {
  32. // Set up an event list
  33. HANDLE aEvents[2];
  34. aEvents[0] = m_hStopEvent;
  35. aEvents[1] = m_hIdleEvent;
  36. // Wait for a stop or idle event
  37. while (WaitForMultipleObjects(2, aEvents, FALSE, INFINITE) != WAIT_OBJECT_0)
  38. {
  39. // Send an idle message
  40. PostMessage(m_hWnd, m_uMsg, m_wParam, m_lParam);
  41. // Wait for a bit...
  42. Sleep(m_dwDelay);
  43. }
  44. return 0;
  45. }
  46. BOOL CWinIdle::StartIdle(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam, DWORD dwDelay)
  47. {
  48. // Make sure it's not already running
  49. if (m_hIdleThread)
  50. return FALSE;
  51. // Make sure they send in a valid handle..
  52. if (!hWnd)
  53. return FALSE;
  54. // Create the events
  55. m_hIdleEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  56. m_hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  57. // Make sure the events got created
  58. if ((!m_hIdleEvent) || (!m_hStopEvent))
  59. return FALSE;
  60. // Create the thread
  61. DWORD dwThreadID;
  62. m_hIdleThread = CreateThread(NULL, 0, CWinIdle::ThreadStub, (void *)this, 0, &dwThreadID);
  63. if (m_hIdleThread)
  64. {
  65. SetThreadPriority(m_hIdleThread, THREAD_PRIORITY_IDLE);
  66. m_hWnd = hWnd;
  67. m_uMsg = uMessage;
  68. m_wParam = wParam;
  69. m_lParam = lParam;
  70. m_dwDelay = dwDelay;
  71. }
  72. return m_hIdleThread != 0;
  73. }
  74. BOOL CWinIdle::EndIdle()
  75. {
  76. // Make sure it's running
  77. if (!m_hIdleThread)
  78. return FALSE;
  79. // Stop the idle thread
  80. SetEvent(m_hStopEvent);
  81. WaitForSingleObject(m_hIdleThread, INFINITE);
  82. CloseHandle(m_hIdleThread);
  83. // Get rid of the event objects
  84. CloseHandle(m_hIdleEvent);
  85. CloseHandle(m_hStopEvent);
  86. // Set everything back to 0
  87. m_hIdleEvent = 0;
  88. m_hStopEvent = 0;
  89. m_hIdleThread = 0;
  90. return TRUE;
  91. }
  92. void CWinIdle::NextIdle()
  93. {
  94. // Make sure the thread's running
  95. if (!m_hIdleThread)
  96. return;
  97. // Signal an idle message
  98. SetEvent(m_hIdleEvent);
  99. }