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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# libavg - Media Playback Engine.
# Copyright (C) 2003-2014 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Current versions can be found at www.libavg.de
#
# Original author of this file is OXullo Interecans <x at brainrapers dot org>
from collections import namedtuple
from libavg import avg, player
IGNORED_KEYMODS = avg.KEYMOD_NUM
KEYMOD_ANY = -1
LOGCAT = avg.logger.configureCategory('KEYBOARDMANAGER',
avg.logger.Severity.WARN)
class KeyboardManagerPublisher(avg.Publisher):
BINDINGS_UPDATED = avg.Publisher.genMessageID()
def __init__(self):
super(KeyboardManagerPublisher, self).__init__()
self.publish(self.BINDINGS_UPDATED)
def notifyUpdate(self):
self.notifySubscribers(self.BINDINGS_UPDATED, [])
publisher = KeyboardManagerPublisher()
_KeyBinding = namedtuple('_KeyBinding',
['keystring', 'handler', 'help', 'modifiers', 'type'])
_modifiedKeyBindings = []
_plainKeyBindings = []
_plainKeyBindingsStack = []
_isEnabled = True
def init():
player.subscribe(player.KEY_DOWN, _onKeyDown)
player.subscribe(player.KEY_UP, _onKeyUp)
avg.logger.debug('Keyboardmanager initialized', LOGCAT)
def bindKeyDown(keystring, handler, help, modifiers=avg.KEYMOD_NONE):
_bindKey(keystring, handler, help, modifiers, avg.KEYDOWN)
def bindKeyUp(keystring, handler, help, modifiers=avg.KEYMOD_NONE):
_bindKey(keystring, handler, help, modifiers, avg.KEYUP)
def unbindKeyUp(keystring, modifiers=avg.KEYMOD_NONE):
_unbindKey(keystring, modifiers, avg.KEYUP)
def unbindKeyDown(keystring, modifiers=avg.KEYMOD_NONE):
_unbindKey(keystring, modifiers, avg.KEYDOWN)
def unbindAll():
global _modifiedKeyBindings, _plainKeyBindings, _plainKeyBindingsStack
_modifiedKeyBindings = []
_plainKeyBindings = []
_plainKeyBindingsStack = []
publisher.notifyUpdate()
def push():
'''
Push the current non-modified defined key bindings to the stack
'''
global _plainKeyBindings
_plainKeyBindingsStack.append(_plainKeyBindings)
_plainKeyBindings = []
publisher.notifyUpdate()
def pop():
'''
Pop from the stack the current non-modified defined key bindings
'''
global _plainKeyBindings
_plainKeyBindings = _plainKeyBindingsStack.pop()
publisher.notifyUpdate()
def getCurrentBindings():
return _modifiedKeyBindings + _plainKeyBindings
def enable():
global _isEnabled
_isEnabled = True
def disable():
global _isEnabled
_isEnabled = False
def _bindKey(keystring, handler, help, modifiers, type_):
if type(keystring) == unicode:
keystring = keystring.encode('utf8')
avg.logger.info('Binding key <%s> (mod:%s) to handler %s (%s)' % (keystring,
modifiers, handler, type), LOGCAT)
_checkDuplicates(keystring, modifiers, type_)
keyBinding = _KeyBinding(keystring, handler, help, modifiers, type_)
if modifiers != avg.KEYMOD_NONE:
_modifiedKeyBindings.append(keyBinding)
else:
_plainKeyBindings.append(keyBinding)
publisher.notifyUpdate()
def _findAndRemoveKeybinding(keystring, modifiers, type, list):
for keybinding in list:
if keybinding.keystring == keystring and \
keybinding.modifiers == modifiers and \
keybinding.type == type:
list.remove(keybinding)
break;
def _unbindKey(keystring, modifiers, type_):
if type(keystring) == unicode:
keystring = keystring.encode('utf8')
avg.logger.info('Unbinding key <%s> (mod:%s) (%s)' % (keystring,
modifiers, type), LOGCAT)
if modifiers != avg.KEYMOD_NONE:
_findAndRemoveKeybinding(keystring, modifiers, type_, _modifiedKeyBindings)
else:
_findAndRemoveKeybinding(keystring, modifiers, type_, _plainKeyBindings)
publisher.notifyUpdate()
def _onKeyDown(event):
if _isEnabled:
_processEvent(event, avg.KEYDOWN)
def _onKeyUp(event):
if _isEnabled:
_processEvent(event, avg.KEYUP)
def _testModifiers(mod1, mod2):
if mod1 == KEYMOD_ANY or mod2 == KEYMOD_ANY:
return True
mod1 &= ~IGNORED_KEYMODS
mod2 &= ~IGNORED_KEYMODS
return mod1 == mod2 or mod1 & mod2
def _testPatternMatch(pattern, text):
if pattern in ('shift', 'alt', 'ctrl', 'meta', 'super'):
return pattern in text
else:
return False
def _testMatchString(keyBinding, keyString, type_):
sameType = keyBinding.type == type_
patternMatch = _testPatternMatch(keyBinding.keystring, keyString)
directMatch = keyBinding.keystring == keyString
return sameType and (directMatch or patternMatch)
def _testMatchEvent(keyBinding, event, type_):
if not _testModifiers(event.modifiers, keyBinding.modifiers):
return False
if _testMatchString(keyBinding, event.keystring, type_):
return True
if type_ == avg.KEYDOWN:
return _testMatchString(keyBinding,
unichr(event.unicode).encode('utf8'), type_)
else:
return False
def _processEvent(event, type_):
avg.logger.debug('Processing event keystring=%s '
'modifiers=%s type=%s' % (event.keystring, event.modifiers, event.type),
LOGCAT)
for keyBinding in _plainKeyBindings + _modifiedKeyBindings:
if _testMatchEvent(keyBinding, event, type_):
avg.logger.debug(' Found keyBinding=%s' % (keyBinding,), LOGCAT)
keyBinding.handler()
return
def _checkDuplicates(keystring, modifiers, type_):
for keyBinding in _plainKeyBindings + _modifiedKeyBindings:
if (_testModifiers(keyBinding.modifiers, modifiers) and
_testMatchString(keyBinding, keystring, type_)):
raise RuntimeError('Key binding keystring=%s modifiers=%s type=%s '
'already defined' % (keystring, modifiers, type_))
|