ToolsMenu/broker.c

136 lines
3.4 KiB
C
Executable File

/*
ToolsMenu - Add tools to the Workbench Tools menu
Copyright (C) 2015, 2018 Kim Fastrup Larsen
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either ver-
sion 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be use-
ful, but WITHOUT ANY WARRANTY; without even the implied war-
ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public Li-
cense along with this program. If not, see
<http://www.gnu.org/licenses/>.
The author can be contacted on <kimflarsen@hotmail.com>
*/
#include <dos/dos.h>
#include <libraries/commodities.h>
#include <clib/commodities_protos.h>
#include <clib/exec_protos.h>
#ifdef USE_PRAGMAS
#include <proto/commodities.h>
#include <proto/exec.h>
#endif
#include <stdlib.h>
#include <strings.h>
#define CATCOMP_NUMBERS
#include "args.h"
#include "broker.h"
#include "catalog.h"
#include "common.h"
#include "cx.h"
#include "error.h"
#include "strings.h"
static struct MsgPort *broker_port;
static CxObj *broker;
ULONG broker_set_up()
{
struct NewBroker new_broker;
LONG error;
CxObj *filter;
if ((broker_port = CreateMsgPort()) == NULL)
exit(RETURN_FAIL);
memset(&new_broker, 0, sizeof new_broker);
new_broker.nb_Version = NB_VERSION;
new_broker.nb_Name = APP_NAME;
new_broker.nb_Title = TITLE;
new_broker.nb_Descr = get_string(MSG_DESCRIPTION);
new_broker.nb_Unique = NBU_UNIQUE | NBU_NOTIFY;
new_broker.nb_Flags = COF_SHOW_HIDE;
new_broker.nb_Pri = args.cx_priority;
new_broker.nb_Port = broker_port;
if ((broker = CxBroker(&new_broker, &error)) == NULL)
exit((error == CBERR_DUP) ? RETURN_OK : RETURN_FAIL);
if ((filter = CxFilter(args.cx_popkey)) == NULL)
exit(RETURN_FAIL);
AttachCxObj(broker, filter);
AttachCxObj(filter, CxSender(broker_port, 0));
AttachCxObj(filter, CxTranslate(NULL));
if ((error = CxObjError(filter)) != 0) {
if (error == COERR_BADFILTER) {
error_message(get_string(MSG_ERR_HOTKEY), NULL);
exit(RETURN_ERROR);
} else
exit(RETURN_FAIL);
}
ActivateCxObj(broker, 1);
return 1UL << broker_port->mp_SigBit;
}
static void handle_command(ULONG msg_id)
{
switch (msg_id) {
case CXCMD_DISABLE:
ActivateCxObj(broker, 0);
cx_disable();
break;
case CXCMD_ENABLE:
ActivateCxObj(broker, 1);
cx_enable();
break;
case CXCMD_KILL:
cx_quit();
break;
case CXCMD_UNIQUE:
case CXCMD_APPEAR:
cx_show();
break;
case CXCMD_DISAPPEAR:
cx_hide();
break;
}
}
void broker_handle_signal()
{
CxMsg *msg;
ULONG msg_type, msg_id;
while ((msg = (CxMsg *) GetMsg(broker_port)) != NULL) {
msg_type = CxMsgType(msg);
msg_id = CxMsgID(msg);
ReplyMsg((struct Message *) msg);
switch (msg_type) {
case CXM_IEVENT:
/* Must be Hot Key event */
cx_show();
break;
case CXM_COMMAND:
handle_command(msg_id);
break;
}
}
}
void broker_clean_up()
{
if (broker != NULL)
DeleteCxObjAll(broker);
delete_port(broker_port);
}