This file is indexed.

/usr/share/pyshared/tegakigtk/fakekey.py is in python-tegaki-gtk 0.3.1-1.

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
# -*- coding: utf-8 -*-

# Copyright (C) 2009 The Tegaki project contributors
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Contributors to this file:
# - Mathieu Blondel

"""
Send fake key events in order to display text
where the cursor is currently located.
"""

import time
import os
import platform

if os.name == 'nt':
    from ctypes import *

    PUL = POINTER(c_ulong)
    class KeyBdInput(Structure):
        _fields_ = [("wVk", c_ushort),
                    ("wScan", c_ushort),
                    ("dwFlags", c_ulong),
                    ("time", c_ulong),
                    ("dwExtraInfo", PUL)]

    class HardwareInput(Structure):
        _fields_ = [("uMsg", c_ulong),
                    ("wParamL", c_short),
                    ("wParamH", c_ushort)]

    class MouseInput(Structure):
        _fields_ = [("dx", c_long),
                    ("dy", c_long),
                    ("mouseData", c_ulong),
                    ("dwFlags", c_ulong),
                    ("time",c_ulong),
                    ("dwExtraInfo", PUL)]

    class Input_I(Union):
        _fields_ = [("ki", KeyBdInput),
                    ("mi", MouseInput),
                    ("hi", HardwareInput)]

    class Input(Structure):
        _fields_ = [("type", c_ulong),
                    ("ii", Input_I)]

    INPUT_KEYBOARD = 1
    KEYEVENTF_KEYUP = 0x2
    KEYEVENTF_UNICODE = 0x4

    def _send_unicode_win(unistr):
        for ch in unistr:
            inp = Input()
            inp.type = INPUT_KEYBOARD

            inp.ii.ki.wVk = 0
            inp.ii.ki.wScan = ord(ch)
            inp.ii.ki.dwFlags = KEYEVENTF_UNICODE

            windll.user32.SendInput(1, byref(inp), sizeof(inp))
            
            inp.ii.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP
            windll.user32.SendInput(1, byref(inp), sizeof(inp))

    _send_unicode = _send_unicode_win

elif platform.system() == "Darwin":
    def _send_unicode_osx(unistr):
        # TODO: use CGPostKeyboardEvent?
        raise NotImplementedError

    _send_unicode = _send_unicode_osx

else:

    try:
        import pyatspi
        from gtk import gdk   

        def _send_unicode_atspi(unistr):
            for ch in unistr:
                keyval = gdk.unicode_to_keyval(ord(ch))
                pyatspi.Registry.generateKeyboardEvent(keyval, None,
                                                        pyatspi.KEY_SYM)

        _send_unicode = _send_unicode_atspi

    except ImportError:

        from ctypes import *

        try:
            Xlib = CDLL("libX11.so.6")
            Xtst = CDLL("libXtst.so.6")
            KeySym = c_uint
            Xlib.XGetKeyboardMapping.restype = POINTER(KeySym)
        except OSError:
            Xlib = None

        def _send_unicode_x11(unistr):
            if Xlib is None: raise NameError

            dpy = Xlib.XOpenDisplay(None)

            if not dpy: raise OSError # no display

            min_, max_, numcodes = c_int(0), c_int(0), c_int(0)
            Xlib.XDisplayKeycodes(dpy, byref(min_), byref(max_))

            for ch in unistr:
                sym = Xlib.XStringToKeysym("U" + hex(ord(ch)).replace("0x", ""))

                keysym = Xlib.XGetKeyboardMapping(dpy, min_,
                                                max_.value-min_.value+1,
                                                byref(numcodes))

                keysym[(max_.value-min_.value-1)*numcodes.value] = sym

                Xlib.XChangeKeyboardMapping(dpy,min_,numcodes,keysym,
                                            (max_.value-min_.value))

                Xlib.XFree(keysym)
                Xlib.XFlush(dpy)

                code = Xlib.XKeysymToKeycode(dpy, sym)

                Xtst.XTestFakeKeyEvent(dpy, code, True, 1)
                Xtst.XTestFakeKeyEvent(dpy, code, False, 1)

            Xlib.XFlush(dpy)
            Xlib.XCloseDisplay(dpy)

        _send_unicode = _send_unicode_x11

def send_unicode(unistr):
    assert(isinstance(unistr, unicode))
    try:
        _send_unicode(unistr)
        return True
    except (OSError, NotImplementedError, NameError), e:
        return False
    except e, msg:
        print "send_unicode", e, msg
        return False

if __name__ == "__main__":
    send_unicode(u"漢字")