<commit_before>#pragma once

#include "boost_defs.hpp"

#include "iokit_utility.hpp"
#include "service_observer.hpp"
#include "types.hpp"
#include <boost/optional.hpp>
#include <list>
#include <memory>

class hid_system_client final {
public:
  hid_system_client(const hid_system_client&) = delete;

  // Note:
  // OS X shares IOHIDSystem among all input devices even the serial_number of IOHIDSystem is same with the one of the input device.
  //
  // Example:
  //   The matched_callback always contains only one IOHIDSystem even if the following devices are connected.
  //     * Apple Internal Keyboard / Track
  //     * HHKB-BT
  //     * org.pqrs.driver.VirtualHIDKeyboard
  //
  //   The IOHIDSystem object's serial_number is one of the connected devices.
  //
  //   But the IOHIDSystem object is shared by all input devices.
  //   Thus, the IOHIDGetModifierLockState returns true if caps lock is on in one device.

  hid_system_client(spdlog::logger& logger) : logger_(logger),
                                              service_(IO_OBJECT_NULL),
                                              connect_(IO_OBJECT_NULL) {
    matching_dictionary_ = IOServiceNameMatching(kIOHIDSystemClass);
    if (!matching_dictionary_) {
      logger_.error("IOServiceNameMatching error @ {0}", __PRETTY_FUNCTION__);
    } else {
      service_observer_ = std::make_unique<service_observer>(logger_,
                                                             matching_dictionary_,
                                                             std::bind(&hid_system_client::matched_callback, this, std::placeholders::_1),
                                                             std::bind(&hid_system_client::terminated_callback, this, std::placeholders::_1));
    }
  }

  ~hid_system_client(void) {
    close_connection();

    if (matching_dictionary_) {
      CFRelease(matching_dictionary_);
    }
  }

  void post_modifier_flags(uint8_t key_code, IOOptionBits flags) {
    NXEventData event{};
    event.key.keyCode = key_code;

    IOGPoint loc{};
    post_event(NX_FLAGSCHANGED, loc, &event, kNXEventDataVersion, flags, kIOHIDSetGlobalEventFlags);
  }

  enum class post_key_type {
    key,
    aux_control_button,
  };

  void post_key(post_key_type type, uint8_t key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    switch (type) {
    case post_key_type::key:
      post_key(key_code, event_type, flags, repeat);
      break;

    case post_key_type::aux_control_button:
      post_aux_control_button(key_code, event_type, flags, repeat);
      break;
    }
  }

  void post_key(krbn::key_code key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    if (auto key = krbn::types::get_mac_aux_control_button(key_code)) {
      post_aux_control_button(*key, event_type, flags, repeat);
      return;
    }

    if (auto key = krbn::types::get_mac_key(key_code)) {
      post_key(*key, event_type, flags, repeat);
      return;
    }

    logger::get_logger().warn("key_code:{1:#x} is unsupported key @ {0}", __PRETTY_FUNCTION__, static_cast<uint32_t>(key_code));
  }

  void post_key(uint8_t key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    NXEventData event{};
    event.key.origCharCode = 0;
    event.key.repeat = repeat;
    event.key.charSet = NX_ASCIISET;
    event.key.charCode = 0;
    event.key.keyCode = key_code;<commit_msg>fix logger<commit_after>#pragma once

#include "boost_defs.hpp"

#include "iokit_utility.hpp"
#include "service_observer.hpp"
#include "types.hpp"
#include <boost/optional.hpp>
#include <list>
#include <memory>

class hid_system_client final {
public:
  hid_system_client(const hid_system_client&) = delete;

  // Note:
  // OS X shares IOHIDSystem among all input devices even the serial_number of IOHIDSystem is same with the one of the input device.
  //
  // Example:
  //   The matched_callback always contains only one IOHIDSystem even if the following devices are connected.
  //     * Apple Internal Keyboard / Track
  //     * HHKB-BT
  //     * org.pqrs.driver.VirtualHIDKeyboard
  //
  //   The IOHIDSystem object's serial_number is one of the connected devices.
  //
  //   But the IOHIDSystem object is shared by all input devices.
  //   Thus, the IOHIDGetModifierLockState returns true if caps lock is on in one device.

  hid_system_client(spdlog::logger& logger) : logger_(logger),
                                              service_(IO_OBJECT_NULL),
                                              connect_(IO_OBJECT_NULL) {
    matching_dictionary_ = IOServiceNameMatching(kIOHIDSystemClass);
    if (!matching_dictionary_) {
      logger_.error("IOServiceNameMatching error @ {0}", __PRETTY_FUNCTION__);
    } else {
      service_observer_ = std::make_unique<service_observer>(logger_,
                                                             matching_dictionary_,
                                                             std::bind(&hid_system_client::matched_callback, this, std::placeholders::_1),
                                                             std::bind(&hid_system_client::terminated_callback, this, std::placeholders::_1));
    }
  }

  ~hid_system_client(void) {
    close_connection();

    if (matching_dictionary_) {
      CFRelease(matching_dictionary_);
    }
  }

  void post_modifier_flags(uint8_t key_code, IOOptionBits flags) {
    NXEventData event{};
    event.key.keyCode = key_code;

    IOGPoint loc{};
    post_event(NX_FLAGSCHANGED, loc, &event, kNXEventDataVersion, flags, kIOHIDSetGlobalEventFlags);
  }

  enum class post_key_type {
    key,
    aux_control_button,
  };

  void post_key(post_key_type type, uint8_t key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    switch (type) {
    case post_key_type::key:
      post_key(key_code, event_type, flags, repeat);
      break;

    case post_key_type::aux_control_button:
      post_aux_control_button(key_code, event_type, flags, repeat);
      break;
    }
  }

  void post_key(krbn::key_code key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    if (auto key = krbn::types::get_mac_aux_control_button(key_code)) {
      post_aux_control_button(*key, event_type, flags, repeat);
      return;
    }

    if (auto key = krbn::types::get_mac_key(key_code)) {
      post_key(*key, event_type, flags, repeat);
      return;
    }

    logger_.warn("key_code:{1:#x} is unsupported key @ {0}", __PRETTY_FUNCTION__, static_cast<uint32_t>(key_code));
  }

  void post_key(uint8_t key_code, krbn::event_type event_type, IOOptionBits flags, bool repeat) {
    NXEventData event{};
    event.key.origCharCode = 0;
    event.key.repeat = repeat;
    event.key.charSet = NX_ASCIISET;
    event.key.charCode = 0;
    event.key.keyCode = key_code;
{'fraction_non_alphanumeric': 0.09815411661295048, 'fraction_numerical': 0.0035159683562847933, 'mean_word_length': 2.8440315315315314, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5085842', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2116, 'n_words': 464}