Skip to content

Commit

Permalink
Merge pull request #3759 from pqrs-org/improve-libkrbn
Browse files Browse the repository at this point in the history
Improve libkrbn
  • Loading branch information
tekezo committed Mar 24, 2024
2 parents 77d7e74 + c7f3b45 commit 54c78bd
Show file tree
Hide file tree
Showing 22 changed files with 1,292 additions and 1,287 deletions.
26 changes: 16 additions & 10 deletions appendix/dump_libkrbn/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ void system_preferences_monitor_callback(const struct libkrbn_system_preferences
<< std::endl;
}

void connected_devices_monitor_callback(libkrbn_connected_devices* devices,
void* refcon) {
// Check that get_device_address returns a fixed allocated address.
auto* p1 = reinterpret_cast<const void*>(libkrbn_connected_devices_get_device_address(devices, 0));
auto* p2 = reinterpret_cast<const void*>(libkrbn_connected_devices_get_device_address(devices, 0));
assert(p1 == p2);
void connected_devices_updated_callback(void) {
std::cout << "connected_devices_updated_callback" << std::endl;
}

auto global_wait = pqrs::make_thread_wait();
Expand All @@ -31,7 +27,11 @@ int main(int argc, const char* argv[]) {
global_wait->notify();
});

libkrbn_enable_connected_devices_monitor(connected_devices_monitor_callback, nullptr);
libkrbn_enable_connected_devices_monitor();
libkrbn_register_connected_devices_updated_callback(connected_devices_updated_callback);
connected_devices_updated_callback();

char buffer[32 * 1024];

{
libkrbn_enable_complex_modifications_assets_manager();
Expand All @@ -43,13 +43,17 @@ int main(int argc, const char* argv[]) {
std::cout << "libkrbn_complex_modifications_assets_manager_get_files_size: " << size << std::endl;

for (size_t i = 0; i < size; ++i) {
std::cout << " " << libkrbn_complex_modifications_assets_manager_get_file_title(i) << std::endl;
if (libkrbn_complex_modifications_assets_manager_get_file_title(i, buffer, sizeof(buffer))) {
std::cout << " " << buffer << std::endl;
}

auto rules_size = libkrbn_complex_modifications_assets_manager_get_rules_size(i);
std::cout << " libkrbn_complex_modifications_assets_manager_get_rules_size: " << rules_size << std::endl;

for (size_t j = 0; j < rules_size; ++j) {
std::cout << " " << libkrbn_complex_modifications_assets_manager_get_rule_description(i, j) << std::endl;
if (libkrbn_complex_modifications_assets_manager_get_rule_description(i, j, buffer, sizeof(buffer))) {
std::cout << " " << buffer << std::endl;
}

if (j >= 2) {
if (rules_size - j > 1) {
Expand Down Expand Up @@ -82,7 +86,9 @@ int main(int argc, const char* argv[]) {
system_preferences_monitor_callback,
nullptr);

std::cout << "libkrbn_get_notification_message_body: " << libkrbn_get_notification_message_body() << std::endl;
if (libkrbn_get_notification_message_body(buffer, sizeof(buffer))) {
std::cout << "libkrbn_get_notification_message_body: " << buffer << std::endl;
}

std::thread thread([] {
global_wait->wait_notice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ private func callback(
) {

Task { @MainActor in
let body = String(cString: libkrbn_get_notification_message_body())
var buffer = [Int8](repeating: 0, count: 32 * 1024)
var body = ""

if libkrbn_get_notification_message_body(&buffer, buffer.count) {
body = String(cString: buffer)
}

NotificationMessage.shared.text = body.trimmingCharacters(in: .whitespacesAndNewlines)
NotificationWindowManager.shared.updateWindowsVisibility()
}
Expand Down
9 changes: 5 additions & 4 deletions src/apps/SettingsWindow/src/AppIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public class AppIcons: ObservableObject {
icons.append(AppIcon(1))
icons.append(AppIcon(2))

if let jsonData = try? Data(
contentsOf: URL(
fileURLWithPath: String(cString: libkrbn_get_system_app_icon_configuration_file_path())))
{
var buffer = [Int8](repeating: 0, count: 32 * 1024)
libkrbn_get_system_app_icon_configuration_file_path(&buffer, buffer.count)
let path = String(cString: buffer)

if let jsonData = try? Data(contentsOf: URL(fileURLWithPath: path)) {
if let jsonDict =
try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any]
{
Expand Down
30 changes: 18 additions & 12 deletions src/apps/SettingsWindow/src/ComplexModificationsAssetFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,33 @@ final class ComplexModificationsAssetFiles: ObservableObject {

let filesSize = libkrbn_complex_modifications_assets_manager_get_files_size()
for fileIndex in 0..<filesSize {
var buffer = [Int8](repeating: 0, count: 32 * 1024)

var rules: [LibKrbn.ComplexModificationsAssetRule] = []

let rulesSize = libkrbn_complex_modifications_assets_manager_get_rules_size(fileIndex)
for ruleIndex in 0..<rulesSize {
rules.append(
LibKrbn.ComplexModificationsAssetRule(
fileIndex,
ruleIndex,
String(
cString: libkrbn_complex_modifications_assets_manager_get_rule_description(
fileIndex, ruleIndex
)
)
))
var ruleDescription = ""
if libkrbn_complex_modifications_assets_manager_get_rule_description(
fileIndex, ruleIndex, &buffer, buffer.count)
{
ruleDescription = String(cString: buffer)
}

rules.append(LibKrbn.ComplexModificationsAssetRule(fileIndex, ruleIndex, ruleDescription))
}

var fileTitle = ""
if libkrbn_complex_modifications_assets_manager_get_file_title(
fileIndex, &buffer, buffer.count)
{
fileTitle = String(cString: buffer)
}

newFiles.append(
LibKrbn.ComplexModificationsAssetFile(
fileIndex,
String(
cString: libkrbn_complex_modifications_assets_manager_get_file_title(fileIndex)),
fileTitle,
libkrbn_complex_modifications_assets_manager_user_file(fileIndex),
Date(
timeIntervalSince1970: TimeInterval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ final class ComplexModificationsFileImport: ObservableObject {

public func save() {
if let data = self.jsonData {
let directory = String(cString: libkrbn_get_user_complex_modifications_assets_directory())
var buffer = [Int8](repeating: 0, count: 32 * 1024)
libkrbn_get_user_complex_modifications_assets_directory(&buffer, buffer.count)
let directory = String(cString: buffer)

let time = Int(NSDate().timeIntervalSince1970)
let path = URL(fileURLWithPath: "\(directory)/\(time).json")

Expand Down
29 changes: 15 additions & 14 deletions src/apps/SettingsWindow/src/LogMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ enum LogLevel {
case error
}

private func callback(
_ logLines: UnsafeMutableRawPointer?,
_ context: UnsafeMutableRawPointer?
) {
if logLines == nil { return }

private func callback() {
var logMessageEntries: [LogMessageEntry] = []
let size = libkrbn_log_lines_get_size(logLines)
let size = libkrbn_log_lines_get_size()
for i in 0..<size {
let line = libkrbn_log_lines_get_line(logLines, i)
if line != nil {
var buffer = [Int8](repeating: 0, count: 32 * 1024)
var line = ""
if libkrbn_log_lines_get_line(i, &buffer, buffer.count) {
line = String(cString: buffer)
}

if line != "" {
var logLevel = LogLevel.info
if libkrbn_log_lines_is_warn_line(line) {
logLevel = LogLevel.warn
Expand All @@ -27,15 +27,14 @@ private func callback(

logMessageEntries.append(
LogMessageEntry(
text: String(cString: line!),
text: line,
logLevel: logLevel,
dateNumber: libkrbn_log_lines_get_date_number(line)))
}
}

let logMessageEntriesCopy = logMessageEntries
Task { @MainActor in
LogMessages.shared.setEntries(logMessageEntriesCopy)
Task { @MainActor [logMessageEntries] in
LogMessages.shared.setEntries(logMessageEntries)
}
}

Expand Down Expand Up @@ -85,7 +84,9 @@ public class LogMessages: ObservableObject {
public func watch() {
entries = []

libkrbn_enable_log_monitor(callback, nil)
libkrbn_enable_log_monitor()
libkrbn_register_log_messages_updated_callback(callback)
callback()

//
// Create timer
Expand Down
8 changes: 5 additions & 3 deletions src/apps/SettingsWindow/src/View/MiscView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ struct MiscView: View {
HStack {
Button(
action: {
let url = URL(
fileURLWithPath: String(cString: libkrbn_get_user_configuration_directory()),
isDirectory: true)
var buffer = [Int8](repeating: 0, count: 32 * 1024)
libkrbn_get_user_configuration_directory(&buffer, buffer.count)
let path = String(cString: buffer)

let url = URL(fileURLWithPath: path, isDirectory: true)
NSWorkspace.shared.open(url)
},
label: {
Expand Down
10 changes: 7 additions & 3 deletions src/apps/share/swift/KarabinerAppHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ final class KarabinerAppHelper: NSObject {
}

func observeConsoleUserServerIsDisabledNotification() {
let name = String(
cString: libkrbn_get_distributed_notification_console_user_server_is_disabled())
let object = String(cString: libkrbn_get_distributed_notification_observed_object())
var buffer = [Int8](repeating: 0, count: 32 * 1024)

libkrbn_get_distributed_notification_console_user_server_is_disabled(&buffer, buffer.count)
let name = String(cString: buffer)

libkrbn_get_distributed_notification_observed_object(&buffer, buffer.count)
let object = String(cString: buffer)

DistributedNotificationCenter.default().addObserver(
self,
Expand Down
80 changes: 40 additions & 40 deletions src/apps/share/swift/LibKrbn/ConnectedDevices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import Combine
import Foundation
import SwiftUI

private func callback(
_ initializedConnectedDevices: UnsafeMutableRawPointer?,
_ context: UnsafeMutableRawPointer?
) {
if initializedConnectedDevices == nil { return }

private func callback() {
Task { @MainActor in
LibKrbn.ConnectedDevices.shared.update(initializedConnectedDevices)
LibKrbn.ConnectedDevices.shared.update()
}
}

Expand All @@ -21,32 +16,38 @@ extension LibKrbn {
@Published var connectedDevices: [ConnectedDevice] = []

private init() {
libkrbn_enable_connected_devices_monitor(callback, nil)
libkrbn_enable_connected_devices_monitor()
libkrbn_register_connected_devices_updated_callback(callback)
callback()
}

public func update(_ libkrbnConnectedDevices: UnsafeMutableRawPointer?) {
var newConnectedDevices: [ConnectedDevice] = []
public func update() {
var newConnectedDevices: [LibKrbn.ConnectedDevice] = []

let size = libkrbn_connected_devices_get_size(libkrbnConnectedDevices)
let size = libkrbn_connected_devices_get_size()
for i in 0..<size {
let transport = String(
cString: libkrbn_connected_devices_get_descriptions_transport(
libkrbnConnectedDevices, i)
)
var buffer = [Int8](repeating: 0, count: 32 * 1024)
var transport = ""
var manufacturerName = ""
var productName = ""
var deviceAddress = ""

var manufacturerName = String(
cString: libkrbn_connected_devices_get_descriptions_manufacturer(
libkrbnConnectedDevices, i)
)
.replacingOccurrences(of: "[\r\n]", with: " ", options: .regularExpression)
if libkrbn_connected_devices_get_descriptions_transport(i, &buffer, buffer.count) {
transport = String(cString: buffer)
}

if libkrbn_connected_devices_get_descriptions_manufacturer(i, &buffer, buffer.count) {
manufacturerName = String(cString: buffer)
.replacingOccurrences(of: "[\r\n]", with: " ", options: .regularExpression)
}
if manufacturerName == "" {
manufacturerName = "No manufacturer name"
}

var productName = String(
cString: libkrbn_connected_devices_get_descriptions_product(libkrbnConnectedDevices, i)
)
.replacingOccurrences(of: "[\r\n]", with: " ", options: .regularExpression)
if libkrbn_connected_devices_get_descriptions_product(i, &buffer, buffer.count) {
productName = String(cString: buffer)
.replacingOccurrences(of: "[\r\n]", with: " ", options: .regularExpression)
}
if productName == "" {
if transport == "FIFO" {
productName = "Apple Internal Keyboard / Trackpad"
Expand All @@ -55,26 +56,25 @@ extension LibKrbn {
}
}

let connectedDevice = ConnectedDevice(
if libkrbn_connected_devices_get_device_address(i, &buffer, buffer.count) {
deviceAddress = String(cString: buffer)
}

let connectedDevice = LibKrbn.ConnectedDevice(
index: i,
manufacturerName: manufacturerName,
productName: productName,
transport: transport,
vendorId: libkrbn_connected_devices_get_vendor_id(libkrbnConnectedDevices, i),
productId: libkrbn_connected_devices_get_product_id(libkrbnConnectedDevices, i),
deviceAddress: libkrbn_connected_devices_get_device_address(libkrbnConnectedDevices, i),
isKeyboard: libkrbn_connected_devices_get_is_keyboard(libkrbnConnectedDevices, i),
isPointingDevice: libkrbn_connected_devices_get_is_pointing_device(
libkrbnConnectedDevices, i),
isGamePad: libkrbn_connected_devices_get_is_game_pad(
libkrbnConnectedDevices, i),
isBuiltInKeyboard: libkrbn_connected_devices_get_is_built_in_keyboard(
libkrbnConnectedDevices, i),
isBuiltInTrackpad: libkrbn_connected_devices_get_is_built_in_trackpad(
libkrbnConnectedDevices, i),
isBuiltInTouchBar: libkrbn_connected_devices_get_is_built_in_touch_bar(
libkrbnConnectedDevices, i),
isAppleDevice: libkrbn_connected_devices_is_apple(libkrbnConnectedDevices, i)
vendorId: libkrbn_connected_devices_get_vendor_id(i),
productId: libkrbn_connected_devices_get_product_id(i),
deviceAddress: deviceAddress,
isKeyboard: libkrbn_connected_devices_get_is_keyboard(i),
isPointingDevice: libkrbn_connected_devices_get_is_pointing_device(i),
isGamePad: libkrbn_connected_devices_get_is_game_pad(i),
isBuiltInKeyboard: libkrbn_connected_devices_get_is_built_in_keyboard(i),
isBuiltInTrackpad: libkrbn_connected_devices_get_is_built_in_trackpad(i),
isBuiltInTouchBar: libkrbn_connected_devices_get_is_built_in_touch_bar(i),
isAppleDevice: libkrbn_connected_devices_is_apple(i)
)

newConnectedDevices.append(connectedDevice)
Expand Down
Loading

0 comments on commit 54c78bd

Please sign in to comment.