2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "target_state.h"
|
|
|
|
#include "common/start_visible.h"
|
|
|
|
#include "keyboard_state.h"
|
2020-04-22 01:25:14 +08:00
|
|
|
#include "common/shared_constants.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
TargetState::TargetState(int ms_delay) :
|
2020-07-01 17:37:50 +08:00
|
|
|
// TODO: All this processing should be done w/o a separate thread etc. in pre_wnd_proc of winkey_popup to avoid
|
|
|
|
// multithreading. Use SetTimer for delayed events
|
|
|
|
delay(std::chrono::milliseconds(ms_delay)),
|
|
|
|
thread(&TargetState::thread_proc, this)
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-07-01 17:37:50 +08:00
|
|
|
constexpr unsigned VK_S = 0x53;
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
bool TargetState::signal_event(unsigned vk_code, bool key_down)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
2020-07-01 17:37:50 +08:00
|
|
|
// Ignore repeated key presses
|
2019-12-06 19:07:54 +08:00
|
|
|
if (!events.empty() && events.back().key_down == key_down && events.back().vk_code == vk_code)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
// Hide the overlay when WinKey + Shift + S is pressed
|
|
|
|
if (key_down && state == Shown && vk_code == VK_S && (GetKeyState(VK_LSHIFT) || GetKeyState(VK_RSHIFT)))
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
|
|
|
// We cannot use normal hide() here, there is stuff that needs deinitialization.
|
|
|
|
// It can be safely done when the user releases the WinKey.
|
|
|
|
instance->quick_hide();
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
const bool win_key_released = !key_down && (vk_code == VK_LWIN || vk_code == VK_RWIN);
|
|
|
|
constexpr auto overlay_fade_in_animation_time = std::chrono::milliseconds(300);
|
|
|
|
const auto overlay_active = state == Shown && (std::chrono::system_clock::now() - signal_timestamp > overlay_fade_in_animation_time);
|
|
|
|
const bool suppress_win_release = win_key_released && (state == ForceShown || overlay_active) && !nonwin_key_was_pressed_during_shown;
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
events.push_back({ key_down, vk_code });
|
|
|
|
lock.unlock();
|
|
|
|
cv.notify_one();
|
2020-07-01 17:37:50 +08:00
|
|
|
if (suppress_win_release)
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
|
|
|
// Send a fake key-stroke to prevent the start menu from appearing.
|
|
|
|
// We use 0xCF VK code, which is reserved. It still prevents the
|
|
|
|
// start menu from appearing, but should not interfere with any
|
|
|
|
// keyboard shortcuts.
|
|
|
|
INPUT input[3] = { {}, {}, {} };
|
|
|
|
input[0].type = INPUT_KEYBOARD;
|
|
|
|
input[0].ki.wVk = 0xCF;
|
2020-04-22 01:25:14 +08:00
|
|
|
input[0].ki.dwExtraInfo = CommonSharedConstants::KEYBOARDMANAGER_INJECTED_FLAG;
|
2019-12-06 19:07:54 +08:00
|
|
|
input[1].type = INPUT_KEYBOARD;
|
|
|
|
input[1].ki.wVk = 0xCF;
|
|
|
|
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
|
2020-04-22 01:25:14 +08:00
|
|
|
input[1].ki.dwExtraInfo = CommonSharedConstants::KEYBOARDMANAGER_INJECTED_FLAG;
|
2019-12-06 19:07:54 +08:00
|
|
|
input[2].type = INPUT_KEYBOARD;
|
2020-05-07 17:21:29 +08:00
|
|
|
input[2].ki.wVk = vk_code;
|
2019-12-06 19:07:54 +08:00
|
|
|
input[2].ki.dwFlags = KEYEVENTF_KEYUP;
|
2020-04-22 01:25:14 +08:00
|
|
|
input[2].ki.dwExtraInfo = CommonSharedConstants::KEYBOARDMANAGER_INJECTED_FLAG;
|
2019-12-06 19:07:54 +08:00
|
|
|
SendInput(3, input, sizeof(INPUT));
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
return suppress_win_release;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:57:19 +08:00
|
|
|
void TargetState::was_hidden()
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
2020-07-01 17:37:50 +08:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(mutex);
|
|
|
|
// Ignore callbacks from the D2DOverlayWindow
|
|
|
|
if (state == ForceShown)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 19:07:54 +08:00
|
|
|
state = Hidden;
|
|
|
|
events.clear();
|
|
|
|
lock.unlock();
|
|
|
|
cv.notify_one();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
void TargetState::exit()
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
events.clear();
|
|
|
|
state = Exiting;
|
|
|
|
lock.unlock();
|
|
|
|
cv.notify_one();
|
|
|
|
thread.join();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
KeyEvent TargetState::next()
|
|
|
|
{
|
|
|
|
auto e = events.front();
|
|
|
|
events.pop_front();
|
|
|
|
return e;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
void TargetState::handle_hidden()
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
if (events.empty())
|
|
|
|
cv.wait(lock);
|
|
|
|
if (events.empty() || state == Exiting)
|
|
|
|
return;
|
|
|
|
auto event = next();
|
|
|
|
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
|
|
|
|
{
|
|
|
|
state = Timeout;
|
|
|
|
winkey_timestamp = std::chrono::system_clock::now();
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 17:37:50 +08:00
|
|
|
void TargetState::handle_shown(const bool forced)
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
if (events.empty())
|
|
|
|
{
|
|
|
|
cv.wait(lock);
|
|
|
|
}
|
|
|
|
if (events.empty() || state == Exiting)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto event = next();
|
2020-07-01 17:37:50 +08:00
|
|
|
if (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN)
|
2019-12-06 19:07:54 +08:00
|
|
|
{
|
2020-07-01 17:37:50 +08:00
|
|
|
if (!forced && (!event.key_down || !winkey_held()))
|
|
|
|
{
|
|
|
|
state = Hidden;
|
|
|
|
}
|
2019-12-06 19:07:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
if (event.key_down)
|
|
|
|
{
|
2020-07-01 17:37:50 +08:00
|
|
|
nonwin_key_was_pressed_during_shown = true;
|
2019-12-06 19:07:54 +08:00
|
|
|
lock.unlock();
|
|
|
|
instance->on_held_press(event.vk_code);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
void TargetState::thread_proc()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case Hidden:
|
|
|
|
handle_hidden();
|
|
|
|
break;
|
|
|
|
case Timeout:
|
|
|
|
handle_timeout();
|
|
|
|
break;
|
|
|
|
case Shown:
|
2020-07-01 17:37:50 +08:00
|
|
|
handle_shown(false);
|
|
|
|
break;
|
|
|
|
case ForceShown:
|
|
|
|
handle_shown(true);
|
2019-12-06 19:07:54 +08:00
|
|
|
break;
|
|
|
|
case Exiting:
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
void TargetState::handle_timeout()
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
auto wait_time = delay - (std::chrono::system_clock::now() - winkey_timestamp);
|
|
|
|
if (events.empty())
|
2020-07-01 17:37:50 +08:00
|
|
|
{
|
2019-12-06 19:07:54 +08:00
|
|
|
cv.wait_for(lock, wait_time);
|
2020-07-01 17:37:50 +08:00
|
|
|
}
|
2019-12-06 19:07:54 +08:00
|
|
|
if (state == Exiting)
|
2020-07-01 17:37:50 +08:00
|
|
|
{
|
2019-12-06 19:07:54 +08:00
|
|
|
return;
|
2020-07-01 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip all VK_*WIN-down events
|
2019-12-06 19:07:54 +08:00
|
|
|
while (!events.empty())
|
|
|
|
{
|
|
|
|
auto event = events.front();
|
|
|
|
if (event.key_down && (event.vk_code == VK_LWIN || event.vk_code == VK_RWIN))
|
|
|
|
events.pop_front();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
// If we've detected that a user is holding anything other than VK_*WIN or start menu is visible, we should hide
|
2019-12-06 19:07:54 +08:00
|
|
|
if (!events.empty() || !only_winkey_key_held() || is_start_visible())
|
|
|
|
{
|
|
|
|
state = Hidden;
|
|
|
|
return;
|
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
if (std::chrono::system_clock::now() - winkey_timestamp < delay)
|
2020-07-01 17:37:50 +08:00
|
|
|
{
|
2019-12-06 19:07:54 +08:00
|
|
|
return;
|
2020-07-01 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:57:19 +08:00
|
|
|
signal_timestamp = std::chrono::system_clock::now();
|
2020-07-01 17:37:50 +08:00
|
|
|
nonwin_key_was_pressed_during_shown = false;
|
2019-12-06 19:07:54 +08:00
|
|
|
state = Shown;
|
|
|
|
lock.unlock();
|
|
|
|
instance->on_held();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:07:54 +08:00
|
|
|
void TargetState::set_delay(int ms_delay)
|
|
|
|
{
|
2020-07-01 17:37:50 +08:00
|
|
|
std::unique_lock lock(mutex);
|
2019-12-06 19:07:54 +08:00
|
|
|
delay = std::chrono::milliseconds(ms_delay);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-07-01 17:37:50 +08:00
|
|
|
|
|
|
|
void TargetState::toggle_force_shown()
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
events.clear();
|
|
|
|
if (state != ForceShown)
|
|
|
|
{
|
|
|
|
state = ForceShown;
|
|
|
|
instance->on_held();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = Hidden;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TargetState::active() const
|
|
|
|
{
|
|
|
|
return state == ForceShown || state == Shown;
|
|
|
|
}
|