platform.bmx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ' Copyright (c) 2019-2023 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: Platform utils
  25. End Rem
  26. Module BRL.Platform
  27. ModuleInfo "Version: 1.04"
  28. ModuleInfo "Author: Bruce A Henderson"
  29. ModuleInfo "License: zlib/libpng"
  30. ModuleInfo "History: 1.04"
  31. ModuleInfo "History: Improved calculation for Win32."
  32. ModuleInfo "History: 1.03"
  33. ModuleInfo "History: Added PhysicalProcessorCount()."
  34. ModuleInfo "History: Added build number for Win32 OS version."
  35. ModuleInfo "History: 1.02"
  36. ModuleInfo "History: Fixed for Android."
  37. ModuleInfo "History: 1.01"
  38. ModuleInfo "History: Added LogicalProcessorCount()."
  39. ModuleInfo "History: 1.00"
  40. ModuleInfo "History: Initial Release"
  41. Import BRL.TextStream
  42. Import Pub.macos
  43. ?android
  44. Import SDL.SDL
  45. ?
  46. ?win32
  47. Import "win32_glue.c"
  48. ?Not win32 And Not android And Not haiku and not macos
  49. Import "glue.c"
  50. ?haiku
  51. Import "haiku_glue.c"
  52. ?macos
  53. Import "macos_glue.c"
  54. ?linux
  55. Import "linux.bmx"
  56. ?
  57. Private
  58. Global _version:String
  59. Public
  60. Rem
  61. bbdoc: Returns a #String representation of the OS version.
  62. about: On Linux, this will return something like "Ubuntu" or "Fedora".
  63. On Windows and macOS, this will return the version number, such as "10.11"
  64. End Rem
  65. Function OSVersion:String()
  66. If _version Then
  67. Return _version
  68. End If
  69. ?linux
  70. Return LinuxVersion()
  71. ?macos
  72. Return MacOSVersion()
  73. ?win32
  74. Return WindowsVersion()
  75. ?haiku
  76. Return HaikuVersion()
  77. ?
  78. End Function
  79. Function LinuxVersion:String()
  80. ?Not linux
  81. Return ""
  82. ?linux And Not android
  83. If _version Then
  84. Return _version
  85. End If
  86. Local rel:String[] = LoadText("/etc/os-release").Split("~n")
  87. For Local line:String = EachIn rel
  88. If line.StartsWith("NAME") Then
  89. Local parts:String[] = line.Split("=")
  90. If parts.length > 1 Then
  91. _version = parts[1].Replace("~q", "").Trim()
  92. Return _version
  93. End If
  94. End If
  95. Next
  96. ?android
  97. Return "Android"
  98. ?
  99. End Function
  100. Function MacOSVersion:String()
  101. ?Not macos
  102. Return ""
  103. ?macos
  104. If _version Then
  105. Return _version
  106. End If
  107. Local major:Int, minor:Int, patch:Int
  108. NSOSVersion(major, minor, patch)
  109. _version = major + "." + minor + "." + patch
  110. Return _version
  111. ?
  112. End Function
  113. Private
  114. Extern
  115. ?win32
  116. Function bmx_os_getwindowsversion(major:Int Var, minor:Int Var, build:Int Var)
  117. ?haiku
  118. Function bmx_os_gethaikuversion:String()
  119. ?
  120. End Extern
  121. Public
  122. Function WindowsVersion:String()
  123. ?Not win32
  124. Return ""
  125. ?win32
  126. If _version Then
  127. Return _version
  128. End If
  129. Local major:Int
  130. Local minor:Int
  131. Local build:Int
  132. bmx_os_getwindowsversion(major, minor, build)
  133. _version = major + "." + minor + "." + build
  134. Return _version
  135. ?
  136. End Function
  137. Function HaikuVersion:String()
  138. ?Not haiku
  139. Return ""
  140. ?haiku
  141. If _version Then
  142. Return _version
  143. End If
  144. _version = bmx_os_gethaikuversion()
  145. Return _version
  146. ?
  147. End Function
  148. Rem
  149. bbdoc: Returns the number of logical processors available.
  150. about: Logical processors are the number of physical processors times the number of threads that can run on each.
  151. End Rem
  152. Function LogicalProcessorCount:Int()
  153. ?Not android
  154. Return bmx_os_getproccount()
  155. ?android
  156. Return SDLGetCPUCount()
  157. ?
  158. End Function
  159. Rem
  160. bbdoc: Returns the number of physical processors available.
  161. End Rem
  162. Function PhysicalProcessorCount:Int()
  163. ?Not android And Not linux
  164. Return bmx_os_getphysproccount()
  165. ?linux And Not android
  166. Local count:Int = _linux_physical_processor_count()
  167. If Not count Then
  168. Return LogicalProcessorCount()
  169. End If
  170. Return count
  171. ?android
  172. Return SDLGetCPUCount()
  173. ?
  174. End Function
  175. Extern
  176. Function bmx_os_getproccount:Int()
  177. Function bmx_os_getphysproccount:Int()
  178. End Extern