2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* event_queue.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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 "event_queue.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error EventQueue::push_call(uint32_t p_instance_ID, const StringName &p_method, VARIANT_ARG_DECLARE) {
|
|
|
|
|
|
|
|
uint8_t room_needed = sizeof(Event);
|
|
|
|
int args = 0;
|
|
|
|
if (p_arg5.get_type() != Variant::NIL)
|
|
|
|
args = 5;
|
|
|
|
else if (p_arg4.get_type() != Variant::NIL)
|
|
|
|
args = 4;
|
|
|
|
else if (p_arg3.get_type() != Variant::NIL)
|
|
|
|
args = 3;
|
|
|
|
else if (p_arg2.get_type() != Variant::NIL)
|
|
|
|
args = 2;
|
|
|
|
else if (p_arg1.get_type() != Variant::NIL)
|
|
|
|
args = 1;
|
2016-03-08 23:00:52 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
args = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
room_needed += sizeof(Variant) * args;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V((buffer_end + room_needed) >= buffer_size, ERR_OUT_OF_MEMORY);
|
|
|
|
Event *ev = memnew_placement(&event_buffer[buffer_end], Event);
|
|
|
|
ev->args = args;
|
|
|
|
ev->instance_ID = p_instance_ID;
|
|
|
|
ev->method = p_method;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
buffer_end += sizeof(Event);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (args >= 1) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
|
|
|
|
buffer_end += sizeof(Variant);
|
|
|
|
*v = p_arg1;
|
2015-04-02 10:22:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (args >= 2) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
|
|
|
|
buffer_end += sizeof(Variant);
|
|
|
|
*v = p_arg2;
|
2015-04-02 10:22:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (args >= 3) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
|
|
|
|
buffer_end += sizeof(Variant);
|
|
|
|
*v = p_arg3;
|
2015-04-02 10:22:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (args >= 4) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
|
|
|
|
buffer_end += sizeof(Variant);
|
|
|
|
*v = p_arg4;
|
2015-04-02 10:22:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (args >= 5) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
|
|
|
|
buffer_end += sizeof(Variant);
|
|
|
|
*v = p_arg5;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-06-28 06:00:21 +00:00
|
|
|
if (buffer_end > buffer_max_used)
|
2017-03-18 23:36:26 +00:00
|
|
|
buffer_max_used = buffer_end;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventQueue::flush_events() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t read_pos = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (read_pos < buffer_end) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Event *event = (Event *)&event_buffer[read_pos];
|
|
|
|
Variant *args = (Variant *)(event + 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
Object *obj = ObjectDB::get_instance(event->instance_ID);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
if (obj) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// events don't expect a return value
|
2017-03-18 23:36:26 +00:00
|
|
|
obj->call(event->method,
|
|
|
|
(event->args >= 1) ? args[0] : Variant(),
|
|
|
|
(event->args >= 2) ? args[1] : Variant(),
|
|
|
|
(event->args >= 3) ? args[2] : Variant(),
|
|
|
|
(event->args >= 4) ? args[3] : Variant(),
|
|
|
|
(event->args >= 5) ? args[4] : Variant());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (event->args >= 1) args[0].~Variant();
|
|
|
|
if (event->args >= 2) args[1].~Variant();
|
|
|
|
if (event->args >= 3) args[2].~Variant();
|
|
|
|
if (event->args >= 4) args[3].~Variant();
|
|
|
|
if (event->args >= 5) args[4].~Variant();
|
2014-02-10 01:10:30 +00:00
|
|
|
event->~Event();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
read_pos += sizeof(Event) + sizeof(Variant) * event->args;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
buffer_end = 0; // reset buffer
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventQueue::EventQueue(uint32_t p_buffer_size) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
buffer_end = 0;
|
|
|
|
buffer_max_used = 0;
|
|
|
|
buffer_size = p_buffer_size;
|
|
|
|
event_buffer = memnew_arr(uint8_t, buffer_size);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
EventQueue::~EventQueue() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t read_pos = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (read_pos < buffer_end) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Event *event = (Event *)&event_buffer[read_pos];
|
|
|
|
Variant *args = (Variant *)(event + 1);
|
|
|
|
for (int i = 0; i < event->args; i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
args[i].~Variant();
|
|
|
|
event->~Event();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
read_pos += sizeof(Event) + sizeof(Variant) * event->args;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete_arr(event_buffer);
|
2017-03-18 23:36:26 +00:00
|
|
|
event_buffer = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|