Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # x11devprop.py
- """
- (C) 2014 G. Bege <therion@ninth-art.de>
- Created on: 02nd June 2014
- Modified on: 03rd June 2014
- Licensed under the GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
- Description
- -------------------
- This script calls the X11 API in order to change
- an input device property.
- However its only as much implemented as necessary
- in order to enable/disable a device.
- Dependencies
- ------------------------
- X Input 1.5 required
- Note
- --------
- Tested against Xorg 7.4 @ Gentoo
- For device ID's use xinput
- """
- import atexit
- from sys import exit, argv
- from ctypes import *
- cdll.LoadLibrary("libX11.so.6") # XopenDisplay, XCloseDisplay, XInternAtom
- cdll.LoadLibrary("libXi.so.6") # XOpenDevice, XCloseDevice
- # XiGetDeviceProperty, XiSetDeviceProperty
- libx11 = CDLL("libX11.so.6")
- libxi = CDLL("libXi.so.6")
- Display = Device = 0x0
- Options = ("Enable", "Disable", "Toggle")
- Version = 0.112
- Success = 0 # <X11/X.h>
- PropModeReplace = 0 # <X11/X.h>
- AnyPropertyType = c_long(0) # <X11/X.h>
- # Atom defined as CARD32 <X11/X.h>
- # CARD32 defined as unsigned int <X11/Xmd.h>
- def Atom(I):
- return c_uint(I)
- def Cleanup():
- global Display, Device
- if Device != 0x0: libxi.XCloseDevice(Display, Device);
- if Display != 0x0: libx11.XCloseDisplay(Display);
- atexit.register(Cleanup)
- # @description Invokes X11 device with "Device Enabled" Atom
- # @param devid device id
- # @param value property value
- # @note this function accepts a toggle value 0xff
- def X11DeviceEnable(devid, value):
- global Display, Device
- assert isinstance(devid, int)
- Display = libx11.XOpenDisplay(None)
- if Display == 0x0:
- print "Error: Unable to open X11 display."
- exit(1)
- Device = libxi.XOpenDevice(Display, c_uint(devid))
- if Device == 0x0:
- print "Error: Unable to open device (%d)." % devid
- exit(1)
- # retrieve current device properties
- prop = libx11.XInternAtom(Display, "Device Enabled", False)
- type = Atom(0)
- format = c_int(0)
- act_nitems = bytes_after = c_ulong(0)
- data = c_int(0)
- ret = libxi.XGetDeviceProperty(Display, Device, prop, 0, 1, False, AnyPropertyType,
- byref(type), byref(format), byref(act_nitems),
- byref(bytes_after), byref(data));
- if ret != Success:
- print "Failed to get property type and format for Deviceice (%d)." % devid
- exit(1)
- curstate = c_byte.from_address(data.value)
- # Note: we only support a single value
- assert isinstance(value, int)
- nelem = 1
- data = (c_int*nelem)()
- # 0xff will act like a switch on the state
- if value == 0xff:
- if curstate.value == 1: data[0] = 0;
- else: data[0] = 1;
- else:
- data[0] = value
- ret = libxi.XChangeDeviceProperty(Display, Device, prop, type, format,
- PropModeReplace, data, nelem);
- if ret != Success:
- print "Failed to set property:%c, type:%d, format:%d on device %d." % (prop, type, format, devid)
- exit(1)
- if __name__ == '__main__':
- if len(argv) < 3 or argv[2] not in Options:
- exit('Usage: %s <devid> (Enable|Disable|Toggle)' % argv[0])
- devid = int(argv[1])
- if argv[2] == "Enable": X11DeviceEnable(devid, 0x1);
- elif argv[2] == "Toggle": X11DeviceEnable(devid, 0xff);
- else: X11DeviceEnable(devid, 0x0);
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement