summaryrefslogtreecommitdiff
path: root/src/clients/client_display.c
blob: 0f6a183e083bf5f4d7c30cbb083749a2827ea2c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#if defined(__linux__) || defined(__GLIBC__)
#define _DEFAULT_SOURCE 1
#endif

#include "client_display.h"

#include "hybbx/terminal.h"
#include "hybbx/traffic.h"

#include <stdio.h>
#include <unistd.h>

void hybbx_client_display_init(hybbx_client_display_t *disp,
                               const hybbx_traffic_config_t *traffic)
{
    if (disp == NULL) {
        return;
    }

    disp->traffic = traffic != NULL ? traffic : hybbx_traffic_config_get();
    disp->col = 0;
}

static void pace_byte(unsigned baud)
{
    unsigned delay_us;

    if (baud == 0) {
        return;
    }

    delay_us = hybbx_traffic_byte_delay_us(baud);
    if (delay_us > 0) {
        usleep(delay_us);
    }
}

void hybbx_client_display_byte(hybbx_client_display_t *disp, uint8_t byte)
{
    const hybbx_traffic_config_t *cfg;

    if (disp == NULL) {
        return;
    }

    cfg = disp->traffic;
    if (cfg == NULL) {
        fputc((int)byte, stdout);
        fflush(stdout);
        return;
    }

    if (!cfg->ansi && byte == 0x1b) {
        return;
    }

    if (cfg->line_width > 0 && (byte == '\n' || byte == '\r')) {
        disp->col = 0;
    } else if (cfg->line_width > 0 && byte >= 0x20 && byte != 0x7f) {
        disp->col++;
        if (disp->col > cfg->line_width) {
            fputc('\n', stdout);
            disp->col = 1;
        }
    }

    fputc((int)byte, stdout);
    fflush(stdout);

    if (cfg->pace_output) {
        pace_byte(cfg->baud);
    }
}

void hybbx_client_display_write(hybbx_client_display_t *disp,
                                const uint8_t *data, size_t len)
{
    size_t i;

    if (disp == NULL || data == NULL) {
        return;
    }

    for (i = 0; i < len; i++) {
        hybbx_client_display_byte(disp, data[i]);
    }
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com