Merge lp:~3v1n0/unity/key-grabber-ref-counting into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
Approved revision: no longer in the source branch.
Merged at revision: 4059
Proposed branch: lp:~3v1n0/unity/key-grabber-ref-counting
Merge into: lp:unity
Diff against target: 109 lines (+18/-22)
2 files modified
unity-shared/GnomeKeyGrabber.cpp (+17/-21)
unity-shared/GnomeKeyGrabberImpl.h (+1/-1)
To merge this branch: bzr merge lp:~3v1n0/unity/key-grabber-ref-counting
Reviewer Review Type Date Requested Status
Andrea Azzarone (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+280343@code.launchpad.net

Commit message

GnomeKeyGrabber: refcount the actions and remove them only when nobody needs

There might be multiple customers for an action (i.e. a menu entry and a dbus customer)
so we can't remove them in this case without ref-counting.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Andrea Azzarone (azzar1) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'unity-shared/GnomeKeyGrabber.cpp'
--- unity-shared/GnomeKeyGrabber.cpp 2015-10-27 17:35:44 +0000
+++ unity-shared/GnomeKeyGrabber.cpp 2015-12-13 10:39:16 +0000
@@ -98,7 +98,9 @@
98 auto it = std::find(actions_.begin(), actions_.end(), action);98 auto it = std::find(actions_.begin(), actions_.end(), action);
99 if (it != actions_.end())99 if (it != actions_.end())
100 {100 {
101 action_id = actions_ids_[it - actions_.begin()];101 auto action_index = it - actions_.begin();
102 action_id = actions_ids_[action_index];
103 ++actions_customers_[action_index];
102 LOG_DEBUG(logger) << "Key binding \"" << action.keyToString() << "\" is already grabbed, reusing id " << action_id;104 LOG_DEBUG(logger) << "Key binding \"" << action.keyToString() << "\" is already grabbed, reusing id " << action_id;
103 return true;105 return true;
104 }106 }
@@ -107,6 +109,7 @@
107 {109 {
108 actions_ids_.push_back(action_id);110 actions_ids_.push_back(action_id);
109 actions_.push_back(action);111 actions_.push_back(action);
112 actions_customers_.push_back(1);
110 return true;113 return true;
111 }114 }
112115
@@ -148,12 +151,23 @@
148 if (!index || index >= actions_.size())151 if (!index || index >= actions_.size())
149 return false;152 return false;
150153
154 if (actions_customers_[index] > 1)
155 {
156 LOG_DEBUG(logger) << "Not removing action " << actions_[index].keyToString()
157 << " as it is used by multiple customers ("
158 << actions_customers_[index] << ")";
159
160 --actions_customers_[index];
161 return false;
162 }
163
151 CompAction* action = &(actions_[index]);164 CompAction* action = &(actions_[index]);
152 LOG_DEBUG(logger) << "RemoveAction (\"" << action->keyToString() << "\")";165 LOG_DEBUG(logger) << "RemoveAction (\"" << action->keyToString() << "\")";
153166
154 screen_->removeAction(action);167 screen_->removeAction(action);
155 actions_.erase(actions_.begin() + index);168 actions_.erase(actions_.begin() + index);
156 actions_ids_.erase(actions_ids_.begin() + index);169 actions_ids_.erase(actions_ids_.begin() + index);
170 actions_customers_.erase(actions_customers_.begin() + index);
157171
158 return true;172 return true;
159}173}
@@ -258,7 +272,7 @@
258 if (it != actions_by_owner_.end())272 if (it != actions_by_owner_.end())
259 {273 {
260 for (auto action_id : it->second.actions)274 for (auto action_id : it->second.actions)
261 RemoveActionForOwner(action_id, name);275 RemoveActionByID(action_id);
262276
263 actions_by_owner_.erase(it);277 actions_by_owner_.erase(it);
264 }278 }
@@ -284,7 +298,7 @@
284 if (actions.empty())298 if (actions.empty())
285 actions_by_owner_.erase(it);299 actions_by_owner_.erase(it);
286300
287 return RemoveActionForOwner(action_id, owner);301 return RemoveActionByID(action_id);
288 }302 }
289303
290 LOG_WARN(logger) << "Action " << action_id << " was not registered by " << owner << ". "304 LOG_WARN(logger) << "Action " << action_id << " was not registered by " << owner << ". "
@@ -292,24 +306,6 @@
292 return false;306 return false;
293}307}
294308
295bool GnomeGrabber::Impl::RemoveActionForOwner(uint32_t action_id, std::string const& owner)
296{
297 for (auto it = actions_by_owner_.begin(); it != actions_by_owner_.end(); ++it)
298 {
299 if (it->first == owner)
300 continue;
301
302 auto const& actions = it->second.actions;
303 if (actions.find(action_id) != actions.end())
304 {
305 LOG_DEBUG(logger) << "Action " << action_id << " registered for multiple destinations, not removed";
306 return false;
307 }
308 }
309
310 return RemoveActionByID(action_id);
311}
312
313void GnomeGrabber::Impl::ActivateDBusAction(CompAction const& action, uint32_t action_id, uint32_t device, uint32_t timestamp) const309void GnomeGrabber::Impl::ActivateDBusAction(CompAction const& action, uint32_t action_id, uint32_t device, uint32_t timestamp) const
314{310{
315 LOG_DEBUG(logger) << "ActivateAction (" << action_id << " \"" << action.keyToString() << "\")";311 LOG_DEBUG(logger) << "ActivateAction (" << action_id << " \"" << action.keyToString() << "\")";
316312
=== modified file 'unity-shared/GnomeKeyGrabberImpl.h'
--- unity-shared/GnomeKeyGrabberImpl.h 2015-10-01 03:52:09 +0000
+++ unity-shared/GnomeKeyGrabberImpl.h 2015-12-13 10:39:16 +0000
@@ -52,7 +52,6 @@
52 uint32_t GrabDBusAccelerator(std::string const& owner, std::string const& accelerator, uint32_t flags);52 uint32_t GrabDBusAccelerator(std::string const& owner, std::string const& accelerator, uint32_t flags);
53 bool UnGrabDBusAccelerator(std::string const& sender, uint32_t action_id);53 bool UnGrabDBusAccelerator(std::string const& sender, uint32_t action_id);
54 void ActivateDBusAction(CompAction const& action, uint32_t id, uint32_t device, uint32_t timestamp) const;54 void ActivateDBusAction(CompAction const& action, uint32_t id, uint32_t device, uint32_t timestamp) const;
55 bool RemoveActionForOwner(uint32_t action_id, std::string const& owner);
5655
57 bool IsActionPostponed(CompAction const& action) const;56 bool IsActionPostponed(CompAction const& action) const;
5857
@@ -63,6 +62,7 @@
6362
64 uint32_t current_action_id_;63 uint32_t current_action_id_;
65 std::vector<uint32_t> actions_ids_;64 std::vector<uint32_t> actions_ids_;
65 std::vector<uint32_t> actions_customers_;
66 CompAction::Vector actions_;66 CompAction::Vector actions_;
6767
68 struct OwnerActions { glib::DBusNameWatcher::Ptr watcher; std::unordered_set<uint32_t> actions; };68 struct OwnerActions { glib::DBusNameWatcher::Ptr watcher; std::unordered_set<uint32_t> actions; };