summaryrefslogtreecommitdiff
path: root/lib/terminal.c
blob: 2b13242288ecf7c217a7abe50f6a1993dee09c05 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include "config.h"
#include "exceptions.h"

/* Quelques fonctions globales pour initialiser le terminal et en faire quelque chose
qui ressemble aux fonctions DOS. En clair, l'interface n'a pas besoin d'attendre que l'utilisateur
tape ENTREE pour lire une touche. De plus, dans ce mode, il n'y a pas d'écho à l'écran. Donc
l'interface peut refaire ce qu'elle veut derrière */

FILE *input;
struct termios initial_settings, new_settings;

void initterm(void)
{
    tcgetattr(fileno(input), &initial_settings);
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VTIME] = 0;
    new_settings.c_lflag &= ~ISIG;

    if (tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) {
	exception(2, _("could not set terminal attributes"));
    }

}

void clearterm(void)
{
    tcsetattr(fileno(input), TCSANOW, &initial_settings);
}

void openterm(void)
{
    if (!(input = fopen("/dev/tty", "r"))) {
	exception(2, _("could not open terminal"));
    }
}