Trust uboot's device list only if it does not look suspicious.
[AROS.git] / workbench / tools / commodities / Exchange.c
blobe2a0b56e340dcdbe8c687a4a27a85527497edc00
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Exchange -- controls commodities.
6 */
8 /******************************************************************************
10 NAME
12 Exchange
14 SYNOPSIS
16 CX_PRIORITY/N/K,CX_POPKEY/K,CX_POPUP/S
18 LOCATION
20 SYS:Tools/Commodities
22 FUNCTION
24 Manages the commodities in the system
26 INPUTS
28 CX_PRIORITY -- Priority of the Exchange broker
29 CX_POPKEY -- Hotkey combination for Exchange
30 CX_POPUP -- Appear at startup
32 RESULT
34 NOTES
36 EXAMPLE
38 BUGS
40 SEE ALSO
42 INTERNALS
44 ******************************************************************************/
46 #define AROS_ALMOST_COMPATIBLE
47 #define MUIMASTER_YES_INLINE_STDARG
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
53 #ifndef __AROS__
54 #include <cxintern.h>
55 #endif
57 //#define DEBUG 1
58 #include <aros/debug.h>
59 #include <aros/asmcall.h>
60 #include <aros/symbolsets.h>
61 #include <libraries/iffparse.h>
62 #include <libraries/mui.h>
63 #include <workbench/startup.h>
64 #include <proto/muimaster.h>
65 #include <proto/locale.h>
66 #include <proto/intuition.h>
67 #include <proto/exec.h>
68 #include <proto/gadtools.h>
69 #include <proto/dos.h>
70 #include <proto/alib.h>
71 #include <proto/commodities.h>
72 #include <proto/utility.h>
73 #include <proto/icon.h>
75 #define CATCOMP_ARRAY
76 #include "strings.h"
78 #ifdef __AROS__
79 #define CATALOG_NAME "System/Tools/Commodities.catalog"
80 #else
81 #define CATALOG_NAME "System/Tools/Exchange.catalog"
82 #endif
83 #define CATALOG_VERSION 3
85 TEXT version[] = "$VER: Exchange 1.2 (14.01.2012)";
87 #define ARG_TEMPLATE "CX_PRIORITY/N/K,CX_POPKEY/K,CX_POPUP/S"
88 #define DEF_POPKEY "ctrl alt h"
90 enum {
91 ARG_CXPRI,
92 ARG_CXPOPKEY,
93 ARG_CXPOPUP,
94 NUM_ARGS
97 static Object *app, *wnd, *listgad, *textgad1, *textgad2, *showgad, *hidegad, *cyclegad, *killgad;
98 static CONST_STRPTR cyclestrings[3];
99 static struct Catalog *catalog;
100 static UBYTE s[257];
101 static struct List brokerList;
103 static struct Hook broker_hook;
104 static struct Hook list_disp_hook;
105 static struct Hook list_select_hook;
106 static struct Hook inform_broker_hook;
107 static struct Hook show_hook;
109 static LONG cx_pri;
110 static char *cx_popkey;
111 static BOOL cx_popup = FALSE;
112 static CxObj *broker;
113 static struct MsgPort *brokermp;
114 static struct Task *maintask;
116 static void Cleanup(CONST_STRPTR txt);
117 static void GetArguments(int argc, char **argv);
118 static void HandleAll(void);
119 static void InitMenus(void);
120 static VOID Locale_Deinitialize(VOID);
121 static int Locale_Initialize(VOID);
122 static void MakeGUI(void);
123 static void showSimpleMessage(CONST_STRPTR msgString);
124 static void update_list(void);
125 static CONST_STRPTR _(ULONG id);
126 static struct DiskObject *disko;
128 /*********************************************************************************************/
130 static CONST_STRPTR _(ULONG id)
132 if (LocaleBase != NULL && catalog != NULL)
134 return GetCatalogStr(catalog, id, CatCompArray[id].cca_Str);
136 else
138 return CatCompArray[id].cca_Str;
142 #define __(id) ((IPTR) _(id)) /* Get a message, as an IPTR */
144 /*********************************************************************************************/
146 static int Locale_Initialize(VOID)
148 if (LocaleBase != NULL)
150 catalog = OpenCatalog
152 NULL, CATALOG_NAME, OC_Version, CATALOG_VERSION, TAG_DONE
155 else
157 catalog = NULL;
159 /* Note that in AROS constructors should return value opposite to the standard one.
160 Probably it's a misdesign, but we can do nothing with it. */
161 #ifdef __AROS__
162 return -1;
163 #else
164 return 0;
165 #endif
168 /*********************************************************************************************/
170 static VOID Locale_Deinitialize(VOID)
172 if(LocaleBase != NULL && catalog != NULL) CloseCatalog(catalog);
175 /*********************************************************************************************/
177 static void GetArguments(int argc, char **argv)
179 static struct RDArgs *myargs;
180 static IPTR args[NUM_ARGS];
181 static UBYTE **wbargs;
182 static struct WBStartup *argmsg;
183 static struct WBArg *wb_arg;
184 static STRPTR cxname;
185 if (argc)
187 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
189 Fault(IoErr(), 0, s, 256);
190 Cleanup(s);
192 if (args[ARG_CXPRI]) cx_pri = *(LONG*)args[ARG_CXPRI];
193 if (args[ARG_CXPOPKEY])
195 cx_popkey = StrDup((char *)args[ARG_CXPOPKEY]);
197 else
199 cx_popkey = StrDup(DEF_POPKEY);
201 if (args[ARG_CXPOPUP]) cx_popup = TRUE;
202 FreeArgs(myargs);
203 cxname = argv[0];
205 else
207 argmsg = (struct WBStartup *)argv;
208 wb_arg = argmsg->sm_ArgList;
209 cxname = wb_arg->wa_Name;
210 wbargs = ArgArrayInit(argc, (UBYTE**)argv);
211 cx_pri = ArgInt(wbargs, "CX_PRIORITY", 0);
212 cx_popkey = StrDup(ArgString(wbargs, "CX_POPKEY", DEF_POPKEY));
213 if (strnicmp(ArgString(wbargs, "CX_POPUP", "NO"), "Y", 1) == 0)
215 cx_popup = TRUE;
217 ArgArrayDone();
219 D(bug("Exchange Arguments pri %d popkey %s popup %d\n", cx_pri, cx_popkey, cx_popup));
220 disko = GetDiskObject(cxname);
223 /*********************************************************************************************/
225 static struct NewMenu nm[] =
227 {NM_TITLE, (STRPTR)MSG_MEN_PROJECT },
228 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_HIDE },
229 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_ICONIFY },
230 {NM_ITEM, NM_BARLABEL },
231 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_QUIT },
232 {NM_END }
235 /*********************************************************************************************/
237 static void InitMenus(void)
239 struct NewMenu *actnm = nm;
241 for(actnm = nm; actnm->nm_Type != NM_END; actnm++)
243 if (actnm->nm_Label != NM_BARLABEL)
245 ULONG id = (IPTR)actnm->nm_Label;
246 CONST_STRPTR str = _(id);
248 if (actnm->nm_Type == NM_TITLE)
250 actnm->nm_Label = str;
251 } else {
252 actnm->nm_Label = str + 2;
253 if (str[0] != ' ') actnm->nm_CommKey = str;
255 actnm->nm_UserData = (APTR)(IPTR)id;
257 } /* if (actnm->nm_Label != NM_BARLABEL) */
259 } /* for(actnm = nm; nm->nm_Type != NM_END; nm++) */
262 /*********************************************************************************************/
264 static void showSimpleMessage(CONST_STRPTR msgString)
266 struct EasyStruct easyStruct;
268 easyStruct.es_StructSize = sizeof(easyStruct);
269 easyStruct.es_Flags = 0;
270 easyStruct.es_Title = _(MSG_EXCHANGE_CXNAME);
271 easyStruct.es_TextFormat = msgString;
272 easyStruct.es_GadgetFormat = _(MSG_OK);
274 if (IntuitionBase != NULL && !Cli() )
276 EasyRequestArgs(NULL, &easyStruct, NULL, NULL);
278 else
280 PutStr(msgString);
284 /*********************************************************************************************/
286 static void update_list(void)
288 struct BrokerCopy *node;
289 CopyBrokerList(&brokerList);
291 nnset(textgad1, MUIA_Text_Contents, NULL);
292 nnset(textgad2, MUIA_Text_Contents, NULL);
294 set(listgad, MUIA_List_Quiet, TRUE);
295 DoMethod(listgad, MUIM_List_Clear);
296 ForeachNode(&brokerList, node)
298 D(bug("Exchange: Brokernode %s\n", node->bc_Name));
299 DoMethod(listgad, MUIM_List_InsertSingle, node, MUIV_List_Insert_Bottom);
301 set(listgad, MUIA_List_Quiet, FALSE);
304 /*********************************************************************************************/
306 AROS_UFH3(void, broker_func,
307 AROS_UFHA(struct Hook *, h, A0),
308 AROS_UFHA(Object * , object, A2),
309 AROS_UFHA(CxMsg * , msg, A1))
311 AROS_USERFUNC_INIT
313 D(bug("Exchange: Broker hook called\n"));
314 if (CxMsgType(msg) == CXM_COMMAND)
316 if (CxMsgID(msg) == CXCMD_APPEAR)
318 CallHookPkt(&show_hook, NULL, NULL);
320 else if (CxMsgID(msg) == CXCMD_DISAPPEAR)
322 set(wnd, MUIA_Window_Open, FALSE);
324 else if (CxMsgID(msg) == CXCMD_LIST_CHG)
326 update_list();
329 AROS_USERFUNC_EXIT
332 /*** show_func ************************************************************/
333 AROS_UFH3(
334 void, show_func,
335 AROS_UFHA(struct Hook *, hook, A0),
336 AROS_UFHA(APTR *, obj, A2),
337 AROS_UFHA(APTR, param, A1)
340 AROS_USERFUNC_INIT
342 if (XGET(app, MUIA_Application_Iconified) == TRUE)
343 set(app, MUIA_Application_Iconified, FALSE);
344 else
345 set(wnd, MUIA_Window_Open, TRUE);
347 AROS_USERFUNC_EXIT
350 /*********************************************************************************************/
352 AROS_UFH3(void, list_display_func,
353 AROS_UFHA(struct Hook * , h, A0),
354 AROS_UFHA(char ** , array, A2),
355 AROS_UFHA(struct BrokerCopy *, node, A1))
357 AROS_USERFUNC_INIT
359 *array = node->bc_Name;
361 AROS_USERFUNC_EXIT
364 /*********************************************************************************************/
366 AROS_UFH3(void, list_select_func,
367 AROS_UFHA(struct Hook *, h, A0),
368 AROS_UFHA(Object * , object, A2),
369 AROS_UFHA(APTR , msg, A1))
371 AROS_USERFUNC_INIT
373 struct BrokerCopy *bc;
374 DoMethod(listgad, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, (IPTR)&bc);
375 if (bc)
377 BOOL showHide = (bc->bc_Flags & COF_SHOW_HIDE) == 0;
378 BOOL active = (bc->bc_Flags & COF_ACTIVE) != 0;
379 nnset(textgad1, MUIA_Text_Contents, bc->bc_Title);
380 nnset(textgad2, MUIA_Text_Contents, bc->bc_Descr);
381 nnset(hidegad, MUIA_Disabled, showHide);
382 nnset(showgad, MUIA_Disabled, showHide);
383 nnset(cyclegad, MUIA_Cycle_Active, active ? 0 : 1);
386 AROS_USERFUNC_EXIT
389 /*********************************************************************************************/
391 AROS_UFH3(void, inform_broker_func,
392 AROS_UFHA(struct Hook *, h, A0),
393 AROS_UFHA(Object* , object, A2),
394 AROS_UFHA(LONG * , command, A1))
396 AROS_USERFUNC_INIT
398 struct BrokerCopy *bc;
399 DoMethod(listgad, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, (IPTR) &bc);
400 if (bc)
402 D(bug("Exchange: Broker inform %s\n", bc->bc_Node.ln_Name));
403 BrokerCommand(bc->bc_Node.ln_Name, *command);
406 AROS_USERFUNC_EXIT
409 /*********************************************************************************************/
411 static void MakeGUI(void)
413 Object *menu;
414 static TEXT wintitle[100];
415 CxObj *popfilter;
417 cyclestrings[0] = _(MSG_EXCHANGE_CYCLE_ACTIVE);
418 cyclestrings[1] = _(MSG_EXCHANGE_CYCLE_INACTIVE);
419 menu = MUI_MakeObject(MUIO_MenustripNM, &nm, 0);
421 broker_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(broker_func);
422 list_disp_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(list_display_func);
423 list_select_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(list_select_func);
424 inform_broker_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(inform_broker_func);
425 show_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(show_func);
427 snprintf(wintitle, sizeof(wintitle), _(MSG_EXCHANGE_WINTITLE), cx_popkey);
429 app = (Object *)ApplicationObject,
430 MUIA_Application_Title, __(MSG_EXCHANGE_CXNAME),
431 MUIA_Application_Version, (IPTR)version,
432 MUIA_Application_Copyright, (IPTR)"Copyright © 1995-2012, The AROS Development TEAM",
433 MUIA_Application_Author, (IPTR)"The AROS Development Team",
434 MUIA_Application_Description, __(MSG_EXCHANGE_CXDESCR),
435 MUIA_Application_BrokerPri, cx_pri,
436 MUIA_Application_BrokerHook, (IPTR)&broker_hook,
437 MUIA_Application_Base, (IPTR)"EXCHANGE",
438 MUIA_Application_SingleTask, TRUE,
439 MUIA_Application_Menustrip, (IPTR)menu,
440 MUIA_Application_DiskObject, (IPTR)disko,
441 SubWindow, (IPTR)(wnd = (Object *)WindowObject,
442 MUIA_Window_Title, (IPTR)wintitle,
443 MUIA_Window_ID, MAKE_ID('E', 'X', 'C', 'H'),
444 WindowContents, (IPTR)(HGroup,
445 Child, (IPTR)(VGroup,
446 GroupFrameT(_(MSG_EXCHANGE_LISTVIEW)),
447 Child, (IPTR)(ListviewObject,
448 MUIA_Listview_List, (IPTR)(listgad = (Object *)ListObject,
449 InputListFrame,
450 MUIA_List_DisplayHook, (IPTR)&list_disp_hook,
451 MUIA_CycleChain, 1,
452 End),
453 End),
454 End),
455 Child, (IPTR)(BalanceObject, MUIA_CycleChain, 1, End),
456 Child, (IPTR)(VGroup,
457 MUIA_HorizWeight, 150,
458 Child, (IPTR)(VGroup,
459 GroupFrameT(_(MSG_EXCHANGE_INFO)),
460 Child, (IPTR)(textgad1 = (Object *)TextObject, TextFrame,
461 MUIA_Background, MUII_TextBack, End),
462 Child, (IPTR)(textgad2 = (Object *)TextObject, TextFrame,
463 MUIA_Background, MUII_TextBack, End),
464 End),
465 Child, (IPTR)HVSpace,
466 Child, (IPTR)(ColGroup(2),
467 MUIA_Group_SameSize, TRUE,
468 Child, (IPTR)(hidegad = SimpleButton(_(MSG_EXCHANGE_GAD_HIDE))),
469 Child, (IPTR)(showgad = SimpleButton(_(MSG_EXCHANGE_GAD_SHOW))),
470 Child, (IPTR)(cyclegad = MUI_MakeObject(MUIO_Cycle, NULL, cyclestrings)),
471 Child, (IPTR)(killgad = SimpleButton(_(MSG_EXCHANGE_GAD_REMOVE))),
472 End),
473 End),
474 End),
475 End),
476 End;
478 if (! app)
479 Cleanup(NULL); // Propably double start
481 // enable hotkey
482 maintask = FindTask(NULL);
483 get(app, MUIA_Application_Broker, &broker);
484 get(app, MUIA_Application_BrokerPort, &brokermp);
485 if ( ! broker || ! brokermp)
486 Cleanup(_(MSG_CANT_CREATE_BROKER));
488 popfilter = CxFilter(cx_popkey);
489 if (popfilter)
491 CxObj *popsig = CxSignal(maintask, SIGBREAKB_CTRL_F);
492 if (popsig)
494 CxObj *trans;
495 AttachCxObj(popfilter, popsig);
496 trans = CxTranslate(NULL);
497 if (trans) AttachCxObj(popfilter, trans);
499 AttachCxObj(broker, popfilter);
502 // initial entry of brokers to listgadget
503 update_list();
505 DoMethod(wnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
506 (IPTR)wnd, 3, MUIM_Set, MUIA_Window_Open, FALSE);
508 DoMethod(app, MUIM_Notify, MUIA_Application_DoubleStart, TRUE,
509 (IPTR)wnd, 2, MUIM_CallHook, &show_hook);
511 DoMethod(listgad, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
512 (IPTR)listgad, 2, MUIM_CallHook, (IPTR)&list_select_hook);
514 DoMethod(cyclegad, MUIM_Notify, MUIA_Cycle_Active, 0 /* Enable */,
515 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_ENABLE);
517 DoMethod(cyclegad, MUIM_Notify, MUIA_Cycle_Active, 1 /* Disable */,
518 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_DISABLE);
520 DoMethod(hidegad, MUIM_Notify, MUIA_Pressed, FALSE,
521 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_DISAPPEAR);
523 DoMethod(showgad, MUIM_Notify, MUIA_Pressed, FALSE,
524 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_APPEAR);
526 DoMethod(killgad, MUIM_Notify, MUIA_Pressed, FALSE,
527 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_KILL);
529 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_QUIT,
530 (IPTR)app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
532 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_HIDE,
533 (IPTR)wnd, 3, MUIM_Set, MUIA_Window_Open, FALSE);
535 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_ICONIFY,
536 (IPTR)app, 3, MUIM_Set, MUIA_Application_Iconified, TRUE);
540 /*********************************************************************************************/
542 static void HandleAll(void)
544 ULONG sigs = 0;
546 set(wnd, MUIA_Window_Open, cx_popup);
548 while((LONG) DoMethod(app, MUIM_Application_NewInput, (IPTR)&sigs)
549 != MUIV_Application_ReturnID_Quit)
551 if (sigs)
553 sigs = Wait(sigs | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
554 if (sigs & SIGBREAKF_CTRL_C)
556 break;
558 if (sigs & SIGBREAKF_CTRL_F)
560 CallHookPkt(&show_hook, NULL, NULL);
566 /*********************************************************************************************/
568 static void Cleanup(CONST_STRPTR txt)
570 MUI_DisposeObject(app);
571 FreeVec(cx_popkey);
572 FreeBrokerList(&brokerList);
573 FreeDiskObject(disko);
574 if (txt)
576 showSimpleMessage(txt);
577 exit(RETURN_ERROR);
579 exit(RETURN_OK);
582 /*********************************************************************************************/
584 int main(int argc, char **argv)
586 D(bug("Exchange started\n"));
587 NewList(&brokerList);
588 GetArguments(argc, argv);
589 InitMenus();
590 MakeGUI();
591 HandleAll();
592 Cleanup(NULL);
593 return RETURN_OK;
596 /*********************************************************************************************/
598 ADD2INIT(Locale_Initialize, 90);
599 ADD2EXIT(Locale_Deinitialize, 90);