mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Add support for USB Webcams on Windows (#1390)
This commit is contained in:
committed by
Peter Johnson
parent
70a66fc943
commit
69cb53b51b
66
cscore/src/main/native/windows/WindowsMessagePump.h
Normal file
66
cscore/src/main/native/windows/WindowsMessagePump.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
namespace cs {
|
||||
class WindowsMessagePump {
|
||||
public:
|
||||
WindowsMessagePump(
|
||||
std::function<LRESULT(HWND, UINT, WPARAM, LPARAM)> callback);
|
||||
~WindowsMessagePump();
|
||||
|
||||
friend LRESULT CALLBACK pWndProc(HWND hwnd, UINT uiMsg, WPARAM wParam,
|
||||
LPARAM lParam);
|
||||
|
||||
template <typename RetVal = LRESULT, typename FirstParam = WPARAM,
|
||||
typename SecondParam = LPARAM>
|
||||
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 <typename FirstParam = WPARAM, typename SecondParam = LPARAM>
|
||||
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<LRESULT(HWND, UINT, WPARAM, LPARAM)> m_callback;
|
||||
|
||||
std::thread m_mainThread;
|
||||
};
|
||||
} // namespace cs
|
||||
Reference in New Issue
Block a user