2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
6 #include <aros/symbolsets.h>
7 #include <workbench/startup.h>
8 #include <intuition/intuition.h>
9 #include <intuition/intuitionbase.h>
10 #include <graphics/gfx.h>
11 #include <graphics/gfxbase.h>
12 #include <libraries/commodities.h>
13 #include <libraries/locale.h>
14 #include <proto/exec.h>
15 #include <proto/dos.h>
16 #include <proto/intuition.h>
17 #include <proto/graphics.h>
18 #include <proto/commodities.h>
19 #include <proto/alib.h>
20 #include <proto/locale.h>
21 #include <proto/icon.h>
24 # include <aros/debug.h>
27 # define kprintf(...) (void)0
40 #define CATALOG_NAME "System/Tools/Commodities.catalog"
41 #define CATALOG_VERSION 2
44 /************************************************************************************/
46 UBYTE version
[] = "$VER: Blanker 0.11 (15.04.2006)";
48 #define ARG_TEMPLATE "CX_PRIORITY=PRI/N/K,SECONDS=SEC/N/K,STARS=ST/N/K"
55 #define MAX_STARS 1000
57 #define CMD_STARTBLANK 1
58 #define CMD_STOPBLANK 2
61 static void BlankerAction(void);
63 static struct EmulLibEntry BlankerActionEntry
=
67 (void (*)(void))BlankerAction
71 static struct NewBroker nb
=
77 NBU_NOTIFY
| NBU_UNIQUE
,
85 static struct MsgPort
*cxport
;
86 static struct Window
*win
;
87 static struct RastPort
*rp
;
88 static struct ColorMap
*cm
;
89 static struct Task
*maintask
;
91 static struct Catalog
*catalog
;
92 static struct RDArgs
*myargs
;
93 static CxObj
*cxbroker
, *cxcust
;
94 static ULONG cxmask
, actionmask
;
95 static WORD scrwidth
, scrheight
, actioncmd
, visible_sky
;
96 static LONG blackpen
, star1pen
, star2pen
, star3pen
;
97 static WORD num_stars
= 200, blankwait
= 30;
98 static UBYTE actionsig
;
99 static BOOL blanked
, quitme
, disabled
, pens_allocated
;
101 static LONG args
[NUM_ARGS
];
103 static WORD star_x
[MAX_STARS
], star_y
[MAX_STARS
],
104 star_speed
[MAX_STARS
], star_col
[MAX_STARS
];
106 /************************************************************************************/
108 static CONST_STRPTR
_(ULONG id
)
110 if (LocaleBase
!= NULL
&& catalog
!= NULL
)
112 return GetCatalogStr(catalog
, id
, CatCompArray
[id
].cca_Str
);
116 return CatCompArray
[id
].cca_Str
;
120 /************************************************************************************/
122 static BOOL
Locale_Initialize(VOID
)
124 if (LocaleBase
!= NULL
)
126 catalog
= OpenCatalog
128 NULL
, CATALOG_NAME
, OC_Version
, CATALOG_VERSION
, TAG_DONE
139 /************************************************************************************/
141 static VOID
Locale_Deinitialize(VOID
)
143 if(LocaleBase
!= NULL
&& catalog
!= NULL
) CloseCatalog(catalog
);
146 /************************************************************************************/
148 static void showSimpleMessage(CONST_STRPTR msgString
)
150 struct EasyStruct easyStruct
;
152 easyStruct
.es_StructSize
= sizeof(easyStruct
);
153 easyStruct
.es_Flags
= 0;
154 easyStruct
.es_Title
= _(MSG_BLANKER_CXNAME
);
155 easyStruct
.es_TextFormat
= msgString
;
156 easyStruct
.es_GadgetFormat
= _(MSG_OK
);
158 if (IntuitionBase
!= NULL
&& !Cli() )
160 EasyRequestArgs(NULL
, &easyStruct
, NULL
, NULL
);
168 /************************************************************************************/
170 static void FreePens(void)
174 ReleasePen(cm
, blackpen
);
175 ReleasePen(cm
, star1pen
);
176 ReleasePen(cm
, star2pen
);
177 ReleasePen(cm
, star3pen
);
179 pens_allocated
= FALSE
;
183 /************************************************************************************/
185 static void Cleanup(CONST_STRPTR msg
)
187 struct Message
*cxmsg
;
207 DeleteCxObjAll(cxbroker
);
211 while((cxmsg
= GetMsg(cxport
)))
216 DeleteMsgPort(cxport
);
224 FreeSignal(actionsig
);
229 /************************************************************************************/
231 static void DosError(void)
233 Fault(IoErr(), 0, s
, sizeof (s
));
237 /************************************************************************************/
239 static void Init(void)
241 maintask
= FindTask(0);
242 actionsig
= AllocSignal(-1);
243 actionmask
= 1L << actionsig
;
246 /************************************************************************************/
248 static void GetArguments(int argc
, char **argv
)
252 UBYTE
**array
= ArgArrayInit(argc
, (UBYTE
**)argv
);
253 nb
.nb_Pri
= ArgInt(array
, "CX_PRIORITY", 0);
254 blankwait
= ArgInt(array
, "SECONDS" , blankwait
);
255 num_stars
= ArgInt(array
, "STARS" , num_stars
);
260 if (!(myargs
= ReadArgs(ARG_TEMPLATE
, args
, 0)))
265 if (args
[ARG_PRI
]) nb
.nb_Pri
= *(LONG
*)args
[ARG_PRI
];
267 if (args
[ARG_SEC
]) blankwait
= *(LONG
*)args
[ARG_SEC
];
269 if (args
[ARG_STARS
]) num_stars
= *(LONG
*)args
[ARG_STARS
];
276 else if (num_stars
> MAX_STARS
)
278 num_stars
= MAX_STARS
;
282 /************************************************************************************/
285 static void BlankerAction(void)
287 static void BlankerAction(CxMsg
*msg
,CxObj
*obj
)
291 CxMsg
*msg
= (CxMsg
*)REG_A0
;
292 CxObj
*obj
= (CxObj
*)REG_A1
;
294 struct InputEvent
*ie
= (struct InputEvent
*)CxMsgData(msg
);
295 static ULONG timecounter
= 0;
297 if (ie
->ie_Class
== IECLASS_TIMER
)
307 if(timecounter
>= blankwait
* 10)
309 actioncmd
= CMD_STARTBLANK
;
310 Signal(maintask
, actionmask
);
316 else if ((ie
->ie_Class
== IECLASS_RAWMOUSE
) || (ie
->ie_Class
== IECLASS_RAWKEY
))
318 if (ie
->ie_Class
!= IECLASS_TIMER
)
324 actioncmd
= CMD_STOPBLANK
;
325 Signal(maintask
, actionmask
);
333 /************************************************************************************/
335 static void InitCX(void)
337 if (!(cxport
= CreateMsgPort()))
339 Cleanup(_(MSG_CANT_CREATE_MSGPORT
));
344 cxmask
= 1L << cxport
->mp_SigBit
;
346 if (!(cxbroker
= CxBroker(&nb
, 0)))
348 Cleanup(_(MSG_CANT_CREATE_BROKER
));
352 if (!(cxcust
= CxCustom(&BlankerActionEntry
, 0)))
354 if (!(cxcust
= CxCustom(BlankerAction
, 0)))
357 Cleanup(_(MSG_CANT_CREATE_CUSTOM
));
360 AttachCxObj(cxbroker
, cxcust
);
361 ActivateCxObj(cxbroker
, 1);
365 /************************************************************************************/
367 #define MY_RAND_MAX 32767
369 static LONG
myrand(void)
373 return (a
= a
* 1103515245 + 12345) & MY_RAND_MAX
;
376 /************************************************************************************/
378 static void HandleWin(void);
380 /************************************************************************************/
382 static void MakeWin(void)
384 struct Screen
*screenPtr
;
385 WORD y
, y2
, stripheight
= 20;
388 if(!(screenPtr
= LockPubScreen(NULL
)))
389 kprintf("Warning: LockPubScreen() failed!\n");
391 win
= OpenWindowTags(0, WA_Left
, 0,
393 WA_Width
, screenPtr
->Width
,
394 WA_Height
, screenPtr
->Height
,
396 WA_BackFill
, (IPTR
)LAYERS_NOBACKFILL
,
397 WA_SimpleRefresh
, TRUE
,
402 UnlockPubScreen(NULL
, screenPtr
);
408 scrwidth
= win
->Width
;
409 scrheight
= win
->Height
;
411 cm
= win
->WScreen
->ViewPort
.ColorMap
;
413 blackpen
= ObtainBestPenA(cm
, 0x00000000, 0x00000000, 0x00000000, NULL
);
414 star1pen
= ObtainBestPenA(cm
, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, NULL
);
415 star2pen
= ObtainBestPenA(cm
, 0xAAAAAAAA, 0xAAAAAAAA, 0xAAAAAAAA, NULL
);
416 star3pen
= ObtainBestPenA(cm
, 0x88888888, 0x88888888, 0x88888888, NULL
);
418 pens_allocated
= TRUE
;
420 for(i
= 0;i
< num_stars
;i
++)
422 star_x
[i
] = myrand() * scrwidth
/ MY_RAND_MAX
;
423 star_y
[i
] = 1 + (myrand() * (scrheight
- 2)) / MY_RAND_MAX
;
424 star_speed
[i
] = 1 + myrand() * 3 / MY_RAND_MAX
;
425 if (star_speed
[i
] < 2)
427 star_col
[i
] = star3pen
;
429 else if (star_speed
[i
] < 3)
431 star_col
[i
] = star2pen
;
435 star_col
[i
] = star1pen
;
439 SetAPen(rp
, blackpen
);
440 for(y
= 0;y
< scrheight
- 1;y
++, stripheight
++)
442 if (CheckSignal(actionmask
))
444 if (actioncmd
== CMD_STOPBLANK
)
453 for(y2
= y
;y2
< scrheight
- 1;y2
+= stripheight
)
455 ClipBlit(rp
, 0, y2
, rp
, 0, y2
+ 1, scrwidth
, scrheight
- y2
- 1, 192);
456 SetAPen(rp
, blackpen
);
457 RectFill(rp
, 0, y2
, scrwidth
- 1, y2
);
462 for(i
= 0; i
< num_stars
; i
++)
466 SetAPen(rp
, star_col
[i
]);
467 WritePixel(rp
, star_x
[i
], y2
);
474 } /* for(y2 = y;y2 < scrheight - 1;y2 += stripheight) */
482 } /* for(y = 0;y < scrheight - 1;y++, stripheight++) */
487 showSimpleMessage(_(MSG_CANT_CREATE_WIN
));
491 /************************************************************************************/
493 static void HandleWin(void)
497 for(i
= 0; i
< num_stars
;i
++)
499 if (star_y
[i
] <= visible_sky
)
501 SetAPen(rp
, blackpen
);
502 WritePixel(rp
, star_x
[i
], star_y
[i
]);
504 star_x
[i
] -= star_speed
[i
];
505 if (star_x
[i
] < 0) star_x
[i
] += scrwidth
;
507 SetAPen(rp
, star_col
[i
]);
508 WritePixel(rp
, star_x
[i
], star_y
[i
]);
513 /************************************************************************************/
515 static void HandleAction(void)
534 /************************************************************************************/
536 static void HandleCx(void)
540 while((msg
= (CxMsg
*)GetMsg(cxport
)))
542 switch(CxMsgType(msg
))
548 ActivateCxObj(cxbroker
,0L);
553 ActivateCxObj(cxbroker
,1L);
562 } /* switch(CxMsgID(msg)) */
565 } /* switch (CxMsgType(msg))*/
567 ReplyMsg((struct Message
*)msg
);
569 } /* while((msg = (CxMsg *)GetMsg(cxport))) */
573 /************************************************************************************/
575 static void HandleAll(void)
585 sigs
= CheckSignal(cxmask
| actionmask
| SIGBREAKF_CTRL_C
);
589 sigs
= Wait(cxmask
| actionmask
| SIGBREAKF_CTRL_C
);
592 if (sigs
& cxmask
) HandleCx();
593 if (sigs
& actionmask
) HandleAction();
594 if (sigs
& SIGBREAKF_CTRL_C
) quitme
= TRUE
;
596 } /* while(!quitme) */
600 /************************************************************************************/
602 int main(int argc
, char **argv
)
606 nb
.nb_Name
= _(MSG_BLANKER_CXNAME
);
607 nb
.nb_Title
= _(MSG_BLANKER_CXTITLE
);
608 nb
.nb_Descr
= _(MSG_BLANKER_CXDESCR
);
610 GetArguments(argc
, argv
);
617 /************************************************************************************/
619 ADD2INIT(Locale_Initialize
, 90);
620 ADD2EXIT(Locale_Deinitialize
, 90);