2011-02-25 11:28:33 +00:00
|
|
|
/*
|
2002-10-12 11:37:38 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-01-07 19:13:47 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup GHOST
|
2011-02-25 11:28:33 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2001 NaN Technologies B.V.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "GHOST_EventManager.h"
|
|
|
|
|
#include "GHOST_Debug.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <algorithm>
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
GHOST_EventManager::GHOST_EventManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GHOST_EventManager::~GHOST_EventManager()
|
|
|
|
|
{
|
|
|
|
|
disposeEvents();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
TConsumerVector::iterator iter = m_consumers.begin();
|
|
|
|
|
while (iter != m_consumers.end()) {
|
|
|
|
|
GHOST_IEventConsumer *consumer = *iter;
|
2009-08-05 02:40:51 +00:00
|
|
|
delete consumer;
|
2013-09-03 15:30:07 +00:00
|
|
|
iter = m_consumers.erase(iter);
|
2009-08-05 02:40:51 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
uint32_t GHOST_EventManager::getNumEvents()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2021-07-05 10:10:20 -07:00
|
|
|
return (uint32_t)m_events.size();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
uint32_t GHOST_EventManager::getNumEvents(GHOST_TEventType type)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2021-07-05 10:10:20 -07:00
|
|
|
uint32_t numEvents = 0;
|
2003-01-01 20:42:27 +00:00
|
|
|
TEventStack::iterator p;
|
2012-09-20 00:55:32 +00:00
|
|
|
for (p = m_events.begin(); p != m_events.end(); ++p) {
|
2002-10-12 11:37:38 +00:00
|
|
|
if ((*p)->getType() == type) {
|
|
|
|
|
numEvents++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return numEvents;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_TSuccess GHOST_EventManager::pushEvent(GHOST_IEvent *event)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
GHOST_TSuccess success;
|
|
|
|
|
GHOST_ASSERT(event, "invalid event");
|
|
|
|
|
if (m_events.size() < m_events.max_size()) {
|
|
|
|
|
m_events.push_front(event);
|
|
|
|
|
success = GHOST_kSuccess;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
success = GHOST_kFailure;
|
|
|
|
|
}
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 18:07:38 +01:00
|
|
|
void GHOST_EventManager::dispatchEvent(GHOST_IEvent *event)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2015-12-27 18:07:38 +01:00
|
|
|
TConsumerVector::iterator iter;
|
|
|
|
|
|
|
|
|
|
for (iter = m_consumers.begin(); iter != m_consumers.end(); ++iter) {
|
|
|
|
|
(*iter)->processEvent(event);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 18:07:38 +01:00
|
|
|
void GHOST_EventManager::dispatchEvent()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2015-12-27 18:07:38 +01:00
|
|
|
GHOST_IEvent *event = m_events.back();
|
2015-12-28 00:35:27 +01:00
|
|
|
m_events.pop_back();
|
|
|
|
|
m_handled_events.push_back(event);
|
2015-12-27 18:07:38 +01:00
|
|
|
|
|
|
|
|
dispatchEvent(event);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-27 18:07:38 +01:00
|
|
|
void GHOST_EventManager::dispatchEvents()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2015-12-27 18:07:38 +01:00
|
|
|
while (!m_events.empty()) {
|
|
|
|
|
dispatchEvent();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2015-12-28 00:35:27 +01:00
|
|
|
|
|
|
|
|
disposeEvents();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_TSuccess GHOST_EventManager::addConsumer(GHOST_IEventConsumer *consumer)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
GHOST_TSuccess success;
|
|
|
|
|
GHOST_ASSERT(consumer, "invalid consumer");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-20 15:01:05 +10:00
|
|
|
/* Check to see whether the consumer is already in our list. */
|
2002-10-12 11:37:38 +00:00
|
|
|
TConsumerVector::const_iterator iter = std::find(
|
|
|
|
|
m_consumers.begin(), m_consumers.end(), consumer);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (iter == m_consumers.end()) {
|
2021-07-20 15:01:05 +10:00
|
|
|
/* Add the consumer. */
|
2002-10-12 11:37:38 +00:00
|
|
|
m_consumers.push_back(consumer);
|
|
|
|
|
success = GHOST_kSuccess;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
success = GHOST_kFailure;
|
|
|
|
|
}
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_TSuccess GHOST_EventManager::removeConsumer(GHOST_IEventConsumer *consumer)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
GHOST_TSuccess success;
|
|
|
|
|
GHOST_ASSERT(consumer, "invalid consumer");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-20 15:01:05 +10:00
|
|
|
/* Check to see whether the consumer is in our list. */
|
2002-10-12 11:37:38 +00:00
|
|
|
TConsumerVector::iterator iter = std::find(m_consumers.begin(), m_consumers.end(), consumer);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (iter != m_consumers.end()) {
|
2021-07-20 15:01:05 +10:00
|
|
|
/* Remove the consumer. */
|
2002-10-12 11:37:38 +00:00
|
|
|
m_consumers.erase(iter);
|
|
|
|
|
success = GHOST_kSuccess;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
success = GHOST_kFailure;
|
|
|
|
|
}
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
void GHOST_EventManager::removeWindowEvents(GHOST_IWindow *window)
|
2003-01-01 20:42:27 +00:00
|
|
|
{
|
|
|
|
|
TEventStack::iterator iter;
|
|
|
|
|
iter = m_events.begin();
|
2016-06-10 07:45:39 +10:00
|
|
|
while (iter != m_events.end()) {
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_IEvent *event = *iter;
|
2016-06-10 07:45:39 +10:00
|
|
|
if (event->getWindow() == window) {
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_PRINT("GHOST_EventManager::removeWindowEvents(): removing event\n");
|
2003-01-01 20:42:27 +00:00
|
|
|
/*
|
|
|
|
|
* Found an event for this window, remove it.
|
|
|
|
|
* The iterator will become invalid.
|
|
|
|
|
*/
|
|
|
|
|
delete event;
|
|
|
|
|
m_events.erase(iter);
|
|
|
|
|
iter = m_events.begin();
|
|
|
|
|
}
|
2012-05-19 09:57:55 +00:00
|
|
|
else {
|
2012-09-20 00:55:32 +00:00
|
|
|
++iter;
|
2003-01-01 20:42:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:57:55 +00:00
|
|
|
void GHOST_EventManager::removeTypeEvents(GHOST_TEventType type, GHOST_IWindow *window)
|
2003-01-01 20:42:27 +00:00
|
|
|
{
|
|
|
|
|
TEventStack::iterator iter;
|
|
|
|
|
iter = m_events.begin();
|
2016-06-10 07:45:39 +10:00
|
|
|
while (iter != m_events.end()) {
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_IEvent *event = *iter;
|
2016-06-10 07:45:39 +10:00
|
|
|
if ((event->getType() == type) && (!window || (event->getWindow() == window))) {
|
2012-05-19 09:57:55 +00:00
|
|
|
GHOST_PRINT("GHOST_EventManager::removeTypeEvents(): removing event\n");
|
2003-01-01 20:42:27 +00:00
|
|
|
/*
|
|
|
|
|
* Found an event of this type for the window, remove it.
|
|
|
|
|
* The iterator will become invalid.
|
|
|
|
|
*/
|
|
|
|
|
delete event;
|
|
|
|
|
m_events.erase(iter);
|
|
|
|
|
iter = m_events.begin();
|
|
|
|
|
}
|
2012-05-19 09:57:55 +00:00
|
|
|
else {
|
2012-09-20 00:55:32 +00:00
|
|
|
++iter;
|
2003-01-01 20:42:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void GHOST_EventManager::disposeEvents()
|
|
|
|
|
{
|
2015-12-28 00:35:27 +01:00
|
|
|
while (m_handled_events.empty() == false) {
|
|
|
|
|
GHOST_ASSERT(m_handled_events[0], "invalid event");
|
|
|
|
|
delete m_handled_events[0];
|
|
|
|
|
m_handled_events.pop_front();
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 00:55:32 +00:00
|
|
|
while (m_events.empty() == false) {
|
2002-10-12 11:37:38 +00:00
|
|
|
GHOST_ASSERT(m_events[0], "invalid event");
|
|
|
|
|
delete m_events[0];
|
|
|
|
|
m_events.pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|