diff options
author | Anthony LaTorre <tlatorre9@gmail.com> | 2011-08-05 18:28:23 -0400 |
---|---|---|
committer | Anthony LaTorre <tlatorre9@gmail.com> | 2011-08-05 18:28:23 -0400 |
commit | 643f3df7b8538d5c52ea782ec3c22406cadc7c6e (patch) | |
tree | c93b44285888c2500fd0d85d58ca688db2ebe9b7 /spnav.py | |
parent | 97467c888720451e97dcd0881d90f61641f43b47 (diff) | |
parent | d7f835b3325611ad25209c9a25256b46d4944827 (diff) | |
download | chroma-643f3df7b8538d5c52ea782ec3c22406cadc7c6e.tar.gz chroma-643f3df7b8538d5c52ea782ec3c22406cadc7c6e.tar.bz2 chroma-643f3df7b8538d5c52ea782ec3c22406cadc7c6e.zip |
merge heads
Diffstat (limited to 'spnav.py')
-rw-r--r-- | spnav.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/spnav.py b/spnav.py new file mode 100644 index 0000000..9079114 --- /dev/null +++ b/spnav.py @@ -0,0 +1,80 @@ +from ctypes import cdll, c_int, c_uint, c_void_p, byref, Structure, Union + +libspnav = cdll.LoadLibrary('libspnav.so') + +SPNAV_EVENT_ANY = 0 +SPNAV_EVENT_MOTION = 1 +SPNAV_EVENT_BUTTON = 2 + +class spnav_event_motion(Structure): + _fields_ = [('type', c_int), + ('x', c_int), + ('y', c_int), + ('z', c_int), + ('rx', c_int), + ('ry', c_int), + ('rz', c_int), + ('period', c_uint), + ('data', c_void_p)] + +class spnav_event_button(Structure): + _fields_ = [('type', c_int), + ('press', c_int), + ('bnum', c_int)] + +class spnav_event(Union): + _fields_ = [('type', c_int), + ('motion', spnav_event_motion), + ('button', spnav_event_button) ] + + def __str__(self): + if self.type == SPNAV_EVENT_ANY: + return 'SPNAV_EVENT_ANY' + elif self.type == SPNAV_EVENT_MOTION: + m = self.motion + return 'SPNAV_EVENT_MOTION t(%d,%d,%d) r(%d,%d,%d)' % (m.x, m.y, m.z, m.rx, m.ry, m.rz) + elif self.type == SPNAV_EVENT_BUTTON: + if self.button.press: + state = 'down' + else: + state = 'up' + return 'SPNAV_EVENT_BUTTON %d %s' % (self.button.bnum, state) + +def spnav_open(): + if libspnav.spnav_open() == -1: + raise Exception('failed to connect to the space navigator daemon') + +def spnav_close(): + libspnav.spnav_close() + +def spnav_fd(): + return libspnav.spnav_fd() + +def spnav_wait_event(): + ev = spnav_event() + ret = libspnav.spnav_wait_event(byref(ev)) + if ret: + return ev + else: + raise Exception('non-zero return code from spnav_wait_event()') + +def spnav_poll_event(): + ev = spnav_event() + ret = libspnav.spnav_poll_event(byref(ev)) + if ret == 0: + return None + else: + return ev + +def spnav_remove_events(event_type): + return libspnav.spnav_remove_events(event_type) + +if __name__ == '__main__': + spnav_open() + try: + while True: + ev = spnav_wait_event() + print ev + finally: + spnav_close() + |