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