1 /** \file   archdep.c
2  * \brief   Miscellaneous system-specific stuff for SDL
3  *
4  * \author  Marco van den Heuvel <blackystardust68@yahoo.com>
5  * \author  Bas Wassink <b.wassink@ziggo.nl>
6  */
7 
8 /*
9  * This file is part of VICE, the Versatile Commodore Emulator.
10  * See README for copyright notice.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25  *  02111-1307  USA.
26  *
27  */
28 
29 #include "vice.h"
30 
31 #ifdef AMIGA_SUPPORT
32 #ifndef __VBCC__
33 #define __USE_INLINE__
34 #endif
35 
36 #include <proto/dos.h>
37 #include <proto/exec.h>
38 
39 #ifndef AMIGA_OS4
40 #include <proto/socket.h>
41 #endif
42 #endif /* AMIGA_SUPPORT */
43 
44 #include "vice_sdl.h"
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #ifdef HAVE_STRINGS_H
50 #include <strings.h>
51 #endif
52 #include <ctype.h>
53 #include <errno.h>
54 #include <signal.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
57 
58 #include "archdep.h"
59 #include "findpath.h"
60 #include "ioutil.h"
61 #include "kbd.h"
62 #include "keyboard.h"
63 #include "lib.h"
64 #include "log.h"
65 #include "machine.h"
66 #include "ui.h"
67 #include "util.h"
68 
69 /* FIXME: includes for beos */
70 /* FIXME: includes for amiga */
71 /* FIXME: includes for os/2 */
72 
73 /* These functions are defined in the files included below and
74    used lower down. */
75 static int archdep_init_extra(int *argc, char **argv);
76 static void archdep_shutdown_extra(void);
77 
78 #ifdef AMIGA_SUPPORT
79 
80 #if defined(AMIGA_OS4)
81 #include <exec/execbase.h>
82 #ifndef __USE_BASETYPE__
83   extern struct Library * SysBase;
84 #else
85   extern struct ExecBase * SysBase;
86 #endif /* __USE_BASETYPE__ */
87 #endif
88 
89 #ifndef AMIGA_OS4
90 struct Library *SocketBase;
91 #endif
92 
93 #ifdef POWERSDL_AMIGA_INLINE
94 struct Library *PowerSDLBase = NULL;
95 #define SDLLIBBASE PowerSDLBase
96 #define SDLLIBNAME "powersdl.library"
97 #endif
98 
99 #ifdef SDL_AMIGA_INLINE
100 struct Library *SDLBase = NULL;
101 #define SDLLIBBASE SDLBase
102 #define SDLLIBNAME "SDL.library"
103 #endif
104 
105 #if defined(SDL_AMIGA_INLINE) || defined(POWERSDL_AMIGA_INLINE)
SDL_Quit(void)106 void SDL_Quit(void)
107 {
108     SDL_RealQuit();
109     CloseLibrary(SDLLIBBASE);
110 }
111 #endif
112 
113 #ifdef SDL_AMIGA_INLINE
SDL_Init(Uint32 flags)114 int SDL_Init(Uint32 flags)
115 {
116     SDLLIBBASE = OpenLibrary(SDLLIBNAME, 0L);
117 
118     if (!SDLLIBBASE) {
119         printf("Unable to open %s\n", SDLLIBNAME);
120         archdep_vice_exit(0);
121     }
122 
123     return SDL_RealInit(flags);
124 }
125 #endif
126 
127 #ifdef POWERSDL_AMIGA_INLINE
VICE_SDL_Init(Uint32 flags)128 int VICE_SDL_Init(Uint32 flags)
129 {
130     SDLLIBBASE = OpenLibrary(SDLLIBNAME, 0L);
131 
132     if (!SDLLIBBASE) {
133         printf("Unable to open %s\n", SDLLIBNAME);
134         archdep_vice_exit(0);
135     }
136     return SDL_Init(flags);
137 }
138 
139 #define SDL_REALINIT VICE_SDL_Init
140 #endif
141 
142 #define __USE_INLINE__
143 
144 #undef BYTE
145 #undef WORD
146 #include <exec/types.h>
147 #include <exec/nodes.h>
148 #include <exec/lists.h>
149 #include <exec/memory.h>
150 
151 #include <proto/exec.h>
152 #include <proto/intuition.h>
153 
154 #ifdef AMIGA_OS4
155 struct Library *ExpansionBase = NULL;
156 struct ExpansionIFace *IExpansion = NULL;
157 #endif
158 
159 #ifdef HAVE_PROTO_OPENPCI_H
160 struct Library *OpenPciBase = NULL;
161 #endif
162 
163 #if defined(HAVE_PROTO_OPENPCI_H) || defined(AMIGA_OS4)
164 int pci_lib_loaded = 1;
165 #endif
166 
167 /* ----------------------------------------------------------------------- */
168 
169 #define LIBS_ACTION_ERROR     0
170 #define LIBS_ACTION_WARNING   1
171 
172 typedef struct amiga_libs_s {
173     char *lib_name;
174     void **lib_base;
175     int lib_version;
176     void **interface_base;
177     int action;
178     int **var;
179 } amiga_libs_t;
180 
181 static amiga_libs_t amiga_libs[] = {
182 #ifdef AMIGA_OS4
183     { "expansion.library", &ExpansionBase, 50, &IExpansion, LIBS_ACTION_WARNING, &pci_lib_loaded },
184 #endif
185 #ifdef HAVE_PROTO_OPENPCI_H
186     { "openpci.library", &OpenPciBase, 0, NULL, LIBS_ACTION_WARNING, &pci_lib_loaded },
187 #endif
188     { NULL, NULL, 0, NULL, 0, NULL }
189 };
190 
load_libs(void)191 int load_libs(void)
192 {
193     int i = 0;
194 
195     while (amiga_libs[i].lib_name) {
196         amiga_libs[i].lib_base[0] = OpenLibrary(amiga_libs[i].lib_name, amiga_libs[i].lib_version);
197 #ifdef AMIGA_OS4
198         if (amiga_libs[i].lib_base[0]) {
199             amiga_libs[i].interface_base[0] = GetInterface(amiga_libs[i].lib_base[0], "main", 1, NULL);
200             if (amiga_libs[i].interface_base[0] == NULL) {
201                 CloseLibrary(amiga_libs[i].lib_base[0]);
202                 amiga_libs[i].lib_base[0] = NULL;
203             }
204         }
205 #endif
206         if (!amiga_libs[i].lib_base[0]) {
207             if (amiga_libs[i].action == LIBS_ACTION_ERROR) {
208                 return -1;
209             } else {
210                 amiga_libs[i].var[0] = 0;
211             }
212         }
213         i++;
214     }
215     return 0;
216 }
217 
close_libs(void)218 void close_libs(void)
219 {
220     int i = 0;
221 
222     while (amiga_libs[i].lib_name) {
223 #ifdef AMIGA_OS4
224         if (amiga_libs[i].interface_base) {
225             DropInterface((struct Interface *)amiga_libs[i].interface_base[0]);
226         }
227 #endif
228         if (amiga_libs[i].lib_base) {
229             CloseLibrary(amiga_libs[i].lib_base[0]);
230         }
231         i++;
232     }
233 }
234 
235 #endif /* AMIGA_SUPPORT */
236 
237 #include "kbd.h"
238 #include "log.h"
239 
240 #ifndef SDL_REALINIT
241 #define SDL_REALINIT SDL_Init
242 #endif
243 
244 
245 /******************************************************************************/
246 static char *argv0 = NULL;
247 #if defined(ARCHDEP_OS_BEOS)
248 static char *orig_workdir = NULL;
249 #endif
250 #if defined(ARCHDEP_OS_AMIGA)
251 static int run_from_wb = 0;
252 #endif
253 
254 /* called from archdep.c:archdep_init */
archdep_init_extra(int * argc,char ** argv)255 static int archdep_init_extra(int *argc, char **argv)
256 {
257 #if defined(ARCHDEP_OS_WINDOWS)
258     _fmode = O_BINARY;
259 
260     _setmode(_fileno(stdin), O_BINARY);
261     _setmode(_fileno(stdout), O_BINARY);
262 #endif
263 
264 #if defined(ARCHDEP_OS_WINDOWS) || defined(ARCHDEP_OS_BEOS) || defined(ARCHDEP_OS_OS2)
265     argv0 = lib_strdup(argv[0]);
266 #endif
267 #if defined(ARCHDEP_OS_BEOS)
268     orig_workdir = getcwd(NULL, PATH_MAX);
269 #endif
270 
271 #if defined(ARCHDEP_OS_AMIGA)
272     if (*argc == 0) { /* run from WB */
273         run_from_wb = 1;
274     } else { /* run from CLI */
275         run_from_wb = 0;
276     }
277     load_libs();
278 #endif
279     return 0;
280 }
281 
282 /* called from archdep.c:archdep_shutdown */
archdep_shutdown_extra(void)283 static void archdep_shutdown_extra(void)
284 {
285     if (argv0) {
286         lib_free(argv0);
287     }
288 #if defined(ARCHDEP_OS_AMIGA)
289     lib_free(boot_path);
290     close_libs();
291 #endif
292 }
293 
294 /******************************************************************************/
295 
archdep_init(int * argc,char ** argv)296 int archdep_init(int *argc, char **argv)
297 {
298     archdep_program_path_set_argv0(argv[0]);
299 
300     archdep_create_user_cache_dir();
301     archdep_create_user_config_dir();
302 
303     if (SDL_REALINIT(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
304         fprintf(stderr, "SDL error: %s\n", SDL_GetError());
305         return 1;
306     }
307 
308     /*
309      * Call SDL_Quit() via atexit() to avoid segfaults on exit.
310      * See: https://wiki.libsdl.org/SDL_Quit
311      * I'm not sure this actually registers SDL_Quit() as the last atexit()
312      * call, but it appears to work at least (BW)
313      */
314     if (atexit(SDL_Quit) != 0) {
315         log_error(LOG_ERR,
316                 "failed to register SDL_Quit() with atexit().");
317         archdep_vice_exit(1);
318     }
319 
320     return archdep_init_extra(argc, argv);
321 }
322 
323 
archdep_shutdown(void)324 void archdep_shutdown(void)
325 {
326     /* free memory used by the exec path */
327     archdep_program_path_free();
328     /* free memory used by the exec name */
329     archdep_program_name_free();
330     /* free memory used by the boot path */
331     archdep_boot_path_free();
332     /* free memory used by the home path */
333     archdep_home_path_free();
334     /* free memory used by the cache files path */
335     archdep_user_cache_path_free();
336     /* free memory used by the config files path */
337     archdep_user_config_path_free();
338     /* free memory used by the sysfile pathlist */
339     archdep_default_sysfile_pathlist_free();
340 
341 #ifdef HAVE_NETWORK
342     archdep_network_shutdown();
343 #endif
344     archdep_shutdown_extra();
345     archdep_extra_title_text_free();
346 }
347