From fbf3208b0a4d699c1a0480fd324af71e7aff8205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Tue, 18 Jun 2013 21:23:26 -0400 Subject: [PATCH] libwinpr-utils: rename certain PubSub functions --- libfreerdp/core/freerdp.c | 4 +-- winpr/include/winpr/collections.h | 13 +++++----- winpr/libwinpr/utils/collections/PubSub.c | 30 +++++++++++------------ winpr/libwinpr/utils/test/TestPubSub.c | 24 ++++-------------- 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/libfreerdp/core/freerdp.c b/libfreerdp/core/freerdp.c index e00b5db8c..9d73e5e96 100644 --- a/libfreerdp/core/freerdp.c +++ b/libfreerdp/core/freerdp.c @@ -318,7 +318,7 @@ void freerdp_get_version(int* major, int* minor, int* revision) *revision = FREERDP_VERSION_REVISION; } -static wEvent FreeRDP_Events[] = +static wEventType FreeRDP_Events[] = { DEFINE_EVENT_ENTRY(WindowStateChange) DEFINE_EVENT_ENTRY(ResizeWindow) @@ -345,7 +345,7 @@ int freerdp_context_new(freerdp* instance) context = instance->context; context->pubSub = PubSub_New(TRUE); - PubSub_Publish(context->pubSub, FreeRDP_Events, sizeof(FreeRDP_Events) / sizeof(wEvent)); + PubSub_AddEventTypes(context->pubSub, FreeRDP_Events, sizeof(FreeRDP_Events) / sizeof(wEventType)); rdp = rdp_new(instance); instance->input = rdp->input; diff --git a/winpr/include/winpr/collections.h b/winpr/include/winpr/collections.h index a54e2cc87..03d06a9a8 100644 --- a/winpr/include/winpr/collections.h +++ b/winpr/include/winpr/collections.h @@ -352,14 +352,14 @@ typedef void (*pEventHandler)(void* context, wEventArgs* e); #define MAX_EVENT_HANDLERS 32 -struct _wEvent +struct _wEventType { const char* EventName; wEventArgs EventArgs; int EventHandlerCount; pEventHandler EventHandlers[MAX_EVENT_HANDLERS]; }; -typedef struct _wEvent wEvent; +typedef struct _wEventType wEventType; #define EventArgsInit(_event_args, _sender) \ memset(_event_args, 0, sizeof(*_event_args)); \ @@ -406,17 +406,16 @@ struct _wPubSub int size; int count; - wEvent* events; + wEventType* events; }; typedef struct _wPubSub wPubSub; WINPR_API BOOL PubSub_Lock(wPubSub* pubSub); WINPR_API BOOL PubSub_Unlock(wPubSub* pubSub); -WINPR_API wEvent* PubSub_Events(wPubSub* pubSub, int* count); -WINPR_API wEvent* PubSub_FindEvent(wPubSub* pubSub, const char* EventName); - -WINPR_API void PubSub_Publish(wPubSub* pubSub, wEvent* events, int count); +WINPR_API wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count); +WINPR_API void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count); +WINPR_API wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName); WINPR_API int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler); WINPR_API int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler); diff --git a/winpr/libwinpr/utils/collections/PubSub.c b/winpr/libwinpr/utils/collections/PubSub.c index 59b0e7af3..160a0ddf0 100644 --- a/winpr/libwinpr/utils/collections/PubSub.c +++ b/winpr/libwinpr/utils/collections/PubSub.c @@ -34,7 +34,7 @@ * Properties */ -wEvent* PubSub_Events(wPubSub* pubSub, int* count) +wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count) { if (count) *count = pubSub->count; @@ -56,10 +56,10 @@ BOOL PubSub_Unlock(wPubSub* pubSub) return ReleaseMutex(pubSub->mutex); } -wEvent* PubSub_FindEvent(wPubSub* pubSub, const char* EventName) +wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName) { int index; - wEvent* event = NULL; + wEventType* event = NULL; for (index = 0; pubSub->count; index++) { @@ -73,7 +73,7 @@ wEvent* PubSub_FindEvent(wPubSub* pubSub, const char* EventName) return event; } -void PubSub_Publish(wPubSub* pubSub, wEvent* events, int count) +void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count) { if (pubSub->synchronized) PubSub_Lock(pubSub); @@ -81,10 +81,10 @@ void PubSub_Publish(wPubSub* pubSub, wEvent* events, int count) while (pubSub->count + count >= pubSub->size) { pubSub->size *= 2; - pubSub->events = (wEvent*) realloc(pubSub->events, pubSub->size); + pubSub->events = (wEventType*) realloc(pubSub->events, pubSub->size); } - CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEvent)); + CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEventType)); pubSub->count += count; if (pubSub->synchronized) @@ -93,13 +93,13 @@ void PubSub_Publish(wPubSub* pubSub, wEvent* events, int count) int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler) { - wEvent* event; + wEventType* event; int status = -1; if (pubSub->synchronized) PubSub_Lock(pubSub); - event = PubSub_FindEvent(pubSub, EventName); + event = PubSub_FindEventType(pubSub, EventName); if (event) { @@ -125,13 +125,13 @@ int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, pEventHandler Event int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler) { int index; - wEvent* event; + wEventType* event; int status = -1; if (pubSub->synchronized) PubSub_Lock(pubSub); - event = PubSub_FindEvent(pubSub, EventName); + event = PubSub_FindEventType(pubSub, EventName); if (event) { @@ -144,7 +144,7 @@ int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler Eve event->EventHandlers[index] = NULL; event->EventHandlerCount--; MoveMemory(&event->EventHandlers[index], &event->EventHandlers[index + 1], - (MAX_EVENT_HANDLERS - index - 1) * sizeof(wEvent)); + (MAX_EVENT_HANDLERS - index - 1) * sizeof(wEventType)); status = 1; } } @@ -159,13 +159,13 @@ int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler Eve int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, wEventArgs* e) { int index; - wEvent* event; + wEventType* event; int status = -1; if (pubSub->synchronized) PubSub_Lock(pubSub); - event = PubSub_FindEvent(pubSub, EventName); + event = PubSub_FindEventType(pubSub, EventName); if (event) { @@ -207,8 +207,8 @@ wPubSub* PubSub_New(BOOL synchronized) pubSub->count = 0; pubSub->size = 64; - pubSub->events = (wEvent*) malloc(sizeof(wEvent) * pubSub->size); - ZeroMemory(pubSub->events, sizeof(wEvent) * pubSub->size); + pubSub->events = (wEventType*) malloc(sizeof(wEventType) * pubSub->size); + ZeroMemory(pubSub->events, sizeof(wEventType) * pubSub->size); } return pubSub; diff --git a/winpr/libwinpr/utils/test/TestPubSub.c b/winpr/libwinpr/utils/test/TestPubSub.c index 68e4cb3cc..78478b732 100644 --- a/winpr/libwinpr/utils/test/TestPubSub.c +++ b/winpr/libwinpr/utils/test/TestPubSub.c @@ -25,25 +25,13 @@ void MouseButtonEventHandler(void* context, MouseButtonEventArgs* e) printf("MouseButtonEvent: x: %d y: %d flags: %d button: %d\n", e->x, e->y, e->flags, e->button); } -static wEvent Node_Events[] = +static wEventType Node_Events[] = { DEFINE_EVENT_ENTRY(MouseMotion) DEFINE_EVENT_ENTRY(MouseButton) }; -#define NODE_EVENT_COUNT (sizeof(Node_Events) / sizeof(wEvent)) - -/* strongly-typed wrappers could be automatically defined using a macro */ - -static INLINE int PubSub_SubscribeMouseMotion(wPubSub* pubSub, pMouseMotionEventHandler MouseMotionEventHandler) -{ - return PubSub_Subscribe(pubSub, "MouseMotion", (pEventHandler) MouseMotionEventHandler); -} - -static INLINE int PubSub_OnMouseMotion(wPubSub* pubSub, void* context, MouseMotionEventArgs* e) -{ - return PubSub_OnEvent(pubSub, "MouseMotion", context, (wEventArgs*) e); -} +#define NODE_EVENT_COUNT (sizeof(Node_Events) / sizeof(wEventType)) int TestPubSub(int argc, char* argv[]) { @@ -51,12 +39,10 @@ int TestPubSub(int argc, char* argv[]) node = PubSub_New(TRUE); - PubSub_Publish(node, Node_Events, NODE_EVENT_COUNT); - - /* Register Event Handler */ + PubSub_AddEventTypes(node, Node_Events, NODE_EVENT_COUNT); PubSub_SubscribeMouseMotion(node, MouseMotionEventHandler); - PubSub_Subscribe(node, "MouseButton", (pEventHandler) MouseButtonEventHandler); + PubSub_SubscribeMouseButton(node, MouseButtonEventHandler); /* Call Event Handler */ { @@ -76,7 +62,7 @@ int TestPubSub(int argc, char* argv[]) e.flags = 7; e.button = 1; - PubSub_OnEvent(node, "MouseButton", NULL, (wEventArgs*) &e); + PubSub_OnMouseButton(node, NULL, &e); } PubSub_Free(node);