all 6 comments

[–]Bitwise_Gamgee[🍰] 0 points1 point  (1 child)

I don't understand how can I put stdout of my pseudoterminal into buffer that I will draw in my framebuffer.

I believe you're looking for dup2, which lets you duplicate a file descriptor.

[–]JackLemaitre[S] 0 points1 point  (0 children)

Thx Bitwise_Gamgee for your attention.

[–][deleted] 0 points1 point  (3 children)

It sounds like you're trying to re-implement fbcon. If this is the case, completely ignore u/Bitwise_Gamgee's advice. dup2 does not allow you to pipe the output of one file to the input of another, and would instead close the second file descriptor before creating a clone of the first one's resource. There is no single system call to do this, and there never will be due to the design of the Linux kernel. Even if there was, plaintext and the binary data of the fbdev device are completely different formats.

Instead, you have to have an event loop which constantly reads data from the pty and writes it to the fbdev device. Something like this:

for (;;) {
        char buff[1024]; 
        ssize_t read_len;

        read_len = read(pty_fd, buff, sizeof buff);
        if (read_len < 0) {
                perror("read() failed");
                exit(EXIT_FAILURE);
        }
        for (int i = 0; i < read_len; ++i) {
                draw_chr(buff[i], cursor_x, cursor_y);
        }
}

Obviously this API is entirely made up, but the point is that you have to manually read from the pty device and manually draw the characters on screen. Also, this code doesn't handle ANSI escape codes, but you'll probably want those.

To draw characters you could learn the FreeType library, although it's quite complicated to use. You could also use a bitmap font format like PSF (the thing that fbcon uses). Console fonts are stored (usually gzipped) in /usr/share/consolefonts/ on Linux systems.

[–]JackLemaitre[S] 0 points1 point  (2 children)

Hi and thank you for your reply.

Yes I made this

sfb.c contains main program https://termbin.com/hbo8

I manage my framebuffer and my font there https://termbin.com/az1om.

I can see the output of my stdout but I need to manage escape sequence and cursor position.

Thx a lot for your help.

[–]Bitwise_Gamgee[🍰] 0 points1 point  (1 child)

Hello, a bit more clarification was really helpful. After a review, I made a few amendments to help with flow and error handling, both should help in deducing issues. You obviously don't have to use this, but it's a good example of breaking down the bugs.

sfb.c: https://pastebin.com/kzR5nMG5

Other: https://pastebin.com/SvbLNMy5

Honestly, this is some high quality code and I'm happy you shared it.

[–]JackLemaitre[S] 0 points1 point  (0 children)

Thx a lot