AndroidOS.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <jni.h>
  24. #include <android/log.h>
  25. #include <cstdarg>
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <dirent.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include "OS.h"
  34. #include "Assert.h"
  35. #include "StringUtils.h"
  36. namespace crown
  37. {
  38. namespace os
  39. {
  40. static timespec base_time;
  41. static uint32_t window_width;
  42. static uint32_t window_height;
  43. //-----------------------------------------------------------------------------
  44. void printf(const char* string, ...)
  45. {
  46. va_list args;
  47. va_start(args, string);
  48. ::vprintf(string, args);
  49. va_end(args);
  50. }
  51. //-----------------------------------------------------------------------------
  52. void vprintf(const char* string, va_list arg)
  53. {
  54. ::vprintf(string, arg);
  55. }
  56. //-----------------------------------------------------------------------------
  57. void log_debug(const char* string, va_list arg)
  58. {
  59. __android_log_vprint(ANDROID_LOG_DEBUG, "crown", string, arg);
  60. }
  61. //-----------------------------------------------------------------------------
  62. void log_error(const char* string, va_list arg)
  63. {
  64. __android_log_vprint(ANDROID_LOG_ERROR, "crown", string, arg);
  65. }
  66. //-----------------------------------------------------------------------------
  67. void log_warning(const char* string, va_list arg)
  68. {
  69. __android_log_vprint(ANDROID_LOG_WARN, "crown", string, arg);
  70. }
  71. //-----------------------------------------------------------------------------
  72. void log_info(const char* string, va_list arg)
  73. {
  74. __android_log_vprint(ANDROID_LOG_INFO, "crown", string, arg);
  75. }
  76. //-----------------------------------------------------------------------------
  77. bool is_root_path(const char* path)
  78. {
  79. CE_ASSERT(path != NULL, "Path must be != NULL");
  80. if (string::strlen(path) == 1)
  81. {
  82. if (path[0] == PATH_SEPARATOR)
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool is_absolute_path(const char* path)
  91. {
  92. CE_ASSERT(path != NULL, "Path must be != NULL");
  93. if (string::strlen(path) > 0)
  94. {
  95. if (path[0] == PATH_SEPARATOR)
  96. {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. //-----------------------------------------------------------------------------
  103. bool exists(const char* path)
  104. {
  105. struct stat dummy;
  106. return (stat(path, &dummy) == 0);
  107. }
  108. //-----------------------------------------------------------------------------
  109. bool is_directory(const char* path)
  110. {
  111. struct stat info;
  112. memset(&info, 0, sizeof(struct stat));
  113. lstat(path, &info);
  114. return ((S_ISDIR(info.st_mode)) != 0 && (S_ISLNK(info.st_mode) == 0));
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool is_file(const char* path)
  118. {
  119. struct stat info;
  120. memset(&info, 0, sizeof(struct stat));
  121. lstat(path, &info);
  122. return ((S_ISREG(info.st_mode) != 0) && (S_ISLNK(info.st_mode) == 0));
  123. }
  124. //-----------------------------------------------------------------------------
  125. bool create_file(const char* path)
  126. {
  127. // Permission mask: rw-r--r--
  128. return ::mknod(path, S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, 0) == 0;
  129. }
  130. //-----------------------------------------------------------------------------
  131. bool delete_file(const char* path)
  132. {
  133. return (::unlink(path) == 0);
  134. }
  135. //-----------------------------------------------------------------------------
  136. bool create_directory(const char* path)
  137. {
  138. // rwxr-xr-x permission mask
  139. return (::mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool delete_directory(const char* path)
  143. {
  144. return (::rmdir(path) == 0);
  145. }
  146. //-----------------------------------------------------------------------------
  147. const char* get_cwd()
  148. {
  149. static char cwdBuf[MAX_PATH_LENGTH];
  150. if (getcwd(cwdBuf, MAX_PATH_LENGTH) == NULL)
  151. {
  152. return string::EMPTY;
  153. }
  154. return cwdBuf;
  155. }
  156. //-----------------------------------------------------------------------------
  157. const char* get_home()
  158. {
  159. char* envHome = NULL;
  160. envHome = getenv("HOME");
  161. if (envHome == NULL)
  162. {
  163. return string::EMPTY;
  164. }
  165. return envHome;
  166. }
  167. //-----------------------------------------------------------------------------
  168. const char* get_env(const char* env)
  169. {
  170. char* envDevel = NULL;
  171. envDevel = getenv(env);
  172. if (envDevel == NULL)
  173. {
  174. return string::EMPTY;
  175. }
  176. return envDevel;
  177. }
  178. //-----------------------------------------------------------------------------
  179. void init_os()
  180. {
  181. // Initilize the base time
  182. clock_gettime(CLOCK_MONOTONIC, &base_time);
  183. }
  184. //-----------------------------------------------------------------------------
  185. uint64_t milliseconds()
  186. {
  187. timespec tmp;
  188. clock_gettime(CLOCK_MONOTONIC, &tmp);
  189. return (tmp.tv_sec - base_time.tv_sec) * 1000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000000;
  190. }
  191. //-----------------------------------------------------------------------------
  192. uint64_t microseconds()
  193. {
  194. timespec tmp;
  195. clock_gettime(CLOCK_MONOTONIC, &tmp);
  196. return (tmp.tv_sec - base_time.tv_sec) * 1000000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000;
  197. }
  198. } // namespace os
  199. //-----------------------------------------------------------------------------
  200. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushIntEvent(JNIEnv * env, jobject obj, jint type, jint a, jint b, jint c, jint d)
  201. {
  202. OsEventParameter values[4];
  203. values[0].int_value = a;
  204. values[1].int_value = b;
  205. values[2].int_value = c;
  206. values[3].int_value = d;
  207. push_event((OsEventType)type, values[0], values[1], values[2], values[3]);
  208. }
  209. //-----------------------------------------------------------------------------
  210. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushFloatEvent(JNIEnv * env, jobject obj, jint type, jfloat a, jfloat b, jfloat c, jfloat d)
  211. {
  212. OsEventParameter values[4];
  213. values[0].float_value = a;
  214. values[1].float_value = b;
  215. values[2].float_value = c;
  216. values[3].float_value = d;
  217. push_event((OsEventType)type, values[0], values[1], values[2], values[3]);
  218. }
  219. } // namespace crown