Fixed AutoDoc indentation, spelling, grammar.
[AROS.git] / workbench / tools / commodities / NoCapsLock.c
blob932e822bc5832c577f6e9eab0cf9512ec33b9c3c
1 /*
2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
3 $Id$
5 NoCapsLock commodity -- Renders the CAPS LOCK key ineffective
6 */
8 /******************************************************************************
10 NAME
12 NoCapsLock
14 SYNOPSIS
16 CX_PRIORITY/N/K
18 LOCATION
20 SYS:Tools/Commodities
22 FUNCTION
24 Renders the CAPS LOCK key ineffective.
26 INPUTS
28 CX_PRIORITY -- The priority of the commodity
30 RESULT
32 NOTES
34 EXAMPLE
36 BUGS
38 SEE ALSO
40 INTERNALS
42 HISTORY
44 05.03.2000 SDuvan implemented
46 ******************************************************************************/
48 #include <aros/symbolsets.h>
49 #include <intuition/intuition.h>
50 #include <intuition/intuitionbase.h>
51 #include <libraries/commodities.h>
52 #include <libraries/locale.h>
53 #include <proto/exec.h>
54 #include <proto/dos.h>
55 #include <proto/intuition.h>
56 #include <proto/layers.h>
57 #include <proto/commodities.h>
58 #include <proto/locale.h>
59 #include <proto/alib.h>
61 #include <stdio.h>
62 #include <string.h>
64 #define DEBUG 0
65 #include <aros/debug.h>
67 #define CATCOMP_ARRAY
68 #include "strings.h"
70 #define CATALOG_NAME "System/Tools/Commodities.catalog"
71 #define CATALOG_VERSION 3
74 /***************************************************************************/
76 UBYTE version[] = "$VER: NoCapsLock 1.1 (15.04.2006)";
78 #define ARG_TEMPLATE "CX_PRIORITY=PRI/N/K"
80 #define ARG_PRI 0
81 #define NUM_ARGS 1
84 static struct NewBroker nb =
86 NB_VERSION,
87 NULL,
88 NULL,
89 NULL,
90 NBU_NOTIFY | NBU_UNIQUE,
93 NULL,
98 typedef struct _APState
100 CxObj *as_broker;
101 struct MsgPort *as_msgPort;
102 } APState;
105 static struct Catalog *catalog;
107 /***************************************************************************/
109 static void freeResources(APState *as);
110 static BOOL initiate(int argc, char **argv, APState *as);
111 static void killCapsLockCustomFunc(CxMsg *msg, CxObj *co);
112 static CONST_STRPTR _(ULONG id);
113 static BOOL Locale_Initialize(VOID);
114 static VOID Locale_Deinitialize(VOID);
115 static void showSimpleMessage(CONST_STRPTR msgString);
117 /***************************************************************************/
119 static CONST_STRPTR _(ULONG id)
121 if (LocaleBase != NULL && catalog != NULL)
123 return GetCatalogStr(catalog, id, CatCompArray[id].cca_Str);
125 else
127 return CatCompArray[id].cca_Str;
131 /***************************************************************************/
133 static BOOL Locale_Initialize(VOID)
135 if (LocaleBase != NULL)
137 catalog = OpenCatalog
139 NULL, CATALOG_NAME, OC_Version, CATALOG_VERSION, TAG_DONE
142 else
144 catalog = NULL;
147 return TRUE;
150 /************************************************************************************/
152 static VOID Locale_Deinitialize(VOID)
154 if(LocaleBase != NULL && catalog != NULL) CloseCatalog(catalog);
157 /************************************************************************************/
159 static void showSimpleMessage(CONST_STRPTR msgString)
161 struct EasyStruct easyStruct;
163 easyStruct.es_StructSize = sizeof(easyStruct);
164 easyStruct.es_Flags = 0;
165 easyStruct.es_Title = _(MSG_NOCAPSLOCK_CXNAME);
166 easyStruct.es_TextFormat = msgString;
167 easyStruct.es_GadgetFormat = _(MSG_OK);
169 if (IntuitionBase != NULL && !Cli() )
171 EasyRequestArgs(NULL, &easyStruct, NULL, NULL);
173 else
175 PutStr(msgString);
179 /***************************************************************************/
181 static BOOL initiate(int argc, char **argv, APState *as)
183 CxObj *customObj, *filterObj;
185 memset(as, 0, sizeof(APState));
187 if (Cli() != NULL)
189 struct RDArgs *rda;
190 IPTR *args[] = { NULL };
192 rda = ReadArgs(ARG_TEMPLATE, (IPTR *)args, NULL);
194 if (rda != NULL)
196 if (args[ARG_PRI] != NULL)
198 nb.nb_Pri = (BYTE)(*args[ARG_PRI]);
202 FreeArgs(rda);
204 else
206 UBYTE **array = ArgArrayInit(argc, (UBYTE **)argv);
208 nb.nb_Pri = ArgInt(array, "CX_PRIORITY", 0);
209 ArgArrayDone();
212 nb.nb_Name = _(MSG_NOCAPSLOCK_CXNAME);
213 nb.nb_Title = _(MSG_NOCAPSLOCK_CXTITLE);
214 nb.nb_Descr = _(MSG_NOCAPSLOCK_CXDESCR);
216 as->as_msgPort = CreateMsgPort();
218 if (as->as_msgPort == NULL)
220 showSimpleMessage(_(MSG_CANT_CREATE_MSGPORT));
222 return FALSE;
225 nb.nb_Port = as->as_msgPort;
227 as->as_broker = CxBroker(&nb, 0);
229 if (as->as_broker == NULL)
231 return FALSE;
234 filterObj = CxFilter(NULL);
235 if (filterObj == NULL)
237 showSimpleMessage(_(MSG_CANT_CREATE_CUSTOM));
239 return FALSE;
241 else
243 static IX ix =
245 IX_VERSION,
246 IECLASS_RAWKEY,
249 IEQUALIFIER_CAPSLOCK,
250 IEQUALIFIER_CAPSLOCK,
254 SetFilterIX(filterObj, &ix);
255 AttachCxObj(as->as_broker, filterObj);
259 customObj = CxCustom(killCapsLockCustomFunc, 0);
261 if (customObj == NULL)
263 showSimpleMessage(_(MSG_CANT_CREATE_CUSTOM));
265 return FALSE;
268 AttachCxObj(filterObj, customObj);
269 ActivateCxObj(as->as_broker, TRUE);
271 return TRUE;
274 /***************************************************************************/
276 static void freeResources(APState *as)
278 struct Message *cxm;
280 if (CxBase != NULL)
282 if (as->as_broker != NULL)
284 DeleteCxObjAll(as->as_broker);
288 if (as->as_msgPort != NULL)
290 while ((cxm = GetMsg(as->as_msgPort)))
292 ReplyMsg(cxm);
295 DeleteMsgPort(as->as_msgPort);
299 /***************************************************************************/
301 /* Our CxCustom() function that is invoked everytime an imputevent is
302 routed to our broker */
303 static void killCapsLockCustomFunc(CxMsg *msg, CxObj *co)
305 struct InputEvent *ie = (struct InputEvent *)CxMsgData(msg);
307 ie->ie_Qualifier &= ~IEQUALIFIER_CAPSLOCK;
310 /***************************************************************************/
312 /* React on command messages sent by commodities.library */
313 static void handleCx(APState *as)
315 CxMsg *msg;
316 BOOL quit = FALSE;
317 LONG signals;
319 while (!quit)
321 signals = Wait((1 << nb.nb_Port->mp_SigBit) | SIGBREAKF_CTRL_C);
323 if (signals & (1 << nb.nb_Port->mp_SigBit))
325 while ((msg = (CxMsg *)GetMsg(as->as_msgPort)))
327 switch (CxMsgType(msg))
329 case CXM_COMMAND:
330 switch (CxMsgID(msg))
332 case CXCMD_DISABLE:
333 ActivateCxObj(as->as_broker, FALSE);
334 break;
336 case CXCMD_ENABLE:
337 ActivateCxObj(as->as_broker, TRUE);
338 break;
340 case CXCMD_UNIQUE:
341 /* Running the program twice <=> quit */
342 /* Fall through */
344 case CXCMD_KILL:
345 quit = TRUE;
346 break;
348 } /* switch(CxMsgID(msg)) */
350 break;
351 } /* switch (CxMsgType(msg))*/
353 ReplyMsg((struct Message *)msg);
355 } /* while((msg = (CxMsg *)GetMsg(cs->cs_msgPort))) */
358 if (signals & SIGBREAKF_CTRL_C)
360 quit = TRUE;
363 } /* while (!quit) */
366 /***************************************************************************/
368 int main(int argc, char **argv)
370 APState aState;
371 int error = RETURN_OK;
373 if (initiate(argc, argv, &aState))
375 handleCx(&aState);
377 else
379 error = RETURN_FAIL;
382 freeResources(&aState);
384 return error;
387 /***************************************************************************/
389 ADD2INIT(Locale_Initialize, 90);
390 ADD2EXIT(Locale_Deinitialize, 90);