This file is indexed.

/usr/lib/python3/dist-packages/autopilot/keybindings.py is in python3-autopilot 1.5.1+16.04.20160412-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2012-2013 Canonical
#
# 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 3 of the License, or
# (at your option) 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


"""Utility functions to get shortcut keybindings for various parts of Unity.

Inside Autopilot we deal with keybindings by naming them with unique names. For
example, instead of hard-coding the fact that 'Alt+F2' opens the command lens,
we might call:

>>> keybindings.get('lens_reveal/command')
'Alt+F2'

Keybindings come from two different places:
 1) Keybindings from compiz. We can get these if we have the plugin name and
    setting name.
 2) Elsewhere. Right now we're hard-coding these in a separate dictionary.
"""

import logging
import re

from autopilot.input import Keyboard
from autopilot.utilities import Silence

_logger = logging.getLogger(__name__)


#
# Fill this dictionary with keybindings we want to store.
#
# If keybindings are from compizconfig, the value should be a 2-value tuple
# containging (plugin_name, setting_name).
#
# If keybindings are elsewhere, just store the keybinding string.
_keys = {
    # Launcher:
    "launcher/reveal": ('unityshell', 'show_launcher'),
    "launcher/keynav": ('unityshell', 'keyboard_focus'),
    "launcher/keynav/next": "Down",
    "launcher/keynav/prev": "Up",
    "launcher/keynav/activate": "Enter",
    "launcher/keynav/exit": "Escape",
    "launcher/keynav/open-quicklist": "Right",
    "launcher/keynav/close-quicklist": "Left",
    "launcher/switcher": ('unityshell', 'launcher_switcher_forward'),
    "launcher/switcher/exit": "Escape",
    "launcher/switcher/next": "Tab",
    "launcher/switcher/prev": "Shift+Tab",
    "launcher/switcher/down": "Down",
    "launcher/switcher/up": "Up",
    # Quicklist:
    "quicklist/keynav/first": "Home",
    "quicklist/keynav/last": "End",
    "quicklist/keynav/next": "Down",
    "quicklist/keynav/prev": "Up",
    "quicklist/keynav/activate": "Enter",
    "quicklist/keynav/exit": "Escape",
    # Panel:
    "panel/show_menus": "Alt",
    "panel/open_first_menu": ('unityshell', 'panel_first_menu'),
    "panel/next_indicator": "Right",
    "panel/prev_indicator": "Left",
    # Dash:
    "dash/reveal": "Super",
    "dash/lens/next": "Ctrl+Tab",
    "dash/lens/prev": "Ctrl+Shift+Tab",
    # Lenses:
    "lens_reveal/command": ("unityshell", "execute_command"),
    "lens_reveal/apps": "Super+a",
    "lens_reveal/files": "Super+f",
    "lens_reveal/music": "Super+m",
    "lens_reveal/video": "Super+v",
    # Hud:
    "hud/reveal": ("unityshell", "show_hud"),
    # Switcher:
    "switcher/reveal_normal": ("unityshell", "alt_tab_forward"),
    "switcher/reveal_impropper": "Alt+Right",
    "switcher/reveal_details": "Alt+`",
    "switcher/reveal_all": ("unityshell", "alt_tab_forward_all"),
    "switcher/cancel": "Escape",
    # Shortcut Hint:
    "shortcuthint/reveal": ('unityshell', 'show_launcher'),
    "shortcuthint/cancel": "Escape",
    # These are in compiz as 'Alt+Right' and 'Alt+Left', but the fact that it
    # lists the Alt key won't work for us, so I'm defining them manually.
    "switcher/next": "Tab",
    "switcher/prev": "Shift+Tab",
    "switcher/right": "Right",
    "switcher/left": "Left",
    "switcher/detail_start": "Down",
    "switcher/detail_stop": "Up",
    "switcher/detail_next": "`",
    "switcher/detail_prev": "`",
    # Workspace switcher (wall):
    "workspace/move_left": ("wall", "left_key"),
    "workspace/move_right": ("wall", "right_key"),
    "workspace/move_up": ("wall", "up_key"),
    "workspace/move_down": ("wall", "down_key"),
    # Window management:
    "window/show_desktop": ("unityshell", "show_desktop_key"),
    "window/minimize": ("core", "minimize_window_key"),
    "window/maximize": ("core", "maximize_window_key"),
    "window/left_maximize": ("unityshell", "window_left_maximize"),
    "window/right_maximize": ("unityshell", "window_right_maximize"),
    "window/restore": ("core", "unmaximize_or_minimize_window_key"),
    "window/close": ("core", "close_window_key"),
    # expo plugin:
    "expo/start": ("expo", "expo_key"),
    "expo/cancel": "Escape",
    # spread (scale) plugin:
    "spread/start": ("scale", "initiate_key"),
    "spread/cancel": "Escape",
}


def get(binding_name):
    """Get a keybinding, given its well-known name.

    :param string binding_name:
    :raises TypeError: if binding_name is not a string
    :raises ValueError: if binding_name cannot be found in the bindings
                        dictionaries.
    :returns: string for keybinding

    """
    if not isinstance(binding_name, str):
        raise TypeError("binding_name must be a string.")
    if binding_name not in _keys:
        raise ValueError("Unknown binding name '%s'." % (binding_name))
    v = _keys[binding_name]
    if isinstance(v, str):
        return v
    else:
        return _get_compiz_keybinding(v)


def get_hold_part(binding_name):
    """Return the part of a keybinding that must be held permanently.

    Use this function to split bindings like "Alt+Tab" into the part that must
    be held down. See :meth:`get_tap_part` for the part that must be tapped.

    :raises ValueError: if the binding specified does not have multiple
     parts.

    """
    binding = get(binding_name)
    parts = binding.split('+')
    if len(parts) == 1:
        _logger.warning(
            "Key binding '%s' does not have a hold part.", binding_name)
        return parts[0]
    return '+'.join(parts[:-1])


def get_tap_part(binding_name):
    """Return the part of a keybinding that must be tapped.

    Use this function to split bindings like "Alt+Tab" into the part that must
    be held tapped. See :meth:`get_hold_part` for the part that must be held
    down.

    :raises ValueError: if the binding specified does not have multiple
     parts.

    """
    binding = get(binding_name)
    parts = binding.split('+')
    if len(parts) == 1:
        _logger.warning(
            "Key binding '%s' does not have a tap part.", binding_name)
        return parts[0]
    return parts[-1]


def _get_compiz_keybinding(compiz_tuple):
    """Given a keybinding name, get the keybinding string from the compiz
    option.

    :raises ValueError: if the compiz setting described does not hold a
     keybinding.
    :raises RuntimeError: if the compiz keybinding has been disabled.
    :returns: compiz keybinding

    """
    plugin_name, setting_name = compiz_tuple
    plugin = _get_compiz_plugin(plugin_name)
    setting = _get_compiz_setting(plugin_name, setting_name)
    if setting.Type != 'Key':
        raise ValueError(
            "Key binding maps to a compiz option that does not hold a "
            "keybinding.")
    if not plugin.Enabled:
        _logger.warning(
            "Returning keybinding for '%s' which is in un-enabled plugin '%s'",
            setting.ShortDesc,
            plugin.ShortDesc)
    if setting.Value == "Disabled":
        raise RuntimeError(
            "Keybinding '%s' in compiz plugin '%s' has been disabled." %
            (setting.ShortDesc, plugin.ShortDesc))

    return _translate_compiz_keystroke_string(setting.Value)


def _translate_compiz_keystroke_string(keystroke_string):
    """Get a string representing the keystroke stored in *keystroke_string*.

    The returned value is suitable for passing into the Keyboard emulator.

    :param string keystroke_string: A compizconfig-style keystroke string.

    """
    if not isinstance(keystroke_string, str):
        raise TypeError("keystroke string must be a string.")

    translations = {
        'Control': 'Ctrl',
        'Primary': 'Ctrl',
    }
    regex = re.compile('[<>]')
    parts = regex.split(keystroke_string)
    result = []
    for part in parts:
        part = part.strip()
        if part != "" and not part.isspace():
            translated = translations.get(part, part)
            if translated not in result:
                result.append(translated)

    return '+'.join(result)


class KeybindingsHelper(object):

    """A helper class that makes it easier to use Unity keybindings."""

    @property
    def _keyboard(self):
        return Keyboard.create()

    def keybinding(self, binding_name, delay=None):
        """Press and release the keybinding with the given name.

        If set, the delay parameter will override the default delay set by the
        keyboard emulator.

        """
        if delay is not None and type(delay) != float:
            raise TypeError(
                "delay parameter must be a float if it is defined.")
        if delay:
            self._keyboard.press_and_release(get(binding_name), delay)
        else:
            self._keyboard.press_and_release(get(binding_name))

    def keybinding_hold(self, binding_name):
        """Hold down the hold-part of a keybinding."""
        self._keyboard.press(get_hold_part(binding_name))

    def keybinding_release(self, binding_name):
        """Release the hold-part of a keybinding."""
        self._keyboard.release(get_hold_part(binding_name))

    def keybinding_tap(self, binding_name):
        """Tap the tap-part of a keybinding."""
        self._keyboard.press_and_release(get_tap_part(binding_name))

    def keybinding_hold_part_then_tap(self, binding_name):
        self.keybinding_hold(binding_name)
        self.keybinding_tap(binding_name)


# Functions that wrap compizconfig to avoid some unpleasantness in that module.
# Local to the this keybindings for now until their removal in the very near
# future.
_global_compiz_context = None


def _get_global_compiz_context():
    """Get the compizconfig global context object.

    :returns: global compiz context, either already defined or from compiz
              config

    """
    global _global_compiz_context
    if _global_compiz_context is None:
        with Silence():
            from compizconfig import Context
            _global_compiz_context = Context()
    return _global_compiz_context


def _get_compiz_plugin(plugin_name):
    """Get a compizconfig plugin with the specified name.

    :raises KeyError: if the plugin named does not exist.
    :returns: compizconfig plugin

    """
    ctx = _get_global_compiz_context()
    with Silence():
        try:
            return ctx.Plugins[plugin_name]
        except KeyError:
            raise KeyError(
                "Compiz plugin '%s' does not exist." % (plugin_name))


def _get_compiz_setting(plugin_name, setting_name):
    """Get a compizconfig setting object, given a plugin name and setting name.

    :raises KeyError: if the plugin or setting is not found.
    :returns: compiz setting object

    """
    plugin = _get_compiz_plugin(plugin_name)
    with Silence():
        try:
            return plugin.Screen[setting_name]
        except KeyError:
            raise KeyError(
                "Compiz setting '%s' does not exist in plugin '%s'." %
                (setting_name, plugin_name))