rm X11 include hack
[pcsx_rearmed.git] / frontend / linux / plat.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  * See the COPYING file in the top-level directory.
9  */
10
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdarg.h>
15 #include <dirent.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include <sys/mman.h>
20
21 #include "../common/plat.h"
22
23
24 int plat_is_dir(const char *path)
25 {
26         DIR *dir;
27         if ((dir = opendir(path))) {
28                 closedir(dir);
29                 return 1;
30         }
31         return 0;
32 }
33
34 int plat_get_root_dir(char *dst, int len)
35 {
36         int j, ret;
37
38         ret = readlink("/proc/self/exe", dst, len - 1);
39         if (ret < 0) {
40                 perror("readlink");
41                 ret = 0;
42         }
43         dst[ret] = 0;
44
45         for (j = strlen(dst); j > 0; j--)
46                 if (dst[j] == '/') {
47                         dst[++j] = 0;
48                         break;
49                 }
50
51         return 0;
52 }
53
54 #ifdef __GP2X__
55 /* Wiz has a borked gettimeofday().. */
56 #define plat_get_ticks_ms plat_get_ticks_ms_good
57 #define plat_get_ticks_us plat_get_ticks_us_good
58 #endif
59
60 unsigned int plat_get_ticks_ms(void)
61 {
62         struct timeval tv;
63         unsigned int ret;
64
65         gettimeofday(&tv, NULL);
66
67         ret = (unsigned)tv.tv_sec * 1000;
68         /* approximate /= 1000 */
69         ret += ((unsigned)tv.tv_usec * 4195) >> 22;
70
71         return ret;
72 }
73
74 unsigned int plat_get_ticks_us(void)
75 {
76         struct timeval tv;
77         unsigned int ret;
78
79         gettimeofday(&tv, NULL);
80
81         ret = (unsigned)tv.tv_sec * 1000000;
82         ret += (unsigned)tv.tv_usec;
83
84         return ret;
85 }
86
87 void plat_sleep_ms(int ms)
88 {
89         usleep(ms * 1000);
90 }
91
92 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
93 {
94         struct timeval tv, *timeout = NULL;
95         int i, ret, fdmax = -1;
96         fd_set fdset;
97
98         if (timeout_ms >= 0) {
99                 tv.tv_sec = timeout_ms / 1000;
100                 tv.tv_usec = (timeout_ms % 1000) * 1000;
101                 timeout = &tv;
102         }
103
104         FD_ZERO(&fdset);
105         for (i = 0; i < count; i++) {
106                 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
107                 FD_SET(fds_hnds[i], &fdset);
108         }
109
110         ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
111         if (ret == -1)
112         {
113                 perror("plat_wait_event: select failed");
114                 sleep(1);
115                 return -1;
116         }
117
118         if (ret == 0)
119                 return -1; /* timeout */
120
121         ret = -1;
122         for (i = 0; i < count; i++)
123                 if (FD_ISSET(fds_hnds[i], &fdset))
124                         ret = fds_hnds[i];
125
126         return ret;
127 }
128
129 void *plat_mmap(unsigned long addr, size_t size)
130 {
131         void *req, *ret;
132
133         req = (void *)addr;
134         ret = mmap(req, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
135         if (ret == MAP_FAILED)
136                 return NULL;
137         if (ret != req)
138                 printf("warning: mmaped to %p, requested %p\n", ret, req);
139
140         return ret;
141 }
142
143 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
144 {
145         void *ret;
146
147         ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
148         if (ret == MAP_FAILED)
149                 return NULL;
150         if (ret != ptr)
151                 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
152
153         return ret;
154 }
155
156 void plat_munmap(void *ptr, size_t size)
157 {
158         munmap(ptr, size);
159 }
160
161 /* lprintf */
162 void lprintf(const char *fmt, ...)
163 {
164         va_list vl;
165
166         va_start(vl, fmt);
167         vprintf(fmt, vl);
168         va_end(vl);
169 }
170