]> git.sesse.net Git - vlc/blob - modules/control/unimotion.c
Should fix compilation
[vlc] / modules / control / unimotion.c
1  /*
2  *  UniMotion - Unified Motion detection for Apple portables.
3  *
4  *  Copyright (c) 2006 Lincoln Ramsay. All rights reserved.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License version 2.1 as published by the Free Software Foundation.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation Inc. 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
18  */
19
20 /*
21  * HISTORY of Motion
22  * Written by Christian Klein
23  * Modified for iBook compatibility by Pall Thayer
24  * Modified for Hi Res Powerbook compatibility by Pall Thayer
25  * Modified for MacBook Pro compatibility by Randy Green
26  * Disparate forks unified into UniMotion by Lincoln Ramsay
27  */
28
29 // This license applies to the portions created by Cristian Klein.
30 /* motion.c
31  *
32  * a little program to display the coords returned by
33  * the powerbook motion sensor
34  *
35  * A fine piece of c0de, brought to you by
36  *
37  *               ---===---
38  * *** teenage mutant ninja hero coders ***
39  *               ---===---
40  *
41  * All of the software included is copyrighted by Christian Klein <chris@5711.org>.
42  *
43  * Copyright 2005 Christian Klein. All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. The name of the author must not be used to endorse or promote
54  *    products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  */
70
71 #ifdef __APPLE__
72
73 #include "unimotion.h"
74 #include <IOKit/IOKitLib.h>
75 #include <CoreFoundation/CoreFoundation.h>
76 #include <string.h>
77 #include <stdint.h>
78
79 enum data_type {
80     PB_IB,
81     MBP
82 };
83
84 struct pb_ib_data {
85     int8_t x;
86     int8_t y;
87     int8_t z;
88     int8_t pad[57];
89 };
90
91 struct mbp_data {
92     int16_t x;
93     int16_t y;
94     int16_t z;
95     int8_t pad[34];
96 };
97
98 union motion_data {
99     struct pb_ib_data pb_ib;
100     struct mbp_data mbp;
101 };
102
103
104 static int set_values(int type, int *kernFunc, char **servMatch, int *dataType)
105 {
106     switch ( type ) {
107         case powerbook:
108             *kernFunc = 21;
109             *servMatch = "IOI2CMotionSensor";
110             *dataType = PB_IB;
111             break;
112         case ibook:
113             *kernFunc = 21;
114             *servMatch = "IOI2CMotionSensor";
115             *dataType = PB_IB;
116             break;
117         case highrespb:
118             *kernFunc = 21;
119             *servMatch = "PMUMotionSensor";
120             *dataType = PB_IB;
121             break;
122         case macbookpro:
123             *kernFunc = 5;
124             *servMatch = "SMCMotionSensor";
125             *dataType = MBP;
126             break;
127         default:
128             return 0;
129     }
130
131     return 1;
132 }
133
134 static int probe_sms(int kernFunc, char *servMatch, int dataType, void *data)
135 {
136     kern_return_t result;
137     mach_port_t masterPort;
138     io_iterator_t iterator;
139     io_object_t aDevice;
140     io_connect_t  dataPort;
141
142     IOItemCount structureInputSize;
143     IOByteCount structureOutputSize;
144
145     union motion_data inputStructure;
146     union motion_data *outputStructure;
147
148     outputStructure = (union motion_data *)data;
149
150     result = IOMasterPort(MACH_PORT_NULL, &masterPort);
151
152     CFMutableDictionaryRef matchingDictionary = IOServiceMatching(servMatch);
153
154     result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
155
156     if (result != KERN_SUCCESS) {
157         //fputs("IOServiceGetMatchingServices returned error.\n", stderr);
158         return 0;
159     }
160
161     aDevice = IOIteratorNext(iterator);
162     IOObjectRelease(iterator);
163
164     if (aDevice == 0) {
165         //fputs("No motion sensor available\n", stderr);
166         return 0;
167     }
168
169     result = IOServiceOpen(aDevice, mach_task_self(), 0, &dataPort);
170     IOObjectRelease(aDevice);
171
172     if (result != KERN_SUCCESS) {
173         //fputs("Could not open motion sensor device\n", stderr);
174         return 0;
175     }
176
177     switch ( dataType ) {
178         case PB_IB:
179             structureInputSize = sizeof(struct pb_ib_data);
180             structureOutputSize = sizeof(struct pb_ib_data);
181             break;
182         case MBP:
183             structureInputSize = sizeof(struct mbp_data);
184             structureOutputSize = sizeof(struct mbp_data);
185             break;
186         default:
187             return 0;
188     }
189
190     memset(&inputStructure, 0, sizeof(union motion_data));
191     memset(outputStructure, 0, sizeof(union motion_data));
192
193     result = IOConnectMethodStructureIStructureO(dataPort, kernFunc, structureInputSize,
194                 &structureOutputSize, &inputStructure, outputStructure);
195
196     IOServiceClose(dataPort);
197
198     if (result != KERN_SUCCESS) {
199         //puts("no coords");
200         return 0;
201     }
202     return 1;
203 }
204
205 int detect_sms()
206 {
207     int kernFunc;
208     char *servMatch;
209     int dataType;
210     union motion_data data;
211     int i;
212
213     for ( i = 1; ; i++ ) {
214         if ( !set_values(i, &kernFunc, &servMatch, &dataType) )
215             break;
216         if ( probe_sms(kernFunc, servMatch, dataType, &data) )
217             return i;
218     }
219
220     return unknown;
221 }
222
223 int read_sms_raw(int type, int *x, int *y, int *z)
224 {
225     int kernFunc;
226     char *servMatch;
227     int dataType;
228     union motion_data data;
229
230     if ( !set_values(type, &kernFunc, &servMatch, &dataType) )
231         return 0;
232     if ( probe_sms(kernFunc, servMatch, dataType, &data) ) {
233         switch ( dataType ) {
234             case PB_IB:
235                 if ( x ) *x = data.pb_ib.x;
236                 if ( y ) *y = data.pb_ib.y;
237                 if ( z ) *z = data.pb_ib.z;
238                 break;
239             case MBP:
240                 if ( x ) *x = data.mbp.x;
241                 if ( y ) *y = data.mbp.y;
242                 if ( z ) *z = data.mbp.z;
243                 break;
244             default:
245                 return 0;
246         }
247         return 1;
248     }
249     return 0;
250 }
251
252 int read_sms(int type, int *x, int *y, int *z)
253 {
254     int _x, _y, _z;
255         int xoff, yoff, zoff;
256         Boolean ok;
257     int ret;
258     
259     ret = read_sms_raw(type, &_x, &_y, &_z);
260     if ( !ret )
261         return 0;
262
263         CFStringRef app = CFSTR("com.ramsayl.UniMotion");
264         CFStringRef xoffstr = CFSTR("x_offset");
265         CFStringRef yoffstr = CFSTR("y_offset");
266         CFStringRef zoffstr = CFSTR("z_offset");
267         xoff = CFPreferencesGetAppIntegerValue(xoffstr, app, &ok);
268         if ( ok ) _x += xoff;
269         yoff = CFPreferencesGetAppIntegerValue(yoffstr, app, &ok);
270         if ( ok ) _y += yoff;
271         zoff = CFPreferencesGetAppIntegerValue(zoffstr, app, &ok);
272         if ( ok ) _z += zoff;
273         
274         *x = _x;
275         *y = _y;
276         *z = _z;
277
278     return ret;
279 }
280
281 int read_sms_real(int type, double *x, double *y, double *z)
282 {
283     int _x, _y, _z;
284         int xscale, yscale, zscale;
285     int ret;
286         Boolean ok;
287     
288     ret = read_sms_raw(type, &_x, &_y, &_z);
289     if ( !ret )
290         return 0;
291
292         static CFStringRef app = CFSTR("com.ramsayl.UniMotion");
293         static CFStringRef xscalestr = CFSTR("x_scale");
294         static CFStringRef yscalestr = CFSTR("y_scale");
295         static CFStringRef zscalestr = CFSTR("z_scale");
296         xscale = CFPreferencesGetAppIntegerValue(xscalestr, app, &ok);
297         if ( !ok ) return 0;
298         yscale = CFPreferencesGetAppIntegerValue(yscalestr, app, &ok);
299         if ( !ok ) return 0;
300         zscale = CFPreferencesGetAppIntegerValue(zscalestr, app, &ok);
301         if ( !ok ) return 0;
302         
303     *x = _x / (double)xscale;
304     *y = _y / (double)yscale;
305     *z = _z / (double)zscale;
306         
307     return 1;
308 }
309
310 #endif