mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-08-02 06:40:42 -07:00
71ba9b6b47
## Link to GitHub Issue or related Pull Request, if one exists Fixes #681 ## Description of change `Sleep` and `sleep_for()` can be very inaccurate and varies depending on what the OS gives us... ### `timeBeginPeriod(1)` On boot, we are now calling `timeBeginPeriod(1)`, which affects the whole process but makes `Sleep` more accurate. There is some risk here if any game was relying on doing things like `Sleep(1)` and expecting it to run for 15.6ms. Most games already call `timeBeginPeriod(1)` in the game engine, though not the whole time, so I'm hoping that this is not too impactful. ### Opt out of Win11 power throttling Ensure that timer resolution change above is respected even when the window is occluded / minimized by opting out of throttling via `PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION`. ### Use Win10 high resolution timer instead of Sleep On Win10 1803 and above, there is a new OS-level API for high resolution timers; if this is available, use it (`CREATE_WAITABLE_TIMER_HIGH_RESOLUTION`). Worth noting that WINE doesn't support this currently. If not, fall back to `Sleep`, which is significantly better than `sleep_for()` in my experiments. Callers of Sleep / sleep_for were replaced with this new timer. Most of them anyway; calls to Sleep() with more than 100ms+ was left alone. ### Add an option as a chicken bit To opt out I'm adding a new option called `Use Legacy Timers` which will revert to behavior before this PR. The code paths that switched from `sleep_for` to `Sleep` will remain in place though, not affected by the option. ## Expected changes In some I/O emulation modules, poll threads may run more frequently, resulting in lower latency. It also means that spice overall may use more CPU resources and power. If you don't like this, you can always enable the option to opt out; e.g., if you're on old arcade cab PC. ## Testing DDR p4io - ok drs touch hook - ok IIDX camera hook - ok CCJ trackball - ok
141 lines
3.8 KiB
C++
141 lines
3.8 KiB
C++
#include <thread>
|
|
#include <chrono>
|
|
|
|
#include "touchpanel.h"
|
|
|
|
#include "util/logging.h"
|
|
#include "util/precise_timer.h"
|
|
#include "touch/touch.h"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
void games::onpara::TouchPanelHandle::set_state(touch_panel_state state) {
|
|
state_ = state;
|
|
// reset the output queue as well for good measure
|
|
output_queue_ = {};
|
|
}
|
|
|
|
void games::onpara::TouchPanelHandle::enqueue_packet(touch_panel_message const &message) {
|
|
// push magic
|
|
output_queue_.push('U');
|
|
|
|
// push message
|
|
for (auto b : message.raw) {
|
|
output_queue_.push(b);
|
|
}
|
|
|
|
// push checksum
|
|
output_queue_.push(message.raw[0] +
|
|
message.raw[1] +
|
|
message.raw[2] +
|
|
message.raw[3] +
|
|
message.raw[4] +
|
|
message.raw[5] +
|
|
message.raw[6] +
|
|
message.raw[7] - 1);
|
|
}
|
|
|
|
bool games::onpara::TouchPanelHandle::open(LPCWSTR lpFileName) {
|
|
if (wcscmp(lpFileName, L"COM1: baud=9600 parity=N data=8 stop=1") != 0) {
|
|
return false;
|
|
}
|
|
log_info("touchpanel", "Opened COM1 (Touch Panel)");
|
|
|
|
return true;
|
|
}
|
|
|
|
int games::onpara::TouchPanelHandle::read(LPVOID lpBuffer, DWORD nNumberOfBytesToRead) {
|
|
|
|
static thread_local timeutils::PreciseSleepTimer timer;
|
|
|
|
DWORD i;
|
|
auto buffer = reinterpret_cast<uint8_t *>(lpBuffer);
|
|
|
|
if (state_ == TOUCH_PANEL_STATE_REPORT && output_queue_.empty()) {
|
|
// create a new status report message
|
|
touch_panel_message report = {
|
|
'T', 'K', 0, 0, 0
|
|
};
|
|
|
|
std::vector<TouchPoint> touch_points;
|
|
touch_get_points(touch_points);
|
|
|
|
if (!touch_points.empty()) {
|
|
auto &touch_point = touch_points[0];
|
|
|
|
report.x = static_cast<uint16_t>(touch_point.x);
|
|
report.y = 768 - static_cast<uint16_t>(touch_point.y);
|
|
report.z = ~0;
|
|
}
|
|
|
|
enqueue_packet(report);
|
|
|
|
// prevent cpu bullying
|
|
timer.sleep(1);
|
|
}
|
|
|
|
// copy from output queue
|
|
for (i = 0; i < nNumberOfBytesToRead && !output_queue_.empty(); i++) {
|
|
buffer[i] = output_queue_.front();
|
|
output_queue_.pop();
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
int games::onpara::TouchPanelHandle::write(LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite) {
|
|
// static messages
|
|
static const touch_panel_message acknowledge_message = {
|
|
'A', 0, 0, 0, 0
|
|
};
|
|
static const touch_panel_message diagnostics_message = {
|
|
'D', 0, 0, 0, 0
|
|
};
|
|
|
|
// make sure that we received the correct amount of data
|
|
if (nNumberOfBytesToWrite != 10) {
|
|
log_warning("touchscreen", "invalid packet size");
|
|
return 0;
|
|
}
|
|
|
|
auto op = reinterpret_cast<const char *>(lpBuffer)[1];
|
|
switch (op) {
|
|
case 'R': // reset
|
|
set_state(TOUCH_PANEL_STATE_INACTIVE);
|
|
break;
|
|
|
|
case 'a': // acknowledge
|
|
if (state_ == TOUCH_PANEL_STATE_INACTIVE) {
|
|
set_state(TOUCH_PANEL_STATE_ACKNOWLEDGE);
|
|
enqueue_packet(acknowledge_message);
|
|
}
|
|
else if (state_ == TOUCH_PANEL_STATE_DIAGNOSTICS) {
|
|
// start reporting
|
|
set_state(TOUCH_PANEL_STATE_REPORT);
|
|
}
|
|
break;
|
|
|
|
case 'D': // diagnositcs
|
|
set_state(TOUCH_PANEL_STATE_DIAGNOSTICS);
|
|
enqueue_packet(diagnostics_message);
|
|
break;
|
|
|
|
default:
|
|
log_warning("touchpanel", "invalid operation: {:c}", op);
|
|
break;
|
|
}
|
|
|
|
return nNumberOfBytesToWrite;
|
|
}
|
|
|
|
int games::onpara::TouchPanelHandle::device_io(DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize,
|
|
LPVOID lpOutBuffer, DWORD nOutBufferSize) {
|
|
return -1;
|
|
}
|
|
|
|
bool games::onpara::TouchPanelHandle::close() {
|
|
log_info("touchpanel", "Closed COM1 (Touch Panel)");
|
|
|
|
return true;
|
|
}
|