COSOperator.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "COSOperator.h"
  5. #ifdef _IRR_WINDOWS_API_
  6. #ifndef _IRR_XBOX_PLATFORM_
  7. #include <windows.h>
  8. #endif
  9. #else
  10. #include <string.h>
  11. #include <unistd.h>
  12. #if !defined(_IRR_SOLARIS_PLATFORM_) && !defined(__CYGWIN__)
  13. #include <sys/param.h>
  14. #include <sys/types.h>
  15. #ifdef ANDROID
  16. #include <linux/sysctl.h>
  17. #else
  18. #include <sys/sysctl.h>
  19. #endif
  20. #endif
  21. #endif
  22. #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
  23. #include "CIrrDeviceLinux.h"
  24. #endif
  25. #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
  26. #include "MacOSX/OSXClipboard.h"
  27. #endif
  28. namespace irr
  29. {
  30. #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
  31. // constructor linux
  32. COSOperator::COSOperator(const core::stringc& osVersion, CIrrDeviceLinux* device)
  33. : OperatingSystem(osVersion), IrrDeviceLinux(device)
  34. {
  35. }
  36. #endif
  37. // constructor
  38. COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVersion)
  39. {
  40. #ifdef _DEBUG
  41. setDebugName("COSOperator");
  42. #endif
  43. }
  44. //! returns the current operating system version as string.
  45. const core::stringc& COSOperator::getOperatingSystemVersion() const
  46. {
  47. return OperatingSystem;
  48. }
  49. //! copies text to the clipboard
  50. #if defined(_IRR_COMPILE_WITH_WINDOWS_DEVICE_)
  51. void COSOperator::copyToClipboard(const wchar_t* text) const
  52. {
  53. if (wcslen(text)==0)
  54. return;
  55. // Windows version
  56. #if defined(_IRR_XBOX_PLATFORM_)
  57. #elif defined(_IRR_WINDOWS_API_)
  58. if (!OpenClipboard(NULL) || text == 0)
  59. return;
  60. EmptyClipboard();
  61. HGLOBAL clipbuffer;
  62. wchar_t * buffer;
  63. clipbuffer = GlobalAlloc(GMEM_DDESHARE, wcslen(text)*sizeof(wchar_t) + sizeof(wchar_t));
  64. buffer = (wchar_t*)GlobalLock(clipbuffer);
  65. wcscpy(buffer, text);
  66. GlobalUnlock(clipbuffer);
  67. SetClipboardData(CF_UNICODETEXT, clipbuffer); //Windwos converts between CF_UNICODETEXT and CF_TEXT automatically.
  68. CloseClipboard();
  69. #else
  70. #endif
  71. }
  72. #else
  73. void COSOperator::copyToClipboard(const c8* text) const
  74. {
  75. if (strlen(text)==0)
  76. return;
  77. // Windows version
  78. #if defined(_IRR_XBOX_PLATFORM_)
  79. #elif defined(_IRR_WINDOWS_API_)
  80. if (!OpenClipboard(NULL) || text == 0)
  81. return;
  82. EmptyClipboard();
  83. HGLOBAL clipbuffer;
  84. char * buffer;
  85. clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1);
  86. buffer = (char*)GlobalLock(clipbuffer);
  87. strcpy(buffer, text);
  88. GlobalUnlock(clipbuffer);
  89. SetClipboardData(CF_TEXT, clipbuffer);
  90. CloseClipboard();
  91. // MacOSX version
  92. #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
  93. OSXCopyToClipboard(text);
  94. #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
  95. if ( IrrDeviceLinux )
  96. IrrDeviceLinux->copyToClipboard(text);
  97. #else
  98. #endif
  99. }
  100. #endif
  101. //! gets text from the clipboard
  102. //! \return Returns 0 if no string is in there.
  103. #if defined(_IRR_COMPILE_WITH_WINDOWS_DEVICE_)
  104. const wchar_t* COSOperator::getTextFromClipboard() const
  105. {
  106. #if defined(_IRR_XBOX_PLATFORM_)
  107. return 0;
  108. #elif defined(_IRR_WINDOWS_API_)
  109. if (!OpenClipboard(NULL))
  110. return 0;
  111. wchar_t * buffer = 0;
  112. HANDLE hData = GetClipboardData( CF_UNICODETEXT ); //Windwos converts between CF_UNICODETEXT and CF_TEXT automatically.
  113. buffer = (wchar_t*)GlobalLock( hData );
  114. GlobalUnlock( hData );
  115. CloseClipboard();
  116. return buffer;
  117. #else
  118. return 0;
  119. #endif
  120. }
  121. #else
  122. const c8* COSOperator::getTextFromClipboard() const
  123. {
  124. #if defined(_IRR_XBOX_PLATFORM_)
  125. return 0;
  126. #elif defined(_IRR_WINDOWS_API_)
  127. if (!OpenClipboard(NULL))
  128. return 0;
  129. char * buffer = 0;
  130. HANDLE hData = GetClipboardData( CF_TEXT );
  131. buffer = (char*)GlobalLock( hData );
  132. GlobalUnlock( hData );
  133. CloseClipboard();
  134. return buffer;
  135. #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
  136. return (OSXCopyFromClipboard());
  137. #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
  138. if ( IrrDeviceLinux )
  139. return IrrDeviceLinux->getTextFromClipboard();
  140. return 0;
  141. #else
  142. return 0;
  143. #endif
  144. }
  145. #endif
  146. bool COSOperator::getProcessorSpeedMHz(u32* MHz) const
  147. {
  148. #if defined(_IRR_WINDOWS_API_) && !defined(_WIN32_WCE ) && !defined (_IRR_XBOX_PLATFORM_)
  149. LONG Error;
  150. HKEY Key;
  151. Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  152. __TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
  153. 0, KEY_READ, &Key);
  154. if(Error != ERROR_SUCCESS)
  155. return false;
  156. DWORD Speed = 0;
  157. DWORD Size = sizeof(Speed);
  158. Error = RegQueryValueEx(Key, __TEXT("~MHz"), NULL, NULL, (LPBYTE)&Speed, &Size);
  159. RegCloseKey(Key);
  160. if (Error != ERROR_SUCCESS)
  161. return false;
  162. else if (MHz)
  163. *MHz = Speed;
  164. _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
  165. return true;
  166. #elif defined(_IRR_OSX_PLATFORM_)
  167. struct clockinfo CpuClock;
  168. size_t Size = sizeof(clockinfo);
  169. if (!sysctlbyname("kern.clockrate", &CpuClock, &Size, NULL, 0))
  170. return false;
  171. else if (MHz)
  172. *MHz = CpuClock.hz;
  173. return true;
  174. #else
  175. // could probably be read from "/proc/cpuinfo" or "/proc/cpufreq"
  176. return false;
  177. #endif
  178. }
  179. bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
  180. {
  181. #if defined(_IRR_WINDOWS_API_) && !defined (_IRR_XBOX_PLATFORM_)
  182. MEMORYSTATUS MemoryStatus;
  183. MemoryStatus.dwLength = sizeof(MEMORYSTATUS);
  184. // cannot fail
  185. GlobalMemoryStatus(&MemoryStatus);
  186. if (Total)
  187. *Total = (u32)(MemoryStatus.dwTotalPhys>>10);
  188. if (Avail)
  189. *Avail = (u32)(MemoryStatus.dwAvailPhys>>10);
  190. _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
  191. return true;
  192. #elif defined(_IRR_POSIX_API_) && !defined(__FreeBSD__)
  193. #if defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
  194. long ps = sysconf(_SC_PAGESIZE);
  195. long pp = sysconf(_SC_PHYS_PAGES);
  196. long ap = sysconf(_SC_AVPHYS_PAGES);
  197. if ((ps==-1)||(pp==-1)||(ap==-1))
  198. return false;
  199. if (Total)
  200. *Total = (u32)((ps*(long long)pp)>>10);
  201. if (Avail)
  202. *Avail = (u32)((ps*(long long)ap)>>10);
  203. return true;
  204. #else
  205. // TODO: implement for non-availablity of symbols/features
  206. return false;
  207. #endif
  208. #else
  209. // TODO: implement for OSX
  210. return false;
  211. #endif
  212. }
  213. } // end namespace