force CC/CXX to the passed in compiler to use.
[AROS.git] / workbench / tools / commodities / Exchange.c
blobd3452b845c3ece3ce68d886b4cced0a906da54a4
1 /*
2 Copyright © 1995-2016, 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 #ifndef __AROS__
50 #include <cxintern.h>
51 #endif
53 //#define DEBUG 1
54 #include <aros/debug.h>
55 #include <aros/asmcall.h>
56 #include <aros/symbolsets.h>
57 #include <libraries/iffparse.h>
58 #include <libraries/mui.h>
59 #include <workbench/startup.h>
61 #include <proto/muimaster.h>
62 #include <proto/locale.h>
63 #include <proto/intuition.h>
64 #include <proto/exec.h>
65 #include <proto/gadtools.h>
66 #include <proto/dos.h>
67 #include <proto/alib.h>
68 #include <proto/commodities.h>
69 #include <proto/utility.h>
70 #include <proto/icon.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
76 #define CATCOMP_ARRAY
77 #include "strings.h"
79 #ifdef __AROS__
80 #define CATALOG_NAME "System/Tools/Commodities.catalog"
81 #else
82 #define CATALOG_NAME "System/Tools/Exchange.catalog"
83 #endif
84 #define CATALOG_VERSION 3
86 TEXT version[] = "$VER: Exchange 1.3 (1.11.2016)";
88 #define ARG_TEMPLATE "CX_PRIORITY/N/K,CX_POPKEY/K,CX_POPUP/S"
89 #define DEF_POPKEY "ctrl alt h"
91 enum {
92 ARG_CXPRI,
93 ARG_CXPOPKEY,
94 ARG_CXPOPUP,
95 NUM_ARGS
98 static Object *app, *wnd, *listgad, *textgad1, *textgad2, *showgad,
99 *hidegad, *cyclegad, *killgad;
100 static CONST_STRPTR cyclestrings[3];
101 static struct Catalog *catalog;
102 static UBYTE s[257];
103 static struct List brokerList;
105 static struct Hook broker_hook;
106 static struct Hook list_disp_hook;
107 static struct Hook list_comp_hook;
108 static struct Hook list_select_hook;
109 static struct Hook inform_broker_hook;
110 static struct Hook show_hook;
112 static LONG cx_pri;
113 static char *cx_popkey;
114 static BOOL cx_popup = FALSE;
115 static CxObj *broker;
116 static struct MsgPort *brokermp;
117 static struct Task *maintask;
119 static void Cleanup(CONST_STRPTR txt);
120 static void GetArguments(int argc, char **argv);
121 static void HandleAll(void);
122 static void InitMenus(void);
123 static VOID Locale_Deinitialize(VOID);
124 static int Locale_Initialize(VOID);
125 static void MakeGUI(void);
126 static void showSimpleMessage(CONST_STRPTR msgString);
127 static void update_list(void);
128 static CONST_STRPTR _(ULONG id);
129 static struct DiskObject *disko;
131 /*********************************************************************************************/
133 static CONST_STRPTR _(ULONG id)
135 if (LocaleBase != NULL && catalog != NULL)
137 return GetCatalogStr(catalog, id, CatCompArray[id].cca_Str);
139 else
141 return CatCompArray[id].cca_Str;
145 #define __(id) ((IPTR) _(id)) /* Get a message, as an IPTR */
147 /*********************************************************************************************/
149 static int Locale_Initialize(VOID)
151 if (LocaleBase != NULL)
153 catalog = OpenCatalog(NULL, CATALOG_NAME, OC_Version,
154 CATALOG_VERSION, TAG_DONE);
156 else
158 catalog = NULL;
160 /* Note that in AROS constructors should return value opposite to the standard one.
161 Probably it's a misdesign, but we can do nothing with it. */
162 #ifdef __AROS__
163 return -1;
164 #else
165 return 0;
166 #endif
169 /*********************************************************************************************/
171 static VOID Locale_Deinitialize(VOID)
173 if(LocaleBase != NULL && catalog != NULL) CloseCatalog(catalog);
176 /*********************************************************************************************/
178 static void GetArguments(int argc, char **argv)
180 static struct RDArgs *myargs;
181 static IPTR args[NUM_ARGS];
182 static UBYTE **wbargs;
183 static struct WBStartup *argmsg;
184 static struct WBArg *wb_arg;
185 static STRPTR cxname;
186 if (argc)
188 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
190 Fault(IoErr(), 0, s, 256);
191 Cleanup(s);
193 if (args[ARG_CXPRI]) cx_pri = *(LONG*)args[ARG_CXPRI];
194 if (args[ARG_CXPOPKEY])
196 cx_popkey = StrDup((char *)args[ARG_CXPOPKEY]);
198 else
200 cx_popkey = StrDup(DEF_POPKEY);
202 if (args[ARG_CXPOPUP]) cx_popup = TRUE;
203 FreeArgs(myargs);
204 cxname = argv[0];
206 else
208 argmsg = (struct WBStartup *)argv;
209 wb_arg = argmsg->sm_ArgList;
210 cxname = wb_arg->wa_Name;
211 wbargs = ArgArrayInit(argc, (UBYTE**)argv);
212 cx_pri = ArgInt(wbargs, "CX_PRIORITY", 0);
213 cx_popkey = StrDup(ArgString(wbargs, "CX_POPKEY", DEF_POPKEY));
214 if (strnicmp(ArgString(wbargs, "CX_POPUP", "NO"), "Y", 1) == 0)
216 cx_popup = TRUE;
218 ArgArrayDone();
220 D(bug("Exchange Arguments pri %d popkey %s popup %d\n", cx_pri,
221 cx_popkey, cx_popup));
222 disko = GetDiskObject(cxname);
225 /*********************************************************************************************/
227 static struct NewMenu nm[] =
229 {NM_TITLE, (STRPTR)MSG_MEN_PROJECT },
230 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_HIDE },
231 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_ICONIFY },
232 {NM_ITEM, NM_BARLABEL },
233 {NM_ITEM, (STRPTR)MSG_MEN_PROJECT_QUIT },
234 {NM_END }
237 /*********************************************************************************************/
239 static void InitMenus(void)
241 struct NewMenu *actnm = nm;
243 for(actnm = nm; actnm->nm_Type != NM_END; actnm++)
245 if (actnm->nm_Label != NM_BARLABEL)
247 ULONG id = (IPTR)actnm->nm_Label;
248 CONST_STRPTR str = _(id);
250 if (actnm->nm_Type == NM_TITLE)
252 actnm->nm_Label = str;
253 } else {
254 actnm->nm_Label = str + 2;
255 if (str[0] != ' ') actnm->nm_CommKey = str;
257 actnm->nm_UserData = (APTR)(IPTR)id;
259 } /* if (actnm->nm_Label != NM_BARLABEL) */
261 } /* for(actnm = nm; nm->nm_Type != NM_END; nm++) */
264 /*********************************************************************************************/
266 static void showSimpleMessage(CONST_STRPTR msgString)
268 struct EasyStruct easyStruct;
270 easyStruct.es_StructSize = sizeof(easyStruct);
271 easyStruct.es_Flags = 0;
272 easyStruct.es_Title = _(MSG_EXCHANGE_CXNAME);
273 easyStruct.es_TextFormat = msgString;
274 easyStruct.es_GadgetFormat = _(MSG_OK);
276 if (IntuitionBase != NULL && !Cli())
278 EasyRequestArgs(NULL, &easyStruct, NULL, NULL);
280 else
282 PutStr(msgString);
286 /*********************************************************************************************/
288 static void update_list(void)
290 struct BrokerCopy *node, *active_node;
291 char active_name[CBD_NAMELEN];
292 IPTR new_pos = 0;
294 /* Copy name of active entry before its node is destroyed */
295 DoMethod(listgad, MUIM_List_GetEntry, MUIV_List_GetEntry_Active,
296 (IPTR)&active_node);
297 if (active_node != NULL)
298 CopyMem(active_node->bc_Name, active_name, CBD_NAMELEN);
300 CopyBrokerList(&brokerList);
302 nnset(textgad1, MUIA_Text_Contents, NULL);
303 nnset(textgad2, MUIA_Text_Contents, NULL);
305 set(listgad, MUIA_List_Quiet, TRUE);
306 DoMethod(listgad, MUIM_List_Clear);
307 ForeachNode(&brokerList, node)
309 D(bug("Exchange: Brokernode '%s'\n", node->bc_Name));
310 DoMethod(listgad, MUIM_List_InsertSingle, node,
311 MUIV_List_Insert_Bottom);
313 /* Reselect broker if its name matches what was previously selected */
314 if (active_node != NULL
315 && Stricmp(active_name, node->bc_Name) == 0)
317 get(listgad, MUIA_List_InsertPosition, &new_pos);
318 set(listgad, MUIA_List_Active, new_pos);
319 D(bug("Exchange: matched old active node '%s' (entry %d)\n",
320 node->bc_Name, new_pos));
323 set(listgad, MUIA_List_Quiet, FALSE);
326 /*********************************************************************************************/
328 AROS_UFH3(void, broker_func,
329 AROS_UFHA(struct Hook *, h, A0),
330 AROS_UFHA(Object * , object, A2),
331 AROS_UFHA(CxMsg * , msg, A1))
333 AROS_USERFUNC_INIT
335 D(bug("Exchange: Broker hook called\n"));
336 if (CxMsgType(msg) == CXM_COMMAND)
338 if (CxMsgID(msg) == CXCMD_APPEAR)
340 CallHookPkt(&show_hook, NULL, NULL);
342 else if (CxMsgID(msg) == CXCMD_DISAPPEAR)
344 set(wnd, MUIA_Window_Open, FALSE);
346 else if (CxMsgID(msg) == CXCMD_LIST_CHG)
348 update_list();
351 AROS_USERFUNC_EXIT
354 /*** show_func ************************************************************/
355 AROS_UFH3(
356 void, show_func,
357 AROS_UFHA(struct Hook *, hook, A0),
358 AROS_UFHA(APTR *, obj, A2),
359 AROS_UFHA(APTR, param, A1)
362 AROS_USERFUNC_INIT
364 if (XGET(app, MUIA_Application_Iconified) == TRUE)
365 set(app, MUIA_Application_Iconified, FALSE);
366 else
367 set(wnd, MUIA_Window_Open, TRUE);
369 AROS_USERFUNC_EXIT
372 /*********************************************************************************************/
374 AROS_UFH3(void, list_display_func,
375 AROS_UFHA(struct Hook * , h, A0),
376 AROS_UFHA(char ** , array, A2),
377 AROS_UFHA(struct BrokerCopy *, node, A1))
379 AROS_USERFUNC_INIT
381 *array = node->bc_Name;
383 AROS_USERFUNC_EXIT
386 /*********************************************************************************************/
388 AROS_UFH3(IPTR, list_compare_func,
389 AROS_UFHA(struct Hook * , h, A0),
390 AROS_UFHA(struct BrokerCopy *, node1, A2),
391 AROS_UFHA(struct BrokerCopy *, node2, A1))
393 AROS_USERFUNC_INIT
395 return Stricmp(node2->bc_Name, node1->bc_Name);
397 AROS_USERFUNC_EXIT
400 /*********************************************************************************************/
402 AROS_UFH3(void, list_select_func,
403 AROS_UFHA(struct Hook *, h, A0),
404 AROS_UFHA(Object * , object, A2),
405 AROS_UFHA(APTR , msg, A1))
407 AROS_USERFUNC_INIT
409 struct BrokerCopy *bc;
410 DoMethod(listgad, MUIM_List_GetEntry, MUIV_List_GetEntry_Active,
411 (IPTR)&bc);
412 if (bc)
414 BOOL showHide = (bc->bc_Flags & COF_SHOW_HIDE) == 0;
415 BOOL active = (bc->bc_Flags & COF_ACTIVE) != 0;
416 nnset(textgad1, MUIA_Text_Contents, bc->bc_Title);
417 nnset(textgad2, MUIA_Text_Contents, bc->bc_Descr);
418 nnset(hidegad, MUIA_Disabled, showHide);
419 nnset(showgad, MUIA_Disabled, showHide);
420 nnset(cyclegad, MUIA_Cycle_Active, active ? 0 : 1);
423 AROS_USERFUNC_EXIT
426 /*********************************************************************************************/
428 AROS_UFH3(void, inform_broker_func,
429 AROS_UFHA(struct Hook *, h, A0),
430 AROS_UFHA(Object* , object, A2),
431 AROS_UFHA(LONG * , command, A1))
433 AROS_USERFUNC_INIT
435 struct BrokerCopy *bc;
436 DoMethod(listgad, MUIM_List_GetEntry, MUIV_List_GetEntry_Active,
437 (IPTR) &bc);
438 if (bc)
440 D(bug("Exchange: Broker inform %s\n", bc->bc_Node.ln_Name));
441 BrokerCommand(bc->bc_Node.ln_Name, *command);
444 AROS_USERFUNC_EXIT
447 /*********************************************************************************************/
449 static void MakeGUI(void)
451 Object *menu;
452 static TEXT wintitle[100];
453 CxObj *popfilter;
455 cyclestrings[0] = _(MSG_EXCHANGE_CYCLE_ACTIVE);
456 cyclestrings[1] = _(MSG_EXCHANGE_CYCLE_INACTIVE);
457 menu = MUI_MakeObject(MUIO_MenustripNM, &nm, 0);
459 broker_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(broker_func);
460 list_disp_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(list_display_func);
461 list_comp_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(list_compare_func);
462 list_select_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(list_select_func);
463 inform_broker_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(inform_broker_func);
464 show_hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(show_func);
466 snprintf(wintitle, sizeof(wintitle), _(MSG_EXCHANGE_WINTITLE), cx_popkey);
468 app = (Object *)ApplicationObject,
469 MUIA_Application_Title, __(MSG_EXCHANGE_CXNAME),
470 MUIA_Application_Version, (IPTR)version,
471 MUIA_Application_Copyright,
472 (IPTR)"Copyright © 1995-2016, The AROS Development Team",
473 MUIA_Application_Author, (IPTR)"The AROS Development Team",
474 MUIA_Application_Description, __(MSG_EXCHANGE_CXDESCR),
475 MUIA_Application_BrokerPri, cx_pri,
476 MUIA_Application_BrokerHook, (IPTR)&broker_hook,
477 MUIA_Application_Base, (IPTR)"EXCHANGE",
478 MUIA_Application_SingleTask, TRUE,
479 MUIA_Application_Menustrip, (IPTR)menu,
480 MUIA_Application_DiskObject, (IPTR)disko,
481 SubWindow, (IPTR)(wnd = (Object *)WindowObject,
482 MUIA_Window_Title, (IPTR)wintitle,
483 MUIA_Window_ID, MAKE_ID('E', 'X', 'C', 'H'),
484 WindowContents, (IPTR)(HGroup,
485 Child, (IPTR)(VGroup,
486 GroupFrameT(_(MSG_EXCHANGE_LISTVIEW)),
487 Child, (IPTR)(ListviewObject,
488 MUIA_Listview_List,
489 (IPTR)(listgad = (Object *)ListObject,
490 InputListFrame,
491 MUIA_List_DisplayHook, (IPTR)&list_disp_hook,
492 MUIA_List_CompareHook, (IPTR)&list_comp_hook,
493 MUIA_CycleChain, 1,
494 End),
495 End),
496 End),
497 Child, (IPTR)(BalanceObject, MUIA_CycleChain, 1, End),
498 Child, (IPTR)(VGroup,
499 MUIA_HorizWeight, 150,
500 Child, (IPTR)(VGroup,
501 GroupFrameT(_(MSG_EXCHANGE_INFO)),
502 Child, (IPTR)(textgad1 = (Object *)TextObject,
503 TextFrame,
504 MUIA_Background, MUII_TextBack,
505 End),
506 Child, (IPTR)(textgad2 = (Object *)TextObject,
507 TextFrame,
508 MUIA_Background, MUII_TextBack,
509 End),
510 End),
511 Child, (IPTR)HVSpace,
512 Child, (IPTR)(ColGroup(2),
513 MUIA_Group_SameSize, TRUE,
514 Child, (IPTR)(hidegad =
515 SimpleButton(_(MSG_EXCHANGE_GAD_HIDE))),
516 Child, (IPTR)(showgad =
517 SimpleButton(_(MSG_EXCHANGE_GAD_SHOW))),
518 Child, (IPTR)(cyclegad =
519 MUI_MakeObject(MUIO_Cycle, NULL, cyclestrings)),
520 Child, (IPTR)(killgad =
521 SimpleButton(_(MSG_EXCHANGE_GAD_REMOVE))),
522 End),
523 End),
524 End),
525 End),
526 End;
528 if (! app)
529 Cleanup(NULL); // Propably double start
531 // enable hotkey
532 maintask = FindTask(NULL);
533 get(app, MUIA_Application_Broker, &broker);
534 get(app, MUIA_Application_BrokerPort, &brokermp);
535 if ( ! broker || ! brokermp)
536 Cleanup(_(MSG_CANT_CREATE_BROKER));
538 popfilter = CxFilter(cx_popkey);
539 if (popfilter)
541 CxObj *popsig = CxSignal(maintask, SIGBREAKB_CTRL_F);
542 if (popsig)
544 CxObj *trans;
545 AttachCxObj(popfilter, popsig);
546 trans = CxTranslate(NULL);
547 if (trans) AttachCxObj(popfilter, trans);
549 AttachCxObj(broker, popfilter);
552 // initial entry of brokers to listgadget
553 update_list();
555 DoMethod(wnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
556 (IPTR)wnd, 3, MUIM_Set, MUIA_Window_Open, FALSE);
558 DoMethod(app, MUIM_Notify, MUIA_Application_DoubleStart, TRUE,
559 (IPTR)wnd, 2, MUIM_CallHook, &show_hook);
561 DoMethod(listgad, MUIM_Notify, MUIA_List_Active, MUIV_EveryTime,
562 (IPTR)listgad, 2, MUIM_CallHook, (IPTR)&list_select_hook);
564 DoMethod(cyclegad, MUIM_Notify, MUIA_Cycle_Active, 0 /* Enable */,
565 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_ENABLE);
567 DoMethod(cyclegad, MUIM_Notify, MUIA_Cycle_Active, 1 /* Disable */,
568 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_DISABLE);
570 DoMethod(hidegad, MUIM_Notify, MUIA_Pressed, FALSE,
571 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook,
572 CXCMD_DISAPPEAR);
574 DoMethod(showgad, MUIM_Notify, MUIA_Pressed, FALSE,
575 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_APPEAR);
577 DoMethod(killgad, MUIM_Notify, MUIA_Pressed, FALSE,
578 (IPTR)app, 3, MUIM_CallHook, (IPTR)&inform_broker_hook, CXCMD_KILL);
580 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_QUIT,
581 (IPTR)app, 2, MUIM_Application_ReturnID,
582 MUIV_Application_ReturnID_Quit);
584 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_HIDE,
585 (IPTR)wnd, 3, MUIM_Set, MUIA_Window_Open, FALSE);
587 DoMethod(wnd, MUIM_Notify, MUIA_Window_MenuAction, MSG_MEN_PROJECT_ICONIFY,
588 (IPTR)app, 3, MUIM_Set, MUIA_Application_Iconified, TRUE);
591 /*********************************************************************************************/
593 static void HandleAll(void)
595 ULONG sigs = 0;
597 set(wnd, MUIA_Window_Open, cx_popup);
599 while((LONG) DoMethod(app, MUIM_Application_NewInput, (IPTR)&sigs)
600 != MUIV_Application_ReturnID_Quit)
602 if (sigs)
604 sigs = Wait(sigs | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
605 if (sigs & SIGBREAKF_CTRL_C)
607 break;
609 if (sigs & SIGBREAKF_CTRL_F)
611 CallHookPkt(&show_hook, NULL, NULL);
617 /*********************************************************************************************/
619 static void Cleanup(CONST_STRPTR txt)
621 MUI_DisposeObject(app);
622 FreeVec(cx_popkey);
623 FreeBrokerList(&brokerList);
624 FreeDiskObject(disko);
625 if (txt)
627 showSimpleMessage(txt);
628 exit(RETURN_ERROR);
630 exit(RETURN_OK);
633 /*********************************************************************************************/
635 int main(int argc, char **argv)
637 D(bug("Exchange started\n"));
638 NewList(&brokerList);
639 GetArguments(argc, argv);
640 InitMenus();
641 MakeGUI();
642 HandleAll();
643 Cleanup(NULL);
644 return RETURN_OK;
647 /*********************************************************************************************/
649 ADD2INIT(Locale_Initialize, 90);
650 ADD2EXIT(Locale_Deinitialize, 90);