Skip to content

Commit

Permalink
Merge branch 'developement' into 'main'
Browse files Browse the repository at this point in the history
Developement

See merge request jesture/actions!28
  • Loading branch information
stormymcstorm committed Apr 14, 2023
2 parents 5367df9 + 86827ea commit 2dd6fe3
Show file tree
Hide file tree
Showing 37 changed files with 1,226 additions and 340 deletions.
8 changes: 6 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ load("//:repositories.bzl", "actions_repositories")

actions_repositories()

load("//:setup.bzl", "actions_setup")
load("@actions//display:display_configure.bzl", "display_configure")

actions_setup()
display_configure()

load("@display//:local_display.bzl", "display_repositories")

display_repositories()
7 changes: 6 additions & 1 deletion actions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cc_library(
hdrs = ["action.h"],
deps = [
"//actions/action:keystroke",
"//actions/action:mouse",
"//actions/action:target",
"@com_google_absl//absl/types:variant",
],
Expand All @@ -14,14 +15,18 @@ cc_library(
name = "actions",
srcs = ["actions.cc"],
hdrs = ["actions.h"],
copts = select({
"@display//:x11": ["-D __x11__"],
"//conditions:default": [],
}),
deps = [
":action",
"//actions/internal:connection",
"//actions/internal:util",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
] + select({
"@platforms//os:linux": ["//actions/internal/linux:xcb_connection"],
"@display//:x11": ["//actions/internal/x11:xcb_connection"],
"//conditions:default": ["//actions/internal/stub:stub_connection"],
}),
)
Expand Down
14 changes: 12 additions & 2 deletions actions/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@

#include "absl/types/variant.h"
#include "actions/action/keystroke.h"
#include "actions/action/mouse.h"
#include "actions/action/target.h"

namespace actions {

using Action = absl::variant<action::Keystroke>;
namespace action {

}
typedef struct NoOp {
} NoOp;
} // namespace action

using Action =
absl::variant<action::Keystroke, action::KeysPress, action::KeysRelease,
action::CursorMove, action::MouseClick, action::MousePress,
action::MouseRelease, action::NoOp>;

} // namespace actions

#endif // ACTIONS_ACTION_H
11 changes: 10 additions & 1 deletion actions/action/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ cc_library(
name = "keystroke",
srcs = ["keystroke.cc"],
hdrs = ["keystroke.h"],
copts = select({
"@display//:x11": ["-D __x11__"],
"//conditions:default": [],
}),
deps = [
"@com_google_absl//absl/types:variant",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
] + select({
"@platforms//os:linux": ["//actions/internal/linux:keystroke"],
"@display//:x11": ["//actions/internal/x11:keystroke"],
"//conditions:default": [],
}),
)

cc_library(
name = "mouse",
hdrs = ["mouse.h"],
)

cc_library(
name = "target",
hdrs = ["target.h"],
Expand Down
10 changes: 9 additions & 1 deletion actions/action/keystroke.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#include "actions/action/keystroke.h"

namespace actions::action {} // namespace actions::action
namespace actions::action {
absl::StatusOr<std::vector<Key> > ParseKeystroke(std::string str) noexcept {
#if defined(__x11__)
return internal::x11::ParseKeystroke(str);
#else
return std::vector<Key>();
#endif
}
} // namespace actions::action
28 changes: 19 additions & 9 deletions actions/action/keystroke.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"

#if defined(__linux__)
#include "actions/internal/linux/keystroke.h"
#if defined(__x11__)
#include "actions/internal/x11/keystroke.h"
#endif

namespace actions::action {

#if defined(__linux__)
using internal::linux::Key;
using internal::linux::ParseKeystroke;
#if defined(__x11__)
using internal::x11::Key;
#else
using Key = unsigned int;
absl::StatusOr<std::vector<Key> > ParseKeystroke(std::string) noexcept {
return std::vector<Key>();
}
#endif

absl::StatusOr<std::vector<Key> > ParseKeystroke(std::string) noexcept;

/// @brief Represents a keystroke. A collection of keys with associated
/// modifiers.
using Keystroke = std::vector<Key>;
using KeySequence = std::vector<Key>;

typedef struct Keystroke {
KeySequence sequence;
} Keystroke;

typedef struct KeysPress {
KeySequence sequence;
} KeysPress;

typedef struct KeysRelease {
KeySequence sequence;
} KeysRelease;

} // namespace actions::action

Expand Down
24 changes: 24 additions & 0 deletions actions/action/mouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ACTIONS_ACTION_MOUSE_CLICK_H
#define ACTIONS_ACTION_MOUSE_CLICK_H

namespace actions::action {

typedef struct CursorMove {
double x = 0;
double y = 0;
bool relative = true;
} CursorMove;

typedef enum MouseClick { LeftClick, RightClick, MiddleClick } MouseClick;

typedef enum MousePress { LeftPress, RightPress, MiddlePress } MousePress;

typedef enum MouseRelease {
LeftRelease,
RightRelease,
MiddleRelease
} MouseRelease;

} // namespace actions::action

#endif // ACTIONS_ACTION_MOUSE_CLICK_H
28 changes: 22 additions & 6 deletions actions/actions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "actions/internal/connection.h"
#include "actions/internal/util.h"

#if defined(__linux)
#include "actions/internal/linux/xcb_connection.h"
#if defined(__x11__)
#include "actions/internal/x11/xcb_connection.h"
#else
#include "actions/internal/stub/stub_connection.h"
#endif
Expand All @@ -18,8 +18,8 @@ absl::StatusOr<Actions> Actions::Create(
}

absl::StatusOr<Actions> Actions::Create() noexcept {
#if defined(__linux)
auto conn = internal::linux::XcbConnection::Create();
#if defined(__x11__)
auto conn = internal::x11::XcbConnection::Create();
#else
auto conn = internal::stub::StubConnection::Create();
#endif
Expand All @@ -45,10 +45,26 @@ Actions& Actions::operator=(Actions&& other) noexcept {
std::future<absl::Status> Actions::Perform(
const Action& action, const action::Target& target) noexcept {
if (absl::holds_alternative<action::Keystroke>(action)) {
return conn->SendKeystroke(absl::get<action::Keystroke>(action),
target);
return conn->Keystroke(absl::get<action::Keystroke>(action), target);
} else if (absl::holds_alternative<action::KeysPress>(action)) {
return conn->KeysPress(absl::get<action::KeysPress>(action), target);
} else if (absl::holds_alternative<action::KeysRelease>(action)) {
return conn->KeysRelease(absl::get<action::KeysRelease>(action),
target);
} else if (absl::holds_alternative<action::CursorMove>(action)) {
return conn->MoveCursor(absl::get<action::CursorMove>(action), target);
} else if (absl::holds_alternative<action::MousePress>(action)) {
return conn->MousePress(absl::get<action::MousePress>(action), target);
} else if (absl::holds_alternative<action::MouseRelease>(action)) {
return conn->MouseRelease(absl::get<action::MouseRelease>(action),
target);
} else if (absl::holds_alternative<action::MouseClick>(action)) {
return conn->MouseClick(absl::get<action::MouseClick>(action), target);
} else if (absl::holds_alternative<action::NoOp>(action)) {
return internal::util::Resolve(absl::OkStatus());
}

// Should be unreachable, but you can never be too careful
return internal::util::Resolve(
absl::UnimplementedError("Not Implemented."));
}
Expand Down
20 changes: 20 additions & 0 deletions actions/examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,23 @@ cc_binary(
"@com_google_absl//absl/status",
],
)

cc_binary(
name = "actions_mouse",
srcs = ["actions_mouse.cc"],
deps = [
"//actions",
"//actions:action",
"@com_google_absl//absl/status",
],
)

cc_binary(
name = "actions_hybrid",
srcs = ["actions_hybrid.cc"],
deps = [
"//actions",
"//actions:action",
"@com_google_absl//absl/status",
],
)
61 changes: 61 additions & 0 deletions actions/examples/actions_hybrid.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>

#include "absl/status/status.h"
#include "actions/action.h"
#include "actions/actions.h"

using namespace actions;

absl::Status run() {
auto keysequence = action::ParseKeystroke("shift");

if (!keysequence.ok()) return keysequence.status();

auto actions = Actions::Create();

if (!actions.ok()) return actions.status();

absl::Status status = actions
->Perform(action::KeysPress{keysequence.value()},
action::target::Focused())
.get();

if (!status.ok()) return status;

status =
actions
->Perform(action::MousePress::LeftPress, action::target::Focused())
.get();

if (!status.ok()) return status;

status = actions
->Perform(action::CursorMove{.x = 0.1, .y = 0},
action::target::Focused())
.get();

if (!status.ok()) return status;

status = actions
->Perform(action::KeysRelease{keysequence.value()},
action::target::Focused())
.get();

if (!status.ok()) return status;

return actions
->Perform(action::MouseRelease::LeftRelease, action::target::Focused())
.get();
}

int main(int argc, char** argv) {
std::cout << "Running Example" << std::endl;
absl::Status status = run();

if (!status.ok()) {
std::cout << status << std::endl;
return 1;
}

return 0;
}
9 changes: 6 additions & 3 deletions actions/examples/actions_keystroke.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ absl::Status run() {

if (!actions.ok()) return actions.status();

auto keystroke = action::ParseKeystroke(FLAGS_keystroke.CurrentValue());
auto keysequence = action::ParseKeystroke(FLAGS_keystroke.CurrentValue());

if (!keystroke.ok()) return keystroke.status();
if (!keysequence.ok()) return keysequence.status();

return actions->Perform(keystroke.value(), action::target::Focused()).get();
return actions
->Perform(action::Keystroke{keysequence.value()},
action::target::Focused())
.get();
}

int main(int argc, char** argv) {
Expand Down
36 changes: 36 additions & 0 deletions actions/examples/actions_mouse.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>

#include "absl/status/status.h"
#include "actions/action.h"
#include "actions/actions.h"

using namespace actions;

absl::Status run() {
auto actions = Actions::Create();

if (!actions.ok()) return actions.status();

absl::Status status = actions
->Perform(action::CursorMove{.x = 0.1, .y = 0.1},
action::target::Focused())
.get();

if (!status.ok()) return status;

return actions
->Perform(action::MouseClick::LeftClick, action::target::Focused())
.get();
}

int main(int argc, char** argv) {
std::cout << "Running Example" << std::endl;
absl::Status status = run();

if (!status.ok()) {
std::cout << status << std::endl;
return 1;
}

return 0;
}
Loading

0 comments on commit 2dd6fe3

Please sign in to comment.