Skip to content

Commit

Permalink
Update vendor/cget
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Dec 2, 2024
1 parent b19d587 commit cda1136
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

// pqrs::osx::frontmost_application_monitor v5.0
// pqrs::osx::frontmost_application_monitor v5.1

// (C) Copyright Takayama Fumihiko 2019.
// Distributed under the Boost Software License, Version 1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class application final {
return *this;
}

const std::optional<std::string>& get_bundle_path(void) const {
return bundle_path_;
}

application& set_bundle_path(const std::optional<std::string>& value) {
bundle_path_ = value;
return *this;
}

const std::optional<std::string>& get_file_path(void) const {
return file_path_;
}
Expand All @@ -34,9 +43,20 @@ class application final {
return *this;
}

const std::optional<pid_t>& get_pid(void) const {
return pid_;
}

application& set_pid(const std::optional<pid_t>& value) {
pid_ = value;
return *this;
}

bool operator==(const application& other) const {
return bundle_identifier_ == other.bundle_identifier_ &&
file_path_ == other.file_path_;
bundle_path_ == other.bundle_path_ &&
file_path_ == other.file_path_ &&
pid_ == other.pid_;
}

bool operator!=(const application& other) const {
Expand All @@ -45,7 +65,9 @@ class application final {

private:
std::optional<std::string> bundle_identifier_;
std::optional<std::string> bundle_path_;
std::optional<std::string> file_path_;
std::optional<pid_t> pid_;
};
} // namespace frontmost_application_monitor
} // namespace osx
Expand All @@ -63,12 +85,24 @@ struct hash<pqrs::osx::frontmost_application_monitor::application> final {
pqrs::hash::combine(h, 0);
}

if (auto& bundle_path = value.get_bundle_path()) {
pqrs::hash::combine(h, *bundle_path);
} else {
pqrs::hash::combine(h, 0);
}

if (auto& file_path = value.get_file_path()) {
pqrs::hash::combine(h, *file_path);
} else {
pqrs::hash::combine(h, 0);
}

if (auto& pid = value.get_pid()) {
pqrs::hash::combine(h, *pid);
} else {
pqrs::hash::combine(h, 0);
}

return h;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ inline void to_json(nlohmann::json& j, const application& s) {
j["bundle_identifier"] = *v;
}

if (auto& v = s.get_bundle_path()) {
j["bundle_path"] = *v;
}

if (auto& v = s.get_file_path()) {
j["file_path"] = *v;
}

if (auto& v = s.get_pid()) {
j["pid"] = *v;
}
}

inline void from_json(const nlohmann::json& j, application& s) {
Expand All @@ -33,11 +41,21 @@ inline void from_json(const nlohmann::json& j, application& s) {

s.set_bundle_identifier(value.get<std::string>());

} else if (key == "bundle_path") {
json::requires_string(value, "`"s + key + "`");

s.set_bundle_path(value.get<std::string>());

} else if (key == "file_path") {
json::requires_string(value, "`"s + key + "`");

s.set_file_path(value.get<std::string>());

} else if (key == "pid") {
json::requires_number(value, "`"s + key + "`");

s.set_pid(value.get<pid_t>());

} else {
throw json::unmarshal_error("unknown key: `"s + key + "`"s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
// Distributed under the Boost Software License, Version 1.0.
// (See https://www.boost.org/LICENSE_1_0.txt)

#include <unistd.h>

#ifdef __cplusplus
extern "C" {
#endif

// Do not use these functions directly.

typedef void (*pqrs_osx_frontmost_application_monitor_callback)(const char* bundle_identifier,
const char* file_path);
const char* bundle_path,
const char* file_path,
pid_t pid);

void pqrs_osx_frontmost_application_monitor_set_callback(pqrs_osx_frontmost_application_monitor_callback callback);
void pqrs_osx_frontmost_application_monitor_unset_callback(void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,35 @@ class monitor final : public dispatcher::extra::dispatcher_client {

private:
static void static_cpp_callback(const char* bundle_identifier,
const char* file_path) {
const char* bundle_path,
const char* file_path,
pid_t pid) {
auto m = shared_monitor_;
if (m) {
m->cpp_callback(bundle_identifier,
file_path);
bundle_path,
file_path,
pid);
}
}

void cpp_callback(const char* bundle_identifier,
const char* file_path) {
const char* bundle_path,
const char* file_path,
pid_t pid) {
auto application_ptr = std::make_shared<application>();
if (bundle_identifier) {
application_ptr->set_bundle_identifier(bundle_identifier);
}
if (bundle_path) {
application_ptr->set_bundle_path(bundle_path);
}
if (file_path) {
application_ptr->set_file_path(file_path);
}
if (pid) {
application_ptr->set_pid(pid);
}

enqueue_to_dispatcher([this, application_ptr] {
frontmost_application_changed(application_ptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ private class PQRSOSXFrontmostApplicationMonitor {
return
}

let bundleIdentifier = runningApplication.bundleIdentifier ?? ""
let path = runningApplication.executableURL?.path ?? ""

self.runCallback(bundleIdentifier: bundleIdentifier, path: path)
self.runCallback(runningApplication)
}
}

Expand All @@ -52,25 +49,31 @@ private class PQRSOSXFrontmostApplicationMonitor {
}
}

func runCallback(bundleIdentifier: String, path: String) {
func runCallback(_ runningApplication: NSRunningApplication) {
let bundleIdentifier = runningApplication.bundleIdentifier ?? ""
let bundlePath = runningApplication.bundleURL?.path ?? ""
let filePath = runningApplication.executableURL?.path ?? ""
let processIdentifier = runningApplication.processIdentifier

lock.withLock {
bundleIdentifier.utf8CString.withUnsafeBufferPointer { bundleIdentifierPtr in
path.utf8CString.withUnsafeBufferPointer { pathPtr in
callback?(
bundleIdentifierPtr.baseAddress,
pathPtr.baseAddress
)
bundlePath.utf8CString.withUnsafeBufferPointer { bundlePathPtr in
filePath.utf8CString.withUnsafeBufferPointer { filePathPtr in
callback?(
bundleIdentifierPtr.baseAddress,
bundlePathPtr.baseAddress,
filePathPtr.baseAddress,
processIdentifier
)
}
}
}
}
}

func runCallbackWithFrontmostApplication() {
if let runningApplication = NSWorkspace.shared.frontmostApplication {
let bundleIdentifier = runningApplication.bundleIdentifier ?? ""
let path = runningApplication.executableURL?.path ?? ""

runCallback(bundleIdentifier: bundleIdentifier, path: path)
runCallback(runningApplication)
}
}
}
Expand Down

0 comments on commit cda1136

Please sign in to comment.