64-bit fix. Changed the user-supplied IDs used with
[AROS.git] / workbench / libs / workbench / uae_integration.c
blob96c4f56837b970a869f9fa8c11d03dc86d64815c
1 /*
2 Copyright 2010, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /**************************************************************************
7 * uae_integration.c
9 * This file contains helper functions to detect, if J-UAE is running and
10 * to detect, if an executeable is a m68k amigaOS file.
12 * It also gives wanderer.c a function, to forward a Message to
13 * the m68k amigaOS launchd via J-UAE, so that the correct
14 * executeable is started with all wanderer arguments.
16 **************************************************************************/
18 #include <proto/utility.h>
19 #include <utility/tagitem.h>
20 #include <workbench/workbench.h>
22 #ifdef __AROS__
23 //#define DEBUG 1
24 #include <aros/debug.h>
25 #endif
27 #include "workbench_intern.h"
28 #include "uae_integration.h"
30 /**************************************************************************
31 * get_j_uae_port
33 * return the port, Janus UAE uses to launch applications
34 **************************************************************************/
35 static struct MsgPort *get_j_uae_port(void)
37 return FindPort((STRPTR) J_UAE_PORT);
40 /**************************************************************************
41 * j_uae_running
43 * test if Janus UAE is running
44 **************************************************************************/
45 BOOL j_uae_running(void)
47 if( get_j_uae_port() != NULL)
49 D(bug("[WBLIB] %s: J-UAE port found\n", __PRETTY_FUNCTION__));
50 return TRUE;
53 D(bug("[WBLIB] %s: J-UAE port \"%s\" *not* found\n", __PRETTY_FUNCTION__, J_UAE_PORT));
54 return FALSE;
57 /**************************************************************************
58 * is_68k(filename with path)
60 * test, if the supplied file is an AmigaOS 68k binary
62 * (Amiga 68k executables start with 0x00 0x00 0x03 0xf3)
63 **************************************************************************/
64 BOOL is_68k(STRPTR filename, APTR WorkbenchBase)
66 BPTR fh;
67 UBYTE begin[4];
69 fh=Open(filename, MODE_OLDFILE);
71 if(fh)
73 if(Read(fh, begin, 4) == 4)
75 if( (begin[0] == 0x00) && (begin[1] == 0x00) && (begin[2] == 0x03) && (begin[3] == 0xf3) )
77 D(bug("[WBLIB] %s: Amiga 68k executable found: %s\n", __PRETTY_FUNCTION__, filename));
78 Close(fh);
79 return TRUE;
82 Close(fh);
84 return FALSE;
87 /**************************************************************************
88 * tag_list_nr(struct TagItem *tstate)
90 * count number of TagItems
91 **************************************************************************/
92 static ULONG tag_list_nr(const struct TagItem *tstate, APTR WorkbenchBase)
94 ULONG count;
96 count=0;
97 while(NextTagItem((struct TagItem **)&tstate))
99 count++;
102 D(bug("[WBLIB] %s: TagList has %d items.\n", __PRETTY_FUNCTION__, count));
103 return count;
106 /**************************************************************************
107 * str_dup_vec(char *in)
109 * Allocate a buffer for string in with AllocVecPooled
110 **************************************************************************/
111 static STRPTR str_dup_vec(void *mempool, char *in)
113 char *result;
115 if(!in)
117 return 0;
120 result=AllocVecPooled(mempool, strlen(in)+1);
121 strcpy(result, in);
123 return (STRPTR) result;
126 #define LOCK_NAME_BUFFER_SIZE 2000
128 /**************************************************************************
129 * tag_list_clone(struct TagItem *in)
131 * clone the TagList in and also clone all strings, in points to
132 **************************************************************************/
133 struct TagItem *tag_list_clone(void *mempool, const struct TagItem *in, APTR WorkbenchBase)
136 ULONG nr;
137 ULONG t;
138 struct TagItem *result;
139 char *name_from_lock;
140 struct TagItem *tag;
142 nr=tag_list_nr(in, WorkbenchBase);
144 if(!nr)
146 return NULL;
149 /* don't ask me, if all that stuff can work on 64-Bit AROS, sorry */
151 /* local only, no pool required */
152 name_from_lock=(char *) AllocVec(LOCK_NAME_BUFFER_SIZE, MEMF_CLEAR);
153 if(!name_from_lock)
155 D(bug("[WBLIB] %s: ERROR: AllocVec failed!\n", __PRETTY_FUNCTION__));
156 return NULL;
159 result=(struct TagItem *) AllocVecPooled(mempool, (nr+1) * sizeof(struct TagItem));
160 if(!result)
162 D(bug("[WBLIB] %s: ERROR: AllocVecPooled failed!\n", __PRETTY_FUNCTION__));
163 FreeVec(name_from_lock);
164 return NULL;
167 t=0;
168 while( (tag=NextTagItem((struct TagItem **)&in)) )
170 switch(tag->ti_Tag) {
172 case WBOPENA_ArgLock:
173 NameFromLock((BPTR) tag->ti_Data, name_from_lock, LOCK_NAME_BUFFER_SIZE);
174 UnLock((BPTR) tag->ti_Data); /* do we have to UnLock it? */
175 result[t].ti_Tag =WBOPENA_ArgLock;
176 result[t].ti_Data=(STACKIPTR) str_dup_vec(mempool, name_from_lock);
177 D(bug("[WBLIB] %s: WBOPENA_ArgLock: %s\n", __PRETTY_FUNCTION__, name_from_lock));
178 t++;
179 break;
181 case WBOPENA_ArgName:
182 result[t].ti_Tag =WBOPENA_ArgName;
183 result[t].ti_Data=(STACKIPTR) str_dup_vec(mempool, (char *) tag->ti_Data);
184 D(bug("[WBLIB] %s: WBOPENA_ArgName: %s\n", __PRETTY_FUNCTION__, tag->ti_Data));
185 t++;
186 break;
190 /* terminate. not necessary, but feels better. */
191 result[t].ti_Tag =TAG_DONE;
192 result[t].ti_Data=TAG_DONE;
194 FreeVec(name_from_lock);
196 return result;
199 /**************************************************************************
200 * forward_to_uae(struct TagItem *argsTagList, char *name)
202 * Sends a message to the J-UAE port in order to launch the
203 * according m68k amigaOS executeable.
205 * Errorhandling is completely up to J-UAE!
206 * J-UAE has to free all message resources!
207 **************************************************************************/
208 void forward_to_uae(struct TagItem *argsTagList, char *name, APTR WorkbenchBase)
211 struct MsgPort *port;
212 struct JUAE_Launch_Message *msg;
213 void *pool;
215 D(bug("[WBLIB] %s: j_uae_running and is_68k!\n", __PRETTY_FUNCTION__));
217 /* We allocate everything in a pool, so we can free it easily afterwards
218 * J-UAE has to free everything
219 * */
220 pool=CreatePool(MEMF_CLEAR|MEMF_PUBLIC, 2048, 1024);
221 if(pool)
223 msg=AllocVecPooled(pool, sizeof(struct JUAE_Launch_Message));
224 msg->mempool=pool;
225 if(msg)
227 msg->tags =tag_list_clone(pool, argsTagList, WorkbenchBase);
228 msg->ln_Name=str_dup_vec(pool, name);
230 /* now ensure, that the port stays alive */
231 Forbid();
232 port=get_j_uae_port();
233 if(port)
235 D(bug("[WBLIB] %s: PutMsg %lx to J-UAE\n", __PRETTY_FUNCTION__, msg));
236 PutMsg(port, (struct Message *) msg); /* one way */
238 else {
239 /* argl. someone closed the port inbetween !? */
240 D(bug("[WBLIB] %s: ERROR: someone closed the port inbetween !? \n", __PRETTY_FUNCTION__));
241 DeletePool(pool);
243 Permit();