Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/view/keyhandler.py
blob: 39ff835f4648fc45e67af1c55616234e8787539b (plain)
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
# Copyright (C) 2006-2007, Red Hat, Inc.
# Copyright (C) 2009 Simon Schampijer
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import subprocess
import logging

import dbus
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GConf

from gi.repository import SugarExt

from jarabe.model import sound
from jarabe.model import shell
from jarabe.model import session
from jarabe.view.tabbinghandler import TabbingHandler
from jarabe.model.shell import ShellModel
from jarabe import config
from jarabe.journal import journalactivity


_VOLUME_STEP = sound.VOLUME_STEP
_VOLUME_MAX = 100
_TABBING_MODIFIER = Gdk.ModifierType.MOD1_MASK

_actions_table = {
    'F1': 'zoom_mesh',
    'F2': 'zoom_group',
    'F3': 'zoom_home',
    'F4': 'zoom_activity',
    'F5': 'open_search',
    'F6': 'frame',
    'XF86AudioMute': 'volume_mute',
    'F11': 'volume_down',
    'XF86AudioLowerVolume': 'volume_down',
    'F12': 'volume_up',
    'XF86AudioRaiseVolume': 'volume_up',
    '<alt>F11': 'volume_min',
    '<alt>F12': 'volume_max',
    'XF86MenuKB': 'frame',
    '<alt>Tab': 'next_window',
    '<alt><shift>Tab': 'previous_window',
    '<alt>Escape': 'close_window',
    'XF86WebCam': 'open_search',
# the following are intended for emulator users
    '<alt><shift>f': 'frame',
    '<alt><shift>q': 'quit_emulator',
    'XF86Search': 'open_search',
    '<alt><shift>o': 'open_search'
}


_instance = None


class KeyHandler(object):
    def __init__(self, frame):
        self._frame = frame
        self._key_pressed = None
        self._keycode_pressed = 0
        self._keystate_pressed = 0
        self._key_handlers_active = True

        self._key_grabber = SugarExt.KeyGrabber()
        self._key_grabber.connect('key-pressed',
                                  self._key_pressed_cb)
        self._key_grabber.connect('key-released',
                                  self._key_released_cb)

        self._tabbing_handler = TabbingHandler(self._frame, _TABBING_MODIFIER)

        for f in os.listdir(os.path.join(config.ext_path, 'globalkey')):
            if f.endswith('.py') and not f.startswith('__'):
                module_name = f[:-3]
                try:
                    logging.debug('Loading module %r', module_name)
                    module = __import__('globalkey.' + module_name, globals(),
                                        locals(), [module_name])
                    for key in module.BOUND_KEYS:
                        if key in _actions_table:
                            raise ValueError('Key %r is already bound' % key)
                        _actions_table[key] = module
                except Exception:
                    logging.exception('Exception while loading extension:')

        self._key_grabber.grab_keys(_actions_table.keys())

    def _change_volume(self, step=None, value=None):
        if step is not None:
            volume = sound.get_volume() + step
        elif value is not None:
            volume = value

        volume = min(max(0, volume), _VOLUME_MAX)

        sound.set_volume(volume)
        sound.set_muted(volume == 0)

    def handle_previous_window(self, event_time):
        self._tabbing_handler.previous_activity(event_time)

    def handle_next_window(self, event_time):
        self._tabbing_handler.next_activity(event_time)

    def handle_close_window(self, event_time):
        active_activity = shell.get_model().get_active_activity()
        if active_activity.is_journal():
            return

        active_activity.get_window().close()

    def handle_zoom_mesh(self, event_time):
        shell.get_model().set_zoom_level(ShellModel.ZOOM_MESH, event_time)

    def handle_zoom_group(self, event_time):
        shell.get_model().set_zoom_level(ShellModel.ZOOM_GROUP, event_time)

    def handle_zoom_home(self, event_time):
        shell.get_model().set_zoom_level(ShellModel.ZOOM_HOME, event_time)

    def handle_zoom_activity(self, event_time):
        shell.get_model().set_zoom_level(ShellModel.ZOOM_ACTIVITY, event_time)

    def handle_volume_max(self, event_time):
        self._change_volume(value=_VOLUME_MAX)

    def handle_volume_min(self, event_time):
        self._change_volume(value=0)

    def handle_volume_mute(self, event_time):
        if sound.get_muted() is True:
            sound.set_muted(False)
        else:
            sound.set_muted(True)

    def handle_volume_up(self, event_time):
        self._change_volume(step=_VOLUME_STEP)

    def handle_volume_down(self, event_time):
        self._change_volume(step=-_VOLUME_STEP)

    def handle_frame(self, event_time):
        self._frame.notify_key_press()

    def handle_quit_emulator(self, event_time):
        session.get_session_manager().shutdown()

    def handle_open_search(self, event_time):
        journalactivity.get_journal().show_journal()

    def handle_accumulate_osk(self, event_time):
        from jarabe.model.shell import get_model
        if get_model().get_active_activity().get_bundle_id() == 'org.laptop.AbiWordActivity':
            return

        # If we are not in ebook-mode, do not do anything.
        is_ebook_mode = False

        command = 'evtest --query /dev/input/event4 EV_SW SW_TABLET_MODE; echo $?'
        try:
            return_code = subprocess.Popen([command],
                                           stdout=subprocess.PIPE,
                                           shell=True).stdout.readlines()[0].rstrip('\n')
            if return_code == '10':
                is_ebook_mode = True
        except Exception, e:
            logging.exception(e)

        if not is_ebook_mode:
            return

        screen = Gdk.Screen.get_default()
        active_window = screen.get_active_window()

        screen_width  = screen.get_width()
        screen_height = screen.get_height()

        client = GConf.Client.get_default()
        if screen_width > screen_height:
            factor = client.get_float('/desktop/sugar/graphics/window_osk_scaling_factor')
        else:
            factor = client.get_float('/desktop/sugar/graphics/window_osk_scaling_factor_in_portrait_mode')

        active_window.resize(screen_width, screen_height * factor)

    def handle_unaccumulate_osk(self, event_time):
        screen = Gdk.Screen.get_default()
        active_window = screen.get_active_window()
        active_window.resize(screen.get_width(), screen.get_height())

    def _key_pressed_cb(self, grabber, keycode, state, event_time):
        key = grabber.get_key(keycode, state)
        logging.debug('_key_pressed_cb: %i %i %s', keycode, state, key)
        if key is not None:
            self._key_pressed = key
            self._keycode_pressed = keycode
            self._keystate_pressed = state

            action = _actions_table[key]
            if self._tabbing_handler.is_tabbing():
                # Only accept window tabbing events, everything else
                # cancels the tabbing operation.
                if not action in ['next_window', 'previous_window']:
                    self._tabbing_handler.stop(event_time)
                    return True

            if hasattr(action, 'handle_key_press'):
                action.handle_key_press(key)
            elif isinstance(action, basestring):
                if not self._key_handlers_active:
                    return

                method = getattr(self, 'handle_' + action)
                method(event_time)
            else:
                raise TypeError('Invalid action %r' % action)

            return True
        else:
            # If this is not a registered key, then cancel tabbing.
            if self._tabbing_handler.is_tabbing():
                if not grabber.is_modifier(keycode):
                    self._tabbing_handler.stop(event_time)
                return True

        return False

    def _key_released_cb(self, grabber, keycode, state, event_time):
        if not self._key_handlers_active:
            return

        logging.debug('_key_released_cb: %i %i', keycode, state)
        if self._tabbing_handler.is_tabbing():
            # We stop tabbing and switch to the new window as soon as the
            # modifier key is raised again.
            if grabber.is_modifier(keycode, mask=_TABBING_MODIFIER):
                self._tabbing_handler.stop(event_time)

            return True
        return False


def setup(frame):
    global _instance

    if _instance:
        del _instance

    _instance = KeyHandler(frame)


def set_key_handlers_active(active):
    """
    The setup(frame) is already run at sugar-session startup.
    So, we can safely assume the "_instance" is fully-grown up.
    """

    _instance._key_handlers_active = active


def get_handle_accumulate_osk_func():
    return _instance.handle_accumulate_osk

def get_handle_unaccumulate_osk_func():
    return _instance.handle_unaccumulate_osk