summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 6b456898a49e866b7c9cb760a4d07790faf02b4f (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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
pkgname=xsendkey
pkgver=0.0
pkgrel=1
pkgdesc="xsendkey is a simple tool for generating keypresses in X"
arch=('i686' 'x86_64')
url="http://people.csail.mit.edu/adonovan/hacks/xsendkey.html"
license=('GPL')
depends=()
#source=('http://people.csail.mit.edu/adonovan/hacks/xsendkey.c')
source=('http://web.archive.org/web/20130528093333/http://people.csail.mit.edu/adonovan/hacks/xsendkey.c')
md5sums=('e7a5538a11c33449c88caa8b901a13eb')

build() {
  cd "$srcdir"
  gcc -o xsendkey -lX11 xsendkey.c
}

package ()
{
  cd "$srcdir"
  install -Dm 755  xsendkey "$pkgdir/usr/bin/xsendkey"
}

# Backup below because site is rly rly old ;)
# In case of emergency paste it as xsendkey.c
backupfile ()
{
echo "backup in PKGBUILD" <<BACKUPFILE
/*
 *  (C) 2000-2004 Alan Donovan
 *
 *  Author: Alan Donovan <adonovan@lcs.mit.edu>
 *
 *  With thanks to:	T.Sato <VEF00200@nifty.ne.jp>.
 *			http://member.nifty.ne.jp/tsato/tools/xvkbd-e.html
 *  and:		Derek Martin (derek_martin at agilent dot com)
 * 			(for -count and XGetInputFocus)
 *  and:		Hanno Hecker <h.hecker@bee.de>
 *                      (for symbolic keysym and modifier lookup.)
 *
 *  xsendkey.cxx -- General purpose keypress generator
 *
 *  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 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.
 *  <http://www.gnu.org/copyleft/gpl.html>.
 *
 *  $Id: xsendkey.c,v 1.3 2004/11/11 16:10:01 adonovan Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#  include "windows_x11.h"
#else
#  define NeedFunctionPrototypes 1
#  include <X11/Xlib.h>
#  include <X11/keysym.h>
#  if XlibSpecificationRelease != 6
#      error Requires X11R6
#  endif
#endif

static char    *progname       = NULL;
static char    *displayname    = NULL;
static Window   window         = 0;
static Display *display        = NULL;
static char     keyname[1024];
static int      shift          = 0;
static int      keysym         = 0;

int	MyErrorHandler(Display *my_display, XErrorEvent *event)
{
    fprintf(stderr, "%s: XSendEvent(0x%lx) failed.\n", progname, window);
    return 1;
}

void	SendEvent(XKeyEvent *event)
{
    XSync(display, False);
    XSetErrorHandler(MyErrorHandler);
    XSendEvent(display, window, True, KeyPressMask, (XEvent*)event);
    XSync(display, False);
    XSetErrorHandler(NULL);
}

void	SendKeyPressedEvent(KeySym keysym, unsigned int shift)
{
    XKeyEvent		event;

    // Meta not yet implemented (Alt used instead ;->)
    int meta_mask=0;

    event.display	= display;
    event.window	= window;
    event.root		= RootWindow(display, 0); // XXX nonzero screens?
    event.subwindow	= None;
    event.time		= CurrentTime;
    event.x		= 1;
    event.y		= 1;
    event.x_root	= 1;
    event.y_root	= 1;
    event.same_screen	= True;
    event.type		= KeyPress;
    event.state		= 0;

    //
    // press down shift keys one at a time...
    //

    if (shift & ShiftMask) {
	event.keycode = XKeysymToKeycode(display, XK_Shift_L);
	SendEvent(&event);
	event.state |= ShiftMask;
    }
    if (shift & ControlMask) {
	event.keycode = XKeysymToKeycode(display, XK_Control_L);
	SendEvent(&event);
	event.state |= ControlMask;
    }

    if (shift & Mod1Mask) {
        event.keycode = XKeysymToKeycode(display, XK_Alt_L);
        SendEvent(&event);
        event.state |= Mod1Mask;
    }
    if (shift & meta_mask) {
	event.keycode = XKeysymToKeycode(display, XK_Meta_L);
	SendEvent(&event);
	event.state |= meta_mask;
    }

    //
    //  Now with shift keys held down, send event for the key itself...
    //


    // fprintf(stderr, "sym: 0x%x, name: %s\n", keysym, keyname);
    if (keysym != NoSymbol) {
        event.keycode = XKeysymToKeycode(display, keysym);
        // fprintf(stderr, "code: 0x%x, %d\n", event.keycode, event.keycode );
        SendEvent(&event);

        event.type = KeyRelease;
        SendEvent(&event);
    }

    //
    // Now release all the shift keys...
    //

    if (shift & ShiftMask) {
	event.keycode = XKeysymToKeycode(display, XK_Shift_L);
	SendEvent(&event);
	event.state &= ~ShiftMask;
    }
    if (shift & ControlMask) {
        event.keycode = XKeysymToKeycode(display, XK_Control_L);
        SendEvent(&event);
        event.state &= ~ControlMask;
    }
    if (shift & Mod1Mask) {
        event.keycode = XKeysymToKeycode(display, XK_Alt_L);
        SendEvent(&event);
        event.state &= ~Mod1Mask;
    }
    if (shift & meta_mask) {
	event.keycode = XKeysymToKeycode(display, XK_Meta_L);
	SendEvent(&event);
	event.state &= ~meta_mask;
    }
}

void	usage()
{
    fprintf(stderr,
        "usage: %s [-window <id>|root]\n"
        "            [-count <count>]\n"
        "            [-display <name>] [Modifier+[Modifier+]]Key\n"
        "(looks up keysyms in <X11/keysymdef.h>)\n"
        "'Alt',   'Control',   'Shift' are replaced by\n"
        "'Alt_L', 'Control_L', 'Shift_L'...\n"
        "  example:\n"
        "     %s Control+Alt+Right\n", progname, progname);
    exit(1);
}

int	main(int argc, char **argv)
{
    int ii, Junk;
    int c;
    char *temp;
    unsigned long count = 1;
    progname = argv[0];

    for(ii=1; ii<argc; ii++) {
        if(strcmp(argv[ii], "-display") == 0) {
            if(++ii == argc)
                usage();

            displayname = argv[ii];
        }
        else if(strcmp(argv[ii], "-count") == 0) {
            if(++ii == argc)
                usage();

            count = strtoul(argv[ii],NULL,0);
        }
        else if(strcmp(argv[ii], "-window") == 0) {
            if(++ii == argc)
                usage();

            if(strcmp(argv[ii], "root") == 0)
                window = (Window)-1;
            else
                window = (Window)strtoul(argv[ii], NULL, 0);
        }
        else if(argv[ii][0] == '-') {
            fprintf(stderr, "%s: bad option `%s'.\n",
                progname, argv[ii]);
            usage();
        }
        else {
            strncpy(keyname, argv[ii], 1023);
            keysym = 0;
            shift  = 0;
            // fprintf(stderr, "keyname: %s\n", keyname);
            temp = strtok((char *)keyname, "+");
            while (temp != NULL) {
                // fprintf(stderr, "temp: %s\n", temp);
                if (strcmp(temp, "Alt") == 0)
                    c = XK_Alt_L;
                else if (strcmp(temp, "Control") == 0)
                    c = XK_Control_L;
                else if (strcmp(temp, "Shift") == 0)
                    c = XK_Shift_L;
                else
                    c = XStringToKeysym(temp);

                if (c == 0) {
                    fprintf(stderr, "unknown: %s\n", temp);
                    usage();
                }

                switch (c) {
                    case XK_Shift_L:
                    case XK_Shift_R:
                        shift |= ShiftMask;
                        break;
                    case XK_Control_L:
                    case XK_Control_R:
                        shift |= ControlMask;
                        break;
                    case XK_Caps_Lock:
                    case XK_Shift_Lock:
                        shift |= LockMask;
                        break;
                    case XK_Meta_L:
                    case XK_Meta_R:
                    case XK_Alt_L:
                    case XK_Alt_R:
                        shift |= Mod1Mask;
                        break;
                    case XK_Super_L:
                    case XK_Super_R:
                    case XK_Hyper_L:
                    case XK_Hyper_R:
                        break;
                    default:
                        keysym = c;
                }
                // fprintf(stderr, "keysym: 0x%x, shift: %d\n", keysym, shift);
                temp = strtok(NULL, "+");
            }
            /* keysym = strtoul(argv[ii], NULL, 0); */
        }
    }
    if (keyname[0] == 0) {
        fprintf(stderr, "%s: you must specify a keyname.\n", progname);
        exit(1);
    }

    if(displayname == NULL)
	displayname = getenv("DISPLAY");

    if(displayname == NULL)
	displayname = ":0.0";

    display = XOpenDisplay(displayname);

    if(display == NULL)
    {
	fprintf(stderr, "%s: can't open display `%s'.\n",
		progname, displayname);
	exit(1);
    }

    if(window == 0)
        XGetInputFocus(display, &window, &Junk);

    if(window == (Window)-1)
    {
        window = RootWindow(display, 0); // XXX nonzero screens?
    }

    // now do the work:
    for (ii=0;ii<(int)count;ii++)
      SendKeyPressedEvent(keysym, shift);

    XCloseDisplay(display);

    return 0;
}

BACKUPFILE
}