summaryrefslogtreecommitdiffstats
path: root/src/c_init.c
blob: f472f47bfdcb32cf223073883af23c73675f4f69 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   u_init.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>
#include <curses.h>

#include "c_init.h"

t_caps
    *c_get_struct(int mode, t_caps *src)
{
    static t_caps *caps;

    if (mode == 1)
    {
        caps = src;
    }
    return (caps);
}

void c_process_key(t_caps *tcaps) {
    char nread[64] = {0};
    int bread;

    while ((bread = read(STDIN_FILENO, nread, sizeof(nread)) != 1)) {
        if (bread == -1)
        {
            printf("error on read");
            return ;
        }
    }
    /* printf("read : [%s] size : [%lu]\n", nread, strlen(nread)); */
    /* printf("cap  : [%s] size : [%lu]\n", tcaps->kl, strlen(tcaps->kl)); */

    if (*nread == 'q')
        exit(0);
    else if (strncmp(nread, "l", 1) == 0)
    {
        /* tputs(tgoto(tcaps->nd, 1, 1), 1, putchar); */
        write(1, tcaps->nd, strlen(tcaps->nd));
    }
    else if (strncmp(nread, "c", 1) == 0)
        write(1, tcaps->cl, strlen(tcaps->cl));
    else if (strncmp(nread, "j", 1) == 0)
        write(1, tcaps->DO, strlen(tcaps->DO));
    else if (strncmp(nread, "k", 1) == 0)
    {
        write(1, tcaps->up, strlen(tcaps->kl));
        /* tputs(tgoto(tcaps->bc, 1, 1), 1, putchar); */
    }
    else if (strncmp(nread, "h", 1) == 0)
    {
        write(1, tcaps->le, strlen(tcaps->le));
        /* tputs(tgoto(tcaps->bc, 1, 1), 1, putchar); */
    }
    else
        write(1, nread, 1);
}

int16_t c_init_tcaps(void)
{
    t_caps tcaps;
    char *bp;
    char *term;

    bp = NULL;
    term = getenv("TERM");
    if (!tgetent(bp, term))
        return (-1);
    tcaps.cl = tgetstr("cl", &term);
    tcaps.ks = tgetstr("ks", &term);
    tcaps.kl = tgetstr("kl", &term);
    tcaps.kr = tgetstr("kr", &term);
    tcaps.pc = tgetstr("pc", &term);
    tcaps.bc = tgetstr("bc", &term);
    tcaps.up = tgetstr("up", &term);
    tcaps.nd = tgetstr("nd", &term);
    tcaps.le = tgetstr("le", &term);
    tcaps.DO = tgetstr("do", &term);
    tcgetattr(STDIN_FILENO, &tcaps.tios);

    tcaps.tios.c_lflag &= ~(ECHO | ICANON);
    tcaps.tios.c_cc[VMIN] = 0;
    tcaps.tios.c_cc[VTIME] = 1;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcaps.tios);
    c_get_struct(1, &tcaps);
    while (1)
    {
        c_process_key(&tcaps);
    }
    return (1);
}