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