1
0

waclient.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // the glue!
  2. #include <precomp.h>
  3. #include <wasabicfg.h>
  4. #include <bfc/assert.h>
  5. #include "wac.h"
  6. #include <api/service/servicei.h>
  7. #include <api/memmgr/api_memmgr.h>
  8. #ifndef STATIC_COMPONENT
  9. extern WAComponentClient *the;
  10. #ifndef WASABINOMAINAPI
  11. ComponentAPI *api; // we do NOT delete this on shutdown!!!
  12. #endif
  13. OSMODULEHANDLE g_modulehandle;
  14. #endif //STATIC_COMPONENT
  15. WAComponentClient::WAComponentClient(const wchar_t *name) :
  16. CfgItemI(name) {
  17. OSModuleHandle = NULL;
  18. }
  19. const wchar_t *WAComponentClient::getName() {
  20. return cfgitem_getName();
  21. }
  22. #ifdef WASABINOMAINAPI
  23. void WAComponentClient::registerServices(api_service *_serviceapi)
  24. {
  25. serviceApi = _serviceapi;
  26. waServiceFactory *sf = serviceApi->service_getServiceByGuid(applicationApiServiceGuid);
  27. if (sf) applicationApi = reinterpret_cast<api_application*>(sf->getInterface());
  28. #ifdef WASABI_COMPILE_SYSCB
  29. sf = serviceApi->service_getServiceByGuid(syscbApiServiceGuid);
  30. if (sf) sysCallbackApi = reinterpret_cast<api_syscb *>(sf->getInterface());
  31. #endif
  32. sf = serviceApi->service_getServiceByGuid(memMgrApiServiceGuid);
  33. if (sf) memmgrApi = reinterpret_cast<api_memmgr *>(sf->getInterface());
  34. #ifdef WASABI_COMPILE_CONFIG
  35. sf = serviceApi->service_getServiceByGuid(configApiServiceGuid);
  36. if (sf) configApi = reinterpret_cast<api_config *>(sf->getInterface());
  37. #endif
  38. #ifdef WASABI_COMPILE_IMGLDR
  39. sf = serviceApi->service_getServiceByGuid(imgLdrApiServiceGuid);
  40. if (sf) imgLoaderApi = reinterpret_cast<imgldr_api *>(sf->getInterface());
  41. #endif
  42. #ifdef WASABI_COMPILE_FONTS
  43. sf = serviceApi->service_getServiceByGuid(fontApiServiceGuid);
  44. if (sf) fontApi = reinterpret_cast<api_font *>(sf->getInterface());
  45. #endif
  46. #ifdef WASABI_COMPILE_FILEREADER
  47. sf = serviceApi->service_getServiceByGuid(fileReaderApiServiceGuid);
  48. if (sf) fileApi = reinterpret_cast<api_fileReader *>(sf->getInterface());
  49. #endif
  50. #ifdef WASABI_COMPILE_LOCALES
  51. sf = serviceApi->service_getServiceByGuid(localesApiServiceGuid);
  52. if (sf) localesApi = reinterpret_cast<api_locales *>(sf->getInterface());
  53. #endif
  54. #ifdef WASABI_COMPILE_TIMERS
  55. sf = serviceApi->service_getServiceByGuid(timerApiServiceGuid);
  56. if (sf) timerApi = reinterpret_cast<timer_api *>(sf->getInterface());
  57. #endif
  58. #ifdef WASABI_COMPILE_WND
  59. sf = serviceApi->service_getServiceByGuid(wndApiServiceGuid);
  60. if (sf) wndApi = reinterpret_cast<wnd_api *>(sf->getInterface());
  61. #endif
  62. #ifdef WASABI_COMPILE_WNDMGR
  63. sf = serviceApi->service_getServiceByGuid(wndMgrApiServiceGuid);
  64. if (sf) wndManagerApi = reinterpret_cast<wndmgr_api *>(sf->getInterface());
  65. #endif
  66. #ifdef WASABI_COMPILE_COMPONENTS
  67. #endif
  68. #ifdef WASABI_COMPILE_METADB
  69. #endif
  70. #ifdef WASABI_COMPILE_SCRIPT
  71. sf = serviceApi->service_getServiceByGuid(makiApiServiceGuid);
  72. if (sf) makiApi = reinterpret_cast<api_maki *>(sf->getInterface());
  73. #endif
  74. #ifdef WASABI_COMPILE_SKIN
  75. sf = serviceApi->service_getServiceByGuid(skinApiServiceGuid);
  76. if (sf) skinApi = reinterpret_cast<api_skin *>(sf->getInterface());
  77. #endif
  78. #ifdef WASABI_COMPILE_MAKIDEBUG
  79. sf = serviceApi->service_getServiceByGuid(makiDebugApiServiceGuid);
  80. if (sf) debugApi = reinterpret_cast<api_makiDebugger *>(sf->getInterface());
  81. #endif
  82. #else
  83. void WAComponentClient::registerServices(ComponentAPI *_api) {
  84. // save API *
  85. api = _api;
  86. #endif
  87. // propagate name from component to cfgitemi
  88. if (cfgitem_getName() == NULL) CfgItemI::setName(WaComponent::getName());
  89. // register resources
  90. foreach(resources)
  91. resources.getfor()->onRegisterServices();
  92. endfor
  93. // our CfgItem GUID defaults to our component GUID
  94. cfgitem_setGUID(getGUID());
  95. // by default, peg us under "Installed Components" in config
  96. // {99CFD75C-1CA7-49e5-B8C0-7D78AA443C10}
  97. const GUID installed_guid =
  98. { 0x99cfd75c, 0x1ca7, 0x49e5, { 0xb8, 0xc0, 0x7d, 0x78, 0xaa, 0x44, 0x3c, 0x10 } };
  99. if (cfgitem_getParentGuid() == INVALID_GUID)
  100. setParentGuid(installed_guid);
  101. WASABI_API_CONFIG->config_registerCfgItem(this);
  102. // allow overridden component class to do stuff
  103. onRegisterServices();
  104. }
  105. int WAComponentClient::RegisterServicesSafeModeOk()
  106. {
  107. return 1;
  108. }
  109. void WAComponentClient::deregisterServices() {
  110. foreach(resources)
  111. resources.getfor()->onDeregisterServices();
  112. endfor
  113. WASABI_API_CONFIG->config_deregisterCfgItem(this);
  114. }
  115. CfgItem *WAComponentClient::getCfgInterface(int n) {
  116. if (n == 0) return this;
  117. return NULL;
  118. }
  119. class RegService : public LoadableResource {
  120. public:
  121. RegService(waServiceFactoryI *sf) : factory(sf) {}
  122. virtual void onRegisterServices() {
  123. WASABI_API_SVC->service_register(factory);
  124. }
  125. virtual void onDeregisterServices() {
  126. WASABI_API_SVC->service_deregister(factory);
  127. }
  128. private:
  129. waServiceFactoryI *factory;
  130. };
  131. void WAComponentClient::registerService(waServiceFactoryI* service) {
  132. registerResource(new RegService(service));
  133. }
  134. class SkinPart : public LoadableResource {
  135. public:
  136. SkinPart(const wchar_t *name, int rel) : filename(name), id(-1), relative(rel) {}
  137. void beforeLoadingSkinElements() {
  138. ASSERT(!filename.isempty());
  139. StringW fn(filename);
  140. switch (relative) {
  141. case RSF_RELATIVETOWAC:
  142. fn.prepend(the->getComponentPath());// api->path_getComponentPath());
  143. break;
  144. case RSF_RELATIVETOTHEME: {
  145. wchar_t theme[WA_MAX_PATH]=L"";
  146. WASABI_API_CONFIG->getStringPrivate(L"theme", theme, WA_MAX_PATH, L"default");
  147. StringW path = WASABI_API_APP->path_getAppPath();
  148. path.AppendPath(L"themes");
  149. path.AppendPath(theme);
  150. fn = StringPathCombine(path, fn);
  151. }
  152. break;
  153. }
  154. id = WASABI_API_SKIN->loadSkinFile(fn);
  155. }
  156. virtual void onDeregisterServices() {
  157. if (id >= 0 && WASABI_API_SKIN) WASABI_API_SKIN->unloadSkinPart(id);
  158. id = -1;
  159. }
  160. private:
  161. StringW filename;
  162. int id;
  163. int relative;
  164. };
  165. void WAComponentClient::registerSkinFile(const wchar_t *filename, int relative)
  166. {
  167. registerResource(new SkinPart(filename, relative));
  168. }
  169. void WAComponentClient::registerResource(LoadableResource *res) {
  170. ASSERT(!resources.haveItem(res));
  171. resources.addItem(res);
  172. if (postregisterservices) res->onRegisterServices();
  173. }
  174. void WAComponentClient::internal_onDestroy() {
  175. onDestroy();
  176. foreach(resources)
  177. if (resources.getfor()->deleteOnShutdown()) delete resources.getfor();
  178. endfor
  179. resources.removeAll();
  180. }
  181. int WAComponentClient::internal_onNotify(int cmd, int param1, int param2, int param3, int param4) {
  182. switch (cmd) {
  183. case WAC_NOTIFY_BEFORELOADINGSKINELEMENTS:
  184. foreach(resources)
  185. resources.getfor()->beforeLoadingSkinElements();
  186. endfor
  187. break;
  188. case WAC_NOTIFY_SKINLOADED:
  189. onSkinLoaded();
  190. break;
  191. }
  192. return onNotify(cmd, param1, param2, param3, param4);
  193. }
  194. #ifndef STATIC_COMPONENT
  195. extern "C" {
  196. __declspec(dllexport) UINT WAC_getVersion(void) {
  197. return WA_COMPONENT_VERSION;
  198. }
  199. __declspec(dllexport) int WAC_init(OSMODULEHANDLE moduleHandle) {
  200. the->setOSModuleHandle(moduleHandle);
  201. if (g_modulehandle == NULL) g_modulehandle = moduleHandle;
  202. return TRUE;
  203. }
  204. __declspec(dllexport) WaComponent *WAC_enumComponent(int n) {
  205. return the;
  206. }
  207. } // end extern "C"
  208. #endif
  209. #define CBCLASS WaComponentI
  210. START_DISPATCH;
  211. CB(GETNAME, getName);
  212. CB(GETGUID, getGUID);
  213. #ifndef WASABINOMAINAPI
  214. VCB(REGISTERSERVICES, registerServices);
  215. #else
  216. VCB(REGISTERSERVICES2, registerServices);
  217. #endif
  218. CB(15, RegisterServicesSafeModeOk)
  219. VCB(DEREGISTERSERVICES, deregisterServices);
  220. VCB(ONCREATE, onCreate);
  221. VCB(ONDESTROY, internal_onDestroy);
  222. CB(CREATEWINDOW, createWindow);
  223. CB(ONNOTIFY, internal_onNotify);
  224. CB(ONCOMMAND, onCommand);
  225. CB(GETCFGINTERFACE, getCfgInterface);
  226. VCB(SETOSMODULEHANDLE, setOSModuleHandle);
  227. CB(GETOSMODULEHANDLE, getOSModuleHandle);
  228. VCB(SETPATH, setComponentPath);
  229. CB(GETPATH, getComponentPath);
  230. END_DISPATCH;
  231. #undef CBCLASS
  232. #ifdef WASABINOMAINAPI
  233. api_application *applicationApi = NULL;
  234. api_service *serviceApi = NULL;
  235. api_syscb *sysCallbackApi = NULL;
  236. api_memmgr *memmgrApi = NULL;
  237. #ifdef WASABI_COMPILE_CONFIG
  238. api_config *configApi = NULL;
  239. #endif
  240. #ifdef WASABI_COMPILE_IMGLDR
  241. imgldr_api *imgLoaderApi = NULL;
  242. #endif
  243. #ifdef WASABI_COMPILE_FONTS
  244. api_font *fontApi = NULL;
  245. #endif
  246. #ifdef WASABI_COMPILE_FILEREADER
  247. api_fileReader *fileApi = NULL;
  248. #endif
  249. #ifdef WASABI_COMPILE_LOCALES
  250. api_locales *localesApi = NULL;
  251. #endif
  252. #ifdef WASABI_COMPILE_TIMERS
  253. timer_api *timerApi = NULL;
  254. #endif
  255. #ifdef WASABI_COMPILE_WND
  256. wnd_api *wndApi = NULL;
  257. #endif
  258. #ifdef WASABI_COMPILE_WNDMGR
  259. wndmgr_api *wndManagerApi = NULL;
  260. #endif
  261. #ifdef WASABI_COMPILE_COMPONENTS
  262. #endif
  263. #ifdef WASABI_COMPILE_METADB
  264. #endif
  265. #ifdef WASABI_COMPILE_SCRIPT
  266. api_maki *makiApi = NULL;
  267. #endif
  268. #ifdef WASABI_COMPILE_SKIN
  269. api_skin *skinApi;
  270. #endif
  271. #ifdef WASABI_COMPILE_MAKIDEBUG
  272. api_makiDebugger *debugApi = NULL;
  273. #endif
  274. #ifdef WASABI_COMPILE_MEDIACORE
  275. api_core *coreApi = NULL;
  276. #endif
  277. #endif // ifdef WASABINOMAINAPI