-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmacroplay.c
More file actions
310 lines (282 loc) · 9.87 KB
/
xmacroplay.c
File metadata and controls
310 lines (282 loc) · 9.87 KB
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
/*****************************************************************************
* Includes
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <X11/Xlibint.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysymdef.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "chartbl.h"
#include "FieldNames.h"
int Delay = 100;
/****************************************************************************/
/*! Connects to the desired display. Returns the \c Display or \c 0 if
no display could be obtained.
\arg const char * DisplayName - name of the remote display.
*/
/****************************************************************************/
Display * remoteDisplay (const char * DisplayName) {
int Event, Error;
int Major, Minor;
// open the display
Display * D = XOpenDisplay ( DisplayName );
// did we get it?
if ( ! D ) {
// nope, so show error and abort
fprintf (stderr, "could not open display \"%s\", aborting.\n",
XDisplayName (DisplayName));
exit ( EXIT_FAILURE );
}
// does the remote display have the Xtest-extension?
if ( ! XTestQueryExtension (D, &Event, &Error, &Major, &Minor ) ) {
// nope, extension not supported
fprintf(stderr, "XTest extension not supported on server \"%s\"\n",DisplayString(D));
// close the display and go away
XCloseDisplay ( D );
exit ( EXIT_FAILURE );
}
// print some information
fprintf(stderr, "XTest for server \"%s\" is version %i.%i\n",DisplayString(D), Major,Minor);
// execute requests even if server is grabbed
XTestGrabControl ( D, True );
// sync the server
XSync ( D,True );
// return the display
return D;
}
/****************************************************************************/
/*! Sends a \a character to the remote display \a RemoteDpy. The character is
converted to a \c KeySym based on a character table and then reconverted to
a \c KeyCode on the remote display. Seems to work quite ok, apart from
something weird with the Alt key.
\arg Display * RemoteDpy - used display.
\arg char c - character to send.
*/
/****************************************************************************/
void sendChar(Display *RemoteDpy, char c)
{
KeySym ks, sks, *kss, ksl, ksu;
KeyCode kc, skc;
int syms;
sks=XK_Shift_L;
ks=XStringToKeysym(chartbl[0][(unsigned char)c]);
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
fprintf(stderr, "No keycode on remote display found for char: %c\n", c);
return;
}
if ( ( skc = XKeysymToKeycode ( RemoteDpy, sks ) ) == 0 )
{
fprintf(stderr, "No keycode on remote display found for char: XK_Shift_L!\n");
return;
}
kss=XGetKeyboardMapping(RemoteDpy, kc, 1, &syms);
if (!kss)
{
fprintf(stderr, "XGetKeyboardMapping failed on the remote display (keycode: %i)\n", kc);
return;
}
for (; syms && (!kss[syms-1]); syms--);
if (!syms)
{
fprintf(stderr, "XGetKeyboardMapping failed on the remote display (no syms) (keycode: %i)\n", kc);
XFree(kss);
return;
}
XConvertCase(ks,&ksl,&ksu);
if (ks==kss[0] && (ks==ksl && ks==ksu)) sks=NoSymbol;
if (ks==ksl && ks!=ksu) sks=NoSymbol;
if (sks!=NoSymbol) XTestFakeKeyEvent ( RemoteDpy, skc, True, Delay );
XTestFakeKeyEvent ( RemoteDpy, kc, True, Delay );
XFlush ( RemoteDpy );
XTestFakeKeyEvent ( RemoteDpy, kc, False, Delay );
if (sks!=NoSymbol) XTestFakeKeyEvent ( RemoteDpy, skc, False, Delay );
XFlush ( RemoteDpy );
XFree(kss);
}
/**
* String splitting
*/
char ** string_split(char *path)
{
char *ptr;
char **retv = NULL;
int items = 0;
if(path == NULL)
return NULL;
ptr = strtok(path," ");
while(ptr != NULL )
{
items++;
/* make space */
retv = realloc(retv,(items+1)*sizeof(char *));
retv[items] = NULL;
retv[items-1] = strdup(ptr);
/* get next token */
ptr = strtok(NULL, " ");
}
return retv;
}
void string_split_free(char **retv)
{
int i=0;
/* look all strings */
for(i=0;retv[i] != NULL;i++)free(retv[i]);
/* free the array */
free(retv);
}
void xmacro_command(Display* RemoteDpy, int RemoteScreen, const char* ev)
{
const char *str=NULL;
int x, y;
unsigned int b;
int ks;
int kc;
if (ev[0]=='#')
{
return;
}
if (!strncasecmp(DELAY,ev,strlen(DELAY)))
{
b = atoi(&ev[strlen(DELAY)+1]);
usleep ( (int)(b*1000) );
}
else if (!strncasecmp(BUTTON_PRESS, ev,strlen(BUTTON_PRESS)))
{
b = atoi(&ev[strlen(BUTTON_PRESS)+1]);
XTestFakeButtonEvent ( RemoteDpy, b, True, Delay );
}
else if (!strncasecmp(BUTTON_RELEASE,ev,strlen(BUTTON_RELEASE)))
{
b = atoi(&ev[strlen(BUTTON_RELEASE)+1]);
XTestFakeButtonEvent ( RemoteDpy, b, False, Delay );
}
else if (!strncasecmp(MOTION_NOTIFY,ev, strlen(MOTION_NOTIFY)))
{
sscanf(ev,MOTION_NOTIFY" %i %i", &x, &y);
XTestFakeMotionEvent ( RemoteDpy, RemoteScreen , x, y, Delay );
}
else if (!strncasecmp(KEY_CODE_PRESS,ev,strlen(KEY_CODE_PRESS)))
{
kc = atoi(&ev[strlen(KEY_CODE_PRESS)+1]);
XTestFakeKeyEvent ( RemoteDpy, kc, True, Delay );
}
else if (!strncasecmp(KEY_CODE_RELEASE,ev,strlen(KEY_CODE_RELEASE)))
{
kc = atoi(&ev[strlen(KEY_CODE_RELEASE)+1]);
XTestFakeKeyEvent ( RemoteDpy, kc, False, Delay );
}
else if (!strncmp(EXEC_BLOCK, ev, strlen(EXEC_BLOCK)))
{
char *script = strdup(&ev[strlen(EXEC_BLOCK)+1]);
int pid;
fprintf (stderr, "Exec '%s' blocking\n",script);
if ((pid=fork()) == 0)
{
char **argv= string_split(script);
execvp(argv[0], argv);
string_split_free(argv);
free(script);
exit(1);
}
/* Wait for execvp to be done */
wait(NULL);
}
else if (!strncmp(EXEC_NO_BLOCK, ev, strlen(EXEC_NO_BLOCK)))
{
char *script = strdup(&ev[strlen(EXEC_NO_BLOCK)+1]);
int pid;
fprintf (stderr, "Exec '%s' non-blocking\n",script);
if ((pid=fork()) == 0)
{
char **argv= string_split(script);
execvp(argv[0], argv);
string_split_free(argv);
free(script);
exit(1);
}
}
else if (!strncasecmp(KEY_SYM_PRESS,ev, strlen(KEY_SYM_PRESS)))
{
ks = atoi(&ev[strlen(KEY_SYM_PRESS)+1]);
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
return;
}
XTestFakeKeyEvent ( RemoteDpy, kc, True, Delay );
}
else if (!strncasecmp(KEY_SYM_RELEASE,ev,strlen(KEY_SYM_RELEASE)))
{
ks = atoi(&ev[strlen(KEY_SYM_RELEASE)+1]);
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
return;
}
XTestFakeKeyEvent ( RemoteDpy, kc, False, Delay );
}
else if (!strncasecmp(KEY_SYM,ev, strlen(KEY_SYM)))
{
ks = atoi(&ev[strlen(KEY_SYM)+1]);
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
fprintf(stderr, "No keycode on remote display found for keysym: %i",ks);
return;
}
XTestFakeKeyEvent ( RemoteDpy, kc, True, Delay );
XFlush ( RemoteDpy );
XTestFakeKeyEvent ( RemoteDpy, kc, False, Delay );
}
else if (!strncasecmp(KEY_STR_PRESS,ev,strlen(KEY_STR_PRESS)))
{
ks=XStringToKeysym(&(ev[strlen(KEY_STR_PRESS)+1]));
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
// cerr << "No keycode on remote display found for '" << ev << "': " << ks << endl;
fprintf(stderr, "No keycode on remote display found for '%s':%i\n", ev, ks);
return;
}
XTestFakeKeyEvent ( RemoteDpy, (KeyCode)kc, True, Delay );
}
else if (!strncasecmp(KEY_STR_RELEASE,ev,strlen(KEY_STR_RELEASE)))
{
ks=XStringToKeysym(&(ev[strlen(KEY_STR_RELEASE)+1]));
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
fprintf(stderr, "No keycode on remote display found for '%s':%i\n", ev, ks);
return;
}
XTestFakeKeyEvent ( RemoteDpy, (KeyCode)kc, False, Delay );
}
else if (!strncasecmp(KEY_STR,ev,strlen(KEY_STR)))
{
ks=XStringToKeysym(&(ev[strlen(KEY_STR)+1]));
if ( ( kc = XKeysymToKeycode ( RemoteDpy, ks ) ) == 0 )
{
fprintf(stderr, "No keycode on remote display found for '%s':%i\n", ev, ks);
return;
}
XTestFakeKeyEvent ( RemoteDpy, kc, True, Delay );
XFlush ( RemoteDpy );
XTestFakeKeyEvent ( RemoteDpy, kc, False, Delay );
}
else if (!strncasecmp(STRING,ev,strlen(STRING)))
{
str = &(ev[strlen(STRING)+1]);
b=0;
while(str[b]) sendChar(RemoteDpy, str[b++]);
}
else{
fprintf(stderr, "error: %s\n", ev);
}
// sync the remote server
XSync ( RemoteDpy, False );
}