<commit_before>#!/usr/bin/env python
""" evdev.py

This is a Python interface to the Linux input system's event device.
Events can be read from an open event file and decoded into spiffy
python objects. The Event objects can optionally be fed into a Device
object that represents the complete state of the device being monitored.

Copyright (C) 2003-2004 <NAME> <<EMAIL>>

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""

import struct, sys, os, time
from fcntl import ioctl

__all__ = ["Event", "Device"]

def demo():
    """Open the event device named on the command line, use incoming
       events to update a device, and show the state of this device.
       """
    dev = Device(sys.argv[1])
    while 1:
        dev.poll()
        time.sleep(0.1)
        print dev


class BaseDevice:
    """Base class representing the state of an input device, with axes and buttons.
       Event instances can be fed into the Device to update its state.
       """
    def __init__(self):
        self.axes = {}
        self.buttons = {}
        self.name = None

    def __repr__(self):
        return "<Device name=%r axes=%r buttons=%r>" % (
            self.name, self.axes, self.buttons)

    def update(self, event):
        f = getattr(self, "update_%s" % event.type, None)
        if f:
            f(event)

    def update_EV_KEY(self, event):
        self.buttons[event.code] = event.value

    def update_EV_ABS(self, event):
        self.axes[event.code] = event.value

    def update_EV_REL(self, event):
        self.axes[event.code] = self.axes.get(event.code, 0) + event.value

    def __getitem__(self, name):
        """Retrieve the current value of an axis or button,
           or zero if no data has been received for it yet.
           """
        if name in self.axes:
            return self.axes[name]
        else:
            return self.buttons.get(name, 0)


# evdev ioctl constants. The horrible mess here
# is to silence silly FutureWarnings
EVIOCGNAME_512 = ~int(~0x82004506L & 0xFFFFFFFFL)
EVIOCGID       = ~int(~0x80084502L & 0xFFFFFFFFL)
EVIOCGBIT_512  = ~int(~0x81fe4520L & 0xFFFFFFFFL)
EVIOCGABS_512  = ~int(~0x80144540L & 0xFFFFFFFFL)


class Device(BaseDevice):
    """An abstract input device attached to a Linux evdev device node"""
    def __init__(self, filename):
        BaseDevice.__init__(self)
        self.fd = os.open(filename, os.O_RDWR | os.O_NONBLOCK)
        self.packetSize = struct.calcsize(Event.format)
        self.readMetadata()

    def poll(self):
        """Receive and process all available input events"""
        while 1:
            try:
                buffer = os.read(self.fd, self.packetSize)
            except OSError:
                return
            self.update(Event(unpack=buffer))

    def loop(self):
        """Receive and process all available input events"""
        while 1:
            try:
                buffer = os.read(self.fd, self.packetSize)
                yield Event(unpack=buffer)
            except OSError:
                time.sleep(0.5)

    def readMetadata(self):
        """Read device identity and capabilities via ioctl()"""<commit_msg>Reduce delay<commit_after>#!/usr/bin/env python
""" evdev.py

This is a Python interface to the Linux input system's event device.
Events can be read from an open event file and decoded into spiffy
python objects. The Event objects can optionally be fed into a Device
object that represents the complete state of the device being monitored.

Copyright (C) 2003-2004 <NAME> <<EMAIL>>

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""

import struct, sys, os, time
from fcntl import ioctl

__all__ = ["Event", "Device"]

def demo():
    """Open the event device named on the command line, use incoming
       events to update a device, and show the state of this device.
       """
    dev = Device(sys.argv[1])
    while 1:
        dev.poll()
        time.sleep(0.1)
        print dev


class BaseDevice:
    """Base class representing the state of an input device, with axes and buttons.
       Event instances can be fed into the Device to update its state.
       """
    def __init__(self):
        self.axes = {}
        self.buttons = {}
        self.name = None

    def __repr__(self):
        return "<Device name=%r axes=%r buttons=%r>" % (
            self.name, self.axes, self.buttons)

    def update(self, event):
        f = getattr(self, "update_%s" % event.type, None)
        if f:
            f(event)

    def update_EV_KEY(self, event):
        self.buttons[event.code] = event.value

    def update_EV_ABS(self, event):
        self.axes[event.code] = event.value

    def update_EV_REL(self, event):
        self.axes[event.code] = self.axes.get(event.code, 0) + event.value

    def __getitem__(self, name):
        """Retrieve the current value of an axis or button,
           or zero if no data has been received for it yet.
           """
        if name in self.axes:
            return self.axes[name]
        else:
            return self.buttons.get(name, 0)


# evdev ioctl constants. The horrible mess here
# is to silence silly FutureWarnings
EVIOCGNAME_512 = ~int(~0x82004506L & 0xFFFFFFFFL)
EVIOCGID       = ~int(~0x80084502L & 0xFFFFFFFFL)
EVIOCGBIT_512  = ~int(~0x81fe4520L & 0xFFFFFFFFL)
EVIOCGABS_512  = ~int(~0x80144540L & 0xFFFFFFFFL)


class Device(BaseDevice):
    """An abstract input device attached to a Linux evdev device node"""
    def __init__(self, filename):
        BaseDevice.__init__(self)
        self.fd = os.open(filename, os.O_RDWR | os.O_NONBLOCK)
        self.packetSize = struct.calcsize(Event.format)
        self.readMetadata()

    def poll(self):
        """Receive and process all available input events"""
        while 1:
            try:
                buffer = os.read(self.fd, self.packetSize)
            except OSError:
                return
            self.update(Event(unpack=buffer))

    def loop(self):
        """Receive and process all available input events"""
        while 1:
            try:
                buffer = os.read(self.fd, self.packetSize)
                yield Event(unpack=buffer)
            except OSError:
                time.sleep(0.1)

    def readMetadata(self):
        """Read device identity and capabilities via ioctl()"""
{'fraction_non_alphanumeric': 0.08732581287325814, 'fraction_numerical': 0.021234240212342402, 'mean_word_length': 2.6195965417867435, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875517', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2124, 'n_words': 828}