added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / tools / commodities / Blanker.c
blob6dff7fb876170a09cd5e0c29c6e3a12f6ef1f071
1 /*
2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
3 $Id$
4 */
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>
23 #ifdef __AROS__
24 # include <aros/debug.h>
25 #else
26 # undef kprintf
27 # define kprintf(...) (void)0
28 # ifndef __MORPHOS__
29 typedef ULONG IPTR;
30 # endif
31 #endif
34 #include <stdio.h>
35 #include <stdlib.h>
37 #define CATCOMP_ARRAY
38 #include "strings.h"
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"
50 #define ARG_PRI 0
51 #define ARG_SEC 1
52 #define ARG_STARS 2
53 #define NUM_ARGS 3
55 #define MAX_STARS 1000
57 #define CMD_STARTBLANK 1
58 #define CMD_STOPBLANK 2
60 #ifdef __MORPHOS__
61 static void BlankerAction(void);
63 static struct EmulLibEntry BlankerActionEntry =
65 TRAP_LIB,
67 (void (*)(void))BlankerAction
69 #endif
71 static struct NewBroker nb =
73 NB_VERSION,
74 NULL,
75 NULL,
76 NULL,
77 NBU_NOTIFY | NBU_UNIQUE,
80 NULL,
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];
102 static char s[256];
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);
114 else
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
131 else
133 catalog = NULL;
136 return TRUE;
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);
162 else
164 PutStr(msgString);
168 /************************************************************************************/
170 static void FreePens(void)
172 if (pens_allocated)
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;
189 if(msg)
191 puts(msg);
194 if(IntuitionBase)
196 if(win)
198 FreePens();
199 CloseWindow(win);
203 if(CxBase)
206 if(cxbroker)
207 DeleteCxObjAll(cxbroker);
209 if(cxport)
211 while((cxmsg = GetMsg(cxport)))
213 ReplyMsg(cxmsg);
216 DeleteMsgPort(cxport);
220 if(myargs)
221 FreeArgs(myargs);
223 if(actionsig)
224 FreeSignal(actionsig);
226 exit(0);
229 /************************************************************************************/
231 static void DosError(void)
233 Fault(IoErr(), 0, s, sizeof (s));
234 Cleanup(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)
250 if (argc == 0)
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);
256 ArgArrayDone();
258 else
260 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, 0)))
262 DosError();
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];
272 if (num_stars < 0)
274 num_stars = 0;
276 else if (num_stars > MAX_STARS)
278 num_stars = MAX_STARS;
282 /************************************************************************************/
284 #ifdef __MORPHOS__
285 static void BlankerAction(void)
286 #else
287 static void BlankerAction(CxMsg *msg,CxObj *obj)
288 #endif
290 #ifdef __MORPHOS__
291 CxMsg *msg = (CxMsg *)REG_A0;
292 CxObj *obj = (CxObj *)REG_A1;
293 #endif
294 struct InputEvent *ie = (struct InputEvent *)CxMsgData(msg);
295 static ULONG timecounter = 0;
297 if (ie->ie_Class == IECLASS_TIMER)
299 if (disabled)
301 timecounter = 0;
303 else if (!blanked)
305 timecounter++;
307 if(timecounter >= blankwait * 10)
309 actioncmd = CMD_STARTBLANK;
310 Signal(maintask, actionmask);
312 blanked = TRUE;
316 else if ((ie->ie_Class == IECLASS_RAWMOUSE) || (ie->ie_Class == IECLASS_RAWKEY))
318 if (ie->ie_Class != IECLASS_TIMER)
320 timecounter = 0;
322 if (blanked)
324 actioncmd = CMD_STOPBLANK;
325 Signal(maintask, actionmask);
327 blanked = FALSE;
333 /************************************************************************************/
335 static void InitCX(void)
337 if (!(cxport = CreateMsgPort()))
339 Cleanup(_(MSG_CANT_CREATE_MSGPORT));
342 nb.nb_Port = cxport;
344 cxmask = 1L << cxport->mp_SigBit;
346 if (!(cxbroker = CxBroker(&nb, 0)))
348 Cleanup(_(MSG_CANT_CREATE_BROKER));
351 #ifdef __MORPHOS__
352 if (!(cxcust = CxCustom(&BlankerActionEntry, 0)))
353 #else
354 if (!(cxcust = CxCustom(BlankerAction, 0)))
355 #endif
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)
371 static LONG a = 1;
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;
386 LONG i;
388 if(!(screenPtr = LockPubScreen(NULL)))
389 kprintf("Warning: LockPubScreen() failed!\n");
391 win = OpenWindowTags(0, WA_Left, 0,
392 WA_Top, 0,
393 WA_Width, screenPtr->Width,
394 WA_Height, screenPtr->Height,
395 WA_AutoAdjust, TRUE,
396 WA_BackFill, (IPTR)LAYERS_NOBACKFILL,
397 WA_SimpleRefresh, TRUE,
398 WA_Borderless, TRUE,
399 TAG_DONE);
401 if(screenPtr)
402 UnlockPubScreen(NULL, screenPtr);
404 if(win)
406 rp = win->RPort;
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;
433 else
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)
446 FreePens();
447 CloseWindow(win);
448 win = 0;
449 break;
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);
459 #if 0
460 if (y2 == y)
462 for(i = 0; i < num_stars; i++)
464 if (star_y[i] == y2)
466 SetAPen(rp, star_col[i]);
467 WritePixel(rp, star_x[i], y2);
471 } /* if (y2 == y) */
472 #endif
474 } /* for(y2 = y;y2 < scrheight - 1;y2 += stripheight) */
476 visible_sky = y;
478 HandleWin();
480 WaitTOF();
482 } /* for(y = 0;y < scrheight - 1;y++, stripheight++) */
484 } /* if (win) */
485 else
487 showSimpleMessage(_(MSG_CANT_CREATE_WIN));
491 /************************************************************************************/
493 static void HandleWin(void)
495 LONG i;
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)
517 switch(actioncmd)
519 case CMD_STARTBLANK:
520 if (!win) MakeWin();
521 break;
523 case CMD_STOPBLANK:
524 if (win)
526 FreePens();
527 CloseWindow(win);
528 win = 0;
530 break;
534 /************************************************************************************/
536 static void HandleCx(void)
538 CxMsg *msg;
540 while((msg = (CxMsg *)GetMsg(cxport)))
542 switch(CxMsgType(msg))
544 case CXM_COMMAND:
545 switch(CxMsgID(msg))
547 case CXCMD_DISABLE:
548 ActivateCxObj(cxbroker,0L);
549 disabled = TRUE;
550 break;
552 case CXCMD_ENABLE:
553 ActivateCxObj(cxbroker,1L);
554 disabled = FALSE;
555 break;
557 case CXCMD_UNIQUE:
558 case CXCMD_KILL:
559 quitme = TRUE;
560 break;
562 } /* switch(CxMsgID(msg)) */
563 break;
565 } /* switch (CxMsgType(msg))*/
567 ReplyMsg((struct Message *)msg);
569 } /* while((msg = (CxMsg *)GetMsg(cxport))) */
573 /************************************************************************************/
575 static void HandleAll(void)
577 ULONG sigs;
579 while(!quitme)
581 if (win)
583 HandleWin();
584 WaitTOF();
585 sigs = CheckSignal(cxmask | actionmask | SIGBREAKF_CTRL_C);
587 else
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)
604 Init();
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);
611 InitCX();
612 HandleAll();
613 Cleanup(0);
614 return 0;
617 /************************************************************************************/
619 ADD2INIT(Locale_Initialize, 90);
620 ADD2EXIT(Locale_Deinitialize, 90);