-
Notifications
You must be signed in to change notification settings - Fork 0
/
amigaos.cpp
334 lines (271 loc) · 9.24 KB
/
amigaos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
** smarkusg: part of the code comes from mplayer 1.5 AOS
** License: GPL v2 or later
*/
#include <proto/dos.h>
#include <proto/icon.h>
#include <workbench/startup.h>
#include <stdio.h>
#include <string.h>
#include "amigaos.h"
#include <stdlib.h>
#include <exec/types.h>
#include <proto/timer.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/asl.h>
#include <proto/icon.h>
#include <proto/dos.h>
#include <proto/wb.h>
#include <proto/application.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/utility.h>
#include <proto/icon.h>
#include <proto/datatypes.h>
#include <libraries/application.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <proto/locale.h>
#include <classes/infowindow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdarg>
static const char *__attribute__((used)) stackcookie = "$STACK: 500000";
static const char *__attribute__((used)) version_tag = "$VER: " AMIGA_VERSION ;
struct Library *IconBase = NULL;
struct IconIFace *IIcon = NULL;
struct Library * IntuitionBase = NULL;
struct IntuitionIFace *IIntuition = NULL;
struct Library *AslBase = NULL;
struct AslIFace *IAsl = NULL;
static struct Task *current_task;
APTR window_mx;
APTR pr_WindowPtr;
char fullpath_dgen[1024] = "";
static int AmigaOS_argc = 0;
static char **AmigaOS_argv = NULL;
static void Free_Arg(void)
{
if (AmigaOS_argv)
{
ULONG i;
for (i=0; i < AmigaOS_argc; i++)
if(AmigaOS_argv[i]) free(AmigaOS_argv[i]);
free(AmigaOS_argv);
}
}
BOOL open_lib( const char *name, int ver , const char *iname, int iver, struct Library **base, struct Interface **interface)
{
*interface = NULL;
*base = OpenLibrary( name , ver);
if (*base)
{
*interface = GetInterface( *base, iname, iver, TAG_END );
if (!*interface) DebugPrintF("Unable to get %s IFace for '%s' %ld!\n",iname,name,ver);
}
else
{
DebugPrintF("Unable to open the '%s' %ld!\n",name,ver);
}
return (*interface) ? TRUE : FALSE;
}
#define GET_PATH(drawer,file,dest) \
dest = (char *) malloc( ( strlen(drawer) + strlen(file) + 2 ) * sizeof(char) ); \
if (dest) \
{ \
if ( strlen(drawer) == 0) strcpy(dest, file); \
else \
{ \
if ( (drawer[ strlen(drawer) - 1 ] == ':') ) sprintf(dest, "%s%s", drawer, file); \
else sprintf(dest, "%s/%s", drawer, file); \
} \
}
void AmigaOS_ParseArg(int argc, char *argv[], int *new_argc, char ***new_argv)
{
struct WBStartup *WBStartup = NULL;
struct DiskObject *icon = NULL;
*new_argc = argc;
*new_argv = argv;
WBStartup = (struct WBStartup *) argv;
if (!WBStartup)
{
return ; // We never know !
}
if ((WBStartup->sm_NumArgs > 1) && (!argc))
{
// The first arg is always the tool name (aka us)
// Then if more than one arg, with have some file name
ULONG i;
// We will replace the original argc/argv by us
AmigaOS_argc = WBStartup->sm_NumArgs;
AmigaOS_argv = (char **) malloc(AmigaOS_argc * sizeof(char *) );
if (!AmigaOS_argv) goto fail;
memset(AmigaOS_argv, 0x00, AmigaOS_argc * sizeof(char *) );
for(i=0; i<AmigaOS_argc; i++)
{
AmigaOS_argv[i] = (char *) malloc(MAX_DOS_NAME + MAX_DOS_PATH + 1);
if (!AmigaOS_argv[i])
{
goto fail;
}
NameFromLock(WBStartup->sm_ArgList[i].wa_Lock, AmigaOS_argv[i], MAX_DOS_PATH);
AddPart(AmigaOS_argv[i], WBStartup->sm_ArgList[i].wa_Name, MAX_DOS_PATH+MAX_DOS_NAME);
*new_argc = AmigaOS_argc;
*new_argv = AmigaOS_argv;
}
}
else
{
ULONG i;
struct FileRequester *AmigaOS_FileRequester = NULL;
BPTR FavoritePath_File;
char FavoritePath_Value[1024];
BOOL FavoritePath_Ok = FALSE;
char *EXTPATTERN = strdup("#?.(bin|md)");
FavoritePath_File = Open("PROGDIR:ROMS", MODE_OLDFILE);
if (FavoritePath_File)
{
LONG size = Read(FavoritePath_File, FavoritePath_Value, sizeof(FavoritePath_Value) );
if (size > 0)
{
if ( strchr(FavoritePath_Value, '\n') ) // There is an \n -> valid file
{
FavoritePath_Ok = TRUE;
*(strchr(FavoritePath_Value, '\n')) = '\0';
}
}
Close(FavoritePath_File);
}
AmigaOS_FileRequester = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, NULL);
if (!AmigaOS_FileRequester)
{
goto fail;
}
if ( ( AslRequestTags( AmigaOS_FileRequester,
ASLFR_TitleText, "Please select the rom to run",
ASLFR_DoMultiSelect, TRUE,
ASLFR_RejectIcons, TRUE,
ASLFR_DoPatterns, TRUE,
ASLFR_InitialPattern, (ULONG)EXTPATTERN,
ASLFR_InitialDrawer, (FavoritePath_Ok) ? FavoritePath_Value : "",
TAG_DONE) ) == FALSE )
{
FreeAslRequest(AmigaOS_FileRequester);
AmigaOS_FileRequester = NULL;
goto error_select;
}
if (!AmigaOS_FileRequester->fr_NumArgs) goto error_select;
AmigaOS_argc = AmigaOS_FileRequester->fr_NumArgs + 1;
AmigaOS_argv = (char **) malloc(AmigaOS_argc * sizeof(char *) );
if (!AmigaOS_argv) goto fail;
AmigaOS_argv[0] = strdup(FilePart(fullpath_dgen));
if (!AmigaOS_argv[0]) goto fail;
for(i=1; i<AmigaOS_argc; i++)
{
GET_PATH(AmigaOS_FileRequester->fr_Drawer,
AmigaOS_FileRequester->fr_ArgList[i-1].wa_Name,
AmigaOS_argv[i]);
if (!AmigaOS_argv[i])
{
FreeAslRequest(AmigaOS_FileRequester);
AmigaOS_FileRequester = NULL;
goto fail;
}
}
*new_argc = AmigaOS_argc;
*new_argv = AmigaOS_argv;
FreeAslRequest(AmigaOS_FileRequester);
}
return;
fail:
*new_argc = argc;
*new_argv = argv;
return;
error_select:
/* nothing selected */
*new_argc = 0;
*new_argv = NULL;
}
int AmigaOS_Open(int argc, char *argv[])
{
AmigaOS_argv = NULL;
if ( ! open_lib( "dos.library", 0L, "main", 1, &DOSBase, (struct Interface **) &IDOS ) ) return -1;
if ( ! open_lib( "utility.library", 51L, "main", 1, &UtilityBase, (struct Interface **) &IUtility ) ) return -1;
if ( ! open_lib( "intuition.library", 51L, "main", 1, &IntuitionBase, (struct Interface **) &IIntuition ) ) return -1;
if ( ! open_lib( "asl.library", 0L, "main", 1, &AslBase, (struct Interface **) &IAsl ) ) return -1;
if ( ! open_lib( "icon.library", 51L, "main", 1, &IconBase, (struct Interface **) &IIcon ) ) return -1;
Forbid();
current_task = FindTask(NULL);
Permit();
return 0;
}
void AmigaOS_Close(void)
{
Forbid();
current_task = FindTask(NULL);
Permit();
((struct Process *) current_task) -> pr_WindowPtr = pr_WindowPtr; // enable insert disk.
Free_Arg();
if (IntuitionBase) CloseLibrary(IntuitionBase); IntuitionBase = 0;
if (IIntuition) DropInterface((struct Interface*) IIntuition); IIntuition = 0;
if (IconBase) CloseLibrary(IconBase); IconBase = 0;
if (IIcon) DropInterface((struct Interface*)IIcon); IIcon = 0;
if (AslBase) CloseLibrary(AslBase); AslBase = 0;
if (IAsl) DropInterface((struct Interface*)IAsl); IAsl = 0;
if (DOSBase) CloseLibrary(DOSBase); DOSBase = 0;
if (IDOS) DropInterface((struct Interface*)IDOS); IDOS = 0;
}
struct Screen *FrontMostScr(void)
{
struct Screen *front_screen_address, *public_screen_address = NULL;
ULONG intuition_lock;
struct List *public_screen_list;
struct PubScreenNode *public_screen_node;
intuition_lock = LockIBase(0L);
front_screen_address = ((struct IntuitionBase *)IntuitionBase)->FirstScreen;
if( (front_screen_address->Flags & PUBLICSCREEN) || (front_screen_address->Flags & WBENCHSCREEN) )
{
UnlockIBase(intuition_lock);
public_screen_list = LockPubScreenList();
public_screen_node = (struct PubScreenNode*)public_screen_list->lh_Head;
while(public_screen_node)
{
if(public_screen_node->psn_Screen == front_screen_address)
{
public_screen_address = public_screen_node->psn_Screen;
break;
}
public_screen_node = (struct PubScreenNode*)public_screen_node->psn_Node.ln_Succ;
}
UnlockPubScreenList();
}
else
{
UnlockIBase(intuition_lock);
}
if(!public_screen_address)
{
public_screen_address = LockPubScreen(NULL);
UnlockPubScreen(NULL, public_screen_address);
}
return(public_screen_address);
}
void VARARGS68K EasyRequester(CONST_STRPTR text, CONST_STRPTR button, ...)
{
struct EasyStruct easyreq = { 0 };
va_list parameters;
STRPTR vargs = NULL;
va_startlinear(parameters, button);
vargs = va_getlinearva(parameters, STRPTR);
easyreq.es_StructSize = sizeof(struct EasyStruct);
easyreq.es_Flags = ESF_SCREEN | ESF_EVENSIZE;// | ESF_TAGGED;
easyreq.es_Title = AMIGA_VERSION ;
easyreq.es_TextFormat = text;
easyreq.es_GadgetFormat = button;
easyreq.es_Screen = FrontMostScr();
EasyRequestArgs(NULL, &easyreq, NULL, vargs);
va_end(parameters);
}