// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. #pragma once #include #include #include namespace cs { class WindowsMessagePump { public: explicit WindowsMessagePump( std::function callback); ~WindowsMessagePump(); friend LRESULT CALLBACK pWndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam); template RetVal SendWindowMessage(UINT msg, FirstParam wParam, SecondParam lParam) { static_assert(sizeof(FirstParam) <= sizeof(WPARAM), "First Parameter Does Not Fit"); static_assert(sizeof(SecondParam) <= sizeof(LPARAM), "Second Parameter Does Not Fit"); static_assert(sizeof(RetVal) <= sizeof(LRESULT), "Return Value Does Not Fit"); WPARAM firstToSend = 0; LPARAM secondToSend = 0; std::memcpy(&firstToSend, &wParam, sizeof(FirstParam)); std::memcpy(&secondToSend, &lParam, sizeof(SecondParam)); LRESULT result = SendMessage(hwnd, msg, firstToSend, secondToSend); RetVal toReturn; std::memset(&toReturn, 0, sizeof(RetVal)); std::memcpy(&toReturn, &result, sizeof(RetVal)); return toReturn; } template BOOL PostWindowMessage(UINT msg, FirstParam wParam, SecondParam lParam) { static_assert(sizeof(FirstParam) <= sizeof(WPARAM), "First Parameter Does Not Fit"); static_assert(sizeof(SecondParam) <= sizeof(LPARAM), "Second Parameter Does Not Fit"); WPARAM firstToSend = 0; LPARAM secondToSend = 0; std::memcpy(&firstToSend, &wParam, sizeof(FirstParam)); std::memcpy(&secondToSend, &lParam, sizeof(SecondParam)); return PostMessage(hwnd, msg, firstToSend, secondToSend); } private: void ThreadMain(HANDLE eventHandle); HWND hwnd; std::function m_callback; std::thread m_mainThread; }; } // namespace cs