/* os9term.c - OS-9 terminal functions.
Copyright (C) 2021-2025 Pierre Sarrazin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "os9term.h"
#include "Ebitor_private.h"
struct Ebitor;
static byte os9term_width, os9term_height;
static byte os9term_winLeftCol, os9term_winTopRow;
static byte os9term_cursorCol, os9term_cursorRow; // lower than os9term_winLeftCol, os9term_winTopRow
static byte (*os9term_waitKeyFuncPtr)(void *cookie); // blocks until key available
static void *os9term_waitKeyFuncCookie; // argument passed to sg4term_inkeyFuncPtr
// Changes the OS-9 terminal's working area.
// Sends the CWArea sequence to standard output.
// Source: "NitrOS-9 EOU Level 2 Windowing System Manual".
// Only works in 40- and 80-column modes.
// This puts the OS-9 terminal cursor at the home position of the window.
//
static void
setWorkingAreaExtraHeight(byte increment)
{
byte buf[] = { 0x1B, 0x25, os9term_winLeftCol, os9term_winTopRow, os9term_width, os9term_height + increment };
putstr((char *) buf, sizeof(buf));
}
void
os9term_clear(void)
{
byte c, r;
os9term_getCursorPos(&c, &r);
// Tell OS-9 to work in a window that only contains the text editing area, not the status line.
// This way, the status line does not get cleared.
setWorkingAreaExtraHeight(0);
putchar(12);
// Restore the OS-9 window that includes the status line.
setWorkingAreaExtraHeight(1);
os9term_locate(c, r);
}
asm byte *
os9term_get32x16ScreenBuffer(void)
{
asm
{
pshs y ; protect global variable segment pointer
lda #1 ; path number
ldb #$1C ; SS.AlfaS (Return Alfa Display Status)
os9 $8D ; I$GetStt
bcs @error
tfr x,d ; return screen buffer address in D
bra @done
@error
clra ; return 0 in D
clrb
@done
puls y
}
}
asm byte
os9term_getGIMEScreenType(byte *screenTypeCode)
{
asm
{
lda #1 ; path number
ldb #$93 ; SS.ScTyp: Returns the type of a screen to a calling program
os9 $8D ; I$GetStt
bcs @error
clrb
sta [2,s] ; put code in *screenTypeCode
@error
; Error code returned in B, as per CMOC calling convention.
}
}
byte
os9term_init(byte width, byte height,
byte winLeftCol, byte winTopRow,
byte (*waitKeyFuncPtr)(void *), void *waitKeyFuncCookie)
{
os9term_width = width;
os9term_height = height;
os9term_winLeftCol = winLeftCol;
os9term_winTopRow = winTopRow;
os9term_cursorCol = 0;
os9term_cursorRow = 0;
os9term_waitKeyFuncPtr = waitKeyFuncPtr;
os9term_waitKeyFuncCookie = waitKeyFuncCookie;
return 0; // success
}
void
os9term_locate(byte column, byte row)
{
char str[] = { 2, 32 + column, 32 + row };
putstr(str, sizeof(str));
os9term_cursorCol = column;
os9term_cursorRow = row;
}
void
os9term_getCursorPos(byte *column, byte *row)
{
*column = os9term_cursorCol;
*row = os9term_cursorRow;
}
// Character 13 does a carriage return and line feed AND clears
// the rest of the row it is applied to (with spaces).
// No other control code is supported.
// Updates os9term_cursorCol and os9term_cursorRow.
//
static BOOL
os9term_putchar(int c) //FIXME: Does not check against window height. Should return FALSE. See gimeterm.c.
{
if (c == '\r')
{
os9term_cursorCol = 0;
++os9term_cursorRow;
putchar(4); // clears to end of line
}
else
{
++os9term_cursorCol;
if (os9term_cursorCol >= os9term_width)
{
os9term_cursorCol = 0;
if (os9term_cursorRow < os9term_height - 1)
++os9term_cursorRow;
}
}
putchar((char) c);
return TRUE;
}
BOOL
os9term_print(const char *str)
{
for ( ; *str; ++str)
if (!os9term_putchar(*str))
return FALSE; // reached end of window
return TRUE;
}
BOOL
os9term_printPrefix(const char *str, size_t prefixLength)
{
for (size_t i = 0; i < prefixLength; ++i)
if (!os9term_putchar(str[i]))
return FALSE; // reached end of window
return TRUE;
}
void
os9term_invertColors(void)
{
// Not supported.
}
// Does not move the cursor.
//
void
os9term_clrtoeol(void)
{
for (byte c = os9term_cursorCol; c < os9term_width; ++c)
putchar(' ');
}
void
os9term_scrollWindowUp(byte numRows)
{
if (numRows == 0)
return;
// Tell OS-9 to work in a window that only contains the text editing area, not the status line.
// This way, the status line does not get scrolled up.
setWorkingAreaExtraHeight(0);
// Move cursor to bottom row if not already on that row.
char goBottom[] = { 2, 32, (32 - 1) + os9term_height };
putstr(goBottom, sizeof(goBottom));
for ( ; numRows--; )
putchar(10); // cursor down
// Restore the OS-9 window that includes the status line.
setWorkingAreaExtraHeight(1);
// Restore original cursor position.
char restore[] = { 2, 32 + os9term_cursorCol, 32 + os9term_cursorRow };
putstr(restore, sizeof(restore));
}
void
os9term_scrollWindowDown(byte numRows)
{
if (numRows == 0)
return;
// Tell OS-9 to work in a window that only contains the text editing area, not the status line.
// This way, the status line does not get scrolled down.
// This puts the OS-9 cursor at the home position.
setWorkingAreaExtraHeight(0);
for ( ; numRows--; )
putstr("\x1F\x30", 2); // insert line, scroll the following lines down
// Restore the OS-9 window that includes the status line.
setWorkingAreaExtraHeight(1);
// Restore original cursor position.
char restore[] = { 2, 32 + os9term_cursorCol, 32 + os9term_cursorRow };
putstr(restore, sizeof(restore));
}
void
os9term_eraseCurrentLine(void)
{
putchar(3);
}
word
os9term_waitEditorKey(void)
{
for (;;)
{
byte key = (*os9term_waitKeyFuncPtr)(os9term_waitKeyFuncCookie);
switch (key)
{
case 177: // F1
// N.B.: If 'key' is 123 when pressing F1, it may be because
// the BControl library has been compiled for DECB
// instead of OS-9. In that case, recompile it by
// passing TARGET=os9 to make.
return ebitor_KEY_ESCAPE;
case 8: // left arrow
return ebitor_KEY_ARROW_LEFT;
case 9: // right arrow
return ebitor_KEY_ARROW_RIGHT;
case 12: // up arrow
return ebitor_KEY_ARROW_UP;
case 10: // down arrow
return ebitor_KEY_ARROW_DOWN;
case 16: // Ctrl-Left
return '\b'; // backspace
case 13: // Enter
return '\r';
default:
if (key >= ' ' && key != 127) // if not control char (127 = Delete)
return key;
}
}
}