Skip to content

Commit

Permalink
/s and /i command line options, code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
lcferrum committed Nov 25, 2017
1 parent 08c67ad commit ff605ca
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 147 deletions.
10 changes: 5 additions & 5 deletions HotkeyEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ HotkeyEngine::KeyPressFn HotkeyEngine::OnKeyPress=NULL;
// 1 - indicates that thread exited due to error
// 2 - indicates that thread was forcefully terminated because it failed to respond in specified time

HotkeyEngine* HotkeyEngine::MakeInstance(HINSTANCE hInstance)
HotkeyEngine* HotkeyEngine::MakeInstance()
{
instance.reset(new HotkeyEngine(hInstance));
instance.reset(new HotkeyEngine());
return instance.get();
}

HotkeyEngine::HotkeyEngine(HINSTANCE hInstance):
running(false), hook_thread_handle(NULL), hook_thread_id(0), app_instance(hInstance), stack_commit(0)
HotkeyEngine::HotkeyEngine():
running(false), hook_thread_handle(NULL), hook_thread_id(0), stack_commit(0)
{}

HotkeyEngine::~HotkeyEngine()
Expand Down Expand Up @@ -103,7 +103,7 @@ DWORD WINAPI HotkeyEngine::ThreadProc(LPVOID lpParameter)
{
HHOOK kb_hook;

if ((kb_hook=SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, instance->app_instance, 0))) {
if ((kb_hook=SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0))) {
SetEvent((HANDLE)lpParameter); //Signal success event and continue
} else {
return 1; //Exit (exit code = 1)
Expand Down
5 changes: 2 additions & 3 deletions HotkeyEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ class HotkeyEngine {
bool running;
HANDLE hook_thread_handle;
DWORD hook_thread_id;
HINSTANCE app_instance;
size_t stack_commit;

HotkeyEngine(HINSTANCE hInstance);
HotkeyEngine();
public:
static HotkeyEngine* MakeInstance(HINSTANCE hInstance);
static HotkeyEngine* MakeInstance();
bool IsRunning();
bool Stop();
bool Start();
Expand Down
287 changes: 157 additions & 130 deletions Suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#endif

enum class CmdRes:char {DEFAULT, SETTINGS_SET, EXTERNAL_CALLED, ERR_MANY_ARGS, ERR_FEW_ARGS, ERR_UNKNOWN, ERR_NOT_IMPLEMENTED};
enum class FstCmd:char {DEFAULT, LONG_PRESS, SHORT_PRESS};

CmdRes ProcessSettingsOptions(std::unique_ptr<SuiteSettings> &Settings, int cmd_argc, wchar_t** cmd_argv, int cmd_shift);

#define ARG_ALL L"machine"
#define ARG_CUR L"user"

#ifdef OBSOLETE_WWINMAIN
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR, int nCmdShow)
Expand Down Expand Up @@ -54,9 +60,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
// /l ... - immidiately run long press event and exit (used with /i and /a options or w/o arguments)

CmdRes cmd_res=CmdRes::DEFAULT;
FstCmd fst_cmd=FstCmd::DEFAULT;
int ext_res;
const wchar_t ARG_ALL[]=L"machine";
const wchar_t ARG_CUR[]=L"user";
if (wcslen(lpCmdLine)) {
wchar_t** cmd_argv;
int cmd_argc;
Expand All @@ -70,146 +75,113 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
std::wcerr<<L"ARG["<<tst_argc<<L"] = \""<<cmd_argv[tst_argc]<<L"\""<<std::endl;
#endif
if (cmd_argc) {
if (!wcscmp(cmd_argv[0], L"/S")) {
if (cmd_argc>4) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Schedule(true, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Schedule(false, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
if (!wcscmp(cmd_argv[0], L"/s"))
fst_cmd=FstCmd::SHORT_PRESS;
else if (!wcscmp(cmd_argv[0], L"/l"))
fst_cmd=FstCmd::LONG_PRESS;

if (fst_cmd!=FstCmd::DEFAULT) {
if (cmd_argc>1) cmd_res=ProcessSettingsOptions(Settings, cmd_argc, cmd_argv, 1);
} else {
if (!wcscmp(cmd_argv[0], L"/S")) {
if (cmd_argc>4) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Schedule(true, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Schedule(false, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/A")) {
if (cmd_argc>4) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToAutorun(true, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToAutorun(false, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcscmp(cmd_argv[0], L"/A")) {
if (cmd_argc>4) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToAutorun(true, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToAutorun(false, cmd_argv+2, cmd_argc-2);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/U")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Unschedule(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Unschedule(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcscmp(cmd_argv[0], L"/U")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Unschedule(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::Unschedule(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/R")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromAutorun(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromAutorun(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcscmp(cmd_argv[0], L"/R")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromAutorun(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromAutorun(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/P")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToPath(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToPath(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcscmp(cmd_argv[0], L"/P")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToPath(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::AddToPath(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/C")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromPath(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromPath(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else if (!wcscmp(cmd_argv[0], L"/i")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
Settings.reset(new SuiteSettingsIni(cmd_argv[1]));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_INI (PATH): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else {
Settings.reset(new SuiteSettingsIni());
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_INI (AUTO): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
}
} else if (!wcscmp(cmd_argv[0], L"/a")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
Settings.reset(new SuiteSettingsAppData(true));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (CURRENT): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
Settings.reset(new SuiteSettingsAppData(false));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (ALL): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else if (!wcscmp(cmd_argv[0], L"/C")) {
if (cmd_argc>2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>1) {
if (!wcsncmp(cmd_argv[1], ARG_CUR, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromPath(true);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else if (!wcsncmp(cmd_argv[1], ARG_ALL, wcslen(cmd_argv[1]))) {
ext_res=SuiteExtRel::RemoveFromPath(false);
cmd_res=CmdRes::EXTERNAL_CALLED;
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
cmd_res=CmdRes::ERR_FEW_ARGS;
}
} else {
Settings.reset(new SuiteSettingsAppData());
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (AUTO): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
cmd_res=ProcessSettingsOptions(Settings, cmd_argc, cmd_argv, 0);
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
}

Expand Down Expand Up @@ -255,12 +227,67 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
if (!Settings->SaveSettings()) {
ErrorMessage(L"Failed to load settings!");
return ERR_SUITE+1;
} else if (fst_cmd!=FstCmd::DEFAULT) {
return SuiteExtRel::FireEvent(fst_cmd==FstCmd::LONG_PRESS, Settings.get());
} else {
int suite_main_err=SuiteMain(hInstance, Settings.get()); //Generates it's own error messages and sets (returns) exit code accordingly
int suite_main_err=SuiteMain(Settings.get()); //Generates it's own error messages and sets (returns) exit code accordingly
if (suite_main_err==ERR_ELEVATE)
SuiteExtRel::RestartApplication(lpCmdLine, true);
else if (suite_main_err==ERR_RESTART)
SuiteExtRel::RestartApplication(lpCmdLine, false);
return suite_main_err;
}
}
}

CmdRes ProcessSettingsOptions(std::unique_ptr<SuiteSettings> &Settings, int cmd_argc, wchar_t** cmd_argv, int cmd_shift)
{
CmdRes cmd_res=CmdRes::DEFAULT;

if (!wcscmp(cmd_argv[cmd_shift], L"/i")) {
if (cmd_argc>cmd_shift+2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>cmd_shift+1) {
Settings.reset(new SuiteSettingsIni(cmd_argv[cmd_shift+1]));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_INI (PATH): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else {
Settings.reset(new SuiteSettingsIni());
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_INI (AUTO): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
}
} else if (!wcscmp(cmd_argv[cmd_shift], L"/a")) {
if (cmd_argc>cmd_shift+2) {
cmd_res=CmdRes::ERR_MANY_ARGS;
} else if (cmd_argc>cmd_shift+1) {
if (!wcsncmp(cmd_argv[cmd_shift+1], ARG_CUR, wcslen(cmd_argv[cmd_shift+1]))) {
Settings.reset(new SuiteSettingsAppData(true));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (CURRENT): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else if (!wcsncmp(cmd_argv[cmd_shift+1], ARG_ALL, wcslen(cmd_argv[cmd_shift+1]))) {
Settings.reset(new SuiteSettingsAppData(false));
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (ALL): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}
} else {
Settings.reset(new SuiteSettingsAppData());
cmd_res=CmdRes::SETTINGS_SET;
#ifdef DEBUG
std::wcerr<<L"SET SETTINGS_APPDATA (AUTO): INI_PATH="<<Settings->GetStoredLocation()<<std::endl;
#endif
}
} else {
cmd_res=CmdRes::ERR_UNKNOWN;
}

return cmd_res;
}
Loading

0 comments on commit ff605ca

Please sign in to comment.