2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
5 Exchange -- controls commodities.
8 /******************************************************************************
16 CX_PRIORITY/N/K,CX_POPKEY/K,CX_POPUP/S
24 Manages the commodities in the system
28 CX_PRIORITY -- Priority of the Exchange broker
29 CX_POPKEY -- Hotkey combination for Exchange
30 CX_POPUP -- Appear at startup
44 ******************************************************************************/
46 #define AROS_ALMOST_COMPATIBLE
47 #define MUIMASTER_YES_INLINE_STDARG
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>
80 #define CATALOG_NAME "System/Tools/Commodities.catalog"
82 #define CATALOG_NAME "System/Tools/Exchange.catalog"
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"
98 static Object
*app
, *wnd
, *listgad
, *textgad1
, *textgad2
, *showgad
,
99 *hidegad
, *cyclegad
, *killgad
;
100 static CONST_STRPTR cyclestrings
[3];
101 static struct Catalog
*catalog
;
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
;
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
);
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
);
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. */
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
;
188 if (!(myargs
= ReadArgs(ARG_TEMPLATE
, args
, NULL
)))
190 Fault(IoErr(), 0, s
, 256);
193 if (args
[ARG_CXPRI
]) cx_pri
= *(LONG
*)args
[ARG_CXPRI
];
194 if (args
[ARG_CXPOPKEY
])
196 cx_popkey
= StrDup((char *)args
[ARG_CXPOPKEY
]);
200 cx_popkey
= StrDup(DEF_POPKEY
);
202 if (args
[ARG_CXPOPUP
]) cx_popup
= TRUE
;
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)
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
},
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
;
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
);
286 /*********************************************************************************************/
288 static void update_list(void)
290 struct BrokerCopy
*node
, *active_node
;
291 char active_name
[CBD_NAMELEN
];
294 /* Copy name of active entry before its node is destroyed */
295 DoMethod(listgad
, MUIM_List_GetEntry
, MUIV_List_GetEntry_Active
,
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
))
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
)
354 /*** show_func ************************************************************/
357 AROS_UFHA(struct Hook
*, hook
, A0
),
358 AROS_UFHA(APTR
*, obj
, A2
),
359 AROS_UFHA(APTR
, param
, A1
)
364 if (XGET(app
, MUIA_Application_Iconified
) == TRUE
)
365 set(app
, MUIA_Application_Iconified
, FALSE
);
367 set(wnd
, MUIA_Window_Open
, TRUE
);
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
))
381 *array
= node
->bc_Name
;
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
))
395 return Stricmp(node2
->bc_Name
, node1
->bc_Name
);
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
))
409 struct BrokerCopy
*bc
;
410 DoMethod(listgad
, MUIM_List_GetEntry
, MUIV_List_GetEntry_Active
,
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);
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
))
435 struct BrokerCopy
*bc
;
436 DoMethod(listgad
, MUIM_List_GetEntry
, MUIV_List_GetEntry_Active
,
440 D(bug("Exchange: Broker inform %s\n", bc
->bc_Node
.ln_Name
));
441 BrokerCommand(bc
->bc_Node
.ln_Name
, *command
);
447 /*********************************************************************************************/
449 static void MakeGUI(void)
452 static TEXT wintitle
[100];
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
,
489 (IPTR
)(listgad
= (Object
*)ListObject
,
491 MUIA_List_DisplayHook
, (IPTR
)&list_disp_hook
,
492 MUIA_List_CompareHook
, (IPTR
)&list_comp_hook
,
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
,
504 MUIA_Background
, MUII_TextBack
,
506 Child
, (IPTR
)(textgad2
= (Object
*)TextObject
,
508 MUIA_Background
, MUII_TextBack
,
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
))),
529 Cleanup(NULL
); // Propably double start
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
);
541 CxObj
*popsig
= CxSignal(maintask
, SIGBREAKB_CTRL_F
);
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
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
,
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)
597 set(wnd
, MUIA_Window_Open
, cx_popup
);
599 while((LONG
) DoMethod(app
, MUIM_Application_NewInput
, (IPTR
)&sigs
)
600 != MUIV_Application_ReturnID_Quit
)
604 sigs
= Wait(sigs
| SIGBREAKF_CTRL_C
| SIGBREAKF_CTRL_F
);
605 if (sigs
& SIGBREAKF_CTRL_C
)
609 if (sigs
& SIGBREAKF_CTRL_F
)
611 CallHookPkt(&show_hook
, NULL
, NULL
);
617 /*********************************************************************************************/
619 static void Cleanup(CONST_STRPTR txt
)
621 MUI_DisposeObject(app
);
623 FreeBrokerList(&brokerList
);
624 FreeDiskObject(disko
);
627 showSimpleMessage(txt
);
633 /*********************************************************************************************/
635 int main(int argc
, char **argv
)
637 D(bug("Exchange started\n"));
638 NewList(&brokerList
);
639 GetArguments(argc
, argv
);
647 /*********************************************************************************************/
649 ADD2INIT(Locale_Initialize
, 90);
650 ADD2EXIT(Locale_Deinitialize
, 90);