Quellcode durchsuchen

device: generalize DeviceEventQueue

Daniele Bartolini vor 3 Jahren
Ursprung
Commit
ffa37c3b09

+ 4 - 0
src/config.h

@@ -130,3 +130,7 @@
 #ifndef CROWN_LUA_MAX_MATRIX4X4_SIZE
 	#define CROWN_LUA_MAX_MATRIX4X4_SIZE (128*1024)
 #endif
+
+#ifndef CROWN_MAX_OS_EVENTS
+	#define CROWN_MAX_OS_EVENTS 128
+#endif

+ 12 - 24
src/device/device_event_queue.inl

@@ -6,24 +6,22 @@
 #pragma once
 
 #include "device/types.h"
-#include "core/thread/spsc_queue.inl"
 #include <string.h> // memcpy
 
 namespace crown
 {
-/// Single Producer Single Consumer event queue.
-/// Used only to pass events from os thread to main thread.
-/// https://www.irif.fr/~guatto/papers/sbac13.pdf
+typedef bool (*QueuePushFunction)(const OsEvent &ev);
+
+/// Used only to pass events from OS to main thread.
 ///
 /// @ingroup Device
 struct DeviceEventQueue
 {
-#define MAX_OS_EVENTS 128
-	SPSCQueue<OsEvent, MAX_OS_EVENTS> _queue;
+	QueuePushFunction _queue_push_function;
 
 	///
-	DeviceEventQueue(Allocator &a)
-		: _queue(a)
+	DeviceEventQueue(QueuePushFunction fn)
+		: _queue_push_function(fn)
 	{
 	}
 
@@ -36,7 +34,7 @@ struct DeviceEventQueue
 		ev.button.button_num = button_id;
 		ev.button.pressed = pressed;
 
-		push_event(ev);
+		_queue_push_function(ev);
 	}
 
 	void push_axis_event(u16 device_id, u16 device_num, u16 axis_id, s16 axis_x, s16 axis_y, s16 axis_z)
@@ -50,7 +48,7 @@ struct DeviceEventQueue
 		ev.axis.axis_y = axis_y;
 		ev.axis.axis_z = axis_z;
 
-		push_event(ev);
+		_queue_push_function(ev);
 	}
 
 	void push_status_event(u16 device_id, u16 device_num, bool connected)
@@ -61,7 +59,7 @@ struct DeviceEventQueue
 		ev.status.device_num = device_num;
 		ev.status.connected = connected;
 
-		push_event(ev);
+		_queue_push_function(ev);
 	}
 
 	void push_resolution_event(u16 width, u16 height)
@@ -71,7 +69,7 @@ struct DeviceEventQueue
 		ev.resolution.width = width;
 		ev.resolution.height = height;
 
-		push_event(ev);
+		_queue_push_function(ev);
 	}
 
 	void push_exit_event()
@@ -79,7 +77,7 @@ struct DeviceEventQueue
 		OsEvent ev;
 		ev.type = OsEventType::EXIT;
 
-		push_event(ev);
+		_queue_push_function(ev);
 	}
 
 	void push_text_event(u8 len, u8 utf8[4])
@@ -89,17 +87,7 @@ struct DeviceEventQueue
 		ev.text.len = len;
 		memcpy(ev.text.utf8, utf8, sizeof(ev.text.utf8));
 
-		push_event(ev);
-	}
-
-	bool push_event(const OsEvent &ev)
-	{
-		return _queue.push(ev);
-	}
-
-	bool pop_event(OsEvent &ev)
-	{
-		return _queue.pop(ev);
+		_queue_push_function(ev);
 	}
 };
 

+ 12 - 2
src/device/main_android.cpp

@@ -10,6 +10,7 @@
 #include "core/guid.h"
 #include "core/memory/globals.h"
 #include "core/memory/memory.inl"
+#include "core/thread/spsc_queue.inl"
 #include "core/thread/thread.h"
 #include "device/device.h"
 #include "device/device_event_queue.inl"
@@ -28,14 +29,18 @@ extern "C"
 
 namespace crown
 {
+static bool push_event(const OsEvent &ev);
+
 struct AndroidDevice
 {
+	SPSCQueue<OsEvent, CROWN_MAX_OS_EVENTS> _events;
 	DeviceEventQueue _queue;
 	Thread _main_thread;
 	DeviceOptions *_opts;
 
 	AndroidDevice(Allocator &a)
-		: _queue(a)
+		: _events(a)
+		, _queue(push_event)
 		, _opts(NULL)
 	{
 	}
@@ -322,9 +327,14 @@ namespace display
 
 static AndroidDevice *s_android_device;
 
+static bool push_event(const OsEvent &ev)
+{
+	return s_android_device->_events.push(ev);
+}
+
 bool next_event(OsEvent &ev)
 {
-	return s_android_device->_queue.pop_event(ev);
+	return s_android_device->_events.pop(ev);
 }
 
 } // namespace crown

+ 11 - 2
src/device/main_linux.cpp

@@ -12,6 +12,7 @@
 #include "core/memory/globals.h"
 #include "core/memory/memory.inl"
 #include "core/os.h"
+#include "core/thread/spsc_queue.inl"
 #include "core/thread/thread.h"
 #include "core/unit_tests.h"
 #include "device/device.h"
@@ -294,6 +295,7 @@ struct Joypad
 
 static bool s_exit = false;
 static Cursor _x11_cursors[MouseCursor::COUNT];
+static bool push_event(const OsEvent &ev);
 
 struct LinuxDevice
 {
@@ -306,6 +308,7 @@ struct LinuxDevice
 	Cursor _x11_hidden_cursor;
 	bool _x11_detectable_autorepeat;
 	XRRScreenConfiguration *_screen_config;
+	SPSCQueue<OsEvent, CROWN_MAX_OS_EVENTS> _events;
 	DeviceEventQueue _queue;
 	Joypad _joypad;
 	::Window _x11_window;
@@ -323,7 +326,8 @@ struct LinuxDevice
 		, _x11_hidden_cursor(None)
 		, _x11_detectable_autorepeat(false)
 		, _screen_config(NULL)
-		, _queue(a)
+		, _events(a)
+		, _queue(push_event)
 		, _x11_window(None)
 		, _mouse_last_x(INT16_MAX)
 		, _mouse_last_y(INT16_MAX)
@@ -885,9 +889,14 @@ namespace display
 
 } // namespace display
 
+static bool push_event(const OsEvent &ev)
+{
+	return s_linux_device->_events.push(ev);
+}
+
 bool next_event(OsEvent &ev)
 {
-	return s_linux_device->_queue.pop_event(ev);
+	return s_linux_device->_events.pop(ev);
 }
 
 } // namespace crown

+ 11 - 2
src/device/main_windows.cpp

@@ -11,6 +11,7 @@
 #include "core/guid.h"
 #include "core/memory/globals.h"
 #include "core/memory/memory.inl"
+#include "core/thread/spsc_queue.inl"
 #include "core/thread/thread.h"
 #include "core/unit_tests.h"
 #include "device/device.h"
@@ -278,11 +279,13 @@ struct Joypad
 
 static bool s_exit = false;
 static HCURSOR _win_cursors[MouseCursor::COUNT];
+static bool push_event(const OsEvent &ev);
 
 struct WindowsDevice
 {
 	HWND _hwnd;
 	HCURSOR _hcursor;
+	SPSCQueue<OsEvent, CROWN_MAX_OS_EVENTS> _events;
 	DeviceEventQueue _queue;
 	Joypad _joypad;
 	s16 _mouse_last_x;
@@ -292,7 +295,8 @@ struct WindowsDevice
 	WindowsDevice(Allocator &a)
 		: _hwnd(NULL)
 		, _hcursor(NULL)
-		, _queue(a)
+		, _events(a)
+		, _queue(push_event)
 		, _mouse_last_x(INT16_MAX)
 		, _mouse_last_y(INT16_MAX)
 		, _cursor_mode(CursorMode::NORMAL)
@@ -783,9 +787,14 @@ namespace display
 
 } // namespace display
 
+static bool push_event(const OsEvent &ev)
+{
+	return s_windows_device->_events.push(ev);
+}
+
 bool next_event(OsEvent &ev)
 {
-	return s_windows_device->_queue.pop_event(ev);
+	return s_windows_device->_events.pop(ev);
 }
 
 } // namespace crown