diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2011-03-17 23:58:19 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2011-03-17 23:58:19 +0200 |
commit | f4a5bae0c010e3c20d6420c9de2bb74182b26b6e (patch) | |
tree | ac120eaa84821ac21fd51235829c1db82570d230 | |
parent | b170dc2b10df0e66acc5bc0bb89ba2c68a755f12 (diff) |
events are working )
-rw-r--r-- | api/ec_pluginapi.h | 12 | ||||
-rw-r--r-- | core/events.cpp | 20 | ||||
-rw-r--r-- | core/events.h | 3 | ||||
-rw-r--r-- | core/main.cpp | 2 | ||||
-rw-r--r-- | core/services.cpp | 4 | ||||
-rw-r--r-- | core/services.h | 2 | ||||
-rw-r--r-- | plugins/example/main.cpp | 51 |
7 files changed, 71 insertions, 23 deletions
diff --git a/api/ec_pluginapi.h b/api/ec_pluginapi.h index e58b55a..67b7c7d 100644 --- a/api/ec_pluginapi.h +++ b/api/ec_pluginapi.h @@ -34,16 +34,16 @@ typedef void* (*EVENT_HANDLER)(void*); typedef struct { - void (*CreateServiceFunction)(const char *,SERVICE); - void* (*CallService)(const char *,void*); - bool (*ServiceExists)(const char *); - bool (*RegisterEventHandler)(int, EVENT_HANDLER); - int (*RegisterEventType)(int, int); //event type, special id + void (*CreateServiceFunction)(const char *,SERVICE); //register new service "service name" in core (will be avaible for all modules) + void* (*CallService)(const char *,void*); //execute "service name" service with custom data (service dependent) + int (*ServiceExists)(const char *); //check if "service name" service exists + int (*RegisterEventHandler)(int, EVENT_HANDLER); //event type, handler function, return 0 on success + int (*RegisterEventType)(int, int); //event type, special id, retrun 0 on success + void* (*ExecuteEvent)(int, int, void*); //event type, special id, custom data } PLUGINLINK; typedef struct { -// int size; wchar_t *name, *description, *author, *authoremail; unsigned long version; int pluginid; //special id will be set by core, neede to call some core services diff --git a/core/events.cpp b/core/events.cpp index a177756..734557e 100644 --- a/core/events.cpp +++ b/core/events.cpp @@ -36,7 +36,7 @@ void* evt_handler::Execute(void* data) return handler(data); } -bool RegisterEventHandler(int type, EVENT_HANDLER func) +int RegisterEventHandler(int type, EVENT_HANDLER func) { if(!event_handlers.empty()) { @@ -55,7 +55,7 @@ bool RegisterEventHandler(int type, EVENT_HANDLER func) event_handlers_mutex.unlock(); } logger.log(LM_DEBUG, "failed to add handler for event %d, event %d not registerd to any plugin or core", type,type); - return true; + return 1; } int RegisterEventType(int type, int specialid) { @@ -69,14 +69,14 @@ int RegisterEventType(int type, int specialid) if((*i)->get_plugin_id() == specialid) { p = *i; - logger.log(LM_DEBUG, "special id is right, plugin is in plugin list.\n"); + logger.log(LM_DEBUG, "special id %d is right, plugin \"%s\" is in plugin list.\n", specialid, toUTF8(p->get_plugininfo()->name).c_str()); break; } } plugin_list_mutex.unlock(); if(!p) { - logger.log(LM_DEBUG, "event registration for plugin: %s failed, trying to register event with wrong special id %d, right special id for this plugin is %d.\n", p->get_plugininfo()->name, specialid, p->get_plugininfo()->pluginid); + logger.log(LM_DEBUG, "event registration for plugin: \"%s\" failed, trying to register event with wrong special id %d, right special id for this plugin is %d.\n", toUTF8(p->get_plugininfo()->name).c_str(), specialid, p->get_plugininfo()->pluginid); return 1; } } @@ -93,7 +93,7 @@ int RegisterEventType(int type, int specialid) { if((*i)->getType() == type) { - logger.log(LM_DEBUG, "event registration for plugin %s failed, event already registered", p->get_plugininfo()->name); + logger.log(LM_DEBUG, "event registration for plugin \"%s\" failed, event already registered", toUTF8(p->get_plugininfo()->name).c_str()); event_handlers_mutex.unlock(); return 1; } @@ -102,10 +102,11 @@ int RegisterEventType(int type, int specialid) } event_handlers.push_back(new evt_handler(type)); p->register_event(type); + logger.log(LM_DEBUG, "event %d registration for plugin \"%s\" succesful.\n", type, toUTF8(p->get_plugininfo()->name).c_str()); return 0; } -void* ExecuteEvent(int type,void* data) +void* ExecuteEvent(int type, int pluginid, void* data) { if(!event_handlers.empty()) { @@ -113,9 +114,12 @@ void* ExecuteEvent(int type,void* data) for(std::list<evt_handler*>::iterator i = event_handlers.begin(); i != end; ++i) { if((*i)->getType() == type) + { + logger.log(LM_DEBUG, "event %d found, executing...\n", type); return (*i)->Execute(data); + } } } - event_handlers.push_back(new evt_handler(type)); -// return (void*)&event_handlers.back()->Execute; TODO: + logger.log(LM_DEBUG, "event %d not found...\n", type); + return 0; } diff --git a/core/events.h b/core/events.h index 17b2497..c5e1dc4 100644 --- a/core/events.h +++ b/core/events.h @@ -16,8 +16,9 @@ #ifndef EVENTS_H_INCLUDED #define EVENTS_H_INCLUDED -bool RegisterEventHandler(int, EVENT_HANDLER); +int RegisterEventHandler(int, EVENT_HANDLER); int RegisterEventType(int, int); +void* ExecuteEvent(int type, int pluginid, void* data); class evt_handler{ public: diff --git a/core/main.cpp b/core/main.cpp index 73e88f3..29d2bd1 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -28,7 +28,7 @@ int current_plugin_id = 13; bool halt_requested = false; -PLUGINLINK pluglink = {&CreateServiceFunction, &CallService, &ServiceExists, &RegisterEventHandler, &RegisterEventType}; +PLUGINLINK pluglink = {&CreateServiceFunction, &CallService, &ServiceExists, &RegisterEventHandler, &RegisterEventType, &ExecuteEvent}; int on_exit() { diff --git a/core/services.cpp b/core/services.cpp index 3344ca4..903ee1a 100644 --- a/core/services.cpp +++ b/core/services.cpp @@ -20,7 +20,7 @@ extern std::list<service*> services; extern boost::mutex service_list_mutex; -bool ServiceExists(const char *name); +int ServiceExists(const char *name); void CreateServiceFunction(const char* name, SERVICE svc) { if(!ServiceExists(name)) @@ -45,7 +45,7 @@ void* CallService(const char *name,void* data) } return 0; } -bool ServiceExists(const char *name) +int ServiceExists(const char *name) { service_list_mutex.lock(); if(!services.empty()) diff --git a/core/services.h b/core/services.h index 61d111b..15da2f3 100644 --- a/core/services.h +++ b/core/services.h @@ -33,7 +33,7 @@ private: void CreateServiceFunction(const char* name, SERVICE svc); void* CallService(const char *,void*); -bool ServiceExists(const char *); +int ServiceExists(const char *); #endif // SERVICES_H_INCLUDED
diff --git a/plugins/example/main.cpp b/plugins/example/main.cpp index c556d47..00fde1e 100644 --- a/plugins/example/main.cpp +++ b/plugins/example/main.cpp @@ -18,19 +18,62 @@ extern PLUGININFO pluginInfo; IMPLEMENT_APP_NO_MAIN(wxPluginForEvilCore) //main() does not needed, we will create instance by hand later +const int EVENT_TEST = 666; + +EVENT_HANDLER our_handler(void* data) //this is event handler fuction +{ + return (void*(*)(void*))data; //just return data which we obtaned +} + bool wxPluginForEvilCore::OnInit() { wxMessageBox(_T("I am a plugin example") ,_T("Info"), wxOK | wxICON_INFORMATION); int *core_version = (int*)CallService("EC/GetVersionInt", NULL); //using service implemented somewhere (in core, in plugins, in other place), NULL is data required for service if(core_version) { - wxChar msg[32]; - wxSnprintf(msg, 31, _T("Core version is %d."), core_version); + wxChar msg[64]; + wxSnprintf(msg, 63, _T("Core version is %d."), core_version); wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); } { - wxChar msg [32]; - wxSnprintf(msg, 31, _T("I have obtained special id %d from core"), pluginInfo.pluginid); //we must obtain special plugin id for calling services like raise event or so. + wxChar msg [64]; + wxSnprintf(msg, 63, _T("I have obtained special id %d from core"), pluginInfo.pluginid); //we must obtain special plugin id for calling services like raise event or so. + wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); + } + { //trying to register event type + if(!pluginLink->RegisterEventType(EVENT_TEST, pluginInfo.pluginid)) + { + wxChar msg[64]; + wxSnprintf(msg, 63, _T("we have succesfuly registered event %d"), EVENT_TEST); + wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); + } + else + { + wxChar msg[64]; + wxSnprintf(msg, 63, _T("we have failed to register event %d"), EVENT_TEST); + wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); + } + } + {//here we trying to handle our event type + if(!pluginLink->RegisterEventHandler(EVENT_TEST, (void* (*)(void*))our_handler)) + { + wxChar msg[64]; + wxSnprintf(msg, 63, _T("we have succesfuly registered event handler")); + wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); + } + else + { + wxChar msg[64]; + wxSnprintf(msg, 63, _T("we have failed to register event handler")); + wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); + } + } + { // raising our event + char* string = (char*)pluginLink->ExecuteEvent(EVENT_TEST, pluginInfo.pluginid, (void*)"this is custom data for test"); + wxChar msg[128]; + wchar_t* str = (wchar_t*)CallService("EC/toUtf16", (void*)string); //and amother example of core service usage + wxSnprintf(msg, 127, _T("we have obtained string \"%s\" in custom data"), str); + free(str); //should be freed wxMessageBox(msg, _T("Info"), wxOK | wxICON_INFORMATION); } return true; |