Skip to content

Commit

Permalink
Merge pull request #3917 from pqrs-org/improve-globe-key-supports
Browse files Browse the repository at this point in the history
[WIP] Improve globe key supports
  • Loading branch information
tekezo authored Sep 28, 2024
2 parents 026ab2f + 27e7501 commit f1eebd2
Show file tree
Hide file tree
Showing 95 changed files with 1,363 additions and 2,336 deletions.
6 changes: 0 additions & 6 deletions appendix/dump_libkrbn/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ void system_preferences_updated_callback(void) {
std::cout << " 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 manipulator_environment_json_file_updated_callback(void) {
Expand Down
1 change: 1 addition & 0 deletions pkginfo/Scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ chmod 4755 '/Library/Application Support/org.pqrs/Karabiner-Elements/bin/karabin
# Relaunch karabiner processes
#

killall Karabiner-VirtualHIDDevice-Daemon
killall karabiner_grabber
killall karabiner_session_monitor
killall karabiner_console_user_server
Expand Down
2 changes: 1 addition & 1 deletion src/apps/EventViewer/src/SystemExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SystemExtensions: ObservableObject {
command.waitUntilExit()

if let data = try? pipe.fileHandleForReading.readToEnd() {
list = String(decoding: data, as: UTF8.self)
list = String(data: data, encoding: .utf8) ?? ""
}
}
}
1 change: 1 addition & 0 deletions src/apps/SettingsWindow/src/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class AppDelegate: NSObject, NSApplicationDelegate {
LibKrbn.GrabberClient.shared.start("")
LibKrbn.Settings.shared.watch()
ServicesMonitor.shared.start()
SettingsChecker.shared.start()
StateJsonMonitor.shared.start()
SystemPreferences.shared.start()

Expand Down
1 change: 1 addition & 0 deletions src/apps/SettingsWindow/src/ContentViewStates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class ContentViewStates: ObservableObject {
@Published public var showDriverVersionMismatchedAlert = false
@Published public var showInputMonitoringPermissionsAlert = false
@Published public var showDoctorAlert = false
@Published public var showSettingsAlert = false

//
// ContentMainView
Expand Down
30 changes: 30 additions & 0 deletions src/apps/SettingsWindow/src/SettingsChecker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Combine
import Foundation
import SwiftUI

final class SettingsChecker: ObservableObject {
static let shared = SettingsChecker()

@ObservedObject private var settings = LibKrbn.Settings.shared
@Published var keyboardTypeEmpty = false
private var subscribers: Set<AnyCancellable> = []

public func start() {
settings.$virtualHIDKeyboardKeyboardTypeV2.sink { [weak self] newValue in
self?.checkVirtualHIDKeyboardKeyboardTypeV2(newValue)
}.store(in: &subscribers)
}

private func checkVirtualHIDKeyboardKeyboardTypeV2(_ virtualHIDKeyboardKeyboardTypeV2: String) {
keyboardTypeEmpty = (virtualHIDKeyboardKeyboardTypeV2 == "")
updateShowSettingsAlert()
}

private func updateShowSettingsAlert() {
if keyboardTypeEmpty {
ContentViewStates.shared.showSettingsAlert = true
} else {
ContentViewStates.shared.showSettingsAlert = false
}
}
}
25 changes: 0 additions & 25 deletions src/apps/SettingsWindow/src/SystemPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ private func callback() {
final class SystemPreferences: ObservableObject {
static let shared = SystemPreferences()

// This variable will be used in VirtualKeyboardView in order to show "log out required" message.
@Published var keyboardTypeChanged = false

private var didSetEnabled = false

private init() {
Expand All @@ -36,16 +33,6 @@ final class SystemPreferences: ObservableObject {
useFkeysAsStandardFunctionKeys =
libkrbn_system_preferences_properties_get_use_fkeys_as_standard_function_keys()

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 All @@ -59,16 +46,4 @@ final class SystemPreferences: ObservableObject {
}
}
}

@Published var keyboardTypes: [LibKrbn.KeyboardType] = [] {
didSet {
if didSetEnabled {
keyboardTypes.forEach { keyboardType in
LibKrbn.GrabberClient.shared.setKeyboardType(keyboardType)
}

keyboardTypeChanged = true
}
}
}
}
4 changes: 4 additions & 0 deletions src/apps/SettingsWindow/src/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct ContentView: View {
OverlayAlertView {
DriverNotActivatedAlertView()
}
} else if contentViewStates.showSettingsAlert {
OverlayAlertView {
SettingsAlertView()
}
}
}
.frame(
Expand Down
2 changes: 1 addition & 1 deletion src/apps/SettingsWindow/src/View/DeviceSelectorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct DeviceSelectorView: View {

ForEach(connectedDevices.connectedDevices) { connectedDevice in
if settings.findConnectedDeviceSetting(connectedDevice)?.modifyEvents ?? false,
!connectedDevice.isKarabinerVirtualHidDevice
!connectedDevice.isVirtualDevice
{
Button(
action: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,9 @@ struct DevicesGamePadSettingsView_Previews: PreviewProvider {
isKeyboard: false,
isPointingDevice: false,
isGamePad: true,
isVirtualDevice: false,
isBuiltInKeyboard: false,
isBuiltInTrackpad: false,
isBuiltInTouchBar: false,
isAppleDevice: false,
isKarabinerVirtualHidDevice: false
isAppleDevice: false
)
@State static var connectedDeviceSetting = LibKrbn.ConnectedDeviceSetting(connectedDevice)
@State static var showing = true
Expand Down
92 changes: 44 additions & 48 deletions src/apps/SettingsWindow/src/View/DevicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ struct DevicesView: View {
if let connectedDeviceSetting = settings.findConnectedDeviceSetting(connectedDevice) {
VStack(alignment: .leading, spacing: 0.0) {
DeviceName(connectedDevice: connectedDevice)
.if(connectedDevice.isKarabinerVirtualHidDevice) {
.if(connectedDevice.isVirtualDevice) {
$0.foregroundColor(Color(NSColor.placeholderTextColor))
}

if !connectedDevice.isKarabinerVirtualHidDevice {
if !connectedDevice.isVirtualDevice {
VStack(alignment: .leading, spacing: 0.0) {
ModifyEventsSetting(connectedDeviceSetting: connectedDeviceSetting)

Expand All @@ -42,7 +42,7 @@ struct DevicesView: View {
.stroke(
Color(NSColor.selectedControlColor),
lineWidth: settings.findConnectedDeviceSetting(connectedDevice)?.modifyEvents
?? false && !connectedDevice.isKarabinerVirtualHidDevice
?? false && !connectedDevice.isVirtualDevice
? 3 : 0
)
.padding(2)
Expand Down Expand Up @@ -121,81 +121,77 @@ struct DevicesView: View {

Text("\(connectedDevice.productName) (\(connectedDevice.manufacturerName))")
.padding(.leading, 12.0)
.frame(maxWidth: .infinity, alignment: .leading)

Spacer()
}
}
}

struct ModifyEventsSetting: View {
@ObservedObject var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting

@ObservedObject private var settings = LibKrbn.Settings.shared

var body: some View {
HStack(alignment: .top) {
if connectedDeviceSetting.connectedDevice.isAppleDevice,
!connectedDeviceSetting.connectedDevice.isKeyboard,
connectedDeviceSetting.connectedDevice.isPointingDevice,
!settings.unsafeUI
{
Text("Apple pointing devices are not supported")
.foregroundColor(Color(NSColor.placeholderTextColor))
.frame(maxWidth: .infinity, alignment: .leading)
} else {
Toggle(isOn: $connectedDeviceSetting.modifyEvents) {
Text("Modify events")
}
.switchToggleStyle()
.frame(width: 140.0)
}

Spacer()

if connectedDeviceSetting.connectedDevice.transport != "FIFO" {
if connectedDevice.transport != "FIFO" {
VStack(alignment: .trailing, spacing: 4.0) {
if connectedDeviceSetting.connectedDevice.vendorId != 0 {
if connectedDevice.vendorId != 0 {
HStack(alignment: .firstTextBaseline, spacing: 0) {
Spacer()

Text("Vendor ID: ")

Text(
String(
format: "%5d (0x%04x)",
connectedDeviceSetting.connectedDevice.vendorId,
connectedDeviceSetting.connectedDevice.vendorId)
format: "Vendor ID: %5d (0x%04x)",
connectedDevice.vendorId,
connectedDevice.vendorId)
)
}
}

if connectedDeviceSetting.connectedDevice.productId != 0 {
if connectedDevice.productId != 0 {
HStack(alignment: .center, spacing: 0) {
Spacer()

Text("Product ID: ")

Text(
String(
format: "%5d (0x%04x)",
connectedDeviceSetting.connectedDevice.productId,
connectedDeviceSetting.connectedDevice.productId)
format: "Product ID: %5d (0x%04x)",
connectedDevice.productId,
connectedDevice.productId)
)
}
}

if !connectedDeviceSetting.connectedDevice.deviceAddress.isEmpty {
if !connectedDevice.deviceAddress.isEmpty {
HStack(alignment: .center, spacing: 0) {
Spacer()

Text("Device Address: ")

Text(connectedDeviceSetting.connectedDevice.deviceAddress)
Text(connectedDevice.deviceAddress)
}
}
}
.font(.custom("Menlo", size: 12.0))
.frame(width: 220.0)
}
}
}
}

struct ModifyEventsSetting: View {
@ObservedObject var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting

@ObservedObject private var settings = LibKrbn.Settings.shared

var body: some View {
HStack(alignment: .top) {
if connectedDeviceSetting.connectedDevice.isAppleDevice,
!connectedDeviceSetting.connectedDevice.isKeyboard,
connectedDeviceSetting.connectedDevice.isPointingDevice,
!settings.unsafeUI
{
Text("Apple pointing devices are not supported")
.foregroundColor(Color(NSColor.placeholderTextColor))
.frame(maxWidth: .infinity, alignment: .leading)
} else {
Toggle(isOn: $connectedDeviceSetting.modifyEvents) {
Text("Modify events")
}
.switchToggleStyle()
.frame(width: 140.0)
}

Spacer()
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/apps/SettingsWindow/src/View/KeyboardTypeSelector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import SwiftUI

struct KeyboardTypeSelectorView: View {
@ObservedObject private var settings = LibKrbn.Settings.shared
@ObservedObject private var grabberClient = LibKrbn.GrabberClient.shared

var body: some View {
Picker(
selection: $settings.virtualHIDKeyboardKeyboardTypeV2, label: Text("Keyboard type:")
) {
Text("ANSI (North America, most of Asia and others)").tag("ansi")
Text("ISO (Europe, Latin America, Middle-East and others)").tag("iso")
Text("JIS (Japanese)").tag("jis")
}
.pickerStyle(RadioGroupPickerStyle())
.disabled(!grabberClient.connected)
}
}

struct KeyboardTypeSelectorView_Previews: PreviewProvider {
static var previews: some View {
KeyboardTypeSelectorView()
}
}
42 changes: 42 additions & 0 deletions src/apps/SettingsWindow/src/View/SettingsAlertView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import SwiftUI

struct SettingsAlertView: View {
@ObservedObject private var settingsChecker = SettingsChecker.shared
@FocusState var focus: Bool

var body: some View {
ZStack(alignment: .topLeading) {
VStack(alignment: .leading, spacing: 6.0) {
if settingsChecker.keyboardTypeEmpty {
VStack(alignment: .leading, spacing: 20.0) {
Label(
"Please select the keyboard type",
systemImage: "gearshape"
)
.font(.system(size: 24))

KeyboardTypeSelectorView()
}
}
}
.padding()
.frame(width: 850)

SheetCloseButton {
ContentViewStates.shared.showSettingsAlert = false
}
}
.onAppear {
focus = true
}
}
}

struct SettingsAlertView_Previews: PreviewProvider {
static var previews: some View {
Group {
SettingsAlertView()
.previewLayout(.sizeThatFits)
}
}
}
Loading

0 comments on commit f1eebd2

Please sign in to comment.