WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Disable blinking cursor in terminal from C program.  (Read 1167 times)

Offline AlexM

  • Newbie
  • *
  • Posts: 3
Disable blinking cursor in terminal from C program.
« on: September 18, 2023, 01:37:25 AM »
Hello!
I need to disable the blinking cursor in the text console from my C program to work directly with the framebuffer.
I'm using this code:
Code: [Select]
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/kd.h>
#include <unistd.h>
#include <stdio.h>

//...
char *tty_n;
int console_fd;

if ((tty_n = ttyname(0)) == NULL){
    fprintf(stderr,"ttyname() error.\n");
    exit(1);
}

console_fd = open(tty_n, O_RDWR);

if (!console_fd) {
    fprintf(stderr,"Could not open console.\n");
    exit(1);
}

if (ioctl( console_fd, KDSETMODE, KD_GRAPHICS))
{
fprintf(stderr,"Could not set console to KD_GRAPHICS mode.\n");
exit(1);
}

close(console_fd);
//...
It works on Tiny Core with TC.tcz installed, but does not work on minimal piCore. The error is returned by ioctl( console_fd, KDSETMODE, KD_GRAPHICS).
Installing TC.tcz increases the loading time, which I would like to avoid and I do not need graphical desctop. But I don’t have enough knowledge of which component is missing for the code to work and whether it can be installed separately from TC.tcz.
Perhaps there is some other way to solve this problem?
OS version piCore 13, board - Raspberry Pi Zero W.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11256
Re: Disable blinking cursor in terminal from C program.
« Reply #1 on: September 18, 2023, 05:32:01 AM »
Hi AlexM
Welcome to the forum.

Try this:
Code: [Select]
system("echo -e \e[?25l");

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11256
Re: Disable blinking cursor in terminal from C program.
« Reply #2 on: September 18, 2023, 06:01:36 AM »
Hi AlexM

Sorry, I forgot to include  stdlib.h.

Hide.c:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main ()
{

   system("echo -e \e[?25l");

   return(0);
}

Show.c:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main ()
{

   system("echo -e \e[?25h");

   return(0);
}

Compile:
Code: [Select]
gcc Hide.c -o Hide
gcc Show.c -o Show

./Hide  will hide the cursor. This works in both a virtual terminal
as provided by  TC.tcz  as well as the console which you want
to be running in.

./Show  will of course make the cursor visible again.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10969
Re: Disable blinking cursor in terminal from C program.
« Reply #3 on: September 18, 2023, 09:41:29 AM »
If you have a framebuffer available, it could be as simple as a permission error. Check the errno code after that ioctl.
The only barriers that can stop you are the ones you create yourself.

Offline AlexM

  • Newbie
  • *
  • Posts: 3
Re: Disable blinking cursor in terminal from C program.
« Reply #4 on: September 18, 2023, 12:08:18 PM »
Try this:
Code: [Select]
system("echo -e \e[?25l");
Thanks, it work!

If you have a framebuffer available, it could be as simple as a permission error. Check the errno code after that ioctl.
ioctl return error "Bad file descriptor"

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11256
Re: Disable blinking cursor in terminal from C program.
« Reply #5 on: September 18, 2023, 06:09:36 PM »
Hi AlexM

This looks wrong:
Code: [Select]
console_fd = open(tty_n, O_RDWR);

if (!console_fd) {
    fprintf(stderr,"Could not open console.\n");
    exit(1);
}

According to the man page:
Quote
RETURN VALUE

       On success, open(), openat(), and creat() return the new file
       descriptor (a nonnegative integer).  On error, -1 is returned and
       errno is set to indicate the error.
Found here:
https://man7.org/linux/man-pages/man2/open.2.html#RETURN_VALUE

Your error test should probably look something like this:
Code: [Select]
if (console_fd < 0)
{
    printf("Could not open console.\n%s\n", strerror(errno));
    exit(1);
}

By the way, ttyname and ioctl both set  errno  when they fail if you want
a little more detail in your error messages:
https://man7.org/linux/man-pages/man3/ttyname.3.html#RETURN_VALUE
https://man7.org/linux/man-pages/man2/ioctl.2.html#RETURN_VALUE

Offline AlexM

  • Newbie
  • *
  • Posts: 3
Re: Disable blinking cursor in terminal from C program.
« Reply #6 on: September 19, 2023, 11:03:27 AM »
This looks wrong:
Code: [Select]
console_fd = open(tty_n, O_RDWR);

if (!console_fd) {
    fprintf(stderr,"Could not open console.\n");
    exit(1);
}
Yes, you are absolutely right. Problem is in permissions and error code checking.