Receive CFMessagePortSendRequest notifications

  • Hello,

    I found that you can notify the screensaver to re-read the values of
    ~/Library/com.apple.screensaver.plist with this code:

    CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR
    ("com.apple.loginwindow.notify"));
    CFMessagePortSendRequest(port, 500, 0, 0, 0, 0, 0);
    CFRelease(port);

    This part works as expected: the screensaver read the new settings.

    I'm now trying to subscribe to the notifications, in case the values
    change not due to my program and a notification is sent.
    Eg, if the user change the setting via System Preferences.app, I want
    to catch the notification sent by System Preferences.app.

    I tried this:

    static CFDataRef *consoleMessage(CFMessagePortRef local, SInt32
    priority, CFDataRef data, void *info) {
      fprintf(stderr, "consoleMessage priority = %ld\n", priority);
      return NULL;
    }


    - (id)init
    {
    CFMessagePortContext context;

    context.version = 0;
    context.info = self;
    context.retain = 0;
    context.release = 0;
    context.copyDescription = 0;

    CFMessagePortRef port = CFMessagePortCreateLocal(NULL,
    kConsoleMessagePort, (CFMessagePortCallBack)&consoleMessage,
    &context, NULL);


    CFRunLoopSourceRef rls = CFMessagePortCreateRunLoopSource(NULL,
    port, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
    CFRelease(rls);


      self = [super init];
      return self;
    }

    It doesn't work. Here is the log:

    2007-10-22 19:19:22.559 SystemUIServer[22311] CFLog (0):
    CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port =
    0x6403, name = 'com.apple.loginwindow.notify'
    See /usr/include/servers/bootstrap_defs.h for the error codes.
    2007-10-22 19:19:22.563 SystemUIServer[22311] CFLog (99):
    CFMessagePortCreateLocal(): failed to name Mach port
    (com.apple.loginwindow.notify)

    From what I understand, the service is already active and can't be
    open twice.
    That seems normal, as the port is already open and used to receive
    the notifications by the screensaver engine (or whatever take care of
    this).

    How can I receive the notifications too?

    I hope I'm being clear about my problem. Let me know if not.

    Thank you for your time and help. It is appreciated.

    --
    Guillaume Cerquant
    <gcerquant...>
  • > I'm now trying to subscribe to the notifications, in case the values
    > change not due to my program and a notification is sent.
    > Eg, if the user change the setting via System Preferences.app, I want
    > to catch the notification sent by System Preferences.app.

    Unfortunately for you, these aren’t notifications at all; they’re messages sent directly to the port registered with that name.  There’s no way to snoop the messages short of code injection.

    Since anything that changes the settings will update the plist, you might want to watch that instead.

    Good luck,
    -Ben
previous month october 2007 next month
MTWTFSS
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        
Go to today