| 1 |
import ctypes |
| 2 |
from ctypes import ( |
| 3 |
Structure, cdll, sizeof, |
| 4 |
c_int8, c_int16, c_size_t |
| 5 |
) |
| 6 |
from ctypes.util import find_library |
| 7 |
import platform |
| 8 |
|
| 9 |
ERROR_DICT = { |
| 10 |
"0": "IOKit Framework not found, is this OSX?", |
| 11 |
"-1": "No SMCMotionSensor service", |
| 12 |
"-2": "No sms device", |
| 13 |
"-3": "Could not open motion sensor device", |
| 14 |
"-4": "Did not receive any coordinates" |
| 15 |
} |
| 16 |
|
| 17 |
IOKit = cdll.LoadLibrary(find_library('IOKit')) |
| 18 |
|
| 19 |
|
| 20 |
class data_structure(Structure): |
| 21 |
_fields_ = [ |
| 22 |
('x', c_int16), |
| 23 |
('y', c_int16), |
| 24 |
('z', c_int16), |
| 25 |
('pad', c_int8 * 34), |
| 26 |
] |
| 27 |
|
| 28 |
|
| 29 |
void_p = ctypes.POINTER(ctypes.c_int) |
| 30 |
|
| 31 |
kern_return_t = ctypes.c_int |
| 32 |
KERN_SUCCESS = 0 |
| 33 |
KERN_FUNC = 5 |
| 34 |
|
| 35 |
mach_port_t = void_p |
| 36 |
MACH_PORT_NULL = 0 |
| 37 |
|
| 38 |
io_object_t = ctypes.c_int |
| 39 |
io_object_t = ctypes.c_int |
| 40 |
io_iterator_t = void_p |
| 41 |
io_object_t = void_p |
| 42 |
io_connect_t = void_p |
| 43 |
IOItemCount = ctypes.c_uint |
| 44 |
|
| 45 |
CFMutableDictionaryRef = void_p |
| 46 |
|
| 47 |
|
| 48 |
def is_os_64bit(): |
| 49 |
return platform.machine().endswith('64') |
| 50 |
|
| 51 |
|
| 52 |
def read_sms(): |
| 53 |
result = kern_return_t() |
| 54 |
masterPort = mach_port_t() |
| 55 |
|
| 56 |
result = IOKit.IOMasterPort(MACH_PORT_NULL, ctypes.byref(masterPort)) |
| 57 |
|
| 58 |
IOKit.IOServiceMatching.restype = CFMutableDictionaryRef |
| 59 |
matchingDictionary = IOKit.IOServiceMatching("SMCMotionSensor") |
| 60 |
|
| 61 |
iterator = io_iterator_t() |
| 62 |
result = IOKit.IOServiceGetMatchingServices( |
| 63 |
masterPort, matchingDictionary, |
| 64 |
ctypes.byref(iterator) |
| 65 |
) |
| 66 |
|
| 67 |
if (result != KERN_SUCCESS): |
| 68 |
raise ("No coordinates received!") |
| 69 |
return -1, None |
| 70 |
|
| 71 |
IOKit.IOIteratorNext.restype = io_object_t |
| 72 |
smsDevice = IOKit.IOIteratorNext(iterator) |
| 73 |
|
| 74 |
if not smsDevice: |
| 75 |
return -2, None |
| 76 |
|
| 77 |
dataPort = io_connect_t() |
| 78 |
result = IOKit.IOServiceOpen( |
| 79 |
smsDevice, IOKit.mach_task_self(), |
| 80 |
0, ctypes.byref(dataPort) |
| 81 |
) |
| 82 |
|
| 83 |
if (result != KERN_SUCCESS): |
| 84 |
return -3, None |
| 85 |
|
| 86 |
inStructure = data_structure() |
| 87 |
outStructure = data_structure() |
| 88 |
|
| 89 |
if is_os_64bit() or hasattr(IOKit, 'IOConnectCallStructMethod'): |
| 90 |
structureInSize = IOItemCount(sizeof(data_structure)) |
| 91 |
structureOutSize = c_size_t(sizeof(data_structure)) |
| 92 |
|
| 93 |
result = IOKit.IOConnectCallStructMethod( |
| 94 |
dataPort, KERN_FUNC, |
| 95 |
ctypes.byref(inStructure), structureInSize, |
| 96 |
ctypes.byref(outStructure), ctypes.byref(structureOutSize) |
| 97 |
) |
| 98 |
else: |
| 99 |
structureInSize = IOItemCount(sizeof(data_structure)) |
| 100 |
structureOutSize = IOItemCount(sizeof(data_structure)) |
| 101 |
|
| 102 |
result = IOKit.IOConnectMethodStructureIStructureO( |
| 103 |
dataPort, KERN_FUNC, |
| 104 |
structureInSize, ctypes.byref(structureOutSize), |
| 105 |
ctypes.byref(inStructure), ctypes.byref(outStructure) |
| 106 |
) |
| 107 |
|
| 108 |
IOKit.IOServiceClose(dataPort) |
| 109 |
|
| 110 |
if (result != KERN_SUCCESS): |
| 111 |
return -4, None |
| 112 |
|
| 113 |
return 1, outStructure |
| 114 |
|
| 115 |
|
| 116 |
def get_coord(): |
| 117 |
if not IOKit: |
| 118 |
raise Exception(ERROR_DICT["0"]) |
| 119 |
|
| 120 |
ret, data = read_sms() |
| 121 |
|
| 122 |
if (ret > 0): |
| 123 |
if data.x: |
| 124 |
return (data.x, data.y, data.z) |
| 125 |
else: |
| 126 |
return (None, None, None) |
| 127 |
else: |
| 128 |
raise Exception(ERROR_DICT[str(ret)]) |