]> git.sesse.net Git - vlc/blob - modules/control/unimotion.c
leaks in case of http connection failure
[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 <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     size_t structureInputSize;
142     size_t 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         //fputs("IOServiceGetMatchingServices returned error.\n", stderr);
157         return 0;
158     }
159
160     aDevice = IOIteratorNext(iterator);
161     IOObjectRelease(iterator);
162
163     if (aDevice == 0) {
164         //fputs("No motion sensor available\n", stderr);
165         return 0;
166     }
167
168     result = IOServiceOpen(aDevice, mach_task_self(), 0, &dataPort);
169     IOObjectRelease(aDevice);
170
171     if (result != KERN_SUCCESS) {
172         //fputs("Could not open motion sensor device\n", stderr);
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 #ifdef __LP64__
193     result = IOConnectCallStructMethod(dataPort, kernFunc, &inputStructure, 
194                 structureInputSize, outputStructure, &structureOutputSize );
195 #else
196     result = IOConnectMethodStructureIStructureO(dataPort, kernFunc, structureInputSize,
197                 &structureOutputSize, &inputStructure, outputStructure);
198 #endif
199
200     IOServiceClose(dataPort);
201
202     if (result != KERN_SUCCESS) {
203         //puts("no coords");
204         return 0;
205     }
206     return 1;
207 }
208
209 int detect_sms()
210 {
211     int kernFunc;
212     char *servMatch;
213     int dataType;
214     union motion_data data;
215     int i;
216
217     for ( i = 1; ; i++ ) {
218         if ( !set_values(i, &kernFunc, &servMatch, &dataType) )
219             break;
220         if ( probe_sms(kernFunc, servMatch, dataType, &data) )
221             return i;
222     }
223
224     return unknown;
225 }
226
227 int read_sms_raw(int type, int *x, int *y, int *z)
228 {
229     int kernFunc;
230     char *servMatch;
231     int dataType;
232     union motion_data data;
233
234     if ( !set_values(type, &kernFunc, &servMatch, &dataType) )
235         return 0;
236     if ( probe_sms(kernFunc, servMatch, dataType, &data) ) {
237         switch ( dataType ) {
238             case PB_IB:
239                 if ( x ) *x = data.pb_ib.x;
240                 if ( y ) *y = data.pb_ib.y;
241                 if ( z ) *z = data.pb_ib.z;
242                 break;
243             case MBP:
244                 if ( x ) *x = data.mbp.x;
245                 if ( y ) *y = data.mbp.y;
246                 if ( z ) *z = data.mbp.z;
247                 break;
248             default:
249                 return 0;
250         }
251         return 1;
252     }
253     return 0;
254 }
255
256 int read_sms(int type, int *x, int *y, int *z)
257 {
258     int _x, _y, _z;
259     int xoff, yoff, zoff;
260     Boolean ok;
261     int ret;
262  
263     ret = read_sms_raw(type, &_x, &_y, &_z);
264     if ( !ret )
265         return 0;
266
267     static CFStringRef app = CFSTR("com.ramsayl.UniMotion");
268     static CFStringRef xoffstr = CFSTR("x_offset");
269     static CFStringRef yoffstr = CFSTR("y_offset");
270     static CFStringRef zoffstr = CFSTR("z_offset");
271     xoff = CFPreferencesGetAppIntegerValue(xoffstr, app, &ok);
272     if ( ok ) _x += xoff;
273     yoff = CFPreferencesGetAppIntegerValue(yoffstr, app, &ok);
274     if ( ok ) _y += yoff;
275     zoff = CFPreferencesGetAppIntegerValue(zoffstr, app, &ok);
276     if ( ok ) _z += zoff;
277     
278     *x = _x;
279     *y = _y;
280     *z = _z;
281
282     return ret;
283 }
284
285 int read_sms_real(int type, double *x, double *y, double *z)
286 {
287     int _x, _y, _z;
288     int xscale, yscale, zscale;
289     int ret;
290     Boolean ok;
291  
292     ret = read_sms_raw(type, &_x, &_y, &_z);
293     if ( !ret )
294         return 0;
295
296     static CFStringRef app = CFSTR("com.ramsayl.UniMotion");
297     static CFStringRef xscalestr = CFSTR("x_scale");
298     static CFStringRef yscalestr = CFSTR("y_scale");
299     static CFStringRef zscalestr = CFSTR("z_scale");
300     xscale = CFPreferencesGetAppIntegerValue(xscalestr, app, &ok);
301     if ( !ok ) return 0;
302     yscale = CFPreferencesGetAppIntegerValue(yscalestr, app, &ok);
303     if ( !ok ) return 0;
304     zscale = CFPreferencesGetAppIntegerValue(zscalestr, app, &ok);
305     if ( !ok ) return 0;
306     
307     *x = _x / (double)xscale;
308     *y = _y / (double)yscale;
309     *z = _z / (double)zscale;
310     
311     return 1;
312 }
313
314 #endif