Fixed a lazySlot bug where lazy slots in different objects end up pointing to the...
[io/quag.git] / addons / AppleExtras / source / AppleSensors.c
blob6ab70fb3c6bf779b6a3bc6089a8085dcb4548f91
2 enum {
3 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
4 kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *)
5 kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *)
6 kSetLEDFadeID = 3, // setLEDFade(int, int, int, int *)
7 // other firmware-related functions
8 // verifyFirmwareID = 4,
9 // verifyFirmware(int *)
10 // getFirmwareVersionID = 5,
11 // getFirmwareVersion(int *)
12 // other flashing-related functions
13 // ...
17 #include <mach/mach.h>
18 //#include <Carbon/Carbon.h>
19 #include <IOKit/IOKitLib.h>
20 #include <CoreFoundation/CoreFoundation.h>
22 static io_connect_t dataPort = 0; // shared?
24 io_connect_t getDataPort(void)
26 kern_return_t kr;
27 io_service_t serviceObject;
29 if (dataPort) return dataPort;
31 // Look up a registered IOService object whose class is AppleLMUController
32 serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
34 if (!serviceObject)
36 printf("getLightSensors() error: failed to find ambient light sensor\n");
37 return 0;
40 // Create a connection to the IOService object
41 kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort);
42 IOObjectRelease(serviceObject);
44 if (kr != KERN_SUCCESS)
46 printf("getLightSensors() error: failed to open IoService object\n");
47 return 0;
50 return dataPort;
53 void getLightSensors(float *left, float *right)
55 IOItemCount scalarInputCount = 0;
56 IOItemCount scalarOutputCount = 2;
57 kern_return_t kr;
58 SInt32 l = 0, r = 0;
60 kr = IOConnectMethodScalarIScalarO(getDataPort(),
61 kGetSensorReadingID,
62 scalarInputCount,
63 scalarOutputCount,
64 &l,
65 &r);
67 if (kr != KERN_SUCCESS)
69 //printf("error getting light sensor values\n");
70 return;
73 *left = l;
74 *right = r;
76 *left /= 2000;
77 *right /= 2000;
80 #include <stdio.h>
81 #include <IOKit/graphics/IOGraphicsLib.h>
82 #include <ApplicationServices/ApplicationServices.h>
84 float getDisplayBrightness(void)
86 CGDisplayErr dErr;
87 io_service_t service;
88 CGDirectDisplayID targetDisplay;
90 CFStringRef key = CFSTR(kIODisplayBrightnessKey);
91 float brightness = HUGE_VALF;
93 targetDisplay = CGMainDisplayID();
94 service = CGDisplayIOServicePort(targetDisplay);
96 dErr = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness);
98 return brightness;
101 void setDisplayBrightness(float brightness)
103 CGDisplayErr dErr;
104 io_service_t service;
105 CGDirectDisplayID targetDisplay;
106 CFStringRef key = CFSTR(kIODisplayBrightnessKey);
108 targetDisplay = CGMainDisplayID();
109 service = CGDisplayIOServicePort(targetDisplay);
111 dErr = IODisplaySetFloatParameter(service, kNilOptions, key, brightness);
115 // Keyboard Brightness
117 float getKeyboardBrightness(void)
119 float f;
120 kern_return_t kr;
121 IOItemCount scalarInputCount = 1;
122 IOItemCount scalarOutputCount = 1;
123 SInt32 in_unknown = 0, out_brightness;
125 kr = IOConnectMethodScalarIScalarO(getDataPort(), kGetLEDBrightnessID,
126 scalarInputCount,
127 scalarOutputCount,
128 in_unknown,
129 &out_brightness);
131 if (kr != KERN_SUCCESS)
133 printf("getKeyboardBrightness() error\n");
134 return 0;
137 f = out_brightness;
138 f /= 0xfff;
139 return (float)f;
142 void setKeyboardBrightness(float in)
144 static io_connect_t dp = 0; // shared?
145 kern_return_t kr;
146 IOItemCount scalarInputCount = 2;
147 IOItemCount scalarOutputCount = 1;
148 SInt32 in_unknown = 0, in_brightness, out_brightness;
150 in_brightness = in * 0xfff;
152 //kr = IOConnectMethodScalarIScalarO(getDataPort(), kSetLEDBrightnessID,
153 kr = IOConnectMethodScalarIScalarO(dp, kSetLEDBrightnessID,
154 scalarInputCount,
155 scalarOutputCount,
156 in_unknown,
157 in_brightness,
158 &out_brightness);
160 if (kr != KERN_SUCCESS)
162 printf("setKeyboardBrightness() error\n");
163 return;