2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 AutoPoint commodity -- activates windows under the mouse pointer.
8 /******************************************************************************
16 CX_PRIORITY/N/K, LAG/S
20 Workbench:Tools/Commodities
24 Automatically activates the window under the mouse pointer.
28 CX_PRIORITY -- The priority of the commodity
30 LAG -- Wait for the next timer event to activate the window
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>
67 #include <aros/debug.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"
87 static struct NewBroker nb
=
93 NBU_NOTIFY
| NBU_UNIQUE
,
101 typedef struct _APState
104 struct MsgPort
*as_msgPort
;
110 struct Window
*ai_thisWindow
;
111 struct Window
*ai_lastActivatedWindow
;
112 BOOL ai_mouseHasMoved
;
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
);
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
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
);
198 /************************************************************************************/
200 static BOOL
initiate(int argc
, char **argv
, APState
*as
)
203 void (*activateFunc
)(CxMsg
*msg
, CxObj
*co
);
205 memset(as
, 0, sizeof(APState
));
207 activateFunc
= autoActivate
;
212 IPTR
*args
[] = { NULL
, (IPTR
)FALSE
};
214 rda
= ReadArgs(ARG_TEMPLATE
, (IPTR
*)args
, NULL
);
218 if (args
[ARG_PRI
] != NULL
)
220 nb
.nb_Pri
= *args
[ARG_PRI
];
224 activateFunc
= autoActivateLag
;
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
;
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
));
256 nb
.nb_Port
= as
->as_msgPort
;
258 as
->as_broker
= CxBroker(&nb
, 0);
260 if (as
->as_broker
== NULL
)
265 customObj
= CxCustom(activateFunc
, 0);
267 if (customObj
== NULL
)
269 showSimpleMessage(_(MSG_CANT_CREATE_CUSTOM
));
274 AttachCxObj(as
->as_broker
, customObj
);
275 ActivateCxObj(as
->as_broker
, TRUE
);
277 apInfo
.ai_thisWindow
= IntuitionBase
->ActiveWindow
;
282 /************************************************************************************/
284 static void freeResources(APState
*as
)
290 if (as
->as_broker
!= NULL
)
292 DeleteCxObjAll(as
->as_broker
);
296 if (as
->as_msgPort
!= NULL
)
298 while ((cxm
= GetMsg(as
->as_msgPort
)))
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
;
320 if (IntuitionBase
->ActiveWindow
!= NULL
)
322 screen
= IntuitionBase
->ActiveWindow
->WScreen
;
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
;
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
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
));
360 D(bug("No window active. %s\n",
361 IntuitionBase
->ActiveWindow
->Title
));
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
;
387 if (ie
->ie_Code
!= IECODE_NOBUTTON
)
392 if (IntuitionBase
->ActiveWindow
!= NULL
)
394 screen
= IntuitionBase
->ActiveWindow
->WScreen
;
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
));
417 /************************************************************************************/
419 /* React on command messages sent by commodities.library */
420 static void handleCx(APState
*as
)
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
))
437 switch (CxMsgID(msg
))
440 ActivateCxObj(as
->as_broker
, FALSE
);
444 ActivateCxObj(as
->as_broker
, TRUE
);
448 /* Running the program twice <=> quit */
455 } /* switch(CxMsgID(msg)) */
458 } /* switch (CxMsgType(msg))*/
460 ReplyMsg((struct Message
*)msg
);
462 } /* while((msg = (CxMsg *)GetMsg(cs->cs_msgPort))) */
465 if (signals
& SIGBREAKF_CTRL_C
)
470 } /* while (!quit) */
473 /************************************************************************************/
475 int main(int argc
, char **argv
)
478 int error
= RETURN_OK
;
480 if (initiate(argc
, argv
, &aState
))
489 freeResources(&aState
);
494 /************************************************************************************/
496 ADD2INIT(Locale_Initialize
, 90);
497 ADD2EXIT(Locale_Deinitialize
, 90);