listenkbd.py 9.61 KB
Newer Older
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
# Copyright (c) 2010 Pablo Seminario <pabluk@gmail.com>
# 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/>.

import threading
import time
import sys
import subprocess
import modmap
import gtk

from Xlib import X, XK, display
from Xlib.ext import record
from Xlib.protocol import rq

MODE_RAW = 0
MODE_NORMAL = 1

REPLACE_KEYS = {
    'XK_Escape':_('Esc '),
    'XK_Tab':u'\u21B9 ',
    'XK_Return':u'\u23CE ',
    'XK_Space':u' ',
    'XK_Caps_Lock':_('Caps '),
    'XK_F1':u'F1 ',
    'XK_F2':u'F2 ',
    'XK_F3':u'F3 ',
    'XK_F4':u'F4 ',
    'XK_F5':u'F5 ',
    'XK_F6':u'F6 ',
    'XK_F7':u'F7 ',
    'XK_F8':u'F8 ',
    'XK_F9':u'F9 ',
    'XK_F10':u'F10 ',
    'XK_F11':u'F11 ',
    'XK_F12':u'F12 ',
    'XK_Home':_('Home '),
    'XK_Up':u'\u2191',
    'XK_Page_Up':_('PgUp '),
    'XK_Left':u'\u2190',
    'XK_Right':u'\u2192',
    'XK_End':_('End '),
    'XK_Down':u'\u2193',
    'XK_Next':_('PgDn '),
    'XK_Insert':_('Ins '),
    'XK_Delete':_('Del '),
    'XK_KP_Home':u'(7)',
    'XK_KP_Up':u'(8)',
    'XK_KP_Prior':u'(9)',
    'XK_KP_Left':u'(4)',
    'XK_KP_Right':u'(6)',
    'XK_KP_End':u'(1)',
    'XK_KP_Down':u'(2)',
    'XK_KP_Page_Down':u'(3)',
    'XK_KP_Begin':u'(5)',
    'XK_KP_Insert':u'(0)',
    'XK_KP_Delete':u'(.)',
    'XK_KP_Add':u'(+)',
    'XK_KP_Subtract':u'(-)',
    'XK_KP_Multiply':u'(*)',
    'XK_KP_Divide':u'(/)',
    'XK_Num_Lock':u'NumLock ',
    'XK_KP_Enter':u'\u23CE ',
}

class ListenKbd(threading.Thread):

    def __init__(self, label, logger, mode):
        threading.Thread.__init__(self)
        self.mode = mode
        self.logger = logger
        self.label = label
        self.text = ""
        self.command = None
        self.shift = None
        self.cmd_keys = {
            'shift': False,
            'ctrl': False,
            'alt': False,
            'capslock': False,
            'meta': False,
            'super':False
            }

        self.logger.debug("Thread created")
        self.keymap = modmap.get_keymap_table()
        self.modifiers = modmap.get_modifier_map()

        self.local_dpy = display.Display()
        self.record_dpy = display.Display()

        if not self.record_dpy.has_extension("RECORD"):
            self.logger.error("RECORD extension not found.")
            print "RECORD extension not found"
            sys.exit(1)

        self.ctx = self.record_dpy.record_create_context(
                0,
                [record.AllClients],
                [{
                        'core_requests': (0, 0),
                        'core_replies': (0, 0),
                        'ext_requests': (0, 0, 0, 0),
                        'ext_replies': (0, 0, 0, 0),
                        'delivered_events': (0, 0),
                        'device_events': (X.KeyPress, X.KeyRelease),
                        'errors': (0, 0),
                        'client_started': False,
                        'client_died': False,
                }])

    def run(self):
        self.logger.debug("Thread started.")
        self.record_dpy.record_enable_context(self.ctx, self.key_press)

    def lookup_keysym(self, keysym):
        for name in dir(XK):
            if name[:3] == "XK_" and getattr(XK, name) == keysym:
                return name[3:]
        return ""

    def replace_key(self, key, keysym):
        for name in dir(XK):
            if name[:3] == "XK_" and getattr(XK, name) == keysym:
                if name in REPLACE_KEYS:
                    return REPLACE_KEYS[name]

    def update_text(self, string=None):
        gtk.gdk.threads_enter()
        if not string is None:
            self.text = "%s%s" % (self.label.get_text(), string)
            self.label.set_text(self.text)
        else:
            self.label.set_text("")
        gtk.gdk.threads_leave()
        self.label.emit("text-changed")

    def key_press(self, reply):

        # FIXME:
        # This is not the most efficient way to detect the
        # use of sudo/gksudo but it works.
        sudo_is_running = subprocess.call(['ps', '-C', 'sudo'],
                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        if not sudo_is_running:
            return

        if reply.category != record.FromServer:
            return
        if reply.client_swapped:
            self.logger.warning(
                "* received swapped protocol data, cowardly ignored"
            )
            return
        if not len(reply.data) or ord(reply.data[0]) < 2:
            # not an event
            return
        data = reply.data
        key = None
        while len(data):
            event, data = rq.EventField(None).parse_binary_value(data,
                                    self.record_dpy.display, None, None)
            if event.type in [X.KeyPress, X.KeyRelease]:
                if self.mode == MODE_NORMAL:
                    key = self.key_normal_mode(event)
                if self.mode == MODE_RAW:
                    key = self.key_raw_mode(event)
                if not key:
                    return
        self.update_text(key)

    def key_normal_mode(self, event):
        key = ''
        mod = ''
        keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)

        if event.detail in self.keymap:
            key_normal, key_shift, key_dead, key_deadshift = \
                                            self.keymap[event.detail]
            self.logger.debug("Key %s(keycode) %s. Symbols %s" %
                (event.detail,
                 event.type == X.KeyPress and "pressed" or "released",
                 self.keymap[event.detail])
                )
        else:
            self.logger.debug('No mapping for scan_code %d' % event.detail)
            return


        # Alt key
        if event.detail in self.modifiers['mod1']:
            if event.type == X.KeyPress:
                self.cmd_keys['alt'] = True
            else:
                self.cmd_keys['alt'] = False
            return
        # Meta key
        # Fixme: it must use self.modifiers['mod5']
        #        but doesn't work
        if event.detail == 108:
            if event.type == X.KeyPress:
                self.cmd_keys['meta'] = True
            else:
                self.cmd_keys['meta'] = False
            return
        # Super key
        if event.detail in self.modifiers['mod4']:
            if event.type == X.KeyPress:
                self.cmd_keys['super'] = True
            else:
                self.cmd_keys['super'] = False
            return
        # Ctrl keys
        elif event.detail in self.modifiers['control']:
            if event.type == X.KeyPress:
                self.cmd_keys['ctrl'] = True
            else:
                self.cmd_keys['ctrl'] = False
            return
        # Shift keys
        elif event.detail in self.modifiers['shift']:
            if event.type == X.KeyPress:
                self.cmd_keys['shift'] = True
            else:
                self.cmd_keys['shift'] = False
            return
        # Capslock key
        elif event.detail in self.modifiers['lock']:
            if event.type == X.KeyPress:
                if self.cmd_keys['capslock']:
                    self.cmd_keys['capslock'] = False
                else:
                    self.cmd_keys['capslock'] = True
            return
        # Backspace key
        elif event.detail == 22 and event.type == X.KeyPress:
            gtk.gdk.threads_enter()
            if len(self.label.get_text()) > 0:
                self.label.set_text(
                    unicode(self.label.get_text(), 'utf-8')[:-1]
                )
                key = ""
                gtk.gdk.threads_leave()
            else:
                gtk.gdk.threads_leave()
                return
        else:
            if event.type == X.KeyPress:
                key = key_normal
                if self.cmd_keys['ctrl']:
                    mod = mod + _("Ctrl+")
                if self.cmd_keys['alt']:
                    mod = mod + _("Alt+")
                if self.cmd_keys['super']:
                    mod = mod + _("Super+")

                if self.cmd_keys['shift']:
                    key = key_shift
                if self.cmd_keys['capslock'] \
                    and ord(key_normal) in range(97,123):
                    key = key_shift
                if self.cmd_keys['meta']:
                    key = key_dead
                if self.cmd_keys['shift'] and self.cmd_keys['meta']:
                    key = key_deadshift

                string = self.replace_key(key, keysym)
                if string:
                    key = string

                if mod != '':
                    key = "%s%s " % (mod, key)
                else:
                    key = "%s%s" % (mod, key)
            else:
                return

        return key

    def key_raw_mode(self, event):
        key = ''
        if event.type == X.KeyPress:
            keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)
            key = self.lookup_keysym(keysym)
        else:
            return
        return key

    def stop(self):
        self.local_dpy.record_disable_context(self.ctx)
        self.local_dpy.flush()
        self.record_dpy.record_free_context(self.ctx)
        self.logger.debug("Thread stopped.")