OS.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. #ifdef LINUX
  31. const size_t MAX_PATH_LENGTH = 1024;
  32. const char PATH_SEPARATOR = '/';
  33. #endif
  34. #ifdef WINDOWS
  35. const size_t MAX_PATH_LENGTH = 1024;
  36. const char PATH_SEPARATOR = '\\';
  37. #define snprintf _snprintf
  38. #undef MK_SHIFT
  39. #undef MK_ALT
  40. #endif
  41. namespace os
  42. {
  43. //-----------------------------------------------------------------------------
  44. // Print and log functions
  45. //-----------------------------------------------------------------------------
  46. void printf(const char* string, ...); //!< Printf wrapper
  47. void vprintf(const char* string, va_list arg); //!< VPrintf wrapper
  48. void log_debug(const char* string, va_list arg); //!< Print debug message
  49. void log_error(const char* string, va_list arg); //!< Print error message
  50. void log_warning(const char* string, va_list arg); //!< Print warning message
  51. void log_info(const char* string, va_list arg); //!< Print info message
  52. //-----------------------------------------------------------------------------
  53. // Paths
  54. //-----------------------------------------------------------------------------
  55. bool is_root_path(const char* path);
  56. bool is_absolute_path(const char* path);
  57. //-----------------------------------------------------------------------------
  58. // File management
  59. //-----------------------------------------------------------------------------
  60. bool exists(const char* path); //!< Returns whether the path is a file or directory on the disk
  61. bool is_dir(const char* path); //!< Returns whether the path is a directory. (May not resolve symlinks.)
  62. bool is_reg(const char* path); //!< Returns whether the path is a regular file. (May not resolve symlinks.)
  63. bool mknod(const char* path); //! Creates a regular file. Returns true if success, false if not
  64. bool unlink(const char* path); //! Deletes a regular file. Returns true if success, false if not
  65. bool mkdir(const char* path); //! Creates a directory. Returns true if success, false if not
  66. bool rmdir(const char* path); //! Deletes a directory. Returns true if success, false if not
  67. //-----------------------------------------------------------------------------
  68. // OS ambient variables
  69. //-----------------------------------------------------------------------------
  70. const char* get_cwd(); //! Fills ret with the path of the current working directory. Returns true if success, false if not
  71. const char* get_home(); //! Fills ret with the path of the user home directory
  72. const char* get_env(const char* env); //! Returns the content of the 'env' environment variable or the empty string
  73. //-----------------------------------------------------------------------------
  74. // Render window and input management
  75. //-----------------------------------------------------------------------------
  76. CE_EXPORT void init_os();
  77. void get_cursor_xy(int32_t& x, int32_t& y);
  78. void set_cursor_xy(int32_t x, int32_t y);
  79. //-----------------------------------------------------------------------------
  80. // Timing
  81. //-----------------------------------------------------------------------------
  82. uint64_t milliseconds();
  83. uint64_t microseconds();
  84. //-----------------------------------------------------------------------------
  85. // Dynamic libraries
  86. //-----------------------------------------------------------------------------
  87. void* open_library(const char* path);
  88. void close_library(void* library);
  89. void* lookup_symbol(void* library, const char* name);
  90. //-----------------------------------------------------------------------------
  91. // Process execution
  92. //-----------------------------------------------------------------------------
  93. void execute_process(const char* program, const char* params);
  94. } // namespace os
  95. //-----------------------------------------------------------------------------
  96. // Events
  97. //-----------------------------------------------------------------------------
  98. enum OsEventType
  99. {
  100. OSET_NONE = 0,
  101. OSET_KEY_PRESS = 1,
  102. OSET_KEY_RELEASE = 2,
  103. OSET_BUTTON_PRESS = 3,
  104. OSET_BUTTON_RELEASE = 4,
  105. OSET_MOTION_NOTIFY = 5,
  106. OSET_TOUCH_DOWN = 6,
  107. OSET_TOUCH_MOVE = 7,
  108. OSET_TOUCH_UP = 8,
  109. OSET_ACCELEROMETER = 9
  110. };
  111. union OsEventParameter
  112. {
  113. int32_t int_value;
  114. float float_value;
  115. };
  116. struct OsEvent
  117. {
  118. OsEventType type;
  119. OsEventParameter data_a;
  120. OsEventParameter data_b;
  121. OsEventParameter data_c;
  122. OsEventParameter data_d;
  123. };
  124. /// Pushes the event @a type along with its parameters into the os' event queue.
  125. void push_event(OsEventType type, OsEventParameter data_a, OsEventParameter data_b, OsEventParameter data_c, OsEventParameter data_d);
  126. /// Returns and pops the first event in the os' event queue.
  127. OsEvent& pop_event();
  128. } // namespace crown