From 880c871705513bb11a0a1ce0f94e2a1550a7082d Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Tue, 8 Oct 2019 02:08:58 +0000
Subject: [PATCH] New sbbs_t method: cursor_xy(), works with both PETSCII and
 ANSI terminals. JS console.gotoxy() and the GOTOXY @-code now work with
 PETSCII terminals (homes the cursor then moves down and right the appropriate
 number of rows and columns). Bummer I didn't think of this method sooner.

---
 src/sbbs3/atcodes.cpp    |  2 +-
 src/sbbs3/con_out.cpp    | 14 ++++++++++++++
 src/sbbs3/js_console.cpp |  6 +++---
 src/sbbs3/sbbs.h         |  1 +
 4 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/sbbs3/atcodes.cpp b/src/sbbs3/atcodes.cpp
index ef552a3a4f..3ce4bbbdd2 100644
--- a/src/sbbs3/atcodes.cpp
+++ b/src/sbbs3/atcodes.cpp
@@ -1042,7 +1042,7 @@ const char* sbbs_t::atcode(char* sp, char* str, size_t maxlen, long* pmode)
 		tp=strchr(sp,',');
 		if(tp!=NULL) {
 			tp++;
-			ansi_gotoxy(atoi(sp+7),atoi(tp));
+			cursor_xy(atoi(sp+7),atoi(tp));
 		}
 		return(nulstr);
 	}
diff --git a/src/sbbs3/con_out.cpp b/src/sbbs3/con_out.cpp
index 3a127be312..4df88b1a6d 100644
--- a/src/sbbs3/con_out.cpp
+++ b/src/sbbs3/con_out.cpp
@@ -912,6 +912,20 @@ void sbbs_t::cursor_left(int count)
 		column=0;
 }
 
+bool sbbs_t::cursor_xy(int x, int y)
+{
+	long term = term_supports();
+	if(term&ANSI)
+		return ansi_gotoxy(x, y);
+	if(term&PETSCII) {
+		outcom(PETSCII_HOME);
+		cursor_down(y - 1);
+		cursor_right(x - 1);
+		return true;
+	}
+	return false;
+}
+
 void sbbs_t::cleartoeol(void)
 {
 	int i,j;
diff --git a/src/sbbs3/js_console.cpp b/src/sbbs3/js_console.cpp
index f1270b655c..1b02b5e3a7 100644
--- a/src/sbbs3/js_console.cpp
+++ b/src/sbbs3/js_console.cpp
@@ -1623,7 +1623,7 @@ js_gotoxy(JSContext *cx, uintN argc, jsval *arglist)
 	}
 
 	rc=JS_SUSPENDREQUEST(cx);
-	sbbs->ansi_gotoxy(x,y);
+	sbbs->cursor_xy(x,y);
 	JS_RESUMEREQUEST(cx, rc);
     return(JS_TRUE);
 }
@@ -2096,7 +2096,7 @@ static jsSyncMethodSpec js_console_functions[] = {
 	},
 	{"ansi_gotoxy",		js_gotoxy,			1, JSTYPE_ALIAS },
 	{"gotoxy",			js_gotoxy,			1, JSTYPE_VOID,		JSDOCSTR("[x,y] or [object { x,y }]")
-	,JSDOCSTR("move cursor to a specific screen coordinate (ANSI, 1-based values), "
+	,JSDOCSTR("move cursor to a specific screen coordinate (ANSI or PETSCII, 1-based values), "
 	"arguments can be separate x and y coordinates or an object with x and y properties "
 	"(like that returned from <tt>console.getxy()</tt>)")
 	,311
@@ -2129,7 +2129,7 @@ static jsSyncMethodSpec js_console_functions[] = {
 	},
 	{"ansi_getxy",		js_getxy,			0, JSTYPE_ALIAS },
 	{"getxy",			js_getxy,			0, JSTYPE_OBJECT,	JSDOCSTR("")
-	,JSDOCSTR("query the current cursor position on the remote terminal "
+	,JSDOCSTR("query the current cursor position on the remote (ANSI) terminal "
 		"and returns the coordinates as an object (with x and y properties)")
 	,311
 	},
diff --git a/src/sbbs3/sbbs.h b/src/sbbs3/sbbs.h
index e184332582..0e497bbaf2 100644
--- a/src/sbbs3/sbbs.h
+++ b/src/sbbs3/sbbs.h
@@ -744,6 +744,7 @@ public:
 	void	cursor_down(int count=1);
 	void	cursor_left(int count=1);
 	void	cursor_right(int count=1);
+	bool	cursor_xy(int x, int y);
 	void	carriage_return(int count=1);
 	void	line_feed(int count=1);
 	void	newline(int count=1);
-- 
GitLab