in_sdl: give names to gamepad buttons
[libpicofe.git] / linux / plat.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2008-2012
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  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #define _GNU_SOURCE 1
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <stdint.h>
17 #include <dirent.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/stat.h>
23 #ifndef __MINGW32__
24 #include <sys/mman.h>
25 #endif
26
27 #include "../plat.h"
28 #include "../posix.h"
29
30 /* XXX: maybe unhardcode pagesize? */
31 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
32 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
33 #ifndef MAP_HUGETLB
34 #define MAP_HUGETLB 0x40000 /* arch specific */
35 #endif
36
37
38 int plat_is_dir(const char *path)
39 {
40         DIR *dir;
41         if ((dir = opendir(path))) {
42                 closedir(dir);
43                 return 1;
44         }
45         return 0;
46 }
47
48 int plat_get_data_dir(char *dst, int len)
49 {
50         if (len > 1)
51                 strcpy(dst, "/");
52         else    *dst = 0;
53         return strlen(dst);
54 }
55
56 static int plat_get_exe_dir(char *dst, int len)
57 {
58 #ifdef PICO_DATA_DIR
59         memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR);
60         return sizeof(PICO_DATA_DIR) - 1;
61 #else
62         int j, ret = readlink(
63 #ifdef __FreeBSD__
64         "/proc/curproc/file",
65 #else
66         "/proc/self/exe",
67 #endif
68         dst, len - 1);
69         if (ret < 0) {
70                 perror("readlink");
71                 ret = 0;
72         }
73         dst[ret] = 0;
74
75         for (j = ret - 1; j > 0; j--)
76                 if (dst[j] == '/') {
77                         dst[++j] = 0;
78                         break;
79                 }
80         return j;
81 #endif
82 }
83
84 int plat_get_skin_dir(char *dst, int len)
85 {
86         int ret = plat_get_exe_dir(dst, len);
87         if (ret < 0)
88                 ret = 0;
89
90         memcpy(dst + ret, "skin/", sizeof "skin/");
91         return ret + sizeof("skin/") - 1;
92 }
93
94 #ifndef PICO_HOME_DIR
95 #define PICO_HOME_DIR "/.picodrive/"
96 #endif
97 int plat_get_root_dir(char *dst, int len)
98 {
99 #if !defined(NO_HOME_DIR) && !defined(__GP2X__) && !defined(PANDORA)
100         const char *home = getenv("HOME");
101         int ret;
102
103         if (home != NULL) {
104                 ret = snprintf(dst, len, "%s%s", home, PICO_HOME_DIR);
105                 if (ret >= len)
106                         ret = len - 1;
107                 mkdir(dst, 0755);
108                 return ret;
109         }
110 #endif
111         return plat_get_exe_dir(dst, len);
112 }
113
114 #ifdef __GP2X__
115 /* Wiz has a borked gettimeofday().. */
116 #define plat_get_ticks_ms plat_get_ticks_ms_good
117 #define plat_get_ticks_us plat_get_ticks_us_good
118 #endif
119
120 unsigned int plat_get_ticks_ms(void)
121 {
122         struct timeval tv;
123         unsigned int ret;
124
125         gettimeofday(&tv, NULL);
126
127         ret = (unsigned)tv.tv_sec * 1000;
128         /* approximate /= 1000 */
129         ret += ((unsigned)tv.tv_usec * 4194) >> 22;
130
131         return ret;
132 }
133
134 unsigned int plat_get_ticks_us(void)
135 {
136         struct timeval tv;
137         unsigned int ret;
138
139         gettimeofday(&tv, NULL);
140
141         ret = (unsigned)tv.tv_sec * 1000000;
142         ret += (unsigned)tv.tv_usec;
143
144         return ret;
145 }
146
147 void plat_sleep_ms(int ms)
148 {
149         usleep(ms * 1000);
150 }
151
152 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
153 {
154 #ifndef __MINGW32__
155         struct timeval tv, *timeout = NULL;
156         int i, ret, fdmax = -1;
157         fd_set fdset;
158
159         if (timeout_ms >= 0) {
160                 tv.tv_sec = timeout_ms / 1000;
161                 tv.tv_usec = (timeout_ms % 1000) * 1000;
162                 timeout = &tv;
163         }
164
165         FD_ZERO(&fdset);
166         for (i = 0; i < count; i++) {
167                 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
168                 FD_SET(fds_hnds[i], &fdset);
169         }
170
171         ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
172         if (ret == -1)
173         {
174                 perror("plat_wait_event: select failed");
175                 sleep(1);
176                 return -1;
177         }
178
179         if (ret == 0)
180                 return -1; /* timeout */
181
182         ret = -1;
183         for (i = 0; i < count; i++)
184                 if (FD_ISSET(fds_hnds[i], &fdset))
185                         ret = fds_hnds[i];
186 #else
187         int ret = -1;
188 #endif
189         return ret;
190 }
191
192 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
193 {
194         static int hugetlb_warned;
195         int prot = PROT_READ | PROT_WRITE;
196         int flags = MAP_PRIVATE | MAP_ANONYMOUS;
197         void *req, *ret;
198
199         req = (void *)addr;
200         if (need_exec)
201                 prot |= PROT_EXEC;
202         /* avoid MAP_FIXED, it overrides existing mappings..
203         if (is_fixed)
204                 flags |= MAP_FIXED;
205         */
206         if (size >= HUGETLB_THRESHOLD)
207                 flags |= MAP_HUGETLB;
208 #ifdef MAP_JIT
209         if (need_exec)
210                 flags |= MAP_JIT;
211 #endif
212
213         ret = mmap(req, size, prot, flags, -1, 0);
214         if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
215                 if (!hugetlb_warned) {
216                         fprintf(stderr,
217                                 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
218                                 req, size, errno);
219                         hugetlb_warned = 1;
220                 }
221                 flags &= ~MAP_HUGETLB;
222                 ret = mmap(req, size, prot, flags, -1, 0);
223 #ifdef MADV_HUGEPAGE
224                 if (ret != MAP_FAILED && ((uintptr_t)ret & (2*1024*1024 - 1))) {
225                         // try to manually realign assuming bottom-to-top alloc
226                         munmap(ret, size);
227                         ret = (void *)((uintptr_t)ret & ~(2*1024*1024 - 1));
228                         ret = mmap(ret, size, prot, flags, -1, 0);
229                 }
230                 if (ret != MAP_FAILED)
231                         madvise(ret, size, MADV_HUGEPAGE);
232 #endif
233         }
234         if (ret == MAP_FAILED)
235                 return NULL;
236
237         if (req != NULL && ret != req) {
238                 fprintf(stderr, "%s: mmaped to %p, requested %p\n",
239                         is_fixed ? "error" : "warning", ret, req);
240                 if (is_fixed) {
241                         munmap(ret, size);
242                         return NULL;
243                 }
244         }
245
246         return ret;
247 }
248
249 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
250 {
251         void *ret;
252
253 #ifdef MREMAP_MAYMOVE
254         ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
255         if (ret == MAP_FAILED)
256 #endif
257         {
258                 fprintf(stderr, "mremap %p %zd %zd: ",
259                         ptr, oldsize, newsize);
260                 perror(NULL);
261                 // might be because huge pages can't be remapped,
262                 // just make a new mapping
263                 ret = plat_mmap(0, newsize, 0, 0);
264                 if (ret == MAP_FAILED)
265                         return NULL;
266                 memcpy(ret, ptr, oldsize);
267                 munmap(ptr, oldsize);
268         }
269         if (ret != ptr)
270                 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
271
272         return ret;
273 }
274
275 void plat_munmap(void *ptr, size_t size)
276 {
277         int ret;
278
279         ret = munmap(ptr, size);
280         if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
281                 // prehaps an autorounded hugetlb mapping?
282                 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
283                 ret = munmap(ptr, size);
284         }
285         if (ret != 0) {
286                 fprintf(stderr,
287                         "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
288         }
289 }
290
291 int plat_mem_set_exec(void *ptr, size_t size)
292 {
293         int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
294         if (ret != 0)
295                 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
296                         ptr, size, errno);
297
298         return ret;
299 }
300
301 /* lprintf */
302 void lprintf(const char *fmt, ...)
303 {
304         va_list vl;
305
306         va_start(vl, fmt);
307         vprintf(fmt, vl);
308         va_end(vl);
309 }
310