OS.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstdarg>
  25. #include "Config.h"
  26. #include "Types.h"
  27. #include "Vector.h"
  28. #include "DynamicString.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. #if defined(LINUX)
  33. const size_t MAX_PATH_LENGTH = 1024;
  34. const char PATH_SEPARATOR = '/';
  35. #elif defined(WINDOWS)
  36. const size_t MAX_PATH_LENGTH = 1024;
  37. const char PATH_SEPARATOR = '\\';
  38. #define snprintf _snprintf
  39. #undef MK_SHIFT
  40. #undef MK_ALT
  41. #elif defined(ANDROID)
  42. const size_t MAX_PATH_LENGTH = 1024;
  43. const char PATH_SEPARATOR = '/';
  44. #else
  45. #error "Oops, invalid platform!"
  46. #endif
  47. namespace os
  48. {
  49. //-----------------------------------------------------------------------------
  50. // Print and log functions
  51. //-----------------------------------------------------------------------------
  52. void printf(const char* string, ...); //!< Printf wrapper
  53. void vprintf(const char* string, va_list arg); //!< VPrintf wrapper
  54. void log_debug(const char* string, va_list arg); //!< Print debug message
  55. void log_error(const char* string, va_list arg); //!< Print error message
  56. void log_warning(const char* string, va_list arg); //!< Print warning message
  57. void log_info(const char* string, va_list arg); //!< Print info message
  58. //-----------------------------------------------------------------------------
  59. // Paths
  60. //-----------------------------------------------------------------------------
  61. bool is_root_path(const char* path);
  62. bool is_absolute_path(const char* path);
  63. //-----------------------------------------------------------------------------
  64. // File management
  65. //-----------------------------------------------------------------------------
  66. /// Returns whether the path is a file or directory on the disk
  67. bool exists(const char* path);
  68. /// Returns whether the path is a directory. (May not resolve symlinks.)
  69. bool is_directory(const char* path);
  70. /// Returns whether the path is a regular file. (May not resolve symlinks.)
  71. bool is_file(const char* path);
  72. /// Creates a regular file. Returns true if success, false if not
  73. bool create_file(const char* path);
  74. /// Deletes a regular file. Returns true if success, false if not
  75. bool delete_file(const char* path);
  76. /// Creates a directory. Returns true if success, false if not
  77. bool create_directory(const char* path);
  78. /// Deletes a directory. Returns true if success, false if not
  79. bool delete_directory(const char* path);
  80. /// Returns the list of @a files in the given @a dir directory. Optionally walks into
  81. /// subdirectories whether @a recursive is true.
  82. /// @note
  83. /// Does not follow symbolic links.
  84. void list_files(const char* path, Vector<DynamicString>& files);
  85. //-----------------------------------------------------------------------------
  86. // OS ambient variables
  87. //-----------------------------------------------------------------------------
  88. /// Fills ret with the path of the current working directory. Returns true if success, false if not
  89. const char* get_cwd();
  90. /// Fills ret with the path of the user home directory
  91. const char* get_home();
  92. /// Returns the content of the 'env' environment variable or the empty string
  93. const char* get_env(const char* env);
  94. //-----------------------------------------------------------------------------
  95. // Render window and input management
  96. //-----------------------------------------------------------------------------
  97. CE_EXPORT void init_os();
  98. void get_cursor_xy(int32_t& x, int32_t& y);
  99. void set_cursor_xy(int32_t x, int32_t y);
  100. //-----------------------------------------------------------------------------
  101. // Timing
  102. //-----------------------------------------------------------------------------
  103. uint64_t milliseconds();
  104. uint64_t microseconds();
  105. //-----------------------------------------------------------------------------
  106. // Dynamic libraries
  107. //-----------------------------------------------------------------------------
  108. void* open_library(const char* path);
  109. void close_library(void* library);
  110. void* lookup_symbol(void* library, const char* name);
  111. //-----------------------------------------------------------------------------
  112. // Process execution
  113. //-----------------------------------------------------------------------------
  114. /// Executes a process.
  115. /// @a args is an array of arguments where:
  116. /// @a args[0] is the path to the program executable,
  117. /// @a args[1, 2, ..., n-1] is a list of arguments to pass to the executable,
  118. /// @a args[n] is NULL.
  119. void execute_process(const char* args[]);
  120. } // namespace os
  121. //-----------------------------------------------------------------------------
  122. // Events
  123. //-----------------------------------------------------------------------------
  124. enum OsEventType
  125. {
  126. OSET_NONE = 0,
  127. OSET_KEY_PRESS = 1,
  128. OSET_KEY_RELEASE = 2,
  129. OSET_BUTTON_PRESS = 3,
  130. OSET_BUTTON_RELEASE = 4,
  131. OSET_MOTION_NOTIFY = 5,
  132. OSET_TOUCH_DOWN = 6,
  133. OSET_TOUCH_MOVE = 7,
  134. OSET_TOUCH_UP = 8,
  135. OSET_ACCELEROMETER = 9
  136. };
  137. union OsEventParameter
  138. {
  139. int32_t int_value;
  140. float float_value;
  141. };
  142. struct OsEvent
  143. {
  144. OsEventType type;
  145. OsEventParameter data_a;
  146. OsEventParameter data_b;
  147. OsEventParameter data_c;
  148. OsEventParameter data_d;
  149. };
  150. /// Pushes the event @a type along with its parameters into the os' event queue.
  151. void push_event(OsEventType type, OsEventParameter data_a, OsEventParameter data_b, OsEventParameter data_c, OsEventParameter data_d);
  152. /// Returns and pops the first event in the os' event queue.
  153. OsEvent& pop_event();
  154. } // namespace crown