• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Event Tap strangeness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Event Tap strangeness


  • Subject: Re: Event Tap strangeness
  • From: Rainer Brockerhoff <email@hidden>
  • Date: Tue, 12 Feb 2008 15:50:30 -0200

At 08:55 -0800 12/02/08, email@hidden wrote:
>From: Martin Redington <email@hidden>
>Precedence: list
>MIME-Version: 1.0 (Apple Message framework v752.3)
>To: Apple Cocoa-Dev Mailing List <email@hidden>
>Date: Tue, 12 Feb 2008 13:13:32 +0000
>Message-ID: <email@hidden>
>Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
>Subject: Event Tap strangeness
>Message: 4
>
>
>I'm trying to observe all mouse down and key down events with an event tap.
>
>Everything seems to work ok, but when I shut the event tap down, I'm seeing some bizarre event related behaviour in my app and other apps.
>...
>- (void) startTapping
>{
>    NSLog(@"Installing event tap");
>
>    if(mEventTap != NULL)
>    {
>        NSLog(@"WARNING: Event tap already exists");
>        return;
>    }
>
>    CGEventMask eventsOfInterest = CGEventMaskBit(kCGEventLeftMouseDown) |
>                       CGEventMaskBit(kCGEventRightMouseDown) |
>                       CGEventMaskBit(kCGEventOtherMouseDown) |
>                       CGEventMaskBit(kCGEventKeyDown);
>
>    mEventTap = CGEventTapCreate (
>                                  kCGSessionEventTap,
>                                  kCGTailAppendEventTap,
>                                  kCGEventTapOptionListenOnly,
>                                  eventsOfInterest,
>                                  MyEventTapCallback,
>                                  self
>                                  );
>
>    if(mEventTap == NULL)
>    {
>        NSLog(@"WARNING: Could not create event tap");
>        return;
>    }
>
>
>    mEventSrc = CFMachPortCreateRunLoopSource(NULL, mEventTap, 0);
>    if(mEventSrc == NULL)
>    {
>        NSLog(@"No event run loop src?\n");
>        return;
>    }
>
>    // Get the CFRunLoop primitive for the Carbon Main Event Loop, and add the new event souce
>    // CFRunLoopAddSource((CFRunLoopRef) (GetCFRunLoopFromEventLoop(GetMainEventLoop())), mEventSrc, kCFRunLoopDefaultMode);
>    CFRunLoopAddSource(CFRunLoopGetCurrent(), mEventSrc, kCFRunLoopCommonModes);
>}
>
>- (void) stopTapping
>{
>    NSLog(@"Removing event tap");
>
>    if(mEventSrc)
>    {
>        // CFRunLoopRemoveSource((CFRunLoopRef) (GetCFRunLoopFromEventLoop(GetMainEventLoop())), mEventSrc, kCFRunLoopDefaultMode);
>        CFRunLoopRemoveSource(CFRunLoopGetCurrent(), mEventSrc, kCFRunLoopCommonModes);
>        CFRelease(mEventSrc);
>    }
>
>    if(mEventTap != NULL)
>    {
>        CFRelease(mEventTap);
>    }
>}

Not really a Cocoa problem, but anyway :-)

I had similar problems (for similar code) until I twiddled the sequence of statements on both start and stop. Here's what I worked out, converted to your names (and condensed for clarity):

- (void) startTapping {
    mEventTap = CGEventTapCreate (...args...);
    mEventSrc = CFMachPortCreateRunLoopSource(NULL, mEventTap, 0);
    CFRelease(mEventTap);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), mEventSrc, kCFRunLoopCommonModes);
    CFRelease(mEventSrc);
}

- (void) stopTapping {
        CFRunLoopRemoveSource(CFRunLoopGetCurrent(), mEventSrc, kCFRunLoopCommonModes);
}

...apparently this makes tap and source go away as soon as the source is removed from the event loop. If you over-retain and release them only after the source is removed, the tap clogs...

HTH,
--
Rainer Brockerhoff  <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
 In their own business even sages err."
Weblog: http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Event Tap strangeness
      • From: Bill Cheeseman <email@hidden>
    • Re: Event Tap strangeness
      • From: Martin Redington <email@hidden>
  • Prev by Date: NSStatusItem setHighlightMode throws NSInternalInconsistencyException on PowerPC ONLY (Works on Intel)
  • Next by Date: Re: NSStatusItem setHighlightMode throws NSInternalInconsistencyException on PowerPC ONLY (Works on Intel)
  • Previous by thread: Re: Event Tap strangeness
  • Next by thread: Re: Event Tap strangeness
  • Index(es):
    • Date
    • Thread