-
Notifications
You must be signed in to change notification settings - Fork 0
/
_aenea.py
347 lines (269 loc) · 11.3 KB
/
_aenea.py
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# This is a command module for Dragonfly. It provides support for several of
# Aenea's built-in capabilities. This module is NOT required for Aenea to
# work correctly, but it is strongly recommended.
# This file is part of Aenea
#
# Aenea is free software: you can redistribute it and/or modify it under
# the terms of version 3 of the GNU Lesser General Public License as
# published by the Free Software Foundation.
#
# Aenea 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 Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Aenea. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (2014) Alex Roper
# Alex Roper <alex@aroper.net>
import os
import sys
import time
import dragonfly
try:
# Internal NatLink module for reloading grammars.
import natlinkmain
except ImportError:
natlinkmain = None
try:
import aenea
import aenea.proxy_contexts
import aenea.configuration
import aenea.communications
import aenea.config
import aenea.configuration
except ImportError:
print 'Unable to import Aenea client-side modules.'
raise
print 'Aenea client-side modules loaded successfully'
print 'Settings:'
print '\tHOST:', aenea.config.DEFAULT_SERVER_ADDRESS[0]
print '\tPORT:', aenea.config.DEFAULT_SERVER_ADDRESS[1]
print '\tPLATFORM:', aenea.config.PLATFORM
print '\tUSE_MULTIPLE_ACTIONS:', aenea.config.USE_MULTIPLE_ACTIONS
print '\tSCREEN_RESOLUTION:', aenea.config.SCREEN_RESOLUTION
try:
aenea.proxy_contexts._get_context()
print 'Aenea: Successfully connected to server.'
except:
print 'Aenea: Unable to connect to server.'
# Commands that can be rebound.
command_table = [
'set proxy server to <proxy>',
'disable proxy server',
'enable proxy server',
'force natlink to reload all grammars',
'disable keyboard',
'enable keyboard',
'shervs test',
'change to Linux',
'change to Windows',
'window list',
'shelf list',
]
command_table = aenea.configuration.make_grammar_commands(
'aenea',
dict(zip(command_table, command_table))
)
def topy(path):
if path.endswith == ".pyc":
return path[:-1]
return path
class DisableRule(dragonfly.CompoundRule):
spec = command_table['disable proxy server']
def _process_recognition(self, node, extras):
aenea.config.disable_proxy()
class EnableRule(dragonfly.CompoundRule):
spec = command_table['enable proxy server']
def _process_recognition(self, node, extras):
aenea.config.enable_proxy()
def unload_code(optional_blacklist = []):
print "Unloading all aenea code"
# Do not reload anything in these directories or their subdirectories.
#blacklist_list = list("core") + optional_blacklist
dir_reload_blacklist = set(["core"])
if len(optional_blacklist):
dir_reload_blacklist.add(optional_blacklist)
print "Blacklist: ", dir_reload_blacklist
macro_dir = "C:\\NatLink\\NatLink\\MacroSystem"
# Unload all grammars if natlinkmain is available.
if natlinkmain and not len(optional_blacklist):
natlinkmain.unloadEverything()
# Unload all modules in macro_dir except for those in directories on the
# blacklist.
# Consider them in sorted order to try to make things as predictable as possible to ease debugging.
for name, module in sorted(sys.modules.items()):
if module and hasattr(module, "__file__"):
# Some builtin modules only have a name so module is None or
# do not have a __file__ attribute. We skip these.
path = module.__file__
# Convert .pyc paths to .py paths.
path = topy(path)
# Do not unimport this module! This will cause major problems!
if (path.startswith(macro_dir) and
not bool(set(path.split(os.path.sep)) & dir_reload_blacklist)
and path != topy(os.path.abspath(__file__))):
print "removing %s from cache (in module %s)" % (name, module)
# Remove the module from the cache so that it will be reloaded
# the next time # that it is imported. The paths for packages
# end with __init__.pyc so this # takes care of them as well.
del sys.modules[name]
def load_code():
print "Loading all aenea code"
try:
# Reload the top-level modules in macro_dir if natlinkmain is available.
if natlinkmain:
natlinkmain.findAndLoadFiles()
except Exception as e:
print "reloading failed: {}".format(e)
else:
print "finished reloading"
def reload_code():
unload_code()
load_code()
def disableKeyboard():
print "Disabling just the keyboard grammar."
# Unload the modules except for all the "core" and "aenea" modules
unload_code("aenea")
print "Switching Dragon to Normal mode."
action = dragonfly.Mimic("switch", "to", "normal", "mode")
#action = dragonfly.Playback([(["switch", "to", "normal", "mode"], 0.0)])
action.execute()
def enableKeyboard():
print "Enabling keyboard."
load_code()
print "Switching Dragon to Command mode."
action = dragonfly.Mimic("switch", "to", "command", "mode")
#action = dragonfly.Playback([(["switch", "to", "command", "mode"], 0.0)])
action.execute()
def shervstest():
print "Running Shervs Test!"
#from six.moves import xmlrpc_client
#server = xmlrpc_client.ServerProxy("http://127.0.0.1:12400", allow_none=False)
#remote_title = server.GetActiveWindowTitle()
#print "Remote ", remote_title
# Switching OSes, when Windows is in a VM on top of a Linux host:
def changeToLinux():
print "Changing to Linux!"
# Make sure all keyboard input gets relayed to the Linux aenea server!
#action = dragonfly.Mimic("switch", "to", "Aenea", "client")
#action = dragonfly.Playback([(["switch", "to", "Aenea", "client"], 0.0)])
action = dragonfly.FocusWindow(title="Aenea client")
action.execute()
time.sleep(0.1)
print "Switching Dragon to Command mode."
action = dragonfly.Mimic("switch", "to", "command", "mode")
#action = dragonfly.Playback([(["switch", "to", "command", "mode"], 0.0)])
action.execute()
time.sleep(0.1)
# Run our aenea plugin script that moves the Windows VM to a minimally visible window in Linux.
aenea.communications.server.change_OS("Linux")
# Switching OSes, when Windows is in a VM on top of a Linux host:
def changeToWindows():
print "Changing to Windows!"
print "Switching Dragon to Normal mode."
action = dragonfly.Mimic("switch", "to", "normal", "mode")
#action = dragonfly.Playback([(["switch", "to", "normal", "mode"], 0.0)])
action.execute()
# Run our aenea plugin script that moves the Windows VM to fullscreen in Linux.
aenea.communications.server.change_OS("Windows")
time.sleep(0.1)
# Make sure the keyboard input doesn't go to the Linux aenea server!
#action = dragonfly.Mimic("start", "dragonpad")
#action = dragonfly.BringApp("")
#action = dragonfly.Playback([(["alt", "escape"], 0.0)])
#action = dragonfly.Key("alt") + dragonfly.Key("escape")
action = dragonfly.FocusWindow(title="Firefox")
action.execute()
time.sleep(0.1)
# Make sure the DragonPad menu bar isn't selected
#action = dragonfly.Mimic("escape")
#action.execute()
def showWindowList():
print "Showing the Linux window list."
#"show window list": Key("win:down/999, tab") + Key("win:up"),
#"show window list": Key("w-l") + Key("tab") + Key("down"),
#action = dragonfly.Key("ctrl:down, alt:down") + dragonfly.Key("ctrl:up, alt:up")
#action = dragonfly.Key("ctrl:down") + dragonfly.Key("o")
#action.execute()
#time.sleep(0.3)
#action = dragonfly.Key("tab")
#action.execute()
#time.sleep(0.1)
#action = dragonfly.Key("down")
#action.execute()
# Run our aenea plugin script that shows the Linux window list.
aenea.communications.server.showWindowList()
def showShelfList():
print "Showing the Linux shelf list."
# Run our aenea plugin script that shows the Linux clipboard shelf list.
# Value -1 means show the graphical list.
aenea.communications.server.shelfCommand(-1)
def shelfNumber(val):
print "Running shelf number", val
# Run our aenea plugin script that pastes the shelf item
aenea.communications.server.shelfCommand(val)
class DisableKeyboard(dragonfly.MappingRule):
mapping = {command_table['disable keyboard']: dragonfly.Function(disableKeyboard)}
class EnableKeyboard(dragonfly.MappingRule):
mapping = {command_table['enable keyboard']: dragonfly.Function(enableKeyboard)}
class ShervsTest(dragonfly.MappingRule):
mapping = {command_table['shervs test']: dragonfly.Function(shervstest)}
class ChangeToLinux(dragonfly.MappingRule):
mapping = {command_table['change to Linux']: dragonfly.Function(changeToLinux)}
class ChangeToWindows(dragonfly.MappingRule):
mapping = {command_table['change to Windows']: dragonfly.Function(changeToWindows)}
class ShowWindowList(dragonfly.MappingRule):
mapping = {command_table['window list']: dragonfly.Function(showWindowList)}
class ShowShelfList(dragonfly.MappingRule):
mapping = {command_table['shelf list']: dragonfly.Function(showShelfList)}
class ShelfNumber(dragonfly.MappingRule):
mapping = {
# Call shelfNumber(<n>)
"shelf <n>": dragonfly.Function(lambda n: shelfNumber(n))
}
extras = [
dragonfly.IntegerRef("n", 0, 99),
]
#defaults = {
# "n": 0,
#}
# Note that you do not need to turn mic off and then on after saying this. This
# also unloads all modules and packages in the macro directory so that they will
# be reloaded the next time that they are imported. It even reloads Aenea!
class ReloadGrammarsRule(dragonfly.MappingRule):
mapping = {command_table['force natlink to reload all grammars']: dragonfly.Function(reload_code)}
server_list = dragonfly.DictList('aenea servers')
server_list_watcher = aenea.configuration.ConfigWatcher(
('grammar_config', 'aenea'))
class ChangeServer(dragonfly.CompoundRule):
spec = command_table['set proxy server to <proxy>']
extras = [dragonfly.DictListRef('proxy', server_list)]
def _process_recognition(self, node, extras):
aenea.communications.set_server_address((extras['proxy']['host'], extras['proxy']['port']))
def _process_begin(self):
if server_list_watcher.refresh():
server_list.clear()
for k, v in server_list_watcher.conf.get('servers', {}).iteritems():
server_list[str(k)] = v
grammar = dragonfly.Grammar('aenea')
grammar.add_rule(EnableRule())
grammar.add_rule(DisableRule())
grammar.add_rule(ReloadGrammarsRule())
grammar.add_rule(ChangeServer())
grammar.add_rule(DisableKeyboard())
grammar.add_rule(EnableKeyboard())
grammar.add_rule(ShervsTest())
grammar.add_rule(ChangeToLinux())
grammar.add_rule(ChangeToWindows())
grammar.add_rule(ShowWindowList())
grammar.add_rule(ShowShelfList())
grammar.add_rule(ShelfNumber())
grammar.load()
# Unload function which will be called at unload time.
def unload():
global grammar
if grammar:
grammar.unload()
grammar = None