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
|
/*
* Copyright 2003-2004 Peter Osterlund <petero2@telia.com>
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "synaptics.h"
static SynapticsSHM *synshm;
static int pad_disabled;
static int background = 0;
static void usage()
{
fprintf(stderr, "Usage: syndaemon [-i idle-time] [-d]\n");
fprintf(stderr, " -i How many seconds to wait after the last key press before\n");
fprintf(stderr, " enabling the touchpad. (default is 2s)\n");
fprintf(stderr, " -d Start as a daemon, ie in the background.\n");
exit(1);
}
static void signal_handler(int signum)
{
if (pad_disabled) {
synshm->touchpad_off = 0;
pad_disabled = 0;
}
kill(getpid(), signum);
}
static void install_signal_handler()
{
static int signals[] = {
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGBUS, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE,
SIGALRM, SIGTERM, SIGPWR
};
int i;
struct sigaction act;
sigset_t set;
sigemptyset(&set);
act.sa_handler = signal_handler;
act.sa_mask = set;
act.sa_flags = SA_ONESHOT;
for (i = 0; i < sizeof(signals) / sizeof(int); i++) {
if (sigaction(signals[i], &act, 0) == -1) {
perror("sigaction");
exit(2);
}
}
}
/**
* Return non-zero if the keyboard state has changed since the last call.
*/
static int keyboard_activity(Display *display)
{
#define KEYMAP_SIZE 32
static char old_key_state[KEYMAP_SIZE];
char key_state[KEYMAP_SIZE];
int i;
XQueryKeymap(display, key_state);
for (i = 0; i < KEYMAP_SIZE; i++) {
if (key_state[i] != old_key_state[i]) {
for (i = 0; i < KEYMAP_SIZE; i++)
old_key_state[i] = key_state[i];
return 1;
}
}
return 0;
}
/**
* Return non-zero if any physical touchpad button is currently pressed.
*/
static int touchpad_buttons_active()
{
int i;
if (synshm->left || synshm->right || synshm->up || synshm->down)
return 1;
for (i = 0; i < 8; i++)
if (synshm->multi[i])
return 1;
if (synshm->guest_left || synshm->guest_mid || synshm->guest_right)
return 1;
return 0;
}
static double get_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
static void main_loop(Display *display, double idle_time)
{
const int poll_delay = 20000; /* 20 ms */
double last_activity = 0.0;
double current_time;
pad_disabled = 0;
keyboard_activity(display);
for (;;) {
current_time = get_time();
if (keyboard_activity(display))
last_activity = current_time;
if (touchpad_buttons_active())
last_activity = 0.0;
if (current_time > last_activity + idle_time) { /* Enable touchpad */
if (pad_disabled) {
if (!background)
printf("Enable\n");
synshm->touchpad_off = 0;
pad_disabled = 0;
}
} else { /* Disable touchpad */
if (!pad_disabled && !synshm->touchpad_off) {
if (!background)
printf("Disable\n");
pad_disabled = 1;
synshm->touchpad_off = 1;
}
}
usleep(poll_delay);
}
}
int main(int argc, char *argv[])
{
double idle_time = 2.0;
Display *display;
int c;
int shmid;
/* Parse command line parameters */
while ((c = getopt(argc, argv, "i:d?")) != EOF) {
switch(c) {
case 'i':
idle_time = atof(optarg);
break;
case 'd':
background = 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);
}
/* Connect to the shared memory area */
if ((shmid = shmget(SHM_SYNAPTICS, sizeof(SynapticsSHM), 0)) == -1) {
if ((shmid = shmget(SHM_SYNAPTICS, 0, 0)) == -1) {
fprintf(stderr, "Can't access shared memory area. SHMConfig disabled?\n");
exit(2);
} else {
fprintf(stderr, "Incorrect size of shared memory area. Incompatible driver version?\n");
exit(2);
}
}
if ((synshm = (SynapticsSHM*) shmat(shmid, NULL, 0)) == NULL) {
perror("shmat");
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 */
}
/* Run the main loop */
main_loop(display, idle_time);
return 0;
}
|