revive pandora and win32 builds, rm gp2x dep for linux, lots of refactoring
[libpicofe.git] / linux / plat.c
CommitLineData
4ab30ad4 1#include <stdio.h>
d2f29611 2#include <string.h>
049a6b3e 3#include <dirent.h>
4ab30ad4 4#include <sys/time.h>
5#include <time.h>
6#include <unistd.h>
7
049a6b3e 8#include "../common/plat.h"
9
10
11int 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
d2f29611 21int 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
b5bfb864 36#ifdef __GP2X__
37/* Wiz has a borked gettimeofday().. */
1eb704b6 38#define plat_get_ticks_ms plat_get_ticks_ms_good
39#define plat_get_ticks_us plat_get_ticks_us_good
b5bfb864 40#endif
41
4ab30ad4 42unsigned 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;
b5bfb864 50 /* approximate /= 1000 */
4ab30ad4 51 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
52
53 return ret;
54}
55
b5bfb864 56unsigned 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
4ab30ad4 69void plat_sleep_ms(int ms)
70{
71 usleep(ms * 1000);
72}
73
74int 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