Skip to content

Commit

Permalink
Update for libkrbn updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Mar 30, 2024
1 parent 2a5c81f commit 5f99f35
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
19 changes: 12 additions & 7 deletions appendix/dump_libkrbn/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
#include <thread>

namespace {
void system_preferences_monitor_callback(const struct libkrbn_system_preferences_properties* properties,
void* refcon) {
std::cout << "system_preferences_monitor_callback" << std::endl;
void system_preferences_updated_callback(void) {
std::cout << "system_preferences_updated_callback" << std::endl;
std::cout << " use_fkeys_as_standard_function_keys: "
<< properties->use_fkeys_as_standard_function_keys
<< libkrbn_system_preferences_properties_get_use_fkeys_as_standard_function_keys()
<< std::endl;

for (int country_code = 0; country_code < 8; ++country_code) {
std::cout << " keyboard_type (country_code: " << country_code << "): "
<< libkrbn_system_preferences_properties_get_keyboard_type(country_code)
<< std::endl;
}
}

void connected_devices_updated_callback(void) {
Expand Down Expand Up @@ -82,9 +87,9 @@ int main(int argc, const char* argv[]) {
}
std::cout << std::endl;

libkrbn_enable_system_preferences_monitor(
system_preferences_monitor_callback,
nullptr);
libkrbn_enable_system_preferences_monitor();
libkrbn_register_system_preferences_updated_callback(system_preferences_updated_callback);
system_preferences_updated_callback();

if (libkrbn_get_notification_message_body(buffer, sizeof(buffer))) {
std::cout << "libkrbn_get_notification_message_body: " << buffer << std::endl;
Expand Down
37 changes: 15 additions & 22 deletions src/apps/SettingsWindow/src/SystemPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import Combine
import Foundation
import SwiftUI

private func callback(
_ systemPreferencesProperties: UnsafePointer<libkrbn_system_preferences_properties>?,
_ context: UnsafeMutableRawPointer?
) {
if systemPreferencesProperties == nil { return }

let systemPreferencesPropertiesCopy = systemPreferencesProperties!.pointee

private func callback() {
Task { @MainActor in
var properties = systemPreferencesPropertiesCopy
SystemPreferences.shared.updateProperties(&properties)
SystemPreferences.shared.updateProperties()
}
}

Expand All @@ -27,25 +19,26 @@ final class SystemPreferences: ObservableObject {
private init() {
didSetEnabled = true

libkrbn_enable_system_preferences_monitor(callback, nil)
libkrbn_enable_system_preferences_monitor()
libkrbn_register_system_preferences_updated_callback(callback)
callback()
}

public func updateProperties(
_ systemPreferencesProperties: inout libkrbn_system_preferences_properties
) {
public func updateProperties() {
didSetEnabled = false

useFkeysAsStandardFunctionKeys =
systemPreferencesProperties.use_fkeys_as_standard_function_keys
libkrbn_system_preferences_properties_get_use_fkeys_as_standard_function_keys()

let keyboardTypesSize = libkrbn_system_preferences_properties_get_keyboard_types_size()
withUnsafePointer(to: &(systemPreferencesProperties.keyboard_types.0)) { keyboardTypesPtr in
var newKeyboardTypes: [LibKrbn.KeyboardType] = []
for i in 0..<keyboardTypesSize {
newKeyboardTypes.append(LibKrbn.KeyboardType(i, Int(keyboardTypesPtr[i])))
}
keyboardTypes = newKeyboardTypes
let countryCodesCount = 8
var newKeyboardTypes: [LibKrbn.KeyboardType] = []
for i in 0..<countryCodesCount {
newKeyboardTypes.append(
LibKrbn.KeyboardType(
i,
Int(libkrbn_system_preferences_properties_get_keyboard_type(UInt64(i)))))
}
keyboardTypes = newKeyboardTypes

didSetEnabled = true
}
Expand Down

0 comments on commit 5f99f35

Please sign in to comment.