summaryrefslogtreecommitdiff
path: root/os/inputthread.c
blob: 9d03972801b2e84aab8279e720faba36207692ff (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
/* inputthread.c -- The input thread implementation.
 *
 * Copyright 2007-2008 Tiago Vignatti <vignatti at freedesktop org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <pthread.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>
#include <X11/Xpoll.h>

#include "inputstr.h"
#include "opaque.h"


typedef struct _ThreadDeviceFunc {
    void (*f) (void *);
    int fd;
    void *closure;
} ThreadDeviceFunc;

ThreadDeviceFunc ThreadDevFunc[MAXDEVICES];

extern fd_set InputThreadFd;
extern int NumDevicesThreaded;
extern int InputThreadReadPipe;
extern int InputThreadWritePipe;

static pid_t tid_generation;

/*
 * Create connections in the input thread. Must be called by the
 * DDX implementation.
 */
void
AddEnabledDeviceThreaded(int fd, void (*f)(void *), void *closure)
{
    FD_SET(fd, &InputThreadFd);

    ThreadDevFunc[NumDevicesThreaded].fd = fd;
    ThreadDevFunc[NumDevicesThreaded].f = f;
    ThreadDevFunc[NumDevicesThreaded].closure = closure;

    NumDevicesThreaded++;

    DebugF("Input thread: Using PTHREAD for input device %d\n", fd);
}

/**
 * The workhorse of threaded input event generation.
 *
 * Runs in parallel with the server main thread, listening to input devices in
 * an endless loop. Whenever new input data is made available, calls the proper
 * device driver's routines which are ultimately responsible for the generation
 * of input events as known by X.
 *
 * @see _DeviceEvent
 * @see _RawDeviceEvent
 * @see _InternalEvent
 */
static void*
WaitForInput(void* argument)
{
    int     i;
    fd_set  ReadyInputDevices;

    tid_generation = syscall(__NR_gettid);

    FD_ZERO(&ReadyInputDevices);

    while (1)
    {
        XFD_COPYSET(&InputThreadFd, &ReadyInputDevices);

        DebugF("inputthread: WaitForInput() blocking in Select()\n");
        i = Select(MaxInputDevices, &ReadyInputDevices, NULL, NULL, NULL);
        DebugF("inputthread: WaitForInput() awakening from Select()\n");

        if (i < 0) /* An error occurred */
        {
            if (errno == EINVAL)
            {
                FatalError("WaitForInput(): select: %s\n",
                strerror(errno));
            }
            else if (errno != EINTR && errno != EAGAIN)
            {
                ErrorF("WaitForInput(): select: %s\n",
                strerror(errno));
            }
        }

        /* Call the device drivers to generate input events for us */
        for (i = 0; i < NumDevicesThreaded; i++)
            if ((FD_ISSET(ThreadDevFunc[i].fd, &ReadyInputDevices))
                && ThreadDevFunc[i].f)
                (*ThreadDevFunc[i].f)(ThreadDevFunc[i].closure);

        /* Kick main thread to process the generated input events */
        DebugF("inputthread: WaitforInput() notifying main thread\n");
        PipeWrite(InputThreadWritePipe);
    }
}

/*
 * Called by the dix to create the input thread.
 */
void
CreateInputThread (void)
{
    pthread_t input_thread;
    pthread_attr_t attr;

    pthread_attr_init(&attr);

    /* In OSes that differentiate processes and threads this bellow may have
     * some sense. Linux uses 1:1 thread model. The scheduler handles every
     * thread as a normal process. Therefore this probably have no meaning
     * if you are under Linux.
     */
    if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0)
        perror("error setting input thread scope");

    if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
        perror("error setting input thread stack size");

    pthread_create(&input_thread, &attr, &WaitForInput, NULL);
    pthread_attr_destroy (&attr);
}

/*
 * Called by the dix to close the input thread cleanly.
 */
void
CloseInputThread (void)
{
    RemoveEnabledDevice(InputThreadReadPipe);
    FD_ZERO(&InputThreadFd);
    NumDevicesThreaded = 0;

    syscall(__NR_tkill, tid_generation);
}