~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/terminal.c -rw-r--r-- 2.0 KiB
a4ef7623Noah Loomans fix build on linux 2 years ago
                                                                                
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <term.h>
#include <unistd.h>
#include "terminal.h"

static int	ft_putchar(int c)
{
	return (write(STDERR_FILENO, &c, 1));
}

void		terminal_send(char *cmd)
{
	tputs(tgetstr(cmd, NULL), 1, ft_putchar);
}

/*
** Modes changed:
** - ~ECHO: Disable the printing of keypresses into the terminal.
** - ~ICANON: Read input char-by-char instead of line-by-line.
** - VIM = 0: Read a minimum bytes to read to 0 bytes.
** - VTIME = 1: A maximum of 100ms per keypress;
*/

void		terminal_configure(enum e_terminal_configure_action action)
{
	static struct termios	original;
	struct termios			new;

	if (action == TERMINAL_CONFIGURE_SETUP)
	{
		tgetent(NULL, getenv("TERM"));
		terminal_send(TERMINAL_ENABLE_CURSOR_MODE);
		terminal_send(TERMINAL_CURSOR_INVISIBLE);
		tcgetattr(STDIN_FILENO, &original);
		new = original;
		new.c_lflag &= ~(ECHO | ICANON);
		new.c_cc[VMIN] = 0;
		new.c_cc[VTIME] = 1;
		tcsetattr(STDIN_FILENO, TCSAFLUSH, &new);
	}
	else
	{
		tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
		terminal_send(TERMINAL_DISABLE_CURSOR_MODE);
		terminal_send(TERMINAL_CURSOR_VISIBLE);
	}
}