From: Neil Brown Date: Fri, 30 Jan 2009 12:00:25 +0000 (+1100) Subject: Improve the check for "are any keys down" X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=5c83e0a11df0b2b8b2f8a53f1c5605b321d073b0;p=freerunner.git Improve the check for "are any keys down" I'm not sure this test is really needed, but while I still have it, make it as accurate as possible. --- diff --git a/lock/lock.py b/lock/lock.py index 27294af..9adc658 100644 --- a/lock/lock.py +++ b/lock/lock.py @@ -50,7 +50,7 @@ class EvDev: if value == 0: # 'up' - remove from downlist if code in self.downlist: - del self.downlist[self.downlist.index(code)] + self.downlist.remove(code) else: # 'down' - add to downlist if code not in self.downlist: @@ -58,26 +58,45 @@ class EvDev: self.on_event(self.down_count(), typ, code, value) return True def down_count(self): + if len(self.downlist) == 0: + return 0 + # double check, we might have missed an 'up' event somehow. + try: + rv = fcntl.ioctl(self.f, EVIOCGKEY(768/8), (768/8) * " " ) + l = len(rv)/4 + ra = struct.unpack('%dI' % l, rv) + isup = [] + for k in self.downlist: + by = int(k/8) + bt = k % 8 + if by < l and ((ra[by] >> bt) & 1) == 0: + isup.append[k] + for k in isup: + self.downlist.remove(k) + except: + pass return len(self.downlist) def grab(self): if self.grabbed: return #print "grab" - fcntl.ioctl(self.f, EVIOC_GRAB, 1) + fcntl.ioctl(self.f, EVIOCGRAB, 1) self.grabbed = True def ungrab(self): if not self.grabbed: return #print "release" - fcntl.ioctl(self.f, EVIOC_GRAB, 0) + fcntl.ioctl(self.f, EVIOCGRAB, 0) self.grabbed = False FBIOBLANK = 0x4611 FB_BLANK_UNBLANK = 0 FB_BLANK_POWERDOWN = 4 -EVIOC_GRAB = 0x40044590 +EVIOCGRAB = 0x40044590 +def EVIOCGKEY(len): + return 0x80004518 + len * 65536 class Screen: def __init__(self):