Skip to content

Commit

Permalink
Added /p command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
lcferrum committed Nov 30, 2017
1 parent 0b4053e commit 2d28aae
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- 'Restart' option to 'About' dialog
- CustomOnHotkey and CustomOnHotkeyLongPress options to config
- /s and /l command line switches (fire short or long press events immediately)
- /p command line switch (launch SnK shell immediately)
* Removed:
* Required SnK version: 2.4+

Expand Down
31 changes: 21 additions & 10 deletions Suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#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};
enum class FstCmd:char {DEFAULT, LONG_PRESS, SHORT_PRESS, SHELL};

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

Expand Down Expand Up @@ -58,6 +58,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
// NO ARGUMENTS GIVEN - launch application and automatically select ini file location
// /s ... - immidiately run sungle press event and exit (used with /i and /a options or w/o arguments)
// /l ... - immidiately run long press event and exit (used with /i and /a options or w/o arguments)
// /p ... - launch SnK shell and exit (used with /i and /a options or w/o arguments)

CmdRes cmd_res=CmdRes::DEFAULT;
FstCmd fst_cmd=FstCmd::DEFAULT;
Expand All @@ -79,6 +80,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
fst_cmd=FstCmd::SHORT_PRESS;
else if (!wcscmp(cmd_argv[0], L"/l"))
fst_cmd=FstCmd::LONG_PRESS;
else if (!wcscmp(cmd_argv[0], L"/p"))
fst_cmd=FstCmd::SHELL;

if (fst_cmd!=FstCmd::DEFAULT) {
if (cmd_argc>1) cmd_res=ProcessSettingsOptions(Settings, cmd_argc, cmd_argv, 1);
Expand Down Expand Up @@ -227,15 +230,23 @@ 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(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;
}

switch (fst_cmd) {
case FstCmd::SHELL:
return SuiteExtRel::LaunchCommandPrompt(Settings.get(), true);
case FstCmd::LONG_PRESS:
case FstCmd::SHORT_PRESS:
return SuiteExtRel::FireEvent(fst_cmd==FstCmd::LONG_PRESS, Settings.get());
default:
{
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;
}
}
}

Expand Down
23 changes: 18 additions & 5 deletions SuiteExternalRelations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,26 @@ void SuiteExtRel::RestartApplication(const wchar_t* cmdline, bool elevate)
}
}

void SuiteExtRel::LaunchCommandPrompt(const wchar_t* dir)
int SuiteExtRel::LaunchCommandPrompt(const SuiteSettings *settings, bool check_snk)
{
std::wstring snk_path=settings->GetSnkPath();

if (check_snk) {
DWORD dwAttrib=GetFileAttributes(snk_path.c_str());
if (dwAttrib==INVALID_FILE_ATTRIBUTES||(dwAttrib&FILE_ATTRIBUTE_DIRECTORY)) {
ErrorMessage(L"Path to SnK is not valid!");
return ERR_SUITEEXTREL+9;
}
}

if (DWORD env_len=GetEnvironmentVariable(L"ComSpec", NULL, 0)) {
std::wstring dir=QuoteArgument(GetDirPath(GetFullPathNameWrapper(snk_path)).c_str());
wchar_t env_buf[env_len];
if (GetEnvironmentVariable(L"ComSpec", env_buf, env_len)) {
std::wstring cd_cmd=L"/s /k title SnK Shell && set Path=";
cd_cmd.append(QuoteArgument(dir));
cd_cmd.append(dir);
cd_cmd.append(L";%Path% && pushd ");
cd_cmd.append(QuoteArgument(dir));
cd_cmd.append(dir);
SHELLEXECUTEINFO sei={sizeof(sei)};
sei.lpVerb=L"open";
sei.lpFile=env_buf;
Expand All @@ -680,9 +691,11 @@ void SuiteExtRel::LaunchCommandPrompt(const wchar_t* dir)
ShellExecuteEx(&sei);
}
}

return 0;
}

int SuiteExtRel::FireEvent(bool long_press, SuiteSettings *settings)
int SuiteExtRel::FireEvent(bool long_press, const SuiteSettings *settings)
{
std::wstring snk_cmdline;
if (long_press?settings->IsCustomLhk():settings->IsCustomShk()) {
Expand Down Expand Up @@ -713,5 +726,5 @@ int SuiteExtRel::FireEvent(bool long_press, SuiteSettings *settings)
}

ErrorMessage(L"Failed to launch SnK!");
return ERR_SUITEEXTREL+10;
return ERR_SUITEEXTREL+10;
}
4 changes: 2 additions & 2 deletions SuiteExternalRelations.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace SuiteExtRel {
int RemoveFromAutorun(bool current_user);
bool LaunchSnkOpenDialog(std::wstring &fpath);
void RestartApplication(const wchar_t* cmdline, bool elevate);
void LaunchCommandPrompt(const wchar_t* dir);
int FireEvent(bool long_press, SuiteSettings *settings);
int LaunchCommandPrompt(const SuiteSettings *settings, bool check_snk=false);
int FireEvent(bool long_press, const SuiteSettings *settings);
}

#endif //SUITEEXTERNALRELATIONS_H
2 changes: 1 addition & 1 deletion SuiteMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ bool IconMenuProc(HotkeyEngine* &hk_engine, SuiteSettings *settings, KeyTriplet
return true;
#endif
case IDM_CMDPRMPT:
SuiteExtRel::LaunchCommandPrompt(GetDirPath(GetFullPathNameWrapper(settings->GetSnkPath())).c_str());
SuiteExtRel::LaunchCommandPrompt(settings);
return true;
case IDM_ABOUT:
{
Expand Down

0 comments on commit 2d28aae

Please sign in to comment.