0403eead |
1 | #include <stdio.h> |
697746df |
2 | #include <string.h> |
713c9224 |
3 | #include <dirent.h> |
0403eead |
4 | #include <sys/time.h> |
5 | #include <time.h> |
6 | #include <unistd.h> |
7 | |
713c9224 |
8 | #include "../common/plat.h" |
9 | |
10 | |
11 | int plat_is_dir(const char *path) |
12 | { |
13 | DIR *dir; |
14 | if ((dir = opendir(path))) { |
15 | closedir(dir); |
16 | return 1; |
17 | } |
18 | return 0; |
19 | } |
20 | |
697746df |
21 | int plat_get_root_dir(char *dst, int len) |
22 | { |
23 | extern char **g_argv; |
24 | int j; |
25 | |
26 | strncpy(dst, g_argv[0], len); |
27 | len -= 32; // reserve |
28 | if (len < 0) len = 0; |
29 | dst[len] = 0; |
30 | for (j = strlen(dst); j > 0; j--) |
31 | if (dst[j] == '/') { dst[j+1] = 0; break; } |
32 | |
33 | return j + 1; |
34 | } |
35 | |
b24e0f6c |
36 | #ifdef __GP2X__ |
37 | /* Wiz has a borked gettimeofday().. */ |
274f95a9 |
38 | #define plat_get_ticks_ms plat_get_ticks_ms_good |
39 | #define plat_get_ticks_us plat_get_ticks_us_good |
b24e0f6c |
40 | #endif |
41 | |
0403eead |
42 | unsigned int plat_get_ticks_ms(void) |
43 | { |
44 | struct timeval tv; |
45 | unsigned int ret; |
46 | |
47 | gettimeofday(&tv, NULL); |
48 | |
49 | ret = (unsigned)tv.tv_sec * 1000; |
b24e0f6c |
50 | /* approximate /= 1000 */ |
0403eead |
51 | ret += ((unsigned)tv.tv_usec * 4195) >> 22; |
52 | |
53 | return ret; |
54 | } |
55 | |
b24e0f6c |
56 | unsigned int plat_get_ticks_us(void) |
57 | { |
58 | struct timeval tv; |
59 | unsigned int ret; |
60 | |
61 | gettimeofday(&tv, NULL); |
62 | |
63 | ret = (unsigned)tv.tv_sec * 1000000; |
64 | ret += (unsigned)tv.tv_usec; |
65 | |
66 | return ret; |
67 | } |
68 | |
0403eead |
69 | void plat_sleep_ms(int ms) |
70 | { |
71 | usleep(ms * 1000); |
72 | } |
73 | |
74 | int plat_wait_event(int *fds_hnds, int count, int timeout_ms) |
75 | { |
76 | struct timeval tv, *timeout = NULL; |
77 | int i, ret, fdmax = -1; |
78 | fd_set fdset; |
79 | |
80 | if (timeout_ms >= 0) { |
81 | tv.tv_sec = timeout_ms / 1000; |
82 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
83 | timeout = &tv; |
84 | } |
85 | |
86 | FD_ZERO(&fdset); |
87 | for (i = 0; i < count; i++) { |
88 | if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i]; |
89 | FD_SET(fds_hnds[i], &fdset); |
90 | } |
91 | |
92 | ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); |
93 | if (ret == -1) |
94 | { |
95 | perror("plat_wait_event: select failed"); |
96 | sleep(1); |
97 | return -1; |
98 | } |
99 | |
100 | if (ret == 0) |
101 | return -1; /* timeout */ |
102 | |
103 | ret = -1; |
104 | for (i = 0; i < count; i++) |
105 | if (FD_ISSET(fds_hnds[i], &fdset)) |
106 | ret = fds_hnds[i]; |
107 | |
108 | return ret; |
109 | } |
110 | |