summaryrefslogtreecommitdiff
path: root/core/os
diff options
context:
space:
mode:
Diffstat (limited to 'core/os')
-rw-r--r--core/os/input.cpp2
-rw-r--r--core/os/input.h2
-rw-r--r--core/os/input_event.cpp78
-rw-r--r--core/os/input_event.h13
-rw-r--r--core/os/mutex.cpp32
-rw-r--r--core/os/mutex.h73
-rw-r--r--core/os/os.h2
-rw-r--r--core/os/thread_dummy.cpp8
-rw-r--r--core/os/thread_dummy.h13
-rw-r--r--core/os/thread_safe.cpp49
-rw-r--r--core/os/thread_safe.h49
11 files changed, 134 insertions, 187 deletions
diff --git a/core/os/input.cpp b/core/os/input.cpp
index 6f0392fec9..1768b851df 100644
--- a/core/os/input.cpp
+++ b/core/os/input.cpp
@@ -57,7 +57,7 @@ Input::MouseMode Input::get_mouse_mode() const {
void Input::_bind_methods() {
- ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed);
+ ClassDB::bind_method(D_METHOD("is_key_pressed", "keycode"), &Input::is_key_pressed);
ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed);
ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed);
ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed);
diff --git a/core/os/input.h b/core/os/input.h
index 8df3b1c5a9..55e0511080 100644
--- a/core/os/input.h
+++ b/core/os/input.h
@@ -79,7 +79,7 @@ public:
static Input *get_singleton();
- virtual bool is_key_pressed(int p_scancode) const = 0;
+ virtual bool is_key_pressed(int p_keycode) const = 0;
virtual bool is_mouse_button_pressed(int p_button) const = 0;
virtual bool is_joy_button_pressed(int p_device, int p_button) const = 0;
virtual bool is_action_pressed(const StringName &p_action) const = 0;
diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp
index 3cb9c2c1c2..204a36bf56 100644
--- a/core/os/input_event.cpp
+++ b/core/os/input_event.cpp
@@ -237,19 +237,31 @@ bool InputEventKey::is_pressed() const {
return pressed;
}
-void InputEventKey::set_scancode(uint32_t p_scancode) {
+void InputEventKey::set_keycode(uint32_t p_keycode) {
- scancode = p_scancode;
+ keycode = p_keycode;
}
-uint32_t InputEventKey::get_scancode() const {
- return scancode;
+uint32_t InputEventKey::get_keycode() const {
+
+ return keycode;
+}
+
+void InputEventKey::set_physical_keycode(uint32_t p_keycode) {
+
+ physical_keycode = p_keycode;
+}
+
+uint32_t InputEventKey::get_physical_keycode() const {
+
+ return physical_keycode;
}
void InputEventKey::set_unicode(uint32_t p_unicode) {
unicode = p_unicode;
}
+
uint32_t InputEventKey::get_unicode() const {
return unicode;
@@ -259,14 +271,30 @@ void InputEventKey::set_echo(bool p_enable) {
echo = p_enable;
}
+
bool InputEventKey::is_echo() const {
return echo;
}
-uint32_t InputEventKey::get_scancode_with_modifiers() const {
+uint32_t InputEventKey::get_keycode_with_modifiers() const {
- uint32_t sc = scancode;
+ uint32_t sc = keycode;
+ if (get_control())
+ sc |= KEY_MASK_CTRL;
+ if (get_alt())
+ sc |= KEY_MASK_ALT;
+ if (get_shift())
+ sc |= KEY_MASK_SHIFT;
+ if (get_metakey())
+ sc |= KEY_MASK_META;
+
+ return sc;
+}
+
+uint32_t InputEventKey::get_physical_keycode_with_modifiers() const {
+
+ uint32_t sc = physical_keycode;
if (get_control())
sc |= KEY_MASK_CTRL;
if (get_alt())
@@ -281,7 +309,7 @@ uint32_t InputEventKey::get_scancode_with_modifiers() const {
String InputEventKey::as_text() const {
- String kc = keycode_get_string(scancode);
+ String kc = keycode_get_string(keycode);
if (kc == String())
return kc;
@@ -306,10 +334,18 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed
if (key.is_null())
return false;
- uint32_t code = get_scancode_with_modifiers();
- uint32_t event_code = key->get_scancode_with_modifiers();
+ bool match = false;
+ if (get_keycode() == 0) {
+ uint32_t code = get_physical_keycode_with_modifiers();
+ uint32_t event_code = key->get_physical_keycode_with_modifiers();
- bool match = get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code);
+ match = get_physical_keycode() == key->get_physical_keycode() && (!key->is_pressed() || (code & event_code) == code);
+ } else {
+ uint32_t code = get_keycode_with_modifiers();
+ uint32_t event_code = key->get_keycode_with_modifiers();
+
+ match = get_keycode() == key->get_keycode() && (!key->is_pressed() || (code & event_code) == code);
+ }
if (match) {
if (p_pressed != NULL)
*p_pressed = key->is_pressed();
@@ -325,8 +361,8 @@ bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
if (key.is_null())
return false;
- uint32_t code = get_scancode_with_modifiers();
- uint32_t event_code = key->get_scancode_with_modifiers();
+ uint32_t code = get_keycode_with_modifiers();
+ uint32_t event_code = key->get_keycode_with_modifiers();
return code == event_code;
}
@@ -335,26 +371,32 @@ void InputEventKey::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
- ClassDB::bind_method(D_METHOD("set_scancode", "scancode"), &InputEventKey::set_scancode);
- ClassDB::bind_method(D_METHOD("get_scancode"), &InputEventKey::get_scancode);
+ ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
+ ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
+
+ ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
+ ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
- ClassDB::bind_method(D_METHOD("get_scancode_with_modifiers"), &InputEventKey::get_scancode_with_modifiers);
+ ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
+ ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "scancode"), "set_scancode", "get_scancode");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "echo"), "set_echo", "is_echo");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo");
}
InputEventKey::InputEventKey() {
pressed = false;
- scancode = 0;
+ keycode = 0;
+ physical_keycode = 0;
unicode = 0; ///unicode
echo = false;
}
diff --git a/core/os/input_event.h b/core/os/input_event.h
index c6b04bcfa5..c105fcd1c1 100644
--- a/core/os/input_event.h
+++ b/core/os/input_event.h
@@ -256,7 +256,8 @@ class InputEventKey : public InputEventWithModifiers {
bool pressed; /// otherwise release
- uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
+ uint32_t keycode; ///< check keyboard.h , KeyCode enum, without modifier masks
+ uint32_t physical_keycode;
uint32_t unicode; ///unicode
bool echo; /// true if this is an echo key
@@ -268,8 +269,11 @@ public:
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
- void set_scancode(uint32_t p_scancode);
- uint32_t get_scancode() const;
+ void set_keycode(uint32_t p_keycode);
+ uint32_t get_keycode() const;
+
+ void set_physical_keycode(uint32_t p_keycode);
+ uint32_t get_physical_keycode() const;
void set_unicode(uint32_t p_unicode);
uint32_t get_unicode() const;
@@ -277,7 +281,8 @@ public:
void set_echo(bool p_enable);
virtual bool is_echo() const;
- uint32_t get_scancode_with_modifiers() const;
+ uint32_t get_keycode_with_modifiers() const;
+ uint32_t get_physical_keycode_with_modifiers() const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp
index f099b4319a..74c308f646 100644
--- a/core/os/mutex.cpp
+++ b/core/os/mutex.cpp
@@ -30,31 +30,17 @@
#include "mutex.h"
-#include "core/error_macros.h"
-
-#include <stddef.h>
-
-Mutex *(*Mutex::create_func)(bool) = 0;
-
-Mutex *Mutex::create(bool p_recursive) {
-
- ERR_FAIL_COND_V(!create_func, 0);
-
- return create_func(p_recursive);
-}
-
-Mutex::~Mutex() {
-}
-
-Mutex *_global_mutex = NULL;
+static Mutex _global_mutex;
void _global_lock() {
-
- if (_global_mutex)
- _global_mutex->lock();
+ _global_mutex.lock();
}
-void _global_unlock() {
- if (_global_mutex)
- _global_mutex->unlock();
+void _global_unlock() {
+ _global_mutex.unlock();
}
+
+template class MutexImpl<std::recursive_mutex>;
+template class MutexImpl<std::mutex>;
+template class MutexLock<MutexImpl<std::recursive_mutex> >;
+template class MutexLock<MutexImpl<std::mutex> >;
diff --git a/core/os/mutex.h b/core/os/mutex.h
index db82eb64f5..6cf8ee7c40 100644
--- a/core/os/mutex.h
+++ b/core/os/mutex.h
@@ -32,42 +32,69 @@
#define MUTEX_H
#include "core/error_list.h"
+#include "core/typedefs.h"
-/**
- * @class Mutex
- * @author Juan Linietsky
- * Portable Mutex (thread-safe locking) implementation.
- * Mutexes are always recursive ( they don't self-lock in a single thread ).
- * Mutexes can be used with a Lockp object like this, to avoid having to worry about unlocking:
- * Lockp( mutex );
- */
+#if !(defined NO_THREADS)
-class Mutex {
-protected:
- static Mutex *(*create_func)(bool);
+#include <mutex>
+
+template <class StdMutexT>
+class MutexImpl {
+ mutable StdMutexT mutex;
public:
- virtual void lock() = 0; ///< Lock the mutex, block if locked by someone else
- virtual void unlock() = 0; ///< Unlock the mutex, let other threads continue
- virtual Error try_lock() = 0; ///< Attempt to lock the mutex, OK on success, ERROR means it can't lock.
+ _ALWAYS_INLINE_ void lock() const {
+ mutex.lock();
+ }
- static Mutex *create(bool p_recursive = true); ///< Create a mutex
+ _ALWAYS_INLINE_ void unlock() const {
+ mutex.unlock();
+ }
- virtual ~Mutex();
+ _ALWAYS_INLINE_ Error try_lock() const {
+ return mutex.try_lock() ? OK : ERR_BUSY;
+ }
};
+template <class MutexT>
class MutexLock {
-
- Mutex *mutex;
+ const MutexT &mutex;
public:
- MutexLock(Mutex *p_mutex) {
- mutex = p_mutex;
- if (mutex) mutex->lock();
+ _ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) :
+ mutex(p_mutex) {
+ mutex.lock();
}
- ~MutexLock() {
- if (mutex) mutex->unlock();
+
+ _ALWAYS_INLINE_ ~MutexLock() {
+ mutex.unlock();
}
};
+#else
+
+template <class StdMutexType>
+class MutexImpl {
+public:
+ _ALWAYS_INLINE_ void lock() const {}
+ _ALWAYS_INLINE_ void unlock() const {}
+ _ALWAYS_INLINE_ Error try_lock() const { return OK; }
+};
+
+template <class MutexT>
+class MutexLock {
+public:
+ explicit MutexLock(const MutexT &p_mutex) {}
+};
+
+#endif // !NO_THREADS
+
+using Mutex = MutexImpl<std::recursive_mutex>; // Recursive, for general use
+using BinaryMutex = MutexImpl<std::mutex>; // Non-recursive, handle with care
+
+extern template class MutexImpl<std::recursive_mutex>;
+extern template class MutexImpl<std::mutex>;
+extern template class MutexLock<MutexImpl<std::recursive_mutex> >;
+extern template class MutexLock<MutexImpl<std::mutex> >;
+
#endif
diff --git a/core/os/os.h b/core/os/os.h
index 77391c3a8b..1d3619b1e6 100644
--- a/core/os/os.h
+++ b/core/os/os.h
@@ -41,8 +41,6 @@
#include <stdarg.h>
-class Mutex;
-
class OS {
static OS *singleton;
diff --git a/core/os/thread_dummy.cpp b/core/os/thread_dummy.cpp
index 916aeeda30..097c90c1fb 100644
--- a/core/os/thread_dummy.cpp
+++ b/core/os/thread_dummy.cpp
@@ -40,14 +40,6 @@ void ThreadDummy::make_default() {
Thread::create_func = &ThreadDummy::create;
};
-Mutex *MutexDummy::create(bool p_recursive) {
- return memnew(MutexDummy);
-};
-
-void MutexDummy::make_default() {
- Mutex::create_func = &MutexDummy::create;
-};
-
SemaphoreOld *SemaphoreDummy::create() {
return memnew(SemaphoreDummy);
};
diff --git a/core/os/thread_dummy.h b/core/os/thread_dummy.h
index 9329cdaa32..c0976ec299 100644
--- a/core/os/thread_dummy.h
+++ b/core/os/thread_dummy.h
@@ -31,7 +31,6 @@
#ifndef THREAD_DUMMY_H
#define THREAD_DUMMY_H
-#include "core/os/mutex.h"
#include "core/os/rw_lock.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"
@@ -46,18 +45,6 @@ public:
static void make_default();
};
-class MutexDummy : public Mutex {
-
- static Mutex *create(bool p_recursive);
-
-public:
- virtual void lock(){};
- virtual void unlock(){};
- virtual Error try_lock() { return OK; };
-
- static void make_default();
-};
-
class SemaphoreDummy : public SemaphoreOld {
static SemaphoreOld *create();
diff --git a/core/os/thread_safe.cpp b/core/os/thread_safe.cpp
deleted file mode 100644
index d8d783ae16..0000000000
--- a/core/os/thread_safe.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-/*************************************************************************/
-/* thread_safe.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#include "thread_safe.h"
-
-#include "core/error_macros.h"
-#include "core/os/memory.h"
-
-ThreadSafe::ThreadSafe() {
-
- mutex = Mutex::create();
- if (!mutex) {
-
- WARN_PRINT("THREAD_SAFE defined, but no default mutex type");
- }
-}
-
-ThreadSafe::~ThreadSafe() {
-
- if (mutex)
- memdelete(mutex);
-}
diff --git a/core/os/thread_safe.h b/core/os/thread_safe.h
index a4238a9225..0221edf491 100644
--- a/core/os/thread_safe.h
+++ b/core/os/thread_safe.h
@@ -33,50 +33,9 @@
#include "core/os/mutex.h"
-class ThreadSafe {
-
- Mutex *mutex;
-
-public:
- inline void lock() const {
- if (mutex) mutex->lock();
- }
- inline void unlock() const {
- if (mutex) mutex->unlock();
- }
-
- ThreadSafe();
- ~ThreadSafe();
-};
-
-class ThreadSafeMethod {
-
- const ThreadSafe *_ts;
-
-public:
- ThreadSafeMethod(const ThreadSafe *p_ts) {
-
- _ts = p_ts;
- _ts->lock();
- }
-
- ~ThreadSafeMethod() { _ts->unlock(); }
-};
-
-#ifndef NO_THREADS
-
-#define _THREAD_SAFE_CLASS_ ThreadSafe __thread__safe__;
-#define _THREAD_SAFE_METHOD_ ThreadSafeMethod __thread_safe_method__(&__thread__safe__);
-#define _THREAD_SAFE_LOCK_ __thread__safe__.lock();
-#define _THREAD_SAFE_UNLOCK_ __thread__safe__.unlock();
-
-#else
-
-#define _THREAD_SAFE_CLASS_
-#define _THREAD_SAFE_METHOD_
-#define _THREAD_SAFE_LOCK_
-#define _THREAD_SAFE_UNLOCK_
-
-#endif
+#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_;
+#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_);
+#define _THREAD_SAFE_LOCK_ _thread_safe_.lock();
+#define _THREAD_SAFE_UNLOCK_ _thread_safe_.unlock();
#endif