1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
Crystal Space Windowing System: Event manager
Copyright (C) 1998 by Jorrit Tyberghein
Copyright (C) 2001 by Eric Sunshine <sunshine@sunshineco.com>
Partially written by Andrew Zabolotny <bit@freya.etu.ru>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stddef.h>
#include "cssysdef.h"
#include "csutil/cseventq.h"
#include "iutil/eventh.h"
#include "cssys/sysfunc.h"
SCF_IMPLEMENT_IBASE (csEventQueue)
SCF_IMPLEMENTS_INTERFACE (iEventQueue)
SCF_IMPLEMENT_IBASE_END
csEventQueue::csEventQueue (iObjectRegistry* r, size_t iLength) :
Registry(r), EventQueue(0), evqHead(0), evqTail(0), Length(0), SpinLock(0),
busy_looping (0), delete_occured (false)
{
SCF_CONSTRUCT_IBASE (0);
Resize (iLength);
// Create the default event outlet.
EventOutlets.Push (new csEventOutlet (0, this, Registry));
}
csEventQueue::~csEventQueue ()
{
// We don't allow deleting the event queue from within an event handler.
CS_ASSERT (busy_looping <= 0);
Clear();
if (EventQueue)
delete[] EventQueue;
for (int i = Listeners.Length() - 1; i >= 0; i--)
Listeners[i].object->DecRef();
EventOutlets.Get(0)->DecRef(); // The default event outlet which we created.
}
void csEventQueue::Post (iEvent *Event)
{
again:
Lock ();
size_t newHead = evqHead + 1;
if (newHead == Length)
newHead = 0;
if (newHead == evqTail) // Queue full?
{
Unlock ();
Resize (Length * 2); // Normally queue should not be more than half full.
goto again;
} /* endif */
EventQueue [evqHead] = Event;
evqHead = newHead;
Unlock ();
}
iEvent *csEventQueue::Get ()
{
iEvent* ev = 0;
if (!IsEmpty ())
{
Lock ();
size_t oldTail = evqTail++;
if (evqTail == Length)
evqTail = 0;
ev = (iEvent*)EventQueue [oldTail];
Unlock ();
} /* endif */
return ev;
}
void csEventQueue::Clear ()
{
iEvent* ev;
while ((ev = Get()) != NULL)
ev->DecRef();
}
void csEventQueue::Resize (size_t iLength)
{
if (iLength <= 0)
iLength = DEF_EVENT_QUEUE_LENGTH;
Lock ();
if (iLength == Length)
{
Unlock ();
return;
}
// Remember old event queue and allocate a new one
volatile iEvent **oldEventQueue = EventQueue;
EventQueue = (volatile iEvent**) new iEvent *[iLength];
// Remember old values of head & tail and set both to 0
size_t oldHead = evqHead, oldTail = evqTail;
evqHead = evqTail = 0;
// Remember old queue length and set new one
size_t oldLength = Length;
Length = iLength;
// Copy old events into the new queue until its full
if (oldEventQueue)
{
while ((oldTail != oldHead) && (evqHead < Length - 1))
{
EventQueue [evqHead++] = oldEventQueue [oldTail++];
if (oldTail == oldLength)
oldTail = 0;
} /* endwhile */
} /* endif */
delete[] oldEventQueue;
Unlock ();
}
void csEventQueue::StartLoop ()
{
busy_looping++;
}
void csEventQueue::EndLoop ()
{
busy_looping--;
if (busy_looping <= 0 && delete_occured)
{
// We are no longer in a loop and a delete occured so here
// we really clean up the entries in the listener array.
delete_occured = false;
for (int i = Listeners.Length() - 1; i >= 0; i--)
{
Listener const& listener = Listeners[i];
if (!listener.object)
Listeners.Delete (i);
}
}
}
void csEventQueue::Notify (iEvent& e)
{
int i;
StartLoop ();
for (i = Listeners.Length() - 1; i >= 0; i--)
{
Listener const& listener = Listeners[i];
if (listener.object && (listener.trigger & CSMASK_Nothing) != 0)
listener.object->HandleEvent (e);
}
EndLoop ();
}
void csEventQueue::Process ()
{
csEvent notification (csGetTicks(), csevBroadcast, cscmdPreProcess);
Notify (notification);
iEvent* ev;
while ((ev = Get ()) != 0)
{
Dispatch (*ev);
ev->DecRef ();
}
notification.Command.Code = cscmdProcess;
Notify (notification);
while ((ev = Get ()) != 0)
{
Dispatch (*ev);
ev->DecRef ();
}
notification.Command.Code = cscmdPostProcess;
Notify (notification);
while ((ev = Get ()) != 0)
{
Dispatch (*ev);
ev->DecRef ();
}
notification.Command.Code = cscmdFinalProcess;
Notify (notification);
}
void csEventQueue::Dispatch (iEvent& e)
{
int const evmask = 1 << e.Type;
bool const canstop = ((e.Flags & CSEF_BROADCAST) == 0);
StartLoop ();
int i, n;
for (i = 0, n = Listeners.Length(); i < n; i++)
{
Listener const& l = Listeners[i];
if ((l.trigger & evmask) != 0 && l.object && l.object->HandleEvent(e)
&& canstop)
break;
}
EndLoop ();
}
int csEventQueue::FindListener (iEventHandler* listener) const
{
int i;
for (i = Listeners.Length() - 1; i >= 0; i--)
{
Listener const& l = Listeners[i];
if (l.object == listener)
return i;
}
return -1;
}
void csEventQueue::RegisterListener (iEventHandler* listener,
unsigned int trigger)
{
int const n = FindListener (listener);
if (n >= 0)
Listeners[n].trigger = trigger;
else
{
Listener l = { listener, trigger };
Listeners.Push (l);
listener->IncRef ();
}
}
void csEventQueue::RemoveListener (iEventHandler* listener)
{
int const n = FindListener(listener);
if (n >= 0)
{
Listeners[n].object->DecRef();
// Only delete the entry in the vector if we're not in a loop.
if (busy_looping <= 0)
Listeners.Delete(n);
else
{
Listeners[n].object = NULL;
delete_occured = true;
}
}
}
void csEventQueue::ChangeListenerTrigger (iEventHandler* l,
unsigned int trigger)
{
int const n = FindListener(l);
if (n >= 0)
Listeners[n].trigger = trigger;
}
iEventOutlet* csEventQueue::CreateEventOutlet (iEventPlug* plug)
{
csEventOutlet* outlet = 0;
if (plug != 0)
{
outlet = new csEventOutlet(plug, this, Registry);
EventOutlets.Push (outlet);
}
return outlet;
}
iEventOutlet* csEventQueue::GetEventOutlet()
{
return EventOutlets.Get(0);
}
iEventCord* csEventQueue::GetEventCord (int cat, int subcat)
{
csEventCord* cord;
int const n = EventCords.Find (cat, subcat);
if (n >= 0)
cord = EventCords.Get(n);
else
{
cord = new csEventCord (cat, subcat);
EventCords.Push (cord);
}
return cord;
}
int csEventQueue::EventCordsVector::Find (int cat, int subcat)
{
int i;
for (i = Length() - 1; i >= 0; i--)
{
csEventCord *cord = Get(i);
if (cat == cord->GetCategory() && subcat == cord->GetSubcategory())
return i;
}
return -1;
}
|