summaryrefslogtreecommitdiff
path: root/tools/syndaemon.c
blob: 50b743573bbe2349980e0f08c2478c414d2eec25 (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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Copyright © 2003-2004 Peter Osterlund
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Red Hat
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  Red
 * Hat makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors:
 *      Peter Osterlund (petero2@telia.com)
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/XInput.h>
#ifdef HAVE_X11_EXTENSIONS_RECORD_H
#include <X11/Xproto.h>
#include <X11/extensions/record.h>
#endif                          /* HAVE_X11_EXTENSIONS_RECORD_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/stat.h>

#include "synaptics-properties.h"

enum TouchpadState {
    TouchpadOn = 0,
    TouchpadOff = 1,
    TappingOff = 2
};

static Bool pad_disabled
    /* internal flag, this does not correspond to device state */ ;
static int ignore_modifier_combos;
static int ignore_modifier_keys;
static int background;
static const char *pid_file;
static Display *display;
static XDevice *dev;
static Atom touchpad_off_prop;
static enum TouchpadState previous_state;
static enum TouchpadState disable_state = TouchpadOff;
static int verbose;

#define KEYMAP_SIZE 32
static unsigned char keyboard_mask[KEYMAP_SIZE];

static void
usage(void)
{
    fprintf(stderr,
            "Usage: syndaemon [-i idle-time] [-m poll-delay] [-d] [-t] [-k]\n");
    fprintf(stderr,
            "  -i How many seconds to wait after the last key press before\n");
    fprintf(stderr, "     enabling the touchpad. (default is 2.0s)\n");
    fprintf(stderr, "  -m How many milli-seconds to wait until next poll.\n");
    fprintf(stderr, "     (default is 200ms)\n");
    fprintf(stderr, "  -d Start as a daemon, i.e. in the background.\n");
    fprintf(stderr, "  -p Create a pid file with the specified name.\n");
    fprintf(stderr,
            "  -t Only disable tapping and scrolling, not mouse movements.\n");
    fprintf(stderr,
            "  -k Ignore modifier keys when monitoring keyboard activity.\n");
    fprintf(stderr, "  -K Like -k but also ignore Modifier+Key combos.\n");
    fprintf(stderr, "  -R Use the XRecord extension.\n");
    fprintf(stderr, "  -v Print diagnostic messages.\n");
    exit(1);
}

static void
store_current_touchpad_state(void)
{
    Atom real_type;
    int real_format;
    unsigned long nitems, bytes_after;
    unsigned char *data;

    if ((XGetDeviceProperty(display, dev, touchpad_off_prop, 0, 1, False,
                            XA_INTEGER, &real_type, &real_format, &nitems,
                            &bytes_after, &data) == Success) &&
        (real_type != None)) {
        previous_state = data[0];
    }
}

/**
 * Toggle touchpad enabled/disabled state, decided by value.
 */
static void
toggle_touchpad(Bool enable)
{
    unsigned char data;

    if (pad_disabled && enable) {
        data = previous_state;
        pad_disabled = False;
        if (verbose)
            printf("Enable\n");
    }
    else if (!pad_disabled && !enable &&
             previous_state != disable_state && previous_state != TouchpadOff) {
        store_current_touchpad_state();
        pad_disabled = True;
        data = disable_state;
        if (verbose)
            printf("Disable\n");
    }
    else
        return;

    /* This potentially overwrites a different client's setting, but ... */
    XChangeDeviceProperty(display, dev, touchpad_off_prop, XA_INTEGER, 8,
                          PropModeReplace, &data, 1);
    XFlush(display);
}

static void
signal_handler(int signum)
{
    toggle_touchpad(True);

    if (pid_file)
        unlink(pid_file);
    kill(getpid(), signum);
}

static void
install_signal_handler(void)
{
    static int signals[] = {
        SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
        SIGBUS, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE,
        SIGALRM, SIGTERM,
#ifdef SIGPWR
        SIGPWR
#endif
    };
    int i;
    struct sigaction act;
    sigset_t set;

    sigemptyset(&set);
    act.sa_handler = signal_handler;
    act.sa_mask = set;
#ifdef SA_ONESHOT
    act.sa_flags = SA_ONESHOT;
#else
    act.sa_flags = 0;
#endif

    for (i = 0; i < sizeof(signals) / sizeof(int); i++) {
        if (sigaction(signals[i], &act, NULL) == -1) {
            perror("sigaction");
            exit(2);
        }
    }
}

/**
 * Return non-zero if the keyboard state has changed since the last call.
 */
static int
keyboard_activity(Display * display)
{
    static unsigned char old_key_state[KEYMAP_SIZE];
    unsigned char key_state[KEYMAP_SIZE];
    int i;
    int ret = 0;

    XQueryKeymap(display, (char *) key_state);

    for (i = 0; i < KEYMAP_SIZE; i++) {
        if ((key_state[i] & ~old_key_state[i]) & keyboard_mask[i]) {
            ret = 1;
            break;
        }
    }
    if (ignore_modifier_combos) {
        for (i = 0; i < KEYMAP_SIZE; i++) {
            if (key_state[i] & ~keyboard_mask[i]) {
                ret = 0;
                break;
            }
        }
    }
    for (i = 0; i < KEYMAP_SIZE; i++)
        old_key_state[i] = key_state[i];
    return ret;
}

static double
get_time(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    return tv.tv_sec + tv.tv_usec / 1000000.0;
}

static void
main_loop(Display * display, double idle_time, int poll_delay)
{
    double last_activity = 0.0;
    double current_time;

    keyboard_activity(display);

    for (;;) {
        current_time = get_time();
        if (keyboard_activity(display))
            last_activity = current_time;

        /* If system times goes backwards, touchpad can get locked. Make
         * sure our last activity wasn't in the future and reset if it was. */
        if (last_activity > current_time)
            last_activity = current_time - idle_time - 1;

        if (current_time > last_activity + idle_time) { /* Enable touchpad */
            toggle_touchpad(True);
        }
        else {                  /* Disable touchpad */
            toggle_touchpad(False);
        }

        usleep(poll_delay);
    }
}

static void
clear_bit(unsigned char *ptr, int bit)
{
    int byte_num = bit / 8;
    int bit_num = bit % 8;

    ptr[byte_num] &= ~(1 << bit_num);
}

static void
setup_keyboard_mask(Display * display, int ignore_modifier_keys)
{
    XModifierKeymap *modifiers;
    int i;

    for (i = 0; i < KEYMAP_SIZE; i++)
        keyboard_mask[i] = 0xff;

    if (ignore_modifier_keys) {
        modifiers = XGetModifierMapping(display);
        for (i = 0; i < 8 * modifiers->max_keypermod; i++) {
            KeyCode kc = modifiers->modifiermap[i];

            if (kc != 0)
                clear_bit(keyboard_mask, kc);
        }
        XFreeModifiermap(modifiers);
    }
}

/* ---- the following code is for using the xrecord extension ----- */
#ifdef HAVE_X11_EXTENSIONS_RECORD_H

#define MAX_MODIFIERS 16

/* used for exchanging information with the callback function */
struct xrecord_callback_results {
    XModifierKeymap *modifiers;
    Bool key_event;
    Bool non_modifier_event;
    KeyCode pressed_modifiers[MAX_MODIFIERS];
};

/* test if the xrecord extension is found */
Bool
check_xrecord(Display * display)
{

    Bool found;
    Status status;
    int major_opcode, minor_opcode, first_error;
    int version[2];

    found = XQueryExtension(display,
                            "RECORD",
                            &major_opcode, &minor_opcode, &first_error);

    status = XRecordQueryVersion(display, version, version + 1);
    if (verbose && status) {
        printf("X RECORD extension version %d.%d\n", version[0], version[1]);
    }
    return found;
}

/* called by XRecordProcessReplies() */
void
xrecord_callback(XPointer closure, XRecordInterceptData * recorded_data)
{

    struct xrecord_callback_results *cbres;
    xEvent *xev;
    int nxev;

    cbres = (struct xrecord_callback_results *) closure;

    if (recorded_data->category != XRecordFromServer) {
        XRecordFreeData(recorded_data);
        return;
    }

    nxev = recorded_data->data_len / 8;
    xev = (xEvent *) recorded_data->data;
    while (nxev--) {

        if ((xev->u.u.type == KeyPress) || (xev->u.u.type == KeyRelease)) {
            int i;
            int is_modifier = 0;

            cbres->key_event = 1;       /* remember, a key was pressed or released. */

            /* test if it was a modifier */
            for (i = 0; i < 8 * cbres->modifiers->max_keypermod; i++) {
                KeyCode kc = cbres->modifiers->modifiermap[i];

                if (kc == xev->u.u.detail) {
                    is_modifier = 1;    /* yes, it is a modifier. */
                    break;
                }
            }

            if (is_modifier) {
                if (xev->u.u.type == KeyPress) {
                    for (i = 0; i < MAX_MODIFIERS; ++i)
                        if (!cbres->pressed_modifiers[i]) {
                            cbres->pressed_modifiers[i] = xev->u.u.detail;
                            break;
                        }
                }
                else {          /* KeyRelease */
                    for (i = 0; i < MAX_MODIFIERS; ++i)
                        if (cbres->pressed_modifiers[i] == xev->u.u.detail)
                            cbres->pressed_modifiers[i] = 0;
                }

            }
            else {
                /* remember, a non-modifier was pressed. */
                cbres->non_modifier_event = 1;
            }
        }

        xev++;
    }

    XRecordFreeData(recorded_data);     /* cleanup */
}

static int
is_modifier_pressed(const struct xrecord_callback_results *cbres)
{
    int i;

    for (i = 0; i < MAX_MODIFIERS; ++i)
        if (cbres->pressed_modifiers[i])
            return 1;

    return 0;
}

void
record_main_loop(Display * display, double idle_time)
{

    struct xrecord_callback_results cbres;
    XRecordContext context;
    XRecordClientSpec cspec = XRecordAllClients;
    Display *dpy_data;
    XRecordRange *range;
    int i;

    dpy_data = XOpenDisplay(NULL);      /* we need an additional data connection. */
    range = XRecordAllocRange();

    range->device_events.first = KeyPress;
    range->device_events.last = KeyRelease;

    context = XRecordCreateContext(dpy_data, 0, &cspec, 1, &range, 1);

    XRecordEnableContextAsync(dpy_data, context, xrecord_callback,
                              (XPointer) & cbres);

    cbres.modifiers = XGetModifierMapping(display);
    /* clear list of modifiers */
    for (i = 0; i < MAX_MODIFIERS; ++i)
        cbres.pressed_modifiers[i] = 0;

    while (1) {

        int fd = ConnectionNumber(dpy_data);
        fd_set read_fds;
        int ret;
        int disable_event = 0;
        struct timeval timeout;

        FD_ZERO(&read_fds);
        FD_SET(fd, &read_fds);

        ret = select(fd + 1 /* =(max descriptor in read_fds) + 1 */ ,
                     &read_fds, NULL, NULL,
                     pad_disabled ? &timeout : NULL
                     /* timeout only required for enabling */ );

        if (FD_ISSET(fd, &read_fds)) {

            cbres.key_event = 0;
            cbres.non_modifier_event = 0;

            XRecordProcessReplies(dpy_data);

            /* If there are any events left over, they are in error. Drain them
             * from the connection queue so we don't get stuck. */
            while (XEventsQueued(dpy_data, QueuedAlready) > 0) {
                XEvent event;

                XNextEvent(dpy_data, &event);
                fprintf(stderr, "bad event received, major opcode %d\n",
                        event.type);
            }

            if (!ignore_modifier_keys && cbres.key_event) {
                disable_event = 1;
            }

            if (cbres.non_modifier_event &&
                !(ignore_modifier_combos && is_modifier_pressed(&cbres))) {
                disable_event = 1;
            }
        }

        if (disable_event) {
            /* adjust the enable_time */
            timeout.tv_sec = (int) idle_time;
            timeout.tv_usec = (idle_time - (double) timeout.tv_sec) * 1.e6;

            toggle_touchpad(False);
        }

        if (ret == 0 && pad_disabled) { /* timeout => enable event */
            toggle_touchpad(True);
        }

    }                           /* end while(1) */

    XFreeModifiermap(cbres.modifiers);
}
#endif                          /* HAVE_X11_EXTENSIONS_RECORD_H */

static XDevice *
dp_get_device(Display * dpy)
{
    XDevice *dev = NULL;
    XDeviceInfo *info = NULL;
    int ndevices = 0;
    Atom touchpad_type = 0;
    Atom *properties = NULL;
    int nprops = 0;
    int error = 0;

    touchpad_type = XInternAtom(dpy, XI_TOUCHPAD, True);
    touchpad_off_prop = XInternAtom(dpy, SYNAPTICS_PROP_OFF, True);
    info = XListInputDevices(dpy, &ndevices);

    while (ndevices--) {
        if (info[ndevices].type == touchpad_type) {
            dev = XOpenDevice(dpy, info[ndevices].id);
            if (!dev) {
                fprintf(stderr, "Failed to open device '%s'.\n",
                        info[ndevices].name);
                error = 1;
                goto unwind;
            }

            properties = XListDeviceProperties(dpy, dev, &nprops);
            if (!properties || !nprops) {
                fprintf(stderr, "No properties on device '%s'.\n",
                        info[ndevices].name);
                error = 1;
                goto unwind;
            }

            while (nprops--) {
                if (properties[nprops] == touchpad_off_prop)
                    break;
            }
            if (nprops < 0) {
                fprintf(stderr, "No synaptics properties on device '%s'.\n",
                        info[ndevices].name);
                error = 1;
                goto unwind;
            }

            break;              /* Yay, device is suitable */
        }
    }

 unwind:
    XFree(properties);
    XFreeDeviceList(info);
    if (!dev)
        fprintf(stderr, "Unable to find a synaptics device.\n");
    else if (error && dev) {
        XCloseDevice(dpy, dev);
        dev = NULL;
    }
    return dev;
}

int
main(int argc, char *argv[])
{
    double idle_time = 2.0;
    int poll_delay = 200000;    /* 200 ms */
    int c;
    int use_xrecord = 0;

    /* Parse command line parameters */
    while ((c = getopt(argc, argv, "i:m:dtp:kKR?v")) != EOF) {
        switch (c) {
        case 'i':
            idle_time = atof(optarg);
            break;
        case 'm':
            poll_delay = atoi(optarg) * 1000;
            break;
        case 'd':
            background = 1;
            break;
        case 't':
            disable_state = TappingOff;
            break;
        case 'p':
            pid_file = optarg;
            break;
        case 'k':
            ignore_modifier_keys = 1;
            break;
        case 'K':
            ignore_modifier_combos = 1;
            ignore_modifier_keys = 1;
            break;
        case 'R':
            use_xrecord = 1;
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage();
            break;
        }
    }
    if (idle_time <= 0.0)
        usage();

    /* Open a connection to the X server */
    display = XOpenDisplay(NULL);
    if (!display) {
        fprintf(stderr, "Can't open display.\n");
        exit(2);
    }

    if (!(dev = dp_get_device(display)))
        exit(2);

    /* Install a signal handler to restore synaptics parameters on exit */
    install_signal_handler();

    if (background) {
        pid_t pid;

        if ((pid = fork()) < 0) {
            perror("fork");
            exit(3);
        }
        else if (pid != 0)
            exit(0);

        /* Child (daemon) is running here */
        setsid();               /* Become session leader */
        chdir("/");             /* In case the file system gets unmounted */
        umask(0);               /* We don't want any surprises */
        if (pid_file) {
            FILE *fd = fopen(pid_file, "w");

            if (!fd) {
                perror("Can't create pid file");
                exit(3);
            }
            fprintf(fd, "%d\n", getpid());
            fclose(fd);
        }
    }

    pad_disabled = False;
    store_current_touchpad_state();

#ifdef HAVE_X11_EXTENSIONS_RECORD_H
    if (use_xrecord) {
        if (check_xrecord(display))
            record_main_loop(display, idle_time);
        else {
            fprintf(stderr, "Use of XRecord requested, but failed to "
                    " initialize.\n");
            exit(4);
        }
    }
    else
#endif                          /* HAVE_X11_EXTENSIONS_RECORD_H */
    {
        setup_keyboard_mask(display, ignore_modifier_keys);

        /* Run the main loop */
        main_loop(display, idle_time, poll_delay);
    }
    return 0;
}

/* vim: set noexpandtab tabstop=8 shiftwidth=4: */