Advertisement
Guest User

Toggles Enabled/Disabled state of X11 input devices

a guest
Jun 2nd, 2014
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # x11devprop.py
  3. """
  4.     (C) 2014 G. Bege <therion@ninth-art.de>
  5.     Created on: 02nd June 2014
  6.     Modified on:    03rd June 2014
  7.     Licensed under the GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  8.    
  9.     Description
  10.     -------------------
  11.     This script calls the X11 API in order to change
  12.     an input device property.
  13.     However its only as much implemented as necessary
  14.     in order to enable/disable a device.
  15.    
  16.     Dependencies
  17.     ------------------------
  18.     X Input 1.5 required
  19.    
  20.     Note
  21.     --------
  22.     Tested against Xorg 7.4 @ Gentoo
  23.     For device ID's use xinput
  24. """
  25. import atexit
  26. from sys import exit, argv
  27. from ctypes import *
  28. cdll.LoadLibrary("libX11.so.6") # XopenDisplay, XCloseDisplay, XInternAtom
  29. cdll.LoadLibrary("libXi.so.6")  # XOpenDevice, XCloseDevice
  30.                 # XiGetDeviceProperty, XiSetDeviceProperty
  31. libx11 = CDLL("libX11.so.6")
  32. libxi = CDLL("libXi.so.6")
  33. Display = Device = 0x0
  34. Options = ("Enable", "Disable", "Toggle")
  35. Version = 0.112
  36.  
  37. Success = 0             # <X11/X.h>
  38. PropModeReplace = 0         # <X11/X.h>
  39. AnyPropertyType = c_long(0)     # <X11/X.h>
  40.  
  41. # Atom defined as CARD32        <X11/X.h>
  42. # CARD32 defined as unsigned int    <X11/Xmd.h>
  43. def Atom(I):
  44.     return c_uint(I)
  45.  
  46. def Cleanup():
  47.     global Display, Device
  48.     if Device != 0x0:   libxi.XCloseDevice(Display, Device);
  49.     if Display != 0x0:  libx11.XCloseDisplay(Display);
  50. atexit.register(Cleanup)
  51.  
  52. # @description      Invokes X11 device with "Device Enabled" Atom
  53. # @param    devid   device id
  54. # @param    value   property value
  55. # @note     this function accepts a toggle value 0xff
  56. def X11DeviceEnable(devid, value):
  57.     global Display, Device
  58.     assert isinstance(devid, int)
  59.    
  60.     Display = libx11.XOpenDisplay(None)
  61.     if Display == 0x0:
  62.         print "Error: Unable to open X11 display."
  63.         exit(1)
  64.    
  65.     Device = libxi.XOpenDevice(Display, c_uint(devid))
  66.     if Device == 0x0:
  67.         print "Error: Unable to open device (%d)." % devid
  68.         exit(1)
  69.    
  70.     # retrieve current device properties
  71.     prop = libx11.XInternAtom(Display, "Device Enabled", False)
  72.     type = Atom(0)
  73.     format = c_int(0)
  74.     act_nitems = bytes_after = c_ulong(0)
  75.     data = c_int(0)
  76.     ret = libxi.XGetDeviceProperty(Display, Device, prop, 0, 1, False, AnyPropertyType,
  77.             byref(type), byref(format), byref(act_nitems),
  78.             byref(bytes_after), byref(data));
  79.     if ret != Success:
  80.         print "Failed to get property type and format for Deviceice (%d)." % devid
  81.         exit(1)
  82.     curstate = c_byte.from_address(data.value)
  83.    
  84.     # Note: we only support a single value
  85.     assert isinstance(value, int)
  86.     nelem = 1
  87.     data = (c_int*nelem)()
  88.    
  89.     # 0xff will act like a switch on the state
  90.     if value == 0xff:
  91.         if curstate.value == 1: data[0] = 0;
  92.         else:           data[0] = 1;
  93.     else:
  94.         data[0] = value
  95.    
  96.     ret = libxi.XChangeDeviceProperty(Display, Device, prop, type,  format,
  97.                         PropModeReplace, data, nelem);
  98.     if ret != Success:
  99.         print "Failed to set property:%c, type:%d, format:%d on device %d." % (prop, type, format, devid)
  100.         exit(1)
  101.  
  102. if __name__ == '__main__':
  103.     if len(argv) < 3 or argv[2] not in Options:
  104.         exit('Usage: %s <devid> (Enable|Disable|Toggle)' % argv[0])
  105.     devid = int(argv[1])
  106.    
  107.     if argv[2] == "Enable":     X11DeviceEnable(devid, 0x1);
  108.     elif argv[2] == "Toggle":   X11DeviceEnable(devid, 0xff);
  109.     else:               X11DeviceEnable(devid, 0x0);
  110.    
  111. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement