/*****************************************************************************
 *
 * Nom:         commo.c
 * Desc:        start & stop commodity
 *
 *
 * version      : $VER: commo.c 1.0 (03.09.99)
 *
 *****************************************************************************
 */

#include <exec/libraries.h>
#include <libraries/commodities.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/alib_stdio_protos.h>
#include <clib/commodities_protos.h>
#include "utils.h"
#include "commo.h"
extern struct Library * CxBase ;
extern struct Library * IconBase ;
extern struct NewBroker newbroker ;

BOOL startCommo(idWin * prj, int argc, char **argv) {
  CxObj *broker, *filter, *sender, *translate;
  UBYTE *hotkey, **ttypes;
  BOOL done = FALSE ;
  struct MsgPort * broker_mp ;

  if (broker_mp = CreateMsgPort()) {
    prj->bk_port = broker_mp ;
    newbroker.nb_Port = broker_mp ;
    ttypes = ArgArrayInit(argc, argv) ;
    newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
    hotkey = ArgString(ttypes, "CX_POPPREFS", "control b");

    if (broker = CxBroker(&newbroker, NULL)) {
      if (filter = CxFilter(hotkey)) {
                        /* Add a CxObject to another's personal list */
        AttachCxObj(broker, filter);
        if (sender = CxSender(broker_mp, EVT_HOTKEY)) {
          AttachCxObj(filter, sender);
          if (translate = (CxTranslate(NULL))) {
            AttachCxObj(filter, translate);
            if (! CxObjError(filter)) {
              ActivateCxObj(broker, 1L);
              prj->broker = broker ;
              done = TRUE ;
            }else
              DeleteCxObjAll(broker);
          }
        }
      }     
    }
  }
  ArgArrayDone();
  return(done) ;
}

void stopCommo(idWin * prj) {
  CxMsg * msg ;
  DeleteCxObjAll(prj->broker) ;
  while(msg = (CxMsg *)GetMsg(prj->bk_port))
    ReplyMsg((struct Message *)msg);
  DeleteMsgPort(prj->bk_port) ;
}
