Skip to content

Commit

Permalink
Fix few bugs related to global hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
tomek-o committed Feb 16, 2022
1 parent 6f2ba25 commit c5e96a1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
14 changes: 14 additions & 0 deletions tSIP/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ struct Action
id(0)
{
}

bool operator==(const Action& right) const {
if (type != right.type ||
id != right.id)
{
return false;
}
return true;
}

bool operator!=(const Action& right) const {
return !(*this == right);
}

static const char* getTypeDescription(enum Type type);

};
Expand Down
7 changes: 6 additions & 1 deletion tSIP/FormMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ void TfrmMain::UpdateSettings(const Settings &prev)
{
frmLog->UpdateUi();
}

if (prev.hotKeyConf != appSettings.hotKeyConf)
{
RegisterGlobalHotKeys();
Expand Down Expand Up @@ -748,6 +749,7 @@ void __fastcall TfrmMain::tmrStartupTimer(TObject *Sender)
Ua::Instance().Start();
SetStatus("Initializing...");

//LOG("Registering hotkeys...\n");
RegisterGlobalHotKeys();

FocusCbCallUri();
Expand Down Expand Up @@ -3039,6 +3041,9 @@ void __fastcall TfrmMain::WMCopyData(TWMCopyData& Message)
void __fastcall TfrmMain::WMHotKey(TWMHotKey &Message)
{
TForm::Dispatch(&Message);
#if 1
LOG("WMHotKey: Message.Hotkey = %d\n", static_cast<int>(Message.HotKey));
#endif
const HotKeyConf* cfg = hotKeys.FindGlobal(Message.HotKey);
if (IsWin7OrLater() == false)
{
Expand Down Expand Up @@ -3216,7 +3221,7 @@ void __fastcall TfrmMain::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
#if 0
LOG("Key = %d, CTRL = %d, SHIFT = %d, ALT = %d\n",
LOG("FormKeyDown: Key = %d, CTRL = %d, SHIFT = %d, ALT = %d\n",
Key,
Shift.Contains(ssCtrl), Shift.Contains(ssShift), Shift.Contains(ssAlt));
#endif
Expand Down
3 changes: 2 additions & 1 deletion tSIP/hotkeys/HotKeyConf.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ struct HotKeyConf
bool operator==(const HotKeyConf& right) const {
if (keyCode != right.keyCode ||
modifiers != right.modifiers ||
global != right.global)
global != right.global ||
action != right.action)
{
return false;
}
Expand Down
50 changes: 27 additions & 23 deletions tSIP/hotkeys/Hotkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ int HotKeys::RegisterGlobal(const std::list<HotKeyConf> &conf, HWND hwnd)
{
exist = true;
it->remove = false;
ghk.hotKeyConf.action = cfg.action;
break;
}
}
Expand Down Expand Up @@ -113,29 +114,18 @@ int HotKeys::RegisterGlobal(const std::list<HotKeyConf> &conf, HWND hwnd)
fsModifiers |= MOD_ALT;
}
int id = FindNextId();
if (id < MIN_ID)
{
LOG("Hotkeys: could not find nextId (id = %d)!\n", id);
break;
}
else
{
//LOG("Registering hotkey with id = %d\n", id);
}
if (RegisterHotKey(hwnd, id, fsModifiers, cfg.vkCode) == 0)
{
AnsiString desc;
if (cfg.modifiers & HotKeyConf::CTRL)
{
desc = "Ctrl";
}
if (cfg.modifiers & HotKeyConf::SHIFT)
{
if (desc != "")
desc += "+";
desc += "Shift";
}
if (cfg.modifiers & HotKeyConf::ALT)
{
if (desc != "")
desc += "+";
desc += "Alt";
}
if (desc != "")
desc += "+";
desc += cfg.keyCode;
LOG("Failed to register global hotkey %s\n", desc.c_str());
LOG("Failed to register global hotkey %s\n", cfg.GetDescription().c_str());
rc++;
}
else
Expand Down Expand Up @@ -183,28 +173,42 @@ void HotKeys::Unregister(HWND hwnd)

int HotKeys::FindNextId(void)
{
//LOG("Hotkeys: FindNextId, nextId = %d, globalHotKeys size = %u\n", nextId, globalHotKeys.size());
#if 1
for (int i=MIN_ID; i<=MAX_ID; i++)
#else
int TODO__RESTORE_DEBUG;
for (int i=MIN_ID; i<=MIN_ID + 20; i++)
#endif
{
std::list<GlobalHotKey>::iterator it;
for (it = globalHotKeys.begin(); it != globalHotKeys.end(); ++it)
{
if (it->id == nextId)
{
//LOG("Hotkeys: id %d is used\n", nextId);
break;
}
}
if (it == globalHotKeys.end())
{
//LOG("Hotkeys: unused id = %d found\n", nextId);
int rc = nextId;
nextId++;
if (nextId > MAX_ID)
{
nextId = 0;
nextId = MIN_ID;
}
return rc;
}
nextId++;
if (nextId > MAX_ID)
{
nextId = MIN_ID;
}
}
return MIN_ID-1;
//LOG("Hotkeys: unused id not found!\n");
return INVALID_HOTKEY_ID;
}


10 changes: 6 additions & 4 deletions tSIP/hotkeys/Hotkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
class HotKeys
{
public:
enum {
MIN_ID = 0x0000,
MAX_ID = 0xBFFF,
INVALID_HOTKEY_ID = MIN_ID - 1
};
HotKeys(void);
/** \brief Find key in configuration
*/
Expand All @@ -26,17 +31,14 @@ class HotKeys
bool registered;
bool remove;
GlobalHotKey(void):
id(INVALID_HOTKEY_ID),
registered(false),
remove(false)
{
}
};
std::list<GlobalHotKey> globalHotKeys;
int nextId;
enum {
MIN_ID = 0x0000,
MAX_ID = 0xBFFF
};
int FindNextId(void);
};

Expand Down
2 changes: 1 addition & 1 deletion tSIP/tSIP.bdsproj
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@
<FILE FILENAME="FormDialpadConf.cpp" CONTAINERID="CCompiler" LOCALCOMMAND="" UNITNAME="FormDialpadConf" FORMNAME="frmDialpadConf" DESIGNCLASS="" ADDITIONAL="FormDialpadConf.h"/>
<FILE FILENAME="scripting\FormLuaScriptHelp.cpp" CONTAINERID="CCompiler" LOCALCOMMAND="" UNITNAME="FormLuaScriptHelp" FORMNAME="frmLuaScriptHelp" DESIGNCLASS="" ADDITIONAL="scripting\FormLuaScriptHelp.h"/>
<FILE FILENAME="hotkeys\HotKeyConf.cpp" CONTAINERID="CCompiler" LOCALCOMMAND="" UNITNAME="HotKeyConf" FORMNAME="" DESIGNCLASS="" ADDITIONAL="hotkeys\HotKeyConf.h"/>
<FILE FILENAME="hotkeys\HotKeyConf.h" CONTAINERID="" LOCALCOMMAND=""/>
<FILE FILENAME="hotkeys\HotKeyConf.h" CONTAINERID="" LOCALCOMMAND="" UNITNAME="" FORMNAME="" DESIGNCLASS=""/>
</FILELIST>
<IDEOPTIONS>
<VersionInfo>
Expand Down

0 comments on commit c5e96a1

Please sign in to comment.