AndroidOS.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <android/log.h>
  23. #include <cstdio>
  24. #include <cstdarg>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include <cstdlib>
  29. #include <sys/time.h>
  30. #include <time.h>
  31. #include <android/asset_manager_jni.h>
  32. #include <pthread.h>
  33. #include "OS.h"
  34. #include "AndroidOS.h"
  35. #include "Log.h"
  36. namespace crown
  37. {
  38. namespace os
  39. {
  40. extern "C"
  41. {
  42. // This is sadly necessary in order to get the asset manager from java...
  43. JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager);
  44. JNIEXPORT void JNICALL Java_crown_android_CrownLib_setRenderWindowMetrics(JNIEnv* env, jobject obj, jint width, jint height);
  45. };
  46. static timespec base_time;
  47. static AAssetManager* asset_manager = NULL;
  48. static uint32_t window_width;
  49. static uint32_t window_height;
  50. //-----------------------------------------------------------------------------
  51. void printf(const char* string, ...)
  52. {
  53. va_list args;
  54. va_start(args, string);
  55. ::vprintf(string, args);
  56. va_end(args);
  57. }
  58. //-----------------------------------------------------------------------------
  59. void vprintf(const char* string, va_list arg)
  60. {
  61. ::vprintf(string, arg);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void log_debug(const char* string, va_list arg)
  65. {
  66. __android_log_vprint(ANDROID_LOG_DEBUG, "crown", string, arg);
  67. }
  68. //-----------------------------------------------------------------------------
  69. void log_error(const char* string, va_list arg)
  70. {
  71. __android_log_vprint(ANDROID_LOG_ERROR, "crown", string, arg);
  72. }
  73. //-----------------------------------------------------------------------------
  74. void log_warning(const char* string, va_list arg)
  75. {
  76. __android_log_vprint(ANDROID_LOG_WARN, "crown", string, arg);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void log_info(const char* string, va_list arg)
  80. {
  81. __android_log_vprint(ANDROID_LOG_INFO, "crown", string, arg);
  82. }
  83. //-----------------------------------------------------------------------------
  84. bool exists(const char* path)
  85. {
  86. struct stat dummy;
  87. return (stat(path, &dummy) == 0);
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool is_dir(const char* path)
  91. {
  92. struct stat info;
  93. memset(&info, 0, sizeof(struct stat));
  94. lstat(path, &info);
  95. return ((S_ISDIR(info.st_mode)) != 0 && (S_ISLNK(info.st_mode) == 0));
  96. }
  97. //-----------------------------------------------------------------------------
  98. bool is_reg(const char* path)
  99. {
  100. struct stat info;
  101. memset(&info, 0, sizeof(struct stat));
  102. lstat(path, &info);
  103. return ((S_ISREG(info.st_mode) != 0) && (S_ISLNK(info.st_mode) == 0));
  104. }
  105. //-----------------------------------------------------------------------------
  106. bool mknod(const char* path)
  107. {
  108. // Permission mask: rw-r--r--
  109. return ::mknod(path, S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, 0) == 0;
  110. }
  111. //-----------------------------------------------------------------------------
  112. bool unlink(const char* path)
  113. {
  114. return (::unlink(path) == 0);
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool mkdir(const char* path)
  118. {
  119. // rwxr-xr-x permission mask
  120. return (::mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
  121. }
  122. //-----------------------------------------------------------------------------
  123. bool rmdir(const char* path)
  124. {
  125. return (::rmdir(path) == 0);
  126. }
  127. //-----------------------------------------------------------------------------
  128. const char* get_cwd()
  129. {
  130. static char cwdBuf[MAX_PATH_LENGTH];
  131. if (getcwd(cwdBuf, MAX_PATH_LENGTH) == NULL)
  132. {
  133. return string::EMPTY;
  134. }
  135. return cwdBuf;
  136. }
  137. //-----------------------------------------------------------------------------
  138. const char* get_home()
  139. {
  140. char* envHome = NULL;
  141. envHome = getenv("HOME");
  142. if (envHome == NULL)
  143. {
  144. return string::EMPTY;
  145. }
  146. return envHome;
  147. }
  148. //-----------------------------------------------------------------------------
  149. const char* get_env(const char* env)
  150. {
  151. char* envDevel = NULL;
  152. envDevel = getenv(env);
  153. if (envDevel == NULL)
  154. {
  155. return string::EMPTY;
  156. }
  157. return envDevel;
  158. }
  159. //-----------------------------------------------------------------------------
  160. // bool ls(const char* path, List<Str>& fileList)
  161. // {
  162. // DIR *dir;
  163. // struct dirent *ent;
  164. // dir = opendir(path);
  165. // if (dir == NULL)
  166. // {
  167. // return false;
  168. // }
  169. // while ((ent = readdir (dir)) != NULL)
  170. // {
  171. // fileList.push_back(Str(ent->d_name));
  172. // }
  173. // closedir (dir);
  174. // return true;
  175. // }
  176. //-----------------------------------------------------------------------------
  177. void init_os()
  178. {
  179. // Initilize the base time
  180. clock_gettime(CLOCK_MONOTONIC, &base_time);
  181. }
  182. //-----------------------------------------------------------------------------
  183. uint64_t milliseconds()
  184. {
  185. timespec tmp;
  186. clock_gettime(CLOCK_MONOTONIC, &tmp);
  187. return (tmp.tv_sec - base_time.tv_sec) * 1000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000000;
  188. }
  189. //-----------------------------------------------------------------------------
  190. uint64_t microseconds()
  191. {
  192. timespec tmp;
  193. clock_gettime(CLOCK_MONOTONIC, &tmp);
  194. return (tmp.tv_sec - base_time.tv_sec) * 1000000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000;
  195. }
  196. //-----------------------------------------------------------------------------
  197. AAssetManager* get_android_asset_manager()
  198. {
  199. return asset_manager;
  200. }
  201. //-----------------------------------------------------------------------------
  202. bool create_render_window(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fullscreen)
  203. {
  204. return true;
  205. }
  206. //-----------------------------------------------------------------------------
  207. bool destroy_render_window()
  208. {
  209. return true;
  210. }
  211. //-----------------------------------------------------------------------------
  212. void get_render_window_metrics(uint32_t& width, uint32_t& height)
  213. {
  214. width = window_width;
  215. height = window_height;
  216. }
  217. //-----------------------------------------------------------------------------
  218. void swap_buffers()
  219. {
  220. // not necessary
  221. }
  222. //-----------------------------------------------------------------------------
  223. void thread_create(ThreadFunction f, void* params, OSThread& thread, const char* name)
  224. {
  225. OSThread tid;
  226. tid.name = name;
  227. // Make thread joinable
  228. pthread_attr_t attr;
  229. pthread_attr_init(&attr);
  230. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  231. // Create thread
  232. int rc = pthread_create(&tid.thread, &attr, f, (void*)params);
  233. if (rc != 0)
  234. {
  235. os::printf("OS: ERROR: Unable to create the thread '%s' Error code: %d\n", name, rc);
  236. exit(-1);
  237. }
  238. // Free memory
  239. pthread_attr_destroy(&attr);
  240. thread = tid;
  241. }
  242. //-----------------------------------------------------------------------------
  243. void thread_join(OSThread thread)
  244. {
  245. pthread_join(thread.thread, NULL);
  246. }
  247. //-----------------------------------------------------------------------------
  248. void thread_detach(OSThread thread)
  249. {
  250. pthread_detach(thread.thread);
  251. }
  252. //-----------------------------------------------------------------------------
  253. void mutex_create(OSMutex& mutex)
  254. {
  255. OSMutex mut;
  256. pthread_mutex_init(&mut.mutex, NULL);
  257. mutex = mut;
  258. }
  259. //-----------------------------------------------------------------------------
  260. void mutex_destroy(OSMutex mutex)
  261. {
  262. pthread_mutex_destroy(&mutex.mutex);
  263. }
  264. //-----------------------------------------------------------------------------
  265. void mutex_lock(OSMutex mutex)
  266. {
  267. pthread_mutex_lock(&mutex.mutex);
  268. }
  269. //-----------------------------------------------------------------------------
  270. void mutex_unlock(OSMutex mutex)
  271. {
  272. pthread_mutex_unlock(&mutex.mutex);
  273. }
  274. //-----------------------------------------------------------------------------
  275. JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
  276. {
  277. asset_manager = AAssetManager_fromJava(env, assetManager);
  278. }
  279. //-----------------------------------------------------------------------------
  280. JNIEXPORT void JNICALL Java_crown_android_CrownLib_setRenderWindowMetrics(JNIEnv* env, jobject obj, jint width, jint height)
  281. {
  282. window_width = width;
  283. window_height = height;
  284. }
  285. } // namespace os
  286. } // namespace crown