Use one InputImpl.hpp header for all implementations

There were 6 copies of InputImpl.hpp for Windows, macOS, Linux,
Linux DRM, iOS, and Android. All but Windows and Linux DRM were
identical so there's already an easy opportunity for consolidation.

The Windows header included some additional private static functions
and data. An equivalent way of expressing this is via more data and
functions instead the anonymous namespace of Win32/InputImpl.cpp so
that's what I did. To fix some issues with functions being used
before they're defined I moved number of functions into the anonymous
namespace of that file which resulted in adding `sf::` in lots of places.

InputImplUDev.hpp used by the DRM backend was trickier because includes
WindowImplDRM as a friend. This creates a weird dependency graph where a
distant class can call private functions that mutate private state. I
kept the definitions of those functions inside DRM/InputImpl.cpp while
moving their declaration to WindowImplDRM.cpp which is the only file
where they're used. This is a bit odd but it avoids using the
preprocessor and ensures that only the file that needs these functions
can call them.

In the end I was able to delete all 6 separate copies of InputImpl.hpp
and consolidate them into src/SFML/Window/InputImpl.hpp saving nearly
1,000 lines of code.

Because `sf::priv::InputImpl` is merely a set of static functions I
converted this class to a simple namespace. The use of a namespace is
what ultimately allowed me to untangle the DRM functions that were not
present in other implementations
This commit is contained in:
Chris Thrasher 2023-09-19 18:36:07 -06:00
parent 56784e198b
commit cbec344876
16 changed files with 733 additions and 1667 deletions

View File

@ -25,7 +25,7 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Android/InputImpl.hpp>
#include <SFML/Window/InputImpl.hpp>
#include <SFML/System/Android/Activity.hpp>
#include <SFML/System/Err.hpp>
@ -36,41 +36,50 @@
#include <ostream>
namespace sf::priv
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key /* key */)
bool isKeyPressed(Keyboard::Key /* key */)
{
// Not applicable
return false;
}
bool InputImpl::isKeyPressed(Keyboard::Scancode /* codes */)
////////////////////////////////////////////////////////////
bool isKeyPressed(Keyboard::Scancode /* codes */)
{
// Not applicable
return false;
}
Keyboard::Key InputImpl::localize(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
Keyboard::Key localize(Keyboard::Scancode /* code */)
{
// Not applicable
return Keyboard::Unknown;
}
Keyboard::Scancode InputImpl::delocalize(Keyboard::Key /* key */)
////////////////////////////////////////////////////////////
Keyboard::Scancode delocalize(Keyboard::Key /* key */)
{
// Not applicable
return Keyboard::Scan::Unknown;
}
String InputImpl::getDescription(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
String getDescription(Keyboard::Scancode /* code */)
{
// Not applicable
return "";
}
////////////////////////////////////////////////////////////
void InputImpl::setVirtualKeyboardVisible(bool visible)
void setVirtualKeyboardVisible(bool visible)
{
// todo: Check if the window is active
@ -153,8 +162,9 @@ void InputImpl::setVirtualKeyboardVisible(bool visible)
lJavaVM->DetachCurrentThread();
}
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button button)
bool isMouseButtonPressed(Mouse::Button button)
{
ALooper_pollAll(0, nullptr, nullptr, nullptr);
@ -166,7 +176,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition()
Vector2i getMousePosition()
{
ALooper_pollAll(0, nullptr, nullptr, nullptr);
@ -178,28 +188,28 @@ Vector2i InputImpl::getMousePosition()
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition(const WindowBase& /* relativeTo */)
Vector2i getMousePosition(const WindowBase& /* relativeTo */)
{
return getMousePosition();
}
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& /* position */)
void setMousePosition(const Vector2i& /* position */)
{
// Injecting events is impossible on Android
}
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& /* relativeTo */)
void setMousePosition(const Vector2i& position, const WindowBase& /* relativeTo */)
{
setMousePosition(position);
}
////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int finger)
bool isTouchDown(unsigned int finger)
{
ALooper_pollAll(0, nullptr, nullptr, nullptr);
@ -211,7 +221,7 @@ bool InputImpl::isTouchDown(unsigned int finger)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger)
Vector2i getTouchPosition(unsigned int finger)
{
ALooper_pollAll(0, nullptr, nullptr, nullptr);
@ -223,9 +233,9 @@ Vector2i InputImpl::getTouchPosition(unsigned int finger)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger, const WindowBase& /* relativeTo */)
Vector2i getTouchPosition(unsigned int finger, const WindowBase& /* relativeTo */)
{
return getTouchPosition(finger);
}
} // namespace sf::priv
} // namespace sf::priv::InputImpl

View File

@ -1,177 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief iOS implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
};
} // namespace sf::priv

View File

@ -60,7 +60,6 @@ if(SFML_OS_WINDOWS)
${SRCROOT}/Win32/ClipboardImpl.hpp
${SRCROOT}/Win32/ClipboardImpl.cpp
${SRCROOT}/Win32/InputImpl.cpp
${SRCROOT}/Win32/InputImpl.hpp
${SRCROOT}/Win32/JoystickImpl.cpp
${SRCROOT}/Win32/JoystickImpl.hpp
${SRCROOT}/Win32/SensorImpl.hpp
@ -97,8 +96,7 @@ elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD OR SFML_OS_OPENBSD OR SFML_OS_NETBSD)
${SRCROOT}/DRM/ClipboardImpl.cpp
${SRCROOT}/Unix/SensorImpl.cpp
${SRCROOT}/Unix/SensorImpl.hpp
${SRCROOT}/DRM/InputImplUDev.cpp
${SRCROOT}/DRM/InputImplUDev.hpp
${SRCROOT}/DRM/InputImpl.cpp
${SRCROOT}/DRM/VideoModeImpl.cpp
${SRCROOT}/DRM/DRMContext.cpp
${SRCROOT}/DRM/DRMContext.hpp
@ -112,7 +110,6 @@ elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD OR SFML_OS_OPENBSD OR SFML_OS_NETBSD)
${SRCROOT}/Unix/ClipboardImpl.hpp
${SRCROOT}/Unix/ClipboardImpl.cpp
${SRCROOT}/Unix/InputImpl.cpp
${SRCROOT}/Unix/InputImpl.hpp
${SRCROOT}/Unix/KeyboardImpl.hpp
${SRCROOT}/Unix/KeyboardImpl.cpp
${SRCROOT}/Unix/KeySymToKeyMapping.hpp
@ -178,7 +175,6 @@ elseif(SFML_OS_MACOS)
${SRCROOT}/macOS/ClipboardImpl.hpp
${SRCROOT}/macOS/ClipboardImpl.mm
${SRCROOT}/macOS/InputImpl.mm
${SRCROOT}/macOS/InputImpl.hpp
${SRCROOT}/macOS/HIDInputManager.hpp
${SRCROOT}/macOS/HIDInputManager.mm
${SRCROOT}/macOS/HIDJoystickManager.hpp
@ -230,7 +226,6 @@ elseif(SFML_OS_IOS)
${SRCROOT}/iOS/EaglContext.mm
${SRCROOT}/iOS/EaglContext.hpp
${SRCROOT}/iOS/InputImpl.mm
${SRCROOT}/iOS/InputImpl.hpp
${SRCROOT}/iOS/JoystickImpl.mm
${SRCROOT}/iOS/JoystickImpl.hpp
${SRCROOT}/iOS/SensorImpl.mm
@ -262,7 +257,6 @@ elseif(SFML_OS_ANDROID)
${SRCROOT}/Android/WindowImplAndroid.hpp
${SRCROOT}/Android/WindowImplAndroid.cpp
${SRCROOT}/Android/VideoModeImpl.cpp
${SRCROOT}/Android/InputImpl.hpp
${SRCROOT}/Android/InputImpl.cpp
${SRCROOT}/Android/JoystickImpl.hpp
${SRCROOT}/Android/JoystickImpl.cpp

View File

@ -25,7 +25,8 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/DRM/InputImplUDev.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/InputImpl.hpp>
#include <SFML/System/Err.hpp>
@ -566,10 +567,10 @@ void update()
}
} // namespace
namespace sf::priv
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key key)
bool isKeyPressed(Keyboard::Key key)
{
const std::lock_guard lock(inputMutex);
if ((key < 0) || (key >= static_cast<int>(keyMap.size())))
@ -579,8 +580,9 @@ bool InputImpl::isKeyPressed(Keyboard::Key key)
return keyMap[static_cast<std::size_t>(key)];
}
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Scancode /* code */)
bool isKeyPressed(Keyboard::Scancode /* code */)
{
// TODO: not implemented
err() << "sf::Keyboard::isKeyPressed(Keyboard::Scancode) is not implemented for DRM." << std::endl;
@ -589,7 +591,7 @@ bool InputImpl::isKeyPressed(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
Keyboard::Key InputImpl::localize(Keyboard::Scancode /* code */)
Keyboard::Key localize(Keyboard::Scancode /* code */)
{
// TODO: not implemented
err() << "sf::Keyboard::localize is not implemented for DRM." << std::endl;
@ -598,7 +600,7 @@ Keyboard::Key InputImpl::localize(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
Keyboard::Scancode InputImpl::delocalize(Keyboard::Key /* key */)
Keyboard::Scancode delocalize(Keyboard::Key /* key */)
{
// TODO: not implemented
err() << "sf::Keyboard::delocalize is not implemented for DRM." << std::endl;
@ -607,7 +609,7 @@ Keyboard::Scancode InputImpl::delocalize(Keyboard::Key /* key */)
////////////////////////////////////////////////////////////
String InputImpl::getDescription(Keyboard::Scancode /* code */)
String getDescription(Keyboard::Scancode /* code */)
{
// TODO: not implemented
err() << "sf::Keyboard::getDescription is not implemented for DRM." << std::endl;
@ -616,14 +618,14 @@ String InputImpl::getDescription(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
void InputImpl::setVirtualKeyboardVisible(bool /*visible*/)
void setVirtualKeyboardVisible(bool /*visible*/)
{
// Not applicable
}
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button button)
bool isMouseButtonPressed(Mouse::Button button)
{
const std::lock_guard lock(inputMutex);
if ((button < 0) || (button >= static_cast<int>(mouseMap.size())))
@ -635,7 +637,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition()
Vector2i getMousePosition()
{
const std::lock_guard lock(inputMutex);
return mousePos;
@ -643,14 +645,14 @@ Vector2i InputImpl::getMousePosition()
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition(const WindowBase& /*relativeTo*/)
Vector2i getMousePosition(const WindowBase& /*relativeTo*/)
{
return getMousePosition();
}
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position)
void setMousePosition(const Vector2i& position)
{
const std::lock_guard lock(inputMutex);
mousePos = position;
@ -658,14 +660,14 @@ void InputImpl::setMousePosition(const Vector2i& position)
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& /*relativeTo*/)
void setMousePosition(const Vector2i& position, const WindowBase& /*relativeTo*/)
{
setMousePosition(position);
}
////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int finger)
bool isTouchDown(unsigned int finger)
{
return std::any_of(touchSlots.cbegin(),
touchSlots.cend(),
@ -674,7 +676,7 @@ bool InputImpl::isTouchDown(unsigned int finger)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger)
Vector2i getTouchPosition(unsigned int finger)
{
for (const auto& slot : touchSlots)
{
@ -687,14 +689,14 @@ Vector2i InputImpl::getTouchPosition(unsigned int finger)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger, const WindowBase& /*relativeTo*/)
Vector2i getTouchPosition(unsigned int finger, const WindowBase& /*relativeTo*/)
{
return getTouchPosition(finger);
}
////////////////////////////////////////////////////////////
bool InputImpl::checkEvent(sf::Event& event)
bool checkEvent(sf::Event& event)
{
const std::lock_guard lock(inputMutex);
if (!eventQueue.empty())
@ -728,7 +730,7 @@ bool InputImpl::checkEvent(sf::Event& event)
////////////////////////////////////////////////////////////
void InputImpl::setTerminalConfig()
void setTerminalConfig()
{
const std::lock_guard lock(inputMutex);
initFileDescriptors();
@ -745,7 +747,7 @@ void InputImpl::setTerminalConfig()
////////////////////////////////////////////////////////////
void InputImpl::restoreTerminalConfig()
void restoreTerminalConfig()
{
const std::lock_guard lock(inputMutex);
initFileDescriptors();
@ -754,4 +756,4 @@ void InputImpl::restoreTerminalConfig()
tcflush(STDIN_FILENO, TCIFLUSH); // flush the buffer
}
} // namespace sf::priv
} // namespace sf::priv::InputImpl

View File

@ -1,201 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2016 Andrew Mickelson
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief Linux (DRM) implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
private:
friend class WindowImplDRM;
////////////////////////////////////////////////////////////
/// \brief Fetch input event from event queue
///
/// \return False if event queue is empty
///
////////////////////////////////////////////////////////////
static bool checkEvent(sf::Event& event);
////////////////////////////////////////////////////////////
/// \brief Backup terminal configuration and disable console feedback
///
////////////////////////////////////////////////////////////
static void setTerminalConfig();
////////////////////////////////////////////////////////////
/// \brief Restore terminal configuration from backup
///
////////////////////////////////////////////////////////////
static void restoreTerminalConfig();
};
} // namespace sf::priv

View File

@ -26,9 +26,9 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/DRM/DRMContext.hpp>
#include <SFML/Window/DRM/InputImplUDev.hpp>
#include <SFML/Window/DRM/WindowImplDRM.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/InputImpl.hpp>
#include <SFML/Window/WindowStyle.hpp>
#include <SFML/System/Err.hpp>
@ -36,6 +36,32 @@
namespace sf::priv
{
// Defined in DRM/InputImpl.cpp because they require access to that file's global state
namespace InputImpl
{
////////////////////////////////////////////////////////////
/// \brief Fetch input event from event queue
///
/// \return False if event queue is empty
///
////////////////////////////////////////////////////////////
bool checkEvent(sf::Event& event);
////////////////////////////////////////////////////////////
/// \brief Backup terminal configuration and disable console feedback
///
////////////////////////////////////////////////////////////
void setTerminalConfig();
////////////////////////////////////////////////////////////
/// \brief Restore terminal configuration from backup
///
////////////////////////////////////////////////////////////
void restoreTerminalConfig();
} // namespace InputImpl
////////////////////////////////////////////////////////////
WindowImplDRM::WindowImplDRM(WindowHandle /*handle*/)
{

View File

@ -29,19 +29,142 @@
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/InputImpl.hpp>
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) || defined(SFML_SYSTEM_OPENBSD) || \
defined(SFML_SYSTEM_NETBSD)
#if defined(SFML_USE_DRM)
#include <SFML/Window/DRM/InputImplUDev.hpp>
#else
#include <SFML/Window/Unix/InputImpl.hpp>
#endif
#elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/macOS/InputImpl.hpp>
#elif defined(SFML_SYSTEM_IOS)
#include <SFML/Window/iOS/InputImpl.hpp>
#elif defined(SFML_SYSTEM_ANDROID)
#include <SFML/Window/Android/InputImpl.hpp>
#endif
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
} // namespace sf::priv::InputImpl

View File

@ -25,8 +25,8 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/InputImpl.hpp>
#include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Unix/KeyboardImpl.hpp>
#include <SFML/Window/WindowBase.hpp>
#include <SFML/Window/WindowHandle.hpp>
@ -37,52 +37,52 @@
#include <X11/keysym.h>
namespace sf::priv
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key key)
bool isKeyPressed(Keyboard::Key key)
{
return KeyboardImpl::isKeyPressed(key);
}
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Scancode code)
bool isKeyPressed(Keyboard::Scancode code)
{
return KeyboardImpl::isKeyPressed(code);
}
////////////////////////////////////////////////////////////
Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
Keyboard::Key localize(Keyboard::Scancode code)
{
return KeyboardImpl::localize(code);
}
////////////////////////////////////////////////////////////
Keyboard::Scancode InputImpl::delocalize(Keyboard::Key key)
Keyboard::Scancode delocalize(Keyboard::Key key)
{
return KeyboardImpl::delocalize(key);
}
////////////////////////////////////////////////////////////
String InputImpl::getDescription(Keyboard::Scancode code)
String getDescription(Keyboard::Scancode code)
{
return KeyboardImpl::getDescription(code);
}
////////////////////////////////////////////////////////////
void InputImpl::setVirtualKeyboardVisible(bool /*visible*/)
void setVirtualKeyboardVisible(bool /*visible*/)
{
// Not applicable
}
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button button)
bool isMouseButtonPressed(Mouse::Button button)
{
// Open a connection with the X server
Display* display = openDisplay();
@ -119,7 +119,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition()
Vector2i getMousePosition()
{
// Open a connection with the X server
Display* display = openDisplay();
@ -143,7 +143,7 @@ Vector2i InputImpl::getMousePosition()
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition(const WindowBase& relativeTo)
Vector2i getMousePosition(const WindowBase& relativeTo)
{
const WindowHandle handle = relativeTo.getNativeHandle();
if (handle)
@ -175,7 +175,7 @@ Vector2i InputImpl::getMousePosition(const WindowBase& relativeTo)
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position)
void setMousePosition(const Vector2i& position)
{
// Open a connection with the X server
Display* display = openDisplay();
@ -189,7 +189,7 @@ void InputImpl::setMousePosition(const Vector2i& position)
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& relativeTo)
void setMousePosition(const Vector2i& position, const WindowBase& relativeTo)
{
// Open a connection with the X server
Display* display = openDisplay();
@ -207,7 +207,7 @@ void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& rel
////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int /*finger*/)
bool isTouchDown(unsigned int /*finger*/)
{
// Not applicable
return false;
@ -215,7 +215,7 @@ bool InputImpl::isTouchDown(unsigned int /*finger*/)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/)
Vector2i getTouchPosition(unsigned int /*finger*/)
{
// Not applicable
return {};
@ -223,10 +223,10 @@ Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/, const WindowBase& /*relativeTo*/)
Vector2i getTouchPosition(unsigned int /*finger*/, const WindowBase& /*relativeTo*/)
{
// Not applicable
return {};
}
} // namespace sf::priv
} // namespace sf::priv::InputImpl

View File

@ -1,177 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief Linux (X11) implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
};
} // namespace sf::priv

View File

@ -26,9 +26,9 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/InputImpl.hpp>
#include <SFML/Window/Unix/ClipboardImpl.hpp>
#include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Unix/KeyboardImpl.hpp>
#include <SFML/Window/Unix/Utils.hpp>
#include <SFML/Window/Unix/WindowImplX11.hpp>

File diff suppressed because it is too large Load Diff

View File

@ -1,194 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cstddef>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief Windows implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
private:
////////////////////////////////////////////////////////////
/// Ensure the mappings are generated from/to Key and Scancode.
///
////////////////////////////////////////////////////////////
static void ensureMappings();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
// NOLINTBEGIN(readability-identifier-naming)
static Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
static Keyboard::Key m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
// NOLINTEND(readability-identifier-naming)
};
} // namespace sf::priv

View File

@ -1,177 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief iOS implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
};
} // namespace sf::priv

View File

@ -25,56 +25,65 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/InputImpl.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Window.hpp>
#include <SFML/Window/iOS/InputImpl.hpp>
#include <SFML/Window/iOS/SFAppDelegate.hpp>
#include <SFML/System/Err.hpp>
namespace sf::priv
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key /* key */)
bool isKeyPressed(Keyboard::Key /* key */)
{
// Not applicable
return false;
}
bool InputImpl::isKeyPressed(Keyboard::Scancode /* codes */)
////////////////////////////////////////////////////////////
bool isKeyPressed(Keyboard::Scancode /* codes */)
{
// Not applicable
return false;
}
Keyboard::Key InputImpl::localize(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
Keyboard::Key localize(Keyboard::Scancode /* code */)
{
// Not applicable
return Keyboard::Unknown;
}
Keyboard::Scancode InputImpl::delocalize(Keyboard::Key /* key */)
////////////////////////////////////////////////////////////
Keyboard::Scancode delocalize(Keyboard::Key /* key */)
{
// Not applicable
return Keyboard::Scan::Unknown;
}
String InputImpl::getDescription(Keyboard::Scancode /* code */)
////////////////////////////////////////////////////////////
String getDescription(Keyboard::Scancode /* code */)
{
// Not applicable
return "";
}
////////////////////////////////////////////////////////////
void InputImpl::setVirtualKeyboardVisible(bool visible)
void setVirtualKeyboardVisible(bool visible)
{
[[SFAppDelegate getInstance] setVirtualKeyboardVisible:visible];
}
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button /* button */)
bool isMouseButtonPressed(Mouse::Button /* button */)
{
// Not applicable
return false;
@ -82,49 +91,49 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button /* button */)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition()
Vector2i getMousePosition()
{
return {};
}
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition(const WindowBase& /* relativeTo */)
Vector2i getMousePosition(const WindowBase& /* relativeTo */)
{
return getMousePosition();
}
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& /* position */)
void setMousePosition(const Vector2i& /* position */)
{
// Not applicable
}
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& /* position */, const WindowBase& /* relativeTo */)
void setMousePosition(const Vector2i& /* position */, const WindowBase& /* relativeTo */)
{
// Not applicable
}
////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int finger)
bool isTouchDown(unsigned int finger)
{
return [[SFAppDelegate getInstance] getTouchPosition:finger] != Vector2i(-1, -1);
}
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger)
Vector2i getTouchPosition(unsigned int finger)
{
return [[SFAppDelegate getInstance] getTouchPosition:finger];
}
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int finger, const WindowBase& /* relativeTo */)
Vector2i getTouchPosition(unsigned int finger, const WindowBase& /* relativeTo */)
{
return getTouchPosition(finger);
}

View File

@ -1,178 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2023 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
namespace sf::priv
{
////////////////////////////////////////////////////////////
/// \brief macOS implementation of inputs (keyboard + mouse)
///
////////////////////////////////////////////////////////////
class InputImpl
{
public:
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Key)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::isKeyPressed(Scancode)
///
////////////////////////////////////////////////////////////
static bool isKeyPressed(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::localize
///
////////////////////////////////////////////////////////////
static Keyboard::Key localize(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::delocalize
///
////////////////////////////////////////////////////////////
static Keyboard::Scancode delocalize(Keyboard::Key key);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::getDescription
///
////////////////////////////////////////////////////////////
static String getDescription(Keyboard::Scancode code);
////////////////////////////////////////////////////////////
/// \copydoc sf::Keyboard::setVirtualKeyboardVisible
///
////////////////////////////////////////////////////////////
static void setVirtualKeyboardVisible(bool visible);
////////////////////////////////////////////////////////////
/// \brief Check if a mouse button is pressed
///
/// \param button Button to check
///
/// \return True if the button is pressed, false otherwise
///
////////////////////////////////////////////////////////////
static bool isMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in desktop coordinates
///
/// This function returns the current position of the mouse
/// cursor, in global (desktop) coordinates.
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition();
////////////////////////////////////////////////////////////
/// \brief Get the current position of the mouse in window coordinates
///
/// This function returns the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it returns desktop coordinates.
///
/// \param relativeTo Reference window
///
/// \return Current position of the mouse
///
////////////////////////////////////////////////////////////
static Vector2i getMousePosition(const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in desktop coordinates
///
/// This function sets the current position of the mouse
/// cursor in global (desktop) coordinates.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position);
////////////////////////////////////////////////////////////
/// \brief Set the current position of the mouse in window coordinates
///
/// This function sets the current position of the mouse
/// cursor, relative to the given window.
/// If no window is used, it sets the position in desktop coordinates.
///
/// \param position New position of the mouse
/// \param relativeTo Reference window
///
////////////////////////////////////////////////////////////
static void setMousePosition(const Vector2i& position, const WindowBase& relativeTo);
////////////////////////////////////////////////////////////
/// \brief Check if a touch event is currently down
///
/// \param finger Finger index
///
/// \return True if \a finger is currently touching the screen, false otherwise
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in desktop coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);
////////////////////////////////////////////////////////////
/// \brief Get the current position of a touch in window coordinates
///
/// This function returns the current touch position
/// in global (desktop) coordinates.
///
/// \param finger Finger index
/// \param relativeTo Reference window
///
/// \return Current position of \a finger, or undefined if it's not down
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger, const WindowBase& relativeTo);
};
} // namespace sf::priv

View File

@ -26,11 +26,11 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/InputImpl.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Window.hpp>
#include <SFML/Window/macOS/AutoreleasePoolWrapper.hpp>
#include <SFML/Window/macOS/HIDInputManager.hpp>
#include <SFML/Window/macOS/InputImpl.hpp>
#import <SFML/Window/macOS/SFOpenGLView.h>
#include <SFML/System/Err.hpp>
@ -44,7 +44,7 @@
///
////////////////////////////////////////////////////////////
namespace sf::priv
namespace
{
////////////////////////////////////////////////////////////
/// \brief Extract the dedicated SFOpenGLView from the SFML window
@ -53,7 +53,7 @@ namespace sf::priv
/// \return nil if something went wrong or a SFOpenGLView*.
///
////////////////////////////////////////////////////////////
SFOpenGLView* getSFOpenGLViewFromSFMLWindow(const WindowBase& window)
SFOpenGLView* getSFOpenGLViewFromSFMLWindow(const sf::WindowBase& window)
{
const id nsHandle = static_cast<id>(window.getNativeHandle());
@ -115,10 +115,12 @@ SFOpenGLView* getSFOpenGLViewFromSFMLWindow(const WindowBase& window)
return view;
}
}
namespace sf::priv::InputImpl
{
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Key key)
bool isKeyPressed(Keyboard::Key key)
{
const AutoreleasePool pool;
return HIDInputManager::getInstance().isKeyPressed(key);
@ -126,42 +128,42 @@ bool InputImpl::isKeyPressed(Keyboard::Key key)
////////////////////////////////////////////////////////////
bool InputImpl::isKeyPressed(Keyboard::Scancode code)
bool isKeyPressed(Keyboard::Scancode code)
{
return HIDInputManager::getInstance().isKeyPressed(code);
}
////////////////////////////////////////////////////////////
Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
Keyboard::Key localize(Keyboard::Scancode code)
{
return HIDInputManager::getInstance().localize(code);
}
////////////////////////////////////////////////////////////
Keyboard::Scancode InputImpl::delocalize(Keyboard::Key key)
Keyboard::Scancode delocalize(Keyboard::Key key)
{
return HIDInputManager::getInstance().delocalize(key);
}
////////////////////////////////////////////////////////////
String InputImpl::getDescription(Keyboard::Scancode code)
String getDescription(Keyboard::Scancode code)
{
return HIDInputManager::getInstance().getDescription(code);
}
////////////////////////////////////////////////////////////
void InputImpl::setVirtualKeyboardVisible(bool /*visible*/)
void setVirtualKeyboardVisible(bool /*visible*/)
{
// Not applicable
}
////////////////////////////////////////////////////////////
bool InputImpl::isMouseButtonPressed(Mouse::Button button)
bool isMouseButtonPressed(Mouse::Button button)
{
const AutoreleasePool pool;
const NSUInteger state = [NSEvent pressedMouseButtons];
@ -171,7 +173,7 @@ bool InputImpl::isMouseButtonPressed(Mouse::Button button)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition()
Vector2i getMousePosition()
{
const AutoreleasePool pool;
// Reverse Y axis to match SFML coord.
@ -184,7 +186,7 @@ Vector2i InputImpl::getMousePosition()
////////////////////////////////////////////////////////////
Vector2i InputImpl::getMousePosition(const WindowBase& relativeTo)
Vector2i getMousePosition(const sf::WindowBase& relativeTo)
{
const AutoreleasePool pool;
SFOpenGLView* const view = getSFOpenGLViewFromSFMLWindow(relativeTo);
@ -202,7 +204,7 @@ Vector2i InputImpl::getMousePosition(const WindowBase& relativeTo)
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position)
void setMousePosition(const Vector2i& position)
{
const AutoreleasePool pool;
// Here we don't need to reverse the coordinates.
@ -221,7 +223,7 @@ void InputImpl::setMousePosition(const Vector2i& position)
////////////////////////////////////////////////////////////
void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& relativeTo)
void setMousePosition(const Vector2i& position, const WindowBase& relativeTo)
{
const AutoreleasePool pool;
SFOpenGLView* const view = getSFOpenGLViewFromSFMLWindow(relativeTo);
@ -239,7 +241,7 @@ void InputImpl::setMousePosition(const Vector2i& position, const WindowBase& rel
////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int /*finger*/)
bool isTouchDown(unsigned int /*finger*/)
{
// Not applicable
return false;
@ -247,7 +249,7 @@ bool InputImpl::isTouchDown(unsigned int /*finger*/)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/)
Vector2i getTouchPosition(unsigned int /*finger*/)
{
// Not applicable
return {};
@ -255,7 +257,7 @@ Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/)
////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/, const WindowBase& /*relativeTo*/)
Vector2i getTouchPosition(unsigned int /*finger*/, const WindowBase& /*relativeTo*/)
{
// Not applicable
return {};