BsUnixPlatformUtility.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Utility/BsPlatformUtility.h"
  4. #include <stdlib.h>
  5. #include <uuid/uuid.h>
  6. #include <sys/utsname.h>
  7. namespace bs
  8. {
  9. GPUInfo PlatformUtility::sGPUInfo;
  10. void PlatformUtility::terminate(bool force)
  11. {
  12. // TODOPORT - Support clean exit by sending the main window a quit message
  13. exit(0);
  14. }
  15. SystemInfo PlatformUtility::getSystemInfo()
  16. {
  17. SystemInfo output;
  18. // Get CPU vendor, model and number of cores
  19. {
  20. std::ifstream file("/proc/cpuinfo");
  21. std::string line;
  22. while(std::getline(file, line))
  23. {
  24. std::stringstream lineStream(line);
  25. std::string token;
  26. lineStream >> token;
  27. if(token == "vendor_id")
  28. {
  29. if(lineStream >> token && token == ":")
  30. {
  31. std::string vendorId;
  32. if(lineStream >> vendorId)
  33. output.cpuManufacturer = vendorId.c_str();
  34. }
  35. }
  36. else if(token == "model")
  37. {
  38. if(lineStream >> token && token == "name")
  39. {
  40. if (lineStream >> token && token == ":")
  41. {
  42. std::stringstream modelName;
  43. if (lineStream >> token)
  44. {
  45. modelName << token;
  46. while (lineStream >> token)
  47. modelName << " " << token;
  48. }
  49. output.cpuModel = modelName.str().c_str();
  50. }
  51. }
  52. }
  53. else if(token == "cpu")
  54. {
  55. if(lineStream >> token)
  56. {
  57. if (token == "cores")
  58. {
  59. if (lineStream >> token && token == ":")
  60. {
  61. UINT32 numCores;
  62. if (lineStream >> numCores)
  63. output.cpuNumCores = numCores;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. // Get CPU frequency
  71. {
  72. std::ifstream file("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
  73. UINT32 frequency;
  74. if(file >> frequency)
  75. output.cpuClockSpeedMhz = frequency / 1000;
  76. }
  77. // Get amount of system memory
  78. {
  79. std::ifstream file("/proc/meminfo");
  80. std::string token;
  81. while(file >> token)
  82. {
  83. if(token == "MemTotal:")
  84. {
  85. UINT32 memTotal;
  86. if(file >> memTotal)
  87. output.memoryAmountMb = memTotal / 1024;
  88. else
  89. output.memoryAmountMb = 0;
  90. break;
  91. }
  92. // Ignore the rest of the line
  93. file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  94. }
  95. }
  96. // Get OS version
  97. utsname osInfo;
  98. uname(&osInfo);
  99. // Note: This won't report the exact distro
  100. output.osName = String(osInfo.sysname) + String(osInfo.version);
  101. if (BS_ARCH_TYPE == BS_ARCHITECTURE_x86_64)
  102. output.osIs64Bit = true;
  103. else
  104. output.osIs64Bit = strstr(osInfo.machine, "64") != nullptr;
  105. // Get GPU info
  106. output.gpuInfo = sGPUInfo;
  107. return output;
  108. }
  109. String PlatformUtility::generateUUID()
  110. {
  111. uuid_t nativeUUID;
  112. uuid_generate(nativeUUID);
  113. char uuidChars[37];
  114. uuid_unparse(nativeUUID, uuidChars);
  115. return String(uuidChars);
  116. }
  117. }