Skip to content
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
28 changes: 14 additions & 14 deletions src/core/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ namespace lvh {

} // namespace

Gamepad::Gamepad(std::shared_ptr<detail::GamepadDevice> device):
Gamepad::Gamepad(detail::RuntimeConstructionToken, std::shared_ptr<detail::GamepadDevice> device):
device_ {std::move(device)} {}

Gamepad::Gamepad(Gamepad &&) noexcept = default;
Expand Down Expand Up @@ -411,7 +411,7 @@ namespace lvh {
});
}

Keyboard::Keyboard(std::shared_ptr<detail::KeyboardDevice> device):
Keyboard::Keyboard(detail::RuntimeConstructionToken, std::shared_ptr<detail::KeyboardDevice> device):
device_ {std::move(device)} {}

Keyboard::Keyboard(Keyboard &&) noexcept = default;
Expand Down Expand Up @@ -517,7 +517,7 @@ namespace lvh {
});
}

Mouse::Mouse(std::shared_ptr<detail::MouseDevice> device):
Mouse::Mouse(detail::RuntimeConstructionToken, std::shared_ptr<detail::MouseDevice> device):
device_ {std::move(device)} {}

Mouse::Mouse(Mouse &&) noexcept = default;
Expand Down Expand Up @@ -627,7 +627,7 @@ namespace lvh {
});
}

Touchscreen::Touchscreen(std::shared_ptr<detail::TouchscreenDevice> device):
Touchscreen::Touchscreen(detail::RuntimeConstructionToken, std::shared_ptr<detail::TouchscreenDevice> device):
device_ {std::move(device)} {}

Touchscreen::Touchscreen(Touchscreen &&) noexcept = default;
Expand Down Expand Up @@ -729,7 +729,7 @@ namespace lvh {
});
}

Trackpad::Trackpad(std::shared_ptr<detail::TrackpadDevice> device):
Trackpad::Trackpad(detail::RuntimeConstructionToken, std::shared_ptr<detail::TrackpadDevice> device):
device_ {std::move(device)} {}

Trackpad::Trackpad(Trackpad &&) noexcept = default;
Expand Down Expand Up @@ -848,7 +848,7 @@ namespace lvh {
});
}

PenTablet::PenTablet(std::shared_ptr<detail::PenTabletDevice> device):
PenTablet::PenTablet(detail::RuntimeConstructionToken, std::shared_ptr<detail::PenTabletDevice> device):
device_ {std::move(device)} {}

PenTablet::PenTablet(PenTablet &&) noexcept = default;
Expand Down Expand Up @@ -941,7 +941,7 @@ namespace lvh {
});
}

Runtime::Runtime(RuntimeOptions options):
Runtime::Runtime(detail::RuntimeConstructionToken, RuntimeOptions options):
state_ {std::make_shared<detail::RuntimeState>(options)} {}

Runtime::Runtime(Runtime &&) noexcept = default;
Expand All @@ -954,7 +954,7 @@ namespace lvh {
}

std::unique_ptr<Runtime> Runtime::create(RuntimeOptions options) {
return std::unique_ptr<Runtime> {new Runtime {options}};
return std::make_unique<Runtime>(detail::RuntimeConstructionToken {}, options);
}

const BackendCapabilities &Runtime::capabilities() const {
Expand Down Expand Up @@ -993,7 +993,7 @@ namespace lvh {
state_->gamepads.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<Gamepad> {new Gamepad {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<Gamepad>(detail::RuntimeConstructionToken {}, std::move(device))};
}

KeyboardCreationResult Runtime::create_keyboard() {
Expand Down Expand Up @@ -1024,7 +1024,7 @@ namespace lvh {
state_->keyboards.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<Keyboard> {new Keyboard {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<Keyboard>(detail::RuntimeConstructionToken {}, std::move(device))};
}

MouseCreationResult Runtime::create_mouse() {
Expand Down Expand Up @@ -1055,7 +1055,7 @@ namespace lvh {
state_->mice.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<Mouse> {new Mouse {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<Mouse>(detail::RuntimeConstructionToken {}, std::move(device))};
}

TouchscreenCreationResult Runtime::create_touchscreen() {
Expand Down Expand Up @@ -1086,7 +1086,7 @@ namespace lvh {
state_->touchscreens.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<Touchscreen> {new Touchscreen {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<Touchscreen>(detail::RuntimeConstructionToken {}, std::move(device))};
}

TrackpadCreationResult Runtime::create_trackpad() {
Expand Down Expand Up @@ -1117,7 +1117,7 @@ namespace lvh {
state_->trackpads.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<Trackpad> {new Trackpad {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<Trackpad>(detail::RuntimeConstructionToken {}, std::move(device))};
}

PenTabletCreationResult Runtime::create_pen_tablet() {
Expand Down Expand Up @@ -1148,7 +1148,7 @@ namespace lvh {
state_->pen_tablets.emplace_back(device);
}

return {OperationStatus::success(), std::unique_ptr<PenTablet> {new PenTablet {std::move(device)}}};
return {OperationStatus::success(), std::make_unique<PenTablet>(detail::RuntimeConstructionToken {}, std::move(device))};
}

std::size_t Runtime::active_device_count() const {
Expand Down
94 changes: 68 additions & 26 deletions src/include/libvirtualhid/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@

namespace lvh {

class Runtime;

namespace detail {
/**
* @brief Token used by Runtime to construct runtime-owned handles.
*/
struct RuntimeConstructionToken {
private:
friend class ::lvh::Runtime;

RuntimeConstructionToken() = default;
};

struct GamepadDevice;
struct KeyboardDevice;
struct MouseDevice;
Expand Down Expand Up @@ -102,6 +114,14 @@ namespace lvh {
*/
Gamepad &operator=(Gamepad &&other) noexcept;

/**
* @brief Construct a gamepad handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared gamepad state.
*/
Gamepad(detail::RuntimeConstructionToken token, std::shared_ptr<detail::GamepadDevice> device);

/**
* @brief Destroy the gamepad handle.
*/
Expand Down Expand Up @@ -184,10 +204,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit Gamepad(std::shared_ptr<detail::GamepadDevice> device);

std::shared_ptr<detail::GamepadDevice> device_;
};

Expand Down Expand Up @@ -223,6 +239,14 @@ namespace lvh {
*/
Keyboard &operator=(Keyboard &&other) noexcept;

/**
* @brief Construct a keyboard handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared keyboard state.
*/
Keyboard(detail::RuntimeConstructionToken token, std::shared_ptr<detail::KeyboardDevice> device);

/**
* @brief Destroy the keyboard handle.
*/
Expand Down Expand Up @@ -300,10 +324,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit Keyboard(std::shared_ptr<detail::KeyboardDevice> device);

std::shared_ptr<detail::KeyboardDevice> device_;
};

Expand Down Expand Up @@ -339,6 +359,14 @@ namespace lvh {
*/
Mouse &operator=(Mouse &&other) noexcept;

/**
* @brief Construct a mouse handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared mouse state.
*/
Mouse(detail::RuntimeConstructionToken token, std::shared_ptr<detail::MouseDevice> device);

/**
* @brief Destroy the mouse handle.
*/
Expand Down Expand Up @@ -437,10 +465,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit Mouse(std::shared_ptr<detail::MouseDevice> device);

std::shared_ptr<detail::MouseDevice> device_;
};

Expand Down Expand Up @@ -476,6 +500,14 @@ namespace lvh {
*/
Touchscreen &operator=(Touchscreen &&other) noexcept;

/**
* @brief Construct a touchscreen handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared touchscreen state.
*/
Touchscreen(detail::RuntimeConstructionToken token, std::shared_ptr<detail::TouchscreenDevice> device);

/**
* @brief Destroy the touchscreen handle.
*/
Expand Down Expand Up @@ -537,10 +569,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit Touchscreen(std::shared_ptr<detail::TouchscreenDevice> device);

std::shared_ptr<detail::TouchscreenDevice> device_;
};

Expand Down Expand Up @@ -576,6 +604,14 @@ namespace lvh {
*/
Trackpad &operator=(Trackpad &&other) noexcept;

/**
* @brief Construct a trackpad handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared trackpad state.
*/
Trackpad(detail::RuntimeConstructionToken token, std::shared_ptr<detail::TrackpadDevice> device);

/**
* @brief Destroy the trackpad handle.
*/
Expand Down Expand Up @@ -645,10 +681,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit Trackpad(std::shared_ptr<detail::TrackpadDevice> device);

std::shared_ptr<detail::TrackpadDevice> device_;
};

Expand Down Expand Up @@ -684,6 +716,14 @@ namespace lvh {
*/
PenTablet &operator=(PenTablet &&other) noexcept;

/**
* @brief Construct a pen tablet handle for Runtime-owned state.
*
* @param token Runtime construction token.
* @param device Shared pen tablet state.
*/
PenTablet(detail::RuntimeConstructionToken token, std::shared_ptr<detail::PenTabletDevice> device);

/**
* @brief Destroy the pen tablet handle.
*/
Expand Down Expand Up @@ -746,10 +786,6 @@ namespace lvh {
std::size_t submit_count() const;

private:
friend class Runtime;

explicit PenTablet(std::shared_ptr<detail::PenTabletDevice> device);

std::shared_ptr<detail::PenTabletDevice> device_;
};

Expand Down Expand Up @@ -929,6 +965,14 @@ namespace lvh {
*/
Runtime &operator=(Runtime &&other) noexcept;

/**
* @brief Construct a runtime for Runtime::create.
*
* @param token Runtime construction token.
* @param options Runtime configuration.
*/
Runtime(detail::RuntimeConstructionToken token, RuntimeOptions options);

/**
* @brief Destroy the runtime and close any remaining devices.
*/
Expand Down Expand Up @@ -1060,8 +1104,6 @@ namespace lvh {
void close_all();

private:
explicit Runtime(RuntimeOptions options);

std::shared_ptr<detail::RuntimeState> state_;
};

Expand Down