UNPKG

7.54 kBJavaScriptView Raw
1/*
2
3BSD LICENSED
4
5Copyright (c) 2011, Janne Julkunen
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without modification,
9are permitted provided that the following conditions are met:
10
11* Redistributions of source code must retain the above copyright notice, this
12list of conditions and the following disclaimer.
13
14* Redistributions in binary form must reproduce the above copyright notice,
15this list of conditions and the following disclaimer in the documentation
16and/or other materials provided with the distribution.
17
18*Neither the name of the Enlightened Linux Solutions nor the names of its
19contributors may be used to endorse or promote products derived from this
20software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31OF THE POSSIBILITY OF SUCH DAMAGE.
32
33*/
34
35var X = null;
36
37var x11 = null;
38
39var applescript = null;
40
41var os = require("os");
42
43var keycodes = require('./x11-keycodes.js').keycodes;
44
45var modifiers = {};
46
47exports.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// var child = exec("xdotool --help", function(error, stdout, stderr) {
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 // Reset modifiers...
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
86exports.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// execute_string = "xdotool mousemove_relative -- " + pos[0] + " " + pos[1];
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// execute_string = "xdotool mousedown " + btn;
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// execute_string = "xdotool mouseup " + btn;
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// execute_string += "xdotool keydown " + key + "; ";
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// console.log("Key press: " + key + " " + keycodes[key].keycode);
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// execute_string += "xdotool keyup " + key + "; ";
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// console.log("Key release: " + key + " " + keycodes[key].keycode);
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 var child = exec(execute_string, function(error, stdout, stderr) {
169 res.header('Content-Type', 'text/javascript');
170
171 if(error !== null) {
172 res.send({status: "offline"});
173 } else {
174 res.send({status: "online"});
175 }
176 });*/
177 } else if(os == "OSX") {
178 var script_string = "";
179
180// tell application "System Events" to keystroke "o"
181
182// with {shift down}
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 // Do nothing...
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