| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <jni.h>
- #include <android/log.h>
- #include <cstdarg>
- #include <cstdio>
- #include <cstdlib>
- #include <dirent.h>
- #include <dlfcn.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <time.h>
- #include <unistd.h>
- #include "OS.h"
- #include "Assert.h"
- #include "StringUtils.h"
- namespace crown
- {
- namespace os
- {
- static timespec base_time;
- static uint32_t window_width;
- static uint32_t window_height;
- //-----------------------------------------------------------------------------
- void printf(const char* string, ...)
- {
- va_list args;
- va_start(args, string);
- ::vprintf(string, args);
- va_end(args);
- }
- //-----------------------------------------------------------------------------
- void vprintf(const char* string, va_list arg)
- {
- ::vprintf(string, arg);
- }
- //-----------------------------------------------------------------------------
- void log_debug(const char* string, va_list arg)
- {
- __android_log_vprint(ANDROID_LOG_DEBUG, "crown", string, arg);
- }
- //-----------------------------------------------------------------------------
- void log_error(const char* string, va_list arg)
- {
- __android_log_vprint(ANDROID_LOG_ERROR, "crown", string, arg);
- }
- //-----------------------------------------------------------------------------
- void log_warning(const char* string, va_list arg)
- {
- __android_log_vprint(ANDROID_LOG_WARN, "crown", string, arg);
- }
- //-----------------------------------------------------------------------------
- void log_info(const char* string, va_list arg)
- {
- __android_log_vprint(ANDROID_LOG_INFO, "crown", string, arg);
- }
- //-----------------------------------------------------------------------------
- bool is_root_path(const char* path)
- {
- CE_ASSERT(path != NULL, "Path must be != NULL");
- if (string::strlen(path) == 1)
- {
- if (path[0] == PATH_SEPARATOR)
- {
- return true;
- }
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool is_absolute_path(const char* path)
- {
- CE_ASSERT(path != NULL, "Path must be != NULL");
- if (string::strlen(path) > 0)
- {
- if (path[0] == PATH_SEPARATOR)
- {
- return true;
- }
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool exists(const char* path)
- {
- struct stat dummy;
- return (stat(path, &dummy) == 0);
- }
- //-----------------------------------------------------------------------------
- bool is_directory(const char* path)
- {
- struct stat info;
- memset(&info, 0, sizeof(struct stat));
- lstat(path, &info);
- return ((S_ISDIR(info.st_mode)) != 0 && (S_ISLNK(info.st_mode) == 0));
- }
- //-----------------------------------------------------------------------------
- bool is_file(const char* path)
- {
- struct stat info;
- memset(&info, 0, sizeof(struct stat));
- lstat(path, &info);
- return ((S_ISREG(info.st_mode) != 0) && (S_ISLNK(info.st_mode) == 0));
- }
- //-----------------------------------------------------------------------------
- bool create_file(const char* path)
- {
- // Permission mask: rw-r--r--
- return ::mknod(path, S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, 0) == 0;
- }
- //-----------------------------------------------------------------------------
- bool delete_file(const char* path)
- {
- return (::unlink(path) == 0);
- }
- //-----------------------------------------------------------------------------
- bool create_directory(const char* path)
- {
- // rwxr-xr-x permission mask
- return (::mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
- }
- //-----------------------------------------------------------------------------
- bool delete_directory(const char* path)
- {
- return (::rmdir(path) == 0);
- }
- //-----------------------------------------------------------------------------
- void list_files(const char* path, Vector<DynamicString>& files)
- {
- DIR *dir;
- struct dirent *entry;
- if (!(dir = opendir(path)))
- {
- return;
- }
- while ((entry = readdir(dir)))
- {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- {
- continue;
- }
- DynamicString filename(default_allocator());
- filename = entry->d_name;
- files.push_back(filename);
- }
- closedir(dir);
- }
- //-----------------------------------------------------------------------------
- const char* normalize_path(const char* path)
- {
- static char norm[MAX_PATH_LENGTH];
- char* cur = norm;
- while ((*path) != '\0')
- {
- if ((*path) == '\\')
- {
- (*cur) = PATH_SEPARATOR;
- }
- else
- {
- (*cur) = (*path);
- }
- path++;
- cur++;
- }
- return norm;
- }
- //-----------------------------------------------------------------------------
- const char* get_cwd()
- {
- static char cwdBuf[MAX_PATH_LENGTH];
- if (getcwd(cwdBuf, MAX_PATH_LENGTH) == NULL)
- {
- return string::EMPTY;
- }
- return cwdBuf;
- }
- //-----------------------------------------------------------------------------
- const char* get_home()
- {
- char* envHome = NULL;
- envHome = getenv("HOME");
- if (envHome == NULL)
- {
- return string::EMPTY;
- }
- return envHome;
- }
- //-----------------------------------------------------------------------------
- const char* get_env(const char* env)
- {
- char* envDevel = NULL;
- envDevel = getenv(env);
- if (envDevel == NULL)
- {
- return string::EMPTY;
- }
- return envDevel;
- }
- //-----------------------------------------------------------------------------
- void init_os()
- {
- // Initilize the base time
- clock_gettime(CLOCK_MONOTONIC, &base_time);
- }
- //-----------------------------------------------------------------------------
- uint64_t milliseconds()
- {
- timespec tmp;
- clock_gettime(CLOCK_MONOTONIC, &tmp);
- return (tmp.tv_sec - base_time.tv_sec) * 1000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000000;
- }
- //-----------------------------------------------------------------------------
- uint64_t microseconds()
- {
- timespec tmp;
- clock_gettime(CLOCK_MONOTONIC, &tmp);
- return (tmp.tv_sec - base_time.tv_sec) * 1000000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000;
- }
- //-----------------------------------------------------------------------------
- void execute_process(const char* args[])
- {
- pid_t pid = fork();
- CE_ASSERT(pid != -1, "Unable to fork");
- if (pid)
- {
- int32_t dummy;
- wait(&dummy);
- }
- else
- {
- int res = execv(args[0], (char* const*)args);
- CE_ASSERT(res != -1, "Unable to exec '%s'. errno %d", args[0], res);
- exit(EXIT_SUCCESS);
- }
- }
- } // namespace os
- //-----------------------------------------------------------------------------
- extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushIntEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jint a, jint b, jint c, jint d)
- {
- OsEventParameter values[4];
- values[0].int_value = a;
- values[1].int_value = b;
- values[2].int_value = c;
- values[3].int_value = d;
- push_event((OsEventType)type, values[0], values[1], values[2], values[3]);
- }
- //-----------------------------------------------------------------------------
- extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushFloatEvent(JNIEnv * /*env*/, jobject /*obj*/, jint type, jfloat a, jfloat b, jfloat c, jfloat d)
- {
- OsEventParameter values[4];
- values[0].float_value = a;
- values[1].float_value = b;
- values[2].float_value = c;
- values[3].float_value = d;
- push_event((OsEventType)type, values[0], values[1], values[2], values[3]);
- }
- } // namespace crown
|