File: xsendkeys.cpp

package info (click to toggle)
lineakd 1%3A0.9-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,264 kB
  • ctags: 823
  • sloc: sh: 9,998; cpp: 6,122; perl: 2,830; ansic: 387; makefile: 194
file content (115 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download | duplicates (3)
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
/* xsendkey.c ************************************************/
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive for
 * more details.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <lineak/lineak_core_functions.h>
#include <lineak/lineak_util_functions.h>
#include <lineak/lobject.h>

#include <vector>
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace lineak_core_functions;
using namespace lineak_util_functions;

char *ProgramName;

Display *dpy;
int scr;
Window win;
unsigned int width, height;

int main(int argc, char **argv)
{
    Window ret_win;
    int x, y;
    unsigned int border_width, depth;
    int keycode;
    string argument;

    ProgramName = argv[0];
    if (argc < 1)
    	return 1;

    argument = string(argv[1]);
    
    dpy = XOpenDisplay("");
    if (!dpy) {
	fprintf(stderr, "%s: Cannot connect to display ...\n", ProgramName);
	return 1;
    }
    scr = DefaultScreen(dpy);
    win = RootWindow(dpy, scr);
    XGetGeometry(dpy, win, &ret_win, &x, &y, &width, &height, &border_width, &depth);
    /////////////////////////////////
    // lineak stuff
    vector<string> args;
    args.push_back(argument);
    vector<string> elements;
    vector<int>codes;
    KeySym mykeysym;
    KeyCode xkeycode;
    
    int index = 0;
    string parsed = "";
    string symname = lineak_util_functions::strip_space(args[0]);
    string smodifiers = "";
    while (symname.find('+') != string::npos) {
	    index = symname.find('+');
	    smodifiers = symname.substr(index+1,symname.size()-(index+1));
	    parsed = symname.substr(0,index);
	    symname = smodifiers;
	    if ( parsed == "control" || parsed == "Control" || parsed == "CONTROL" )
		    parsed = "Control_L";
	    if ( parsed == "shift" || parsed == "Shift" || parsed == "SHIFT" )
		    parsed = "Shift_L";
	    if ( parsed == "mod1" || parsed == "alt" || parsed == "Alt" || parsed == "ALT" )
		    parsed = "Alt_L";
            //cout << "element = " << parsed << endl;
	    elements.push_back(parsed);
	    //cout << "remainder = " << smodifiers << endl;
    }
    elements.push_back(smodifiers);
    //inspect_vector(elements);
    
    for (vector<string>::const_iterator it = elements.begin(); it != elements.end(); it++) {
        mykeysym = XStringToKeysym(it->c_str());
        xkeycode = XKeysymToKeycode(dpy, mykeysym);	
	//cout << *it << " has code of " << (int)xkeycode << endl; 
	codes.push_back((int)xkeycode);
    }
    /*
    cout << "vector has " << codes.size() << " elements: { ";
    for (vector<int>::const_iterator it = codes.begin(); it != codes.end(); it++)
		                    cout << *it << " ";
    cout << "} " << endl;
    */
    for (vector<int>::const_iterator it = codes.begin(); it != codes.end(); it++) {
	    keycode = *it;
	    XTestFakeKeyEvent(dpy, keycode, 1, 0);
	    //cout << "sending key down: " << keycode << endl;
	    XSync(dpy, 1);
    }
    vector<int>::const_iterator it = codes.end();
    
    do {
	    it--;
	    keycode = *it;
	    XTestFakeKeyEvent(dpy, keycode, 0, 0);
	    //cout << "sending key up: " << keycode << endl;
	    XSync(dpy, 1);
    }
    while (it != codes.begin());
	    
    return 0;
}