revert between 56095 -> 55830 in arch
[AROS.git] / rom / devs / console / consoleclass.c
bloba543c8764fc245c0d4b581048f4afb1a23b3b803
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Base class for console units
6 Lang: english
7 */
9 #include <string.h>
11 #include <proto/intuition.h>
12 #include <proto/utility.h>
13 #include <aros/asmcall.h>
14 #include <devices/conunit.h>
15 #include <intuition/classes.h>
16 #include <intuition/intuition.h>
18 #define SDEBUG 0
19 #define DEBUG 0
20 #include <aros/debug.h>
22 #include "consoleif.h"
23 #include "console_gcc.h"
27 VOID normalizecoords(Object *o, WORD *x_ptr, WORD *y_ptr);
30 Base class for the classes handling standard, charmap and snipmap
31 consoles. This is a whitebox base class (like GADGETCLASS), which means subclasses
32 can have direct access to its instance data.
35 struct consoledata
37 struct intConUnit intunit;
41 #undef ConsoleDevice
42 #define ConsoleDevice ((struct ConsoleBase *)cl->cl_UserData)
44 /*********************
45 ** Console::New() **
46 *********************/
47 static Object *console_new(Class *cl, Object *o, struct opSet *msg)
49 struct Window *win;
50 EnterFunc(bug("Console::New()\n"));
51 struct Library *UtilityBase;
53 UtilityBase = TaggedOpenLibrary(TAGGEDOPEN_UTILITY);
54 if (!UtilityBase)
55 ReturnPtr("Console::New", Object *, NULL);
57 /* Get console window */
58 win =
59 (struct Window *)GetTagData(A_Console_Window, 0, msg->ops_AttrList);
60 CloseLibrary(UtilityBase);
61 if (!win)
63 ReturnPtr("Console::New", Object *, NULL);
66 o = (Object *) DoSuperMethodA(cl, o, (Msg) msg);
67 if (o)
69 struct ConUnit *unit;
70 struct consoledata *data;
71 struct RastPort *rp = win->RPort;
72 WORD i;
74 data = INST_DATA(cl, o);
76 unit = (struct ConUnit *)data;
78 memset(data, 0, sizeof(struct consoledata));
80 /* Initialize the unit fields */
81 unit->cu_Window = win;
83 /* For now one should use only non-proportional fonts */
84 unit->cu_XRSize = rp->Font->tf_XSize;
85 unit->cu_YRSize = rp->Font->tf_YSize;
87 D(bug("cu_XRSize: %d, cu_YRSize: %d\n",
88 unit->cu_XRSize, unit->cu_YRSize));
90 /* Use whole window for console. */
91 unit->cu_XMax =
92 (win->Width - (win->BorderLeft +
93 win->BorderRight)) / unit->cu_XRSize - 1;
94 unit->cu_YMax =
95 (win->Height - (win->BorderTop +
96 win->BorderBottom)) / unit->cu_YRSize - 1;
98 unit->cu_XROrigin =
99 win->Flags & WFLG_GIMMEZEROZERO ? 0 : win->BorderLeft;
100 unit->cu_YROrigin =
101 win->Flags & WFLG_GIMMEZEROZERO ? 0 : win->BorderTop;
103 D(bug("cu_XROrigin: %d, cu_YROrigin: %d\n",
104 unit->cu_XROrigin, unit->cu_YROrigin));
106 unit->cu_XRExtant =
107 unit->cu_XROrigin + (unit->cu_XRSize * (unit->cu_XMax + 1) - 1);
108 unit->cu_YRExtant =
109 unit->cu_YROrigin + (unit->cu_YRSize * (unit->cu_YMax + 1) - 1);
111 unit->cu_XCP = DEF_CHAR_XMIN;
112 unit->cu_YCP = DEF_CHAR_YMIN;
114 unit->cu_XCCP = DEF_CHAR_XMIN;
115 unit->cu_YCCP = DEF_CHAR_YMIN;
117 for (i = 0; i < MAXTABS - 1; i++)
119 unit->cu_TabStops[i] = i * 8;
121 unit->cu_TabStops[i] = (UWORD) - 1;
123 ICU(o)->conFlags = 0UL;
124 ICU(o)->numStoredChars = 0;
126 NEWLIST(&ICU(o)->pasteData);
128 SET_MODE(o, PMB_ASM); /* auto-scroll-mode ON */
129 SET_MODE(o, PMB_AWM); /* auto-wrap-mode ON */
131 /* RKM: Default mode is SET */
132 SET_MODE(o, M_LNM); /* linefeed mode = linefeed+carriage return */
135 ReturnPtr("Console::New", Object *, o);
138 /**********************
139 ** Console::Left() **
140 **********************/
141 static VOID console_left(Class *cl, Object *o, struct P_Console_Left *msg)
143 WORD newx;
145 EnterFunc(bug("Console::Left()\n"));
147 newx = XCCP - msg->Num;
149 if (CHECK_MODE(o, PMB_AWM))
151 WORD scrollcount = 0;
153 while (newx < CHAR_XMIN(o))
155 newx += (CHAR_XMAX(o) + 1);
156 scrollcount++;
159 XCP = XCCP = newx;
161 Console_Up(o, scrollcount);
163 else
165 if (newx < CHAR_XMIN(o))
166 newx = CHAR_XMIN(o);
169 XCP = XCCP = newx; /* XCP always same as XCCP?? */
171 D(bug("XCP=%d, XCCP=%d\n", XCP, XCCP));
173 ReturnVoid("Console::Left");
176 /***********************
177 ** Console::Right() **
178 ***********************/
179 static VOID console_right(Class *cl, Object *o, struct P_Console_Right *msg)
181 WORD newx;
183 EnterFunc(bug("Console::Right()\n"));
185 newx = XCCP + msg->Num;
187 if (CHECK_MODE(o, PMB_AWM))
189 WORD scrollcount = 0;
191 while (newx > CHAR_XMAX(o))
193 newx -= (CHAR_XMAX(o) + 1);
194 scrollcount++;
197 XCP = XCCP = newx;
199 Console_Down(o, scrollcount);
201 else
203 if (newx > CHAR_XMAX(o))
204 newx = CHAR_XMAX(o);
207 XCP = XCCP = newx; /* XCP always same as XCCP?? */
209 D(bug("XCP=%d, XCCP=%d\n", XCP, XCCP));
211 ReturnVoid("Console::Right");
214 /********************
215 ** Console::Up() **
216 ********************/
217 static VOID console_up(Class *cl, Object *o, struct P_Console_Up *msg)
219 EnterFunc(bug("Console::Up(num=%d)\n", msg->Num));
221 YCCP -= msg->Num;
223 if (YCCP < 0)
225 if (CHECK_MODE(o, PMB_ASM))
227 IPTR scroll_param = -YCCP;
229 YCCP = YCP = 0;
230 Console_DoCommand(o, C_SCROLL_DOWN, 1, &scroll_param);
232 else
234 YCCP = 0;
237 YCP = YCCP; /* YCP always same as YCCP ?? */
239 D(bug("New coords: char (%d, %d), gfx (%d, %d)\n",
240 XCCP, YCCP, CP_X(o), CP_Y(o)));
241 ReturnVoid("Console::Up");
246 /**********************
247 ** Console::Down() **
248 **********************/
249 static VOID console_down(Class *cl, Object *o, struct P_Console_Down *msg)
251 EnterFunc(bug("Console::Down(num=%d)\n", msg->Num));
253 YCCP += msg->Num;
255 if (YCCP > CHAR_YMAX(o))
257 if (CHECK_MODE(o, PMB_ASM))
259 IPTR scroll_param = YCCP - CHAR_YMAX(o);
261 YCCP = YCP = CHAR_YMAX(o);
262 Console_DoCommand(o, C_SCROLL_UP, 1, &scroll_param);
264 else
266 YCCP = CHAR_YMAX(o);
269 YCP = YCCP; /* YCP always same as YCCP ?? */
271 D(bug("New coords: char (%d, %d), gfx (%d, %d)\n",
272 XCCP, YCCP, CP_X(o), CP_Y(o)));
273 ReturnVoid("Console::Down");
276 /***************************
277 ** Console::DoCommand() **
278 ***************************/
279 static VOID console_docommand(Class *cl, Object *o,
280 struct P_Console_DoCommand *msg)
282 EnterFunc(bug("Console::DoCommand(cmd=%d)\n", msg->Command));
284 switch (msg->Command)
286 case C_SET_LF_MODE:
287 D(bug("Set LF mode ON\n"));
288 /* LF==LF+CR */
289 /* ICU(o)->conFlags |= CF_LF_MODE_ON ; */
290 SET_MODE(o, M_LNM);
291 break;
293 case C_RESET_LF_MODE:
294 /* LF==LF */
295 D(bug("Set LF mode OFF\n"));
296 /* ICU(o)->conFlags &= ~CF_LF_MODE_ON; */
297 CLEAR_MODE(o, M_LNM);
298 break;
300 case C_SET_AUTOSCROLL_MODE:
301 SET_MODE(o, PMB_ASM);
302 break;
304 case C_RESET_AUTOSCROLL_MODE:
305 CLEAR_MODE(o, PMB_ASM);
306 break;
308 case C_SET_AUTOWRAP_MODE:
309 SET_MODE(o, PMB_AWM);
310 break;
312 case C_RESET_AUTOWRAP_MODE:
313 CLEAR_MODE(o, PMB_AWM);
314 break;
316 case C_SELECT_GRAPHIC_RENDITION:
317 D(bug("Select graphic Rendition, params=%d\n", msg->NumParams));
319 UBYTE i, param;
321 for (i = 0; i < msg->NumParams; i++)
323 param = msg->Params[i];
324 D(bug("param%d=%d\n", i, param));
326 switch (param)
328 case 0:
329 CU(o)->cu_FgPen = 1;
330 CU(o)->cu_BgPen = 0;
331 CU(o)->cu_TxFlags = 0;
332 break;
333 case 1:
334 CU(o)->cu_TxFlags |= CON_TXTFLAGS_BOLD;
335 break;
336 case 2:
337 /* Set "faint" */
338 break;
339 case 3:
340 CU(o)->cu_TxFlags |= CON_TXTFLAGS_ITALIC;
341 break;
342 case 4:
343 CU(o)->cu_TxFlags |= CON_TXTFLAGS_UNDERLINED;
344 break;
345 case 7:
346 CU(o)->cu_TxFlags |= CON_TXTFLAGS_REVERSED;
347 break;
348 case 8:
349 CU(o)->cu_TxFlags |= CON_TXTFLAGS_CONCEALED;
350 break;
351 case 22:
352 CU(o)->cu_TxFlags &= ~CON_TXTFLAGS_BOLD;
353 break;
354 case 23:
355 CU(o)->cu_TxFlags &= ~CON_TXTFLAGS_ITALIC;
356 break;
357 case 24:
358 CU(o)->cu_TxFlags &= ~CON_TXTFLAGS_UNDERLINED;
359 break;
360 case 27:
361 CU(o)->cu_TxFlags &= ~CON_TXTFLAGS_REVERSED;
362 break;
363 case 28:
364 CU(o)->cu_TxFlags &= ~CON_TXTFLAGS_CONCEALED;
365 break;
366 case 30:
367 case 31:
368 case 32:
369 case 33:
370 case 34:
371 case 35:
372 case 36:
373 case 37:
374 CU(o)->cu_FgPen = param - 30;
375 break;
376 case 39:
377 CU(o)->cu_FgPen = 1;
378 break;
380 case 40:
381 case 41:
382 case 42:
383 case 43:
384 case 44:
385 case 45:
386 case 46:
387 case 47:
388 CU(o)->cu_BgPen = param - 40;
389 break;
391 case 49:
392 CU(o)->cu_BgPen = 1;
393 break;
394 } /* switch(param) */
395 } /* for(i = 0; i < msg->NumParams; i++) */
397 break;
399 case C_SET_RAWEVENTS:
400 D(bug("Set Raw Events\n"));
402 UBYTE i, param;
404 for (i = 0; i < msg->NumParams; i++)
406 param = msg->Params[i];
408 if (param <= IECLASS_MAX)
410 SET_RAWEVENT(o, param);
413 } /* for(i = 0; i < msg->NumParams; i++) */
415 break;
417 case C_RESET_RAWEVENTS:
418 D(bug("Set Raw Events\n"));
420 UBYTE i, param;
422 for (i = 0; i < msg->NumParams; i++)
424 param = msg->Params[i];
426 if (param <= IECLASS_MAX)
428 RESET_RAWEVENT(o, param);
431 } /* for(i = 0; i < msg->NumParams; i++) */
433 break;
434 } /* switch (msg->Command) */
436 ReturnVoid("Console::DoCommand");
439 /**********************************
440 ** Console::GetDefaultParams() **
441 **********************************/
442 static VOID console_getdefaultparams(Class *cl, Object *o,
443 struct P_Console_GetDefaultParams *msg)
445 switch (msg->Command)
447 case C_INSERT_CHAR:
448 case C_CURSOR_UP:
449 case C_CURSOR_DOWN:
450 case C_CURSOR_FORWARD:
451 case C_CURSOR_BACKWARD:
452 case C_CURSOR_NEXT_LINE:
453 case C_CURSOR_PREV_LINE:
454 case C_SET_TOP_OFFSET:
455 case C_SET_PAGE_LENGTH:
456 msg->Params[0] = 1;
457 break;
458 case C_CURSOR_POS:
459 msg->Params[0] = YCCP + 1;
460 msg->Params[1] = XCCP + 1;
461 break;
463 /* FIXME: Autodocs state commands in between here, has params RKRM: Devs saye the do not */
464 case C_CURSOR_HTAB:
465 case C_DELETE_CHAR:
466 case C_SCROLL_UP:
467 case C_SCROLL_DOWN:
468 msg->Params[0] = 1;
469 break;
471 case C_CURSOR_TAB_CTRL:
472 msg->Params[0] = 0; /* set tab */
473 break;
475 case C_CURSOR_BACKTAB:
476 msg->Params[0] = 1;
477 break;
479 case C_SELECT_GRAPHIC_RENDITION:
480 /* don't do anything, as params may be in any order */
481 break;
483 case C_SET_RAWEVENTS:
484 /* don't do anything, as params may be in any order */
485 break;
487 case C_RESET_RAWEVENTS:
488 /* don't do anything, as params may be in any order */
489 break;
490 } /* switch (msg->Command) */
492 return;
495 /*******************************
496 ** Console::NewWindowSize() **
497 *******************************/
498 static VOID console_newwindowsize(Class *cl, Object *o,
499 struct P_Console_NewWindowSize *msg)
501 struct ConUnit *unit;
502 struct consoledata *data;
503 struct Window *win;
505 data = INST_DATA(cl, o);
506 unit = (struct ConUnit *)data;
508 win = unit->cu_Window;
510 unit->cu_XMax =
511 (win->Width - win->BorderRight -
512 unit->cu_XROrigin) / unit->cu_XRSize - 1;
513 unit->cu_YMax =
514 (win->Height - win->BorderBottom -
515 unit->cu_YROrigin) / unit->cu_YRSize - 1;
517 unit->cu_XRExtant =
518 unit->cu_XROrigin + (unit->cu_XRSize * (unit->cu_XMax + 1) - 1);
519 unit->cu_YRExtant =
520 unit->cu_YROrigin + (unit->cu_YRSize * (unit->cu_YMax + 1) - 1);
522 if (unit->cu_XCCP > unit->cu_XMax)
523 unit->cu_XCCP = unit->cu_XMax;
524 if (unit->cu_YCCP > unit->cu_YMax)
525 unit->cu_YCCP = unit->cu_YMax;
527 unit->cu_XCP = unit->cu_XCCP;
528 unit->cu_YCP = unit->cu_YCCP;
530 return;
535 /********* Console class dispatcher **********************************/
536 AROS_UFH3S(IPTR, dispatch_consoleclass,
537 AROS_UFHA(Class *, cl, A0),
538 AROS_UFHA(Object *, o, A2), AROS_UFHA(Msg, msg, A1))
540 AROS_USERFUNC_INIT
542 IPTR retval = 0UL;
544 switch (msg->MethodID)
546 case OM_NEW:
547 retval = (IPTR) console_new(cl, o, (struct opSet *)msg);
548 break;
550 case M_Console_Left:
551 console_left(cl, o, (struct P_Console_Left *)msg);
552 break;
554 case M_Console_Right:
555 console_right(cl, o, (struct P_Console_Right *)msg);
556 break;
558 case M_Console_Up:
559 console_up(cl, o, (struct P_Console_Up *)msg);
560 break;
562 case M_Console_Down:
563 console_down(cl, o, (struct P_Console_Down *)msg);
564 break;
566 case M_Console_DoCommand:
567 console_docommand(cl, o, (struct P_Console_DoCommand *)msg);
568 break;
570 case M_Console_GetDefaultParams:
571 console_getdefaultparams(cl, o,
572 (struct P_Console_GetDefaultParams *)msg);
573 break;
575 case M_Console_NewWindowSize:
576 console_newwindowsize(cl, o, (struct P_Console_NewWindowSize *)msg);
577 break;
579 default:
580 retval = DoSuperMethodA(cl, o, msg);
581 break;
583 return (retval);
585 AROS_USERFUNC_EXIT
589 /************************
590 ** normalizecoords() **
591 ************************/
593 /* Normalizes "out of window" coords, so that they
594 fit into the window
597 #define ABS(a) (((a) < 0) ? -(a) : (a))
600 #undef ConsoleDevice
601 #define ConsoleDevice ((struct ConsoleBase *)OCLASS(o)->cl_UserData)
603 /* FIXME: Currently dead code */
604 VOID normalizecoords(Object *o, WORD *x_ptr, WORD *y_ptr)
606 EnterFunc(bug("normalizecoords(o=%p, x=%d, y=%d)\n",
607 o, *x_ptr, *y_ptr));
609 if (*x_ptr > CU(o)->cu_XMax) /* charpos too far to the right */
611 D(bug("Pos right of window\n"));
612 /* Increase y */
613 Console_Down(o, *x_ptr / CHAR_XMAX(o));
615 /* Normalize x */
616 *x_ptr = *x_ptr % (CHAR_XMAX(o) - CHAR_XMIN(o));
618 else if (*x_ptr < CHAR_XMIN(o))
620 D(bug("Pos left of window\n"));
622 /* Decrease y */
623 Console_Up(o, ABS(*x_ptr) / CHAR_XMAX(o) - CHAR_XMIN(o));
625 /* Normalize Z */
626 *x_ptr = *x_ptr % (CHAR_XMAX(o) - CHAR_XMIN(o));
629 if (*y_ptr > CHAR_YMAX(o)) /* pos below window bounds */
632 *y_ptr = CHAR_YMAX(o);
634 else if (*y_ptr < CHAR_YMIN(o)) /* pos above window bounds */
636 UBYTE scroll_param = CHAR_YMIN(o) - *y_ptr;
638 D(bug("Pos above window\n"));
640 Console_DoCommand(o, C_SCROLL_DOWN, 1, &scroll_param);
642 *y_ptr = CHAR_YMIN(o);
645 ReturnVoid("normalizecoords");
649 #undef ConsoleDevice
651 Class *makeConsoleClass(struct ConsoleBase *ConsoleDevice)
653 Class *cl;
655 cl = MakeClass(NULL, ROOTCLASS, NULL, sizeof(struct consoledata), 0UL);
656 if (cl)
658 cl->cl_Dispatcher.h_Entry = (APTR) dispatch_consoleclass;
659 cl->cl_Dispatcher.h_SubEntry = NULL;
661 cl->cl_UserData = (IPTR) ConsoleDevice;
663 return cl;
665 return NULL;