| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <dirent.h> |
| 4 | #include <sys/time.h> |
| 5 | #include <time.h> |
| 6 | #include <unistd.h> |
| 7 | #include <sys/mman.h> |
| 8 | |
| 9 | #include "../common/plat.h" |
| 10 | |
| 11 | |
| 12 | int plat_is_dir(const char *path) |
| 13 | { |
| 14 | DIR *dir; |
| 15 | if ((dir = opendir(path))) { |
| 16 | closedir(dir); |
| 17 | return 1; |
| 18 | } |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | int plat_get_root_dir(char *dst, int len) |
| 23 | { |
| 24 | extern char **g_argv; |
| 25 | int j; |
| 26 | |
| 27 | strncpy(dst, g_argv[0], len); |
| 28 | len -= 32; // reserve |
| 29 | if (len < 0) len = 0; |
| 30 | dst[len] = 0; |
| 31 | for (j = strlen(dst); j > 0; j--) |
| 32 | if (dst[j] == '/') { dst[j+1] = 0; break; } |
| 33 | |
| 34 | return j + 1; |
| 35 | } |
| 36 | |
| 37 | #ifdef __GP2X__ |
| 38 | /* Wiz has a borked gettimeofday().. */ |
| 39 | #define plat_get_ticks_ms plat_get_ticks_ms_good |
| 40 | #define plat_get_ticks_us plat_get_ticks_us_good |
| 41 | #endif |
| 42 | |
| 43 | unsigned int plat_get_ticks_ms(void) |
| 44 | { |
| 45 | struct timeval tv; |
| 46 | unsigned int ret; |
| 47 | |
| 48 | gettimeofday(&tv, NULL); |
| 49 | |
| 50 | ret = (unsigned)tv.tv_sec * 1000; |
| 51 | /* approximate /= 1000 */ |
| 52 | ret += ((unsigned)tv.tv_usec * 4195) >> 22; |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | unsigned int plat_get_ticks_us(void) |
| 58 | { |
| 59 | struct timeval tv; |
| 60 | unsigned int ret; |
| 61 | |
| 62 | gettimeofday(&tv, NULL); |
| 63 | |
| 64 | ret = (unsigned)tv.tv_sec * 1000000; |
| 65 | ret += (unsigned)tv.tv_usec; |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | void plat_sleep_ms(int ms) |
| 71 | { |
| 72 | usleep(ms * 1000); |
| 73 | } |
| 74 | |
| 75 | int plat_wait_event(int *fds_hnds, int count, int timeout_ms) |
| 76 | { |
| 77 | struct timeval tv, *timeout = NULL; |
| 78 | int i, ret, fdmax = -1; |
| 79 | fd_set fdset; |
| 80 | |
| 81 | if (timeout_ms >= 0) { |
| 82 | tv.tv_sec = timeout_ms / 1000; |
| 83 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 84 | timeout = &tv; |
| 85 | } |
| 86 | |
| 87 | FD_ZERO(&fdset); |
| 88 | for (i = 0; i < count; i++) { |
| 89 | if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i]; |
| 90 | FD_SET(fds_hnds[i], &fdset); |
| 91 | } |
| 92 | |
| 93 | ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); |
| 94 | if (ret == -1) |
| 95 | { |
| 96 | perror("plat_wait_event: select failed"); |
| 97 | sleep(1); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | if (ret == 0) |
| 102 | return -1; /* timeout */ |
| 103 | |
| 104 | ret = -1; |
| 105 | for (i = 0; i < count; i++) |
| 106 | if (FD_ISSET(fds_hnds[i], &fdset)) |
| 107 | ret = fds_hnds[i]; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | void *plat_mmap(unsigned long addr, size_t size) |
| 113 | { |
| 114 | void *req, *ret; |
| 115 | req = (void *)addr; |
| 116 | ret = mmap(req, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
| 117 | if (ret == MAP_FAILED) |
| 118 | return NULL; |
| 119 | if (ret != req) |
| 120 | printf("warning: mmaped to %p, requested %p\n", ret, req); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | void plat_munmap(void *ptr, size_t size) |
| 126 | { |
| 127 | munmap(ptr, size); |
| 128 | } |
| 129 | |