| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
#ifdef __APPLE__ |
|---|
| 72 |
|
|---|
| 73 |
#include "unimotion.h" |
|---|
| 74 |
#include <IOKit/IOKitLib.h> |
|---|
| 75 |
#include <CoreFoundation/CoreFoundation.h> |
|---|
| 76 |
#include <stdint.h> |
|---|
| 77 |
|
|---|
| 78 |
enum data_type { |
|---|
| 79 |
PB_IB, |
|---|
| 80 |
MBP |
|---|
| 81 |
}; |
|---|
| 82 |
|
|---|
| 83 |
struct pb_ib_data { |
|---|
| 84 |
int8_t x; |
|---|
| 85 |
int8_t y; |
|---|
| 86 |
int8_t z; |
|---|
| 87 |
int8_t pad[57]; |
|---|
| 88 |
}; |
|---|
| 89 |
|
|---|
| 90 |
struct mbp_data { |
|---|
| 91 |
int16_t x; |
|---|
| 92 |
int16_t y; |
|---|
| 93 |
int16_t z; |
|---|
| 94 |
int8_t pad[34]; |
|---|
| 95 |
}; |
|---|
| 96 |
|
|---|
| 97 |
union motion_data { |
|---|
| 98 |
struct pb_ib_data pb_ib; |
|---|
| 99 |
struct mbp_data mbp; |
|---|
| 100 |
}; |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
static int set_values(int type, int *kernFunc, char **servMatch, int *dataType) |
|---|
| 104 |
{ |
|---|
| 105 |
switch ( type ) { |
|---|
| 106 |
case powerbook: |
|---|
| 107 |
*kernFunc = 21; |
|---|
| 108 |
*servMatch = "IOI2CMotionSensor"; |
|---|
| 109 |
*dataType = PB_IB; |
|---|
| 110 |
break; |
|---|
| 111 |
case ibook: |
|---|
| 112 |
*kernFunc = 21; |
|---|
| 113 |
*servMatch = "IOI2CMotionSensor"; |
|---|
| 114 |
*dataType = PB_IB; |
|---|
| 115 |
break; |
|---|
| 116 |
case highrespb: |
|---|
| 117 |
*kernFunc = 21; |
|---|
| 118 |
*servMatch = "PMUMotionSensor"; |
|---|
| 119 |
*dataType = PB_IB; |
|---|
| 120 |
break; |
|---|
| 121 |
case macbookpro: |
|---|
| 122 |
*kernFunc = 5; |
|---|
| 123 |
*servMatch = "SMCMotionSensor"; |
|---|
| 124 |
*dataType = MBP; |
|---|
| 125 |
break; |
|---|
| 126 |
default: |
|---|
| 127 |
return 0; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
return 1; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
static int probe_sms(int kernFunc, char *servMatch, int dataType, void *data) |
|---|
| 134 |
{ |
|---|
| 135 |
kern_return_t result; |
|---|
| 136 |
mach_port_t masterPort; |
|---|
| 137 |
io_iterator_t iterator; |
|---|
| 138 |
io_object_t aDevice; |
|---|
| 139 |
io_connect_t dataPort; |
|---|
| 140 |
|
|---|
| 141 |
IOItemCount structureInputSize; |
|---|
| 142 |
IOByteCount structureOutputSize; |
|---|
| 143 |
|
|---|
| 144 |
union motion_data inputStructure; |
|---|
| 145 |
union motion_data *outputStructure; |
|---|
| 146 |
|
|---|
| 147 |
outputStructure = (union motion_data *)data; |
|---|
| 148 |
|
|---|
| 149 |
result = IOMasterPort(MACH_PORT_NULL, &masterPort); |
|---|
| 150 |
|
|---|
| 151 |
CFMutableDictionaryRef matchingDictionary = IOServiceMatching(servMatch); |
|---|
| 152 |
|
|---|
| 153 |
result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); |
|---|
| 154 |
|
|---|
| 155 |
if (result != KERN_SUCCESS) { |
|---|
| 156 |
|
|---|
| 157 |
return 0; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
aDevice = IOIteratorNext(iterator); |
|---|
| 161 |
IOObjectRelease(iterator); |
|---|
| 162 |
|
|---|
| 163 |
if (aDevice == 0) { |
|---|
| 164 |
|
|---|
| 165 |
return 0; |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
result = IOServiceOpen(aDevice, mach_task_self(), 0, &dataPort); |
|---|
| 169 |
IOObjectRelease(aDevice); |
|---|
| 170 |
|
|---|
| 171 |
if (result != KERN_SUCCESS) { |
|---|
| 172 |
|
|---|
| 173 |
return 0; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
switch ( dataType ) { |
|---|
| 177 |
case PB_IB: |
|---|
| 178 |
structureInputSize = sizeof(struct pb_ib_data); |
|---|
| 179 |
structureOutputSize = sizeof(struct pb_ib_data); |
|---|
| 180 |
break; |
|---|
| 181 |
case MBP: |
|---|
| 182 |
structureInputSize = sizeof(struct mbp_data); |
|---|
| 183 |
structureOutputSize = sizeof(struct mbp_data); |
|---|
| 184 |
break; |
|---|
| 185 |
default: |
|---|
| 186 |
return 0; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
memset(&inputStructure, 0, sizeof(union motion_data)); |
|---|
| 190 |
memset(outputStructure, 0, sizeof(union motion_data)); |
|---|
| 191 |
|
|---|
| 192 |
result = IOConnectMethodStructureIStructureO(dataPort, kernFunc, structureInputSize, |
|---|
| 193 |
&structureOutputSize, &inputStructure, outputStructure); |
|---|
| 194 |
|
|---|
| 195 |
IOServiceClose(dataPort); |
|---|
| 196 |
|
|---|
| 197 |
if (result != KERN_SUCCESS) { |
|---|
| 198 |
|
|---|
| 199 |
return 0; |
|---|
| 200 |
} |
|---|
| 201 |
return 1; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
int detect_sms() |
|---|
| 205 |
{ |
|---|
| 206 |
int kernFunc; |
|---|
| 207 |
char *servMatch; |
|---|
| 208 |
int dataType; |
|---|
| 209 |
union motion_data data; |
|---|
| 210 |
int i; |
|---|
| 211 |
|
|---|
| 212 |
for ( i = 1; ; i++ ) { |
|---|
| 213 |
if ( !set_values(i, &kernFunc, &servMatch, &dataType) ) |
|---|
| 214 |
break; |
|---|
| 215 |
if ( probe_sms(kernFunc, servMatch, dataType, &data) ) |
|---|
| 216 |
return i; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
return unknown; |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
int read_sms_raw(int type, int *x, int *y, int *z) |
|---|
| 223 |
{ |
|---|
| 224 |
int kernFunc; |
|---|
| 225 |
char *servMatch; |
|---|
| 226 |
int dataType; |
|---|
| 227 |
union motion_data data; |
|---|
| 228 |
|
|---|
| 229 |
if ( !set_values(type, &kernFunc, &servMatch, &dataType) ) |
|---|
| 230 |
return 0; |
|---|
| 231 |
if ( probe_sms(kernFunc, servMatch, dataType, &data) ) { |
|---|
| 232 |
switch ( dataType ) { |
|---|
| 233 |
case PB_IB: |
|---|
| 234 |
if ( x ) *x = data.pb_ib.x; |
|---|
| 235 |
if ( y ) *y = data.pb_ib.y; |
|---|
| 236 |
if ( z ) *z = data.pb_ib.z; |
|---|
| 237 |
break; |
|---|
| 238 |
case MBP: |
|---|
| 239 |
if ( x ) *x = data.mbp.x; |
|---|
| 240 |
if ( y ) *y = data.mbp.y; |
|---|
| 241 |
if ( z ) *z = data.mbp.z; |
|---|
| 242 |
break; |
|---|
| 243 |
default: |
|---|
| 244 |
return 0; |
|---|
| 245 |
} |
|---|
| 246 |
return 1; |
|---|
| 247 |
} |
|---|
| 248 |
return 0; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
int read_sms(int type, int *x, int *y, int *z) |
|---|
| 252 |
{ |
|---|
| 253 |
int _x, _y, _z; |
|---|
| 254 |
int xoff, yoff, zoff; |
|---|
| 255 |
Boolean ok; |
|---|
| 256 |
int ret; |
|---|
| 257 |
|
|---|
| 258 |
ret = read_sms_raw(type, &_x, &_y, &_z); |
|---|
| 259 |
if ( !ret ) |
|---|
| 260 |
return 0; |
|---|
| 261 |
|
|---|
| 262 |
static CFStringRef app = CFSTR("com.ramsayl.UniMotion"); |
|---|
| 263 |
static CFStringRef xoffstr = CFSTR("x_offset"); |
|---|
| 264 |
static CFStringRef yoffstr = CFSTR("y_offset"); |
|---|
| 265 |
static CFStringRef zoffstr = CFSTR("z_offset"); |
|---|
| 266 |
xoff = CFPreferencesGetAppIntegerValue(xoffstr, app, &ok); |
|---|
| 267 |
if ( ok ) _x += xoff; |
|---|
| 268 |
yoff = CFPreferencesGetAppIntegerValue(yoffstr, app, &ok); |
|---|
| 269 |
if ( ok ) _y += yoff; |
|---|
| 270 |
zoff = CFPreferencesGetAppIntegerValue(zoffstr, app, &ok); |
|---|
| 271 |
if ( ok ) _z += zoff; |
|---|
| 272 |
|
|---|
| 273 |
*x = _x; |
|---|
| 274 |
*y = _y; |
|---|
| 275 |
*z = _z; |
|---|
| 276 |
|
|---|
| 277 |
return ret; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
int read_sms_real(int type, double *x, double *y, double *z) |
|---|
| 281 |
{ |
|---|
| 282 |
int _x, _y, _z; |
|---|
| 283 |
int xscale, yscale, zscale; |
|---|
| 284 |
int ret; |
|---|
| 285 |
Boolean ok; |
|---|
| 286 |
|
|---|
| 287 |
ret = read_sms_raw(type, &_x, &_y, &_z); |
|---|
| 288 |
if ( !ret ) |
|---|
| 289 |
return 0; |
|---|
| 290 |
|
|---|
| 291 |
static CFStringRef app = CFSTR("com.ramsayl.UniMotion"); |
|---|
| 292 |
static CFStringRef xscalestr = CFSTR("x_scale"); |
|---|
| 293 |
static CFStringRef yscalestr = CFSTR("y_scale"); |
|---|
| 294 |
static CFStringRef zscalestr = CFSTR("z_scale"); |
|---|
| 295 |
xscale = CFPreferencesGetAppIntegerValue(xscalestr, app, &ok); |
|---|
| 296 |
if ( !ok ) return 0; |
|---|
| 297 |
yscale = CFPreferencesGetAppIntegerValue(yscalestr, app, &ok); |
|---|
| 298 |
if ( !ok ) return 0; |
|---|
| 299 |
zscale = CFPreferencesGetAppIntegerValue(zscalestr, app, &ok); |
|---|
| 300 |
if ( !ok ) return 0; |
|---|
| 301 |
|
|---|
| 302 |
*x = _x / (double)xscale; |
|---|
| 303 |
*y = _y / (double)yscale; |
|---|
| 304 |
*z = _z / (double)zscale; |
|---|
| 305 |
|
|---|
| 306 |
return 1; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
#endif |
|---|