Fix to allow destroying of gwin buttons

ugfx_release_2.6
Andrew Hannam 2013-03-02 22:03:40 +10:00
parent 6ee8b005ae
commit 57435fb4c2
2 changed files with 743 additions and 741 deletions

View File

@ -1,197 +1,197 @@
/* /*
ChibiOS/GFX - Copyright (C) 2012 ChibiOS/GFX - Copyright (C) 2012
Joel Bodenmann aka Tectu <joel@unormal.org> Joel Bodenmann aka Tectu <joel@unormal.org>
This file is part of ChibiOS/GFX. This file is part of ChibiOS/GFX.
ChibiOS/GFX is free software; you can redistribute it and/or modify ChibiOS/GFX is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
ChibiOS/GFX is distributed in the hope that it will be useful, ChibiOS/GFX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* @file src/gevent/gevent.c * @file src/gevent/gevent.c
* @brief GEVENT Driver code. * @brief GEVENT Driver code.
* *
* @addtogroup GEVENT * @addtogroup GEVENT
* @{ * @{
*/ */
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "gfx.h" #include "gfx.h"
#if GFX_USE_GEVENT || defined(__DOXYGEN__) #if GFX_USE_GEVENT || defined(__DOXYGEN__)
#if GEVENT_ASSERT_NO_RESOURCE #if GEVENT_ASSERT_NO_RESOURCE
#define GEVENT_ASSERT(x) assert(x) #define GEVENT_ASSERT(x) assert(x)
#else #else
#define GEVENT_ASSERT(x) #define GEVENT_ASSERT(x)
#endif #endif
/* This mutex protects access to our tables */ /* This mutex protects access to our tables */
static MUTEX_DECL(geventMutex); static MUTEX_DECL(geventMutex);
/* Our table of listener/source pairs */ /* Our table of listener/source pairs */
static GSourceListener Assignments[GEVENT_MAX_SOURCE_LISTENERS]; static GSourceListener Assignments[GEVENT_MAX_SOURCE_LISTENERS];
/* Loop through the assignment table deleting this listener/source pair. */ /* Loop through the assignment table deleting this listener/source pair. */
/* Null is treated as a wildcard. */ /* Null is treated as a wildcard. */
static void deleteAssignments(GListener *pl, GSourceHandle gsh) { static void deleteAssignments(GListener *pl, GSourceHandle gsh) {
GSourceListener *psl; GSourceListener *psl;
for(psl = Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) { for(psl = Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) {
if ((!pl || psl->pListener == pl) && (!gsh || psl->pSource == gsh)) { if ((!pl || psl->pListener == pl) && (!gsh || psl->pSource == gsh)) {
if (chSemGetCounterI(&psl->pListener->waitqueue) < 0) { if (chSemGetCounterI(&psl->pListener->waitqueue) < 0) {
chBSemWait(&psl->pListener->eventlock); // Obtain the buffer lock chBSemWait(&psl->pListener->eventlock); // Obtain the buffer lock
psl->pListener->event.type = GEVENT_EXIT; // Set up the EXIT event psl->pListener->event.type = GEVENT_EXIT; // Set up the EXIT event
chSemSignal(&psl->pListener->waitqueue); // Wake up the listener chSemSignal(&psl->pListener->waitqueue); // Wake up the listener
chBSemSignal(&psl->pListener->eventlock); // Release the buffer lock chBSemSignal(&psl->pListener->eventlock); // Release the buffer lock
} }
psl->pListener = 0; psl->pListener = 0;
} }
} }
} }
void geventListenerInit(GListener *pl) { void geventListenerInit(GListener *pl) {
chSemInit(&pl->waitqueue, 0); // Next wait'er will block chSemInit(&pl->waitqueue, 0); // Next wait'er will block
chBSemInit(&pl->eventlock, FALSE); // Only one thread at a time looking at the event buffer chBSemInit(&pl->eventlock, FALSE); // Only one thread at a time looking at the event buffer
pl->callback = 0; // No callback active pl->callback = 0; // No callback active
pl->event.type = GEVENT_NULL; // Always safety pl->event.type = GEVENT_NULL; // Always safety
} }
bool_t geventAttachSource(GListener *pl, GSourceHandle gsh, unsigned flags) { bool_t geventAttachSource(GListener *pl, GSourceHandle gsh, unsigned flags) {
GSourceListener *psl, *pslfree; GSourceListener *psl, *pslfree;
// Safety first // Safety first
if (!pl || !gsh) { if (!pl || !gsh) {
GEVENT_ASSERT(FALSE); GEVENT_ASSERT(FALSE);
return FALSE; return FALSE;
} }
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
// Check if this pair is already in the table (scan for a free slot at the same time) // Check if this pair is already in the table (scan for a free slot at the same time)
pslfree = 0; pslfree = 0;
for(psl = Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) { for(psl = Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) {
if (pl == psl->pListener && gsh == psl->pSource) { if (pl == psl->pListener && gsh == psl->pSource) {
// Just update the flags // Just update the flags
chBSemWait(&pl->eventlock); // Safety first - just in case a source is using it chBSemWait(&pl->eventlock); // Safety first - just in case a source is using it
psl->listenflags = flags; psl->listenflags = flags;
chBSemSignal(&pl->eventlock); // Release this lock chBSemSignal(&pl->eventlock); // Release this lock
chMtxUnlock(); chMtxUnlock();
return TRUE; return TRUE;
} }
if (!pslfree && !psl->pListener) if (!pslfree && !psl->pListener)
pslfree = psl; pslfree = psl;
} }
// A free slot was found - allocate it // A free slot was found - allocate it
if (pslfree) { if (pslfree) {
pslfree->pListener = pl; pslfree->pListener = pl;
pslfree->pSource = gsh; pslfree->pSource = gsh;
pslfree->listenflags = flags; pslfree->listenflags = flags;
pslfree->srcflags = 0; pslfree->srcflags = 0;
} }
chMtxUnlock(); chMtxUnlock();
GEVENT_ASSERT(pslfree != 0); GEVENT_ASSERT(pslfree != 0);
return pslfree != 0; return pslfree != 0;
} }
void geventDetachSource(GListener *pl, GSourceHandle gsh) { void geventDetachSource(GListener *pl, GSourceHandle gsh) {
if (pl && gsh) { if (pl) {
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
deleteAssignments(pl, gsh); deleteAssignments(pl, gsh);
if (!gsh && chSemGetCounterI(&pl->waitqueue) < 0) { if (!gsh && chSemGetCounterI(&pl->waitqueue) < 0) {
chBSemWait(&pl->eventlock); // Obtain the buffer lock chBSemWait(&pl->eventlock); // Obtain the buffer lock
pl->event.type = GEVENT_EXIT; // Set up the EXIT event pl->event.type = GEVENT_EXIT; // Set up the EXIT event
chSemSignal(&pl->waitqueue); // Wake up the listener chSemSignal(&pl->waitqueue); // Wake up the listener
chBSemSignal(&pl->eventlock); // Release the buffer lock chBSemSignal(&pl->eventlock); // Release the buffer lock
} }
chMtxUnlock(); chMtxUnlock();
} }
} }
GEvent *geventEventWait(GListener *pl, systime_t timeout) { GEvent *geventEventWait(GListener *pl, systime_t timeout) {
if (pl->callback || chSemGetCounterI(&pl->waitqueue) < 0) if (pl->callback || chSemGetCounterI(&pl->waitqueue) < 0)
return 0; return 0;
return chSemWaitTimeout(&pl->waitqueue, timeout) == RDY_OK ? &pl->event : 0; return chSemWaitTimeout(&pl->waitqueue, timeout) == RDY_OK ? &pl->event : 0;
} }
void geventRegisterCallback(GListener *pl, GEventCallbackFn fn, void *param) { void geventRegisterCallback(GListener *pl, GEventCallbackFn fn, void *param) {
if (pl) { if (pl) {
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
chBSemWait(&pl->eventlock); // Obtain the buffer lock chBSemWait(&pl->eventlock); // Obtain the buffer lock
pl->param = param; // Set the param pl->param = param; // Set the param
pl->callback = fn; // Set the callback function pl->callback = fn; // Set the callback function
if (chSemGetCounterI(&pl->waitqueue) < 0) { if (chSemGetCounterI(&pl->waitqueue) < 0) {
pl->event.type = GEVENT_EXIT; // Set up the EXIT event pl->event.type = GEVENT_EXIT; // Set up the EXIT event
chSemSignal(&pl->waitqueue); // Wake up the listener chSemSignal(&pl->waitqueue); // Wake up the listener
} }
chBSemSignal(&pl->eventlock); // Release the buffer lock chBSemSignal(&pl->eventlock); // Release the buffer lock
chMtxUnlock(); chMtxUnlock();
} }
} }
GSourceListener *geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr) { GSourceListener *geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr) {
GSourceListener *psl; GSourceListener *psl;
// Safety first // Safety first
if (!gsh) if (!gsh)
return 0; return 0;
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
// Unlock the last listener event buffer // Unlock the last listener event buffer
if (lastlr) if (lastlr)
chBSemSignal(&lastlr->pListener->eventlock); chBSemSignal(&lastlr->pListener->eventlock);
// Loop through the table looking for attachments to this source // Loop through the table looking for attachments to this source
for(psl = lastlr ? (lastlr+1) : Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) { for(psl = lastlr ? (lastlr+1) : Assignments; psl < Assignments+GEVENT_MAX_SOURCE_LISTENERS; psl++) {
if (gsh == psl->pSource) { if (gsh == psl->pSource) {
chBSemWait(&psl->pListener->eventlock); // Obtain a lock on the listener event buffer chBSemWait(&psl->pListener->eventlock); // Obtain a lock on the listener event buffer
chMtxUnlock(); chMtxUnlock();
return psl; return psl;
} }
} }
chMtxUnlock(); chMtxUnlock();
return 0; return 0;
} }
GEvent *geventGetEventBuffer(GSourceListener *psl) { GEvent *geventGetEventBuffer(GSourceListener *psl) {
// We already know we have the event lock // We already know we have the event lock
return &psl->pListener->callback || chSemGetCounterI(&psl->pListener->waitqueue) < 0 ? &psl->pListener->event : 0; return &psl->pListener->callback || chSemGetCounterI(&psl->pListener->waitqueue) < 0 ? &psl->pListener->event : 0;
} }
void geventSendEvent(GSourceListener *psl) { void geventSendEvent(GSourceListener *psl) {
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
if (psl->pListener->callback) { // This test needs to be taken inside the mutex if (psl->pListener->callback) { // This test needs to be taken inside the mutex
chMtxUnlock(); chMtxUnlock();
// We already know we have the event lock // We already know we have the event lock
psl->pListener->callback(psl->pListener->param, &psl->pListener->event); psl->pListener->callback(psl->pListener->param, &psl->pListener->event);
} else { } else {
// Wake up the listener // Wake up the listener
if (chSemGetCounterI(&psl->pListener->waitqueue) < 0) if (chSemGetCounterI(&psl->pListener->waitqueue) < 0)
chSemSignal(&psl->pListener->waitqueue); chSemSignal(&psl->pListener->waitqueue);
chMtxUnlock(); chMtxUnlock();
} }
} }
void geventDetachSourceListeners(GSourceHandle gsh) { void geventDetachSourceListeners(GSourceHandle gsh) {
chMtxLock(&geventMutex); chMtxLock(&geventMutex);
deleteAssignments(0, gsh); deleteAssignments(0, gsh);
chMtxUnlock(); chMtxUnlock();
} }
#endif /* GFX_USE_GEVENT */ #endif /* GFX_USE_GEVENT */
/** @} */ /** @} */

File diff suppressed because it is too large Load Diff