Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uDraw GameTablet device emulation #15457

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rpcs3/Emu/Cell/lv2/sys_usbd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Emu/Io/ghltar_config.h"
#include "Emu/Io/Buzz.h"
#include "Emu/Io/buzz_config.h"
#include "Emu/Io/GameTablet.h"
#include "Emu/Io/Turntable.h"
#include "Emu/Io/turntable_config.h"
#include "Emu/Io/RB3MidiKeyboard.h"
Expand Down Expand Up @@ -478,6 +479,12 @@ usb_handler_thread::usb_handler_thread()
sys_usbd.notice("Adding emulated Buzz! buzzer (5-7 players)");
usb_devices.push_back(std::make_shared<usb_device_buzz>(4, 6, get_new_location()));
}

if (g_cfg.io.gametablet == gametablet_handler::enabled)
{
sys_usbd.notice("Adding emulated uDraw GameTablet");
usb_devices.push_back(std::make_shared<usb_device_gametablet>(get_new_location()));
}
}

usb_handler_thread::~usb_handler_thread()
Expand Down
274 changes: 274 additions & 0 deletions rpcs3/Emu/Io/GameTablet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
#include "stdafx.h"
#include "GameTablet.h"
#include "MouseHandler.h"
#include "Emu/IdManager.h"
#include "Emu/Cell/lv2/sys_usbd.h"
#include "Emu/system_config.h"
#include "Input/pad_thread.h"

LOG_CHANNEL(gametablet_log);

usb_device_gametablet::usb_device_gametablet(const std::array<u8, 7>& location)
: usb_device_emulated(location)
{
device = UsbDescriptorNode(USB_DESCRIPTOR_DEVICE,
UsbDeviceDescriptor {
.bcdUSB = 0x0200,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 0x08,
.idVendor = 0x20d6,
.idProduct = 0xcb17,
.bcdDevice = 0x0108,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x00,
.bNumConfigurations = 0x01});
auto& config0 = device.add_node(UsbDescriptorNode(USB_DESCRIPTOR_CONFIG,
UsbDeviceConfiguration {
.wTotalLength = 0x0029,
.bNumInterfaces = 0x01,
.bConfigurationValue = 0x01,
.iConfiguration = 0x00,
.bmAttributes = 0x80,
.bMaxPower = 0x32}));
config0.add_node(UsbDescriptorNode(USB_DESCRIPTOR_INTERFACE,
UsbDeviceInterface {
.bInterfaceNumber = 0x00,
.bAlternateSetting = 0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = 0x03,
.bInterfaceSubClass = 0x00,
.bInterfaceProtocol = 0x00,
.iInterface = 0x00}));
config0.add_node(UsbDescriptorNode(USB_DESCRIPTOR_HID,
UsbDeviceHID {
.bcdHID = 0x0110,
.bCountryCode = 0x00,
.bNumDescriptors = 0x01,
.bDescriptorType = 0x22,
.wDescriptorLength = 0x0089}));
config0.add_node(UsbDescriptorNode(USB_DESCRIPTOR_ENDPOINT,
UsbDeviceEndpoint {
.bEndpointAddress = 0x83,
.bmAttributes = 0x03,
.wMaxPacketSize = 0x0040,
.bInterval = 0x0A}));
config0.add_node(UsbDescriptorNode(USB_DESCRIPTOR_ENDPOINT,
UsbDeviceEndpoint {
.bEndpointAddress = 0x04,
.bmAttributes = 0x03,
.wMaxPacketSize = 0x0040,
.bInterval = 0x0A}));

add_string("THQ Inc");
add_string("THQ uDraw Game Tablet for PS3");
}

usb_device_gametablet::~usb_device_gametablet()
{
}

void usb_device_gametablet::control_transfer(u8 bmRequestType, u8 bRequest, u16 wValue, u16 wIndex, u16 wLength, u32 buf_size, u8* buf, UsbTransfer* transfer)
{
transfer->fake = true;
transfer->expected_count = buf_size;
transfer->expected_result = HC_CC_NOERR;
transfer->expected_time = get_timestamp() + 100;

// Control transfers are nearly instant
switch (bmRequestType)
{
case 0U /*silences warning*/ | LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE: // 0x21
switch (bRequest)
{
case 0x09: // SET_REPORT
ensure(buf_size > 2);
gametablet_log.trace("Leds: %s/%s/%s/%s",
Florin9doi marked this conversation as resolved.
Show resolved Hide resolved
buf[2] & 1 ? "ON" : "OFF",
buf[2] & 2 ? "ON" : "OFF",
buf[2] & 4 ? "ON" : "OFF",
buf[2] & 8 ? "ON" : "OFF");
break;
default:
gametablet_log.error("Unhandled Request: 0x%02X/0x%02X", bmRequestType, bRequest);
break;
}
break;
default:
usb_device_emulated::control_transfer(bmRequestType, bRequest, wValue, wIndex, wLength, buf_size, buf, transfer);
break;
}
}

extern bool is_input_allowed();

void usb_device_gametablet::interrupt_transfer(u32 buf_size, u8* buf, u32 /*endpoint*/, UsbTransfer* transfer)
{
ensure(buf_size > 0x1a);

transfer->fake = true;
Florin9doi marked this conversation as resolved.
Show resolved Hide resolved
transfer->expected_count = 27;
transfer->expected_result = HC_CC_NOERR;
// Interrupt transfers are slow (6ms, TODO accurate measurement)
transfer->expected_time = get_timestamp() + 6000;

std::memset(buf, 0, buf_size);

buf[0x02] = 0x0f; // dpad

buf[0x03] = 0x80;
buf[0x04] = 0x80;
buf[0x05] = 0x80;
buf[0x06] = 0x80;

buf[0x0d] = 0x72; // pressure
buf[0x0f] = 0x0f; // pos X
buf[0x10] = 0x0f; // pos Y
buf[0x11] = 0xff;
buf[0x12] = 0xff;

buf[0x13] = 0x01; // accel X
buf[0x14] = 0x02;
buf[0x15] = 0x00; // accel Y
buf[0x16] = 0x02;
buf[0x17] = 0xea; // accel Z
buf[0x18] = 0x01;

buf[0x1a] = 0x02;

if (!is_input_allowed())
{
return;
}

if (g_cfg.io.mouse != mouse_handler::basic)
Florin9doi marked this conversation as resolved.
Show resolved Hide resolved
{
gametablet_log.warning("GameTablet requires mouse_handler configured to basic");
return;
}

std::lock_guard lock(pad::g_pad_mutex);
Florin9doi marked this conversation as resolved.
Show resolved Hide resolved
const auto gamepad_handler = pad::get_current_handler();
const auto& pads = gamepad_handler->GetPads();

bool up = false, right = false, down = false, left = false;

const int pad_index = 1; // Player2
const auto& pad = pads[pad_index];
Florin9doi marked this conversation as resolved.
Show resolved Hide resolved
if (pad->m_port_status & CELL_PAD_STATUS_CONNECTED)
{
for (Button& button : pad->m_buttons)
{
if (!button.m_pressed)
{
continue;
}

if (button.m_offset == CELL_PAD_BTN_OFFSET_DIGITAL1)
{
switch (button.m_outKeyCode)
{
case CELL_PAD_CTRL_SELECT:
buf[1] |= (1 << 0);
break;
case CELL_PAD_CTRL_START:
buf[1] |= (1 << 1);
break;
case CELL_PAD_CTRL_UP:
up = true;
break;
case CELL_PAD_CTRL_RIGHT:
right = true;
break;
case CELL_PAD_CTRL_DOWN:
down = true;
break;
case CELL_PAD_CTRL_LEFT:
left = true;
break;
default:
break;
}
}
else if (button.m_offset == CELL_PAD_BTN_OFFSET_DIGITAL2)
{
switch (button.m_outKeyCode)
{
case CELL_PAD_CTRL_SQUARE:
buf[0] |= (1 << 0);
break;
case CELL_PAD_CTRL_CROSS:
buf[0] |= (1 << 1);
break;
case CELL_PAD_CTRL_CIRCLE:
buf[0] |= (1 << 2);
break;
case CELL_PAD_CTRL_TRIANGLE:
buf[0] |= (1 << 3);
break;
case CELL_PAD_CTRL_PS:
buf[1] |= (1 << 4);
break;
default:
break;
}
}
}
}

if (!up && !right && !down && !left)
buf[0x02] = 0x0f;
else if (up && !left && !right)
buf[0x02] = 0x00;
else if (up && right)
buf[0x02] = 0x01;
else if (right && !up && !down)
buf[0x02] = 0x02;
else if (down && right)
buf[0x02] = 0x03;
else if (down && !left && !right)
buf[0x02] = 0x04;
else if (down && left)
buf[0x02] = 0x05;
else if (left && !up && !down)
buf[0x02] = 0x06;
else if (up && left)
buf[0x02] = 0x07;


auto& mouse_handler = g_fxo->get<MouseHandlerBase>();
std::lock_guard mouse_lock(mouse_handler.mutex);

mouse_handler.Init(1);

const uint8_t mouse_index = 0;
if (mouse_index >= mouse_handler.GetMice().size())
{
return;
}

const Mouse& mouse_data = ::at32(mouse_handler.GetMice(), mouse_index);
if (mouse_data.x_max <= 0 || mouse_data.y_max <= 0)
{
return;
}

static uint8_t noise_x = 0; // Toggle the LSB to simulate a noisy signal, Instant Artist dislikes a pen held perfectly still
static uint8_t noise_y = 0;
const int tablet_max_x = 1920;
const int tablet_max_y = 1080;

const long tablet_x_pos = (mouse_data.x_pos * tablet_max_x / mouse_data.x_max) ^ noise_x;
const long tablet_y_pos = (mouse_data.y_pos * tablet_max_y / mouse_data.y_max) ^ noise_y;
noise_x = !noise_x;
noise_y = !noise_y;

buf[0x0b] = 0x40; // pen
buf[0x0d] = mouse_data.buttons & CELL_MOUSE_BUTTON_1 ? 0xbb : 0x72; // pressure
buf[0x0f] = tablet_x_pos / 0x100;
buf[0x10] = tablet_y_pos / 0x100;
buf[0x11] = tablet_x_pos % 0x100;
buf[0x12] = tablet_y_pos % 0x100;
}
13 changes: 13 additions & 0 deletions rpcs3/Emu/Io/GameTablet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "Emu/Io/usb_device.h"

class usb_device_gametablet : public usb_device_emulated
{
public:
usb_device_gametablet(const std::array<u8, 7>& location);
~usb_device_gametablet();

void control_transfer(u8 bmRequestType, u8 bRequest, u16 wValue, u16 wIndex, u16 wLength, u32 buf_size, u8* buf, UsbTransfer* transfer) override;
void interrupt_transfer(u32 buf_size, u8* buf, u32 endpoint, UsbTransfer* transfer) override;
};
1 change: 1 addition & 0 deletions rpcs3/Emu/system_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ struct cfg_root : cfg::node
cfg::_enum<buzz_handler> buzz{ this, "Buzz emulated controller", buzz_handler::null };
cfg::_enum<turntable_handler> turntable{this, "Turntable emulated controller", turntable_handler::null};
cfg::_enum<ghltar_handler> ghltar{this, "GHLtar emulated controller", ghltar_handler::null};
cfg::_enum<gametablet_handler> gametablet{this, "GameTablet emulated controller", gametablet_handler::disabled};
cfg::_enum<pad_handler_mode> pad_mode{this, "Pad handler mode", pad_handler_mode::single_threaded, true};
cfg::_bool keep_pads_connected{this, "Keep pads connected", false, true};
cfg::uint<0, 100'000> pad_sleep{this, "Pad handler sleep (microseconds)", 1'000, true};
Expand Down
15 changes: 15 additions & 0 deletions rpcs3/Emu/system_config_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,21 @@ void fmt_class_string<ghltar_handler>::format(std::string& out, u64 arg)
});
}

template <>
void fmt_class_string<gametablet_handler>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](auto value)
{
switch (value)
{
case gametablet_handler::disabled: return "Disabled";
case gametablet_handler::enabled: return "Enabled";
}

return unknown;
});
}

template <>
void fmt_class_string<ppu_decoder_type>::format(std::string& out, u64 arg)
{
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/Emu/system_config_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ enum class ghltar_handler
two_controllers,
};

enum class gametablet_handler
{
disabled,
enabled,
};

enum class microphone_handler
{
null,
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/emucore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<ClCompile Include="Emu\Io\RB3MidiKeyboard.cpp" />
<ClCompile Include="Emu\Io\recording_config.cpp" />
<ClCompile Include="Emu\Io\Turntable.cpp" />
<ClCompile Include="Emu\Io\GameTablet.cpp" />
<ClCompile Include="Emu\Io\GHLtar.cpp" />
<ClCompile Include="Emu\Io\Buzz.cpp" />
<ClCompile Include="Emu\Io\usio.cpp" />
Expand Down Expand Up @@ -544,6 +545,7 @@
<ClInclude Include="Emu\Io\RB3MidiKeyboard.h" />
<ClInclude Include="Emu\Io\recording_config.h" />
<ClInclude Include="Emu\Io\Turntable.h" />
<ClInclude Include="Emu\Io\GameTablet.h" />
<ClInclude Include="Emu\Io\GHLtar.h" />
<ClInclude Include="Emu\Io\Buzz.h" />
<ClInclude Include="Emu\Io\turntable_config.h" />
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/emucore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,9 @@
<ClCompile Include="Emu\RSX\Overlays\Shaders\shader_loading_dialog.cpp">
<Filter>Emu\GPU\RSX\Overlays\Shaders</Filter>
</ClCompile>
<ClCompile Include="Emu\Io\GameTablet.cpp">
<Filter>Emu\Io</Filter>
</ClCompile>
<ClCompile Include="Emu\Io\GHLtar.cpp">
<Filter>Emu\Io</Filter>
</ClCompile>
Expand Down Expand Up @@ -2055,6 +2058,9 @@
<ClInclude Include="Emu\RSX\Overlays\overlay_compile_notification.h">
<Filter>Emu\GPU\RSX\Overlays</Filter>
</ClInclude>
<ClInclude Include="Emu\Io\GameTablet.h">
<Filter>Emu\Io</Filter>
</ClInclude>
<ClInclude Include="Emu\Io\GHLtar.h">
<Filter>Emu\Io</Filter>
</ClInclude>
Expand Down
Loading
Loading