mirror of https://github.com/odrling/Aegisub
Fix hotkey memory leaks
Originally committed to SVN as r5472.
This commit is contained in:
parent
1e2abbd45a
commit
a8a30d6ac1
|
@ -46,30 +46,32 @@ namespace agi {
|
||||||
|
|
||||||
Hotkey *hotkey;
|
Hotkey *hotkey;
|
||||||
|
|
||||||
std::string Combo::Str() {
|
std::string Combo::Str() const {
|
||||||
std::string str(key_map[0]);
|
std::string str(key_map[0]);
|
||||||
|
str.reserve(str.size() + (key_map.size() - 1) * 2);
|
||||||
for (unsigned int i=1; i < key_map.size(); i++) {
|
for (unsigned int i=1; i < key_map.size(); i++) {
|
||||||
str.append("-" + key_map[i]);
|
str.append("-" + key_map[i]);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Combo::StrMenu() {
|
std::string Combo::StrMenu() const {
|
||||||
return Str();
|
return Str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hotkey::ComboInsert(Combo *combo) {
|
void Hotkey::ComboInsert(Combo const& combo) {
|
||||||
str_map.insert(make_pair(combo->Str(), combo));
|
str_map.insert(std::make_pair(combo.Str(), combo));
|
||||||
cmd_map.insert(make_pair(combo->CmdName(), combo));
|
cmd_map.insert(std::make_pair(combo.CmdName(), combo));
|
||||||
}
|
}
|
||||||
|
|
||||||
Hotkey::~Hotkey() {
|
Hotkey::~Hotkey() {
|
||||||
Flush();
|
Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
Hotkey::Hotkey(const std::string &file, const std::string &default_config):
|
Hotkey::Hotkey(const std::string &file, const std::string &default_config)
|
||||||
config_file(file), config_default(default_config) {
|
: config_file(file)
|
||||||
|
, config_default(default_config)
|
||||||
|
{
|
||||||
LOG_D("hotkey/init") << "Generating hotkeys.";
|
LOG_D("hotkey/init") << "Generating hotkeys.";
|
||||||
|
|
||||||
std::istream *stream;
|
std::istream *stream;
|
||||||
|
@ -101,13 +103,13 @@ Hotkey::Hotkey(const std::string &file, const std::string &default_config):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Hotkey::BuildHotkey(std::string context, const json::Object& object) {
|
void Hotkey::BuildHotkey(std::string const& context, const json::Object& object) {
|
||||||
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) {
|
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) {
|
||||||
const json::Object::Member& member = *index;
|
const json::Object::Member& member = *index;
|
||||||
const json::Array& array = member.element;
|
const json::Array& array = member.element;
|
||||||
|
|
||||||
for (json::Array::const_iterator arr_index(array.Begin()); arr_index != array.End(); arr_index++) {
|
for (json::Array::const_iterator arr_index(array.Begin()); arr_index != array.End(); arr_index++) {
|
||||||
Combo *combo = new Combo(context, member.name);
|
Combo combo(context, member.name);
|
||||||
|
|
||||||
const json::Object& obj = *arr_index;
|
const json::Object& obj = *arr_index;
|
||||||
|
|
||||||
|
@ -116,12 +118,12 @@ void Hotkey::BuildHotkey(std::string context, const json::Object& object) {
|
||||||
if (arr_mod.Size() > 0) {
|
if (arr_mod.Size() > 0) {
|
||||||
for (json::Array::const_iterator arr_mod_index(arr_mod.Begin()); arr_mod_index != arr_mod.End(); arr_mod_index++) {
|
for (json::Array::const_iterator arr_mod_index(arr_mod.Begin()); arr_mod_index != arr_mod.End(); arr_mod_index++) {
|
||||||
const json::String& key_mod = *arr_mod_index;
|
const json::String& key_mod = *arr_mod_index;
|
||||||
combo->KeyInsert(key_mod.Value());
|
combo.KeyInsert(key_mod.Value());
|
||||||
} // for arr_mod_index
|
} // for arr_mod_index
|
||||||
|
|
||||||
}
|
}
|
||||||
combo->KeyInsert(static_cast<const json::String&>(obj["key"]).Value());
|
combo.KeyInsert(static_cast<const json::String&>(obj["key"]).Value());
|
||||||
combo->Enable(static_cast<const json::Boolean&>(obj["enable"]).Value());
|
combo.Enable(static_cast<const json::Boolean&>(obj["enable"]).Value());
|
||||||
|
|
||||||
ComboInsert(combo);
|
ComboInsert(combo);
|
||||||
} // for arr_index
|
} // for arr_index
|
||||||
|
@ -133,16 +135,18 @@ bool Hotkey::Scan(const std::string &context, const std::string &str, std::strin
|
||||||
|
|
||||||
HotkeyMap::const_iterator index, end;
|
HotkeyMap::const_iterator index, end;
|
||||||
for (std::tr1::tie(index, end) = str_map.equal_range(str); index != end; ++index) {
|
for (std::tr1::tie(index, end) = str_map.equal_range(str); index != end; ++index) {
|
||||||
std::string ctext = index->second->Context();
|
std::string const& ctext = index->second.Context();
|
||||||
|
|
||||||
if (ctext == "Always") {
|
if (ctext == "Always") {
|
||||||
cmd = index->second->CmdName();
|
cmd = index->second.CmdName();
|
||||||
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Always Command: " << cmd;
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Always Command: " << cmd;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (ctext == "Default") {
|
}
|
||||||
dfault = index->second->CmdName();
|
if (ctext == "Default") {
|
||||||
} else if (ctext == context) {
|
dfault = index->second.CmdName();
|
||||||
local = index->second->CmdName();
|
}
|
||||||
|
else if (ctext == context) {
|
||||||
|
local = index->second.CmdName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +154,8 @@ bool Hotkey::Scan(const std::string &context, const std::string &str, std::strin
|
||||||
cmd = local;
|
cmd = local;
|
||||||
LOG_D("agi/hotkey/found") << "Found: " << str << " Context: " << context << " Command: " << local;
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context: " << context << " Command: " << local;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (!dfault.empty()) {
|
}
|
||||||
|
if (!dfault.empty()) {
|
||||||
cmd = dfault;
|
cmd = dfault;
|
||||||
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << dfault;
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << dfault;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -164,9 +169,9 @@ std::vector<std::string> Hotkey::GetHotkeys(const std::string &context, const st
|
||||||
|
|
||||||
HotkeyMap::const_iterator it, end;
|
HotkeyMap::const_iterator it, end;
|
||||||
for (std::tr1::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
for (std::tr1::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
||||||
std::string ctext = it->second->Context();
|
std::string ctext = it->second.Context();
|
||||||
if (ctext == "Always" || ctext == "Default" || ctext == context) {
|
if (ctext == "Always" || ctext == "Default" || ctext == context) {
|
||||||
ret.push_back(it->second->StrMenu());
|
ret.push_back(it->second.StrMenu());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,13 +182,10 @@ std::vector<std::string> Hotkey::GetHotkeys(const std::string &context, const st
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hotkey::Flush() {
|
void Hotkey::Flush() {
|
||||||
|
|
||||||
json::Object root;
|
json::Object root;
|
||||||
|
|
||||||
HotkeyMap::iterator index;
|
for (HotkeyMap::iterator index = str_map.begin(); index != str_map.end(); ++index) {
|
||||||
for (index = str_map.begin(); index != str_map.end(); ++index) {
|
Combo::ComboMap combo_map(index->second.Get());
|
||||||
|
|
||||||
Combo::ComboMap combo_map(index->second->Get());
|
|
||||||
|
|
||||||
json::Array modifiers;
|
json::Array modifiers;
|
||||||
for (int i = 0; i != combo_map.size()-1; i++) {
|
for (int i = 0; i != combo_map.size()-1; i++) {
|
||||||
|
@ -193,17 +195,16 @@ void Hotkey::Flush() {
|
||||||
json::Object hotkey;
|
json::Object hotkey;
|
||||||
hotkey["modifiers"] = modifiers;
|
hotkey["modifiers"] = modifiers;
|
||||||
hotkey["key"] = json::String(combo_map.back());
|
hotkey["key"] = json::String(combo_map.back());
|
||||||
hotkey["enable"] = json::Boolean(index->second->IsEnabled());
|
hotkey["enable"] = json::Boolean(index->second.IsEnabled());
|
||||||
|
|
||||||
json::Object& context_obj = root[index->second->Context()];
|
json::Object& context_obj = root[index->second.Context()];
|
||||||
json::Array& combo_array = context_obj[index->second->CmdName()];
|
json::Array& combo_array = context_obj[index->second.CmdName()];
|
||||||
|
|
||||||
combo_array.Insert(hotkey);
|
combo_array.Insert(hotkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
io::Save file(config_file);
|
io::Save file(config_file);
|
||||||
json::Writer::Write(root, file.Get());
|
json::Writer::Write(root, file.Get());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace toolbar
|
} // namespace toolbar
|
||||||
|
|
|
@ -48,27 +48,27 @@ public:
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// @param ctx Context
|
/// @param ctx Context
|
||||||
/// @param cmd Command name
|
/// @param cmd Command name
|
||||||
Combo(std::string ctx, std::string cmd): cmd_name(cmd), context(ctx) {}
|
Combo(std::string const& ctx, std::string const& cmd): cmd_name(cmd), context(ctx) {}
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Combo() {}
|
~Combo() {}
|
||||||
|
|
||||||
/// String representation of the Combo
|
/// String representation of the Combo
|
||||||
std::string Str();
|
std::string Str() const;
|
||||||
|
|
||||||
/// String suitable for usage in a menu.
|
/// String suitable for usage in a menu.
|
||||||
std::string StrMenu();
|
std::string StrMenu() const;
|
||||||
|
|
||||||
/// Get the literal combo map.
|
/// Get the literal combo map.
|
||||||
/// @return ComboMap (std::vector) of linear key sequence.
|
/// @return ComboMap (std::vector) of linear key sequence.
|
||||||
const ComboMap& Get() { return key_map; }
|
const ComboMap& Get() const { return key_map; }
|
||||||
|
|
||||||
/// Command name triggered by the combination.
|
/// Command name triggered by the combination.
|
||||||
/// @return Command name
|
/// @return Command name
|
||||||
const std::string& CmdName() { return cmd_name; }
|
const std::string& CmdName() const { return cmd_name; }
|
||||||
|
|
||||||
/// Context this Combo is triggered in.
|
/// Context this Combo is triggered in.
|
||||||
const std::string& Context() { return context; }
|
const std::string& Context() const { return context; }
|
||||||
|
|
||||||
/// Enable or disable Combo or "Hotkey".
|
/// Enable or disable Combo or "Hotkey".
|
||||||
/// @param e Bool state.
|
/// @param e Bool state.
|
||||||
|
@ -76,7 +76,7 @@ public:
|
||||||
|
|
||||||
/// Check whether Combo is currently enabled or disabled.
|
/// Check whether Combo is currently enabled or disabled.
|
||||||
/// @return State.
|
/// @return State.
|
||||||
const bool& IsEnabled() { return enable; }
|
bool IsEnabled() const { return enable; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ComboMap key_map; ///< Map.
|
ComboMap key_map; ///< Map.
|
||||||
|
@ -115,7 +115,7 @@ public:
|
||||||
std::vector<std::string> GetHotkeys(const std::string &context, const std::string &command) const;
|
std::vector<std::string> GetHotkeys(const std::string &context, const std::string &command) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::multimap<std::string, Combo*> HotkeyMap; ///< Map to hold Combo instances.
|
typedef std::multimap<std::string, Combo> HotkeyMap; ///< Map to hold Combo instances.
|
||||||
HotkeyMap str_map; ///< String representation -> Combo
|
HotkeyMap str_map; ///< String representation -> Combo
|
||||||
HotkeyMap cmd_map; ///< Command name -> Combo
|
HotkeyMap cmd_map; ///< Command name -> Combo
|
||||||
const std::string config_file; ///< Default user config location.
|
const std::string config_file; ///< Default user config location.
|
||||||
|
@ -124,11 +124,11 @@ private:
|
||||||
/// Build hotkey map.
|
/// Build hotkey map.
|
||||||
/// @param context Context being parsed.
|
/// @param context Context being parsed.
|
||||||
/// @param object json::Object holding items for context being parsed.
|
/// @param object json::Object holding items for context being parsed.
|
||||||
void BuildHotkey(std::string context, const json::Object& object);
|
void BuildHotkey(std::string const& context, const json::Object& object);
|
||||||
|
|
||||||
/// Insert Combo into HotkeyMap instance.
|
/// Insert Combo into HotkeyMap instance.
|
||||||
/// @param combo Combo to insert.
|
/// @param combo Combo to insert.
|
||||||
void ComboInsert(Combo *combo);
|
void ComboInsert(Combo const& combo);
|
||||||
|
|
||||||
/// Write active Hotkey configuration to disk.
|
/// Write active Hotkey configuration to disk.
|
||||||
void Flush();
|
void Flush();
|
||||||
|
|
Loading…
Reference in New Issue