diff options
62 files changed, 634 insertions, 1301 deletions
diff --git a/src/citra/emu_window/emu_window_glfw.cpp b/src/citra/emu_window/emu_window_glfw.cpp index 02f524e03..b911e60c5 100644 --- a/src/citra/emu_window/emu_window_glfw.cpp +++ b/src/citra/emu_window/emu_window_glfw.cpp @@ -8,18 +8,55 @@ #include "citra/emu_window/emu_window_glfw.h" -static void OnKeyEvent(GLFWwindow* win, int key, int action) { - // TODO(bunnei): ImplementMe -} +static const std::pair<int, HID_User::PadState> default_key_map[] = { + { GLFW_KEY_A, HID_User::PAD_A }, + { GLFW_KEY_B, HID_User::PAD_B }, + { GLFW_KEY_BACKSLASH, HID_User::PAD_SELECT }, + { GLFW_KEY_ENTER, HID_User::PAD_START }, + { GLFW_KEY_RIGHT, HID_User::PAD_RIGHT }, + { GLFW_KEY_LEFT, HID_User::PAD_LEFT }, + { GLFW_KEY_UP, HID_User::PAD_UP }, + { GLFW_KEY_DOWN, HID_User::PAD_DOWN }, + { GLFW_KEY_R, HID_User::PAD_R }, + { GLFW_KEY_L, HID_User::PAD_L }, + { GLFW_KEY_X, HID_User::PAD_X }, + { GLFW_KEY_Y, HID_User::PAD_Y }, + { GLFW_KEY_H, HID_User::PAD_CIRCLE_RIGHT }, + { GLFW_KEY_F, HID_User::PAD_CIRCLE_LEFT }, + { GLFW_KEY_T, HID_User::PAD_CIRCLE_UP }, + { GLFW_KEY_G, HID_User::PAD_CIRCLE_DOWN }, +}; + +/// Called by GLFW when a key event occurs +void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) { + + if (!VideoCore::g_emu_window) { + return; + } + + int keyboard_id = ((EmuWindow_GLFW*)VideoCore::g_emu_window)->keyboard_id; + + if (action == GLFW_PRESS) { + EmuWindow::KeyPressed({key, keyboard_id}); + } -static void OnWindowSizeEvent(GLFWwindow* win, int width, int height) { - EmuWindow_GLFW* emu_window = (EmuWindow_GLFW*)glfwGetWindowUserPointer(win); - emu_window->SetClientAreaWidth(width); - emu_window->SetClientAreaHeight(height); + if (action == GLFW_RELEASE) { + EmuWindow::KeyReleased({key, keyboard_id}); + } + HID_User::PadUpdateComplete(); } /// EmuWindow_GLFW constructor EmuWindow_GLFW::EmuWindow_GLFW() { + + // Register a new ID for the default keyboard + keyboard_id = KeyMap::NewDeviceId(); + + // Set default key mappings for keyboard + for (auto mapping : default_key_map) { + KeyMap::SetKeyMapping({mapping.first, keyboard_id}, mapping.second); + } + // Initialize the window if(glfwInit() != GL_TRUE) { printf("Failed to initialize GLFW! Exiting..."); @@ -45,8 +82,7 @@ EmuWindow_GLFW::EmuWindow_GLFW() { // Setup callbacks glfwSetWindowUserPointer(m_render_window, this); - //glfwSetKeyCallback(m_render_window, OnKeyEvent); - //glfwSetWindowSizeCallback(m_render_window, OnWindowSizeEvent); + glfwSetKeyCallback(m_render_window, OnKeyEvent); DoneCurrent(); } diff --git a/src/citra/emu_window/emu_window_glfw.h b/src/citra/emu_window/emu_window_glfw.h index c1b41203b..29325bb75 100644 --- a/src/citra/emu_window/emu_window_glfw.h +++ b/src/citra/emu_window/emu_window_glfw.h @@ -25,8 +25,9 @@ public: /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread void DoneCurrent(); - GLFWwindow* m_render_window; ///< Internal GLFW render window + static void OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods); private: - + GLFWwindow* m_render_window; ///< Internal GLFW render window + int keyboard_id; ///< Device id of keyboard for use with KeyMap }; diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp index 9ccb935ab..cf4d8b32b 100644 --- a/src/citra_qt/bootmanager.cpp +++ b/src/citra_qt/bootmanager.cpp @@ -108,8 +108,35 @@ EmuThread& GRenderWindow::GetEmuThread() return emu_thread; } +static const std::pair<int, HID_User::PadState> default_key_map[] = { + { Qt::Key_A, HID_User::PAD_A }, + { Qt::Key_B, HID_User::PAD_B }, + { Qt::Key_Backslash, HID_User::PAD_SELECT }, + { Qt::Key_Enter, HID_User::PAD_START }, + { Qt::Key_Right, HID_User::PAD_RIGHT }, + { Qt::Key_Left, HID_User::PAD_LEFT }, + { Qt::Key_Up, HID_User::PAD_UP }, + { Qt::Key_Down, HID_User::PAD_DOWN }, + { Qt::Key_R, HID_User::PAD_R }, + { Qt::Key_L, HID_User::PAD_L }, + { Qt::Key_X, HID_User::PAD_X }, + { Qt::Key_Y, HID_User::PAD_Y }, + { Qt::Key_H, HID_User::PAD_CIRCLE_RIGHT }, + { Qt::Key_F, HID_User::PAD_CIRCLE_LEFT }, + { Qt::Key_T, HID_User::PAD_CIRCLE_UP }, + { Qt::Key_G, HID_User::PAD_CIRCLE_DOWN }, +}; + GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this) { + // Register a new ID for the default keyboard + keyboard_id = KeyMap::NewDeviceId(); + + // Set default key mappings for keyboard + for (auto mapping : default_key_map) { + KeyMap::SetKeyMapping({mapping.first, keyboard_id}, mapping.second); + } + // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose QGLFormat fmt; fmt.setProfile(QGLFormat::CoreProfile); @@ -208,27 +235,13 @@ QByteArray GRenderWindow::saveGeometry() void GRenderWindow::keyPressEvent(QKeyEvent* event) { - /* - bool key_processed = false; - for (unsigned int channel = 0; channel < 4 && controller_interface(); ++channel) - if (controller_interface()->SetControllerStatus(channel, event->key(), input_common::GCController::PRESSED)) - key_processed = true; - - if (!key_processed) - QWidget::keyPressEvent(event); - */ + EmuWindow::KeyPressed({event->key(), keyboard_id}); + HID_User::PadUpdateComplete(); } void GRenderWindow::keyReleaseEvent(QKeyEvent* event) { - /* - bool key_processed = false; - for (unsigned int channel = 0; channel < 4 && controller_interface(); ++channel) - if (controller_interface()->SetControllerStatus(channel, event->key(), input_common::GCController::RELEASED)) - key_processed = true; - - if (!key_processed) - QWidget::keyPressEvent(event); - */ + EmuWindow::KeyReleased({event->key(), keyboard_id}); + HID_User::PadUpdateComplete(); } diff --git a/src/citra_qt/bootmanager.hxx b/src/citra_qt/bootmanager.hxx index 51cb781e9..eedf19471 100644 --- a/src/citra_qt/bootmanager.hxx +++ b/src/citra_qt/bootmanager.hxx @@ -116,4 +116,6 @@ private: EmuThread emu_thread; QByteArray geometry; + + int keyboard_id; }; diff --git a/src/citra_qt/debugger/callstack.cpp b/src/citra_qt/debugger/callstack.cpp index f59f2d8c8..77fb0c9ed 100644 --- a/src/citra_qt/debugger/callstack.cpp +++ b/src/citra_qt/debugger/callstack.cpp @@ -37,7 +37,7 @@ void CallstackWidget::OnCPUStepped() /* TODO (mattvail) clean me, move to debugger interface */ u32 insn = Memory::Read32(call_addr); - if (disasm->decode(insn) == OP_BL) + if (disasm->Decode(insn) == OP_BL) { std::string name; // ripped from disasm diff --git a/src/citra_qt/debugger/disassembler.cpp b/src/citra_qt/debugger/disassembler.cpp index 507a35718..856baf63d 100644 --- a/src/citra_qt/debugger/disassembler.cpp +++ b/src/citra_qt/debugger/disassembler.cpp @@ -36,22 +36,20 @@ QVariant DisassemblerModel::data(const QModelIndex& index, int role) const { switch (role) { case Qt::DisplayRole: { - static char result[255]; - u32 address = base_address + index.row() * 4; u32 instr = Memory::Read32(address); - ARM_Disasm::disasm(address, instr, result); + std::string disassembly = ARM_Disasm::Disassemble(address, instr); if (index.column() == 0) { return QString("0x%1").arg((uint)(address), 8, 16, QLatin1Char('0')); } else if (index.column() == 1) { - return QString::fromLatin1(result); + return QString::fromStdString(disassembly); } else if (index.column() == 2) { if(Symbols::HasSymbol(address)) { TSymbol symbol = Symbols::GetSymbol(address); return QString("%1 - Size:%2").arg(QString::fromStdString(symbol.name)) .arg(symbol.size / 4); // divide by 4 to get instruction count - } else if (ARM_Disasm::decode(instr) == OP_BL) { + } else if (ARM_Disasm::Decode(instr) == OP_BL) { u32 offset = instr & 0xFFFFFF; // Sign-extend the 24-bit offset diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 55a5f9eba..9d5a90762 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -4,10 +4,12 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scm_rev.cpp.in" "${CMAKE_CURRENT_SOU set(SRCS break_points.cpp console_listener.cpp + emu_window.cpp extended_trace.cpp file_search.cpp file_util.cpp hash.cpp + key_map.cpp log_manager.cpp math_util.cpp mem_arena.cpp @@ -38,8 +40,8 @@ set(HEADERS fifo_queue.h file_search.h file_util.h - fixed_size_queue.h hash.h + key_map.h linear_disk_cache.h log.h log_manager.h @@ -49,9 +51,6 @@ set(HEADERS msg_handler.h platform.h scm_rev.h - std_condition_variable.h - std_mutex.h - std_thread.h string_util.h swap.h symbols.h diff --git a/src/common/common.h b/src/common/common.h index cb69eabe4..9f3016d34 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -20,11 +20,6 @@ #define STACKALIGN -#if __cplusplus >= 201103L || defined(_MSC_VER) || defined(__GXX_EXPERIMENTAL_CXX0X__) -#define HAVE_CXX11_SYNTAX 1 -#endif - -#if HAVE_CXX11_SYNTAX // An inheritable class to disallow the copy constructor and operator= functions class NonCopyable { @@ -36,7 +31,6 @@ private: NonCopyable(NonCopyable&); NonCopyable& operator=(NonCopyable& other); }; -#endif #include "common/log.h" #include "common/common_types.h" diff --git a/src/common/common_types.h b/src/common/common_types.h index 9d41e5971..00fde828d 100644 --- a/src/common/common_types.h +++ b/src/common/common_types.h @@ -100,7 +100,7 @@ union t128 { __m128 a; ///< 128-bit floating point (__m128 maps to the XMM[0-7] registers) }; -namespace common { +namespace Common { /// Rectangle data structure class Rect { public: diff --git a/src/common/console_listener.cpp b/src/common/console_listener.cpp index 27697ef1f..40122224c 100644 --- a/src/common/console_listener.cpp +++ b/src/common/console_listener.cpp @@ -3,14 +3,10 @@ // Refer to the license.txt file included. #include <algorithm> -#include <cmath> -#include <cstdio> -#include <string> + #ifdef _WIN32 #include <windows.h> #include <array> -#else -#include <cstdarg> #endif #include "common/common.h" @@ -47,7 +43,7 @@ void ConsoleListener::Open(bool Hidden, int Width, int Height, const char *Title // Save the window handle that AllocConsole() created hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Set the console window title - SetConsoleTitle(UTF8ToTStr(Title).c_str()); + SetConsoleTitle(Common::UTF8ToTStr(Title).c_str()); // Set letter space LetterSpace(80, 4000); //MoveWindow(GetConsoleWindow(), 200,200, 800,800, true); @@ -193,11 +189,11 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool { Str.resize(Str.size() + 1); if (!ReadConsoleOutputCharacter(hConsole, Str.back().data(), ReadBufferSize, coordScreen, &cCharsRead)) - SLog += StringFromFormat("WriteConsoleOutputCharacter error"); + SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error"); Attr.resize(Attr.size() + 1); if (!ReadConsoleOutputAttribute(hConsole, Attr.back().data(), ReadBufferSize, coordScreen, &cAttrRead)) - SLog += StringFromFormat("WriteConsoleOutputAttribute error"); + SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error"); // Break on error if (cAttrRead == 0) break; @@ -223,9 +219,9 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool for (size_t i = 0; i < Attr.size(); i++) { if (!WriteConsoleOutputCharacter(hConsole, Str[i].data(), ReadBufferSize, coordScreen, &cCharsWritten)) - SLog += StringFromFormat("WriteConsoleOutputCharacter error"); + SLog += Common::StringFromFormat("WriteConsoleOutputCharacter error"); if (!WriteConsoleOutputAttribute(hConsole, Attr[i].data(), ReadBufferSize, coordScreen, &cAttrWritten)) - SLog += StringFromFormat("WriteConsoleOutputAttribute error"); + SLog += Common::StringFromFormat("WriteConsoleOutputAttribute error"); BytesWritten += cAttrWritten; coordScreen = GetCoordinates(BytesWritten, LBufWidth); diff --git a/src/common/emu_window.cpp b/src/common/emu_window.cpp new file mode 100644 index 000000000..7a2c50ac8 --- /dev/null +++ b/src/common/emu_window.cpp @@ -0,0 +1,17 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "emu_window.h" + +void EmuWindow::KeyPressed(KeyMap::HostDeviceKey key) { + HID_User::PadState mapped_key = KeyMap::GetPadKey(key); + + HID_User::PadButtonPress(mapped_key); +} + +void EmuWindow::KeyReleased(KeyMap::HostDeviceKey key) { + HID_User::PadState mapped_key = KeyMap::GetPadKey(key); + + HID_User::PadButtonRelease(mapped_key); +} diff --git a/src/common/emu_window.h b/src/common/emu_window.h index 5e2c33d7a..23f178fdf 100644 --- a/src/common/emu_window.h +++ b/src/common/emu_window.h @@ -7,6 +7,8 @@ #include "common/common.h" #include "common/scm_rev.h" +#include "common/key_map.h" + // Abstraction class used to provide an interface between emulation code and the frontend (e.g. SDL, // QGLWidget, GLFW, etc...) class EmuWindow @@ -32,6 +34,12 @@ public: /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread virtual void DoneCurrent() = 0; + /// Signals a key press action to the HID module + static void KeyPressed(KeyMap::HostDeviceKey key); + + /// Signals a key release action to the HID module + static void KeyReleased(KeyMap::HostDeviceKey key); + Config GetConfig() const { return m_config; } diff --git a/src/common/extended_trace.cpp b/src/common/extended_trace.cpp index 66dae4935..9cd0398ed 100644 --- a/src/common/extended_trace.cpp +++ b/src/common/extended_trace.cpp @@ -278,7 +278,7 @@ void PrintFunctionAndSourceInfo(FILE* file, const STACKFRAME& callstack) GetFunctionInfoFromAddresses((ULONG)callstack.AddrPC.Offset, (ULONG)callstack.AddrFrame.Offset, symInfo); GetSourceInfoFromAddress((ULONG)callstack.AddrPC.Offset, srcInfo); - etfprint(file, " " + TStrToUTF8(srcInfo) + " : " + TStrToUTF8(symInfo) + "\n"); + etfprint(file, " " + Common::TStrToUTF8(srcInfo) + " : " + Common::TStrToUTF8(symInfo) + "\n"); } void StackTrace( HANDLE hThread, const char* lpszMessage, FILE *file ) diff --git a/src/common/file_search.cpp b/src/common/file_search.cpp index a9d19477d..63580f688 100644 --- a/src/common/file_search.cpp +++ b/src/common/file_search.cpp @@ -4,15 +4,13 @@ #include "common/common.h" -#include "common/common_paths.h" + #ifndef _WIN32 -#include <sys/types.h> #include <dirent.h> #else #include <windows.h> #endif -#include <string> #include <algorithm> #include "common/file_search.h" @@ -35,10 +33,10 @@ CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, cons void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath) { std::string GCMSearchPath; - BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); + Common::BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); #ifdef _WIN32 WIN32_FIND_DATA findData; - HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData); + HANDLE FindFirst = FindFirstFile(Common::UTF8ToTStr(GCMSearchPath).c_str(), &findData); if (FindFirst != INVALID_HANDLE_VALUE) { @@ -49,7 +47,7 @@ void CFileSearch::FindFiles(const std::string& _searchString, const std::string& if (findData.cFileName[0] != '.') { std::string strFilename; - BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName)); + Common::BuildCompleteFilename(strFilename, _strPath, Common::TStrToUTF8(findData.cFileName)); m_FileNames.push_back(strFilename); } diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index b6ff2e40b..970041007 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -4,9 +4,7 @@ #include "common/common.h" -#include "common/common_paths.h" #include "common/file_util.h" -#include "common/string_util.h" #ifdef _WIN32 #include <windows.h> @@ -16,10 +14,7 @@ #include <io.h> #include <direct.h> // getcwd #else -#include <cerrno> -#include <cstdlib> #include <sys/param.h> -#include <sys/types.h> #include <dirent.h> #endif @@ -32,8 +27,6 @@ #include <algorithm> #include <sys/stat.h> -#include "common/string_util.h" - #ifndef S_ISDIR #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) #endif @@ -71,7 +64,7 @@ bool Exists(const std::string &filename) StripTailDirSlashes(copy); #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); #else int result = stat64(copy.c_str(), &file_info); #endif @@ -88,7 +81,7 @@ bool IsDirectory(const std::string &filename) StripTailDirSlashes(copy); #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); #else int result = stat64(copy.c_str(), &file_info); #endif @@ -124,7 +117,7 @@ bool Delete(const std::string &filename) } #ifdef _WIN32 - if (!DeleteFile(UTF8ToTStr(filename).c_str())) + if (!DeleteFile(Common::UTF8ToTStr(filename).c_str())) { WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", filename.c_str(), GetLastErrorMsg()); @@ -146,7 +139,7 @@ bool CreateDir(const std::string &path) { INFO_LOG(COMMON, "CreateDir: directory %s", path.c_str()); #ifdef _WIN32 - if (::CreateDirectory(UTF8ToTStr(path).c_str(), NULL)) + if (::CreateDirectory(Common::UTF8ToTStr(path).c_str(), NULL)) return true; DWORD error = GetLastError(); if (error == ERROR_ALREADY_EXISTS) @@ -225,7 +218,7 @@ bool DeleteDir(const std::string &filename) } #ifdef _WIN32 - if (::RemoveDirectory(UTF8ToTStr(filename).c_str())) + if (::RemoveDirectory(Common::UTF8ToTStr(filename).c_str())) return true; #else if (rmdir(filename.c_str()) == 0) @@ -254,7 +247,7 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename) INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str()); #ifdef _WIN32 - if (CopyFile(UTF8ToTStr(srcFilename).c_str(), UTF8ToTStr(destFilename).c_str(), FALSE)) + if (CopyFile(Common::UTF8ToTStr(srcFilename).c_str(), Common::UTF8ToTStr(destFilename).c_str(), FALSE)) return true; ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", @@ -342,7 +335,7 @@ u64 GetSize(const std::string &filename) struct stat64 buf; #ifdef _WIN32 - if (_tstat64(UTF8ToTStr(filename).c_str(), &buf) == 0) + if (_tstat64(Common::UTF8ToTStr(filename).c_str(), &buf) == 0) #else if (stat64(filename.c_str(), &buf) == 0) #endif @@ -415,7 +408,7 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) // Find the first file in the directory. WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); + HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); @@ -425,7 +418,7 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) do { FSTEntry entry; - const std::string virtualName(TStrToUTF8(ffd.cFileName)); + const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); #else struct dirent dirent, *result = NULL; @@ -482,7 +475,7 @@ bool DeleteDirRecursively(const std::string &directory) #ifdef _WIN32 // Find the first file in the directory. WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile(UTF8ToTStr(directory + "\\*").c_str(), &ffd); + HANDLE hFind = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); if (hFind == INVALID_HANDLE_VALUE) { @@ -493,7 +486,7 @@ bool DeleteDirRecursively(const std::string &directory) // windows loop do { - const std::string virtualName(TStrToUTF8(ffd.cFileName)); + const std::string virtualName(Common::TStrToUTF8(ffd.cFileName)); #else struct dirent dirent, *result = NULL; DIR *dirp = opendir(directory.c_str()); @@ -631,7 +624,7 @@ std::string& GetExeDirectory() { TCHAR Dolphin_exe_Path[2048]; GetModuleFileName(NULL, Dolphin_exe_Path, 2048); - DolphinPath = TStrToUTF8(Dolphin_exe_Path); + DolphinPath = Common::TStrToUTF8(Dolphin_exe_Path); DolphinPath = DolphinPath.substr(0, DolphinPath.find_last_of('\\')); } return DolphinPath; @@ -826,7 +819,7 @@ bool IOFile::Open(const std::string& filename, const char openmode[]) { Close(); #ifdef _WIN32 - _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()); + _tfopen_s(&m_file, Common::UTF8ToTStr(filename).c_str(), Common::UTF8ToTStr(openmode).c_str()); #else m_file = fopen(filename.c_str(), openmode); #endif diff --git a/src/common/file_util.h b/src/common/file_util.h index 0871734d4..fdae5c9c8 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -213,7 +213,7 @@ template <typename T> void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { #ifdef _WIN32 - fstream.open(UTF8ToTStr(filename).c_str(), openmode); + fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode); #else fstream.open(filename.c_str(), openmode); #endif diff --git a/src/common/fixed_size_queue.h b/src/common/fixed_size_queue.h deleted file mode 100644 index 1e3a5dea6..000000000 --- a/src/common/fixed_size_queue.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 -// Refer to the license.txt file included. - -#pragma once - -// STL-look-a-like interface, but name is mixed case to distinguish it clearly from the -// real STL classes. - -// Not fully featured, no safety checking yet. Add features as needed. - -// TODO: "inline" storage? - -template <class T, int N> -class fixed_size_queue.h -{ - T *storage; - int head; - int tail; - int count; // sacrifice 4 bytes for a simpler implementation. may optimize away in the future. - - // Make copy constructor private for now. - fixed_size_queue.h(fixed_size_queue.h &other) { } - -public: - fixed_size_queue.h() - { - storage = new T[N]; - clear(); - } - - ~fixed_size_queue.h() - { - delete [] storage; - } - - void clear() { - head = 0; - tail = 0; - count = 0; - } - - void push(T t) { - storage[tail] = t; - tail++; - if (tail == N) - tail = 0; - count++; - } - - void pop() { - head++; - if (head == N) - head = 0; - count--; - } - - T pop_front() { - const T &temp = storage[head]; - pop(); - return temp; - } - - T &front() { return storage[head]; } - const T &front() const { return storage[head]; } - - size_t size() const { - return count; - } -}; diff --git a/src/common/key_map.cpp b/src/common/key_map.cpp new file mode 100644 index 000000000..309caab98 --- /dev/null +++ b/src/common/key_map.cpp @@ -0,0 +1,25 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "key_map.h" +#include <map> + +namespace KeyMap { + +static std::map<HostDeviceKey, HID_User::PadState> key_map; +static int next_device_id = 0; + +int NewDeviceId() { + return next_device_id++; +} + +void SetKeyMapping(HostDeviceKey key, HID_User::PadState padState) { + key_map[key].hex = padState.hex; +} + +HID_User::PadState GetPadKey(HostDeviceKey key) { + return key_map[key]; +} + +} diff --git a/src/common/key_map.h b/src/common/key_map.h new file mode 100644 index 000000000..b5acfbab0 --- /dev/null +++ b/src/common/key_map.h @@ -0,0 +1,45 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/hid.h" + +namespace KeyMap { + +/** + * Represents a key for a specific host device. + */ +struct HostDeviceKey { + int key_code; + int device_id; ///< Uniquely identifies a host device + + bool operator < (const HostDeviceKey &other) const { + if (device_id == other.device_id) { + return key_code < other.key_code; + } + return device_id < other.device_id; + } + + bool operator == (const HostDeviceKey &other) const { + return device_id == other.device_id && key_code == other.key_code; + } +}; + +/** + * Generates a new device id, which uniquely identifies a host device within KeyMap. + */ +int NewDeviceId(); + +/** + * Maps a device-specific key to a PadState. + */ +void SetKeyMapping(HostDeviceKey key, HID_User::PadState padState); + +/** + * Gets the PadState that's mapped to the provided device-specific key. + */ +HID_User::PadState GetPadKey(HostDeviceKey key); + +} diff --git a/src/common/log_manager.cpp b/src/common/log_manager.cpp index 4e1cb60bd..43346f279 100644 --- a/src/common/log_manager.cpp +++ b/src/common/log_manager.cpp @@ -8,7 +8,6 @@ #include "common/console_listener.h" #include "common/timer.h" #include "common/thread.h" -#include "common/file_util.h" void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line, const char* function, const char* fmt, ...) @@ -121,7 +120,7 @@ void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const if (!log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners()) return; - CharArrayFromFormatV(temp, MAX_MSGLEN, fmt, args); + Common::CharArrayFromFormatV(temp, MAX_MSGLEN, fmt, args); static const char level_to_char[7] = "ONEWID"; sprintf(msg, "%s %s:%u %c[%s] %s: %s\n", Common::Timer::GetTimeFormatted().c_str(), file, line, diff --git a/src/common/log_manager.h b/src/common/log_manager.h index 81d808825..ce62d0361 100644 --- a/src/common/log_manager.h +++ b/src/common/log_manager.h @@ -6,11 +6,11 @@ #include "common/log.h" #include "common/string_util.h" -#include "common/thread.h" #include "common/file_util.h" #include <cstring> #include <set> +#include <mutex> #define MAX_MESSAGES 8000 #define MAX_MSGLEN 1024 diff --git a/src/common/math_util.cpp b/src/common/math_util.cpp index 82eceab00..ab0e6b75c 100644 --- a/src/common/math_util.cpp +++ b/src/common/math_util.cpp @@ -6,8 +6,7 @@ #include "common/common.h" #include "common/math_util.h" -#include <cmath> -#include <numeric> +#include <numeric> // Necessary on OS X, but not Linux namespace MathUtil { diff --git a/src/common/mem_arena.cpp b/src/common/mem_arena.cpp index b76ac92d3..40d9c03a2 100644 --- a/src/common/mem_arena.cpp +++ b/src/common/mem_arena.cpp @@ -22,11 +22,7 @@ #include "common/string_util.h" #ifndef _WIN32 -#include <sys/stat.h> #include <fcntl.h> -#include <unistd.h> -#include <cerrno> -#include <cstring> #ifdef ANDROID #include <sys/ioctl.h> #include <linux/ashmem.h> @@ -143,7 +139,7 @@ void MemArena::GrabLowMemSpace(size_t size) // a bit more. for (int i = 0; i < 10000; i++) { - std::string file_name = StringFromFormat("/citramem.%d", i); + std::string file_name = Common::StringFromFormat("/citramem.%d", i); fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); if (fd != -1) { diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp index e01e63175..bab7d9f7a 100644 --- a/src/common/memory_util.cpp +++ b/src/common/memory_util.cpp @@ -10,9 +10,6 @@ #ifdef _WIN32 #include <windows.h> #include <psapi.h> -#else -#include <cerrno> -#include <cstdio> #endif #if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) @@ -190,7 +187,7 @@ std::string MemUsage() if (NULL == hProcess) return "MemUsage Error"; if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) - Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); + Ret = Common::StringFromFormat("%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); CloseHandle(hProcess); return Ret; diff --git a/src/common/msg_handler.cpp b/src/common/msg_handler.cpp index 3e02ec4d7..b3556aaa8 100644 --- a/src/common/msg_handler.cpp +++ b/src/common/msg_handler.cpp @@ -72,7 +72,7 @@ bool MsgAlert(bool yes_no, int Style, const char* format, ...) va_list args; va_start(args, format); - CharArrayFromFormatV(buffer, sizeof(buffer)-1, str_translator(format).c_str(), args); + Common::CharArrayFromFormatV(buffer, sizeof(buffer)-1, str_translator(format).c_str(), args); va_end(args); ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer); diff --git a/src/common/std_condition_variable.h b/src/common/std_condition_variable.h deleted file mode 100644 index ad2022f5a..000000000 --- a/src/common/std_condition_variable.h +++ /dev/null @@ -1,168 +0,0 @@ - -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ - -// GCC 4.4 provides <condition_variable> -#include <condition_variable> - -#elif __has_include(<condition_variable>) && !ANDROID - -// clang and libc++ provide <condition_variable> on OSX. However, the version -// of libc++ bundled with OSX 10.7 and 10.8 is buggy: it uses _ as a variable. -// -// We work around this issue by undefining and redefining _. - -#undef _ -#include <condition_variable> -#define _(s) wxGetTranslation((s)) - -#else - -// partial std::condition_variable implementation for win32/pthread - -#include "common/std_mutex.h" - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#if defined(_WIN32) && defined(_M_X64) -#define USE_CONDITION_VARIABLES -#elif defined(_WIN32) -#define USE_EVENTS -#endif - -namespace std -{ - -class condition_variable -{ -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - typedef CONDITION_VARIABLE native_type; -#elif defined(_WIN32) - typedef HANDLE native_type; -#else - typedef pthread_cond_t native_type; -#endif - -public: - -#ifdef USE_EVENTS - typedef native_type native_handle_type; -#else - typedef native_type* native_handle_type; -#endif - - condition_variable() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - InitializeConditionVariable(&m_handle); -#elif defined(_WIN32) - m_handle = CreateEvent(NULL, false, false, NULL); -#else - pthread_cond_init(&m_handle, NULL); -#endif - } - - ~condition_variable() - { -#if defined(_WIN32) && !defined(USE_CONDITION_VARIABLES) - CloseHandle(m_handle); -#elif !defined(_WIN32) - pthread_cond_destroy(&m_handle); -#endif - } - - condition_variable(const condition_variable&) /*= delete*/; - condition_variable& operator=(const condition_variable&) /*= delete*/; - - void notify_one() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - WakeConditionVariable(&m_handle); -#elif defined(_WIN32) - SetEvent(m_handle); -#else - pthread_cond_signal(&m_handle); -#endif - } - - void notify_all() - { -#if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) - WakeAllConditionVariable(&m_handle); -#elif defined(_WIN32) - // TODO: broken - SetEvent(m_handle); -#else - pthread_cond_broadcast(&m_handle); -#endif - } - - void wait(unique_lock<mutex>& lock) - { -#ifdef _WIN32 - #ifdef USE_SRWLOCKS - SleepConditionVariableSRW(&m_handle, lock.mutex()->native_handle(), INFINITE, 0); - #elif defined(USE_CONDITION_VARIABLES) - SleepConditionVariableCS(&m_handle, lock.mutex()->native_handle(), INFINITE); - #else - // TODO: broken, the unlock and wait need to be atomic - lock.unlock(); - WaitForSingleObject(m_handle, INFINITE); - lock.lock(); - #endif -#else - pthread_cond_wait(&m_handle, lock.mutex()->native_handle()); -#endif - } - - template <class Predicate> - void wait(unique_lock<mutex>& lock, Predicate pred) - { - while (!pred()) - wait(lock); - } - - //template <class Clock, class Duration> - //cv_status wait_until(unique_lock<mutex>& lock, - // const chrono::time_point<Clock, Duration>& abs_time); - - //template <class Clock, class Duration, class Predicate> - // bool wait_until(unique_lock<mutex>& lock, - // const chrono::time_point<Clock, Duration>& abs_time, - // Predicate pred); - - //template <class Rep, class Period> - //cv_status wait_for(unique_lock<mutex>& lock, - // const chrono::duration<Rep, Period>& rel_time); - - //template <class Rep, class Period, class Predicate> - // bool wait_for(unique_lock<mutex>& lock, - // const chrono::duration<Rep, Period>& rel_time, - // Predicate pred); - - native_handle_type native_handle() - { -#ifdef USE_EVENTS - return m_handle; -#else - return &m_handle; -#endif - } - -private: - native_type m_handle; -}; - -} - -#endif diff --git a/src/common/std_mutex.h b/src/common/std_mutex.h deleted file mode 100644 index 5711d791b..000000000 --- a/src/common/std_mutex.h +++ /dev/null @@ -1,362 +0,0 @@ -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ -// GCC 4.4 provides <mutex> -#include <mutex> -#elif __has_include(<mutex>) && !ANDROID -// Clang + libc++ -#include <mutex> -#else - -// partial <mutex> implementation for win32/pthread - -#include <algorithm> - -#if defined(_WIN32) -// WIN32 -#define WIN32_LEAN_AND_MEAN -#include <Windows.h> - -#else -// POSIX -#include <pthread.h> - -#endif - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#if defined(_WIN32) && defined(_M_X64) -#define USE_SRWLOCKS -#endif - -namespace std -{ - -class recursive_mutex -{ -#ifdef _WIN32 - typedef CRITICAL_SECTION native_type; -#else - typedef pthread_mutex_t native_type; -#endif - -public: - typedef native_type* native_handle_type; - - recursive_mutex(const recursive_mutex&) /*= delete*/; - recursive_mutex& operator=(const recursive_mutex&) /*= delete*/; - - recursive_mutex() - { -#ifdef _WIN32 - InitializeCriticalSection(&m_handle); -#else - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&m_handle, &attr); -#endif - } - - ~recursive_mutex() - { -#ifdef _WIN32 - DeleteCriticalSection(&m_handle); -#else - pthread_mutex_destroy(&m_handle); -#endif - } - - void lock() - { -#ifdef _WIN32 - EnterCriticalSection(&m_handle); -#else - pthread_mutex_lock(&m_handle); -#endif - } - - void unlock() - { -#ifdef _WIN32 - LeaveCriticalSection(&m_handle); -#else - pthread_mutex_unlock(&m_handle); -#endif - } - - bool try_lock() - { -#ifdef _WIN32 - return (0 != TryEnterCriticalSection(&m_handle)); -#else - return !pthread_mutex_trylock(&m_handle); -#endif - } - - native_handle_type native_handle() - { - return &m_handle; - } - -private: - native_type m_handle; -}; - -#if !defined(_WIN32) || defined(USE_SRWLOCKS) - -class mutex -{ -#ifdef _WIN32 - typedef SRWLOCK native_type; -#else - typedef pthread_mutex_t native_type; -#endif - -public: - typedef native_type* native_handle_type; - - mutex(const mutex&) /*= delete*/; - mutex& operator=(const mutex&) /*= delete*/; - - mutex() - { -#ifdef _WIN32 - InitializeSRWLock(&m_handle); -#else - pthread_mutex_init(&m_handle, NULL); -#endif - } - - ~mutex() - { -#ifdef _WIN32 -#else - pthread_mutex_destroy(&m_handle); -#endif - } - - void lock() - { -#ifdef _WIN32 - AcquireSRWLockExclusive(&m_handle); -#else - pthread_mutex_lock(&m_handle); -#endif - } - - void unlock() - { -#ifdef _WIN32 - ReleaseSRWLockExclusive(&m_handle); -#else - pthread_mutex_unlock(&m_handle); -#endif - } - - bool try_lock() - { -#ifdef _WIN32 - // XXX TryAcquireSRWLockExclusive requires Windows 7! - // return (0 != TryAcquireSRWLockExclusive(&m_handle)); - return false; -#else - return !pthread_mutex_trylock(&m_handle); -#endif - } - - native_handle_type native_handle() - { - return &m_handle; - } - -private: - native_type m_handle; -}; - -#else -typedef recursive_mutex mutex; // just use CriticalSections - -#endif - -enum defer_lock_t { defer_lock }; -enum try_to_lock_t { try_to_lock }; -enum adopt_lock_t { adopt_lock }; - -template <class Mutex> -class lock_guard -{ -public: - typedef Mutex mutex_type; - - explicit lock_guard(mutex_type& m) - : pm(m) - { - m.lock(); - } - - lock_guard(mutex_type& m, adopt_lock_t) - : pm(m) - { - } - - ~lock_guard() - { - pm.unlock(); - } - - lock_guard(lock_guard const&) /*= delete*/; - lock_guard& operator=(lock_guard const&) /*= delete*/; - -private: - mutex_type& pm; -}; - -template <class Mutex> -class unique_lock -{ -public: - typedef Mutex mutex_type; - - unique_lock() - : pm(NULL), owns(false) - {} - - /*explicit*/ unique_lock(mutex_type& m) - : pm(&m), owns(true) - { - m.lock(); - } - - unique_lock(mutex_type& m, defer_lock_t) - : pm(&m), owns(false) - {} - - unique_lock(mutex_type& m, try_to_lock_t) - : pm(&m), owns(m.try_lock()) - {} - - unique_lock(mutex_type& m, adopt_lock_t) - : pm(&m), owns(true) - {} - - //template <class Clock, class Duration> - //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); - - //template <class Rep, class Period> - //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); - - ~unique_lock() - { - if (owns_lock()) - mutex()->unlock(); - } - -#ifdef USE_RVALUE_REFERENCES - unique_lock& operator=(const unique_lock&) /*= delete*/; - - unique_lock& operator=(unique_lock&& other) - { -#else - unique_lock& operator=(const unique_lock& u) - { - // ugly const_cast to get around lack of rvalue references - unique_lock& other = const_cast<unique_lock&>(u); -#endif - swap(other); - return *this; - } - -#ifdef USE_RVALUE_REFERENCES - unique_lock(const unique_lock&) /*= delete*/; - - unique_lock(unique_lock&& other) - : pm(NULL), owns(false) - { -#else - unique_lock(const unique_lock& u) - : pm(NULL), owns(false) - { - // ugly const_cast to get around lack of rvalue references - unique_lock& other = const_cast<unique_lock&>(u); -#endif - swap(other); - } - - void lock() - { - mutex()->lock(); - owns = true; - } - - bool try_lock() - { - owns = mutex()->try_lock(); - return owns; - } - - //template <class Rep, class Period> - //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); - //template <class Clock, class Duration> - //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); - - void unlock() - { - mutex()->unlock(); - owns = false; - } - - void swap(unique_lock& u) - { - std::swap(pm, u.pm); - std::swap(owns, u.owns); - } - - mutex_type* release() - { - auto const ret = mutex(); - - pm = NULL; - owns = false; - - return ret; - } - - bool owns_lock() const - { - return owns; - } - - //explicit operator bool () const - //{ - // return owns_lock(); - //} - - mutex_type* mutex() const - { - return pm; - } - -private: - mutex_type* pm; - bool owns; -}; - -template <class Mutex> -void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) -{ - x.swap(y); -} - -} - -#endif diff --git a/src/common/std_thread.h b/src/common/std_thread.h deleted file mode 100644 index ce1336ee7..000000000 --- a/src/common/std_thread.h +++ /dev/null @@ -1,314 +0,0 @@ -#pragma once - -#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) -#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) - -#ifndef __has_include -#define __has_include(s) 0 -#endif - -#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ -// GCC 4.4 provides <thread> -#ifndef _GLIBCXX_USE_SCHED_YIELD -#define _GLIBCXX_USE_SCHED_YIELD -#endif -#include <thread> -#elif __has_include(<thread>) && !ANDROID -// Clang + libc++ -#include <thread> -#else - -// partial std::thread implementation for win32/pthread - -#include <algorithm> - -#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) -#define USE_RVALUE_REFERENCES -#endif - -#ifdef __APPLE__ -#import <Foundation/NSAutoreleasePool.h> -#endif - -#if defined(_WIN32) -// WIN32 - -#define WIN32_LEAN_AND_MEAN -#include <Windows.h> - -#if defined(_MSC_VER) && defined(_MT) -// When linking with LIBCMT (the multithreaded C library), Microsoft recommends -// using _beginthreadex instead of CreateThread. -#define USE_BEGINTHREADEX -#include <process.h> -#endif - -#ifdef USE_BEGINTHREADEX -#define THREAD_ID unsigned -#define THREAD_RETURN unsigned __stdcall -#else -#define THREAD_ID DWORD -#define THREAD_RETURN DWORD WINAPI -#endif -#define THREAD_HANDLE HANDLE - -#else -// PTHREAD - -#include <unistd.h> - -#ifndef _POSIX_THREADS -#error unsupported platform (no pthreads?) -#endif - -#include <pthread.h> - -#define THREAD_ID pthread_t -#define THREAD_HANDLE pthread_t -#define THREAD_RETURN void* - -#endif - -namespace std -{ - -class thread -{ -public: - typedef THREAD_HANDLE native_handle_type; - - class id - { - friend class thread; - public: - id() : m_thread(0) {} - id(THREAD_ID _id) : m_thread(_id) {} - - bool operator==(const id& rhs) const - { - return m_thread == rhs.m_thread; - } - - bool operator!=(const id& rhs) const - { - return !(*this == rhs); - } - - bool operator<(const id& rhs) const - { - return m_thread < rhs.m_thread; - } - - private: - THREAD_ID m_thread; - }; - - // no variadic template support in msvc - //template <typename C, typename... A> - //thread(C&& func, A&&... args); - - template <typename C> - thread(C func) - { - StartThread(new Func<C>(func)); - } - - template <typename C, typename A> - thread(C func, A arg) - { - StartThread(new FuncArg<C, A>(func, arg)); - } - - thread() /*= default;*/ {} - -#ifdef USE_RVALUE_REFERENCES - thread(const thread&) /*= delete*/; - - thread(thread&& other) - { -#else - thread(const thread& t) - { - // ugly const_cast to get around lack of rvalue references - thread& other = const_cast<thread&>(t); -#endif - swap(other); - } - -#ifdef USE_RVALUE_REFERENCES - thread& operator=(const thread&) /*= delete*/; - - thread& operator=(thread&& other) - { -#else - thread& operator=(const thread& t) - { - // ugly const_cast to get around lack of rvalue references - thread& other = const_cast<thread&>(t); -#endif - if (joinable()) - detach(); - swap(other); - return *this; - } - - ~thread() - { - if (joinable()) - detach(); - } - - bool joinable() const - { - return m_id != id(); - } - - id get_id() const - { - return m_id; - } - - native_handle_type native_handle() - { -#ifdef _WIN32 - return m_handle; -#else - return m_id.m_thread; -#endif - } - - void join() - { -#ifdef _WIN32 - WaitForSingleObject(m_handle, INFINITE); - detach(); -#else - pthread_join(m_id.m_thread, NULL); - m_id = id(); -#endif - } - - void detach() - { -#ifdef _WIN32 - CloseHandle(m_handle); -#else - pthread_detach(m_id.m_thread); -#endif - m_id = id(); - } - - void swap(thread& other) - { - std::swap(m_id, other.m_id); -#ifdef _WIN32 - std::swap(m_handle, other.m_handle); -#endif - } - - static unsigned hardware_concurrency() - { -#ifdef _WIN32 - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - return static_cast<unsigned>(sysinfo.dwNumberOfProcessors); -#else - return 0; -#endif - } - -private: - id m_id; - -#ifdef _WIN32 - native_handle_type m_handle; -#endif - - template <typename F> - void StartThread(F* param) - { -#ifdef USE_BEGINTHREADEX - m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); -#elif defined(_WIN32) - m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); -#else - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 1024 * 1024); - if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param)) - m_id = id(); -#endif - } - - template <typename C> - class Func - { - public: - Func(C _func) : func(_func) {} - - void Run() { func(); } - - private: - C const func; - }; - - template <typename C, typename A> - class FuncArg - { - public: - FuncArg(C _func, A _arg) : func(_func), arg(_arg) {} - - void Run() { func(arg); } - - private: - C const func; - A arg; - }; - - template <typename F> - static THREAD_RETURN RunAndDelete(void* param) - { -#ifdef __APPLE__ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -#endif - static_cast<F*>(param)->Run(); - delete static_cast<F*>(param); -#ifdef __APPLE__ - [pool release]; -#endif - return 0; - } -}; - -namespace this_thread -{ - -inline void yield() -{ -#ifdef _WIN32 - SwitchToThread(); -#else - sleep(0); -#endif -} - -inline thread::id get_id() -{ -#ifdef _WIN32 - return GetCurrentThreadId(); -#else - return pthread_self(); -#endif -} - -} // namespace this_thread - -} // namespace std - -#undef USE_RVALUE_REFERENCES -#undef USE_BEGINTHREADEX -#undef THREAD_ID -#undef THREAD_RETURN -#undef THREAD_HANDLE - -#endif diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index c1f22bda3..9199e30bc 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -3,34 +3,28 @@ // Refer to the license.txt file included. #include <algorithm> -#include <cstdlib> -#include <cstdio> #include "common/common.h" -#include "common/common_paths.h" #include "common/string_util.h" #ifdef _WIN32 #include <Windows.h> #else - #include <cerrno> #include <iconv.h> #endif +namespace Common { + /// Make a string lowercase -void LowerStr(char* str) { - for (int i = 0; str[i]; i++) { - str[i] = tolower(str[ i ]); - } +std::string ToLower(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + return str; } /// Make a string uppercase -void UpperStr(char* str) { - for (int i=0; i < strlen(str); i++) { - if(str[i] >= 'a' && str[i] <= 'z') { - str[i] &= 0xDF; - } - } +std::string ToUpper(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::toupper); + return str; } // faster than sscanf @@ -283,12 +277,17 @@ std::string TabsToSpaces(int tab_size, const std::string &in) std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) { - while(1) + size_t pos = 0; + + if (src == dest) + return result; + + while ((pos = result.find(src, pos)) != std::string::npos) { - size_t pos = result.find(src); - if (pos == std::string::npos) break; result.replace(pos, src.size(), dest); + pos += dest.length(); } + return result; } @@ -545,3 +544,5 @@ std::string UTF16ToUTF8(const std::wstring& input) } #endif + +} diff --git a/src/common/string_util.h b/src/common/string_util.h index ba4cd363e..16ce39bc1 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -12,11 +12,13 @@ #include "common/common.h" +namespace Common { + /// Make a string lowercase -void LowerStr(char* str); +std::string ToLower(std::string str); /// Make a string uppercase -void UpperStr(char* str); +std::string ToUpper(std::string str); std::string StringFromFormat(const char* format, ...); // Cheap! @@ -111,3 +113,5 @@ inline std::string UTF8ToTStr(const std::string& str) #endif #endif + +} diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 7341035c2..60d8ed075 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -3,16 +3,13 @@ // Refer to the license.txt file included. #include "common/thread.h" -#include "common/common.h" #ifdef __APPLE__ #include <mach/mach.h> #elif defined(BSD4_4) || defined(__OpenBSD__) #include <pthread_np.h> -#endif - -#ifdef USE_BEGINTHREADEX -#include <process.h> +#elif defined(_WIN32) +#include <Windows.h> #endif namespace Common diff --git a/src/common/thread.h b/src/common/thread.h index dbb9da53b..f7ace21b4 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -4,14 +4,13 @@ #pragma once -#include "common/std_condition_variable.h" -#include "common/std_mutex.h" -#include "common/std_thread.h" - // Don't include common.h here as it will break LogManager #include "common/common_types.h" #include <cstdio> #include <cstring> +#include <thread> +#include <condition_variable> +#include <mutex> // This may not be defined outside _WIN32 #ifndef _WIN32 diff --git a/src/common/utf8.cpp b/src/common/utf8.cpp index c83824d35..be4ebc855 100644 --- a/src/common/utf8.cpp +++ b/src/common/utf8.cpp @@ -19,12 +19,8 @@ #endif #include <cstdlib> -#include <cstdio> #include <cstring> -#include <cstdarg> - #include <algorithm> -#include <string> #include "common/common_types.h" #include "common/utf8.h" diff --git a/src/core/arm/disassembler/arm_disasm.cpp b/src/core/arm/disassembler/arm_disasm.cpp index db1567498..45c720e16 100644 --- a/src/core/arm/disassembler/arm_disasm.cpp +++ b/src/core/arm/disassembler/arm_disasm.cpp @@ -1,8 +1,8 @@ // Copyright 2006 The Android Open Source Project -#include <stdio.h> -#include <string.h> +#include <string> +#include "common/string_util.h" #include "core/arm/disassembler/arm_disasm.h" static const char *cond_names[] = { @@ -135,20 +135,14 @@ static const char* cond_to_str(int cond) { return cond_names[cond]; } -char *ARM_Disasm::disasm(uint32_t addr, uint32_t insn, char *result) +std::string ARM_Disasm::Disassemble(uint32_t addr, uint32_t insn) { - static char buf[80]; - char *ptr; - - ptr = result ? result : buf; - Opcode opcode = decode(insn); + Opcode opcode = Decode(insn); switch (opcode) { case OP_INVALID: - sprintf(ptr, "Invalid"); - return ptr; + return "Invalid"; case OP_UNDEFINED: - sprintf(ptr, "Undefined"); - return ptr; + return "Undefined"; case OP_ADC: case OP_ADD: case OP_AND: @@ -165,28 +159,26 @@ char *ARM_Disasm::disasm(uint32_t addr, uint32_t insn, char *result) case OP_SUB: case OP_TEQ: case OP_TST: - return disasm_alu(opcode, insn, ptr); + return DisassembleALU(opcode, insn); case OP_B: case OP_BL: - return disasm_branch(addr, opcode, insn, ptr); + return DisassembleBranch(addr, opcode, insn); case OP_BKPT: - return disasm_bkpt(insn, ptr); + return DisassembleBKPT(insn); case OP_BLX: // not supported yet break; case OP_BX: - return disasm_bx(insn, ptr); + return DisassembleBX(insn); case OP_CDP: - sprintf(ptr, "cdp"); - return ptr; + return "cdp"; case OP_CLZ: - return disasm_clz(insn, ptr); + return DisassembleCLZ(insn); case OP_LDC: - sprintf(ptr, "ldc"); - return ptr; + return "ldc"; case OP_LDM: case OP_STM: - return disasm_memblock(opcode, insn, ptr); + return DisassembleMemblock(opcode, insn); case OP_LDR: case OP_LDRB: case OP_LDRBT: @@ -195,53 +187,52 @@ char *ARM_Disasm::disasm(uint32_t addr, uint32_t insn, char *result) case OP_STRB: case OP_STRBT: case OP_STRT: - return disasm_mem(insn, ptr); + return DisassembleMem(insn); case OP_LDRH: case OP_LDRSB: case OP_LDRSH: case OP_STRH: - return disasm_memhalf(insn, ptr); + return DisassembleMemHalf(insn); case OP_MCR: case OP_MRC: - return disasm_mcr(opcode, insn, ptr); + return DisassembleMCR(opcode, insn); case OP_MLA: - return disasm_mla(opcode, insn, ptr); + return DisassembleMLA(opcode, insn); case OP_MRS: - return disasm_mrs(insn, ptr); + return DisassembleMRS(insn); case OP_MSR: - return disasm_msr(insn, ptr); + return DisassembleMSR(insn); case OP_MUL: - return disasm_mul(opcode, insn, ptr); + return DisassembleMUL(opcode, insn); case OP_PLD: - return disasm_pld(insn, ptr); + return DisassemblePLD(insn); case OP_STC: - sprintf(ptr, "stc"); - return ptr; + return "stc"; case OP_SWI: - return disasm_swi(insn, ptr); + return DisassembleSWI(insn); case OP_SWP: case OP_SWPB: - return disasm_swp(opcode, insn, ptr); + return DisassembleSWP(opcode, insn); case OP_UMLAL: case OP_UMULL: case OP_SMLAL: case OP_SMULL: - return disasm_umlal(opcode, insn, ptr); + return DisassembleUMLAL(opcode, insn); default: - sprintf(ptr, "Error"); - return ptr; + return "Error"; } return NULL; } -char *ARM_Disasm::disasm_alu(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleALU(Opcode opcode, uint32_t insn) { static const uint8_t kNoOperand1 = 1; static const uint8_t kNoDest = 2; static const uint8_t kNoSbit = 4; - char rn_str[20]; - char rd_str[20]; + std::string rn_str; + std::string rd_str; + uint8_t flags = 0; uint8_t cond = (insn >> 28) & 0xf; uint8_t is_immed = (insn >> 25) & 0x1; @@ -250,7 +241,7 @@ char *ARM_Disasm::disasm_alu(Opcode opcode, uint32_t insn, char *ptr) uint8_t rd = (insn >> 12) & 0xf; uint8_t immed = insn & 0xff; - const char *opname = opcode_names[opcode]; + const char* opname = opcode_names[opcode]; switch (opcode) { case OP_CMN: case OP_CMP: @@ -269,14 +260,14 @@ char *ARM_Disasm::disasm_alu(Opcode opcode, uint32_t insn, char *ptr) // The "mov" instruction ignores the first operand (rn). rn_str[0] = 0; if ((flags & kNoOperand1) == 0) { - sprintf(rn_str, "r%d, ", rn); + rn_str = Common::StringFromFormat("r%d, ", rn); } // The following instructions do not write the result register (rd): // tst, teq, cmp, cmn. rd_str[0] = 0; if ((flags & kNoDest) == 0) { - sprintf(rd_str, "r%d, ", rd); + rd_str = Common::StringFromFormat("r%d, ", rd); } const char *sbit_str = ""; @@ -284,9 +275,8 @@ char *ARM_Disasm::disasm_alu(Opcode opcode, uint32_t insn, char *ptr) sbit_str = "s"; if (is_immed) { - sprintf(ptr, "%s%s%s\t%s%s#%u ; 0x%x", - opname, cond_to_str(cond), sbit_str, rd_str, rn_str, immed, immed); - return ptr; + return Common::StringFromFormat("%s%s%s\t%s%s#%u ; 0x%x", + opname, cond_to_str(cond), sbit_str, rd_str.c_str(), rn_str.c_str(), immed, immed); } uint8_t shift_is_reg = (insn >> 4) & 1; @@ -300,33 +290,29 @@ char *ARM_Disasm::disasm_alu(Opcode opcode, uint32_t insn, char *ptr) rotated_val = (rotated_val >> rotate2) | (rotated_val << (32 - rotate2)); if (!shift_is_reg && shift_type == 0 && shift_amount == 0) { - sprintf(ptr, "%s%s%s\t%s%sr%d", - opname, cond_to_str(cond), sbit_str, rd_str, rn_str, rm); - return ptr; + return Common::StringFromFormat("%s%s%s\t%s%sr%d", + opname, cond_to_str(cond), sbit_str, rd_str.c_str(), rn_str.c_str(), rm); } const char *shift_name = shift_names[shift_type]; if (shift_is_reg) { - sprintf(ptr, "%s%s%s\t%s%sr%d, %s r%d", - opname, cond_to_str(cond), sbit_str, rd_str, rn_str, rm, + return Common::StringFromFormat("%s%s%s\t%s%sr%d, %s r%d", + opname, cond_to_str(cond), sbit_str, rd_str.c_str(), rn_str.c_str(), rm, shift_name, rs); - return ptr; } if (shift_amount == 0) { if (shift_type == 3) { - sprintf(ptr, "%s%s%s\t%s%sr%d, RRX", - opname, cond_to_str(cond), sbit_str, rd_str, rn_str, rm); - return ptr; + return Common::StringFromFormat("%s%s%s\t%s%sr%d, RRX", + opname, cond_to_str(cond), sbit_str, rd_str.c_str(), rn_str.c_str(), rm); } shift_amount = 32; } - sprintf(ptr, "%s%s%s\t%s%sr%d, %s #%u", - opname, cond_to_str(cond), sbit_str, rd_str, rn_str, rm, + return Common::StringFromFormat("%s%s%s\t%s%sr%d, %s #%u", + opname, cond_to_str(cond), sbit_str, rd_str.c_str(), rn_str.c_str(), rm, shift_name, shift_amount); - return ptr; } -char *ARM_Disasm::disasm_branch(uint32_t addr, Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleBranch(uint32_t addr, Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint32_t offset = insn & 0xffffff; @@ -339,37 +325,34 @@ char *ARM_Disasm::disasm_branch(uint32_t addr, Opcode opcode, uint32_t insn, cha offset += 8; addr += offset; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s\t0x%x", opname, cond_to_str(cond), addr); - return ptr; + return Common::StringFromFormat("%s%s\t0x%x", opname, cond_to_str(cond), addr); } -char *ARM_Disasm::disasm_bx(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleBX(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rn = insn & 0xf; - sprintf(ptr, "bx%s\tr%d", cond_to_str(cond), rn); - return ptr; + return Common::StringFromFormat("bx%s\tr%d", cond_to_str(cond), rn); } -char *ARM_Disasm::disasm_bkpt(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleBKPT(uint32_t insn) { uint32_t immed = (((insn >> 8) & 0xfff) << 4) | (insn & 0xf); - sprintf(ptr, "bkpt\t#%d", immed); - return ptr; + return Common::StringFromFormat("bkpt\t#%d", immed); } -char *ARM_Disasm::disasm_clz(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleCLZ(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rd = (insn >> 12) & 0xf; uint8_t rm = insn & 0xf; - sprintf(ptr, "clz%s\tr%d, r%d", cond_to_str(cond), rd, rm); - return ptr; + return Common::StringFromFormat("clz%s\tr%d, r%d", cond_to_str(cond), rd, rm); } -char *ARM_Disasm::disasm_memblock(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMemblock(Opcode opcode, uint32_t insn) { - char tmp_reg[10], tmp_list[80]; + std::string tmp_reg; + std::string tmp_list; uint8_t cond = (insn >> 28) & 0xf; uint8_t write_back = (insn >> 21) & 0x1; @@ -393,8 +376,7 @@ char *ARM_Disasm::disasm_memblock(Opcode opcode, uint32_t insn, char *ptr) tmp_list[0] = 0; for (int ii = 0; ii < 16; ++ii) { if (reg_list & (1 << ii)) { - sprintf(tmp_reg, "%sr%d", comma, ii); - strcat(tmp_list, tmp_reg); + tmp_list += Common::StringFromFormat("%sr%d", comma, ii); comma = ","; } } @@ -414,12 +396,11 @@ char *ARM_Disasm::disasm_memblock(Opcode opcode, uint32_t insn, char *ptr) } } - sprintf(ptr, "%s%s%s\tr%d%s, {%s}%s", - opname, cond_to_str(cond), addr_mode, rn, bang, tmp_list, carret); - return ptr; + return Common::StringFromFormat("%s%s%s\tr%d%s, {%s}%s", + opname, cond_to_str(cond), addr_mode, rn, bang, tmp_list.c_str(), carret); } -char *ARM_Disasm::disasm_mem(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMem(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t is_reg = (insn >> 25) & 0x1; @@ -451,20 +432,20 @@ char *ARM_Disasm::disasm_mem(uint32_t insn, char *ptr) if (is_reg == 0) { if (is_pre) { if (offset == 0) { - sprintf(ptr, "%s%s%s\tr%d, [r%d]", + return Common::StringFromFormat("%s%s%s\tr%d, [r%d]", opname, cond_to_str(cond), byte, rd, rn); } else { - sprintf(ptr, "%s%s%s\tr%d, [r%d, #%s%u]%s", + return Common::StringFromFormat("%s%s%s\tr%d, [r%d, #%s%u]%s", opname, cond_to_str(cond), byte, rd, rn, minus, offset, bang); } } else { const char *transfer = ""; if (write_back) transfer = "t"; - sprintf(ptr, "%s%s%s%s\tr%d, [r%d], #%s%u", + + return Common::StringFromFormat("%s%s%s%s\tr%d, [r%d], #%s%u", opname, cond_to_str(cond), byte, transfer, rd, rn, minus, offset); } - return ptr; } uint8_t rm = insn & 0xf; @@ -476,21 +457,18 @@ char *ARM_Disasm::disasm_mem(uint32_t insn, char *ptr) if (is_pre) { if (shift_amount == 0) { if (shift_type == 0) { - sprintf(ptr, "%s%s%s\tr%d, [r%d, %sr%d]%s", + return Common::StringFromFormat("%s%s%s\tr%d, [r%d, %sr%d]%s", opname, cond_to_str(cond), byte, rd, rn, minus, rm, bang); - return ptr; } if (shift_type == 3) { - sprintf(ptr, "%s%s%s\tr%d, [r%d, %sr%d, RRX]%s", + return Common::StringFromFormat("%s%s%s\tr%d, [r%d, %sr%d, RRX]%s", opname, cond_to_str(cond), byte, rd, rn, minus, rm, bang); - return ptr; } shift_amount = 32; } - sprintf(ptr, "%s%s%s\tr%d, [r%d, %sr%d, %s #%u]%s", + return Common::StringFromFormat("%s%s%s\tr%d, [r%d, %sr%d, %s #%u]%s", opname, cond_to_str(cond), byte, rd, rn, minus, rm, shift_name, shift_amount, bang); - return ptr; } const char *transfer = ""; @@ -499,25 +477,22 @@ char *ARM_Disasm::disasm_mem(uint32_t insn, char *ptr) if (shift_amount == 0) { if (shift_type == 0) { - sprintf(ptr, "%s%s%s%s\tr%d, [r%d], %sr%d", + return Common::StringFromFormat("%s%s%s%s\tr%d, [r%d], %sr%d", opname, cond_to_str(cond), byte, transfer, rd, rn, minus, rm); - return ptr; } if (shift_type == 3) { - sprintf(ptr, "%s%s%s%s\tr%d, [r%d], %sr%d, RRX", + return Common::StringFromFormat("%s%s%s%s\tr%d, [r%d], %sr%d, RRX", opname, cond_to_str(cond), byte, transfer, rd, rn, minus, rm); - return ptr; } shift_amount = 32; } - sprintf(ptr, "%s%s%s%s\tr%d, [r%d], %sr%d, %s #%u", + return Common::StringFromFormat("%s%s%s%s\tr%d, [r%d], %sr%d, %s #%u", opname, cond_to_str(cond), byte, transfer, rd, rn, minus, rm, shift_name, shift_amount); - return ptr; } -char *ARM_Disasm::disasm_memhalf(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMemHalf(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t is_load = (insn >> 20) & 0x1; @@ -553,29 +528,27 @@ char *ARM_Disasm::disasm_memhalf(uint32_t insn, char *ptr) if (is_immed) { if (is_pre) { if (offset == 0) { - sprintf(ptr, "%s%sh\tr%d, [r%d]", opname, cond_to_str(cond), rd, rn); + return Common::StringFromFormat("%s%sh\tr%d, [r%d]", opname, cond_to_str(cond), rd, rn); } else { - sprintf(ptr, "%s%sh\tr%d, [r%d, #%s%u]%s", + return Common::StringFromFormat("%s%sh\tr%d, [r%d, #%s%u]%s", opname, cond_to_str(cond), rd, rn, minus, offset, bang); } } else { - sprintf(ptr, "%s%sh\tr%d, [r%d], #%s%u", + return Common::StringFromFormat("%s%sh\tr%d, [r%d], #%s%u", opname, cond_to_str(cond), rd, rn, minus, offset); } - return ptr; } if (is_pre) { - sprintf(ptr, "%s%sh\tr%d, [r%d, %sr%d]%s", + return Common::StringFromFormat("%s%sh\tr%d, [r%d, %sr%d]%s", opname, cond_to_str(cond), rd, rn, minus, rm, bang); } else { - sprintf(ptr, "%s%sh\tr%d, [r%d], %sr%d", + return Common::StringFromFormat("%s%sh\tr%d, [r%d], %sr%d", opname, cond_to_str(cond), rd, rn, minus, rm); } - return ptr; } -char *ARM_Disasm::disasm_mcr(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMCR(Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t crn = (insn >> 16) & 0xf; @@ -585,12 +558,11 @@ char *ARM_Disasm::disasm_mcr(Opcode opcode, uint32_t insn, char *ptr) uint8_t crm = insn & 0xf; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s\t%d, 0, r%d, cr%d, cr%d, {%d}", + return Common::StringFromFormat("%s%s\t%d, 0, r%d, cr%d, cr%d, {%d}", opname, cond_to_str(cond), cpnum, crd, crn, crm, opcode2); - return ptr; } -char *ARM_Disasm::disasm_mla(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMLA(Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rd = (insn >> 16) & 0xf; @@ -600,12 +572,11 @@ char *ARM_Disasm::disasm_mla(Opcode opcode, uint32_t insn, char *ptr) uint8_t bit_s = (insn >> 20) & 1; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s%s\tr%d, r%d, r%d, r%d", + return Common::StringFromFormat("%s%s%s\tr%d, r%d, r%d, r%d", opname, cond_to_str(cond), bit_s ? "s" : "", rd, rm, rs, rn); - return ptr; } -char *ARM_Disasm::disasm_umlal(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleUMLAL(Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rdhi = (insn >> 16) & 0xf; @@ -615,12 +586,11 @@ char *ARM_Disasm::disasm_umlal(Opcode opcode, uint32_t insn, char *ptr) uint8_t bit_s = (insn >> 20) & 1; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s%s\tr%d, r%d, r%d, r%d", + return Common::StringFromFormat("%s%s%s\tr%d, r%d, r%d, r%d", opname, cond_to_str(cond), bit_s ? "s" : "", rdlo, rdhi, rm, rs); - return ptr; } -char *ARM_Disasm::disasm_mul(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMUL(Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rd = (insn >> 16) & 0xf; @@ -629,22 +599,20 @@ char *ARM_Disasm::disasm_mul(Opcode opcode, uint32_t insn, char *ptr) uint8_t bit_s = (insn >> 20) & 1; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s%s\tr%d, r%d, r%d", + return Common::StringFromFormat("%s%s%s\tr%d, r%d, r%d", opname, cond_to_str(cond), bit_s ? "s" : "", rd, rm, rs); - return ptr; } -char *ARM_Disasm::disasm_mrs(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMRS(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rd = (insn >> 12) & 0xf; uint8_t ps = (insn >> 22) & 1; - sprintf(ptr, "mrs%s\tr%d, %s", cond_to_str(cond), rd, ps ? "spsr" : "cpsr"); - return ptr; + return Common::StringFromFormat("mrs%s\tr%d, %s", cond_to_str(cond), rd, ps ? "spsr" : "cpsr"); } -char *ARM_Disasm::disasm_msr(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleMSR(uint32_t insn) { char flags[8]; int flag_index = 0; @@ -668,19 +636,17 @@ char *ARM_Disasm::disasm_msr(uint32_t insn, char *ptr) uint8_t rotate = (insn >> 8) & 0xf; uint8_t rotate2 = rotate << 1; uint32_t rotated_val = (immed >> rotate2) | (immed << (32 - rotate2)); - sprintf(ptr, "msr%s\t%s_%s, #0x%x", + return Common::StringFromFormat("msr%s\t%s_%s, #0x%x", cond_to_str(cond), pd ? "spsr" : "cpsr", flags, rotated_val); - return ptr; } uint8_t rm = insn & 0xf; - sprintf(ptr, "msr%s\t%s_%s, r%d", + return Common::StringFromFormat("msr%s\t%s_%s, r%d", cond_to_str(cond), pd ? "spsr" : "cpsr", flags, rm); - return ptr; } -char *ARM_Disasm::disasm_pld(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassemblePLD(uint32_t insn) { uint8_t is_reg = (insn >> 25) & 0x1; uint8_t is_up = (insn >> 23) & 0x1; @@ -692,29 +658,26 @@ char *ARM_Disasm::disasm_pld(uint32_t insn, char *ptr) if (is_reg) { uint8_t rm = insn & 0xf; - sprintf(ptr, "pld\t[r%d, %sr%d]", rn, minus, rm); - return ptr; + return Common::StringFromFormat("pld\t[r%d, %sr%d]", rn, minus, rm); } uint16_t offset = insn & 0xfff; if (offset == 0) { - sprintf(ptr, "pld\t[r%d]", rn); + return Common::StringFromFormat("pld\t[r%d]", rn); } else { - sprintf(ptr, "pld\t[r%d, #%s%u]", rn, minus, offset); + return Common::StringFromFormat("pld\t[r%d, #%s%u]", rn, minus, offset); } - return ptr; } -char *ARM_Disasm::disasm_swi(uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleSWI(uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint32_t sysnum = insn & 0x00ffffff; - sprintf(ptr, "swi%s 0x%x", cond_to_str(cond), sysnum); - return ptr; + return Common::StringFromFormat("swi%s 0x%x", cond_to_str(cond), sysnum); } -char *ARM_Disasm::disasm_swp(Opcode opcode, uint32_t insn, char *ptr) +std::string ARM_Disasm::DisassembleSWP(Opcode opcode, uint32_t insn) { uint8_t cond = (insn >> 28) & 0xf; uint8_t rn = (insn >> 16) & 0xf; @@ -722,26 +685,25 @@ char *ARM_Disasm::disasm_swp(Opcode opcode, uint32_t insn, char *ptr) uint8_t rm = insn & 0xf; const char *opname = opcode_names[opcode]; - sprintf(ptr, "%s%s\tr%d, r%d, [r%d]", opname, cond_to_str(cond), rd, rm, rn); - return ptr; + return Common::StringFromFormat("%s%s\tr%d, r%d, [r%d]", opname, cond_to_str(cond), rd, rm, rn); } -Opcode ARM_Disasm::decode(uint32_t insn) { +Opcode ARM_Disasm::Decode(uint32_t insn) { uint32_t bits27_26 = (insn >> 26) & 0x3; switch (bits27_26) { case 0x0: - return decode00(insn); + return Decode00(insn); case 0x1: - return decode01(insn); + return Decode01(insn); case 0x2: - return decode10(insn); + return Decode10(insn); case 0x3: - return decode11(insn); + return Decode11(insn); } return OP_INVALID; } -Opcode ARM_Disasm::decode00(uint32_t insn) { +Opcode ARM_Disasm::Decode00(uint32_t insn) { uint8_t bit25 = (insn >> 25) & 0x1; uint8_t bit4 = (insn >> 4) & 0x1; if (bit25 == 0 && bit4 == 1) { @@ -767,21 +729,21 @@ Opcode ARM_Disasm::decode00(uint32_t insn) { return OP_SWP; } // One of the multiply instructions - return decode_mul(insn); + return DecodeMUL(insn); } uint8_t bit7 = (insn >> 7) & 0x1; if (bit7 == 1) { // One of the load/store halfword/byte instructions - return decode_ldrh(insn); + return DecodeLDRH(insn); } } // One of the data processing instructions - return decode_alu(insn); + return DecodeALU(insn); } -Opcode ARM_Disasm::decode01(uint32_t insn) { +Opcode ARM_Disasm::Decode01(uint32_t insn) { uint8_t is_reg = (insn >> 25) & 0x1; uint8_t bit4 = (insn >> 4) & 0x1; if (is_reg == 1 && bit4 == 1) @@ -808,7 +770,7 @@ Opcode ARM_Disasm::decode01(uint32_t insn) { return OP_STR; } -Opcode ARM_Disasm::decode10(uint32_t insn) { +Opcode ARM_Disasm::Decode10(uint32_t insn) { uint8_t bit25 = (insn >> 25) & 0x1; if (bit25 == 0) { // LDM/STM @@ -833,7 +795,7 @@ Opcode ARM_Disasm::decode10(uint32_t insn) { return OP_BL; } -Opcode ARM_Disasm::decode11(uint32_t insn) { +Opcode ARM_Disasm::Decode11(uint32_t insn) { uint8_t bit25 = (insn >> 25) & 0x1; if (bit25 == 0) { // LDC, SDC @@ -882,7 +844,7 @@ Opcode ARM_Disasm::decode11(uint32_t insn) { return OP_MCR; } -Opcode ARM_Disasm::decode_mul(uint32_t insn) { +Opcode ARM_Disasm::DecodeMUL(uint32_t insn) { uint8_t bit24 = (insn >> 24) & 0x1; if (bit24 != 0) { // This is an unexpected bit pattern. Create an undefined @@ -916,7 +878,7 @@ Opcode ARM_Disasm::decode_mul(uint32_t insn) { return OP_SMLAL; } -Opcode ARM_Disasm::decode_ldrh(uint32_t insn) { +Opcode ARM_Disasm::DecodeLDRH(uint32_t insn) { uint8_t is_load = (insn >> 20) & 0x1; uint8_t bits_65 = (insn >> 5) & 0x3; if (is_load) { @@ -946,7 +908,7 @@ Opcode ARM_Disasm::decode_ldrh(uint32_t insn) { return OP_STRH; } -Opcode ARM_Disasm::decode_alu(uint32_t insn) { +Opcode ARM_Disasm::DecodeALU(uint32_t insn) { uint8_t is_immed = (insn >> 25) & 0x1; uint8_t opcode = (insn >> 21) & 0xf; uint8_t bit_s = (insn >> 20) & 1; diff --git a/src/core/arm/disassembler/arm_disasm.h b/src/core/arm/disassembler/arm_disasm.h index 9600e2ade..f94bd4669 100644 --- a/src/core/arm/disassembler/arm_disasm.h +++ b/src/core/arm/disassembler/arm_disasm.h @@ -1,9 +1,9 @@ // Copyright 2006 The Android Open Source Project -#ifndef ARMDIS_H -#define ARMDIS_H +#pragma once -#include <stdint.h> +#include <cstdint> +#include <string> // Note: this list of opcodes must match the list used to initialize // the opflags[] array in opcode.cpp. @@ -109,38 +109,33 @@ enum Opcode { class ARM_Disasm { public: - static char *disasm(uint32_t addr, uint32_t insn, char *buffer); - static Opcode decode(uint32_t insn); + static std::string Disassemble(uint32_t addr, uint32_t insn); + static Opcode Decode(uint32_t insn); private: - static Opcode decode00(uint32_t insn); - static Opcode decode01(uint32_t insn); - static Opcode decode10(uint32_t insn); - static Opcode decode11(uint32_t insn); - static Opcode decode_mul(uint32_t insn); - static Opcode decode_ldrh(uint32_t insn); - static Opcode decode_alu(uint32_t insn); + static Opcode Decode00(uint32_t insn); + static Opcode Decode01(uint32_t insn); + static Opcode Decode10(uint32_t insn); + static Opcode Decode11(uint32_t insn); + static Opcode DecodeMUL(uint32_t insn); + static Opcode DecodeLDRH(uint32_t insn); + static Opcode DecodeALU(uint32_t insn); - static char *disasm_alu(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_branch(uint32_t addr, Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_bx(uint32_t insn, char *ptr); - static char *disasm_bkpt(uint32_t insn, char *ptr); - static char *disasm_clz(uint32_t insn, char *ptr); - static char *disasm_memblock(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_mem(uint32_t insn, char *ptr); - static char *disasm_memhalf(uint32_t insn, char *ptr); - static char *disasm_mcr(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_mla(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_umlal(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_mul(Opcode opcode, uint32_t insn, char *ptr); - static char *disasm_mrs(uint32_t insn, char *ptr); - static char *disasm_msr(uint32_t insn, char *ptr); - static char *disasm_pld(uint32_t insn, char *ptr); - static char *disasm_swi(uint32_t insn, char *ptr); - static char *disasm_swp(Opcode opcode, uint32_t insn, char *ptr); + static std::string DisassembleALU(Opcode opcode, uint32_t insn); + static std::string DisassembleBranch(uint32_t addr, Opcode opcode, uint32_t insn); + static std::string DisassembleBX(uint32_t insn); + static std::string DisassembleBKPT(uint32_t insn); + static std::string DisassembleCLZ(uint32_t insn); + static std::string DisassembleMemblock(Opcode opcode, uint32_t insn); + static std::string DisassembleMem(uint32_t insn); + static std::string DisassembleMemHalf(uint32_t insn); + static std::string DisassembleMCR(Opcode opcode, uint32_t insn); + static std::string DisassembleMLA(Opcode opcode, uint32_t insn); + static std::string DisassembleUMLAL(Opcode opcode, uint32_t insn); + static std::string DisassembleMUL(Opcode opcode, uint32_t insn); + static std::string DisassembleMRS(uint32_t insn); + static std::string DisassembleMSR(uint32_t insn); + static std::string DisassemblePLD(uint32_t insn); + static std::string DisassembleSWI(uint32_t insn); + static std::string DisassembleSWP(Opcode opcode, uint32_t insn); }; - -extern char *disasm_insn_thumb(uint32_t pc, uint32_t insn1, uint32_t insn2, char *result); -extern Opcode decode_insn_thumb(uint32_t given); - -#endif /* ARMDIS_H */ diff --git a/src/core/arm/disassembler/load_symbol_map.cpp b/src/core/arm/disassembler/load_symbol_map.cpp index b4a5429a2..0f384ad3e 100644 --- a/src/core/arm/disassembler/load_symbol_map.cpp +++ b/src/core/arm/disassembler/load_symbol_map.cpp @@ -6,7 +6,6 @@ #include <vector> #include "common/symbols.h" -#include "common/common_types.h" #include "common/file_util.h" #include "core/arm/disassembler/load_symbol_map.h" diff --git a/src/core/arm/interpreter/armsupp.cpp b/src/core/arm/interpreter/armsupp.cpp index 219ba78ce..3d3545c65 100644 --- a/src/core/arm/interpreter/armsupp.cpp +++ b/src/core/arm/interpreter/armsupp.cpp @@ -17,6 +17,7 @@ //#include <util.h> +#include <string> #include "core/arm/interpreter/armdefs.h" #include "core/arm/interpreter/armemu.h" #include "core/hle/coprocessor.h" @@ -870,12 +871,8 @@ ARMul_CDP (ARMul_State * state, ARMword instr) void ARMul_UndefInstr (ARMul_State * state, ARMword instr) { - /*SKYEYE_LOG_IN_CLR(RED, "In %s, line = %d, undef instr: 0x%x\n", - __func__, __LINE__, instr);*/ - char buff[512]; - ARM_Disasm disasm = ARM_Disasm(); - disasm.disasm(state->pc, instr, buff); - ERROR_LOG(ARM11, "Undefined instruction!! Disasm: %s Opcode: 0x%x", buff, instr); + std::string disasm = ARM_Disasm::Disassemble(state->pc, instr); + ERROR_LOG(ARM11, "Undefined instruction!! Disasm: %s Opcode: 0x%x", disasm.c_str(), instr); ARMul_Abort (state, ARMul_UndefinedInstrV); } diff --git a/src/core/core.cpp b/src/core/core.cpp index f21801e52..01d4f0afa 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -3,15 +3,9 @@ // Refer to the license.txt file included. #include "common/common_types.h" -#include "common/log.h" -#include "common/symbols.h" - -#include "video_core/video_core.h" #include "core/core.h" -#include "core/mem_map.h" #include "core/hw/hw.h" -#include "core/hw/gpu.h" #include "core/arm/disassembler/arm_disasm.h" #include "core/arm/interpreter/arm_interpreter.h" diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index c30e36732..25fccce76 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -5,9 +5,9 @@ #include <vector> #include <cstdio> #include <atomic> +#include <mutex> #include "common/msg_handler.h" -#include "common/std_mutex.h" #include "common/chunk_file.h" #include "core/core_timing.h" diff --git a/src/core/hle/config_mem.cpp b/src/core/hle/config_mem.cpp index 8c898b265..a45e61427 100644 --- a/src/core/hle/config_mem.cpp +++ b/src/core/hle/config_mem.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include "common/common_types.h" -#include "common/log.h" #include "core/hle/config_mem.h" diff --git a/src/core/hle/coprocessor.cpp b/src/core/hle/coprocessor.cpp index 9a5b0deda..1eb33eb86 100644 --- a/src/core/hle/coprocessor.cpp +++ b/src/core/hle/coprocessor.cpp @@ -5,7 +5,6 @@ #include "core/hle/coprocessor.h" #include "core/hle/hle.h" #include "core/mem_map.h" -#include "core/core.h" namespace HLE { diff --git a/src/core/hle/hle.cpp b/src/core/hle/hle.cpp index 53cda4a61..b03894ad7 100644 --- a/src/core/hle/hle.cpp +++ b/src/core/hle/hle.cpp @@ -6,7 +6,6 @@ #include "core/mem_map.h" #include "core/hle/hle.h" -#include "core/hle/svc.h" #include "core/hle/kernel/thread.h" #include "core/hle/service/service.h" diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp index 1596367c3..20536f40f 100644 --- a/src/core/hle/kernel/archive.cpp +++ b/src/core/hle/kernel/archive.cpp @@ -7,7 +7,6 @@ #include "core/file_sys/archive.h" #include "core/hle/service/service.h" -#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/archive.h" //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index a4a258875..e56f1879e 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -2,8 +2,6 @@ // Licensed under GPLv2 // Refer to the license.txt file included. -#include <string.h> - #include "common/common.h" #include "core/core.h" diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 8bd9ca1a1..33c0b2a47 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -3,10 +3,8 @@ // Refer to the license.txt file included. #include <algorithm> -#include <cstdio> #include <list> #include <map> -#include <string> #include <vector> #include "common/common.h" @@ -15,7 +13,6 @@ #include "core/core.h" #include "core/mem_map.h" #include "core/hle/hle.h" -#include "core/hle/svc.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/thread.h" diff --git a/src/core/hle/service/fs.cpp b/src/core/hle/service/fs.cpp index 5eabf36ad..9e1998b0f 100644 --- a/src/core/hle/service/fs.cpp +++ b/src/core/hle/service/fs.cpp @@ -4,8 +4,6 @@ #include "common/common.h" -#include "core/loader/loader.h" -#include "core/hle/hle.h" #include "core/hle/service/fs.h" #include "core/hle/kernel/archive.h" diff --git a/src/core/hle/service/gsp.cpp b/src/core/hle/service/gsp.cpp index 46c5a8ddd..accbe84e3 100644 --- a/src/core/hle/service/gsp.cpp +++ b/src/core/hle/service/gsp.cpp @@ -7,7 +7,6 @@ #include "common/bit_field.h" #include "core/mem_map.h" -#include "core/hle/hle.h" #include "core/hle/kernel/event.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/service/gsp.h" diff --git a/src/core/hle/service/hid.cpp b/src/core/hle/service/hid.cpp index 4e470795f..b6ec1b8ff 100644 --- a/src/core/hle/service/hid.cpp +++ b/src/core/hle/service/hid.cpp @@ -14,7 +14,128 @@ namespace HID_User { -Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service +// Handle to shared memory region designated to HID_User service +static Handle shared_mem = 0; + +// Event handles +static Handle event_pad_or_touch_1 = 0; +static Handle event_pad_or_touch_2 = 0; +static Handle event_accelerometer = 0; +static Handle event_gyroscope = 0; +static Handle event_debug_pad = 0; + +// Next Pad state update information +static PadState next_state = {{0}}; +static u32 next_index = 0; +static s16 next_circle_x = 0; +static s16 next_circle_y = 0; + +/** + * Gets a pointer to the PadData structure inside HID shared memory + */ +static inline PadData* GetPadData() { + if (0 == shared_mem) + return nullptr; + + return reinterpret_cast<PadData*>(Kernel::GetSharedMemoryPointer(shared_mem, 0)); +} + +/** + * Circle Pad from keys. + * + * This is implemented as "pushed all the way to an edge (max) or centered (0)". + * + * Indicate the circle pad is pushed completely to the edge in 1 of 8 directions. + */ +void UpdateNextCirclePadState() { + static const s16 max_value = 0x9C; + next_circle_x = next_state.circle_left ? -max_value : 0x0; + next_circle_x += next_state.circle_right ? max_value : 0x0; + next_circle_y = next_state.circle_down ? -max_value : 0x0; + next_circle_y += next_state.circle_up ? max_value : 0x0; +} + +/** + * Sets a Pad state (button or button combo) as pressed + */ +void PadButtonPress(PadState pad_state) { + next_state.hex |= pad_state.hex; + UpdateNextCirclePadState(); +} + +/** + * Sets a Pad state (button or button combo) as released + */ +void PadButtonRelease(PadState pad_state) { + next_state.hex &= ~pad_state.hex; + UpdateNextCirclePadState(); +} + +/** + * Called after all Pad changes to be included in this update have been made, + * including both Pad key changes and analog circle Pad changes. + */ +void PadUpdateComplete() { + PadData* pad_data = GetPadData(); + + // Update PadData struct + pad_data->current_state.hex = next_state.hex; + pad_data->index = next_index; + next_index = (next_index + 1) % pad_data->entries.size(); + + // Get the previous Pad state + u32 last_entry_index = (pad_data->index - 1) % pad_data->entries.size(); + PadState old_state = pad_data->entries[last_entry_index].current_state; + + // Compute bitmask with 1s for bits different from the old state + PadState changed; + changed.hex = (next_state.hex ^ old_state.hex); + + // Compute what was added + PadState additions; + additions.hex = changed.hex & next_state.hex; + + // Compute what was removed + PadState removals; + removals.hex = changed.hex & old_state.hex; + + // Get the current Pad entry + PadDataEntry* current_pad_entry = &pad_data->entries[pad_data->index]; + + // Update entry properties + current_pad_entry->current_state.hex = next_state.hex; + current_pad_entry->delta_additions.hex = additions.hex; + current_pad_entry->delta_removals.hex = removals.hex; + + // Set circle Pad + current_pad_entry->circle_pad_x = next_circle_x; + current_pad_entry->circle_pad_y = next_circle_y; + + // If we just updated index 0, provide a new timestamp + if (pad_data->index == 0) { + pad_data->index_reset_ticks_previous = pad_data->index_reset_ticks; + pad_data->index_reset_ticks = (s64)Core::g_app_core->GetTicks(); + } + + // Signal both handles when there's an update to Pad or touch + Kernel::SignalEvent(event_pad_or_touch_1); + Kernel::SignalEvent(event_pad_or_touch_2); +} + + +// TODO(peachum): +// Add a method for setting analog input from joystick device for the circle Pad. +// +// This method should: +// * Be called after both PadButton<Press, Release>(). +// * Be called before PadUpdateComplete() +// * Set current PadEntry.circle_pad_<axis> using analog data +// * Set PadData.raw_circle_pad_data +// * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_x >= 41 +// * Set PadData.current_state.circle_up = 1 if current PadEntry.circle_pad_y >= 41 +// * Set PadData.current_state.circle_left = 1 if current PadEntry.circle_pad_x <= -41 +// * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_y <= -41 + /** * HID_User::GetIPCHandles service function @@ -34,12 +155,12 @@ void GetIPCHandles(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); cmd_buff[1] = 0; // No error - cmd_buff[3] = g_shared_mem; - cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventA"); - cmd_buff[5] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventB"); - cmd_buff[6] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventC"); - cmd_buff[7] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); - cmd_buff[8] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventD"); + cmd_buff[3] = shared_mem; + cmd_buff[4] = event_pad_or_touch_1; + cmd_buff[5] = event_pad_or_touch_2; + cmd_buff[6] = event_accelerometer; + cmd_buff[7] = event_gyroscope; + cmd_buff[8] = event_debug_pad; DEBUG_LOG(KERNEL, "called"); } @@ -57,11 +178,19 @@ const Interface::FunctionInfo FunctionTable[] = { {0x00170000, nullptr, "GetSoundVolume"}, }; + //////////////////////////////////////////////////////////////////////////////////////////////////// // Interface class Interface::Interface() { - g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object + shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object + + // Create event handles + event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch1"); + event_pad_or_touch_2 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch2"); + event_accelerometer = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventAccelerometer"); + event_gyroscope = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); + event_debug_pad = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventDebugPad"); Register(FunctionTable, ARRAY_SIZE(FunctionTable)); } diff --git a/src/core/hle/service/hid.h b/src/core/hle/service/hid.h index b17fcfa86..a077e25cd 100644 --- a/src/core/hle/service/hid.h +++ b/src/core/hle/service/hid.h @@ -5,15 +5,101 @@ #pragma once #include "core/hle/service/service.h" +#include "common/bit_field.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace HID_User -// This service is used for interfacing to physical user controls... perhaps "Human Interface -// Devices"? Uses include game pad controls, accelerometers, gyroscopes, etc. +// This service is used for interfacing to physical user controls. +// Uses include game pad controls, touchscreen, accelerometers, gyroscopes, and debug pad. namespace HID_User { +/** + * Structure of a Pad controller state. + */ +struct PadState { + union { + u32 hex; + + BitField<0, 1, u32> a; + BitField<1, 1, u32> b; + BitField<2, 1, u32> select; + BitField<3, 1, u32> start; + BitField<4, 1, u32> right; + BitField<5, 1, u32> left; + BitField<6, 1, u32> up; + BitField<7, 1, u32> down; + BitField<8, 1, u32> r; + BitField<9, 1, u32> l; + BitField<10, 1, u32> x; + BitField<11, 1, u32> y; + + BitField<28, 1, u32> circle_right; + BitField<29, 1, u32> circle_left; + BitField<30, 1, u32> circle_up; + BitField<31, 1, u32> circle_down; + }; +}; + +/** + * Structure of a single entry in the PadData's Pad state history array. + */ +struct PadDataEntry { + PadState current_state; + PadState delta_additions; + PadState delta_removals; + + s16 circle_pad_x; + s16 circle_pad_y; +}; + +/** + * Structure of all data related to the 3DS Pad. + */ +struct PadData { + s64 index_reset_ticks; + s64 index_reset_ticks_previous; + u32 index; // the index of the last updated Pad state history element + + u32 pad1; + u32 pad2; + + PadState current_state; // same as entries[index].current_state + u32 raw_circle_pad_data; + + u32 pad3; + + std::array<PadDataEntry, 8> entries; // Pad state history +}; + +// Pre-defined PadStates for single button presses +const PadState PAD_NONE = {{0}}; +const PadState PAD_A = {{1u << 0}}; +const PadState PAD_B = {{1u << 1}}; +const PadState PAD_SELECT = {{1u << 2}}; +const PadState PAD_START = {{1u << 3}}; +const PadState PAD_RIGHT = {{1u << 4}}; +const PadState PAD_LEFT = {{1u << 5}}; +const PadState PAD_UP = {{1u << 6}}; +const PadState PAD_DOWN = {{1u << 7}}; +const PadState PAD_R = {{1u << 8}}; +const PadState PAD_L = {{1u << 9}}; +const PadState PAD_X = {{1u << 10}}; +const PadState PAD_Y = {{1u << 11}}; +const PadState PAD_CIRCLE_RIGHT = {{1u << 28}}; +const PadState PAD_CIRCLE_LEFT = {{1u << 29}}; +const PadState PAD_CIRCLE_UP = {{1u << 30}}; +const PadState PAD_CIRCLE_DOWN = {{1u << 31}}; + +// Methods for updating the HID module's state +void PadButtonPress(PadState pad_state); +void PadButtonRelease(PadState pad_state); +void PadUpdateComplete(); + +/** + * HID service interface. + */ class Interface : public Service::Interface { public: diff --git a/src/core/hle/service/ndm.cpp b/src/core/hle/service/ndm.cpp index 48755b6a7..f6af0a153 100644 --- a/src/core/hle/service/ndm.cpp +++ b/src/core/hle/service/ndm.cpp @@ -2,8 +2,6 @@ // Licensed under GPLv2 // Refer to the license.txt file included. -#include "common/log.h" - #include "core/hle/hle.h" #include "core/hle/service/ndm.h" diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 00ac1c9c6..9eb1726aa 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -3,11 +3,8 @@ // Refer to the license.txt file included. #include "common/common.h" -#include "common/log.h" #include "common/string_util.h" -#include "core/hle/hle.h" - #include "core/hle/service/service.h" #include "core/hle/service/apt.h" #include "core/hle/service/fs.h" @@ -16,8 +13,6 @@ #include "core/hle/service/ndm.h" #include "core/hle/service/srv.h" -#include "core/hle/kernel/kernel.h" - namespace Service { Manager* g_manager = nullptr; ///< Service manager diff --git a/src/core/hle/service/srv.cpp b/src/core/hle/service/srv.cpp index 23be3cf2c..eb2c73f93 100644 --- a/src/core/hle/service/srv.cpp +++ b/src/core/hle/service/srv.cpp @@ -4,7 +4,6 @@ #include "core/hle/hle.h" #include "core/hle/service/srv.h" -#include "core/hle/service/service.h" #include "core/hle/kernel/event.h" //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index bdcfae6f5..490e05cde 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include <map> -#include <string> #include "common/string_util.h" #include "common/symbols.h" @@ -12,13 +11,11 @@ #include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/event.h" -#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/mutex.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/kernel/thread.h" #include "core/hle/function_wrappers.h" -#include "core/hle/svc.h" #include "core/hle/service/service.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -221,7 +218,7 @@ Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 p TSymbol symbol = Symbols::GetSymbol(entry_point); name = symbol.name; } else { - name = StringFromFormat("unknown-%08x", entry_point); + name = Common::StringFromFormat("unknown-%08x", entry_point); } Handle thread = Kernel::CreateThread(name.c_str(), entry_point, priority, arg, processor_id, diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp index 8709b8eb7..2e0943776 100644 --- a/src/core/hw/gpu.cpp +++ b/src/core/hw/gpu.cpp @@ -3,13 +3,11 @@ // Refer to the license.txt file included. #include "common/common_types.h" -#include "common/log.h" #include "core/core.h" #include "core/mem_map.h" #include "core/hle/hle.h" -#include "core/hle/kernel/thread.h" #include "core/hle/service/gsp.h" #include "core/hw/gpu.h" diff --git a/src/core/hw/hw.cpp b/src/core/hw/hw.cpp index ed70486e6..efd94f147 100644 --- a/src/core/hw/hw.cpp +++ b/src/core/hw/hw.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include "common/common_types.h" -#include "common/log.h" #include "core/hw/hw.h" #include "core/hw/gpu.h" diff --git a/src/core/hw/ndma.cpp b/src/core/hw/ndma.cpp index f6aa72d16..158241fd6 100644 --- a/src/core/hw/ndma.cpp +++ b/src/core/hw/ndma.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include "common/common_types.h" -#include "common/log.h" #include "core/hw/ndma.h" diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 365f5a277..577a2297a 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -5,7 +5,6 @@ #include <memory> #include "core/file_sys/archive_romfs.h" -#include "core/loader/loader.h" #include "core/loader/elf.h" #include "core/loader/ncch.h" #include "core/hle/kernel/archive.h" @@ -26,22 +25,23 @@ FileType IdentifyFile(const std::string &filename) { ERROR_LOG(LOADER, "invalid filename %s", filename.c_str()); return FileType::Error; } - std::string extension = filename.size() >= 5 ? filename.substr(filename.size() - 4) : ""; - if (!strcasecmp(extension.c_str(), ".elf")) { - return FileType::ELF; // TODO(bunnei): Do some filetype checking :p - } - else if (!strcasecmp(extension.c_str(), ".axf")) { - return FileType::ELF; // TODO(bunnei): Do some filetype checking :p - } - else if (!strcasecmp(extension.c_str(), ".cxi")) { - return FileType::CXI; // TODO(bunnei): Do some filetype checking :p - } - else if (!strcasecmp(extension.c_str(), ".cci")) { - return FileType::CCI; // TODO(bunnei): Do some filetype checking :p - } - else if (!strcasecmp(extension.c_str(), ".bin")) { - return FileType::BIN; // TODO(bunnei): Do some filetype checking :p + size_t extension_loc = filename.find_last_of('.'); + if (extension_loc == std::string::npos) + return FileType::Unknown; + std::string extension = Common::ToLower(filename.substr(extension_loc)); + + // TODO(bunnei): Do actual filetype checking instead of naively checking the extension + if (extension == ".elf") { + return FileType::ELF; + } else if (extension == ".axf") { + return FileType::ELF; + } else if (extension == ".cxi") { + return FileType::CXI; + } else if (extension == ".cci") { + return FileType::CCI; + } else if (extension == ".bin") { + return FileType::BIN; } return FileType::Unknown; } diff --git a/src/core/mem_map.cpp b/src/core/mem_map.cpp index 14fc01471..cf12f24d9 100644 --- a/src/core/mem_map.cpp +++ b/src/core/mem_map.cpp @@ -6,7 +6,6 @@ #include "common/mem_arena.h" #include "core/mem_map.h" -#include "core/core.h" //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/mem_map_funcs.cpp b/src/core/mem_map_funcs.cpp index 391b75fc2..f510df835 100644 --- a/src/core/mem_map_funcs.cpp +++ b/src/core/mem_map_funcs.cpp @@ -8,7 +8,6 @@ #include "core/mem_map.h" #include "core/hw/hw.h" -#include "hle/hle.h" #include "hle/config_mem.h" namespace Memory { diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 4ab1ccb2c..bc1683cb5 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -77,7 +77,7 @@ void RendererOpenGL::SwapBuffers() { // // TODO(princesspeachum): (related to above^) this should only be called when there's new data, not every frame. // Currently this uploads data that shouldn't have changed. - common::Rect framebuffer_size(0, 0, resolution_width, resolution_height); + Common::Rect framebuffer_size(0, 0, resolution_width, resolution_height); RenderXFB(framebuffer_size, framebuffer_size); // XFB->Window copy @@ -113,7 +113,7 @@ void RendererOpenGL::FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info * @param src_rect Source rectangle in XFB to copy * @param dst_rect Destination rectangle in output framebuffer to copy to */ -void RendererOpenGL::RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect) { +void RendererOpenGL::RenderXFB(const Common::Rect& src_rect, const Common::Rect& dst_rect) { const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0]; const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1]; const u32 active_fb_top = (framebuffer_top.active_fb == 1) diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index eac91df51..0d25b2a52 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -27,7 +27,7 @@ public: * @param src_rect Source rectangle in XFB to copy * @param dst_rect Destination rectangle in output framebuffer to copy to */ - void RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect); + void RenderXFB(const Common::Rect& src_rect, const Common::Rect& dst_rect); /** * Set the emulator window to use for renderer diff --git a/src/video_core/video_core.h b/src/video_core/video_core.h index d227b6aa4..5e8129b5a 100644 --- a/src/video_core/video_core.h +++ b/src/video_core/video_core.h @@ -27,6 +27,7 @@ static const int kScreenBottomHeight = 240; ///< 3DS bottom screen height extern RendererBase* g_renderer; ///< Renderer plugin extern int g_current_frame; ///< Current frame +extern EmuWindow* g_emu_window; ///< Emu window /// Start the video core void Start(); |