File: olpc-kbdshim.c

package info (click to toggle)
olpc-kbdshim 27-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 276 kB
  • ctags: 203
  • sloc: ansic: 2,388; sh: 236; makefile: 93
file content (255 lines) | stat: -rw-r--r-- 5,910 bytes parent folder | download | duplicates (2)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * olpc-kbdshim.c -- special touchpad and keyboard support for XO:
 *  - mouse-based scrolling use the XO grab keys
 *  - touchpad and dpad rotation (to match screen rotation)
 *  - user activity monitoring
 *
 * Copyright (C) 2009,2010,2011, Paul G. Fox, inspired in places
 *  by code from mouseemu, by Colin Leroy and others.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: http://www.gnu.org/licenses
 */

#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include <linux/input.h>

#include "common.h"

/* are we in user-idle state? */
static int idleness;
static int wasidle;

/* input event devices */
static int kbd_fd = -1;
static int tpad_fd = -1;

/* bit array ops */
#define bits2bytes(x) ((((x)-1)/8)+1)
#define test_bit(bit, array) ( array[(bit)/8] & (1 << (bit)%8) )



int
setup_input()
{
    int i;
    int dfd;
    char devname[128];
    unsigned char bit[bits2bytes(EV_MAX)];
    struct input_id id;

    for (i = 0; i < EVENTDEVICES; i++) {

        snprintf(devname, sizeof(devname), "/dev/input/event%d", i);
        if ((dfd = open(devname, O_RDONLY)) < 0)
            continue;

        if (ioctl(dfd, EVIOCGID, &id) < 0) {
            report("failed ioctl EVIOCGID on %d", i);
            close(dfd);
            continue;
        }

        if (ioctl(dfd, EVIOCGBIT(0, EV_MAX), bit) < 0) {
            report("failed ioctl EVIOCGBIT on %d", i);
            close(dfd);
            continue;
        }

        if (kbd_fd < 0 &&
                test_bit(EV_KEY, bit) &&
                test_bit(EV_REP, bit) &&
                is_local_kbd(&id)) {
            kbd_fd = dfd;
            if (ioctl(kbd_fd, EVIOCGRAB, 1) < 0)
                die("ioctl EVIOCGRAB on keyboard");
            report("found keyboard %s (%02x:%02x:%02x)", devname,
                id.bustype, id.vendor, id.product);
            continue;
        }

        if (tpad_fd < 0 &&
                test_bit(EV_REL, bit) &&
                is_local_tpad(&id)) {
            tpad_fd = dfd;
            if (ioctl(tpad_fd, EVIOCGRAB, 1) < 0)
                die("ioctl EVIOCGRAB on touchpad");
            report("found touchpad %s (%02x:%02x:%02x)", devname,
                id.bustype, id.vendor, id.product);
            continue;
        }

        close(dfd);
    }

    if (kbd_fd == -1)
        report("didn't find keyboard");
    if (tpad_fd == -1)
        report("didn't find touchpad");

    return (kbd_fd >= 0 && tpad_fd >= 0);
}

static int
keyboard_event(void)
{
    struct input_event ev[1];

    if (read(kbd_fd, ev, sizeof(ev)) != sizeof(ev))
        die("bad read from keyboard");

    return keyboard_event_worker(ev, 1);
}

static void touchpad_event(void)
{
    struct input_event ev[1];

    if (read(tpad_fd, ev, sizeof(ev)) != sizeof(ev))
        die("bad read from touchpad");

    touchpad_event_worker(ev, 1);
}

static void
indicate_idleness(void)
{
    static char useridle[] = "useridleX";

    useridle[8] = idleness + '1';
    if (idleness < MAXTIMERS) idleness++;

    send_event(useridle);
    wasidle = 1;
}

void
reinit_activity_monitor(void)
{
    wasidle = 1;
    idleness = 0;
}

static void
indicate_activity(void)
{
    if (wasidle)
        send_event("useractive");

    reinit_activity_monitor();
    wasidle = 0;
}

static void
get_command(void)
{
    int n;
    char command[128];
    n = read(fifo_fd, command, sizeof(command)-1);

    if (n < 1) {
        if (n < 0)
            die("read from rotation fifo");
        deinit_fifo();
    }
    command[n] = '\0';

    command_worker(command);
}

void
monitor_input(void)
{
    fd_set inputs, errors;
    int maxfd, r;
    struct timeval tv;
    struct timeval *tvp;
    int pressed;

    indicate_activity();

    maxfd = MAX(fifo_fd, MAX(kbd_fd, tpad_fd));

    while (1) {

        FD_ZERO(&inputs);
        FD_SET(kbd_fd, &inputs);
        FD_SET(tpad_fd, &inputs);
        if (fifo_fd >= 0) FD_SET(fifo_fd, &inputs);

        FD_ZERO(&errors);
        FD_SET(kbd_fd, &errors);
        FD_SET(tpad_fd, &errors);
        if (fifo_fd >= 0) FD_SET(fifo_fd, &errors);

        if (idledeltas[idleness]) {
            tv.tv_sec = idledeltas[idleness];
            tv.tv_usec = 0;
            tvp = &tv;
        } else {
            tvp = 0;
        }

        r = select(maxfd+1, &inputs, NULL, &errors, tvp);
        if (r < 0)
            die("select failed");

        if (r == 0) {
            indicate_idleness();
            continue;
        }

        pressed = 0;

        if (fifo_fd >= 0 && FD_ISSET(fifo_fd, &errors))
            die("select reports error on rotation event fifo");

        if (FD_ISSET(kbd_fd, &errors))
            die("select reports error on keyboard");

        if (FD_ISSET(tpad_fd, &errors))
            die("select reports error on touchpad");

        if (fifo_fd >= 0 && FD_ISSET(fifo_fd, &inputs))
            get_command();

        if (FD_ISSET(kbd_fd, &inputs))
            pressed |= keyboard_event();

        if (FD_ISSET(tpad_fd, &inputs)) {
            touchpad_event();
            pressed = 1;
        }

        if (pressed)
            indicate_activity();

    }

}