views:

225

answers:

6

Hi,

I am creating a software on Mac and I would like to change the value of an IORegistryEntry. I can view it on the IORegistryExplorer, but I can't edit it. So it's my understanding that I have to edit it via code. Here is my code:

CFMutableDictionaryRef matchingDict = IOServiceNameMatching("AppleUSBMultitouchDriver");
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);
if(service) {
 CFStringRef manufacturer = IORegistryEntryCreateCFProperty(service, CFSTR("Manufacturer"), kCFAllocatorDefault,0);
 NSLog(@"%@", (NSString*)manufacturer);
 kern_return_t err = IORegistryEntrySetCFProperty(service, CFSTR("Manufacturer"), CFSTR("test"));
 NSLog(@"error = %d", err);
}

This will output

2010-04-10 16:09:09.015 Test[41548:a0f] Apple Inc.
2010-04-10 16:09:09.015 Test[41548:a0f] error = 0

But after I check the value in the IORegistryExplorer, it still doesn't change. Does anybody have any suggestions?

Thank you

A: 

In order for this to be possible, usually the driver for the particular hardware you're changing has to implement setProperties() (in IOKit) that makes this change for you.

It's unlikely that Apple will implement setProperty() in their AppleUSBMultitouchDriver in a way that allows you to change the manufacturer name. They want to specify what kind of fruit they are. ;)

WhirlWind
Thank you very much for your answer. Actually, I don't want to change the manufacturer name. That's just for the example :-) I want to change the value of "trackpadUserPreferences." Apparently, Apple's Trackpad preference pane can do this, but I don't know how it does.Could you tell me how I can obtain the object that I can call the setProperty method, please? I looked at the binary of /System/Library/Extensions/AppleUSBMultitouch.kext/Contents/MacOS/AppleUSBMultitouch and found a string "AppleUSBMultitouchDriver::setProperty" but I don't know if this is related to this in any way.
yangumi
I don't know -- I'd run System Preferences in a debugger and have it break on that call to see how it's calling into the registry functions.
WhirlWind
A: 

Use IOConnectSetCFProperties instead of IORegistryEntrySetCFProperty. Pass it a dictionary with the settings you want to set.

For example to turn off three finger swipe to navigate, call it with a dictionary containing { TrackpadThreeFingerSwipe = 0; }

Ken Aspeslagh
Thank you very much for your help. I tried your suggestion, but it still doesn't work :-( I've posted my actual code below.
yangumi
A: 

Here is my code:

kern_return_t kernResult;
io_registry_entry_t service;
io_connect_t connect;

CFDictionaryRef matchingDict = IOServiceNameMatching("AppleUSBMultitouchDriver");
service = (io_registry_entry_t)IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);

if (service) {
    CFDictionaryRef trackpadUserPreferences = IORegistryEntryCreateCFProperty(service, CFSTR("TrackpadUserPreferences"), kCFAllocatorDefault,0);
    NSLog(@"%@", trackpadUserPreferences);

    kernResult = IOServiceOpen(service, mach_task_self(), 0, &connect);
    if (kernResult != KERN_SUCCESS) {
        printf("IOServiceOpen returned %d\n", kernResult);
    }

    CFMutableDictionaryRef t = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, trackpadUserPreferences);
    CFDictionarySetValue(t, CFSTR("TrackpadThreeFingerSwipe"), kCFBooleanFalse);

    CFMutableDictionaryRef a = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
    CFDictionarySetValue(a, CFSTR("TrackpadUserPreferences"), t);

    CFMutableDictionaryRef b = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
    CFDictionarySetValue(b, CFSTR("TrackpadThreeFingerSwipe"), kCFBooleanFalse);

    kernResult = IOConnectSetCFProperties(connect, a);
    //kernResult = IOConnectSetCFProperties(connect, b); //this doesn't work either
    if (kernResult != KERN_SUCCESS) {
        printf("IOConnectSetCFProperties returned %d\n", kernResult);
    }
}

And the output is

{
    Clicking = 0;
    DragLock = 0;
    Dragging = 0;
    JitterNoClick = 1;
    JitterNoMove = 1;
    TrackpadCornerSecondaryClick = 0;
    TrackpadFourFingerHorizSwipeGesture = 1;
    TrackpadFourFingerVertSwipeGesture = 1;
    TrackpadHorizScroll = 1;
    TrackpadPinch = 1;
    TrackpadRightClick = 1;
    TrackpadRotate = 1;
    TrackpadScroll = 1;
    TrackpadThreeFingerSwipe = 1;
    USBMouseStopsTrackpad = 0;
}
IOConnectSetCFProperties returned -536870201

The value still doesn't change. Am I doing something wrong here?

yangumi
A: 

did you figure out how system preferences does it? I'd like to do s.th. similar but also can't get it working :-(

Measunny
A: 

Doesn't anyone ever offer proof of statements???

I would be happy to see any code that can be placed in an application or command line tool that allows you to set or change an entry in the IOREG, even if it doesn't serve any real purpose, it would show how to do it so that those who claim it can be done will actually have some sample code they can reference rather than never substantiating the claim.

Bob
A: 

This is example how to change trackpad settings properly. Trackpad.prefpane do exactly this, but also save this setting somewhere in defaults (if you will not find out where exactly, ask here about it).

P.S. getEVSHandle() may be found in MachineSettings.framework.

P.P.S. Checked only on 10.5 & 10.6.

NSInteger zero = 0, one = 1;

CFNumberRef _numberWith0 = CFNumberCreate(kCFAllocatorDefault, kCFNumberNSIntegerType, &zero);
CFNumberRef _numberWith1 = CFNumberCreate(kCFAllocatorDefault, kCFNumberNSIntegerType, &one);

CFMutableDictionaryRef propertyDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL);

CFDictionarySetValue(propertyDict, @"TrackpadThreeFingerSwipe", flag ? _numberWith1 : _numberWith0);

io_connect_t connect = getEVSHandle();

if (!connect)
{
// error
}

kern_return_t status = IOConnectSetCFProperties(connect, propertyDict);

if (status != KERN_SUCCESS)
{
//error
}

CFRelease(propertyDict);
J59