IFeel.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "stdh.h"
  13. #include <Engine/Base/IFeel.h>
  14. #include <Engine/Base/FileName.h>
  15. #include <Engine/Base/Stream.h>
  16. #include <Engine/Base/Console.h>
  17. #include <Engine/Base/Shell.h>
  18. //Imm_GetProductName
  19. HINSTANCE _hLib = NULL;
  20. BOOL (*immCreateDevice)(HINSTANCE &hInstance, HWND &hWnd) = NULL;
  21. void (*immDeleteDevice)(void) = NULL;
  22. BOOL (*immProductName)(char *strProduct,int iMaxCount) = NULL;
  23. BOOL (*immLoadFile)(const char *fnFile) = NULL;
  24. void (*immUnloadFile)(void) = NULL;
  25. void (*immPlayEffect)(const char *pstrEffectName) = NULL;
  26. void (*immStopEffect)(const char *pstrEffectName) = NULL;
  27. void (*immChangeGain)(const float fGain) = NULL;
  28. FLOAT ifeel_fGain = 1.0f;
  29. INDEX ifeel_bEnabled = FALSE;
  30. #ifndef NDEBUG
  31. #define IFEEL_DLL_NAME "Bin/Debug/ImmWrapper.dll"
  32. #else
  33. #define IFEEL_DLL_NAME "Bin/ImmWrapper.dll"
  34. #endif
  35. void ifeel_GainChange(void *ptr)
  36. {
  37. IFeel_ChangeGain(ifeel_fGain);
  38. }
  39. CTString IFeel_GetProductName()
  40. {
  41. char strProduct[MAX_PATH];
  42. if(immProductName != NULL)
  43. {
  44. if(immProductName(strProduct,MAX_PATH))
  45. {
  46. return strProduct;
  47. }
  48. }
  49. return "";
  50. }
  51. CTString IFeel_GetProjectFileName()
  52. {
  53. CTString strIFeelTable;
  54. CTFileName fnIFeelTable = (CTString)"Data\\IFeel.txt";
  55. CTString strDefaultProjectFile = "Data\\Default.ifr";
  56. // get product name
  57. CTString strProduct = IFeel_GetProductName();
  58. try
  59. {
  60. strIFeelTable.Load_t(fnIFeelTable);
  61. }
  62. catch(char *strErr)
  63. {
  64. CPrintF("%s\n",strErr);
  65. return "";
  66. }
  67. CTString strLine;
  68. // read up to 1000 devices
  69. for(INDEX idev=0;idev<1000;idev++)
  70. {
  71. char strDeviceName[256];
  72. char strProjectFile[256];
  73. strLine = strIFeelTable;
  74. // read first line
  75. strLine.OnlyFirstLine();
  76. if(strLine==strIFeelTable)
  77. {
  78. break;
  79. }
  80. // remove that line
  81. strIFeelTable.RemovePrefix(strLine);
  82. strIFeelTable.DeleteChar(0);
  83. // read device name and project file name
  84. strIFeelTable.ScanF("\"%256[^\"]\" \"%256[^\"]\"",&strDeviceName,&strProjectFile);
  85. // check if this is default
  86. if(strcmp(strDeviceName,"Default")==0) strDefaultProjectFile = strProjectFile;
  87. // check if this is current device
  88. if(strProduct == strDeviceName) return strProjectFile;
  89. }
  90. // device was not found, return default project file
  91. CPrintF("No project file specified for device '%s'.\nUsing default project file\n",strProduct);
  92. return strDefaultProjectFile;
  93. }
  94. // inits imm ifeel device
  95. BOOL IFeel_InitDevice(HINSTANCE &hInstance, HWND &hWnd)
  96. {
  97. _pShell->DeclareSymbol("void inp_IFeelGainChange(INDEX);", &ifeel_GainChange);
  98. _pShell->DeclareSymbol("persistent user FLOAT inp_fIFeelGain post:inp_IFeelGainChange;", &ifeel_fGain);
  99. _pShell->DeclareSymbol("const user INDEX sys_bIFeelEnabled;", &ifeel_bEnabled);
  100. IFeel_ChangeGain(ifeel_fGain);
  101. // load iFeel lib
  102. CTFileName fnmExpanded;
  103. ExpandFilePath(EFP_READ | EFP_NOZIPS,(CTString)IFEEL_DLL_NAME,fnmExpanded);
  104. if(_hLib!=NULL) return FALSE;
  105. UINT iOldErrorMode = SetErrorMode( SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
  106. _hLib = LoadLibraryA(fnmExpanded);
  107. SetErrorMode(iOldErrorMode);
  108. if(_hLib==NULL)
  109. {
  110. CPrintF("Error loading ImmWraper.dll.\n\tIFeel disabled\n");
  111. return FALSE;
  112. }
  113. // take func pointers
  114. immCreateDevice = (BOOL(*)(HINSTANCE &hInstance, HWND &hWnd)) GetProcAddress(_hLib,"Imm_CreateDevice");
  115. immDeleteDevice = (void(*)(void)) GetProcAddress(_hLib,"Imm_DeleteDevice");
  116. immProductName = (BOOL(*)(char *strProduct,int iMaxCount)) GetProcAddress(_hLib,"Imm_GetProductName");
  117. immLoadFile = (BOOL(*)(const char *fnFile))GetProcAddress(_hLib,"Imm_LoadFile");
  118. immUnloadFile = (void(*)(void))GetProcAddress(_hLib,"immUnloadFile");
  119. immPlayEffect = (void(*)(const char *pstrEffectName))GetProcAddress(_hLib,"Imm_PlayEffect");
  120. immStopEffect = (void(*)(const char *pstrEffectName))GetProcAddress(_hLib,"Imm_StopEffect");
  121. immChangeGain = (void(*)(const float fGain))GetProcAddress(_hLib,"Imm_ChangeGain");
  122. // create device
  123. if(immCreateDevice == NULL)
  124. {
  125. return FALSE;
  126. }
  127. BOOL hr = immCreateDevice(hInstance,hWnd);
  128. if(!hr)
  129. {
  130. CPrintF("IFeel mouse not found.\n");
  131. IFeel_DeleteDevice();
  132. return FALSE;
  133. }
  134. CPrintF("IFeel mouse '%s' initialized\n",(const char*)IFeel_GetProductName());
  135. ifeel_bEnabled = TRUE;
  136. return TRUE;
  137. }
  138. // delete imm ifeel device
  139. void IFeel_DeleteDevice()
  140. {
  141. if(immDeleteDevice != NULL) immDeleteDevice();
  142. immCreateDevice = NULL;
  143. immDeleteDevice = NULL;
  144. immProductName = NULL;
  145. immLoadFile = NULL;
  146. immUnloadFile = NULL;
  147. immPlayEffect = NULL;
  148. immStopEffect = NULL;
  149. immChangeGain = NULL;
  150. if(_hLib != NULL) FreeLibrary(_hLib);
  151. _hLib = NULL;
  152. }
  153. // loads project file
  154. BOOL IFeel_LoadFile(CTFileName fnFile)
  155. {
  156. CTFileName fnmExpanded;
  157. ExpandFilePath(EFP_READ | EFP_NOZIPS,fnFile,fnmExpanded);
  158. if(immLoadFile!=NULL)
  159. {
  160. BOOL hr = immLoadFile((const char*)fnmExpanded);
  161. if(hr)
  162. {
  163. CPrintF("IFeel project file '%s' loaded\n", fnFile);
  164. return TRUE;
  165. }
  166. else
  167. {
  168. CPrintF("Error loading IFeel project file '%s'\n", fnFile);
  169. return FALSE;
  170. }
  171. }
  172. return FALSE;
  173. }
  174. // unloads project file
  175. void IFeel_UnloadFile()
  176. {
  177. if(immUnloadFile!=NULL) immUnloadFile();
  178. }
  179. // plays effect from ifr file
  180. void IFeel_PlayEffect(char *pstrEffectName)
  181. {
  182. IFeel_ChangeGain(ifeel_fGain);
  183. if(immPlayEffect!=NULL) immPlayEffect(pstrEffectName);
  184. }
  185. // stops effect from ifr file
  186. void IFeel_StopEffect(char *pstrEffectName)
  187. {
  188. if(immStopEffect!=NULL) immStopEffect(pstrEffectName);
  189. }
  190. // change gain
  191. void IFeel_ChangeGain(FLOAT fGain)
  192. {
  193. if(immChangeGain!=NULL)
  194. {
  195. immChangeGain(fGain);
  196. //CPrintF("Changing IFeel gain to '%g'\n",fGain);
  197. }
  198. }