Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / dmx / input / dmxsigio.c
1 /*
2 * Copyright 2002-2003 Red Hat Inc., Durham, North Carolina.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 /*
29 * Authors:
30 * Rickard E. (Rik) Faith <faith@redhat.com>
31 *
32 */
33
34 /** \file
35 *
36 * Provides an interface for handling SIGIO signals for input devices. */
37
38 #ifdef HAVE_DMX_CONFIG_H
39 #include <dmx-config.h>
40 #endif
41
42 #include "inputstr.h"
43 #include "dmxinputinit.h"
44 #include "dmxsigio.h"
45 #include "dmxevents.h"
46 #include <signal.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49
50 static int dmxFdCount = 0;
51 static Bool dmxInputEnabled = TRUE;
52
53 /* Define equivalents for non-POSIX systems (e.g., SGI IRIX, Solaris) */
54 #ifndef O_ASYNC
55 #ifdef FASYNC
56 #define O_ASYNC FASYNC
57 #else
58 #define O_ASYNC 0
59 #endif
60 #endif
61 #ifndef O_NONBLOCK
62 #define O_NONBLOCK FNONBLK
63 #endif
64
65 static void
66 dmxSigioHandler(int sig)
67 {
68 int i, j;
69 DMXInputInfo *dmxInput;
70
71 for (i = 0, dmxInput = &dmxInputs[0]; i < dmxNumInputs; i++, dmxInput++) {
72 if (dmxInput->sigioState == DMX_ACTIVESIGIO) {
73 for (j = 0; j < dmxInput->numDevs; j++) {
74 DMXLocalInputInfoPtr dmxLocal = dmxInput->devs[j];
75
76 if (dmxLocal->collect_events) {
77 dmxLocal->collect_events(&dmxLocal->pDevice->public,
78 dmxMotion,
79 dmxEnqueue,
80 dmxCheckSpecialKeys, DMX_NO_BLOCK);
81 }
82 }
83 }
84 }
85 }
86
87 static void
88 dmxSigioHook(void)
89 {
90 struct sigaction a;
91 sigset_t s;
92
93 memset(&a, 0, sizeof(a));
94 a.sa_handler = dmxSigioHandler;
95 sigemptyset(&a.sa_mask);
96 sigaddset(&a.sa_mask, SIGIO);
97 sigaddset(&a.sa_mask, SIGALRM);
98 sigaddset(&a.sa_mask, SIGVTALRM);
99 sigaction(SIGIO, &a, 0);
100
101 sigemptyset(&s);
102 sigprocmask(SIG_SETMASK, &s, 0);
103 }
104
105 static void
106 dmxSigioUnhook(void)
107 {
108 struct sigaction a;
109
110 memset(&a, 0, sizeof(a));
111 a.sa_handler = SIG_IGN;
112 sigemptyset(&a.sa_mask);
113 sigaction(SIGIO, &a, 0);
114 }
115
116 static void
117 dmxSigioAdd(DMXInputInfo * dmxInput)
118 {
119 int flags;
120 int i;
121
122 switch (dmxInput->sigioState) {
123 case DMX_NOSIGIO:
124 return;
125 case DMX_USESIGIO:
126 dmxInput->sigioState = DMX_ACTIVESIGIO;
127 break;
128 case DMX_ACTIVESIGIO:
129 return;
130 }
131
132 for (i = 0; i < dmxInput->sigioFdCount; i++) {
133 if (!dmxInput->sigioAdded[i]) {
134 int fd = dmxInput->sigioFd[i];
135
136 fcntl(fd, F_SETOWN, getpid());
137 flags = fcntl(fd, F_GETFL);
138 flags |= O_ASYNC | O_NONBLOCK;
139 fcntl(fd, F_SETFL, flags);
140
141 AddEnabledDevice(fd);
142 dmxInput->sigioAdded[i] = TRUE;
143
144 if (++dmxFdCount == 1)
145 dmxSigioHook();
146 }
147 }
148 }
149
150 static void
151 dmxSigioRemove(DMXInputInfo * dmxInput)
152 {
153 int flags;
154 int i;
155
156 switch (dmxInput->sigioState) {
157 case DMX_NOSIGIO:
158 return;
159 case DMX_USESIGIO:
160 return;
161 case DMX_ACTIVESIGIO:
162 dmxInput->sigioState = DMX_USESIGIO;
163 break;
164 }
165
166 for (i = 0; i < dmxInput->sigioFdCount; i++) {
167 if (dmxInput->sigioAdded[i]) {
168 int fd = dmxInput->sigioFd[i];
169
170 dmxInput->sigioAdded[i] = FALSE;
171 RemoveEnabledDevice(fd);
172
173 flags = fcntl(fd, F_GETFL);
174 flags &= ~(O_ASYNC | O_NONBLOCK);
175 fcntl(fd, F_SETFL, flags);
176
177 if (!--dmxFdCount)
178 dmxSigioUnhook();
179 }
180 }
181 }
182
183 /** Enable SIGIO handling. This instantiates the handler with the OS. */
184 void
185 dmxSigioEnableInput(void)
186 {
187 int i;
188 DMXInputInfo *dmxInput;
189
190 dmxInputEnabled = TRUE;
191 for (i = 0, dmxInput = &dmxInputs[0]; i < dmxNumInputs; i++, dmxInput++)
192 dmxSigioAdd(dmxInput);
193 }
194
195 /** Disable SIGIO handling. This removes the hanlder from the OS. */
196 void
197 dmxSigioDisableInput(void)
198 {
199 int i;
200 DMXInputInfo *dmxInput;
201
202 dmxInputEnabled = FALSE;
203 for (i = 0, dmxInput = &dmxInputs[0]; i < dmxNumInputs; i++, dmxInput++)
204 dmxSigioRemove(dmxInput);
205 }
206
207 /** Make a note that the input device described in \a dmxInput will be
208 * using the file descriptor \a fd for SIGIO signals. Calls
209 * AddEnabledDevice ifi SIGIO handling has been enabled with
210 * #dmxSigioEnableInput(). */
211 void
212 dmxSigioRegister(DMXInputInfo * dmxInput, int fd)
213 {
214 dmxInput->sigioState = DMX_USESIGIO;
215 if (dmxInput->sigioFdCount >= DMX_MAX_SIGIO_FDS)
216 dmxLog(dmxFatal, "Too many SIGIO file descriptors (%d >= %d)\n",
217 dmxInput->sigioFdCount, DMX_MAX_SIGIO_FDS);
218
219 dmxInput->sigioFd[dmxInput->sigioFdCount++] = fd;
220 if (dmxInputEnabled)
221 dmxSigioAdd(dmxInput);
222 }
223
224 /** Remove the notes that \a dmxInput is using any file descriptors for
225 * SIGIO signals. Calls RemoveEnabledDevice. */
226 void
227 dmxSigioUnregister(DMXInputInfo * dmxInput)
228 {
229 if (dmxInput->sigioState == DMX_NOSIGIO)
230 return;
231 dmxSigioRemove(dmxInput);
232 dmxInput->sigioState = DMX_NOSIGIO;
233 dmxInput->sigioFdCount = 0;
234 }