Skip to content

Commit

Permalink
Merge pull request #3944 from pqrs-org/open_application
Browse files Browse the repository at this point in the history
Add open_application
  • Loading branch information
tekezo authored Oct 6, 2024
2 parents 786ae74 + f8a39a9 commit 6e33de8
Show file tree
Hide file tree
Showing 40 changed files with 243 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <pqrs/osx/iokit_power_management.hpp>
#include <pqrs/osx/iokit_return.hpp>
#include <pqrs/osx/system_preferences.hpp>
#include <pqrs/osx/workspace.hpp>

namespace krbn {
namespace console_user_server {
Expand All @@ -26,6 +27,8 @@ class software_function_handler final : public pqrs::dispatcher::extra::dispatch
execute_cg_event_double_click(*v);
} else if (auto v = software_function.get_if<software_function_details::iokit_power_management_sleep_system>()) {
execute_iokit_power_management_sleep_system(*v);
} else if (auto v = software_function.get_if<software_function_details::open_application>()) {
execute_open_application(*v);
} else if (auto v = software_function.get_if<software_function_details::set_mouse_cursor_position>()) {
execute_set_mouse_cursor_position(*v);
}
Expand Down Expand Up @@ -56,6 +59,14 @@ class software_function_handler final : public pqrs::dispatcher::extra::dispatch
when_now() + pqrs::osx::chrono::make_milliseconds(duration));
}

void execute_open_application(const software_function_details::open_application& open_application) {
if (auto v = open_application.get_bundle_identifier()) {
pqrs::osx::workspace::open_application_by_bundle_identifier(*v);
} else if (auto v = open_application.get_file_path()) {
pqrs::osx::workspace::open_application_by_file_path(*v);
}
}

void execute_set_mouse_cursor_position(const software_function_details::set_mouse_cursor_position& set_mouse_cursor_position) {
if (auto target_display_id = get_target_display_id(set_mouse_cursor_position)) {
auto local_display_point = set_mouse_cursor_position.get_point(CGDisplayBounds(*target_display_id));
Expand Down
1 change: 1 addition & 0 deletions src/core/console_user_server/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ targets:
- '-std=c++20'
- path: ../../vendor/cget/src/pqrs/osx/frontmost_application_monitor
- path: ../../vendor/cget/src/pqrs/osx/process_info
- path: ../../vendor/cget/src/pqrs/osx/workspace
dependencies:
- framework: Carbon.framework
13 changes: 13 additions & 0 deletions src/share/types/software_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "software_function_details/cg_event_double_click.hpp"
#include "software_function_details/iokit_power_management_sleep_system.hpp"
#include "software_function_details/open_application.hpp"
#include "software_function_details/set_mouse_cursor_position.hpp"
#include <pqrs/hash.hpp>
#include <pqrs/json.hpp>
Expand All @@ -11,6 +12,7 @@ class software_function final {
public:
using value_t = std::variant<software_function_details::cg_event_double_click,
software_function_details::iokit_power_management_sleep_system,
software_function_details::open_application,
software_function_details::set_mouse_cursor_position,
std::monostate>;

Expand Down Expand Up @@ -43,6 +45,8 @@ inline void to_json(nlohmann::json& json, const software_function& value) {
json = nlohmann::json::object({{"cg_event_double_click", *v}});
} else if (auto v = value.get_if<software_function_details::iokit_power_management_sleep_system>()) {
json = nlohmann::json::object({{"iokit_power_management_sleep_system", *v}});
} else if (auto v = value.get_if<software_function_details::open_application>()) {
json = nlohmann::json::object({{"open_application", *v}});
} else if (auto v = value.get_if<software_function_details::set_mouse_cursor_position>()) {
json = nlohmann::json::object({{"set_mouse_cursor_position", *v}});
}
Expand All @@ -58,19 +62,28 @@ inline void from_json(const nlohmann::json& json, software_function& value) {
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", k, e.what()));
}

} else if (k == "iokit_power_management_sleep_system") {
try {
value.set_value(v.get<software_function_details::iokit_power_management_sleep_system>());
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", k, e.what()));
}

} else if (k == "open_application") {
try {
value.set_value(v.get<software_function_details::open_application>());
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", k, e.what()));
}

} else if (k == "set_mouse_cursor_position") {
try {
value.set_value(v.get<software_function_details::set_mouse_cursor_position>());
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", k, e.what()));
}

} else {
throw pqrs::json::unmarshal_error(fmt::format("unknown key: `{0}`", k));
}
Expand Down
78 changes: 78 additions & 0 deletions src/share/types/software_function_details/open_application.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include <CoreGraphics/CoreGraphics.h>
#include <optional>
#include <pqrs/hash.hpp>
#include <pqrs/json.hpp>
#include <regex>

namespace krbn {
namespace software_function_details {
class open_application {
public:
open_application(void) {
}

const std::optional<std::string>& get_bundle_identifier(void) const {
return bundle_identifier_;
}

void set_bundle_identifier(const std::optional<std::string>& value) {
bundle_identifier_ = value;
}

const std::optional<std::string>& get_file_path(void) const {
return file_path_;
}

void set_file_path(const std::optional<std::string>& value) {
file_path_ = value;
}

constexpr bool operator==(const open_application&) const = default;

private:
std::optional<std::string> bundle_identifier_;
std::optional<std::string> file_path_;
};

inline void to_json(nlohmann::json& json, const open_application& value) {
if (auto v = value.get_bundle_identifier()) {
json["bundle_identifier"] = *v;
}
if (auto v = value.get_file_path()) {
json["file_path"] = *v;
}
}

inline void from_json(const nlohmann::json& json, open_application& value) {
pqrs::json::requires_object(json, "json");

for (const auto& [k, v] : json.items()) {
if (k == "bundle_identifier") {
pqrs::json::requires_string(v, "`" + k + "`");
value.set_bundle_identifier(v.get<std::string>());
} else if (k == "file_path") {
pqrs::json::requires_string(v, "`" + k + "`");
value.set_file_path(v.get<std::string>());
} else {
throw pqrs::json::unmarshal_error(fmt::format("unknown key: `{0}`", k));
}
}
}
} // namespace software_function_details
} // namespace krbn

namespace std {
template <>
struct hash<krbn::software_function_details::open_application> final {
std::size_t operator()(const krbn::software_function_details::open_application& value) const {
std::size_t h = 0;

pqrs::hash::combine(h, value.get_bundle_identifier());
pqrs::hash::combine(h, value.get_file_path());

return h;
}
};
} // namespace std
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

// pqrs::osx::workspace v2.3
// pqrs::osx::workspace v2.4

// (C) Copyright Takayama Fumihiko 2022.
// Distributed under the Boost Software License, Version 1.0.
Expand All @@ -13,6 +13,14 @@ namespace pqrs {
namespace osx {
namespace workspace {

inline void open_application_by_bundle_identifier(const std::string& bundle_identifier) {
pqrs_osx_workspace_open_application_by_bundle_identifier(bundle_identifier.c_str());
}

inline void open_application_by_file_path(const std::string& file_path) {
pqrs_osx_workspace_open_application_by_file_path(file_path.c_str());
}

inline std::string find_application_url_by_bundle_identifier(const std::string& bundle_identifier) {
char buffer[512];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extern "C" {

// Do not use these functions directly.

void pqrs_osx_workspace_open_application_by_bundle_identifier(const char* bundle_identifier);

void pqrs_osx_workspace_open_application_by_file_path(const char* file_path);

void pqrs_osx_workspace_find_application_url_by_bundle_identifier(const char* bundle_identifier,
char* buffer,
int buffer_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@

import AppKit

@_cdecl("pqrs_osx_workspace_open_application_by_bundle_identifier")
func pqrs_osx_workspace_open_application_by_bundle_identifier(
_ bundleIdentifierPtr: UnsafePointer<Int8>
) {
let bundleIdentifier = String(cString: bundleIdentifierPtr)

if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) {
NSWorkspace.shared.openApplication(
at: url,
configuration: NSWorkspace.OpenConfiguration(),
completionHandler: nil
)
}
}

@_cdecl("pqrs_osx_workspace_open_application_by_file_path")
func pqrs_osx_workspace_open_application_by_file_path(_ filePathPtr: UnsafePointer<Int8>) {
let filePath = String(cString: filePathPtr)

let url = URL(filePath: filePath, directoryHint: .notDirectory, relativeTo: nil)
NSWorkspace.shared.openApplication(
at: url,
configuration: NSWorkspace.OpenConfiguration(),
completionHandler: nil
)
}

@_cdecl("pqrs_osx_workspace_find_application_url_by_bundle_identifier")
func pqrs_osx_workspace_find_application_url_by_bundle_identifier(
_ bundleIdentifierPtr: UnsafePointer<Int8>,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/async_file_writer/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: build_make
rm -rf tmp/*
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test
[ `cat tmp/example` = 'example3' ]
[ `stat -f '%p' tmp/mode644` = '100644' ]
[ `stat -f '%p' tmp/mode666` = '100666' ]
Expand Down
2 changes: 1 addition & 1 deletion tests/src/complex_modifications_assets/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
3 changes: 1 addition & 2 deletions tests/src/configuration_monitor/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

include ../Makefile.rules

2 changes: 1 addition & 1 deletion tests/src/connected_devices/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/connected_devices_monitor/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/core_configuration/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/device_properties/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/device_properties_manager/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/duktape_utility/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/event_queue/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/event_tap_utility/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/exprtk_utility/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/iokit_utility/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/keyboard_repeat_detector/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator_basic/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator_conditions/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator_mouse_basic/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator_mouse_motion_to_scroll/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
2 changes: 1 addition & 1 deletion tests/src/manipulator_types/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: build_make
./build/karabiner_test
MallocNanoZone=0 ./build/karabiner_test

clean: clean_builds

Expand Down
Loading

0 comments on commit 6e33de8

Please sign in to comment.