src/video/cocoa/SDL_cocoamousetap.m
changeset 10653 f87d76304c76
parent 10177 faa36f2de933
child 10654 a9713e5c7788
     1.1 --- a/src/video/cocoa/SDL_cocoamousetap.m	Fri Nov 25 14:16:27 2016 -0500
     1.2 +++ b/src/video/cocoa/SDL_cocoamousetap.m	Sat Nov 26 10:26:22 2016 -0800
     1.3 @@ -142,15 +142,12 @@
     1.4  {
     1.5      SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;
     1.6  
     1.7 -    /* Create a tap. */
     1.8 -    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
     1.9 -                                              kCGEventTapOptionDefault, allGrabbedEventsMask,
    1.10 -                                              &Cocoa_MouseTapCallback, tapdata);
    1.11 +    /* Tap was created on main thread but we own it now. */
    1.12 +    CFMachPortRef eventTap = tapdata->tap;
    1.13      if (eventTap) {
    1.14          /* Try to create a runloop source we can schedule. */
    1.15          CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    1.16          if  (runloopSource) {
    1.17 -            tapdata->tap = eventTap;
    1.18              tapdata->runloopSource = runloopSource;
    1.19          } else {
    1.20              CFRelease(eventTap);
    1.21 @@ -202,15 +199,30 @@
    1.22  
    1.23      tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
    1.24      if (tapdata->runloopStartedSemaphore) {
    1.25 -        tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 512 * 1024, tapdata);
    1.26 -        if (!tapdata->thread) {
    1.27 -            SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
    1.28 +        tapdata->tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
    1.29 +                                        kCGEventTapOptionDefault, allGrabbedEventsMask,
    1.30 +                                        &Cocoa_MouseTapCallback, tapdata);
    1.31 +        if (tapdata->tap) {
    1.32 +            tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 512 * 1024, tapdata);
    1.33 +            if (tapdata->thread) {
    1.34 +                /* Success - early out. Ownership transferred to thread. */
    1.35 +            	return;
    1.36 +            }
    1.37 +            CFRelease(tapdata->tap);
    1.38          }
    1.39 +        SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
    1.40      }
    1.41 +    SDL_free(driverdata->tapdata);
    1.42 +    driverdata->tapdata = NULL;
    1.43 +}
    1.44  
    1.45 -    if (!tapdata->thread) {
    1.46 -        SDL_free(driverdata->tapdata);
    1.47 -        driverdata->tapdata = NULL;
    1.48 +void
    1.49 +Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
    1.50 +{
    1.51 +    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
    1.52 +    if (tapdata && tapdata->tap)
    1.53 +    {
    1.54 +        CGEventTapEnable(tapdata->tap, enabled);
    1.55      }
    1.56  }
    1.57  
    1.58 @@ -246,6 +258,11 @@
    1.59  }
    1.60  
    1.61  void
    1.62 +Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
    1.63 +{
    1.64 +}
    1.65 +
    1.66 +void
    1.67  Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
    1.68  {
    1.69  }