Original author: Pierre Sarrazin. Source: http://sarrazip.com/dev/ebitor-0.1.1.tar.gz Archive SHA-256: 6911a9d2ee80925622f1d82db1b77710a1f896865e63d29a04dc16489b150e83
1042 lines
28 KiB
C
1042 lines
28 KiB
C
/* ebitdemo.c - Demo of the Ebitor full screen text editor.
|
|
By Pierre Sarrazin <http://sarrazip.com/>
|
|
This file is in the public domain.
|
|
|
|
The OS-9 version of this demo shows how to save the editor buffer to a file.
|
|
*/
|
|
|
|
#include "sg4term.h" /* 32x16 terminal to be used by the editor */
|
|
#include "gimeterm.h" /* 40/64/80x25 terminal to be used by the editor */
|
|
#include "Ebitor.h" /* the editor */
|
|
|
|
#include <assert.h>
|
|
|
|
#ifdef OS9
|
|
|
|
#include "Keyboard.h" /* bcontrol */
|
|
#include "os9term.h"
|
|
|
|
#define ESCAPE_KEY_NAME "F1" /* See os9term_waitEditorKey() in os9term.c. */
|
|
|
|
|
|
// Key codes.
|
|
enum
|
|
{
|
|
KEY_CODE_UP_ARROW = 12,
|
|
KEY_CODE_DOWN_ARROW = 10,
|
|
};
|
|
|
|
Keyboard keyboard;
|
|
|
|
// Keyboard_init() must have been called on 'keyboard'.
|
|
//
|
|
#define inkey() (Keyboard_poll(&keyboard))
|
|
|
|
|
|
byte
|
|
waitKeyOS9(void *cookie)
|
|
{
|
|
// A way to block while waiting, instead of polling repeatedly, would be good
|
|
// to save CPU time for other processes.
|
|
byte key;
|
|
do
|
|
key = inkey();
|
|
while (key == 0);
|
|
return key;
|
|
}
|
|
|
|
|
|
// Returns a screen mode (32, 40, 80), or 0 upon failure to determine the terminal type.
|
|
//
|
|
byte
|
|
determineOS9ScreenMode(void)
|
|
{
|
|
if (os9term_get32x16ScreenBuffer() != NULL) // if 32x16
|
|
return 32;
|
|
byte screenTypeCode;
|
|
byte err = os9term_getGIMEScreenType(&screenTypeCode);
|
|
if (err != 0)
|
|
return 0;
|
|
return screenTypeCode * 40;
|
|
}
|
|
|
|
|
|
#else /* not OS9: */
|
|
|
|
|
|
#define ESCAPE_KEY_NAME "BREAK"
|
|
|
|
|
|
// Key codes.
|
|
enum
|
|
{
|
|
KEY_CODE_UP_ARROW = '^',
|
|
KEY_CODE_DOWN_ARROW = 10,
|
|
};
|
|
|
|
|
|
#endif /* not OS9: */
|
|
|
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
|
|
enum
|
|
{
|
|
BASIC_TIMER_ADDRESS = 0x112, // incremented 60 times per second by Basic IRQ routine
|
|
BASIC_32X16_SCREEN = 0x0400, // address of 32x16 screen buffer
|
|
|
|
// CoCo 3 only:
|
|
GIME_SCREEN_BLOCK = 0x36,
|
|
GIME_SCREEN_BUFFER = 0x6000, // in 64k addressable space; see setupCoCo3MMU()
|
|
GIME_SCREEN_VERT_OFFSET = GIME_SCREEN_BLOCK * 1024, // where screen is in 512k space (abs. addr. / 8)
|
|
GIME_EDITOR_BG_COLOR = 2,
|
|
DEFAULT_TEXT_ATTR = 3 * 8 + GIME_EDITOR_BG_COLOR, // foreground * 8 + background
|
|
INVERTED_TEXT_ATTR = 0 * 8 + 4,
|
|
};
|
|
|
|
|
|
// Function pointers that depend on the screen mode.
|
|
//
|
|
static ebitor_clear clearFunc;
|
|
static ebitor_locateFunc locateFunc;
|
|
static ebitor_printFunc printFunc;
|
|
static ebitor_waitKeyFunc waitKeyFunc;
|
|
static ebitor_getCursorPosFunc getCursorPosFunc;
|
|
static void (*invertColorsFunc)(void);
|
|
static void (*eraseCurrentLineFunc)(void);
|
|
|
|
|
|
// Contains the edited text.
|
|
//
|
|
char textBuffer[600];
|
|
|
|
|
|
typedef union WindowUnion
|
|
{
|
|
#ifdef _COCO_BASIC_
|
|
ebitor_Window sg4;
|
|
gimeterm_Window gime;
|
|
#elif defined(OS9)
|
|
ebitor_Window os9;
|
|
#endif
|
|
} WindowUnion;
|
|
|
|
|
|
#ifdef _COCO_BASIC_
|
|
|
|
|
|
// Sets the screen mode to a CoCo 3 text screen, with 25 rows.
|
|
// Clears the screen to spaces of the background color specified by bgColor.
|
|
// numColumns: 40, 64, 80.
|
|
// bgColor: CLS color number 1..8 for the screen background.
|
|
// borderColor: 0..63 color number.
|
|
//
|
|
static void
|
|
setGimeHiResTextMode(byte numColumns, byte bgColor, byte borderColor)
|
|
{
|
|
byte widthField;
|
|
switch (numColumns)
|
|
{
|
|
default:
|
|
case 40: widthField = 0x04; break;
|
|
case 64: widthField = 0x10; break;
|
|
case 80: widthField = 0x14; break;
|
|
}
|
|
* (byte *) 0xFF90 = 0x4C; // reset CoCo 2 compatible bit
|
|
* (byte *) 0xFF98 = 0x03; // text mode
|
|
* (byte *) 0xFF99 = 0x21 | widthField; // 200 lines per field,
|
|
// width specified by variable,
|
|
// 25 rows,
|
|
// color attributes enabled
|
|
* (byte *) 0xFF9A = borderColor;
|
|
* (word *) 0xFF9D = GIME_SCREEN_VERT_OFFSET;
|
|
|
|
memset16(GIME_SCREEN_BUFFER, 0x2000 | (bgColor - 1), numColumns * (word) 25);
|
|
}
|
|
|
|
|
|
#endif /* _COCO_BASIC_ */
|
|
|
|
|
|
// newStatus: Must be shorter than the terminal width.
|
|
// Does not move the terminal cursor.
|
|
//
|
|
static void
|
|
setStatus(const ebitor_Window *window, const char *newStatus)
|
|
{
|
|
assert(getCursorPosFunc < 0xFF00); // check for corruption of data segment
|
|
|
|
byte origCursorCol, origCursorRow;
|
|
(*getCursorPosFunc)(&origCursorCol, &origCursorRow);
|
|
|
|
(*locateFunc)(0, window->height); // cursor deliberately on row below window
|
|
(*invertColorsFunc)();
|
|
(*eraseCurrentLineFunc)(); // also puts cursor at start of line
|
|
(*printFunc)(newStatus);
|
|
(*invertColorsFunc)();
|
|
|
|
(*locateFunc)(origCursorCol, origCursorRow);
|
|
}
|
|
|
|
|
|
static void
|
|
showHelp(const ebitor_Window *window)
|
|
{
|
|
assert(window);
|
|
assert(clearFunc);
|
|
assert(locateFunc);
|
|
assert(printFunc);
|
|
assert(waitKeyFunc);
|
|
(*clearFunc)();
|
|
setStatus(window, "HELP PAGE");
|
|
(*locateFunc)(0, 0);
|
|
(*printFunc)(
|
|
"BACKSPACE IS "
|
|
#ifdef OS9
|
|
"CTRL+LEFT ARROW"
|
|
#else
|
|
"THE CLEAR KEY"
|
|
#endif
|
|
".\r"
|
|
"\r"
|
|
"AFTER PRESSING " ESCAPE_KEY_NAME ":\r"
|
|
"\r"
|
|
"F: FIND SOME TEXT\r"
|
|
"N: FIND THE NEXT OCCURRENCE\r"
|
|
" OF THE SEARCHED TEXT\r"
|
|
"UP/DOWN ARROW: PAGE UP/DOWN\r"
|
|
#ifdef OS9
|
|
"L: LOAD FROM A FILE\r"
|
|
"S: SAVE TO A FILE\r"
|
|
#endif
|
|
"Q: QUIT THIS EDITOR\r"
|
|
"H: SHOW THIS HELP PAGE\r"
|
|
"\r"
|
|
"PRESS A KEY TO CONTINUE: ");
|
|
(*waitKeyFunc)();
|
|
}
|
|
|
|
|
|
// Lets the user type a single line, displayed in inverted colors.
|
|
// Returns an ASCIIZ in dest[].
|
|
// The cursor is restored to its original position upon exit.
|
|
//
|
|
// fieldCol, fieldRow: Relative to the window (fieldRow allowed to be 1 row below window).
|
|
// maxLen: Maximum number of characters to accept;
|
|
// dest[] must have room for a subsequent NUL terminator.
|
|
// Returns TRUE if the user pressed Enter (then dest[] contains the entered ASCIIZ),
|
|
// or FALSE if the user pressed Break.
|
|
//
|
|
static BOOL
|
|
getLine(byte fieldCol, byte fieldRow, byte maxLen, char dest[])
|
|
{
|
|
byte origCursorCol, origCursorRow;
|
|
(*getCursorPosFunc)(&origCursorCol, &origCursorRow);
|
|
|
|
// Print the initial contents of dest[] to the screen field.
|
|
//
|
|
(*invertColorsFunc)();
|
|
(*locateFunc)(fieldCol, fieldRow);
|
|
(*printFunc)(dest);
|
|
|
|
byte writer = (byte) strlen(dest); // offset to end of string
|
|
word key;
|
|
|
|
for (;;)
|
|
{
|
|
key = (*waitKeyFunc)();
|
|
if (key == ebitor_KEY_ESCAPE)
|
|
break;
|
|
if (key == '\r') // Enter
|
|
break;
|
|
switch (key)
|
|
{
|
|
case '\b':
|
|
if (writer == 0)
|
|
break;
|
|
--writer;
|
|
(*locateFunc)(fieldCol + writer, fieldRow);
|
|
(*printFunc)(" ");
|
|
(*locateFunc)(fieldCol + writer, fieldRow);
|
|
break;
|
|
default:
|
|
// Add character if printable and there is still room.
|
|
if (key < 128 && (byte) key > 32 && (byte) key != 127 && writer < maxLen)
|
|
{
|
|
char keyString[2] = { (char) key, '\0' };
|
|
(*printFunc)(keyString);
|
|
dest[writer] = (char) key;
|
|
++writer;
|
|
}
|
|
}
|
|
}
|
|
|
|
(*invertColorsFunc)();
|
|
(*locateFunc)(origCursorCol, origCursorRow);
|
|
|
|
if (key == ebitor_KEY_ESCAPE)
|
|
return FALSE;
|
|
dest[writer] = '\0';
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
static size_t searchStartOffset; // position where handleFind() starts searching for string
|
|
static size_t foundLogicalOffset; // position where handleFind() last found its target string
|
|
|
|
const char findPrompt[] = "FIND: ";
|
|
const char filenamePrompt[] = "FILENAME: ";
|
|
|
|
enum
|
|
{
|
|
MAX_WINDOW_WIDTH = 80, // of all screen modes supported by this demo
|
|
FIND_PROMPT_LEN = sizeof(findPrompt) - 1,
|
|
TARGET_STRING_MAX_LEN = MAX_WINDOW_WIDTH - FIND_PROMPT_LEN - 1, // leaves one column for the cursor
|
|
|
|
#ifdef OS9
|
|
|
|
FILENAME_PROMPT_LEN = sizeof(filenamePrompt) - 1,
|
|
FILENAME_MAX_LEN = MAX_WINDOW_WIDTH - FILENAME_PROMPT_LEN - 1, // leaves one column for the cursor
|
|
|
|
#endif
|
|
};
|
|
|
|
|
|
// These buffers are global so that the previously entered string is remembered
|
|
// the next find Find or Save is used.
|
|
// These buffers are initialized to all NULs before main().
|
|
//
|
|
static char strSearchedFor[TARGET_STRING_MAX_LEN + 1];
|
|
|
|
#ifdef OS9
|
|
|
|
static char filename[FILENAME_MAX_LEN + 1];
|
|
|
|
#endif
|
|
|
|
|
|
// Returns TRUE if this function set a status that should be left visible for the user.
|
|
// Reads findPrompt.
|
|
// Modifies searchStartOffset, strSearchedFor[].
|
|
//
|
|
static BOOL
|
|
handleFind(const ebitor_Window *window, BOOL newFind)
|
|
{
|
|
if (newFind)
|
|
{
|
|
setStatus(window, findPrompt);
|
|
if (!getLine(FIND_PROMPT_LEN, window->height, window->width - FIND_PROMPT_LEN - 1, strSearchedFor))
|
|
return FALSE;
|
|
searchStartOffset = Ebitor_getLogicalCursorOffset();
|
|
}
|
|
else
|
|
{
|
|
size_t curOffset = Ebitor_getLogicalCursorOffset();
|
|
if (curOffset == foundLogicalOffset) // if cursor did not move since last find
|
|
searchStartOffset = foundLogicalOffset + 1; // start "next find" after current find
|
|
else
|
|
searchStartOffset = curOffset; // cursor moved, so find "next" from new cursor pos
|
|
}
|
|
|
|
BOOL found = Ebitor_findStringAndScrollToShow(searchStartOffset, strSearchedFor, &foundLogicalOffset);
|
|
if (found)
|
|
{
|
|
setStatus(window, "FOUND");
|
|
return TRUE;
|
|
}
|
|
|
|
setStatus(window, "NOT FOUND");
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
#ifdef OS9
|
|
|
|
|
|
byte
|
|
getOpenFileSize(unsigned char fd, unsigned long *sizeInBytes)
|
|
{
|
|
unsigned long tmp;
|
|
unsigned char errorCode;
|
|
asm
|
|
{
|
|
pshs u,y ; protect data segment pointer (Y) and frame pointer (U) against OS-9 call;
|
|
; cannot refer to local C vars when U modified
|
|
|
|
lda :fd ; path number
|
|
ldb #2 ; function code 2
|
|
os9 $8D ; I$GetStt
|
|
bcs @error
|
|
clrb
|
|
@error
|
|
tfr u,y ; save low word of file size in Y; free up U
|
|
ldu 2,s ; restore ability to refer to C local vars
|
|
stx :tmp[0] ; store high word of file size in local C var
|
|
sty :tmp[2] ; store low word
|
|
stb :errorCode
|
|
|
|
puls y,u ; restore Y, U already restored (discards the 4 bytes pushed)
|
|
}
|
|
if (errorCode == 0) // if success
|
|
*sizeInBytes = tmp;
|
|
return errorCode;
|
|
}
|
|
|
|
|
|
// filePath: Must end with '\r'.
|
|
// Returns an error code upon failure, 0 upon success.
|
|
// *errorPhase receives:
|
|
// 1 if error happened on file open;
|
|
// 2 on reading;
|
|
// 3 on closing;
|
|
// 4 if the file size could not be obtained;
|
|
// 5 if the file is too large for maxBufferLength.
|
|
// *numBytesRead receives the number of bytes effectively read, upon success.
|
|
// *fileSizeInBytes receives the file size upon success and error codes 2 to 5.
|
|
//
|
|
byte
|
|
loadFileToBuffer(const char *filePath, void *buffer, size_t maxBufferLength,
|
|
unsigned long *fileSizeInBytes, byte *errorPhase, size_t *numBytesRead)
|
|
{
|
|
unsigned char fd, errorCode;
|
|
|
|
// Open the file in read mode.
|
|
asm
|
|
{
|
|
lda #$01 ; A = read mode
|
|
ldx :filePath
|
|
os9 $84 ; I$Open
|
|
bcs @error
|
|
clrb
|
|
sta :fd
|
|
@error
|
|
stb :errorCode
|
|
}
|
|
|
|
if (errorCode != 0) // if open failed
|
|
{
|
|
*errorPhase = 1;
|
|
return errorCode;
|
|
}
|
|
|
|
errorCode = getOpenFileSize(fd, fileSizeInBytes);
|
|
if (errorCode != 0)
|
|
{
|
|
*errorPhase = 4;
|
|
return errorCode;
|
|
}
|
|
|
|
if (*fileSizeInBytes > maxBufferLength)
|
|
{
|
|
*errorPhase = 5;
|
|
return 207; // hack: return E$MemFul
|
|
}
|
|
|
|
// Load bytes from the file to the buffer.
|
|
*numBytesRead = 0;
|
|
asm
|
|
{
|
|
lda :fd ; path number
|
|
ldx :buffer ; address where to store the bytes
|
|
pshs y ; save global data ptr; cannot refer to globals for now
|
|
ldy :maxBufferLength ; number of bytes to read
|
|
os9 $89 ; I$Read
|
|
bcs @error
|
|
clrb
|
|
sty [:numBytesRead] ; store in *numBytesRead
|
|
@error
|
|
stb :errorCode
|
|
puls y ; restore global data ptr; can refer to globals again
|
|
}
|
|
|
|
if (errorCode != 0)
|
|
{
|
|
*errorPhase = 2;
|
|
// Do not return because we need to close the file.
|
|
}
|
|
|
|
unsigned char closeErrorCode;
|
|
asm
|
|
{
|
|
lda :fd ; path number
|
|
os9 $8F ; I$Close
|
|
bcs @error
|
|
clrb
|
|
@error
|
|
stb :closeErrorCode
|
|
}
|
|
|
|
if (errorCode != 0) // if read error
|
|
return errorCode;
|
|
if (closeErrorCode != 0)
|
|
{
|
|
*errorPhase = 3;
|
|
return closeErrorCode;
|
|
}
|
|
|
|
*errorPhase = 0; // success
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Calls setStatus() before returning.
|
|
//
|
|
static void
|
|
loadFromFile(const ebitor_Window *window)
|
|
{
|
|
setStatus(window, filenamePrompt);
|
|
|
|
if (!getLine(FILENAME_PROMPT_LEN, window->height, window->width - FILENAME_PROMPT_LEN - 1, filename))
|
|
{
|
|
setStatus(window, "TEXT NOT LOADED");
|
|
return;
|
|
}
|
|
|
|
size_t filenameLen = strlen(filename);
|
|
filename[filenameLen] = '\r'; // terminator required by OS-9 I$Open
|
|
byte errorPhase;
|
|
unsigned long fileSizeInBytes;
|
|
size_t numBytesRead;
|
|
byte errorCode = loadFileToBuffer(filename, textBuffer, sizeof(textBuffer) - 1,
|
|
&fileSizeInBytes, &errorPhase, &numBytesRead);
|
|
|
|
filename[filenameLen] = '\0'; // restore terminator (required by next call to getLine())
|
|
|
|
char status[MAX_WINDOW_WIDTH];
|
|
|
|
if (errorCode != 0)
|
|
{
|
|
if (errorPhase == 4)
|
|
sprintf(status, "FAILED TO GET FILE SIZE");
|
|
else if (errorPhase == 5)
|
|
sprintf(status, "FILE TOO BIG (%lu VS MAX %u)", fileSizeInBytes, sizeof(textBuffer) - 1);
|
|
else
|
|
sprintf(status, "ERROR #%u WHEN %sING [%lu]",
|
|
errorCode,
|
|
errorPhase == 1 ? "OPEN" : (errorPhase == 2 ? "READ" : "CLOS"),
|
|
fileSizeInBytes);
|
|
}
|
|
else
|
|
{
|
|
sprintf(status, "READ ALL %u CHARACTERS", numBytesRead);
|
|
|
|
// Tell the editor that it now has text.
|
|
Ebitor_setContentLength(numBytesRead);
|
|
}
|
|
|
|
setStatus(window, status);
|
|
}
|
|
|
|
|
|
// filePath: Must end with '\r'.
|
|
// Returns an error code upon failure, 0 upon success.
|
|
// *errorPhase receives:
|
|
// 1 if error happened on file creation;
|
|
// 2 on reading;
|
|
// 3 on closing.
|
|
// *numBytesWritten receives the number of bytes effectively written, upon success.
|
|
//
|
|
byte
|
|
createFileFromBuffer(const char *filePath, const void *buffer, size_t bufferLength, byte *errorPhase, size_t *numBytesWritten)
|
|
{
|
|
unsigned char fd, errorCode;
|
|
asm
|
|
{
|
|
ldd #$0203 ; A = write mode; B = readable, writable, sharable
|
|
ldx :filePath
|
|
os9 $83 ; I$Create
|
|
bcs @error
|
|
clrb
|
|
sta :fd
|
|
@error
|
|
stb :errorCode
|
|
}
|
|
|
|
if (errorCode != 0)
|
|
{
|
|
*errorPhase = 1;
|
|
return errorCode;
|
|
}
|
|
|
|
*numBytesWritten = 0;
|
|
asm
|
|
{
|
|
lda :fd ; path number
|
|
ldx :buffer ; address of bytes to write
|
|
pshs y ; save global data ptr; cannot refer to globals for now
|
|
ldy :bufferLength ; number of bytes to write
|
|
os9 $8A ; I$Write
|
|
bcs @error
|
|
clrb
|
|
sty [:numBytesWritten] ; store in *numBytesWritten
|
|
@error
|
|
stb :errorCode
|
|
puls y ; restore global data ptr; can refer to globals again
|
|
}
|
|
|
|
if (errorCode != 0)
|
|
{
|
|
*errorPhase = 2;
|
|
// Do not return because we need to close the file.
|
|
}
|
|
|
|
unsigned char closeErrorCode;
|
|
asm
|
|
{
|
|
lda :fd ; path number
|
|
os9 $8F ; I$Close
|
|
bcs @error
|
|
clrb
|
|
@error
|
|
stb :closeErrorCode
|
|
}
|
|
|
|
if (errorCode != 0) // if write error
|
|
return errorCode;
|
|
if (closeErrorCode != 0)
|
|
{
|
|
*errorPhase = 3;
|
|
return closeErrorCode;
|
|
}
|
|
|
|
*errorPhase = 0; // success
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Calls setStatus() before returning.
|
|
//
|
|
static void
|
|
saveToFile(const ebitor_Window *window)
|
|
{
|
|
setStatus(window, filenamePrompt);
|
|
|
|
if (!getLine(FILENAME_PROMPT_LEN, window->height, window->width - FILENAME_PROMPT_LEN - 1, filename))
|
|
{
|
|
setStatus(window, "TEXT NOT SAVED");
|
|
return;
|
|
}
|
|
|
|
//FIXME: Check if file already exists, prompt for overwrite.
|
|
|
|
setStatus(window, "SAVING...");
|
|
|
|
size_t saveFilenameLen = strlen(filename);
|
|
filename[saveFilenameLen] = '\r'; // terminator required by OS-9 I$Create
|
|
byte errorPhase;
|
|
size_t numBytesWritten;
|
|
byte errorCode = createFileFromBuffer(filename, textBuffer, Ebitor_getContentLength(), &errorPhase, &numBytesWritten);
|
|
|
|
filename[saveFilenameLen] = '\0'; // restore terminator (required by next call to getLine())
|
|
|
|
char status[MAX_WINDOW_WIDTH];
|
|
|
|
if (errorCode != 0)
|
|
sprintf(status, "SAVE FAILED: ERROR #%u WHEN %sING",
|
|
errorCode,
|
|
errorPhase == 1 ? "CREAT" : (errorPhase == 2 ? "WRIT" : "CLOS"));
|
|
else if (numBytesWritten != Ebitor_getContentLength())
|
|
sprintf(status, "FAILED: WROTE %u of %u", numBytesWritten, Ebitor_getContentLength());
|
|
else
|
|
sprintf(status, "WROTE ALL %u CHARACTERS", numBytesWritten);
|
|
|
|
setStatus(window, status);
|
|
}
|
|
|
|
|
|
#endif /* OS9 */
|
|
|
|
|
|
#ifdef _COCO_BASIC_
|
|
|
|
|
|
static byte
|
|
inkey1(void *cookie)
|
|
{
|
|
return inkey();
|
|
}
|
|
|
|
|
|
#ifndef NDEBUG /* Condition need to avoid warning on failedAssertHandler_gime() not getting called. */
|
|
|
|
static void
|
|
failedAssertHandler_gime(const char *file, int lineno, const char *condition)
|
|
{
|
|
char temp[256];
|
|
sprintf(temp, "[ASSERT FAILED at %s:%d: %s]", file, lineno, condition);
|
|
gimeterm_locate(0, 0);
|
|
gimeterm_print(temp);
|
|
for (;;)
|
|
;
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
// screenMode: Must be 32, 40, 64 or 80.
|
|
// window: Pointer to an object that gets filled with the definition of
|
|
// the text window.
|
|
// Must be the appropriate ebitor_Window field of *windowUnion.
|
|
// Returns TRUE upon success.
|
|
// Fills globals, locateFunc, getCursorPosFunc, etc.
|
|
//
|
|
static BOOL
|
|
initEbitor(byte screenMode, WindowUnion *windowUnion, ebitor_Window *window)
|
|
{
|
|
window->cursorCol = window->cursorRow = 0;
|
|
|
|
// Specify a window for the editor.
|
|
// Excludes the status line, which this demo renders below this window.
|
|
//
|
|
#ifdef _COCO_BASIC_
|
|
byte numRows = screenMode == 32 ? 16 : 25;
|
|
// A border is left unused, to show that the sg4 and gime drivers
|
|
// can use a subwindow of the screen.
|
|
window->width = screenMode - 2;
|
|
window->height = numRows - 3;
|
|
window->leftCol = 1;
|
|
window->topRow = 1;
|
|
|
|
if (screenMode != 32)
|
|
{
|
|
if (!isCoCo3)
|
|
{
|
|
printf("MODE %u NOT SUPPORTED ON A COCO 1 OR 2\n", screenMode);
|
|
return FALSE;
|
|
}
|
|
windowUnion->gime.textAttribute = DEFAULT_TEXT_ATTR;
|
|
windowUnion->gime.invertedTextAttribute = INVERTED_TEXT_ATTR;
|
|
}
|
|
|
|
switch (screenMode)
|
|
{
|
|
case 32:
|
|
width(32); // by SECB
|
|
cls(3);
|
|
sg4term_init((char *) BASIC_32X16_SCREEN, window,
|
|
inkey1, NULL, (word *) BASIC_TIMER_ADDRESS);
|
|
|
|
Ebitor_init(textBuffer, sizeof(textBuffer),
|
|
window->width, window->height,
|
|
clearFunc = sg4term_clear,
|
|
locateFunc = sg4term_locate,
|
|
getCursorPosFunc = sg4term_getCursorPos,
|
|
printFunc = sg4term_print,
|
|
sg4term_printPrefix,
|
|
sg4term_clrtoeol,
|
|
sg4term_scrollWindowUp,
|
|
sg4term_scrollWindowDown,
|
|
waitKeyFunc = sg4term_waitEditorKey);
|
|
invertColorsFunc = sg4term_invertColors;
|
|
eraseCurrentLineFunc = sg4term_eraseCurrentLine;
|
|
return TRUE;
|
|
|
|
case 40:
|
|
case 64:
|
|
case 80:
|
|
setGimeHiResTextMode(screenMode, 8, 7);
|
|
gimeterm_init(GIME_SCREEN_BUFFER, screenMode, &windowUnion->gime,
|
|
inkey1, NULL, (word *) BASIC_TIMER_ADDRESS);
|
|
|
|
_SetFailedAssertHandler(failedAssertHandler_gime);
|
|
|
|
Ebitor_init(textBuffer, sizeof(textBuffer),
|
|
window->width, window->height,
|
|
clearFunc = gimeterm_clear,
|
|
locateFunc = gimeterm_locate,
|
|
getCursorPosFunc = gimeterm_getCursorPos,
|
|
printFunc = gimeterm_print,
|
|
gimeterm_printPrefix,
|
|
gimeterm_clrtoeol,
|
|
gimeterm_scrollWindowUp,
|
|
gimeterm_scrollWindowDown,
|
|
waitKeyFunc = gimeterm_waitEditorKey);
|
|
invertColorsFunc = gimeterm_invertColors;
|
|
eraseCurrentLineFunc = gimeterm_eraseCurrentLine;
|
|
return TRUE;
|
|
|
|
default:
|
|
printf("UNSUPPORTED SCREEN MODE %u\n", screenMode);
|
|
return FALSE;
|
|
}
|
|
|
|
#elif defined(OS9)
|
|
|
|
window->width = screenMode;
|
|
window->height = 24;
|
|
window->leftCol = 0;
|
|
window->topRow = 0;
|
|
|
|
byte err = os9term_init(window->width, window->height,
|
|
window->leftCol, window->topRow,
|
|
waitKeyOS9, NULL);
|
|
if (err != 0)
|
|
{
|
|
printf("Failed to initialize support for this terminal: error #%u.\n", err);
|
|
return FALSE;
|
|
}
|
|
|
|
Ebitor_init(textBuffer, sizeof(textBuffer),
|
|
window->width, window->height,
|
|
clearFunc = os9term_clear,
|
|
locateFunc = os9term_locate,
|
|
getCursorPosFunc = os9term_getCursorPos,
|
|
printFunc = os9term_print,
|
|
os9term_printPrefix,
|
|
os9term_clrtoeol,
|
|
os9term_scrollWindowUp,
|
|
os9term_scrollWindowDown,
|
|
waitKeyFunc = os9term_waitEditorKey);
|
|
invertColorsFunc = os9term_invertColors;
|
|
eraseCurrentLineFunc = os9term_eraseCurrentLine;
|
|
|
|
return TRUE;
|
|
|
|
#endif
|
|
}
|
|
|
|
|
|
// Does not animate a cursor.
|
|
//
|
|
byte
|
|
waitUpperCaseKey(void)
|
|
{
|
|
byte key;
|
|
while ((key = (byte) toupper(inkey())) == 0)
|
|
;
|
|
return key;
|
|
}
|
|
|
|
|
|
BOOL
|
|
askConfirmation(const ebitor_Window *window)
|
|
{
|
|
setStatus(window, "REALLY QUIT? (Y/N)");
|
|
byte key = waitUpperCaseKey();
|
|
return key == 'Y';
|
|
}
|
|
|
|
|
|
BOOL
|
|
work(byte screenMode)
|
|
{
|
|
Ebitor *editor = Ebitor_getInstance();
|
|
|
|
WindowUnion windowUnion;
|
|
|
|
ebitor_Window *window =
|
|
#ifdef _COCO_BASIC_
|
|
(screenMode == 32 ? &windowUnion.sg4 : &windowUnion.gime.window)
|
|
#elif defined(OS9)
|
|
&windowUnion.os9
|
|
#endif
|
|
;
|
|
|
|
if (!initEbitor(screenMode, &windowUnion, window))
|
|
return FALSE;
|
|
|
|
// Main editing loop.
|
|
//
|
|
for (BOOL refresh = TRUE, resetStatus = TRUE; ; )
|
|
{
|
|
if (resetStatus)
|
|
setStatus(window, "PRESS " ESCAPE_KEY_NAME " FOR MENU");
|
|
else
|
|
resetStatus = TRUE;
|
|
|
|
Ebitor_processEditing(refresh);
|
|
|
|
refresh = FALSE;
|
|
|
|
setStatus(window, "F=FIND, Q=QUIT, H=HELP");
|
|
byte key = waitUpperCaseKey();
|
|
if (key == 3) // "escape" key
|
|
continue; // back to processing
|
|
if (key == 'Q')
|
|
{
|
|
if (askConfirmation(window))
|
|
break; // quit
|
|
continue; // back to processing
|
|
}
|
|
switch ((char) key)
|
|
{
|
|
case KEY_CODE_UP_ARROW:
|
|
Ebitor_processUpOrDownArrowsAndRefresh(TRUE, window->height);
|
|
break;
|
|
case KEY_CODE_DOWN_ARROW:
|
|
Ebitor_processUpOrDownArrowsAndRefresh(FALSE, window->height);
|
|
break;
|
|
case 'F':
|
|
resetStatus = ! handleFind(window, TRUE);
|
|
break;
|
|
case 'N':
|
|
resetStatus = ! handleFind(window, FALSE);
|
|
break;
|
|
#ifdef OS9
|
|
case 'L':
|
|
loadFromFile(window);
|
|
refresh = TRUE;
|
|
resetStatus = FALSE;
|
|
break;
|
|
case 'S':
|
|
saveToFile(window);
|
|
resetStatus = FALSE;
|
|
break;
|
|
#endif
|
|
case 'H':
|
|
showHelp(window);
|
|
refresh = TRUE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ebitor_shutdown();
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
#ifdef _COCO_BASIC_
|
|
|
|
|
|
void
|
|
delay(unsigned ticks)
|
|
{
|
|
for (unsigned limit = getTimer() + ticks; getTimer() != limit; )
|
|
;
|
|
}
|
|
|
|
|
|
// Returns 32, 40, 64, 80.
|
|
//
|
|
byte
|
|
chooseScreenMode(void)
|
|
{
|
|
printf("[3] 32X16\n"
|
|
"[4] 40X25\n"
|
|
"[6] 64X25\n"
|
|
"[8] 80X25\n"
|
|
"MODE? ");
|
|
for (;;)
|
|
{
|
|
byte screenMode;
|
|
word key = waitkey(TRUE);
|
|
switch (key)
|
|
{
|
|
case '3': screenMode = 32; break;
|
|
case '4': screenMode = 40; break;
|
|
case '6': screenMode = 64; break;
|
|
case '8': screenMode = 80; break;
|
|
default: key = 0;
|
|
}
|
|
if (key == 0) // if invalid response
|
|
continue;
|
|
printf("%c\n", (char) key);
|
|
delay(30);
|
|
return screenMode;
|
|
}
|
|
}
|
|
|
|
|
|
static void *originalS;
|
|
|
|
|
|
BOOL
|
|
setupCoCo3MMU(void)
|
|
{
|
|
/*
|
|
Assume that PCLEAR 1 has been done before executing this program.
|
|
S is assumed to be in $7Fxx.
|
|
Save S in a global.
|
|
Put S at the end of the graphics page.
|
|
At $6000 (GIME_SCREEN_BUFFER), map in an 8k MMU page that will contain the GIME text screen.
|
|
(This will preserve the page that contains the Basic stack space.)
|
|
Run the editor.
|
|
When it ends, map the original MMU page at $6000, restoring the Basic stack space.
|
|
Load S from the global.
|
|
Quit the program.
|
|
*/
|
|
disableInterrupts();
|
|
asm
|
|
{
|
|
sts :originalS ; uses U register to refer to C local variable
|
|
lds <$BC ; GRPRAM: Start of graphics page
|
|
leas 1536,s ; skip 1 PMODE page (assuming PCLEAR 1)
|
|
ldb #GIME_SCREEN_BLOCK ; block used by SECB to store 40/80-column screen
|
|
stb $FFA3 ; bank at $6000
|
|
}
|
|
enableInterrupts();
|
|
|
|
byte screenMode = chooseScreenMode();
|
|
BOOL success = work(screenMode);
|
|
|
|
disableInterrupts();
|
|
asm
|
|
{
|
|
ldb #$3B ; block that is normally mapped at $6000
|
|
stb $FFA3 ; bank at $6000
|
|
lds :originalS ; uses U register to refer to C local variable
|
|
}
|
|
enableInterrupts();
|
|
|
|
width(32);
|
|
return success;
|
|
}
|
|
|
|
|
|
#endif /* _COCO_BASIC_ */
|
|
|
|
|
|
int
|
|
main()
|
|
{
|
|
BOOL success = FALSE;
|
|
|
|
#ifdef OS9
|
|
// This reference to OS9RawTerminal_init() helps prove that the bcontrol library
|
|
// was compiled with TARGET=os9.
|
|
// If it was compiled for the wrong target platform, then building this program
|
|
// will fail with this linking error:
|
|
// External symbol _OS9RawTerminal_init not found in ebitdemo.o:code
|
|
//
|
|
assert(OS9RawTerminal_init != NULL);
|
|
|
|
//printf("main: %s %s\n", __DATE__, __TIME__);
|
|
Keyboard_init(&keyboard);
|
|
|
|
byte screenMode = determineOS9ScreenMode();
|
|
if (screenMode != 40 && screenMode != 80)
|
|
{
|
|
printf("Error: This terminal type is\n"
|
|
"not supported by this editor.\n");
|
|
return FALSE;
|
|
}
|
|
|
|
success = work(screenMode);
|
|
|
|
#else
|
|
initCoCoSupport(); // initializes 'isCoCo3'
|
|
width(32);
|
|
rgb();
|
|
|
|
if (isCoCo3)
|
|
success = setupCoCo3MMU(); // calls work()
|
|
else
|
|
success = work(32);
|
|
|
|
#endif
|
|
|
|
#ifdef _COCO_BASIC_
|
|
cls(255);
|
|
#elif defined(OS9)
|
|
if (success) // don't clear screen upon error, b/c error msg has just been printed
|
|
putchar(12); // clear whole screen
|
|
Keyboard_shutdown(&keyboard);
|
|
#endif
|
|
|
|
return 0; // returning 1 on OS-9 causes "Unconditional Abort" error message
|
|
}
|