Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clipboard: fix wrong memory layout with tiny string #167

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- name: Lint
run: |
find macosfrontend macosnotifications webpanel src tests -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run -style=file:fcitx5/.clang-format
clang-format -Werror --dry-run macosfrontend/pasteboard.mm
swift-format lint --configuration .swift-format.json -rs macosfrontend macosnotifications src assets
./check-code-style.sh
file assets/zh-Hans.lproj/Localizable.strings | grep UTF-16
Expand Down
2 changes: 1 addition & 1 deletion macosfrontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _swift_generate_cxx_header(
SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR}"
)

add_library(macosfrontend STATIC macosfrontend.cpp keycode.cpp)
add_library(macosfrontend STATIC macosfrontend.cpp pasteboard.mm keycode.cpp)
add_dependencies(macosfrontend SwiftFrontend)
target_link_libraries(macosfrontend Fcitx5::Core)
target_include_directories(macosfrontend PUBLIC
Expand Down
5 changes: 2 additions & 3 deletions macosfrontend/macosfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ void MacosFrontend::pollPasteboard() {
if (auto clipboard =
instance_->addonManager().addon("clipboard", true)) {
bool isPassword = false;
const char *p = SwiftFrontend::getPasteboardString(&isPassword);
if (p) {
std::string str = p;
std::string str = getPasteboardString(&isPassword);
if (!str.empty()) {
clipboard->call<IClipboard::setClipboardV2>("", str,
isPassword);
FCITX_DEBUG() << "Add to clipboard: " << str;
Expand Down
2 changes: 2 additions & 0 deletions macosfrontend/macosfrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ class MacosFrontendFactory : public AddonFactory {
}
};

std::string getPasteboardString(bool *isPassword);

} // namespace fcitx

#endif
19 changes: 0 additions & 19 deletions macosfrontend/macosfrontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,3 @@ public func getCursorCoordinates(
}
return false
}

private var lastChangeCount = 0
private var data: Data?
private let passwordType = NSPasteboard.PasteboardType("org.nspasteboard.ConcealedType")

// Retrieve the current string data from the default pasteboard.
public func getPasteboardString(_ isPassword: UnsafeMutablePointer<Bool>) -> UnsafePointer<CChar>? {
let pasteboard = NSPasteboard.general
if pasteboard.changeCount == lastChangeCount {
return nil
}
lastChangeCount = pasteboard.changeCount
data = pasteboard.data(forType: .string)
if let data = data {
isPassword.pointee = pasteboard.string(forType: passwordType) != nil
return data.withUnsafeBytes { $0.baseAddress?.assumingMemoryBound(to: CChar.self) }
}
return nil
}
21 changes: 21 additions & 0 deletions macosfrontend/pasteboard.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "macosfrontend.h"
#import <AppKit/AppKit.h>

static int lastChangeCount = 0;
static NSPasteboardType passwordType = @"org.nspasteboard.ConcealedType";

namespace fcitx {
std::string getPasteboardString(bool *isPassword) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
if (pasteboard.changeCount == lastChangeCount) {
return "";
}
lastChangeCount = (int)pasteboard.changeCount;
NSString *stringData = [pasteboard stringForType:NSPasteboardTypeString];
if (stringData) {
*isPassword = ([pasteboard stringForType:passwordType] != nil);
return std::string([stringData UTF8String]);
}
return "";
}
} // namespace fcitx