Skip to content

Commit 5d245e5

Browse files
committed
Fix CUP (cursor position) escape code
1 parent 31ca6f5 commit 5d245e5

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

libctru/source/console.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ static void consoleClearLine(char mode) {
178178
}
179179

180180

181+
//---------------------------------------------------------------------------------
182+
static inline void consolePosition(int x, int y) {
183+
//---------------------------------------------------------------------------------
184+
// invalid position
185+
if(x < 0 || y < 0)
186+
return;
187+
188+
// 1-based, but we'll take a 0
189+
if(x < 1)
190+
x = 1;
191+
if(y < 1)
192+
y = 1;
193+
194+
// clip to console edge
195+
if(x > currentConsole->windowWidth)
196+
x = currentConsole->windowWidth;
197+
if(y > currentConsole->windowHeight)
198+
y = currentConsole->windowHeight;
199+
200+
// 1-based adjustment
201+
currentConsole->cursorX = x - 1;
202+
currentConsole->cursorY = y - 1;
203+
}
204+
181205
//---------------------------------------------------------------------------------
182206
ssize_t con_write(struct _reent *r,void *fd,const char *ptr, size_t len) {
183207
//---------------------------------------------------------------------------------
@@ -256,32 +280,28 @@ ssize_t con_write(struct _reent *r,void *fd,const char *ptr, size_t len) {
256280
int x, y;
257281
char c;
258282
if(sscanf(escapeseq,"[%d;%d%c", &y, &x, &c) == 3 && (c == 'f' || c == 'H')) {
259-
currentConsole->cursorX = x;
260-
currentConsole->cursorY = y;
283+
consolePosition(x, y);
261284
escaping = false;
262285
break;
263286
}
264287

265288
x = y = 1;
266289
if(sscanf(escapeseq,"[%d;%c", &y, &c) == 2 && (c == 'f' || c == 'H')) {
267-
currentConsole->cursorX = x;
268-
currentConsole->cursorY = y;
290+
consolePosition(x, y);
269291
escaping = false;
270292
break;
271293
}
272294

273295
x = y = 1;
274296
if(sscanf(escapeseq,"[;%d%c", &x, &c) == 2 && (c == 'f' || c == 'H')) {
275-
currentConsole->cursorX = x;
276-
currentConsole->cursorY = y;
297+
consolePosition(x, y);
277298
escaping = false;
278299
break;
279300
}
280301

281302
x = y = 1;
282303
if(sscanf(escapeseq,"[;%c", &c) == 1 && (c == 'f' || c == 'H')) {
283-
currentConsole->cursorX = x;
284-
currentConsole->cursorY = y;
304+
consolePosition(x, y);
285305
escaping = false;
286306
break;
287307
}

0 commit comments

Comments
 (0)