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 | var X = null;
|
36 |
|
37 | var x11 = null;
|
38 |
|
39 | var applescript = null;
|
40 |
|
41 | var os = require("os");
|
42 |
|
43 | var keycodes = require('./x11-keycodes.js').keycodes;
|
44 |
|
45 | var modifiers = {};
|
46 |
|
47 | exports.setup = function(cb) {
|
48 | if(os.type() == "Darwin") {
|
49 | var applescript = require("applescript");
|
50 |
|
51 | cb("input", "Mac OS X", "System Input");
|
52 | } else if(os.type() == "Linux") {
|
53 |
|
54 |
|
55 | x11 = require('./x11-pre-release');
|
56 |
|
57 | x11.createClient(function(display) {
|
58 | if((display) && (display.client) && (display.client.display) &&
|
59 | (display.client.display.screen[0]) && (display.client.display.screen[0].root))
|
60 | {
|
61 | X = display.client;
|
62 |
|
63 | X.require('xtest', function(ext) {
|
64 | X.Test = ext;
|
65 |
|
66 | cb("input", "Linux X11", "System Input");
|
67 |
|
68 |
|
69 |
|
70 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Shift_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
71 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Shift_R"].keycode, 0, X.display.screen[0].root, 0, 0);
|
72 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Control_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
73 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Control_R"].keycode, 0, X.display.screen[0].root, 0, 0);
|
74 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Super_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
75 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Multi_key"].keycode, 0, X.display.screen[0].root, 0, 0);
|
76 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Alt_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
77 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["ISO_Level3_Shift"].keycode, 0, X.display.screen[0].root, 0, 0);
|
78 | });
|
79 | } else {
|
80 | cb(false);
|
81 | }
|
82 | });
|
83 | }
|
84 | };
|
85 |
|
86 | exports.execute = function(req, res) {
|
87 | console.log("Executing input command: " + req.params[0]);
|
88 |
|
89 | if(os == "Linux") {
|
90 | if(req.params[0] == "mouse") {
|
91 | if(req.param("move")) {
|
92 | var pos = req.param("move").split(",");
|
93 |
|
94 |
|
95 |
|
96 | X.QueryPointer(X.display.screen[0].root, function(pointer) {
|
97 | X.WarpPointer(0,X.display.screen[0].root, 0, 0, 0, 0,
|
98 | (parseInt(pointer[2]) + parseInt(pos[0])),
|
99 | (parseInt(pointer[3]) + parseInt(pos[1])));
|
100 | });
|
101 | } else if(req.param("down")) {
|
102 | var btn = req.param("down");
|
103 |
|
104 |
|
105 |
|
106 | X.QueryPointer(X.display.screen[0].root, function(pointer) {
|
107 | X.Test.FakeInput(X.Test.ButtonPress, btn, 0, X.display.screen[0].root,
|
108 | parseInt(pointer[2]), parseInt(pointer[3]));
|
109 | });
|
110 | } else if(req.param("up")) {
|
111 | var btn = req.param("up");
|
112 |
|
113 |
|
114 |
|
115 | X.QueryPointer(X.display.screen[0].root, function(pointer) {
|
116 | X.Test.FakeInput(X.Test.ButtonRelease, btn, 0, X.display.screen[0].root,
|
117 | parseInt(pointer[2]), parseInt(pointer[3]));
|
118 | });
|
119 | }
|
120 | } else if(req.params[0] == "keyboard") {
|
121 | if((req.param("key")) || (req.param("down"))) {
|
122 | var key = req.param("key") || req.param("down");
|
123 |
|
124 |
|
125 |
|
126 | if(keycodes[key]) {
|
127 | if(key == "Shift_L")
|
128 | modifiers.shift = true;
|
129 | else if(key == "ISO_Level3_Shift")
|
130 | modifiers.altgr = true;
|
131 |
|
132 | if((keycodes[key].modifier == "shift") && (!modifiers.shift))
|
133 | X.Test.FakeInput(X.Test.KeyPress, keycodes["Shift_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
134 | else if((keycodes[key].modifier == "altgr") && (!modifiers.altgr))
|
135 | X.Test.FakeInput(X.Test.KeyPress, keycodes["ISO_Level3_Shift"].keycode, 0, X.display.screen[0].root, 0, 0);
|
136 |
|
137 |
|
138 |
|
139 | X.Test.FakeInput(X.Test.KeyPress, keycodes[key].keycode, 0, X.display.screen[0].root, 0, 0);
|
140 | }
|
141 | }
|
142 |
|
143 | if((req.param("key")) || (req.param("up"))) {
|
144 | var key = req.param("key") || req.param("up");
|
145 |
|
146 |
|
147 |
|
148 | if(keycodes[key]) {
|
149 | if(key == "Shift_L")
|
150 | modifiers.shift = false;
|
151 | else if(key == "ISO_Level3_Shift")
|
152 | modifiers.altgr = false;
|
153 |
|
154 |
|
155 |
|
156 | X.Test.FakeInput(X.Test.KeyRelease, keycodes[key].keycode, 0, X.display.screen[0].root, 0, 0);
|
157 |
|
158 | if((keycodes[key].modifier == "shift") && (!modifiers.shift))
|
159 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["Shift_L"].keycode, 0, X.display.screen[0].root, 0, 0);
|
160 | else if((keycodes[key].modifier == "altgr") && (!modifiers.altgr))
|
161 | X.Test.FakeInput(X.Test.KeyRelease, keycodes["ISO_Level3_Shift"].keycode, 0, X.display.screen[0].root, 0, 0);
|
162 | }
|
163 | }
|
164 | }
|
165 |
|
166 | res.send({status: "online"});
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | } else if(os == "OSX") {
|
178 | var script_string = "";
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | if(req.params[0] == "mouse") {
|
186 | if(req.param("move")) {
|
187 | var pos = req.param("move").split(",");
|
188 |
|
189 | script_string = "set mypoint to (get position of the mouse)\n";
|
190 |
|
191 | script_string += "move mouse {(item 1 of mypoint) + " + pos[0] +
|
192 | ", (item 2 of mypoint) + " + pos[1] + "}\n";
|
193 | } else if(req.param("down")) {
|
194 |
|
195 | } else if(req.param("up")) {
|
196 | var btn = req.param("up");
|
197 |
|
198 | var buttons = ["primary", "middle", "secondary"];
|
199 |
|
200 | script_string = "set mypoint to (get position of the mouse)\n";
|
201 |
|
202 | script_string += "click mouse {(item 1 of mypoint), (item 2 of mypoint)}" +
|
203 | " using " + buttons[btn - 1] + " button\n";
|
204 | }
|
205 | }
|
206 |
|
207 | applescript.execString(script_string, function(error, result) {
|
208 | res.header('Content-Type', 'text/javascript');
|
209 |
|
210 | if(error)
|
211 | res.send({"state": "offline"});
|
212 | else
|
213 | res.send({"state": "online"});
|
214 | });
|
215 | }
|
216 | };
|
217 |
|