How to determine terminal cursor visibility?

92 views Asked by At

I am writing application on C++ that can change cursor state via ANSI sequences. I want to restore cursor visibility state after application is done.

You can easily achieve this in Windows:

#include <windows.h>

bool IsCursorVisible() {
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursorInfo;
    GetConsoleCursorInfo(out, &cursorInfo);

    return cursorInfo.bVisible;
}

But with Linux situation becomes different. How can I achieve the same functionality on Linux or is there any cross-platform non-lib solution?

1

There are 1 answers

0
Thomas Dickey On

Some terminals (i.e., xterm and those which copy the feature from xterm) provide an extension to DECRQM which gives the state from DECTCEM.

That has been a feature of xterm since 2010:

implement ANSI and DEC request-mode control sequences. The latter includes the xterm-specific private modes such as the mouse mode. The feature is ifdef'd with the rectangle operations since its decoding overlaps that feature.

For other terminals, consult their documentation.