Skip to content

Commit

Permalink
chore: fixes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazer committed Jul 4, 2024
1 parent 512af0e commit 1aca214
Show file tree
Hide file tree
Showing 25 changed files with 442 additions and 356 deletions.
6 changes: 3 additions & 3 deletions src/input/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @file src/input/common.cpp
* @brief Definitions for common input.
*/
#include "common.h"
#include "init.h"
#include "src/input/common.h"
#include "src/input/gamepad.h"
#include "src/input/init.h"

#include "gamepad.h"
#include "src/thread_pool.h"

#include <boost/endian/buffers.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/input/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {
#include "src/platform/common.h"
#include "src/thread_pool.h"

#include "gamepad.h"
#include "src/input/gamepad.h"

namespace input {

Expand Down
10 changes: 5 additions & 5 deletions src/input/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @file src/input/gamepad.cpp
* @brief Definitions for common gamepad input.
*/
#include "gamepad.h"
#include "common.h"
#include "init.h"
#include "platform_input.h"
#include "processor.h"
#include "src/input/gamepad.h"
#include "src/input/common.h"
#include "src/input/init.h"
#include "src/input/platform_input.h"
#include "src/input/processor.h"

#include "src/globals.h"
#include "src/thread_pool.h"
Expand Down
2 changes: 1 addition & 1 deletion src/input/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
#include "src/platform/common.h"
#include "src/thread_pool.h"

#include "init.h"
#include "src/input/init.h"

namespace input::gamepad {
static std::bitset<platf::MAX_GAMEPADS> gamepadMask = {};
Expand Down
297 changes: 149 additions & 148 deletions src/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* @file src/input/keyboard.cpp
* @brief Definitions for common keyboard input.
*/
#include "keyboard.h"
#include "init.h"

#include "platform_input.h"
#include "processor.h"
#include "src/input/keyboard.h"
#include "src/input/init.h"
#include "src/input/platform_input.h"
#include "src/input/processor.h"

namespace input::keyboard {

Expand All @@ -23,176 +22,178 @@ namespace input::keyboard {
static task_pool_util::TaskPool::task_id_t key_press_repeat_id {};
static std::unordered_map<key_press_id_t, bool> key_press {};

key_press_id_t
make_kpid(uint16_t vk, uint8_t flags) {
return (key_press_id_t) vk << 8 | flags;
}
namespace {
key_press_id_t
make_kpid(uint16_t vk, uint8_t flags) {
return (key_press_id_t) vk << 8 | flags;

Check warning on line 28 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L27-L28

Added lines #L27 - L28 were not covered by tests
}

uint16_t
vk_from_kpid(key_press_id_t kpid) {
return kpid >> 8;
}
uint16_t
vk_from_kpid(key_press_id_t kpid) {
return kpid >> 8;

Check warning on line 33 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L32-L33

Added lines #L32 - L33 were not covered by tests
}

uint8_t
flags_from_kpid(key_press_id_t kpid) {
return kpid & 0xFF;
}
uint8_t
flags_from_kpid(key_press_id_t kpid) {
return kpid & 0xFF;

Check warning on line 38 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L37-L38

Added lines #L37 - L38 were not covered by tests
}

void
print(PNV_KEYBOARD_PACKET packet) {
BOOST_LOG(debug)
<< "--begin keyboard packet--"sv << std::endl
<< "keyAction ["sv << util::hex(packet->header.magic).to_string_view() << ']' << std::endl
<< "keyCode ["sv << util::hex(packet->keyCode).to_string_view() << ']' << std::endl
<< "modifiers ["sv << util::hex(packet->modifiers).to_string_view() << ']' << std::endl
<< "flags ["sv << util::hex(packet->flags).to_string_view() << ']' << std::endl
<< "--end keyboard packet--"sv;
}
/**
* @brief Apply shortcut based on VKEY.
* @param keyCode The VKEY code.
* @return 0 if no shortcut applied, > 0 if shortcut applied.
*/
int
apply_shortcut(short keyCode) {
constexpr auto VK_F1 = 0x70;
constexpr auto VK_F13 = 0x7C;

Check warning on line 49 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L47-L49

Added lines #L47 - L49 were not covered by tests

void
print(PNV_UNICODE_PACKET packet) {
std::string text(packet->text, util::endian::big(packet->header.size) - sizeof(packet->header.magic));
BOOST_LOG(debug)
<< "--begin unicode packet--"sv << std::endl
<< "text ["sv << text << ']' << std::endl
<< "--end unicode packet--"sv;
}
BOOST_LOG(debug) << "Apply Shortcut: 0x"sv << util::hex((std::uint8_t) keyCode).to_string_view();

/**
* @brief Apply shortcut based on VKEY.
* @param keyCode The VKEY code.
* @return 0 if no shortcut applied, > 0 if shortcut applied.
*/
inline int
apply_shortcut(short keyCode) {
constexpr auto VK_F1 = 0x70;
constexpr auto VK_F13 = 0x7C;

BOOST_LOG(debug) << "Apply Shortcut: 0x"sv << util::hex((std::uint8_t) keyCode).to_string_view();

if (keyCode >= VK_F1 && keyCode <= VK_F13) {
mail::man->event<int>(mail::switch_display)->raise(keyCode - VK_F1);
return 1;
}

switch (keyCode) {
case 0x4E /* VKEY_N */:
display_cursor = !display_cursor;
if (keyCode >= VK_F1 && keyCode <= VK_F13) {
mail::man->event<int>(mail::switch_display)->raise(keyCode - VK_F1);
return 1;

Check warning on line 55 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L55

Added line #L55 was not covered by tests
}

switch (keyCode) {
case 0x4E /* VKEY_N */:
display_cursor = !display_cursor;
return 1;

Check warning on line 61 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L59-L61

Added lines #L59 - L61 were not covered by tests
}

return 0;

Check warning on line 64 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L64

Added line #L64 was not covered by tests
}

return 0;
}
short
map_keycode(short keycode) {

Check warning on line 68 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L68

Added line #L68 was not covered by tests
auto it = config::input.keybindings.find(keycode);
if (it != std::end(config::input.keybindings)) {
return it->second;

Check warning on line 71 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L71

Added line #L71 was not covered by tests
}

short
map_keycode(short keycode) {
auto it = config::input.keybindings.find(keycode);
if (it != std::end(config::input.keybindings)) {
return it->second;
return keycode;

Check warning on line 74 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L74

Added line #L74 was not covered by tests
}

return keycode;
}
/**
* @brief Update flags for keyboard shortcut combos
*/
void
update_shortcutFlags(int *flags, short keyCode, bool release) {

Check warning on line 81 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L81

Added line #L81 was not covered by tests
switch (keyCode) {
case VKEY_SHIFT:
case VKEY_LSHIFT:
case VKEY_RSHIFT:

Check warning on line 85 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L83-L85

Added lines #L83 - L85 were not covered by tests
if (release) {
*flags &= ~input_t::SHIFT;

Check warning on line 87 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L87

Added line #L87 was not covered by tests
}
else {
*flags |= input_t::SHIFT;

Check warning on line 90 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L90

Added line #L90 was not covered by tests
}
break;
case VKEY_CONTROL:
case VKEY_LCONTROL:
case VKEY_RCONTROL:

Check warning on line 95 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L92-L95

Added lines #L92 - L95 were not covered by tests
if (release) {
*flags &= ~input_t::CTRL;

Check warning on line 97 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L97

Added line #L97 was not covered by tests
}
else {
*flags |= input_t::CTRL;

Check warning on line 100 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L100

Added line #L100 was not covered by tests
}
break;
case VKEY_MENU:
case VKEY_LMENU:
case VKEY_RMENU:

Check warning on line 105 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L102-L105

Added lines #L102 - L105 were not covered by tests
if (release) {
*flags &= ~input_t::ALT;

Check warning on line 107 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L107

Added line #L107 was not covered by tests
}
else {
*flags |= input_t::ALT;

Check warning on line 110 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L110

Added line #L110 was not covered by tests
}
break;

Check warning on line 112 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L112

Added line #L112 was not covered by tests
}
}

bool
is_modifier(uint16_t keyCode) {

Check warning on line 117 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L117

Added line #L117 was not covered by tests
switch (keyCode) {
case VKEY_SHIFT:
case VKEY_LSHIFT:
case VKEY_RSHIFT:
case VKEY_CONTROL:
case VKEY_LCONTROL:
case VKEY_RCONTROL:
case VKEY_MENU:
case VKEY_LMENU:
case VKEY_RMENU:
return true;
default:
return false;

Check warning on line 130 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L128-L130

Added lines #L128 - L130 were not covered by tests
}
}

/**
* @brief Update flags for keyboard shortcut combos
*/
inline void
update_shortcutFlags(int *flags, short keyCode, bool release) {
switch (keyCode) {
case VKEY_SHIFT:
case VKEY_LSHIFT:
case VKEY_RSHIFT:
if (release) {
*flags &= ~input_t::SHIFT;
void
send_key_and_modifiers(uint16_t key_code, bool release, uint8_t flags, uint8_t synthetic_modifiers) {

Check warning on line 135 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L135

Added line #L135 was not covered by tests
if (!release) {
// Press any synthetic modifiers required for this key
if (synthetic_modifiers & MODIFIER_SHIFT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_SHIFT, false, flags);

Check warning on line 139 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L139

Added line #L139 was not covered by tests
}
else {
*flags |= input_t::SHIFT;
if (synthetic_modifiers & MODIFIER_CTRL) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_CONTROL, false, flags);

Check warning on line 142 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L142

Added line #L142 was not covered by tests
}
break;
case VKEY_CONTROL:
case VKEY_LCONTROL:
case VKEY_RCONTROL:
if (release) {
*flags &= ~input_t::CTRL;
if (synthetic_modifiers & MODIFIER_ALT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_MENU, false, flags);

Check warning on line 145 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L145

Added line #L145 was not covered by tests
}
else {
*flags |= input_t::CTRL;
}

platf::keyboard_update(PlatformInput::getInstance(), map_keycode(key_code), release, flags);

Check warning on line 149 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L149

Added line #L149 was not covered by tests

if (!release) {
// Raise any synthetic modifier keys we pressed
if (synthetic_modifiers & MODIFIER_SHIFT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_SHIFT, true, flags);

Check warning on line 154 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L154

Added line #L154 was not covered by tests
}
break;
case VKEY_MENU:
case VKEY_LMENU:
case VKEY_RMENU:
if (release) {
*flags &= ~input_t::ALT;
if (synthetic_modifiers & MODIFIER_CTRL) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_CONTROL, true, flags);

Check warning on line 157 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L157

Added line #L157 was not covered by tests
}
else {
*flags |= input_t::ALT;
if (synthetic_modifiers & MODIFIER_ALT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_MENU, true, flags);

Check warning on line 160 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L160

Added line #L160 was not covered by tests
}
break;
}
}

bool
is_modifier(uint16_t keyCode) {
switch (keyCode) {
case VKEY_SHIFT:
case VKEY_LSHIFT:
case VKEY_RSHIFT:
case VKEY_CONTROL:
case VKEY_LCONTROL:
case VKEY_RCONTROL:
case VKEY_MENU:
case VKEY_LMENU:
case VKEY_RMENU:
return true;
default:
return false;
}
}
}

void
send_key_and_modifiers(uint16_t key_code, bool release, uint8_t flags, uint8_t synthetic_modifiers) {
if (!release) {
// Press any synthetic modifiers required for this key
if (synthetic_modifiers & MODIFIER_SHIFT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_SHIFT, false, flags);
}
if (synthetic_modifiers & MODIFIER_CTRL) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_CONTROL, false, flags);
}
if (synthetic_modifiers & MODIFIER_ALT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_MENU, false, flags);
void
repeat_key(uint16_t key_code, uint8_t flags, uint8_t synthetic_modifiers) {

Check warning on line 166 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L166

Added line #L166 was not covered by tests
// If key no longer pressed, stop repeating
if (!key_press[make_kpid(key_code, flags)]) {
key_press_repeat_id = nullptr;
return;

Check warning on line 170 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L169-L170

Added lines #L169 - L170 were not covered by tests
}
}

platf::keyboard_update(PlatformInput::getInstance(), map_keycode(key_code), release, flags);
send_key_and_modifiers(key_code, false, flags, synthetic_modifiers);

Check warning on line 173 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L173

Added line #L173 was not covered by tests

if (!release) {
// Raise any synthetic modifier keys we pressed
if (synthetic_modifiers & MODIFIER_SHIFT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_SHIFT, true, flags);
}
if (synthetic_modifiers & MODIFIER_CTRL) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_CONTROL, true, flags);
}
if (synthetic_modifiers & MODIFIER_ALT) {
platf::keyboard_update(PlatformInput::getInstance(), VKEY_MENU, true, flags);
}
key_press_repeat_id = task_pool.pushDelayed(repeat_key, config::input.key_repeat_period, key_code, flags, synthetic_modifiers).task_id;
}
}
} // namespace

void
repeat_key(uint16_t key_code, uint8_t flags, uint8_t synthetic_modifiers) {
// If key no longer pressed, stop repeating
if (!key_press[make_kpid(key_code, flags)]) {
key_press_repeat_id = nullptr;
return;
}

send_key_and_modifiers(key_code, false, flags, synthetic_modifiers);
print(PNV_KEYBOARD_PACKET packet) {

Check warning on line 180 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L180

Added line #L180 was not covered by tests
BOOST_LOG(debug)
<< "--begin keyboard packet--"sv << std::endl
<< "keyAction ["sv << util::hex(packet->header.magic).to_string_view() << ']' << std::endl
<< "keyCode ["sv << util::hex(packet->keyCode).to_string_view() << ']' << std::endl
<< "modifiers ["sv << util::hex(packet->modifiers).to_string_view() << ']' << std::endl
<< "flags ["sv << util::hex(packet->flags).to_string_view() << ']' << std::endl
<< "--end keyboard packet--"sv;
}

key_press_repeat_id = task_pool.pushDelayed(repeat_key, config::input.key_repeat_period, key_code, flags, synthetic_modifiers).task_id;
void
print(PNV_UNICODE_PACKET packet) {

Check warning on line 191 in src/input/keyboard.cpp

View check run for this annotation

Codecov / codecov/patch

src/input/keyboard.cpp#L191

Added line #L191 was not covered by tests
std::string text(packet->text, util::endian::big(packet->header.size) - sizeof(packet->header.magic));
BOOST_LOG(debug)
<< "--begin unicode packet--"sv << std::endl
<< "text ["sv << text << ']' << std::endl
<< "--end unicode packet--"sv;
}

void
Expand Down
Loading

0 comments on commit 1aca214

Please sign in to comment.