aboutsummaryrefslogtreecommitdiffstats
path: root/ttytools.c
blob: 1c3824924765c41c229f4c7307c318b74977d649 (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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
#include <time.h>
#include <termios.h>

#include <sys/time.h>

#include <libudev.h>
#include <libinput.h>

#include "ttytools.h"
#include "logind.h"

static struct termios  saved_attributes;
static int             saved_fl;

static struct libinput *ctx;

const struct libinput_interface libinput_if = {
    .open_restricted  = logind_open,
    .close_restricted = logind_close,
};

void tty_raw(void)
{
    struct termios tattr;
    struct udev *udev;

    if (have_logind()) {
        udev = udev_new();
        ctx = libinput_udev_create_context(&libinput_if, NULL, udev);
        libinput_udev_assign_seat(ctx, getenv("XDG_SEAT"));
    } else {
        fcntl(STDIN_FILENO, F_GETFL, &saved_fl);
        tcgetattr (0, &saved_attributes);
        fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
        memcpy(&tattr,&saved_attributes,sizeof(struct termios));
        tattr.c_lflag &= ~(ICANON|ECHO);
        tattr.c_cc[VMIN] = 1;
        tattr.c_cc[VTIME] = 0;
        tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
    }
}

void tty_restore(void)
{
    if (have_logind()) {
        libinput_unref(ctx);
    } else {
        fcntl(STDIN_FILENO, F_SETFL, saved_fl);
        tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
    }
}

int kbd_wait(int timeout)
{
    struct timeval limit;
    fd_set set;
    int fd, rc;

    if (have_logind()) {
        fd = libinput_get_fd(ctx);
    } else {
        fd = STDIN_FILENO;
    }

    FD_ZERO(&set);
    FD_SET(fd, &set);
    limit.tv_sec = timeout;
    limit.tv_usec = 0;
    rc = select(fd + 1, &set, NULL, NULL,
                timeout ? &limit : NULL);
    return rc;
}

void kbd_read(void)
{
    char buf[32];

    if (have_logind()) {
        /* nothing */
    } else {
        read(0, buf, sizeof(buf));
    }
}