diff --git a/rpcs3/Emu/Cell/lv2/sys_usbd.cpp b/rpcs3/Emu/Cell/lv2/sys_usbd.cpp index 9f43033e99..29abfa9137 100644 --- a/rpcs3/Emu/Cell/lv2/sys_usbd.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_usbd.cpp @@ -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" @@ -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(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(get_new_location())); + } } usb_handler_thread::~usb_handler_thread() diff --git a/rpcs3/Emu/Io/GameTablet.cpp b/rpcs3/Emu/Io/GameTablet.cpp new file mode 100644 index 0000000000..af9201e3bd --- /dev/null +++ b/rpcs3/Emu/Io/GameTablet.cpp @@ -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& 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", + 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; + 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) + { + gametablet_log.warning("GameTablet requires mouse_handler configured to basic"); + return; + } + + std::lock_guard lock(pad::g_pad_mutex); + 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]; + 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(); + 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; +} diff --git a/rpcs3/Emu/Io/GameTablet.h b/rpcs3/Emu/Io/GameTablet.h new file mode 100644 index 0000000000..832e31853c --- /dev/null +++ b/rpcs3/Emu/Io/GameTablet.h @@ -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& 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; +}; diff --git a/rpcs3/Emu/system_config.h b/rpcs3/Emu/system_config.h index 14dc33c2e7..4c199914fa 100644 --- a/rpcs3/Emu/system_config.h +++ b/rpcs3/Emu/system_config.h @@ -274,6 +274,7 @@ struct cfg_root : cfg::node cfg::_enum buzz{ this, "Buzz emulated controller", buzz_handler::null }; cfg::_enum turntable{this, "Turntable emulated controller", turntable_handler::null}; cfg::_enum ghltar{this, "GHLtar emulated controller", ghltar_handler::null}; + cfg::_enum gametablet{this, "GameTablet emulated controller", gametablet_handler::disabled}; cfg::_enum 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}; diff --git a/rpcs3/Emu/system_config_types.cpp b/rpcs3/Emu/system_config_types.cpp index 6a13b6129d..abeb781b19 100644 --- a/rpcs3/Emu/system_config_types.cpp +++ b/rpcs3/Emu/system_config_types.cpp @@ -507,6 +507,21 @@ void fmt_class_string::format(std::string& out, u64 arg) }); } +template <> +void fmt_class_string::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::format(std::string& out, u64 arg) { diff --git a/rpcs3/Emu/system_config_types.h b/rpcs3/Emu/system_config_types.h index f430bec500..1c89b87ed2 100644 --- a/rpcs3/Emu/system_config_types.h +++ b/rpcs3/Emu/system_config_types.h @@ -167,6 +167,12 @@ enum class ghltar_handler two_controllers, }; +enum class gametablet_handler +{ + disabled, + enabled, +}; + enum class microphone_handler { null, diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index 600b076202..b95746baf0 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -78,6 +78,7 @@ + @@ -544,6 +545,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 322fc2dc02..6ac6b84e8d 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -924,6 +924,9 @@ Emu\GPU\RSX\Overlays\Shaders + + Emu\Io + Emu\Io @@ -2055,6 +2058,9 @@ Emu\GPU\RSX\Overlays + + Emu\Io + Emu\Io diff --git a/rpcs3/rpcs3qt/emu_settings.cpp b/rpcs3/rpcs3qt/emu_settings.cpp index fc5c0a103a..2cf3252215 100644 --- a/rpcs3/rpcs3qt/emu_settings.cpp +++ b/rpcs3/rpcs3qt/emu_settings.cpp @@ -1088,6 +1088,13 @@ QString emu_settings::GetLocalizedSetting(const QString& original, emu_settings_ case ghltar_handler::two_controllers: return tr("2 controllers", "GHLtar handler"); } break; + case emu_settings_type::GameTablet: + switch (static_cast(index)) + { + case gametablet_handler::disabled: return tr("Disabled", "GameTablet handler"); + case gametablet_handler::enabled: return tr("Enabled", "GameTablet handler"); + } + break; case emu_settings_type::InternetStatus: switch (static_cast(index)) { diff --git a/rpcs3/rpcs3qt/emu_settings_type.h b/rpcs3/rpcs3qt/emu_settings_type.h index dd5b49bf90..9f1ac6e29b 100644 --- a/rpcs3/rpcs3qt/emu_settings_type.h +++ b/rpcs3/rpcs3qt/emu_settings_type.h @@ -161,6 +161,7 @@ enum class emu_settings_type Buzz, Turntable, GHLtar, + GameTablet, MidiDevices, SDLMappings, @@ -350,6 +351,7 @@ inline static const QMap settings_location = { emu_settings_type::Buzz, { "Input/Output", "Buzz emulated controller" }}, { emu_settings_type::Turntable, { "Input/Output", "Turntable emulated controller" }}, { emu_settings_type::GHLtar, { "Input/Output", "GHLtar emulated controller" }}, + { emu_settings_type::GameTablet, { "Input/Output", "GameTablet emulated controller" }}, { emu_settings_type::MidiDevices, { "Input/Output", "Emulated Midi devices" }}, { emu_settings_type::SDLMappings, { "Input/Output", "Load SDL GameController Mappings" }}, diff --git a/rpcs3/rpcs3qt/gs_frame.cpp b/rpcs3/rpcs3qt/gs_frame.cpp index 0916c7afd2..784d736bdc 100644 --- a/rpcs3/rpcs3qt/gs_frame.cpp +++ b/rpcs3/rpcs3qt/gs_frame.cpp @@ -1070,7 +1070,7 @@ void gs_frame::take_screenshot(std::vector data, u32 sshot_width, u32 sshot_ void gs_frame::mouseDoubleClickEvent(QMouseEvent* ev) { - if (m_disable_mouse || g_cfg.io.move == move_handler::mouse) return; + if (m_disable_mouse || g_cfg.io.move == move_handler::mouse || g_cfg.io.gametablet == gametablet_handler::enabled) return; #ifdef HAVE_LIBEVDEV if (g_cfg.io.move == move_handler::gun) return; #endif diff --git a/rpcs3/rpcs3qt/settings_dialog.cpp b/rpcs3/rpcs3qt/settings_dialog.cpp index 09e4baf390..6b6a216917 100644 --- a/rpcs3/rpcs3qt/settings_dialog.cpp +++ b/rpcs3/rpcs3qt/settings_dialog.cpp @@ -1217,6 +1217,9 @@ settings_dialog::settings_dialog(std::shared_ptr gui_settings, std m_emu_settings->EnhanceComboBox(ui->ghltarBox, emu_settings_type::GHLtar); SubscribeTooltip(ui->gb_ghltar_emulated, tooltips.settings.ghltar); + m_emu_settings->EnhanceComboBox(ui->gameTabletBox, emu_settings_type::GameTablet); + SubscribeTooltip(ui->gametablet_emulated, tooltips.settings.gametablet); + m_emu_settings->EnhanceCheckBox(ui->backgroundInputBox, emu_settings_type::BackgroundInput); SubscribeTooltip(ui->backgroundInputBox, tooltips.settings.background_input); diff --git a/rpcs3/rpcs3qt/settings_dialog.ui b/rpcs3/rpcs3qt/settings_dialog.ui index a425c80907..11acc18e66 100644 --- a/rpcs3/rpcs3qt/settings_dialog.ui +++ b/rpcs3/rpcs3qt/settings_dialog.ui @@ -1731,6 +1731,18 @@ + + + + GameTablet + + + + + + + + @@ -1750,7 +1762,7 @@ - + Emulated MIDI Device 3 @@ -1769,7 +1781,7 @@ - + Emulated MIDI Device 2 @@ -1788,7 +1800,7 @@ - + Additional Settings diff --git a/rpcs3/rpcs3qt/tooltips.h b/rpcs3/rpcs3qt/tooltips.h index 8857ba91a4..809b4bb32f 100644 --- a/rpcs3/rpcs3qt/tooltips.h +++ b/rpcs3/rpcs3qt/tooltips.h @@ -231,6 +231,7 @@ public: const QString buzz = tr("Buzz! support.\nSelect 1 or 2 controllers if the game requires Buzz! controllers and you don't have real controllers.\nSelect Null if the game has support for DualShock or if you have real Buzz! controllers."); const QString turntable = tr("DJ Hero Turntable controller support.\nSelect 1 or 2 controllers if the game requires DJ Hero Turntable controllers and you don't have real turntable controllers.\nSelect Null if the game has support for DualShock or if you have real turntable controllers.\nA real turntable controller can be used at the same time as an emulated turntable controller."); const QString ghltar = tr("Guitar Hero Live (GHL) Guitar controller support.\nSelect 1 or 2 controllers if the game requires GHL Guitar controllers and you don't have real guitar controllers.\nSelect Null if the game has support for DualShock or if you have real guitar controllers.\nA real guitar controller can be used at the same time as an emulated guitar controller."); + const QString gametablet = tr("GameTablet emulated controller support.\nDisable to use a physical GameTablet device"); const QString background_input = tr("Allows pad and keyboard input while the game window is unfocused."); const QString show_move_cursor = tr("Shows the raw position of the PS Move input.\nThis can be very helpful during calibration screens."); const QString midi_devices = tr("Select up to 3 emulated MIDI devices and their types.");