summaryrefslogtreecommitdiffstats
path: root/mbbsd/term.c
blob: 555840d1babae6f1d8507e9209b0706a4bd05a94 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* $Id$ */
#include "bbs.h"

int             tgetent(const char *bp, char *name);
char           *tgetstr(const char *id, char **area);
int             tgetflag(const char *id);
int             tgetnum(const char *id);
int             tputs(const char *str, int affcnt, int (*putc) (int));
char           *tparm(const char *str,...);
char           *tgoto(const char *cap, int col, int row);

static struct termios tty_state, tty_new;

/* ----------------------------------------------------- */
/* basic tty control                                     */
/* ----------------------------------------------------- */
void
init_tty()
{
    if (tcgetattr(1, &tty_state) < 0) {
    syslog(LOG_ERR, "tcgetattr(): %m");
    return;
    }
    memcpy(&tty_new, &tty_state, sizeof(tty_new));
    tty_new.c_lflag &= ~(ICANON | ECHO | ISIG);
    /*
     * tty_new.c_cc[VTIME] = 0; tty_new.c_cc[VMIN] = 1;
     */
    tcsetattr(1, TCSANOW, &tty_new);
    system("stty raw -echo");
}

/* ----------------------------------------------------- */
/* init tty control code                                 */
/* ----------------------------------------------------- */


#define TERMCOMSIZE (40)

#if 0
static char    *outp;
static int     *outlp;

static int
outcf(int ch)
{
    if (*outlp < TERMCOMSIZE) {
    (*outlp)++;
    *outp++ = ch;
    }
    return 0;
}
#endif

static void
term_resize(int sig)
{
    struct winsize  newsize;
    screenline_t   *new_picture;

    signal(SIGWINCH, SIG_IGN);  /* Don't bother me! */
    ioctl(0, TIOCGWINSZ, &newsize);

    /* make sure reasonable size */
    newsize.ws_row = MAX(24, MIN(100, newsize.ws_row));
    newsize.ws_col = MAX(80, MIN(200, newsize.ws_col));

    if (newsize.ws_row > t_lines) {
    new_picture = (screenline_t *) calloc(newsize.ws_row,
                          sizeof(screenline_t));
    if (new_picture == NULL) {
        syslog(LOG_ERR, "calloc(): %m");
        return;
    }
    memcpy(new_picture, big_picture, t_lines * sizeof(screenline_t));
    free(big_picture);
    big_picture = new_picture;
    }
    t_lines = newsize.ws_row;
    t_columns = newsize.ws_col;
    scr_lns = t_lines;  /* XXX: scr_lns 跟 t_lines 有什麼不同, 為何分成兩個 */
    b_lines = t_lines - 1;
    p_lines = t_lines - 4;

    signal(SIGWINCH, term_resize);
}

int
term_init()
{
    signal(SIGWINCH, term_resize);
    return YEA;
}

char            term_buf[32];

void
do_move(int destcol, int destline)
{
    char            buf[16], *p;

    snprintf(buf, sizeof(buf), "\33[%d;%dH", destline + 1, destcol + 1);
    for (p = buf; *p; p++)
    ochar(*p);
}

void
save_cursor()
{
    ochar('\33');
    ochar('7');
}

void
restore_cursor()
{
    ochar('\33');
    ochar('8');
}

void
change_scroll_range(int top, int bottom)
{
    char            buf[16], *p;

    snprintf(buf, sizeof(buf), "\33[%d;%dr", top + 1, bottom + 1);
    for (p = buf; *p; p++)
    ochar(*p);
}

void
scroll_forward()
{
    ochar('\33');
    ochar('D');
}