"Fossies" - the Fresh Open Source Software Archive

Member "screenkey-1.1/Screenkey/xlib.py" (7 May 2018, 13363 Bytes) of package /linux/privat/screenkey-1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "xlib.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.9_vs_1.0.

    1 # -*- coding: utf-8 -*-
    2 # Distributed under the GNU GPLv3+ license, WITHOUT ANY WARRANTY.
    3 # Copyright(c) 2015: wave++ "Yuri D'Elia" <wavexx@thregr.org>
    4 
    5 from __future__ import unicode_literals
    6 from ctypes import *
    7 
    8 ## base X11
    9 libX11 = CDLL('libX11.so.6')
   10 
   11 # types
   12 Atom = c_ulong
   13 Bool = c_int
   14 XID = c_ulong
   15 Colormap = XID
   16 Cursor = XID
   17 KeyCode = c_ubyte
   18 KeySym = XID
   19 Pixmap = XID
   20 Status = c_int
   21 String = c_char_p
   22 Time = c_ulong
   23 Window = XID
   24 
   25 class Display(Structure):
   26     pass
   27 
   28 class Visual(Structure):
   29     pass
   30 
   31 class XKeyEvent(Structure):
   32     _fields_ = [('type', c_int),
   33                 ('serial', c_ulong),
   34                 ('send_event', Bool),
   35                 ('display', POINTER(Display)),
   36                 ('window', Window),
   37                 ('root', Window),
   38                 ('subwindow', Window),
   39                 ('time', Time),
   40                 ('x', c_int),
   41                 ('y', c_int),
   42                 ('x_root', c_int),
   43                 ('y_root', c_int),
   44                 ('state', c_uint),
   45                 ('keycode', c_uint),
   46                 ('same_screen', Bool)]
   47 
   48 XKeyPressedEvent = XKeyEvent
   49 XKeyReleasedEvent = XKeyEvent
   50 
   51 class XButtonEvent(Structure):
   52     _fields_ = [('type', c_int),
   53                 ('serial', c_ulong),
   54                 ('send_event', Bool),
   55                 ('display', POINTER(Display)),
   56                 ('window', Window),
   57                 ('root', Window),
   58                 ('subwindow', Window),
   59                 ('time', Time),
   60                 ('x', c_int),
   61                 ('y', c_int),
   62                 ('x_root', c_int),
   63                 ('y_root', c_int),
   64                 ('state', c_uint),
   65                 ('button', c_uint),
   66                 ('same_screen', Bool)]
   67 
   68 class XMotionEvent(Structure):
   69     _fields_ = [('type', c_int),
   70                 ('serial', c_ulong),
   71                 ('send_event', Bool),
   72                 ('display', POINTER(Display)),
   73                 ('window', Window),
   74                 ('root', Window),
   75                 ('subwindow', Window),
   76                 ('time', Time),
   77                 ('x', c_int),
   78                 ('y', c_int),
   79                 ('x_root', c_int),
   80                 ('y_root', c_int),
   81                 ('state', c_uint),
   82                 ('is_hint', c_ubyte),
   83                 ('same_screen', Bool)]
   84 
   85 class XClientMessageEvent(Structure):
   86     _fields_ = [('type', c_int),
   87                 ('serial', c_ulong),
   88                 ('send_event', Bool),
   89                 ('display', POINTER(Display)),
   90                 ('window', Window),
   91                 ('message_type', Atom),
   92                 ('format', c_int),
   93                 ('data', c_long * 5)]
   94 
   95 class XEvent(Union):
   96     _fields_ = [('type', c_int),
   97                 ('xkey', XKeyEvent),
   98                 ('xbutton', XButtonEvent),
   99                 ('xmotion', XMotionEvent),
  100                 ('xclient', XClientMessageEvent),
  101                 ('pad', c_long * 24)]
  102 
  103 class XSetWindowAttributes(Structure):
  104     _fields_ = [('background_pixmap', Pixmap),
  105                 ('background_pixel', c_ulong),
  106                 ('border_pixmap', Pixmap),
  107                 ('border_pixel', c_ulong),
  108                 ('bit_gravity', c_int),
  109                 ('win_gravity', c_int),
  110                 ('backing_store', c_int),
  111                 ('backing_planes', c_ulong),
  112                 ('backing_pixel', c_ulong),
  113                 ('save_under', Bool),
  114                 ('event_mask', c_long),
  115                 ('do_not_propagate_mask', c_long),
  116                 ('override_redirect', Bool),
  117                 ('colormap', Colormap),
  118                 ('cursor', Cursor)]
  119 
  120 
  121 # constants
  122 KeyPress = 2
  123 KeyRelease = 3
  124 ButtonPress = 4
  125 ButtonRelease = 5
  126 MotionNotify = 6
  127 FocusIn = 9
  128 FocusOut = 10
  129 ClientMessage = 33
  130 
  131 CopyFromParent = 0
  132 InputOnly = 2
  133 
  134 CWOverrideRedirect = (1<<9)
  135 
  136 ShiftMask = (1<<0)
  137 LockMask = (1<<1)
  138 ControlMask = (1<<2)
  139 Mod1Mask = (1<<3)
  140 Mod2Mask = (1<<4)
  141 Mod3Mask = (1<<5)
  142 Mod4Mask = (1<<6)
  143 Mod5Mask = (1<<7)
  144 
  145 
  146 # functions
  147 XOpenDisplay = libX11.XOpenDisplay
  148 XOpenDisplay.argtypes = [String]
  149 XOpenDisplay.restype = POINTER(Display)
  150 
  151 XCloseDisplay = libX11.XCloseDisplay
  152 XCloseDisplay.argtypes = [POINTER(Display)]
  153 XCloseDisplay.restype = c_int
  154 
  155 XConnectionNumber = libX11.XConnectionNumber
  156 XConnectionNumber.argtypes = [POINTER(Display)]
  157 XConnectionNumber.restype = c_int
  158 
  159 XInternAtom = libX11.XInternAtom
  160 XInternAtom.argtypes = [POINTER(Display), String, Bool]
  161 XInternAtom.restype = c_int
  162 
  163 XDefaultRootWindow = libX11.XDefaultRootWindow
  164 XDefaultRootWindow.argtypes = [POINTER(Display)]
  165 XDefaultRootWindow.restype = Window
  166 
  167 XCreateWindow = libX11.XCreateWindow
  168 XCreateWindow.argtypes = [POINTER(Display), Window, c_int, c_int, c_uint, c_uint, c_uint, c_int, c_uint, POINTER(Visual), c_ulong, POINTER(XSetWindowAttributes)]
  169 XCreateWindow.restype = Window
  170 
  171 XDestroyWindow = libX11.XDestroyWindow
  172 XDestroyWindow.argtypes = [POINTER(Display), Window]
  173 XDestroyWindow.restype = c_int
  174 
  175 XFree = libX11.XFree
  176 XFree.argtypes = [POINTER(None)]
  177 XFree.restype = c_int
  178 
  179 XNextEvent = libX11.XNextEvent
  180 XNextEvent.argtypes = [POINTER(Display), POINTER(XEvent)]
  181 XNextEvent.restype = c_int
  182 
  183 XPeekEvent = libX11.XPeekEvent
  184 XPeekEvent.argtypes = [POINTER(Display), POINTER(XEvent)]
  185 XPeekEvent.restype = c_int
  186 
  187 XSendEvent = libX11.XSendEvent
  188 XSendEvent.argtypes = [POINTER(Display), Window, c_int, c_long, POINTER(XEvent)]
  189 XSendEvent.restype = c_int
  190 
  191 XFlush = libX11.XFlush
  192 XFlush.argtypes = [POINTER(Display)]
  193 XFlush.restype = c_int
  194 
  195 XPending = libX11.XPending
  196 XPending.argtypes = [POINTER(Display)]
  197 XPending.restype = c_int
  198 
  199 XSynchronize = libX11.XSynchronize
  200 XSynchronize.argtypes = [POINTER(Display), c_int]
  201 XSynchronize.restype = POINTER(CFUNCTYPE(c_int, POINTER(Display)))
  202 
  203 
  204 ## xim
  205 # types
  206 class _XIC(Structure):
  207     pass
  208 
  209 XIC = POINTER(_XIC)
  210 
  211 class _XIM(Structure):
  212     pass
  213 
  214 XIM = POINTER(_XIM)
  215 
  216 class _XrmDatabase(Structure):
  217     pass
  218 
  219 XrmDatabase = POINTER(_XrmDatabase)
  220 
  221 
  222 # constants
  223 XNInputStyle = b'inputStyle'
  224 XNClientWindow = b'clientWindow'
  225 
  226 XIMPreeditNothing = 0x0008
  227 XIMPreeditNone = 0x0010
  228 XIMStatusNothing = 0x0400
  229 XIMStatusNone = 0x0800
  230 
  231 XBufferOverflow = -1
  232 NoSymbol = 0
  233 XLookupNone = 1
  234 XLookupChars = 2
  235 XLookupKeySym = 3
  236 XLookupBoth = 4
  237 
  238 
  239 # functions
  240 XFilterEvent = libX11.XFilterEvent
  241 XFilterEvent.argtypes = [POINTER(XEvent), Window]
  242 XFilterEvent.restype = c_int
  243 
  244 XOpenIM = libX11.XOpenIM
  245 XOpenIM.argtypes = [POINTER(Display), XrmDatabase, String, String]
  246 XOpenIM.restype = XIM
  247 
  248 XCloseIM = libX11.XCloseIM
  249 XCloseIM.argtypes = [XIM]
  250 XCloseIM.restype = c_int
  251 
  252 XCreateIC = libX11.XCreateIC
  253 XCreateIC.restype = XIC
  254 
  255 XDestroyIC = libX11.XDestroyIC
  256 XDestroyIC.argtypes = [XIC]
  257 XDestroyIC.restype = None
  258 
  259 XSetICFocus = libX11.XSetICFocus
  260 XSetICFocus.argtypes = [XIC]
  261 XSetICFocus.restype = None
  262 
  263 Xutf8ResetIC = libX11.Xutf8ResetIC
  264 Xutf8ResetIC.argtypes = [XIC]
  265 Xutf8ResetIC.restype = String
  266 
  267 Xutf8LookupString = libX11.Xutf8LookupString
  268 Xutf8LookupString.argtypes = [XIC, POINTER(XKeyPressedEvent), String, c_int, POINTER(KeySym), POINTER(c_int)]
  269 Xutf8LookupString.restype = c_int
  270 
  271 XKeysymToString = libX11.XKeysymToString
  272 XKeysymToString.argtypes = [KeySym]
  273 XKeysymToString.restype = String
  274 
  275 XkbKeycodeToKeysym = libX11.XkbKeycodeToKeysym
  276 XkbKeycodeToKeysym.argtypes = [POINTER(Display), KeyCode, c_uint, c_uint]
  277 XkbKeycodeToKeysym.restype = KeySym
  278 
  279 
  280 ## record extensions
  281 libXtst = CDLL('libXtst.so.6')
  282 
  283 # types
  284 XPointer = String
  285 XRecordContext = c_ulong
  286 XRecordClientSpec = c_ulong
  287 
  288 class XRecordRange8(Structure):
  289     _fields_ = [('first', c_ubyte),
  290                 ('last', c_ubyte)]
  291 
  292 class XRecordRange16(Structure):
  293     _fields_ = [('first', c_ushort),
  294                 ('last', c_ushort)]
  295 
  296 class XRecordExtRange(Structure):
  297     _fields_ = [('ext_major', XRecordRange8),
  298                 ('ext_minor', XRecordRange16)]
  299 
  300 class XRecordRange(Structure):
  301     _fields_ = [('core_requests', XRecordRange8),
  302                 ('core_replies', XRecordRange8),
  303                 ('ext_requests', XRecordExtRange),
  304                 ('ext_replies', XRecordExtRange),
  305                 ('delivered_events', XRecordRange8),
  306                 ('device_events', XRecordRange8),
  307                 ('errors', XRecordRange8),
  308                 ('client_started', c_int),
  309                 ('client_died', c_int)]
  310 
  311 class XRecordInterceptData(Structure):
  312     _fields_ = [('id_base', XID),
  313                 ('server_time', Time),
  314                 ('client_seq', c_ulong),
  315                 ('category', c_int),
  316                 ('client_swapped', c_int),
  317                 ('data', POINTER(c_ubyte)),
  318                 ('data_len', c_ulong)]
  319 
  320 XRecordInterceptProc = CFUNCTYPE(None, XPointer, POINTER(XRecordInterceptData))
  321 
  322 
  323 # constants
  324 XRecordAllClients = 3
  325 XRecordFromServer = 0
  326 
  327 # functions
  328 XRecordAllocRange = libXtst.XRecordAllocRange
  329 XRecordAllocRange.argtypes = []
  330 XRecordAllocRange.restype = POINTER(XRecordRange)
  331 
  332 XRecordCreateContext = libXtst.XRecordCreateContext
  333 XRecordCreateContext.argtypes = [POINTER(Display), c_int, POINTER(XRecordClientSpec), c_int, POINTER(POINTER(XRecordRange)), c_int]
  334 XRecordCreateContext.restype = XRecordContext
  335 
  336 XRecordEnableContextAsync = libXtst.XRecordEnableContextAsync
  337 XRecordEnableContextAsync.argtypes = [POINTER(Display), XRecordContext, XRecordInterceptProc, XPointer]
  338 XRecordEnableContextAsync.restype = c_int
  339 
  340 XRecordProcessReplies = libXtst.XRecordProcessReplies
  341 XRecordProcessReplies.argtypes = [POINTER(Display)]
  342 XRecordProcessReplies.restype = None
  343 
  344 XRecordDisableContext = libXtst.XRecordDisableContext
  345 XRecordDisableContext.argtypes = [POINTER(Display), XRecordContext]
  346 XRecordDisableContext.restype = c_int
  347 
  348 XRecordFreeContext = libXtst.XRecordFreeContext
  349 XRecordFreeContext.argtypes = [POINTER(Display), XRecordContext]
  350 XRecordFreeContext.restype = c_int
  351 
  352 XRecordFreeData = libXtst.XRecordFreeData
  353 XRecordFreeData.argtypes = [POINTER(XRecordInterceptData)]
  354 XRecordFreeData.restype = None
  355 
  356 
  357 ## wire protocol
  358 CARD8 = c_ubyte
  359 CARD16 = c_ushort
  360 CARD32 = c_uint
  361 BOOL = CARD8
  362 BYTE = CARD8
  363 INT16 = c_short
  364 
  365 class xEventType(Structure):
  366     _fields_ = [('type', BYTE),
  367                 ('detail', BYTE),
  368                 ('sequenceNumber', CARD16)]
  369 
  370 class xKeyButtonPointer(Structure):
  371     _fields_ = [('pad00', CARD32),
  372                 ('time', CARD32),
  373                 ('root', CARD32),
  374                 ('event', CARD32),
  375                 ('child', CARD32),
  376                 ('rootX', INT16),
  377                 ('rootY', INT16),
  378                 ('eventX', INT16),
  379                 ('eventY', INT16),
  380                 ('state', CARD16),
  381                 ('sameScreen', BOOL),
  382                 ('pad1', BYTE)]
  383 
  384 class xEvent(Union):
  385     _fields_ = [('u', xEventType),
  386                 ('keyButtonPointer', xKeyButtonPointer)]
  387 
  388 
  389 def _kbd_wire_to_event(dpy, wev):
  390     ev = XEvent()
  391     ev.xkey.type = wev.u.type
  392     ev.xkey.serial = wev.u.sequenceNumber
  393     ev.xkey.send_event = ((wev.u.type & 0x80) != 0)
  394     ev.xkey.display = dpy
  395     ev.xkey.window = wev.keyButtonPointer.event
  396     ev.xkey.root = wev.keyButtonPointer.root
  397     ev.xkey.subwindow = wev.keyButtonPointer.child
  398     ev.xkey.time = wev.keyButtonPointer.time
  399     ev.xkey.x = wev.keyButtonPointer.eventX
  400     ev.xkey.y = wev.keyButtonPointer.eventY
  401     ev.xkey.x_root = wev.keyButtonPointer.rootX
  402     ev.xkey.y_root = wev.keyButtonPointer.rootY
  403     ev.xkey.state = wev.keyButtonPointer.state
  404     ev.xkey.keycode = wev.u.detail
  405     ev.xkey.same_screen = wev.keyButtonPointer.sameScreen
  406     return ev
  407 
  408 
  409 def _btn_wire_to_event(dpy, wev):
  410     ev = XEvent()
  411     ev.xbutton.type = wev.u.type
  412     ev.xbutton.serial = wev.u.sequenceNumber
  413     ev.xbutton.send_event = ((wev.u.type & 0x80) != 0)
  414     ev.xbutton.display = dpy
  415     ev.xbutton.window = wev.keyButtonPointer.event
  416     ev.xbutton.root = wev.keyButtonPointer.root
  417     ev.xbutton.subwindow = wev.keyButtonPointer.child
  418     ev.xbutton.time = wev.keyButtonPointer.time
  419     ev.xbutton.x = wev.keyButtonPointer.eventX
  420     ev.xbutton.y = wev.keyButtonPointer.eventY
  421     ev.xbutton.x_root = wev.keyButtonPointer.rootX
  422     ev.xbutton.y_root = wev.keyButtonPointer.rootY
  423     ev.xbutton.state = wev.keyButtonPointer.state
  424     ev.xbutton.button = wev.u.detail
  425     ev.xbutton.same_screen = wev.keyButtonPointer.sameScreen
  426     return ev
  427 
  428 
  429 def _mtn_wire_to_event(dpy, wev):
  430     ev = XEvent()
  431     ev.xmotion.type = wev.u.type
  432     ev.xmotion.serial = wev.u.sequenceNumber
  433     ev.xmotion.send_event = ((wev.u.type & 0x80) != 0)
  434     ev.xmotion.display = dpy
  435     ev.xmotion.window = wev.keyButtonPointer.event
  436     ev.xmotion.root = wev.keyButtonPointer.root
  437     ev.xmotion.subwindow = wev.keyButtonPointer.child
  438     ev.xmotion.time = wev.keyButtonPointer.time
  439     ev.xmotion.x = wev.keyButtonPointer.eventX
  440     ev.xmotion.y = wev.keyButtonPointer.eventY
  441     ev.xmotion.x_root = wev.keyButtonPointer.rootX
  442     ev.xmotion.y_root = wev.keyButtonPointer.rootY
  443     ev.xmotion.state = wev.keyButtonPointer.state
  444     ev.xmotion.is_hint = wev.u.detail
  445     ev.xmotion.same_screen = wev.keyButtonPointer.sameScreen
  446     return ev
  447 
  448 
  449 def XWireToEvent(dpy, data):
  450     # this could have been avoided if _XWireToEvent didn't have internal state
  451     wev = cast(data, POINTER(xEvent)).contents
  452     if wev.u.type in [KeyPress, KeyRelease]:
  453         return _kbd_wire_to_event(dpy, wev)
  454     elif wev.u.type in [ButtonPress, ButtonRelease]:
  455         return _btn_wire_to_event(dpy, wev)
  456     elif wev.u.type == MotionNotify:
  457         return _mtn_wire_to_event(dpy, wev)
  458     return XEvent(wev.u.type)