aboutsummaryrefslogtreecommitdiff
path: root/keyboard_light.c
blob: 636117654a65c4520a765082377363d9713941c2 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* --------------------------------------------------------------------------*/
/*                                                                           */
/* get/set the keyboard light level on Apple Mac OS X                        */
/* Written by Hans-Christoph Steiner <hans@at.or.at>                         */
/*                                                                           */
/* Copyright (c) 2008 Free Software Foundation                               */
/*                                                                           */
/* This program is free software; you can redistribute it and/or             */
/* modify it under the terms of the GNU General Public License               */
/* as published by the Free Software Foundation; either version 2            */
/* of the License, or (at your option) any later version.                    */
/*                                                                           */
/* See file LICENSE for further informations on licensing terms.             */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/* GNU General Public License for more details.                              */
/*                                                                           */
/* You should have received a copy of the GNU General Public License         */
/* along with this program; if not, write to the Free Software Foundation,   */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
/*                                                                           */
/* --------------------------------------------------------------------------*/

#include <mach/mach.h> 
#include <IOKit/IOKitLib.h> 
#include <CoreFoundation/CoreFoundation.h> 
#include <m_pd.h>

#define DEBUG(x)
//#define DEBUG(x) x 

#define DEFAULT_DELAYTIME  250
#define BRIGHTNESS_MAX     0xfff

/*------------------------------------------------------------------------------
 *  CLASS DEF
 */

static t_class *keyboard_light_class;

typedef struct _keyboard_light {
    t_object            x_obj;
    t_float             fade_time;
    io_service_t        io_service;
    io_connect_t        io_connect;
} t_keyboard_light;



enum {
	kGetSensorReadingID = 0, // getSensorReading(int *, int *)  
	kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *)  
	kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *)  
	kSetLEDFadeID = 3, // setLEDFade(int, int, int, int *)  
	// other firmware-related functions  
	verifyFirmwareID = 4,    // verifyFirmware(int *)  
	getFirmwareVersionID = 5,// getFirmwareVersion(int *)  
	// other flashing-related functions  
	// ... 
}; 

/*------------------------------------------------------------------------------
 * IMPLEMENTATION                    
 */

static void keyboard_light_output(t_keyboard_light* x)
{
	DEBUG(post("keyboard_light_output"););
    kern_return_t kernResult;  
    uint32_t out_brightness;
        
    if(! x->io_connect) return;

#if !defined(__LP64__)
	// Check if Mac OS X 10.5 API is available...
	if (IOConnectCallScalarMethod != NULL) {
		// ...and use it if it is.
#endif
        uint64_t inputCount = 1;
        uint64_t inputValues[1] = {0};
        uint32_t outputCount = 1;
		kernResult = IOConnectCallScalarMethod(x->io_connect,
                                               kGetLEDBrightnessID,  
                                               inputValues,
                                               inputCount,
                                               &out_brightness,
                                               &outputCount);
#if !defined(__LP64__)
	}
	else {
		// Otherwise fall back to older API.
        IOItemCount scalarInputCount = 1;  
        IOItemCount scalarOutputCount = 1;  
        uint32_t in_brightness;
        kernResult = IOConnectMethodScalarIScalarO(x->io_connect, 
                                                   kGetLEDBrightnessID,  
                                                   scalarInputCount, 
                                                   scalarOutputCount, 
                                                   in_brightness, 
                                                   &out_brightness);
	}    
#endif
    if( kernResult == KERN_SUCCESS)
        outlet_float(x->x_obj.ob_outlet, (t_float)out_brightness / BRIGHTNESS_MAX);
    else if(kernResult == kIOReturnBusy)
        pd_error(x,"[keyboard_light]: device busy");
    else
        pd_error(x,"[keyboard_light]: could not read device");
}


static void keyboard_light_float(t_keyboard_light* x, t_float f)
{
	DEBUG(post("keyboard_light_float"););

    kern_return_t kernResult;  
    t_float brightness;

    if(!x->io_connect) return;
    
    brightness = f * BRIGHTNESS_MAX;
    if(brightness < 0)
        brightness = 0;
    else if(brightness > BRIGHTNESS_MAX)
        brightness = BRIGHTNESS_MAX;
    
#if !defined(__LP64__)
	// Check if Mac OS X 10.5 API is available...
	if (IOConnectCallScalarMethod != NULL) {
		// ...and use it if it is.
#endif
        uint64_t inputValues[3];
        uint32_t inputCount = 3;
        uint64_t outputValues[1];
        uint32_t outputCount = 1;
        inputValues[0] = 0;
        inputValues[1] = brightness;
        inputValues[2] = x->fade_time;
        kernResult = IOConnectCallScalarMethod(x->io_connect,
                                               kSetLEDFadeID,
                                               inputValues,
                                               inputCount,
                                               outputValues,
                                               &outputCount);
#if !defined(__LP64__)
	}
	else {
		// Otherwise fall back to older API.
        IOItemCount scalarInputCount = 3;
        IOItemCount scalarOutputCount = 1;
        SInt32 in_unknown = 0, in_brightness, out_brightness;
        in_brightness = brightness;
        kernResult = IOConnectMethodScalarIScalarO(x->io_connect, 
                                                   kSetLEDFadeID,  
                                                   scalarInputCount, 
                                                   scalarOutputCount, 
                                                   in_unknown,
                                                   in_brightness,
                                                   (SInt32) x->fade_time,
                                                   &out_brightness);
	}    
#endif

    if( kernResult != KERN_SUCCESS)
    {
        if(kernResult == kIOReturnBusy)
            pd_error(x,"[keyboard_light]: device busy");
        else
            pd_error(x,"[keyboard_light]: could not write to device");
    }
}


static void keyboard_light_free(t_keyboard_light* x)
{
	DEBUG(post("keyboard_light_free"););
}


static void *keyboard_light_new(t_float level, t_float fade_time) 
{
	DEBUG(post("keyboard_light_new"););
	t_keyboard_light *x = (t_keyboard_light *)pd_new(keyboard_light_class);
    kern_return_t kernResult;

    x->io_service = IOServiceGetMatchingService(kIOMasterPortDefault, 
                                                IOServiceMatching("AppleLMUController"));
    if(x->io_service)
    {
        logpost(x, 4, "[keyboard_light]: found AppleLMUController");
    }
    else
    {
        error("[keyboard_light]: AppleLMUController not found, trying IOI2CDeviceLMU");
        x->io_service = IOServiceGetMatchingService(kIOMasterPortDefault, 
                                                    IOServiceMatching("IOI2CDeviceLMU"));
        if(x->io_service)
        {
            logpost(x, 4, "[keyboard_light]: found IOI2CDeviceLMU");
        }
        else
            pd_error(x,"[keyboard_light]: no sensor found");
    }
	kernResult = IOServiceOpen(x->io_service, mach_task_self(), 0, &x->io_connect);  

    IOObjectRelease(x->io_service);  
	if (kernResult != KERN_SUCCESS) 
    {
		error("[keyboard_light]: IOServiceOpen(): %d", kernResult);  
	}

    x->fade_time = fade_time;
	floatinlet_new(&x->x_obj, &x->fade_time);
	outlet_new(&x->x_obj, &s_float);

    keyboard_light_float(x, level);

	return (x);
}

void keyboard_light_setup(void) 
{
	keyboard_light_class = class_new(gensym("keyboard_light"), 
                                     (t_newmethod)keyboard_light_new,
                                     (t_method)keyboard_light_free,
                                     sizeof(t_keyboard_light), 
                                     CLASS_DEFAULT, 
                                     A_DEFFLOAT, A_DEFFLOAT, 0);
	/* add inlet datatype methods */
	class_addbang(keyboard_light_class,(t_method) keyboard_light_output);
	class_addfloat(keyboard_light_class,(t_method) keyboard_light_float);
}