<commit_before>#pragma once

#include "gcd_utility.hpp"
#include "logger.hpp"
#include "modifier_flag_manager.hpp"
#include <CoreGraphics/CoreGraphics.h>

namespace manipulator {
class event_tap_manager final {
public:
  event_tap_manager(const event_tap_manager&) = delete;

  event_tap_manager(modifier_flag_manager& modifier_flag_manager) : modifier_flag_manager_(modifier_flag_manager),
                                                                    event_tap_(nullptr),
                                                                    run_loop_source_(nullptr) {
    // Observe all mouse events
    auto mask = CGEventMaskBit(kCGEventLeftMouseDown) |
                CGEventMaskBit(kCGEventLeftMouseUp) |
                CGEventMaskBit(kCGEventRightMouseDown) |
                CGEventMaskBit(kCGEventRightMouseUp) |
                CGEventMaskBit(kCGEventMouseMoved) |
                CGEventMaskBit(kCGEventLeftMouseDragged) |
                CGEventMaskBit(kCGEventRightMouseDragged) |
                CGEventMaskBit(kCGEventScrollWheel) |
                CGEventMaskBit(kCGEventTabletPointer) |
                CGEventMaskBit(kCGEventTabletProximity) |
                CGEventMaskBit(kCGEventOtherMouseDown) |
                CGEventMaskBit(kCGEventOtherMouseUp) |
                CGEventMaskBit(kCGEventOtherMouseDragged);

    event_tap_ = CGEventTapCreate(kCGHIDEventTap,
                                  kCGHeadInsertEventTap,
                                  kCGEventTapOptionDefault,
                                  mask,
                                  event_tap_manager::static_callback,
                                  this);

    if (event_tap_) {
      run_loop_source_ = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, event_tap_, 0);
      if (run_loop_source_) {
        CFRunLoopAddSource(CFRunLoopGetMain(), run_loop_source_, kCFRunLoopCommonModes);
        CGEventTapEnable(event_tap_, true);

        logger::get_logger().info("event_tap_manager grabbed mouse events");
      }
    }
  }

  ~event_tap_manager(void) {
    if (event_tap_) {
      CGEventTapEnable(event_tap_, false);

      if (run_loop_source_) {
        CFRunLoopRemoveSource(CFRunLoopGetMain(), run_loop_source_, kCFRunLoopCommonModes);
        CFRelease(run_loop_source_);
        run_loop_source_ = nullptr;
      }

      CFRelease(event_tap_);
      event_tap_ = nullptr;
    }

    logger::get_logger().info("event_tap_manager ungrabbed mouse events");
  }<commit_msg>use dispatch_sync_in_main_queue<commit_after>#pragma once

#include "gcd_utility.hpp"
#include "logger.hpp"
#include "modifier_flag_manager.hpp"
#include <CoreGraphics/CoreGraphics.h>

namespace manipulator {
class event_tap_manager final {
public:
  event_tap_manager(const event_tap_manager&) = delete;

  event_tap_manager(modifier_flag_manager& modifier_flag_manager) : modifier_flag_manager_(modifier_flag_manager),
                                                                    event_tap_(nullptr),
                                                                    run_loop_source_(nullptr) {
    // Observe all mouse events
    auto mask = CGEventMaskBit(kCGEventLeftMouseDown) |
                CGEventMaskBit(kCGEventLeftMouseUp) |
                CGEventMaskBit(kCGEventRightMouseDown) |
                CGEventMaskBit(kCGEventRightMouseUp) |
                CGEventMaskBit(kCGEventMouseMoved) |
                CGEventMaskBit(kCGEventLeftMouseDragged) |
                CGEventMaskBit(kCGEventRightMouseDragged) |
                CGEventMaskBit(kCGEventScrollWheel) |
                CGEventMaskBit(kCGEventTabletPointer) |
                CGEventMaskBit(kCGEventTabletProximity) |
                CGEventMaskBit(kCGEventOtherMouseDown) |
                CGEventMaskBit(kCGEventOtherMouseUp) |
                CGEventMaskBit(kCGEventOtherMouseDragged);

    event_tap_ = CGEventTapCreate(kCGHIDEventTap,
                                  kCGHeadInsertEventTap,
                                  kCGEventTapOptionDefault,
                                  mask,
                                  event_tap_manager::static_callback,
                                  this);

    if (event_tap_) {
      run_loop_source_ = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, event_tap_, 0);
      if (run_loop_source_) {
        CFRunLoopAddSource(CFRunLoopGetMain(), run_loop_source_, kCFRunLoopCommonModes);
        CGEventTapEnable(event_tap_, true);

        logger::get_logger().info("event_tap_manager grabbed mouse events");
      }
    }
  }

  ~event_tap_manager(void) {
    // Release event_tap_ in main thread to avoid callback invocations after object has been destroyed.
    gcd_utility::dispatch_sync_in_main_queue(^{
      if (event_tap_) {
        CGEventTapEnable(event_tap_, false);

        if (run_loop_source_) {
          CFRunLoopRemoveSource(CFRunLoopGetMain(), run_loop_source_, kCFRunLoopCommonModes);
          CFRelease(run_loop_source_);
          run_loop_source_ = nullptr;
        }

        CFRelease(event_tap_);
        event_tap_ = nullptr;
      }
      logger::get_logger().info("event_tap_manager ungrabbed mouse events");
    });
  }
{'fraction_non_alphanumeric': 0.06975845410628019, 'fraction_numerical': 0.0003864734299516908, 'mean_word_length': 2.2249221183800625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1460203', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1440, 'n_words': 190}