summaryrefslogtreecommitdiffstats
path: root/src/c_init.c
blob: 2c0b7c369d60e6789df9884ca40d834be55686f8 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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 <libft.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);
}

int16_t
    c_set_term_raw(uint8_t mode)
{
    struct termios tios;

	ft_memset(&tios, 0, sizeof(tios));
	tcgetattr(STDIN_FILENO, &tios);
	if (mode)
	{
		tios.c_lflag &= ~(ECHO | ICANON | ISIG);
		tios.c_oflag &= ~(OPOST);
		tios.c_cc[VMIN] = 1;
		tios.c_cc[VTIME] = 0;
	}
	else
	{
		tios.c_lflag |= (ECHO | ICANON | ISIG);
		tios.c_oflag |= (OPOST);
	}
	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
	return (1);
}

int16_t c_process_key(char *buf)
{
    int i;

    /* tputs(tgetstr("cl", NULL), 1, ft_putchar); */
    i = 0;

    if (ft_isprint(buf[0]))
    {
        if (*buf == 'q')
            exit(0);
        write(1, buf, 1);
    }
    else
    {
        ft_printf("escaped char inc,[%c] [%c] [%c]\n", buf[0], buf[1], buf[2]);
        if(strncmp(buf, tgetstr("kr", NULL), ft_strlen(tgetstr("kr", NULL))) == 0)
        {
            tputs(tgetstr("nd", NULL), 1, ft_putchar);
            return (1);
        }
    }
    return (0);
}

int16_t c_init_tcaps(void)
{
    t_caps tcaps;
    char *bp;
    char *term;
    char nread[5];
    int ret;

    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.ke = tgetstr("ke", &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); */

    c_set_term_raw(1);
    c_get_struct(1, &tcaps);
    while (1)
    {

        ft_bzero(nread, 5);
        if (!(read(STDIN_FILENO, nread, 4)))
            return (0);
        ret = c_process_key(nread);
    }
    ft_printf("%d\n", ret);
    return (1);
}