Skip to content

Commit

Permalink
Some code optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Nov 14, 2023
1 parent afc4de1 commit 52d2199
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 210 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/solaris.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
build:
runs-on: macos-12
runs-on: ubuntu-22.04

steps:
- name: Repository checkout
Expand All @@ -27,7 +27,7 @@ jobs:
pkg list -af gcc-7
pkg install -v --accept developer/gcc-7@7.3.0-11.4.0.0.1.14.0
pkgutil -y -i automake autoconf autoconf_archive libtool pkgconfig libcppunit1_12_1 libcppunit_dev gsed ggrep
mem: 4096
mem: 6144
run: |
uname -a
whoami
Expand Down
151 changes: 92 additions & 59 deletions final/input/fmouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,60 +413,26 @@ void FMouseGPM::processEvent (const TimeValue&)

if ( Gpm_GetEvent(&gpm_ev) == 1 )
{
Gpm_FitEvent (&gpm_ev);
GPM_DRAWPOINTER(&gpm_ev);
handleMouseEvent();

if ( ! hasSignificantEvents() )
{
has_gpm_mouse_data = false;
clearEvent();
resetMouseState();
return;
}

if ( gpm_ev.type & GPM_DRAG && gpm_ev.wdx == 0 && gpm_ev.wdy == 0 )
getButtonState().mouse_moved = true;

if ( gpm_ev.wdy > 0 )
getButtonState().wheel_up = true;
else if ( gpm_ev.wdy < 0 )
getButtonState().wheel_down = true;

if ( gpm_ev.wdx > 0 )
getButtonState().wheel_right = true;
else if ( gpm_ev.wdx < 0 )
getButtonState().wheel_left = true;

switch ( gpm_ev.type & 0x0f )
{
case GPM_DOWN:
case GPM_DRAG:
interpretKeyDown();
break;

case GPM_UP:
interpretKeyUp();
break;

default:
break;
}

setPos (FPoint{ std::max(gpm_ev.x, sInt16(1))
, std::max(gpm_ev.y, sInt16(1)) });

if ( gpmEvent(false) == gpmEventType::Mouse )
setPending(true);
else
setPending(false);

handleMouseMovement();
handleMouseWheel();
interpretMouseEvent();
updateMousePosition();
setPending ( gpmEvent(false) == gpmEventType::Mouse );
has_gpm_mouse_data = false;
setEvent();
return;
}

gpm_fd = -1;
has_gpm_mouse_data = false;
clearEvent();
resetMouseState();
}

//----------------------------------------------------------------------
Expand Down Expand Up @@ -576,6 +542,68 @@ void FMouseGPM::drawPointer() const
GPM_DRAWPOINTER(&gpm_ev);
}

// private methods of FMouseGPM
//----------------------------------------------------------------------
inline void FMouseGPM::handleMouseEvent()
{
Gpm_FitEvent(&gpm_ev);
GPM_DRAWPOINTER(&gpm_ev);
}

//----------------------------------------------------------------------
inline void FMouseGPM::resetMouseState()
{
has_gpm_mouse_data = false;
clearEvent();
}

//----------------------------------------------------------------------
inline void FMouseGPM::handleMouseMovement()
{
if ( gpm_ev.type & GPM_DRAG && gpm_ev.wdx == 0 && gpm_ev.wdy == 0 )
getButtonState().mouse_moved = true;
}

//----------------------------------------------------------------------
inline void FMouseGPM::handleMouseWheel()
{
if ( gpm_ev.wdy > 0 )
getButtonState().wheel_up = true;
else if ( gpm_ev.wdy < 0 )
getButtonState().wheel_down = true;

if ( gpm_ev.wdx > 0 )
getButtonState().wheel_right = true;
else if ( gpm_ev.wdx < 0 )
getButtonState().wheel_left = true;
}

//----------------------------------------------------------------------
inline void FMouseGPM::interpretMouseEvent()
{
switch ( gpm_ev.type & 0x0f )
{
case GPM_DOWN:
case GPM_DRAG:
interpretKeyDown();
break;

case GPM_UP:
interpretKeyUp();
break;

default:
break;
}
}

//----------------------------------------------------------------------
inline void FMouseGPM::updateMousePosition()
{
setPos (FPoint{ std::max(gpm_ev.x, sInt16(1))
, std::max(gpm_ev.y, sInt16(1)) });
}

//----------------------------------------------------------------------
auto FMouseGPM::gpmEvent (bool clear) const -> gpmEventType
{
Expand Down Expand Up @@ -704,7 +732,7 @@ void FMouseX11::setMoveState (const FPoint& mouse_position, int btn) noexcept
}

//----------------------------------------------------------------------
inline bool FMouseX11::isMouseClickButton (const int btn) const noexcept
inline auto FMouseX11::isMouseClickButton (const int btn) const noexcept -> bool
{
return btn == button1_pressed
|| btn == button2_pressed
Expand All @@ -715,7 +743,7 @@ inline bool FMouseX11::isMouseClickButton (const int btn) const noexcept
}

//----------------------------------------------------------------------
inline bool FMouseX11::isMouseWheelButton (const int btn) const noexcept
inline auto FMouseX11::isMouseWheelButton (const int btn) const noexcept -> bool
{
return btn == button_up
|| btn == button_down
Expand Down Expand Up @@ -910,7 +938,7 @@ void FMouseSGR::processEvent (const TimeValue& time)
const auto& mouse_position = getPos();
Tokens token{};

if ( parseSGRMouseString(token) )
if ( parseSGRMouseString(token) == ParseError::Yes )
{
clearEvent();
sgr_mouse[0] = '\0'; // Delete already interpreted data
Expand Down Expand Up @@ -968,7 +996,7 @@ void FMouseSGR::setMoveState (const FPoint& mouse_position, int btn) noexcept
}

//----------------------------------------------------------------------
inline bool FMouseSGR::isMouseClickButton (const int btn) const noexcept
inline auto FMouseSGR::isMouseClickButton (const int btn) const noexcept -> bool
{
return btn == button1
|| btn == button2
Expand All @@ -979,7 +1007,7 @@ inline bool FMouseSGR::isMouseClickButton (const int btn) const noexcept
}

//----------------------------------------------------------------------
inline bool FMouseSGR::isMouseWheelButton (const int btn) const noexcept
inline auto FMouseSGR::isMouseWheelButton (const int btn) const noexcept -> bool
{
return btn == button_up
|| btn == button_down
Expand All @@ -988,27 +1016,30 @@ inline bool FMouseSGR::isMouseWheelButton (const int btn) const noexcept
}

//----------------------------------------------------------------------
inline bool FMouseSGR::parseSGRMouseString (FMouseSGR::Tokens& token) const noexcept
inline auto FMouseSGR::parseSGRMouseString (FMouseSGR::Tokens& token) const noexcept -> ParseError
{
// Parse the SGR mouse string
token.p = sgr_mouse.data();

// Parse button
if ( ! parseNumberIf (token.p, token.btn, [] (char ch) { return ch != ';'; }) )
return true;
return ParseError::Yes;

if ( *token.p )
token.p++; // ship one character after the number

// Parse x-value
if ( ! parseNumberIf (token.p, token.x, [] (char ch) { return ch != ';'; }) )
return true;
return ParseError::Yes;

if ( *token.p )
token.p++; // ship one character after the number

// Parse y-value
return ! parseNumberIf (token.p, token.y, [] (char ch) { return ch != 'M' && ch != 'm'; });
if ( ! parseNumberIf (token.p, token.y, [] (char ch) { return ch != 'M' && ch != 'm'; }) )
return ParseError::Yes;

return ParseError::No;
}

//----------------------------------------------------------------------
Expand Down Expand Up @@ -1199,7 +1230,7 @@ void FMouseUrxvt::processEvent (const TimeValue& time)
const auto& mouse_position = getPos();
Tokens token{};

if ( parseUrxvtMouseString(token) )
if ( parseUrxvtMouseString(token) == ParseError::Yes )
{
clearEvent();
urxvt_mouse[0] = '\0'; // Delete already interpreted data
Expand Down Expand Up @@ -1253,7 +1284,7 @@ void FMouseUrxvt::setMoveState (const FPoint& mouse_position, int btn) noexcept
}

//----------------------------------------------------------------------
inline bool FMouseUrxvt::isMouseClickButton (const int btn) const noexcept
inline auto FMouseUrxvt::isMouseClickButton (const int btn) const noexcept -> bool
{
return btn == button1_pressed
|| btn == button2_pressed
Expand All @@ -1264,7 +1295,7 @@ inline bool FMouseUrxvt::isMouseClickButton (const int btn) const noexcept
}

//----------------------------------------------------------------------
inline bool FMouseUrxvt::isMouseWheelButton (const int btn) const noexcept
inline auto FMouseUrxvt::isMouseWheelButton (const int btn) const noexcept -> bool
{
return btn == button_up
|| btn == button_down
Expand All @@ -1273,15 +1304,14 @@ inline bool FMouseUrxvt::isMouseWheelButton (const int btn) const noexcept
}

//----------------------------------------------------------------------
inline bool FMouseUrxvt::parseUrxvtMouseString (FMouseUrxvt::Tokens& token) const noexcept
inline auto FMouseUrxvt::parseUrxvtMouseString (FMouseUrxvt::Tokens& token) const noexcept -> ParseError
{

// Parse the Urxvt mouse string
token.p = urxvt_mouse.data();

// Parse button
if ( ! parseNumberIf (token.p, token.btn, [] (char ch) { return ch != ';'; }) )
return true;
return ParseError::Yes;

if ( *++token.p == '-' )
{
Expand All @@ -1291,7 +1321,7 @@ inline bool FMouseUrxvt::parseUrxvtMouseString (FMouseUrxvt::Tokens& token) cons

// Parse x-value
if ( ! parseNumberIf (token.p, token.x, [] (char ch) { return ch != ';'; }) )
return true;
return ParseError::Yes;

if ( *++token.p == '-' )
{
Expand All @@ -1300,7 +1330,10 @@ inline bool FMouseUrxvt::parseUrxvtMouseString (FMouseUrxvt::Tokens& token) cons
}

// Parse y-value
return ! parseNumberIf (token.p, token.y, [] (char ch) { return ch != 'M'; });
if ( ! parseNumberIf (token.p, token.y, [] (char ch) { return ch != 'M'; }) )
return ParseError::Yes;

return ParseError::No;
}

//----------------------------------------------------------------------
Expand Down
30 changes: 20 additions & 10 deletions final/input/fmouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ class FMouseGPM final : public FMouse
};

// Method
void handleMouseEvent();
void resetMouseState();
void handleMouseMovement();
void handleMouseWheel();
void interpretMouseEvent();
void updateMousePosition();
auto gpmEvent (bool = true) const -> gpmEventType;

// Data member
Expand Down Expand Up @@ -368,8 +374,8 @@ class FMouseX11 final : public FMouse
// Methods
void setKeyState (int) noexcept;
void setMoveState (const FPoint&, int) noexcept;
bool isMouseClickButton (const int) const noexcept;
bool isMouseWheelButton (const int) const noexcept;
auto isMouseClickButton (const int) const noexcept -> bool;
auto isMouseWheelButton (const int) const noexcept -> bool;
auto noChanges (const FPoint&, uChar) const noexcept -> bool;
void handleMouseClickButton (int, const TimeValue&) noexcept;
void handleMouseWheelButton (int) noexcept;
Expand Down Expand Up @@ -412,7 +418,9 @@ class FMouseSGR final : public FMouse
const char* p{nullptr}; // Current read position
};

// Enumeration
// Enumerations
enum class ParseError { No, Yes };

enum x11_ext_btn_states
{
key_shift = 0x04,
Expand Down Expand Up @@ -440,9 +448,9 @@ class FMouseSGR final : public FMouse
// Methods
void setKeyState (int) noexcept;
void setMoveState (const FPoint&, int) noexcept;
bool isMouseClickButton (const int) const noexcept;
bool isMouseWheelButton (const int) const noexcept;
bool parseSGRMouseString (Tokens&) const noexcept;
auto isMouseClickButton (const int) const noexcept -> bool;
auto isMouseWheelButton (const int) const noexcept -> bool;
auto parseSGRMouseString (Tokens&) const noexcept -> ParseError;
auto noChanges (const FPoint&, uChar) const noexcept -> bool;
void handleMouseClickButton (int, const TimeValue&) noexcept;
void handleMouseWheelButton (int) noexcept;
Expand Down Expand Up @@ -487,7 +495,9 @@ class FMouseUrxvt final : public FMouse
const char* p{nullptr}; // Current read position
};

// Enumeration
// Enumerations
enum class ParseError { No, Yes };

enum urxvt_btn_states
{
key_shift = 0x04,
Expand All @@ -514,9 +524,9 @@ class FMouseUrxvt final : public FMouse
// Methods
void setKeyState (int) noexcept;
void setMoveState (const FPoint&, int) noexcept;
bool isMouseClickButton (const int) const noexcept;
bool isMouseWheelButton (const int) const noexcept;
bool parseUrxvtMouseString (Tokens&) const noexcept;
auto isMouseClickButton (const int) const noexcept -> bool;
auto isMouseWheelButton (const int) const noexcept -> bool;
auto parseUrxvtMouseString (Tokens&) const noexcept -> ParseError;
void adjustAndSetPosition (Tokens&);
auto noChanges (const FPoint&, uChar) const noexcept -> bool;
void handleMouseClickButton (int, const TimeValue&) noexcept;
Expand Down
Loading

0 comments on commit 52d2199

Please sign in to comment.