added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / tools / commodities / AutoPoint.c
blob892eb94a2292e7dff828ea8a6787e549a9b14ed0
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 AutoPoint commodity -- activates windows under the mouse pointer.
6 */
8 /******************************************************************************
10 NAME
12 AutoPoint
14 SYNOPSIS
16 CX_PRIORITY/N/K, LAG/S
18 LOCATION
20 Workbench:Tools/Commodities
22 FUNCTION
24 Automatically activates the window under the mouse pointer.
26 INPUTS
28 CX_PRIORITY -- The priority of the commodity
30 LAG -- Wait for the next timer event to activate the window
32 RESULT
34 NOTES
36 EXAMPLE
38 BUGS
40 SEE ALSO
42 INTERNALS
44 HISTORY
46 05.03.2000 SDuvan implemented
48 ******************************************************************************/
50 #include <aros/symbolsets.h>
51 #include <intuition/intuition.h>
52 #include <intuition/intuitionbase.h>
53 #include <libraries/commodities.h>
54 #include <libraries/locale.h>
55 #include <proto/exec.h>
56 #include <proto/dos.h>
57 #include <proto/intuition.h>
58 #include <proto/layers.h>
59 #include <proto/commodities.h>
60 #include <proto/locale.h>
61 #include <proto/alib.h>
63 #include <stdio.h>
64 #include <string.h>
66 #define DEBUG 0
67 #include <aros/debug.h>
69 #define CATCOMP_ARRAY
70 #include "strings.h"
72 #define CATALOG_NAME "System/Tools/Commodities.catalog"
73 #define CATALOG_VERSION 2
76 /***************************************************************************/
78 UBYTE version[] = "$VER: AutoPoint 0.3 (15.04.2006)";
80 #define ARG_TEMPLATE "CX_PRIORITY=PRI/N/K,LAG/S"
82 #define ARG_PRI 0
83 #define ARG_LAG 1
84 #define NUM_ARGS 2
87 static struct NewBroker nb =
89 NB_VERSION,
90 NULL,
91 NULL,
92 NULL,
93 NBU_NOTIFY | NBU_UNIQUE,
96 NULL,
101 typedef struct _APState
103 CxObj *as_broker;
104 struct MsgPort *as_msgPort;
105 } APState;
108 typedef struct AP
110 struct Window *ai_thisWindow;
111 struct Window *ai_lastActivatedWindow;
112 BOOL ai_mouseHasMoved;
113 } AP;
116 static AP apInfo =
118 NULL,
119 FALSE
123 static struct Catalog *catalog;
125 /************************************************************************************/
127 static void freeResources(APState *as);
128 static BOOL initiate(int argc, char **argv, APState *as);
129 static void autoActivateLag(CxMsg *msg, CxObj *co);
130 static void autoActivate(CxMsg *msg, CxObj *co);
131 static CONST_STRPTR _(ULONG id);
132 static BOOL Locale_Initialize(VOID);
133 static VOID Locale_Deinitialize(VOID);
134 static void showSimpleMessage(CONST_STRPTR msgString);
136 /************************************************************************************/
138 static CONST_STRPTR _(ULONG id)
140 if (LocaleBase != NULL && catalog != NULL)
142 return GetCatalogStr(catalog, id, CatCompArray[id].cca_Str);
144 else
146 return CatCompArray[id].cca_Str;
150 /************************************************************************************/
152 static BOOL Locale_Initialize(VOID)
154 if (LocaleBase != NULL)
156 catalog = OpenCatalog
158 NULL, CATALOG_NAME, OC_Version, CATALOG_VERSION, TAG_DONE
161 else
163 catalog = NULL;
166 return TRUE;
169 /************************************************************************************/
171 static VOID Locale_Deinitialize(VOID)
173 if(LocaleBase != NULL && catalog != NULL) CloseCatalog(catalog);
176 /************************************************************************************/
178 static void showSimpleMessage(CONST_STRPTR msgString)
180 struct EasyStruct easyStruct;
182 easyStruct.es_StructSize = sizeof(easyStruct);
183 easyStruct.es_Flags = 0;
184 easyStruct.es_Title = _(MSG_AUTOPOINT_CXNAME);
185 easyStruct.es_TextFormat = msgString;
186 easyStruct.es_GadgetFormat = _(MSG_OK);
188 if (IntuitionBase != NULL && !Cli() )
190 EasyRequestArgs(NULL, &easyStruct, NULL, NULL);
192 else
194 PutStr(msgString);
198 /************************************************************************************/
200 static BOOL initiate(int argc, char **argv, APState *as)
202 CxObj *customObj;
203 void (*activateFunc)(CxMsg *msg, CxObj *co);
205 memset(as, 0, sizeof(APState));
207 activateFunc = autoActivate;
209 if (Cli() != NULL)
211 struct RDArgs *rda;
212 IPTR *args[] = { NULL, (IPTR)FALSE };
214 rda = ReadArgs(ARG_TEMPLATE, (IPTR *)args, NULL);
216 if (rda != NULL)
218 if (args[ARG_PRI] != NULL)
220 nb.nb_Pri = *args[ARG_PRI];
222 if (args[ARG_LAG])
224 activateFunc = autoActivateLag;
227 FreeArgs(rda);
229 else
231 UBYTE **array = ArgArrayInit(argc, (UBYTE **)argv);
233 nb.nb_Pri = ArgInt(array, "CX_PRIORITY", 0);
235 if (ArgString(array, "LAG", 0))
237 activateFunc = autoActivateLag;
240 ArgArrayDone();
243 nb.nb_Name = _(MSG_AUTOPOINT_CXNAME);
244 nb.nb_Title = _(MSG_AUTOPOINT_CXTITLE);
245 nb.nb_Descr = _(MSG_AUTOPOINT_CXDESCR);
247 as->as_msgPort = CreateMsgPort();
249 if (as->as_msgPort == NULL)
251 showSimpleMessage(_(MSG_CANT_CREATE_MSGPORT));
253 return FALSE;
256 nb.nb_Port = as->as_msgPort;
258 as->as_broker = CxBroker(&nb, 0);
260 if (as->as_broker == NULL)
262 return FALSE;
265 customObj = CxCustom(activateFunc, 0);
267 if (customObj == NULL)
269 showSimpleMessage(_(MSG_CANT_CREATE_CUSTOM));
271 return FALSE;
274 AttachCxObj(as->as_broker, customObj);
275 ActivateCxObj(as->as_broker, TRUE);
277 apInfo.ai_thisWindow = IntuitionBase->ActiveWindow;
279 return TRUE;
282 /************************************************************************************/
284 static void freeResources(APState *as)
286 struct Message *cxm;
288 if (CxBase != NULL)
290 if (as->as_broker != NULL)
292 DeleteCxObjAll(as->as_broker);
296 if (as->as_msgPort != NULL)
298 while ((cxm = GetMsg(as->as_msgPort)))
300 ReplyMsg(cxm);
303 DeleteMsgPort(as->as_msgPort);
307 /************************************************************************************/
309 /* Our CxCustom() function that is invoked everytime an imputevent is
310 routed to our broker */
311 static void autoActivateLag(CxMsg *msg, CxObj *co)
313 struct InputEvent *ie = (struct InputEvent *)CxMsgData(msg);
315 if (ie->ie_Class == IECLASS_TIMER)
317 struct Screen *screen;
318 struct Layer *layer;
320 if (IntuitionBase->ActiveWindow != NULL)
322 screen = IntuitionBase->ActiveWindow->WScreen;
324 else
326 screen = IntuitionBase->ActiveScreen;
329 layer = screen ? WhichLayer(&screen->LayerInfo, screen->MouseX, screen->MouseY) : NULL;
331 apInfo.ai_thisWindow = (layer != NULL) ?
332 (struct Window *)layer->Window : NULL;
334 if (apInfo.ai_mouseHasMoved ||
335 (IntuitionBase->ActiveWindow == apInfo.ai_thisWindow) ||
336 (apInfo.ai_lastActivatedWindow == apInfo.ai_thisWindow))
338 apInfo.ai_mouseHasMoved = FALSE;
339 return;
342 /* Two timer events and mouse hasn't moved in between. */
344 apInfo.ai_mouseHasMoved = FALSE;
346 /* Should be possible to use ActivateWindow(NULL)? Else, we must
347 hack... */
348 if (apInfo.ai_thisWindow != NULL)
350 apInfo.ai_lastActivatedWindow = apInfo.ai_thisWindow;
351 ActivateWindow(apInfo.ai_thisWindow);
354 if (apInfo.ai_thisWindow != NULL)
356 D(bug("Activated window %s\n", apInfo.ai_thisWindow->Title));
358 else
360 D(bug("No window active. %s\n",
361 IntuitionBase->ActiveWindow->Title));
364 return;
367 if (ie->ie_Class == IECLASS_RAWMOUSE)
369 if (ie->ie_Code == IECODE_NOBUTTON)
371 apInfo.ai_mouseHasMoved = TRUE;
376 /************************************************************************************/
378 static void autoActivate(CxMsg *msg, CxObj *co)
380 struct InputEvent *ie = (struct InputEvent *)CxMsgData(msg);
382 if (ie->ie_Class == IECLASS_RAWMOUSE)
384 struct Screen *screen;
385 struct Layer *layer;
387 if (ie->ie_Code != IECODE_NOBUTTON)
389 return;
392 if (IntuitionBase->ActiveWindow != NULL)
394 screen = IntuitionBase->ActiveWindow->WScreen;
396 else
398 screen = IntuitionBase->ActiveScreen;
401 layer = screen ? WhichLayer(&screen->LayerInfo, screen->MouseX, screen->MouseY) : NULL;
403 apInfo.ai_thisWindow = (layer != NULL) ?
404 (struct Window *)layer->Window : NULL;
406 if (apInfo.ai_thisWindow != NULL &&
407 apInfo.ai_thisWindow != IntuitionBase->ActiveWindow)
409 ActivateWindow(apInfo.ai_thisWindow);
410 D(bug("Activated window %s\n", apInfo.ai_thisWindow->Title));
413 return;
417 /************************************************************************************/
419 /* React on command messages sent by commodities.library */
420 static void handleCx(APState *as)
422 CxMsg *msg;
423 BOOL quit = FALSE;
424 LONG signals;
426 while (!quit)
428 signals = Wait((1 << nb.nb_Port->mp_SigBit) | SIGBREAKF_CTRL_C);
430 if (signals & (1 << nb.nb_Port->mp_SigBit))
432 while ((msg = (CxMsg *)GetMsg(as->as_msgPort)))
434 switch (CxMsgType(msg))
436 case CXM_COMMAND:
437 switch (CxMsgID(msg))
439 case CXCMD_DISABLE:
440 ActivateCxObj(as->as_broker, FALSE);
441 break;
443 case CXCMD_ENABLE:
444 ActivateCxObj(as->as_broker, TRUE);
445 break;
447 case CXCMD_UNIQUE:
448 /* Running the program twice <=> quit */
449 /* Fall through */
451 case CXCMD_KILL:
452 quit = TRUE;
453 break;
455 } /* switch(CxMsgID(msg)) */
457 break;
458 } /* switch (CxMsgType(msg))*/
460 ReplyMsg((struct Message *)msg);
462 } /* while((msg = (CxMsg *)GetMsg(cs->cs_msgPort))) */
465 if (signals & SIGBREAKF_CTRL_C)
467 quit = TRUE;
470 } /* while (!quit) */
473 /************************************************************************************/
475 int main(int argc, char **argv)
477 APState aState;
478 int error = RETURN_OK;
480 if (initiate(argc, argv, &aState))
482 handleCx(&aState);
484 else
486 error = RETURN_FAIL;
489 freeResources(&aState);
491 return error;
494 /************************************************************************************/
496 ADD2INIT(Locale_Initialize, 90);
497 ADD2EXIT(Locale_Deinitialize, 90);