1 /*
2  * missing.cpp
3  *
4  * Written by
5  *  Locnet <android.locnet@gmail.com>
6  *
7  * This file is part of VICE, the Versatile Commodore Emulator.
8  * See README for copyright notice.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  *  02111-1307  USA.
24  *
25  */
26 
27 #include <time.h>
28 #include <malloc.h>
29 #include <sys/select.h>
30 #include <android/log.h>
31 #include "loader.h"
32 
33 #include "SDL.h"
34 #include "SDL_thread.h"
35 
36 /* Fallback */
37 #ifndef LOCNET_LOG_TAG
38 #define LOCNET_LOG_TAG "c64"
39 #endif
40 
41 void (SDLCALL *mixerCallBack)(void *userdata, Uint8 *stream, int len) = NULL;
42 
43 struct SDL_mutex {
44     int recursive;
45     Uint32 owner;
46     SDL_sem *sem;
47 };
48 
49 SDL_Surface sdl_surface;
50 SDL_PixelFormat sdl_format;
51 char tmp[20] = { 0 };
52 
53 //C64
SDL_MapRGB(const SDL_PixelFormat * const format,const Uint8 r,const Uint8 g,const Uint8 b)54 extern DECLSPEC Uint32 SDLCALL SDL_MapRGB(const SDL_PixelFormat *const format, const Uint8 r, const Uint8 g, const Uint8 b)
55 {
56     return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x1F);
57 }
58 
SDL_CreateRGBSurface(Uint32 flags,int width,int height,int bitsPerPixel,Uint32 Rmask,Uint32 Gmask,Uint32 Bmask,Uint32 Amask)59 extern DECLSPEC SDL_Surface* SDLCALL SDL_CreateRGBSurface(Uint32 flags, int width, int height, int bitsPerPixel, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
60 {
61     __android_log_print(ANDROID_LOG_INFO, LOCNET_LOG_TAG, "SDL_CreateRGBSurface: %d %d %d %d", width, height, bitsPerPixel, flags);
62     return SDL_SetVideoMode(width, height, bitsPerPixel, flags);
63 }
64 
SDL_SetColors(SDL_Surface * surface,SDL_Color * colors,int firstcolor,int ncolors)65 extern DECLSPEC int SDLCALL SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
66 {
67     __android_log_print(ANDROID_LOG_INFO, LOCNET_LOG_TAG, "SDL_SetColors");
68     return 0;
69 }
70 
SDL_GetMouseState(int * x,int * y)71 extern DECLSPEC Uint8 SDLCALL SDL_GetMouseState(int *x, int *y)
72 {
73     return 0;
74 }
75 
SDL_GetAppState(void)76 extern DECLSPEC Uint8 SDLCALL SDL_GetAppState(void)
77 {
78     return SDL_APPACTIVE | SDL_APPINPUTFOCUS | SDL_APPMOUSEFOCUS;
79 }
80 
SDL_EnableKeyRepeat(int delay,int interval)81 extern DECLSPEC int SDLCALL SDL_EnableKeyRepeat(int delay, int interval)
82 {
83     return 0;
84 }
SDL_EnableUNICODE(int enable)85 extern DECLSPEC int SDLCALL SDL_EnableUNICODE(int enable)
86 {
87     return 0;
88 }
SDL_CloseAudio(void)89 extern DECLSPEC void SDLCALL SDL_CloseAudio(void)
90 {
91 }
92 
SDL_FreeSurface(SDL_Surface * surface)93 extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface *surface)
94 {
95 }
96 
97 extern struct loader_config *loadf;
98 
SDL_UnlockSurface(SDL_Surface * surface)99 extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface)
100 {
101 }
102 
SDL_LockSurface(SDL_Surface * surface)103 extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface *surface)
104 {
105     Android_LockSurface();
106     surface->pixels = loadf->videoBuffer;
107     return 0;
108 }
109 
SDL_SetVideoMode(int width,int height,int bitsperpixel,Uint32 flags)110 extern DECLSPEC SDL_Surface *SDLCALL SDL_SetVideoMode(int width, int height, int bitsperpixel, Uint32 flags)
111 {
112     Android_SetVideoMode(width, height, 16);
113     sdl_surface.flags = (flags & SDL_FULLSCREEN);
114     sdl_surface.w = width;
115     sdl_surface.h = height;
116     sdl_surface.pitch = width * 2;
117     sdl_surface.format = &sdl_format;
118     sdl_surface.pixels = loadf->videoBuffer;
119     sdl_surface.format->BytesPerPixel = 2;
120     sdl_surface.format->BitsPerPixel = 16;
121     sdl_surface.format->palette = 0;
122     sdl_surface.format->Rmask = 0x00f800;
123     sdl_surface.format->Gmask = 0x0007e0;
124     sdl_surface.format->Bmask = 0x00001f;
125     sdl_surface.format->Amask = 0;
126     sdl_surface.format->Rloss = 3;
127     sdl_surface.format->Gloss = 2;
128     sdl_surface.format->Bloss = 3;
129     sdl_surface.format->Rshift = 11;
130     sdl_surface.format->Gshift = 5;
131     sdl_surface.format->Bshift = 0;
132 
133     return &sdl_surface;
134 }
135 
SDL_FillRect(SDL_Surface * dst,SDL_Rect * dstrect,Uint32 color)136 extern DECLSPEC int SDLCALL SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
137 {
138     __android_log_print(ANDROID_LOG_INFO, LOCNET_LOG_TAG, "SDL_FillRect");
139     return 0;
140 }
141 
SDL_UpdateRect(SDL_Surface * screen,Sint32 x,Sint32 y,Uint32 w,Uint32 h)142 extern DECLSPEC void SDLCALL SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
143 {
144     if ((x == 0) && (y == 0) && (w == 0) && (h == 0)) {
145         __android_log_print(ANDROID_LOG_INFO, LOCNET_LOG_TAG, "SDL_UpdateRect: %d %d %d %d", x, y, w, h);
146     }
147     Android_UnlockSurface(y, y + h);
148 }
149 
150 #define SDL_malloc      malloc
151 #define SDL_free        free
152 
stdio_seek(SDL_RWops * context,int offset,int whence)153 static int SDLCALL stdio_seek(SDL_RWops *context, int offset, int whence)
154 {
155     if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
156         return(ftell(context->hidden.stdio.fp));
157     } else {
158         return(-1);
159     }
160 }
161 
stdio_read(SDL_RWops * context,void * ptr,int size,int maxnum)162 static int SDLCALL stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
163 {
164     size_t nread;
165 
166     nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
167     if (nread == 0 && ferror(context->hidden.stdio.fp)) {
168     }
169     return(nread);
170 }
171 
stdio_write(SDL_RWops * context,const void * ptr,int size,int num)172 static int SDLCALL stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
173 {
174     size_t nwrote;
175 
176     nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
177     if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
178     }
179     return(nwrote);
180 }
181 
stdio_close(SDL_RWops * context)182 static int SDLCALL stdio_close(SDL_RWops *context)
183 {
184     if (context) {
185         if (context->hidden.stdio.autoclose) {
186             fclose(context->hidden.stdio.fp);
187         }
188         SDL_FreeRW(context);
189     }
190     return(0);
191 }
192 
SDL_AllocRW(void)193 SDL_RWops *SDL_AllocRW(void)
194 {
195     SDL_RWops *area;
196 
197     area = (SDL_RWops *)SDL_malloc(sizeof *area);
198     if (area == NULL) {
199     }
200     return(area);
201 }
202 
SDL_FreeRW(SDL_RWops * area)203 void SDL_FreeRW(SDL_RWops *area)
204 {
205     SDL_free(area);
206 }
207 
SDL_RWFromFP(FILE * fp,int autoclose)208 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
209 {
210     SDL_RWops *rwops = NULL;
211 
212     rwops = SDL_AllocRW();
213     if (rwops != NULL) {
214         rwops->seek = stdio_seek;
215         rwops->read = stdio_read;
216         rwops->write = stdio_write;
217         rwops->close = stdio_close;
218         rwops->hidden.stdio.fp = fp;
219         rwops->hidden.stdio.autoclose = autoclose;
220     }
221     return(rwops);
222 }
223 
SDL_RWFromFile(const char * file,const char * mode)224 extern DECLSPEC SDL_RWops * SDLCALL  SDL_RWFromFile(const char *file, const char *mode)
225 {
226     SDL_RWops *rwops = NULL;
227     FILE *fp = NULL;
228     if (!file || !*file || !mode || !*mode) {
229         return NULL;
230     }
231 
232     fp = fopen(file, mode);
233     if (fp != NULL) {
234         rwops = SDL_RWFromFP(fp, 1);
235     }
236 
237     return(rwops);
238 }
239 
SDL_CreateMutex(void)240 extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void)
241 {
242     SDL_mutex *mutex;
243 
244     /* Allocate mutex memory */
245     mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
246     if (mutex) {
247         /* Create the mutex semaphore, with initial value 1 */
248         mutex->sem = 0;
249         mutex->recursive = 0;
250         mutex->owner = 0;
251         if (!mutex->sem) {
252             SDL_free(mutex);
253             mutex = NULL;
254         }
255     }
256     return mutex;
257 }
258 
SDL_DestroyMutex(SDL_mutex * mutex)259 extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
260 {
261     if (mutex) {
262         SDL_free(mutex);
263     }
264 }
265 
SDL_mutexP(SDL_mutex * mutex)266 extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex)
267 {
268     return 0;
269 }
270 
SDL_mutexV(SDL_mutex * mutex)271 extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex)
272 {
273     return 0;
274 }
275 
SDL_OpenAudio(SDL_AudioSpec * desired,SDL_AudioSpec * obtained)276 extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
277 {
278     *obtained = *desired;
279     mixerCallBack = desired->callback;
280     int bufSize = Android_OpenAudio(desired->freq, desired->channels, 1, desired->samples << 2);
281 
282     if (bufSize == 0) {
283         return -1;
284     }
285 
286     obtained->samples = bufSize >> 2;
287     Android_AudioGetBuffer();
288 
289     return 0;
290 }
291 
SDL_Delay(Uint32 ms)292 extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms)
293 {
294     int was_error;
295 
296 #if HAVE_NANOSLEEP
297     struct timespec elapsed, tv;
298 #else
299     struct timeval tv;
300     Uint32 then, now, elapsed;
301 #endif
302 
303     /* Set the timeout interval */
304 #if HAVE_NANOSLEEP
305     elapsed.tv_sec = ms / 1000;
306     elapsed.tv_nsec = (ms % 1000) * 1000000;
307 #else
308     then = SDL_GetTicks();
309 #endif
310     do {
311 
312 #if HAVE_NANOSLEEP
313         tv.tv_sec = elapsed.tv_sec;
314         tv.tv_nsec = elapsed.tv_nsec;
315         was_error = nanosleep(&tv, &elapsed);
316 #else
317         /* Calculate the time interval left (in case of interrupt) */
318         now = SDL_GetTicks();
319         elapsed = (now - then);
320         then = now;
321         if (elapsed >= ms) {
322             break;
323         }
324         ms -= elapsed;
325         tv.tv_sec = ms / 1000;
326         tv.tv_usec = (ms % 1000) * 1000;
327 
328         was_error = select(0, NULL, NULL, NULL, &tv);
329 #endif /* HAVE_NANOSLEEP */
330     } while (was_error);
331 }
332 
SDL_GetTicks(void)333 extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void)
334 {
335 #if HAVE_CLOCK_GETTIME
336     Uint32 ticks;
337     struct timespec now;
338     clock_gettime(CLOCK_MONOTONIC, &now);
339     ticks = (now.tv_sec) * 1000 + (now.tv_nsec) / 1000000;
340     return (ticks);
341 #else
342     Uint32 ticks;
343     struct timeval now;
344     gettimeofday(&now, NULL);
345     ticks = (now.tv_sec) * 1000 + (now.tv_usec) / 1000;
346     return (ticks);
347 #endif
348 }
349 
350 
351 //dummy functions
352 
353 int sdl_joystick;
354 
SDL_JoystickOpen(int index)355 extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int index)
356 {
357     return (SDL_Joystick *)&sdl_joystick;
358 }
359 
SDL_JoystickClose(SDL_Joystick * joystick)360 extern DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick *joystick)
361 {
362 }
363 
SDL_NumJoysticks(void)364 extern DECLSPEC int SDLCALL SDL_NumJoysticks(void)
365 {
366     return 1;
367 }
368 
SDL_JoystickNumButtons(SDL_Joystick * joystick)369 extern DECLSPEC int SDLCALL SDL_JoystickNumButtons(SDL_Joystick *joystick)
370 {
371     return 2;
372 }
373 
SDL_JoystickGetAxis(SDL_Joystick * joystick,int axis)374 extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
375 {
376     return 0;
377 }
378 
SDL_JoystickNumAxes(SDL_Joystick * joystick)379 extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick *joystick)
380 {
381     return 2;
382 }
383 
SDL_JoystickGetButton(SDL_Joystick * joystick,int button)384 extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
385 {
386     return 0;
387 }
388 
SDL_JoystickNumHats(SDL_Joystick * joystick)389 extern DECLSPEC int SDLCALL SDL_JoystickNumHats(SDL_Joystick *joystick)
390 {
391     return 0;
392 }
393 
SDL_JoystickNumBalls(SDL_Joystick * joystick)394 extern DECLSPEC int SDLCALL SDL_JoystickNumBalls(SDL_Joystick *joystick)
395 {
396     return 0;
397 }
398 
SDL_JoystickName(int index)399 extern DECLSPEC const char * SDLCALL SDL_JoystickName(int index)
400 {
401     sprintf(tmp, "Joystick %d", index);
402     return tmp;
403 }
404 
SDL_JoystickUpdate(void)405 extern DECLSPEC void SDLCALL SDL_JoystickUpdate(void)
406 {
407 }
408 
SDL_JoystickEventState(int state)409 extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state)
410 {
411     return 0;
412 }
413 
SDL_GetKeyName(SDLKey key)414 extern DECLSPEC char * SDLCALL SDL_GetKeyName(SDLKey key)
415 {
416     sprintf(tmp, "Key %d", key);
417     return key;
418 }
419 
SDL_Flip(SDL_Surface * screen)420 extern DECLSPEC int SDLCALL SDL_Flip(SDL_Surface* screen)
421 {
422     return 0;
423 }
424 
SDL_WM_GrabInput(SDL_GrabMode mode)425 extern DECLSPEC SDL_GrabMode SDLCALL SDL_WM_GrabInput(SDL_GrabMode mode)
426 {
427     return SDL_GRAB_ON;
428 }
429 
SDL_ShowCursor(int toggle)430 extern DECLSPEC int SDLCALL SDL_ShowCursor(int toggle)
431 {
432     return SDL_ENABLE;
433 }
434 
SDL_WM_SetCaption(const char * title,const char * icon)435 extern DECLSPEC void SDLCALL SDL_WM_SetCaption(const char *title, const char *icon)
436 {
437 }
438 
SDL_CDNumDrives()439 extern DECLSPEC int SDLCALL SDL_CDNumDrives()
440 {
441     return 0;
442 }
443 
SDL_CDName(int drive)444 extern DECLSPEC const char *SDLCALL SDL_CDName(int drive)
445 {
446     sprintf(tmp, "CDROM %d", drive);
447     return tmp;
448 }
449 
SDL_GetError(void)450 extern DECLSPEC char *SDLCALL SDL_GetError(void)
451 {
452     return (char *)"";
453 }
454 
SDL_PauseAudio(int pause_on)455 extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on)
456 {
457 }
458 
SDL_LockAudio(void)459 extern DECLSPEC void SDLCALL SDL_LockAudio(void)
460 {
461 }
462 
SDL_UnlockAudio(void)463 extern DECLSPEC void SDLCALL SDL_UnlockAudio(void)
464 {
465 }
466 
SDL_Init(Uint32 flags)467 extern DECLSPEC int SDLCALL SDL_Init(Uint32 flags)
468 {
469     return 0;
470 }
471 
SDL_InitSubSystem(Uint32 flags)472 extern DECLSPEC int SDLCALL SDL_InitSubSystem(Uint32 flags)
473 {
474     return 0;
475 }
476 
SDL_Quit(void)477 extern DECLSPEC void SDLCALL SDL_Quit (void)
478 {
479 }
480