@@ -6,7 +6,7 @@ use bitflags::bitflags;
6
6
use core:: ffi:: { c_ulong, c_void} ;
7
7
use core_foundation:: {
8
8
base:: { CFRelease , CFRetain , CFTypeID , TCFType } ,
9
- mach_port:: { CFMachPort , CFMachPortRef } ,
9
+ mach_port:: { CFMachPort , CFMachPortInvalidate , CFMachPortRef } ,
10
10
} ;
11
11
use foreign_types:: { foreign_type, ForeignType } ;
12
12
use std:: mem:: ManuallyDrop ;
@@ -417,7 +417,7 @@ macro_rules! CGEventMaskBit {
417
417
}
418
418
419
419
pub type CGEventTapProxy = * const c_void ;
420
- pub type CGEventTapCallBackFn < ' tap_life > =
420
+ type CGEventTapCallBackFn < ' tap_life > =
421
421
Box < dyn Fn ( CGEventTapProxy , CGEventType , & CGEvent ) -> Option < CGEvent > + ' tap_life > ;
422
422
type CGEventTapCallBackInternal = unsafe extern "C" fn (
423
423
proxy : CGEventTapProxy ,
@@ -458,9 +458,9 @@ unsafe extern "C" fn cg_event_tap_callback_internal(
458
458
/// ) {
459
459
/// Ok(tap) => unsafe {
460
460
/// let loop_source = tap
461
- /// .mach_port
461
+ /// .mach_port()
462
462
/// .create_runloop_source(0)
463
- /// .expect("Somethings is bad ");
463
+ /// .expect("Runloop source creation failed ");
464
464
/// current.add_source(&loop_source, kCFRunLoopCommonModes);
465
465
/// tap.enable();
466
466
/// CFRunLoop::run_current();
@@ -469,9 +469,8 @@ unsafe extern "C" fn cg_event_tap_callback_internal(
469
469
/// }
470
470
/// ```
471
471
pub struct CGEventTap < ' tap_life > {
472
- pub mach_port : CFMachPort ,
473
- pub callback_ref :
474
- Box < dyn Fn ( CGEventTapProxy , CGEventType , & CGEvent ) -> Option < CGEvent > + ' tap_life > ,
472
+ mach_port : CFMachPort ,
473
+ _callback : Box < CGEventTapCallBackFn < ' tap_life > > ,
475
474
}
476
475
477
476
impl < ' tap_life > CGEventTap < ' tap_life > {
@@ -487,7 +486,7 @@ impl<'tap_life> CGEventTap<'tap_life> {
487
486
. fold ( CGEventType :: Null as CGEventMask , |mask, & etype| {
488
487
mask | CGEventMaskBit ! ( etype)
489
488
} ) ;
490
- let cb = Box :: new ( Box :: new ( callback) as CGEventTapCallBackFn ) ;
489
+ let cb: Box < CGEventTapCallBackFn > = Box :: new ( Box :: new ( callback) ) ;
491
490
let cbr = Box :: into_raw ( cb) ;
492
491
unsafe {
493
492
let event_tap_ref = CGEventTapCreate (
@@ -502,7 +501,7 @@ impl<'tap_life> CGEventTap<'tap_life> {
502
501
if !event_tap_ref. is_null ( ) {
503
502
Ok ( Self {
504
503
mach_port : ( CFMachPort :: wrap_under_create_rule ( event_tap_ref) ) ,
505
- callback_ref : Box :: from_raw ( cbr) ,
504
+ _callback : Box :: from_raw ( cbr) ,
506
505
} )
507
506
} else {
508
507
let _ = Box :: from_raw ( cbr) ;
@@ -511,11 +510,21 @@ impl<'tap_life> CGEventTap<'tap_life> {
511
510
}
512
511
}
513
512
513
+ pub fn mach_port ( & self ) -> & CFMachPort {
514
+ & self . mach_port
515
+ }
516
+
514
517
pub fn enable ( & self ) {
515
518
unsafe { CGEventTapEnable ( self . mach_port . as_concrete_TypeRef ( ) , true ) }
516
519
}
517
520
}
518
521
522
+ impl Drop for CGEventTap < ' _ > {
523
+ fn drop ( & mut self ) {
524
+ unsafe { CFMachPortInvalidate ( self . mach_port . as_CFTypeRef ( ) as * mut _ ) } ;
525
+ }
526
+ }
527
+
519
528
foreign_type ! {
520
529
#[ doc( hidden) ]
521
530
pub unsafe type CGEvent {
0 commit comments