Skip to content

Commit

Permalink
Merge pull request #36 from malxau/finalcursor
Browse files Browse the repository at this point in the history
Try to leave the cursor in a sensible location on exit in Win32
  • Loading branch information
mbert authored Jun 10, 2021
2 parents 4692da7 + 19c5624 commit 8c3e850
Showing 1 changed file with 64 additions and 66 deletions.
130 changes: 64 additions & 66 deletions oswin32/tcaphelp.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static ELVBOOL clipopen(ELVBOOL forwrite);
static int clipwrite(CHAR *text, int len);
static int clipread(CHAR *text, int len);
static void clipclose(void);
static void switchcsbi(HANDLE which);
static void switchcsbi(HANDLE which, COORD *curpos);
static int optisttyrows(OPTDESC *opt, OPTVAL *val, CHAR *newval);
static int optisttycols(OPTDESC *opt, OPTVAL *val, CHAR *newval);
static void ttyresize(int rows, int cols);
Expand Down Expand Up @@ -97,21 +97,21 @@ static int optisttyrows (opt, val, newval)
{
int lines;
int cols;
long value = atol ((char *)newval);

if (value < 2)
return -1;
else if (value == val->value.number)
return 0;
switchcsbi(myConsole);
ttyresize(value, -1);
ttysize(&lines, &cols);
long value = atol ((char *)newval);

if (value < 2)
return -1;
else if (value == val->value.number)
return 0;
switchcsbi(myConsole, NULL);
ttyresize(value, -1);
ttysize(&lines, &cols);
o_ttyrows = lines;
o_ttycolumns = cols;
if (windefault)
resized = ElvTrue;

return 1;
return 1;
}

static int optisttycols (opt, val, newval)
Expand All @@ -121,21 +121,21 @@ static int optisttycols (opt, val, newval)
{
int lines;
int cols;
long value = atol ((char *)newval);

if (value < 30)
return -1;
else if (value == val->value.number)
return 0;
switchcsbi(myConsole);
ttyresize(-1, value);
ttysize(&lines, &cols);
long value = atol ((char *)newval);

if (value < 30)
return -1;
else if (value == val->value.number)
return 0;
switchcsbi(myConsole, NULL);
ttyresize(-1, value);
ttysize(&lines, &cols);
o_ttyrows = lines;
o_ttycolumns = cols;
if (windefault)
resized = ElvTrue;

return 1;
return 1;
}
/* This function catches signals, especially SIGINT */
static void catchsig(signo)
Expand Down Expand Up @@ -259,7 +259,7 @@ void ttynormal()
if (useconsole)
{
/* switch back to the original mode */
switchcsbi(outConsole);
switchcsbi(outConsole, NULL);
SetConsoleMode(inConsole, inMode);
SetConsoleMode(outConsole, outMode);
}
Expand All @@ -272,12 +272,11 @@ void ttynormal()


/* This function switches between screen buffers. */
static void switchcsbi(HANDLE which)
static void switchcsbi(HANDLE which, COORD * curpos)
{
int savedHeight;
int savedWidth;
COORD coord; /* coordinates of cursor position */
long dummy;
CONSOLE_SCREEN_BUFFER_INFO localconsinfo;

#if 1
/* if no change, then do nothing */
Expand Down Expand Up @@ -305,16 +304,6 @@ static void switchcsbi(HANDLE which)
{
/* set the title */
SetConsoleTitle(orig_title);

/* use the original attributes */
attr = origattr;

/* move to the bottom row, and clear to EOL */
coord.X = 0;
coord.Y = consinfo.dwSize.Y - 1;
SetConsoleCursorPosition(console, coord);
FillConsoleOutputCharacter(console, ' ', consinfo.dwSize.X, coord, &dummy);
FillConsoleOutputAttribute(console, origattr, consinfo.dwSize.Y, coord, &dummy);
}
else
{
Expand All @@ -326,6 +315,12 @@ static void switchcsbi(HANDLE which)
}
attr = myattr;
}

if (curpos != NULL)
{
GetConsoleScreenBufferInfo(console, &localconsinfo);
*curpos = localconsinfo.dwCursorPosition;
}
}


Expand Down Expand Up @@ -828,7 +823,7 @@ void ttywrite(buf, len)
case 'h':
if (arg[0] == 1)
{
switchcsbi(myConsole);
switchcsbi(myConsole, &coord);
}
else if (arg[0] == 12)
{
Expand All @@ -842,7 +837,7 @@ void ttywrite(buf, len)
case 'l':
if (arg[0] == 1)
{
switchcsbi(outConsole);
switchcsbi(outConsole, &coord);
}
else if (arg[0] == 12)
{
Expand Down Expand Up @@ -1000,19 +995,20 @@ ELVBOOL ttysize(linesptr, colsptr)
int *colsptr; /* where to store the number of columns */
{
SMALL_RECT size;
CONSOLE_SCREEN_BUFFER_INFO localconsinfo;

if (!useconsole)
return ElvFalse;

/* Get the console buffer size */
if (!GetConsoleScreenBufferInfo(myConsole, &consinfo))
if (!GetConsoleScreenBufferInfo(myConsole, &localconsinfo))
return ElvFalse;
#if 0
prevHeight = *linesptr = consinfo.dwSize.Y;
prevWidth = *colsptr = consinfo.dwSize.X;
prevHeight = *linesptr = localconsinfo.dwSize.Y;
prevWidth = *colsptr = localconsinfo.dwSize.X;
#else
*linesptr = consinfo.dwSize.Y;
*colsptr = consinfo.dwSize.X;
*linesptr = localconsinfo.dwSize.Y;
*colsptr = localconsinfo.dwSize.X;
#endif

/* make the window as large as the console buffer */
Expand Down Expand Up @@ -1040,40 +1036,42 @@ static void ttyresize(rows, cols)
int rows;
int cols;
{
SMALL_RECT rect;
COORD coord;
SMALL_RECT rect;
COORD coord;
BOOL b;
CONSOLE_SCREEN_BUFFER_INFO localconsinfo;

if (!useconsole)
return;
if (!useconsole)
return;

#if 0
if (console != myConsole)
SetConsoleActiveScreenBuffer(myConsole);
#endif

coord = GetLargestConsoleWindowSize(myConsole);
GetConsoleScreenBufferInfo(myConsole, &consinfo);
rect.Left = 0;
rect.Top = 0;
if (rows > 0)
rect.Bottom = min(coord.Y, rows) - 1;
else
rect.Bottom = consinfo.srWindow.Bottom - consinfo.srWindow.Top;
if (cols > 0)
rect.Right = min(coord.X, cols) - 1;
else
rect.Right = consinfo.srWindow.Right - consinfo.srWindow.Left;
coord.X = rect.Right + 1;
coord.Y = rect.Bottom + 1;
b = SetConsoleWindowInfo(myConsole, TRUE, &rect);
b = SetConsoleScreenBufferSize(myConsole, coord);
if (curHeight != coord.Y)
curHeight = coord.Y;
if (curWidth != coord.X)
curWidth = coord.X;
coord = GetLargestConsoleWindowSize(myConsole);
GetConsoleScreenBufferInfo(myConsole, &localconsinfo);
rect.Left = 0;
rect.Top = 0;
if (rows > 0)
rect.Bottom = min(coord.Y, rows) - 1;
else
rect.Bottom = localconsinfo.srWindow.Bottom - localconsinfo.srWindow.Top;
if (cols > 0)
rect.Right = min(coord.X, cols) - 1;
else
rect.Right = localconsinfo.srWindow.Right - localconsinfo.srWindow.Left;
coord.X = rect.Right + 1;
coord.Y = rect.Bottom + 1;
b = SetConsoleWindowInfo(myConsole, TRUE, &rect);
b = SetConsoleScreenBufferSize(myConsole, coord);
if (curHeight != coord.Y)
curHeight = coord.Y;
if (curWidth != coord.X)
curWidth = coord.X;
}


/* This function sets the new console title */
static void retitle(GUIWIN *gw, char *name)
{
Expand Down

0 comments on commit 8c3e850

Please sign in to comment.