<commit_before>//
//  MouseHook.swift
//  ColorInspector
//
//  Created by <NAME> on 11/1/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Cocoa

@objc protocol MouseHookDelegate {
    func onMouseMove(point: CGPoint);
    func onMouseUp(point: CGPoint);
}

// Haven't been able to figure out how to pass in a value for the refcon parameter of CGEventTapCreate
// yet so the workaround for now is to just store off a singleton-ish reference to it (feelsbadman)
private var instance: MouseHook?;

func handleCGEvent(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>? {
    if (type == .MouseMoved || type == .LeftMouseDragged) {
        instance!.delegate?.onMouseMove(CGEventGetLocation(event));
    }
    else if (type == .LeftMouseUp) {
        instance!.delegate?.onMouseUp(CGEventGetLocation(event));
    }
    else if (type == .TapDisabledByTimeout) {
        CGEventTapEnable(instance!.eventTap!, true);
    }
    
    return Unmanaged.passRetained(event);
}

@objc class MouseHook: NSObject {
    
    var delegate: MouseHookDelegate?;
    
    private var eventTap: CFMachPortRef?;

    override init() {
        super.init();
        
        instance = self;
        
        self.setupMouseHook();
    }
    
    func setupMouseHook() {
        self.eventTap = CGEventTapCreate(.CGHIDEventTap, .HeadInsertEventTap, .ListenOnly, ~CGEventMask(0), handleCGEvent, nil);
        let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.eventTap, 0);
        CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
    }
}<commit_msg>Figured out how to package up the self pointer inside the refcon parameter, so don't need the singleton-ish hack anymore.<commit_after>//
//  MouseHook.swift
//  ColorInspector
//
//  Created by <NAME> on 11/1/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Cocoa

@objc protocol MouseHookDelegate {
    func onMouseMove(point: CGPoint);
    func onMouseUp(point: CGPoint);
}

func handleCGEvent(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>? {
    let mouseHook: MouseHook = Unmanaged.fromOpaque(COpaquePointer(refcon)).takeUnretainedValue();
    
    if (type == .MouseMoved || type == .LeftMouseDragged) {
        mouseHook.delegate?.onMouseMove(CGEventGetLocation(event));
    }
    else if (type == .LeftMouseUp) {
        mouseHook.delegate?.onMouseUp(CGEventGetLocation(event));
    }
    else if (type == .TapDisabledByTimeout) {
        CGEventTapEnable(mouseHook.eventTap!, true);
    }
    
    return Unmanaged.passRetained(event);
}

@objc class MouseHook: NSObject {
    
    var delegate: MouseHookDelegate?;
    
    private var eventTap: CFMachPortRef?;

    override init() {
        super.init();
        
        self.setupMouseHook();
    }
    
    func setupMouseHook() {
        let refcon = UnsafeMutablePointer<Void>(Unmanaged.passUnretained(self).toOpaque());
        
        self.eventTap = CGEventTapCreate(.CGHIDEventTap, .HeadInsertEventTap, .ListenOnly, ~CGEventMask(0), handleCGEvent, refcon);
        let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.eventTap, 0);
        CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
    }
}
{'fraction_non_alphanumeric': 0.11068249258160237, 'fraction_numerical': 0.006528189910979229, 'mean_word_length': 3.4355263157894735, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3307547', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1060, 'n_words': 241}