David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // A broad set of APIs that allow the client application to query build and |
| 16 | // runtime properties of the enclosing system. |
| 17 | |
| 18 | #ifndef STARBOARD_SYSTEM_H_ |
| 19 | #define STARBOARD_SYSTEM_H_ |
| 20 | |
| 21 | #include "starboard/configuration.h" |
| 22 | #include "starboard/export.h" |
| 23 | #include "starboard/types.h" |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | // A type that can represent a system error code across all Starboard platforms. |
| 30 | typedef int SbSystemError; |
| 31 | |
| 32 | // Enumeration of special paths that the platform can define. |
| 33 | typedef enum SbSystemPathId { |
| 34 | // Path to where the local content files that ship with the binary are |
| 35 | // available. |
| 36 | kSbSystemPathContentDirectory, |
| 37 | |
| 38 | // Path to the directory that can be used as a local file cache, if |
| 39 | // available. |
| 40 | kSbSystemPathCacheDirectory, |
| 41 | |
| 42 | // Path to the directory where debug output (e.g. logs, trace output, |
| 43 | // screenshots) can be written into. |
| 44 | kSbSystemPathDebugOutputDirectory, |
| 45 | |
| 46 | // Path to the directory containing the root of the source tree. |
| 47 | kSbSystemPathSourceDirectory, |
| 48 | |
| 49 | // Path to a directory where temporary files can be written. |
| 50 | kSbSystemPathTempDirectory, |
| 51 | |
| 52 | // Path to a directory where test results can be written. |
| 53 | kSbSystemPathTestOutputDirectory, |
| 54 | |
| 55 | // Full path to the executable file. |
| 56 | kSbSystemPathExecutableFile, |
| 57 | } SbSystemPathId; |
| 58 | |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 59 | // System properties that can be queried for. Many of these are used in |
| 60 | // User-Agent string generation. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 61 | typedef enum SbSystemPropertyId { |
| 62 | // The full model number of the main platform chipset, including any |
| 63 | // vendor-specific prefixes. |
| 64 | kSbSystemPropertyChipsetModelNumber, |
| 65 | |
| 66 | // The production firmware version number which the device is currently |
| 67 | // running. |
| 68 | kSbSystemPropertyFirmwareVersion, |
| 69 | |
| 70 | // A friendly name for this actual device. It may include user-personalization |
| 71 | // like "Upstairs Bedroom." It may be displayed to users as part of some kind |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 72 | // of device selection (e.g. in-app DIAL). |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 73 | kSbSystemPropertyFriendlyName, |
| 74 | |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 75 | // A deprecated alias for |kSbSystemPropertyBrandName|. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 76 | kSbSystemPropertyManufacturerName, |
| 77 | |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 78 | // The name of the brand under which the device is being sold. |
| 79 | kSbSystemPropertyBrandName = kSbSystemPropertyManufacturerName, |
| 80 | |
| 81 | // The final production model number of the device. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 82 | kSbSystemPropertyModelName, |
| 83 | |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 84 | // The year the device was launched, e.g. "2016". |
| 85 | kSbSystemPropertyModelYear, |
| 86 | |
| 87 | // The name of the network operator that owns the target device, if |
| 88 | // applicable. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 89 | kSbSystemPropertyNetworkOperatorName, |
| 90 | |
David Ghandehari | 8d74a97 | 2016-09-05 02:00:53 -0700 | [diff] [blame] | 91 | // The name of the operating system and platform, suitable for inclusion in a |
| 92 | // User-Agent, say. |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 93 | kSbSystemPropertyPlatformName, |
| 94 | |
| 95 | // A universally-unique ID for the current user. |
| 96 | kSbSystemPropertyPlatformUuid, |
| 97 | } SbSystemPropertyId; |
| 98 | |
| 99 | // Enumeration of device types. |
| 100 | typedef enum SbSystemDeviceType { |
| 101 | // Blue-ray Disc Player (BDP). |
| 102 | kSbSystemDeviceTypeBlueRayDiskPlayer, |
| 103 | |
| 104 | // A relatively high-powered TV device used primarily for playing games. |
| 105 | kSbSystemDeviceTypeGameConsole, |
| 106 | |
| 107 | // Over the top (OTT) devices stream content via the Internet over another |
| 108 | // type of network, e.g. cable or satellite. |
| 109 | kSbSystemDeviceTypeOverTheTopBox, |
| 110 | |
| 111 | // Set top boxes (STBs) stream content primarily over cable or satellite. |
| 112 | // Some STBs can also stream OTT content via the Internet. |
| 113 | kSbSystemDeviceTypeSetTopBox, |
| 114 | |
| 115 | // A Smart TV is a TV that can directly run applications that stream OTT |
| 116 | // content via the Internet. |
| 117 | kSbSystemDeviceTypeTV, |
| 118 | |
| 119 | // Desktop PC. |
| 120 | kSbSystemDeviceTypeDesktopPC, |
| 121 | |
| 122 | // Unknown device. |
| 123 | kSbSystemDeviceTypeUnknown, |
| 124 | } SbSystemDeviceType; |
| 125 | |
| 126 | // Enumeration of network connection types. |
| 127 | typedef enum SbSystemConnectionType { |
| 128 | // The system is on a wired connection. |
| 129 | kSbSystemConnectionTypeWired, |
| 130 | |
| 131 | // The system is on a wireless connection. |
| 132 | kSbSystemConnectionTypeWireless, |
| 133 | |
| 134 | // The system connection type is unknown. |
| 135 | kSbSystemConnectionTypeUnknown, |
| 136 | } SbSystemConnectionType; |
| 137 | |
| 138 | // Runtime capabilities are boolean properties of a platform that can't be |
| 139 | // determined at compile-time, and may vary from device to device, but will not |
| 140 | // change over the course of a single execution. They often specify particular |
| 141 | // behavior of other APIs within the bounds of their operating range. |
| 142 | typedef enum SbSystemCapabilityId { |
| 143 | // Whether this system has reversed Enter and Back keys. |
| 144 | kSbSystemCapabilityReversedEnterAndBack, |
David Ghandehari | 4653224 | 2016-09-08 14:45:18 -0700 | [diff] [blame] | 145 | |
| 146 | // Whether this system has the ability to report on GPU memory usage. |
| 147 | // If (and only if) a system has this capcability will |
| 148 | // SbSystemGetTotalGPUMemory() and SbSystemGetUsedGPUMemory() be valid to |
| 149 | // call. |
| 150 | kSbSystemCapabilityCanQueryGPUMemoryStats, |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 151 | } SbSystemCapabilityId; |
| 152 | |
David Ghandehari | 8c5039b | 2016-08-17 19:39:30 -0700 | [diff] [blame] | 153 | // Enumeration of possible values for the |type| parameter passed to the |
| 154 | // |SbSystemRaisePlatformError| function. |
| 155 | typedef enum SbSystemPlatformErrorType { |
| 156 | // Cobalt received a network connection error, or a network disconnection |
| 157 | // event. |
| 158 | kSbSystemPlatformErrorTypeConnectionError, |
| 159 | |
| 160 | // The current user is not signed in (e.g. to PSN network). |
| 161 | kSbSystemPlatformErrorTypeUserSignedOut, |
| 162 | |
| 163 | // The current user does not meet the age requirements to use the app. |
| 164 | kSbSystemPlatformErrorTypeUserAgeRestricted |
| 165 | } SbSystemPlatformErrorType; |
| 166 | |
| 167 | // Possible responses for |SbSystemPlatformErrorCallback|. |
| 168 | typedef enum SbSystemPlatformErrorResponse { |
| 169 | kSbSystemPlatformErrorResponsePositive, |
| 170 | kSbSystemPlatformErrorResponseNegative, |
| 171 | kSbSystemPlatformErrorResponseCancel |
| 172 | } SbSystemPlatformErrorResponse; |
| 173 | |
| 174 | // Type of callback function that may be called in response to an error |
| 175 | // notification from |SbSystemRaisePlatformError|. |response| is a code to |
| 176 | // indicate the user's response, e.g. if the platform raised a dialog to notify |
| 177 | // the user of the error. |user_data| is the opaque pointer that was passed to |
| 178 | // the call to |SbSystemRaisePlatformError|. |
| 179 | typedef void (*SbSystemPlatformErrorCallback)( |
| 180 | SbSystemPlatformErrorResponse response, |
| 181 | void* user_data); |
| 182 | |
| 183 | // Private structure used to represent a raised platform error. |
| 184 | typedef struct SbSystemPlatformErrorPrivate SbSystemPlatformErrorPrivate; |
| 185 | |
| 186 | // Opaque handle returned by |SbSystemRaisePlatformError| that can be passed |
| 187 | // to |SbSystemClearPlatformError|. |
| 188 | typedef SbSystemPlatformErrorPrivate* SbSystemPlatformError; |
| 189 | |
| 190 | // Well-defined value for an invalid |SbSystemPlatformError|. |
| 191 | #define kSbSystemPlatformErrorInvalid (SbSystemPlatformError) NULL |
| 192 | |
| 193 | // Checks whether a |SbSystemPlatformError| is valid. |
| 194 | static SB_C_INLINE bool SbSystemPlatformErrorIsValid( |
| 195 | SbSystemPlatformError handle) { |
| 196 | return handle != kSbSystemPlatformErrorInvalid; |
| 197 | } |
| 198 | |
| 199 | // Called by Cobalt to notify the platform that an error has occurred in the |
| 200 | // application that may have to be handled by the platform. It is expected the |
| 201 | // platform will notify the user of the error, and provide interaction if |
| 202 | // required, for example by showing a dialog. |
| 203 | // |
| 204 | // |type| is one of the enumerated types above to define the error; |callback| |
| 205 | // is a function that may be called by the platform to let the caller know the |
| 206 | // user has reacted to the error; |user_data| is an opaque pointer that the |
| 207 | // platform should pass as an argument to the callback, if called. |
| 208 | // Returns a handle that may be used in a subsequent call to |
| 209 | // |SbClearPlatformError|, for example to programatically dismiss a dialog that |
| 210 | // may have been raised in response to the error. The lifetime of |
| 211 | // the object referenced by the handle is until the user reacts to the error |
| 212 | // or the error is dismissed by a call to |SbSystemClearPlatformError|, |
| 213 | // whichever happens first. If the platform cannot respond to the error, then |
| 214 | // this function should return |kSbSystemPlatformErrorInvalid|. |
| 215 | // |
| 216 | // This function may be called from any thread; it is the responsibility of the |
| 217 | // platform to decide how to handle an error received while a previous error is |
| 218 | // still pending - if only one error can be handled at a time, then the |
| 219 | // platform may queue the second error or ignore it by returning |
| 220 | // |kSbSystemPlatformErrorInvalid|. |
| 221 | SB_EXPORT SbSystemPlatformError |
| 222 | SbSystemRaisePlatformError(SbSystemPlatformErrorType type, |
| 223 | SbSystemPlatformErrorCallback callback, |
| 224 | void* user_data); |
| 225 | |
| 226 | // Clears a platform error that was previously raised by a call to |
| 227 | // |SbSystemRaisePlatformError|, specified by the handle that was returned by |
| 228 | // that function. The platform may use this, for example, to close a dialog |
| 229 | // that was opened in response to the error. |
| 230 | SB_EXPORT void SbSystemClearPlatformError(SbSystemPlatformError handle); |
| 231 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 232 | // Pointer to a function to compare two items, returning less than zero, zero, |
| 233 | // or greater than zero depending on whether |a| is less than |b|, equal to |b|, |
| 234 | // or greater than |b|, respectively (standard *cmp semantics). |
| 235 | typedef int (*SbSystemComparator)(const void* a, const void* b); |
| 236 | |
| 237 | // Breaks the current program into the debugger, if a debugger is |
| 238 | // attached. Aborts the program otherwise. |
| 239 | SB_EXPORT void SbSystemBreakIntoDebugger(); |
| 240 | |
| 241 | // Attempts to determine if the current program is running inside or attached to |
| 242 | // a debugger. |
| 243 | SB_EXPORT bool SbSystemIsDebuggerAttached(); |
| 244 | |
| 245 | // Returns the number of processor cores available to this application. If the |
| 246 | // process is sandboxed to a subset of the physical cores, it will return that |
| 247 | // sandboxed limit. |
| 248 | SB_EXPORT int SbSystemGetNumberOfProcessors(); |
| 249 | |
David Ghandehari | 4653224 | 2016-09-08 14:45:18 -0700 | [diff] [blame] | 250 | // Gets the total CPU memory (in bytes) potentially available to this |
| 251 | // application. If the process is sandboxed to a maximum allowable limit, it |
| 252 | // will return the lesser of the physical and sandbox limits. |
| 253 | SB_EXPORT int64_t SbSystemGetTotalCPUMemory(); |
| 254 | |
| 255 | // Returns the total physical CPU memory (in bytes) used by this application. |
| 256 | // This value should always be less than (or in particularly exciting |
| 257 | // situations, equal to) SbSystemGetTotalCPUMemory(). |
| 258 | SB_EXPORT int64_t SbSystemGetUsedCPUMemory(); |
| 259 | |
| 260 | // Returns the total GPU memory available (in bytes) for use by this |
| 261 | // application. |
| 262 | // This function may only be called if true is the return value for calls to |
| 263 | // SbSystemHasCapability(kSbSystemCapabilityCanQueryGPUMemoryStats). |
| 264 | SB_EXPORT int64_t SbSystemGetTotalGPUMemory(); |
| 265 | |
| 266 | // Returns the current amount of GPU memory (in bytes) that is currently being |
| 267 | // used by this application. |
| 268 | // This function may only be called if true is the return value for calls to |
| 269 | // SbSystemHasCapability(kSbSystemCapabilityCanQueryGPUMemoryStats). |
| 270 | SB_EXPORT int64_t SbSystemGetUsedGPUMemory(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 271 | |
| 272 | // Returns the type of the device. |
| 273 | SB_EXPORT SbSystemDeviceType SbSystemGetDeviceType(); |
| 274 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 275 | // Returns the device's current network connection type. |
| 276 | SB_EXPORT SbSystemConnectionType SbSystemGetConnectionType(); |
| 277 | |
| 278 | // Gets the platform-defined system path specified by |path_id|, placing it as a |
| 279 | // zero-terminated string into the user-allocated |out_path|, unless it is |
| 280 | // longer than |path_length| - 1. Returns false if |path_id| is invalid for this |
| 281 | // platform, or if |path_length| is too short for the given result, or if |
| 282 | // |out_path| is NULL. On failure, |out_path| will not be changed. |
| 283 | // Implementation must be thread-safe. |
| 284 | SB_EXPORT bool SbSystemGetPath(SbSystemPathId path_id, |
| 285 | char* out_path, |
| 286 | int path_length); |
| 287 | |
| 288 | // Gets the platform-defined system property specified by |property_id|, placing |
| 289 | // it as a zero-terminated string into the user-allocated |out_value|, unless it |
| 290 | // is longer than |value_length| - 1. Returns false if |property_id| is invalid |
| 291 | // for this platform, or if |value_length| is too short for the given result, or |
| 292 | // if |out_value| is NULL. On failure, |out_value| will not be changed. |
| 293 | // Implementation must be thread-safe. |
| 294 | SB_EXPORT bool SbSystemGetProperty(SbSystemPropertyId property_id, |
| 295 | char* out_value, |
| 296 | int value_length); |
| 297 | |
| 298 | // Returns whether the platform has the runtime capability specified by |
| 299 | // |capability_id|. Returns false for any unknown capabilities. Implementation |
| 300 | // must be thread-safe. |
| 301 | SB_EXPORT bool SbSystemHasCapability(SbSystemCapabilityId capability_id); |
| 302 | |
| 303 | // Gets the system's current POSIX-style Locale ID. The locale represents the |
| 304 | // location, language, and cultural conventions that the system wants to use, |
| 305 | // which affects which text is displayed to the user, and how display numbers, |
| 306 | // dates, currency, and the like are formatted. |
| 307 | // |
| 308 | // At its simplest, it can just be a BCP 47 language code, like "en_US". These |
| 309 | // days, POSIX also wants to include the encoding, like "en_US.UTF8". POSIX |
| 310 | // additionally allows a couple very bare-bones locales, like "C" or "POSIX", |
| 311 | // but they are not supported here. POSIX also supports different locale |
| 312 | // settings for a few different purposes, but Starboard only exposes a single |
| 313 | // locale at a time. |
| 314 | // |
| 315 | // RFC 5646 describing BCP 47 language codes: |
| 316 | // https://tools.ietf.org/html/bcp47 |
| 317 | // |
| 318 | // For more information than you want on POSIX locales: |
| 319 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html |
| 320 | SB_EXPORT const char* SbSystemGetLocaleId(); |
| 321 | |
| 322 | // Gets sixty-four random bits returned as an uint64_t. This is expected to be a |
| 323 | // cryptographically secure random number generator, and doesn't require manual |
| 324 | // seeding. |
| 325 | SB_EXPORT uint64_t SbSystemGetRandomUInt64(); |
| 326 | |
| 327 | // Produces an arbitrary non-negative number, |buffer_size|, of random bytes |
| 328 | // which must not be negative, placing the result in |out_buffer|, which must |
| 329 | // not be null. This is expected to be a cryptographically secure random number |
| 330 | // generator, and doesn't require manual seeding. |
| 331 | SB_EXPORT void SbSystemGetRandomData(void* out_buffer, int buffer_size); |
| 332 | |
| 333 | // Gets the last platform-specific error code produced by any Starboard call in |
| 334 | // the current thread for diagnostic purposes. Semantic reactions to Starboard |
| 335 | // function call results should be modeled explicitly. |
| 336 | SB_EXPORT SbSystemError SbSystemGetLastError(); |
| 337 | |
| 338 | // Clears the last error set by a Starboard call in the current thread. |
| 339 | SB_EXPORT void SbSystemClearLastError(); |
| 340 | |
| 341 | // Writes a human-readable string for |error|, up to |string_length| bytes, into |
| 342 | // the provided |out_string|. Returns the total desired length of the string. |
| 343 | // |out_string| may be null, in which case just the total desired length of the |
| 344 | // string is returned. |out_string| is always terminated with a null byte. |
| 345 | SB_EXPORT int SbSystemGetErrorString(SbSystemError error, |
| 346 | char* out_string, |
| 347 | int string_length); |
| 348 | |
| 349 | // Places up to |stack_size| instruction pointer addresses of the current |
| 350 | // execution stack into |out_stack|, returning the number of entries |
| 351 | // populated. |out_stack| must point to a non-NULL array of void * of at least |
| 352 | // |stack_size| entries. The returned stack frames will be in "downward" order |
| 353 | // from the calling frame towards the entry point of the thread. So, if all the |
| 354 | // stack frames do not fit, the ones truncated will be the less interesting ones |
| 355 | // towards the thread entry point. This function must be async-signal-safe on |
| 356 | // platforms that support signals, as it will be used in crash signal handlers. |
| 357 | // |
| 358 | // See the following about what it means to be async-signal-safe on POSIX: |
| 359 | // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03 |
| 360 | SB_EXPORT int SbSystemGetStack(void** out_stack, int stack_size); |
| 361 | |
| 362 | // Looks up |address| as an instruction pointer and places up to |buffer_size| - |
| 363 | // 1 characters of the symbol associated with it in |out_buffer|, which must not |
| 364 | // be NULL. |out_buffer| will be NULL-terminated. Returns whether it found a |
| 365 | // reasonable match for |address|. If the return value is false, |out_buffer| |
| 366 | // will not be modified. This function must be async-signal-safe on platforms |
| 367 | // that support signals, as it will be used in crash signal handlers. |
| 368 | SB_EXPORT bool SbSystemSymbolize(const void* address, |
| 369 | char* out_buffer, |
| 370 | int buffer_size); |
| 371 | |
| 372 | // Requests that the application be terminated gracefully at the next convenient |
| 373 | // point. Some work may continue to be done, and unrelated system events |
| 374 | // dispatched, in the meantime. This will eventually result in a |
| 375 | // kSbEventTypeStop event being dispatched to the application. When the process |
| 376 | // finally terminates, it will return |error_level|, if that has any meaning on |
| 377 | // the current platform. |
| 378 | SB_EXPORT void SbSystemRequestStop(int error_level); |
| 379 | |
| 380 | // Binary searches a sorted table |base| of |element_count| objects, each |
| 381 | // element |element_width| bytes in size for an element that |comparator| |
| 382 | // compares equal to |key|. Meant to be a drop-in replacement for bsearch. |
| 383 | SB_EXPORT void* SbSystemBinarySearch(const void* key, |
| 384 | const void* base, |
| 385 | size_t element_count, |
| 386 | size_t element_width, |
| 387 | SbSystemComparator comparator); |
| 388 | |
| 389 | // Sorts an array of elements |base|, with |element_count| elements of |
| 390 | // |element_width| bytes each, using |comparator| as the comparison function. |
| 391 | // Meant to be a drop-in replacement for qsort. |
| 392 | SB_EXPORT void SbSystemSort(void* base, |
| 393 | size_t element_count, |
| 394 | size_t element_width, |
| 395 | SbSystemComparator comparator); |
| 396 | |
David Ghandehari | ab2460b | 2016-08-30 10:52:21 -0700 | [diff] [blame] | 397 | // Hides the system splash screen, on systems that support a splash screen that |
| 398 | // is displayed while the application is loading. This function may be called |
| 399 | // from any thread and must be idempotent. |
| 400 | SB_EXPORT void SbSystemHideSplashScreen(); |
| 401 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 402 | #ifdef __cplusplus |
| 403 | } // extern "C" |
| 404 | #endif |
| 405 | |
| 406 | #endif // STARBOARD_SYSTEM_H_ |