2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_event.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01: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, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01: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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_EVENT_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
|
|
|
|
#include "gbx_event.h"
|
|
|
|
|
|
|
|
//#define DEBUG_ME 1
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void *EVENT_Last = NULL;
|
2010-12-21 15:05:42 +01:00
|
|
|
|
|
|
|
char *EVENT_PreviousName = NULL;
|
2009-07-08 21:57:50 +02:00
|
|
|
char *EVENT_Name = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static EVENT_POST *_post_list = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static void check_event_method(CLASS *class, const char *name, CLASS_DESC_METHOD *desc, CLASS_EVENT *event)
|
|
|
|
{
|
2012-10-22 18:22:15 +02:00
|
|
|
int n = event->n_param;
|
|
|
|
|
|
|
|
if (n < desc->npmin)
|
2011-09-26 03:46:48 +02:00
|
|
|
{
|
|
|
|
/*printf("event->n_param = %d desc->npmin = %d desc->npmax = %d\n", event->n_param, desc->npmin, desc->npmax);*/
|
2012-10-19 23:00:35 +02:00
|
|
|
THROW(E_EVENT, CLASS_get_name(class), name, "Too many arguments");
|
2011-09-26 03:46:48 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-22 18:22:15 +02:00
|
|
|
if (n > desc->npmax)
|
|
|
|
{
|
|
|
|
if (!desc->npvar)
|
|
|
|
THROW(E_EVENT, CLASS_get_name(class), name, "Not enough arguments");
|
|
|
|
|
|
|
|
n = desc->npmax;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-10-22 18:22:15 +02:00
|
|
|
if (TYPE_compare_signature(desc->signature, n, (TYPE *)event->param, n))
|
2012-10-19 23:00:35 +02:00
|
|
|
THROW(E_EVENT, CLASS_get_name(class), name, "Type mismatch");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (desc->type != T_VOID)
|
2012-10-19 23:00:35 +02:00
|
|
|
THROW(E_EVENT, CLASS_get_name(class), name, "Not a procedure");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void EVENT_search(CLASS *class, ushort *event, const char *name, OBJECT *parent)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
//ushort *event;
|
|
|
|
//ushort *self;
|
2011-09-26 03:46:48 +02:00
|
|
|
CLASS *class_parent;
|
|
|
|
int i, index;
|
|
|
|
const char *event_name;
|
|
|
|
CLASS_DESC *desc;
|
|
|
|
int kind;
|
|
|
|
int len_name, len_event_name;
|
|
|
|
char *func_name = COMMON_buffer;
|
|
|
|
char *pevent_name;
|
|
|
|
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "EVENT_search: class = %s name = %s parent = %p\n", class->name, name, parent);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//event = OBJECT_event(object)->event;
|
|
|
|
//self = OBJECT_event(object)->self;
|
|
|
|
|
|
|
|
if (class->n_event <= 0)
|
|
|
|
return;
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
len_name = name ? strlen(name) : 0;
|
|
|
|
|
|
|
|
if (len_name == 0 || len_name >= (COMMON_BUF_MAX - 4))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
memset(event, 0, class->n_event * sizeof(ushort));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
kind = parent->class == CLASS_Class ? CD_STATIC_METHOD : CD_METHOD;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
if (kind == CD_STATIC_METHOD)
|
|
|
|
class_parent = (CLASS *)parent;
|
|
|
|
else
|
|
|
|
class_parent = parent->class;
|
|
|
|
|
|
|
|
strcpy(func_name, name);
|
|
|
|
pevent_name = &func_name[len_name];
|
|
|
|
*pevent_name++ = '_';
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
for (i = 0; i < class->n_event; i++)
|
|
|
|
{
|
|
|
|
event[i] = 0;
|
|
|
|
|
|
|
|
event_name = class->event[i].name;
|
|
|
|
if (*event_name == ':')
|
|
|
|
event_name++;
|
|
|
|
|
|
|
|
len_event_name = strlen(event_name);
|
|
|
|
if ((len_name + len_event_name + 1) >= COMMON_BUF_MAX)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strcpy(pevent_name, event_name);
|
2007-12-30 17:41:49 +01:00
|
|
|
index = CLASS_find_symbol(class_parent, func_name);
|
|
|
|
|
|
|
|
if (index != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
desc = class_parent->table[index].desc;
|
|
|
|
if (CLASS_DESC_get_type(desc) == kind)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
fprintf(stderr, "EVENT_search: FOUND [%d] %s.%s index = %d parent = %s.%p\n", i, class_parent->name, func_name, index, parent->class->name, parent);
|
|
|
|
#endif
|
|
|
|
check_event_method(class_parent, func_name, &desc->method, &class->event[i]);
|
|
|
|
event[i] = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
#if 0
|
2007-12-30 17:41:49 +01:00
|
|
|
snprintf(func_name, COMMON_BUF_MAX, "ME_%s", event_name);
|
|
|
|
index = CLASS_find_symbol(class, func_name);
|
2011-09-26 03:46:48 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (index != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
desc = class->table[index].desc;
|
|
|
|
if (CLASS_DESC_get_type(desc) == CD_METHOD)
|
|
|
|
{
|
|
|
|
#if DEBUG_ME
|
|
|
|
printf("EVENT_search: FOUND [%d] %s.%s index = %d parent = %s.%p\n", i, class->name, func_name, index, parent->class->name, parent);
|
|
|
|
#endif
|
|
|
|
check_event_method(class, func_name, &desc->method, &class->event[i]);
|
|
|
|
if (!self)
|
|
|
|
{
|
|
|
|
ALLOC_ZERO(&self, sizeof(ushort) * class->n_event, "EVENT_search");
|
|
|
|
OBJECT_event(object)->self = self;
|
|
|
|
}
|
|
|
|
self[i] = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-26 03:46:48 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void EVENT_exit()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-26 03:46:48 +02:00
|
|
|
EVENT_POST *ep;
|
|
|
|
|
|
|
|
while (_post_list)
|
|
|
|
{
|
|
|
|
ep = _post_list;
|
|
|
|
_post_list = _post_list->list.next;
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&ep);
|
2011-09-26 03:46:48 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
STRING_free(&EVENT_Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void post(void (*func)(), int nparam, intptr_t param, intptr_t param2)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-26 03:46:48 +02:00
|
|
|
EVENT_POST *ep;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
/*printf("EVENT_post\n");
|
|
|
|
fflush(NULL);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&ep, sizeof(EVENT_POST));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
ep->func = func;
|
|
|
|
ep->nparam = nparam;
|
|
|
|
ep->param = param;
|
|
|
|
ep->param2 = param2;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
LIST_insert(&_post_list, ep, &ep->list);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
HOOK(post)();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void EVENT_post(void (*func)(), intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
post(func, 1, param, 0);
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void EVENT_post2(void (*func)(), intptr_t param, intptr_t param2)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
post(func, 2, param, param2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void post_event(void *object, int event)
|
|
|
|
{
|
|
|
|
GB_Raise(object, event, 0);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(object);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void EVENT_post_event(void *object, int event)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(object);
|
2008-01-17 22:39:26 +01:00
|
|
|
post((void (*)())post_event, 2, (intptr_t)object, (intptr_t)event);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool EVENT_check_post(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-26 03:46:48 +02:00
|
|
|
EVENT_POST *ep;
|
|
|
|
EVENT_POST save;
|
|
|
|
bool ret = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
#ifdef DEBUG_POST
|
|
|
|
fprintf(stderr, "EVENT_check_post: START\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
while (_post_list)
|
|
|
|
{
|
|
|
|
ret = TRUE;
|
|
|
|
ep = _post_list;
|
|
|
|
save = *ep;
|
|
|
|
LIST_remove(&_post_list, _post_list, &_post_list->list);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&ep);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (save.nparam == 1)
|
2011-09-26 03:46:48 +02:00
|
|
|
(*save.func)(save.param);
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2011-09-26 03:46:48 +02:00
|
|
|
(*save.func)(save.param, save.param2);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
/*norec = FALSE;*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
#ifdef DEBUG_POST
|
|
|
|
fprintf(stderr, "EVENT_check_post: END\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-26 03:46:48 +02:00
|
|
|
return ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-12-21 15:05:42 +01:00
|
|
|
|
|
|
|
char *EVENT_enter_name(const char *name)
|
|
|
|
{
|
|
|
|
char *save = EVENT_PreviousName;
|
|
|
|
EVENT_PreviousName = EVENT_Name;
|
2010-12-21 18:29:39 +01:00
|
|
|
EVENT_Name = (char *)name;
|
2010-12-21 15:05:42 +01:00
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVENT_leave_name(const char *save)
|
|
|
|
{
|
|
|
|
EVENT_Name = EVENT_PreviousName;
|
2010-12-21 18:29:39 +01:00
|
|
|
EVENT_PreviousName = (char *)save;
|
2010-12-21 15:05:42 +01:00
|
|
|
}
|