revert between 56095 -> 55830 in arch
[AROS.git] / rom / usb / trident / Trident.c
blobccfb6ebee3f783c779f95b3b4a49822bfe1db398
1 /*
2 ** Trident V0.0 (08-Mar-02) by Chris Hodges <hodges@in.tum.de>
3 **
4 ** Project started: 08-Mar-02
5 ** Releases :
6 */
8 #include "debug.h"
10 #define USE_INLINE_STDARG
11 #define __NOLIBBASE__
12 #include <proto/muimaster.h>
13 #include <proto/exec.h>
14 #include <proto/dos.h>
15 #include <proto/poseidon.h>
16 #include <proto/intuition.h>
17 #include <proto/graphics.h>
18 #include <proto/usbclass.h>
19 #include <proto/icon.h>
20 #include <proto/utility.h>
22 #include <workbench/startup.h>
24 #include "Trident.h"
25 #include "ActionClass.h"
26 #include "IconListClass.h"
27 #include "DevWinClass.h"
28 #include "CfgListClass.h"
29 #include "locale.h"
31 #define TMPL_SWITCH 1
32 #define TMPL_NUMERIC 2
33 #define TMPL_ARGUMENT 4
36 #include <stdlib.h>
39 /* defines */
41 struct WBStartup *_WBenchMsg;
43 extern struct ExecBase *SysBase;
44 extern struct DosLibrary *DOSBase;
45 extern struct IntuitionBase *IntuitionBase;
46 extern struct Library *UtilityBase;
47 extern struct Library *MUIMasterBase;
48 struct Library *ps;
49 extern struct Library *IconBase;
51 struct MUI_CustomClass *ActionClass = NULL;
52 struct MUI_CustomClass *IconListClass = NULL;
53 struct MUI_CustomClass *DevWinClass = NULL;
54 struct MUI_CustomClass *CfgListClass = NULL;
56 Object *appobj = NULL;
57 Object *mainwinobj = NULL;
58 Object *actionobj = NULL;
59 BOOL registermode = FALSE;
61 static Object *mi_about;
62 static Object *mi_aboutmui;
63 static Object *mi_help;
64 static Object *mi_online;
65 static Object *mi_offline;
66 static Object *mi_iconify;
67 static Object *mi_quit;
68 static Object *mi_saveerrors;
69 static Object *mi_flusherrors;
70 static Object *mi_savedevlist;
71 static Object *mi_loadprefs;
72 static Object *mi_saveprefs;
73 static Object *mi_saveprefsas;
74 static Object *mi_muiprefs;
76 #define ARGS_REGMODE 0
77 #define ARGS_NOGUI 1
78 #define ARGS_SAVE 2
79 #define ARGS_SIZEOF 3
81 static char *template = "REGISTERMODE/S,NOGUI/S,SAVE/S";
82 static IPTR ArgsArray[ARGS_SIZEOF];
83 static struct RDArgs *ArgsHook = NULL;
84 static struct DiskObject *IconObject = NULL;
85 static LONG NumericArgsBuffer[32];
87 /* /// "GetToolTypes()" */
88 BOOL GetToolTypes(void)
90 BPTR dir;
91 struct WBArg *wba;
92 char currtemp[512];
93 char currkey[128];
94 LONG currtype;
95 char *templatepos = template;
96 char *bufpos;
97 char *bufpos2;
98 BOOL valid = FALSE;
99 IPTR *argsptr = ArgsArray;
100 LONG *numargsptr = NumericArgsBuffer;
102 if(!_WBenchMsg) return(0);
103 wba = _WBenchMsg->sm_ArgList;
105 dir = CurrentDir(wba->wa_Lock);
106 if((IconObject = GetDiskObject(wba->wa_Name)))
108 valid = TRUE;
109 if(IconObject->do_ToolTypes)
111 while(*templatepos)
113 bufpos = currtemp;
114 while(*templatepos)
116 if(*templatepos == ',')
118 templatepos++;
119 break;
121 *bufpos++ = *templatepos++;
123 *bufpos = 0;
124 bufpos = currtemp;
125 currtype = 0;
126 while(*bufpos)
128 if(*bufpos++ == '/')
130 switch(*bufpos++)
132 case 'S':
133 currtype |= TMPL_SWITCH;
134 break;
135 case 'A':
136 currtype |= TMPL_ARGUMENT;
137 break;
138 case 'N':
139 currtype |= TMPL_NUMERIC;
140 break;
144 bufpos2 = currtemp;
145 *argsptr = 0L;
146 while(*bufpos2)
148 bufpos = currkey;
149 while(*bufpos2)
151 if(*bufpos2 == '/')
153 bufpos2 = currtemp + strlen(currtemp);
154 break;
156 if(*bufpos2 == '=')
158 bufpos2++;
159 break;
161 *bufpos++ = *bufpos2++;
163 *bufpos = 0;
164 if((bufpos = FindToolType(IconObject->do_ToolTypes, currkey)))
166 if(currtype & TMPL_SWITCH)
168 *argsptr = -1;
169 } else {
170 if(currtype & TMPL_NUMERIC)
172 *numargsptr = atoi(bufpos);
173 *argsptr = (IPTR) numargsptr++;
174 } else {
175 *argsptr = (IPTR) bufpos;
180 if((currtype & TMPL_ARGUMENT) && (*argsptr == 0L))
182 valid = FALSE;
183 break;
185 argsptr++;
189 CurrentDir(dir);
190 return(valid);
192 /* \\\ */
194 /* /// "CleanupClasses()" */
195 void CleanupClasses(void)
197 if(DevWinClass)
199 MUI_DeleteCustomClass(DevWinClass);
200 DevWinClass = NULL;
202 if(ActionClass)
204 MUI_DeleteCustomClass(ActionClass);
205 ActionClass = NULL;
207 if(IconListClass)
209 MUI_DeleteCustomClass(IconListClass);
210 IconListClass = NULL;
212 if(CfgListClass)
214 MUI_DeleteCustomClass(CfgListClass);
215 CfgListClass = NULL;
218 /* \\\ */
220 /* /// "SetupClasses()" */
221 BOOL SetupClasses(void)
223 DevWinClass = MUI_CreateCustomClass(NULL, MUIC_Window, NULL, sizeof(struct DevWinData) , DevWinDispatcher);
224 ActionClass = MUI_CreateCustomClass(NULL, MUIC_Group , NULL, sizeof(struct ActionData) , ActionDispatcher);
225 IconListClass = MUI_CreateCustomClass(NULL, MUIC_List , NULL, sizeof(struct IconListData) , IconListDispatcher);
226 CfgListClass = MUI_CreateCustomClass(NULL, MUIC_List , NULL, sizeof(struct CfgListData) , CfgListDispatcher);
228 if(ActionClass && IconListClass && DevWinClass && CfgListClass)
229 return(TRUE);
231 return(FALSE);
233 /* \\\ */
235 /* /// "fail()" */
236 void fail(char *str)
238 if(appobj)
240 MUI_DisposeObject(appobj);
241 appobj = NULL;
243 CleanupClasses();
245 if(ArgsHook)
247 FreeArgs(ArgsHook);
248 ArgsHook = NULL;
251 if(ps)
253 CloseLibrary(ps);
254 ps = NULL;
257 if(IconObject)
259 FreeDiskObject(IconObject);
260 IconObject = NULL;
263 if(_WBenchMsg && str)
265 struct EasyStruct errorReq = { sizeof(struct EasyStruct), 0, "Trident", NULL, "Cancel" };
266 errorReq.es_TextFormat = str;
267 EasyRequest(NULL, &errorReq, NULL, NULL);
270 if(MUIMasterBase)
272 CloseLibrary(MUIMasterBase);
273 MUIMasterBase = NULL;
276 if(str)
278 PutStr(str);
279 exit(20);
281 exit(0);
283 /* \\\ */
285 /* /// "main()" */
286 int main(int argc, char *argv[])
288 if(!argc)
290 _WBenchMsg = (struct WBStartup *) argv;
291 if(!GetToolTypes())
293 fail("Wrong or missing ToolTypes!");
295 } else {
296 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
298 fail("Wrong arguments!\n");
302 Locale_Initialize();
304 if(!(ps = OpenLibrary("poseidon.library", 4)))
306 fail("Failed to open version 4 of poseidon.library.\n");
309 if(ArgsArray[ARGS_NOGUI])
311 if(ArgsArray[ARGS_SAVE])
313 InternalCreateConfig();
314 psdSaveCfgToDisk(NULL, FALSE);
315 } else {
316 IPTR cfgread = FALSE;
317 psdGetAttrs(PGA_STACK, NULL, PA_ConfigRead, &cfgread, TAG_DONE);
318 if(!cfgread)
320 psdLoadCfgFromDisk(NULL);
321 } else {
322 psdParseCfg();
325 fail(NULL);
328 if((ps->lib_Version == 4) && (ps->lib_Revision < 3))
330 fail("Sorry, this version of Trident requires at least version 4.3 of poseidon.library!\n");
334 struct Task *thistask;
335 IPTR stackfree;
336 thistask = FindTask(NULL);
337 stackfree = ((IPTR) thistask->tc_SPUpper) - ((IPTR) thistask->tc_SPLower);
338 if(stackfree < 16000)
340 fail("Trident needs at least 16KB of stack! Please change the tooltypes accordingly!\n");
345 IPTR cfgread = FALSE;
346 psdGetAttrs(PGA_STACK, NULL, PA_ConfigRead, &cfgread, TAG_DONE);
347 if(!cfgread)
349 psdLoadCfgFromDisk(NULL);
350 } else {
351 psdParseCfg();
354 registermode = ArgsArray[ARGS_REGMODE];
356 if(!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
357 fail("Failed to open "MUIMASTER_NAME". This application requires MUI!\n");
359 if(!SetupClasses())
360 fail("Could not create custom classes.\n");
362 appobj = ApplicationObject,
363 MUIA_Application_Title , __(MSG_APP_TITLE),
364 MUIA_Application_Version , __(MSG_APP_VERSION),
365 MUIA_Application_Copyright , (IPTR) "©2002-2009 Chris Hodges",
366 MUIA_Application_Author , (IPTR) "Chris Hodges <chrisly@platon42.de>",
367 MUIA_Application_Description, __(MSG_APP_DESC),
368 MUIA_Application_Base , "TRIDENT",
369 MUIA_Application_SingleTask , TRUE,
370 MUIA_Application_DiskObject , IconObject,
371 MUIA_Application_HelpFile , "HELP:Poseidon.guide",
372 MUIA_Application_Menustrip , MenustripObject,
373 Child, MenuObjectT(_(MSG_MENU_PROJECT)),
374 Child, mi_about = MenuitemObject,
375 MUIA_Menuitem_Title, __(MSG_MENU_ABOUT),
376 MUIA_Menuitem_Shortcut, "?",
377 End,
378 Child, mi_aboutmui = MenuitemObject,
379 MUIA_Menuitem_Title, __(MSG_MENU_ABOUTMUI),
380 End,
381 Child, mi_help = MenuitemObject,
382 MUIA_Menuitem_Title, __(MSG_MENU_HELP),
383 End,
384 Child, MenuitemObject,
385 MUIA_Menuitem_Title, NM_BARLABEL,
386 End,
387 Child, mi_online = MenuitemObject,
388 MUIA_Menuitem_Title, __(MSG_MENU_ONLINE),
389 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_ONLINE),
390 End,
391 Child, mi_offline = MenuitemObject,
392 MUIA_Menuitem_Title, __(MSG_MENU_OFFLINE),
393 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_OFFLINE),
394 End,
395 Child, MenuitemObject,
396 MUIA_Menuitem_Title, NM_BARLABEL,
397 End,
398 Child, mi_iconify = MenuitemObject,
399 MUIA_Menuitem_Title, __(MSG_MENU_ICONIFY),
400 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_ICONIFY),
401 End,
402 Child, MenuitemObject,
403 MUIA_Menuitem_Title, NM_BARLABEL,
404 End,
405 Child, mi_quit = MenuitemObject,
406 MUIA_Menuitem_Title, __(MSG_MENU_QUIT),
407 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_QUIT),
408 End,
409 End,
410 Child, MenuObjectT(_(MSG_MENU_INFO)),
411 Child, mi_saveerrors = MenuitemObject,
412 MUIA_Menuitem_Title, __(MSG_MENU_SAVELOG),
413 End,
414 Child, mi_flusherrors = MenuitemObject,
415 MUIA_Menuitem_Title, __(MSG_MENU_FLUSH),
416 End,
417 Child, MenuitemObject,
418 MUIA_Menuitem_Title, NM_BARLABEL,
419 End,
420 Child, mi_savedevlist = MenuitemObject,
421 MUIA_Menuitem_Title, __(MSG_MENU_SAVEDEV),
422 End,
423 End,
424 Child, MenuObjectT(_(MSG_MENU_SETTINGS)),
425 Child, mi_loadprefs = MenuitemObject,
426 MUIA_Menuitem_Title, __(MSG_MENU_LOAD),
427 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_LOAD),
428 End,
429 Child, mi_saveprefs = MenuitemObject,
430 MUIA_Menuitem_Title, __(MSG_MENU_SAVE),
431 MUIA_Menuitem_Shortcut, _(MSG_MENU_SC_SAVE),
432 End,
433 Child, mi_saveprefsas = MenuitemObject,
434 MUIA_Menuitem_Title, __(MSG_MENU_SAVEAS),
435 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_SAVEAS),
436 End,
437 Child, MenuitemObject,
438 MUIA_Menuitem_Title, NM_BARLABEL,
439 End,
440 Child, mi_muiprefs = MenuitemObject,
441 MUIA_Menuitem_Title, __(MSG_MENU_MUISETTINGS),
442 MUIA_Menuitem_Shortcut, __(MSG_MENU_SC_MUISETTINGS),
443 End,
444 End,
445 End,
447 SubWindow, mainwinobj = WindowObject,
448 MUIA_Window_ID , MAKE_ID('M','A','I','N'),
449 MUIA_Window_Title, __(MSG_WINDOW_TITLE),
450 MUIA_HelpNode, "usingtrident",
452 WindowContents, actionobj = NewObject(ActionClass->mcc_Class, 0, TAG_END),
453 End,
454 End;
456 if(!appobj) fail("Failed to create Application. Already running?\n");
459 ** Add notifications
461 DoMethod(mi_aboutmui, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
462 appobj, 2, MUIM_Application_AboutMUI, mainwinobj);
463 DoMethod(mi_about, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
464 actionobj, 1, MUIM_Action_About);
465 DoMethod(mi_online, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
466 actionobj, 1, MUIM_Action_Online);
467 DoMethod(mi_offline, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
468 actionobj, 1, MUIM_Action_Offline);
469 DoMethod(mi_help, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
470 appobj, 5, MUIM_Application_ShowHelp, mainwinobj, NULL, "main", 0);
471 DoMethod(mi_iconify, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
472 appobj, 3, MUIM_Set, MUIA_Application_Iconified, TRUE);
473 DoMethod(mi_quit, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
474 appobj, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
476 DoMethod(mi_saveerrors, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
477 actionobj, 1, MUIM_Action_SaveErrors);
478 DoMethod(mi_flusherrors, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
479 actionobj, 1, MUIM_Action_FlushErrors);
480 DoMethod(mi_savedevlist, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
481 actionobj, 1, MUIM_Action_SaveDeviceList);
483 DoMethod(mi_loadprefs, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
484 actionobj, 1, MUIM_Action_LoadPrefs);
485 DoMethod(mi_saveprefs, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
486 actionobj, 1, MUIM_Action_SavePrefs);
487 DoMethod(mi_saveprefsas, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
488 actionobj, 1, MUIM_Action_SavePrefsAs);
489 DoMethod(mi_muiprefs, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
490 appobj, 2, MUIM_Application_OpenConfigWindow, 0);
492 DoMethod(mainwinobj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
493 actionobj, 1, MUIM_Action_UseQuit);
494 DoMethod(appobj, MUIM_Notify, MUIA_Application_DoubleStart, MUIV_EveryTime,
495 actionobj, 1, MUIM_Action_WakeUp);
497 DoMethod(appobj, MUIM_Application_Load, MUIV_Application_Load_ENVARC);
500 ** This is the ideal input loop for an object oriented MUI application.
501 ** Everything is encapsulated in classes, no return ids need to be used,
502 ** we just check if the program shall terminate.
503 ** Note that MUIM_Application_NewInput expects sigs to contain the result
504 ** from Wait() (or 0). This makes the input loop significantly faster.
508 ULONG sigs = 0;
509 ULONG sigmask;
510 LONG isopen = 0;
511 LONG iconify = 0;
513 get(appobj, MUIA_Application_Iconified, &iconify);
514 set(mainwinobj, MUIA_Window_Open, TRUE);
515 get(mainwinobj, MUIA_Window_Open, &isopen);
516 if(!(isopen || iconify))
517 fail("Couldn't open window! Maybe screen is too small. Try a higher resolution!\n");
519 sigmask = 0;// FIXME 1L<<eventmsgport->mp_SigBit;
520 while(DoMethod(appobj, MUIM_Application_NewInput, &sigs) != MUIV_Application_ReturnID_Quit)
522 if(sigs)
524 sigs = Wait(sigs | sigmask | SIGBREAKF_CTRL_C);
525 if(sigs & SIGBREAKF_CTRL_C)
526 break;
529 set(mainwinobj, MUIA_Window_Open, FALSE);
531 DoMethod(appobj, MUIM_Application_Save, MUIV_Application_Save_ENVARC);
534 ** Shut down...
536 Locale_Deinitialize();
538 fail(NULL);
539 return(0); // never gets here, just to shut the compiler up
541 /* \\\ */