Initial commit.
[amiga/xmodule.git] / Cx.c
1 /*
2 **      Cx.c
3 **
4 **      Copyright (C) 1994,95,96,97,98 Bernardo Innocenti
5 **
6 **      Commodity support functions
7 */
8
9
10 #include <exec/memory.h>
11 #include <libraries/commodities.h>
12
13 #include <proto/exec.h>
14 #include <proto/dos.h>
15 #include <proto/intuition.h>
16 #include <proto/commodities.h>
17
18 #include "XModulePriv.h"
19 #include "Gui.h"
20
21
22 #define CX_POPKEY       'POP!'
23
24
25 XDEF BYTE CxPri = 0;
26 XDEF UBYTE CxPopKey[32] = "control alt x";
27 XDEF BOOL CxPopup = TRUE;
28 XDEF ULONG CxSig = 0;
29
30 XDEF struct MsgPort *CxPort = NULL;
31 XDEF CxObj *MyBroker = NULL;
32
33
34 static struct NewBroker MyNewBroker =
35 {
36         NB_VERSION,
37         PRGNAME,
38         Version+6,
39         "Module Processing Utility",
40         NBU_DUPLICATE,
41         COF_SHOW_HIDE,
42         0,
43         NULL,
44         0
45 };
46
47
48
49 GLOBALCALL void HandleCx (void)
50 {
51         CxMsg   *cxm;
52         ULONG    type;
53         LONG     id;
54
55         while (cxm = (CxMsg *) GetMsg (CxPort))
56         {
57                 type = CxMsgType (cxm);
58                 id = CxMsgID (cxm);
59
60                 switch (type)
61                 {
62                         case CXM_IEVENT:
63                                 if (id == CX_POPKEY) DeIconify();
64                                 break;
65
66                         case CXM_COMMAND:
67                                 switch (id)
68                                 {
69                                         case CXCMD_DISABLE:
70                                                 ActivateCxObj (MyBroker, FALSE);
71                                                 break;
72
73                                         case CXCMD_ENABLE:
74                                                 ActivateCxObj (MyBroker, TRUE);
75                                                 break;
76
77                                         case CXCMD_APPEAR:
78                                                 DeIconify();
79                                                 break;
80
81                                         case CXCMD_DISAPPEAR:
82                                                 CloseDownScreen();
83                                                 break;
84
85                                         case CXCMD_KILL:
86                                                 Quit = TRUE;
87                                                 GuiSwitches.AskExit = FALSE;
88                                                 break;
89
90                                         default:
91                                                 break;
92
93                                 }       /* End Switch (id) */
94
95                         default:
96                                 break;
97
98                 }       /* End Switch (type) */
99
100                 ReplyMsg ((struct Message *) cxm);
101
102         }       /* End While (GetMsg()) */
103 }
104
105
106
107 GLOBALCALL LONG SetupCx (void)
108 {
109         CxObj *filter, *sender;
110
111         if (!CxPopKey[0]) return RETURN_FAIL;
112
113         if (!(CxBase = OpenLibrary ("commodities.library", 37L)))
114                 return RETURN_FAIL;
115
116         if (!(CxPort = CreateMsgPort ()))
117         {
118                 CleanupCx();
119                 return ERROR_NO_FREE_STORE;
120         }
121
122         CxSig = 1 << CxPort->mp_SigBit;
123         Signals |= CxSig;
124
125         MyNewBroker.nb_Pri = CxPri;
126         MyNewBroker.nb_Port = CxPort;
127
128         if (!(MyBroker = CxBroker (&MyNewBroker, NULL)))
129         {
130                 CleanupCx();
131                 return RETURN_FAIL;
132         }
133
134         /* Create PopKey Filter/Sender */
135
136         if (filter = CxFilter (CxPopKey))
137         {
138                 if (CxObjError (filter) & COERR_BADFILTER)
139                         ShowMessage (MSG_BAD_HOTKEY);
140
141                 AttachCxObj (MyBroker, filter);
142
143                 if (sender = CxSender (CxPort, CX_POPKEY))
144                         AttachCxObj (filter, sender);
145         }
146
147         ActivateCxObj (MyBroker, TRUE);
148
149         return RETURN_OK;
150 }
151
152
153
154 GLOBALCALL void CleanupCx (void)
155 {
156         if (CxBase)
157         {
158                 if (MyBroker)
159                         { DeleteCxObjAll (MyBroker); MyBroker = NULL; }
160
161                 if (CxPort)
162                 {
163                         KillMsgPort (CxPort);
164                         CxPort = NULL;
165                         Signals &= ~CxSig;
166                         CxSig = 0;
167                 }
168
169                 CloseLibrary (CxBase); CxBase = NULL;
170         }
171 }